Title: Chapter 7 - Control I: Expressions and Statements
1Chapter 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.
2Expressions
- 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.
3Side 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 orderclass
Order static int x 1 public static int
getX() return x public static void
main(String args) System.out.println(
xgetX() ) - This prints 2, but the corresponding C program
will usually print 3! - Why?
4Strictness
- 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.
5Short Circuit Evaluation
- Suppose Java did not use short-circuit evaluation
- Problem table look-up
- index 1
- while (index lt length) (LISTindex !
value) - index
6Short 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 gt b) (b / 3)
7Function 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.
8Case 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
9Switch Statements
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 '?'
- 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
10Jump Table Structure
Jump Targets
Switch Form
Jump Table
switch(op) case 0 Block 0 case 1
Block 1 Â Â case n-1 Block n1
Approx. Translation
target JTabop goto target
11- 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.
12Jump 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 cmax
and cmin needs ? cmax cmin entries. - This can be wasteful if the entries arent 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/(cmax cmin )
- Compilers will not generate a jump table if
density below some threshold (typically, 0.5).
13Use 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.
14Guarded Commands
- Selection if ltbooleangt -gt ltstatementgt
- ltbooleangt -gt ltstatementgt
- ...
- ltbooleangt -gt ltstatementgt
- 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
15Guarded Commands Idea if the order of evaluation
is not important, the program should not specify
one
In Haskell, first one that matches is used.
16Guarded Commands
- Loops do ltbooleangt -gt ltstatementgt
- ltbooleangt -gt ltstatementgt
- ...
- ltbooleangt -gt ltstatementgt
- od
17Guarded 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
18Summary
- 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.