Hey abc people...
I came up with a very simple example of why Jimple is better than
bytecode, both for Thursday and for the paper(s):
public class Foo {
public static final void main( String[] args ) {
System.out.println(new Foo().foo());
}
public int foo() {
return bar(1,2,3);
}
public int bar(int a, int b, int c) {
return a+b+c;
}
}
/*
aspect A {
before(Foo x) : call(int bar(int,int,int)) && target(x) {
System.out.println(x);
}
}
*/
1. When compiled with javac, without the aspect, the code for foo() is:
0: aload_0
1: iconst_1
2: iconst_2
3: iconst_3
4: invokevirtual Foo.bar (III)I (7)
7: ireturn
Question: If you were going to weave in the advice by hand, what would it
look like?
Hint: it would look like what abc does, *not* what ajc does. See below.
2. When compiled with ajc, with the aspect, the code for foo() is:
0: aload_0
1: iconst_1
2: iconst_2
3: iconst_3
4: istore_1
5: istore_2
6: istore_3
7: astore %4
9: invokestatic A.aspectOf ()LA; (50)
12: aload %4
14: invokevirtual A.ajc$before$A$10a (LFoo;)V (54)
17: aload %4
19: iload_3
20: iload_2
21: iload_1
22: invokevirtual Foo.bar (III)I (38)
25: ireturn
Bleh! We load the consts, store them away again, do the before, then
load the consts back. 25 bytes instead of 7. Three pointless locals.
Recall that we have all those benchmarks where having lots of locals
slows things down by a lot.
3. When compiled with abc, with the aspect, the code for foo() is:
0: invokestatic A.aspectOf ()LA; (15)
3: aload_0
4: invokevirtual A.before$0 (LFoo;)V (21)
7: aload_0
8: iconst_1
9: iconst_2
10: iconst_3
11: invokevirtual Foo.bar (III)I (9)
14: ireturn
This is exactly the natural thing one would do by hand! We have the
receiver, so pass it to the aspect. Then do what the method did before:
call bar with the constants.
Notice that having Jimple with variables (3-address) and not having to care
about the stack means we don't have to do anything fancy: we just do the
natural weaving. On the other hand, in order to do this in ajc, on
bytecode, we would need to apply an optimization like code motion or
constant propagation, just to get the obvious thing.
I've ommitted the Jimple versions of the code before and after weaving
with abc, but we should maybe include them as well to make things clear.
The above example shows very simply why 3-address is a good idea. I'm
sure we can come up with something else for why the types are useful.
Ondrej
Received on Tue Sep 7 12:12:51 2004
This archive was generated by hypermail 2.1.8 : Tue Sep 07 2004 - 17:50:02 BST