Intermediate Code Generation.pptx

ABHISHEKTIRKEY6 146 views 53 slides Sep 16, 2023
Slide 1
Slide 1 of 53
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53

About This Presentation

Compiler Design - Intermediate Code Generator
Engineering


Slide Content

Intermediate Code Generator Dr.P.Kumaran AP/CSE

Intermediate Code Generations An intermediate representation of the final machine language code is produced. This phase bridges the analysis and synthesis phases of translation. Example temp1:= int to real (60) temp2:= id3 * temp1 temp3:= id2 + temp2 id1:= temp3. id1 = id2 + id3 * id4

Phases of Compiler

Semantic Analysis Position:= initial + rate *60 Example

Syntax Directed Definition (SDD) Production + Semantic Rules Syntax Directed Translation (SDT) Production + Semantic Action Semantic Analysis

Grammar + Semantic Rules= SDD SDD for Evaluation of Expression E  E+T { E.value = E.value+T.value } E T { E.value = T.value } T T*F { T.value = T.value * F.value } T F { T.value = F.value } F  num { F.value = num.value } Syntax Directed Definition (SDD) Example1

2+3*4 Syntax Directed Definition (SDD) Cont..

Example2 Syntax Directed Definition (SDD) Cont.. L  E {L.val=E.val} E  E 1 + T { E.val= E 1 .val+ T.val} E  T {E.val=T.val} T T 1 *F { T.val= T 1 .val* F.val } TF {T.val=F.val} F(E) {F.val=E.val} F digit {F.val= digit.lexval }

Syntax Directed Definition (SDD) Cont.. (9+10)

SDD  Attribute Types Synthesized Attribute  value defined by its child and itself

D  TL {L.inh= T.type } T int { T.type =‘integer’} Treal { T.type =‘real’} L L 1 ,id {L 1 .inh= L.inh } addtype { id.entry , L.inh} L id addtype { id.entry , L.inh} Syntax Directed Definition (SDD) Cont.. Example3

real id1,id2,id3 Syntax Directed Definition (SDD) Cont..

SDD  Attribute Types Inherited Attribute  value defined by its parent or siblings or itself S- Attributed Definitions: synthesized L- Attributed Definitions: Synthesized and Inherited

E E*T {E.val=E.val* T.val } E T {E.val=T.val} T  F-T {T.val=F.val – T.val} T F {T.val=F.val} F 2 {F.val=2} F 4 {F.val=4} Syntax Directed Definition (SDD) Cont.. Example4

4-2-4*2 Syntax Directed Definition (SDD) Cont..

Example1 Syntax Directed Translation (SDT) E  E+T { printf(“+”); } 1 E T {} 2 T T*F { printf(“*”); } 3 T F {} 4 F  num { printf(num.val); } 5

2+3*4 (Infix to Postfix conversion) – TDP  top down and left to right Syntax Directed Translation (SDT) Cont..

2+3*4 (Infix to Postfix conversion) – BUP Syntax Directed Translation (SDT) Cont..

Example2 Syntax Directed Translation (SDT) Cont.. S  xxW {printf(1);} S  y {printf(2);} W  Sz {printf(3);}

xxxxyzz Syntax Directed Translation (SDT) Cont..

Graphical IR Graph Control-Flow Graph Dependence Graph Call Graph Tree Parse Tree Abstract syntax tree Directed acyclic graph Linear IR 3-Address Code Stack Machine Code Linear Code Hybrid IR Intermediate Code Generator

Graphical IR - Graph Control-flow graph

Graphical IR- Graph Dependence Graph

Graphical IR- Graph Call Graph

Graphical IR - Tree Parse Tree

Graphical IR - Tree Abstract Syntax Trees a ×2+ a× 2 × b

Graphical IR - Tree Directed Acyclic Graphs a ×2+ a× 2 × b

ICG- Graphical Representation Common Sub Expression Elimination a= b*-c + b*-c

Linear IR 3 Address Code

Linear IR Stack Machine Code

3- Address Code x:=y op z x,y, and z names, constants, or temporaries op  Operator (arithmetic or logical ) Example: x+y *z t1:=y*z t2:=x+t1

3- Address Code a= b*-c + b*-c

Assignment Statement : x:=y op z op  Arithmetic or logical operator Assignment Statement : x:= op y op Unary Operator Copy Statement : x:=y Unconditional Jump : goto L Conditional Jump : if x relop y goto L Procedure call : p(x1,x2,x3,…, xn ) Param x1 Param x2 ………….. Param xn Call p,n Type of 3- Address Statements

In compiler the 3 address code is field of records Quadruples Triples Indirect Triples 3- Address Code

3- Address Code Indirect Triples

Examples

Example 1

Example 2

Example 3

IMG-DECLARATIONS

D  TL {L.inh= T.type } T int { T.type =‘integer’} Treal { T.type =‘real’} L L 1 ,id {L 1 .inh= L.inh } addtype { id.entry , L.inh} L id addtype { id.entry , L.inh} IMG-Declarations

real id1,id2,id3 IMG-Declarations

mktable (previous) enter( table,name,type,offset ) addwidth ( table,width ) enterproc ( table,name,newtable ) lookup( table,name ) IMG-Declarations

IMG-Declarations

IMG-Declarations

IMG-Declarations

IMG-Declarations

Example 4

Example 5

Example 6

Example 7

Example 8

Example 9