The Activation Record (AR)

Similar documents
Code Generation. Lecture 19

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Code Generation. Lecture 12

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Functions and Procedures

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Implementing Procedure Calls

Midterm II CS164, Spring 2006

Code Generation. Lecture 30

Compilers and computer architecture: A realistic compiler to MIPS

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

CSE Lecture In Class Example Handout

Anne Bracy CS 3410 Computer Science Cornell University

MIPS Procedure Calls. Lecture 6 CS301

Course Administration

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

Assembly Language: Function Calls" Goals of this Lecture"

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

Review of Activation Frames. FP of caller Y X Return value A B C

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Anne Bracy CS 3410 Computer Science Cornell University

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

CS153: Compilers Lecture 8: Compiling Calls

Instruction Set Architectures (4)

CS 316: Procedure Calls/Pipelining

MIPS Functions and Instruction Formats

CS64 Week 5 Lecture 1. Kyle Dewey

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

MIPS Functions and the Runtime Stack

THEORY OF COMPILATION

ECE232: Hardware Organization and Design

CA Compiler Construction

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Code Generation Super Lectures

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Procedure Call and Return Procedure call

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

CS61C : Machine Structures

Hakim Weatherspoon CS 3410 Computer Science Cornell University

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Anne Bracy CS 3410 Computer Science Cornell University

Lecture 9: Procedures & Functions. CS 540 George Mason University

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Functions in MIPS. Functions in MIPS 1

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

ECE260: Fundamentals of Computer Engineering

COMP2611: Computer Organization MIPS function and recursion

CIT Week13 Lecture

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

MIPS Instruction Set

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

CSE Lecture In Class Example Handout

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

UCB CS61C : Machine Structures

SPIM Procedure Calls

Lecture #19: Code Generation

Lecture 7: Procedures

Plan. Regression testing: Demo of how to use the regress.sh script.

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

CS61C : Machine Structures

Today. Putting it all together

Systems I. Machine-Level Programming V: Procedures

Lecture 7: Procedures and Program Execution Preview

ECE 30 Introduction to Computer Engineering

Storage in Programs. largest. address. address

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

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Lecture 5: Procedure Calls

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Topic 3-a. Calling Convention 2/29/2008 1

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Lecture #31: Code Generation

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

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

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

CS 61c: Great Ideas in Computer Architecture

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

EE 361 University of Hawaii Fall

Transcription:

The Activation Record (AR) [Notes adapted from R. Bodik] Code for function calls and function definitions depends on the layout of the activation record Very simple AR suffices for this language: The result is always in the accumulator; no need to store the result in the AR. The activation record of the caller holds actual parameters just below callee s AR. For f(x1,...,xn), push xn,...,x1 on the stack These are the only variables in this language AR must also save return address. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 1

The Frame Pointer Thestackdisciplineguaranteesthatonfunctionexit$spisthesame as it was on function entry. No need to save $sp But it s handy to have a pointer to start of the current AR. Lives in register $fp (frame pointer) Useful for giving addresses of variables and parameters fixed offsets while manipulating $sp. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 2

Layout of Frame For our simple language, if h calls g, which calls f(x,y), then g s AR will contain x and y, f sarwillcontainreturnaddress(backtog)andg sframepointer. SP x SP, FP g s FP RA to g x f s frame SP, FP h s FP RA to h FP y h s FP RA to h y h s FP RA to h g s frame Before f(x,y) & After f(x,y) Before call f & After return During call Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 3

Basic Tools for Calling The calling sequence is the instructions to set up a function invocation and restore state on return. The function prologue is the code in the function definition that sets up the AR. The function epilogue is the code in the function that returns and deletes the activation record. Most machines have special instructions for calls: On MIPS, jal LABEL, jumps to LABEL and saves address of next instruction after the jal in $ra. On ia32, the return address is stored on the stack by the call LABEL instruction And returns: On MIPS, jr REG jumps to address in REG. On ia32, ret pops return address from stack and goes there. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 4

Code Generation Strategy for Call cgen (f(e 1,...,e n )): cgen (e n ) push $acc... cgen (e 1 ) push $acc jal f addiu $sp, $sp, 4*n # Evaluate and push # parameters in reverse # Jump to f and save return # Pop parameters from stack Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 5

Code Generation for Function Prologue and Epilogue cgen (def f(x 1,...,x n ) = e) = push $ra push $fp move $fp, $sp cgen (e) lw $ra, 8($fp) lw $fp, 4($fp) addiu $sp, $fp, 8 jr $ra # Save return address # Save frame pointer # Set new frame pointer # Restore return address # Restore frame pointer # Restore the stack pointer # And return to caller Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 6

IA32 Version of Function Prologue and Epilogue The last slide not a typical MIPS sequence: biased to look like the ia32: cgen (def f(x 1,...,x n ) = e) = pushl %ebp movl %esp,%ebp cgen (e) leave ret # (Call instruction has already # pushed return address.) # Save frame pointer # Set new frame pointer # Pop frame pointer from stack. # Pop return address and return Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 7

Code Generation for Local Variables Local variables are stored on the stack (thus not at fixed location). One possibility: access relative to the stack pointer. Problem: stack pointer changes in strategy we ve been using for cgen. Solution: use frame pointer, which is constant over execution of function. For simple language, use fact that parameter i is at location $fp + 4(i + 2): cgen (x i ) = lw $a0, K($fp), where K = 4(i + 2). If we had local variables other than parameters, they would be at negative offsets from $fp. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 8

Passing Static Links (I) When using static links, the link can be treated as a parameter. In the Pyth runtime, for example, a function value consistsof a code address followed by a static link. So, if we have a function-valued variable at, say, offset -8 from frame pointer, can call it with lw $t1, -8($fp) lw $t2, -4($fp) push $t2 jalr $t1 # Fetch address of code # Fetch static link # And pass as first parameter # Jump to address in $t1. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 9

Accessing Non-Local Variables In program on left, how does f3 access x1? f3 will have been passed a static link as its first parameter. The static link passed to f3 will be f2 s frame pointer def f1 (x1): def f2 (x2): def f3 (x3):... x1...... f3 (12)... f2 (9) lw $t, 8($fp) # Fetch FP for f2 lw $t, 8($t) # Fetch FP for f1 lw $a0, 12($t) # Fetch x1 In general, for a function at nesting level n to access a variable at nesting level m < n, perform n m loads of static links. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 10

Passing Static Links (II) In previous example, how do we call f2 from f3? f3 from f2? f2 from f3? def f1 (x1): def f2 (x2): def f3 (x3):... f2 (9)...... f3 (12) f2 (10) # (recursively)... Could create a function value, and call as in previous slide. To get static link for f2(9): lw$t8($fp) #FetchFPforf2 lw $t 8($t) # Fetch FP for f1 push $t # Push static link To get static link for f3 (12): push$fp #f2 sfpisstaticlink To get static link for f2(10): lw $t 8($fp) push $t Can do better. Functions and their nesting levels are known. If in a function at nesting level n, calling another at known nesting level m n + 1, get correct static link in $t with: Set $t to $fp. Perform lw $t, 8($t) n m + 1 times. Last modified: Wed Nov 8 10:52:12 2006 CS164: Lecture #33 11