Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Forth (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Compiler wikipedia , lookup

Corecursion wikipedia , lookup

C syntax wikipedia , lookup

History of compiler construction wikipedia , lookup

C++ wikipedia , lookup

One-pass compiler wikipedia , lookup

Program optimization wikipedia , lookup

Assembly language wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Subroutine wikipedia , lookup

Tail call wikipedia , lookup

Cross compiler wikipedia , lookup

Name mangling wikipedia , lookup

Call stack wikipedia , lookup

Transcript
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
• Register allocation the easy way
CS 412/413 Introduction to
Compilers and Translators -- Spring
2
Function calls
• How to generate code for function calls?
• Two kinds of IR statements:
MOVE
EXP
CALL
CALL
dest
f
e1 … en
f
CS 412/413 Introduction to
Compilers and Translators -- Spring
e1 … en
3
Stack layout
old pc
old bp
old pc
old bp
bp
bp
locals current stack
locals current stack
frame
sp
frame
sp
en
:
:
e1
sp
CS 412/413 Introduction to
Compilers and Translators -- Spring
4
CALL
f
Two translations
old pc
old fp
fp
locals
e1 … en
sp
en
:
:
RISC
non-RISC
push en
…
push e1
call f
mov dest, eax
add sp, 4*n
e1
sp
sub sp, 4*n
mov [sp + 4], e1
...
mov [sp + 4*n], en
call f
mov dest, eax
add sp, 4*n
CS 412/413 Introduction to
Compilers and Translators -- Spring
5
How to generate code?
CALL
f
?
e1 … en
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
6
Tiling calls
• Break down into simpler IR constructs
MOVE
dest
CALL
f
e1 … en
MOVE
PUSH
PUSH
...
en
e1
dest CALL
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
7
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
8
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
9
Stack frame setup
• Need code to set up stack frame on entry
prev. bp
prev. stack locals
frame
push bp
bp
prev
mov bp,sp
locals
sub sp, 4*l
en
en
:
:
e1
prev pc
prev. stack
frame
mov sp, bp
pop bp
sp
new bp
:
:
e1
prev pc
prev bp
new new stack
frame
locals
new sp
CS 412/413 Introduction to
Compilers and Translators -- Spring
10
Function code
f: push bp
function prologue
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
function epilogue
pop bp
ret
CS 412/413 Introduction to
Compilers and Translators -- Spring
11
Compiling return
• Iota return statement returns immediately
from function. Translation:
T [ return E; ] = SEQ(MOVE(RV, T[E]),
JUMP (epilogue))
• Every function f has
f: prologue
epilogue label
body
f_epilogue:
epilogue
CS 412/413 Introduction to
Compilers and Translators -- Spring
12
Glories of CISC
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
enter 4*l, 0
...
...
...
...
…
leave
ret
CS 412/413 Introduction to
Compilers and Translators -- Spring
13
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
14
Stack frame setup
prev. bp
prev. stack locals
frame
sub sp, 4*l
bp
en
:
:
e1
prev
locals
en
prev. stack
frame
:
:
add sp, 4*l
e1
prev pc
prev pc
sp
new
locals
new sp
max arg
space
CS 412/413 Introduction to
Compilers and Translators -- Spring
new stack
frame (size 4l)
15
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 heapallocated
CS 412/413 Introduction to
Compilers and Translators -- Spring
16
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
17
Program Heap
• Program has 4 memory areas: code
segment, stack segment, static data, heap
• Two typical memory layouts (OS-dep.):
heap
stack
sp
static data
pc
sp
heap
code
static data
stack
pc
CS 412/413 Introduction to
Compilers and Translators -- Spring
code
18
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 statically
allocated in data segment
– string constants
– assembler supports data
segment declarations
sp
stack
heap
static data
pc
CS 412/413 Introduction to
Compilers and Translators -- Spring
code
19
Iota dynamic structures
a: array[T]
a.length
a = new T[n] = E;
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
20
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
21
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
22
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
23
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
24
Where we are
High-level source code

Assembly code
CS 412/413 Introduction to
Compilers and Translators -- Spring
25
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
26