Advanced Code Generation • Parameter Passing • Short-circuit boolean evaluation • Arrays • Classes
Advanced Code Generation • Parameter Passing • Short-circuit boolean evaluation • Arrays • Classes • I’m away this coming Thursday (lecture) and Friday (lab hours). • I’m away this coming Thursday (lecture) and Friday (lab hours). • Will have substitutes for both • Will have substitutes for both - Instruction Scheduling (look ahead in slides/notes) - Instruction Scheduling (look ahead in slides/notes) - Will be on quiz and/or final - Will be on quiz and/or final • I’m going to a research conference, ACM SIGCSE • I’m going to a research conference, ACM SIGCSE - Special Interest Group on Computer Science Education - Special Interest Group on Computer Science Education - Presenting a paper there with Beth Simon and a student - Presenting a paper there with Beth Simon and a student on Ubiquitous Presenter on Ubiquitous Presenter
Parameter Passing � sp � sp mid For call, need to put args at right place (locals & temps) on the stack, without using ‘new’ FP other registers (state) old sp old fp return pc return value w := qsort(A, lo+mid, hi) hi lo A fp � � fp � sp � sp mid (locals & temps) other registers (state) old sp old fp Let’s take model-driven approach return pc return value high (Remember, I’m not doing SPARC) low fp � � A fp
Parameter Passing w := qsort(A, lo+mid, hi) Des ::= Des ‘(‘ A ‘)’ String r1 = Machine.getReg() String r1 = Machine.getReg() A ::= A ‘,’ E | E foreach a := STO in argument list, foreach a := STO in argument list, p := STO in parameter list do p := STO in parameter list do Machine.emitLoad(a, r1); <T1 = lo + mid> ; E Machine.emitLoad(a, r1); Machine.emitStoreArg(r1, p); Machine.emitStoreArg(r1, p); ld &A, r1 ; A:E end end st r1, [sp + -4] ; A:E ld T1, r1 ; A:E “load” st r1, [sp + -8] ; A:E “store” ld hi, r1 ; A:E • What to do for retrieving • What to do for retrieving st r1, [sp + -12] ; A:E return val? return val? call qsort ; Des:Des(A) • What to do for args passed ld [sp + -16], r1 ; Des:Des(A) • What to do for args passed in regs, like SPARC? in regs, like SPARC? st r1, T2 ; Des:Des(A) ld T2, r1 ; S : R := E /E st r1, w ; S : R := E /R :=
Short-Circuit Boolean Evaluation Some nice ‘syntactic sugar’ for programmer: (* when 1 st fails, don't test 2 nd *) if (x != 0 && y/x > 2) S1 (* when 1 st succeeds, don't test 2 nd *) if (y == 0 || x/y < 0) S2 Instead of: if (x != 0) if (y/x > 2) S1 if (y == 0) S2 else if (x/y < 0) S2 How about compiling directly with if/else like this?
Short-Circuit Boolean Evaluation Some nice ‘syntactic sugar’ for programmer: (* when 1 st fails, don't test 2 nd *) if (x != 0 && y/x > 2) S1 (* when 1 st succeeds, don't test 2 nd *) if (y == 0 || x/y < 0) S2 1. Replicate S2 - sounds complicated (e.g., whether to dup 1. Replicate S2 - sounds complicated (e.g., whether to dup Instead of: code is an inherited attribute, need a code buffer). code is an inherited attribute, need a code buffer). 2. Since generating assembly, have both pieces of code 2. Since generating assembly, have both pieces of code if (x != 0) jump to the same single piece of code? jump to the same single piece of code? if (y/x > 2) S1 3. Short-circuit booleans are expressions , not statements, 3. Short-circuit booleans are expressions , not statements, so produce a value, not control flow. so produce a value, not control flow. if (y == 0) S2 We abandoned our model-driven approach! We abandoned our model-driven approach! else if (x/y < 0) S2 How about compiling directly with if/else like this?
Mode-Driven Short-Circuit Evaluation if (y == 0 || x/y < 0) if (y == 0 || x/y < 0) S2 E ::= E T_AND E S2 | E T_OR E Target code: < T1 := y = 0 > ; E : E = E ld T1, r1 ; E OR {} E if r1 goto L1 < T2 := x/y < 0> ; E : E < E ld T2, r2 ; E OR E {} if r2 goto L1 ; 0 � FALSE st 0, T3 goto L2 ; 1 � TRUE L1: st 1, T3 L2: ...
Mode-Driven Short-Circuit Evaluation if (y == 0 || x/y < 0) if (y == 0 || x/y < 0) S2 S2 E ::= E T_AND E E ::= E:e1 T_OR E ::= E:e1 T_OR | E T_OR E {: orTrueStack.push(Machine.newLabel()); {: orTrueStack.push(Machine.newLabel()); orFalseStack.push(Machine.newLabel()); orFalseStack.push(Machine.newLabel()); Target code: RESULT = new ExprSTO(TypeValues.bool); RESULT = new ExprSTO(TypeValues.bool); Machine.emitCond(e1, orTrueStack.top()); Machine.emitCond(e1, orTrueStack.top()); < T1 := y = 0 > ; E : E = E // if true, jump to set true // if true, jump to set true ld T1, r1 ; E OR {} E :} :} E:e2 if r1 goto L1 E:e2 {: Machine.emitCond(e2, orTrueStack.top()); {: Machine.emitCond(e2, orTrueStack.top()); < T2 := x/y < 0> ; E : E < E // if true, jump, to set true // if true, jump, to set true ld T2, r2 ; E OR E {} Machine.emitSetFalse(RESULT); Machine.emitSetFalse(RESULT); if r2 goto L1 // fall through to set false // fall through to set false st 0, T3 ; 0 represents FALSE Machine.emitGoto(orFalseStack.top()); Machine.emitGoto(orFalseStack.top()); Machine.emitLabel(orTrueStack.top()); goto L2 Machine.emitLabel(orTrueStack.top()); Machine.emitSetTrue(RESULT); Machine.emitSetTrue(RESULT); L1: st 1, T3 ; 1 represents TRUE Machine.emitLabel(orFalseStack.pop()); Machine.emitLabel(orFalseStack.pop()); L2: ... Machine.emitLabel(orTrueStack.pop()); :} Machine.emitLabel(orTrueStack.pop()); :}
Recommend
More recommend