Docsity

UNC Chapel Hill: Lecture 15 - Control Abstraction and Closures - Prof. Aaron Block, Study notes of Programming Languages

Partial preview of the text

Download UNC Chapel Hill: Lecture 15 - Control Abstraction and Closures - Prof. Aaron Block and more Study notes Programming Languages in PDF only on Docsity!

Lecture 15: Control Abstraction

COMP 524 Programming Language Concepts Aaron Block March 8, 2007 Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Closures

  • Deep binding is implemented using closures
  • A closure is the combination of a reference to a subroutine and an explicit representation of its referencing environment
  • Typically implemented with
    • A pointer to the subroutines code
    • If the scoping is dynamic, we need a way to temporarily unroll al the changes since the reference was created. 2

Abstraction

  • Programming languages support the binding of names with potentially complex program fragments that can be used through an interface - Programmers only need to know about the purpose of the fragment rather than its implementation => Abstraction
  • A control abstraction performs a well-defined operation
    • Subroutines
  • A data abstraction represents information
    • Data structures
    • most data structures include some number of control abstractions 4

Subroutines

  • Execute an operation on behalf of a calling program unit
  • Subroutines can be parameterized
    • The parameters in the definition of the function are known as formal parameters
    • The parameters passed in the subroutine call are known as actual parameters or arguments
    • At the time of the call, actual parameters are mapped to formal parameters
  • Functions are subroutines that return a value, while 5

A A A A Return Address Misc. Bookkeeping Local Variables Temps sp fp fp A Stack growth 1001: A( 3 ) ... 2001: int A(int n){ int m = n *n; return m + A(n-1); }

Actual Parameters

Formal Parameters

Arguments & returns n m

8 Saved Register Local variables and temporaries Space to build argument lists fp,sp Arguments & Returns Current frame Previous frame

  • Stack Pointer sp
    • Top of frame stack
  • Frame Pointer fp
    • Access to arguments and locals via offset of fp
  • The differ if temporary space is allocated in stack

Calling Sequence

  • On procedure call and return compilers generate code that execute to manage the runtime stack. - Setup at call to procedure foo(a,b). - Prologue before foo code executes. - Epilogue at the end of foo code. - “Teardown” right after calling the code. 10

Setup foo(a,b)

  • Move sp to allocate a new stack frame
  • Copy args a,b into frame
  • Copy return address into frame
  • Set fp to point to new frame
  • Maintain static chain or display
  • Move PC to procedure address 11

Setup foo(a,b)

  • Move sp to allocate a new stack frame
  • Copy args a,b into frame
  • Copy return address into frame
  • Set fp to point to new frame
  • Maintain static chain or display
  • Move PC to procedure address 13 Subroutine B Subroutine A (called from main) Subroutine Bar sp fp Subroutine foo

Setup foo(a,b)

  • Move sp to allocate a new stack frame
  • Copy args a,b into frame
  • Copy return address into frame
  • Set fp to point to new frame
  • Maintain static chain or display
  • Move PC to procedure address 14 Subroutine B Subroutine A (called from main) Subroutine Bar sp fp Subroutine foo A & B

Setup foo(a,b)

16 Subroutine B Subroutine A (called from main) Subroutine Bar sp fp A & B Return Ad Subroutine foo

  • Move sp to allocate a new stack frame
  • Copy args a,b into frame
  • Copy return address into frame
  • Set fp to point to new frame
  • Maintain static chain or display
  • Move PC to procedure address

Setup foo(a,b)

17 Subroutine B Subroutine A (called from main) Subroutine Bar sp A & B Return Ad Subroutine foo

  • Move sp to allocate a new stack frame
  • Copy args a,b into frame
  • Copy return address into frame
  • Set fp to point to new frame
  • Maintain static chain or display
  • Move PC to procedure address fp

If the callee is nested inside

the caller , then the callee’s

static link should refer to the

caller’s frame.

Setup foo(a,b)

19 Subroutine B Subroutine A (called from main) Subroutine Bar sp A & B Return Ad Subroutine foo

  • Move sp to allocate a new stack frame
  • Copy args a,b into frame
  • Copy return address into frame
  • Set fp to point to new frame
  • Maintain static chain or display
  • Move PC to procedure address fp

This changes

where the code

is executed.

Prologue

  • Copy registers into local slots
  • Object initialization. 20