PROGRAM PROCEDURE ENTRY/EXIT
Implement the rest of the Frame module, which contains all the machine-dependent parts of the compiler: register sets, calling sequences, and activation record (frame) layout. Program 12.1 shows the Frame class. Most of this interface has been described elsewhere. What remains isPackage Frame.
package Frame; import Temp.Temp; public abstract class Frame implements Temp.TempMap { abstract public Temp RV(); (see p. 157) abstract public Temp FP(); (p. 143) abstract public Temp.TempList registers(); abstract public String tempMap(Temp temp); abstract public int wordSize(); (p. 143) abstract public Tree.Exp externalCall(String func,Tree.ExpList args); (p. 153) abstract public Frame newFrame(Temp.Label name, Util.BoolList formals); (p. 127) public AccessList formals; (p. 128) public Temp.Label name; (p. 127) abstract public Access allocLocal(boolean escape); (p. 129) abstract public Tree.Stm procEntryExit1(Tree.Stm body); (p. 251) abstract public Assem.InstrList procEntryExit2(Assem.InstrList body); (p. 199) abstract public Proc procEntryExit3(Assem.InstrList body); abstract public Assem.InstrList codegen(Tree.Stm stm); (p. 196) }
- registers A list of all the register names on the machine, which can be used as "colors" for register allocation.
- tempMap For each machine register, the Frame module maintains a particular Temp that serves as the "precolored temporary" that stands for the register. These temps appear in the Assem instructions generated from CALL nodes, in procedure entry sequences generated by procEntryExit1, and so on. The tempMap tells the "color" of each of these precolored temps.
- procEntryExit1 For each incoming register parameter, move it to the place from which it is seen from within the function. This could be a fresh temporary. One good way to handle this is for newFrame to create a sequence of Tree.MOVE statements as it creates all the formal parameter "accesses." newFrame can put this into the frame data structure, and procEntryExit1 can just concatenate it onto the procedure body. Also concat enated to the body are statements for saving and restoring of callee-save registers (including the return-address register). If your register allocator does not implement spilling, all the callee-save (and return-address) registers should be written to the frame at the beginning of the procedure body and fetched back afterward. Therefore, procEntryExit1 should call allocLocal for each register to be saved, and generate Tree.MOVE instructions to save and restore the registers. With luck, saving and restoring the callee-save registers will give the register allocator enough headroom to work with, so that some nontrivial programs can be compiled. Of course, some programs just cannot be compiled without spilling.
If your register allocator implements spilling, then the callee-save registers should not always be written to the frame. Instead, if the register allocator needs the space, it may choose to spill only some of the callee-save registers. But "precolored" temporaries are never spilled; so procEntryExit1 should make up new temporaries for each callee-save (and return-address) register. On entry, it should move all these registers to their new temporary locations, and on exit, it should move them back. Of course, these moves (for nonspilled registers) will be eliminated by register coalescing, so they cost nothing.
- procEntryExit3 Creates the procedure prologue and epilogue assembly language. First (for some machines) it calculates the size of the outgoing parameter space in the frame. This is equal to the maximum number of outgoing parameters of any CALL instruction in the procedure body. Unfortunately, after conversion to Assem trees the procedure calls have been separated from their arguments, so the outgoing parameters are not obvious. Either procEntryExit2 should scan the body and record this information in some new component of the frame type, or procEntryExit3 should use the maximum legal value.
Once this is known, the assembly language for procedure entry, stackpointer adjustment, and procedure exit can be put together; these are the prologue and epilogue.