Download presentation
Presentation is loading. Please wait.
Published byBertha Philippa Moody Modified over 9 years ago
1
c Chuen-Liang Chen, NTUCS&IE / 279 CONTROL STRUCTURES Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei, TAIWAN
2
c Chuen-Liang Chen, NTUCS&IE / 280 Relevant grammar if #start_if #if_test then { elseif #gen_jump #gen_else_label #if_test then } end if ; #gen_out_label else #gen_jump #gen_else_label #gen_else_label loop #gen_loop_label end loop ; #loop_back while #start_while #while_test loop end loop ; #finish_while for #enter_for_id in #init_loop loop end loop ; #finish_loop #set_in reverse #set_reverse
3
c Chuen-Liang Chen, NTUCS&IE / 281 Semantic routine construction process a suggestion grammar semantic record output for each production rule –generated code –semantic record action symbol –input/output parameters –triggering place semantic routine QUIZ: other approaches QUIZ: review semantic routines constructed before
4
c Chuen-Liang Chen, NTUCS&IE / 282 If statement (1/3) generated code if then{ Evaluate } ( JUMP0,, Else1 ) elseif then{ code for } ( JUMP, Out )...( LABEL, Else1 ) elseif then{ Evaluate } ( JUMP0,, Else2 ) else { code for } end if;( JUMP, Out )... /* italic -- optional */( LABEL, ElseN-1 ) { Evaluate } ( JUMP0,, ElseN ) { code for } ( JUMP, Out ) ( LABEL, ElseN ) { code for } ( LABEL, Out )
5
c Chuen-Liang Chen, NTUCS&IE / 283 If statement (2/3) relevant grammar if #start_if #if_test then { elseif #gen_jump #gen_else_label #if_test then } end if ; #gen_out_label else #gen_jump #gen_else_label #gen_else_label semantic record struct if_stmt { string out_label, next_else_label; } ;
6
c Chuen-Liang Chen, NTUCS&IE / 284 If statement (3/3) semantic routines start_if(void) => if { if (if_stmt) {.out_label = new_label();.next_else_label = “”; } } if_test (if, ) => if { Check.data_object.object_type == BOOLEAN if.if_stmt.next_else_label = new_label() generate(JUMP0,.data_object, if.if_stmt.next_else_label, “”); if the updated struct if_stmt } gen_jump (if) { generate(JUMP, if.if_stmt.out_label, “”, “”); } gen_else_label (if) { generate(LABEL, if.if_stmt.next_else_label, “”, “”); } gen_out_label (if) { generate(LABEL, if.if_stmt.out_label, “”, “”); }
7
c Chuen-Liang Chen, NTUCS&IE / 285 Loop statement generated code loop( LABEL, LoopStart ) { code for } end loop;( JUMP, LoopStart ) relevant grammar loop #gen_loop_label end loop ; #loop_back semantic record semantic routines struct label {gen_loop_label(void) => loop string label;{ } ;L = new_label(); generate(LABEL, L, “”, “”); loop (label) {.label = L; } } loop_back(loop) generate(JUMP, loop.label.label, “”, “”}; }
8
c Chuen-Liang Chen, NTUCS&IE / 286 While statement (1/3) generated code while loop( LABEL, LoopStart ) { code for } end loop;( JUMP0,, Out ) { code for } ( JUMP, LoopStart ) ( LABEL, Out ) relevant grammar while #start_while #while_test loop end loop ; #finish_while semantic record struct while_stmt { string top_label, out_label; } ;
9
c Chuen-Liang Chen, NTUCS&IE / 287 While statement (2/3) semantic routines start_while(void) => while { L = new_label(); generate(LABEL, L, “”, “”); while (while_stmt) {.top_label = L;.out_label = ""; } } while_test(while, ) => while { Check that.data_object.object_type == BOOLEAN while.while_stmt.out_label = new_label(); generate (JUMP0,.data_object, while.while_stmt.out_label, “” ); while the updated struct while_stmt } finish-while(while) { generate(JUMP, while.while_stmt.top_label, “”, “”); generate(LABEL, while.while_stmt.out_label, “”, “”); }
10
c Chuen-Liang Chen, NTUCS&IE / 288 While statement (3/3) ( LABEL, LoopStart )( JUMP, TestStart ) { code for }( LABEL, LoopStart ) ( JUMP0,, Out )v.s.{ code for } { code for }( LABEL, TestStart ) ( JUMP, LoopStart ){ code for } ( LABEL, Out )( JUMP1,, LoopStart )
11
c Chuen-Liang Chen, NTUCS&IE / 289 For statement (1/6) generated code for in loopfor in reverse loop<stmts>end loop;{ compute LowerBound }{ compute UpperBound }( GT, LowerBound, UpperBound, t1 )( JUMP1, t1, Out ) ( ASSIGN, LowerBound, Index )( ASSIGN, LowerBound, Limit ) ( ASSIGN, UpperBound, Limit )( ASSIGN, UpperBound, Index )( LABEL, Next ){ code for <stmts> }( EQ, Index, Limit, t2 )( JUMP1, t2, Out ) ( ADDI, Index, 1, Index )( SUBI, Index, 1, Index )( JUMP, Next )( LABEL, Out ) /* Index will be equal to Limit, if all iterations are executed completely */
12
c Chuen-Liang Chen, NTUCS&IE / 290 For statement (2/6) relevant grammar for #enter_for_id in #init_loop loop end loop ; #finish_loop #set_in reverse #set_reverse semantic record struct reverse { boolean reverse_flag; } ; struct for_stmt { data_object id; data_object limit_val; string next_label, out_label; boolean reverse_flag; } ;
13
c Chuen-Liang Chen, NTUCS&IE / 291 For statement (3/6) semantic routines enter_for_id ( ) { Open a new name scope Enter the identifier in the symbol table in the new scope with attributes indicating that it is "unavailable" } set_in(void) => { (reverse) {.reverse_flag = FALSE; } } set_reverse(void) => { (reverse) {.reverse_flag = TRUE; } }
14
c Chuen-Liang Chen, NTUCS&IE / 292 For statement (4/6) init _loop(,, ) => for { data_object upper, lower, init, limit; struct address T; Change the attributes of in the symbol table to make it "available" Let loop_info be a FORSTMT semantic record Allocate a temporary for use as the loop index, and create a data_object record, loop_info.id, for it. loop_info.id.object_type =.type_ref.object_type loop_info.id.addr.read_only = TRUE; Create data_object records upper and lower describing the upper and lower bounds of the index range, based on the struct constraint_des:.type_ref.object_type.constraint T = get_temporary(); loop_info.out_label = new_label(); generate(GT, lower, upper, T); generate(JUMP1, T, loop_info.out_label, “”); loop_info.reverse_flag =.reverse.reverse_flag;
15
c Chuen-Liang Chen, NTUCS&IE / 293 For statement (5/6) if (loop_info.reverse_flag) { init = upper; limit = lower; } else { init = lower; limit = upper; } generate(ASSIGN, loop_info.id, INTEGERSIZE, init); if (limit does not describe a static value) { Allocate a temporary to hold the loop limit, and create a data_object record, loop_info.limit_val, for it generate(ASSIGN, loop_info.limit_val, INTEGERSIZE, limit); } else loop_info.limit_val = limit; Update the attributes of in the symbol table to be consistent with loop_info.id loop_info.next_label = new_label(); generate(LABEL, loop_info.next_label, “”, “”); for loop_info; }
16
c Chuen-Liang Chen, NTUCS&IE / 294 For statement (6/6) finish_loop(for) { struct address T; T = get_temporary(); generate(EQ, for.for_stmt.id, for.for_stmt.limit_val, T); generate(JUMP1, T, for.for_stmt.out_label, “”); if (for.for_stmt.reverse_flag) generate(SUBI, for.for_stmt.id, 1, for.for_stmt.id); else generate(ADDI, for.for_stmt.id, 1, for.for_stmt.id); generate(JUMP, for.for_stmt.next_label, “”, “”); generate(LABEL, for.for_stmt.out_label, “”, “”); The temporaries for the loop index and limit value can be freed now (if the action routines explicitly free such temporaries) Terminate the current scope, discarding the symbol table entry for the loop index }
17
c Chuen-Liang Chen, NTUCS&IE / 295 Other features exit statement switch statement goto statement exception handling short-circuit boolean expressions QUIZ: how?
18
c Chuen-Liang Chen, NTUCS&IE / 296 QUIZQUIZ QUIZ: Term Project
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.