Re: BCEL newbie question (solved!)

Andrew Huntwork <[email protected]> Sat, 5 Nov 2005 22:19:22 -0700
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
On 11/2/05, Jacob Kjome <[email protected]> wrote:
>
> [...]I really don't have the first clue about this stuff and am happy to
> have
> the code generated for me. But just for giggles, how about this...
>
> The instruction lists append stuff and return instruction
> handles. Great. But if this appending is akin to something like a
> StringBuffer, order matters. Is that the case with Instruction lists? If
> so, how do I deal with this?...



Order definitely matters. You can append a forward branch (like 4: ifnonnull
32 in your example) by simply inserting the branch instruction when you come
to it in the code segment you're generating, but initially give the branch a
null target. Then you can set the target of the branch instruction after
inserting the target instruction. For example, if you want to generate this
code:

0: aconst_null[1](1)
1: ifnonnull[199](3) -> nop
4: nop[0](1)
5: return[177](1)

you can use this code:
InstructionList il = new InstructionList();
il.append(new ACONST_NULL());
IFNONNULL inn = new IFNONNULL(null); //create the branch but don't set a
target yet
il.append(inn); // append the branch
InstructionHandle ih = il.append(new NOP()); //append the target
inn.setTarget(ih); //set the target of the branch after inserting the target
of the branch

...
> 4: ifnonnull 32
> ...
> 32: putstatic #47; //Field XMLC_GENERATED_CLASS:Ljava/lang/Class;
> ...
>
> So, If I'm going in order, how do I reference InstructionHandle 32 before
> I
> create it? for instance...
>
> ...
> il.append(InstructionFactory.createBranchInstruction(Constants.IFNONNULL,
> ih_32));
> ...
> InstructionHandle ih_32 = il.append(_factory.createFieldAccess(className,
> "XMLC_GENERATED_CLASS",
> new ObjectType("java.lang.Class"), Constants.PUTSTATIC));
> ...
>
> I must be missing some mind-bogglingly obvious here. Please point it out,
> if you could.
>
> thanks,
>
>
> Jake
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>