Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Intermediate Code Generation
#1

[attachment=7315]
Presented by:Dewan Tanvir Ahmed
Intermediate Code Generation


Intermediate Code Generation
Translating source program into an intermediate language.
Simple
CPU Independent,
yet, close in spirit to machine language.

Or, depending on the application other intermediate languages may be used, but in general, we opt for simple, well structured intermediate forms.

(and this completes the Front-End of Compilation).

Benefits
Retargeting is facilitated
Machine independent Code Optimization can be applied.

Intermediate codes are machine independent codes, but they are close to machine instructions.

The given program in a source language is converted to an equivalent program in an intermediate language by the intermediate code generator.

Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language.
syntax trees can be used as an intermediate language.

postfix notation can be used as an intermediate language.

three-address code (Quadraples) can be used as an intermediate language
we will use quadraples to discuss intermediate code generation
quadraples are close to machine instructions, but they are not actual machine instructions.

some programming languages have well defined intermediate languages.
java java virtual machine
prolog warren abstract machine
In fact, there are byte-code emulators to execute instructions in these intermediate languages.

Syntax Dir. Definition for Assignment Statements

PRODUCTION Semantic Rule
S id := E { S.nptr = mknode ( assign , mkleaf(id, id.entry), E.nptr) }


E E1 + E2 {E.nptr = mknode( + , E1.nptr,E2.nptr) }

E E1 * E2 {E.nptr = mknode( * , E1.nptr,E2.nptr) }

E - E1 {E.nptr = mknode( uminus ,E1.nptr) }

E ( E1 ) {E.nptr = E1.nptr }

E id {E.nptr = mkleaf(id, id.entry) }

Three Address Code

Statements of general form x:=y op z

No built-up arithmetic expressions are allowed.

As a result, x:=y + z * w should be represented as t1:=z * w t2:=y + t1 x:=t2

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
More Advanced:
Procedure:
param x1 param x2 param xn call p,n

Index Assignments:
x:=y[i]
x[i]:=y

Address and Pointer Assignments:
x:=&y
x:=*y
*x:=y
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.