Title: Chap 7: Runtime Organization
1Chap 7 Runtime Organization
Prof. Steven A. Demurjian Computer Science
Engineering Department The University of
Connecticut 371 Fairfield Way, Unit 2155 Storrs,
CT 06269-3155
steve_at_engr.uconn.edu http//www.engr.uconn.edu/st
eve (860) 486 - 4818
Material for course thanks to Laurent
Michel Aggelos Kiayias Robert LeBarre
2Overview
- Runtime Environment Focuses on the wayt hat a
Program is Managed/Controlled During its
Execution - Well Focus on
- Basic Definitions and Concepts
- A First Look at Activation Records
- Memory Allocation Strategies
- Revisiting Activation Records
- Impact of Parameter Passing Mechanisms
- Data Layout and Scoping of Declarations
- Memory Manager and Compilation Process
- Concluding Remarks/Looking Ahead
3Basic Definitions and Concepts
- Procedure Definition
- Declaration that has a Name and has a Body
- If Returns a value, then Function
- Procedure Definition Contains a Sequence of
Identifiers Called the Formal Parameters - Procedure Call Contains a List of Arguments
passed to the Procedure or the Actual Parameters - Information in Program can be Characterized
- Environment Maps Name to Storage Loc (l-value)
- Store Maps Location to Value it Contains
(l-value to an r-value)
Environment
State
Compile Time
Run Time
Name Storage Value
4Program Organization
- Purpose
- Define what constitutes a Program
- Review overall OS Structure
- Examine Process Structure
- Present and Understand Memory Management issues
- Remember, a Compiler Must
- Target a Specific Operating System
- Interact within that Operating System
- Provide Access at Machine Level to Operating
System Components
5The Basic Abstractions
- Processes
- Unit of Computation
- Program/Application Composed of One or More
Interacting Processes - Processes Interact with and Utilize Resources
- Files
- Unit of Information Storage
- Classic, Default Utilized to Support Programs
- Read, Write, and Transfer via Files
- Other Resources
- CPU, Printer, Memory, etc.
- Network, Display, etc.
6Program, Processes Resources
Resources
Processes
Operating System
Data
CPU
Device
Device
Program
Device
Memory
Device
Hardware
7What are Executables?
- Executable
- File Containing Machine Code
- Examples
- A bash script, a perl script, a compiled java
program, a compiled C program, ... - Java Bytecode and JVM
8Java Virtual Machine (JVM)
- JVM is a Platform Specific Program which
Interprets and Executes Java Code - JVM Interprets and Executes Bytecodes
- JVM Targeted as Small/Efficient - Embeddable
within Consumer Electronics - JVM Stack Based Machine - Simulates Real Processor
CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01
08 00 20 08
9Java Visualization
10What are Executables?
- Native Executable
- File Containing Machine Code that the CPU
understands without any intervening layers of
abstractions - Examples
- A compiled C program
- A Java program compiled natively with Gnu
Compiler for Java
11From Code to Loaded Program
ORIGINAL SOURCE static int gVar . . . int
proc_a (int arg) . . . gVar 7
put_record(gVar) . . .
RELOCATABLE OBJECT MODULE 0000 . . . . .
. 0008 entry proc_a . . . 0036 Space for gVar .
. . 0220 load 7, R1 0224 store R1,
0036 0228 push 0036 0232 call put_record . .
. 0400 External Reference Table 0404 put_record
0232 . . . 0500 External Definition
Table 0540 proc_a 0008 0600 (optional symbol
table) 0799 (last location in module)
12From Code to Loaded Program
THE ABSOLUTE PROGRAM 0000 (Other Modules) . .
. 1008 entry proc_a . . . 1036 Space for gVar .
. . 1220 load 7, R1 1224 store R1,
1036 1228 push 1036 1232 call 2334 1399 (end of
proc_a) . . . (Other Modules) 2334 entry
put_record . . . 2670 (opt. symbol
table) 2999 (last loc. in module)
PROGRAM AT LOCATION 4000 0000 (Other
Processes) 4000 (Other Modules) . .
. 5008 entry proc_a . . . 5036 Space for gVar .
. . 5220 load 7, R1 5224 store R1,
5036 5228 push 5036 5232 call 6334 5399 (end of
proc_a) . . . (Other Modules) 6334 entry
put_record . . . 6670 (opt. symbol
table) 6999 (last loc. in module) 7000 (Other
Processes)
13What is a Process?
- An Executable with its Execution Context and
State - Consists of
- Executable (.exe, a.out, etc.)
- Loaded into main memory
- All of the resources currently committed
- With the value of all the CPU registers
- Computers can Run Multiple Processes at Same Time
14What is a Process?
- Sequential Program for Execution
- Object Program (a.out) to Execute
- Data on Which Program will Execute
- Resources Required During Execution
- Status of the Process Execution (ps -aux)
- Program is Static Source Code/Executable
Statements which Lacks Data Set - Process is Dynamic Entity Executing on Actual
Data Set - For Example, Multiple Unix Users Can Execute
emacs, Latex, vi, grep, at same Time - Shared Program Executable emacs
- Dedicated Process for Each User of emacs
15A Process
Abstract Machine Environment (OS)
16UNIX Process
Resources
Data
Text
Stack
Process Status
File
UNIX kernel
17More on UNIX Processes
- Each Process Has its Own Address Space
- Subdivided into Text, Data, Stack Segment
- a.Out File Describes the Address Space
- OS Creates Descriptor to Manage Process
- Process Identifier (PID) is Integer User Handle
for the Process (Descriptor) - Try ps and Ps aux (Read Man Page)
USER PID CPU MEM SZ RSS TT S
START TIME COMMAND wangz 25150 26.1
15.33048019308 pts/16 O Aug 05 631826
/apps/matlab/bin/s manish 7590 25.1 0.821436
964 ? O Jul 22 2462834
/apps/matlab/bin/s marios 29536 0.6 3.5 5156
4376 pts/6 S 132223 024 mailtool steve
29893 0.2 0.8 1080 980 pts/18 S 174111
000 -csh root 29905 0.2 0.7 1040 888
pts/18 O 174153 000 ps -aux
18Multiprogramming
- Load and Execute Multiple Programs into
Space-Multiplexed Mem. while Time-Multiplexing
CPU - Technique for Sharing the CPU Among Runnable
Processes - Process May Be Blocked on I/O
- What is Deadlock and How can it Occur?
- Process May Be Blocked Waiting for Other
Resources to Complete - What is Starvation and How is it Prevented?
- While One Process is Blocked, Another Should Be
Able to Run - Multiprogramming OS Accomplishes CPU Sharing
Automatically
19How Multiprogramming Works
Process 1
Process 2
CPU
Space-Multiplexed Memory
Process 3
Time-Multiplexed
Process 4
20Batch Processing
- Uses Multiprogramming
- Job (File of OS Commands) Prepared Offline
- Batch of Jobs Given to OS at One Time
- OS Processes Jobs One-After-the-Other
- No Human-Computer Interaction
- OS Optimizes Resource Utilization
- Batch Processing (as an Option) Still Used Today
- Batch Files in DOS/Unix to Repetitively Execute
Same Set of Commands - ASIDE Using Batch Processing
- 1978/79 First Two Computer Courses at BC
- Keypunch IBM Cards
- Run Batch Job Return 1-2hrs Later for Output
21Timesharing
- Uses Multiprogramming
- Support Interactive Computing Model (Illusion of
Multiple Consoles) - Different Scheduling Memory Allocation
Strategies Than Batch - Tends to Propagate Processes
- Considerable Attention to Resource Isolation
(Security Protection) - Tend to Optimize Response Time
22How Timesharing Works
- Timesharing Still Pervasive in Computing
- Engineering Network Still Time-Shared in Some
Respects - Access to User Files
- Email/Web Server
- Download Software
- Terminals Replaced by Unix Workstations and PCs
- Example of Network File System/OS
23Personal Computers
- CPU Sharing Among One Persons Processes
- Ability of Only One Active Process at One Time
- Early Apple/Mac Only do 1 Action at Time
- Same for Win 3.1 and Win 95
- Improved with Win 98 and NT
- Todays Apple and Windows MS Multi-Process!
- Increased Power of Computing for Personal Tasks
- Graphics - Two-D, Three-D, Real-Time
- Multimedia - CDs and DVDs
24Process Control Real-Time
- Computer is Dedicated to a Single Purpose
- Classic Embedded System
- Must Respond to External Stimuli in Fixed Time
- Continuous Media Popularizing Real-time
Techniques - An Area of Growing Interest
- Embedded Computing Estimated as one of Key
Technologies for 21st Century - Pervasiveness Consumer Products/Electronics
- Cars, Refrigerators, Phones, Microwaves, etc.
- Elevators, Smart Products and Buildings, etc.
- Embedded Java
- Microsoft CE
25Evolution of Modern OS
Timesharing
Network OS
PC Wkstation
Memory Mgmt
Client-Server Model
Scheduling
Protection
System Software
Protocols
Batch
Human- Computer Interface
Real-Time
Memory Mgmt
Protection
Scheduling
Scheduling
Files
Devices
Modern OS
Java/JINI/JavaOS
26Virtual Address Space
- An Abstraction provided by the Operating System
- Essentially
- A sandbox for the process
- Practically
- A Contiguous Memory area that
- Starts at offset 0
- Ends at offset 232-1
- Entirely devoted to a single process
- Is transparently managed by the O.S.
- Has a layout imposed by the O.S.
27Virtual Memory
- Components of Program (Identifiers, Labels,
variables, etc.) Define Name Space - Language Translation
- Converts Name Space Elements to Virtual Addresses
- Binds Symbolic Names to Virtual Addresses
- Virtual Memory System
- Dynamically Binds Virtual Addresses to Physical
Addresses - Defers Binding Decision to During Program
Execution - Perform Binding As Needed
28Names, Virtual Addresses, Physical Addresses
- Distinguish Between
- Symbolic Names vs. Virtual Addrs. vs.
Physical Addrs.
Source Program
Absolute Module
Executable Image
Name Space
Virtual Address Space
Physical Address Space
Compile and Link Tools
Yt Virtual Address Space ? Physical Address
Space ? ?
29Virtual Address Space
- Traditional Organization
- Code Area at the bottom
- Static Data above
- Constants
- Static strings
- Static variables
- Heap
- Grows upward
- Stack
- Grows downward
- Lots of free VM in between
0xffffffff
0x0
30OS Conventions
- Layout is dictated by the OS
- Specifically by the Loader
- Loader
- The OS module responsible for
- Allocating a virtual address space
- Bringing the content of the executable file into
the address space - Bringing any dependent shared library into the
address space - Performing any required relocation
- Transferring control to the entry point of the
program.
31Zooming In.
- Close look on the code area
32Loading
- Loader extracts the information from the
executable - Executable holds a header with
- Offset and size of code (blue area)
- Offset of entry point (main) within blue box
- Offset and size of constant data (green area)
- List of needed shared library (optional)
Program Executable
33Loading and Execution
- Loader
- Grabs file zones and move them in virtual memory
- Relocate the code as necessary
- Sets up the execution stack
- Transfer control to the entry point with a jump
- Execution Stack
- A memory area at the top of the VM
- Grows downward
- Grows on demand (with OS collaboration)
- Purpose
- Automatic storage for local variables
34A First Look at Activation Records
- Storage Organization for Program Execution
- Code Referenced by PC
- Global/Local Variables
- Static Data Area
- Stack Contains
- Set of Activation Records
- All Active Procedures and Functions
- Heap for Dynamic Memory Allocation
Code
Static Data
Stack
Heap
35A General Activation Record
- To the Calling Procedure
- Passed in to Procedure
- To Act. Record of Caller
- Referenced Non-Local Data
- Needed to Restart Caller
- Local Variables for Scope
- Compiler Generated
36An Activation Record in C
- Actual Parameters Supplied by Caller
- Needed to Restart Caller
- Local Variables for Scope
- If Callee Calls Another Procedure/Function
Etc. Incoming Param 2 Incoming Param 1
Saved State Info
Local Variables
Temporary Storage
Outgoing Parameters
37Activation Records
- Procedure Activation Represents the Actions that
Must Occur when a Caller Invokes a Callee - Transfer of Actuals into Formals by the
Languages Parameter Passing Mechanism - Modification of Environment and State by Alloc of
Memory for Variables that are Local to Callee - Identification of the Control Return Point of
Caller After Callee Complets - Every Procedure Activation has Lifetime which is
the Sequence of Steps (Code) of Procedure Body
of Callee
38What are Possible Activations?
- Nested
- A calls B calls C
- Non-Overlapping
- A calls B
- B calls C
- Recursive
- A calls itself
- Concurrent
- A calls B (spawns process)
- A calls C (spawns process)
- B, C execute in parallel and compete for
Resources
39Activation Tree
- Graphical Representation of Activations over Time
40How is Tree Interpreted?
- Each Node is a Specific Procedure Call
- Root Node is Start of Program
- Parent Node
- Flow from Parent to Child
- Caller to Callee
- Sibling Node
- All Nodes to Left have Completed
- Parent Comples when All Children Complete
- Utilize Control Stack to Represent Current State
of an Activation - s, q(1,9), q(1,3), q(2,3)
- Where is this on Prior Slide?
- Represents state at Point in Time
41Relationship to Environment and State
- Recall
- Assignments Change State
- Declarations Change Environment
- Differentiation also Possible from Static and
Dynamic LevelsProcedure Definition
vs. Procedure ActivationName Declaration vs.
Name BindingScope of Declaration vs.
Lifetime of Binding
Environment
State
Compile Time
Run Time
Name Storage Value
Environment
State
42Issues Impacting Runtime Environment
- Are Procedure/Functions Recursive?
- What happens to Values of Local names after the
Procedure Activation Completes? - Can a Procedure Refer to Non-Local Names?
- What are Parameter Passing Mechanisms?
- How are Results Returned from Functions?
- Can Proc/Func be Returned as a Result? Passed as
Parameters? - Can Programmer Dynamically Allocate Storage?
- How is Deallocation Handled?
- These are
- Rules of the Game
- What Every Software Engineer Should Know!
43Memory Allocation Strategies
- Details the way that Memory is Managed within OS
- Crucial for Activation Records
- Different Allocation Strategies Used by Each Area
- Static Compile Time
- Stack Run Time
- Sizes of Data Known at Compile Time
- Parameters during Calls
- Heap Run Time
- Allocate and Deallocate Memory (malloc)
- Programmatic Needs
Code
Static
Stack (Automatic)
Heap (Dynamic)
44Memory Pools
- Where does memory comes from ?
- Three pools
- Static
- Automatic (Stack)
- Dynamic
Automatic (Stack)
Dynamic
Static
45Static Pool
- Content
- All the static strings that appear in the
program - All the static constants used in the program
- All the static variables declared in the program
- static int
- static arrays
- static records
- static ....
- Allocation ?
- Well... it is static, i.e.,
- All the sizes are determined at compile time.
- Cannot grow or shrink
46Static Allocation
- Binding Names to Storage Location at Compile Time
- Program and Procedure/Function Variables
- Consider C Program
int xyz char c20 int abc () static int x
. . . static int y . . .
. . .
Global vars known at compile time Static means
to allocate at compile Data values persist over
multiple calls to abc() x, y retain values
in successive calls to abc!
47Static Allocation
- Static Allocation is Limited and Programming
Language Specific - In Pascal, Global Program Variables Only
- In C, Global Variables and Specific Static
- What are Implications to Compilation Process?
- Are these two Programs Same? Why? Different?
How?
main() int x1000000 . . .
main() static int x1000000 . . .
48Automatic Pool (Stack Allocation)
- Content
- Local variables
- Actuals (arguments to methods/functions/procedures
) - Allocation
- Automatic when calling a method/function/procedure
- Deallocation
- Automatic when returning from a
method/function/procedure - Management policy
- Stack-like
49Automatic Pool (Stack Allocation)
- Tightly Coupled with Procedure Activation
- Parameters, Local Variables, Temporary Variables
- Allocated as Each Procedure/Function Called
- Stack Expands for Nested (and Recursive) Calls
- Two Step Process
- Call Sequence Allocation Activation Record and
Enter Data into its Fields - Return Sequence Restores State of Program to
Caller to Continue Execution - What Does Caller vs. Callee Do?
50Caller and Callee
51Caller vs. Callee
- Call Sequence
- Caller Evaluates Actuals
- Caller Stores Return Address and old Value of
top_sp in Callees Activation Record - Callee Saves Register Values and other Status
Info. - Callee Inits its Local data and Begins Execution
- Return Sequence
- Callee Places a Return value Adjacent to
Activation Record of Caller - Using Status Info, callee Restores top_sp and
Registers, and Jumps to Return Addres iN caller - Caller Copies Return Value into its Own
Activation Record for Subsequent Usage
52What are Possible Problems
int x 10000 char c20 abc (t -----) . .
. zzz(t) . . . xyz (a -----) . . .
abc(a) . . . xyz(x)
What Happens to Stack During these multiple
calls? Whats the Problem Here? What are two
Possible Solutions?
53What are Possible Problems
main() int p p call_it() int
call_it () int i 23 return i
Is there a Problem? What is it? How is it
Solved?
54Heap (Dynamic) Allocation
- Allows for the Management of Dynamic Storage
- Allow Local Names to Persist When Procedure
Activation Completes - When a Called Activation Outlives Caller (Fork
off a Process ) - Important Issues Include
- How Does Allocation Occur?
- Is Garbage Collection Available for Programmer
Initiated Deallocation? - Or, does Deallocation Occur Automatically?
55Dynamic Pool
- Content
- Anything allocated by the program at runtime
- Allocation
- Depends on the language
- C malloc
- C/Java/C new
- ML/Lisp/Scheme implicit
- Deallocation
- Depends on the language
- C free
- C delete
- Java/C/ML/Lisp/Scheme Garbage collection
56Revisiting Activation Records
- Focus on Execution Environment for Imperative
Language (C, C, Java, Pascal, etc.) - Code Reference by ProgramCounter
- Stack Contains Activation Records (Grows Down)
- Snapshot of Activations
- Status of Execution
- Heap for Dynamic Memory(Grows Up)
Code
Static Data
Stack
Heap
57What Activation Record Contains?
58Recall Activation Record in C
- Actual Parameters Supplied by Caller
- Needed to Restart Caller
- Local Variables for Scope
- If Callee Calls Another Procedure/Function
Etc. Incoming Param 2 Incoming Param 1
Saved State Info
Local Variables
Temporary Storage
Outgoing Parameters
When Callee Invokes/Activates Another Procedure,
Outgoing Parameters of One Record are Incoming
Parameters of Another Activation Record
59Activation Records
- Also known as Frames
- A record pushed on the execution stack
60Creating the Frame
- Three actors
- The caller
- The CPU
- The callee
int foo(int x,int y) ... bar() ...
x foo(3,y) ...
61Creating the Frame
- Three actors
- The caller
- The CPU
- The callee
int foo(int x,int y) ... bar() ...
x foo(3,y) ...
Actual Function Call
62Creating the Frame
- Three actors
- The caller
- The CPU
- The callee
int foo(int x,int y) ... bar() ...
x foo(3,y) ...
63Closeup on Management Data
Links Allow Callee to Refer to Non-Local Scopes
64Returning From a Call
- Easy
- The RET instruction simply
- Access MGMT Area from FP
- Restores SP
- Restores FP
- Transfer control to return address
65Returning From a Call
- Easy
- The RET instruction simply
- Access MGMT Area from FP
- Restores SP
- Restores FP
- Transfer control to return address
66Returning From a Call
- Easy
- The RET instruction simply
- Access MGMT Area from FP
- Restores SP
- Restores FP
- Transfer control to return address
67Returning From a Call
- Easy
- The RET instruction simply
- Access MGMT Area from FP
- Restores SP
- Restores FP
- Transfer control to return address
68Variations
- This is architecture dependent
- On Intel
- The arguments are on the stack
- The return address is on the stack
- The return value is always in the accumulator
(eax register) - On PPC
- The first 8 arguments are in registers
- Remaining arguments are on the stack
- Space is reserved for all arguments anyway
- The return address is in the LINK register
- The return value is left in R1 (register 1)
69Impact of Parameter Passing Mechanisms
- Different Parameter Passing Mechanisms all have
Different Techniques for Handling Formal and
Actual Parameters - Well Consider Four Approaches
- Call By Value
- Call By Reference
- Copy Restore
- Call By Name
- In Process
- Consider Impact on Environment and State
- Identify Which Languages Use Each Approach
70Call By Value
- Environment and State of Actual/Formals is
Different - Push on the stack a copy of the argument
- Size depends on argument type
- Any write operation overwrites the copy
- Copy automatically discarded on exit
swap (x, y integer) var temp
integer begin temp x x y y
temp end . . . int a, b . . . swap (a,
b) Print(a, b)
x
y
x and y are in callees activation record
(scope) a and b are in callers activation
record (scope)
a
b
Do a and b Change?
71Call By Reference
- Actuals and Formals Refer to Same Storage
Location - Place the address of the actual on the stack
- A write operation simply follows the pointer
- Locations are Passed!
- Advantages?
a and b are in callers activation record
(scope)
swap (var x, y integer) var temp
integer begin temp x x y y
temp end . . . int a, b . . . swap (a,
b) Print(a, b)
x
y
a
b
72C is Call by Value ONLY!
- Swap Will NOT Change a and b
- Values are Unchanged!
main() int x5, y10 swap (x, y) void
swap (int px, py) int temp temp px
px py py temp What is Effect of
Call?
main() int x5, y10 swap (x, y) void swap
(int x, y) int temp temp x x y
y temp
73Copy Restore
- Call by Value Result in Ada Programming Language
- Three Types of Parameters in Ada
- In Corresponding to Value Parameters
- Out Corresponding to Copy-Out Phase(Final
r-value of Formals Copied out to Actuals) - Inout Either
- Call by Reference
- True Call by Value Result (Compiler Dependent)
- How Does this work?
74Copy Restore
- Key Interpretation
- If an Actual has Just an r-value - Call By Value
- If an Actual has a l-value then Copy Out Formal
to Actual Storage not Shared! - What Does Following Code do
program copyout (input, output) var a
integer procedure unsafe (var x
integer) begin x 2 a
0 end begin a 1 unsafe(a) writeln(a) e
nd.
x
a
75Call By Name
- Two Views of this Process
- Macro Expansion in C where a Substitution of
Procedure Code (the Callee) Occurs to Replace a
Call (in Caller) - Substitution of the Actual Parameters (from
Caller) into the Procedure Itself (into Callee) - What are Implications of each Approach?
76Version 1 Macro Expansion
int x MAXBUF 1 int m 0 void p(void)
m(m1)x int out (void) int x x
bufm m (m1) x return x
int x MAXBUF 1 int m 0 void p(void)
m(m1)x int out (void) int x x
bufm p() return x
What Happens when m (m1) x is
substituted for p()? How Does Resulting Code
Work?
77Version 2 Actual Substitution
Procedure Definition void q(int y) int i
i 5 y y 1
Procedure Call main() int i int A10 i
6 q (Ai)
What is a result of this Call by Substitution
Ai (actual) into y (formal)?
int i i 5 Ai) Ai) 1
78Parameter Passing - Synopsis
- Call by Value
- Environment and Store of Actuals and Formals are
Distinct - Environment maps to Separate l-values and Store
Maps to Separate r-values - Call by Reference
- Formals and Actual MAY have Different Variable
Names - However, Environment Maps those Different Names
to the Same l-Values - NY City vs. Big Apple
- Boston vs. Beantown
-
79Parameter Passing - Synopsis
- Call by Value Result
- Copy in Phase
- Formals have Separate l-values and contain
r-values of Actuals - l-values of Actuals are Saved
- Copy Out Phase Copy Final values of Formals to
Saved l-values of Actuals - Call by Name Textual Substitution
- Procedure Body of Code Replaces Call
- Actuals for Formals
- Formals Dont have Own Environment and Store
- Locals have Own Environment and Store
- Environment and Store of Actuals are Used
80Data Typing and Layout
- Basic and Advanced Data Types Play a Role in
Procedure Activation - Basic Types
- Compute Map, Relative Address, and Offsets at
Compile Time for Local Data in Act. Record - Two Important Concepts
- Alignment Data Place at specific Positions in
Memory (Ints at Addresses Divisible by 4) - Padding Extra Space to Arrive at a Boundary
(Char(5) may allocate 6 or even 8 bytes)
81Consider an Example in C
- What is a Possible Memory Layout?
main() int i1 . . . for . . . int c if.
. . int i2 . . . . .
. while int i3 . . .
Need own l-value since global scope
i1
Share l-value since independent scopes
c,i3
i2
Need own l-value since nested scope
82Advanced Data Types
- Arrays, Records, Variable Records, Classes, etc.
all Have Sizes that are Known at Compile Time - Dynamic Types (Lists, Sets, etc.) have Fixed
Component Sizes (Size of List Element), but are
Allocated at Runtime - For Static Types
- Runt Time We must Compute Addresses of Variable
References - Anticipate this Need at Compile Time to Simplify
and Speed Access - For Dynamic Types
- Consider their Definition and Usage
- Well Briefly Review Arrays
83One Dimensional Arrays
- Data Stored in Consecutive Locations
- Slots each have Same Width w
- 1st Slot Begins at Location Base
- Slots are
- base, base w, base 2w, etc.
- iw (base loww) where i is the index and low
is the lower bound of the array - Compiler Computes
- c(base loww)
- Runtime calculation is iw c
A0
A1
A2
A3
84Two Dimensional Arrays
- Stored in Row Major Order (row by row)
- Compute Mi,j ir je (base lowrr
lowee)where - r and e are widths of rows and elements
- lowr and lowe are lower bounds of row and element
of a row - Simplify to d (base lowrr lowee)with
runtime calculation of - ir je d
M1,1 M1,2 M2,1 M2,2 M3,1 M3,2
85Scope of Declarations
- Recall Languages have Scoping Rules
- Two variations
- Everything Declared up-front
- Scoped declarations
- Pascal
- Local Scope within Procedure or Function
- Non-Local to Parent, Grandparent, etc.
- Proc/Func can be Defined within other Proc/Func
in Order to Hide their Existence (Scope) - C
- Local Scope within
- When Hit , the Lifetime of the Identifiers End
- Non-Local within File
- Visibility within Blocks
86Declarations Ahead
- Convention used by C
- Simplify the compiler
- After reading the declaration we know exactly the
frame size.
int foo(int x,int y) int a, i int d
float b bool c,e, g while (x lt 100)
d alloca(sizeof(int)y) if (y gt 0)
... for(i0iltxi)
g ...
87Declarations in Scopes
- Convention used by C/Java/....
- Get the declaration close to the code that uses
it. - Easier to read/track
- Downside
- Look at entire code to know the frame size.
int foo(int x,int y) int a float b
while (x lt 100) bool c int d
alloca(sizeof(int)y) if (y gt 0)
bool e .... for(int
i0iltxi) bool g ...
88Layout of Locals
- Problem
- Place locals in the frame
- Objective
- Minimize frame size
- Satisfy alignment constraints
- int 4 bytes
- float 4 bytes
- pointer/ref 4 bytes
- bool 1 byte
- short 2 bytes
- double 8 bytes
- long long 8 bytes
- Can we improve ?
89Further Improvements?
- Exploit the nested declarations!
int foo(int x,int y) int a float b
while (x lt 100) bool c int d
alloca(sizeof(int)y) if (y gt 0)
bool e .... for(int
i0iltxi) bool g ...
Overlay the storage for names whose scopes are
disjoint i and d share the same area of the
frame c and g as well
90Fixed-sized Local Arrays
- Fixed-sized means
- Size known at compile time
- e.g.int a10
- Questions
- Is this a problem ?
- Storage comes from ?
- Size ?
91What happens with nested procedures ?
- Name in nested scopes must be visible (
accessible)
program sort(input,output) var a array0..10
of integer x integer procedure
readarray var i integer begin ....
end procedure exchange(i,j integer)
begin x aiai ajaj
x end procedure quickSort(m,n
integer) var k,v integer function
partition(y,z integer) integer var i,j
integer begin ... a ... ( access
the array a ) ... v ... ( use the
pivoting element ) ... exchange(i,j)
... end begin end begin .... end
92Difficulty
- Names come from
- Frames of different functions/procedures
- The reference is based on lexical scoping.
- Name refers to definition in deepest nested scope
that contains the scope the reference comes from - Solution
- In each Frame
- Add a reference to the frame of the parent scope
- Note
- Book uses the terms
- Access link to refer to link to parent scope
- Control link to refer to link to previous frame
along invocations
93Solving Nested Scopes
94Example
program sort(input,output) var a array0..10
of integer x integer procedure
readarray var i integer begin ....
end procedure exchange(i,j integer)
begin x aiai ajaj
x end procedure quickSort(m,n
integer) var k,v integer function
partition(y,z integer) integer var i,j
integer begin ... a ... ( access
the array a ) ... v ... ( use the
pivoting element ) ... exchange(i,j)
... end begin end begin .... end
95Access Link Frames
program sort(input,output) var a array0..10
of integer x integer procedure
readarray var i integer begin ....
end procedure exchange(i,j integer)
begin x aiai ajaj
x end procedure quickSort(m,n
integer) var k,v integer function
partition(y,z integer) integer var i,j
integer begin ... a ... ( access
the array a ) ... v ... ( use the
pivoting element ) ... exchange(i,j)
... end begin end begin .... end
To access a name Find out the lexical
distance If distance is k, jump over k access
links.
96Memory Manager Compilation View
- Management of Primary and Secondary Memory
- Automatic Movement Between Two
- From Secondary to Primary to Start or Restart
- From Primary to Secondary to Suspend
- Capturing the Executable State to Secondary
Memory for Ease of Restart - Strategies for Determining When Movement Will
Occur - Involves Management of
- Memory Hierarchy of Hardware
- Creation of Address Space for Process
- Mapping Process During Allocation
97Memory Manager
- Requirements
- Minimize Primary Memory Access Time
- Registers vs. Cache vs. Primary Memory
- Maximize Primary Memory Size
- Virtual Memory Appearance of Larger Memory
- Primary Memory Must Be Cost-Effective
- Doubling of Capacity has Had Greatest Impact
- Todays Memory Manager
- Allocates Primary Memory to Processes
- Maps Process Address Space to Primary Memory
- Minimizes Access Time Using Cost-effective Memory
Configuration
98Address Space vs. Primary Memory
Primary Memory
Process Address Space
Mapped to object other than memory
99Building the Address Space
Adjust Addresses Translation Time Load Time
Compose Elements
Allocate Executable Memory Space
100Unix Style Memory Layout for Process
- Storage Organization for a Process
- Code Referenced by PC
- Global/Local Variables
- Other Variables
- Heap for Dynamic Memory Allocation
- For Activation Records
- Still Other Variables
Text Segment
Initialized Data Segment
Un-initialized Data Segment
Heap Storage
Stack Segment
Environment Variables, etc.
101Recall Key Compilation Concepts
- Compile Produce Relocatable Object Module
- Static Variables Allocated at Compile
- Non-Static Variables via Run Time Stack
- Dynamic Variables via Heap
- Recall Activation Record in C (next slide)
- Link Time Produce Absolute (or Load) Module
- Collect and Combine Individual Relocatable
Modules - Linkage Editor Internal Organization of
Processes Address Space - Absolute Module at Location 0000
- All Addresses w.r.t. Base Location
102Recall An Activation Record in C
- Actual Parameters Supplied by Caller
- Needed to Restart Caller
- Local Variables for Scope
- If Callee Calls Another Procedure/Function
Etc. Incoming Param 2 Incoming Param 1
Saved State Info
Local Variables
Temporary Storage
Outgoing Parameters
103Recall Key Compilation Concepts
- Loader Produce Executable Image for Primary
Memory - Readjust Addresses for Execution
- Offset from Location 0000 Must be Changed
- Once Partition for Process Execution has Been
Chosen - Bind Addresses to Physical Memory Locations
- Referencing Both Instructions and Data
- Dynamic Memory for Data Structures
- Each Address Space of Process Contains Unused
Memory - User Programs Utilize for Dynamic Memory
Allocation (e.g., malloc and new)
104From Code to Loaded Program
ORIGINAL SOURCE static int gVar . . . int
proc_a (int arg) . . . gVar 7
put_record(gVar) . . .
RELOCATABLE OBJECT MODULE 0000 . . . . .
. 0008 entry proc_a . . . 0036 Space for gVar .
. . 0220 load 7, R1 0224 store R1,
0036 0228 push 0036 0232 call put_record . .
. 0400 External Reference Table 0404 put_record
0232 . . . 0500 External Definition
Table 0540 proc_a 0008 0600 (optional symbol
table) 0799 (last location in module)
105From Code to Loaded Program
THE ABSOLUTE PROGRAM 0000 (Other Modules) . .
. 1008 entry proc_a . . . 1036 Space for gVar .
. . 1220 load 7, R1 1224 store R1,
1036 1228 push 1036 1232 call 2334 1399 (end of
proc_a) . . . (Other Modules) 2334 entry
put_record . . . 2670 (opt. symbol
table) 2999 (last loc. in module)
PROGRAM AT LOCATION 4000 0000 (Other
Processes) 4000 (Other Modules) . .
. 5008 entry proc_a . . . 5036 Space for gVar .
. . 5220 load 7, R1 5224 store R1,
5036 5228 push 5036 5232 call 6334 5399 (end of
proc_a) . . . (Other Modules) 6334 entry
put_record . . . 6670 (opt. symbol
table) 6999 (last loc. in module) 7000 (Other
Processes)
106Concluding Remarks/Looking Ahead
- Runtime Environment is Complex and Complicated
Part of Compilation Process - Software Engineers Need Solid Understanding
- Key Issue are Questions from Slide 7.XX
- Once Answer Questions, know Assumptions and
Available Techniques to Solve Problems - Other Issues of Note
- Activation, Scoping, Memory Allocation
- Parameter Passing, Layout, etc.
- Looking Ahead
- Intermediate and Machine Code Generation
- Optimization