1 / 26

CS412/413

CS412/413. Introduction to Compilers and Translators March 10, 1999 Lecture 17: Finishing basic code generation. Outline. Implementing function calls Implementing functions Optimizing away the frame pointer Dynamically-allocated structures: strings and arrays

amos-good
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 March 10, 1999 Lecture 17: Finishing basic code generation

  2. Outline • Implementing function calls • Implementing functions • Optimizing away the frame pointer • Dynamically-allocated structures: strings and arrays • Register allocation the easy way CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  3. Function calls • How to generate code for function calls? • Two kinds of IR statements: MOVE EXP CALL dest CALL f e1 … en f e1 … en CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  4. Stack layout old pc old pc old bp old bp bp bp current stack frame locals current stack frame locals sp en sp : : e1 sp CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  5. CALL f e1 … en Two translations old pc old fp fp sub sp, 4*n mov [sp + 4], e1 ... mov [sp + 4*n], en call f mov dest, eax add sp, 4*n locals en sp : : RISC e1 non-RISC sp push en … push e1 call f mov dest, eax add sp, 4*n CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  6. CALL f e1 … en How to generate code? push en … push e1 call f mov dest, eax add sp, 4*n ? • Problem: doesn’t fit into tiling paradigm; unbounded # tiles required CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  7. MOVE dest CALL f e1 … en Tiling calls • Break down into simpler IR constructs MOVE dest PUSH PUSH CALL ... en e1 f • Use tiles for push, call instructions to generate code (push r/m32, push imm, call addr, call r/m32) CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  8. Compiling function bodies • Translation to IR: T [ f(…) : T = E ] = MOVE(RV, T[E]) T [ f(…) = E] = EXP(T[E]) • Try it out: f(x: int, y:int) = x + y CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  9. f(x: int, y:int) = x + y Abstract assembly for f mov t1, [fp + x] mov t2, [fp + y] mov t3, t1 add t3, t2 mov eax, t3 MOVE RV + MEM MEM + + FP x FP y What’s missing here? CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  10. Stack frame setup • Need code to set up stack frame on entry push bp mov bp,sp sub sp, 4*l bp prev. bp prev locals prev. stack frame prev. stack frame locals en en : : : : mov sp, bp pop bp e1 e1 prev pc prev pc sp prev bp new bp new locals new stack frame new sp CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  11. Function code f: push bp mov bp,sp sub sp, 4*l mov t1, [fp + x] mov t2, [fp + y] mov t3, t1 add t3, t2 mov eax, t3 mov sp, bp pop bp ret function prologue function epilogue CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  12. Compiling return • Iota return statement returns immediately from function. Translation: T [ return E; ] = SEQ(MOVE(RV, T[E]), JUMP (epilogue)) • Every function f has epilogue label f: prologue body f_epilogue: epilogue CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  13. Glories of CISC f: push bp enter 4*l, 0 mov bp, sp sub sp, 4*l mov t1, [fp + x] ... mov t2, [fp + y] ... mov t3, t1 ... add t3, t2 ... mov eax, t3 … mov sp, bp leave pop bp ret ret CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  14. Optimizing away bp • Idea: maintain constant offset k between frame pointer and stack pointer • Use RISC-style argument passing rather than pushing arguments on stack • All references to FP+n translated to operand sp + (n + k) instead of to bp + n • Advantage: get whole extra register to use when allocating registers (7!) CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  15. Stack frame setup bp prev. bp sub sp, 4*l prev locals prev. stack frame prev. stack frame locals en en : : : : add sp, 4*l e1 e1 prev pc prev pc sp new locals new stack frame (size 4l) max arg space new sp CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  16. Caveats • Get even faster (and RISC-core) prologue and epilogue than with enter/leave but: • Must save bp register if we want to use it (like sp, callee-save) • Doesn’t work if stack frame is truly variable-sized : e.g., alloca() call in C allocates variable-sized array on the stack -- not a problem in Iota where arrays heap-allocated CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  17. Dynamic structures • Modern programming languages allow dynamically allocated data structures: strings, arrays, objects C:char *x = (char *)malloc(strlen(s) + 1); C++:Foo *f = new Foo(…); Java:Foo f = new Foo(…); String s = s1 + s2; Iota:x: array[int] = new int[5] = 0; String s = s1 + s2; CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  18. stack Program Heap • Program has 4 memory areas: code segment, stack segment, static data, heap • Two typical memory layouts (OS-dep.): sp heap static data heap pc code static data stack sp pc code CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  19. stack Object allocation • Dynamic objects allocated in the heap • array creation, string concatenation • malloc(n) returns new chunk of n bytes, free(x) releases memory starting at x • Constants staticallyallocated in data segment • string constants • assembler supports datasegment declarations sp heap static data pc code CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  20. Iota dynamic structures a: array[T] a = new T[n] = E; a.length a elements of a T temp = E; a = malloc(n * 4 + 4); MEM(a) = n; a = a + 4; for (i = 0; i < n; i++) { MEM(a + 4*i) = temp; } CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  21. Trivial register allocation • Can convert abstract assembly to real assembly easily (but generate bad code) • Allocate every temporary to location in the current stack frame rather than to a register • Every temporary stored in different place -- no possibility of conflict • Three registers needed to shuttle data in and out of stack frame (max. # registers used by one instruction) : e.g, eax, ebx, ecx CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  22. Rewriting abstract code • Given instruction, replace every temporary in instruction with one of three registers • Add mov instructions before instruction to load registers properly • Add mov instructions after instruction to put data back onto stack (if necessary) push t1  mov eax, [fp - t1off]; push eax mov [fp+4], t3  ? add t1, [fp - 4]  ? CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  23. Result • Simple way to get working code • Code is longer than necessary, slower • Also can allocate temporaries to registers until registers run out (3 temporaries on Pentium, 20+ on MIPS, Alpha) • Code generation technique actually used by some compilers when all optimization turned off (-O0) • Will use for Programming Assignment 3 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  24. Summary • Now: complete code generation technique • Use tiling to perform instruction selection • Function code generated by gluing prologue, epilogue onto body • Dynamic structure allocation handled by relying on heap allocation routines (malloc) • Allocate temporaries to stack locations to eliminate use of unbounded # of registers CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  25. Where we are High-level source code  Assembly code CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  26. Further topics • Generating better code • register allocation • optimization (high- and low-level) • Supporting language features • objects, polymorphism, function values • advanced GC techniques • dynamic linking, loading, & PIC • dynamic types and reflection • Compiler-like programs • source-to-source translation • interpreters CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

More Related