
Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Names and Abstractions: What's in a Name? Names, Scopes, and Bindings Names enable programmers to refer to variables, constants, operations, and types using identifier names rather than Binding time low-level hardware addresses Object lifetime Names are also control and data abstractions of complicated Object storage management program fragments and data structures Static allocation Control abstraction: Stack allocation Subroutines (procedures and functions) allow programmers Heap allocation to focus on manageble subset of program text Scope rules Subroutine interface hides implementation details, e.g. Static and dynamic scoping sort(MyArray) Reference environments Data abstraction: Overloading Object-oriented classes hide data representation details behind a simple set of operations Abstraction in the context of high-level programming languages refers to the degree or level of language features Level of machine-independence "Power" of constructs Note: These slides cover Chapter 3 of the textbook, except Section 3.6. We encourage you to study Section 3.6 of the textbook, but you are not required to do so. You are not required to study Sections 3.3.3 and 3.3.4. Binding Time Language feature Binding time Syntax, e.g. if (a>0) b:=a; in C or Language design A binding is an association between a name and the thing that is if a>0 then b:=a end if in Ada named Keywords, e.g. class in C++ and Java Language design Binding time is the time at which an implementation decision is Reserved words, e.g. main in C and made to create a binding Language design writeln in Pascal 1. Language design time: the design of specific program constructs (syntax), primitive types, and meaning Meaning of operators, e.g. + (add) Language design (semantics) Primitive types, e.g. float Language design 2. Language implementation time: fixation of implementation and struct in C constants such as numeric precision, run-time memory sizes, Internal representation of literals, Language implementation max identifier name length, number and types of built-in e.g. 3.1 and "foo bar" exceptions, etc. The specific type of a variable in a Compile time 3. Program writing time: the programmer's choice of C or Pascal declaration algorithms and data structures Language design, 4. Compile time: the time of translation of high-level constructs Storage allocation method for a variable language implementation, to machine code and choice of memory layout for objects and/or compile time 5. Link time: the time at which multiple object codes (machine Linking calls to static library routines, code files) and libraries are combined into one executable Linker 6. Load time: the time at which the operating system loads the e.g. printf in C executable in memory Merging multiple object codes Linker 7. Run time: the time during which a program executes (runs) into one executable Loading executable in memory Loader (OS) and adjusting absolute addresses Binding Time Examples Nonstatic allocation of space for Run time variable The Effect of Binding Time Object Lifetime Early binding times (before run time) are associated with greater Key events in object lifetime efficiency Object creation Compilers try to fix decisions that can be taken at compile Creation of bindings time to avoid to generate code that makes a decision at run References to variables, subroutines, types are made using time bindings Syntax and static semantics checking is performed only once Deactivation and reactivation of temporarely unusable Late binding times (at run time) are associated with greater bindings flexibility Destruction of bindings Interpreters allow program to be extended at run time Destruction of objects Languages such as Smalltalk-80 with polymorphic types Binding lifetime: time between creation and destruction of allow variable names to refer to objects of multiple types at binding to object run time E.g. a Java reference variable is assigned the address of an Method binding in object-oriented languages must be late object E.g. a function's formal argument is bound to an actual argument (object) Object lifetime: time between creation and destruction of an object Object Lifetime Example Object Storage Management Example C++ fragment: An object has to be stored somewhere in memory during its { SomeClass& myobject = *new SomeClass; lifetime ... Static objects have an absolute storage address that is retained { OtherClass& myobject = *new OtherClass; throughout the execution of the program ... myobject // is bound to other object ... Global variables } Subroutine code ... myobject // is visible again Class method code ... delete myobject; Stack objects are allocated in last-in first-out order, usually in } conjunction with subroutine calls and returns Actual arguments of a subroutine Local variables of a subroutine Heap objects may be allocated and deallocated at arbitrary times, but require an expensive storage management algorithm E.g. Java class instances are always stored on the heap recursive subroutines Typical Program and Data Layout in Memory Every (recursive) subroutine call must have separate Program code is at the bottom of the memory region (code instantiations of local variables section) Fortran 77 has no recursion Static data objects are stored in static region (data section) Both global and local variables can be statically allocated as Stack grows downward (data section) decided by compiler Heap grows upward (data section) Avoids overhead of creation and destruction of local objects for every subroutine call Stack Typical static subroutine data memory layout: ¯ Temporaries - (for intermediate values) Heap Local Static variables data Miscellaneous Program bookkeeping code (saved processor registers) Return address The code section is protected from run-time modification Arguments (in and out) Static Allocation Program code is statically allocated in most implementations of imperative languages Statically allocated variables are history sensitive Global variables static local variables in C function retain value even after function returns Advantage of statically allocated object is the fast access due to absolute addressing of the object Static allocation does not work for local variables in potentialy Stack-Based Allocation Subroutine A Temporaries Each instance of a subroutine at run time has a frame on the Local vars run-time stack (also called activation record) Bookkeeping Compiler generates subroutine calling sequence to setup ¬ fp (frame pointer) Return address Example C program frame, call the routine, and to destroy the frame afterwards Subroutine prologue and epilogue code operate and maintain Arguments main() the frame { ... Subroutine B A(); Frame layouts vary between languages and implementations Temporaries ... Typical frame layout: } Local vars Temporaries Bookkeeping A() Local vars { ... Return address B(); Bookkeeping ... Arguments Return address } Arguments Subroutine A B() Temporaries { ... A frame pointer (fp) points to the frame of the currently active A(); subroutine at run time (always topmost frame on stack) Local vars ... Subroutine arguments, local variables, and return values are Bookkeeping } accessed by constant address offsets from fp Return address main calls A, The stack pointer (sp) points to free space on the stack Arguments A calls B, B calls A Main program Stack-Based Allocation Example Temporaries Local vars - (growth) ¬ sp (stack pointer) Bookkeeping Return address Arguments Example Frame Heap-Based Allocation The word sizes of the types of local variables and arguments Implicit heap allocation: determines the fp offset in a frame Java class instances are always placed on the heap Scripting languages and functional languages make Temporaries extensive use of the heap for storing objects Local vars Some procedural languages allow array declarations with var offset size run-time dependent array size foo -18 2 words Example Pascal procedure Resizable character strings Explicit heap allocation: bar -16 4 words procedure P(a:integer, var b:real) (* a is passed by value Statements and/or functions for allocation and deallocation p -12 2 words b is passed by reference, Heap allocation is performed by searching heap for available which is a pointer to b's value Bookkeeping *) free space (8 words) var foo:integer;(* integer: 2 words *) Object free (4 Object Object free (12 Object free (10 Return address bar:real; (* float: 4 words *) A words) B C words) D words) (2 words) p:^integer; (* pointer: 2 words *) begin ¬ fp ... Request allocation for object E of 10 words: Arguments end arg offset size Object E (10 words) a 0 2 words Deletion of objects leaves free blocks in the heap that can be b 2 2 words reused Internal heap fragmentation: If allocated object is smaller than The compiler determines the slots for the local variables and the free block the extra space is wasted arguments in a frame External heap fragmentation: Smaller free blocks cannot always The fp of the previous active frame is saved in the current frame be reused resulting in wasted space and restored after the call Heap Allocation Algorithms Garbage Collection Maintain a linked list of free heap blocks Explicit manual deallocation errors are among the most First-fit: select the first block that is large enough on the list of expensive and hard to detect problems in real-world applications free heap blocks If an object is deallocated too soon, a reference to the object Best-fit: search entire list for the smallest free block
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages10 Page
-
File Size-