Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiling Control Statements

Similar presentations


Presentation on theme: "Compiling Control Statements"— Presentation transcript:

1 Compiling Control Statements
Fall 2013 Josh Bargar & James Newberry

2 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.

3 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.

4 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.

5 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.

6 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

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

8 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.

9 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.

10 REPEAT Loop Compiled Into Jasmin

11 WHILE Loop Compiled Into Jasmin

12 FOR Loop Compiled Into Jasmin

13 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

14 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.

15 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.

16 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.

17 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.

18 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.

19 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.

20 CASE Statement Compiled Into Jasmin


Download ppt "Compiling Control Statements"

Similar presentations


Ads by Google