110 likes | 240 Views
This document outlines the objectives and implementation details for developing a backend in SC that generates Java bytecodes from an AST created in Project 3. The guide provides steps to utilize the ClassFile.java skeleton for inserting BCEL library calls, leveraging a symbol table for variable storage, and ensuring seamless mapping from SC programs to Java classes. It includes essential coding methods, necessary modifications to project files, and procedures for compiling and testing the custom compiler. A comprehensive reference to Java bytecode documentation is also included.
E N D
Problem 4 SC Java Byte Code Generator Cheng-Chia Chen
Goals • Build a back end for SC to generate Java byte codes from the AST created in Problem 3. • Use the skeleton back end in the file ClassFile.java, inserting calls to routines from the BCEL libraries to generate a sequence of Java byte codes. • Use information from your project 3 symbol table to assign storage for variables in your program.
> type Mysc.c int x; int[] y; int m1(int a) { …} void m2() { …} main(){…} global vars ==> static fields procedure ==> static method main() ==> main() > type Mysc.java public class mysc { public static int x; public static int[] y; public static int m1(int 1){…} public static void m2(int 1){…} public static main(String[]) {…} } Mapping from sc programs to java classes
project files • Files you need to modify • mysc.lex Regular expressions & actions for scanner (should require no changes from project 3) • mysc.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)
Project Files • (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 mysc.cup • parser.java produced by CUP from mysc.cup • Yylex.java produced by JLex
Project Files • go.bat • script for building compiler • go1.bat <prog> • script for testing compiler on one file <prog.c> • goTest.bat • script for 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
main functions to be implemented • Documentation on Java Byte code • Document on BCEL : http://jakarta.apache.org/bcel/ • Main methods you should implement in classFile.java • genProcCode() : Generate code for a procedure. Allocate storage for local variables, then create code as a list of instructions. • genTreeCode() : Generate code for a SC astNode representing TREE_BLOCK, TREE_IF or TREE_WHILE statements. • genExpCode() : Generate code for a SC expNode representing a single TREE_INSTRUCTION • genOpCode(): Generate code for a SC expNode representing an operand
Main data structures used • InstructionList • instances of which you would used most often. • 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. • Ex: given il x and expNode (plus op1 op2) => • 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 returns an InstructionHandle (label) that can be used as targets of other generated 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
From mysc.cup • main(String[]) {… String cname = fname.substring(0, fname.length()-2); FileReader fp = new FileReader(fname); addPrintFunctions(); // add print functions in the global symbol table new parser(new Yylex(fp)).parse(); // parse input code.print(); // ast saved in code; print AST ClassFile class_file = new ClassFile(cname); // prepare codegen ClassFile.generate_code(code, globalSymTab); // generate code }