]
>
Loops
6.8 Loops
switch(i){
case 16.0: INFO[top-1] = INFO[top]
top--
case 17.0: temp = newtemp()
NEWQUAD(4, SYMTBL[temp,3], - loc_of_1_in_int_tbl, 0)
top++
INFO[top] = temp
case 15.0: temp = newtemp()
NEWQUAD(4, SYMTBL[ INFO[top-6], 3],
SYMTBL[ INFO[top-4], 3], 0) // id = init
NEWQUAD(7, ADDR + 28, SYMTBL[ INFO[top-3], 3], 0)// goto check_gt
NEWQUAD(5, ADDR + 12, , 0) // goto check_lt
/*next:*/ INFO[top-7] = ADDR
NEWQUAD(0, SYMTBL[ INFO[top-6], 3],
SYMTBL[ INFO[top-6], 3],
SYMTBL[ INFO[top-3], 3]) // id += step
NEWQUAD(7, ADDR + 16, SYMTBL[ INFO[top-3], 3], 0)// goto check_gt
/*check_lt:*/ NEWQUAD(0, SYMTBL[ temp1, 3],
SYMTBL[ INFO[top-1], 3],
SYMTBL[ INFO[top-6], 3]) // temp = bound - id
NEWQUAD(7, ???, SYMTBL[ temp1, 3], 0) // goto out
NEWQUAD(5, ADDR + 12 , 0, 0) // goto body
/*check_gt:*/ NEWQUAD(0, SYMTBL[ temp1, 3],
SYMTBL[ INFO[top-1], 3],
SYMTBL[ INFO[top-6], 3]) // temp = id - bound
NEWQUAD(7, ??? , SYMTBL[ temp1, 3], 0) // goto out
/*body:*/
top -= 6
case 12.0: NEWQUAD(5, INFO[top], 0, 0)
MEM[ INFO[top-2] + 13 ] = ADDR
MEM[ INFO[top-2] + 21 ] = ADDR
top -= 2
}
12. < stat1 > → < for> < body> NEXT
15. < for> → FOR < id> = < expr> < step> TO < expr> DO
16. < step> → STEP < expr>
17. → ϵ
init = ...
step = ...
bound = ...
id = init
if step > 0 goto check_gt
goto check_lt
next: id += step
if step > 0 goto check_gt
check_lt: if id < bound goto out
goto body
check_gt: if id > bound goto out
body: ...
goto next
out:
Instruction
Interpretation
5 OP1 — — LC ← OP1
6 OP1 OP2 — if MEM[OP2+BR]=0 then LC ← OP1
7 OP1 OP2 — if MEM[OP2+BR] > 0 then LC ← OP1