Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

Programming Languages and Paradigms
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
ISBN Chapter 10 Implementing Subprograms.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
The Procedure Abstraction Part IV: Run-time Structures for OOLs Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Run time vs. Compile time
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
The Procedure Abstraction, Part IV Addressability Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Subprograms - implementation
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Design issues for Object-Oriented Languages
Advanced Programming in C
Chapter 14 Functions.
Run-Time Environments Chapter 7
Name Spaces: ALL versus OOL
Functions.
Names and Attributes Names are a key programming language feature
Abstract Data Types and Encapsulation Concepts
COMPILERS Activation Records
Implementing Subprograms
Chapter 14 Functions.
Abstract Data Types and Encapsulation Concepts
The Procedure Abstraction Part V: Run-time Structures for OOLs
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scope, Function Calls and Storage Management
Where is all the knowledge we lost with information? T. S. Eliot
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 14 Functions.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records, Object Oriented Languages, Parameter Passing, Returning Values.

Cse321, Programming Languages and Compilers 2 6/12/2015 Notices Reading Assignment –Read Chapter 6 “The Procedure Abstraction” –Pages 251 – 306 The Final Exam will be Monday, March 19, –Monday, Mar. 19, Time: 1930 – 2120 (7:30pm – 9:20pm). –It will NOT start at about 6:00 pm like our regular class meeting. –I have no control over this. – Project 3 is due Monday, March 19. I will accept projects until midnight. I must grade exams and projects and get all grades in before I leave on Thursday. So late projects will not be accepted.

Cse321, Programming Languages and Compilers 3 6/12/2015 Roles of Procedures Control Abstraction –Call and Return –Depends upon call;ing conventions »All parties must agree »Supports separate compilation Name Space Management –Separate private name space for each procedure –Allocation of data is automatic External Interface –Name scoping –Addressability –Libraries –Interface with operating system

Cse321, Programming Languages and Compilers 4 6/12/2015 Purpose of Procedures Create Name spaces and variables Maps variables onto virtual addresses –( The operating system maps virtual addresses to physical addresses ) Establish rules for visibility Break large systems into smaller manageable pieces –(Linkers and loaders compose them together) Lays out memory

Cse321, Programming Languages and Compilers 5 6/12/2015 Control Abstraction Procedures are a common form of control abstraction –Call – return –The control flow of a program can be described by a graph of what procedures can call what other procedures. –A procedure may never return »Non-termination »Using some other control abstraction to escape Other forms of control abstractions –Goto –Break –Co-routines –Exceptions –Continuations

Cse321, Programming Languages and Compilers 6 6/12/2015 Name Spaces Name space supports a set of names and objects bound to those names –Variables –Types –Labels A name space has scope –The region within the program text where the names in a name space are visible –Outside of the scope the name and its object are unreachable Some languages have several independent name spaces, with incommensurate scopes –In ML values (functions and variables) and Types live in independent name spaces –In Fortran labels and variables live in independent name spaces

Cse321, Programming Languages and Compilers 7 6/12/2015 Nested Lexical Scopes Name spaces can be nested. Names in an enclosing scope are visible in the inner scope. –A name refers to its lexically closest declaration –The most recent enclosing scope Many languages support some sort of nested scopes. Names in lexically scoped languages can be uniquely determined by a 2 place coordinate. –(scope, position within that scope)

Cse321, Programming Languages and Compilers 8 6/12/2015 Scoping Rules Every Language has its own scoping rules Fortran – 2 levels global & local Scheme – 1 global, nested lets ML – structures (libraries) unbounded nested scopes (functions, let, anonymous functions) C – Global, file limited, local procedural, nested blocks Java – Global classes, packages, local method scope

Cse321, Programming Languages and Compilers 9 6/12/2015 Fortran Global scope Foo variables parameters labels Common Block data Bar variables parameters labels

Cse321, Programming Languages and Compilers 10 6/12/2015 C Global scope int a, b File scope static int x,y int Bar() variables parameters labels File scope Block int Foo() variables int Foo() block

Cse321, Programming Languages and Compilers 11 6/12/2015 Scheme map foo cond head cons let

Cse321, Programming Languages and Compilers 12 6/12/2015 ML Struct a = end fun foo datatype fun bar fun deep let fun baz (fn x => (fn y =>

Cse321, Programming Languages and Compilers 13 6/12/2015 Java Public classes Package A Class M static int x Package B Method Parameters Local vars class N Package C

Cse321, Programming Languages and Compilers 14 6/12/2015 Dynamic Scoping fun map f x = if null x then [ ] else (f (hd x))::(map f (tl x)) fun add x = (fn y => y + x) map (add 5) [0,0] = map (fn y => y + x) [0,0] = where x = 5 map (fn y => y + x) [0,0] = if null x where x = [0,0], then [ ] f = (fn y => y + x) else (f (hd x))::(map f (tl x)) ((fn y => y + x) (hd x))::(map f (tl x))

Cse321, Programming Languages and Compilers 15 6/12/2015 Activation Records Created every time a procedure is called Must be accessible to both the caller and the callee Allocates space for –Parameters –Local variables –Return address –Other links and pointers to provide access to non-local data Other issues –Initializing local variables –Stack vs. heap allocated –Optimizing activation records by coalescing

Cse321, Programming Languages and Compilers 16 6/12/2015 Creating Activation Records AR creation is a cooperative effort Shared by the caller and the callee Has 4 parts –Precall –Postreturn –Prolog –Epilog Precall & Postreturn can be split prolog precall postcall epilog prolog epilog Call Return

Cse321, Programming Languages and Compilers 17 6/12/2015 Object Oriented Languages Control oriented around the data, not the procedures Name spaces are fundamentally different –Procedural languages are almost always lexically scoped »Names linked to position in source of the text executing –Object-oriented languages are object based »Names are linked to dynamic objects »The scope rules are linked to the class of the current object, not the lexical position of the code executing. »subtyping & inheritance make the type of the “current object” impossible to determine in general Inheritance –Imposes an hierarchy on the structure of data »Classes, superclasses, subclasses –Classes definition is the mechanism to create the hierarchy

Cse321, Programming Languages and Compilers 18 6/12/2015 Terminology Instance –An object Object Record –Concrete representation – contains its members (or pointers) Instance Variable –A variable local to a single object Method –Code associated with an object –Full fledged procedure (parameters, local variables, return values) Receiver –Methods are always invoked relative to some object. The receiver »When activation of the method begins the receiver becomes the current object. Class –An object to describe the structure of other objects Class Variable –A variable with only one instance per class. Shared amongst all instances

Cse321, Programming Languages and Compilers 19 6/12/2015 Mapping Names to Methods Inheritance allows methods to be over-ridden Thus more than one set of executable instructions can have the same name. Method disambiguation follows the class hierarchy Start with the receiver (or current object) –If it has a method with the correct name, then use it –If not look for a method with that name further up the hierarchy in the super class of the receiver Optimization. –All members of a class share the same methods –Methods are usually implemented via indirection (pointers)

Cse321, Programming Languages and Compilers 20 6/12/2015 Example class three int n int fee(int n) bool fum() class two extends three float x float y int fee(int n) float foe() class one extends two one z int fee(int n) float fie()

Cse321, Programming Languages and Compilers 21 6/12/2015 class three fee fum class two fee foe class one fee fie Object  Obj a X=2.0 Y=0.1 Z = N =1 Obj c X=5.0 Y=3.1 N =1 Obj b X=5.0 Y=3.0 Z = N =1

Cse321, Programming Languages and Compilers 22 6/12/2015 class three fee fum Object  class two fee fum foe class one fee fum foe fie fum … fee … fee … foe … fee … fie … Obj c X=5.0 Y=3.1 N =1 Obj a X=2.0 Y=0.1 Z = N =1 Obj b X=5.0 Y=3.0 Z = N =1

Cse321, Programming Languages and Compilers 23 6/12/2015 Communicating between procedures Common global variables –Simple, but prone to abuse –Recursion becomes problematic Passing parameters –Call by value –Call by reference –Call by copy return –Call by name Returning values

Cse321, Programming Languages and Compilers 24 6/12/2015 Addressability Base addresses (variables at a constant address) Indirection (pointers) Stack based –Push – pop Variables in activation records –Parameters –Local variables –Local variables of other procedures in enclosing scopes –Global variables Access links –Static links –Display

Cse321, Programming Languages and Compilers 25 6/12/2015 Static links fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin g(b) end Ap register x Ret addr Static link y f dyn link z Ret addr Static link a g dyn link a Ret addr Static link i b c h dyn link

Cse321, Programming Languages and Compilers 26 6/12/2015 Display inside f fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin g(b) end x Ret addr Static link y f Old display  

Cse321, Programming Languages and Compilers 27 6/12/2015 Display inside g fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin g(b) end x Ret addr Static link y f Old display z Ret addr Static link a g display 

Cse321, Programming Languages and Compilers 28 6/12/2015 Display inside h called from g fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin g(b) end x Ret addr Static link y f Old display z Ret addr Static link a g display  a Ret addr Static link i b c h display

Cse321, Programming Languages and Compilers 29 6/12/2015 Costs Costs to access a variable –Static link –Display Cost to maintain the structure –Static link –Display Cache Costs

Cse321, Programming Languages and Compilers 30 6/12/2015 Next-time Intro to what we’ll cover next semester Summary of concepts possible on the final exam Help-session for project 3.