COMP 3002 Assignment 3

For this assignment you will be continuing the implementation of the CMM to Jasmin compiler that can be downloaded here. The main file you will be editing is the CMMJasminVisitor.java file. Here is an example of how to compile the compiler, run the compiler, assemble the Jasmin code, and then run the resulting class file:
morin@miniscule:~/src/ssCC/examples/cmm-interpreter$ make
./sscc --prefix CMM cmm.t cmm.g
ssCC finished with no errors.
javac *.java
morin@miniscule:~/src/ssCC/examples/cmm-interpreter$ java CMMJasminVisitor simple.cmm 
Parsing...compiling...done
Output written to simple.j
morin@miniscule:~/src/ssCC/examples/cmm-interpreter$ jasmin simple.j 
morin@miniscule:~/src/ssCC/examples/cmm-interpreter$ java simple
2.0
3.0
5.0
-1.0
6.0
0.6666667
8.0
2.0
2.0
true
false
false
true
true
0.0
1.0
4.0
9.0
16.0
25.0
36.0
49.0
64.0
81.0
morin@miniscule:~/src/ssCC/examples/cmm-interpreter$ 
Most of the skeleton of the code generator is already in this file. You just have to finish it. In particular, take care of the following items:
  1. [5 marks] Currently, the language includes 3 built-in print functions for printing numbers, booleans, and strings. Add the following functions number cos(number), number sin(number), and string concat(string, string) that return the cosine, sine and concatenation, respectively, of their arguments.
  2. [5 marks] Code for generating if statements and do loops is still missing. Complete this code. Use the implementation of while loops as an example.
  3. [5 marks] The language's boolean negation operator (not) is not implemented. Implement it.
  4. [5 marks] Currently, variables are not initialized when they are declared. Change this so that numbers are initialized to 0, booleans are initialized to false and strings are initialized to the empty string "".
  5. [5 marks] Currently, the ^ operator denotes exponentiation of two numbers. Modify the code so that, if the arguments to ^ are both boolean, then it computes their exclusive-or.
  6. [5 marks] Currently, the + operator only works for numeric arguments. Modify this operator so that, if any of its arguments are strings, then they are converted to strings and then concatenated.
    5 + 2                   // 7
    5 + " little monkeys"   // "5 little monkeys"
    5 + false               // "5false"
    "Oceans " + 11          // "Oceans 11"
    
  7. [5 marks] Most assignments in this language result in a redundant dup/pop pair of instructions. Eliminate these.
      ;; code generated for i = 0
      ldc 0.0    ; [ 0.0 ]
      dup        ; [ 0.0 , 0.0 ] 
      fstore 1   ; [ 0.0 ] (i is now stored) 
      pop        ; [] 
    
  8. [5 marks] Currently, the way in which conditions involving numerical comparisons are evaluated is messy. First, the comparison is done and (through jump instructions) this leaves a boolean value on the top of the stack, and then this boolean value is used by (for example) a while loop to determine whether or not to jump.
      ;; code for while (i < n) { ... }
    label2:
      fload 1   ;i
      fload 0   ;n
      fcmpl
      iflt label4
      ldc 0
      goto label5
    label4:
      ldc 1
    label5:
      ifeq label3
      ...
      goto label2
    label3:
    
    Clean this up so that, if the Condition of a while loop is a comparison, the ifxx instruction is used to jump directly. The above code would then become:
    label2:
      fload 1   ;i
      fload 0   ;n
      fcmpl
      ifge label3
      ...
      goto label2
    label3:
    
  9. [5 marks] Currently, the compiler does nothing to ensure that functions return any value at all. Modify this so that the compiler generates default return values - 0 for numbers, false for booleans and the empty string for strings.
  10. [10 marks] Currently, the implementation is limited to 50 local variables per function and an operand stack of size 50. Get rid of this limit by making a first pass through the parse tree of each function definition and computing the maximum number of local variables in scope at any given time and computing the maximum number of values on the operand stack at any given time. Use these values in the .locals and .stack Jasmin directives.
Some examples of Jasmin code, including common constructs, are in the file SimpleTest.j.