w7Color - Loop Processing While-Stmt WHILE #start B-expr...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Loop Processing While-Stmt WHILE #start B-expr #test LOOP Stmts ENDLOOP #finish For-Stmt FOR Id #enter IN Range #init LOOP Stmts ENDLOOP #finish Semantic record : struct while_stmt { string top_label; string out_label; } Semantic record : struct for_stmt { data_object id; data_object limit_val; string next_label, out_label; boolean reverse_flag; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 While loops While-Stmt WHILE #start B-expr #test LOOP Stmts ENDLOOP #finish Create a label at the top of the loop Evaluate B-expr, if false jump to end of the loop Evaluate Stmts and branch to label at the top of the loop. Add a label at the end of the loop to jump to when B- expr is false. Semantic record : struct while_stmt { string top_label; string out_label; }
Background image of page 2
3 While loop semantic routines While-Stmt WHILE #start B-expr #test LOOP Stmts ENDLOOP #finish #start creates a new top_label and adds it to while_stmt and generates LABEL: top_label #test generates code to test value of B-expr, creates the out_label , and generates JUMP0 out_label to jump to out_label if B_expr is false #finish generates JUMP top_label and LABEL: out_label Semantic record : struct while_stmt { string top_label; string out_label; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 For loop processing For-Stmt FOR Id #enter IN reverse_option range #init LOOP Stmts ENDLOOP #finish Reverse_option #set_in Reverse_option reverse #set_reverse What we want to do (in the “not reversed”) case Generate code to initialize id (the loop index) Set id to “first” value in the range, jump to end if range is empty Generate “top of loop” label Execute Stmts, increment index, jump to “top of loop” next_label if still in range Insert “end of loop” out_label Semantic record : struct for_stmt { data_object id; data_object limit_val; string next_label, out_label; ( boolean reverse_flag; ) }
Background image of page 4
5 For loop coping issues • Need to create a scoping block for loop identifier: consider for K in K. .10 loop • Need to keep track of whether loop goes up or down through the range • Loop index can only be changed by loop increment code, should not be changeable by other program statements (flag as index in symbol table) – This varies by language – C/Java for example treat index like a regular variable
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 For loop code example For-Stmt FOR Id #enter IN reverse_option range #init LOOP Stmts ENDLOOP #finish reverse option #set_in reverse option reverse #set_reverse Compute LB Compute UB GT, LB, UB, t1 JUMP1, t1, OUT ASSIGN, LB, Index ASSIGN, UB, Limit Next: Stmts EQ, INDEX, Limit, t2 JUMP1, t2, OUT ADDI, Index, 1, Index JUMP, Next OUT: Code generated for the non-reversed case
Background image of page 6
Reversed For loop code example For-Stmt FOR Id #enter IN reverse_option range #init LOOP Stmts ENDLOOP #finish reverse option #set_in reverse option reverse #set_reverse Compute LB Compute UB GT, LB, UB, t1 JUMP1, t1, OUT ASSIGN, LB, Limit ASSIGN, UB, Index NEXT: Stmts EQ, Index, Limit, t2 JUMP1, t2, OUT SUBI, Index, 1, Index
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

w7Color - Loop Processing While-Stmt WHILE #start B-expr...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online