Languages with Static Local Variables - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Languages with Static Local Variables

Description:

For Stack-Dynamic Local Variables, need a new activation ... Name is the class name, preceded by a tilde (~) 26. An Example in C . class stack { private: ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 33
Provided by: david2550
Category:

less

Transcript and Presenter's Notes

Title: Languages with Static Local Variables


1
Languages with Static Local Variables
  • For static variable languages, there is only one
    activation record instance per subroutine.
  • Can be organized at compile time and allocation
    at load time.

Procedure A(int x, real y) ... end Procedure
B(int a) real z 5.0 A(a, 5.0) end Main int n
6 B(n) end
Main
Local variables
Local variables
Parameters
A
Data
Return address
Local variables
B
Parameters
Return address
Main

A
Code
B

2
Stack-Dynamic Local Variables
  • For Stack-Dynamic Local Variables, need a new
    activation record instance for each time a
    subroutine is called.
  • Allows recursion
  • Use run-time stack

Procedure A(int x, real y) ... end Procedure
B(int a) real z 5.0 A(a, 5.0) end Main int n
6 B(n) end
A
B
B
B
Main
Main
Main
Main
Main
Call Sequence
3
Simple Example
int n 6 Procedure A(int x, real
y) ... end Procedure B(int a) real z
5.0 A(a, 5.0) end Main B(n) end
4
Example Pascal Program
  • program MAIN_2
  • var X integer
  • procedure BIGSUB
  • var A, B, C integer
  • procedure SUB1
  • var A, D integer
  • begin SUB1
  • A B C lt-----------------------1
  • end SUB1
  • procedure SUB2(X integer)
  • var B, E integer
  • procedure SUB3
  • var C, E integer
  • begin SUB3
  • SUB1
  • E B A lt--------------------2
  • end SUB3
  • begin SUB2
  • SUB3
  • Call sequence for MAIN_2
  • MAIN_2 calls BIGSUB
  • BIGSUB calls SUB2
  • SUB2 calls SUB3
  • SUB3 calls SUB1

5
Stack Contents at Position 1
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
Parameters that are Subprogram Names Referencing
Environment
  • Shallow binding
  • The environment of the call statement that enacts
    the passed subprogram
  • Deep binding
  • The environment of the definition of the passed
    subprogram
  • Ad hoc binding
  • The environment of the call statement that passed
    the subprogram

10
Subprogram as Parameters
  • What is the correct referencing environment for a
    subprogram that was sent as a parameter?
  • a. That of the subprogram that enacted it
  • Shallow binding
  • b. That of the subprogram that declared it
  • Deep binding

11
Passing Subprograms as Parameters
  • Example sub1
  • sub2
  • sub3
  • call sub4(sub2)
  • sub4(subx)
  • call subx
  • call sub3
  • Shallow binding gt sub2, sub4, sub3, sub1
  • Deep binding gt sub2, sub1

What is the referencing environment of sub2
when it is called in sub4?
12
Reference environment
13
Parameters are Subprogram Names
  • For static-scoped languages, deep binding is most
    natural
  • For dynamic-scoped languages, shallow binding is
    most natural
  • It is that of the subprogram that passed it
  • Ad hoc binding (Has never been used)

14
Deep Access
15
(No Transcript)
16
The Concept of Abstraction
  • An abstraction is a view or representation of an
    entity that includes only the most significant
    attributes
  • The concept of abstraction is fundamental in
    programming (and computer science)
  • Nearly all programming languages support process
    abstraction with subprograms

17
Introduction to Data Abstraction
  • An abstract data type is a user-defined data type
    that satisfies the following two conditions
  • The representation of, and operations on, objects
    of the type are defined in a single syntactic
    unit
  • The representation of objects of the type is
    hidden from the program units that use these
    objects, so the only operations possible are
    those provided in the type's definition

18
Encapsulation
  • When code size grows larger (more than thousand
    lines)
  • code organization
  • compilation time
  • Encapsulation
  • a grouping of subprograms and the data they
    manipulate
  • FORTRAN C files containing one or more
    subprograms can be independently compiled

19
Encapsulation in C
  • Files containing one or more subprograms can be
    independently compiled
  • The interface is placed in a header file
  • Problem the linker does not check types between
    a header and associated implementation
  • include preprocessor specification

20
Naming Encapsulations
  • Large programs define many global names need a
    way to divide into logical groupings
  • A naming encapsulation is used to create a new
    scope for names
  • C Namespaces
  • Can place each library in its own namespace and
    qualify names used outside with the namespace
  • C also includes namespaces

21
Abstract data type
  • Abstract Data Type an encapsulation that
    includes only the data representation and the
    subprograms that provides operations for that
    type.
  • reliability user cannot directly access objects
  • readability without implementation details
  • Example float-point types in C
  • actual format is hidden from the user
  • operations on the type is defined in PL itself

22
Abstract data type example
23
C Class
  • data members data defined in a class
  • stack_ptr, max_len, top_ptr
  • member functions functions defined in a class
  • also called access interfaces
  • push( ), pop( ), top( ), empty( )
  • inline vs. call-return linkage
  • private entities (data or member functions) that
    are to be hidden outside the class
  • public entities (data or member functions) that
    are visible outside the class

24
Language Examples C (continued)
  • Constructors
  • Functions to initialize the data members of
    instances (they do not create the objects)
  • May also allocate storage if part of the object
    is heap-dynamic
  • Can include parameters to provide
    parameterization of the objects
  • Implicitly called when an instance is created
  • Can be explicitly called
  • Name is the same as the class name

25
Language Examples C (continued)
  • Destructors
  • Functions to cleanup after an instance is
    destroyed usually just to reclaim heap storage
  • Implicitly called when the objects lifetime ends
  • Can be explicitly called
  • Name is the class name, preceded by a tilde ()

26
An Example in C
  • class stack
  • private
  • int stackPtr, maxLen, topPtr
  • public
  • stack() // a constructor
  • stackPtr new int 100
  • maxLen 99
  • topPtr -1
  • stack () delete stackPtr
  • void push (int num)
  • void pop ()
  • int top ()
  • int empty ()

27
Examples in Java
28
Concurrency
  • Concurrency units execute simultaneously
  • Physical separate CPU
  • Logical same CPU with time-sliced thread
    execution
  • MIMD (focused) vs. SIMD architecture
  • MIMD multiple-instruction multiple-data shared
    memory or distributed memory
  • SIMD single-instruction multiple data vector
    processors

29
SIMD Architecture
30
DM MIMD Architecture
31
SM MIMD Architecture
32
Thread Execution
  • A process can have multiple threads executing
    simultaneously
  • Process one address space
  • All threads share the same address space
  • All global variables are shared by all threads
  • A thread has its own register values stack
    Independent execution status (PC, call stacks,
    etc)
  • independent stack-dynamic variables
  • Thread is the scheduling unit, not process
  • Thread synchronization for shared data
Write a Comment
User Comments (0)
About PowerShow.com