How does running a .Net application work?

15

There are several terms that I always hear / read when I talk about running .Net applications, such as MSIL , CIL JITer , JITer , JIT among others I should have forgotten.

I would like a brief explanation of how running a .Net application works, along with a brief explanation of the names I cited.

If it makes a difference, I have preference for C # examples and, if possible, examples based on that code (this is to keep a pattern between the answers).

using System;
public static void Main(string[] args)
{
     string nome = "Joaquim da Silva";
     Console.WriteLine(nome);
}  
    
asked by anonymous 17.03.2016 / 20:40

2 answers

12

The .Net Compiler Platform (formerly called Roslyn) is used as the basis for the C # or VB.Net. This can be understood in the question What is a programming language, IDE, and compiler? .

Once compiled, the language itself no longer matters. Everything turns into a "machine code."

After the source code compilation process, an executable is generated (in the background a container , not a common executable such as natively generated by compiler like C ++, for example) IL (Intermadiate Language) which is a binary code we could call machine code CLR (Commom Language Runtime ) and metadata . The CIL is the "official" name of this Common Intermediate Language. MSIL is what some call it. The MS obviously is from Microsoft, since initially the expectation was that this would be a proprietary technology of this company.

One of the components of the CLR is JITter . We could call it JIT Compiler (although the "er" can be explained by the English grammar indicating that it is an executor of some action). JIT stands for Just-In-Time, or "when you need it". This means that it will be invoked when the code is executed. Its role is to transform this machine's built-in machine code (.Net, Mono, etc.) into the machine code of the physical platform where the compiled software is running.

After this process the code, roughly, runs as if it were native from the beginning. JITter has the advantage of always being able to optimize execution with each execution, but it takes longer to initialize the software and requires a more complete and cumbersome environment to track execution.

This environment is implemented by VES (Virtual Environment System).

All this is defined by CLI (Common Language Infrastructure).

This answer might help a little.

The names and versions , which is what, are a bit confusing.

Obviously many doubts can still persist and I suggest new, more specific questions at every point that deserves something dedicated.

The above code is compiled for CIL code (shown in a way for a human to see and not for the computer):

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly '41188d85-05c0-4a3b-bbaf-cfc8464a6216'
{
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.module '41188d85-05c0-4a3b-bbaf-cfc8464a6216.dll'
// MVID: {A36CB87C-35BA-4424-83CC-9E8C29792DDE}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x00BD0000


// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Exemplo
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    // 
    .maxstack  1
    .locals init (string V_0)
    IL_0000:  nop
    IL_0001:  ldstr      "Joaquim da Silva"
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000d:  nop
    IL_000e:  ret
  } // end of method Exemplo::Main

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  nop
    IL_0007:  ret
  } // end of method Exemplo::.ctor

} // end of class Exemplo
    
17.03.2016 / 21:20
5

The managed execution process includes the following steps, which are discussed in more detail later in this topic:

Choosing a compiler.

To get the advantages provided by the common language runtime, you must use one or more language compilers that target the runtime.

Compile your code for MSIL.

Compilation converts your source code into Microsoft intermediate language (MSIL) and generates the required metadata.

MSIL compilation for native code.

At runtime, a just-in-time (JIT) translates MSIL into native code. During this build, the code must pass a verification process that examines the MSIL and metadata to find out if the code can be determined to be strongly typed.

Executing code.

The common language runtime provides the infrastructure that allows execution to occur and services that can be used during execution.

link

    
17.03.2016 / 21:04