



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!




In this assignment, you will implement a code generator for Cool. When successfully completed, you will have a fully functional Cool compiler! The code generator makes use of the AST constructed in PA3 and static analysis performed in PA4. Your code generator should produce MIPS assembly code that faithfully implements any correct Cool program. There is no error recovery in code generation—all erroneous Cool programs have been detected by the front-end phases of the compiler. As with the static analysis assignment, this assignment has considerable room for design decisions. Your program is correct if the code it generates works correctly; how you achieve that goal is up to you. We will suggest certain conventions that we believe will make your life easier, but you do not have to take our advice. As always, explain and justify your design decisions in the README file. This assignment is about twice the amount of the code of the previous programming assignment, though they share much of the same infrastructure. Start early! Critical to getting a correct code generator is a thorough understanding of both the expected behavior of Cool constructs and the interface between the runtime system and the generated code. The expected behavior of Cool programs is defined by the operational semantics for Cool given in Section 13 of the Cool Reference Manual. Recall that this is only a specification of the meaning of the language constructs—not how to implement them. The interface between the runtime system and the generated code is given in The Cool Runtime System. See that document for a detailed discussion of the requirements of the runtime system on the generated code. There is a lot of information in this handout and the aforementioned documents, and you need to know most of it to write a correct code generator. Please read thoroughly. You may work in a group of one or two people.
To get started, create a directory where you want to do the assignment and execute one of the following commands in that directory. If you are using C++, run this command to get the initial skeleton:
gmake -f ~ecs142/s09/assignments/PA5/Makefile
For Java, type:
gmake -f ~ecs142/s09/assignments/PA5J/Makefile
(notice the “J” in the path name). As usual, there are other files used in the assignment that are symbolically linked to your directory or are included from ecs142/s09/include and ecs142/s09/src. You should not modify these files. Almost all of these files have been described in previous assignments. We now describe the most important files for each version of the project.
This is a list of the files that you may want to modify. You should already be familiar with most of the other files from previous assignments. See the README file for details about the additional files.
This is a list of the files that you may want to modify. You should already be familiar with most of the other files from previous assignments. See the README file for details about the additional files.
You do not need to generate the same code as coolc. Coolc includes a very simple register allocator and other small changes that are not required for this assignment. The only requirement is to generate code that runs correctly with the runtime system.
The end of the Cool manual lists six errors that will terminate the program. Of these, your generated code should catch the first three—dispatch on void, case on void, and missing branch—and print a suitable error message before aborting. You may allow SPIM to catch division by zero. Catching the last two errors—substring out of range and heap overflow—is the responsibility of the runtime system in trap.handler. See Figure 4 of the Cool Runtime System manual for a listing of functions that display error messages for you.
We will offer extra credit for up 10% of the total value of the project. This will be factored in after the curve has been determined. It will be tested via a number of test cases. To receive this extra credit, you need to indicate in your project write up which of the optimizations or garbage collection techniques that you have implemented and how you have done so. Only optimizations that you indicate will be tested. Optimizations that you indicate which do not work will result in point deductions. Optimization extra credit works as follows: Roughly speaking, we will calculate an average speedup over how coolc is implemented in terms of the # of instructions executed and average over passed tests. These will be split into rough equivalent classes of optimizations (constant folding, copy folding, etc...). For this assignment, if you decide to implement optimizations that don’t result in fewer instructions, but do result in better instructions, please describe this and we will grade those optimizations separately. Additionally, you can receive extra credit by working correctly with the generational garbage col- lector in the Cool runtime system. The skeletons contain functions code select gc (C++) and Cgen- ClassTable.codeSelectGc (Java) that generate code that sets GC options from command line flags. The command-line flags that affect garbage collection are -g, -t, and -T. Garbage collection is disabled by default; the flag -g enables it. When enabled, the garbage collector not only reclaims memory, but also verifies that “-1” separates all objects in the heap, thus checking that the program (or the collector!) has not accidentally overwritten the end of an object. The -t and -T flags are used for additional testing. With -t the collector performs collections very frequently (on every allocation). The garbage collector does not directly use -T; in coolc the -T option causes extra code to be generated that performs more runtime validity checks. You are free to use (or not use) -T for whatever you wish. For your implementation, the simplest way to start is not to use the collector at all (this is the default). When you decide to use the collector, be sure to carefully review the garbage collection interface
described in the Cool Runtime System manual. Ensuring that your code generator correctly works with the garbage collector in all circumstances is not trivial.
4 Testing and Debugging
You will need a working scanner, parser, and semantic analyzer to test your code generator. You may use either your own components or the components from coolc. By default, the coolc components are used. To change that, replace the lexer, parser, and/or semant executable (which are symbolic links in your project directory) with your own scanner/parser/semantic analyzer. Even if you use your own components, it is wise to test your code generator with the coolc scanner, parser, and semantic analyzer at least once because we will grade your project using coolc’s version of the other phases. You will run your code generator using mycoolc, a shell script that “glues” together the generator with the rest of compiler phases. Note that mycoolc takes a -c flag for debugging the code generator; using this flag merely causes cgen debug (a global variable in the C++ version and a static field of class Flags in the Java version) to be set. Adding the actual code to produce useful debugging information is up to you. See the project README for details.
Coolaid is a tool to statically verify some basic correctness properties of the MIPS assembly code produced from Cool source. Coolaid will check that the assembly code is “well-typed” with respect to the Cool typing rules, just like the Java bytecode verifier checks that the bytecode output by a Java compiler is type safe. Aside from checking the safety of the output code, this tool can greatly benefit the development process of the code generation phase. Since the compiler front-end ensures that the source program is well-typed and that the type system guarantees certain safety properties, we expect that those properties should also hold on the resulting assembly code. If Coolaid is not able to verify some particular safety property, then a likely cause is a bug in the compiler itself. Coolaid has been tested on thousands of programs generated by student compilers from the compilers clsas at UC Berkeley. The standard testing procedure (which runs the generated code on the MIPS simulator spim) used for grading found errors in 1000 of those. Often the error messages were in the form of garbled output or output that did not match the expected output, whereas Coolaid was able to give precise error messages where the unsafe operation was performed that most likely caused the garbled output. Coolaid also finds errors in 800 of the 2500 programs that pass the testing procedure! These were errors that the testing procedure missed because the sample input for the code did not exercise the bad code portions. See the Coolaid Reference Manual to get started using Coolaid. Note Coolaid imposes some additional requirements on the generated code to be able to check it; these requirements are also discussed in the Cool Runtime System manual.
The executables spim and xspim are simulators for MIPS architecture on which you can run your generated code. The program xspim works like spim in that it lets you run MIPS assembly programs. However, it has many features that allow you to examine the virtual machine’s state, including the memory locations, registers, data segment, and code segment of the program. You can also set breakpoints and single step your program. The documentation for spim/xspim is on the course web page.