1 / 25

CS412/413

CS412/413. Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames. Administration. Homework 2 due now. Handling Recursion. Java, Iota: all global identifiers visible through a module

adeola
Télécharger la présentation

CS412/413

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 |– returnE : ? CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  7. How to check return? A |– E : T A |– returnE : 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 { ..., ai : Ti, ...}  {return : T} A’ |–E : T A|– [ id ( ..., ai : Ti, ...) : T = E ] A |– E : T {return : T}  A A |– returnE : 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 regular expressions Token stream Syntactic Analysis grammars Abstract syntax tree Semantic Analysis static semantics Abstract syntax tree + types Intermediate Code Generation translation functions Intermediate Code CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

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

  12. if boolean int == = ; intb int 0 int a intb fp 4 fp 8 Variables in IR CJUMP == MEM CONST MOVE NOP - 0 MEM MEM fp - 8 - • 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 increasing memory address • 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 arguments static link fp local vars temporaries sp unused 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 +16 +12 x calling frame ret pc +8 +4 call fp fp 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 +16 x +12 calling frame ret pc +8 temp1 +4 old fp current frame +0 t fp -4 temp1 t always at [fp+0] sp CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

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

  20. Entering a function y • 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 +16 x +12 ret pc +8 +4 old fp +0 t fp -4 temp1 y +16 calling frame temp1 +12 ret addr +8 fp sp fp t current frame sp 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 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 sp ra K locals 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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

More Related