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