Module 5:
A Run-Time System for Klein

Stage 5 of the Klein compiler project
Out: Friday, October 31
Due: Friday, November 14, at 11:59 PM
STATUS CHECK DUE: Friday, November 7

Tasks

This stage consists of the fourth component of your Klein compiler, supporting documentation, and one auxiliary program.

To complete this project, you need to be able to execute TM programs. If you have not already downloaded the TM virtual machine and run it, see below for details on downloading the VM and using it for the project.

1. A Run-Time System

Your primary task is:

Write a minimal run-time environment and code generator for Klein programs.

A code generator is a function or object that takes as input:

  • the abstract syntax tree of a semantically-correct Klein program, and
  • its associated symbol table.

It produces as output an equivalent TM program.

For this assignment, your code generator is required to correctly handle the abstract syntax tree of a single Klein program, print-one.kln:

function main() : integer
    print(1)
    1

To be clear, please note:

  • The code generator takes as input the outputs produced by the semantic checker.
  • It produces a run-time system that calls the program's main() function, prints its value, and halts.
  • It produces code for the primitive function print() as a part of the run-time system.
  • It produces code for the only user-defined function in the program, main().
  • The body of main() calls print before returning its value.

Even though this main program takes no arguments and the print function always takes one argument, your code generator should look up the number of arguments for each function (in the symbol table or in the AST) and use that value when generating the code for a function call, including creation of the stack frame and computing locations within it.

2. Design Documentation

Document the design of your run-time environment using text and diagrams of memory organization.

Create four diagrams:
  • one that shows the layout of your stack frames
  • one that shows the layout of DMEM, including where the run-time stack begins and how it grows
  • one that shows the layout of IMEM, including where the run-time system begins and where the code for the main program and the print function are placed
  • one that shows how the eight registers are used by the run-time system /li>

Use text to explain how memory is allocated and initialized by the code generator. Give as much detail as possible about how the compiler can compute offsets within and across stack frames.

You may base your run-time system on the material discussed in class, which is based in part on a generic high-level design on Pages 347-348 in Louden's textbook, Compiler Construction. A handout is available.

You should think about and design your run-time system before writing your code generator. You may, of course, adjust your documentation to reflect the decisions you make while implementing the program.

3. The kleinc Command

Finally, in order to use your client program as a tool,

Create a Unix command-line script named kleinc that compiles a Klein program into a TM program.

kleinc should take the name of a Klein source program as its argument, run your scanner, parser, semantic checker, and code generator on it, and produce a TM program of the same name.

For example, compiling the source file print-one.kln will produce an output file named print-one.tm.

For this project, I will test your code generator with only one input program, print-one.kln. It does not have to work correctly for any other Klein program. The goal of the assignment is to implement the basic run-time system, including the primitive print() function, and be able to generate simple function calls. The only kind of expression present in this program is an integer literal. (You may, of course, implement more functionality if you want. You will be asked to implement the rest of the functionality for Module 6.)

This is the fifth in a series of tools that makes up the command-line suite of your Klein compiler.

Extra Credit Opportunity

Implement kleinc so that the .kln extension is optional. If we call kleinc with a filename that does not contain the .kln extension, such as print-one, it will look for a file with the extension before proceeding.

Deliverables

As before, submit only one copy of each assignment per team. The team captain or a designated team member can be responsible for the submission.

Status Check

For this project, you may determine as a team what you would like to submit as mid-project to demonstrate progress. In order to stay on track to complete the project on time, your status check deliverable should probably consist of a combination of design documentation and code.

Submit:

  • items of your choosing
  • any questions you have

Make any design documentation you submit a part of the ongoing documentation of your compiler.

Final Deliverable

By the due time and date, use the course submission system to submit your project directory electronically as a zip file named project05.zip or project05.tar.gz.

A Refresher on the TM Simulator

The TM virtual machine is a self-contained C program named tm[...].c. You can download several versions of the virtual machines and two sample TM programs as a zip file or as individual files from this directory.

This directory contains three extensions of Louden's original TM simulator. You can read about them in the README file. For our purposes, the most important of these is tm-cli.c.

Louden's original TM simulator does not take command-line arguments other than the name of the TM source file. This is a problem for us, because the Klein language specification says:

Users may provide arguments to main on the command line.

Thanks to Mike Volz, CS 4550 Class of 2007, we have an extended TM simulator that supports command-line arguments. For example:

> tm factorial-cli.tm 10
TM simulation (enter h for help)...
Enter command: g
OUT instruction prints: 3628800
HALT: 0,0,0
Halted
Enter command: q
Simulation done.

If the user provides n command-line arguments after the filename, the simulator stores them at the top of the TM's data memory, in slots DMEM[1] through DMEM[n]. A TM assembly language program can access them directly from those slots.

Your code generator should generate such code for the arguments passed to the main function.