Note: If mars fails to operate normally or becomes inaccessible at any time after 6 p.m. on the due date, the deadline will not be extended. Try to submit no later than noon on that day, and on an earlier day if possible.
This assignment counts 1.5% towards your grade if computed using rule A.
Assignment
Your assignment is to complete a compiler that does the following whenever its input is a syntactically valid TinyJ source file:
Check that declarations and uses of identifiers are consistent with Java’s scope rules
Translate the source file into a sequence of instructions for a stack-based virtual machine
Write an “enhanced parse tree” showing static addresses, stackframe offsets, method code start addresses, and instruction generation times
Write a list of the instructions generated to the output file
Virtual Machine Instructions
The 35 virtual machine instructions that may be generated by the compiler are shown below. A specification of their effects is given in the PDF document Memory-allocation-VM-instruction-set-and-hints-for-asn-2 (posted to Brightspace):
Operation
Operand 1
Operand 2
STOP
PUSHNUM
<integer>
PUSHSTATADDR
<address of static variable>
PUSHLOCADDR
<local offset or parameter's stackframe variable>
LOADFROMADDR
SAVETOADDR
WRITELNOP
WRITEINT
WRITESTRING
<address char first of>
<address char last of>
READINT
CHANGESIGN
NOT
ADD
SUB
MUL
DIV
MOD
AND
OR
EQ
LT
GT
NE
GE
LE
JUMP
<address of target instruction>
JUMPONFALSE
<address of target instruction>
CALLSTATMETHOD
<address of first instruction of method's body>
INITSTKFRM
<no. of local vars declared in method>
RETURN
<no. of parameters the method has>
HEAPALLOC
ADDTOPTR
PASSPARAM
DISCARDVALUE
NOP
Installation Before You Start
After completing TinyJ Assignment 1, do the following:
On mars
Login to your xxxxx_yyyy316 mars account and enter /home/faculty/ykong/TJ2setup at the xxxxx_yyyy316@mars:~$ prompt
Wait for TJ2setup done to appear on the screen (no error messages should appear)
On your PC or Mac (if doing the assignment locally)
If you are doing or have already done TinyJ Assignment 1 on your PC/Mac and plan to do this assignment on the same machine:
Open a PowerShell (PC) or Terminal (Mac) window
Change to your working directory:
cd ~/316java
Download the file TJasn.jar from your mars home directory to ~/316java on your PC/Mac using scp or sftp
Extract and compile the files:
jar xvf TJasn.jarjavac -cp . TJasn/TJ.javajavac -cp . TJasn/virtualMachine/*.java
How to Do This Assignment
The only file you need to change is TJasn/ParserAndTranslator.java.
In this file, each /* ???????? */ comment must be replaced with appropriate code. For most of these comments (specifically, lines 511-723), a good way to start is:
Copy code you wrote for Parser.java in TinyJ Assignment 1
Make changes so appropriate TinyJ virtual machine instructions will be generated
Important: For the comment on line 511, only copy 3 statements. Don’t copy the lines from Parser.java corresponding to:
Lines 482-3, 500, 502-4, 507, and 513
Lines 515, 519, and 537-8
Recommended Completion Schedule
To have a good chance of finishing before the deadline:
Immediately after completing Assignment 1: Copy code from corresponding parts of Parser.java into the /* ???????? */ comments on lines 511-723
No later than Friday, December 5: Fill in the /* ???????? */ gaps on lines 638-682
Before the deadline: Fill in remaining gaps on lines 492, 495, 511, 549, 593, 610, 627, and 723
Some helpful hints are given in the PDF document Memory-allocation-VM-instruction-set-and-hints-for-asn-2 (posted to Brightspace).
Compiling Your Changes
After editing TJasn/ParserAndTranslator.java, recompile it:
javac -cp . TJasn/ParserAndTranslator.java
On mars, this assumes your working directory is your home directory. On your PC/Mac, this assumes your working directory is ~/316java.
When you are asked these questions, respond as follows:
Want debugging stop or post-execution dump? (y/n)
→ enter: y
Enter MINIMUM no. of instructions to execute before debugging stop.
(Enter -1 to get a post-execution dump but no debugging stop.):
→ enter: 0
Stop after executing what instruction? (e.g., PUSHNUM)
(Enter * to stop after executing just 0 instructions.):
→ enter: *
Testing Against the Solution
You can run the instructor’s solution to Assignment 2 similarly:
The output files from your solution and the instructor’s solution must contain the same “Instructions Generated:” lists (found near the end, just above the “Data memory dump”)
The files need not be identical otherwise
For each test file CS316ex{k}.java ({k} = 0 to 15):
Run your program: java -cp . TJasn.TJ CS316ex{k}.java {k}.out
Run the instructor’s solution: java -cp TJsolclasses:. TJasn.TJ CS316ex{k}.java {k}.sol
Compare the output files using:
mars/Mac:diff -c {k}.out {k}.sol
PC:fc.exe /n {k}.out {k}.sol
Your solution is correct even if files differ, provided the “Instructions Generated:” lists match.
Submission Instructions
This assignment is to be submitted no later than Thursday, December 11, 2025. If mars becomes inaccessible after 6 p.m. on the due date, the deadline will not be extended. Try to submit no later than noon on that day, and on an earlier day if possible.
Submission Steps
Add a comment at the beginning of your completed ParserAndTranslator.java that gives your name and the names of any students you worked with (you may work with up to two other students)
Leave your completed ParserAndTranslator.java in the TJasn directory of your xxxxx_yyyy316 mars account
Verify your submission by entering at the xxxxx_yyyy316@mars:~$ prompt:
less TJasn/ParserAndTranslator.java
Check that it displays the beginning of your file (including the comment you added)
Enter the submission command at the xxxxx_yyyy316@mars:~$ prompt:
submit_TJ_asn2
Important: If your submitted version cannot be compiled without error on mars, you will receive no credit.
Copying from PC/Mac to mars
If you did this assignment on your PC/Mac, copy TJasn/ParserAndTranslator.java to mars using these steps:
Open a PowerShell/Terminal window on your PC/Mac
Change to your working directory:
cd ~/316java
Copy the file to mars (requires connection to qwifi-secured network or QC VPN):