CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedure Calls Prof. Sirer CS 316 Cornell University.
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
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:
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.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
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.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Intro to Computer Architecture
Run time vs. Compile time
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.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
COP4020 Programming Languages
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Activation Records (in Tiger) CS 471 October 24, 2007.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
CSC 8505 Compiler Construction Runtime Environments.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Chapter 14 Functions.
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
COMPILERS Activation Records
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Machine-Level Programming 4 Procedures
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
The University of Adelaide, School of Computer Science
Procedures and Calling Conventions
Systems Architecture I
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 2 Administration Homework 2 due now

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 3 Handling Recursion Java, Iota: all global identifiers visible through a module Need to create environment (symbol table) containing all of them for checking each function definition Global identifiers bound to their types x: int  {x : int} Functions bound to function types gcd(x: int, y:int): int  {gcd: int  int  int}

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 4 Auxiliary environment info Entries representing functions are not normal environment entries {gcd: int  int  int} int  int  int not a type in the type system : functions not first-class values in Iota Can’t use gcd as a variable name In general all kinds of information can be stuck into symbol table

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 5 Handling Recursion Need environment containing at least {f: int  int, g: int  int} when checking both f and g Two-pass approach: –Scan top level of AST picking up all function signatures and creating an environment binding all global identifiers –Type-check each function individually using this global environment

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 6 Non-local control flow How to check non-local control transfers? –return, throw, break, continue Fits into existing framework for type checking (mostly) Question: what is the type of a return statement? A |– return E : ?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 7 How to check return? A |– E : T A |– return E : void A return statement has no value, so its type is void But… how to make sure the return type of the current function is T ?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 8 More auxiliary info Answer: put it in the symbol table Add entry {return : int } when we start checking the function, look up this entry when we hit a return statement. A’ = A  {..., a i : T i,... }  {return : T} A’ |– E : T A |– [ id (..., a i : T i,... ) : T = E ] A |– E : T {return : T}  A A |– return E : void

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 9 Summary Static semantics provides a way to describe semantic analysis concisely If properly designed, leads to natural recursive implementation

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 10 Where we are Source code (character stream) Lexical analysis Syntactic Analysis Token stream Semantic Analysis Intermediate Code Generation Abstract syntax tree + types Abstract syntax tree Intermediate Code regular expressions grammars static semantics translation functions

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 11 Intermediate Code Code for an abstract processor Processor-specific details avoided (e.g. # of registers) Generality enables optimization Tree representation conversion if == int b int 0 = int a int b boolean int ; CJUMP == MEM fp8 + CONSTMOVE 0MEM fp4 8 NOP + + if (b==0) a = b;

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 12 Variables in IR if == int b int 0 = int a int b boolean int ; CJUMP == MEM fp 8 - CONSTMOVE 0MEM fp4 8 NOP - - Variables mapped to memory locations b  Mem[fp - 8] How do we map them?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 13 IR Architecture Infinite no. of general purpose registers Stack pointer register (sp) Frame pointer register (fp) Program counter register (pc) Versus Pentium: –Very finite number of registers (EAX–EDX, ESI, EDI) –None really “general purpose” –Stack pointer (ESP), frame pointer (EBP), instruction pointer (EIP) Versus MIPS, Alpha: –32 general purpose registers (r0–r31) + PC

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 14 Representing variables Global variables: mapped to particular locations in memory Local variables, arguments: can’t map to fixed locations because of recursion, threads fact(x: int): int = { if (x==0) 1; else x*fact(x-1); } where to store x?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 15 Stack Local storage allocated on stack –area of memory for storage specific to function invocations –each function invocation: a new stack frame –same variable in different invocations stored in different stack frames: no conflict

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 16 Stack Frames Program stack pointed to by two registers –sp: stack pointer –fp: frame pointer New stack allocation at sp Stack contents accessed relative to fp Positive offsets: function arguments, static link to previous function Negative offsets: local storage, e.g. b  fp - 8 sp unused fp arguments local vars temporaries static link increasing memory address

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 17 Arguments (std. Pentium) gcd(x:int, y:int): int = { … } Arguments part of calling stack frame Pushed onto stack before return address (positive offset from fp) End of frame: static link y x ret pc call fp fp calling frame current frame

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 18 Local variables gcd(x:int, y:int): int = { if (x == 0) y; else { if (x < y) { t:int = x; x = y; y = t; } gcd(x%y,y); }} y x ret pc old fp fp t temp sp calling frame current frame t always at [fp+0]

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 19 Making a call gcd(temp1,y) y x ret pc old fp fp t temp sp calling frame current frame Caller: push y push temp1 call function push ret addr pc := gcd On return: sp := sp + 8 y temp1 ret addr sp

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 20 Entering a function Callee: need to establish new frame Push fp from calling frame Move sp to fp Adjust sp to make room for local variables On return: –move fp to sp –pop fp –return y x ret pc old fp fp t temp sp calling frame y temp1 ret addr fp t sp current frame

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 21 Modern architectures Pentium calling conventions (for C): lots of memory traffic Modern processors: use of memory is much slower than register accesses Pentium has impoverished register set (4 somewhat general purpose registers) More registers  better calling conventions?

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 22 MIPS, Alpha calling conventions 32 registers! Up to 4 arguments (6 on Alpha) passed in registers Return address placed in register (r31) No frame pointer unless needed Local variables, temporary values placed in registers

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 23 MIPS stack frame sp Caller: Use jal gcd, r31 leaf procedure: Return addr in r31, sp = r30 K = max size of locals, temps On entry: sp := sp - K On exit: sp := sp + K; ret r31 fp = sp + K non-leaf procedure: Put return addr on stack Save temporary registers on stack when making call On entry: sp := sp - K; [sp + K - 4] := r31 On exit: r31 := [sp + K - 4]; sp += K; ret r31; locals K ra K sp

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 24 Mapping variables Variables, temporaries assigned to locations during intermediate code generation (& optimization) –assigned to one of infinite no. registers initially –eventually allocated locations relative to fp Unoptimized code: –all locations are on stack –arguments pushed onto stack

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 25 Compiling Functions Abstract machine for intermediate code Stack frames store state for functions Calling conventions –x86/Pentium: everything on stack –MIPS, Alpha: everything in registers Code transformations for intermediate code generation