big picture control flow ordering in program execution
play

Big Picture: Control Flow Ordering in Program Execution - PDF document

Big Picture: Control Flow Ordering in Program Execution Ordering/Flow Mechanisms: ! Sequencing (statements executed (evaluated) in a specified order) CSCI: 4500/6500 Programming Imperative language - very important Functional - doesn


  1. Big Picture: Control Flow Ordering in Program Execution Ordering/Flow Mechanisms: ! Sequencing (statements executed (evaluated) in a specified order) CSCI: 4500/6500 Programming – Imperative language - very important – Functional - doesn ’ t matter as much (emphasizes evaluation of Languages expression, de emphasize or eliminates statements, e.g., pure fl don ’ t have assignment statements) ! Selection -- Choice among two or more – Deemphasized in logical languages Control Flow ! Iteration Chapter 6 » Repeating structure – emphasized in imperative languages ! Procedural abstraction, recursion, requires stack ! Concurrency » 2 or more code fragments executed at the same time ! Non-determinacy (unspecified order) 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Expression Evaluation: Classification Outline Evaluation: * fix operators ! Expression: ! Infix, Prefix or Postfix » Operator (built-in function) and operands ! Precedence & Associativity (arguments) ! Side effects ! Infix, prefix, postfix operators ! Statement versus Expression Oriented Languages » (+ 5 5) or 5 + 6 ! Value and Reference Model for Variables » operators in many languages are just `syntactic sugar ’ 1 for a function call: ! Orthogonality – a + b ! a.operator+( b ) in C++ ! Initialization – “ + ” (a, b) in Ada ! Aggregates » Cambridge Polish prefix and function name inside parenthesis. ! Assignment » Postfix - postscript, Forth input languages, calculators 1 Landin “ adding “ sugar ” to a language to make it easier to read (for humans) 3 4 Maria Hybinette, UGA Maria Hybinette, UGA Expression Evaluation: Precedence & Associativity Precedence & Associativity How should this be evaluated? ! Precedence specify that some operators group more tightly than others ! a + b * c**d**e / f » Richness of rules across languages varies (overview next slide) Depends on the language, possibilities: ! Associativity rules specify that sequences of ! (((( a + b ) * c ) ** d ) ** e ) / f operators of equal precedence groups either ! a + (((b * c ) ** d ) ** ( e / f ) ) left or right. ! a + ( (b * ( c ** ( d ** e ) ) ) / f ) » (or up or down? for a weird language of your own creation) » Fortran does this last option ! or something entirely different? » Associatively rules are somewhat uniform across languages but there are variations 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. Precedence ! Most languages avoid this problem by adopting the following rules. Higher precedence » arithmetic operators » relational operators » logical operators Lower precedence ! Some languages give all operators equal precedence. » Parentheses must be used to specify grouping. Example Precedence: � ! if A < B and C < D then K = 5 � How would Pascal evaluate this? � 7 8 Maria Hybinette, UGA Maria Hybinette, UGA ! A < (B and C) < D [could be an error] � Precedence: Rule of Thumb Associativity Example ! Basic operators almost always left to right ! C has 15 levels - too many to remember » 9-3-2 = (9-3)-2 = 4 (left right) ! Pascal has 3 levels - too few for good » 9-3-2 = 9-(3-2) = 8 (right-left) semantics ! Exponential operator: ** ! Fortran has 8 » right-left (as do mathematics) in Fortran – 4**3**2 = 4**(3**2) = 262,144 ! Ada has 6 » Language syntax requires parenthesized form (Ada) » Note: Ada puts and, or at same level ! Assignment ‘ = ‘ in expressions associates: ! Lesson: when unsure (e.g., programmer right-left using many languages, better to circumvent » a = b = a + c => a = ( b = (a+c) ) precedence and use parentheses! – assigns (a+c) to b then assigns the same value to a 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Side Effects & Idempotent Functions Side Effects – a function has a side effect if it Referentially transparent - Expressions in a influences subsequent computation in any purely functional language are referentially way other than by returning a value. A side transparent, their value depends only on the effect occurs when a function changes the referencing environment. environment in which it exists Imperative programming – “ Programming with Idempotent – an idempotent function is one that side effects ” (programming in terms of if called again with the same parameters, will statements, state). always give the same result 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Side Effects Side Effects (cont) ! Assignment statements provide the ultimate example of side effects ! Side effects are a particular problem if they affect – they change the value of a variable state used in other parts of the expression in which – Fundamental in the von Neumann model of computation. a function call appears: ! Several languages outlaw side effects for functions (these ! Example: languages are called single assignment languages) » easier to prove things about programs » a - f(b) - c * d /* f(b) may affect ‘ d ’ */ � » closer to Mathematical intuition » What is evaluated first: � » easier to optimize � a – f(b) � » (often) easier to understand c * d � ! But side effects can be nice: consider - rand() � – Needs to have a side effect, or else the same random number every time it is called. 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Ordering within Expressions Evaluation of Operands and Side Effects ! Another Example: int x = 0; � �� » f( a, g(b), c ) which parameter is evaluated first? int foo() � � � { � ! Why is it important: � � x += 5; � » Side-effects: � � return x; � – if g(b) modifies a or c then the values passed into f will depend � � } � on the order that parameters are evaluated � » Code improvements: � int a = foo() + x + foo(); � – a = B[i] � – c = a * b + d * 3 What is the value of a? ! Note: precedence or associativity does not say if we evaluated a*b or d*3 first. a = 5 + x + foo() – Evaluate: d*3 first, so the previous load (slow) of B[i] from memory occurs in parallel of a doing something different, i.e. computing d*3. 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Re-ordering using mathematical properties Mathematical Identities ! Commutative Example: » (a+b) = (b+a) a = b + c ! Associative d = c + e + b » (a+b) + c = a + (b + c) Re-order to: ! Distributive a = b + c » a * (b + c) = a * b + a * c. d = b + c + e (already evaluated b+c (it is a)) 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

  4. Expression vs. Statement Mathematical Identities Orientation ! Statements : ! Problem: Computer has limited precision » executed solely for their side effects and » return no useful value » associativity (known to be dangerous) (a + b) + c » most imperative languages works if a~=maxint and b~=minint and c<0 » time dependent a + (b + c) does not ! Expressions : » may or may not have side effects » always produces a value and » functional languages (Lisp, Scheme, ML) » time less ! C kinda halfway in-between (distinguishes) » allows expression to appear instead of statement 19 20 Maria Hybinette, UGA Maria Hybinette, UGA Assignment References and Values ! statement (or expression) executed for its side effect ! Assignment seems straightforward ! assignment operators (+=, -=, etc) ! Semantic differences depending if languages » handy uses a » avoid redundant work (or need for optimization) – No need for redundant address calculations (guaranteed) » A reference model » perform side effects exactly once (avoids pecularities) » or value model of variables. – A[ f(i) ] = A[ f(i) ] + 1 (f(i) may have a side effect x 2). ! Impact on programs that use pointers (we will – A[ f(i) ] += 1 see why shortly). 21 22 Maria Hybinette, UGA Maria Hybinette, UGA a � 4 � a � 4 � Value Model Value Model: Example b = 2 � ! Variable is a named container for a value a � 4 � c = b � ! left-hand side of expressions denote “ locations ” and are referenced as l-values a = b + c � b � 2 � ! right-hand side of expressions denote “ values ” and are referred to as r-values c � 2 � ! Expressions can be either an l-value or an r-value depending on context: » 2 + 3 = a � 1. Put the value 2 in b » a = 2 + 3 � 2. Copy value of b into c » ( f(x)+3 ) -> b[c] = 2 /* l-value expression */ � » k = (f(x)+3)->b[c] � 3. Read b and c and put result in a Example languages who use value model: C and C++ 23 24 Maria Hybinette, UGA Maria Hybinette, UGA

Recommend


More recommend