



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Compiler Design (CIS 4930/6930) [Fall 2007] Programming Assignment VI Objectives
Notes on Code Generation in dj2dism
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.)