ASM (vs. BCEL) - can I do this?

Discussion in 'Java' started by Francesco Devittori, Dec 20, 2005.

  1. Hi all,

    I want to port an application from BCEL to ASM (which is smaller and
    seems to be faster)

    What I currently do with BCEL is:

    an existing class is read. For each method in the class:
    - l is the list of instructions in the method
    - an algorithm re-creates the control flow graph:
    some Node objects are created, each node contains:
    - an instructionHandler that points to the instruction in l that
    starts the node
    - a size, i.e. the number of instructions in the node.
    - then some instructions are inserted in each node (actually inserted
    in l)
    finally the class is dumped as an array of bytes (with the modified
    instruction list of each method).

    For example, imagine a fake example, with a method that has the
    following instructions:

    1: ldc
    2: ifeq
    3: iadd
    4: ifnull
    5: iinc
    6: ldc
    7: goto

    Then these nodes are created:

    Node 1: { start = 1; size = 2 }
    Node 2: { start = 3; size = 1 }
    Node 3: { start = 4; size = 4 }


    What I whish to know from someone that already used ASM is: is it
    possible to do something like that?

    I played a bit with ASM and I was not able to do anything in the right
    direction (but maybe it's because I'm used to BCEL).

    TIA

    Francesco
     
    Francesco Devittori, Dec 20, 2005
    #1
    1. Advertising

  2. Francesco Devittori

    Chris Uppal Guest

    Francesco Devittori wrote:

    > I want to port an application from BCEL to ASM (which is smaller and
    > seems to be faster)


    I rather suspect that for your application, the difference may not be as great
    as you might expect. More below


    > an existing class is read. For each method in the class:
    > - l is the list of instructions in the method
    > - an algorithm re-creates the control flow graph:
    > some Node objects are created, each node contains:
    > - an instructionHandler that points to the instruction in l that
    > starts the node
    > - a size, i.e. the number of instructions in the node.
    > - then some instructions are inserted in each node (actually inserted
    > in l)
    > finally the class is dumped as an array of bytes (with the modified
    > instruction list of each method).

    [...]
    > What I whish to know from someone that already used ASM is: is it
    > possible to do something like that?
    >
    > I played a bit with ASM and I was not able to do anything in the right
    > direction (but maybe it's because I'm used to BCEL).


    The way ASM naturally works is unlike BCEL, and it does not (by default) create
    a web of objects corresponding to the structure of the classfile. The
    difference is very like that between processing XML using a SAX-style API and a
    DOM-style one. The SAX-style tends to be small and fast, but only if you don't
    need to consider the overall structure of the document. ASM (by default) has
    similar advantages. However, for your application I think you do need
    "non-local" processing -- you want to have a complete, modifiable,
    representation of each method's bytecode in memory at the same time. BCEL
    provides that for you whether you need it or not, ASM (by default) does not.

    You /can/ tell ASM to build an object representation of the bytecode, see
    package org.objectweb.asm.tree, but the cost is that ASM will run slower and
    use more memory (both for allocated objects, and for more loaded classes).
    Thus the advantage over BCEL will be reduced, it might even be eliminated (I
    would /guess/ not).

    I find the ASM API a little obscure. If you haven't already read it, then the
    tutorial
    http://asm.objectweb.org/doc/tutorial-asm-2.0.html
    seems pretty good. It doesn't directly address how to build a tree, but it
    should provide a useful background "orientation". The javadoc for
    org.objectweb.asm.tree has more info on how to use it. You might find the
    org.objectweb.asm.tree.analysis package useful too, but there doesn't seem to
    be much guidance on how to use it -- and in any case you probably already have
    code that does what the analysis framework helps with.

    -- chris
     
    Chris Uppal, Dec 21, 2005
    #2
    1. Advertising

  3. Chris Uppal wrote:
    > Francesco Devittori wrote:
    >
    >
    >>I want to port an application from BCEL to ASM (which is smaller and
    >>seems to be faster)

    >
    > [...]
    >
    > The way ASM naturally works is unlike BCEL, and it does not (by default) create
    > a web of objects corresponding to the structure of the classfile. The
    > difference is very like that between processing XML using a SAX-style API and a
    > DOM-style one. The SAX-style tends to be small and fast, but only if you don't
    > need to consider the overall structure of the document. ASM (by default) has
    > similar advantages. However, for your application I think you do need
    > "non-local" processing -- you want to have a complete, modifiable,
    > representation of each method's bytecode in memory at the same time. BCEL
    > provides that for you whether you need it or not, ASM (by default) does not.
    >
    > You /can/ tell ASM to build an object representation of the bytecode, see
    > package org.objectweb.asm.tree, but the cost is that ASM will run slower and
    > use more memory (both for allocated objects, and for more loaded classes).
    > Thus the advantage over BCEL will be reduced, it might even be eliminated (I
    > would /guess/ not).
    >
    > [...]
    > -- chris
    >


    Thanks! This is exactly what I suspected, but I was not able to put it
    in words like you did!

    Francesco
     
    Francesco Devittori, Dec 21, 2005
    #3
    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:
    453
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 4, 2005
  2. Thomas Zangl

    [BCEL] If then else construct - again

    Thomas Zangl, May 13, 2005, in forum: Java
    Replies:
    4
    Views:
    844
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 13, 2005
  3. Replies:
    1
    Views:
    808
    Thomas Hawtin
    Sep 7, 2005
  4. oulan bator

    Creating a Constructor in BCEL

    oulan bator, Jan 25, 2006, in forum: Java
    Replies:
    5
    Views:
    925
    oulan bator
    Jan 26, 2006
  5. Boris Gorjan

    Instrumentation + BCEL | ASM

    Boris Gorjan, Jun 6, 2006, in forum: Java
    Replies:
    18
    Views:
    1,452
    Boris Gorjan
    Jun 12, 2006
Loading...

Share This Page