Download - Compiler Lec1
-
8/13/2019 Compiler Lec1
1/16
CS346: Compilers
Instructor:Instructor: Dr. Arnab Sarkar
TA:TA: Mr. Satish Kumar
10-Jan-13 CSE346: Compilers, IIT Guwahati 1
Book:Book: Compilers: Principles,Tools and Techniques. Aho,
Lam, Sethi, Ullman
-
8/13/2019 Compiler Lec1
2/16
Lecture #1
Introduction
CSE346: Compilers, IIT Guwahati
-
8/13/2019 Compiler Lec1
3/16
-
8/13/2019 Compiler Lec1
4/16
Compilation Principles The compiler must preserve the meaning of the program
being compiled.
Other issues:
Speed
10-Jan-13 CSE346: Compilers, IIT Guwahati 4
Space Energy Efficiency
Feedback (Information returned back to the user)
Debugging
Compilation time efficiency
-
8/13/2019 Compiler Lec1
5/16
Front-End Back-EndIntermediate
Representation
Source
code
Target
code
Front-end performs the analysis of the source language:
Breaks u the source code into ieces and im oses a
Structure of a Compiler
10-Jan-13 CSE346: Compilers, IIT Guwahati 5
grammatical structure.
Using this, creates a generic Intermediate Representation(IR) of the source code.
Checks for syntax / semantics and provides informativemessages back to user in case of errors.
Builds a symbol table to collect and store information aboutsource program.phases?
-
8/13/2019 Compiler Lec1
6/16
Front-End Back-EndIntermediate
Representation
Source
code
Target
code
Back-end does the tar et lan ua e s nthesis:
Structure of a Compiler
10-Jan-13 CSE346: Compilers, IIT Guwahati 6
Builds the desired target program from the IR andinformation in the symbol table.
Why do we need to separate the compilation process intofront-end and back-end phases?
-
8/13/2019 Compiler Lec1
7/16
Structure of a Compiler
Front-End
Lexical analysis: Scan the source program and identify logical
pieces of the description.
Syntax analysis (Parsing): Identify how those pieces relate toeach other.
10-Jan-13 CSE346: Compilers, IIT Guwahati 7
Semantic analysis: Identify the meaning of the overall structure.
IR Generation: Design one possible structure.
Back-End
IR Optimization: Simplify the intended structure.
Target Code Generation: Fabricate the structure.
Target Code Optimization: Improve the resulting structure.
-
8/13/2019 Compiler Lec1
8/16
Lexical Analysis
Reads characters in the source program and groups them intomeaningful sequences called lexemes.
Produces as output a token of the form andpasses it to the next phase syntax analysis
Token-class: The symbol for this token to be used during syntax
10-Jan-13 CSE346: Compilers, IIT Guwahati 8
analysis Attribute: Points to symbol table entry for this token
E.g.: The tokens for:fin = ini + rate * 60 are:
Symbol Table
1 fin 2 ini
3 rate
-
8/13/2019 Compiler Lec1
9/16
Syntax Analysis (Parsing)
Creates a tree-like IR using the tokens produced by thelexical analyser.
This IR depicts the grammatical structure of the token
10-Jan-13 CSE346: Compilers, IIT Guwahati 9
s ream.
The IR is usually represented as syntax trees
Interior nodes represent operation
Leaves depict the arguments of the operation
-
8/13/2019 Compiler Lec1
10/16
Syntax Analysis (Parsing)
E.g.: Parse tree for:fin = ini + rate * 60
Tokens:
=
10-Jan-13 CSE346: Compilers, IIT Guwahati 10
+
*
60
-
8/13/2019 Compiler Lec1
11/16
Semantic Analysis
Collects context (semantic) information from syntax tree and symboltable and checks for semantic consistency with language definition.
Annotates nodes of the tree with the results.
Semantic errors:
10-Jan-13 CSE346: Compilers, IIT Guwahati 11
Type mismatches, incompatible operands, function called withimproper arguments, undeclared variable, etc.
Eg: int ary[10], x; x = ary * 20;
Type checkers in the semantic analyser may also do automatic typeconversions if permitted by the language specification (Coercions).
-
8/13/2019 Compiler Lec1
12/16
Semantic Analysis
=
+
float
float
10-Jan-13 CSE346: Compilers, IIT Guwahati 12
*
60
inttofloat
float
float
float
-
8/13/2019 Compiler Lec1
13/16
Intermediate Code Generation
Translate language-specific constructs in the AST into more generalconstructs.
A criterion for the level of generality: it should be straightforwardto generate the target code from the intermediate representationchosen.
10-Jan-13 CSE346: Compilers, IIT Guwahati 13
-
t1 = inttofloat(60)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3
=
+
*
60
inttofloat
-
8/13/2019 Compiler Lec1
14/16
Code Optimization
Generates streamlined code still in intermediate representation.
A range of optimization techniques may be applied. Eg. Removing unused variables
Suppressing generation of unreachable code segments Constant Folding
Common Sub-expression Elimination
Loop optimization (Removing unmodified statements in a loop)... etc.
10-Jan-13 CSE346: Compilers, IIT Guwahati 14
Our Examp e:t1 = inttofloat(60) t1 = id3 * 60.0t2 = id3 * t1 id1 = id2 + t1
t3 = id2 + t2
id1 = t3
-
8/13/2019 Compiler Lec1
15/16
Target Code Generation
Map 3-address code into assembly code of the target architecture
Instruction selection: A pattern matching problem.
Register allocation: Each value should be in a register when it isused (but there is only a limited number).
Instruction scheduling: take advantage of multiple functional units.
Our Example A possible translation using two registers:
10-Jan-13 CSE346: Compilers, IIT Guwahati 15
t1 = id3 * 60.0 MOVF id3, R2
id1 = id2 + t1 MULF #60.0, R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
-
8/13/2019 Compiler Lec1
16/16
Next Lecture
Desi n of the Front-End of a Ver Sim le Com iler
10-Jan-13 CSE346: Compilers, IIT Guwahati 16