C Chuen-Liang Chen, NTUCS&IE / 279 CONTROL STRUCTURES Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Control Structures.
CPSC 388 – Compiler Design and Construction
How SAS implements structured programming constructs
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Semantics Static semantics Dynamic semantics attribute grammars
Air Force Institute of Technology Electrical and Computer Engineering
Intermediate Code Generation
Programming Languages and Paradigms
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Programming Languages and Paradigms The C Programming Language.
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Comp Sci Control structures 1 Ch. 5 Control Structures.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Parasol.tamu.edu/courses/minipolaris. What is Polaris? Polaris is a source to source transforming compiler, originally developed at UIUC, intended to.
Control Flow C and Data Structures Baojian Hua
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
C Chuen-Liang Chen, NTUCS&IE / 51 CONTEXT-FREE GRAMMARS Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
CIS 3301 C# Lesson 3 Control Statements - Selection.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
C Chuen-Liang Chen, NTUCS&IE / 35 SCANNING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Code Generation for Control Flow Mooly Sagiv html:// Chapter
Three Address Code Generation of Control Statements continued..
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Computer Science Up Down Controls, Decisions and Random Numbers.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Program with PL/SQL Lesson 4.
Syntax versus Semantics
DECLARATION Chuen-Liang Chen Department of Computer Science
Three Address Code Generation Control Statement
Statement-Level Control Structures
EXPRESSION Chuen-Liang Chen Department of Computer Science
Homework Any Questions?.
Conditional Statements
Languages and Compilers (SProg og Oversættere)
By Hector M Lugo-Cordero September 3, 2008
Programming Languages and Paradigms
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

c Chuen-Liang Chen, NTUCS&IE / 279 CONTROL STRUCTURES Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei, TAIWAN

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

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

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 )

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; } ;

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, “”, “”); }

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, “”, “”}; }

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; } ;

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, “”, “”); }

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 )

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 */

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; } ;

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; } }

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;

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; }

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 }

c Chuen-Liang Chen, NTUCS&IE / 295 Other features exit statement switch statement goto statement exception handling short-circuit boolean expressions QUIZ: how?

c Chuen-Liang Chen, NTUCS&IE / 296 QUIZQUIZ QUIZ: Term Project