Introduction:

CLRCLR „ What is a virtual machine?

AA newnew virtualvirtual machinemachine „ CLI and CLR

„ JIT execution and performance on CLR João Ferreira „ Unix with “.NET” [email protected]

(c) João Ferreira - ICCA 2004 1 (c) João Ferreira - ICCA 2004 2

Virtual Machines Virtual Machines „ Problems with native • Machine arquitecture dependence „ Why “virtual”? • Operating system conventions • Emulation software that translates from • specific issues one language to another

C HIGH LEVEL LANGUAGE

„ To solve this... • Develop a language that executes as Assembly LOW LEVEL LANGUAGE bytecode (UNCOL, Lisp, P-Code, etc.) • Failure Æ not running at native speed!

(c) João Ferreira - ICCA 2004 3 (c) João Ferreira - ICCA 2004 4 Virtual Machines New era of VM

„ JVM and CLR Source Code (High Level Language) • Use JIT (Just-In-Time) to produce well- optimized native machine code

Compiler produces bytecode • Bytecode (portability) +

VIRTUAL MACHINE • native code generation (speed) +

MISC HARDWARE/SOFTWARE • Bytecode verifier • Stack based – doesn’t now the mean of registers

(c) João Ferreira - ICCA 2004 5 (c) João Ferreira - ICCA 2004 6

Common Language Runtime Common Language Infraestructure

„ marketing Æ .NET (DotNet) „ International Standard (ECMA)

„ Is nothing more than a virtual „ CLI Specification machine, however.. CTS () „ Based on CLI Æ a standard designed from the scracth to support multiple CLS (Common Language Specification) languages CIL (Common Intermediate Language)

VES ()

(c) João Ferreira - ICCA 2004 7 (c) João Ferreira - ICCA 2004 8 CTS (Common Type Specification) CLS (Common Language Specification)

„ A rich type system that supports the types and operations found in many „ A subset of CTS programming languages „ Defines the rules for each individual „ To support a wide range of interop with each other languages „ Defines a set of rules for types „ Designed for object oriented, procedural and functional languages „ More than 15 languages implemented CTS

(c) João Ferreira - ICCA 2004 9 (c) João Ferreira - ICCA 2004 10

CIL Example CIL (Common Intermediate Language) public static void TestMethod (int a, int b) // arguments { int c; int d; int e; // locals „ Also known as IL or MSIL (Microsoft) c = a + b; d = 10; „ A language more “higher” than native e = c + d; instruction set } ldarg num load argument no. num onto the stack „ Based on metadata – self describing … Æ …, value

„ An oriented language • Can create instances of objects ldarg.1 • Call virtual methods ldloc indx load local variable no. indx onto the stack • Work with arrays … Æ …, value • Throw and catch exceptions! ldloc.1 (c) João Ferreira - ICCA 2004 11 (c) João Ferreira - ICCA 2004 12 public static void TestMethod (int a, int b) { intc; intd; inte; evaluation stack c = a + b; CIL Example d = 10; e = c + d; ldc num load numeric constant }

… Æ …, num .method public hidebysig static void TestMethod(int32 a,int32 b) cil managed { // Code size 12 (0xc) ldc.i4 10 .maxstack 2 .locals init ([0] int32 c, [1] int32 d, stloc indx pop value from stack to local [2] int32 e) variable IL_0000: ldarg.0 …, value Æ … IL_0001: ldarg.1 IL_0002: add stloc.0 IL_0003: stloc.0 IL_0004: ldc.i4.s 10 IL_0006: stloc.1 IL_0007: ldloc.0 starg num store a value in an argument IL_0008: ldloc.1 slot IL_0009: add …, value Æ … IL_000a: stloc.2 IL_000b: ret } // end of method Class1::TestMethod starg.1 (c) João Ferreira - ICCA 2004 13 (c) João Ferreira - ICCA 2004 14

VES (Virtual Execution System) CLR „ Is the Microsoft VES (Virtual Execution „ Implements and enforces CTS System) for CLI

„ Will load and run programs written „ CLR will use: for the CLI (with IL code) • Managed modules • Assemblies „ Handles all the major overheads of traditional programming models • JIT compilers

„ How it will load? To run CLI-compliant programms into Win32 • JIT and Intel x86 architectures. • Install time compilers

(c) João Ferreira - ICCA 2004 15 (c) João Ferreira - ICCA 2004 16 During Development... CLR Execution Model During Development...

„ Ok.. Some code...written with notepad:

using System; class Hello { public static void Main() { System.Console.WriteLine("Hello world!"); System.Console.WriteLine(“Bye world!”); } }

(c) João Ferreira - ICCA 2004 17 (c) João Ferreira - ICCA 2004 18

Compiling the source code User executes ola.exe... CLR starts!

„ csc ola.il (managed code) Process's primary csc ola.il (managed code) thread starts Ola EXE „ Result: ola.exe Æ Assembly! Address Space PE header

„ Resulting file is a PE () Managed EXE .text section JMP _CorExeMain

Managed module Tool combining Assembly .idata section (IL and metadata) multiple (Manifest describes the MSCorEE.dll managed modules set of files in the assembly) CLR header DLL: MSCorLib.dll Managed module and Function: _CorExeMain (IL and metadata) resource files into Managed module IL an assembly (IL and metadata) Metadata C# compiler Managed module (CSC.exe), (IL and metadata) Visual Basic 1. MSCorEE examines CLR header to get Main method's metadata token. Resource file Compiler Resource file 2. MSCorEE examines the Main's metadata to get location of IL within the EXE. (.jpeg, .gif, etc.) (.jpeg, .gif, etc.) (VBC.exe), 3. MSCorEE compiles Main's IL to native CPU. 4. MSCorEE jumps to Main's (using primary thread) – the application runs. Assembly Linker Resource file (c) João Ferreira - ICCA 2004 19 (c) João Ferreira - ICCA 2004 20 Resource file (AL.exe) (.jpeg, .gif, etc.) (.jpeg, .gif, etc.) … Ok.. We got the IL.. And now?? JIT will translate IL to CPU instructions Method Structure to Console .method public hidebysig static void Main() cil managed static void WriteLine() { ola.exe JitCompiler .entrypoint static void Main() { Console.WriteLine("Hello World"); // Code size 21 (0x15) Console.WriteLine("Bye World"); static void WriteLine(string) .maxstack 1 } JitCompiler IL_0000: ldstr "Hello world!" IL_0005: call void [mscorlib]System.Console::WriteLine(string) (remaining members) IL_000a: ldstr "Bye world!" JitCompiler Native CPU instructions IL_000f: call void [mscorlib]System.Console::WriteLine(string) IL_0014: ret MSCorEE.dll } // end of method Hello::Main 1. JITCompiler function { 2. In the assembly that implements the type (Console) look up the method (Writeline) being called in metadata. 3. From the metadata, get the IL for this method. 4. Allocate the block of memory. 5. Compile the IL into native CPU instructions; the native code is saved in the memory allocated in step 4. 6. Modify the method's entry in the Type's table so that it now points to the memory block allocated in step 4. 7. Jump to the native code contained inside the memory block. 8. }

(c) João Ferreira - ICCA 2004 21 (c) João Ferreira - ICCA 2004 22

Already Jited?

Console JIT Performance and Issues static void WriteLine() Managed.exe JitCompiler static void Main() { „ Platform Independence Console.WriteLine("Hello"); static void WriteLine(string) Platform Independence Console.WriteLine("GoodBye"); Native CPU Native } instructions • Realized when high-level language (remaining members) compilers convert source code to platform JitCompiler agnostic MSIL code MSCorEE.dll • The application or software component is 1. JITCompiler function { 2. In the assembly that implements the type (Console) look up the method distributed in this form (Writeline) being called in metadata. 3. From the metadata, get the IL for this method. 4. Allocate the block of memory. • JIT compiles to native code either at 5. Compile the IL into native CPU instructions; the native code is saved in the memory allocated in step 4. runtime or at install time 6. Modify the method's entry in the Type's table so that it now points to the runtime or at install time memory block allocated in step 4. 7. Jump to the native code contained inside the memory block. 8. }

(c) João Ferreira - ICCA 2004 23 (c) João Ferreira - ICCA 2004 24 JIT Performance and Issues JIT Performance and Issues

„ Language Interoperability „ Runtime Stack Manipulation • Occurs when different language compilers • The JIT Compiler populates important data compile to language-agnostic MSIL code structures for object tracking and specific stack-frame construction • Metadata and the Common Type System • The JIT Compiler can be used to identify play a major role in cross-language and play a major role in cross-language and specific code elements as they are platform independence consumed, i.e., exception handlers and security descriptors (Verifier) • Doesn’t need registers • CPU independent

(c) João Ferreira - ICCA 2004 25 (c) João Ferreira - ICCA 2004 26

JIT performance and issues JIT Performance and Issues

„ JIT compiler knows more about the execution „ Small Memory Footprint JIT compiler knows more about the execution environment than an unmanaged compiler • JIT compilation takes advantage of the would know possibility that some code may never be „ JIT compiler can take advantage of used instructions offered by the chip that the unmanaged compiler knows nothing about • The JIT Compiler compiles methods only as „ JIT compiler could detect that a certain test is needed always false, and short-circuit „ The CLR could profile the code’s execution and recompile the IL on the fly reducing branching, etc.

(c) João Ferreira - ICCA 2004 27 (c) João Ferreira - ICCA 2004 28 The problems... Alternatives to JIT

„ Unmanaged code is pre-compiled and „ NGEN.EXE – Install Time tool to can just execute create a native image into the native

„ Managed code requires 2 compilation cache phases „ Native Image Æ a file containing • Compiler produces IL compiled processor-specific machine • IL compiled to native code at runtime, code requiring more memory to be allocated, and „ Good for heavy startup applications additional CPU cycles „ Use it at client-side

(c) João Ferreira - ICCA 2004 29 (c) João Ferreira - ICCA 2004 30

Unix with .NET What they did?

Mono Project „ Designed a compiler generating IL code (ECMA 334) – C# taking note of CTS and www.go-.com CLS rules „ DotGnu „ A VES with JIT’s „ A base class library resides in the `mcs' „ http://www.gnu.org/projects/dotgnu A base class library resides in the `mcs' module in the directoy `class'. Each directory in the directory represents the assembly where the code belongs to, and inside each directory they divide the code based on the namespace they implement.

(c) João Ferreira - ICCA 2004 31 (c) João Ferreira - ICCA 2004 32 DotGnu Conclusion

„ Created ilrun Æ interpret programms „ CLI is a standard – if you want in the CIL bytecode format (Ecma portability, all languages should be CLI- 335) compliant

„ CIL Æ converted to CVM (Converted „ Want run CLI? Virtual Machine) • make your own VES! „ Cscc – compiler ANSI C and Ecma • Make your JIT! 334

(c) João Ferreira - ICCA 2004 33 (c) João Ferreira - ICCA 2004 34