Run a Cool program that prints out the results of testing 2+2=4,
2+2==4, and 4==4.
What do you get? Explain your results.
(You may need to look at the generated assembly code.)
When you generate code in PA7 for Cool, you are going to generate code
following templates for each type of expression. That is, each
expression will
have some standard pattern of code with ``holes'' for the code
generated by its sub-expressions. Each expression's code must have
its value in $a0 after the last instruction of that code.
Each template assumes that the sub-expressions' code follows this
rule.
You will not generate the assembly code for these templates until PA7, but for now, think about what the templates should be. Write the MIPS assembly code that should be generated as the template for each of the following expressions: dispatch (with two actuals), sub, leq, neg, new_, loop, let, null, string_const, block (with three sub-expressions), branch, and typcase (with three branches). The template for a cond is shown below to help you start.
TEMPLATE(cond) = [
[code for pred]
# we know that the object in $a0 must be a Boolean Object
lw $t1 12($a0) #load truth value from Boolean result into $t1
beqz $t1 labelx # if false, goto labelx
[code for then_exp]
# $a0 has the result from the then branch
b labely
labelx:
[code for else_exp]
# $a0 has the result from the else branch
labely: # Now $a0 will have the result from either branch
]
Some of your templates will need to use a temporary in the stack
frame. Use the notation T($fp) to refer to the temporary that
will be allocated for the template in question.