Top Banner
144

Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

May 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Scopes, and Bindings

Akim Demaille Étienne Renault Roland [email protected]

EPITA � École Pour l'Informatique et les Techniques Avancées

February 10, 2019

Page 2: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Scopes, and Bindings

1 Bindings

2 Symbol Tables

3 Complications

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 2 / 56

Page 3: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Bindings

1 BindingsNamesScopesBinding Time

2 Symbol Tables

3 Complications

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 3 / 56

Page 4: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names

1 BindingsNamesScopesBinding Time

2 Symbol Tables

3 Complications

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 4 / 56

Page 5: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Identi�ers, Symbols

Name (Identi�ers, Symbols)

reference

address

value

To refer to some entities: variable, type, function, namespace,constant, control structure (e.g., named next, continue in Perl), etc.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 5 / 56

Page 6: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Identi�ers

usually alphanumeric and underscore, letter �rst, without white spaces.

ALGOL 60, FORTRAN ignore white spaces.limitation on the length

6 characters for the original FORTRAN (Fortran 90: 31),ISO C: 31no limit for most others.

case insensitive in Modula-2 and Ada.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 6 / 56

Page 7: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Objects, and Bindings [Edwards, 2003]

Object 2

Object 3

Object 1

Object 4

Name 1

Name 2

Name 3

Name 4

binding

binding

binding

binding

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 7 / 56

Page 8: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Objects, and Bindings

When are objects created and destroyed?Lifetimes (deferred to a later lecture).

When are names created and destroyed?Scopes.

When are bindings created and destroyed?Binding times.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 8 / 56

Page 9: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Objects, and Bindings

When are objects created and destroyed?Lifetimes (deferred to a later lecture).

When are names created and destroyed?Scopes.

When are bindings created and destroyed?Binding times.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 8 / 56

Page 10: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Names, Objects, and Bindings

When are objects created and destroyed?Lifetimes (deferred to a later lecture).

When are names created and destroyed?Scopes.

When are bindings created and destroyed?Binding times.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 8 / 56

Page 11: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes

1 BindingsNamesScopesBinding Time

2 Symbol Tables

3 Complications

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 9 / 56

Page 12: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes [Edwards, 2003]

When are names created, visible, and destroyed?

ScopeThe textual region in the source in which the binding is active.

Static ScopingThe scope can be computed at compile-time.

Dynamic ScopingThe scope depends on runtime conditions such as the function calls.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 10 / 56

Page 13: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes [Edwards, 2003]

When are names created, visible, and destroyed?

ScopeThe textual region in the source in which the binding is active.

Static ScopingThe scope can be computed at compile-time.

Dynamic ScopingThe scope depends on runtime conditions such as the function calls.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 10 / 56

Page 14: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes [Edwards, 2003]

When are names created, visible, and destroyed?

ScopeThe textual region in the source in which the binding is active.

Static ScopingThe scope can be computed at compile-time.

Dynamic ScopingThe scope depends on runtime conditions such as the function calls.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 10 / 56

Page 15: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 16: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 17: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 18: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 19: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 20: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 21: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Why Scopes?

Scopes are the �rst form of structure/modularity

No scopes in assembly

No scopes in MFS(First generation of the Macintosh File System)

Without scopes, names have a global in�uence

With scopes, the programmer can focus on local in�uences

Scopes in correct programs with unique identi�ers are �useless�

C++ namespaces are �pure scopes�

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 11 / 56

Page 22: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Declaration

Blocks determine scopes.

local variables

non local variables

global variables

int global;

auto outer(void)

{

int local, non_local;

int inner(void)

{

return global + non_local;

}

return inner;

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 12 / 56

Page 23: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 24: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 25: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 26: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 27: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 28: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 29: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Static Scoping

In most languages(Ada, C, Tiger, FORTRAN, Scheme, Perl (my), etc.).

Enables static binding.

Enables static typing.Enables strong typing (Ada, ALGOL 68, Tiger).

saferfasterclearer

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 13 / 56

Page 30: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Dynamic Scoping

In most scripting/interpreted languages (Perl (local), Shell Script,TEX etc.) but also in Lisp (as opposed to Scheme).

Dynamic Scoping in TeX

% \x, \y undefined.

{

% \x, \y undefined.

\def \x 1

% \x defined, \y undefined.

\ifnum \a < 42

\def \y 51

\fi

% \x defined, \y may be defined.

}

% \x, \y undefined.

Prevents static typingAn identi�er may refer to di�erent values, with di�erent types.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 14 / 56

Page 31: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Dynamic Scoping

In most scripting/interpreted languages (Perl (local), Shell Script,TEX etc.) but also in Lisp (as opposed to Scheme).

Dynamic Scoping in TeX

% \x, \y undefined.

{

% \x, \y undefined.

\def \x 1

% \x defined, \y undefined.

\ifnum \a < 42

\def \y 51

\fi

% \x defined, \y may be defined.

}

% \x, \y undefined.

Prevents static typingAn identi�er may refer to di�erent values, with di�erent types.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 14 / 56

Page 32: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Dynamic Scoping

In most scripting/interpreted languages (Perl (local), Shell Script,TEX etc.) but also in Lisp (as opposed to Scheme).

Dynamic Scoping in TeX

% \x, \y undefined.

{

% \x, \y undefined.

\def \x 1

% \x defined, \y undefined.

\ifnum \a < 42

\def \y 51

\fi

% \x defined, \y may be defined.

}

% \x, \y undefined.

Prevents static typingAn identi�er may refer to di�erent values, with di�erent types.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 14 / 56

Page 33: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in Tiger

Many di�erent t, including several �variables�.

t time

let

type t = { h: int, t: t }

function t (h: int, t: t) : t =

t { h = h, t = t }

var t := t (12, nil)

var t := t (12, t)

in

t.t = t

end

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 15 / 56

Page 34: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes [Appel, 1998]

ML

structure M = struct

structure E = struct

val a = 5;

end

structure N = struct

val b = 10;

val a = E.a + b;

end

structure D = struct

val d = E.a + N.a;

end

end

Java (fwd declaration allowed)

package M;

class E {

static int a = 5;

}

class N {

static int b = 10;

static int a = E.a + b;

}

class D {

static int d = E.a + N.a;

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 16 / 56

Page 35: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes [Appel, 1998]

structure M = struct

structure E = struct

val a = 5;

end

structure N = struct

val b = 10;

val a = E.a + b;

end

structure D = struct

val d = E.a + N.a;

end

end

σ0 = Prelude

σ1 = {a : int}σ2 = {E : σ1}σ3 = {b : int, a : int}σ4 = {N : σ3}σ5 = {d : int}σ6 = {D : σ5}σ7 = σ2 + σ4 + σ6

σ0 + σ2 ` N : σ3 (ML)

σ0 + σ2 + σ4 ` N : σ3 (Java)

σ0 + σ2 + σ4 + σ6 ` M : σ7

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 17 / 56

Page 36: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lifetime (or extent)

Lifetime is a di�erent matter, related to the execution (as opposed tovisibility).

Extent bound to lifetime of block tend to promote global variables(Pascal).

Static local variables as in C (static), ALGOL 60 own, PL/I.Initialization?

Modules tend to replace this block related feature.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 18 / 56

Page 37: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lifetime (or extent)

Lifetime is a di�erent matter, related to the execution (as opposed tovisibility).

Extent bound to lifetime of block tend to promote global variables(Pascal).

Static local variables as in C (static), ALGOL 60 own, PL/I.Initialization?

Modules tend to replace this block related feature.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 18 / 56

Page 38: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lifetime (or extent)

Lifetime is a di�erent matter, related to the execution (as opposed tovisibility).

Extent bound to lifetime of block tend to promote global variables(Pascal).

Static local variables as in C (static), ALGOL 60 own, PL/I.Initialization?

Modules tend to replace this block related feature.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 18 / 56

Page 39: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lifetime (or extent)

Lifetime is a di�erent matter, related to the execution (as opposed tovisibility).

Extent bound to lifetime of block tend to promote global variables(Pascal).

Static local variables as in C (static), ALGOL 60 own, PL/I.Initialization?

Modules tend to replace this block related feature.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 18 / 56

Page 40: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lifetime (or extent)

Lifetime is a di�erent matter, related to the execution (as opposed tovisibility).

Extent bound to lifetime of block tend to promote global variables(Pascal).

Static local variables as in C (static), ALGOL 60 own, PL/I.Initialization?

Modules tend to replace this block related feature.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 18 / 56

Page 41: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 42: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 43: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 44: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 45: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 46: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 47: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 48: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scopes in C++

A lot of scope in a real programming language.

Block scope

Function parameter scope

Function scope

Namespace scope

Class scope

Enumeration Scope

Template parameter scope

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 19 / 56

Page 49: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binding Time

1 BindingsNamesScopesBinding Time

2 Symbol Tables

3 Complications

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 20 / 56

Page 50: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binding Time [Edwards, 2003]

When a binding from a name to an object is made.

Binding Time Examples

language design if

language implementation data widthprogram writing foo, barcompilation static objects, codelinkage relative addressesloading shared objectsexecution heap objects

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 21 / 56

Page 51: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binding Time: the moving IN

Roughly, �exibility and e�ciency

are mutually exclusive

depend on binding time.

The Moving IN

binding-time

early ---------------------------------> late

INflexibility flexibility

efficiency INefficiency

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 22 / 56

Page 52: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binding Time: the moving IN

Roughly, �exibility and e�ciency

are mutually exclusive

depend on binding time.

The Moving IN

binding-time

early ---------------------------------> late

INflexibility flexibility

efficiency INefficiency

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 22 / 56

Page 53: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binding Time: the moving IN

Roughly, �exibility and e�ciency

are mutually exclusive

depend on binding time.

The Moving IN

binding-time

early ---------------------------------> late

INflexibility flexibility

efficiency INefficiency

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 22 / 56

Page 54: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Dynamic Binding: virtual in C++

Dynamic dispatch is roughly runtime overloading.

Dynamic Dispatch in C++

struct Shape

{

virtual void draw() const = 0;

};

struct Square : public Shape

{

void draw() const override {};

};

struct Circle : public Shape

{

void draw() const override {};

};

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 23 / 56

Page 55: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Dynamic Binding: virtual in C++

Dynamic Dispatch in C++

#include <vector>

#include "shapes.hh"

using shapes_type = std::vector<Shape*>;

int main()

{

auto ss = shapes_type{new Circle, new Square};

for (auto s: ss)

// Inclusion polymorphism.

s->draw();

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 24 / 56

Page 56: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Late Code Binding: eval

Most interpretedlanguages support eval(explicit or not): runtimecode evaluation.

Enables languageextensions.

try/catch in Perl

sub try (&@) {

my ($try, $catch) = @_;

eval { &$try }; # Explicit eval.

if ($@) {

local $_ = $@;

&$catch;

}

}

sub catch (&) {

$_[0]; # implicit eval.

}

try {

die "phooey";

} catch {

/phooey/ and print "unphooey\n";

};A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 25 / 56

Page 57: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Late Code Binding: eval

Most interpretedlanguages support eval(explicit or not): runtimecode evaluation.

Enables languageextensions.

try/catch in Perl

sub try (&@) {

my ($try, $catch) = @_;

eval { &$try }; # Explicit eval.

if ($@) {

local $_ = $@;

&$catch;

}

}

sub catch (&) {

$_[0]; # implicit eval.

}

try {

die "phooey";

} catch {

/phooey/ and print "unphooey\n";

};A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 25 / 56

Page 58: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Late Code Binding: eval

Most interpretedlanguages support eval(explicit or not): runtimecode evaluation.

Enables languageextensions.

try/catch in Perl

sub try (&@) {

my ($try, $catch) = @_;

eval { &$try }; # Explicit eval.

if ($@) {

local $_ = $@;

&$catch;

}

}

sub catch (&) {

$_[0]; # implicit eval.

}

try {

die "phooey";

} catch {

/phooey/ and print "unphooey\n";

};A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 25 / 56

Page 59: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Late Code Binding: eval

Most interpretedlanguages support eval(explicit or not): runtimecode evaluation.

Enables languageextensions.

try/catch in Perl

sub try (&@) {

my ($try, $catch) = @_;

eval { &$try }; # Explicit eval.

if ($@) {

local $_ = $@;

&$catch;

}

}

sub catch (&) {

$_[0]; # implicit eval.

}

try {

die "phooey";

} catch {

/phooey/ and print "unphooey\n";

};A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 25 / 56

Page 60: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binding Times in Tiger [Edwards, 2003]

Design Keywords

Program Identi�ers

Compile Function code, frames, types

Execution Records, arrays addresses

Little dynamic behavior

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 26 / 56

Page 61: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables

1 Bindings

2 Symbol Tables

3 Complications

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 27 / 56

Page 62: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Visiting an ast

For statically scoped languages

many traversals check uses against de�nitions

most traversals need a form of memory (binding, type, escapes,inlining, translation, etc.)

this memory is related to scopes

we need some reversible memory (do/undo)

Similarly for narrow compilers without ast

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 28 / 56

Page 63: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Visiting an ast

For statically scoped languages

many traversals check uses against de�nitions

most traversals need a form of memory (binding, type, escapes,inlining, translation, etc.)

this memory is related to scopes

we need some reversible memory (do/undo)

Similarly for narrow compilers without ast

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 28 / 56

Page 64: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Visiting an ast

For statically scoped languages

many traversals check uses against de�nitions

most traversals need a form of memory (binding, type, escapes,inlining, translation, etc.)

this memory is related to scopes

we need some reversible memory (do/undo)

Similarly for narrow compilers without ast

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 28 / 56

Page 65: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Visiting an ast

For statically scoped languages

many traversals check uses against de�nitions

most traversals need a form of memory (binding, type, escapes,inlining, translation, etc.)

this memory is related to scopes

we need some reversible memory (do/undo)

Similarly for narrow compilers without ast

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 28 / 56

Page 66: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Visiting an ast

For statically scoped languages

many traversals check uses against de�nitions

most traversals need a form of memory (binding, type, escapes,inlining, translation, etc.)

this memory is related to scopes

we need some reversible memory (do/undo)

Similarly for narrow compilers without ast

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 28 / 56

Page 67: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables: Scopes

Handle scopes?

not needed if all the names areunique

or if there exists a uniqueidenti�er

required otherwise

Handle scopes explicitly?

yes: the tables support undo:scoped symbol tables

no: rely on automatic variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 29 / 56

Page 68: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables: Scopes

Handle scopes?

not needed if all the names areunique

or if there exists a uniqueidenti�er

required otherwise

Handle scopes explicitly?

yes: the tables support undo:scoped symbol tables

no: rely on automatic variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 29 / 56

Page 69: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables: Scopes

Handle scopes?

not needed if all the names areunique

or if there exists a uniqueidenti�er

required otherwise

Handle scopes explicitly?

yes: the tables support undo:scoped symbol tables

no: rely on automatic variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 29 / 56

Page 70: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables: Scopes

Handle scopes?

not needed if all the names areunique

or if there exists a uniqueidenti�er

required otherwise

Handle scopes explicitly?

yes: the tables support undo:scoped symbol tables

no: rely on automatic variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 29 / 56

Page 71: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables: Scopes

Handle scopes?

not needed if all the names areunique

or if there exists a uniqueidenti�er

required otherwise

Handle scopes explicitly?

yes: the tables support undo:scoped symbol tables

no: rely on automatic variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 29 / 56

Page 72: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Symbol Tables: Scopes

Handle scopes?

not needed if all the names areunique

or if there exists a uniqueidenti�er

required otherwise

Handle scopes explicitly?

yes: the tables support undo:scoped symbol tables

no: rely on automatic variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 29 / 56

Page 73: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 74: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 75: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 76: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 77: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 78: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 79: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 80: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

(Non Scoped) Symbol Tables

An associative array

put

get

Implementation

a list

a tree

a hash

...

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 30 / 56

Page 81: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scoped Symbol Table: symbol::Table

class Table

template <typename Entry_T>

class Table

{

public:

Table();

auto put(symbol key, Entry_T& val) -> void;

auto get(symbol key) const -> Entry_T*;

auto scope_begin() -> void;

auto scope_end() -> void;

auto print(std::ostream& ostr) const -> void;

};

Not very C++ (iterators instead of pointers, operator[], etc.)

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 31 / 56

Page 82: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scoped Symbol Table Implementations

Mixing Stacks and Associative Arrays

Copying, or not copying?

Functional (Non Destructive) Versions

Mongrels

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 32 / 56

Page 83: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scoped Symbol Table Implementations

Mixing Stacks and Associative Arrays

Copying, or not copying?

Functional (Non Destructive) Versions

Mongrels

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 32 / 56

Page 84: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scoped Symbol Table Implementations

Mixing Stacks and Associative Arrays

Copying, or not copying?

Functional (Non Destructive) Versions

Mongrels

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 32 / 56

Page 85: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Scoped Symbol Table Implementations

Mixing Stacks and Associative Arrays

Copying, or not copying?

Functional (Non Destructive) Versions

Mongrels

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 32 / 56

Page 86: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind

and Paracetamol

never

tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 87: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind

and Paracetamol

never

tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 88: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind

and Paracetamol

never

tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 89: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind

and Paracetamol

never

tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 90: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind and Paracetamol

never

tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 91: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind and Paracetamol

never

tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 92: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management

When do you deallocate associated data?

scope end deallocate everything since the latest scope_begin

pass end deallocate auxiliary data after the traversal is completed

ast bind the data to the ast and delegate deallocation

by hand thanks God for Valgrind and Paracetamol

never tu sors

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 33 / 56

Page 93: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate on scope exit

But then...

Twice foo

let var foo := 42

var foo := 51

in foo end

Two lets

let var foo := 42 in

let var foo := 51

in foo end end

but then again...

Escaping type

let type rec = {}

in rec {} end <> nil

Segmentation violation...

Courtesy of Arnaud Fabre.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 34 / 56

Page 94: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate on scope exit

But then...

Twice foo

let var foo := 42

var foo := 51

in foo end

Two lets

let var foo := 42 in

let var foo := 51

in foo end end

but then again...

Escaping type

let type rec = {}

in rec {} end <> nil

Segmentation violation...

Courtesy of Arnaud Fabre.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 34 / 56

Page 95: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate on scope exit

But then...

Twice foo

let var foo := 42

var foo := 51

in foo end

Two lets

let var foo := 42 in

let var foo := 51

in foo end end

but then again...

Escaping type

let type rec = {}

in rec {} end <> nil

Segmentation violation...

Courtesy of Arnaud Fabre.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 34 / 56

Page 96: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate on scope exit

But then...

Twice foo

let var foo := 42

var foo := 51

in foo end

Two lets

let var foo := 42 in

let var foo := 51

in foo end end

but then again...

Escaping type

let type rec = {}

in rec {} end <> nil

Segmentation violation...Courtesy of Arnaud Fabre.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 34 / 56

Page 97: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate with the AST

annotate each node of ast

annotate each scoping node with a symbol table and link them

leave tables outside

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 35 / 56

Page 98: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate with the AST

annotate each node of ast

annotate each scoping node with a symbol table and link them

leave tables outside

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 35 / 56

Page 99: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Memory Management: Deallocate with the AST

annotate each node of ast

annotate each scoping node with a symbol table and link them

leave tables outside

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 35 / 56

Page 100: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Factoring Scope Handling

no scope handling needed if names are unique

so use regular associative containers

but how can you guarantee unique names

do you need to make names uniques?

Bind the names/Label by de�nition address

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 36 / 56

Page 101: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Factoring Scope Handling

no scope handling needed if names are unique

so use regular associative containers

but how can you guarantee unique names

do you need to make names uniques?

Bind the names/Label by de�nition address

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 36 / 56

Page 102: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Factoring Scope Handling

no scope handling needed if names are unique

so use regular associative containers

but how can you guarantee unique names

do you need to make names uniques?

Bind the names/Label by de�nition address

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 36 / 56

Page 103: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Factoring Scope Handling

no scope handling needed if names are unique

so use regular associative containers

but how can you guarantee unique names

do you need to make names uniques?

Bind the names/Label by de�nition address

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 36 / 56

Page 104: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Factoring Scope Handling

no scope handling needed if names are unique

so use regular associative containers

but how can you guarantee unique names

do you need to make names uniques?

Bind the names/Label by de�nition address

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 36 / 56

Page 105: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds...

breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 106: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds...

breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 107: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds...

breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 108: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds...

breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 109: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds...

breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 110: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds...

breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 111: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Binder

annotates uses with links to their de�nitions

uses scoped symbol tables

or regular containers and recursion

checks multiple de�nitions

checks missing de�nitions

and also binds... breaks to their loops

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 37 / 56

Page 112: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Complications

1 Bindings

2 Symbol Tables

3 ComplicationsOverloadingNon Local Variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 38 / 56

Page 113: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Overloading

1 Bindings

2 Symbol Tables

3 ComplicationsOverloadingNon Local Variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 39 / 56

Page 114: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Overloading

Overloading: HomonymsSeveral entities bearing the same name,but statically distinguishable, e.g., bytheir arity, type etc.

Aliasing: SynonymsOne entity bearing several names.

// foo is overloaded.

int foo(int);

int foo(float);

// x and y are aliases.

int x;

int& y = x;

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 40 / 56

Page 115: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Operator Overloading

Overloading is meant to simplify the user's life. Since FORTRAN!

Overloading in Caml

# 1 + 2;;

- : int = 3

# 1.0 + 2.0;;

Characters 0-3:

1.0 + 2.0;;

^^^

This expression has type float but is here used with type int

# 1.0 +. 2.0;;

- : float = 3.

Thank God, C was invented to improve Caml:int a = 1 + 2;;

float b = 1.0 + 2.0;;

Of course this is unfair: Caml has type inference.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 41 / 56

Page 116: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Operator Overloading

Overloading is meant to simplify the user's life. Since FORTRAN!

Overloading in Caml

# 1 + 2;;

- : int = 3

# 1.0 + 2.0;;

Characters 0-3:

1.0 + 2.0;;

^^^

This expression has type float but is here used with type int

# 1.0 +. 2.0;;

- : float = 3.

Thank God, C was invented to improve Caml:int a = 1 + 2;;

float b = 1.0 + 2.0;;

Of course this is unfair: Caml has type inference.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 41 / 56

Page 117: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Operator Overloading

Overloading is meant to simplify the user's life. Since FORTRAN!

Overloading in Caml

# 1 + 2;;

- : int = 3

# 1.0 + 2.0;;

Characters 0-3:

1.0 + 2.0;;

^^^

This expression has type float but is here used with type int

# 1.0 +. 2.0;;

- : float = 3.

Thank God, C was invented to improve Caml:int a = 1 + 2;;

float b = 1.0 + 2.0;;

Of course this is unfair: Caml has type inference.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 41 / 56

Page 118: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Function Overloading

Usually based on the arguments(Ada, C++, Java...; not C, ALGOL 60, Fortran...).

ALGOL 60

integer I;

real X;

...

PUTSTRING("results are: "); PUTINT(I); PUTREAL(X);

Ada [ARM, 1983]

I : INTEGER;

X : REAL;

...

PUT("results are: "); PUT(I); PUT(X);

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 42 / 56

Page 119: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Overloading is Syntactic Sugar

Overloaded

#include <string>

void foo(int);

void foo(char);

void foo(const char*);

void foo(std::string);

int

main ()

{

foo(0);

foo('0');

foo("0");

foo(std::string("0"));

}

Un-overloaded

#include <string>

void foo_int(int);

void foo_char(char);

void foo_char_p(const char*);

void foo_std_string(std::string);

int

main ()

{

foo_int(0);

foo_char('0');

foo_char_p("0");

foo_std_string(std::string("0"));

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 43 / 56

Page 120: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Overloading is Syntactic Sugar

Overloaded

#include <string>

void foo(int);

void foo(char);

void foo(const char*);

void foo(std::string);

int

main ()

{

foo(0);

foo('0');

foo("0");

foo(std::string("0"));

}

Un-overloaded

#include <string>

void foo_int(int);

void foo_char(char);

void foo_char_p(const char*);

void foo_std_string(std::string);

int

main ()

{

foo_int(0);

foo_char('0');

foo_char_p("0");

foo_std_string(std::string("0"));

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 43 / 56

Page 121: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Overloading is Syntactic Sugar

Usually solved by renaming/mangling.

g++-2.95, comof__Fi -> int f(int);

f__FPc -> int f(char*);

g++-3.2, icc_Z1fi -> int f(int);

_Z1fPc -> int f(char*);

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 44 / 56

Page 122: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Overloading in Tiger

Ordering <, <=, >, and >=

overloaded for pairs of integers, or strings.

Identity = and <>

overloaded for (type coherent) pairs of integers, strings,arrays or records.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 45 / 56

Page 123: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Non Local Variables

1 Bindings

2 Symbol Tables

3 ComplicationsOverloadingNon Local Variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 46 / 56

Page 124: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lambda Shifting

With nested functions

int global;

int outer(void)

{

int local, non_local;

int inner(void)

{

return

global + non_local;

}

return inner();

}

Without

int global;

int outer_inner_(int* non_local)

{

return global + *non_local;

}

int outer(void)

{

int local, non_local;

return outer_inner_(&non_local);

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 47 / 56

Page 125: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Lambda Shifting

With nested functions

int global;

int outer(void)

{

int local, non_local;

int inner(void)

{

return

global + non_local;

}

return inner();

}

Without

int global;

int outer_inner_(int* non_local)

{

return global + *non_local;

}

int outer(void)

{

int local, non_local;

return outer_inner_(&non_local);

}

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 47 / 56

Page 126: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Non Local Variables

let

function outer(): int =

let

nonlocal var outer := 0

in

let

function inner() : int =

let

var inner := 1

in

inner + outer

end

in

inner()

end

end

in

outer ()

endA. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 48 / 56

Page 127: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Non Non Local Variables

let

let

local var outer := 0

in

let

let

var inner := 1

in

inner + outer

end

in

end

end

in

endA. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 49 / 56

Page 128: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Non Non Local Variables

let

function outer(): int =

let

local var outer := 0

in

let

let

var inner := 1

in

inner + outer

end

in

end

end

in

outer()

endA. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 50 / 56

Page 129: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

The Escapes and Functional Programming

let

function add(nonlocal a: int, b: int) : int =

let

function add_a(x: int) : int = a + x

in

add_a(b)

end

in

print_int(add(1, 2));

print("\n")

end

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 51 / 56

Page 130: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Closures

let

function add_gen(nonlocal a: int) : int -> int =

let

function add_a(x: int) : int = a + x

in

add_a

end

incr = add_gen(1);

in

print_int(incr(2));

print("\n");

end

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 52 / 56

Page 131: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

The Escapes & Recursion

let

function one(input : int) =

let

function two() =

(print("two: "); print_int(input);

print("\n");

one(input))

in

if input > 0 then

(input := input - 1;

two(); print("one: ");

print_int(input); print("\n"))

end

in

one (3)

end

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 53 / 56

Page 132: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 133: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 134: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 135: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 136: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 137: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 138: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Escaping Variables/Arguments

Technically escaping means �cannot be stored in a register�.

In C Large values (arrays, structs).Variables whose address is taken.Variable arguments.

In Tiger variables/arguments from outer functions.not variables/arguments from outer scopes.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 54 / 56

Page 139: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Annotating the ast

being non local means having non local uses

obviously non local variables need to be accessible from inner functions

to simplify the compiler, it is easier to leave them on the stack

hence the translation to intermediate representation needs to knowwhich variables are non local from their de�nitions

therefore a preleminary pass should �ag non local variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 55 / 56

Page 140: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Annotating the ast

being non local means having non local uses

obviously non local variables need to be accessible from inner functions

to simplify the compiler, it is easier to leave them on the stack

hence the translation to intermediate representation needs to knowwhich variables are non local from their de�nitions

therefore a preleminary pass should �ag non local variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 55 / 56

Page 141: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Annotating the ast

being non local means having non local uses

obviously non local variables need to be accessible from inner functions

to simplify the compiler, it is easier to leave them on the stack

hence the translation to intermediate representation needs to knowwhich variables are non local from their de�nitions

therefore a preleminary pass should �ag non local variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 55 / 56

Page 142: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Annotating the ast

being non local means having non local uses

obviously non local variables need to be accessible from inner functions

to simplify the compiler, it is easier to leave them on the stack

hence the translation to intermediate representation needs to knowwhich variables are non local from their de�nitions

therefore a preleminary pass should �ag non local variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 55 / 56

Page 143: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Annotating the ast

being non local means having non local uses

obviously non local variables need to be accessible from inner functions

to simplify the compiler, it is easier to leave them on the stack

hence the translation to intermediate representation needs to knowwhich variables are non local from their de�nitions

therefore a preleminary pass should �ag non local variables

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 55 / 56

Page 144: Names, Scopes, and Bindingstiger/lecture-notes/slides/ccmp/05-names.pdf · Scope Thetextualregion in thesourcein which the binding is active. Static Scoping The scope can be computed

Bibliography I

Appel, A. W. (1998).Modern Compiler Implementation in C, Java, ML.Cambridge University Press.

ARM (1983).Ada Reference Manual.

Edwards, S. (2003).COMS W4115 Programming Languages and Translators.http://www.cs.columbia.edu/~sedwards/classes/2003/w4115/.

A. Demaille, E. Renault, R. Levillain Names, Scopes, and Bindings 56 / 56