[BCEL] If then else construct - again

Discussion in 'Java' started by Thomas Zangl, May 13, 2005.

  1. Thomas Zangl

    Thomas Zangl Guest

    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,
    --
    ----------------------------------------------------------------
    ,yours Thomas Zangl - -TZ1-6BONE-
    -http://tzi.dhs.org - http://www.borg-kindberg.ac.at
    Use YAMC! now! Get it at http://www.borg-kindberg.ac.at/yamc/
     
    Thomas Zangl, May 13, 2005
    #1
    1. Advertising

  2. Hi,

    Sure :

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

    (forget about the "then" keyword)

    Regards,

    Arnaud


    "Thomas Zangl" <> a écrit dans le message news:
    d61ih3$2qd$-gems.net...
    > 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,
    > --
    > ----------------------------------------------------------------
    > ,yours Thomas Zangl - -TZ1-6BONE-
    > -http://tzi.dhs.org - http://www.borg-kindberg.ac.at
    > Use YAMC! now! Get it at http://www.borg-kindberg.ac.at/yamc/
     
    Arnaud Berger, May 13, 2005
    #2
    1. Advertising

  3. Thomas Zangl

    Thomas Zangl Guest

    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,
    --
    ----------------------------------------------------------------
    ,yours Thomas Zangl - -TZ1-6BONE-
    -http://tzi.dhs.org - http://www.borg-kindberg.ac.at
    Use YAMC! now! Get it at http://www.borg-kindberg.ac.at/yamc/
     
    Thomas Zangl, May 13, 2005
    #3
  4. Thomas Zangl wrote:
    > 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.

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, May 13, 2005
    #4
  5. Daniel Sjöblom wrote:

    >
    > 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

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, May 13, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Zangl

    BCEL and IfThenElse construct

    Thomas Zangl, May 4, 2005, in forum: Java
    Replies:
    1
    Views:
    446
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 4, 2005
  2. Replies:
    1
    Views:
    800
    Thomas Hawtin
    Sep 7, 2005
  3. Mukesh
    Replies:
    4
    Views:
    625
    Paul N
    Mar 26, 2010
  4. Replies:
    0
    Views:
    295
  5. Replies:
    15
    Views:
    223
Loading...

Share This Page