[antlr-interest] build issues: bytecode assembly generation
Terence Parr
parrt at cs.usfca.edu
Thu Oct 21 11:55:29 PDT 2004
On Oct 21, 2004, at 10:53 AM, Matt Benson wrote:
>
> --- Terence Parr <parrt at cs.usfca.edu> wrote:
>> My goal is basically to avoid .class file generation
>> / reading totally
>> by making the compiler do it. All I would have to
>> do is open up the
>> ..class file and replace a blank method (there's a
>> comment as the method
>> containing the bytecodes)
>> with the real code after I assemble it. The only
>
> Hmm, seems if you have to know where the classfiles
> are anyway you might as well write them yourself. Or
> create a part of the ANTLR runtime that would compile
> the bytecodes from byte[]s at runtime (ducking).
I just had another idea. The original problem is "I need gotos in
Java." What if I could make a quick translator for a .java file with
special comments that would fix up the .class file to allow the gotos?
Let's see...one might do this (remember, this file is *generated*):
public static int dfa1(IntegerStream input) {
short from, to;
int i = input.LA(1);
if ( (i>='a'&&i<='z') || (i>='0'&&i<='9') ) {
input.consume();
from = (short)0xBAAA; // a secret GOTO
}
to=(short)0xBAAB; // a secret GOTO target
return 0;
}
We need some unique thing that doesn't get thrown out by the compiler
to signify the goto location and the target of the goto. The
from = (short)0xBAAA;
results in the following bytecode.
sipush -17750
The target is just:
sipush -17749
Now we probably need something a little more unique like two in a row,
but you get the idea (they need to be short so the operand doesn't get
dumped into the const pool).
Here's the cool translation part. You look in a .class file for a
binary pattern then replace goto targets with no-ops, but you know the
byte code address now :) The native compiler will throw these out.
The gotos themselves are simply replaced with a goto_w or goto bytecode
instruction, filling with no-ops. Direct manipulation of simple binary
patterns in a .class file--totally ignoring the format minus the
structure of a like 3 byte code instructions.
Cool, eh? This should work great! That said, I do like my bytecode
assembler ;)
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list