Due Date: Friday, April 5 at 11:59pm via GitHub.
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.
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.
csc488/codegen
, and put an empty file __init__.py
there.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 Instrn
s, Label
s, etc. (see csc488/machine/defs.py
from A4)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.
csc488/machine/tests/test_machine_core.py
and csc488/machine/tests/test_codegen_ideas.py
, which contain extensive examples and tips on how to programmatically generate code for the Machine. Remember, prior to assembly the code is simply lists of instructions, so think in terms of creating helper functions and methods for yourself.Label
s into the AST. They will help you as you go. Just don't forget to eventually include them in the code list, so that their address can be resolved.print
statementprint 1
, print 1 + 2
, print 1 < 2
, print (1 if true else 2)
, etc.and
/or
conditionalsif
statementsbreak
statements0
input
statementsImplement 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:
print 488
should output 488
var x integer x = 2019 print x, " ", x + 1
should output 2019 2020
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.
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.
teach.cs.toronto.edu
a5/csc488/codegen/codegen.py
a5/examples/
which your compiler is able to runa5/csc488/codegen/tests/
), and remember, automated testing is your friend and ally