Lecture 6: HLL VM - I

Similar documents
Cloud Computing. Up until now

picojava TM : A Hardware Implementation of the Java Virtual Machine

1 The Java Virtual Machine

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

General Introduction

Chapter 7D The Java Virtual Machine

Introduction to Virtual Machines

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Instruction Set Design

02 B The Java Virtual Machine

The Java Virtual Machine (JVM) Pat Morin COMP 3002

Jonathan Worthington Scarborough Linux User Group

A Unified View of Virtual Machines

Habanero Extreme Scale Software Research Project

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Fachbereich Informatik und Elektrotechnik SunSPOT. Ubiquitous Computing. Ubiquitous Computing, Helmut Dispert

- Applet java appaiono di frequente nelle pagine web - Come funziona l'interprete contenuto in ogni browser di un certo livello? - Per approfondire

Instruction Set Architecture (ISA)

The Design of the Inferno Virtual Machine. Introduction

Virtual Machines. Virtual Machines

Restraining Execution Environments

C# and Other Languages

System Structures. Services Interface Structure

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

CSCI E 98: Managed Environments for the Execution of Programs

Multi-core Programming System Overview

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Caml Virtual Machine File & data formats Document version: 1.4

Instruction Set Architecture

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

Memory management basics (1) Requirements (1) Objectives. Operating Systems Part of E1.9 - Principles of Computers and Software Engineering

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Stack Allocation. Run-Time Data Structures. Static Structures

The Hotspot Java Virtual Machine: Memory and Architecture

CHAPTER 7: The CPU and Memory

Chapter 3: Operating-System Structures. Common System Components

Lecture 1 Introduction to Android

Let s put together a Manual Processor

Java's garbage-collected heap

An Overview of Stack Architecture and the PSC 1000 Microprocessor

Lecture 32: The Java Virtual Machine. The Java Virtual Machine

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

LSN 2 Computer Processors

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CS5460: Operating Systems

Garbage Collection in the Java HotSpot Virtual Machine

Chapter 6, The Operating System Machine Level

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Hardware/Software Co-Design of a Java Virtual Machine

Section 1.4. Java s Magic: Bytecode, Java Virtual Machine, JIT,

Computer Organization and Architecture

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Sources: On the Web: Slides will be available on:

İSTANBUL AYDIN UNIVERSITY

An Overview of Java. overview-1

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Java Virtual Machine Locks

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Virtual Machines. Adapted from J.S. Smith and R. Nair, VIRTUAL MACHINES, Morgan-Kaufmann Teodor Rus.

Real-time Java Processor for Monitoring and Test

X86-64 Architecture Guide

12. Introduction to Virtual Machines

Virtualization. Pradipta De

Java Garbage Collection Basics

Mobile Application Languages XML, Java, J2ME and JavaCard Lesson 04 Java

Instrumentation Software Profiling

HVM TP : A Time Predictable and Portable Java Virtual Machine for Hard Real-Time Embedded Systems JTRES 2014

MICROPROCESSOR AND MICROCOMPUTER BASICS

Java Virtual Machine, JVM

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

Suh yun Ki m (KIS T) (KIS suhyunk@.com

Practical Performance Understanding the Performance of Your Application

C Compiler Targeting the Java Virtual Machine

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.


Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Crash Course in Java

Chapter 3 Operating-System Structures

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

CSC 8505 Handout : JVM & Jasmin

1. Overview of the Java Language

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

2 Introduction to Java. Introduction to Programming 1 1

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

Chapter 2 System Structures

Armed E-Bunny: A Selective Dynamic Compiler for Embedded Java Virtual Machine Targeting ARM Processors

OPERATING SYSTEM - MEMORY MANAGEMENT

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Microprocessor & Assembly Language

CPU Organization and Assembly Language

Under the Hood: The Java Virtual Machine. Lecture 24 CS 2110 Fall 2011

Transcription:

CSCI-GA.3033-015 Virtual Machines: Concepts & Applications Lecture 6: HLL VM - I Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Disclaimer: Many slides of this lecture are based on the slides of authors of the textbook from Elsevier. All copyrights reserved.

What are we talking about? Traditional process VMs are afterthoughts How about if we design a special guest ISA/system interface: With portability as the main goal define an abstract interface that can be supported by all conventional OSes. Reflects important features of specific HLL or class of HLLs. Simplifies compilation This is why we call them HLL VMs

HLL VM is similar to Process VM BUT ISA defined for user-mode programs only ISA not designed for real hardware Only to be executed on virtual processor Referred to as virtual-isa or V-ISA System interface is a set of standardized APIs

HLL VMs from language/compiler perspective Goal: complete platform independence for applications Virtual instruction set + libraries Instead of ISA and OS interface HLL Program HLL Program Compiler front-end Compiler Intermediate Code Object Code ( ISA ) Memory Image Compiler back-end Loader Portable Code ( Virtual ISA ) Virt. Mem. Image Host Instructions VM loader VM Interpreter/Translator Traditional HLL VM

P-Code VM Popularized HLL VMs Provided highly portable version of Pascal Consists of Primitive libraries Machine-independent object file format A set of byte-oriented pseudo-codes Virtual machine definition of pseudo-code semantics 5

P-Code VM function value MP Instruction set Stack oriented Stack Frame is part of VM definition MP: Mark Pointer EP: Extreme Pointer NP: New Pointer SP: Stack Pointer MP EP NP stack frame stack frame heap constant area static link dynamic link previous EP return address parameters locals operand stack mark stack SP EP lodi 0 3 // load variable from current frame (nest 0 depth), // offset 3 from top of mark stack. ldci 1 // push constant 1 addi // add stri 0 3 // store variable back to location 3 of current frame

P-Code VM Advantages Porting is simplified Don t have to develop compilers for all platforms VM implementation is smaller/simpler than a compiler VM provides concise definition of semantics Through interpretation, startup time is reduced Generic I/O and Memory interface Tended to be least common denominator Relatively weak I/O capabilities

Modern HLL VMs Superficially similar to P-code scheme Stack-oriented ISA Standard libraries Network Computing Environment Untrusted software (this is the internet, after all) Robustness (generally a good idea) object-oriented programming Bandwidth is a consideration Good performance must be maintained Two major examples Java VM Microsoft Common Language Infrastructure (CLI)

Modern HLL VMs Compiler forms program files (e.g. class files) Standard format Program files contain both code and metadata Machine Independent Program File Metadata Loader Virtual Machine Implementation Internal Data Structures Machine Dependent Interpreter Code Translator Native Code

Terminology Java Virtual Machine Architecture CLI Analogous to an ISA Java Virtual Machine Implementation CLR (Common Language Runtime) Analogous to a computer implementation Java bytecodes Microsoft Intermediate Language (MSIL), CIL, IL The instruction part of the ISA Java Platform.NET framework ISA + Libraries; a higher level ABI

4 Characteristics of HLL VMs Security Robustness Networking Performance

Security A key aspect of modern network-oriented VMs Must protect: Local files and resources Runtime from user process The program runs in a sandbox at the host machine. It is managed by the VM runtime. The ability to load an untrusted application and run it in a managed secure fashion is a very big challenge! Public File application VMM User Process Remote System Network Local System Other File Accessible Local File Other Local File Sandbox Boundary

Protection Sandbox Remote resources Protected by remote system Local resources Protected by security manager VM software Protected via static/dynamic checking class file class file class file Network, File System lib. method loaded method loaded method lib. method loaded method class file loaded method loaded method local file local file native method standard libraries trusted native method loaded method security agent trusted Emulation Engine trusted loader trusted

Robustness: Object-Orientation Objects Data carrying entities Dynamically allocated Must be accessed via pointers or references Methods Procedures that operate on objects Class A type of object and its associated methods Object created at runtime is an instance of the class Data associated with a class may be dynamic or static OO programming paradigm has become the model of choice for modern HLL VMs. Both Java and CLI are designed to support OO software.

Networking The application must use the available bandwidth (scarce) efficiently Application loaded incrementally dynamic linking Improves program startup-time

Performance Of course we sacrifice some performance for the sake of portability Yet, we can use the techniques we learned so far (and some more as we proceed) to ensure good performance.

Java Virtual Machine (JVM) Source: http://i.stack.imgur.com/deo2s.png

Data items Types are defined, but not implementation details Reference types (pointers): number of bits needed is not part of Java ISA. Primitive types, e.g. int, char, byte, short, long, float, double, Another primitive type: ReturnAddress (not in Java HLL but in Java ISA) Exact sizes of data types are not given Only the range of values that can be held e.g. byte is between 128 and +127

Objects and Arrays Objects: Logical structure, defined by programmer, to carry data Composed of primitive data types and references Array Fixed number of elements All elements must be of the same type If the elements are references then they must all point to objects of the same type

Data Storage Types Global the main memory where globally declared variables reside Local temporary storage for variables local to a method Operand holds variables while they are being operated on by functional instructions Allocated on the stack

Data Storage Types All storage is divided into cells or slots A cell/slot usually holds a single data item Actual amount of bits needed for cell/slot is implementation dependent

Stack Arguments Locals Operands In that order As each method is called, a stack frame is allocated. Locals Operands Arguments Locals Operands Of fixed size determined at compile time Arguments Locals Operands

Global Memory Method area for holding code Global storage area for holding arrays and objects managed as a heap of unspecified size with respect to JVM architecture Can contain both static and dynamic objects

Heap Objects are created on heap Each application has only one Each application has its own JVM instructions allocate objects on heap No instruction to release memory Garbage collection is part of implementation Object representation is implementation dependent

Constant Pool ISA allows constants to be expressed in the instruction as immediate operands But some constants: are used by several instructions are of different ranges So: Constant data associated with a program is placed in a block called constant pool Instructions access them by indexing constant pool Constant pool then: defined as part of the ISA Exact size of constants is specified Does not change with program execution

Putting is All Together: Memory Hierarchy in JVM CONSTANT POOL HEAP Array index Object Instruction stream opcode operand operand opcode opcode operand opcode operand operand opcode opcode operand opcode opcode operand index index implied implied STACK FRAME Locals Operands Object Object

Network Friendliness Support dynamic class file loading on demand Load only classes that are needed Spread loading out over time Compact instruction encoding Use stack-oriented ISA (as in Pascal)

Garbage Collected Heap Objects are created and float in memory space Tethered by references In architecture, memory is unbounded in size In reality it is limited Garbage creation During program execution, many objects are created then abandoned (become garbage) Collection Due to limited memory space, Garbage should be collected so memory can be re-used Forcing programmer to explicitly free objects places more burden on programmer Can lead to memory leaks, reducing robustness To improve robustness, have VM collect garbage automatically

Instruction Set Stack based Defined for class file, not memory image Bytecodes One byte opcode Zero or more operands Opcode indicates how many Can take operands from Instruction Current constant pool Current frame local variables Values on operand stack Distinguish storage types and computation types opcode opcode index opcode index1 index2 opcode data opcode data1 data2 index into constant pool or into local storage

Implied Registers Program Counter Local variable pointer Operand stack pointer Current frame pointer Constant pool base

Instruction Types Pushing constants onto the stack Moving local variable contents to and from the stack Managing arrays Generic stack instructions (dup, swap, pop & nop) Arithmetic and logical instructions Conversion instructions Control transfer and function return Manipulating object fields Method invocation Miscellaneous operations Monitors

Data Movement All data movement takes place through stack CONSTANT POOL GLOBAL STORAGE STACK FRAME Instruction stream opcode operand operand opcode opcode operand locals operand stack ALU

Bytecode Example PC instruction 0: iconst_2 //pushes constant 2 onto operand stack 1: aload_0 //pushes local variable 0 onto the stack 2: getfield #2; //object ref on the stack, entry 2 on constant pool gives descr 5: iconst_0 6: iaload 7: aload_0 8: getfield #2; 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn

Stack Tracking Operand stack at any point in program have: Same number of operands Of same types In same order Regardless of control flow path getting there Helps with static type checking by the loader

Formal ISA Specification Magic number and header Binary Classes Major regions preceded by counts Constant pool Interfaces Field information Methods Attributes Magic Number Version Information Const. Pool Size Constant Pool Access Flags This Class Super Class Interface Count Interfaces Field count Field Information Methods count Methods Attributes Count Attributes

Binary Classes Holds all constant values and references used by the methods that are to follow. Provides access information, example: whether public whether interface Given as indices in the constant pool Magic Number Version Information Const. Pool Size Constant Pool Access Flags This Class Super Class Interface Count Interfaces Field count Field Information Methods count Methods Attributes Count Attributes

Binary Classes Contains a number of references to the superinterfaces to this class Given as indices in the constant pool The constant pool entries are references to the interfaces Contains the specifications of the fields declared in this class The information regarding each method, as well as the methods themselves (encoded as bytecode) Contains detailed information regarding the previous sections Magic Number Version Information Const. Pool Size Constant Pool Access Flags This Class Super Class Interface Count Interfaces Field count Field Information Methods count Methods Attributes Count Attributes

A Note About: Garbage Collection Garbage: objects that are no longer accessible Examples: A Global Heap B D, G, H Root Set C D... E F G H

Garbage Collection A large topic on its own Mark and sweep Start with root set of references On stack, static objects, constant pool Trace and mark all reachable objects Sweep through heap, collecting marked objects Keep free space in linked list Advantage: Fast Does not require moving object/pointers Disadvantage: Discontiguous free space, fragmentation Allocate new objects from best-fit free list 39

Compacting Collector Make free space contiguous Involves multiple passes through heap A lot of object movement => many pointer updates free A B C D E F G H free A B C E G

Copying Collector Divide heap into halves Collect when one half full Copy into unused half during sweep phase + Reduces passes through heap - Wastes half of heap unused free A B C D E F free A B C E G unused G H

Generational Collector Divide heap into halves tenured and nursery Collect nursery more frequently Move long-lived objects into tenured half Objects have either very long or very short lives

JVM Bytecode Emulation Interpretation Simple, fast startup, but slow Just-In-Time (JIT) Compilation Compile each method when first touched Simple, static optimizations Hot-Spot Compilation Find frequently executed code Apply more aggressive optimizations on that code Typically phased with interpretation or JIT Dynamic Compilation Based on Hot-Spot compilation Use runtime information to optimize More later

So JVM is: An abstract entity that gives meaning to class files Has many concrete implementations Hardware Interpreter JIT compiler Persistence An instance is created when an application starts Terminates when the application finishes

Conclusions HLL VM is built with portability as main goal: Building a loader and JIT compiler is easier than building a full-fledged compiler API and not ABI