Status: Profs. Aiken CS 143 Lecture 11 1 Profs. Aiken CS 143 Lecture 11 2
Status: Profs. Aiken CS 143 Lecture 11 1 Profs. Aiken CS 143 Lecture 11 2
Status: Profs. Aiken CS 143 Lecture 11 1 Profs. Aiken CS 143 Lecture 11 2
High Address
Profs. Aiken CS 143 Lecture 11 5 Profs. Aiken CS 143 Lecture 11 6
1
Notes What is Other Space?
• By tradition, pictures of machine organization • Holds all data for the program
have: • Other Space = Data Space
– Low address at the top
– High address at the bottom
• Compiler is responsible for:
– Lines delimiting areas for different kinds of data
– Generating code
– Orchestrating use of the data area
• These pictures are simplifications
– E.g., not all memory need be contiguous
2
Activation Trees Example
g
Profs. Aiken CS 143 Lecture 11 13 Profs. Aiken CS 143 Lecture 11 14
Example 2 Notes
Example Example
3
Example Example
g Main g Main
f f
f f
g g
Profs. Aiken CS 143 Lecture 11 19 Profs. Aiken CS 143 Lecture 11 20
High Address
Profs. Aiken CS 143 Lecture 11 21 Profs. Aiken CS 143 Lecture 11 22
4
Example 2, Revisited Stack After Two Calls to f
Class Main {
Main
g() : Int { 1 };
f (result)
f(x:Int):Int {if x=0 then g() else f(x - 1)(**)fi};
3
main(): Int {{f(3); (*)
}};} (*)
result f (result)
AR for f: argument 2
control link
return address (**)
Profs. Aiken CS 143 Lecture 11 25 Profs. Aiken CS 143 Lecture 11 26
Example Discussion
The picture shows the state after the call to the • The advantage of placing the return value 1st
2nd invocation of f returns in a frame is that the caller can find it at a
Main fixed offset from its own frame
f (result)
3 • There is nothing magic about this organization
– Can rearrange order of frame elements
(*) – Can divide caller/callee responsibilities differently
f 1 – An organization is better if it improves execution
2 speed or simplifies code generation
5
Discussion (Cont.) Globals
• Real compilers hold as much of the frame as • All references to a global variable point to the
possible in registers same object
– Especially the method result and arguments – Can’t store a global in an activation record
High Address
Profs. Aiken CS 143 Lecture 11 33 Profs. Aiken CS 143 Lecture 11 34
• The code area contains object code • Both the heap and the stack grow
– For most languages, fixed size and read only
• The static area contains data (not code) with • Must take care that they don’t grow into each
fixed addresses (e.g., global data) other
– Fixed size, may be readable or writable
• The stack contains an AR for each currently • Solution: start heap and stack at opposite
active procedure ends of memory and let them grow towards
– Each AR usually fixed size, contains locals each other
• Heap contains all other data
– In C, heap is managed by malloc and free
Profs. Aiken CS 143 Lecture 11 35 Profs. Aiken CS 143 Lecture 11 36
6
Memory Layout with Heap Data Layout
7
Example of a Stack Machine Program Why Use a Stack Machine ?
• Consider two instructions • Each operation takes operands from the same
– push i - place the integer i on top of the stack place and puts results in the same place
– add - pop two elements, add them and put
the result back on the stack • This means a uniform compilation scheme
• A program to compute 7 + 5:
push 7 • And therefore a simpler compiler
push 5
add
• Location of the operands is implicit • The add instruction does 3 memory operations
– Always on the top of the stack – Two reads and one write to the stack
• No need to specify operands explicitly – The top of the stack is frequently accessed
• No need to specify the location of the result • Idea: keep the top of the stack in a register
• Instruction “add” as opposed to “add r1, r2” (called accumulator)
– Register accesses are faster
Smaller encoding of instructions
More compact programs • The “add” instruction is now
• This is one reason why Java Bytecodes use a acc acc + top_of_stack
stack evaluation model – Only one memory operation!
8
A Bigger Example: 3 + (7 + 5) Notes