For our specific implementation:
\$a0 is the accumulator
\$sp is stack pointer
top of stack (unallocated) is \$sp + 4
We define code generation cgen()
: For expression e, cgen(e)
produce code for e such that the code will evaluate e and preserve the stack after evaluation.
For constant i, cgen(i) = li $a0 i
cgen(e1 + e2) =
cgen(e1)
sw $a0 0($sp)
addiu $sp $sp -4
cgen(e2)
lw $t1 4($sp)
add $a0 $t1 $a0
addiu $sp $sp 4
Good for stack machine:
we only need to store function parameter in activation record
no need for control link since $sp
is the same when function returns
we do need return address
$fp
frame pointer that point to current activation may be useful
Calling Sequence: instructions to set up a function activation
jal label
: jump to label, save address of next instruction in $ra
(other architecture return address is stored by call
instruction)
jr reg
: jump to address in register
Code for variable reference is generated using relative position to the frame pointer: cgen(x_i) = lw $a0 z($fp)
where z = 4 \times i (z is computed at compile time) for ith variable.
For production compiler:
more emphasis on keeping values on register
intermediate results are stored in AR (that means we reserve intermediate result location on stack at compile time instead of dynamically grow the stack) this is to avoid changing $sp
.
In general, let NT(e)
be a function that calculate the number of temporary location we need to reserve in AR, then, NT(e1 + e2) = max(NT(e1), NT(e2) + 1)
Now activation record stores: - return address - frame pointer - n arguments - NT(e) many temporaries
Field: object's attribute
Our goal:
class fields can be inherited can referenced by any inherited class (location of fields should be unchanged)
class methods should be overwritten or inherited
Layout: in contiguous memory
class tag: identify class of the object
object size: size of object in words
dispatch pointer: pointer to table of methods (Object Table)
attributes: field values
In this structure, if a child object want to add attribute, it can just change object size
and then append additional attributes
to attributes
(and change class tag
)
Object Table: a global table to map each object type to their the method address that object can invoke. It is global because the number of method for a class does not change.
Table of Content