Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Overview of Compilation and Execution in .NET

Posted by Ravi Varma Thumati on May 20, 2010

Compiling to the Common Intermediate Language (CIL)

The compiler for a .NET language takes a source code file and produces an output file called an assembly. An assembly is either an executable or a DLL. The process is illustrated in Figure.

–         The code in an assembly is not native machine code, but an intermediate language called the Common Intermediate Language (CIL).

–         An assembly, among other things, contains the following items:

  • The program’s CIL
  • Metadata about the types used in the program
  • Metadata about references to other assemblies

Fig: The compilation process

The acronym for the intermediate language has changed over time, and different references use different terms. Two other terms for the CIL that you might encounter are IL (Intermediate Language) and MSIL (Microsoft Intermediate Language), which was used during initial development and early documentation.

Compiling to Native Code and Execution

The program’s CIL is not compiled to native machine code until it is called to run. At run time, the CLR performs the following steps:

–         It checks the assembly’s security characteristics.

–         It allocates space in memory.

–         It sends the assembly’s executable code to the Just-in-Time (JIT) compiler, which compiles portions of it to native code.

The executable code in the assembly is compiled by the JIT compiler as it is needed. It is then cached in case it is needed for execution again later in the program. Using this process means that code that isn’t called isn’t compiled to native code, and code that is called is only compiled once.

Fig: Compilation to native code occurs at run time

Once the CIL is compiled to native code, the CLR manages it as it runs, performing such tasks as releasing orphaned memory, checking array bounds, checking parameter types, and managing exceptions. This brings up two important terms:

–         Managed code: Code written for the .NET Framework is called managed code, and needs the CLR.

–         Unmanaged code: Code that does not run under the control of the CLR, such as Win32 C/C++ DLLs, is called unmanaged code.

Microsoft also supplies a tool called the Native Image Generator, or Ngen, which takes an assembly and produces native code for the current processor. Code that has been run through Ngen avoids the JIT compilation process at run time.

The same compilation and execution process is followed regardless of the language of the original source files. Figure 1-5 illustrates the entire compilation and runtime processes for three programs written in different languages.

Fig: Overview of the compile-time and runtime processes

8 Responses to “Overview of Compilation and Execution in .NET”

  1. Diego Pacheco said

    Well explained.

  2. vicky said

    Hi This is vivek Mahajan

  3. divya said

    can understand easily

  4. dhanya said

    well explained. it will be very useful

  5. thiru said

    awesome explanation… got clarified..

  6. chaitanya said

    its really good information for start up people who learn .net

  7. simple and straighforward explaination

  8. sugumar said

    clear explanation
    thanks

Leave a comment