Analysis of the Relationship between the intermediate language (IL) and the real-time Compiler (JIT)

Source: Internet
Author: User
For beginners of. Net, a very confusing question is: What is the process from advanced languages (such as C # and Visual Basic) to managed languages to machine languages. Understanding this process is also the key to understanding. NET language interoperability (that is, the core principle of language independence) and is also related to binary compatibility. Although this book has been trying not to explore the implementation of these underlying details, it mainly focuses on how to best apply them. net. However, an overview of the CLR code generation process is helpful for understanding her internal mechanism. In addition, understanding how. Net generates code can also help solve some special security problems.

. NET language compilation is divided into two phases. first, advanced languages are compiled into an intermediate language called Il. Compared with advanced languages, Il is more like machine languages. However, il contains some abstract concepts (such as classes and exceptions ), this is also why this language is called an intermediate language. Il is packaged in a DLL or EXE file, while DLL and exe are in. the main difference in net is that only EXE can be directly run, and both can be dynamically loaded by a running process (this article will be detailed later ). Since the machine's CPU can only execute local assembly language, rather than Il, further compilation of IL into assembly language (that is, the second stage) needs to be done at runtime, this process is completed by the instant Compiler (JIT.

When a high-level language is compiled for the first time, the compiler will do two things: first, store the compiled il in the DLL or EXE, and then create a stub function for each method of the class, this function calls the Real-Time compiler and sends its own address as a parameter to the compiler. The instant compiler obtains the corresponding il from the DLL or EXE, compiles it into the machine language, and replaces the original zero-time calling function in the memory with the machine language. The idea of this process is to use compiled methods to call uncompiled methods. In essence, Stub function is called. Stub function then calls the compiler to compile itself into a local machine language; finally ,. net will call this method again, and the method will be actually executed. When a function is called repeatedly, the machine command is executed directly, and it takes only time for the compiler to compile the method for the first time. Methods that are not called will not be compiled.

After the compiler generates an EXE file, the program's entry function is the main () method. The loader loads this EXE file and detects that this is a hosted EXE file. Therefore, it loads the. NET Runtime Library file (including the instant compiler) and then calls the main () method of exe. This will trigger instant compilation of the main () method. The main () method is replaced with the local machine language in the memory, so the. NET application starts to run. After being compiled into a local language, the application can freely call local code. When the program is aborted, the local code is released from the memory, so the Il needs to be re-compiled by the instant compiler at the next run.

You may be worried about the impact of instant compilation on efficiency. However, this concern should be mitigated because this compilation method is faster than the traditional static code compilation method. For example, even if the compiler distinguishes a specific type of CPU (such as Pentium III and Pentium IV) and exploits the new instruction set based on the CPU type. In comparison, the code generated by traditional compilers must depend on the minimum standard, such as the 386 instruction set. Therefore, the functions of the CPU of a newer model cannot be used. Future real-time compilers may be rewritten to adapt to the application's encoding methods (such as the use frequency of branch commands, branch prediction, and so on), and then re-compile the code, to optimize a special application (or even a user !) How to use components. The instant compiler can also optimize its own code based on actual machine resources (such as memory and CPU speed. Please note that although these advanced features are not yet implemented, future mechanisms are likely to provide all of these capabilities. In short, the optimization process of the instant compiler is to increase the compilation time in exchange for the application execution efficiency. The effect of this optimization depends on the method and purpose of calling the program. In the future, this cost can be measured during the application installation process, or you can find it from the user preference library. Intermediate Language (IL) and instant Compiler (JIT)
This article is taken from the http://www.lan27.com/Article/200711/5763.htm

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.