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

  • Thread starter Francesco Devittori
  • Start date
F

Francesco Devittori

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
 
C

Chris Uppal

Francesco said:
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
 
F

Francesco Devittori

Chris said:
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
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top