TinyJ Assignment 2

Due Date: Thursday, December 11, 2025

Try to finish by: Tuesday, December 9 (to have more time for Assignment 3 and exam preparation)

Prerequisite: Complete TinyJ Assignment 1 first

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:

  1. Check that declarations and uses of identifiers are consistent with Java’s scope rules
  2. Translate the source file into a sequence of instructions for a stack-based virtual machine
  3. Write an “enhanced parse tree” showing static addresses, stackframe offsets, method code start addresses, and instruction generation times
  4. 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

  1. Login to your xxxxx_yyyy316 mars account and enter /home/faculty/ykong/TJ2setup at the xxxxx_yyyy316@mars:~$ prompt
  2. 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:

  1. Open a PowerShell (PC) or Terminal (Mac) window
  2. Change to your working directory:
    cd ~/316java
    
  3. Download the file TJasn.jar from your mars home directory to ~/316java on your PC/Mac using scp or sftp
  4. Extract and compile the files:
    jar xvf TJasn.jar
    javac -cp . TJasn/TJ.java
    javac -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:

  1. Copy code you wrote for Parser.java in TinyJ Assignment 1
  2. 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:

To have a good chance of finishing before the deadline:

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.

How to Test Your Solution

First, recompile your program:

javac -cp . TJasn/ParserAndTranslator.java

Then run your program on a TinyJ source file:

java -cp . TJasn.TJ TinyJ-source-file-name output-file-name

Example:

java -cp . TJasn.TJ CS316ex12.java 12.out

Working directory assumptions:

Debugging Stop Prompts

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:

On mars or Mac:

java -cp TJsolclasses:. TJasn.TJ TinyJ-source-file-name output-file-name
java -cp TJsolclasses:. TJasn.TJ CS316ex12.java 12.sol

On PC (PowerShell):

java -cp "TJsolclasses;." TJasn.TJ TinyJ-source-file-name output-file-name
java -cp "TJsolclasses;." TJasn.TJ CS316ex12.java 12.sol

Validation Criteria

For correct TinyJ programs:

For each test file CS316ex{k}.java ({k} = 0 to 15):

  1. Run your program: java -cp . TJasn.TJ CS316ex{k}.java {k}.out
  2. Run the instructor’s solution: java -cp TJsolclasses:. TJasn.TJ CS316ex{k}.java {k}.sol
  3. 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

  1. 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)

  2. Leave your completed ParserAndTranslator.java in the TJasn directory of your xxxxx_yyyy316 mars account

  3. 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)

  4. 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:

  1. Open a PowerShell/Terminal window on your PC/Mac
  2. Change to your working directory:
    cd ~/316java
    
  3. Copy the file to mars (requires connection to qwifi-secured network or QC VPN):
    scp TJasn/ParserAndTranslator.java xxxxx_yyyy316@mars.cs.qc.cuny.edu:TJasn
    

    Where xxxxx_yyyy316 is your mars account username

You will be prompted to enter your mars account password.

IMPORTANT: After copying, you must still do steps 3 and 4 of the submission steps above. The assignment won’t be submitted until you do step 4!

Verifying Successful Submission

See the 1st-day-announcements document on Brightspace for information on how to verify that your assignment has been submitted successfully.