Monday, November 16, 2015

The .NET Framework Compilation-Execution Process

          A compiler is a program that convert source codes written in high-level programming language to a machine language that a computer can understand. Compilation can either be explicit or implicit. Explicit compilation is a process where a program is compiled directly to machine code. The source code is compiled to an object file which is then linked to a an executable(.EXE) or library(.DLL) file which the computer can immediately process during runtime . Implicit compilation on the other hand compiles the source code to an intermediate language which is then compiled to native code usually by a virtual machine prior execution. The compilation process of .NET Framework follows that of the Implicit compilation process.

          When a program written under the .NET framework is compiled, the high-level language is converted to an intermediate language - Common Intermediate Language (also known as Microsoft Intermediate Language). The output of the compilation is called an Assembly which contains IL instructions, type metadata, assembly manifest and a set of resources. Program written in different languages are almost identical because CIL is composed of platform-independent set of instructions. This makes assemblies work on any platform that .NET framework supports. However, take note that if the code calls platform-specific APIs or class library, it will only run on that specific platform.

          CIL is then converted to machine code by either using Just-In-Time (JIT) compiler at the Common Language Runtime (.NET's virtual machine) during runtime or by using the Native Image Generator (Ngen.exe) before the application runs. JIT compiles the IL instructions on demand at application runtime to platform-specific machine-level instructions. On demands means that it will only convert methods as needed during execution and stores it in memory for easy access during subsequent calls of that process. This saves time and memory from converting the whole assembly to native code but the disadvantage is that the stored native code is bound to the process that triggered the compilation. To allow sharing of the generated code across multiple processes, CLR allows for ahead-of-time compilation mode through the Ngen.exe. The Ngen.exe performs compilation of CIL to native code before running the application. It compiles the entire assembly in one go and persists the generated code in the Native Image Cache as a file on disk.

          During the compilation of CIL code to native code, the CIL code is also verified to examine if the IL instructions and metadata are type safe. A type safe code basically means that the code only accesses the memory location it is allowed or authorized to access. This helps isolate objects from each other and protect them from corruption. This code verification can be bypassed by establishing a security policy to do so by the administrator. If the code verification is not bypassed and the code fails the type safe verification, and exception is thrown when the code is run.

          When application is executed, the CLR via mscoree.dll will load the assembly into the memory and the references of the assembly based on its manifest. The CLR will also provide the needed services like garbage collection, security, interoperability with unmanaged code, cross-language debugging support, enhanced deployment and versioning support during execution. Each method called is JIT compiled as mention earlier until the execution is complete. In case of Ngen.exe generated native code, the Native Image Cache is used for each method. 

The .NET Framework

          The .NET Framework is a software development environment created by Microsoft. It is composed of tools, technologies and languages used to build and deploy robust desktop and web applications.
 
         
          At the bottom of the .Net Framework structure is Common Language Runtime (CLR)which handles code execution. Above the CLR layer is the .NET Base Class Library which contains runtime libraries that support common functions like file reading and writing, XML document manipulation, exception handling, application globalization, network communication, threading and reflection, which makes the programmer's job easier. ADO.NET and XML layer supports tasks related to data access, parsing, manipulation and generating XML. The next layer, ASP.NET and Windows Forms build robust web applications and standard Windows applications as well as develop and consume web services. Common Language Specification (CLS) then facilitates interoparability between languages. It defines the reasonable subset of Common Type System (CTS), the shared data type that serves a great role in cross-language integration. The flexibility of CTS make many languages adapt to .NET platform.