(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 11
CLR CLR
A A new new virtual virtual machine machine
João Ferreira João Ferreira
joao.ferreira@progmat.com joao.ferreira@progmat.com
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 22
Introduction Introduction : :
What What is is a virtual a virtual machine machine? ?
CLI CLI and and CLR CLR
JIT JIT execution execution and and performance on performance on CLR CLR
Unix Unix with with “.NET” “.NET”
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 33
Virtual
Virtual Machines Machines
Why Why “virtual”? “virtual”?
••EmulationEmulation softwaresoftware thatthat translatestranslates fromfrom oneone languagelanguage to to anotheranother
HIGH LEVEL LANGUAGE
LOW LEVELLANGUAGE C
Assembly
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 44
Virtual Machines Virtual Machines
Problems Problems with with native native compilers compilers
••MachineMachine arquitecturearquitecture dependencedependence
••OperatingOperating systemsystem conventionsconventions
•
•CompilerCompiler specificspecific issuesissues
To solve this To solve this... ...
••DevelopDevelop a languagea language thatthat executes as executes as bytecode
bytecode (UNCOL, Lisp(UNCOL, Lisp, , PP--Code, Code, etcetc.).)
••FailureFailure ÆÆ notnot runningrunning atat nativenative speed!speed!
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 55
Virtual
Virtual Machines Machines
Source Code (High Level Language)
Compiler produces bytecode
VIRTUAL MACHINE
MISC HARDWARE/SOFTWARE
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 66
New New era era of of VM VM
JVM and JVM and CLR CLR
••Use JITUse JIT ((Just-Just-InIn--Time) to Time) to produceproduce well-well- optimized
optimized nativenative machinemachine codecode
••BytecodeBytecode (portability(portability) ) + +
•
• nativenative codecode generationgeneration (speed(speed)) ++
••BytecodeBytecode verifierverifier
••StackStack basedbased –– doesn’tdoesn’t nownow thethe meanmean ofof registers
registers
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 77
Common
Common Language Language Runtime Runtime
Microsoft marketing Æ Microsoft marketing Æ .NET (DotNet .NET ( DotNet) )
Is Is nothing nothing more than more than a virtual a virtual machine
machine, , however however.. ..
Based Based on on CLI CLI Æ Æ a standard designed a standard designed from from the the scracth scracth to support to support multiple multiple languages
languages
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 88
Common
Common Language Language Infraestructure Infraestructure
International International Standard (ECMA) Standard (ECMA)
CLI Specification CLI Specification
CTS (Common Type System) CLS (Common Language Specification) CIL (Common Intermediate Language)
VES (Virtual Execution System)
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 99
CTS (
CTS (Common Common Type Type Specification) Specification )
A rich A rich type type system system that that supports supports the the types
types and and operations operations found found in in many many programming
programming languages languages
To support To support a wide a wide range of range of programming programming languages
languages
Defines a set Defines a set of of rules rules for types for types
Designed Designed for object for object oriented, oriented , procedural procedural and and functional functional languages languages
More More than than 15 languages 15 languages implemented implemented CTS CTS
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1010
CLS (
CLS (Common Common Language Language Specification) Specification )
A subset A subset of of CTS CTS
Defines the Defines the rules rules for each for each individual individual language
language interop interop with with each each other other
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1111
CIL (
CIL (Common Common Intermediate Intermediate Language) Language )
AlsoAlso knownknown as ILas IL oror MSIL (Microsoft)MSIL (Microsoft)
A A languagelanguage more “highermore “higher” ” thanthan nativenative instruction
instruction setset
BasedBased onon metadatametadata –– self describingself describing
AnAn assemblyassembly orientedoriented languagelanguage
•
• CanCancreatecreateinstancesinstances ofof objectsobjects
•• CallCallvirtual methodsvirtual methods
•• WorkWorkwithwitharraysarrays
•• ThrowThrow andand catchcatch exceptionsexceptions!!
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1212
CIL Example CIL Example
ldargnum load argument no. num onto the stack
…Æ…, value
ldarg.1
public static void TestMethod (int a, int b) // arguments {
int c; int d; int e; // locals c = a + b;
d = 10;
e = c + d;
}
ldlocindx load local variable no. indx onto the stack
…Æ…, value
ldloc.1
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1313
CIL CIL Example Example
ldcnum load numeric constant
…Æ…, num
ldc.i4 10
stargnum store a value in an argument slot
…, value Æ…
starg.1
stloc indx pop value from stack to local variable
…, value Æ…
stloc.0
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1414
public static void TestMethod (int a, int b) {
int c; int d; int e;
c = a + b;
d = 10;
e = c + d;
}
.method public hidebysig static void TestMethod(int32 a,int32 b) cil managed
{
// Code size 12 (0xc) .maxstack 2 .locals init ([0] int32 c,
[1] int32 d, [2] int32 e) IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: add IL_0003: stloc.0 IL_0004: ldc.i4.s 10 IL_0006: stloc.1 IL_0007: ldloc.0 IL_0008: ldloc.1 IL_0009: add IL_000a: stloc.2 IL_000b: ret
} // end of method Class1::TestMethod
evaluation stack
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1515
VES (Virtual
VES (Virtual Execution Execution System) System )
Implements Implements and and enforces enforces CTS CTS
Will Will load load and and run run programs programs written written for the for the CLI CLI (with ( with IL code IL code) )
Handles Handles all all the the major overheads major overheads of of traditional
traditional programming programming models models
How How it it will will load? load ?
••JITJIT
••InstallInstall timetime compilerscompilers
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1616
CLR CLR
Is Is the the Microsoft VES (Virtual Execution Microsoft VES (Virtual Execution System
System) for CLI ) for CLI
CLR will CLR will use: use:
••ManagedManaged modulesmodules
••AssembliesAssemblies
••JIT compilersJIT compilers To
To runrun CLICLI--compliantcompliant programmsprogramms intointo Win32 Win32 andand IntelIntel x86 architecturesx86 architectures..
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1717
CLR CLR Execution Execution Model Model
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1818
During
During Development... Development ...
Ok.. Some Ok .. Some code code... ...written written with with notepad: notepad :
using
usingSystemSystem;; class
classHelloHello {
{
public
publicstaticstaticvoidvoidMain()Main() {
{
System.Console.WriteLine(
System.Console.WriteLine(""HelloHelloworldworld!");!");
System.Console.WriteLine(“Bye
System.Console.WriteLine(“Byeworld!”); world!”);
}} }}
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 1919
Compiling
Compiling the the source source code code
csccsc ola.ilola.il ((managedmanaged code)code)
Result: Result: ola.exeola.exe ÆÆ AssemblyAssembly!!
ResultingResulting file file isis a PE (Portablea PE (Portable Executable)Executable)
Managed module (IL and metadata) Managed module (IL and metadata)
Resource file (.jpeg, .gif, etc.)
Resource file (.jpeg, .gif, etc.)
Tool combining multiple managed modules
and resource files into
an assembly C# compiler (CSC.exe), Visual Basic
Compiler (VBC.exe), Assembly Linker
(AL.exe)
…
Assembly (Manifest describes the
set of files in the assembly) Managed module (IL and metadata) Managed module (IL and metadata) Resource file (.jpeg, .gif, etc.)
Resource file
(.jpeg, .gif, etc.) (c) João Ferreira (c) João Ferreira --ICCA 2004ICCA 2004 2020
User User executes executes ola.exe ola.exe ... CLR ... CLR starts starts ! !
PE header .text section .idata section
CLR header IL Metadata Ola EXE
JMP _CorExeMain
DLL: MSCorLib.dll Function: _CorExeMain
Process's primary Process's primary
thread starts thread starts
Managed EXE
MSCorEE.dll Address Space
1.
1. MSCorEEMSCorEEexamines CLR header to get Main method's metadata token.examines CLR header to get Main method's metadata token.
2.
2. MSCorEEMSCorEEexamines the Main's metadata to get location of IL within the EXE.examines the Main's metadata to get location of IL within the EXE.
3.
3. MSCorEEMSCorEEcompiles Main's IL to native CPU.compiles Main's IL to native CPU.
4.
4. MSCorEEMSCorEEjumps to Main's (using primary thread) –jumps to Main's (using primary thread) –the application runs.the application runs.
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2121
Ok Ok .. We .. We got got the the IL.. And IL.. And now?? now ??
..methodmethod publicpublic hidebysighidebysig staticstatic voidvoid Main() Main() cilcil managedmanaged {{
.entrypoint.entrypoint //
// CodeCode sizesize 21 (0x15)21 (0x15) .
.maxstackmaxstack 11 IL_0000:
IL_0000: ldstrldstr "Hello"Hello world!"world!"
IL_0005:
IL_0005: callcall voidvoid [mscorlib[mscorlib]]System.ConsoleSystem.Console::::WriteLine(stringWriteLine(string)) IL_000a:
IL_000a: ldstrldstr "Bye"Bye world!"world!"
IL_000f:
IL_000f: callcall voidvoid [mscorlib[mscorlib]]System.ConsoleSystem.Console::::WriteLine(stringWriteLine(string)) IL_0014:
IL_0014: retret } //
} // endend ofof methodmethod Hello::Hello::MainMain
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2222
JIT will JIT will translate translate IL to CPU instructions IL to CPU instructions
MSCorEE.dll ola.exe
static void Main() { Console.WriteLine("Hello Console.WriteLine("HelloWorld");World");
Console.WriteLine("Bye World");
}
Method Structure to Console
JitCompiler static void WriteLine()
JitCompiler
static void WriteLine(string)
JitCompiler (remaining members)
1. JITCompiler function {
2. In the assembly that implements thetype (Console) look up the method (Writeline) being called in metadata.
3. From the metadata, get the ILfor this method.
4. Allocate the block of memory.
5. Compile the IL into native CPU instructions; the native code is savedin 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 blockallocated in step 4.
7. Jump to the native codecontained inside the memory block.
8. }
Native CPU instructions
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2323
Already
Already Jited? Jited ?
MSCorEE.dll
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. }
Managed.exe
static void Main() { Console.WriteLine("Hello");
Console.WriteLine("GoodBye");
}
Console
JitCompiler static void WriteLine()
Native
static void WriteLine(string)
JitCompiler (remaining members)
Native CPU instructions
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2424
JIT Performance
JIT Performance and and Issues Issues
Platform Independence Platform Independence
••Realized when high-Realized when high-level language level language compilers
compilers convert source codeconvert source code to platform to platform agnostic
agnostic MSIL codeMSIL code
••The application or software componentThe application or software component is is distributed in this form
distributed in this form
••JIT compiles to native code either at JIT compiles to native code either at runtime
runtime or at install timeor at install time
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2525
JIT Performance
JIT Performance and and Issues Issues
Language Interoperability Language Interoperability
••Occurs when Occurs when different languagedifferent language compilers compilers compile to
compile to languagelanguage--agnostic MSIL codeagnostic MSIL code
••MetadataMetadata and the Common Type Systemand the Common Type System play a major role in cross
play a major role in cross--language and language and platform independence
platform independence
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2626
JIT Performance
JIT Performance and and Issues Issues
Runtime Stack Manipulation Runtime Stack Manipulation
••The JIT CompilerThe JIT Compiler populates important data populates important data structures for object tracking and specific structures for object tracking and specific stack
stack--frame constructionframe construction
••The JIT Compiler can be used to identifyThe JIT Compiler can be used to identify specific code elements as they are
specific code elements as they are consumed
consumed, i.e., exception handlers and , i.e., exception handlers and security descriptors (Verifier)
security descriptors (Verifier)
••Doesn’t need registersDoesn’t need registers
••CPU independentCPU independent
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2727
JIT performance
JIT performance and and issues issues
Small Memory Footprint Small Memory Footprint
••JIT compilation takes advantage of the JIT compilation takes advantage of the possibility that
possibility that some code may never be some code may never be usedused
••The JIT Compiler compiles methods only as The JIT Compiler compiles methods only as needed
needed
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2828
JIT Performance
JIT Performance and and Issues Issues
JIT compiler knows more about the execution JIT compiler knows more about the execution environment
environment than an unmanaged compiler than an unmanaged compiler would know
would know
JIT compiler can take advantage of JIT compiler can take advantage of instructions
instructions offered by the chip that the offered by the chip that the unmanaged compiler knows nothing about unmanaged compiler knows nothing about
JIT compiler could detect that a certain test is JIT compiler could detect that a certain test is always false
always false, and short, and short--circuitcircuit
The CLR could profileThe CLR could profile the code’s execution the code’s execution and recompileand recompile the IL on the fly reducing the IL on the fly reducing branching, etc.
branching, etc.
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 2929
The The problems problems ... ...
Unmanaged code Unmanaged code is pre- is pre -compiled and compiled and can just execute
can just execute
Managed code requires 2 compilation Managed code requires 2 compilation phases
phases
••Compiler produces ILCompiler produces IL
••IL compiled to native code at runtime, IL compiled to native code at runtime,
requiring more memory to be allocated, and requiring more memory to be allocated, and additional CPU cycles
additional CPU cycles
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 3030
Alternatives
Alternatives to JIT to JIT
NGEN.EXE – NGEN.EXE – Install Install Time Time tool tool to to create
create a native a native image image into into the the native native cache
cache
Native Native Image Image Æ Æ a file containing a file containing compiled
compiled processor- processor -specific specific machine machine code code
Good Good for for heavy heavy startup startup applications applications
Use it Use it at at client client -side - side
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 3131
Unix Unix with with .NET .NET
Mono Project Mono Project
www.go
www.go- -mono.com mono.com
DotGnu DotGnu
http://www.gnu.org/projects/dotgnu http://www.gnu.org/projects/dotgnu
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 3232
What What they they did? did ?
DesignedDesigned a compilera compiler generatinggenerating IL IL codecode (ECMA 334)
(ECMA 334) –– C# takingC# taking note note ofof CTS CTS andand CLS CLS rulesrules
A A VESVES withwith JIT’sJIT’s
A A base base classclass librarylibrary resides inresides in thethe `mcs'`mcs' module
module inin thethe directoydirectoy `class'. `class'. EachEach directory
directory inin thethe directorydirectory representsrepresents thethe assembly
assembly wherewhere thethe codecode belongsbelongs to, andto, and inside
inside eacheach directorydirectory theythey divide thedivide the codecode based
based onon thethe namespacenamespace theythey implementimplement. .
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 3333
DotGnu DotGnu
Created Created ilrun ilrun Æ Æ interpret interpret programms programms in in the the CIL CIL bytecode bytecode format format (Ecma ( Ecma 335) 335)
CIL CIL Æ Æ converted converted to CVM to CVM (Converted ( Converted Virtual
Virtual Machine Machine) )
Cscc Cscc – – compiler compiler ANSI C and ANSI C and Ecma Ecma 334 334
(c) João Ferreira
(c) João Ferreira --ICCA 2004ICCA 2004 3434
Conclusion Conclusion
CLI CLI is is a standard – a standard – if if you you want want portability
portability, , all all languages languages should should be be CLI- CLI - compliant
compliant
Want Want run run CLI? CLI?
••makemake youryour ownown VES!VES!
••MakeMake youryour JIT!JIT!