28
1 High-Level Language Virtual Machine Architecture

High-Level Language Virtual Machine Architecture

  • Upload
    lloyd

  • View
    59

  • Download
    0

Embed Size (px)

DESCRIPTION

High-Level Language Virtual Machine Architecture. Contents. Introduction of HLL VM The Pascal P-code VM Object-Oriented HLL VM Java VM Architecture. HLL Program. Library of HLL OS. Hardware. HLL program characteristics. Platform dependence Library, OS system call, ISA - PowerPoint PPT Presentation

Citation preview

Page 1: High-Level Language Virtual Machine Architecture

1

High-Level LanguageVirtual Machine Architecture

Page 2: High-Level Language Virtual Machine Architecture

2

Contents

Introduction of HLL VM The Pascal P-code VM Object-Oriented HLL VM Java VM Architecture

Page 3: High-Level Language Virtual Machine Architecture

3

HLL program characteristics Platform dependence

Library, OS system call, ISA

Porting to another platform, At least re-compile Rewrite build environment

compiler, library, assembler, linker, etc. Modify source code ( e.g., system call ) Painful for S/W venders

Employ process VM to avoid porting Emulating application’s ISA and OS calls Difficult to emulate some OS interfaces completely High-performance is hard to achieve

Hardware

Library of HLL

OS

HLL Program

Page 4: High-Level Language Virtual Machine Architecture

4

HLL VM New design of ISA/interface with VM-based

portability Generic ISA free of implementation-specific features Abstracted system interface easily supported by OS Support for target HLL such as OO languages Compared to process VM,

Supports virtual ISA (V-ISA) Interface based on standard libraries (API) for network, file, graphics

V-ISA includes data specification as well as instruction set Important for platform independency data set architecture than instruction set architecture

Page 5: High-Level Language Virtual Machine Architecture

5

HLL VMs from Language Perspectives

HLL program

Intermediate code

Object code(Real ISA)

Memory image

Compiler frontend

Compiler backend

LoaderVM interpreter or translator

HLL program

Portable code(Virtual ISA)

Virtual memory image

Host instruction

Compiler frontend

VM loader

distributed

Page 6: High-Level Language Virtual Machine Architecture

6

HLL VM Examples Pascal VM with a V-ISA called P-code Java VM with a V-ISA called bytecode Common language infrastructure (CLI) in .NET platform with

a V-ISA called MSIL

Page 7: High-Level Language Virtual Machine Architecture

7

The Pascal P-Code VM P-code is a simple, stack-based ISA Only one Pascal compiler needs to be developed

Distributed as a P-code version Porting Pascal is implementing the VM only

Much simpler than writing a Pascal compiler for the platform

Hardware

Library of Pascal

OS

Pascal Program binary

Platform( OS, Hardware, etc)

Pascal P-code VM(P-code emulator, Library)

Pascal Program P-code

Page 8: High-Level Language Virtual Machine Architecture

8

Memory Architecture Program memory, constant area, stack, and heap

All data areas are divided into cells, Each cell can hold a single value Actual size of cell is implementation dependent but large

enough Constant area (immediate operands) A stack

Procedure stack Operand stack for instruction execution

No garbage collection

Page 9: High-Level Language Virtual Machine Architecture

9

Memory Architecture

stack frame

stack frame

heap

constant area

MP

EP

NP

• MP : beginning of the stack frame

• EP : maximum extent of current frame

• NP : maximum extent of the heap

• SP : current top of the operand stack

EP

function valuestatic link

dynamic linkprevious EP

return addressparameters

locals

operand stack

MP

SP

mark stack

function valuestatic link

dynamic linkprevious EP

return addressparameters

locals

operand stack

Page 10: High-Level Language Virtual Machine Architecture

10

P-code Basis for later HLL VM’s V-ISA

Stack ISA with minimum registers is easily portable to any ISA Stack ISA leads to small binaries Cell-based memory model whose details are not part of ISA Interface to OS is through libraries only

Minimum libraries common to all platforms lead to weak I/O

Difference to modern HLL VM Support for OO programming Support for networked computing

Page 11: High-Level Language Virtual Machine Architecture

11

Modern HLL VMs Similar to P-code scheme like stack-oriented ISA

Not just for easing the porting of compiler, though For platform-independent distribution of applications

Platform-independence of data as well as instructions Data in P-code is encoded in the P-code itself Abstraction of data in modern HLL VMs

Metadata: a platform independent form of data Data structures or resource-related information is encoded in a

platform-independent way VM runtime convert metadata into internal machine-dependent

data

Page 12: High-Level Language Virtual Machine Architecture

12

Transformation done by VM

Metadata

Code

Loader

Interpreter

Translator

Internal DataStructures

Native Code

Machine IndependentProgram File

Virtual Machine Implementation

Transform machine-independent program files to machine-dependent code and data

Page 13: High-Level Language Virtual Machine Architecture

13

Security and Protection of HLL VM Allow load/execute programs from untrusted sources

VM implementation is protected from applications Rely on “protection sandbox”

Access to remote files Protected by the remote system

Access to local files Trusted libraries and security manager

Prevent application from accessing memory and code that is outside the sandbox, even though they are shared with VM Static checking by a trusted loader Dynamic checks by a trusted emulation engine

Page 14: High-Level Language Virtual Machine Architecture

14

Static and Dynamic Checking Branch instructions

All branches are fixed offsets within the code region The only indirection control transfer is through

Explicit call instruction Explicit return instruction

Load and store instruction Statically checked by loader w.r.t the metadata Loader also checks control flow instructions

Most out-of-bound accesses or transfers are checked Dynamically checked by runtime

Array accesses or null derefernces

Page 15: High-Level Language Virtual Machine Architecture

15

Robustness : Object-Orientation Class and objects Inheritance, Methods, Polymorphism Objects can only be accessed via references

Array is also intrinsic form of object in JVM and CLI

Page 16: High-Level Language Virtual Machine Architecture

16

Garbage Collection Objects are created and float in memory space Garbage

During program execution, many objects are created then abandoned, so become garbage

Collection Due to limited memory, we collect garbage To improve robustness, make the VM collect

garbage automatically

Page 17: High-Level Language Virtual Machine Architecture

17

Networking Considerations Support for limited network bandwidth Reduce network bandwidth for downloading

Small code size V-ISA code shows just the specification

Emulation converts the specification into real native instructions

Stack-oriented instruction set Support dynamic class loading on demand

Load only classes on an as-needed basis Spread loading overhead out over whole running time

Page 18: High-Level Language Virtual Machine Architecture

18

Performance Issues OO languages are slower than non-OO languages VM-based HLL are even slower than native execution CPU speed increases can reduce performance issues But most VMs employ high-performance techniques

Most emulation techniques can be used Interpretation, dynamic binary translation, static translation

Translation is much simpler than in other VMs No code discovery problem

Dynamic translation can be done even w/o interpretation Even static translation is possible

Page 19: High-Level Language Virtual Machine Architecture

19

Java VM Architecture Java VM is for executing Java programs

Usually referred to as Java Runtime Environment (JRE) Contains the Java virtual machine, classes comprising

the Java 2 Platform API, and supporting files JDK (Java development kit): JRE, Development Tools

(compiler, debugger), additional library

a.java b.java c.java

a.class b.class c.class

a.class b.class c.class

Object.class String.class

Javacompiler JVM

Page 20: High-Level Language Virtual Machine Architecture

20

JRE Components Execution engine – virtual (or sometimes real) processor f

or executing bytecodes Memory manager – allocate memory for instances and arr

ays and perform garbage collection Error and exception manager – deal with exception Native method support – for calling c/c++ methods Threads interface – supporting threads and monitors Class loader – dynamically load Java classes from Java cl

ass files Security manager – verify that classes are safe and contr

olling access to system resources

Page 21: High-Level Language Virtual Machine Architecture

21

Data Types

Primitive data types int, char, byte, short, float, double

Reference type Hold a reference value or null

Objects Carry data declared by the class Composed of primitive data or references to other objects array is a special object with ISA support

Page 22: High-Level Language Virtual Machine Architecture

22

JRE Components and Runtime Data

Page 23: High-Level Language Virtual Machine Architecture

23

Runtime Data Area Method area, heap, stack, PC registers

Page 24: High-Level Language Virtual Machine Architecture

24

Data Area PC: each JVM thread has its own program counter which

is the index in bytecode stream

Stack Each JVM thread has its own stack Invoke a method -> a new frame is allocated on the

stack Arguments and local variables: numbered from 0 Frame data holds return address, data for CP resoluti

on, and exception table Operand stack is used for computation Caller pushes arguments on its operand stack which

becomes local variables of the callee

Arguments

Local variables

Frame data

Operand stack

Stack FrameStructure

Page 25: High-Level Language Virtual Machine Architecture

25

Data Area Heap

Memory for objects, arrays, and class instances One heap per one JVM

All threads share it Careful synchronization of multi-threaded access is needed

Never deallocated GC collects garbage

Method area One method area for one JVM, created on JVM start-up Analogous to ‘text’ segment of conventional binaries Constant pool Type (class or interface) information and field information Method table, information, and code Class variables

Page 26: High-Level Language Virtual Machine Architecture

26

Object Representation in Heap Object Representation in the heap: two ways

(a)(b)

Page 27: High-Level Language Virtual Machine Architecture

27

Data Area Constant pool (CP)

Analogous to symbol table Collection of all symbolic data need by a class Instructions often need constants (which might be shared)

E.g, integer operands, addresses, etc. Instead of storing them within an instruction, they are saved in CP

and instructions include indexes to CP E.g., ldc #4, invokespecial #6 #4: 3040550 #6: java/lang/object/<init>() V

Each constant pool is private to its class

Page 28: High-Level Language Virtual Machine Architecture

28

Memory Hierarchy