Code Generation I (codegen1) Project
COP-3402
Table of Contents
1. Overview
In this project you will compile arithmetic and branching statements.
2. Preparing and submitting your project
2.1. git setup
Be sure to complete the vc exercise before attempting this project.
Create a new local repository, following the directions in the vc exercise (including the git --set-upstream last step) and set the local and remote repository URLs to be the following locations:
| Local repository | ~/cop3402fall25/codegen1 |
| Remote repository | gitolite3@eustis3.eecs.ucf.edu:cop3402/$USER/codegen1 |
Commands to setup your repo
These steps only setup the repo, do not submit your code, and assume you have already completed the vc exercise. Consult that exercise for specifics on validating each step and submitting your code.
mkdir -p ~/cop3402fall25/codegen1 cd ~/cop3402fall25/codegen1 git init echo "codegen1 project" > README.md git add README.md git commit README.md # Enter a commit message in the editor that pops up git remote add submission gitolite3@eustis3.eecs.ucf.edu:cop3402/$USER/codegen1 git push --set-upstream submission master
2.2. Downloading the project template
Download and untar the project template. The template has only been created and tested for use on eustis. You will create a new git repo and start it with a fresh copy of template for each separate codegen project.
cd ~/cop3402fall25/codegen1 wget https://www.cs.ucf.edu/~gazzillo/teaching/cop3402fall25/files/compiler-project.tar tar -xvf compiler-project.tar mv CodeGen.template.cpp CodeGen.cpp
Be sure that you include all files from the project template in your repo.
2.3. Building your project
The following will build your compiler (tested on eustis):
cd ~/cop3402fall25/codegen1 make
The result is a program called CodeGen.
Included are also a number of files prefixed with SimpleIR which includes the grammar (SimpleIR.g4) and generated parser files.
Again, be sure that you include all files from the project template in your repo so that it builds with make.
You can make a fresh clone of your repo to test that it builds properly.
You can sanity check your empty CodeGen template by giving it a SimpleIR program, e.g.,
./CodeGen << EOT function main return 0 end function EOT
You should see no errors.
2.4. Adding required project files
Add, commit, and push CodeGen.cpp along with all other files included in the project template. Don't forget to commit and push changes to CodeGen.cpp as you complete the project.
Here is a quick way to add all the required files:
git add CodeGen.cpp $(tar -tf compiler-project.tar | grep -v 'CodeGen.template.cpp')
Be sure to commit and push them as well.
2.5. Self-check
See the hello project for instructions on cloning a project from the grading server.
In brief, you can sanity check your repo on the grading server with the following:
git clone gitolite3@eustis3.eecs.ucf.edu:cop3402/$USER/codegen1 ~/tmp/codegen1_selfcheck cd ~/tmp/codegen1_selfcheck make ./CodeGen << EOT function main return 0 end function EOT
If any of these steps fail, then your project is likely not submitted correctly and will fail grading. Keep in mind that while necessary, passing the above steps is not sufficient for demonstrating a correctly-working compiler. See the descriptions of the test cases and grading criteria below for more information.
3. Project requirements
See the Code Generation Project Handbook for implementation details and examples.
In this project, you will be implementing the following listener methods for your compiler. These correspond to the constructs that generate function definitions and calls as well as listeners given to you to enable having a complete, testable assembly program.
| Listener | Instructions |
|---|---|
| enterUnit | Given to you |
| enterLocalVariables | Given to you |
| enterAssign | Given to you |
| enterFunction | Hard-coded |
| enterEnd | Hard-coded |
| enterParameters | Hard-coded |
| enterReturnStatement | Hard-coded |
| enterCall | Hard-coded |
| enterOperation | Implement on your own |
| enterLabel | Implement on your own |
| enterGoto | Implement on your own |
| enterIfGoto | Implement on your own |
4. Using your compiler
Be sure to be in your project directory,
cd ~/cop3402fall25/codegen1
and build after any change to the source code,
make
Compile your .ir file with your CodeGen program, saving the output to a .s file.
./CodeGen main.ir > main.s
Then use gcc to invoke the assembly and linker (it will see the .s and assume it's an assembly file):
gcc -o main main.s
Finally, you can run your program as usual:
./main
You can inspect the exit code:
echo $?
For the example above, this should return 0.
If you have multiple .ir files, compile them each separately
./CodeGen main.ir > main.s ./CodeGen paramtest.ir > paramtest.s
then use gcc to assemble and link them into a single executable:
gcc -o main main.s paramtest.s
Finally, you can run your program as usual:
./main
5. Grading
The test cases used to grade the project are listed below. Scripts to run the the odd-numbered test cases are provided publicly, while the even-numbered test cases are private.
To use the automated tests, first download them, e.g.,
cd ~/cop3402fall25/codegen1 wget https://www.cs.ucf.edu/~gazzillo/teaching/cop3402fall25/files/codegen1_public_tests.tar tar -xvf codegen1_public_tests.tar
To run a test case, e.g., tests/01.sh, use the following bash command from the root of your local repository directory:
bash codegen1-tests/01.sh $(realpath CodeGen) echo $?
This runs the bash script, passing in the absolute path to your program, which is what realpath finds for you. The result of echo $? should be 0 which indicates the test was succesful.
To see what commands the test case is running exactly, use bash -x instead of just bash:
bash -x codegen1-tests/01.sh $(realpath CodeGen) echo $?
Here is a bash for-loop that goes through all tests:
for i in codegen1-tests/*.sh; do # loop over all test scripts bash $i $(realpath CodeGen) # run the test echo $? # emit the exit code echo "" # give some extra space done # end the loop
5.1. Test cases
All test cases use this SimpleIR template. Be sure to follow the project requirements to hard-code the function-related listeners for this template.
function main # DO NOT CHANGE localVariables a b c d e f g result retval # DO NOT CHANGE # add your code to try out here retval := call print_int result # DO NOT CHANGE return 0 # DO NOT CHANGE end function # DO NOT CHANGE
| # | Short Name | Description |
|---|---|---|
| 1 | add-const | Test adding constants |
| 2 | sub-const | Test subtracting constants |
| 3 | mul-const | Test multiplying constants |
| 4 | div-const | Test dividing constants |
| 5 | mod-const | Test modulo of constants |
| 6 | sub-var | Test subtracting a variable and a constant |
| 7 | mul-var | Test multiplying a variable and a constant |
| 8 | mod-vars | Test modulo of two variables |
| 9 | sub-vars | Test subtracting two variables |
| 10 | all | Using all arithmetic operations to compute a value |
| 11 | goto | An unconditional branch |
| 12 | lt-true | Test branch on less than |
| 13 | lt-false | Test not branch on less than |
| 14 | gt-true | Test branch on greater than |
| 15 | eq-true | Test branch on equals |
| 16 | eq-false | Test not branch on equals |
| 17 | ne-true | Test branch on not equals |
| 18 | gte-false | Test not branch on greater than or equal |
| 19 | lte-true | Test branch on less than or equal |
| 20 | factorial | Test an implementation of factorial |
| 21 | combined | Test a combination of arithmetic and branching |
| 22 | exponent | Test an implementation of exponent |
6. Grading schema
| Criterion | Points |
|---|---|
| The git repo exists | 1 |
| The program is written, builds, and runs as described here | 1 |
| Tests pass, prorated by the number of test cases passing | 6 |
| TOTAL | 8 |