non-simple extensions to tiny

8
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 S1 E Sn . . . L2(CL?) S1 S2 Sn E COND L1 L2 L1

Upload: lexi

Post on 05-Jan-2016

17 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Non-simple Extensions to Tiny

Non-simple Extensions to Tiny

• The “repeat” Statement

• Constrainer: E must be boolean; process S1 , … Sn

• Code Generator: Cascade CurrLabel through S1, … Sn

repeat

S1 ESn. . .

L2(CL?) S1 S2 … Sn E COND L1 L2 L1

Page 2: Non-simple Extensions to Tiny

Loop-pool statement w/exit

loop

S1 Sn

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 ‘<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);

exit

Page 3: Non-simple Extensions to Tiny

Loop-pool statement w/exit

loop

S1 Sn

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

Page 4: Non-simple Extensions to Tiny

Pascal’s for loop (upto only)

upto

<id>

iI F S

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;

enclosing ‘for’

No exit allowed

Page 5: Non-simple Extensions to Tiny

Pascal’s for loop (cont’d)

upto

<id>

iI F S

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

Page 6: Non-simple Extensions to Tiny

Pascal’s for loop (cont’d)

upto

<id>

iI F S

for i := I to F do S CL F I ST iL1 DUP LD i BOP BGE COND L2 L3L2 S LD i UNOP USUCC ST i GOTO L1L3 POP 1 LIT 0 ST i

CodeGenerator:

Generate code (duh, see diagram)

Remember: ProcessNode always returns a label

return Nolabel

Page 7: Non-simple Extensions to Tiny

Case statementcase v of 1..3: S1; 2: S2; otherwise S3end;

case

Ecase_clause otherwisecase_clause. . .

CL1 S1 CLn Sn S

optional

two possibilities:

<integer>

n

..

<integer> <integer>

l u

or

Constrainer:

Assume E is correct, compare with others.

Page 8: Non-simple Extensions to Tiny

Case Statementcase

Ecase_clause otherwisecase_clause. . .

CL1 S1 CLn Sn S

optional

CL E CL1 COND L1 L2L1 POP 1 S1 GOTO LE

L2 CL2 COND L3 L4L3 POP 1 S2 GOTO LE

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

L DUP LIT n BOP BEQ

Each CLi is one of these two

no ‘otherwise’

L2n POP 1 SLE

L2n POP 1

LE

‘otherwise’

OR