Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Similar documents
Run-Time Environments

High-Level Language VMs

Compiling Techniques

Run-time Environments

Run-time Environments

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

CSc 453 Interpreters & Interpretation

Advanced Programming & C++ Language

Compiler construction 2009

Name, Scope, and Binding. Outline [1]

The Java Virtual Machine

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Compiler Construction

Run-Time Environments/Garbage Collection

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

In Java we have the keyword null, which is the value of an uninitialized reference type

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

G Programming Languages - Fall 2012

CS61C : Machine Structures

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Concepts Introduced in Chapter 7

Compiler Construction

Structure of Programming Languages Lecture 10

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

Intermediate Representations

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Lecture 13: Garbage Collection

Programming Language Implementation

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Heap Management. Heap Allocation

Contents. 8-1 Copyright (c) N. Afshartous

Programming Languages

Compiler Construction D7011E

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Run-time Environments - 3

CS558 Programming Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

JAM 16: The Instruction Set & Sample Programs

Memory Management: The Details

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CMSC 330: Organization of Programming Languages

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Lecture 3: C Programm

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

Running class Timing on Java HotSpot VM, 1

Topic 7: Activation Records

Names, Scope, and Bindings

Memory management COSC346

Dynamic Storage Allocation

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Java: framework overview and in-the-small features

G Programming Languages - Fall 2012

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

StackVsHeap SPL/2010 SPL/20

CSC 533: Organization of Programming Languages. Spring 2005

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

Run-time Environments -Part 3

MEMORY MANAGEMENT IN C++ AND JAVA

CMSC 330: Organization of Programming Languages

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Lecture 7: Binding Time and Storage

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

CS 241 Honors Memory

CS558 Programming Languages

CS558 Programming Languages

Managed runtimes & garbage collection

6. Names, Scopes, and Bindings

Short Notes of CS201

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CIT Week13 Lecture

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

CS201 - Introduction to Programming Glossary By

Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Compiler construction 2009

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Abstract Interpretation Continued

Dynamic Memory Management! Goals of this Lecture!

Course Administration

Dynamic Memory Allocation

Implementation Garbage Collection

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Course introduction. Advanced Compiler Construction Michel Schinz

Systems I. Machine-Level Programming V: Procedures

Transcription:

Project there are a couple of 3 person teams regroup or see me or forever hold your peace a new drop with new type checking is coming using it is optional 1

Compiler Architecture source code Now we jump over code generation parsing analysis code generation Abstract Syntax Tree compiled code runtime hardware 2

Overview of Runtime Lectures A compiler translates a program from a high-level language into an an corresponding program in in a low-level language. The low level program must correspond to the high-level program. => High-level concepts must be modeled in terms of the low-level machine. These lectures are not about compilation itself instead are about compiled code and how to use compiled code to implement high level constructs => We need to know this before we can talk about code generation. 3

Overview of Runtime Lectures Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) even x + y * z is surprisingly high-level Routines: How to implement procedures, operators (and how to pass their parameters and return values) Data Representation: how to represent values of the source language on the target machine. Storage Allocation: How to organize storage for variables (considering different lifetimes of global, local and heap variables) A runtime design involves all of these, in delicate balance. there is no forward reference free order to present this 4

Different Levels of Machines Java portable, objects, type checking, modules, lexical scoping, gc, tree structured code compile virtual machine??? interpret compile native binary machine-specific, flat code, flat memory 5

Different Levels of Machines Java portable, objects, type checking, modules, lexical scoping, gc, tree structured code compile virtual machine interpret JVM: GC, stack machine, flat code, type safe compile native binary machine-specific, flat code, flat memory 6

Examples: Virtual Machine Code P-code for early Pascal interpreters Smalltalk virtual machines Lisp Machine instruction set Java byte codes, CLR byte codes Advantages easy to generate byte code code is architecture independent code can be more compact Disadvantage true interpretation is slower 7

Example: Compiling Virtual Machine Code gcc works in two steps first compiles to RTL then compiles RTL to native code Advantages: exposes key properties of underlying architecture abstracts over true details of underlying architecture facilitates writing code generators for multiple target machines Disadvantages code bloat have to write code generator for each target architecture 8

Interpreting and Compiling Byte Code Modern virtual machines do both Interpret byte code Selectively compile byte-code to native machine code aka Just-in Time (JIT) compiler Attempt to get best of both worlds compact code and fast code Requires subtle decisions about when to compile Originally Smalltalk VM, later Self, now all VMs the Sun HotSpot VM uses just-in time compiling 9

Has: garbage-collected memory registers condition codes execution unit Java Virtual Machine 10

stack JVM memory method (procedure) call frames heap used for dynamically allocated memory (objects and arrays) constant pool shared constant data code segment JVM instructions of all loaded classes 11

JVM registers no general purpose registers byte code has limited control over registers stack pointer (sp) points to top of stack local stack pointer (lsp) points to location in current frame used for intermediate values in this stack frame program counter (pc) current instruction 12

JVM condition codes stores result of last instruction that sets condition codes used for branching 13

reads instruction at PC JVM execution unit decodes and executes instruction state of machine may change memory, registers, condition codes pc is incremented after executing instruction method calls and branches explicitly change pc 14

JVM stack frames locals reference to current object (this) method arguments local variables local stack for intermediate results returned from calls and operations ready to be passed to other calls and operations or ready to be stored in locals number of locals and size of local stack fixed at compile time 15

static void foo(int x, int y) { int c = x + y;... } picture from Artima.com iload_0 // push the int in local variable 0 iload_1 // push the int in local variable 1 iadd // pop two ints, add them, push result istore_2 // pop int, store into local variable 2 16

iadd Instruction (from http://mrl.nyu.edu/~meyer/jvmref/) (also see http://java.sun.com/docs/books/vmspec/2nd-edition/html/instructions2.doc.html) add two integers Stack Before After value1 result value2...... Description Pops two integers from the operand stack, adds them, and pushes the integer result back onto the stack. On overflow, iadd produces a result whose low order bits are correct, but whose sign bit may be incorrect. Example bipush 5 bipush 4 iadd ; push first int ; push second int ; add integers ; the top of the stack now ; contains the integer 9 17

public int abs(int x) { if( x < 0 ) return x * -1; else return x; Simple Java Method.method public abs(i)i // (int)->int.limit stack 2.limit locals 2 // --locals-- --stack-- iload_1 // [ ] [ ] ifge Label1 // [ ] [ ] iload_1 // [ ] [ ] iconst_m1 // [ ] [ ] imul // [ ] [ ] ireturn // [ ] [ ] Label1: iload_1 // ireturn //.end method 18

public int abs(int x) { if( x < 0 ) return x * -1; else return x; iload_1 iload 3 Simple Java Method.method public abs(i)i // (int)->int.limit stack 2.limit locals 2 // --locals-- --stack-- iload_1 // [ ] [ ] ifge Label1 // [ ] [ ] iload_1 // [ ] [ ] iconst_m1 // [ ] [ ] imul // [ ] [ ] ireturn // [ ] [ ] Label1: iload_1 // ireturn //.end method 19

public int abs(int x) { if( x < 0 ) return x * -1; else return x; iload_1 iload 3 Simple Java Method.method public abs(i)i // (int)->int.limit stack 2.limit locals 2 // --locals-- --stack-- iload_1 // [ o -3 ] [ -3 * ] ifge Label1 // [ o -3 ] [ * * ] iload_1 // [ o -3 ] [ -3 * ] iconst_m1 // [ o -3 ] [ -3-1 ] imul // [ o -3 ] [ 3 * ] ireturn // [ o -3 ] ---------- Label1: iload_1 // ireturn //.end method 20

Sketch of Interpreter pc = code.start; while( true ) { npc = pc + instructionlength(code[pc]); switch( opcode(code[pc]) ) { case ILOAD_1: push(local[1]); break; case ILOAD: push(local[code[pc+1]]); break; case ISTORE: t = pop(); local[code[pc+1]] = t; break; case IADD: } case IFEQ: t = pop(); if( t == 0 ) npc = code[pc+1]; break;... } pc = npc; 21

what happens to stack 22

23

24

25

26

27

28

29

Overview of Runtime Lectures Expression Evaluation: organize computing values of expressions taking care of intermediate results Routines: implement procedures, operators pass parameters and return values Data Representation: represent values of source language Storage Allocation: organize storage for variables (considering different lifetimes of global, local and heap variables) JVM is a stack machine typed instructions garbage collected object-level memory How does this answer these? 30

static void foo(int x, int y) { int c = x + y;... } picture from Artima.com iload_0 // push the int in local variable 0 iload_1 // push the int in local variable 1 iadd // pop two ints, add them, push result istore_2 // pop int, store into local variable 2 Operands passed on stack (most) operators single JVM instructions 31

picture from Artima.com public static int runclassmethod(int i, long l, float f, double d, Object o, byte b) { return 0; } public int runinstancemethod(char c, double d, short s, boolean b) { return 0; } 32

compiler MAY share space for locals public static void runtwoloops() { for (int i = 0; i < 10; ++i) { System.out.println(i); } } for (int j = 9; j >= 0; --j) { System.out.println(j); } 33

34

35

36

public static void addandprint() { double result = addtwotypes(1, 88.88); System.out.println(result); } public static double addtwotypes(int i, double d) { return i + d; } 37

picture from Artima.com 38

public static void foo() { int x = 1, y = 2; System.out.println(bar(x, y)); } public static int bar(int i, int j) { int result = i + j; return result; } 1 2 1 2 1 2 1 2 fp sp pc 1 2 3 fp sp pc 3 3 39

Sketch of Interpreter int fp, sp, pc; invoke(..find the main routine..) while( true ) { npc = pc + instructionlength(code[pc]); switch( opcode(code[pc]) ) { case ILOAD_1: push(local[1]); break; case ILOAD: push(local[code[pc+1]]); break; case ISTORE: t = pop(); local[code[pc+1]] = t; break; case IADD: } case IFEQ: t = pop(); if( t == 0 ) npc = code[pc+1]; break;... } pc = npc; 40

Overview of Runtime Lectures Expression Evaluation: organize computing values of expressions taking care of intermediate results Routines: implement procedures, operators pass parameters and return values Data Representation: represent values of source language Storage Allocation: organize storage for variables (considering different lifetimes of global, local and heap variables) JVM is a stack machine typed instructions garbage collected object-level memory 41

Register Machines x + a * b iload_1 iload_2 iload_3 imult iadd ld [fp-32], r0 ld [fp-64], r1 mul(r0, r1, r0) ld [fp-0], r1 add(r0, r1, r1) fixed set of general-purpose registers compiled code reads/writes to them explicitly registers are simple and fast - a push requires write, read/+/write opportunity to call routines in registers - still call procedures on a stack 42

Heap Storage Allocation RECAP: we have mentioned 2 storage allocation models so far: heap allocation objects exist forever stack allocation local variables and arguments for procedures (methods) lifetime follows procedure activation NEXT: Heap allocation allocation of indefinite lifetime values (C malloc, C++ / Pascal / Java new operation). Example: (Java) int[] nums = new int[computedneededsize]; 43

Heap Storage Allocation Explicit versus Implicit Deallocation In explicit memory management, the program must explicitly call an operation to release memory back to the memory management system. In implicit memory management, heap memory is reclaimed automatically by a garbage collector. Examples: Implicit: Java, Scheme Explicit: Pascal and C To free heap memory a specific operation must be called. Pascal ==> dispose C ==> free 44

Heap Storage Allocation Memory managers (both implicit as well explicit) have been studied extensively. => algorithms for fast allocation / deallocation of memory and efficient representation (low memory overhead for memory management administration). => There are many complicated, sophisticated and interesting algorithms. We could dedicate an entire course to this topic alone! We will look at memory management only superficially. Maybe at the end of the course we might cover some algorithms in detail. Now we will look at superficially at the kind of algorithms/issues are associated with explicit and implicit memory management. 45

Where to put the heap? The heap is an area of memory which is dynamically allocated. Like a stack, it may grow and shrink during runtime. (Unlike a stack it is not a LIFO => more complicated to manage) In a typical programming language implementation we will have both heap-allocated and stack allocated memory coexisting. Q: How do we allocate memory for both A simple approach is to divide the available memory at the start of the program into two areas: stack and heap. Another question then arises => - How do we decide what portion to allocate for stack vs. heap? => Issue: if one of the areas is full, then even though we still have more memory (in the other area) we will get out-of-memory errors. Q: Isn t there a better way? 46

Where to put the heap? Q: Isn t there a better way? A: Yes, there is an often used trick : let both stack and heap share same memory area, but grow towards each other from opposite ends. SB ST Stack memory area Stack grows downward HT Heap can expand upward Heap memory area HB 47

How to keep track of free memory? Stack is LIFO allocation => ST moves up/down everything above ST is in use/allocated. Below is free memory. This is easy! But Heap is not LIFO, how to manage free space in the middle of the heap? SB Allocated HT Free ST Free reuse? Mixed: Allocated and Free HB 48

How to keep track of free memory? How to manage free space in the middle of the heap? => keep track of free blocks in a data structure: the free list. For example we could use a linked list pointing to free blocks. freelist Where should free HT Free Next list list go? Free Next HB Free Next 49

Thread free-list through the blocks Q: Where do we find the memory to store a freelist data structure? A: Since the free blocks are not used for anything by the program => memory manager can use them for storing the freelist itself. HT HF HF free block size next free HB 50

Simple Explicit Memory Manager Algorithm Our memory manager is intended for a programming language with explicit deallocation of memory. Q: What operations does the memory management library provide? Pointer malloc(int size); ask memory manager to find and allocate a block of size. Returns a pointer to beginning of that block. void free(pointer tofree,int size); called when a block tofree is released by the program and returned to the memory manager. 51

Simple Explicit Memory Manager Algorithm Pointer malloc(int size) { block = search the freelist until a block of at least size is found. if (block is found) and (block.size == size) { delete block from free list. return pointer to start of block. else if (block found) and (block.size>size) { delete block from free list. insert remainder of block into free list return pointer to start of block. else // no block found try to expand heap by size if (expansion succeeded) then return HT; else out of memory error } 52

Simple Explicit Memory Manager Algorithm void free(pointer tofree,int size) { insert block(tofree,size) into the freelist } A bit more detail: void free(pointer tofree,int size) { tofree[0] = size; // size at offset 0 tofree[1] = HF; // next at offset 1 HF = tofree; } This algorithm is rather simplistic and has some rather big problems. Q: What are those problems? How could we fix them? 53

Fragmentation One of the biggest problems with the simplistic algorithm is memory fragmentation. blocks are split to create smaller blocks to fit requested size. blocks are never merged blocks will get smaller and smaller. will run out of memory without really being out. Better algorithm merges released blocks with neighboring blocks that are free. => less fragmentation Q1: Analyze impact on the performance (time taken for) the allocation and release of blocks. Q2: Does this solve the fragmentation problem completely? 54

Heap Compaction To fight fragmentation, some memory management algorithms perform heap compaction once in a while. Q: Can compaction be done for all programming languages? before after HT HF a b HT a HB c d HB b c d 55

Complication with Explicit Memory Allocation Problems with explicit memory allocation algorithms: Two common, nasty kinds of bugs in programs using explicit memory management are Memory leaks Dangling pointers Q1: What are memory leaks and dangling pointers? Q2: Is it true that there can be no memory leaks in a garbagecollected language? Q3: Is it true that there can be no dangling pointer in a garbagecollected language. Q4: What is a weak reference? 56

Automatic Storage Deallocation Everybody probably knows what a garbage collector is. Find data (objects) which are no longer referenced. Reclaim that memory. 57

Mark and Sweep Garbage Collection SB before gc mark as free phase SB ST ST HT e HT X e d X d c b X XX c b HB a HB a 58

Mark and Sweep Garbage Collection mark as free phase SB mark reachable SB collect free SB ST ST ST HT X e HT X e HT X e X d X d X d HB X XX c b a HB X X X c b a HB X b 59

Mark and Sweep Garbage Collection Algorithm pseudo code: void garbagecollect() { mark all heap variables as free for each frame in the stack scan(frame) for each heapvar (still) marked as free add heapvar to freelist } void scan(region) { for each pointer p in region if p points to region marked as free then mark region at p as reachable scan(region at p ) } Q: This algorithm is recursive. What do you think about that? 60

Incremental Other Garbage Collection Schemes do a little at a time Generational young objects a more likely to be garbage Copying move (and compact) objects during collection Program directed programmer suggests what pattern object lifetime will follow 61