Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Access to non-local names and parameter passing

How compilers resolve names that are not local to a routine (non-local names) and the precise semantics and implementation of common parameter-passing methods used at procedure/function calls.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Compilers must decide which variable a name refers to and how actual parameters are delivered to called routines. Correct semantics for non-local names and parameter passing affect program correctness, performance, and generated code (activation records, links, and calling sequences). Engineers use these concepts when implementing front ends, run-time systems, optimizers, or when reasoning about language semantics.

Key ideas

Start with definitions (each term is defined before first use).

  • Name: an identifier in source code that stands for a variable, function, type, or other entity.
  • Local name: a name whose declaration appears in the current scope (block or function) and refers to an entity stored in the current activation record.
  • Non-local name: a name used in a routine but declared in an enclosing scope (lexically enclosing function or global scope).
  • Scope: the region of program text where a declaration is visible.
  • Binding: the association between a name and its declaration (and storage location).
  • Static (lexical) scoping: the rule that the binding of a name is determined by program text and the nesting of declarations. If multiple declarations with the same name exist, the most closely nested (in the source tree) is used.
  • Dynamic scoping: the rule that the binding of a name is determined by the runtime call chain (the most recent activation with a matching declaration).
  • Activation record (AR): a block of run-time storage (stack frame) for one call of a routine; typically contains return address, local variables, control link, access link, and saved registers.
  • Control link: pointer in an AR that points to the AR of the caller (used for stack unwinding and return control).
  • Access link (also called static link): a pointer in an activation record that points to the AR of the lexically enclosing routine; used to access non-local names when using a static-chain implementation. With a display, there is no per-AR access link; instead the display array holds the current AR at each lexical level.
  • Display: an array data structure where each entry points to the most recent AR at a particular lexical nesting level; used to access non-local names in O(1) time.
  • Closure: a pair consisting of a function code pointer and the environment (access links or saved variables) needed to resolve non-local names when the function is called later.
  • Call-by-value: actual parameter value is evaluated, a copy is passed to the callee; the callee cannot modify the caller's variable.
  • Call-by-reference: callee receives an implicit reference (address) to the actual variable, so updates in callee change caller's variable.
  • Call-by-value-result (copy-in/copy-out): value is copied into a local location on call and copied back to the actual on return; semantics differ if the same actual is passed to multiple formals.
  • Call-by-name: actual is textually substituted into callee and re-evaluated each time the formal is used; implemented by producing a thunk (deferred computation) or by textual substitution semantics (Algol 60).
  • Call-by-need (lazy): like call-by-name, but the actual is evaluated at most once (memoized) and value reused.
  • Call-by-sharing (object reference semantics): the value of the reference to the object is passed by value; the callee can mutate the object via the reference but cannot rebind the caller's variable. Used in languages like Python and Java.

Exact statements universities expect:

  • Static scoping law: "Given a program with nested declarations, a name occurrence is bound to the declaration found by searching outward through textually enclosing scopes from the occurrence's scope; if none, then to a global or error." (Assumes well-formed program with clear nesting.)

  • Dynamic scoping law: "Given a runtime call chain of activations, a name occurrence is bound to the most recent activation on the call chain that contains a declaration for that name; if none, then to global or error." (Assumes calls build a well-defined chain.)

Implementation methods for accessing non-local names (describe what to draw):

  1. Static chain (access links): draw a vertical stack of activation records. Label each AR with routine name and nesting level. In each AR draw a field labeled "static link" pointing to the AR of the lexically enclosing routine (the AR for the declaration environment). To resolve a non-local name at nesting depth difference d, start at current AR and follow static link d times; then find the variable at fixed offset in that AR.

  2. Display: draw an array called Display indexed by lexical depth. Show that at function entry the compiler saves Display[depth] and sets Display[depth] = address of current AR; on exit it restores the saved Display[depth]. To access a variable declared at depth k, use Display[k] and a fixed offset.

  3. Closures (for first-class nested functions): draw a function object containing code pointer and environment pointer (which may itself be a display or static link). Passing a nested function as parameter means copying the closure so that later calls resolve non-local names to the saved environment. If the closure can outlive its defining activation (escapes), the captured environment must have heap lifetime (e.g., heap-allocated frame or boxes) or be transformed via lambda lifting; keeping variables solely on the stack is unsafe.

Time/space trade-offs:

  • Static chain: O(d) time to access a non-local at depth difference d; simple to implement on stack.
  • Display: O(1) time to access non-local; O(1) work per activation to update one slot; requires a global array of size O(max_nesting) and saving/restoring the overwritten slot.

Parameter passing semantics and implementation notes (assumptions: caller/callee agree on calling convention and layout):

  • Call-by-value: implement by copying value into callee's AR (or passing in registers). Fast, safe, no aliasing.
  • Call-by-reference: implement by passing address (pointer) on stack; requires l-value actuals (variables), not r-values.
  • Call-by-value-result: allocate local slot, copy-in on entry, copy-out on return. Alias handling: if same actual passed to multiple formals, final copies may overwrite in which order often specified or undefined.
  • Call-by-result (copy-out only): allocate a local slot (initial value unspecified), execute the body, then copy-out to the actual on return. Requires l-value actuals; aliasing and copy-out order issues are similar to value-result.
  • Call-by-name: implemented by generating a thunk (a procedure that when called computes or accesses the actual) or by syntactic macro-like substitution; subtle semantics around side-effects and evaluation order.

Edge cases to state: passing the same variable twice, passing array slices, passing expressions as addressable entities (for reference), and parameter passing in presence of exceptions or early returns.

Formulas

(list syntax/short rules to remember; each on its own line, with symbols explained)

static_access(address, d) = follow_static_link(address, d) — address is pointer to current AR, d is lexical difference (integer); follow_static_link means follow the static link d times. Applies when using static chain to locate non-local declaration.

address = display[k] + offset — display is an array indexed by lexical depth k; display[k] is the base address of the AR at level k; offset is the compile-time offset of the variable within that AR. Use when implementing a display.

call sequence (stack): push actuals; push return address; push control link; push static link (if needed); allocate locals — shorthand to remember a typical stack layout used by compilers when supporting nesting. Order is ABI-dependent; the return address is usually placed by the call instruction; the static link may be passed as a hidden parameter or derived from the display. Symbols: actuals are argument values or addresses; control link = caller AR pointer; static link = access link for lexical nesting.

pass-by-reference: push &A — push the address of actual A. Applies when actual is an l-value.

pass-by-value-result: in: formal := value(actual); out on return: actual := value(formal) — copy-in/copy-out semantics; be careful with aliasing.

closure = (code_pointer, env) — environment env is either a static link or a pointer to a display capturing necessary ARs. Use when passing nested functions.

Worked examples

Example 1 — static vs dynamic scoping (write program and resolve names): Program (pseudo-pascal):

  1. procedure P;
  2. var x = 1;
  3. procedure Q;
  4. var x = 2;
    
  5. procedure R;
    
  6.   print(x);
    
  7. end;
    
  8. begin
  9. R();
    
  10. end;
  11. begin
  12. Q();
  13. end;

Question: what does R print under static scoping? under dynamic scoping? Answer (step-by-step):

  • Under static scoping: R is nested inside Q which is nested inside P; the textual nearest x to R is the x declared in Q (line 4). So print(x) prints 2. (Show: draw nesting tree P -> Q -> R and indicate lookup from R up to Q.)
  • Under dynamic scoping: binding depends on call chain at runtime. Call chain when R is called is P -> Q -> R. Search backward along call chain for x: most recent activation with x is Q's x (value 2). So R prints 2 in this call. But if instead code called R from P directly, dynamic scoping could bind to P's x = 1. (Give alternative call sequence to illustrate difference.)

Example 2 — call-by-reference vs call-by-value-result with aliasing: Program (pseudo):

procedure S(a, b) a := a + 1; b := b + 2; end;

x := 1; S(x, x);

Call-by-reference: both formals refer to same variable x. Execution: a := a+1 sets x = 2; b := b+2 sets x = 4. Final x = 4. Call-by-value-result: copy-in: a_slot := 1; b_slot := 1; execute: a_slot := 2; b_slot := 3; copy-out: order matters — if copy-out left-to-right: x := a_slot (2) then x := b_slot (3) final x = 3; if right-to-left: final x = 2. Many languages define unspecified or require no aliasing.

Example 3 — static chain access trace (stack drawing described): Suppose nesting levels: 0 (global), 1 (A), 2 (B), 3 (C). At call C which needs variable declared in A (depth difference d = 2): start at C's AR, follow static link to B's AR (1 step), follow static link to A's AR (2 steps); then variable is at known offset in A's AR. Show numbered steps and offsets.

Common mistakes

  • In call-by-reference, passing an expression (like x+1) as actual: some write code as if a reference to a temporary is legal without addressing semantics; compilers require l-value.
  • Ignoring aliasing in value-result: failing to state copy-out order or consequences.
  • Assuming call-by-name is implemented literally by textual substitution — students miss subtle runtime effects like repeated evaluation and the need for thunks.
  • Not mentioning closures when passing nested functions as parameters; forgetting to capture environment.

For your exam

Types of questions:

  • Definitions: define static link, display, closure, each parameter passing mode.
  • Short notes: compare static chain vs display, pros/cons; explain call-by-name with a thunk example.
  • Derivations: show how a compiler computes lexical difference and offsets, or derive cost (time) to access non-local variable for static chain vs display.
  • Traces: given small program with nested functions, trace name resolution under static and dynamic scoping; trace parameter-passing outcomes for given actuals, including aliasing.
  • Programs/diagrams: draw activation records and show layout of static links and control links.

What full-marks answers include:

  • Precise definitions (bold terms in your answer help), a small example illustrating the rule, clear diagram for ARs or display, and discussion of edge cases (aliasing, temporary values). If asked to compare methods, include time and space trade-offs and typical implementation steps.

Quick check

  1. Define static link (one line).
  2. What value results from passing the same variable twice to a procedure using call-by-value-result (state what affects the result)?
  3. Which parameter passing method evaluates the actual expression 0 or more times and may require a thunk to implement?

Answers: 1) A pointer in an activation record that points to the activation record of the lexically enclosing routine used to access non-local declarations. 2) The result depends on copy-out ordering (left-to-right or right-to-left) — aliasing causes ambiguity; may produce different final values. 3) Call-by-name (needs thunks); call-by-need is a memoized variant.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?