Upload
tekli
View
34
Download
0
Embed Size (px)
DESCRIPTION
Chap 7: Runtime Organization. Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield Way, Unit 2155 Storrs, CT 06269-3155. [email protected] http://www.engr.uconn.edu/~steve (860) 486 - 4818. Material for course thanks to: - PowerPoint PPT Presentation
Citation preview
CH7.1
CSE4100
Chap 7: Runtime OrganizationChap 7: Runtime Organization
Prof. Steven A. Demurjian Computer Science & Engineering Department
The University of Connecticut371 Fairfield Way, Unit 2155
Storrs, CT [email protected]
http://www.engr.uconn.edu/~steve(860) 486 - 4818
Material for course thanks to:Laurent MichelAggelos KiayiasRobert LeBarre
CH7.2
CSE4100
OverviewOverview Runtime Environment Focuses on the wayt hat a Runtime Environment Focuses on the wayt hat a
Program is Managed/Controlled During its ExecutionProgram is Managed/Controlled During its Execution We’ll Focus on We’ll 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
CH7.3
CSE4100
Basic Definitions and ConceptsBasic Definitions and Concepts Procedure Definition Procedure Definition
Declaration that has a Name and has a Body If Returns a value, then Function
Procedure Definition Contains a Sequence of Identifiers Procedure Definition Contains a Sequence of Identifiers Called the Called the Formal ParametersFormal Parameters
Procedure Call Contains a List of Arguments passed to Procedure Call Contains a List of Arguments passed to the Procedure or the the Procedure or the Actual ParametersActual Parameters
Information in Program can be Characterized 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
Name Storage Value
CompileTime
RunTime
CH7.4
CSE4100
Program OrganizationProgram Organization PurposePurpose
Define what constitutes a Program Review overall OS Structure Examine Process Structure Present and Understand Memory Management
issues Remember, a Compiler Must:Remember, a Compiler Must:
Target a Specific Operating System Interact within that Operating System Provide Access at Machine Level to Operating
System Components
CH7.5
CSE4100
The Basic AbstractionsThe Basic Abstractions ProcessesProcesses
Unit of Computation Program/Application Composed of One or More
Interacting Processes Processes Interact with and Utilize Resources
FilesFiles Unit of Information Storage Classic, Default Utilized to Support Programs Read, Write, and Transfer via Files
Other ResourcesOther Resources CPU, Printer, Memory, etc. Network, Display, etc.
CH7.6
CSE4100
Program, Processes & ResourcesProgram, Processes & Resources
ProcessesResources
CPU
Memory
DeviceDevice
DeviceDevice
Operating System
Hardware
Data
Program
CH7.7
CSE4100
What are Executables?What are Executables? ExecutableExecutable
File Containing Machine Code Examples
A bash script, a perl script, a ‘compiled’ java program, a compiled C program, ...
Java Bytecode and JVM
CH7.8
CSE4100
Java Virtual Machine (JVM)Java Virtual Machine (JVM) JVM is a Platform Specific Program which Interprets JVM is a Platform Specific Program which Interprets
and Executes Java Codeand Executes Java Code JVM Interprets and Executes BytecodesJVM Interprets and Executes Bytecodes
JVM Targeted as Small/Efficient - Embeddable within JVM Targeted as Small/Efficient - Embeddable within Consumer ElectronicsConsumer Electronics
JVM Stack Based Machine - Simulates Real ProcessorJVM Stack Based Machine - Simulates Real Processor
CA FE BA BE 00 03 00 2D 00 3ECA FE BA BE 00 03 00 2D 00 3E08 00 3B 08 00 01 08 00 20 0808 00 3B 08 00 01 08 00 20 08
CH7.9
CSE4100
Java Visualization Java Visualization
CH7.10
CSE4100
What are Executables?What are Executables? Native ExecutableNative 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
CH7.11
CSE4100
From Code to Loaded ProgramFrom 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, R10224 store R1, 00360228 push 00360232 call ‘put_record’. . .0400 External Reference Table0404 ‘put_record’ 0232. . .0500 External Definition Table0540 ‘proc_a’ 00080600 (optional symbol table)0799 (last location in module)
CH7.12
CSE4100
From Code to Loaded ProgramFrom Code to Loaded Program
THE ABSOLUTE PROGRAM
0000 (Other Modules). . .1008 entry proc_a. . .1036 [Space for gVar]. . .1220 load =7, R11224 store R1, 10361228 push 10361232 call 23341399 (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, R15224 store R1, 50365228 push 50365232 call 63345399 (end of proc_a). . . (Other Modules)6334 entry put_record. . .6670 (opt. symbol table)6999 (last loc. in module)7000 (Other Processes)
CH7.13
CSE4100
What is a Process?What is a Process? An Executable with its Execution Context and StateAn Executable with its Execution Context and State Consists of: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 Computers can Run Multiple Processes at Same Time
CH7.14
CSE4100
What is a Process?What is a Process? Sequential Program for ExecutionSequential 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 Program is “Static Source Code/Executable Statements” which Lacks Data SetStatements” which Lacks Data Set
Process is “Dynamic Entity Executing on Actual Data Process is “Dynamic Entity Executing on Actual Data Set”Set”
For Example, Multiple Unix Users Can Execute emacs, For Example, Multiple Unix Users Can Execute emacs, Latex, vi, grep, at same TimeLatex, vi, grep, at same Time Shared Program Executable emacs Dedicated Process for Each User of emacs
CH7.15
CSE4100
A ProcessA Process
Abstract Machine Environment (OS)
Code Data
Process Status
ResourcesResourcesResources
CH7.16
CSE4100
UNIX ProcessUNIX Process
Text
Process Status
ResourcesResources
File
UNIX kernel
Stack
Data
File
CH7.17
CSE4100
More on UNIX ProcessesMore on UNIX Processes Each Process Has its Own Address SpaceEach Process Has its Own Address Space
Subdivided into Text, Data, & Stack Segment a.Out File Describes the Address Space
OS Creates OS Creates DescriptorDescriptor to Manage Process to Manage Process Process IdentifierProcess Identifier (PID) is Integer: User Handle for the (PID) is Integer: User Handle for the
Process (Descriptor) Process (Descriptor) Try “Try “psps” and “” and “Ps auxPs aux” (Read Man Page)” (Read Man Page)
USER PID %CPU %MEM SZ RSS TT S START TIME COMMANDwangz 25150 26.1 15.33048019308 pts/16 O Aug 05 6318:26 /apps/matlab/bin/smanish 7590 25.1 0.821436 964 ? O Jul 22 24628:34 /apps/matlab/bin/smarios 29536 0.6 3.5 5156 4376 pts/6 S 13:22:23 0:24 mailtoolsteve 29893 0.2 0.8 1080 980 pts/18 S 17:41:11 0:00 -cshroot 29905 0.2 0.7 1040 888 pts/18 O 17:41:53 0:00 ps -aux
CH7.18
CSE4100
MultiprogrammingMultiprogramming Load and Execute Multiple Programs into Space-Load and Execute Multiple Programs into Space-
Multiplexed Mem. while Time-Multiplexing CPUMultiplexed Mem. while Time-Multiplexing CPU Technique for Sharing the CPU Among Runnable Technique for Sharing the CPU Among Runnable
ProcessesProcesses 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 While One Process is Blocked, Another Should Be Able to RunAble to Run
Multiprogramming OS Accomplishes CPU Sharing Multiprogramming OS Accomplishes CPU Sharing “Automatically”“Automatically”
CH7.19
CSE4100
How Multiprogramming WorksHow Multiprogramming Works
CPU
Process 1
Process 2
Process 3
Process 4
Spac
e-M
ultip
lexe
d M
emor
y
Time-Multiplexed
CH7.20
CSE4100
Batch ProcessingBatch Processing Uses MultiprogrammingUses Multiprogramming Job (File of OS Commands) Prepared OfflineJob (File of OS Commands) Prepared Offline Batch of Jobs Given to OS at One TimeBatch of Jobs Given to OS at One Time OS Processes Jobs One-After-the-OtherOS Processes Jobs One-After-the-Other No Human-Computer InteractionNo Human-Computer Interaction OS Optimizes Resource UtilizationOS Optimizes Resource Utilization Batch Processing (as an Option) Still Used TodayBatch Processing (as an Option) Still Used Today Batch Files in DOS/Unix to Repetitively Execute Same Batch Files in DOS/Unix to Repetitively Execute Same
Set of CommandsSet of Commands ASIDE: Using Batch ProcessingASIDE: Using Batch Processing
1978/79 First Two Computer Courses at BC Keypunch IBM Cards Run Batch Job: Return 1-2hrs Later for Output
CH7.21
CSE4100
TimesharingTimesharing Uses MultiprogrammingUses Multiprogramming Support Interactive Computing Model (Illusion of Support Interactive Computing Model (Illusion of
Multiple Consoles)Multiple Consoles) Different Scheduling & Memory Allocation Strategies Different Scheduling & Memory Allocation Strategies
Than BatchThan Batch Tends to Propagate ProcessesTends to Propagate Processes Considerable Attention to Resource Isolation (Security Considerable Attention to Resource Isolation (Security
& Protection)& Protection) Tend to Optimize Response TimeTend to Optimize Response Time
CH7.22
CSE4100
How Timesharing WorksHow Timesharing Works
Timesharing OS
VM VM VM
Terminal Multiplexor
Timesharing Still Timesharing Still Pervasive in ComputingPervasive in Computing
Engineering Network Still Engineering Network Still Time-Shared in Some Time-Shared in Some RespectsRespects Access to User Files Email/Web Server Download Software
Terminals Replaced by Terminals Replaced by Unix Workstations and Unix Workstations and PCsPCs
Example of Network File Example of Network File System/OSSystem/OS
CH7.23
CSE4100
Personal ComputersPersonal Computers CPU Sharing Among One Person’s ProcessesCPU Sharing Among One Person’s Processes Ability of Only One Active Process at One TimeAbility 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 Today’s Apple and Windows MS – Multi-Process!
Increased Power of Computing for Personal TasksIncreased Power of Computing for Personal Tasks Graphics - Two-D, Three-D, Real-Time Multimedia - CDs and DVDs
CH7.24
CSE4100
Process Control & Real-TimeProcess Control & Real-Time Computer is Dedicated to a Single PurposeComputer is Dedicated to a Single Purpose Classic Embedded SystemClassic Embedded System Must Respond to External Stimuli in Fixed TimeMust Respond to External Stimuli in Fixed Time Continuous Media Popularizing Real-time TechniquesContinuous Media Popularizing Real-time Techniques An Area of Growing InterestAn 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
CH7.25
CSE4100
Evolution of Modern OSEvolution of Modern OS
Modern OS
Batch
Timesharing
PC & WkstationNetwork OS
Real-TimeMemory MgmtProtection
SchedulingFilesDevices
Memory Mgmt
ProtectionScheduling
System Software Human-ComputerInterface
Client-Server ModelProtocols
Scheduling
Java/JINI/JavaOS
CH7.26
CSE4100
Virtual Address SpaceVirtual Address Space An Abstraction provided by the Operating SystemAn Abstraction provided by the Operating System EssentiallyEssentially
A sandbox for the process PracticallyPractically
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.
CH7.27
CSE4100
Virtual MemoryVirtual Memory Components of Program (Identifiers, Labels, variables, Components of Program (Identifiers, Labels, variables,
etc.) Define etc.) Define Name SpaceName Space Language TranslationLanguage Translation
Converts Name Space Elements to Virtual Addresses
Binds Symbolic Names to Virtual Addresses Virtual Memory SystemVirtual Memory System
Dynamically Binds Virtual Addresses to Physical Addresses
Defers Binding Decision to During Program Execution
Perform Binding “As Needed”
CH7.28
CSE4100
Names, Virtual Addresses, Physical AddressesNames, Virtual Addresses, Physical Addresses
SourceProgram
AbsoluteModule
ExecutableImage
Name Space
PhysicalAddress Space
VirtualAddress Space
t: Virtual Address Space Physical Address Space {}
Compile andLink Tools
Distinguish Between Distinguish Between Symbolic Names vs. Virtual Addrs. vs. Physical Addrs.Symbolic Names vs. Virtual Addrs. vs. Physical Addrs.
CH7.29
CSE4100
Virtual Address SpaceVirtual Address Space Traditional OrganizationTraditional Organization
Code Area at the bottom
Static Data above Constants Static strings Static variables
Heap Grows upward
Stack Grows downward
Lot’s of free VM in between
0x0
0xffffffff
CH7.30
CSE4100
OS ConventionsOS Conventions Layout is dictated by the OSLayout is dictated by the OS
Specifically by the “Loader” LoaderLoader
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.
CH7.31
CSE4100
Zooming In.Zooming In. Close look on the code areaClose look on the code area
CH7.32
CSE4100
LoadingLoading Loader extracts the information from the executableLoader extracts the information from the executable Executable holds a header withExecutable 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)
ProgramExecutable:
CH7.33
CSE4100
Loading and ExecutionLoading and Execution LoaderLoader
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 StackExecution Stack A memory area at the top of the VM
Grows downward Grows on demand (with OS collaboration)
Purpose Automatic storage for local variables
CH7.34
CSE4100
A First Look at Activation RecordsA First Look at Activation Records Storage Organization for Storage Organization for
Program ExecutionProgram Execution Code Referenced by PCCode Referenced by PC Global/Local VariablesGlobal/Local Variables
Static Data Area Stack ContainsStack Contains
Set of Activation Records
All Active Procedures and Functions
Heap for Dynamic Memory Heap for Dynamic Memory AllocationAllocation
Code
Static Data
Stack
Heap
CH7.35
CSE4100
A General Activation RecordA General Activation Record
To the Calling ProcedureTo the Calling Procedure
Passed in to ProcedurePassed in to Procedure
To Act. Record of CallerTo Act. Record of Caller
Referenced Non-Local DataReferenced Non-Local Data
Needed to Restart CallerNeeded to Restart Caller
Local Variables for ScopeLocal Variables for Scope
Compiler GeneratedCompiler Generated
Returned Value
Actual Parameters
Optional Control Link
Temporaries
Saved Machine Status
Local Data
Optional Access Link
CH7.36
CSE4100
An Activation Record in CAn Activation Record in C
Actual Parameters Actual Parameters
Supplied by CallerSupplied by Caller
Needed to Restart CallerNeeded to Restart Caller
Local Variables for ScopeLocal Variables for Scope
If Callee Calls Another If Callee Calls Another Procedure/FunctionProcedure/Function
Etc. Incoming Param 2Incoming Param 1
Saved State Info
Temporary Storage
Outgoing Parameters
Local Variables
CH7.37
CSE4100
Activation RecordsActivation Records Procedure Activation Represents the Actions that Must Procedure Activation Represents the Actions that Must
Occur when a Caller Invokes a Callee:Occur when a Caller Invokes a Callee: Transfer of Actuals into Formals by the Language’s
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 Every Procedure Activation has Lifetime which is the
Sequence of Steps (Code) of Procedure Body of Sequence of Steps (Code) of Procedure Body of CalleeCallee
CH7.38
CSE4100
What are Possible Activations?What are Possible Activations? NestedNested
A calls B calls C Non-OverlappingNon-Overlapping
A calls B B calls C
RecursiveRecursive A calls itself
ConcurrentConcurrent A calls B (spawns process) A calls C (spawns process) B, C: execute in parallel and compete for Resources
CH7.39
CSE4100
Activation TreeActivation Tree Graphical Representation of Activations over TimeGraphical Representation of Activations over Time
CH7.40
CSE4100
How is Tree Interpreted?How is Tree Interpreted? Each Node is a Specific Procedure CallEach Node is a Specific Procedure Call Root Node is Start of ProgramRoot Node is Start of Program Parent NodeParent Node
Flow from Parent to Child Caller to Callee
Sibling NodeSibling Node All Nodes to Left have Completed
Parent Comples when All Children CompleteParent Comples when All Children Complete Utilize Control Stack to Represent Current State of an Utilize Control Stack to Represent Current State of an
ActivationActivation s, q(1,9), q(1,3), q(2,3) Where is this on Prior Slide? Represents “state” at Point in Time
CH7.41
CSE4100
Relationship to Environment and StateRelationship to Environment and State Recall:Recall:
Assignments Change State Declarations Change Environment
Differentiation also Possible from Static and Dynamic Differentiation also Possible from Static and Dynamic LevelsLevels
Procedure Definition Procedure Definition vs. vs. Procedure ActivationProcedure Activation
Name DeclarationName Declaration vs. vs. Name BindingName Binding
Scope of DeclarationScope of Declaration vs. vs. Lifetime of BindingLifetime of Binding
Environment State
Name Storage Value
CompileTime
RunTime
Environment State
CH7.42
CSE4100
Issues Impacting Runtime EnvironmentIssues Impacting Runtime Environment Are Procedure/Functions Recursive?Are Procedure/Functions Recursive? What happens to Values of Local names after the What happens to Values of Local names after the
Procedure Activation Completes?Procedure Activation Completes? Can a Procedure Refer to Non-Local Names?Can a Procedure Refer to Non-Local Names? What are Parameter Passing Mechanisms?What are Parameter Passing Mechanisms? How are Results Returned from Functions?How are Results Returned from Functions? Can Proc/Func be Returned as a Result? Passed as Can Proc/Func be Returned as a Result? Passed as
Parameters?Parameters? Can Programmer Dynamically Allocate Storage?Can Programmer Dynamically Allocate Storage? How is Deallocation Handled?How is Deallocation Handled?
These are:These are: Rules of the Game What Every Software Engineer Should Know!
CH7.43
CSE4100
Memory Allocation StrategiesMemory Allocation Strategies Details the way that Memory is Details the way that Memory is
Managed within OSManaged within OS Crucial for Activation RecordsCrucial for Activation Records Different Allocation Strategies Different Allocation Strategies
Used by Each AreaUsed 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)
CH7.44
CSE4100
Memory PoolsMemory Pools Where does memory comes from ?Where does memory comes from ? Three poolsThree pools
Static Automatic (Stack) Dynamic
StaticStatic
Automatic (Stack)Automatic (Stack)
DynamicDynamic
CH7.45
CSE4100
Static PoolStatic Pool ContentContent
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 ?Allocation ? Well... it is static, i.e.,
All the sizes are determined at compile time. Cannot grow or shrink
CH7.46
CSE4100
Static AllocationStatic Allocation Binding Names to Storage Location at Compile TimeBinding Names to Storage Location at Compile Time Program and Procedure/Function VariablesProgram and Procedure/Function Variables Consider C Program:Consider C Program:
int xyz;char c[20];
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!
CH7.47
CSE4100
Static AllocationStatic Allocation Static Allocation is Limited and Programming Static Allocation is Limited and Programming
Language SpecificLanguage Specific In Pascal, Global Program Variables Only In C, Global Variables and Specific Static
What are Implications to Compilation Process?What are Implications to Compilation Process? Are these two Programs Same? Why? Different? Are these two Programs Same? Why? Different?
How?How?main(){ int x[1000000]; . . .}
main(){ static int x[1000000]; . . .}
CH7.48
CSE4100
Automatic Pool (Stack Allocation)Automatic Pool (Stack Allocation) ContentContent
Local variables Actuals (arguments to
methods/functions/procedures) AllocationAllocation
Automatic when calling a method/function/procedure
DeallocationDeallocation Automatic when returning from a
method/function/procedure Management policyManagement policy
Stack-like
CH7.49
CSE4100
Automatic Pool (Stack Allocation)Automatic Pool (Stack Allocation) Tightly Coupled with Procedure ActivationTightly Coupled with Procedure Activation Parameters, Local Variables, Temporary Variables Parameters, Local Variables, Temporary Variables
Allocated as Each Procedure/Function Called Stack Expands for Nested (and Recursive) Calls
Two Step ProcessTwo 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?What Does Caller vs. Callee Do?
CH7.50
CSE4100
Caller and CalleeCaller and Callee
CH7.51
CSE4100
Caller vs. CalleeCaller vs. Callee Call SequenceCall Sequence
Caller Evaluates Actuals Caller Stores Return Address and “old” Value of
top_sp in Callee’s Activation Record Callee Saves Register Values and other Status Info. Callee Inits its Local data and Begins Execution
Return SequenceReturn 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
CH7.52
CSE4100
What are Possible ProblemsWhat are Possible Problems Passing Arrays by ValuePassing Arrays by Value
int x [10000];char c[20];
abc (t: -----){ . . . zzz(t); . . .}
xyz (a: -----){ . . . abc(a); . . .}
xyz(x);
What Happens to Stack During these multiple calls?
What’s the Problem Here?
What are two Possible Solutions?
CH7.53
CSE4100
What are Possible ProblemsWhat are Possible Problems Dangling ReferencesDangling References
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?
CH7.54
CSE4100
Heap (Dynamic) AllocationHeap (Dynamic) Allocation Allows for the Management of Dynamic Storage: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:Important Issues Include: How Does Allocation Occur? Is Garbage Collection Available for Programmer
Initiated Deallocation? Or, does Deallocation Occur Automatically?
CH7.55
CSE4100
Dynamic PoolDynamic Pool ContentContent
Anything allocated by the program at runtime AllocationAllocation
Depends on the language C malloc C++/Java/C# new ML/Lisp/Scheme implicit
DeallocationDeallocation Depends on the language
C free C++ delete Java/C#/ML/Lisp/Scheme Garbage collection
CH7.56
CSE4100
Revisiting Activation RecordsRevisiting Activation Records Focus on Execution Environment for Imperative Focus on Execution Environment for Imperative
Language (C, C++, Java, Pascal, etc.)Language (C, C++, Java, Pascal, etc.) Code Reference by ProgramCode Reference by Program
CounterCounter Stack Contains Activation Stack Contains Activation
Records (Grows Down)Records (Grows Down) Snapshot of Activations Status of Execution
Heap for Dynamic MemoryHeap for Dynamic Memory(Grows Up)(Grows Up)
Code
Static Data
Stack
Heap
CH7.57
CSE4100
What Activation Record Contains?What Activation Record Contains?
CH7.58
CSE4100
Recall Activation Record in CRecall Activation Record in C Actual Parameters Actual Parameters
Supplied by CallerSupplied by Caller
Needed to Restart CallerNeeded to Restart Caller
Local Variables for ScopeLocal Variables for Scope
If Callee Calls Another If Callee Calls Another Procedure/FunctionProcedure/Function
Etc. Incoming Param 2Incoming Param 1
Saved State Info
Temporary Storage
Outgoing Parameters
Local Variables
When Callee Invokes/Activates Another Procedure, Outgoing Parameters of One Record are Incoming Parameters of Another Activation Record
CH7.59
CSE4100
Activation RecordsActivation Records Also known as “Frames”Also known as “Frames”
A record pushed on the execution stack
CH7.60
CSE4100
Creating the FrameCreating the Frame Three actorsThree actors
The caller The CPU The callee
int foo(int x,int y) { ...}
bar() { ... x = foo(3,y); ...}
CH7.61
CSE4100
Creating the FrameCreating the Frame Three actorsThree actors
The caller The CPU The callee
int foo(int x,int y) { ...}
bar() { ... x = foo(3,y); ...}
Actual Function Call
CH7.62
CSE4100
Creating the FrameCreating the Frame Three actorsThree actors
The caller The CPU The callee
int foo(int x,int y) { ...}
bar() { ... x = foo(3,y); ...}
CH7.63
CSE4100
Closeup on Management DataCloseup on Management Data
Links Allow Callee Links Allow Callee to Refer toto Refer to Non-Local ScopesNon-Local Scopes
CH7.64
CSE4100
Returning From a CallReturning From a Call EasyEasy
The RET instruction simplyAccess MGMT Area
from FPRestores SPRestores FPTransfer control to return
address
CH7.65
CSE4100
Returning From a CallReturning From a Call EasyEasy
The RET instruction simplyAccess MGMT Area
from FPRestores SPRestores FPTransfer control to return
address
CH7.66
CSE4100
Returning From a CallReturning From a Call EasyEasy
The RET instruction simplyAccess MGMT Area
from FPRestores SPRestores FPTransfer control to return
address
CH7.67
CSE4100
Returning From a CallReturning From a Call EasyEasy
The RET instruction simplyAccess MGMT Area
from FPRestores SPRestores FPTransfer control to return
address
CH7.68
CSE4100
VariationsVariations This is architecture dependentThis 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)
CH7.69
CSE4100
Impact of Parameter Passing MechanismsImpact of Parameter Passing Mechanisms Different Parameter Passing Mechanisms all have Different Parameter Passing Mechanisms all have
Different Techniques for Handling Formal and Actual Different Techniques for Handling Formal and Actual ParametersParameters
We’ll Consider Four ApproachesWe’ll Consider Four Approaches Call By Value Call By Reference Copy Restore Call By Name
In Process…In Process… Consider Impact on Environment and State Identify Which Languages Use Each Approach
CH7.70
CSE4100
Call By ValueCall By Value Environment and State of Actual/Formals is DifferentEnvironment 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;
begintemp := x;x := y;y := temp;
end;. . .int a, b;. . .swap (a, b)Print(a, b)
x and y are in callee’s activation record (scope)a and b are in caller’s activation record (scope)
x y
a b
Do a and b Change?
CH7.71
CSE4100
Call By ReferenceCall By Reference Actuals and Formals Refer to Same Storage LocationActuals 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?Advantages?swap (var x, y: integer);var temp: integer;
begintemp := x;x := y;y := temp;
end;. . .int a, b;. . .swap (a, b)Print(a, b)
a and b are in caller’s activation record (scope)
x y
a b
CH7.72
CSE4100
C is Call by Value ONLY!C is Call by Value ONLY! Swap Will NOT Change a Swap Will NOT Change a
and band b Values are Unchanged!Values are Unchanged!
Fake “Call by Reference”Fake “Call by Reference”
main(){int x=5, y=10;swap (x, y);}void swap (int x, y);{int temp; temp := x; x := y; y := temp;}
main(){int x=5, y=10;swap (&x, &y);}void swap (int *px, *py);{int temp; temp := *px; *px := *py; *py := temp;}
What is Effect of Call?
CH7.73
CSE4100
Copy RestoreCopy Restore Call by Value Result in Ada Programming LanguageCall by Value Result in Ada Programming Language Three Types of Parameters in Ada: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?How Does this work?
CH7.74
CSE4100
Copy RestoreCopy Restore Key InterpretationKey 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:What Does Following Code do:program copyout (input, output);var a: integer;procedure unsafe (var x: integer);
begin x := 2; a := 0;end;
begina := 1;unsafe(a);writeln(a);
end.
x
a
CH7.75
CSE4100
Call By NameCall By Name Two Views of this ProcessTwo 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?What are Implications of each Approach?
CH7.76
CSE4100
Version 1: Macro ExpansionVersion 1: Macro Expansion
int x = MAXBUF + 1;int m = 0;void p(void) {m=(m+1)%x;}int out (void){
int x;x = buf[m];p();return x;
}
int x = MAXBUF + 1;int m = 0;void p(void) {m=(m+1)%x;}int out (void){
int x;x = buf[m];{ m = (m+1) % x; } return x;
}
What Happens when { m = (m+1) % x; } is substituted for p()?
How Does Resulting Code Work?
CH7.77
CSE4100
Version 2: Actual SubstitutionVersion 2: Actual SubstitutionProcedure Definition
void q(int y) {
int i; i = 5;y = y + 1;
}
Procedure Call
main(){int i;int A[10];i = 6;q (A[i]);}
What is a result of this Call by Substitution A[i] (actual) into y (formal)?
{int i; i = 5;A[i]) = A[i]) + 1;
}
CH7.78
CSE4100
Parameter Passing - SynopsisParameter Passing - Synopsis Call by ValueCall 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 ReferenceCall 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
CH7.79
CSE4100
Parameter Passing - SynopsisParameter Passing - Synopsis Call by Value ResultCall 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 SubstitutionCall by Name: Textual Substitution Procedure Body of Code Replaces Call Actuals for Formals
Formals Don’t have Own Environment and Store Locals have Own Environment and Store Environment and Store of Actuals are Used
CH7.80
CSE4100
Data Typing and LayoutData Typing and Layout Basic and Advanced Data Types Play a Role in Basic and Advanced Data Types Play a Role in
Procedure ActivationProcedure Activation Basic TypesBasic Types
Compute Map, Relative Address, and Offsets at Compile Time for Local Data in Act. Record
Two Important ConceptsTwo 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)
CH7.81
CSE4100
Consider an Example in CConsider an Example in C What is a Possible Memory Layout?What is a Possible Memory Layout?
main(){int i1;. . .for . . . {int c; if. . . {int i2; . . . } . . .while {int i3; . . . }}
i1
c,i3
i2
Need own l-value since global scope
Share l-value since independent scopes
Need own l-value since nested scope
CH7.82
CSE4100
Advanced Data TypesAdvanced Data Types Arrays, Records, Variable Records, Classes, etc. all Arrays, Records, Variable Records, Classes, etc. all
Have Sizes that are Known at Compile TimeHave Sizes that are Known at Compile Time Dynamic Types (Lists, Sets, etc.) have Fixed Dynamic Types (Lists, Sets, etc.) have Fixed
Component Sizes (Size of List Element), but are Component Sizes (Size of List Element), but are Allocated at RuntimeAllocated at Runtime
For Static TypesFor Static Types Runt Time We must Compute Addresses of
Variable References Anticipate this Need at Compile Time to Simplify
and Speed Access For Dynamic TypesFor Dynamic Types
Consider their Definition and Usage We’ll Briefly Review Arrays …We’ll Briefly Review Arrays …
CH7.83
CSE4100
One Dimensional ArraysOne Dimensional Arrays Data Stored in Consecutive LocationsData Stored in Consecutive Locations
Slots each have Same Width wSlots each have Same Width w 11stst Slot Begins at Location Base Slot Begins at Location Base Slots are:Slots are:
base, base + w, base +2w, etc. i*w + (base –low*w) where i is the index and low
is the lower bound of the array Compiler Computes:Compiler Computes:
c=(base – low*w) Runtime calculation is: i*w + c
A[0] A[1]A[2] A[3]
CH7.84
CSE4100
Two Dimensional ArraysTwo Dimensional Arrays
M[1,1] M[1,2] M[2,1] M[2,2] M[3,1] M[3,2]
Stored in Row Major Order (row by row)Stored in Row Major Order (row by row)
Compute M[i,j] = i*r + j*e + (base – lowr*r – lowe*e)Compute M[i,j] = i*r + j*e + (base – lowr*r – lowe*e)wherewhere 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 – lowr*r – lowe*e)Simplify to d = (base – lowr*r – lowe*e)
with runtime calculation of:with runtime calculation of: i*r + j*e + d
CH7.85
CSE4100
Scope of DeclarationsScope of Declarations Recall Languages have Scoping RulesRecall Languages have Scoping Rules Two variationsTwo variations
Everything Declared up-front Scoped declarations
PascalPascal 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) CC
Local Scope within {} When Hit }, the Lifetime of the Identifiers End Non-Local within “File” Visibility within Blocks
CH7.86
CSE4100
Declarations AheadDeclarations Ahead Convention used by CConvention 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 < 100) { d = alloca(sizeof(int)*y); if (y > 0) { ...; } } for(i=0;i<x;i++) { g = ...; }}
CH7.87
CSE4100
Declarations in ScopesDeclarations in Scopes Convention used by C++/Java/....Convention used by C++/Java/....
Get the declaration “close” to the code that uses it. Easier to read/track
DownsideDownside Look at entire code to know the frame size.
int foo(int x,int y) { int a; float b; while (x < 100) { bool c; int* d = alloca(sizeof(int)*y); if (y > 0) { bool e = ....; } } for(int i=0;i<x;i++) { bool g = ...; }}
CH7.88
CSE4100
Layout of LocalsLayout of Locals ProblemProblem
Place locals in the frame ObjectiveObjective
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 ?Can we improve ?
CH7.89
CSE4100
Further Improvements?Further Improvements? Exploit the nested declarations!Exploit the nested declarations!
int foo(int x,int y) { int a; float b; while (x < 100) { bool c; int* d = alloca(sizeof(int)*y); if (y > 0) { bool e = ....; } } for(int i=0;i<x;i++) { bool g = ...; }}
Overlay the storage for names whose scopes are disjointi and d share the same “area” of the framec and g as well
CH7.90
CSE4100
Fixed-sized Local ArraysFixed-sized Local Arrays Fixed-sized meansFixed-sized means
Size known at compile time e.g.int a[10];
QuestionsQuestions Is this a problem ? Storage comes from ? Size ?
CH7.91
CSE4100
What happens with nested procedures ?What happens with nested procedures ? Name in nested scopes must be visible (& accessible)Name in nested scopes must be visible (& accessible)program sort(input,output);
var a: array[0..10] of integer; x: integer;
procedure readarray;var i : integerbegin .... end
procedure exchange(i,j : integer) begin
x := a[i];a[i] := a[j];a[j] := 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); ...
endbeginend
begin ....end
CH7.92
CSE4100
DifficultyDifficulty Names come from 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
SolutionSolution In each Frame
Add a reference to the frame of the parent scope NoteNote
Book uses the terms Access link to refer to link to parent scope Control link to refer to link to previous frame along
invocations
CH7.93
CSE4100
Solving Nested ScopesSolving Nested Scopes
CH7.94
CSE4100
ExampleExample
program sort(input,output);var a: array[0..10] of integer;
x: integer;procedure readarray;
var i : integerbegin .... end
procedure exchange(i,j : integer) begin
x := a[i];a[i] := a[j];a[j] := 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); ...
endbeginend
begin .... end
CH7.95
CSE4100
Access Link & FramesAccess Link & Framesprogram sort(input,output);
var a: array[0..10] of integer; x: integer;
procedure readarray;var i : integerbegin .... end
procedure exchange(i,j : integer) begin
x := a[i];a[i] := a[j];a[j] := 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
beginend
begin .... end
To access a nameFind out the lexical distanceIf distance is k, jump over k access links.
CH7.96
CSE4100
Memory Manager – Compilation ViewMemory Manager – Compilation View Management of Primary and Secondary MemoryManagement 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 Involves Management of Memory Hierarchy of Hardware Creation of Address Space for “Process” Mapping “Process” During Allocation
CH7.97
CSE4100
Memory ManagerMemory Manager RequirementsRequirements
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
Today’s Memory Manager:Today’s Memory Manager: Allocates Primary Memory to Processes Maps Process Address Space to Primary Memory Minimizes Access Time Using Cost-effective
Memory Configuration
CH7.98
CSE4100
Address Space vs. Primary MemoryAddress Space vs. Primary Memory
Mapped to objectother than memory
Process Address Space Primary Memory
CH7.99
CSE4100
Building the Address SpaceBuilding the Address Space
Loader ProcessAddressSpace
Executable Memory
Adjust AddressesTranslation TimeLoad TimeSource
Code
Translation
Librarycode
OtherObjects
Secondary Memory
Process Address Space
Compose Elements
Allocate ExecutableMemory Space
CH7.100
CSE4100
Unix Style Memory Layout for ProcessUnix Style Memory Layout for Process Storage Organization for a Storage Organization for a
ProcessProcess ““Code” Referenced by PCCode” Referenced by PC Global/Local VariablesGlobal/Local Variables
Other VariablesOther Variables Heap for Dynamic Memory Heap for Dynamic Memory
AllocationAllocation
For Activation RecordsFor Activation Records
Still Other VariablesStill Other Variables
Text SegmentInitialized DataSegmentUn-initializedData Segment
Stack Segment
Heap Storage
Environment Variables, etc.
CH7.101
CSE4100
Recall Key Compilation Concepts Recall Key Compilation Concepts Compile: Produce Relocatable Object ModuleCompile: 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) ModuleLink 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
CH7.102
CSE4100
Recall: An Activation Record in CRecall: An Activation Record in C
Actual Parameters Actual Parameters
Supplied by CallerSupplied by Caller
Needed to Restart CallerNeeded to Restart Caller
Local Variables for ScopeLocal Variables for Scope
If Callee Calls Another If Callee Calls Another Procedure/FunctionProcedure/Function
Etc. Incoming Param 2Incoming Param 1
Saved State Info
Temporary Storage
Outgoing Parameters
Local Variables
CH7.103
CSE4100
Recall Key Compilation Concepts Recall Key Compilation Concepts Loader: Produce Executable Image for Primary Loader: Produce Executable Image for Primary
MemoryMemory 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 StructuresDynamic Memory for Data Structures Each Address Space of Process Contains “Unused”
Memory User Programs Utilize for Dynamic Memory
Allocation (e.g., malloc and new)
CH7.104
CSE4100
From Code to Loaded ProgramFrom 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, R10224 store R1, 00360228 push 00360232 call ‘put_record’. . .0400 External Reference Table0404 ‘put_record’ 0232. . .0500 External Definition Table0540 ‘proc_a’ 00080600 (optional symbol table)0799 (last location in module)
CH7.105
CSE4100
From Code to Loaded ProgramFrom Code to Loaded Program
THE ABSOLUTE PROGRAM
0000 (Other Modules). . .1008 entry proc_a. . .1036 [Space for gVar]. . .1220 load =7, R11224 store R1, 10361228 push 10361232 call 23341399 (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, R15224 store R1, 50365228 push 50365232 call 63345399 (end of proc_a). . . (Other Modules)6334 entry put_record. . .6670 (opt. symbol table)6999 (last loc. in module)7000 (Other Processes)
CH7.106
CSE4100
Concluding Remarks/Looking AheadConcluding Remarks/Looking Ahead Runtime Environment is Complex and Complicated Runtime Environment is Complex and Complicated
Part of Compilation ProcessPart of Compilation Process Software Engineers Need Solid UnderstandingSoftware Engineers Need Solid Understanding Key Issue are Questions from Slide 7.XXKey Issue are Questions from Slide 7.XX Once Answer Questions, know Assumptions and Once Answer Questions, know Assumptions and
Available Techniques to Solve ProblemsAvailable Techniques to Solve Problems Other Issues of Note:Other Issues of Note:
Activation, Scoping, Memory Allocation Parameter Passing, Layout, etc.
Looking Ahead:Looking Ahead: Intermediate and Machine Code Generation Optimization