Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

INTERMEDIATE CODE GENERATION(III)

3AC for Programming Constructs


If (E) Stmt1 else Stmt2 while (E) do Stmt
Equivalent 3AC/IR Equivalent 3AC/IR
if E goto L1 L: if E goto L2
code for Stmt2 goto L2
L1: code for Stmt
goto L2
goto L
L1: code for Stmt1
L2:
L2:
3AC for Programming Constructs
for(E1;E2;E3) Stmt
Equivalent 3AC/IR
code for E1
L: if E2 goto L1
goto L2
L1: code for Stmt
code for E3
goto L
L2:
Exercises
Translate the following statement into 3AC

a. if (E1) { if (E2) A1; else A2; } else A3; A4;

b. while (E1) {if (E2) then A1; else A2;} A3;

c. switch (E) { case V1: S1; break; default: S2;}

d. if (a<b or c<d or e<f) 0; else 1;


3AC for 2D array or arr[i][j]
• The requirements are,

• Size of 2D array

• Data type of array/size of each element

• Storage type (Row Major Order/Column Major Order)


Contd...
• Need to find the number of bytes need to be crossed to reach to the
given element and using the base address we can get the element.

• Suppose a 2d array arr is given having dimension r x c where r is


number of rows, c is number of columns and w is size of each
element

• For RMO, address of arr[i,j] element = arr + (i x c + j) x w

• For CMO, address of arr[i,j] element = arr + (j x r + i) x w


Example
• Let storage be RMO, data type is integer (4 bytes) and size of
array 3 x 4, find the 3AC of c = arr[i][j] + b.

t1 = i x 16
t2 = j x 4
t3 = t1 + t2
t4 = arr[t3]
c = t4 + b

• Assignment: Solve Example 6.4.3 a, b, and c from text book.


Static Single Assignment (SSA) Form
• Static single-assignment form (SSA) is an intermediate
representation that facilitates certain code optimizations.

• Two distinctive aspects distinguish SSA from three-address


code.

• The first is that all assignments in SSA are to variables with


distinct names; hence the term static single-assigment.
Example of SSA
Static Single Assignment (SSA) Form...
• In some cases, The same variable may be defined in two
different control-flow paths in a program.

• For example, the source program


Static Single Assignment (SSA) Form...
• Here is where the second distinctive aspect of SSA comes into
play.
• SSA uses a notational convention called the Φ-function to
combine the two definitions of x.
Example-2
Example-3
Home Work: Find SSA for Following Code

You might also like