Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 - Control II: Procedures and Environments

Similar presentations


Presentation on theme: "Chapter 8 - Control II: Procedures and Environments"— Presentation transcript:

1 Chapter 8 - Control II: Procedures and Environments
4/23/2017 Chapter 8 - Control II: Procedures and Environments Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden © Kenneth C. Louden, 2003

2 Three major parts of a runtime environment:
Static area allocated at load/startup time. Examples: global/static variables and load-time constants. Stack area for execution-time data that obeys a last-in first-out lifetime rule. Examples: nested declarations and temporaries. Heap or dynamically allocated area for "fully dynamic" data, i.e. data that does not obey a LIFO rule. Examples: objects in Java, lists in Scheme. Chapter 8 K. Louden, Programming Languages

3 Procedures/functions and the environment
Local data in a procedure/function is typically allocated in a contiguous area of the environment, called an activation record or frame ("activation" means "used during a call"): © 2003 Brooks/Cole - Thomson Learning™ Chapter 8 K. Louden, Programming Languages

4 Languages and Environments
Languages differ on where activation records must go in the environment: Fortran is static: all data, including activation records, are statically allocated. (Each function has only one activation record—no recursion!) Functional languages (Scheme,ML) and some OO languages (Smalltalk) are heap-oriented: all (or almost all) data, including activation records, are allocated dynamically. Most languages are in between: data can go anywhere (depending on its properties); activation records go on the stack. Chapter 8 K. Louden, Programming Languages

5 Simple stack-based allocation
Described in Chapter 5. Nested declarations are added to the stack as their code blocks are entered, and removed as their code blocks are exited. Example: Stack at Point 1: { int x; int y; { int z; } { int w; // Point 1 } } x y w Chapter 8 K. Louden, Programming Languages

6 Activation records and the stack
New piece Frames can be kept on the stack (C, Java) if access to the local environment (frame) ends with the call. To facilitate popping of frames, a control link is added to the activation record that records the position of the calling activation, so a pop can be done in one operation. © 2003 Brooks/Cole - Thomson Learning™ A pointer to the current frame is also kept, called the environment pointer, or ep (could be the stack pointer). Chapter 8 K. Louden, Programming Languages

7 K. Louden, Programming Languages
Example (C): int x; void p( int y) { int i = x; char c; ... } void q ( int a) { int x; p(1); main() { q(2); return 0; Chapter 8 K. Louden, Programming Languages

8 Environment stack during exec of p:
© 2003 Brooks/Cole - Thomson Learning™ Note: the ep in this picture actually points to the "bottom" of the frame, as do the control links (which are stored old ep values), so ep  top of stack. Chapter 8 K. Louden, Programming Languages

9 Local variable access using the ep
In a typical language with a stack-based runtime environment, the local declarations in a procedure are fixed at compile-time, both in size and in sequence. This information can be used to speed up accesses to local variables, by precomputing these locations as offsets from the ep. Then the local frame need not have a name-based lookup operation (unlike the symbol table). In fact, names can be dispensed with altogether. The next slide shows how that would look for the procedure p of slide 7. Chapter 8 K. Louden, Programming Languages

10 Activation record of p:
© 2003 Brooks/Cole - Thomson Learning™ Chapter 8 K. Louden, Programming Languages

11 Non-local variable access
Requires that the environment be able to identify frames representing enclosing scopes. Using the control link results in dynamic scope (and also kills the fixed-offset property). If procedures can't be nested (C, C++, Java), the enclosing scope is always locatable by other means: it is either global/static (accessed directly) or belongs to the current object (accessed through the this pointer). If procedures can be nested (Ada, Pascal), to maintain lexical scope a new link must be added to each frame: the access link, pointing to the activation of the defining environment of each procedure. Chapter 8 K. Louden, Programming Languages

12 K. Louden, Programming Languages
Example (Ada): procedure q is x: integer; procedure p (y: integer) is i: integer := x; begin ... end p; procedure r is x: float; begin p(1);... end r; r; end q; Chapter 8 K. Louden, Programming Languages

13 Environment during exec of p:
© 2003 Brooks/Cole - Thomson Learning™ Chapter 8 K. Louden, Programming Languages

14 Procedure values as pointer pairs
With nested procedures in lexically scoped languages requiring an access link, when a procedure is called, this access link must be available at the point of call. One way it can be is for the procedure itself to record its access link (necessary if procedures can be parameters). Then each procedure becomes a pair of pointers: a code pointer (called the instruction pointer or ip in the text), and an environment pointer (ep in the text) pointing to the definition environment of the procedure (which will become the access link during a call). Such an <ep,ip> pair is sometimes called a closure. Chapter 8 K. Louden, Programming Languages

15 K. Louden, Programming Languages
Example (Ada): procedure lastex is procedure p(n: integer) is procedure show is begin if n > 0 then p(n - 1); end if; put(n); new_line; end show; begin -- p show; end p; begin -- lastex p(1); end lastex; Chapter 8 K. Louden, Programming Languages

16 Environment during 2nd call to show:
Note 2nd show has a different access link Chapter 8 K. Louden, Programming Languages

17 Fully dynamic environments
Languages with lambdas or where functions can be created locally and returned from a call (ML, Scheme). Activation records cannot in general be popped after a call. Thus, activations no longer behave as LIFO structures and must be allocated in the heap. Control links make little sense, since each control link might have to point far back in the stack. Access links and closures still make perfect sense, however. Chapter 8 K. Louden, Programming Languages

18 Example (Scheme–simplified from Chapter 11, see also page 338):
(define (makeNewBalance balance) (define (withdraw amt) (set! balance (- balance amt)) balance) withdraw) (define withdraw1 (makeNewBalance 500)) (define withdraw2 (makeNewBalance 100)) Chapter 8 K. Louden, Programming Languages

19 Scheme example, continued
Environment at end of previous code (see also page 339): Chapter 8 K. Louden, Programming Languages

20 Scheme example, concluded
Environment just before exiting the call (withdraw1 200): Chapter 8 K. Louden, Programming Languages


Download ppt "Chapter 8 - Control II: Procedures and Environments"

Similar presentations


Ads by Google