Compiling Control Statements

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Introduction to C Programming
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Intermediate Code Generation
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Chapter 8 . Sequence Control
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Flow of Control Part 1: Selection
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 153: Concepts of Compiler Design September 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CS 153: Concepts of Compiler Design September 14 Class Meeting
Chapter 4 – C Program Control
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 153: Concepts of Compiler Design September 7 Class Meeting
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
Control Structures – Selection
CMPE 152: Compiler Design September 4 Class Meeting
CS 432: Compiler Construction Lecture 7
CMPE 152: Compiler Design September 11 Class Meeting
Starting JavaProgramming
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
Chapter 4 - Program Control
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 8: More on the Repetition Structure
Selection Statements.
CS 153: Concepts of Compiler Design November 6 Class Meeting
Computer Science Core Concepts
UNIT V Run Time Environments.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 14 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 19 Class Meeting
CSC215 Lecture Control Flow.
CMPE 152: Compiler Design April 16 Class Meeting
Controlling Program Flow
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

Compiling Control Statements Fall 2013 Josh Bargar & James Newberry

Abstract Syntax Tree – REPEAT Statement REPEAT j := i; k := i UNTIL i <= j The LOOP node can have any number of children that are statement subtrees At least one child should be a TEST node whose only child is a relational expression subtree At runtime, the loop exits if the expression evaluates to true. For a Pascal REPEAT statement, the TEST node is the LOOP node’s last child and therefore, the exit test is at the end of the loop.

Abstract Syntax Tree – WHILE Statement WHILE i > j DO k := i the LOOP node’s first child is the TEST node and the second child is the subtree of the nested statement. At runtime, the exit test occurs at the start of the loop. Because a WHILE loop exits when the test expression is false, the parent of the relational expression subtree is a generated NOT node.

Abstract Syntax Tree – FOR Statement FOR k := j TO 5 DO n := k The root of the parse tree is a COMPOUND node. The COMPOUND node’s first child is the subtree of the embedded assignment which initializes the control variable. The second child is a LOOP node. The first child of the LOOP node is a TEST node. The TEST node’s child is either a GT or the LT relational expression subtree The second child of the LOOP node is the subtree of the nested statement. The third child is either an ADD or a SUBTRACT arithmetic expression subtree which increments or decrements the control variable’s value by 1.

Abstract Syntax Tree – IF Statement IF (i = j) THEN t := 200 ELSE f := -200; The IF node has either two or three children. The first child is the relational expression subtree and the second child is the subtree of the THEN nested statement. If there is an ELSE part, the third child is the subtree of the ELSE nested statement. Otherwise, the IF node has only two children.

Abstract Syntax Tree – CASE Statement CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END SELECT node is created CASE expression is then parsed Each case branch is then parsed Eventually exits after the END token SELECT node is returned

Code Generation Classes for Control Statements All extend the StatementGenerator in package backend.compiler.generators StatementGenerator extends CodeGenerator of package backend.compiler

Code Template for Compiling Looping Statements Used for REPEAT, WHILE and FOR statements Generated object code for a boolean expression will leave a value of 0 (false) or 1 (true) on top of the operand stack at run time The IFNE instruction tests whether the value is not equal to 0 (the value is true). If so, the instruction branches out of the loop.

Method generate() of class LoopGenerator Method generate() simply loops over the children of the LOOP parse tree node. For a TEST child node, it calls expressionGenerator.generate() to generate code for the boolean expression and then it emits the IFNE instruction. For a statement child node, the method calls statementGenerator.generate() to generate code for the statement.

REPEAT Loop Compiled Into Jasmin

WHILE Loop Compiled Into Jasmin

FOR Loop Compiled Into Jasmin

Code Template for the IF Statement LeftIF-THEN Statement The IFEQ instruction tests whether the value is equal to 0 (the value is true). If so, the instruction enters the statement RightIF-THEN-ELSE Statement If not, the instruction enters the next ELSE statement

Method generate() of class IfGenerator The generate() method generates Jasmin object code for either an IF-THEN or IF-THEN-ELSE statement according to the code template.

IF Statement Compiled Into Jasmin In the statements starting in lines 15 and 22, each nested IF is a separate statement that has its own “next” labels. See these labels L020, L016, L012, and L008 in the object code before line 22 and labels L027 and L024 at the end.

Code Template for the CASE Statement Code generation for a CASE statement is broken up into three parts: code to evaluate the SELECT expression, the LOOKUPSWITCH instruction with its value-label pairs, and code for the SELECT branch statements. The compiler emits one value-label pair for each SELECT value ending with a default-label pair. The value-label pairs are sorted by their values, and more than one pair can have the same branch label. For each branch label, the compiler generates code for the corresponding SELECT branch statement preceded by the label. A GOTO instruction at the end of the code for each SELECT branch statement takes the execution out of the CASE statement.

Nested local class ValueLabelPair and method generate() of class SelectGenerator Method generate() creates the list of branch labels branchLabels and initializes it with the “next” label. It calls exprGenerator.generate() to generate code for theSELECT expression and leave its value on top of the operand stack. It calls processSelectBranches() to create the array list of ValueLabelPair objects,generateLookupSwitch() to generate the LOOKUPSWITCH instruction and its value-label pairs, and finally generateBranchStatements() to generate code for the SELECT branch statements.

Methods processSelectBranches() and sortPairs() of class SelectGenerator Method processSelectBranches() loops over the SELECT_BRANCH nodes and for each node, it creates a new branch statement label branchLabel, which it adds to thebranchLabels list, and then it loops over the constant nodes of the SELECT branch. For each constant, it creates a new ValueLabelPair using the constant value and thebranchLabel. Before returning the array list of ValueLabelPair objects, the method calls sortPairs() to sort the pairs by their values.

Methods generateLookupSwitch() and generateBranchStatements() of class SelectGenerator Method generateLookupSwitch() emits the LOOKUPSWITCH instruction and then loops over the list of ValueLabelPair objects to emit each value-label pair. Method generateBranchStatements() loops over the SELECT_BRANCH nodes and uses the branchLabels list to emit each branch label and generate code for the corresponding SELECT branch statement. The method calls stmtGenerator.generate() and then emits a GOTO instruction to branch out of the CASE statement. After generating code for all the SELECT branch statements, it emits the “next” label.

CASE Statement Compiled Into Jasmin