Code Execution Process

Code Execution Process

The Code Execution Process involves the following two stages:

  1. Compiler time process.
  2. Runtime process.

Code Execution

Compiler time process

  1. The .Net framework has one or more language compilers, such as Visual Basic, C#, Visual C++, and JScript, or one of many third-party compilers such as an Eiffel, Perl, or COBOL compiler.
  2. Any of the compilers translates your source code into Microsoft Intermediate Language (MSIL) code.
  3. For example, if you are using the C# programming language to develop an application when you compile the application, the C# language compiler will convert your source code into Microsoft Intermediate Language (MSIL) code.
  4. In short, VB.NET, C#, and other language compilers generate MSIL code. (In other words, compiling translates your source code into MSIL and generates the required metadata.)
  5. Currently "Microsoft Intermediate Language" (MSIL) code is also known as the "Intermediate Language" (IL) Code or "Common Intermediate Language" (CIL) Code.
    SOURCE CODE -----.NET COMPILER-----> BYTE CODE (MSIL + METADATA)
    

Runtime process

  1. The Common Language Runtime (CLR) includes a JIT compiler for converting MSIL to native code.
  2. The JIT Compiler in CLR converts the MSIL code into native machine code that is then executed by the OS.
  3. During the runtime of a program, the "Just in Time" (JIT) compiler of the Common Language Runtime (CLR) uses the Metadata and converts Microsoft Intermediate Language (MSIL) into native code.
    BYTE CODE (MSIL + META DATA) ----- Just-In-Time (JIT) compiler------> NATIVE CODE

Notes

When you compile a C# application or any application written in a CLS-compliant language, the application is compiled into MSIL. This MSIL is then further compiled into native CPU instructions when the application is executed for the first time by the CLR. (Actually, only the called functions are compiled the first time they are invoked.)

  1. You write source code in C#.
  2. You then compile it using the C# compiler (csc.exe) into an EXE.
  3. The C# compiler outputs the MSIL code and a manifest into a read-only part of the EXE that has a standard PE (Win32-portable executable) header.
  4. When the compiler creates the output, it also imports a function named "_CorExeMain" from the .NET runtime.
  5. When the application is executed, the operating system loads the PE, as well as any dependent dynamic-link libraries (DLLs), such as the one that exports the "_CorExeMain" function (mscoree.dll), just as it does with any valid PE.
  6. However, since the operating system obviously can't execute the MSIL code, the entry point is just a small stub that jumps to the "_CorExeMain" function in mscoree.dll.
  7. The "_CorExeMain" function starts the execution of the MSIL code that was placed in the PE.
  8. Since MSIL code cannot be executed directly, because it's not in a machine-executable format, the CLR compiles the MSIL using the Just-In-Time (JIT) compiler (or JITter) into native CPU instructions as it processes the MSIL. The JIT compiling occurs only as methods in the program are called. The compiled executable code is cached on the machine and is recompiled only if there's some change to the source code.