[BCEL] If then else construct - again

T

Thomas Zangl

Hi,

Quote:
I have some piece of code which I want to convert to Java using the
BCEL. Methodcalls etc work fine but now I am stuck with the "if then
else" structure.

Can someone please post an example for something simple like:

if y > x then
Foo()
else
Bar()
-- End of Quote

I have already posted this and now I am stuck with the following.
I use the InstructionFactory from BCEL to generate a "IFEQ" branch
insttruction:

aIh = some intruction handle. the target as far as I can figure out.
Must this be some single instruction or can I have multiple ones? (Like
in java if I have an "if" with braces containing multiple statements.)

aBranch = factory_.createBranchInstruction(Constants.IFEQ,aIh);

How can I generate the "else" statement? I thought of adding an
"inverse" branche (IFNE) with the code in the "else" branch but is there
something easier and more elegant?

Maybe somebody can post a small example how to generate this if then
else snippet to BCEL (Using InstructionList, InstructionFactory):

if ( dummyFunction(x) > 0 ) {
foo(x-1)
} else {
return 0;
}

TIA!

Best regards,
 
A

Arnaud Berger

Hi,

Sure :

if (y > x){
Foo();
}
else{
Bar();
}

(forget about the "then" keyword)

Regards,

Arnaud
 
T

Thomas Zangl

Arnaud Berger wrote:

Hi,
Sure :

if (y > x){
Foo();
}
else{
Bar();
}

(forget about the "then" keyword)

Are you sure you understood my question ... ? I asked about the BCEL
syntax for that piece of code. I found a nice utily (BCELifer) that
helps me a bit.

Best regards,
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Thomas said:
Can someone please post an example for something simple like:

if y > x then
Foo()
else
Bar()
-- End of Quote

I have already posted this and now I am stuck with the following.
I use the InstructionFactory from BCEL to generate a "IFEQ" branch
insttruction:

aIh = some intruction handle. the target as far as I can figure out.
Must this be some single instruction or can I have multiple ones? (Like
in java if I have an "if" with braces containing multiple statements.)

aBranch = factory_.createBranchInstruction(Constants.IFEQ,aIh);

How can I generate the "else" statement? I thought of adding an
"inverse" branche (IFNE) with the code in the "else" branch but is there
something easier and more elegant?

I already posted a reply to your original post, but you did not reply.
To turn an if/else statement into bytecode the following idiom is used:

java:

if (cond)
{
code;
}
else
{
other code;
}

code not in if/else;

bytecode:

if_invert_cond ELSE
...
code
...
goto ENDIF
ELSE: ...
other code
...
ENDIF: code not in if/else

where ELSE and ENDIF are labels. In BCEL you use instruction handles,
which function somewhat like labels, so in BCEL, ELSE == first
instruction handle in else block code, ENDIF == first instruction handle
in code outside the if/else statement.

Hope that helps.

PS. I'm not sure if this needs to be pointed out, but the if_xxx
instructions work like this: if condition is true, continue execution at
specified adress, else continue execution at next instruction.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Daniel said:
if_invert_cond ELSE
...
code
...
goto ENDIF
ELSE: ...
other code
...
ENDIF: code not in if/else

The indentation is a bit messed up. More like this (hope it works this
time):

if_invert_cond ELSE
...
code
...
goto ENDIF
ELSE:
...
other code
...
ENDIF:
code not in if/else
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top