Advanced Code Generation Parameter Passing Short-circuit boolean - - PowerPoint PPT Presentation

advanced code generation
SMART_READER_LITE
LIVE PREVIEW

Advanced Code Generation Parameter Passing Short-circuit boolean - - PowerPoint PPT Presentation

Advanced Code Generation Parameter Passing Short-circuit boolean evaluation Arrays Classes Advanced Code Generation Parameter Passing Short-circuit boolean evaluation Arrays Classes Im away this


slide-1
SLIDE 1

Advanced Code Generation

  • Parameter Passing
  • Short-circuit boolean evaluation
  • Arrays
  • Classes
slide-2
SLIDE 2

Advanced Code Generation

  • Parameter Passing
  • Short-circuit boolean evaluation
  • Arrays
  • Classes
  • I’m away this coming Thursday (lecture) and Friday (lab hours).
  • Will have substitutes for both
  • Instruction Scheduling (look ahead in slides/notes)
  • Will be on quiz and/or final
  • I’m going to a research conference, ACM SIGCSE
  • Special Interest Group on Computer Science Education
  • Presenting a paper there with Beth Simon and a student
  • n Ubiquitous Presenter
  • I’m away this coming Thursday (lecture) and Friday (lab hours).
  • Will have substitutes for both
  • Instruction Scheduling (look ahead in slides/notes)
  • Will be on quiz and/or final
  • I’m going to a research conference, ACM SIGCSE
  • Special Interest Group on Computer Science Education
  • Presenting a paper there with Beth Simon and a student
  • n Ubiquitous Presenter
slide-3
SLIDE 3

Parameter Passing

For call, need to put args at right place

  • n the stack, without using ‘new’ FP

w := qsort(A, lo+mid, hi) Let’s take model-driven approach (Remember, I’m not doing SPARC)

mid (locals & temps)

  • ther registers

(state)

  • ld sp
  • ld fp

return pc high low A

return value

sp sp

  • fp

fp

  • hi

lo A mid (locals & temps)

  • ther registers

(state)

  • ld sp
  • ld fp

return pc

return value

sp sp

  • fp

fp

slide-4
SLIDE 4

Parameter Passing

Des ::= Des ‘(‘ A ‘)’ A ::= A ‘,’ E | E <T1 = lo + mid> ; E ld &A, r1 ; A:E st r1, [sp + -4] ; A:E ld T1, r1 ; A:E “load” st r1, [sp + -8] ; A:E “store” ld hi, r1 ; A:E st r1, [sp + -12] ; A:E call qsort ; Des:Des(A) ld [sp + -16], r1 ; Des:Des(A) st r1, T2 ; Des:Des(A) ld T2, r1 ; S : R := E /E st r1, w ; S : R := E /R :=

String r1 = Machine.getReg() foreach a := STO in argument list, p := STO in parameter list do Machine.emitLoad(a, r1); Machine.emitStoreArg(r1, p); end String r1 = Machine.getReg() foreach a := STO in argument list, p := STO in parameter list do Machine.emitLoad(a, r1); Machine.emitStoreArg(r1, p); end

  • What to do for retrieving

return val?

  • What to do for args passed

in regs, like SPARC?

  • What to do for retrieving

return val?

  • What to do for args passed

in regs, like SPARC?

w := qsort(A, lo+mid, hi)

slide-5
SLIDE 5

Short-Circuit Boolean Evaluation

Some nice ‘syntactic sugar’ for programmer:

if (x != 0 && y/x > 2) (* when 1st fails, don't test 2nd *) S1 if (y == 0 || x/y < 0) (* when 1st succeeds, don't test 2nd *) 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?

slide-6
SLIDE 6

Short-Circuit Boolean Evaluation

Some nice ‘syntactic sugar’ for programmer:

if (x != 0 && y/x > 2) (* when 1st fails, don't test 2nd *) S1 if (y == 0 || x/y < 0) (* when 1st succeeds, don't test 2nd *) 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?

  • 1. Replicate S2 - sounds complicated (e.g., whether to dup

code is an inherited attribute, need a code buffer).

  • 2. Since generating assembly, have both pieces of code

jump to the same single piece of code?

  • 3. Short-circuit booleans are expressions, not statements,

so produce a value, not control flow. We abandoned our model-driven approach!

  • 1. Replicate S2 - sounds complicated (e.g., whether to dup

code is an inherited attribute, need a code buffer).

  • 2. Since generating assembly, have both pieces of code

jump to the same single piece of code?

  • 3. Short-circuit booleans are expressions, not statements,

so produce a value, not control flow. We abandoned our model-driven approach!

slide-7
SLIDE 7

Mode-Driven Short-Circuit Evaluation

E ::= E T_AND E | 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 st 0, T3 ; 0 FALSE goto L2 L1: st 1, T3 ; 1 TRUE L2: ... if (y == 0 || x/y < 0) S2 if (y == 0 || x/y < 0) S2

slide-8
SLIDE 8

Mode-Driven Short-Circuit Evaluation

E ::= E T_AND E | 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 st 0, T3 ; 0 represents FALSE goto L2 L1: st 1, T3 ; 1 represents TRUE L2: ...

E ::= E:e1 T_OR {: orTrueStack.push(Machine.newLabel());

  • rFalseStack.push(Machine.newLabel());

RESULT = new ExprSTO(TypeValues.bool); Machine.emitCond(e1, orTrueStack.top()); // if true, jump to set true :} E:e2 {: Machine.emitCond(e2, orTrueStack.top()); // if true, jump, to set true Machine.emitSetFalse(RESULT); // fall through to set false Machine.emitGoto(orFalseStack.top()); Machine.emitLabel(orTrueStack.top()); Machine.emitSetTrue(RESULT); Machine.emitLabel(orFalseStack.pop()); Machine.emitLabel(orTrueStack.pop()); :} E ::= E:e1 T_OR {: orTrueStack.push(Machine.newLabel());

  • rFalseStack.push(Machine.newLabel());

RESULT = new ExprSTO(TypeValues.bool); Machine.emitCond(e1, orTrueStack.top()); // if true, jump to set true :} E:e2 {: Machine.emitCond(e2, orTrueStack.top()); // if true, jump, to set true Machine.emitSetFalse(RESULT); // fall through to set false Machine.emitGoto(orFalseStack.top()); Machine.emitLabel(orTrueStack.top()); Machine.emitSetTrue(RESULT); Machine.emitLabel(orFalseStack.pop()); Machine.emitLabel(orTrueStack.pop()); :}

if (y == 0 || x/y < 0) S2 if (y == 0 || x/y < 0) S2