CMSC 430: Project 4: C-- Byte Code Generator Your goal is to build a back end for C-- to generate Java byte codes from the AST created in project 3. You will use the skeleton back end in the file ClassFile.java, inserting calls to routines from the JavaClass libraries to generate a sequence of Java byte codes. You will also use information from your project 3 symbol table to assign storage for variables in your program. ********************************************** Setup ********************************************** Project 4 requires a new library, JavaClass. Download de.tar and extract its contents to the Java library directory in your CLASSPATH (same place where JLex and java_cup are located). For Windows, extracting the tar file in c:\Java should place all the contents in c:\Java\de. ** Commands to build compiler (in script "go") java JLex.Main mycc.lex mv mycc.lex.java Yylex.java java java_cup.Main < mycc.cup javac -g -d . *.java ** Commands to test the compiler (in script "go1") java mycc.parser $prog.c > $prog.log cat $prog.log javap -c $prog java $prog The compiler will generate a Java class file .class for an input file named .c. You can examine the contents of the class file using ``javap -c'' to disassemble the class file. **** Description of project files (Files you need to modify) mycc.lex Regular expressions & actions for scanner (should require no changes from project 3) mycc.cup Grammar & actions for parser (only change should be uncommenting a few lines) ClassFile.java Class to handle byte code generation (most if not all of your code changes) (Files you should not need to modify) Const.java Constants for mycc symTabEntry.java Symbol table entries symTab.java Symbol tables astNode.java AST nodes astNodeList.java list of AST nodes expNode.java expression nodes sym.java produced by CUP from mycc.cup parser.java produced by CUP from mycc.cup Yylex.java produced by JFlex go script for building compiler go1 script for testing compiler on one file goTest script for testing compiler on several test files goAll script for building & testing compiler on several test files toy*.c toy input files that the skeleton compiler can compile test*.c test input files for project test*.out example compiler output files from working project ************************************************* Description of main data structures and functions ************************************************* Documentation on Java bytecodes and the JavaClass library is available from the class web page. Be sure to take a quick look before starting the project. The three procedures you need to implement in the compiler backend are: genProcCode() Generate code for a procedure. Allocate storage for local variables, then create code as a list of instructions. genInstCode() Generate code for a C-- astNode representing TREE_BLOCK, TREE_IF or TREE_WHILE statements. genExpCode() Generate code for a C-- expNode representing a single TREE_INSTRUCTION genOpCode() Generate code for a C-- expNode representing an operand The main data structure you will be manipulating is the InstructionList class. It maintains a list of Java byte code instruction equivalents that make up the output of the compiler back end. New instructions may be added to the list, and other InstructionLists may also be appended to existing list. New java byte codes instructions are created through constructors and added to an InstructionList. For instance, given the instruction list x, we can add a new instruction as follows: InstructionList x; genExpCode(x, op1); // generate instructions for first operand, append to x genExpCode(x, op2); // generate instructions for 2nd operand, append to x x.append(new IADD()); // generate IADD instruction, append to x The append() function also returns an InstructionHandle (label) that can be used when generating conditional branch instructions: InstructionList x, y; InstructionHandle label; label = x.append(new NOP()); // returns label for conditional branch targets y.append(new IFEQ(labe1)); // create new conditional branch instruction