CIT Week13 Lecture

Similar documents
Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Systems I. Machine-Level Programming V: Procedures

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Machine-level Programming (3)

CPSC W Term 2 Problem Set #3 - Solution

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Assembly III: Procedures. Jo, Heeseung

Run-time Environments

CS213. Machine-Level Programming III: Procedures

Run-time Environments

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assignment 11: functions, calling conventions, and the stack

Intel assembly language using gcc

CSC 8400: Computer Systems. Using the Stack for Function Calls

Sungkyunkwan University

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Machine-Level Programming III: Procedures

THEORY OF COMPILATION

CSC 2400: Computer Systems. Using the Stack for Function Calls

X86 Stack Calling Function POV

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Instructor: Alvin R. Lebeck

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

University of Washington

Machine Programming 3: Procedures

System Software Assignment 1 Runtime Support for Procedures

X86 Assembly -Procedure II:1

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Process Layout and Function Calls

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

System Programming and Computer Architecture (Fall 2009)

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

x86 assembly CS449 Fall 2017

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Run-time Environment

CS61 Section Solutions 3

CMSC 313 Lecture 12 [draft] How C functions pass parameters

An Elegant Weapon for a More Civilized Age

Run-time Environments - 2

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

CS241 Computer Organization Spring 2015 IA

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

You may work with a partner on this quiz; both of you must submit your answers.

CA Compiler Construction

CSC 2400: Computer Systems. Using the Stack for Function Calls

Compiler Construction D7011E

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Giving credit where credit is due

Stack Discipline Jan. 19, 2018

G Programming Languages - Fall 2012

Code Generation & Parameter Passing

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

An Experience Like No Other. Stack Discipline Aug. 30, 2006

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Machine- Level Programming III: Switch Statements and IA32 Procedures

CS 314 Principles of Programming Languages. Lecture 13

Code Generation. Lecture 30

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Page 1. IA32 Stack CISC 360. Machine-Level Programming III: Procedures Sept. 22, IA32 Stack Popping Stack Bottom. IA32 Stack Pushing

Course Administration

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

Machine-Level Programming II: Control and Arithmetic

Status. Lecture #15: Introduction to Runtime Organization. Run-time environments. Code Generation Goals and Considerations

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

CSE Lecture In Class Example Handout

Concepts Introduced in Chapter 7

Lab 10: Introduction to x86 Assembly

Lecture 4 CIS 341: COMPILERS

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

CPS104 Recitation: Assembly Programming

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Scope, Functions, and Storage Management

Transcription:

CIT 3136 - Week13 Lecture

Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there are three choices for allocating variables and parameters: Assign them fixed locations in global memory ( static allocation). Put them on the processor stack. Allocate them dynamically in memory managed by the program (the heap ). 2

Schematic: code area static/global area stack free space heap 3

C uses all three: Global storage for definitions at level 0 ( external definitions) and statically declared locals. Stack storage for parameters and normal local variables. Heap storage for dynamically allocated data addressed through pointers (call to an allocator such as malloc required). 4

Dynamic languages like Scheme and Smalltalk use mostly the heap: All variables are implicitly pointers. Stack is used only to maintain the heap. Java is similar, except that simple vars are kept on the stack. FORTRAN and TINY use only global storage: All variables are static (even global in TINY). 5

What about C-Minus? C There are no pointers. Local variables must still be kept on the stack (we ll see why in a minute). Level 0 declarations can still be statically allocated. Thus, C-Minus can avoid having to manage a heap. 6

Function calls: Every call, no matter what the language, needs some allocated space to work correctly: incoming parameter values outgoing return value return address space for local and temporary variables This space is called an activation record. Recursion forces the activation record to be kept on the stack, since several activations of the same function can exist simultaneously. 7

FORTRAN does not allow recursion, so activation records can be kept in static storage. TINY has no functions or procedures, so no activation records, and so needs only static storage too. Some languages allow local data within a call to be accessed after the call has finished (Scheme). Then activation record must go on the heap. (Well, C does too, but that is defined to be an error.) An activation record on the stack is called a stack frame. 8

Schematic of an activation record on the stack: space for arguments (parameters) What kind of bookkeeping information? Pointer to previous activation so that whole activation can be popped at once. space for bookkeeping information, including return address space for local data space for local temporaries 9

Example in C: int x,y; int gcd( int u, int v) { if (v == 0) return u; else return gcd(v,u % v); } main() { scanf("%d%d",&x,&y); printf("%d\n",gcd(x,y)); return 0; } 10

Call structure on input 15, 10: x: 15 y: 10 Global/static area > Activation record of main u: 15 > v: 10 control link return address Activation record of first call to gcd > u: 10 v: 5 control link return address Activation record of second call to gcd fp sp > > u: 5 v: 0 control link return address Activation record of third call to gcd free space Direction of stack growth 11

Sp is the stack pointer, typically a register managed by the processor. Fp is the frame pointer, which may or may not be a register (if it isn t a register, it is usually not there). Arguments are computed by caller and pushed onto the stack; thus, they may be viewed as belonging to the previous frame (red lines above). However, from the point of view of scope, they are part of the callee (green lines above). 12

Calling Sequence: standard code inserted at call sites, and on entry and exit from functions: Caller responsibility: Before call, compute arguments and push in order onto the stack Store return address at call On exit, pop remaining old frame info from stack (like args, return value) Callee responsibility: Set up and remove current frame Record return value 13

? Example in C (gcc( gcc): _gcd: L2: pushl %ebp %esp, %ebp subl $4, %esp cmpl $0, 12(%ebp) jne L2 8(%ebp), %eax jmp L1.p2align 4,,7 Compute arguments (reverse order) fp Adjust stack ): Entry sequence? subl $8, %esp 8(%ebp), %edx %edx, %eax sarl $31, %edx idivl 12(%ebp) pushl %edx pushl 12(%ebp) call _gcd addl $16, %esp L1: %ebp, %esp popl %ebp ret Exit sequence 14

Where did the return address go? Handled automatically by the processor Call pushes retaddr onto stack Ret assumes retaddr is on top of stack, pops it to the program counter Thus, in such an architecture, retaddr goes above control link in stack frame Remember, too, in C: args go onto stack in reverse order. Return value typically goes into a register (eax in the case of the PC) 15

Local variable and parameter access: All locals are accessed by fixed offset from the frame pointer. Offset is computable at compile time (keep a running total), but frame pointer is not. In a typical processor-managed stack, parameters have positive offset, local variables have negative offset. 16

Example in C: int f( int x, int y) { int z = 42; return x + y + z;} ------------ _f: pushl %ebp subl addl addl popl ret %esp, %ebp $4, %esp // reserve space for z $42, -4(%ebp) // initialize z 12(%ebp), %eax // move y to reg eax 8(%ebp), %eax // add x to it -4(%ebp), %eax // add z to it %ebp, %esp %ebp 17

Picture (32-bit architecture): fp(ebp) sp y x return address control link z 16 12 8 4 0-4 yoffset = 12 xoffset = 8 zoffset = -4 18

Non-local references - always global in C: int x; // non-local int f(void) { int z = 42; return x + z;} ------------.globl _f _f:... addl... ret $42, -4(%ebp) -4(%ebp), %eax // load z _x, %eax.comm _x,16 // allocate 16 bytes of common // storage, give it the name _x 19

Arrays - allocate in stack (size must be fixed in C): int f(void) { int z[4]; return z[0] + z[2];} ------------ _f: pushl subl addl popl ret %ebp %esp, %ebp $24, %esp -16(%ebp), %eax // load z[2] -24(%ebp), %eax // add z[0] %ebp, %esp %ebp 20

Array picture: fp(ebp) sp return address control link?? z[3] z[2] z[1] z[0] 8 4 0-4 -8-12 -16-20 -24 zoffset = -24 21

Computed array subscripts: int f(int i) { int z[4]; return z[i];} ------------ _f: pushl %ebp subl %esp, %ebp $24, %esp 8(%ebp), %eax // move i to eax leal 0(,%eax,4), %edx // mult by 4 leal popl ret -24(%ebp), %eax // load z addr (%edx,%eax), %eax // *(z+4*i) -> eax %ebp, %esp %ebp 22

Nested scopes: void f(void) { int x; } { int y = 2; } { int z = 3; } x = y + z; { int w = 4; } int v = 5; x = v + w; _f: pushl subl addl addl popl ret %ebp %esp, %ebp $12, %esp $2, -8(%ebp) $3, -12(%ebp) -12(%ebp), %eax -8(%ebp), %eax %eax, -4(%ebp) $4, -12(%ebp) $5, -8(%ebp) -12(%ebp), %eax -8(%ebp), %eax %eax, -4(%ebp) %ebp, %esp %ebp 23

Functions as parameters - they are just pointers: int f(int (*g)(int),int x) { return g(x); } ------------ _f: pushl %ebp %esp, %ebp subl $8, %esp //?? subl $12, %esp //?? pushl 12(%ebp) // push x call addl popl ret *8(%ebp) // call g $16, %esp %ebp, %esp %ebp 24

int g(int y) { return f(g,y); } ------------ _g: pushl %ebp %esp, %ebp subl $8, %esp subl $8, %esp pushl 8(%ebp) // push y pushl $_g // push global g call _f addl $16, %esp %ebp, %esp popl %ebp ret 25

Nested functions - a major complication! (Pascal, Ada,, Scheme) procedure main is -- Ada example y : integer := 2; procedure p(x: integer) is function q return integer is begin return x + y; end; y: integer := 3; -- nonlocal/nonglobal to r! function r return integer is begin -- p end; begin end; put(r); begin return q + y; end; p(1); -- prints 6 26

Stack during call to q: y:2 x:1 control link y:3 Activation record of main program Activation record of call to p fp sp control link control link free space Activation record of call to r Activation record of call to q Inside r we must find this y Inside q we must find this y 27

New piece of bookkeeping info - the access link: Each activation record must keep a record of the location of the activation record in which its corresponding function was defined. This can be computed and stored at the time of call, but not before. It is typically called the access link. 28

Stack with access links: y:2 x:1 access link control link y:3 Activation record of main program Activation record of call to p access link control link Activation record of call to r fp sp access link control link free space Activation record of call to q 29

Use access link for nonlocal references: Inside r, follow one access link to find y. Inside q, follow one access link to find x, two access links to find y. Number of access links to follow = difference in nesting levels. Following multiple access links is called access chaining. 30

Nested functions as parameters: When a function is passed to another function, its access link must be passed as well as its code (instruction) pointer. Functions become pairs of pointers, called the ip (instruction pointer) and the ep (environment pointer): <ip,ep>. This is called a closure. ip can be computed at compile time, but ep cannot. 31

Parameters: values or references? In C, all parameters are value parameters: arguments are copied values, which become initial values of the parameters during each call. This means that simple variables in the calling environment cannot be changed by the callee. Well, not quite: arrays are implicitly pointers or references, so the values stored in an array can be changed by a callee. Sometimes we say arrays are passed in C by reference. 32

In some languages (Pascal, Ada), you can specify explicitly that you want a reference parameter instead of a value parameter: p(var x:integer) (Pascal). Any such reference parameter needs an extra level of indirection to fetch its value. In some languages (Scheme, Java), certain parameters (lists in Scheme, objects in Java) are, like C arrays, implicitly references. So a similar indirection is necessary, and values can be changed by a callee. 33