Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer...

16
Run-Time Storage Run-Time Storage Organization Organization 66.648 Compiler Design Lecture 66.648 Compiler Design Lecture (03/23/98) (03/23/98) Computer Science Computer Science Rensselaer Polytechnic Rensselaer Polytechnic

Transcript of Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer...

Page 1: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Run-Time Storage Run-Time Storage OrganizationOrganization

66.648 Compiler Design Lecture (03/23/98)66.648 Compiler Design Lecture (03/23/98)

Computer ScienceComputer Science

Rensselaer PolytechnicRensselaer Polytechnic

Page 2: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Lecture OutlineLecture Outline

• Run-Time Storage OrganizationRun-Time Storage Organization

• ExamplesExamples

• AdministrationAdministration

Page 3: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Run-Time Storage Run-Time Storage OrganizationOrganization

A program obtains a single contiguous block of A program obtains a single contiguous block of storage (virtual memory) from the operating storage (virtual memory) from the operating system at the start of program execution. The system at the start of program execution. The generated code assumes a subdivision of the generated code assumes a subdivision of the storage into different areas/storage into different areas/

Code -- this area contains the generated target Code -- this area contains the generated target code for all procedures in the program. The size code for all procedures in the program. The size of this can be determined statically by the of this can be determined statically by the compiler/linker.compiler/linker.

Page 4: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Static DataStatic Data

Static Data-- this area contains global data objects Static Data-- this area contains global data objects whose size can be determined statically at whose size can be determined statically at compile time. Static variables are mapped to compile time. Static variables are mapped to offsets in the static data area.offsets in the static data area.

Page 5: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

StackStack

Stack--runtime stack of activation records Stack--runtime stack of activation records reflecting the stack structure of dynamic reflecting the stack structure of dynamic procedure calls and returns. An activation procedure calls and returns. An activation record contains the information needed by a record contains the information needed by a single procedure call. Local variables are single procedure call. Local variables are mapped to offsets in the activation record.mapped to offsets in the activation record.

Page 6: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

HeapHeap

Heap -- used to store all other program data (data Heap -- used to store all other program data (data that is dynamically sized or data with lifetime that is dynamically sized or data with lifetime pattern that cannot be represented in the run-pattern that cannot be represented in the run-time stack). Heap data allocation incurs more time stack). Heap data allocation incurs more overhead than static or stack data allocation. overhead than static or stack data allocation.

Page 7: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Languages and Storage Languages and Storage OrganizationOrganization

Fortran - StaticFortran - Static

Pascal - Stack/heapPascal - Stack/heap

C Stack/HeapC Stack/Heap

Page 8: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Public class X {Public class X {

public static void main(String argsv[]) {public static void main(String argsv[]) {

int n;int n;

n =10;n =10;

System.out.println(fib(n));System.out.println(fib(n));

}}

int fib(int n) {int fib(int n) {

if (n==0) return 1 else if (n==1) return 1 else if (n==0) return 1 else if (n==1) return 1 else return(fib(n-1)+fib(n-2)); }}return(fib(n-1)+fib(n-2)); }}

Activation RecordActivation Record

Page 9: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Local variables offsets are calculated depending Local variables offsets are calculated depending on its size. These offsets can be computed on its size. These offsets can be computed relative to the start of the stack frame, and can relative to the start of the stack frame, and can be used to specify the layout of local data in the be used to specify the layout of local data in the stack frame.stack frame.

Local variables accesses get translated to Local variables accesses get translated to negative-valued offsets on the stack pointer. negative-valued offsets on the stack pointer.

Compile Time Layout of Local Compile Time Layout of Local DataData

Page 10: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

An access to a lexically scoped nonlocal variable An access to a lexically scoped nonlocal variable gets translated to <level-count,frame-offset>.gets translated to <level-count,frame-offset>.

Level-count = k indicates that the variable can be Level-count = k indicates that the variable can be found in the kth enclosing scope.found in the kth enclosing scope.

Non-Local VariableNon-Local Variable

Page 11: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Variable/Field of Unknown Variable/Field of Unknown SizeSize

How do we allocate storage for a variable/ field How do we allocate storage for a variable/ field with statically unknown size e.g. an array or an with statically unknown size e.g. an array or an adt (abstract Data Type).adt (abstract Data Type).

By allocating storage separately (on the heap or By allocating storage separately (on the heap or on top of the AR on the stack) and storing a on top of the AR on the stack) and storing a pointer to the storage in the AR.pointer to the storage in the AR.

Page 12: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Other Basic Concepts in Other Basic Concepts in Runtime StructuesRuntime Structues

Call Sequence -- instruction Sequences that Call Sequence -- instruction Sequences that allocates AR for callee procedures and fills in allocates AR for callee procedures and fills in some of the fields.some of the fields.

Return Sequence- instruction sequence that Return Sequence- instruction sequence that restores machine state so that caller procedure restores machine state so that caller procedure can continue execution.can continue execution.

Lexical scope -- Nonlocal names are resolved via Lexical scope -- Nonlocal names are resolved via static nesting.static nesting.

Dynamic Scope: nonlocal names are resolved by Dynamic Scope: nonlocal names are resolved by inhering name bindings (following control links) inhering name bindings (following control links)

Page 13: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

ExampleExample

Page 14: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Parameter PassingParameter Passing

Call by value -- caller places r-value for actual Call by value -- caller places r-value for actual parameter in the storage formal parameter.parameter in the storage formal parameter.

Call by reference -- caller places l-value for actual Call by reference -- caller places l-value for actual parameter in the storage for formal parameter.parameter in the storage for formal parameter.

Call by value result -- caller places r-value for the Call by value result -- caller places r-value for the actual parameter in the storage for formal actual parameter in the storage for formal parameter and also determines the l-value of the parameter and also determines the l-value of the actual parameter. On return, the current r-value actual parameter. On return, the current r-value of the formal parameter is copied to the l-value of the formal parameter is copied to the l-value of the actual parameter.of the actual parameter.

Page 15: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

ExampleExample

Page 16: Run-Time Storage Organization 66.648 Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.

Comments and FeedbackComments and Feedback

Project 3 is out. Please start working. PLEASE do Project 3 is out. Please start working. PLEASE do not wait for the due date to come.not wait for the due date to come.

We are in chapter 8. Please read that chapter and We are in chapter 8. Please read that chapter and read the relevant portion of Java. Please keep read the relevant portion of Java. Please keep studying this material and work exercises.studying this material and work exercises.