Assignment 7
CS 550 Programming Languages
Instructor: Jeremy Johnson
Due date: Wed. June 3 at 11:59pm
Mini Language Compiler (version 2) (100 points)
In this assignment you will implement a compiler for version 2 of the
mini language
(i.e. the version with procedures). The target langauge for your compiler
will be the RAL instruction set, and you will be able to run your compiled programs
on the RAM simulator from lecture 5.
Note that you must first extend the RAM simulator to an indirect
jump (JMI) instruction.
You should follow the approach outlined
in lecture 5.
In this version, unlike the version in assignment 5 you
will have to use a runtime call stack to store activation records which store
parameters, local variables, temporary variables, etc. for procedures.
You may reuse the parser
and data structure constructed for the mini language interpreter from
lecture 2c. You can simply replace the eval
method by a translate method, which should construct a list (or vector) of
symbolic RAL instructions. Each of the different classes that define the program
data structure (e.g. Expr, StmtList, AssignStmtn, IfStmt, WhileStmt, Proc, FunCall) will need a translate
method. These methods are implemented using the approach outlined in
lecture 5. You may want
You will also need to modify the symbol table to store information such as the address
in memory that a variable or constant is stored, the type of entry in the symbol table
(e.g. constant, variable, temporary). Note that the address field may be "unknown".
You will also need to implement a link method which converts the symbolic RAL instructions
to absolute RAL instructions (i.e. hard coded addresses). This may be done after the
program is translated and all necessary information is in the symbol table (i.e. the
number of constants and their values, and the number of parameters, program variables,
temporary variables, and the number of instructions for each procedure that was
defined with a Define Statement). Since there are possibly multiple procedures you
will have to decide how to order the code for the different procedures. It is beneficial
to treat the main program as a procedure that is called first when the program is executed.
You will need to know the number of instructions, parameters, local variables, temporary
variables when generating the code for function calls. Finally, you will need an indirect
jump (JMI) instruction to jump to the return address (i.e. the instruction to execute
after returning form a function call) which is stored in the activation record for the called
procedure. If you want to you may introduce a call instruction (perhaps a psuedo instruction,
i.e. one that gets mapped to a sequence of real instructions) that does the jump to the
procedure call and possibly some other actions such as storing the return address in the
activation record (you could compute this in terms of the PC).
Finally, a compile method simply calls the translate and link methods.
After compiling you can dump the RAL program to a file (or standard out) and then you
may execute it on the RAM simulator.
Background Information
Background information is available in lectures
5 and 2.
What to do
Modify the mini language interpreter from
Lecture 2c (the one with procedures) to provide
- an updated symbol table that stores information needed to generate code for the
procedure calling convention outlined in lecture 5
- a translate method that produces symbolic RAL instructions to exectute the
programs on the RAM simulator that use procedures (you will need to support
a runtime call stack with activation records for each procedure call).
- a link method that produces absolute (i.e. hard coded addresses) code,
corresponding to the translated symbolic RAL code, that can be simulated
on the RAM simulator
- Modify the RAM simulator to support an indirect jump (JMI) instruction.
- a compile method which calls translate, and then calls link.
- You may need to modify the RAM load and init methods so that the PC is initialized
to the first address of main. This depends on how you load the procedures.
- an output method that dumps the compiled RAL program.
You must test your compiler on several mini language programs which define
and use procedures.
How to submit
Students should submit their solution electronically using webct.
Only one submission is required per group. The group leader for the
assignment should submit the assignment.
Submit a gzipped tar file, called A6.tar.gz (the tar file should contain
a directory called A6 which contains the files). The tar file should
contain source code, instructions how to run your programs, sample input
and output files, and a README file. You should provide your modified RAM simulator
that you used to execute your compiled programs.
The README file should describe all
files that are included, contain instructions how to build and use the
code, and outline how the code works. The README file should also contain
a list of all group members and the group leader for the submitted assignment.
You should also indicate how you
tested your code. If your program is not working, you
should clearly state this in the README file. Code should be
documented (clear specifications and comments for any tricky parts of the code).