of a Compiler Different Phases of a Compiler Example ... - CS 434
Recommend Documents
Dependency diagram of a typical Multi Pass Compiler: A multi pass compiler
makes several passes over the program. The output of a preceding phase is
stored ...
May 1, 2017 - RegularExpression, and the semicolon character, ;, is used to terminate the token specification. 3.1 Regular Expression Syntax. While the rules ...
A parser for the Oberon-2 language was compiled by all the compiler-compilers, using as a basis a YACC specification1 following the definition given in 14].
This module provides an interface to the standard Erlang compiler. ... generated,
but more compiler passes will be run to ensure also warnings generated.
Universität Dortmund. Fri2 - 3 -. Anatomy of a compiler. C-source frontend. HL-IR.
LL-IR. HL2LL .... *A. W. Appel, Modern compiler implementation in C, 1998 ...
semantic action, which is executed if the given pattern matches the input term. The semantic ... forms matching only at the top position of the input term (see Section 4 and. Section 6.5 for full ... We will give an answer to this question after expl
Feb 1, 2008 - Qubiter a âquantum compilerâ because, like a classical compiler, it produces a SEO for manipulating bits. Our algorithm is general in the sense ...
Engineering a Compiler. Second Edition. Keith D. Cooper. Linda Torczon. Rice
University. Houston, Texas. M
Standard ML is a major revision of earlier dialects of the functional language. ML. We describe the first compiler written for Standard ML in Standard ML.
Andrew W. Appel : Modern Compiler Implementation in C. A. Aho, R. Sethi ... S.
Muchnick, Advanced Compiler Design and Implementation, Morgan. Kaufman ...
Most C compilers consist of a multitude of passes with numerous interfaces. ... ers
as benchmarks, it seemed easy to make a new compiler that could execute ...
We present STARLET, a new compiler compiler which compiles. Extended Affix
Grammars defining a translation into an executable program : the translator.
We present STARLET, a new compiler compiler which compiles. Extended Affix
Grammars defining a translation into an executable program : the translator.
paper appeared at The Second Asia-Pacific Conference on ... Conferences in Research and Practice in ..... the testing is to use java.lang.reflect to call instance ...
Dec 20, 1997 - Borland C++ version 3.1 and Microsoft C++ version 7 on MS-DOS, ... The rdp system itself is c Adrian Johnstone but may be freely copied.
58 References. Share. Facebook · Twitter · Google+ · LinkedIn · Reddit · Download full-text PDF ..... But it is perfectly possible to write a plain BNF script that. yacc cannot implement ... yacc script with a shift instead of a reduction implements
Collin McCurdy and John Mellor-Crummey[8] found that to achieve acceptable performance with HPF encoded N- body code it was necessary to use extrinsic ...
Microsoft Corporation. Abstrax, Inc. ... framework for constructing such compilers. 1 Introduction ..... tion, which are
Node 3 enables one of the two FIRs by either asserting or de-asserting vr1325, depending the value of flag (vr78). Node 10 is activated by the done signal from.
that tha copies ara not mada or distributed for direct commercial advantage, ...... J. F. Bartlett, âCompacting garbage collection with ambiguous roots: Research ...
Abstract Syntax Tree. Error Reports. Note: Not all compilers construct an explicit
representation of an AST. (e.g. on a “single pass compiler” generally no need.
Course Overview
Chapter 3 Compilation So far we have treated language processors (including compilers) as “black boxes”
PART I: overview material 1 2
Introduction Language processors (tombstone diagrams, bootstrapping)
3
Architecture of a compiler
GOAL this lecture: – A first look "inside the box": how to build compilers. – Different “phases” and their relationships.
PART II: inside a compiler 4 5
Syntax analysis Contextual analysis
6 7
Runtime organization Code generation
PART III: conclusion 8 9
Interpretation Review
Compilation (Chapter 3)
1
The Major “Phases” of a Compiler
The different phases can be seen as different transformation steps to transform source code into object code. The different phases correspond roughly to the different parts of the language specification: • Syntax analysis Syntax • Contextual analysis Contextual constraints • Code generation Semantics
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.
Source Program Syntax Analysis
!! This This program program is is useless useless except except for for !! illustration illustration let let var var n: n: integer; integer; var var c: c: char char in in begin begin cc := := ‘&’; ‘&’; nn := := n+1 n+1 end end
Compilation (Chapter 3)
Abstract Syntax Tree
5
Compilation (Chapter 3)
Error Reports
Note: Note: Not Notall allcompilers compilersconstruct constructan an explicit explicit representation representation of of an an AST. AST. (e.g. (e.g. on on aa “single “single pass pass compiler” compiler” generally generally no no need need to toconstruct constructan anAST) AST)
• 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
VNameExp Int.Expr
‘&’
Ident Op Int.Lit
n
n
+
1
Compilation (Chapter 3)
7
Compilation (Chapter 3)
8
Contextual Analysis
2) Contextual Analysis --> Decorated AST
Finds scope and type errors.
Program LetCommand
Example 1: AssignCommand SequentialCommand
***TYPE ERROR (incompatible types in AssignCommand)
let var n: integer; var c: char in begin c := ‘&’; n := n+1 end
Code Generation Object Code
• 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)
***SCOPE ERROR (undeclared variable foo)
VarDecl address = 0[SB]
PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT
SimpleT Ident
Ident
n Integer 11
Compilation (Chapter 3)
12
2
Compiler Passes
Single Pass Compiler
• 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.
A single pass compiler makes a single pass over the source text, parsing, analyzing, and generating code all at once.
Dependency diagram of a typical Single Pass Compiler: Compiler Driver calls Syntactic Analyzer calls Contextual Analyzer
Compilation (Chapter 3)
13
let var n: integer; var c: char in begin c := ‘&’; n := n+1 end
Dependency diagram of a typical Multi Pass Compiler: Compiler Driver calls
A multi pass compiler makes several passes over the program. The output of a preceding phase is stored in a data structure and used by subsequent phases.
calls
Decorated AST
Ident n c
Object Code
Compilation (Chapter 3)
15
Type int char
Address 0[SB] 1[SB]
better
worse
Memory Modularity
better for large programs worse
(potentially) better for small programs better
Flexibility
worse
better
“Global” optimization
impossible
possible
Source Language
single pass compilers are not possible for many programming languages
Compilation (Chapter 3)
16
Language Issues Example Pascal: Pascal was explicitly designed to be easy to implement with a single pass compiler:
Multi Pass
Speed
PUSH 2 LOADL 38 STORE 1[SB] LOAD 0[SB] LOADL 1 CALL add STORE 0[SB] POP 2 HALT
Compilation (Chapter 3)
Compiler Design Issues Single Pass
Code Generator
Compilation (Chapter 3)
Multi Pass Compiler
calls
calls
– Every identifier must be declared before its first use. ? var n:integer; procedure inc; begin n:=n+1 end
17
Compilation (Chapter 3)
procedure inc; begin n:=n+1 end; Undeclared Variable! var n:integer;
18
3
Language Issues
Language Issues
Example Pascal:
Example Pascal:
– Every identifier must be declared before it is used.
– Every identifier must be declared before it is used.
– How to handle mutual recursion then?
– How to handle mutual recursion then?
procedure ping(x:integer) begin ... pong(x-1); ... end;
forward procedure pong(x:integer) procedure ping(x:integer) begin ... pong(x-1); ... end; OK! procedure pong(x:integer) begin ... ping(x–1); ... end;
procedure pong(x:integer) begin ... ping(x–1); ... end;
Compilation (Chapter 3)
19
Compilation (Chapter 3)
20
Example: The Triangle Compiler Driver public public class classCompiler Compiler {{ public public static static void void compileProgram(...) compileProgram(...) {{ Parser Parser parser parser = = new new Parser(...); Parser(...); Checker Checker checker checker = = new newChecker(...); Checker(...); Encoder Encoder generator generator = = new new Encoder(...); Encoder(...); Program // Program theAST theAST = = parser.parse( parser.parse( );); // first first pass pass checker.check(theAST // checker.check(theAST);); // second second pass pass generator.encode(theAST // generator.encode(theAST);); // third third pass pass
}}
}} public public static staticvoid void main(String[ main(String[ ]] args args)) {{ ... ... compileProgram(...); compileProgram(...); ... ... }}