Storage Allocation Mechanisms

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
Intro to Computer Architecture
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Chapter 8 :: Subroutines and Control Abstraction
COP4020 Programming Languages
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Basic Semantics Associating meaning with language entities.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CSC 8505 Compiler Construction Runtime Environments.
Lecture 19: Control Abstraction (Section )
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Implementing Subprograms Chapter 10
Computer Science 210 Computer Organization
CS 326 Programming Languages, Concepts and Implementation
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Topic 3-b Run-Time Environment
Binding Times Binding is an association between two things Examples:
Name Binding and Object Lifetimes
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Run-time environments
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Storage Allocation Mechanisms Module 12.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Topics Types of storage allocation: Static Stack-based Frame management Heap-based

Storage Allocation Mechanisms Three main storage allocation mechanisms: Static allocation. Objects retain absolute address throughout. Examples: global variables, literal constants: "abc", 3. Stack-based allocation: Object addresses relative to a stack (segment) base, usually in conjunction with fcn/proc calls. Heap-based allocation. Objects allocated and deallocated at programmer's discretion. We’ll discuss each in turn.

Static Allocation Special case: No recursion. Original Fortran, most BASICs. Variables local to procedures allocated statically, not on a stack. Procedures can share their local variables ! No more since Fortran 90.

Stack-Based Allocation Necessary to implement recursion. Storage is allocated/deallocated/managed on a stack. Each subroutine that is called gets a stack frame/activation record on top of the stack. A frame contains: Arguments that are passed into the subroutine. Bookkeeping info (caller return address, saved registers). Local variables / temporaries. Frame organization varies by language and implementation.

Frame Management Responsibility shared between caller and callee: Prologue (caller): Push arguments and return address onto stack, save return address, change program counter, change SP, save registers, jump to target. During function (callee): make space for local variables, do the work, jump to saved return address. Epilogue (caller): Remove return address and parameters from the stack, store SP, restore registers, and continue.) Calling sequence varies by processor, and by compiler.

Frame management – general scheme int g; main() {A();} fcn A() {A(); B();} proc B() {C();} proc C() {} bp: Base pointer: global references. fp: Frame pointer: local references. sp: Stack pointer: Top of stack.

Sample Frame management int g=2; //g: bp+0 main() { int m=1; //m: fp+0 print(A(m)); //ret add 1 } int A(int p) { //p: fp+1 int a; //a: fp+3 if p=1 return 1+A(2); //ret add 2 else return B(p+1); //ret add 3 int B(int q) { //q: fp+1 int b=4; //b: fp 3 print(q+b+g); // HERE

Heap-Based Allocation Heap: Memory market. Memory region where memory blocks can be bought and sold (allocated and deallocated). Many strategies for managing the heap. Main problem: fragmentation. After many allocations and deallocations, many small free blocks scattered and intermingled with used blocks.  Allocation request Heap

Heap-Based Allocation Allocation is usually explicit in PL's: malloc, new. Strategies for fragmentation: Compaction. Expensive. Internal fragmentation: Allocate larger block than needed. Space wasted. External fragmentation: Can't handle a request for a large block. Plenty of free space, but no large blocks available.

External fragmentation Use a linked list of free blocks. First fit strategy: allocate first block that suffices. More efficient, but more fragmentation. Best fit strategy: allocate smallest block that suffices. Less efficient, less fragmentation. Maintain "pools" of blocks, of various sizes. "Buddy system": blocks of size 2k. Allocate blocks of nearest power of two. If none available, split up one of size 2k+1. When freed later, "buddy it" back, if possible. Fibonacci heap: Use block sizes that increase as Fibonacci numbers do: f(n)=f(n-1)+f(n-2), instead of doubling.

Heap Deallocation Implicit: Garbage Collection (Java). Automatic, but expensive (getting better). Explicit: free (C, C++), dispose (Pascal). Risky. Very costly errors, memory leaks, but efficient.

summary Types of storage allocation: Static Stack-based Frame management Heap-based