Docsity

Examples of Basic Blocks - Advanced Compiler Design - Lecture Slides, Slides of Computer Science

Toggle sidebar
bg1
Examples of Basic Blocks
Following sequence of three-address statements forms a basic block:
t1 : = a * a
t2 := a * b
t3 := 2 * t2
t4 := t1+ t3
t5 := b * b
t6 := t4 + t5
It computes: a*a + 2*a*b + b*b
Given 3-AS x := y+z:
-it defines X
-it uses (reference) y and z
Live name: A name is a basic block is said to be live at a given point if its value
is used after that point in the program, perhaps in another basic block.
a b
t6 t7
+ *
+ *
*
*
a a 2
a b
b b
t3
t2
t5
t7
t1 t4
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Examples of Basic Blocks - Advanced Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Examples of Basic Blocks

Following sequence of three-address statements forms a basic block: t1 : = a * a t2 := a * b t3 := 2 * t t4 := t1+ t t5 := b * b t6 := t4 + t It computes: aa + 2ab + bb Given 3-AS x := y+z: -it defines X -it uses (reference) y and z Live name: A name is a basic block is said to be live at a given point if its value is used after that point in the program, perhaps in another basic block.** a b t6 t


a a 2 a b b b t t t t t t

Examples of 3 address code translation

C-code void quicksort(m,n) int m,n { int i,j; int v,x; if (n<=m) return; / fragment begins / i:=m-1; j=n ; v=a[n]; while(1) { do i=i+1; while(a[i]<v) do j=j-1; while(a[j]>v) if (i>=j) break; x=a[i];a[i]=a[n];a[n]=x;} x=a[i];a[i]=a[n];a[n]=x; / fragment ends / quicksort( m ,j); quicksort(l+1,n); } 3 Address Code. i = m-1; j = n ; v = a[n]; (1) i := m-l; j:=n; t1 := 4n; v := a[t1]; while(1) { do i = i + 1; while(a[i]<v) ; (5) i := i+1; t2 := 4i; t3 := a[t2]; (8) if t3<v goto(5) do j = j-1; while (a[j] > v); (9) j:=j+1; t4:=4j ; t5:=a[t4]; (12) if t5 >v goto (9) (13) if( i >=j) goto (23) x = a[i]; a[i] = a[n]; a[n]=x (14) t6 := 4i; x:=a[t6]; (16) t7 := 4i; x:=a[t6]; (18) t9:= a[t8]; a[t7]:=t9; (20) t10:= 4j; a[t10]:= x (22) goto(5) (23) ...

Partition into Basic Blocks algorithm

Input : A sequence of 3 address statements Output: A sequence of basic blocks with each 3A Statement in exactly one block. Method: (1) First determine a set of leaders, the 1st statement of basic blocks: a) The first statement is a leader b) Any statement that is a target of a conditional or unconditional goto is a leader c) Any statement that immediately follows a goto, or conditional goto statement is a leader. (2) For each leader its basic block consists of: a) The leader b) All statements upto but not including the next leader or the end of the program.

Example of Partition into Basic Blocks

(1) prod : = 0 (2) i : = 1 (3) t1 : = 4 * i (4) t2 : = a[t1] (5) t3 : = 4 * i (6) t4 : = b[t3] (7) t5 : = t2 * t (8) t6 : = prod + t (9) prod : = t (10) t7 : = i+ (11) i : = t (12) if( i <=20) goto (3)