Project 4: Code Generation Specification

Due: Thursday June 12th 23:59(=11:59pm) sharp. No extensions.

In this project, you are to write the compiler that converts src3 programs into assemblys code for JVM.


General Specification

Your program should be named parse.

It should read src3 source from standard input and output Jasmin assembly code to standard output.

When I run jasmin on the generated assembly code, the class file generated should be in the current directory. The class name should match the src3 program name. You are allowed to include some other Java class files in your turn in tar file as external run time libraries necessary for running the compiled code, though the Java source needs to be included. You can share the external run time libraries but must document where it came from if the code is borrowed.

src3 test programs will not contain lexical, syntactical, or semantic errors, although some run time warnings/errors are to be expected. If an src3 test program requires certain input from console, then assume the input is valid. I will try not to give out test cases that may raise confusions. If you think that there is a confusion point, send me an email/post it on the board so that it will be either clarified or not given in the grading tests.

Except passing values by reference, all features of src3 language need to be implemented. Implementing passing by reference will earn you 10 extra points.

Be sure to implement the src3 write statement correctly since it is the only way for me to figure out if your code actually worked or not.


Data Types

SRC3 Type Equivalent Java Type Default Value
bool boolean false
int int 0
real double 0.0
string String empty string
N-dimensional array of base type N/A N-dimensional array of default values of the base type

Since some variables can be used before they get initiated, we assume that they were assigned to their default value of the corresponding type. For example:

test1;
begin
  var a : int
  a := a + 3;
  write a;
end;
end test1

The expected output is

3

Array Dimensions

For an array that was defined as array of [a,b,c] type, it is equivalent to Java's type[a][b][c]. If an array is declared to have N dimensions, I will not give out test cases that contain number of indices less than N dimensions. That is, I will not give any cases of type[i] or type[i,j] if the array is declared as array of [a,b,c] type.


read/write Statements

For the read statement, three types of variables needs to be implemented. They are INT, REAL, and STRING. The input is from console. The lexical format is similar to C's scanf function's "%d", "%lf" and "%s" respectively. Which means, you read one WORD at a time for string. You may wish to reference this java code. You may safely assume that all inputs are valid (e.g. if 3 reals are expected, then 3 reals will be provided in the test input).

For the write statement, the output for INT, REAL, and STRING are the same as Java's System.out.print output. For boolean type, they are T and F for true and false values (described in the semantic spec). For arrays, the output is a comma separated list of elements surrounded by square brackets. For example: an array of 3 integers from 0 to 2 is [0,1,2]. There are no spaces before and after commas. There are no writeln statements. The write statement emits a new line after it finishes. Please see the examples.


Return values from a subroutine

All function returns are by value. For example:

foo (var a : array [5] of int) : array [5] of int
begin
	return a;
end;
end foo

In this case, a was passed by reference to foo. When the function returns, it returns a copy of a, rather than a itself since that would be return by reference.

If you are not going to implement passing by reference as extra credit, it is not necessary to clone the object on return. However, it will be necessary to clone the array when trying to pass the array by value to another function.

Hint: do a google search on java object cloning. Have a function that does object cloning in an external Java run time library and simply calls that function to do the job.


Run Time Warnings/Errors

The following warnings/errors are not produced at the compile time. You are to expect some tests that generate these problems at run time.

No Return Values

Since it is possible for src3 programmers to "forget" to return a value at end of a function, it is therefore necessary to come up a solution in that case. You should

  1. Print a warning statement. The message is in the following format
    WARNING:\tNo return values specified in function.
    Replace function with the appropriate function name.
  2. Return a default value for that function.

Array Index out of Bound

Programmers often make this type of mistakes, which often result in crashes/core dumps in some compiled programs. In this project, you need to catch this type of error and exit gracefully after printing the following message.

ERROR:\tArray index out of bound at line #.

The line number is the starting line number for the array variable.

Hint: Either insert some JVM codes before the access of an array element to check the range of the index, or call an internal function which is responsible for index range checking and obtaining the array element.


Grading

I will use a script to test your program. If I use your program to compile src3 program and it runs and generates the desired output, test is passed. If for any reason, Jasmin or Java complained, or the output did not match mine, it would be a failure. Given an input of a src3 program

test2;
begin
	write "hello world!";
end;
end test2

My script would be similar to the following

./parse < test2.src3 > tmp.j	# obtain assembly code from you
jasmin tmp.j			# I expect test2.class will be generated in the current directory
java -cp . test2 > output	# try to run the code and generate output
diff output test2.output	# compare the difference between yours and mine

Of course, the script here is simplified a bit for the purpose of illustrating how the grading tests are conducted. The actual script would detect errors occurred at all stages. Only tests with correct outputs w/o errors in any of the stages would be counted.

Given that scripts are used to test your program, be sure to strictly follow the specification. As mentioned above, ask if you think that some things need to be clarified. Partial credits is probably not possible for individual test cases. If you finished early, you may be able to earn extra credits by notifying me such that your program may be used to generate more test cases that can be helpful to other students.


What to turn in