Beruflich Dokumente
Kultur Dokumente
Chapter 3 Compilation
So far we have treated language processors (including compilers) as black boxes GOAL this lecture: A first look "inside the box": how to build compilers. Different phases and their relationships.
Compilation (Chapter 3)
Compilation (Chapter 3)
Compilation (Chapter 3)
Example Program
We now look at each of the three different phases in a little more detail. We look at each of the steps in transforming an example Triangle program into TAM code.
! This program is useless except for ! This program is useless except for ! illustration ! illustration let var n: integer; let var n: integer; var c: char var c: char in begin in begin c := &; c := &; n := n+1 n := n+1 end end Source Program Syntax Analysis
1) Syntax Analysis
Error Reports
Note: Not all compilers construct an Note: Not all compilers construct an explicit representation of an AST. (e.g. on explicit representation of an AST. (e.g. on a single pass compiler generally no need a single pass compiler generally no need to construct an AST) to construct an AST)
Compilation (Chapter 3)
Compilation (Chapter 3)
SequentialCommand SequentialDeclaration AssignCommand VarDecl SimpleT Ident Ident Ident VarDecl Char.Expr AssignCommand
Integer
Char
&
1
7
Scope checking: verify that all applied occurrences of identifiers are declared Type checking: verify that all operations in the program are used according to their type rules. Annotate AST: Applied identifier occurrences => declaration Expressions => Type
Compilation (Chapter 3)
8
Compilation (Chapter 3)
Contextual Analysis
Finds scope and type errors.
Example 1:
AssignCommand
:char :int
AssignCommand AssignCommand SimpleV BinaryExpr :int
:int
VarDecl SimpleT Ident Ident Ident VarDecl Char.Expr
:char
VNameExp Int.Expr
:int
SimpleV
:int
:char & n
:int + 1
9
Ident Op Int.Lit
n Integer
Compilation (Chapter 3)
c Char
foo
Compilation (Chapter 3)
10
3) Code Generation
Decorated Abstract Syntax Tree Code Generation Object Code
3) Code Generation
let var n: integer; var c: char in begin c := &; n := n+1 end PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT
Assumes that program has been thoroughly checked and is well formed (scope & type rules) Takes into account semantics of the source language as well as the target language. Transforms source program into target code.
Compilation (Chapter 3)
11
n Integer
Compilation (Chapter 3)
12
Compiler Passes
A pass is a complete traversal of the source program, or a complete traversal of some internal representation of the source program (such as an AST). A pass can correspond to a phase but it does not have to! Sometimes a single pass corresponds to several phases that are interleaved in time. What and how many passes a compiler does over the source program is an important design decision.
Dependency diagram of a typical Single Pass Compiler: Compiler Driver calls Syntactic Analyzer calls Contextual Analyzer calls Code Generator
14
Compilation (Chapter 3)
13
Compilation (Chapter 3)
calls
calls
Contextual Analyzer Code Generator output input output input output AST Decorated AST Object Code
15
Ident n c
PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT
Compilation (Chapter 3)
16
Language Issues
Example Pascal: Pascal was explicitly designed to be easy to implement with a single pass compiler:
Every identifier must be declared before its first use. ? var n:integer; procedure inc; begin n:=n+1 end procedure inc; begin n:=n+1 end; Undeclared Variable! var n:integer;
Multi Pass worse (potentially) better for small programs better better possible
single pass compilers are not possible for many programming languages
17
Compilation (Chapter 3)
18
Language Issues
Example Pascal:
Every identifier must be declared before it is used. How to handle mutual recursion then? procedure ping(x:integer) begin ... pong(x-1); ... end; procedure pong(x:integer) begin ... ping(x1); ... end;
Language Issues
Example Pascal:
Every identifier must be declared before it is used. How to handle mutual recursion then? forward procedure pong(x:integer) procedure ping(x:integer) begin ... pong(x-1); ... end; OK! procedure pong(x:integer) begin ... ping(x1); ... end;
19
Compilation (Chapter 3)
Compilation (Chapter 3)
20
} }
} } public static void main(String[ ]] args )) { public static void main(String[ args { ... compileProgram(...); ... ... compileProgram(...); ... } }
Compilation (Chapter 3)
21