Compilation 2014
Basic Blocks
Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst
Basic Blocks Aslan Askarov aslan@cs.au.dk Revised from slides by E. - - PowerPoint PPT Presentation
Compilation 2014 Basic Blocks Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst IR = Intermediate IR does not match either end perfectly Source: Translation non-trivial, e.g., using the static link
Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst
MOVE(TEMP t, ESEQ…) MOVE(BINOP…, TEMP s)
MOVE(MEM…,CALL…) ESEQ(MOVE…, TEMP t) SEQ(MEM…,SEQ(…,SEQ…))
Only a subset of type correct terms used
signature CANON = sig val linearize: stm -> stm list val basicBlocks: stm list -> (stm list list * Temp.label) val traceSchedule: stm list list Temp.label -> stm list end structure Canon: CANON = struct ... end
tree IR will be replaced by another tree IR
nodes, bind subtrees to metavariables
ESEQ ESEQ s1 s2 e ESEQ SEQ e s2 s1
BINOP ESEQ e2 s e1 ESEQ BINOP s e2
e1 MEM(ESEQ(s,e1)) ESEQ(s,MEM(e1)) JUMP(ESEQ(s,e1)) ESEQ(s,JUMP(e1)) CJUMP(op,ESEQ(s,e1) ,e2,l1,l2) SEQ(s,CJUMP(op,e1,e2 ,l1,l2))
BINOP ESEQ e1 s e2
ESEQ BINOP MOVE e2
TEMP TEMP t e1 t ESEQ s CJUMP(op,e1 ,ESEQ(s,e2) ,l1,l2) SEQ(MOVE(TEMP t,e1) ,SEQ(s ,CJUMP(op,TEMP t ,e2,l1,l2)
BINOP ESEQ e1 s e2
BINOP e2
e1 ESEQ s CJUMP(op,e1 ,ESEQ(s,e2) ,l1,l2) SEQ(s,CJUMP(op,e1,e2 ,l1,l2)) s,e1 commute s,e1 commute
val reorderStm: exp list * (exp list -> stm) -> stm val reorderExp: exp list * (exp list -> exp) -> (stm*exp) fun doStm (T.JUMP(e,labs)) = reorderStm ([e], fn [e] => T.JUMP(e,labs)) | doStm (T.CJUMP(p,a,b,t,f)) = reorderStm ([a,b], fn [a,b] => T.CJUMP(p,a,b,t,f)) | doStm (T.MOVE(T.TEMP t, b)) = reorderStm ([b], fn [b] => T.MOVE(T.TEMP t, b)) ... and doExp (T.BINOP(p,a,b)) = reorderExp ([a,b], fn [a,b] => T.BINOP(p,a,b)) | doExp (T.MEM(a)) = reorderExp ([a], fn [a] => T.MEM(a)) ...
CALL(f,args)) ESEQ(MOVE(TEMP t ,CALL(f,args)) ,TEMP t)
SEQ(SEQ(a,b),c)) SEQ(a,SEQ(b,c)) a::(b::c) SEQ(a,SEQ(b,c))
fun linearize (stm0: stm): stm list = let definitions of reorderExp, reorderStm, doExp, … fun linear (T.SEQ(a,b),l) = linear(a,linear(b,l)) | linear (s,l) = s::l in linear(doStm stm0, nil) end
values, just “movement” (*JUMP , step)
‘done’ label at very end
consecutively (choice: CJUMP)
‘false’ label, thus enabling fall through
Put all blocks of the program into a list Q. while Q is not empty Start a new (empty) trace, call it T Remove the head element b from Q. while b is not marked Mark b; append b to the end of the current trace T. Examine the successors of b; if there is any unmarked successor c b := c end current trace T.
prologue statements JUMP (NAME test) LABEL test CJUMP (>,i,N,done,body) LABEL body loop body statements JUMP(NAME test) LABEL done epilogue statements prologue statements JUMP (NAME test) LABEL test CJUMP(<=,i,N,done,body) LABEL done epilogue statements LABEL body loop body statements JUMP(NAME test) prologue statements JUMP (NAME test) LABEL body loop body statements JUMP(NAME test) LABEL test CJUMP (>,i,N,done,body) LABEL done epilogue statements
basicBlocks, traceSchedule)
subset of type correct trees, carefully..
canon.sml:81.30-81.54 Warning: match nonexhaustive e :: nil => ... canon.sml:83.32-83.62 Warning: match nonexhaustive a :: b :: nil => ...
source, nor for target
ensure parent EXP(…) or MOVE(TEMP t, …)
ESEQ, pull ESEQ over expression, ditto for free