Presentation is loading. Please wait.

Presentation is loading. Please wait.

Non-simple Extensions to Tiny The “repeat” Statement Constrainer: E must be boolean; process S 1, … S n Code Generator: Cascade CurrLabel through S 1,

Similar presentations


Presentation on theme: "Non-simple Extensions to Tiny The “repeat” Statement Constrainer: E must be boolean; process S 1, … S n Code Generator: Cascade CurrLabel through S 1,"— Presentation transcript:

1 Non-simple Extensions to Tiny The “repeat” Statement Constrainer: E must be boolean; process S 1, … S n Code Generator: Cascade CurrLabel through S 1, … S n repeat S1ESn... L2(CL?) S1 S2 … Sn E COND L1 L2 L1

2 Loop-pool statement w/exit loop S1Sn loop n=read; if (n=0)then exit else output(n); pool;... Constrainer: At ’program’ node, DTEnter(LOOP_CTXT,T,T) At ‘loop’ node: -- Open_Scope; -- DTEnter(LOOP_CTXT,T,T); // LOOP_CTXT is ‘ ’ -- Process kids; -- Close_scope; -- if (Decoration(T)=0) print (‘Warning: no ‘exit’) At ‘exit’ node: -- Temp = Lookup(LOOP_CTXT); -- if NodeName(Temp) <> LoopNode then Error -- Decorate(T,Temp); Decorate(Temp,T); exit

3 Loop-pool statement w/exit loop S1S1 SnSn L2(CL?) S1 S2 … Sn GOTO L2 L1 loop n=read; if (n=0)then exit else output(n); pool;... Code Generator: At ’loop’ node: -- Decorate (T, L1=Makelabel); -- Generate code (diagram); At ‘exit’ node: -- L1=Decoration(Decoration(T)); -- CodeGen1(GOTOOP, L1, Currlabel) exit GOTO L1 L1

4 Pascal’s for loop (upto only) upto i IFS for i := I to F do S Constrainer: At ‘program’ node: DTEnter(FOR_CTXT, T); At ‘upto’ node: Temp = Lookup(FOR_CTXT); Decorate (T,Temp); Open_scope; DTEnter(FOR_CTXT,T); DTEnter(LOOP_CTXT); // disallows “exit” Process kids // assume has correct type. while NodeName(Temp) != ProgramNode // i must be different if (NN(FK(FK(Temp)) = NN(FK(FK(T)) then Error // from lcv’s of all Temp = Decoration(Temp) // enclosing for loops Close_scope; enclosing ‘for’ No exit allowed

5 Pascal’s for loop (cont’d) upto i IFS for i := I to F do S Constrainer: At ‘assign’ node: Temp = Lookup(FOR_CTXT); while NodeName(Temp) != ProgramNode if (NN(FK(FK(Temp)) = NN(FK(FK(T)) then Error // x (left of assign) Temp = Decoration(Temp) // cannot match // the lcv of any // enclosing for loop enclosing ‘for’ Prohibit assignment to i

6 Pascal’s for loop (cont’d) upto i IFS for i := I to F do S CL F I ST i L1 DUP LD i BOP BGE COND L2 L3 L2 S LD i UNOP USUCC ST i GOTO L1 L3 POP 1 LIT 0 ST i CodeGenerator: Generate code (duh, see diagram) Remember: ProcessNode always returns a label return Nolabel

7 Case statement case v of 1..3: S1; 2: S2; otherwise S3 end; case E case_clauseotherwisecase_clause... CL1S1CLnSnS optional two possibilities: n.. lu or Constrainer: Assume E is correct, compare with others.

8 Case Statement case E case_clauseotherwisecase_clause... CL1S1CLnSnS optional CL E CL1 COND L1 L2 L1 POP 1 S1 GOTO LE L2 CL2 COND L3 L4 L3 POP 1 S2 GOTO LE Ln CLn COND L 2n-1 L 2n L 2n-1 POP 1 Sn GOTO LE... L DUP DUP LIT l BOP BGE SWAP LIT u BOP BLE BOP BAND L DUP LIT n BOP BEQ Each CLi is one of these two no ‘otherwise’ L 2n POP 1 S LE L 2n POP 1 LE ‘otherwise’ OR


Download ppt "Non-simple Extensions to Tiny The “repeat” Statement Constrainer: E must be boolean; process S 1, … S n Code Generator: Cascade CurrLabel through S 1,"

Similar presentations


Ads by Google