Based on Mike Feeley’s and Tamara Munzner’s original slides; Modified by George Tsiknis Parameters and Local Variables Relevant Information CPSC 213 Companion.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Based on Mike Feeley’s original slides; Modified by George Tsiknis Unit 11 Local Variables, Parameters and the Stack Relevant Information CPSC 213 Companion.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
25 seconds left…...
Code Generation.
The University of Adelaide, School of Computer Science
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS 536 Spring Code generation I Lecture 20.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Run-Time Storage Organization
Intro to Computer Architecture
Run time vs. Compile time
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
CSC 8505 Compiler Construction Runtime Environments.
Compiler Construction Code Generation Activation Records
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Rocky K. C. Chang Version 0.1, 25 September 2017
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Run-time organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
The University of Adelaide, School of Computer Science
Understanding Program Address Space
UNIT V Run Time Environments.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Implementing Functions: Overview
Presentation transcript:

Based on Mike Feeley’s and Tamara Munzner’s original slides; Modified by George Tsiknis Parameters and Local Variables Relevant Information CPSC 213 Companion -2.8 (all subsections) (Optional) Textbook - 3.7

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

3 Local Variables of Functions and Methods

Unit 12 4 Local Variables of a Function/Method Are part of the local scope (local environment) of a procedure/method Exist only when procedure/method is running Each call has its own instances Need to allocate and deallocate a method’s environment at each call Similar to instance variables? void b () { int local0 = 0; int local1 = 1; } void foo () { b (); }

Unit 12 5 Accessing Local Scope Similar to instance variables : Compiler sets their layout Compiler stores all local variables together Accessing local variables is similar to accessing instance variables  need the address of the start of the local scope  each variable will be determined by a constant offset Can have a register holding the address of the beginning of the local scope of the current call  usually called stack pointer  in our machine we use r5

Unit 12 6 Accessing Local Scope (cont') Unlike instance variables : Local scopes have to be allocated/deallocated fast  allocated at each call  deallocated when the call returns Note that function calls return in reverse order Therefore, dealocation of local scopes is in reverse order of allocation  what structure should we use to store the local scopes?

Unit 12 7 Example 1 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();... Java C So, since r5 will point to the local scope 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

Unit 12 8 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 frame with 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) –can 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 Stack pointer register (r5 in our machine) points to ( the top of the) current stack frame Locating local variables within a frame  use the stack pointer (r5 ) and constant offsets

Unit 12 9 The Memory Segments 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 address 0 max

Unit Example 2.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(); } main’s frame r5 n m stack

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  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. Unit , 2

Unit Saving the Return Address A procedure g may call another procedure f  g needs to set in r6 its own return address before it calls f  when f returns, g needs the address that WAS in r6 to return If a procedure calls many other procedures only needs to save r6 once 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  At the end: 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()... }

Unit Example 3: Return-Address Saving & Locals in Registers.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 = 2 add r1, r0# r0 = return value j 0x0(r6)# return int x; void f() { int m = 2; int n = 3; return m+n; } void g() { x = f(); } stack r5 main’s frame 1000C C r c C

Unit 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 3, 4

Unit Arguments of Procedures and Methods

Unit 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)  on return, calling procedure pops the arguments We’ll always pass arguments on the stack.

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 Unit 12 17

Unit Responsibilities of a Function/Method 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

Unit Example 4: Putting all Together # 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 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); } r5 j i stack c r0 20

Unit Example 4: Putting all Together # 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 # 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 r6 r c 8008 r6 j i stack frame of caller of foo 610

Unit The First Activation Frame The first method that is executed by any thread 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 5, 6

APPENDIX Example 4 with Local Variables Stored on the Stack Unit 12 22

Unit Example 4: Putting all Together # 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 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); } r5 j i n m stack c r0 20 7f f f

Unit Example 4: Putting all Together # 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 # 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 r6 r c 8008 k r6 j i8 10 ? 500 stack frame of caller of foo