1 / 11

Problem 4

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.

amora
Download Presentation

Problem 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem 4 SC Java Byte Code Generator Cheng-Chia Chen

  2. 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.

  3. > 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

  4. 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)

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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 }

More Related