Docsity

Compiler Design - Assignment 6 - Fall 2007 | CIS 4930, Assignments of Computer Science

Toggle sidebar
bg1
Compiler Design (CIS 4930/6930) [Fall 2007]
Programming Assignment VI
Objectives
1. To learn basic rules for generating RISC-style machine code equivalent to high-level
abstract-syntax trees (ASTs).
2. To implement a code generator that converts Diminished Java (DJ) ASTs into
Diminished Instruction Set Machine (DISM) programs.
3. To test a complete dj2dism compiler.
Due Date: Friday, December 7, 2007 (at 11:59pm).
Machine Details
Complete this assignment by yourself on the following CSEE network computers:
c4labpc11, c4labpc12, ..., c4labpc29. These machines are physically located in the
Center 4 lab (Room 220). Do not use any server machines like grad, babbage, sunblast,
etc. You can connect to the C4 machines from home using SSH. (Example: Host name:
c4labpc11.csee.usf.edu Login ID and Password: <your login id and password>) You are
responsible for ensuring that your program executes properly on these machines.
Assignment Description
This assignment asks you to complete your dj2dism compiler by implementing the code-
generation phase of compilation.
Begin by downloading and studying the header file for the code-generation module. This
file, codegen.h, is posted at http://www.cse.usf.edu/~ligatti/compilers-07/as6. Then
implement the generateDISM method, which is declared in codegen.h, in a new file
called codegen.c.
If you are completing this assignment by extending your Assignment V implementation
(as is recommended), you will also have to make a few minor modifications to your dj.y:
1. The codegen.h header file needs to be included.
2. The main method in dj.y, which is the main method of the entire compiler, should
now enforce that the compiler is invoked with the command “dj2dism s.dj”, where
“s.dj” is the source-program filename and “s” can be any string.
3. After typechecking the input file “s.dj”, the main method of dj.y should open the
file “s.dism”, into which the target program will be output. (If “s.dism” already
exists, your compiler should overwrite the existing “s.dism” file.)
4. Having opened and obtained a file pointer for “s.dism”, the main method of dj.y
should invoke the code generator by calling the generateDISM method.
5. Before exiting, the main method of dj.y should close the “s.dism” target-code file.
1
pf3
pf4
pf5

Partial preview of the text

Download Compiler Design - Assignment 6 - Fall 2007 | CIS 4930 and more Assignments Computer Science in PDF only on Docsity!

Compiler Design (CIS 4930/6930) [Fall 2007] Programming Assignment VI Objectives

  1. To learn basic rules for generating RISC-style machine code equivalent to high-level abstract-syntax trees (ASTs).
  2. To implement a code generator that converts Diminished Java (DJ) ASTs into Diminished Instruction Set Machine (DISM) programs.
  3. To test a complete dj2dism compiler. Due Date: Friday, December 7, 2007 (at 11:59pm). Machine Details Complete this assignment by yourself on the following CSEE network computers: c4labpc11, c4labpc12, ..., c4labpc29. These machines are physically located in the Center 4 lab (Room 220). Do not use any server machines like grad, babbage, sunblast, etc. You can connect to the C4 machines from home using SSH. (Example: Host name: c4labpc11.csee.usf.edu Login ID and Password: ) You are responsible for ensuring that your program executes properly on these machines. Assignment Description This assignment asks you to complete your dj2dism compiler by implementing the code- generation phase of compilation. Begin by downloading and studying the header file for the code-generation module. This file, codegen.h , is posted at http://www.cse.usf.edu/~ligatti/compilers-07/as6. Then implement the generateDISM method, which is declared in codegen.h , in a new file called codegen.c. If you are completing this assignment by extending your Assignment V implementation (as is recommended), you will also have to make a few minor modifications to your dj.y :
  4. The codegen.h header file needs to be included.
  5. The main method in dj.y , which is the main method of the entire compiler, should now enforce that the compiler is invoked with the command “dj2dism s.dj”, where “s.dj” is the source-program filename and “s” can be any string.
  6. After typechecking the input file “s.dj”, the main method of dj.y should open the file “s.dism”, into which the target program will be output. (If “s.dism” already exists, your compiler should overwrite the existing “s.dism” file.)
  7. Having opened and obtained a file pointer for “s.dism”, the main method of dj.y should invoke the code generator by calling the generateDISM method.
  8. Before exiting, the main method of dj.y should close the “s.dism” target-code file.

Notes on Code Generation in dj2dism

  1. Your generated DISM-code file must contain exactly one instruction per line. This will make it easy to find the precise instruction at which your program halts. (The sim-dism virtual machine only reports the instruction number at which the DISM program halted; if sim-dism reports that the program halted at instruction n and the code file contains exactly one instruction per line, then we can find the halting instruction at code-file line number n+1 .)
  2. In order to satisfy the constraint that every line of the generated “.dism” file must contain exactly one DISM instruction, you may sometimes find it convenient to write instructions of the form “#LABEL: mov 0 0” or “mov 0 0 ; comment”. In such instructions, the “mov 0 0” is used as an empty operation (i.e., a no-op ) so that we can output a DISM label or a comment on an otherwise empty line of code.
  3. Your code generator must implement the conjunction operator (&&) in the standard short-circuit style. File good6.dj illustrates this operator’s dynamic semantics.
  4. Your code generator must check and halt before dereferencing null-valued objects. Files good20.dj , good21.dj , and good22.dj illustrate the required behavior. Hints on Organizing the Code Generator You may find it helpful to organize your codegen.c such that it contains the following (in addition to the definition of the generateDISM method declared in codegen.h ): #define MAX_DISM_ADDR 65535 /* Global for the DISM output file */ FILE fout; / Global to remember the last label number generated / unsigned int labelNumber = 0; / Declare mutually recursive functions (defs and docs appear below) */ void codeGenExpr(ASTree *t, int classNumber, int methodNumber); void codeGenExprList(ASTree expList, int classNumber, int methodNumber); / Print a message and exit under an exceptional condition */ void internalCGerror(char msg) / Returns a new, unique DISM symbolic label */ char getNewLabel() / Using the global classesST, calculate the total number of fields (including inherited fields) in an object of the given type. / int getNumObjectFields(int type) / Generate code that increments the stack pointer / void incSP() / Generate code that decrements the stack pointer */ void decSP()

void getDynamicMethodInfo(int staticClass, int staticMethod, int dynamicType, int *dynamicClassToCall, int dynamicMethodToCall) / Emit code for the program's vtable, beginning at label #VTABLE. The vtable jumps (i.e., dispatches) to code based on (1) the dynamic calling object's address (at M[SP+4]), (2) the calling object's static type (at M[SP+3]), and (3) the static method number (at M[SP+2]). */ void genVTable() More Hints My codegen.c file is 595 lines of code (100 of which are comments/whitespace). As with Assignment V, this is a relatively challenging assignment, requiring you to write several hundred lines of code. Please budget your time accordingly. Many examples of valid DJ programs, on which you can test your compiler, are posted at http://www.cse.usf.edu/~ligatti/compilers-07/as1/dj/examples/good/. A complete solution to this assignment will correctly compile all of the valid DJ programs into equivalent, valid DISM code that can be executed in the sim-dism virtual machine. As usual, though, we will grade your compiler’s performance on DJ programs that have not been distributed to the class. Grading Your grade on this assignment is determined by the level to which you implement the desired functionality:  Level I : generateDISM correctly generates DISM code for all DJ programs that contain no classes and no local variables.  Level II : generateDISM correctly generates DISM code for all DJ programs that contain no classes but do use ( nat -type) local variables in the main block.  Level III : generateDISM correctly generates DISM code for all DJ programs. Undergraduate students will earn: 80% credit for reaching Level I, 100% credit for reaching Level II, and 115% credit for reaching Level III. Graduate students will earn: 65% credit for reaching Level I, 85% credit for reaching Level II, and 100% credit for reaching Level III. If you have implemented the complete dj2dism compiler by yourself, you may also submit its full source code for +20% (for undergraduate students) or +10% (for graduate students) extra credit. Compilation of the Compiler Use the following commands to compile the complete dj2dism compiler from scratch:

flex dj.l bison -v dj.y gcc dj.tab.c ast.c symtbl.c typecheck.c codegen.c -lfl -o dj2dism

Alternatively, you may download (typically by right-clicking and selecting “Save as”) working versions of the lexer, parser, AST, symbol-table, and type-checker modules as object-code files dj.tab.o , ast.o , symtbl.o , and typecheck.o from http://www.cse.usf.edu/~ligatti/compilers-07/as6/. With these object-code files, and the header files ast.h , symtbl.h , typecheck.h , and codegen.h , you can compile dj2dism with:

gcc dj.tab.o ast.o symtbl.o typecheck.o codegen.c -lfl -o dj2dism Example Executions Your compiler should continue to report lexical, syntactic, and semantic errors in the source program before exiting. As with previous assignments, you need not report multiple errors. If there are no lexical, syntactic, or semantic errors, your compiler generates DISM code. Your compiler should also enforce that a source-program filename ending in “.dj” has been given: ./dj2dism Usage: dj2dism file.dj ./dj2dism good99.dj ERROR: could not open file good99.dj ./dj2dism good1.dism Error: Input filename does not match *.dj The compiler produces no output (besides the target-code file) when compiling a valid DJ program: ./dj2dism good1.dj

After compiling good1.dj , we can find the dj2dism-generated code in file good1.dism : mov 7 65535 ; initialize FP mov 6 65535 ; initialize SP mov 5 1 ; initialize HP mov 0 0 ; ALLOCATE STACK SPACE FOR MAIN LOCALS mov 0 0 ; BEGIN METHOD/MAIN-BLOCK BODY mov 1 0 str 6 0 1 ; M[SP] <- R[r1] (a nat literal) mov 1 1 sub 6 6 1 ; SP-- blt 5 6 #0 ; branch if HP<SP hlt ; out of stack memory! (HP >= SP) #0: mov 0 0 hlt ; NORMAL TERMINATION AT END OF MAIN BLOCK #VTABLE: lod 1 6 4 ; r1 <- caller's address lod 1 1 0 ; r1 <- caller's dynamic type lod 2 6 3 ; r2 <- static class containing method lod 3 6 2 ; r3 <- static method number hlt ; vtable entry not found! (Of course, the DISM files your compiler generates must only be functionally equivalent —rather than identical to—the DISM files my compiler generates.)