Scope Functions, - Scope Functions and Storage Management Implementing Functions and Blocks cs3723 1 Topics Block-structured languages and stack

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
cs3723 1 Scope, Functions, and Storage Management Implementing Functions and Blocks
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs3723 2 Topics Block-structured languages and stack storage In-line Blocks Storage for local, global variables First-order functions Implementing function call and return Passing parameters and returning result Higher-order functions deviations from stack discipline language expressiveness => implementation complexity Additional issues parameter passing tail recursion and iteration
Background image of page 2
cs3723 3 Blocks in C/C++ Blocks: regions of code that introduces new variables Blocks are nested but not partially overlapped What about jumping into the middle of a block? Local variable: created by the current block Global variable: created by surrounding blocks Storage management Enter block: allocate space for variables Exits block: some or all space may be deallocated inner block outer block { int x = 2; { int y = 3; x = y +2; } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs3723 4 Blocks in Functional languages ML: let fun g(y) = y + 3 in let fun h(z) = g(g(z)) in h(3) end end; Lisp: ( (lambda (g) ((lambda (h) (h 3)) (lambda (z) (g (g z)))) (lambda (y) (+ y 3)))
Background image of page 4
cs3723 5 Summary of Blocks Blocks in common languages C { … } Algol begin … end ML let … in … end Two forms of blocks In-line blocks Blocks associated with functions or procedures Topic: block-based memory management Access to local variables, global variables, and function parameters
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs3723 6 Managing Data Storage In a Block Local variables Declared inside the current block Enter block: allocate space Exit block: de-allocate space Global variables Declared in a enclosing block Already allocated before entering current Block Remain allocated after exiting current block Function parameters Input parameters Allocated and initialized before entering function body De-allocated after exiting function body Return parameters Address remembered before entering function body Value set after exiting function body
Background image of page 6
cs3723 7 Scoping rules Finding non-local (global) variables Global and local variables Static scope Find global declarations in the closest enclosing blocks in program text Dynamic scope Find global variables in the most recently entered blocks { int x=0; fun g(z) = x+z; fun h(z) = let x = 1 in g(z) end; h(3) }; x 0 x 1 z 3 z 3 outer block h(3) g(12)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs3723 8 Managing Blocks Activation record: data structure allocated on runtime stack Contains space for local variables in the block determined as compile time Contains values of local variables Evaluated at runtime Before evaluating each block, push it’s activation record onto a runtime stack; after exit the block, pop activation record off stack May need space for variables and intermediate results like (x+y), (x-y ) { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; }; Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block
Background image of page 8
cs3723 9 Simplified Machine Model Registers Environment Pointer Program Counter Data Code Heap Stack
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/13/2009 for the course CS 3773 taught by Professor Staff during the Spring '08 term at The University of Texas at San Antonio- San Antonio.

Page1 / 30

Scope Functions, - Scope Functions and Storage Management Implementing Functions and Blocks cs3723 1 Topics Block-structured languages and stack

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online