Re: ASM analysis problem

Marcin Rzeźnicki <[email protected]>
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
Hi,
I faced similar problem (for some optimization of copying I needed to
know which variables are "final", and that is not marked in bytecode).
You might want to check my MethodTransformer in coroutines package (it
is linked to on the asm user projects page). Basically, I introduced
finals array (its length equal to number of local variables, if you do
one-pass analysis with core package you might want to make it a list
because you would not know this number in advance). finals[i] = true
means that ith variable has not been written to since some point in
bytecode (that is specific to my problem, you'd probably want to
consider whole method scope instead - that dictates WHEN you change
finals[i] to true - in your case I'd set them all to true upon
initialization). So:


for (int i = 0; i < methodArguments.length; i++) {
            finals[localIndex] = true;
            localIndex += methodArguments[i].getSize();
        }

I mark all arguments (with implicit this) as read-only first, you'd
probably just init the whole array to TRUEs

and then, only xSTORE instruction may change state from final to
not-final, so you track them:

    while (i.hasNext()) {
            AbstractInsnNode insn = i.next();
            /*
             * track locals
             */
            int insnType = insn.getType();
            if (insnType == AbstractInsnNode.VAR_INSN) {
                int opcode = insn.getOpcode();
                if (opcode == ASTORE || opcode == DSTORE || opcode ==
LSTORE || opcode == ISTORE || opcode == FSTORE) {
                    int varIndex = ((VarInsnNode) insn).var;
                    finals[varIndex] = false;
                }
            }
...
}

After reading all instructions (with one assumption, that all code you
read is reachable, this is the case if this code is generated by a
compiler) you know which variables are "read-only" (those with finals
set to true still) and which are not.
Hope it helped.

2010/11/18 João Barbosa <[email protected]>:
> Hi,
>
> I'm new with ASM and I'm having some problems, event after doing a full search
> on the web.
> My goal is to get a list of field names from a class X that are writable, i.e.
> that have writes, and another list of those that are readOnly;
>
> The only thing that I have right now is this code, where the only thing that i
> can do is getting the opcode of methods instructions.
>
> MethodNode method = (MethodNode) methods.get(i);
> for (int x = 0; x < method.localVariables.size(); ++x) {
>    Object insn = method.localVariables.get(i);
>    int opcode = ((AbstractInsnNode) insn).getOpcode();
> }
>
>
> example of what I want
>
> class X{
>   int y;
>
>   public void method1(){
>       y=x;
>   }
>
> }
> result:
> y -> writable
> x -> readOnly
>
> Does anyone can help me with this? I solve this problem with BCEL. However, ASM
> is much faster but to me is much harder too.
>
>
>
> --
> You receive this message as a subscriber of the [email protected] mailing list.
> To unsubscribe: mailto:[email protected]
> For general help: mailto:[email protected]?subject=help
> OW2 mailing lists service home page: http://www.ow2.org/wws
>
>



-- 
Pozdrawiam
Marcin Rzeźnicki
message-footer.txt (text/plain, 238 B)
-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.