100 likes | 114 Views
Three-Address Implementation. Triples Quads N-tuples (my choice) Lhs = oper(op 1 , op 2 , …, op n ) Lhs = call(func, arg 1 , arg 2 , … arg n ) If condOper(op 1 , op 2 , Label) br Label. a = ((c-1) * b) + (-c * b). Example “ Source ”. t1 = c - 1 t2 = b * t1 t3 = -c t4 = t3 * b
E N D
Three-Address Implementation • Triples • Quads • N-tuples (my choice) • Lhs = oper(op1, op2, …, opn) • Lhs = call(func, arg1, arg2, … argn) • If condOper(op1, op2, Label) • br Label
a = ((c-1) * b) + (-c * b) Example “Source”
t1 = c - 1 t2 = b * t1 t3 = -c t4 = t3 * b t5 = t2 + t4 a = t5 Example 3-Address
Control Flow Graph • Nodes are Basic Blocks • Single entry, single exit • No branch exempt (possibly) at bottom • Edges represent one possible flow of execution between two basic blocks • Whole CFG represents a function
begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1 10 A[i] = random(); Do 20 i = 1, 9, 1 Do 20 j = 1, 9, 1 20 if( A[j] > A[j+1]) swap(j); } Bubble Sort
int swap(int i) { int temp; temp = A[i]; A[i] = A[i+1]; A[i+1] = temp; } end; Bubble Sort (cont.)
Building CFG • Starting with 3-addr code • Each leader starts a basic block which ends immediately before the next leader • ID “leaders” (heads of basic blocks) • First statement is a leader • Any statement that is the target of a conditional of unconditional goto is a leader • Any statement immediately following a goto or conditional goto is a leader • Each leader starts a basic block which ends immediately before the next leader
Live Variables – Basic Block • For each block, BB, compute • Written, read, readB4written • Initialize each BB • Live-in = readB4written, Live-out = Ø • Repeat until neither live-in, live-out change • For each BB • Live-out(BB) U= U Live-in(Successor) • Live-in(BB) U= Live-out(BB) – written(BB)
Live Variables – Statement • For each block, BB, compute, in any order • Maintain “local” set, Live • Live = BB->live_out • For each Statement, S, in BB, in reverse order • Live = Live – S->written • S->live = Live • Live = Live U S->read