Compilers construction DD2488 lecture 2 Torbj¨ orn Granlund Nada, tg@gmplib.org
Compilers construction DD2488 lecture 2 Torbj¨ orn Granlund Nada, tg@gmplib.org These slides will be available from the course web page
Course news Please see course web page for news Informal session 2 scheduled (Mon, Wed in two weeks) New milestones set for session 2
Today’s subjects ◮ Activation records ◮ Translation to intermediate code
PART1: Activation records
The ”Stack” The stack is main abstraction for method calls
The ”Stack” The stack is main abstraction for method calls ◮ Typically grows downwards
The ”Stack” The stack is main abstraction for method calls ◮ Typically grows downwards ◮ Operations: PUSH and POP
The ”Stack” The stack is main abstraction for method calls ◮ Typically grows downwards ◮ Operations: PUSH and POP ◮ Stack pointer points at last item PUSHed
The ”Stack” The stack is main abstraction for method calls ◮ Typically grows downwards ◮ Operations: PUSH and POP ◮ Stack pointer points at last item PUSHed ◮ Stack pointer is special CPU register
The ”Stack” The stack is main abstraction for method calls ◮ Typically grows downwards ◮ Operations: PUSH and POP ◮ Stack pointer points at last item PUSHed ◮ Stack pointer is special CPU register ◮ Operating systems supports the stack .
Activation records Activation record = data a method needs during execution and for returning to its caller
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents:
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents: ◮ Incoming parameters
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents: ◮ Incoming parameters ◮ Return address
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents: ◮ Incoming parameters ◮ Return address ◮ Register save area
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents: ◮ Incoming parameters ◮ Return address ◮ Register save area ◮ Space for local variables
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents: ◮ Incoming parameters ◮ Return address ◮ Register save area ◮ Space for local variables ◮ Space for compiler-generated temporaries
Activation records Activation record = data a method needs during execution and for returning to its caller Activation records live on the stack In fact, activation records are the only thing there Contents: ◮ Incoming parameters ◮ Return address ◮ Register save area ◮ Space for local variables ◮ Space for compiler-generated temporaries ◮ Static link .
higher addresses argument n . incoming . . arguments argument 1 previous frame static link frame pointer local variables return address current frame saved registers argument n . outgoing . . arguments argument 1 static link stack pointer next frame lower addresses
Managing activation records The generated code must manage activation records For JVM: Higher abstraction level—details in its documentation For ASM: Lower abstraction level—follow ”ABI” ◮ Compiler computes exact layout ◮ Compiler generates instructions for adjusting stack pointer, saving registers, updating static link, etc.
Static nesting—variable access long factorial (long n) { long bar (long i) { if (i == n) return n; else return i * bar (i + 1); } return bar (1); }
PART2: Translation to Intermediate Representation
Compiler passes text lex tokens tokens parse parse tree + symtab parse tree + symtab semantic analysis parse tree + symtab parse tree + symtab canonicalise IR IR flow analysis IR + dependency graph IR ”optimisations” IR IR instruction selection ASM with ∞ # of regs ASM register allocation ASM ASM allocation fixup ASM ASM stack layout ASM ASM code emission
Translation to Intermediate Representation (IR) IN: Parse tree (and symbol table(s) with type info) OUT: Abstract Intermediate Representation What are the differences between PT and IR?
Translation to Intermediate Representation (IR) IN: Parse tree (and symbol table(s) with type info) OUT: Abstract Intermediate Representation What are the differences between PT and IR? ◮ Parse tree depends on source language, IR is source language independent
Translation to Intermediate Representation (IR) IN: Parse tree (and symbol table(s) with type info) OUT: Abstract Intermediate Representation What are the differences between PT and IR? ◮ Parse tree depends on source language, IR is source language independent ◮ IR ”portable assembly” .
Intermediate Representation properties
Intermediate Representation properties ◮ Convenient for parse tree visitor to generate
Intermediate Representation properties ◮ Convenient for parse tree visitor to generate ◮ Convenient to translate to actual assembly code
Intermediate Representation properties ◮ Convenient for parse tree visitor to generate ◮ Convenient to translate to actual assembly code ◮ Allow analysis in optimising compiler
Intermediate Representation properties ◮ Convenient for parse tree visitor to generate ◮ Convenient to translate to actual assembly code ◮ Allow analysis in optimising compiler ◮ Start as a full tree, gradually shallowed
Intermediate Representation properties ◮ Convenient for parse tree visitor to generate ◮ Convenient to translate to actual assembly code ◮ Allow analysis in optimising compiler ◮ Start as a full tree, gradually shallowed ◮ Infinite number of registers
Intermediate Representation properties ◮ Convenient for parse tree visitor to generate ◮ Convenient to translate to actual assembly code ◮ Allow analysis in optimising compiler ◮ Start as a full tree, gradually shallowed ◮ Infinite number of registers GCC uses two IR’s, ”Tree” and ”RTL”
Translation to IR—issues Plain expressions are easy, right? x = y + z * (a + b - t * t) * (b + 4711)
Translation to IR—issues Plain expressions are easy, right? x = y + z * (a + b - t * t) * (b + 4711) But how about this? x = y + z * (a + factorial(17) - t * t) * ((b++) + binom(n,k))
Translation to IR—issues Plain expressions are easy, right? x = y + z * (a + b - t * t) * (b + 4711) But how about this? x = y + z * (a + factorial(17) - t * t) * ((b++) + binom(n,k)) Conclusion: We have deal with side effects !
Translation to IR—issues Plain expressions are easy, right? x = y + z * (a + b - t * t) * (b + 4711) But how about this? x = y + z * (a + factorial(17) - t * t) * ((b++) + binom(n,k)) Conclusion: We have deal with side effects ! Which side-effects exist is language-dependent In Java: function calls and increments In C: function calls, increments, and nested assignments (yuk)
Mangling IR Directly after translation, statements in IR will closely reflect statements in source code program
Mangling IR Directly after translation, statements in IR will closely reflect statements in source code program We gradually break this down to simpler but more statements
Mangling IR Directly after translation, statements in IR will closely reflect statements in source code program We gradually break this down to simpler but more statements In the end, we have statements that are close to assembly code, with side effects sequenced correctly
Basic blocks OK, we have shallowed our tree to a simple sequence: . . . LAB(L1) r23 = ADD(r4711, r23) r3 = MOV(r4711) r4 = LD(r23) r3 = SUB(r3, r4) r3 = MUL(r3, 17) ST(r23, r3) r1 = ADD(r1,1) CMP(r1, 10) JNEQ(LAB(L1)) . . .
Recommend
More recommend