All posts by Albert Zündorf
A helpful link which explains the JVM and Java bytecode in great detail: The Java Virtual Machine Specification.
This subpage has all bytecode instructions listed and explained: The Java Virtual Machine Instruction Set.
Also, here are the JavaDocs for BCEL: http://jakarta.apache.org/bcel/apidocs/index.html
We’re now ready to actually generate working code. Your task, should you accept it, is to change your interpreter to a compiler for arithmetic expressions. Generate code for the following “features”:
- Numbers (PUSH)
- Addition / subtraction (DADD, DSUB)
- Multiplication / division (DMUL, DDIV)
- Variable assignment (DSTORE)
- Variable access (DLOAD)
- Edit: variable assignments do NOT have to work like expressions, i.e. you don’t have to support “x = y = 5”!
A common problem with code generation is that you have to distinguish between compile time and run time. These are two separate phases, which is different from the way most interpreters work. Compile time is the first phase, in which you parse statements and generate code. You can use the JVM debugger to check this phase for errors, have a look at the symbol table and the results of the parser. When running the generated code, you cannot simply attach a debugger to it. This means that in the case of a wrong result, you have to manually check the bytecode to see what’s wrong. You do not see that stack, you have no variable names and there are no explicit control structures. You can use the Navigator view in Eclipse to open the .class files in your bin folder and see the bytecode. To make sure the bytecode is working, manually interpret it in your head. Keep track of the values on the stack by writing them on a sheet of paper. Check out the BCEL manual for help on the bytecode!
And if anybody happens to know a good bytecode debugger, please let everyone know on the mailing list…
Dies ist die ResourceMap!
Jeder Pixel (100*100) dieser png Datei entspricht einem möglichem Produktionsfeld auf der original Map!
Die Farbwerte beschreiben welche Gebäude wo gebaut werden können!
Blau = Unbebaubar
Grün = Holzfällerplätze
Rot = Steinmetzplätze
Dunkelrot/Braun = Fischerhüttenplätze
Schwarz = Normale Gebäudeplätze
We’ve made the jump from interpretation to actual compiling of expressions. To start your code-generating compiler, download the BCEL library from http://jakarta.apache.org/bcel/.
Add the library to your project build path and write an example Java program as shown in the lecture. Run the BCELifier tool that comes with the library and take a good look at the output. Refer to the BCEL manual to get a better understanding of the code. Now, modify your interpreter to initialize the bytecode generation facilities. Remove the call to your recursive interpreter. This will cause your tests to fail, so delete or ignore them for now. Write a simple test which evaluates a string which only contains a number. Include the code produced by BCELifier in your parser method. You can leave the hardcoded numerical value in the code. Check that a .class file is generated and load it using the Java ClassLoader, invoke the generated method using java.lang.reflect, and return the result produced by the generated code.
We will start to generate code from the input text in the next homework.