10.26.05
Posted in PL Research at 3:25 pm by site admin
My research on the NextGen Generic Java Compiler has progressed on to supporting mixins. A Mixin is a parametric class that extends its parametric type parameter, for example “Mixin extends T”. One advantage of Mixins is that they provide an abstract mechanism to specifiy uniform class extensions. For example I can define a class TimeStamped<T> that can extend any given class, provide additional functionality, but still preserve the same functionality as its parent.
One of the most interesting issues with Mixins in Java concerns what kind of mechanism is used to instantiate Mixins. In Java, there is no uniform way to instantaniate classes. As a result, it is not straightfoward to create a Mixin that satisfies all the possible constructor types in its super class. In otherwords, the set of constructors must be determined based on its parent class.
Now with that basic introduction, I would like to discuss how I create a Mixin instantiation. Basically, I take a Mixin template and instantiate with respect to its parent class. Wihle this process is a straightfoward “fill-in the holes”, the main difficulty I have encountered is preserving the constructor methods. What I needed to do was create a constructor in the sub class for each constructor in the parent. In addition to “bridging”, the constructor must initialize any additional state from the template. Now to do this I would normally use BCEL. However, I know development on BCEL has waned. So I did some research and discovered that it is kind of in maintence mode. In the mailing list I found a Comparison between ASM and BCEL, as well as a few other comparisons between ASM, BCEL, JavaAssist, etc. On the ASM homepage, they boast a 700% performance gain over BCEL. This seemed too good to be true, so I decided to test the performance of ASM vs BCEL in my task of creating Mixins. I am providing a copy of my source asm_vs_bcel.tgz.
Now my simple test shows that ASM is about 350% faster than BCEl. My test does the following:
1. read both the Mixin template and the new super class (used for the mixin)
2. Parse out the available constructors in the parent
3. Use the init method found in the mixin as a template to generate the appropriate ‘bridge’ constructors found in 3. After each super call, I then append all the remaining initialization necessary for the mixin (found in the mixin’s init method)
4. Write out the newly instantiated class.
The bcel code follows a very procedural methodology. While there are some annoyances resulting from using JavaClass vs ClassGen and Method vs MethodGen, a top-down read can easily explain the method.
Now the ASM code is different. At first the ASM Tutorial did not seem to stick. It is however, very correct. The two intersting points of ASM are:
* ASM works through a series of visitors.
* ASM follows a SAX model for traversing a java class.
So a ClassVisitor works through a series of sequential calls:
visit [ visitSource ] [ visitOuterClass ] ( visitAnnotation | visitAttribute )* (visitInnerClass | visitField | visitMethod )* visitEnd.
Thus to perform any code manipulation, it is necessary to define a new ClassVisitor (and probably MethodVisitor). The ASM developers have conviently provided a ClassAdapter and MethodAdapter to faciliate development. Now the use of Visitors in Java is not very new. What is kinda weird is how the SAX-ish design of ASM plays in to everything. For example, to parse a class, the class reader must accept a subclass a ClassVisitor, namely ClassNode. ClassNode represents a class tree in ASM. Naturally, ClassNode also has an __visit__ method for ClassVisitors.
ClassReader super_cr = new ClassReader(super_class);
ClassNode cn = new ClassNode();
super_cr.accept(cn, true);
And then to add a method to parsed tree, I follow a similiar pattern:
newMethod.accept (cn);
Check out the provided source code if you are interested in how ASM code and BCEL code compare, or simply need an example how to write a small class transformation using either of these two languages. For my purposes, ASM is faster and smaller and thus is my choice for the job. I will however, be performing future metrics comparing ASM, BCEl, and also down and dirty manual byte hacking for the purpose of changing the Constant Pool. The issue of course, is that ASM does not provide direct access to the constant pool. The ASM SAX model seems cumbersome, and unintuitive, in this case. I leave this task for another day.
Permalink
08.17.05
Posted in health, PL Research at 5:22 pm by James
I have been busily working on my research for the NextGen Generic Java compiler. I have spend the last couple days starting on this paper for SAC 2006. I was reviewing the list of type dependent operations supported by NextGen. In particular, I was double checking if NextGen currently handles casting correctly. I proceeded to create the following test case:
public class A {
static class Box < T> { }
public static void main(String[] args) {
Box < String> s = new Box< String>();
Box< ?> a1 = (Box< ?>) (Object)s;
Box< Object> a2 = (Box< Object>) (Object) s;
}
}
After executing this code, I inspected the resulting bytecode and discovered that casting was not working properly. So after some fiddling, I found the segment of code I need to modify. At the top of this section, I also found the following comment:
//TODO: cant cast Object to Box< String>
//will need to fix this case to support
//generic casts
So I’ve returned to fix a problem that I had skimmed over before, using the same basic names and types. Well I guess I like using Box< T> as an example. Still kinda funny though.
Permalink
01.18.05
Posted in PL Research at 7:07 pm by James
I found this article on slate about Who Needs Harvard?. Basically talks about how the percentage of Ivy League graduates are declining in US corporations. They attribute this to both the improvement of other schools and also the necessity of the rich to work.
My 2c is that the these days there is a extreme level of diversity among different fields. A small private university cannot offer a premier education for the great diversity in careers. On the other hand, larger public universities not only have the critical mass to offer specialized education in many diverse fields, but also can pool the resources necessary for each field.
A professor at school told me that university is derivied from the two roots: “uni” means everything, and “versity” meaning knowledge. What he was trying to tell me is that in a university, you have to learn a little bit of everything. I can see how this applies in a specialized sense. For example, learning computer science is ok, but if you have the opportunity to learn CS and business, or CS and something else, it gives you a better level of understanding how one topic of knowledge can relate to others.
Permalink
10.12.04
Posted in PL Research, Religion/Philosophy at 1:10 pm by James
Lazyiness pays off. Yesterday, my advisor Corky casually mentioned a Professor would be coming to Rice today. He casual asked if i wanted to meet with him, and I casually responded ok. Today i was busy working on my research. I checked my email at 2pm and found out that I was scheduled a appointment at 11:45 (without my knowledge… so much for casual). So I hurried home to eat lunch and then to school to make a presence. Turns out the visitor arrived late and the whole schedule was out of wack. So I didnt miss anything, didnt piss any one off, and i was actually basically on time for the guys presentation. And now I think it would matter even if i missed the presentation… not as formal as it was beginning to seem. Score 1 for casually late.
Permalink
09.03.04
Posted in Daoism, health, PL Research, Religion/Philosophy at 2:49 pm by James
Wing Chun is not just a martial art, its a way of life. The foundations of Wing Chun are rooted in daoist philosophy. One of the precepts of Wing Chun is the notion of “Attack the Attack”. So in fighting this means to always stay on the offensive and never think defensively. This doesnt mean that you should trade punches or just take hits. What this means is to think offensively and always look for openings.
I kinda get this, yet I don’t. But now, I’m getting there. I am a teaching assistant for a class at school. I had to sit and listen to a Professor tell me to (1.) Do all the class assignments by myself to have an answer key ready for the class, (2.) That after I finished, he didnt think what I did was correct, and (3) When I explained to him why, he basically told me what I was doing (which was logical) was wrong, and to add a little bit of icing on the cake (4) That I was to grade everything (grading is a bit automated, but that doesnt mean its a walk in the park). So after all of this, I was a bit bummed out.
So before our next meeting (with the other 2 teaching assistants presents), I did my homework. I talked it over with my advisor, and got these issues straightened out. He told me everything I was thinking was right. So I walked into the meeting with my plan.
Prof: So can TA #1 you can help with the grading. Wait you only know Java.
TA #1: And Scheme. but theres only 1 group.
Prof: Ok well thats about half. That might be ok
Me: We really should split the grading evenly. He needs to be familiar with all the different languages because someone might ask him for help. [Attack the Attack!]
…
Me: Well we also need to talk about the datatypes that are in question
Prof: Well we can do that just between the two of us
Me: I think everyone here should know.. just in case there are questions.. (everyone is a teaching assistant) [Attack the attack!]
Prof: Ok, does everyone want to work on it?
Everyone: Ok.
Then while we looked at the stuff, I made my points, and sat back and let the other TAs support my claims.
“Attack the attack” is a very subtle technique. I think it requires:
1. Objectives
2. Preparation
3. Subterfuge
Permalink
08.16.04
Posted in internet, linux, PL Research, Unfiled at 8:48 pm by James
I like the sound of that.. Well its not really much of a paradox.. While python is not a language pressed by the education system, it does have its home in the land of scripting, the web, and unix. And it does have a more theoretical basis… lambda! I use python a decent amount. However, for me its a struggle between python and php (with the stabilization of php-cgi). Because the Python db 2.0 APIs are too Java-ish, i prefer php for all my more complex tasks. php has the ability to get results in associative arrays, uses ” for empty string and null. I can deal with ‘None’ popping up everywhere, but I gotta have associative arrays. I just cant see myself doing row[ 23 ] and being correct. I stand corrected!! ADOdb for python! Here is the full article.
Permalink
04.07.04
Posted in PL Research at 9:21 am by James
A java compiler must follow the existing Java Compiler Model:
1. Write once, read most places
2. Modular compilation
3. Compilation of child classes cannot affect parents
4. Compilation of parent classes may break child classes
5. Two steps: compilation & execution
Permalink
04.16.03
Posted in PL Research at 6:24 pm by James
I’m currently trying to augment the java compiler to support auto-boxing of primitives. The problem I’m currently facing is that there is no OO way to mutate objects in the visitor pattern. In the visitor pattern, you can call obj.visit(this), and then it will call the appropriate method. Once inside this method, its impossible to mutate the type of the parameter you have passed in. This is an inherent benefit/problem with recursive decent design. So of course the correct approach is to modify the code one level up. However, the design of sun’s java compiler doesnt make this easy without a moderate amount of modification to the code.
Permalink
03.03.03
Posted in PL Research at 3:43 pm by James
This semester in school I will be researching the following: 1) Getting up to speed with JavaPLT’s nextgen compiler, and 2) exploring mechanisms to unify the java type system.
The other ideas currently on the back burner is type inference of java byte code. For this I was thinking about using the becl library.
Permalink