80 likes | 161 Views
repeat. S1. Sn. E. . . . 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. L2(CL?) S1 S2 … Sn E
E N D
repeat S1 Sn E . . . Non-simple Extensions to Tiny • The “repeat” Statement • Constrainer: E must be boolean; process S1 , … Sn • Code Generator: Cascade CurrLabel through S1, … Sn L2(CL?) S1 S2 … Sn E COND L1 L2 L1
loop S1 Sn Loop-pool statement w/exit loop n=read; if (n=0)then exit else output(n); pool; 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 NodeName(Temp) <> LoopNode then Error -- Decorate(T,Temp); Decorate(Temp,T);
loop S1 Sn Loop-pool statement w/exit L2(CL?) S1 S2 … Sn GOTO L2 L1 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 GOTOL1
upto <id> I F S i Pascal’s for loop (upto only) enclosing ‘for’ 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 <id> 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; No exit allowed
upto <id> I F S i Pascal’s for loop (cont’d) enclosing ‘for’ for i := I to F do S Prohibit assignment to i 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
upto <id> I F S i Pascal’s for loop (cont’d) 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
optional case E case_clause . . . case_clause otherwise CL1 S1 CLn Sn S .. <integer> <integer> l u Case statement case v of 1..3: S1; 2: S2; otherwise S3 end; two possibilities: <integer> or Constrainer: Assume E is correct, compare with others. n
optional case . . . E case_clause case_clause otherwise CL1 S1 CLn Sn S L DUP LIT n BOP BEQ CL E CL1 COND L1 L2 L1 POP 1 S1 GOTO LE L2 CL2 COND L3 L4 L3 POP 1 S2 GOTO LE Case Statement ... Ln CLn COND L2n-1 L2n L2n-1 POP 1 Sn GOTO LE L DUP DUP LIT l BOP BGE SWAP LIT u BOP BLE BOP BAND L2n POP 1 LE L2n POP 1 S LE Each CLi is one of these two OR no ‘otherwise’ ‘otherwise’