"C # Advanced Series" execution model of the CLR--a story of Hello World

Source: Internet
Author: User

All right, cut the crap. First chapter Hello World diagram:

We have a Hello World program, so simple, plus I'm not programming O (>﹏<) o with kanji today, so it's simple and straightforward.

The story begins:

Compile:

A program to write a certain to compile, before what C ah what is compiled the CPU instructions for the machine, but our C # is not.

C#,vb. NET will compile them into managed modules, managed modules in a standard portable PE file. ( those ignorant teenager must panic, this is what ghost, again is the host module is PE file again.) Don't panic, all the tall terms you don't understand are actually very simple, you don't need to understand now, listen to me slowly.

The so-called PE file is a portable actuator, simply speaking. EXE and. dll This kind of ghost thing ( this exe and C language generated a difference ). This PE file in the managed module, you can as an object, the object has four properties, a PE header (description file), a CLR header (describing the object's overall information, such as the main portal), metadata (this is the key, A metadata table contains descriptive information about those types and members that are defined in the source code, and the other contains descriptive information about the type and members of the reference, and the Il code (which is the code that is compiled for your source codes, also known as the intermediate language).

If you feel trouble, the first two attributes you can forget, remember the next two is good: metadata and IL code (simply speaking, is the code description information and compiled code).

However, a PE file can be more than one managed module, which can consist of several managed modules. The compiler merges multiple managed modules and resource files into an assembly containing the manifest, which is the final PE file.

Run:

But this managed module, the PE file, does not run directly, he needs the CLR.

CLR common shorthand words, CLR is the common language runtime (Common Language runtime) and Java virtual machine is also a runtime environment, it is responsible for resource management ( memory allocation and garbage collection, etc.), and ensure the necessary separation between the application and the underlying operating system. The CLR has two different translation names: the common language runtime and the common language runtime.

This description and subsequent descriptions are from the Baidu Encyclopedia, I am lazy to play.

The core features of the CLR include: memory management, assembly loading, security, exception handling, and thread synchronization.

This thing is also called the common language runtime.

In front of the said can not directly run, here are ignorant teenager certainly said, I generated EXE file can directly run.

This is because the. NET Framework is installed on your computer. When you open the EXE program, the main thread of your process calls the MSCorEE.dll method, which initializes the CLR, loads the EXE assembly, and then calls the entry method, the main function.

So the fact that your code runs is needed on the CLR.

So how does the CLR play our Hello world?

It compiles our IL code at run time. (Don't spit The groove why to compile two times, after the talk)

Before the main function executes, the CLR detects all the types referenced by the code in main, and then generates an internal data structure to manage access to the reference type.

In this internal structure, each type, such as the console, each method will have a portal, each entry has an address (this is called address a bar), this address a can find the implementation code of the method.

When this internal data structure is initialized, all the addresses of these portals are set to a function called Jitcompiler.

As in the first sentence of the Hello World code in the above image, the CLR runs into the Jitcompiler function when executing this code, which is an internal operation:

    1. Runs into the managed module and then, based on the metadata, matches the class name and the method name, gets the address B inside the class of the console that you want to execute WriteLine the function in the IL code.
    2. The IL code is then compiled into a native CPU instruction placed in a memory space with the address C. (Verify that the IL code is safe before this compilation.)
    3. The value of address A in the metadata is then changed to the value of address C.
    4. Finally jump to address C to start executing the CPU instruction of address C.

So what if we're going to execute the WriteLine function the second time? At this point the address of the WriteLine function entry in the internal structure is already the address of the compiled CPU instruction, so it is not going to execute the Jitcompiler function.

Don't think it's going to be slow. Oh, except for the first run, the compilation of Jitcompiler may take time to compile and optimize, and then the local CPU instructions are executed. Plus Jitcompiler This compilation will be based on the CPU of your machine may be to generate some special instructions for the CPU to optimize the IL code, some of which may be faster than the unmanaged program.

Well, the essence of this chapter is the above.

Now let's think about why we compiled two times.

Because of this, whether in C #, VB, F # These things you can generate the same as the IL Code of the managed assembly, and then this managed assembly runs on the CLR, that is, you can mix code, a C # code can call the VB code DLL, in the most appropriate language to do the most suitable things.

And the IL code executed under CLR monitoring will improve the robustness and reliability of the program because it performs a security check before execution.

"C # Advanced Series" execution model of the CLR--a story of Hello World

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.