Managed Execution Process in .NET Framework
The following article will include the steps involved in the process of Managed Execution perform by the CLR(Common Language Runtime) in .NET framework.
In brief, the execution works by converting our .NET code into intermediate code(MSIL) and then convert this intermediate code to the machine level code.
Lets see what are the steps involved in the execution process:
Compiler is a multi language execution environment which is responsible to target the run time data types and syntax used in the code. It can be VB, C#, Visual C++, jScript, Eiffel or COBOL compiler(third party).
It has CLS( Common Language Specification) which determines the common types information and specification found in most of the programming languages under the .net framework.
Microsoft Intermediate Language
The CLR(Common Language Runtime) is responsible to translate our source code into an Intermediate level code. This intermediatory is known an MSIL(Microsoft Intermediate Language). As we know that .NET supports more than 40+ languages, hence it was necessay to convert all these available languages into an intermediary so that transferring to CPU specific code by JIT compiler will not be an issue.
MSIL is a CPU Independent set of instructions that contains commands for the following:
Loading the objects
Storing the objects
Initializing the objects
Calling method on objects
Consist of instruction for Arithmetic and Logical Operations
Direct Memory access
Just In Time Compiler
JIT is responsible to convert the MSIL code to the CPU specific code. JIT can be used to compile and execute MSIL code on any supported architecture.
MSIL code is present in the Portable Executable(PE) File in conjunction with Common Object File Format(COFF). JIT also produces Metadata ( data about types, definition and signature of code). The CLR produces the metadata from the files during execution
CLR also provides a type of compilation mode called as Install Time generation. It does the same thing like JIT but it convert larger amount of code at a time and stores the resulting CPU specific code when the assembly is subsequently loaded and executed. The entire assembly being installed is converted in to the native code. Hence resulting solution proves to be more quicker than the traditional JIT compiler.
The compiling MSIL code to Native code also passes a verification process by examining MSIL and metadata to be type safe (authorized memory access) to ensure objects are isolated from each other thereby reducing malicious activities. It also avoid the memory locations on being overrun.
Final Code execution
The infrastructure is provided by the CLR when each method in MSIL compiled by the JIT is called for the first time by the CPU specific code. Next time the existing JIT compiled code is execute and the process continues until the execution is complete
Also manage code must be provided with some features like automatic memory management to avoid leaks and overrun, security for type safety, interoperability with unmanaged code, cross language debugging and versioning.