2. Compiler Lecture Note, Intermediate Language Page 2
Contents
โข Introduction
โข Polish Notation
โข Three Address Code
โข Tree Structured Code
โข Abstract Machine Code
โข Concluding Remarks
3. Compiler Lecture Note, Intermediate Language Page 3
โข Compiler Model
Source
Program
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate
Code Generator
tokens
AST
Front-End
Code Optimizer
Target Code Generator
IC
Back-End
IL
Object
Program
Front-End- language dependant part
Back-End - machine dependant part
Introduction
7. Compiler Lecture Note, Intermediate Language Page 7
โ Polish mathematician Lucasiewiez invented the parenthesis-free notation.
โข Postfix(Suffix) Polish Notation
โข earliest IL
โข popular for interpreted language - SNOBOL, BASIC
โ general form :
e1 e2 ... ek OP (k โฅ 1)
where, OP : k_ary operator
ei : any postfix expression (1 โค i โค k)
Polish Notation
8. Compiler Lecture Note, Intermediate Language Page 8
โ example :
if a then if c-d then a+c else a*c else a+b
ใใa L1 BZ c d - L2 BZ a c + L3 BR
L2: a c * L3 BR L1: a b + L3:
โ note
1) high level: source to IL - fast & easy translation
IL to target - difficulty
2) easy evaluation - operand stack
3) optimization ๋ถ์ ๋น - ๋ค๋ฅธ IL๋ก์ translation ํ์
4) parentheses free notation - arithmetic expression
โ interpretive language์ ์ ํฉ
Source Translator Postfix Evaluator Result
9. Compiler Lecture Note, Intermediate Language Page 9
โข most popular IL, optimizing compiler
โข General form:
A := B op C
where, A : result address
B, C : operand addresses
op : operator
(1) Quadruple - 4-tuple notation
<operator>,<operand1>,<operand2>,<result>
(2) Triple - 3-tuple notation
<operator>,<operand1>,<operand2>
(3) Indirect triple - execution order table & triples
Three Address Code
10. Compiler Lecture Note, Intermediate Language Page 10
โ example
โข A โ B + C * D / E
โข F โ C * D
Quadruple Triple
Indirect Triple
Operations Triple
* C D T1 (1) * C D 1.(1) (1) * C D
/ T1 E T2 (2) / (1) E 2.(2) (2) / (1) E
+ B T2 T3 (3) + B (2) 3.(3) (3) + B (2)
๏ฌ T3 A (4) ๏ฌ A (3) 4.(4) (4) ๏ฌ A (3)
* C D T4 (5) * C D 5.(1) (5) ๏ฌ F (1)
๏ฌ T4 F (6) ๏ฌ F (5) 6.(5)
11. Compiler Lecture Note, Intermediate Language Page 11
โข Note
โข Quadruple vs. Triple
โ quadruple - optimization ์ฉ์ด
โ triple - removal of temporary addresses
โ Indirect Triple
โข extensive code optimization ์ฉ์ด
โ IL rearrange ๊ฐ๋ฅ (triple ์ ์ธ)
โข easy translation - source to IL
โข difficult to generate good code
โ quadruple to two-address machine
โ triple to three-address machine
12. Compiler Lecture Note, Intermediate Language Page 12
โข Abstract Syntax Tree
โ parse tree์์ redundantํ information ์ ๊ฑฐ.
โข โ leaf node --- variable name, constant
โ internal node --- operator
โ ์ : a * b + c / d a[i] = exp;
Tree Structured Code
add
divmul
b ca d
assign
expindex
ia
13. Compiler Lecture Note, Intermediate Language Page 13
โข Tree Structured Common Language(TCOL)
โ Variants of AST - containing the result of semantic analysis.
Example) int a; float b;
...
b = a + 1;
AST TCOL
add
1a
assign
b float
assign
b
addi
1๏ฎ
a
15. Compiler Lecture Note, Intermediate Language Page 15
โข Motivation
โข โ rapid development of machine architectures
โ proliferation of programming languages
โ portable & adaptable compiler design --- P_CODE
โข porting --- rewriting only back-end
โ compiler building system --- EM_CODE
M front-ends
N back-ends
+ M compilers for N target machines
25. 5. ์ด์์ฑ(Portability)
โข ํฌ๊ฒ ์ํฅ์ ์ฃผ๋ ๊ธฐ๊ณ ์ข ์์ฑ๊ณผ ์ธ์ด ์ข ์์ฑ์ ๋ถ๋ช ํ ๊ตฌ๋ถ์ฑ์์๋
TCOL๊ณผ ๊ฐ์ ๊ธฐ๊ณ ์ฝ๋๊ฐ ๊ฐ์ฅ ์ฐ์
โข ์์ค ์ธ์ด์ ๊ฐ๊น์ด ๊ณ ๊ธ ํํ์ธ postfix ํํ๊ณผ AST๋ ๋ถ์ ๋น
โข Intermediate Language์ ๋น๊ต
Compiler Lecture Note, Intermediate Language Page 25
IL
Criteria
Polish
Notation
Three Address
Code
Tree
Structured
Abstract
Machine
CodePost IR Quadra Triple AST TCOL
Intermediate level C B B B C A B
efficient
processing
source to IL
translation
A C B B A B C
IL to target
translation
C A B B C A A
interpretation A B B B C C A
optimization C B A C A A B
external representation B B A B C B A
extensibility A A A A A A B
clean separation C B B B C A A
A : ์ข๋ค
B : ๋ณดํต์ด๋ค
C : ๋์๋ค