1 / 26

Unit 11 Local Variables, Parameters and the Stack

Unit 11 Local Variables, Parameters and the Stack. Relevant Information CPSC 213 Companion - 2.8 (all subsections) (Optional) Textbook - 3.7. Last Updated : 7/28/2014 12:14 PM. Learning Outcomes. At the end of this unit you should be able to:

chill
Télécharger la présentation

Unit 11 Local Variables, Parameters and the Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 11Local Variables, Parameters and the Stack Relevant Information CPSC 213 Companion - 2.8 (all subsections) (Optional) Textbook- 3.7 Last Updated: 7/28/2014 12:14 PM

  2. Learning Outcomes • At the end of this unit you should be able to: • translate java methods that contain local variables and parameters into sm213 assembly and vice versa • translate C functions that contain local variables and parameters into sm213 assembly and vice versa • describe at least two ways that compilers deal with local variables and list their advantages and disadvantages • describe at least two ways that compilers deal with function/method parameters and arguments and list their advantages and disadvantages

  3. Local Variables of Functions and Methods

  4. void b () { int local0 = 0; int local1 = 1; . . . } void foo () { b (); } void bar () { b (); } Can local variables be static? • Consider the procedures on the right. Can local0 and local1 be allocated statically? • Yes • Yes, but only if the function is not recursive • Yes, but only for specific functions with specific properties • No, no local variable can be allocated statically • I don’t know what you are talking about

  5. void b () { int local0 = 0; int local1 = 1; } void foo () { b (); } Local Variables of a Procedure • Scope • are part of the local scope (local environment) of a procedure • accessible only within that procedure • Lifetime • allocated when procedure starts • de-allocated (freed) when procedure returns • exist only when procedure/method is running • each call has its own instances • Need to allocate and deallocate a procedure’s environment at each procedure activation

  6. l0:0 l1: 1 a0: ? a1: ? void b () { int local0 = 0; int local1 = 1; } void foo () { b (); } Local Variables of a Procedure (cont’) • Activation • execution of a procedure • starts when procedure is called and ends when it returns • there can be many activations of a single procedure alive at once • Activation Frame • memory that stores an activation’s state • including its locals and arguments • Should we allocate Activation Frames from the Heap? • call malloc() to create frame on procedure call and call free() on procedure return?

  7. Heap isn’t Best for Activation Frames • Order of frame allocation and deallocation is special • frames are de-allocated in the reverse order in which they are allocated • We can thus build a very simple allocator for frames • lets start by reserving a BIG chunk of memory for all the frames • assuming you know the address of this chunk • how would you allocate and free frames? • What data structure should we use to store the frames? • What restriction do we place on lifetime of local variables and arguments? 8 5 Requires more complicated and thus more costly allocation and deallocation to avoid fragmentation. Simple, cheap allocation. Just add or subtract from a pointer. 7 6 1 6 5 4 4 3 7 3 2 1 8 2 Activation Frames Explicit Allocation in Heap

  8. Stack Top Current Frame The Runtime Stack • Every (thread in a) program is given a part of memory called the runtime stack • A call to a method pushes onto the stack an activation framewith the method’s local scope • local variables • saved registers • any storage needed just for this call • The compiler generates code (as part of a method's code) • to allocate a frame when a method is called (size is known) • may allocate space for one variable at a time or all space at once • to deallocate the frame when a method returns • to access local variables within the frame (constant offsets) • Stack usually starts at the highest address and grows towards lower addresses r5 Stack Pointer (SP) Grows Up Stack Bottom

  9. The Memory Segments address 0 • STATIC • instructions • constants • static variables • HEAP • all dynamically allocated objects created by • new • malloc • STACK • all local scopes of the methods invoked but not returned yet max

  10. Accessing Local Variables Similar to object or struct member variables : • Compiler sets their layout • Compiler stores all local variables together • Accessing local variables is similar to accessing object or struct member variables • need the address of the start of the local scope • each variable will be determined by a constant offset • Compilers typically use a register to hold the address of the top of the current stack frame • usually it is called the stack pointer • in our machine we use r5 as the stack pointer • Locating local variables within a frame • use the stack pointer(r5 ) and constant offsets

  11. Accessing Local Variables: Example 1 Java C public class A { public static void f () { int m = 0; int n = 1; . . . } ... void main() { A.f (); ... void f () { int m = 0; int n = 1;. . . } void main() { f(); ... • So, since r5 will point to the activation frame when f is called, to initialize m and n, f has to do: • ld $0x0, r0 # r0 = 0 • st r0, 0x0(r5) # m = 0 • ld $0x1, r0 # r0 = 1 • st r0, 0x4(r5) # n = 1

  12. Example 2 r5 .address 0x100 start: ld $0x400000, r5 # initialize stack # pointer .address 0x200 main: gpc $6 r6 # r6 = pc+6 j f # call f () halt .address 0x300 f: deca r5 # create space for n deca r5 # create space for m ld $0x0, r0 # r0 = 0 st r0, 0x0(r5) # m = 0 ld $0x1, r0 # r0 = 1 st r0, 0x4(r5) # n = 1 inca r5 # remove m inca r5 # remove n j 0x0(r6) # return void f () { int m = 0; int n = 1; } void main() { f(); } stack 0 m 1 n main’s frame

  13. Decision on Local Variables • Local variables are only needed during the execution of the function • Therefore we can • either store them on the stack as part of the function’s frame (as we did so far) • or keep them in registers, whenever it is possible • If we keep them in registers, we can still save them on the stack when we run out of registers. • Compiler can decide which method to use for each function • For instance, if function has few locals (1-3), store them in registers; otherwise store them on the stack • We’ll try to keep the local variables in registers until we run out of registers. 1

  14. Saving the Return Address • A procedure g may call another procedure f • main sets in r6 a return address before it calls g • g needs to set in r6 a new return address before it calls f • when f returns, g needs the address that WAS in r6 to return • If a procedure calls another procedure it needs to save current r6 on the stack • Optimizing procedure code: • At the beginning: If a procedure calls another procedure, save r6 on the stack • Before a call: compute new return address and put it in r6 • Before returning: restore r6 to the value on the stack and release its space. void f() { int m = 0; int n = 1; } void g() { f(); ... } void main() { g() ... }

  15. Example 3: Return-Address Saving & Locals in Registers r5 .address 0x200 g: deca r5 # create space for r6 st r6, 0x0(r5) # save r6 to stack gpc $6, r6 # r6 = pc + 6 j f # call f() 20c: ld $x, r1 st r0, 0(r1) # x = f() ld 0x0(r5), r6 # restore r6 from stack inca r5 # remove r6 space j 0x0(r6) # return .address 0x300 f: ld $0x2, r0 # r0 = m = 2 ld $0x3, r1 # r1 = n = 3 add r1, r0 # r0 = return value j 0x0(r6) # return 1000C 10008 1000C r6 int x; void f() { int m = 2; int n = 3; return m+n; } void g() { x = f(); } 500 500 20c stack 10000 10008 500 1000C main’s frame r0 5 2

  16. Return Value • In C and Java functions/methods return a single value • Traditionally return value is left in register 0 (%eax in IA32) • Our machine also uses r0 for it • Normal procedure: • function moves return value to r0 before it returns • caller gets the value from r0 2, 3

  17. Arguments of Procedures and Methods

  18. Function/Method Arguments • Formal arguments (or parameters) are part of method’s scope • Are different than local variables • values are supplied at the call ( actual arguments ) by the caller • Two ways to pass arguments • through registers (good for small number of arguments) • on the stack (more general) • When stack is used • calling procedure pushes the actual arguments onto the stack • arguments are pushed in the reverse order ( from right to left), so first argument is at the top of the stack • on return, calling procedure pops the arguments • We’ll always pass arguments on the stack.

  19. Register Usage Convention • r0-r3 are caller-save registers: • function may use them freely, but if it calls other functions, their values may be changed after a call. • Use them for values that do not need to be preserved across function calls. • r4-r7 are callee-save registers: • function may use them, but must save their values on the stack upon entering, and restore their values before returning. • Note that r5 is an exception: It should not be changed and used for anything else, except as stack pointer. • Note that you have to pop registers from the stack in the reverse order in which they were pushed

  20. At the beginning Save on stack any of registers r4-r7 if you plan to use them Specifically, save r6 on the stack if function calls another Before it returns Set the return value in r0 Restore saved registers Restore r6 Before calling another function Set callee’s arguments on the stack (in reverse order) Set the return address in r6 After the call to another function Pop the called function’s arguments from the stack Get return value from r0 Responsibilities of a Function/Method

  21. # bar’s code (reg use: mr0, nr1) bar: ld 0x0(r5), r0 # r0 = i (holds m) inc r0 # r0 = m = i+1 ld 0x4(r5), r1 # r1 = j (holds n) inc r1 # r1 = = n = j+1 # leave result in r0 add r1, r0 # r0 = m+n # return j 0x0(r6) #return Example 4: Putting all Together int bar(int i, int j) { int m = i+1; int n = j+1; rerurn m+n; } void foo() { int k; k = bar(8, 10); } stack r5 8000 8004 8008 8 i 800c 10 j r0 20

  22. # set ret addr and call bar gpc $6, r6 # r6 = ret. addr j bar # remove arg’s inca r5 # remove i inca r5 # remove j # restore and remove r6 ld 0x0(r5), r6 # restore r6 inca r5 # remove r6 # return value is in 0 # asume that this is k #just return j 0x0(r6) #return Example 4: Putting all Together # foo’s code foo: # save r6 deca r5 #space for r6 st r6, 0x0(r5) # save r6 # prepare for calling bar(8,10) # set actual arguments deca r5 # space for j ld $0xa, r1 st r1, 0x0(r5) # j = 10 deca r5 # space for i ld $0x8, r1 st r1, 0x0(r5) # i = 8 500 610 r6 r5 stack 610 8008 8 i 800c 10 j 500 8010 r6 8014 frame of caller of foo

  23. The First Activation Frame • The first procedure that is executed by any program is main(). So at the bottom of every stack is the frame for main. • How is the stack pointer set initially? • How is main called? • There is an OS operation _start() or crt0() or crt1() that • its code is executed first • sets the stack pointer to (largest memory address +1) • calls main • _start() for our sm213 Machine’s with 4 MB memory: # _start () _start: ld 0x400000, r5 # initialize stack pointer # to the base of stack gpc $6, r6 # r6 = pc jmp main # goto main() halt 4, 5, 6

  24. APPENDIXExample 4 with Local Variables Stored on the Stack

  25. # bar’s code # set local var’s deca r5 # space for n deca r5 # space for m # do calculations ld 0x8(r5), r0 # r0 = i inc r0 # r0 = i+1 st r0, 0x0(r5) # m = i+1 ld 0xc(r5), r1 # r1 = j inc r1 # r1 = j+1 st r1, 0x4(r5) # n = j+1 # leave result in r0 add r1, r0 # r0 = m+n # remove local var’s inca r5 # remove m inca r5 # remove n # return j 0x0(r6) #return Example 4: Putting all Together int bar(int i, int j) { int m = i+1; int n = j+1; rerurn m+n; } void foo() { int k; k = bar(8, 10); } stack r5 7f f f 9 m 8000 11 n 8004 8008 8 i 800c 10 j r0 20

  26. # set return address and call bar gpc $6, r6 # r6 = ret. addr j bar # perform after call tasks # remove arg’s inca r5 # remove i inca r5 # remove j # restore and remove r6 ld 0x0(r5), r6 # restore r6 inca r5 # remove r6 # set k to the return value st r0, 0x0(r5) # prepare to return inca r5 # remove k j 0x0(r6) #return Example 4: Putting all Together # foo’s code # set local var’s foo: deca r5 # space for k # prepare for calling bar(8,10) # save r6 deca r5 #space for r6 st r6, 0x0(r5) # save r6 # set actual arguments deca r5 # space for j ld $0xa, r1 st r1, 0x0(r5) # j = 10 deca r5 # space for i ld $0x8, r1 st r1, 0x0(r5) # i = 8 500 610 r6 r5 stack 610 8008 8 i 800c 10 j 500 8010 r6 8014 k 20 ? 8018 frame of caller of foo

More Related