Escolar Documentos
Profissional Documentos
Cultura Documentos
Overview
Study fundamental semantic issues of variables:
Attributes of variables
Name, value, type, address, lifetime, scope
Binding of variables
Scope
Referencing environments
$sp (stack
Pointer)
int i,a[100];
void foo()
{ int i,j,*p;
.. = j;
i = ..;
p=malloc();
}
Stack
Heap
Static
Code
Program code
3
Overview
Study fundamental semantic issues of variables:
Attributes of variables
Name, value, type, address, lifetime, scope
Binding of variables
Scope
Referencing environments
Static Variables
Bound to memory cells before execution begins
and remains throughout execution, e.g., C static
and global variables
Advantages:
Efficiency (direct addressing, no runtime allocation
overhead), for globally accessible variables, historysensitive subprogram support
Disadvantage:
Stack-dynamic Variables
Storage bindings are created when declaration
statements are elaborated at runtime
If scalar, all attributes except address are statically
bound, e.g., local variables in C subprograms and Java
methods, allocated from the runtime stack
Advantage:
Allows recursion; conserves storage by all subprog.
Disadvantages:
Overhead of allocation and deallocation
Subprograms cannot be history sensitive
Inefficient references (indirect addressing)
8
Stack-dynamic Variables
Heap-dynamic Variables
Heap: a section of virtual address space
reserved for dynamic memory allocation
Allocated and deallocated (in heap) by explicit
directives or operators, specified by the
programmer, which take effect during
execution.
10
Heap-dynamic Variables
Person *p;
p=(Person *) malloc(sizeof
Person);
p->name = Mike; p->age = 40;
free(p);
Advantage: can construct dynamic structures
Disadvantage: inefficient, unreliable, heap
management cost
11
Heap-dynamic Variables
12
1-12
Overview
Study fundamental semantic issues of variables:
Attributes of variables
Binding of variables
Scope
Referencing environments
13
Why Scope?
Given multiple bindings of a single name, how
do we know which binding does an
occurrence of this name refer to?
14
Scope
The scope of a variable is the range of
statements over which it is visible
A variable is visible in a statement if it can be
referenced in that statement
Scope
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
A: begin
int i,j; real x,y;
B: procedure text (int a,b)
begin boolean i;
----------------x=i*j+y;
----------------End b
---------------------C: begin int x,y; real i,j ;
---------------------------D: begin boolean j;
-----------------------Call text (x,y)
-----------------------End d
-----------------------End c
-----------------------End a
16
Scope
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
A: begin
int i,j; real x,y;
B: procedure text (int a,b)
begin boolean i;
----------------x=i*j+y;
----------------End b
---------------------C: begin int x,y; real i,j ;
---------------------------D: begin boolean j;
-----------------------Call text (x,y)
-----------------------End d
-----------------------End c
-----------------------End a
i -> int(2)
Scope of i = 2,3,9,19, 20
Hole of i = 4 to 8, 10 to 18
17
Scope
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
A: begin
int i,j; real x,y;
B: procedure text (int a,b)
begin boolean i;
----------------x=i*j+y;
----------------End b
---------------------C: begin int x,y; real i,j ;
---------------------------D: begin boolean j;
-----------------------Call text (x,y)
-----------------------End d
-----------------------End c
-----------------------End a
i -> int(2)
Scope of i = 2,3,9,19, 20
Hole of i = 4 to 8, 10 to 18
j -> int(2)
Scope of j = 2 to 9,19,20
Hole of j = 10 to 18
18
Static Scope
The Scheme is statically scoped => the
environment that is extended (and becomes
current) when a procedure is called is the
environment in which the procedure was
created
Scope of a variable can be statically
determined
Based on program text, a spatial concept
19
Dynamic Scope
Based on calling sequences of program units,
not their textual layout (temporal versus
spatial)
Can only be determined at run time
21
Example
X:real;
procedure show()
{
print(x);
}
procedure small()
{
x:real ;
x = 0.125;
show();
}
main()
{
x = 0.25;
show();
small();
}
22
Example
X:real;
procedure show()
{
print(x);
}
procedure small()
{
x:real ;
x = 0.125;
show();
}
main()
{
x = 0.25;
show();
small();
}
Static :
Show() -> 0.25
Small() ->0.25
23
Example
X:real;
procedure show()
{
print(x);
}
procedure small()
{
x:real ;
x = 0.125;
show();
}
main()
{
x = 0.25;
show();
small();
}
Static :
Show() -> 0.25
Small() ->0.25
Dynamic
Show() ->0.25
Small() ->0.125
24
question
begin integer m, n;
procedure hardy;
begin
print("in hardy -- n = ", n);
end;
procedure laurel(n: integer);
begin
print("in laurel -- m = ", m);
print("in laurel -- n = ", n);
hardy;
end;
m := 50; n := 100;
print("in main program -- n = ", n);
laurel(1);
hardy;
end;
25
in
in
in
in
in
in
in
in
in
in
26
Disadvantage:
Cannot statically type check references to
nonlocals
Poor readability
Longer accesses to nonlocal variables
27
Overview
Study fundamental semantic issues of variables:
Attributes of variables
Binding of variables
Type
Scope
Referencing environments
28
Referencing Environments
Referencing environment of a statement is the
collection of all names visible to the statement
In a static-scoped language, it is the local
variables plus all of the visible variables in all of
the enclosing scopes
In a dynamic-scoped language, the referencing
environment is the local variables plus all visible
variables in all active subprograms
A subprogram is active if its execution has begun but
has not yet terminated
29
Referencing Environments
30
Summary
Variables are abstractions for memory cells of
the computer and are characterized by name,
address, value, type, lifetime, scope
Binding is the association of attributes with
program entities: type and storage binding
Scope determines the visibility of variables in
a statement
31