Introduction to the CLR

The CLR
The Common Language Runtime (CLR) coupled with the Common Type system (CTS) provides the basis for numerous languages to work on the .NET Framework. But what is the CLR? It is an environment that compiles and runs executable code similarities to the Java Runtime. The CLR Performs its compilation on Managed code; this provides type safety, garbage collection (memory management) and a number of other useful features. This makes it easier to develop applications, and gives the developer a level of abstraction from the system.

Compilation Process?
So how is the CLR compilation process different from that of the previous Win32 DLLs and EXEs?  When compiling source code using  a Win32 Compiler (Fig 1.1) the file that is created contains x86 machine code. In the case of CLR compilation (Fig 1.2) the file contains Microsoft Intermediate Assembly Language (MSIL) [Instructions defined by MSIL] . MSIL is a machine independent assembly language. What this boils down to is a language that is very close to what a WIN32 compiler would produce, but has a number of beneficial characteristics. These include: being easily human readable, contains meta-data about compiled code, can simply be compiled down to x86 instruction code!

 

Fig 1.1 - WIN32 Compilation Process

[Source Code] >> [Compiler] >> [Machine Code]

Fig 1.2 - CLR Compilation Process

[Source Code] >> [Compiler] >> [MSIL]

 

Trivia:

Q:- When I Compile Win32 Code I get an .exe, but when I compile Managed code I also get a .exe! What gives?

A:- Even though both files contain different infromation, they are both compiled down to a Portable Executable file (PE). These portable executable files can be run on most WIN32 Operating systems, hence the name. Even though the file type is the same, the DO NOT contain the same 'data'. Rather they contain x86 assembly and MSIL respectively.

MSIL!?

So, now that we have our MSIL code, how does this execute on my machine? Well, in proper Microsoft tradition we have another acronym, the CLR uses the Virtual Execution System (VES). The VES takes care of loading, and then running applications written in managed code. The VES in encapsulated by a number of core features such as a set of built-in data structures, defines a hypothetical machine, control flow constructs and exceptions. [A great Introduction to MSIL]

So how is this MSIL compiled? The MSIL is compiled using Just-in-Time compilation  (JIT). What this means is that MSIL is compiled as it is executed, and then cached for the duration of the applications execution lifetime. This results in a small hit the first time a  piece of code is executed, but results in quicker time on further executions. Compiled on the fly?! Doesn’t that mean slow? Well, in some interpreted languages this would be true but due to the construction of MSIL the performance hit is low:
• Only code that you use is compiled.
• MSIL has a close mapping to machine code and the compilation process that is undertaken by a compiler such as a Win32 is quite different.

So what about all these languages…

The CLR provides the functionality to use any CLR-compliant language in an interoperable fashion. This boils down to letting us use VB .NET classes with C#, or even Execute a C# DLL in Iron Python! So how is this all achieved? To be a CLR-compliant language the compiler writer must implement the Common Type System (CTS) and a number of rules. This allows the language to be usable by other CLR-compliant languages. The CTS and rules are flexible enough to allow languages of different methodologies to be implemented. [Learn all about the CTS/CLS and VES here - https://download.microsoft.com/download/f/9/a/f9a26c29-1640-4f85-8d07-98c3c0683396/partition_i_architecture.zip]

So what languages are implemented using the CLR?
ADA, C, C++, C#, Cobol, Delphi, Fortran and many others. Check out this blog for a list and links to various implementations. https://blogs.ugidotnet.org/nettools/articles/8060.aspx

Well, I hope that has got your clued up on the CLR, and ready to attack the rest of this newsletter!