INF5110 – Compiler Construction
Spring 2016
1 / 98
INF5110 Compiler Construction Spring 2016 1 / 98 Outline 1. - - PowerPoint PPT Presentation
INF5110 Compiler Construction Spring 2016 1 / 98 Outline 1. Intermediate code generation Intro Intermediate code Three-address code P-code Generating P-code Generation of three address code Basic: From P-code to TA-Code and back:
1 / 98
2 / 98
3 / 98
4 / 98
5 / 98
aThis section, based on slides from Stein Krogdahl, 2015.
6 / 98
7 / 98
1.exe-files include more, and “assembly” in .NET even more 8 / 98
9 / 98
10 / 98
11 / 98
12 / 98
13 / 98
14 / 98
15 / 98
16 / 98
17 / 98
read x ; { i n p u t an i n t e g e r } i f 0<x then f a c t := 1 ; r e p e a t f a c t := f a c t ∗ x ; x := x −1 u n t i l x = 0 ; w r i t e f a c t {
f a c t o r i a l
x } end
r e a d x ; { i n p u t an i n t e g e r } i f 0<x then f a c t := 1 ; r e p e a t f a c t := f a c t ∗ x ; x := x −1 u n t i l x = 0 ; w r i t e f a c t {
f a c t o r i a l
18 / 98
19 / 98
20 / 98
21 / 98
2There’s also two-address codes, but those have fallen more or less in disuse. 22 / 98
23 / 98
24 / 98
read x ; { i n p u t an i n t e g e r } i f 0<x then f a c t := 1 ; r e p e a t f a c t := f a c t ∗ x ; x := x −1 u n t i l x = 0 ; w r i t e f a c t {
f a c t o r i a l
x } end 25 / 98
26 / 98
27 / 98
28 / 98
29 / 98
3So, the result is not 100% linear. In general, one should not produce a flat
30 / 98
result lod x ldc 3 lod x ldc 3 adi ldc 4 lda x lod x ldc 3 adi 3 stn
31 / 98
t y p e symbol = s t r i n g t y p e e xpr = | Var
symbol | Num
i n t | Plus
e xp r ∗ e xp r | A s s i g n
symbol ∗ e xp r t y p e i n s t r = (∗ p−code i n s t r u c t i o n s ∗) LDC
i n t | LOD o f symbol | LDA
symbol | ADI | STN | STO t y p e t r e e = O n e l i n e
i n s t r | Seq
t r e e ∗ t r e e t y p e program = i n s t r l i s t
32 / 98
33 / 98
34 / 98
p r o c e d u r e genCode (T: t r e e n o d e ) b e g i n i f T = n i l then ‘ ‘ g e n e r a t e code to p r e p a r e f o r code f o r l e f t c h i l d ’ ’ // p r e f i x genCode ( l e f t c h i l d
T ) ; // p r e f i x
‘ ‘ g e n e r a t e code to p r e p a r e f o r code f o r r i g h t c h i l d ’ ’ // i n f i x genCode ( r i g h t c h i l d
T ) ; // i n f i x
‘ ‘ g e n e r a t e code to implement a c t i o n ( s ) f o r T’ ’ // p o s t f i x end ; 35 / 98
36 / 98
37 / 98
38 / 98
read x ; { i n p u t an i n t e g e r } i f 0<x then f a c t := 1 ; r e p e a t f a c t := f a c t ∗ x ; x := x −1 u n t i l x = 0 ; w r i t e f a c t {
f a c t o r i a l
x } end
r e a d x ; { i n p u t an i n t e g e r } i f 0<x then f a c t := 1 ; r e p e a t f a c t := f a c t ∗ x ; x := x −1 u n t i l x = 0 ; w r i t e f a c t {
f a c t o r i a l
39 / 98
40 / 98
t y p e symbol = s t r i n g t y p e e xpr = | Var
symbol | Num
i n t | Plus
e xp r ∗ e xp r | A s s i g n
symbol ∗ e xp r t y p e mem = Var
symbol | Temp
symbol | Addr
symbol t y p e
i n t | Mem
t y p e cond = Bool
| Not
| Eq
∗
| Leq
∗
| Le
∗
t y p e r h s = Plus
∗
| Times
∗
| I d
t y p e i n s t r = Read
symbol | Write
symbol | Lab
symbol (∗ pseudo i n s t r u c t i o n ∗) | A s s i g n
symbol ∗ r h s | A s s i g n R I
∗
∗
(∗ a := b [ i ] ∗) | A s s i g n L I
∗
∗
(∗ a [ i ] := b ∗) | BranchComp
cond ∗ l a b e l | Halt | Nop t y p e t r e e = O n e l i n e
i n s t r | Seq
t r e e ∗ t r e e 41 / 98
42 / 98
4In the p-code, the result of evaluating expression (also assignments) ends
43 / 98
44 / 98
t y p e symbol = s t r i n g t y p e e xpr = | Var
symbol | Num
i n t | Plus
e xp r ∗ e xp r | A s s i g n
symbol ∗ e xp r t y p e mem = Var
symbol | Temp
symbol | Addr
symbol t y p e
i n t | Mem
t y p e cond = Bool
| Not
| Eq
∗
| Leq
∗
| Le
∗
t y p e r h s = Plus
∗
| Times
∗
| I d
t y p e i n s t r = Read
symbol | Write
symbol | Lab
symbol (∗ pseudo i n s t r u c t i o n ∗) | A s s i g n
symbol ∗ r h s | A s s i g n R I
∗
∗
(∗ a := b [ i ] ∗) | A s s i g n L I
∗
∗
(∗ a [ i ] := b ∗) | BranchComp
cond ∗ l a b e l | Halt | Nop t y p e t r e e = O n e l i n e
i n s t r | Seq
t r e e ∗ t r e e 45 / 98
46 / 98
5Whether that is a good design from a compiler-perspective and code
47 / 98
48 / 98
49 / 98
50 / 98
51 / 98
52 / 98
53 / 98
54 / 98
55 / 98
56 / 98
57 / 98
58 / 98
result lod x ldc 3 lod x ldc 3 adi ldc 4 lda x lod x ldc 3 adi 3 stn 59 / 98
60 / 98
61 / 98
62 / 98
63 / 98
64 / 98
6In C, arrays start at an 0-offset as the first array index is 0. Details may
65 / 98
7Still in TAC format. Apart from the “readable” notation, it’s just two
66 / 98
67 / 98
l d a a l o d i l d c 1 a d i i x a elem_size ( a ) l d a a l o d j l d c 2 mpi i x a elem_size ( a ) i n d l d c 3 a d i s t o 68 / 98
69 / 98
70 / 98
71 / 98
72 / 98
73 / 98
74 / 98
75 / 98
76 / 98
77 / 98
78 / 98
8“inside” a procedure. inter-procedural control-flow refers to calls and
9gotos are almost trivial in code generation (as they are basically available
79 / 98
80 / 98
81 / 98
82 / 98
83 / 98
84 / 98
85 / 98
86 / 98
87 / 98
88 / 98
89 / 98
90 / 98
91 / 98
92 / 98
93 / 98
94 / 98
95 / 98
96 / 98
97 / 98
[Aho et al., 2007] Aho, A. V., Lam, M. S., Sethi, R., and Ullman, J. D. (2007). Compilers: Principles, Techniques and Tools. Pearson,Addison-Wesley, second edition. [Aho et al., 1986] Aho, A. V., Sethi, R., and Ullman, J. D. (1986). Compilers: Principles, Techniques and Tools. Addison-Wesley. 98 / 98