CLR in the. NET Platform
The first thing to note is that. NET platform is not the same as C # it is a platform for C#,vb.net and other programs to run.
The CLR is the common language runtime and is an important part of the. NET Framework. It provides services such as memory management, thread management, and exception handling, and is responsible for enforcing strict type safety checks on the code to ensure that the code is correct.
In fact, many of the features in C #, such as type Checker, garbage collection (garbage Collector), exception handling (Exception Manager), backward compatibility (COM Marshaler), are provided by the CLR.
What is IL
The. NET framework is a virtual running platform on the Windows platform, and you can imagine swapping the most down-level windows for other operating systems, such as Linux, to implement CLS-compliant (Common Language Specification, the common Language specification). NET language, which is actually the function that mono plans to implement. Thus, in theory, C # is a language that can be cross-platform.
C # Another Place like Java is that it is also a (special) language, as in Java, C # programming code is first compiled by the C # compiler into a special byte code, (Microsoft intermediate Language,msil, Microsoft) Intermediate language, the runtime is then compiled into machine code by a specific compiler (JIT compiler, Just in time, Jiter) for the operating system to execute. (Introduction to JIT.) Can read this blog, please click here)
Il is an intermediate language,. The compilers of various high-level languages (such as c#,vb,f#) on the net platform convert their text representations to IL. The various forms of writing are eventually unified to the way IL is expressed.
After the CLR loads the IL, when each method is executed for the first time, the IL code is compiled into a machine code using JIT, and the machine code and the assembly are actually one by one corresponding, it can be understood that: The assembly is the text representation of the machine code, provides some convenient memory of the "mnemonic."
For the same il,jit it will generate different machine codes for different CPU architectures (such as X86/IA64, etc.).
C # code and its corresponding IL intermediate code
?
The //hidebysig instruction indicates that if the current class is a parent class, the method marked with the directive will not inherit from the quilt class//cil managed indicates that the code in the method body is IL code and is managed code, that is, code running on the CLR runtime. methodPrivateHidebysigStatic voidMain (string[] args) CIL managed{. entrypoint//The directive represents the entry function of the function program. Each managed application has only one entry function, and the CLR loader starts with the. entrypoint function first. . maxstack2 //Executes the constructor, the evaluation stack can hold the maximum number of data items. The evaluation stack is an area of memory that holds the value of the variable required in the method, which is emptied at the end of the method execution, or stores a return value. . Locals init ([0]Int32Num, [1]Int32NUM2, [2]Int32NUM3)//Represents a variable that defines the type int, and the variable name is NUM,NUM2,NUM3. stored in the call stack. L_0000:nop//no operation means that there is no action. L_0001:ldc.i4. 1 //Press "1" into the evaluation stack, at which point "1" is at the top of the stack on the evaluation stack. L_0002:stloc. 0 This instruction indicates that a value is popped from the evaluation stack and assigned to the NO. 0 variable num of the call stack. L_0003:ldc.i4. 2L_0004:stloc. 1L_0005:ldc.i4. 3L_0006:stloc. 2 //From. Locals init to l_0006, equivalent to C # code for I,J,K assignment. L_0007:ldloc. 0 //Take the element with position 0 in the call stack and press it into the evaluation stack (take the value of i). L_0008:ldloc. 1 //Take the element with position 1 in the call stack and press it into the evaluation stack (take the value of J). L_0009:add//Do addition operationsL_000a:ldloc. 2 //Take the element with position 2 in the call stack and press it into the evaluation stack (the value of k is taken). L_000b:add//Do addition operationsL_000c:callvoid[mscorlib] System.console::writeline (Int32)//Call output methodL_0011:nop//no OperationL_0012:call valuetype [Mscorlib]system.consolekeyinfo [Mscorlib]system.console::readkey ()//Call the Readkey methodL_0017:pop//Clear the contents of the evaluation stackL_0018:ret//return Tag return value}//main Method End
From the above code, we can summarize:. Maxstack: The variables in the code need to occupy several positions in the call stack;. locals int (...) : Defines the variables to initialize and puts them into the call stack, nop:no operation, without any action, ldstr:load string, and presses the strings into the evaluation stack (Evaluation stack); ldc.i4.1: The value 2 is pressed into the evaluation stack in the form of a 4-byte length integer; stloc: The value in the evaluation stack (Evaluation) is assigned to the call stack, ldloc: Call stack stack), the value of the specified position is removed (Copy) into the evaluation stack (Evaluation stack); Call: Invokes the specified method, which is typically used to invoke a static method, while Callvir is typically used to invoke an instance method; Ret:return, the token is returned.
Let's look at one more example.
namespace TestConsole { class program { [MethodImpl (methodimploptions.noinlining)] private static void SomeMethod () {Console.WriteLine ( "Hello world!" ); static void Main (string [] args) {Console.WriteLine ( "Bef Ore jited. "); Console.ReadLine (); SomeMethod (); Console.WriteLine ( "after jited" ); Console.ReadLine (); } }}
The main method of the IL code corresponding to it:
. MethodPrivate hidebysig static void Main (string[] args) CIL managed{. EntryPoint . Maxstack 8assigning strings"Before jited"L_0000:ldstr"before jited."Call Console. WriteLineMethod l_0005:Pagervoid [Mscorlib]system. Console:: WriteLine (String)//Call Console. ReadLineMethod l_000a:Pagerstring [Mscorlib]system. Console:: ReadLine () l_000f:PopCall Program. SomeMethodMethod l_0010:Pagervoid TestConsole. Program:: SomeMethod ()//Assign String"After Jited"L_0015:ldstr"After Jited"Call Console. WriteLineMethod l_001a:Pagervoid [Mscorlib]system. Console:: WriteLine (String)//Call Console. ReadLineMethod l_001f:Pagerstring [Mscorlib]system. Console:: ReadLine () l_0024:Popl_0025:ret}?
CLR (Common Language Runtime) and IL (Intermediate code) in C #