Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.

Similar presentations


Presentation on theme: "Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution."— Presentation transcript:

1 Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution paths through code: what gets executed, when, and in what order.

2 Chapter 7Louden, Programming Languages2 Expressions In their purest form, expressions do not involve control issues: subexpressions can be evaluated in arbitrary order, and the order does not affect the result. Functional programming tries to achieve this goal for whole programs. If expressions could have arbitrary evaluation order, programs would become non-deterministic: any of a number of different outcomes might be possible.

3 Chapter 7Louden, Programming Languages3 Side Effects A side effect is any observable change to memory, input or output. A program without any side effect is useless. Side effects expose evaluation order: class Order { static int x = 1; public static int getX() { return x++; } public static void main(String[] args) { System.out.println( x+getX() ); } } This prints 2, but the corresponding C program will usually print 3! Why?

4 Chapter 7Louden, Programming Languages4 Strictness An evaluation order for expressions is strict (eager) if all subexpressions of an expression are evaluated, whether or not they are needed to determine the value of the result, non-strict (lazy) otherwise. Arithmetic is almost always strict. Every language has at least a few non-strict expressions (?:, &&, || in Java). Some languages use a form of non-strictness called normal-order evaluation: no expression is ever evaluated until it is needed (Haskell). Also called delayed evaluation. A form of strict evaluation called applicative-order is more common: "bottom-up" or "inside-out". Still leaves open whether left-to-right or not.

5 Chapter 7Louden, Programming Languages5 Short Circuit Evaluation Suppose Java did not use short-circuit evaluation Problem: table look-up index = 1; while (index <= length) && (LIST[index] != value) index++;

6 Chapter 7Louden, Programming Languages6 Short Circuit Evaluation C, C++, and Java: use short-circuit evaluation for the usual Boolean operators ( && and || ), but also provide bitwise Boolean operators that are not short circuit ( & and | ) Ada: programmer can specify either (short-circuit is specified with and then and or else) FORTRAN 77: short circuit, but any side-affected place must be set to undefined Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)

7 Chapter 7Louden, Programming Languages7 Function calls Obey evaluation rules like other expressions. Applicative order: evaluate all arguments (left to right?), then call the procedure. Normal order: pass in unevaluated representations of the arguments. Only evaluate when needed. With side effects, order makes a difference.

8 Chapter 7Louden, Programming Languages8 Case Statements Rules cases can be constants, constant expressions, constant ranges no overlapping cases error if unspecified case occurs (or may decide to do nothing if unspecfied case) Implemented via jump table: vector stored sequentially in memory - each of whose components is an unconditional jump Need one location in jump table for every value between range of possible values

9 Chapter 7Louden, Programming Languages9 Switch Statements Implementation Options –Series of conditionals Good if few cases Slow if many –Jump Table Lookup branch target Avoids conditionals Possible when cases are small integer constants –GCC Picks one based on case structure –Bug in example code No default given typedef enum {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; char unparse_symbol(op_type op) { switch (op) { case ADD : return '+'; case MULT: return '*'; case MINUS: return '-'; case DIV: return '/'; case MOD: return '%'; case BAD: return '?'; }

10 Chapter 7Louden, Programming Languages10 Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: target = JTab[op]; goto *target; switch(op) { case 0: Block 0 case 1: Block 1 case n-1: Block n–1 } Switch Form Approx. Translation Jump Table Jump Targets

11 Chapter 7Louden, Programming Languages11 A jump table is an array of code addresses: –Tbl[ i ] is the address of the code to execute if the expression evaluates to i. –if the set of case labels have “ holes ”, the correspond jump table entries point to the default case. Bounds checks: –Before indexing into a jump table, we must check that the expression value is within the proper bounds (if not, jump to the default case). –The check lower_bound  exp_value  upper bound can be implemented using a single unsigned comparison.

12 Chapter 7Louden, Programming Languages12 Jump Table Space Costs jump tables best for large no. of case labels (  8) may take a large amount of space if the labels are not well- clustered. A jump table with max. and min. case labels c max and c min needs  c max – c min entries. This can be wasteful if the entries aren’t “dense enough”, e.g.: switch (x) { case 1: … case 1000: … case 1000000: … } Define the density of a set of case labels as density = number of case labels/(c max – c min ) Compilers will not generate a jump table if density below some threshold (typically, 0.5).

13 Chapter 7Louden, Programming Languages13 Use of Switch Statements if no. of case labels is small (  ~ 8), use linear or binary search. –use no. of case labels to decide between the two. if density  threshold (~ 0.5) : generate a jump table; else : divide the set of case labels into sub-ranges s.t. each sub-range has density  threshold; generate code to use binary search to choose amongst the sub-ranges; handle each sub-range recursively.

14 Chapter 7Louden, Programming Languages14 Guarded Commands Selection: if -> [] ->... [] -> fi Semantics: when this construct is reached, –Evaluate all boolean expressions –If more than one are true, choose one nondeterministically –If none are true, it is a runtime error

15 Chapter 7Louden, Programming Languages15 Guarded Commands Idea: if the order of evaluation is not important, the program should not specify one In Haskell, first one that matches is used.

16 Chapter 7Louden, Programming Languages16 Guarded Commands Loops do -> [] ->... [] -> od

17 Chapter 7Louden, Programming Languages17 Guarded Commands Semantics: For each iteration: –Evaluate all boolean expressions –If more than one are true, choose one nondeterministically; then start loop again –If none are true, exit loop

18 Chapter 7Louden, Programming Languages18 Summary Every language has three major program components: expressions, statements, and declarations. Expressions are executed for their values (but may have side effects), and may or may not be sequenced. Statements are executed solely for their side effects, and they must be sequenced. Declarations define names; they can also give values to those names. They may or may not be viewed by a language as expressions or statements.


Download ppt "Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution."

Similar presentations


Ads by Google