Due Date: Friday, April 5 at 11:59pm via GitHub.

Introduction

In this assignment your team will be completing the backend of your Source language compiler by implementing a code generator that targets the CSC488 Machine.

Getting started

There is no new starter code for Assignment 5. Copy the contents of your A3 assignment submission with the A4 starter code into a new a5 directory. The A4 starter code package is disjoint from previous packages, so it should not accidentally clobber any of your teams' work when you combine them.

  1. Create a new directory csc488/codegen, and put an empty file __init__.py there.
  2. Create a new class CodeGen in csc488/codegen/codegen.py, similar to the SemanticAnalyzer in csc488/semantics/analyzer.py. This class should have an entry point that takes a Program AST as input (plus anything else you deem important), and returns a Python list of AssemblyCode such as Instrns, Labels, etc. (see csc488/machine/defs.py from A4)
  3. Modify the sc tool (under csc488/tool/sc/main.py) to add a code generation step just after successful semantic analysis (at the # Backend... comment). Duplicate what csc488/tool/mas/main.py does from about line 316 onwards (that is, instantiate a Machine, assemble the code into it, and then run it.)

Inside of the CodeGen class, implement your code generation strategy. If you choose to use the suggested visitor pattern approach, this will take the form of a series of AST node type visit methods that will work together to generate Machine instructions for each aspect of the language.

The net effect of running sc on a semantically valid .488 program source file should be to compile and execute it.

Suggestions

Testing

Implement and document a strategy for testing your code generation. Whereas earlier assignments focused on testing for correct values and the structure of certain outputs, for this assignment consider testing your code generation in terms of the correct effect of actually running the resultant code.

For example, a good series of functional tests would take the form:

Consider how you can wire up all the phases of your compiler in order to write this kind of test. The tests in csc488/machine/tests/ demonstrate what is required in terms of providing input and capturing output from the Machine towards accomplish this.

How to submit

Copy your a3 directory and the starter code in the a4 directory (from the a4starter branch) into in a final a5 directory.

Implement code generation, along with any associated tests.

You submission must run correctly on teach.cs.toronto.edu. Failure to correctly run in that environment may result in an overall assignment mark of 0.

Modify a5/README.md by adding the following:

Push your commits to the master branch of your repository.

Submission checklist