Download presentation
Presentation is loading. Please wait.
Published byLeo Hardy Modified over 9 years ago
1
CS 36 – February 2 Control structures –Sequence √ –Loops √ –Selection If-statement Case-statement Finding loops
2
Variants of “if” Fortran: if goto More “structured” version: if then else –the “else” arm is optional –How do we recognize the end of the ? –body is single stmt or a block of statements
3
Ambiguous “else” What should this mean: if c1 then if c2 then s1 else s2 ??? Using { } blocks makes meaning clear: if c1 then { if c2 then s1 } else s2 OR if c1 then { if c2 then s1 else s2 } Language should provide meaning in case we omit the { }.
4
Case/switch statement Purpose: for multiple choices, less tedious than a long string of if-else’s First appeared in Algol Typical rules –Case values are constant –Case values are unique –Cases are integral type
5
Pascal example case month of 1,3,5,7,8,10,12: days := 31; 4,6,9,11: days := 30; 2 : if year mod 4 = 0 then days := 29 else days := 28 end;
6
Case variants Some languages allow a default case Example from Ada: case ch is when ‘0’.. ‘9’ => put_line(“digit”); when ‘a’.. ‘z’ => put_line(“letter”); when others => put_line(“special character”); end case; –Avoids a runtime error or mysterious bug Ada’s case statement allows range!
7
C, C++, Java Case statement called “switch” Break statement is required, or else we fall into the next case! switch(month) { case 4: case 6: case 9: case 11: days = 30; break; case 2: days = (y%4==0)? 29 : 28; break; default: days = 31; } Having to say “case” for each value is annoying
8
Dijkstra’s “Guarded” stmt Functions like a non-deterministic case statement: if x >= y max := x [] y >= x max := y fi Evaluate all the conditions. If more than 1 is true, pick any and do corresponding statement. Very few languages use non-determinism (e.g. Haskell). Hard to verify correctness.
9
Finding loops For many optimizations, compiler needs to know where loops are. Can’t rely on “for,” “while” because they are gone by the time we generate code. Procedure –Represent control-flow as a graph. –Focus on blocks of code instead of tiny instructions so not bogged down by unnecessary detail.
10
Basic block In assembly code, a basic block is a sequence of instructions either beginning with a label or ending with a branch/jump. Let’s assume the compiler has already figured out where the blocks are (CS 25). A typical function may have 50 instructions, but only 10 blocks. Easier to analyze.
11
Where are the loops? Not hard for us But control-flow data just has sequence of blocks Need to gather info about transitions between blocks. 1 2 3 4 5 6 7
12
Finding loops For each block, determine –Successors Where can I go immediately after this block? –Predecessors Where could I have just come from? –Dominators Where must I have been, to reach here?
13
Example 1 2 3 4 5 6 7 blockpredsuccdom 1-2 21,63 324,6 43,55 544,6 63,52,7 76-
14
Example 1 2 3 4 5 6 7 blockpredsuccdom 1-2 1 21,63 1,2 324,6 1,2,3 43,55 1,2,3,4 544,6 1,2,3,4,5 63,52,7 1,2,3,6 76- 1,2,3,6,7
15
Aha! A loop We have a loop whenever a block can say: “One of my successors is also one of my dominators.” In other words, I’m going to a place I’ve already been. Hence a back edge, and a loop.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.