From 3-address code / pseudo assembly (after register allocation) to real assembly code:
3-address | R-V assembly | R-V assembly |
---|---|---|
mv r, $0 | li r, 0 | add r, x0, x0 |
mv r, $16 | li r, 16 | addi r, x0, 16 |
r
stands for a RISC-V register name (i.e., temporaries have been mapped to registers, already)li r, n
is an assembler macro that gets expanded to different instructions depending on the size ofn
.
stack frame layout
stack space of the caller before call
+-----
|
| local variables of caller
| caller-saved registers
| arguments to callee
sp +------------------
when call happens
+-----
|
| local variables of caller
| arguments to callee
fp +------------------
| return address
| frame pointer of caller
| callee-saved registers
| stack space of callee
sp +------------------
calculation of the interference graph
We consider instruction Ik with Lk standing for the set of temporaries live after Ik
- regular instructions: Ik = op d, s1, s2
- d is written, s1 and s2 are read
- d interferes with all r in Lk, but not with d itself
- move instruction: lk = mv d, s
- d is written, s is read
- d interferes with all r in Lk, but neither with d nor with s
- rationale: consider the outcome of subsequent graph coloring
- if d and s are assigned different colors, then there is no issue.
- if d and s are assigned the same color, then the move becomes trivial, d and s contain the same value so that they become exchangeable in the rest of the basic block.
including the implicit temporary in register allocation
- our suggestions for code generation use
a0
as a scratch register - can we instead assign a scratch register during register allocation?
- not really:
- we can eliminate some immediate arguments:
add d, $42, s
–>mv tmp, $42; add, d, tmp, s
(though we’d prefer to use theaddi
instruction in this case) - mostly, we use the scratch register to eliminate memory operands like
-24(fp)
. But memory operands are eliminates after register allocation, so it wouldn’t work to introduce fresh temporaries at that point.
- we can eliminate some immediate arguments: