11
Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

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

  • View
    217

  • Download
    2

Embed Size (px)

Citation preview

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

1

Problem 4

Cheng-Chia Chen

SC Java Byte Code Generator

Page 2: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

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.

Page 3: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

3

Mapping from sc programs to java classes

> 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[]) {…}

}

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

4

Page 5: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

5

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)

Page 6: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

6

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

Page 7: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

7

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

Page 8: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

8

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

Page 9: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

9

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

Page 10: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

10

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

Page 11: 1 Problem 4 Cheng-Chia Chen SC Java Byte Code Generator

11

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

}