Nonsimple Extensions to Tiny The repeat Statement repeat

  • Slides: 8
Download presentation
Non-simple Extensions to Tiny • The “repeat” Statement repeat S 1 . . .

Non-simple Extensions to Tiny • The “repeat” Statement repeat S 1 . . . Sn E L 2(CL? ) S 1 S 2 … Sn E COND L 1 L 2 L 1 • Constrainer: E must be boolean; process S 1 , … Sn • Code Generator: Cascade Curr. Label through S 1, … Sn

Loop-pool statement w/exit loop n=read; if (n=0)then exit else output(n); pool; loop S 1.

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

Loop-pool statement w/exit loop L 1 loop n=read; if (n=0)then exit else output(n); pool;

Loop-pool statement w/exit loop L 1 loop n=read; if (n=0)then exit else output(n); pool; S 1 . . . Sn L 2(CL? ) S 1 S 2 … Sn GOTO L 2 L 1 Code Generator: At ’loop’ node: -- Decorate (T, L 1=Makelabel); -- Generate code (diagram); At ‘exit’ node: -- L 1=Decoration(T)); -- Code. Gen 1(GOTOOP, L 1, Currlabel) exit GOTO L 1

Pascal’s for loop (upto only) enclosing ‘for’ upto for i : = I to

Pascal’s for loop (upto only) enclosing ‘for’ upto for i : = I to F do S <id> Constrainer: i I F 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 <id> has correct type. while Node. Name(Temp) != Program. Node if (NN(FK(FK(Temp)) = NN(FK(FK(T)) then Error Temp = Decoration(Temp) Close_scope; S No exit allowed // i must be different // from lcv’s of all // enclosing for loops

Pascal’s for loop (cont’d) enclosing ‘for’ upto for i : = I to F

Pascal’s for loop (cont’d) enclosing ‘for’ upto for i : = I to F do S <id> i I F S Prohibit assignment to i Constrainer: At ‘assign’ node: Temp = Lookup(FOR_CTXT); while Node. Name(Temp) != Program. Node 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

Pascal’s for loop (cont’d) for i : = I to F do S upto

Pascal’s for loop (cont’d) for i : = I to F do S upto <id> i I F S Code. Generator: Generate code (duh, see diagram) Remember: Process. Node always returns a label return Nolabel CL F I ST i L 1 DUP LD i BOP BGE COND L 2 L 3 L 2 S LD i UNOP USUCC ST i GOTO L 1 L 3 POP 1 LIT 0 ST i

Case statement case v of 1. . 3: S 1; 2: S 2; otherwise

Case statement case v of 1. . 3: S 1; 2: S 2; otherwise S 3 end; optional case_clause. . . case_clause otherwise E CL 1 S 1 Assume E is correct, compare with others. Sn . . two possibilities: Constrainer: CLn S <integer> or <integer> n l u

CL E CL 1 COND L 1 L 2 L 1 POP 1 S

CL E CL 1 COND L 1 L 2 L 1 POP 1 S 1 GOTO LE L 2 COND L 3 L 4 L 3 POP 1 S 2 GOTO LE Case Statement case_clause. . . case_clause otherwise E . . . Ln L 2 n-1 L 2 n CL 1 CLn COND L 2 n-1 L 2 n POP 1 Sn GOTO LE POP 1 S LE ‘otherwise’ OR optional case S 1 CLn Sn L DUP LIT l BOP BGE SWAP Each CLi is one of LIT u BOP BLE these two BOP BAND L DUP LIT n BOP BEQ L 2 n LE POP 1 no ‘otherwise’ S