4 scope - CS 242 2010 Announcements Midtermexam...

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

View Full Document Right Arrow Icon
1 Scope, Function Calls and Storage Management CS 242 2010 John Mitchell Reading: Chapter 7, Concepts in Programming Languages Announcements Midterm exam Wed 10/21, 7 9PM, Room TBA Local SCPD students are required to come to campus Closed book, one page of notes allowed Homework Homework 4 will be posted on Wednesday, Oct 14 One graded problem due by class time on Monday, Oct 19 Remaining problems will not be graded We will hand out solutions on Monday Oct 19 See web site for calendar Topics Block structured languages and stack storage In line Blocks activation records storage for local, global variables First order functions parameter passing tail recursion and iteration Higher order functions deviations from stack discipline language expressiveness => implementation complexity Block Structured Languages Nested blocks, local variables Example { int x = 2; { in y 3 new variables declared in nested blocks inner outer { int y = 3; x = y+2; } } Storage management Enter block: allocate space for variables Ei bl k ll bdl l d inner block block local variable global variable Examples Blocks in common languages C, JavaScript * { … } Algol begin … end ML let … in … end Two forms of block Two forms of blocks In line blocks Blocks associated with functions or procedures Topic: block based memory management, access to local variables, parameters, global variables * JavaScript functions provide blocks Simplified Machine Model Registers Data Code Stack Environment Pointer Program Counter Heap
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Interested in Memory Mgmt Only Registers, Code segment, Program counter Ignore registers Details of instruction set will not matter Data Segmen Data Segment Stack contains data related to block entry/exit Heap contains data of varying lifetime Environment pointer points to current stack position Block entry: add new activation record to stack Block exit: remove most recent activation record Some basic concepts Scope Region of program text where declaration is visible Lifetime Period of time when location is allocated to program Inner declaration of x hides outer one. Called “hole in scope” Lifetime of outer x includes time when inner block is executed Lifetime scope Lines indicate “contour model” of scope. { int x = … ; { int y = … ; { int x = … ; …. }; }; }; In line Blocks Activation record Data structure stored on run time stack Contains space for local variables Example 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 Activation record for in line block Control link pointer to previous record on stack Push record on stack: Control link Local variables Intermediate results
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2011 for the course CS 242 at Stanford.

Page1 / 8

4 scope - CS 242 2010 Announcements Midtermexam...

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

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