Due: By Sunday, December 11, at 11:00 pm. Use this online turnin form to submit your project.
The purpose of this final part of the project is to complete the compiler by adding code generation and implementing the runtime support needed to executed the generated x86 assembly code. To be sure you finish by the end of the quarter, we suggest that you use the simple code generation strategy outlined in class, although you are free to do something different (i.e., better) if you have time. Whatever strategy you use, remember that simple, correct, and working is better than clever, complex, and not done. You also will get more out of the project if you have simple implementations of most of the language rather than highly optimized implementations of a small part.
Code generation incorporates many more-or-less independent tasks. One of the first things to do is figure out what to implement first, what to put off, and how to test your code as you go along. The following sections outline one reasonable way to break the job down into smaller parts. We suggest that you tackle the job in roughly this order so you can implement the central part of the code generator first, and put off more peripheral topics until the core parts are done. Your experience implementing the first parts of the code generator should also give you insights that will ease implementation of the rest.
Implement code generation for arithmetic expressions involving integer constants, and the MiniJava "System.out.println" statement, plus the basic prologue and return statement code for the MiniJava main method. This will give you enough to compile and run main programs that print out the value of an integer expression.
Next, try implementing objects with methods, but without instance variables, method parameters, or local variables. This includes:
Once you've gotten this far, you should be able to run programs that create objects and call their methods. These methods can contain System.out.println statements to verify that objects are created and that evaluation and printing of arithmetic expressions works in this context.
Once you've gotten this far, you can add
This involves
Add the remaining code for classes that don't extend other classes, including calculating object sizes and assigning offsets to instance variables, and access to instance variables in expressions and as the targets of assignments. At this point, you should be able to compile and execute substantial programs.
The main issue here is generating the right object layouts and method tables for extended classes, including handling method overiding properly. Once you've done this, dynamic dispatching of method calls should work, and you will have almost all of MiniJava working.
We suggest you leave this until the end, since you can get everything else working without it.
Whatever is left, including any extensions you've added to the project.
As discussed in class, the easiest way to run the compiled x86 code is to call it from a trivial C program. That ensures that the stack is properly set up when the compiled code begins, and provides a convenient place to put other functions that provide an interface between the compiled code and the external world.
Feel free to embelish the sample bootstrap program presented in class as
you wish. In particular, you may find that it is sometimes easier
to have
your
compiler
generate code
that
calls
a
C runtime function to do something instead of generating the full sequence
of instructions directly in the .asm
file.
To execute the .asm file produced by your compiler, you will need to create a Visual Studio project with the C (not C++) main program similar to boot.c and the assembler code from the compiler. The resulting program can be run and debugged using Visual Studio.
The MASM assembler ml.exe
is included in Visual Studio .NET and
Visual Studio 2003. It is alleged to be available in Visual Studio
2005 Professional, but we haven't verified this yet. MASM can assemble 32-bit
code, which can
then be linked and executed with other programs,
in
particular
our
C main
program.
You may
find
it easiest to use the assembler from a command line, but it is also possible
to configure Visual Studio to use MASM
to assemble the .asm
file containing the compiled program. Here's
how (or at least, this has been known to work with
VS.NET).
.asm
file
generated by your compiler to the project. (You may have to change the type
of files
displayed in the dialog to ``all files'' to see the .asm
file.).asm
file.
Select Project>Settings. In the dialog box that appears, be sure that Win32
Debug is displayed in the Settings: field. Expand the file list if needed,
then select your .asm
file -- and only this
file. Click on the Custom Build tab.In the first line of the Build Command(s)
field, enter the MASM command to be used to assemble the file.
ml.exe /c /Cx /coff /Zi ${InputPath}
(The executable file name ml.exe
has a letter l
in it, not a digit 1
. The InputPath macro can be entered by
clicking on button Files and selecting Input Path in the menu that appears.)
Finally, you need to specify the output file name that MASM should use for
the assembled object code. In the Output File(s) field, enter filename.obj
,
where filename is the name of your assembly source file (without
the .asm
suffix).
You should now be able to compile, link, and execute your program with the
normal Visual Studio Build commands. Visual Studio will use MASM to assemble
the .asm
file as needed. You can use the symbolic debugger to step through the assembly
language code, set breakpoints in it, etc.
Your online turnin should include
Your group will meet with the course staff after the project is done to discuss it. This isn't a formal presentation (i.e., don't waste time preparing PowerPoint slides or anything like that). At, or before this meeting, you should hand in brief written report summarizing what your compiler does, what was implemented and what was omitted, any extra features you added, and, if you are working in a group, a summary of how the work was divided and who was responsible for what.
If you are working with others, you should turn in only one assignment per group, and all group members should attend the meeting.