Compilation 0368-3133 Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1.

Slides:



Advertisements
Similar presentations
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Compilation (Semester A, 2013/14) Lecture 14: Compiling Object Oriented Programs Noam Rinetzky Slides credit: Mooly Sagiv 1.
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Linking & Loading CS-502 Operating Systems
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
Compiling Object Oriented Programs Mooly Sagiv Chapter
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.
Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Compiler Summary Mooly Sagiv html://
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Assembler, Linker and OO Paradigm Professor Yihjia Tsai Tamkang University.
Compiling Object Oriented Programs Mooly Sagiv Chapter 6.2.9, 6.5.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
MIPS coding. SPIM Some links can be found such as:
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Basic Semantics Associating meaning with language entities.
Topic 2d High-Level languages and Systems Software
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
COMP3190: Principle of Programming Languages
CS 241 Spring 2007 System Programming 1 Introduction to Memory Management Lecture 28 Lawrence Angrave/Klara Nahrstedt.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
CSc 453 Linking and Loading
Memory Management Chapter 5 Advanced Operating System.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Design issues for Object-Oriented Languages
Lecture 3 Translation.
Assemblers, linkers, loaders
Computer Architecture & Operations I
Names and Attributes Names are a key programming language feature
COMBINED PAGING AND SEGMENTATION
Linking & Loading.
Chapter 8 Main Memory.
Program Execution in Linux
CS-3013 Operating Systems C-term 2008
PZ09A - Activation records
Linking & Loading CS-502 Operating Systems
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
Presentation transcript:

Compilation Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1

Compiling OO Programs 2

Features of OO languages  Inheritance  Subclass gets (inherits) properties of superclass  Method overriding  Multiple methods with the same name with different signatures  Abstract (aka virtual) methods  Polymorphism  Multiple methods with the same name and different signatures but with different implementations  Dynamic dispatch  Lookup methods by (runtime) type of target object 3

Compiling OO languages  “Translation into C”  Powerful runtime environment  Adding “gluing” code 4

Runtime Environment  Mediates between the OS and the programming language  Hides details of the machine from the programmer  Ranges from simple support functions all the way to a full-fledged virtual machine  Handles common tasks  Runtime stack (activation records)  Memory management  Runtime type information  Method invocation  Type conversions 5

Handling Single Inheritance  Simple type extension 6 class A { field a1; field a2; method m1() {…} method m2() {…} } class B extends A { field b1; method m3() {…} }

Adding fields Fields aka Data members, instance variables  Adds more information to the inherited class  “Prefixing” fields ensures consistency 7 class A { field a1; field a2; method m1() {…} method m2() {…} } class B extends A { field b1; method m2() {…} method m3() {…} } typedef struct { field a1; field a2; } A; void m1A_A(A* this){…} void m2A_A(A* this){…} typedef struct { field a1; field a2; field b1; } B; void m2A_B(B* this) {…} void m3B_B(B* this) {…}

Method Overriding  Redefines functionality  More specific  Can access additional fields 8 class A { field a1; field a2; method m1() {…} method m2() {…} } class B extends A { field b1; method m2() { … b1 … } method m3() {…} }

Handling Polymorphism  When a class B extends a class A  variable of type pointer to A may actually refer to object of type B  Upcasting from a subclass to a superclass  Prefixing fields guarantees validity 9 class B *b = …; class A *a = b ; a1 a2 b1 Pointer to B Pointer to A inside B (also) classA *a = convert_ptr_to_B_to_ptr_A(b) ; A B

Dynamic Binding  An object (“pointer”) o declared to be of class A can actually be (“refer”) to a class B  What does ‘o.m()’ mean?  Static binding  Dynamic binding  Depends on the programming language rules  How to implement dynamic binding?  The invoked function is not known at compile time  Need to operate on data of the B and A in consistent way 10

Virtual function table 11 typedef struct { field a1; field a2; } A; void m1A_A(A* this){…} void m2A_A(A* this, int x){…} typedef struct { field a1; field a2; field b1; } B; void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … } void m3B_B(B* this){…} p.m2(3); p  dispatch_table  m2A(, 3); a1 a2 Runtime object b1 vtable p convert_ptr_to_B_to_ptr_to_A(p) m1A_A m2A_B (Runtime) Dispatch Table m3B_B

Multiple Inheritance 12 class C { field c1; field c2; method m1(){…} method m2(){…} } class D { field d1; method m3() {…} method m4(){…} } class E extends C, D { field e1; method m2() {…} method m4() {…} method m5(){…} }

Multiple Inheritance  Allows unifying behaviors  But raises semantic difficulties  Ambiguity of classes  Repeated inheritance  Hard to implement  Semantic analysis  Code generation  Prefixing no longer work  Need to generate code for downcasts  Hard to use 13

A simple implementation  Merge dispatch tables of superclases  Generate code for upcasts and downcasts 14

A simple implementation 15 class C { field c1; field c2; method m1(){…} method m2(){…} } class D { field d1; method m3() {…} method m4(){…} } class E extends C, D { field e1; method m2() {…} method m4() {…} method m5(){…} } d1 e1 Runtime object m3D_D m4D_E (Runtime) Dispatch Table m5E_E vtable c1 c2 vtable Pointer to - E - C inside E Pointer to - D inside E m1C_C m2C_E

Dependent multiple Inheritance 16 class C extends A { field c1; field c2; method m1(){…} method m2(){…} } class D extends A { field d1; method m3(){…} method m4(){…} } class E extends C, D { field e1; method m2() {…} method m4() {…} method m5(){…} } class A{ field a1; field a2; method m1(){…} method m3(){…} }

Interface Types  Java supports limited form of multiple inheritance  Interface consists of several methods but no fields  A class can implement multiple interfaces Simpler to implement/understand/use 17 public interface Comparable { public int compare(Comparable o); }

Interface Types  Implementation: record with 2 pointers:  A separate dispatch table per interface  A pointer to the object 18 a1 a2 interface table vtable i. table object vtable field1 field2 a1 b1 a2

Assembly  Image Linker Loader Assembler Compiler Source file (e.g., utils) Assembly (.s) Executable (“.elf”) Image (in memory): Assembler Compiler Source file (e.g., main) Assembly (.s) Assembler Compiler library Assembly (.s) Object (.o)

Assembler  Converts (symbolic) assembler to binary (object) code  Object files contain a combination of machine instructions, data, and information needed to place instructions properly in memory  Yet another(simple) compiler  One-to one translation  Converts constants to machine repr. (3  0…011)  Resolve internal references  Records info for code & data relocation

Object File Format  Header: Admin info + “file map”  Text seg.: machine instruction  Data seg.: (Initialized) data in machine format  Relocation info: instructions and data that depend on absolute addresses  Symbol table: “exported” references + unresolved references HeaderText Segment Data Segment Relocation Information Symbol Table Debugging Information

Resolving Internal Addresses  Two scans of the code  Construct a table label  address  Replace labels with values  One scan of the code (Backpatching)  Simultaneously construct the table and resolve symbolic addresses  Maintains list of unresolved labels  Useful beyond assemblers

Handling External Addresses  Record symbol table in “external” table  Exported (defined) symbols  G, foo()  Imported (required) symbols  Extern_G, extern_bar(), printf()  Relocation bits  Mark instructions that depend on absolute (fixed) addresses  Instructions using globals,

Linker  Merges object files to an executable  Enables separate compilation  Combine memory layouts of object modules  Links program calls to library routines  printf(), malloc()  Relocates instructions by adjusting absolute references  Resolves references among files

Linker Code Segment 1 Data Segment 1 Code Segment 2 Data Segment ext_bar() 380 ext_bar 150 zoo 180 Data Segment 1 Code Segment 2 Data Segment ext_bar 250 zoo Code Segment 1 foo

External References  The code may include references to external names (identifiers)  Library calls  External data  Stored in external symbol table

Example of External Symbol Table

Example

Loader (Summary)  Initializes the runtime state  Part of the operating system  Privileged mode  Does not depend on the programming language  “Invisible activation record”

Dynamic Linking  Why dynamic linking?  Shared libraries  Save space  Consistency  Dynamic loading  Load on demand

Sharing code 31 a.o b.o libX.o GOT libY.o GOT

Sharing code 32 a.o b.o libX.o GOT libY.o GOT libX.o GOT libY.o GOT

Sharing code 33 a.o b.o libX.o GOT libY.o GOT libX.o GOT libY.o GOT libX.o GOT libY.o GOT

Position-Independent Code (PIC)  Code which does not need to be changed regardless of the address in which it is loaded  Enable loading the same object file at different addresses  Thus, shared libraries and dynamic loading  “Good” instructions for PIC: use relative addresses  relative jumps  reference to activation records  “Bad” instructions for : use fixed addresses  Accessing global and static data  Procedure calls  Where are the library procedures located?

ELF-Position Independent Code  Executable and Linkable code Format  Introduced in Unix System V  Observation  Executable consists of code followed by data  The offset of the data from the beginning of the code is known at compile-time GOT (Global Offset Table) Data Segment Code Segment XX0000 call L2 L2: popl %ebx addl $_GOT[.-..L2], %ebx

ELF: Accessing global data

ELF: Calling Procedures (before 1st call)

ELF: Calling Procedures (after 1st call)

Memory Management  Manual memory management  Automatic memory management 39

 A data structure records the location and size of free cells of memory.  The allocator considers each free cell in turn, and according to some policy, chooses one to allocate.  Three basic types of free-list allocation:  First-fit  Next-fit  Best-fit Free-list Allocation

Memory chunks 41

Free list 42

free  Free too late – waste memory (memory leak)  Free too early – dangling pointers / crashes  Free twice – error 43

Garbage collection  approximate reasoning about object liveness  use reachability to approximate liveness  assume reachable objects are live  non-reachable objects are dead 44

Garbage Collection – Classical Techniques  reference counting  mark and sweep  copying 45

GC using Reference Counting  add a reference-count field to every object  how many references point to it  when (rc==0) the object is non reachable  non reachable => dead  can be collected (deallocated) 46

The Mark-and-Sweep Algorithm [McCarthy 1960]  Marking phase  mark roots  trace all objects transitively reachable from roots  mark every traversed object  Sweep phase  scan all objects in the heap  collect all unmarked objects 47

Mark&Sweep in Depth mark(Obj)= if mark_bit(Obj) == unmarked mark_bit(Obj)=marked for C in Children(Obj) mark(C)  How much memory does it consume?  Recursion depth?  Can you traverse the heap without worst-case O(n) stack?  Deutch-Schorr-Waite algorithm for graph marking without recursion or stack (works by reversing pointers) 48

Copying GC  partition the heap into two parts  old space  new space  Copying GC algorithm  copy all reachable objects from old space to new space  swap roles of old/new space 49

Example oldnew Roots A D C B E 50

Example oldnew Roots A D C B E A C 51