UP | HOME

Code Generation I (codegen1) Project
COP-3402

Table of Contents

1. Overview

In this project you will compile function definitions and calls to x86 assembly.

2. Preparing and submitting your project

2.1. git setup

Be sure to complete the git exercise before attempting this project.

Create a new local repository, following the directions in the git exercise (including the git --set-upstream last step) and set the local and remote repository URLs to be the following locations:

Local repository ~/cop3402spring25/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 git exercise. Consult that exercise for specifics on validating each step and submitting your code.

mkdir -p ~/cop3402spring25/codegen1
cd ~/cop3402spring25/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 ~/cop3402spring25/codegen1
wget https://www.cs.ucf.edu/~gazzillo/teaching/cop3402spring25/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 ~/cop3402spring25/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 output or 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.

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 three 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 Implement on your own
enterEnd Implement on your own
enterParameters Implement on your own
enterReturnStatement Implement on your own
enterCall Implement on your own

4. Using your compiler

Be sure to be in your project directory,

cd ~/cop3402spring25/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

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.

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 ~/cop3402spring25/codegen1
wget https://www.cs.ucf.edu/~gazzillo/teaching/cop3402spring25/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

Test cases

# Short Name Description Expected Output
1 function-return-0 A function that returns 0. It exits with 0.
2 function-return-nonzero A function that returns a non-zero value. It exits with a non-zero value.
3 function-return-var A function that returns the value of a variable. It exits with the variable's value.
4 function-return-var-copy A function that assigns one variable to another and returns its value. It exits with the variable's value.
5 function-call-return-0 A function that calls another and returns that function's 0 return value. It exits with 0
6 function-call-return-nonzero A function that calls another and returns that function's non-zero return value. It exits with a non-zero value.
7 function-call-return-var A function that calls another and returns that function's variable return value. It exits with the variable's value
8 function-call-one-param-return A function that calls another and returns that function's parameter. It exits with the parameter's values.
9 function-call-two-params-return A function that calls another and returns one of that function's parameters. It exits with one of the parameters' values.
10 function-call-two-params-var-return A function that calls another and returns one of that function's parameters via a variable assignment. It exits with one of the parameters' values.
11 function-call-six-params-return A function that calls another and returns one of that function's parameter. It exits with one of the parameters' values.
12 function-call-six-params-var-return A function that calls another and returns one of that function's parameters via a variable assignment. It exits with one of the parameters' values.
13 function-call-seven-params-register-return A function that calls another and returns one of that function's register parameters. It exits with one of the parameters' values.
14 function-call-seven-params-stack-return A function that calls another and returns one of that function's stack parameters. It exits with one of the parameters' values.
15 function-call-eight-params-stack-var-return A function that calls another and returns one of that function's stack parameters via a variable assignment. It exits with one of the parameters' values.
16 function-call-nine-params-stack-var-assign-return A function that calls another and returns one of that function's stack parameters via two variable assignments. It exits with one of the parameters' values.

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

Author: Paul Gazzillo

Created: 2025-04-02 Wed 07:43

Validate