test repository contains test code
- Evaluation by register and stack - done
- Optimization to detect which register cost - not started
The states of registers for temporary values are no longer important in the following situation.
- Between instructions.
- Between each argument of a function call.
The call to tr_expr
means to evaluate the expresion begining from the first register. we call tr_expr
at tr_instr
.
For the argument evaluation, we call aux 0 arg
for each argument, which is equivlent.
In mips.ml
, expression_registers
defines available registers for temporary values, they are $t2 ~ $t7
.
There's a function t: int -> register
to simplify the access, so (t 0)
will give us $t2
.
Register $t0
and $t1
are preserved to
- hold intermiedirea values from stack while evaluate a binary operation.
- other occasional values.
No assupetions on these 2 registers.
Within the tr_expr
function, there are 2 functions, aux
and aux_stack
.
Function aux_stack
translates expression only by stack, the detail will be explain later.
Function aux
is responsible for implementation of registers manage and call for aux_stack
when no register available. The return of aux
is a tuple: (code, count)
, code
contains mips code, count
is the number of register comsumed for this expression. aux
calls itself for sub-expressions.
While evaluate binary expression, the register for the first operand will be used for the final result, so that each a binary operation consumes 2 registers instead of 3.
While using the stack, $t0
and $t1
are for intermediare value.
the Binop
branch of aux
function
While enconter a function call:
- push all used registers (
$t2
~$t7
) to stack. - push arguments, make the call.
- retrive return value from
(t 0)
, save to$t0
. - clear argument.
- restore used registers.
- put return value to the register for current expression.
the Call
branch of aux
function.
when no registers availables, all intermediare data will be store to the stack.
The function call becomes simpler in this situation, as the value of each argument is already in the stack after evaluation.
Function aux_stack
is responsible for the implmentation, it will be called by aux
when there are no register available.