Jacil Technical Report
Total Page:16
File Type:pdf, Size:1020Kb
JaCIL: A CLI to JVM Compiler Technical Report Almann T. Goo [email protected] Rochester Institute of Technology Department of Computer Science July 25, 2006 Abstract Both the .NET Framework via the Common Language Infrastructure (CLI) [15] and the Java Virtual Machine (JVM) [14] provide a managed, virtual exe- cution environment for running high-level virtual machine code. As these two platforms become ubiquitous, it is of pragmatic and academic interest to have the capability of running CLI code on the JVM and vice-versa. This report describes JaCIL (pronounced “jackal”), a project that implements a CLI assembly to JVM class file byte-code compiler framework. Also pro- vided by JaCIL, is a user-land tool interfacing this framework, and a run- time library to support translated code. Contents 1 Introduction 11 1.1 The Java Virtual Machine ..................... 11 1.2 The Common Language Infrastructure .............. 12 1.3 Related Work ............................. 13 1.3.1 Previous Translation Efforts and Platform Comparisons 13 1.3.2 IKVM.NET Java Virtual Machine ............ 13 1.3.3 Visual MainWin for J2EE ................. 14 1.3.4 Retroweaver ......................... 14 1.3.5 The Mono and DotGNU Projects ............. 14 2 JaCIL Software Design 16 2.1 Functional Specification ...................... 16 2.1.1 JaCIL Compiler Framework ................ 16 2.1.2 JaCIL Run-time Library .................. 17 2.2 Technical Design and Architecture ................ 17 2.2.1 Compiler Framework Overview .............. 18 2.2.2 The Compiler API ...................... 18 2.2.3 The Context API ....................... 21 2.2.3.1 Standard Context API Implementation .... 23 1 2.2.4 The Translator API ..................... 23 2.2.4.1 Standard Translator API Implementation .. 26 2.2.5 The Utility API ....................... 28 2.2.6 The jacilc Application .................. 28 3 CLI Feature Specification 30 3.1 Assemblies .............................. 30 3.1.1 Assembly Translation Limitations ............ 31 3.2 Assembly Entry Points ....................... 31 3.2.1 Entry Point Translation Limitations ........... 31 3.3 Modules ................................ 32 3.4 Types ................................. 32 3.4.1 Type Translation Limitations ............... 33 3.4.1.1 Name Mangling .................. 33 3.4.1.2 Array Support ................... 33 3.4.1.3 Generics ...................... 34 3.4.1.4 Inner Classes ................... 34 3.4.1.5 Enumerations ................... 34 3.4.1.6 Delegates ..................... 34 3.5 Managed Pointers .......................... 34 3.5.1 Managed Pointer Translation Limitations ........ 35 3.6 Value Types ............................. 35 3.6.1 Value Type Translation Limitations ........... 35 3.7 Type Definitions ........................... 35 3.7.1 Value Type Definitions ................... 37 3.7.2 Type Definition Translation Limitations ......... 37 2 3.8 Field Definitions ........................... 37 3.8.1 Field Definition Translation Limitations ......... 37 3.9 Method Definitions ......................... 39 3.9.1 Method Name Mangling .................. 39 3.9.2 Local Variables ....................... 41 3.9.3 Exception Handling Support ................ 42 3.9.4 Constructors ......................... 42 3.9.5 Method Definition Translation Limitations ....... 43 3.10 JaCIL Implementation of Standard CLI API .......... 44 3.10.1 System.Object Implementation ............. 44 3.10.2 System.Type Implementation .............. 45 3.10.3 Other Classes ........................ 45 3.11 Instruction Set Support ....................... 45 3.11.1 Non-operation Instructions ................ 45 3.11.2 Stack Instructions ...................... 48 3.11.2.1 Stack Duplication of Value Types ........ 48 3.11.3 Local Variable and Argument Instructions ....... 48 3.11.3.1 Implicit Conversions ............... 49 3.11.3.2 Loading and Storing to Boxed Local Variables 49 3.11.3.3 Value Type Loading ............... 50 3.11.4 Constant Loading Instructions .............. 50 3.11.5 Arithmetic Instructions .................. 50 3.11.5.1 Arithmetic Instruction Translation Limitations 52 3.11.6 Numeric Conversion Instructions ............. 53 3.11.7 Branch and Comparison Instructions .......... 53 3.11.7.1 Unconditional Branches ............. 55 3 3.11.7.2 The switch Instruction ............. 55 3.11.7.3 Comparison and Other Conditional Branches 55 3.11.8 Method Invocation and Return Instructions ....... 57 3.11.8.1 Method Invocation on Value Types ....... 57 3.11.8.2 Method Invocation Translation Limitations . 58 3.11.9 Static and Instance Field Access Instructions ...... 58 3.11.9.1 Field Access on Value Types ........... 59 3.11.10Object Creation and Initialization ............ 59 3.11.11Vector Instructions ..................... 60 3.11.11.1Vector Element Access .............. 60 3.11.11.2Vector Creation .................. 62 3.11.11.3Vector Length ................... 62 3.11.12Exception Instructions ................... 62 3.11.12.1Exception Throwing ............... 62 3.11.12.2Leaving Protected Blocks ............ 62 3.11.12.3Leaving Finally Blocks .............. 63 3.11.13Indirection Instructions .................. 63 3.11.14Address Load Instructions ................. 64 3.11.14.1Local Variable Address Load .......... 64 3.11.14.2Static and Instance Field Address Load .... 65 3.11.14.3Array Element Address Load .......... 65 3.11.15Value Type and Reference Type Instructions ...... 65 3.11.15.1Boxing and Unboxing .............. 65 3.11.15.2The initobj instruction ............ 66 3.11.15.3Reference Type Checking and Conversion ... 66 3.12 Translation Examples ....................... 67 4 3.12.1 Basic Operations ...................... 67 3.12.2 Exceptions .......................... 67 3.12.3 Value Types and Managed Pointers ........... 68 4 JaCIL User Guide 77 4.1 Building JaCIL ........................... 77 4.2 Using the Compiler Tool ...................... 78 4.3 Running Compiled Code ...................... 80 5 Performance and Benchmarking 82 5.1 Blowfish Cipher ........................... 83 5.2 MD5 Message Digest ........................ 84 5.3 Line Drawing Algorithm with Value Types ........... 84 5.4 Bresenham’s Line Drawing Algorithm with Managed Pointers 85 6 Conclusions 86 6.1 Lessons Learned ........................... 86 6.2 Future Work ............................. 87 6.2.1 Missing Compiler Features ................ 87 6.2.2 CLI Standard API ...................... 87 6.2.3 JaCIL on the JVM ...................... 87 6.2.4 CLI Implementation .................... 87 5 List of Figures 2.1 Compiler API class relationships ................. 20 2.2 Context API interface hierarchy .................. 22 2.3 Cross-section of the Context API interface relationships .... 23 2.4 Standard Context API control flow ................ 24 2.5 Cross-section of Translator API interface relationships .... 25 2.6 IInstructionTranslator creation flow in standard factory implementation ........................... 27 2.7 jacilc control flow ......................... 29 3.1 Value type translation memory diagram for local variables .. 36 3.2 Sample C# method definition with basic operations ...... 69 3.3 CIL code compiled from source in figure 3.2 ........... 70 3.4 Java byte-code generated by JaCIL from the code in figure 3.3 71 3.5 Sample C# method definition with a catch handler ....... 72 3.6 CIL code compiled from source in figure 3.5 ........... 73 3.7 Java byte-code generated by JaCIL from the code in figure 3.6 74 3.8 Sample C# method definition with value type operations ... 75 3.9 CIL code compiled from source in figure 3.8 ........... 75 3.10 Java byte-code generated by JaCIL from the code in figure 3.9 76 6 4.1 jacilc.exe output with -? switch ............... 79 4.2 Using jacilc.exe to compile an assembly to a JAR file ... 80 4.3 Running translated code ...................... 80 4.4 Sample C# “Hello World” type program ............. 81 7 List of Tables 2.1 JaCIL Compiler Framework namespaces ............ 18 2.2 ICompiler primary methods ................... 19 2.3 ITypeTranslator translation methods ............. 26 2.4 JaCIL Compiler utility primary classes ............. 28 3.1 JaCIL built-in type mapping .................... 32 3.2 JaCIL managed pointer type mapping .............. 33 3.3 Supported CLI type attributes and their JVM translations .. 36 3.4 Supported CLI field attributes and their JVM translations .. 38 3.5 Supported CLI method attributes and their JVM translations 40 3.6 Supported CLI implementation attributes and their JVM trans- lations ................................. 41 3.7 java.lang.Object method overrides in System.Object .. 44 3.8 JaCIL supported instructions add through ldelem.u1 .... 46 3.9 JaCIL supported instructions ldelem.u2 through xor .... 47 3.10 CIL Local variable and argument load instructions ...... 48 3.11 JVM Local variable load instructions ............... 49 3.12 CIL Local variable and argument store instructions ...... 49 3.13 JVM Local variable store instructions .............. 49 3.14 CIL field access instruction translation semantics ....... 50 8 3.15 JVM numeric instruction prefixes ................. 51 3.16 JaCIL translations for CIL arithmetic operations on integers and floating point values ...................... 52 3.17 CIL Arithmetic Instructions for integer types only ....... 52 3.18 CIL conversion instruction target types ............. 53 3.19 CIL conversion instruction target JVM stack types ....... 54 3.20 Additional operations for small integer conversion ....... 54 3.21 Translation semantics