Top Banner
21

Subjects - cdl.uni-saarland.de

Oct 17, 2021

Download

Documents

dariahiddleston
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: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerStru ture of the CompilerMooly SagivTel Aviv Universitysagiv�math.tau.a .ilandReinhard WilhelmUniversität des Saarlandeswilhelm� s.uni-sb.de31. Oktober 2007

Page 2: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerSubje ts◮ Stru ture of the ompiler◮ Automati Compiler Generation◮ Real Compiler Stru tures

Page 3: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerMotivation◮ The ompilation pro ess is de omposable into a sequen e oftasks.Aspe ts:

◮ Modularity◮ Reusabilty

◮ The fun tionality of the tasks is well de�ned.◮ The programs that implement some of the tasks an beautomati ally generated from formal spe i� ations

Page 4: Subjects - cdl.uni-saarland.de

Stru ture of the Compiler�Standard� Stru ture and implementing devi essour e(text)?lexi al analysis (7) �nite automata?tokenized-program?syntax analysis (8) pushdown automata?syntax-tree?semanti -analysis (9) attribute grammar evaluators?de orated syntax-tree?optimizations (10) abstra t interpretation + transformations?intermediate rep.?...

Page 5: Subjects - cdl.uni-saarland.de

Stru ture of the Compiler�Standard� Stru ture ont'd?intermediate rep.? ode-generation(11, 12) tree automata + dynami programming + · · ·

?ma hine-program

Page 6: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerA Running Exampleprogram foo ;var i, j : real ;begin read (i);j := i + 3 ∗ iend.

Page 7: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerLexi al Analysis (S anning)◮ Fun tionalityInput program text as sequen e of hara tersOutput program text as sequen e of symbols (tokens)◮ Read input �le◮ Report errors about symbols illegal in the programminglanguage◮ S reening subtask:

◮ Identify language keywords and standard identi�ers◮ Eliminate �white-spa e�, e.g., onse utive blanks and newlines◮ Count line numbers

Page 8: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerAutomati Generation of Lexi al Analyzers◮ The symbols of programming languages an be spe i�ed by regularexpressions.◮ Examples:

◮ program as a sequen e of hara ters.◮ (alpha (alpha | digit)*) for Pas al identi�ers◮ �(*� until �*)� for Pas al omments

◮ The re ognition of input strings an be performed by a �niteautomaton.◮ A table representation or a program for the automaton isautomati ally generated from a regular expression.

Page 9: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerAutomati Generation of Lexi al Analyzers ( ont'd)regular-expression(s)?FLEX?s anner-programinput-program - tokenized-program-Numerous generators for lexi al analyzers: lex, �ex, oolex, quex,ml-lex.

Page 10: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerSyntax Analysis (Parsing)◮ Fun tionalityInput Sequen e of symbols (tokens)Output Stru ture of the program:

◮ on rete syntax tree (parse tree),◮ abstra t syntax tree, or◮ derivation.

◮ Treat syntax errorsReport (as many as possible) syntax errors,Diagnose syntax errors,Corre t syntax errors.

Page 11: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerParse 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 12: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerAutomati Generation of Syntax Analysis◮ Parsing of programs an be performed by a pushdown automaton.◮ A table representation or a program for the pushdown automaton isautomati ally generated from a ontext free grammar. ontext-free-grammar

?BISON?parser-programtokenized-program - abstra t-syntax-tree-Numerous parser generators: ya , bison, ml-ya , java-CC,ANTLR.

Page 13: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerSemanti Analysis◮ Fun tionalityInput Abstra t syntax treeOutput Abstra t tree �de orated� with attributes, e.g.,types of sub-expressions◮ Report �semanti � errors, e.g., unde lared variables, typemismat hes◮ Resolve usages of variables:Identify the right de�ning o urren es of variables for appliedo urren es.◮ Compute type of every (sub-)expression, resolving overloading.

Page 14: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerDe orated 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 15: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerMa hine Independent Optimizations◮ Fun tionalityInput Abstra t tree de orated with attributesOutput A semanti ally equivalent abstra t tree de oratedwith attributes◮ Analyzes the program for global properties.◮ Transforms the program based on these global properties inorder to improve e� ien y.◮ Analysis may also report program anomalies, e.g., uninitializedvariables.

Page 16: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerExample1: Constant Propagation onst i = 5;var x , y : integer;beginx := 5 + i ;read y ;if x = ythen y := y + xelse y := y − x�;y := y + x ∗ 9end;

Page 17: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerExample2: Loop Invariant Code Motion and Redu tion inOperator Strength onst i = 5;var n, x , y : integer;begin x := 5 + i ;y := 1;read n;for k := 1 to 100 doy := y + k × (x + n)od;print yend;

Page 18: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerAddress Assignment◮ Map variables into the stati area, sta k, heap◮ Compute stati sizes◮ Generate proper alignments

Page 19: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerGeneration of the target programPartly ontradi tory goals:◮ Code Sele tion: Sele t heapest instru tion sequen e.◮ Register Allo ation: Perform most or all of the omputationsin registers.◮ Instru tion S heduling: On ma hines with intrapro essorparallelism, e.g., super-s alar, pipelined, VLIW:exploit intrapro essor parallelism as mu h as possible.◮ Partial problems are already NP-hard.◮ �Good� solutions are obtained by ombining suboptimalsolutions obtained by heuristi s

Page 20: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerExample: Lo al Register Allo ation◮ Try to perform all omputations in registers:◮ One register is su� ient for the (trivial) expression x ; soexe ute the ommand: 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?)◮ 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 21: Subjects - cdl.uni-saarland.de

Stru ture of the CompilerReal Compiler Stru ture◮ Simple ompilers are �one-pass�; on eptually separated tasksare ombined.Parser is the driver.◮ One task in the on eptual ompiler stru ture may need morethan one pass, e.g., mixed de larations and uses.◮ Almost all use automati ally generated lexers and parsers.◮ Compilers use global information, e.g., symbol tables.◮ There may be many representation levels in a multipass ompiler.