12-01: Introduction to Code Generation
- The accumulator is kept in MIPS register $a0;
- The address of the next location on the stack is kept in MIPS register $sp:
- The top of the stack is at address $sp + 4.
- lw r1 offset(r2):
- Load 32-bit word from address r2 + offset into r1.
- add r1 2r r3:
- sw r1 offset(r2):
- Store 32-bit word in r1 at address r2 + offset into.
- addiu r1 r2 i:
- li r i:
12-02: Code Generation I
- beq r1 r2 label:
- Branch to label if r1 = r2.
- b label:
- Unconditional jump to label.
12-03: Code Generation II
- jal label:
- Jump to label, save address of next instruction in $ra.
- jr r:
- Jump to address in register r.
12-05: Temporaries
- Let NT(e) = # of temps needed to evaluate e;
- NT(e1 + e2):
- Needs at least as many temps as NT(e1);
- Needs at least as many temps as NT(e2) + 1.
- Space used for temps in e1 can be reused for temps in e2;
- Code generation must know how many temporaries are in use at each point;
- Add a new argument to code generation:
- the position of the next available temporary.
- The temporary is used like a small, fixed-size stack.
12-06: Object Layout
- Objects are laid out in contiguous memory;
- Each attribute stored at a fixed offset in the object:
- The attribute is in the same place in every object of that class.
- When a method is invoked, the object is self and the fields are the object’s attributes;
- The offset for an attribute is the same in a class and all of its subclasses;
- Every class has a fixed set of methods:
- Including inherited methods.
- A dispatch table indexes these methods:
- An array of method entry points;
- A method f lives at a fixed offset in the dispatch table for a class and all of its subclasses.
13-01: Semantics Overview
- Operational semantics describes program evaluation via execution rules on an abstract machine;
- Denotational semantics:
- Program’s meaning is a mathematical function.
- Axiomatic semantics:
- Program behavior described via logical formulas.
13-02: Operational Semantics
- A variable environment maps variables to locations:
- Keeps track of which variables are in scope;
- Tells up where those variables are.