OOPS - INTERMEDIATE CODE GENERATION, Study notes of Object Oriented Programming

Intermediate Code Generation, Translating source program, CPU Independent, Benefits, Retargeting is facilitated, Example of 3-address code.

Typology: Study notes

2010/2011

Uploaded on 09/04/2011

vrunda
vrunda 🇮🇳

4.1

(21)

76 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intermediate Code Generation
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download OOPS - INTERMEDIATE CODE GENERATION and more Study notes Object Oriented Programming in PDF only on Docsity!

Intermediate Code Generation

Intermediate Code Generation

 Translating source program into an “intermediate

language.”

 Simple

 CPU Independent,

 This completes the “Front-End” of Compilation.

Benefits

1. Retargeting is facilitated

2. Machine independent Code Optimization

can be applied.

Types of Intermediate Languages

 (^) Graphical Representations.  (^) Consider the assignment a:=b-c+b-c:

assign

a +

uminus

b uminus

c c

b

assign

a

uminus

b c

Three Address Code

 (^) Statements of general form x:=y op z  (^) As a result, x:=y + z * w should be represented as t 1 :=z * w t 2 :=y + t 1 x:=t 2  (^) Observe that given the syntax-tree or the dag of the graphical representation we can easily derive a three address code for assignments as above.  (^) In fact three-address code is a linearization of the tree.  (^) Three-address code is useful: related to machine-language/ simple/ optimizable.

Types of Three-Address Statements.

Assignment Statement: x:=y op z Assignment Statement: x:=op z Copy Statement: x:=z Unconditional Jump: goto L Conditional Jump: if x relop y goto L Stack Operations:Push/pop

M ore Advanced:

Procedure: param x 1 param x 2 … param xn call p,n Index Assignments: x:=y[i] x[i]:=y Address and Pointer Assignments: x:=&y x:=*y *x:=y

Syntax-Directed Translation into 3-address code.  (^) First deal with assignments.  (^) Use attributes  (^) E. place: the name that will hold the value of E  (^) Identifier will be assumed to already have the place attribute defined.  (^) E. code: hold the three address code statements that evaluate E (this is the `translation’ attribute).

 Use function newtemp that returns a new temporary variable that we

can use.

 Use function gen to generate a single three address statement given the

necessary information (variable names and operations).

What about things that are not assignments?

 E.g. while statements of the form “while E do S”

(intepreted as while the value of E is not 0 do S)

Extension to the previous syntax-dir. Def. PRODUCTION Swhile E do S 1 Semantic Rule S.begin = newlabel ; S.after = newlabel ; S. code = gen (S. begin ‘:’) || E. code || gen (‘if’ E. place ‘=’ ‘0’ ‘goto’ S. after ) || S 1. code || gen(‘goto’ S. begin ) || gen(S. after ‘:’)

Implementations of 3-address statements

 (^) Quadruples t 1 :=- c t 2 :=b * t 1 t 3 :=- c t 4 :=b * t 3 t 5 :=t 2 + t 4 a:=t 5 op arg1 arg2 result (0) uminus c t 1 (1) * b t 1 t 2 (2) uminus c (3) * b t 3 t 4 (4) + t 2 t 4 t 5 (5) := t 5 a Temporary names must be entered into the symbol table as they are created.

Other types of 3-address statements

 (^) e.g. ternary operations like x[i]:=y x:=y[i]  (^) require two or more entries. e.g. op arg1 arg (0) [ ] = x i (1) assign (0) y op arg1 arg (0) [ ] = y i (1) assign x (0)

Implementations of 3-address statements, III

Indirect Triples

op arg1 arg (14) uminus c (15) * b (14) (16) uminus c (17) * b (16) (18) + (15) (17) (19) assign a (18) op (0) (14) (1) (15) (2) (16) (3) (17) (4) (18) (5) (19)