Вы находитесь на странице: 1из 4

System Software - Compilers & October 06

Assemblers

Phases & Passes Phases & Passes


• most obvious task of compiler • besides translation, compiler must check
– translate source program in source language to meaning of source code
object program in object language – does it make sense
• usually a machine language or close to it – if it doesn’t, produce an error message or
warning messages
• can translate from any language into
another • several transformations before an object
program is produced.
– will be varying degrees of efficiency
– Syntactic Analysis, Contextual Analysis, Code
Generation.

October 06 Phases & Passes 1 October 06 Phases & Passes 2

Phases Phases
• Between phases source
– Represent the source to reflect the analysis done.
– Abstract Syntax Tree Syntactic analysis Error report
• AST – representst the source phrase structure. AST
– Subtrees for commands, expressions, declarations…….. Contextual analysis Error report
– Leaf nodes are the identifiers, literals and operators of
Decorated AST
the source code.
Code Generation
Object program

October 06 Phases & Passes 3 October 06 Phases & Passes 4

Syntactic Analysis Syntactic Analysis


• Sample program • Determining the phrase structure or
Let parsing .
var n: integer; – Essential as the subsequent phases depend on
car c: char; knowing how the program is composed.
In
begin • Source is parsed to check conformation to
c:= ‘&’;
n := n+1;
grammer and to construct the AST (or
end suitable representation).

October 06 Phases & Passes 5 October 06 Phases & Passes 6

Damien Costello, Dept of Computing &


Maths, GMIT 1
System Software - Compilers & October 06
Assemblers

Syntactic Analysis Contextual Analysis


• In general, the AST has • Further analysis for conformity to
– Terminal nodes for the identifiers, literals and contextual constraints
operators. – Scope rules – associate each applied occurrence
– Sub trees for the phrases on the program. of an identifier with the corresponding
• Brackets and punctuation have no declaration – detect undeclared identifiers.
counterparts – Type rules – infer the type of each expression
and detect type errors.
– Serve only to separate and enclose phrases
• Once parsed, they have no use.

October 06 Phases & Passes 7 October 06 Phases & Passes 8

Contextual Analysis Code Generation


• Yields a decorated AST • Source is known to be well formed now
– Scope rules – link to declaration after Analysis.
– Type rules – addition of type for expressions. • One issue is the treatment of identifiers.
– Declaration binds an identifier to some sort of
entity
• Will show up some errors which can be • Const m = 7; binds m to 7 and the generator must
easily identified. replace applied occurrences of m by 7.
• Var b: boolean; binds b to some address decided by
the code generator and must be replaced with this
address.

October 06 Phases & Passes 9 October 06 Phases & Passes 10

Code Generation Passes


• May also consider the nature of the target • A Pass is a complete traversal of the source
language program (or of an internal representation).
– Machine code or assembley for the target • each phase transforms the program fragment -by-
machine. fragment
– input some fragment, output another
• single pass compiler
– if each fragment goes through all phases of
transformation before compilation of the next fragment
starts
– source compiled in single pass over source text
October 06 Phases & Passes 11 October 06 Phases & Passes 12

Damien Costello, Dept of Computing &


Maths, GMIT 2
System Software - Compilers & October 06
Assemblers

Passes Phases & Passes


• entire program goes through each phase before • multi pass
any proceeds to the next
– allows for easier understanding
– multi-pass compiler
– each phase performs a separate pass over the program – clarity of design
• if there are N phases, can organise the compiler – memory capacities now make them
into 1 to N passes advantageous by storing intermediate output
• every compiler is multi phase, but may or may not – easier to construct
be multi pass
– efficiency of multi pass versus single pass
– understanding of multi pass versus single pass

October 06 Phases & Passes 13 October 06 Phases & Passes 14

Design Issues Design Issues


• Speed • Modularity
– One pass wins. – Favours a multipass compiler.
– Construction and subsequent traversals is a – Each module takes a single functions. Coordinating
modest time overhead ina multi-pass. code is simpler.
– Really only an issue where the AST is stored on • Flexibility
disk. – Favours a multipass approach.
• Space – Syntactic Analyser constructs the AST, the other
modules can work in any convenient order.
– Multipass requires memory to store the AST. • May generate more optimal code.
– Only one module active at a time though. – Single-pass requires sequential working on code.

October 06 Phases & Passes 15 October 06 Phases & Passes 16

Design Issues Example of Compilation


• Semantics preserving tranformations • after syntax analysis comes object description
(optimisations) may require analysis of the – uses the tree and the symbol table
whole code prior to code generation and – analyses declarative nodes in tree producing descriptive
information as shown
force a multipass design.
Name Description
• Source Language Properties may restrict a variable, integer, address #1
design.
– Single pass only if every phrase can be b variable, real, address #2
compiled using information preceding it. print procedure, one integer argument,
address from loader
October 06 Phases & Passes 17 October 06 Phases & Passes 18

Damien Costello, Dept of Computing &


Maths, GMIT 3
System Software - Compilers & October 06
Assemblers

Example of Compilation
• standard procedures (print)
– may receive default declaration before translation
• tree contains pointers to symbol table entries using
identifiers
– therefore, neither object description nor translation need
search the tree - just use the pointer
• Simple translation then creates the target code
• loader will relocate addresses as required and code
is runable

October 06 Phases & Passes 19

Damien Costello, Dept of Computing &


Maths, GMIT 4

Вам также может понравиться