Array Dependence Analysis and Vectorization with the Chains ...
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan Overview Scope rules Static versus dynamic scoping 02/28/20 COP4020 Spring 2014 2 Scope Scope is the textual region of a program in which a name-to-object binding is active Statically scoped language: the scope of bindings is determined at compile time
Dynamically scoped language: the scope of bindings is determined at run time Used by almost all but a few programming languages More intuitive to user compared to dynamic scoping Used in Lisp (early versions), APL, Snobol, and Perl (selectively) Scoping in action: see scope.cpp 02/28/20 COP4020 Spring 2014 3 Effect of Static Scoping Program execution:
binding a:integer main() a:=2 second() a:integer first() a:=1 write_integer(a) Program prints 1 02/28/20 The following pseudo-code program demonstrates the effect of scoping on variable bindings: a:integer procedure first a:=1 procedure second a:integer first() procedure main a:=2 second()
write_integer(a) COP4020 Spring 2014 4 Effect of Dynamic Scoping Program execution: a:integer main() a:=2 second() binding a:integer first() a:=1 write_integer(a) Program prints 2 02/28/20 The following pseudo-code program demonstrates the effect of scoping on variable
bindings: a:integer procedure first a:=1 Binding depends on execution procedure second a:integer first() procedure main a:=2 second() write_integer(a) COP4020 Spring 2014 5 Static Scoping The bindings between names and objects can be determined by examination of the program text Scope rules of a program language define the scope of variables and subroutines, which is the region of program text in which a name-to-object binding is usable
02/28/20 Early Basic: all variables are global and visible everywhere Fortran 77: the scope of a local variable is limited to a subroutine; the scope of a global variable is the whole program text unless it is hidden by a local variable declaration with the same variable name Algol 60, Pascal, and Ada: these languages allow nested subroutines definitions and adopt the closest nested scope rule with slight variations in implementation COP4020 Spring 2014 6 Closest Nested Scope Rule procedure P1(A1:T1) var X:real; ... procedure P2(A2:T2); ... procedure P3(A3:T3); ... begin (* body of P3: P3,A3,P2,A2,X of P1,P1,A1 are visible *)
end; ... begin (* body of P2: P3,P2,A2,X of P1,P1,A1 are visible *) end; procedure P4(A4:T4); ... function F1(A5:T5):T6; var X:integer; ... begin (* body of F1: X of F1,F1,A5,P4,A4,P2,P1,A1 are visible *) end; ... begin (* body of P4: F1,P4,A4,P2,X of P1,P1,A1 are visible *) end; ... begin (* body of P1: X of P1,P1,A1,P2,P4 are visible *) end 02/28/20 COP4020 Spring 2013 To find the object referenced by a given
name: Look for a declaration in the current innermost scope If there is none, look for a declaration in the immediately surrounding scope, etc. 7 Static Scope Implementation with Static Links With the Closest Nested Scope Rule, the program can only refer to variables that are alive: the variable must have been stored in the frame of a subroutine If a variable is not in the local scope, we are sure there is a frame for the surrounding scope somewhere below on the stack:
The current subroutine can only be called when it was visible The current subroutine is visible only when the surrounding scope is active Each frame on the stack contains a static link pointing to the frame of the static parent 02/28/20 COP4020 Spring 2014 8 Example Static Links Subroutines C and D are declared nested in B B and E are nested in A
02/28/20 B is static parent of C and D A is static parent of B and E The fp points to the frame at the top of the stack to access locals The static link in the frame points to the frame of the static parent COP4020 Spring 2014 9 Static Chains How do we access non-local objects?
The static links form a static chain, which is a linked list of static parent frames When a subroutine at nesting level j has a reference to an object declared in a static parent at the surrounding scope nested at level k, then j-k static links forms a static chain that is traversed to get to the frame containing the object The compiler generates code to make these traversals over frames to reach non-local objects 02/28/20 COP4020 Spring 2014 10 Example Static Chains 02/28/20 Subroutine A is at nesting level 1 and C at nesting level 3 When C accesses an object of A, 2 static links are traversed to get to A's frame that
contains that object COP4020 Spring 2014 11 Out of Scope Non-local objects can be hidden by local name-to-object bindings and the scope is said to have a hole in which the non-local binding is temporarily inactive but not destroyed Some languages, notably Ada and C++ use qualifiers or scope resolution operators to access non-local objects that are hidden 02/28/20 P1.X in Ada to access variable X of P1 and ::X to access global variable X in C++ COP4020 Spring 2014 12
Out of Scope Example procedure P1; var X:real; procedure P2; var X:integer begin ... (* X of P1 is hidden *) end; begin ... end 02/28/20 P2 is nested in P1 P1 has a local variable X P2 has a local variable X that hides X in P1 When P2 is called, no extra code is executed to inactivate the binding of X to P1
COP4020 Spring 2014 13 Dynamic Scope Scope rule: the "current" binding for a given name is the one encountered most recently during execution Typically adopted in (early) functional languages that are interpreted With dynamic scope: Name-to-object bindings cannot be determined by a compiler in general Easy for interpreter to look up name-to-object binding in a stack of declarations Generally considered to be "a bad programming language feature" Hard to keep track of active bindings when reading a program text Most languages are now compiled, or a compiler/interpreter mix 02/28/20 COP4020 Spring 2014
14 Dynamic Scoping Problems In this example, function scaled_score probably does not do what the programmer intended: with dynamic scoping, max_score in scaled_score is bound to foo's local variable max_score after foo calls scaled_score, which was the most recent binding during execution: max_score:integer function scaled_score(raw_score:integer):real return raw_score/max_score*100 ... procedure foo max_score:real := 0 ... foreach student in class student.percent := scaled_score(student.points) if student.percent > max_score max_score := student.percent 02/28/20 COP4020 Spring 2014 15
Dynamic Scope Implementation with Bindings Stacks Each time a subroutine is called, its local variables are pushed on a stack with their name-to-object binding When a reference to a variable is made, the stack is searched top-down for the variable's name-to-object binding After the subroutine returns, the bindings of the local variables are popped Different implementations of a binding stack are used in programming languages with dynamic scope, each with advantages and disadvantages 02/28/20 COP4020 Spring 2014 16
Static and Dynamic Scope example a : integer; procedure foo a = 10 goo() hoo() write (a) procedure goo a = 20 procedure hoo write(a) procedure main a : integer a = 30 foo() write (a) 02/28/20 COP4020 Spring 2014 17
Executive Privilege. Nixon was ordered to turn over tapes, he claimed absolute privilege of confidentiality. There is an inherent authority that flows from the nature of enumerated powers. The separation of powers insulates a president from judicial subpoena in criminal...
Software Requirements for Interactive Mathematics Textbooks Catherine Schell CSC 509 Winter 2005 Introduction New phase of work on paper from 508 Goal for first phase: develop a set of requirements for interactive mathematics textbooks.
Conceptually the late merge disadvantages: Driver understanding . Drivers are not familiar with the late merge. There is a learning curve. Approach taper at high speeds and low volumes. when traffic demand is below the capacity of the open lane...
Simple harmonic motion (SHM) refers an oscillatory, or wave-like motion where the restoring force is linearly or directly proportional to the displacement from the equilibrium position, and in the opposite direction. Eg of SHM are: a pendulum.
3.2.1 Target Hardening, Physical Barriers, Page . 15) Windows and Glass (continued): Blast curtains: Reinforced fabrics provide protection from flying debris - explosions. No protection from intrusion. Can reduce injury from projectiles (weather)
Operant Conditioning A type of learning in which behavior is strengthened if followed by reinforcement or diminished if followed by punishment. Edward Thorndike Law of Effect: rewarded behavior is likely to recur. B.F. Skinner Shaping A procedure in Operant Conditioning...