Top Banner
The Structure of Compilers The Structure of Compilers Mooly Sagiv Tel Aviv University [email protected] and Reinhard Wilhelm Universität des Saarlandes [email protected] 19. Oktober 2011
22

The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

May 11, 2018

Download

Documents

truongphuc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

The Structure of Compilers

Mooly SagivTel Aviv University

[email protected]

andReinhard Wilhelm

Universität des [email protected]

19. Oktober 2011

Page 2: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Material fromChapter 6 in Wilhelm/Maurer: Compiler Design, Pearson,Chapter 6 in Wilhelm/Maurer: Übersetzerbau, Springer, 2ndedition, 1997Chapter 1 in Wilhelm/Seidl/Hack: Übersetzerbau, Vol. 2, Springer,2012Chapter 1 in Wilhelm/Seidl/Hack: Compiler Design — Syntacticand Semantic Analysis —, Vol. 2, Springer, 2012

Page 3: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Subjects

◮ Structure of the compiler

◮ Automatic Compiler Generation

◮ Real Compiler Structures

Page 4: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Motivation

◮ The compilation process is decomposable into a sequence oftasks.Aspects:

◮ Modularity◮ Reusabilty

◮ The functionality of the tasks is well defined.

◮ The programs that implement some of the tasks can beautomatically generated from formal specifications

Page 5: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

“Standard” Structure and implementing devicessource(text)

?

lexical analysis (7) finite automata

?

tokenized-program

?

syntax analysis (8) pushdown automata

?

syntax-tree

?semantic-analysis (9) attribute grammar evaluators

?

decorated syntax-tree

?

optimizations (10) abstract interpretation + transformations?

intermediate rep.

?...

Page 6: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

“Standard” Structure cont’d

?

intermediate rep.

?

code-generation(11, 12) tree automata + dynamic programming + · · ·

?

machine-program

Page 7: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

A Running Example

program foo ;var i, j : real ;begin

read (i);j := i + 3 ∗ i

end.

Page 8: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Lexical Analysis (Scanning)

◮ Functionality

Input program text as sequence of charactersOutput program text as sequence of symbols (tokens)

◮ Read input file

◮ Report errors about symbols illegal in the programminglanguage

◮ Screening subtask:◮ Identify language keywords and standard identifiers◮ Eliminate “white-space”, e.g., consecutive blanks and newlines◮ Count line numbers

Page 9: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Automatic Generation of Lexical Analyzers

◮ The symbols of programming languages can be specified by regularexpressions.

◮ Examples:

◮ program as a sequence of characters.◮ (alpha (alpha | digit)*) for identifiers◮ ““ until “*‘̀ for comments

◮ The recognition of input strings can be performed by a finite

automaton.

◮ A table representation or a program for the automaton isautomatically generated from a regular expression.

Page 10: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Automatic Generation of Lexical Analyzers (cont’d)

regular-expression(s)

?

FLEX

?

scanner-programinput-program - tokenized-program-

Numerous generators for lexical analyzers: lex, flex, oolex, quex,ml-lex.

Page 11: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Syntax Analysis (Parsing)

◮ Functionality

Input Sequence of symbols (tokens)Output Structure of the program:

◮ concrete syntax tree (parse tree),◮ abstract syntax tree, or◮ derivation.

◮ Treat syntax errors

Report (as many as possible) syntax errors,Diagnose syntax errors,

Correct syntax errors.

Page 12: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Parse Tree

, : i ; ; : ;

sep

int(’’1’’)int(’’2’’)

PROGRAM

DECLIST

E

E

T

TT

FFF

STATLIST

STATLIST

STAT

ASSIGN

E

T

FTYP

DECL

IDLIST

IDLIST

intvar semcolid(2)comid(1) id(2)id(1) id(1) id(1)bec sem bec mul add

id(’’var’’) com col sem sep sembec int(’’2’’) sembecsep id(’’b’’) mul add int(’’1’’) sepid(’’a’’) id(’’a’’)id(’’a’’)id(’’a’’) id(’’b’’) id(’’int’’)

ASSIGN

STAT

a = 2 NL b = a * a + 1 NLNLv a r a b n t

Page 13: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Automatic Generation of Syntax Analysis

◮ Parsing of programs can be performed by a pushdown automaton.

◮ A table representation or a program for the pushdown automaton isautomatically generated from a context free grammar.

context-free-grammar

?

BISON

?

parser-programtokenized-program - abstract-syntax-tree-

Numerous parser generators: yacc, bison, ml-yacc, java-CC,ANTLR.

Page 14: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Semantic Analysis

◮ Functionality

Input Abstract syntax treeOutput Abstract tree “decorated“ with attributes, e.g.,

types of sub-expressions

◮ Report “semantic“ errors, e.g., undeclared variables, typemismatches

◮ Resolve usages of variables:Identify the right defining occurrences of variables for appliedoccurrences.

◮ Compute type of every (sub-)expression, resolving overloading.

Page 15: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Decorated parse tree

1

1

2

2

3

3

4 5

3

4

5

222

1

var

(id(1),(var,int))

(id(2),(var,int))

(id(1),(var,int,0))

(id(2),(var,int,0))

(var,int)

(var,int,0) (var,int,1)

(var,int) int int

addmulbecsembec id(1)id(1)id(1) id(2)id(1) com id(2) col semint

IDLIST

IDLIST

DECL

TYP F

T

E

ASSIGN

STAT

STATLIST

STATLIST

F F F

T T

T

E

E

ASSIGN

DECLIST

PROGRAM

int(’’2’’) int(’’1’’)

(C)

(D)

(E)

STAT

Page 16: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Machine Independent Optimizations

◮ Functionality

Input Abstract tree decorated with attributesOutput A semantically equivalent abstract tree decorated

with attributes

◮ Analyzes the program for global properties.

◮ Transforms the program based on these global properties inorder to improve efficiency.

◮ Analysis may also report program anomalies, e.g., uninitializedvariables.

Page 17: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Example1: Constant Propagation

const i = 5;var x , y : integer;begin

x := 5 + i ;read y ;if x = y

then y := y + x

else y := y − x

fi;y := y + x ∗ 9

end;

Page 18: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Example2: Loop Invariant Code Motion and Reduction inOperator Strength

const i = 5;var n, x , y : integer;begin

x := 5 + i ;y := 1;read n;for k := 1 to 100 do

y := y + k × (x + n)od;print y

end;

Page 19: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Address Assignment

◮ Map variables into the static area, stack, heap

◮ Compute static sizes

◮ Generate proper alignments

Page 20: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Generation of the target program

Partly contradictory goals:

◮ Code Selection: Select cheapest instruction sequence.

◮ Register Allocation: Perform most or all of the computationsin registers.

◮ Instruction Scheduling: On machines with intraprocessorparallelism, e.g., super-scalar, pipelined, VLIW:exploit intraprocessor parallelism as much as possible.

◮ Partial problems are already NP-hard.

◮ “Good” solutions are obtained by combining suboptimalsolutions obtained by heuristics

Page 21: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Example: Local Register Allocation

◮ Try to perform all computations in registers:

◮ One register is sufficient for the (trivial) expression x ; soexecute the command:

load ri , ρ(x)

◮ If the expression e1 takes m registers to evaluate and e2 takesn registers and m > n, then e1 + e2 takes m registers(why? Implicit assumption?)

◮ If the expression e1 takes m registers and e2 takes n registersand m < n, then e1 + e2 takes n registers(why?)

◮ What happens if m = n?

◮ What happens if there aren’t enough registers?

Page 22: The Structure of Compilers - Compiler Design Lab, …compilers.cs.uni-saarland.de/teaching/cc/2011/slides/structure.pdf · Numerous generators for lexical analyzers: lex, flex, oolex,

The Structure of Compilers

Real Compiler Structure

◮ Simple compilers are “one-pass”; conceptually separated tasksare combined.Parser is the driver.

◮ One task in the conceptual compiler structure may need morethan one pass, e.g., mixed declarations and uses.

◮ Many use automatically generated lexers and parsers.

◮ Compilers record declaration information, e.g., in symboltables.

◮ There may be many representation levels in a multipasscompiler.