CLR (Common Language Runtime) and IL (Intermediate code) in C #

Source: Internet
Author: User
Tags mscorlib return tag
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.

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 directive 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, which is code running on the CLR runtime. Method private Hidebysig static void Main (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: When the Maxstack 2//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] int32 num, [1] int32 num2, [2] int32 num3)//Represents a variable that defines the type int, and the variable name is NUM,NUM2,NUM3 respectively.    stored in the call stack.    L_0000:nop//no operation means that nothing is done.    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.2 l_0004:stloc.1 l_0005:ldc.i4.3 l_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 the addition operation L_000A:LDLOC.2//Take the element with position 2 in the call stack and press the evaluation stack (the value of k is taken).    L_000b:add//Do addition operation L_000c:call Void [Mscorlib]system.console::writeline (Int32)//Call output methodL_0011:nop//no Operation L_0012:call ValueType [Mscorlib]system.consolekeyinfo [Mscorlib]system.console::readkey] Call the Readkey method L_0017:pop//Empty the contents of the evaluation stack L_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 ("Before jited.");            Console.ReadLine ();             SomeMethod ();             Console.WriteLine ("After Jited");            Console.ReadLine ();}}}    

The main method of the IL code corresponding to it:

. method private Hidebysig static void Main (string[] args) CIL managed{    . entrypoint    . maxstack 8    //allocation string " Before jited "    l_0000:ldstr" before jited. "    Call Console.WriteLine Method    L_0005:call void [Mscorlib]system.console::writeline (string)    // Call the Console.ReadLine method    L_000a:call string [Mscorlib]system.console::readline ()    l_000f:pop    // Call the Program.somemethod method    L_0010:call void Testconsole.program::somemethod ()    //Assign string "After Jited"    L_ 0015:ldstr "After jited"    //Call Console.WriteLine method    L_001a:call void [Mscorlib]system.console::writeline ( String)    //Call Console.ReadLine method    L_001f:call string [Mscorlib]system.console::readline ()    L_0024:pop    L_0025:ret}

These are the contents of the CLR (Common Language Runtime) and IL (Intermediate code) in C #, and more about topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.