1 / 40

High-Level Language Virtual Machine Architecture

High-Level Language Virtual Machine Architecture. 2005, 10, 17 Choonki Jang. Contents. Introduction of HLL VM The Pascal P-code VM Object-Oriented HLL VM .NET VM Common Language Runtime. HLL Program. Library of HLL OS. Hardware. HLL program characteristic.

daveigh
Download Presentation

High-Level Language Virtual Machine Architecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. High-Level LanguageVirtual Machine Architecture 2005, 10, 17 Choonki Jang Advanced Compiler Research Lab.

  2. Contents • Introduction of HLL VM • The Pascal P-code VM • Object-Oriented HLL VM • .NET VM • Common Language Runtime Advanced Compiler Research Lab.

  3. HLL Program Library of HLL OS Hardware HLL program characteristic • Platform dependency • Library, OS system call, ISA • To move program another platform, • At least re-compile • Rewrite build environment • compiler, library, assembler, linker, etc. • Modify source code ( e.g, system call ) • Process VM • Execution environment must be provided • Some features may be impossible to emulate exactly Advanced Compiler Research Lab.

  4. HLL VM • With new ISA and standard specification, • Higher level abstraction • Efficient implementation of the targeted HLL • Simplifies compilation process • Machine independent VS Machine dependent • Metadata • Data specification becomes very important for platform independency • data set architecture than instruction set architecture Advanced Compiler Research Lab.

  5. HLL program HLL program Compiler frontend Compiler frontend Portable code (Virtual ISA) Intermediate code Compiler backend VM loader Object code (ISA) virt. mem. image VM interpreter or translator Loader Memory image Host instruction Difference Advanced Compiler Research Lab.

  6. P-code Shortly, stack-oriented instruction Easy to port to new platform porting compiler and library only porting or implementing VM Pascal Program binary Library of Pascal OS Pascal Program P-code Pascal P-code VM (P-code emulator, Library) Hardware Platform( OS, Hardware, etc) The Pascal P-Code VM Advanced Compiler Research Lab.

  7. Memory Architecture • Memory architecture • All data areas are divided into cells, • each cell can hold a single value • actual size of cell is implementation dependent, • but large enough to hold largest value specified in P-code • Constant area (immediate operands) • With single stack for • procedure stack • stack for instruction execution • No garbage collection Advanced Compiler Research Lab.

  8. function value static link dynamic link stack frame previous EP MP return address parameters stack frame locals EP operand stack MP function value static link mark stack dynamic link previous EP return address NP parameters locals heap SP operand stack constant area Memory Architecture EP • 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 Advanced Compiler Research Lab.

  9. P-code • Basic and typed instruction • memory, arithmetic, logical, shift (of course, branch, call, return also) • different instruction for type • e.g, adi(add integer), adr(add real) • with operand (memory, branch, call) • just opcode (arithmetic, logical, shift, return) Advanced Compiler Research Lab.

  10. Modern HLL VMs • Similar to P-code scheme • Stack-oriented ISA • For platform-independent distribution of application • Not for easing the porting of compiler • Network computing environment • Untrusted software (internet) • Robustness  object oriented programming • Bandwidth is a consideration • Good performance must be maintained • Two major example • Java VM • Microsoft Common Language Infrastructure (CLI) Advanced Compiler Research Lab.

  11. Modern HLL VMs • Compiler forms program files (e.g. class files) • Standard format • In theory any compiler and language can be used • Program files contain both code and metadata • P-code focused just instruction set aspect • Abstraction of data also important • Platform independent form of data  metadata • VM runtime convert metadata into internal machine-dependent data Advanced Compiler Research Lab.

  12. Virtual Machine Implementation Machine Independent Program File Loader Internal Data Structures Metadata Interpreter Code Native Code Translator Modern HLL VMs solid dotted : movement of code and data during emulation : transformation of code and data Advanced Compiler Research Lab.

  13. Remote System Other file Public files Network application Public files VMM User Process Local Accessible files Local System Other files Security and Protection • A Key aspect of modern network-oriented VMs • Rely on “protection sandbox” • Aspects of sandbox • Public files • Local files that accessible • Prevent application from accessing memory and code that is outside the sandbox • This is the first generation security method • Still the default Advanced Compiler Research Lab.

  14. Java Protection Sandbox class file class file class file class file Network File System loaded method loaded method loaded method loaded method lib. method loaded method lib. method Local File System loaded method native method local file native method local file Standard Libraries trusted Emulation Engine trusted security agent trusted loader trusted Advanced Compiler Research Lab.

  15. Security and Protection • Remote resource • Java default • Not responsibility of HLL VM • Up to the user of the remote system • Java 2 Standard Edition • Contains a number of APIs to support network-based security • Security, authentication, and cryptography APIs • Memory • Combination of static and run-time check • Today’s popular HLL VMs (JVM, CLI) • Strongly typed HLL and rely on high degree on static checking Advanced Compiler Research Lab.

  16. Basic Java Protection • Restrict the virtual ISA • All data structures defined as part of the metadata • All memory accesses are to fixed fields and typed • All branch instructions 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 at loading process • Dynamically checked by runtime Advanced Compiler Research Lab.

  17. Robustness : Object-Orientation • Class • Type of object • Object • Data carrying entites • Dynamically allocated • Can be accessed via managed pointers or references • Inheritance, Methods, Polymorphism • Not PL lecture • But, objects can only be accessed via managed pointers, or references • Array is also intrinsic form of object in JVM and CLI Advanced Compiler Research Lab.

  18. Garbage Collection • Objects are created and float in memory space • Tethered by reference • In architecture, memory is unbounded in size • In reality it is limited • Garbage • During program execution, many objects are created then abandoned, so become garbages • Collection • Due to limited memory, garbage should be colledted • Think about destructor of c++ • To improve robustness, have VM collect garbage automatically Advanced Compiler Research Lab.

  19. Networking • For the limitation of network bandwidth • To reduce network bandwidth • Reduce the size of a program • Specification • Virtual instruction such as Java byte code • Translation (or interpreter) converts the specification into real native instructions • Using stack-oriented instruction set • Support dynamic loading on demand • Load only classes that are needed • Spread loading out over time Advanced Compiler Research Lab.

  20. Performance • Most of the emulation techniques can be used • JIT compilation • By V-ISA, the code-discovery problem is very simple • will be discussed at chapter 6 Advanced Compiler Research Lab.

  21. Terminology • JVM Architecture  CLI • Analogous to an ISA • JVM Implementation  CLR • Analogous to an ISA implementation • Java bytecode  MSIL • The instruction part of the ISA • Java Platform  .NET framework • ISA implementation + Libraries Advanced Compiler Research Lab.

  22. Microsoft CLI The Common Language Interface • Common Language Infrastructure • Virtual machine architecture that is part of .NET framework • Common Language Runtime • Microsoft’s implementation of the CLI • Other implementations • DotGNU Portable.NET ( version 0.7.2 ) • http://www.southern-storm.com.au/portable_net.html • Mono ( version 1.1.9.1 ) • http://www.go-mono.com Advanced Compiler Research Lab.

  23. Microsoft .NET Overview • Initiated in July 2000 by Microsoft • Incorporated into .NET • COM+ component services • ASP web development framework • XML • New web services protocols • SOAP, WSDL, UDDI • Focus on the Internet Visual Studio .NET .NET Enterprise Servers eg, SQL Server .NET Framework .NET Building Block Services eg. MS Passport Operating system on servers, desktops, and devices Advanced Compiler Research Lab.

  24. .NET Framework • Design goal • Component infrastructure • “plug-and-play” • Language integration • COM was binary reuse, not language integration • Reliability • Security • Internet interoperation • Simple development • Simple deployment .NET Framework Web Services Web Forms Windows Forms Data and XML classes Framework Base Classes Common Language Runtime Windows Platform Advanced Compiler Research Lab.

  25. The Common Language Infrastructure • Similar with Java environment • Support OOP within a managed runtime environment, • Includes built-in protection and garbage collection • Platform independent • Different at supporting • Not only platform independent, but also HLL independence • multiple, interoperating HLLs • programs verifiable by loader • eg, C#, Visual Basic .NET, Java, and etc. • programs that don’t have to be (or cannot be) verified by loader • eg, Managed C++, Legacy C and C++ Advanced Compiler Research Lab.

  26. C# program Java program Visual Basic .NET program Verifiable Module Verifiable Module Verifiable Module CLR Other CLI implementation IA-32 Platform IPF Platform Independence and Interoperability Managed C++ program • To achieve both, • Standard rule among language • Platform independent and verifiable IR • Platform independent module specification • Methodology or specification • for plumbing type of objects • Specification to represent information • of program • Specification of VM runtime • …… • So CLI, MSIL, PE/COFF, CLS, CTS, etc,. are defined by Microsoft. Compile Unverifiable Module Advanced Compiler Research Lab.

  27. PE/COFF Headers CLR Header CLR Data Metadata IL code Native Code and Data (optional) .data, .rdata, .rsrc, .text Microsoft PE/COFF • Portable Executable and Common Object File Format • Highlighting on interoperability • Just like any other executable file to loader of OS • Native Code and Data • eg, with support of compiler, create encrypted data and only you can access Advanced Compiler Research Lab.

  28. PE/COFF Headers CLR Header CLR Data Metadata IL code Native Code and Data (optional) .data, .rdata, .rsrc, .text Metadata • consists of • Object definitions, constants, etc. • Interoperability implemented • Not method level such as JNI • But more integrated way that also extends to data • So, interoperability requires • Standardization of Language  CLS • Standardization of Type  CTS Advanced Compiler Research Lab.

  29. C# HLL Program C HLL Program Compile and Load Compile and Load Bytecode Methods Bytecode Methods (some unverifiable) getfield/putfield get/put via managed pointers get/put via unmanaged pointers Object Object Memory Block Array Interoperate example Advanced Compiler Research Lab.

  30. Attributes • Included in COM+ Services in .NET • Built in attributes • Key elements that help programmer writes less code • eg. System.ObsoleteAttribute • User-defined extension • Programmer can assign an attribute • eg. [centimeters], [inches] using System; public class MainApp { public static void Main() { // This generates a compile-time warning. int MyInt = Add(2,2); } // Specify attributes between square brackets // in C#. This attribute is applied only to // the Add method. [Obsolete("Will be removed in next version")] public static int Add( int a, int b) { return (a+b); } } Advanced Compiler Research Lab.

  31. Metadata Streams Local variables Argument Table GLOBAL STORAGE opcode operand operand opcode opcode operand evaluation stack Instruction Stream ALU MSIL • Stack oriented • MSIL support metadata tables refered to as stream and accessed via token • Control flow instruction • Verifiable PC relative or call/return. • Unverifiable function pointer • Only for vector instruction (no array) • APIs for array Advanced Compiler Research Lab.

  32. Example Java code Evaluation Stack class Retangle { protected int sides []; ... public int perimeter() { return 2*(sides[0] + sides[1] ); } ... } MSIL code Java bytecode 0: iconst_2 1: aload_0 2: getfield #2 5: iconst_0 6: iaload 7: aload_0 8: getfield #2 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 0: ldc.i4.2 1: ldarg.0 2: ldobj <token> 5: ldc.i4.0 6: ldelem.i4 7: ldarg.0 8: ldobj <token> 11: ldc.i4.1 12: ldelem.i4 13: add 14: mul 15: ret 0 Reference of <this> Reference of <sides> 2 Advanced Compiler Research Lab.

  33. Example Java code Evaluation Stack class Retangle { protected int sides []; ... public int perimeter() { return 2*(sides[0] + sides[1] ); } ... } MSIL code Java bytecode 1 0: iconst_2 1: aload_0 2: getfield #2 5: iconst_0 6: iaload 7: aload_0 8: getfield #2 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 0: ldc.i4.2 1: ldarg.0 2: ldobj <token> 5: ldc.i4.0 6: ldelem.i4 7: ldarg.0 8: ldobj <token> 11: ldc.i4.1 12: ldelem.i4 13: add 14: mul 15: ret Reference of <this> Reference of <sides> Value of sides[0] 2 Advanced Compiler Research Lab.

  34. Example Java code Evaluation Stack class Retangle { protected int sides []; ... public int perimeter() { return 2*(sides[0] + sides[1] ); } ... } MSIL code Java bytecode 0: iconst_2 1: aload_0 2: getfield #2 5: iconst_0 6: iaload 7: aload_0 8: getfield #2 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 0: ldc.i4.2 1: ldarg.0 2: ldobj <token> 5: ldc.i4.0 6: ldelem.i4 7: ldarg.0 8: ldobj <token> 11: ldc.i4.1 12: ldelem.i4 13: add 14: mul 15: ret Value of sides[1] Value of sides[0] 2 Advanced Compiler Research Lab.

  35. Example Java code Evaluation Stack class Retangle { protected int sides []; ... public int perimeter() { return 2*(sides[0] + sides[1] ); } ... } MSIL code Java bytecode 0: iconst_2 1: aload_0 2: getfield #2 5: iconst_0 6: iaload 7: aload_0 8: getfield #2 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 0: ldc.i4.2 1: ldarg.0 2: ldobj <token> 5: ldc.i4.0 6: ldelem.i4 7: ldarg.0 8: ldobj <token> 11: ldc.i4.1 12: ldelem.i4 13: add 14: mul 15: ret sides[0] * sides[1] 2 Advanced Compiler Research Lab.

  36. Example Java code Evaluation Stack class Retangle { protected int sides []; ... public int perimeter() { return 2*(sides[0] + sides[1] ); } ... } MSIL code Java bytecode 0: iconst_2 1: aload_0 2: getfield #2 5: iconst_0 6: iaload 7: aload_0 8: getfield #2 11: iconst_1 12: iaload 13: iadd 14: imul 15: ireturn 0: ldc.i4.2 1: ldarg.0 2: ldobj <token> 5: ldc.i4.0 6: ldelem.i4 7: ldarg.0 8: ldobj <token> 11: ldc.i4.1 12: ldelem.i4 13: add 14: mul 15: ret 2*(sides[0] * sides[1]) Advanced Compiler Research Lab.

  37. MSIL • Generic instruction • No operand type in arithmetic instruction • To support C-like memory operation • Incur slow interpreting • although not an absolute requirement • actually CLR always JIT-compile before execute • More about, see • Andrew Kennedy, Don Syme, Design and Implementation of Generics for the .NET Common Language Runtime, Microsoft Research, Cambridge, U.K. • Support unmanaged memory, pointer, and API • Support indirect call, such as function pointer Advanced Compiler Research Lab.

  38. CLR Execution .NET PE files (metadata and IL) • Verifier verifies • The metadata is well formed, meaning that metadata must be valid • The IL code is type safe, meaning type signatures are used correctly • JIT compiler • JIT compilation occurs only the first time a method is invoked • ngen • preJIT during installation and setup time OS loader Class loader JIT compiler Verifier JIT Execution support and management (garbage collection, security engine, code manager, exception manager, thread support, etc. ) CLR’s Virtual Execution Engine Advanced Compiler Research Lab.

  39. Application Application Application AppDomain AppDomain AppDomain VM VM VM VM(CLR) Host Process Host Process Host Process Host Process Host OS Host OS Isolation and AppDomains • Isolation • isolated and independent environment • Or, security hole • AppDomains (application domains) • Share same VM with complete isolation • Each process may contain multiple threads Advanced Compiler Research Lab.

  40. Reference • Thuan Thai, Hoang Q. Lam, .NET Framework Essentials, O’RELLY • Platt, Intruducing .NET 3rd edition, Microsoft Press • http://msdn.microsoft.com • Specification of CLI • Example of Attributes Advanced Compiler Research Lab.

More Related