Compilation 2014 Activation Records (Part 1)

Similar documents
Compilation 2014 Activation Records

Compilation 2014 Code Generation Issues

CA Compiler Construction

Run-time Environment

Topic 7: Activation Records

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

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

Run-time Environments

Run-time Environments

Course Administration

Code Generation. Lecture 12

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

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

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

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

ECE260: Fundamentals of Computer Engineering

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CSE Lecture In Class Example Handout

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

Code Generation. Lecture 19

Functions and Procedures

Chap. 8 :: Subroutines and Control Abstraction

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

CSE 504: Compiler Design. Runtime Environments

Programming Languages

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

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

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

G Programming Languages - Fall 2012

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Compilers and Code Optimization EDOARDO FUSELLA

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

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Procedure Call. Procedure Call CS 217. Involves following actions

Compilers and computer architecture: A realistic compiler to MIPS

Concepts Introduced in Chapter 7

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

CS64 Week 5 Lecture 1. Kyle Dewey

Control Abstraction. Hwansoo Han

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

System Software Assignment 1 Runtime Support for Procedures

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Module 27 Switch-case statements and Run-time storage management

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

Lecture 7: Binding Time and Storage

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

Control Instructions

MIPS Functions and Instruction Formats

Implementing Procedure Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Storage in Programs. largest. address. address

LECTURE 19. Subroutines and Parameter Passing

Run Time Environments

Anne Bracy CS 3410 Computer Science Cornell University

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

CS A331 Programming Language Concepts

Functions in MIPS. Functions in MIPS 1

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

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.

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

Chapter 2A Instructions: Language of the Computer

ECE232: Hardware Organization and Design

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

The Activation Record (AR)

Lecture 7: Procedures and Program Execution Preview

CSCI565 Compiler Design

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

Implementing Subroutines. Outline [1]

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS61C : Machine Structures

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Run-time Environments - 2

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

MIPS Functions and the Runtime Stack

CS 61c: Great Ideas in Computer Architecture

Anne Bracy CS 3410 Computer Science Cornell University

Code Generation & Parameter Passing

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

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

The Procedure Abstraction

Today. Putting it all together

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

MIPS Procedure Calls. Lecture 6 CS301

CS153: Compilers Lecture 8: Compiling Calls

CS143 - Written Assignment 4 Reference Solutions

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

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

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

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

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

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

CS143 Compilers - Written Assignment 4

Concepts Introduced in Chapter 3

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

Hakim Weatherspoon CS 3410 Computer Science Cornell University

CSCI Compiler Design

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Transcription:

Compilation 2014 Activation Records (Part 1) Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst

(Abstract) computer organization Program memory code segment contains program text data segment contains static program data (globals) stack for locals and function arguments heap for dynamically allocated memory Processors registers Operations for moving data between registers/memory }size fixed prior to runtime }size changes at runtime

Call stack of a program Higher addresses in memory contiguous region in memory that the program can use the stack grows from higher memory addresses to low ones (could be otherwise depending on architecture) Purpose of the stack: store local variables pass arguments store return values save registers Low addresses in memory stack limit set by OS

Call stack of a program Higher addresses in memory contiguous region in memory that the program can use the stack grows from higher memory addresses to low ones (could be otherwise depending on architecture) Purpose of the stack: store local variables pass arguments store return addresses save registers Low addresses in memory Q: what happens if we push past beyond stack limit? stack limit set by OS

Stack frames Function 1 calls that calls Function 3 active functions, because they haven t returned yet Function 1 Function 1 Function 1 Function 3 Function 3 Function 3 Idea: the maximum amount of memory that each function needs for its locals, temps, etc can be (usually) precomputed by the compiler Let s increase the stack by that much at once instead of many small increases Call the region of the stack corresponding to each active function that function s stack frame (also called activation record) Function 3 stack manipulations by each of the functions

Stack frames Function 1 calls that calls Function 3 active functions, because they haven t returned yet Function 1 Function 1 activation record (or stack frame) for Function 1 Function 1 stack frame for Function 3 Function 3 Function 3 stack frame for Function 3 Function 3 stack manipulations by each of the functions

Frame pointer and Stack pointer Stack pointer (): points to the top of the stack FP activation record for Function 1 Frame pointer (FP): the value of at the time the frame got activated activation record for Not allocated: garbage

Frame layout: calling conventions Cross-language calls important: using libraries Reasonable to follow a standard: calling convention Specifies stack frame layout, register usage, routine entry, exit code Likely C bias FP argument 2 argument 1 returnaddr localvar1 localvar2 storedr1 temp1 Not allocated: garbage

Typical frame layout Fits RISC architectures (such as MIPS) well Note staticlink Consider offsets from FP and : are all known at compile time? FP could be virtual, if frame size is fixed FP (prev. frame) arg_k arg_1 staticlink localvar_1 localvar_m returnaddr temp_1 temp_p saved_r1 saved_rt (args for next)

Our Tiger frame layout Fits x86, with simple register usage strategy Worth noting return address pushed automatically by call instruction FP non-virtual, always saved adjusted at runtime: arguments pushed old FP FP (prev. frame) arg_k arg_1 staticlink returnaddr saved_fp localvar_1 localvar_m returnaddr temp_1 temp_p nextarg_k nextarg_2 nextarg_1

Accessing Tiger frame slots old FP (prev.frame) arg_k arg_1 staticlink returnaddr Memory[saved_FP], Memory[FP+4*(2+k)] Memory[FP+4*(2+1)] Memory[FP+8] Memory[FP+4] Memory[staticLink]? FP saved_fp localvar_1 localvar_m temp_1 temp_p (args for next) Memory[FP] Memory[FP-4*1] Memory[FP-4*m] Memory[FP-4*(m+1)] Memory[FP-4*(m+p)] Memory[FP-who_cares] Top of frame: known early Bottom: known later Activations

The Frame Pointer Relative concepts: caller/callee frames, routines On routine entry points to arg_1 or staticlink or returnaddr.. (will return to this later) Allocate new frame (same figure): push FP FP := := - framesize If fixed, may replace FP by - framesize ( virtual FP) FP (prev.frame)???

Saving Registers Re: A frame/routine may be a caller or a callee Roles for registers: Caller-save vs. callee-save E.g. on MIPS: r16-r23 callee-save, others callersave Don t mess with r16-r23 / Don t rely on others Scenario: Nice if value in r3 is dead before call Scenario: Put long-lived value in r21 (think: loop)

Passing Parameters Pre-1960: Use globals, no recursion 1970 ies: Pass parameters on stack Later: 4-6 parameters in registers, rest on stack Experience: Few routines >6 parameters Our approach: Pass parameters on stack (fits x86, C calling conventions)

Why is register passing useful? Scenario: Calling f(a1..an) which calls g(z) As much as possible kept in registers (prev.frame) saved_ri staticlink Tempting claim: No difference Concept: Leaf routines How rare? Most invocations are leaves May not even need stack frame f frame g frame localvar_1 localvar_m returnaddr saved_a1 staticlink localvar_1

C extremely well-established, collaboration needed Address-of operator & applicable to arguments Varargs (printf) requires address arithmetics Allocate space for all arguments at end of frame, save only if address taken Some C Language Issues Address taken also known as escaping, may use separate analysis

Managing Return Addresses Return address not statically known (.. why?) Solution: Store return address Old approach: Push at call instruction New: Store in register at call instruction Non-leaf routines will have to write to the stack

Forcing Memory Storage Using registers a good default, may fail Address of variable taken ( &, pass-by-reference) Variable used from nested function Variable too large for register (use several?) Pointer arithmetics used (C arrays) Spilling