![Page 1: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/1.jpg)
INF 3110 –
2018
INF3110 – Programming LanguagesScope and Runtime Organization
9/21/18 1
![Page 2: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/2.jpg)
INF 3110 –
2018
9/21/18 2
Outline: Runtime Organization I & II
§ Block-structured languages and stack organization§ In-line Blocks
– activation records– storage for local and global variables
§ First-order functions§ Parameter passing§ Higher-order functions (not today)
![Page 3: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/3.jpg)
INF 3110 –
2018
9/21/18 3
Simplified Reference Model of a Machine- used to understand memory managementRegisters(not in this class)
Environment Pointer
Program Counter
DataCode
Heap
Stack
Our main focus today
![Page 4: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/4.jpg)
INF 3110 –
2018
9/21/18 4
Block-Structured Languages§ Blocks are syntactical structures§ Can be nested within each other
...;{ int x = 2;
{ int y = 3;x = y+2;
}};...
– Storage management – memory representation§ Enter block: allocate space for variables§ Exits block: space may be de-allocated
new variables declared in nested blocks
innerblock
outer block
x: global variabley: local variable
![Page 5: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/5.jpg)
INF 3110 –
2018
9/21/18 5
Examples
§ Blocks in common languages– C/C++/Java/C# { … }– Algol/Simula/Basic begin … end– ML let … in … end– Python (whitespace!)
§ Two forms of blocks to start with– In-line blocks – Blocks associated with functions
or procedures
– To come: blocks associated with classes:
class Node {
Node left, right;void insert(Node n) { ...}
};
![Page 6: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/6.jpg)
INF 3110 –
2018
9/21/18 6
Some basic concepts§ Declaration
– Specifies properties (name, type, kind) of an identifier§ Scope
– Region of program text where declaration is visible§ Lifetime
– Period of time when location is allocated
– Inner declaration of x hides outer one.– Called �hole in scope�
– Lifetime of outer x includes time when inner block is executed
– Lifetime ¹ scope
{ int x = … ; { int y = … ;
{ int x = … ;
…; x; …};…; x; …
};};
![Page 7: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/7.jpg)
INF 3110 –
2018
9/21/18 7
In-line blocks
§ Activation record– Data structure stored on run-time stack– Contains space for local variables in a block
May also need space for intermediate results like (x+y), (x-y)
{ int x = 0;
int y = x+1;
{ int z = (x+y)*(x-y);
};
};
Push record with space for x, y Set values of x, y
Push record for inner blockSet value of zPop record for inner block
Pop record for outer block
Do we need activation records for blocks? Yes, for loops (and funcs etc)
![Page 8: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/8.jpg)
INF 3110 –
2018
9/21/18 8
Activation record for in-line block
§ Environment pointer– Pointer to current record on stack
§ Control link (dynamic link)– Pointer to previous record on
stack– Why is it called dynamic?
§ Push record on stack– Set new control link (in new
record) to point to old env ptr– Set env ptr to new record
§ Pop record off stack– Follow control link of current
record to reset environment pointer
– (No need to actively blank memory)
Control link
Local variables
Intermediate results
Control link
Local variables
Intermediate results
Environment Pointer
Stack grows dow
nwards
![Page 9: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/9.jpg)
INF 3110 –
2018
9/21/18 9
Example
Control link
xy
01
x+yx-y
Environment Pointer
1-1
Control link
z -1
{ int x = 0;
int y = x+1;
{ int z = (x+y)*(x-y);
// figure shows state here!
};
};
Push record with space for x, y Set values of x, y
Push record for inner block
Set value of z
Pop record for inner block
Pop record for outer block
![Page 10: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/10.jpg)
INF 3110 –
2018
9/21/18 10
Initial values – what is in the activation block?
§ Specified initial value
§ Default initial value
§ Languages differ
– C/C++: no default initial value
§ Undefined?
§ Arbitrary value?
– Algol/Simula/Java/C#:
default initial value
{
int x = 0;
int y;
{
int z = (x+y)*(x-y);
};
};
![Page 11: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/11.jpg)
INF 3110 –
2018
http://stackoverflow.com/questions/31739792/is-uninitialized-local-variable-the-fastest-random-number-generator
![Page 12: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/12.jpg)
INF 3110 –
2018
9/21/18 12
Scope rules
§ Global and local variables
– x, y are local to outer block – z is local to inner bock– x, y are global to inner block
§ Static scope– global refers to declaration in closest enclosing block
§ Dynamic scope– global refers to most recent activation record
These are the same until we consider function calls. à
{ int x = 0;
int y = x+1;
{ int z = (x+y)*(x-y);
};
};
![Page 13: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/13.jpg)
INF 3110 –
2018
9/21/18 13
Example – static/dynamic scoping{ -- block 0
int x = 0;{ -- block 1
int x = 1; };{ -- block 2
int x = 2;
};{ -- block 3, called
int z = x;
}}
Control link
x 0
Control link
x 2
Control link
z ??
Control link
x 1
If block 1 executes block 3Static scope: z = 0Dynamic scope: z = 1
If block 2 executes block 3Static scope: z = 0Dynamic scope: z = 2
1 2
3
If block 0 executes block 3Static scope: z = 0Dynamic scope: z = 0
0
? ? ?
![Page 14: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/14.jpg)
INF 3110 –
2018
9/21/18 14
Functions and procedures
§ Activation record must include space for
– parameters– local variables– return address– return value
(an intermediate result)– location to put return value
on function exit
int fact(int n) { … }int i, j;...i = 3;j = fact(i);print(j)
![Page 15: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/15.jpg)
INF 3110 –
2018
9/21/18 15
Activation record for function call§ Return address
– Location of code to execute on function return
§ Return-result address– Address in activation record of
calling block to receive return value
§ Parameters– Locations to contain data from
calling block
Control link
Local variables
Intermediate results
Environment Pointer
Parameters
Return address
Return-result addr
![Page 16: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/16.jpg)
INF 3110 –
2018
9/21/18 16
Example§ Function
fact(n) = if n <= 1 then 1else n * fact(n-1)
§ Return result address– location to put fact(n)
§ Parameter– set to value of n by calling
sequence§ Intermediate result
– location to contain value of fact(n-1)
Control link
Local variables
Intermediate results
Environment Pointer
Parameters
Return address
Return result addr
![Page 17: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/17.jpg)
INF 3110 –
2018
9/21/18 17
Function call
Return address omitted; would be ptr into code segment
fact(n) = if n<= 1 then 1else n * fact(n-1)
Control link
fact(n-1)n
Return-result addrk
fact(k)
Environment Pointer
Control link
fact(n-1)n
Return-result addr3
fact(3)
Control link
fact(n-1)n
Return-result addr2
fact(2)
Control link
fact(n-1)n
Return-result addr1
fact(1)
Function return next slide ®
![Page 18: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/18.jpg)
INF 3110 –
2018
9/21/18 18
Function return – store result, pop record from the stack
Control link
fact(n-1)n
Return result addr3
fact(3)
Control link
fact(n-1)n
Return result addr
12
fact(2)
Control link
nReturn result addr
1
fact(1)
fact(n) = if n<= 1 then 1else n * fact(n-1)
Control link
fact(n-1)n
Return result addr3
fact(3)
Control link
fact(n-1)n
Return result addr
12
fact(2)2
![Page 19: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/19.jpg)
INF 3110 –
2018
9/21/18 19
Access to global variables
§ Two possible scoping conventions– Static scope: refer to closest enclosing block (syntactically)– Dynamic scope: most recent activation record on stack
§ Example
int x = 1;function g(z) = x+z;function f(y) =
{ int x = y+1; return g(y*x)
};f(3);
x 1
x 4y 3
z 12
outer block
f(3)
g(12)
Which x is used for expression x+z?
![Page 20: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/20.jpg)
INF 3110 –
2018
9/21/18 20
Activation record for static scope
§ Control link (dynamic link)– Link to activation record of
previous (calling) block§ Access link (static link)
– Link to activation record corresponding to the closest enclosing block in program text
– Why is it called static?§ Difference
– Control link depends on dynamic behavior of program
– Access link depends on static form of program text
Control link
Local variables
Intermediate results
Environment Pointer
Parameters
Return address
Return result addr
Access link
![Page 21: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/21.jpg)
INF 3110 –
2018
9/21/18 21
Static scope with access links (C-like notation){ int x = 1;
int function g(z) { return x+z };
int function f(y) { int x = y+1; return g(y*x)
};
main() {f(3);
}}
x 1
x 4y 3
z 12
outer block
f(3)
g(12) control linkaccess link
control linkaccess link
access linkcontrol link
main
Use access link to find global variable:§ Access link is always set to
frame of closest enclosing lexical block
§ For function body, this is the block that contains function declaration
g …f …
![Page 22: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/22.jpg)
INF 3110 –
2018
9/21/18 22
Static scope with access links (ML-like notation)
x 1
x 4y 3
z 12
outer block
f(3)
g(12) control linkaccess link
g …
f …
control linkaccess link
control linkaccess link
access linkcontrol linkUse access link to find global
variable:§ Access link is always set to
frame of closest enclosing lexical block
§ For function body, this is the block that contains function declaration
int x = 1;function g(z) = x+z;function f(y) =
{ int x = y+1; return g(y*x)
};f(3);
![Page 23: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/23.jpg)
INF 3110 –
2018
9/21/18 23
Issues for first-order functions
§ Access to global variables
§ Parameter passing– pass-by-value – pass-by-reference – pass-by-name
int a = 5;
void f(int x){
x = x+1;}
void main(){
f(a);print(a);
}
ü
![Page 24: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/24.jpg)
INF 3110 –
2018
9/21/18 24
Parameter passing§ Call-by-value (or “pass-by-value”)
– Caller places R-value (contents) of actual parameter in activation record
– Function cannot change value of caller�s variable– Reduces aliasing (alias: two names refer to same location)
§ Call-by-reference– Caller places L-value (address) of actual parameter in activation
record– Function can assign to variable that is passed (must have an L-
value!)– Aliasing
§ Call-by-name– Actual parameter expression is passed as such and evaluated
whenever the formal parameter is used in the function– (Not common in most languages)
![Page 25: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/25.jpg)
INF 3110 –
2018
9/21/18 25
Different variants of copying the value
§ Call-by-value– Local variable
assigned at call
– f(int in x){...}
§ Call-by-result– Local variable not
assigned at call, butreturned at exit
– f(int out x){...}
§ Call-by-value-result– Local variable
assigned at call, and returned at exit
– f(int in-out x){...}
int a = 5;
void f(int x){
x = x+1;}
void main(){
f(a);print(a);
}
f.x = a...
...a = f.x
f.x = a;...
a = f.x
a
global
x
f
![Page 26: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/26.jpg)
INF 3110 –
2018
§ Java?§ C#?§ Python?§ JavaScript?§ SML?
9/21/18 26
What is the parameter passing semantics ofYOUR favorite language?
class Person {public String name = “”;
}
public static void main(String[] args) {Person p = new Person();p.name = “Wonderwoman!”;changeName(p);println(p.name); // what happens here?
}
public static void changeName(Person p) {p.name = “Batman!”;
}
![Page 27: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/27.jpg)
INF 3110 –
2018
§ Java?§ C#?§ Python?§ JavaScript?§ SML?
9/21/18 27
What is the parameter passing semantics ofYOUR favorite language?
class Person {public String name = “”;
}
public static void main(String[] args) {Person p = new Person();p.name = “Wonderwoman!”;changeName(p);println(p.name); // ?
}
public static void changeName(Person p) {p = new Person();p.name = “Batman!”;
}
![Page 28: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/28.jpg)
INF 3110 –
2018
§ Java?§ C#?§ Python?§ JavaScript?§ SML?
9/21/18 28
What is the parameter passing semantics ofYOUR favorite language?
class Person {public String name = “”;
}
public static void main(String[] args) {Person p = new Person();p.name = “Wonderwoman!”;changeName(p);println(p.name); // ?
}
public static void changeName(ref Person p) {p.name = “Batman!”;
}
![Page 29: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/29.jpg)
INF 3110 –
2018
§ Java?§ C#?§ Python?§ JavaScript?§ SML?
9/21/18 29
What is the parameter passing semantics ofYOUR favorite language?
class Person {public String name = “”;
}
public static void main(String[] args) {Person p = new Person();p.name = “Wonderwoman!”;changeName(p);println(p.name); // ?
}
public static void changeName(ref Person p) {p = new Person();p.name = “Batman!”;
}
![Page 30: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/30.jpg)
INF 3110 –
2018
§ Java?
§ C#?
§ Python?
§ JavaScript?
§ SML?
9/21/18 30
What is the parameter passing semantics ofYOUR favorite language?
class Person {
public String name = “”;
}
public static void main(String[] args) {
Person p = new Person();
p.name = “Wonderwoman!”;
changeName(p);
println(p.name); // ?}
public static void changeName(ref Person p) {
p = null;
}
![Page 31: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/31.jpg)
INF 3110 –
2018
9/21/18 31
Call by-reference§ The ’x’ (within f) is set to
the address of ’a’ (L-value).
§ The assignment ’x+1’assigns the value of ’x+1’, that is 6, to the variable whose L-value is kept by ’x’, i.e. the global variable ’a’.
§ ’a’ is therefore changed to 6, and 6 is printed.
int a = 5;void f(int x){x = x+1;
}void main(){f(a);print(a);
}
a
global
x
f
![Page 32: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/32.jpg)
INF 3110 –
2018
9/21/18 32
Example - aliasing
void f(ref real v1, ref real v2, ref real v3) {v1 = v1 - v3;v2 = v2 + v3;
};
real x, y, z;
x = 4.0;y = 6.0;z = 1.0;
f(x, y, z); // ? x = 3.0;y = 7.0;
x = 0.0;y = 6.0; f(a[i], a[j], a[k]);
f(x, y, x) // ?
Shouldn’t the compiler warn me about this stuff???
![Page 33: INF3110 –Programming Languages Scope and Runtime ......INF3110 –Programming Languages Scope and Runtime Organization 9/21/18 1 10 – 2018 9/21/18 2 Outline: Runtime Organization](https://reader035.vdocuments.site/reader035/viewer/2022071401/60edbc2b51b2ed79152534b7/html5/thumbnails/33.jpg)
INF 3110 –
2018
Going forward
9/21/18 35
• Higher-order functions• Closures• Classes and objects
Next time:• Part II of this lecture