UP | HOME

Code Generation
Compiler Project
COP-5621

Table of Contents

1. Overview

In this project, you will implement the code generation phase of a compiler that translates While code into SimpleIR code. Use the given semantics to guide the implementation of the translation, which specifies the meaning of each While construct as a sequence of SimpleIR instructions.

Be sure to test your program amply so that visitors for all constructs are working correctly. Create your own test cases and submit at least 10 of them with your code to your submission repo.

2. Compiler definition

This semantics defines the translation rules for the compiler:

semantics/semantics.pdf

Here are two example while programs, their parse trees, and the generated IR code:

  1. board/codegen_III.pdf
  2. board/codegen_IV.pdf

3. Setup instructions

3.1. Get the code generation template

cd ~/cop5621spring25/compiler/
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/IRGen.py > compiler/IRGen.py

3.2. Get the SimpleIR interpreter

cd ~/cop5621spring25/compiler
wget https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/simpleir.tar
tar -xvf simpleir.tar
make -C simpleir

3.3. Running the code generator

cd ~/cop5621spring25/compiler
wget https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/assign.while
python3 compiler/IRGen.py < assign.while

You should see this as your output (with the orignal code template)

function main
# visitNum
_t1 := 10
x := _t1
# visitVar
_t2 := x
y := _t2
return 0
end function

3.4. Running the SimpleIR interpreter

First generate IR with your compiler:

python3 compiler/IRGen.py < assign.while > assign.ir

Then use SimpleIR's interpreter to run the IR program:

python3 simpleir/Interpreter.py assign.ir

The following should be the output:

DEBUG:root:running main
DEBUG:root:memory:
M(0): 10 (_t1)
M(1): 10 (x)
M(2): 10 (_t2)
M(3): 10 (y)
DEBUG:root:return value: 0

This shows the state of each memory location, e.g., M(0), including its number value and the variable name associated with this memory location.

You can also pipe the While compiler's output to the IR interpreter:

python3 compiler/IRGen.py < assign.while | python3 simpleir/Interpreter.py

Author: Paul Gazzillo

Created: 2025-03-04 Tue 06:17

Validate