Assignment 4

CS 550 Programming Languages
Instructor: Jeremy Johnson 
Due: Thursday May 16 by 9am

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 on mini language compiler. Note that you must first extend the RAM simulator to an indirect jump (JMI) instruction. You should follow the approach outlined in the lecture on the Mini Language Compiler. 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 Mini Language Lecture Part III. 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 the Mini Language Compiler Lecture.

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 on the Mini Language and Mini Language Compiler.

What to do

Modify the mini language interpreter from the Mini Language Lecture Part III (the one with procedures) to provide
  1. an updated symbol table that stores information needed to generate code for the procedure calling convention outlined in the Mini Language Compiler Lecture
  2. 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).
  3. 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
  4. Modify the RAM simulator to support an indirect jump (JMI) instruction.
  5. a compile method which calls translate, and then calls link.
  6. 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.
  7. 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 via Blackboard Vista. Only one submission is required per group. The group leader for the assignment should submit the assignment.

The README file should describe all files that are included, contain instructions how to build [you should use make or Ant to build you program and make sure it successfully builds and runs on the CS compute server tux] 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).

Okay, memory size. I will set an environment var called RAM_MEM before running your run target . It is quite simple to read an environment variable in a makefile:

foo :
	echo $$RAM_MEM

You can test it like this:

$ RAM_MEM=300 make foo

, or, if you add it to your environment, remember to export it. Please don't set it in your makefile.

Since you can do this in a number of languages, submit a makefile or Ant file. Here is a sample makefile. Please ask for help w/makefiles.

Submit the following files to Bb Vista:

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.

Note: Please pay attention to file names. E.g., makefile is not Makefile, and README is not README.txt.