1.1.1 Summary
We know that computers cannot directly understand advanced languages. They can only understand machine languages. Therefore, we must translate advanced languages into machine languages so that computers can execute advanced languages.ProgramIn the next blog, we will introduce the compilation process of unmanaged and hosted speech.
1.1.2 compilation process of the text in an unmanaged environment (C/C ++)
A pure C/C ++ program usually runs in an unmanaged environment, and the class is composed of header files (. h) and implementation file (. CPP), each class forms a separate compilation unit. When we compile the program, several basic components willSource codeTranslate to binaryCodeNext, we will describe the compilation process of the unmanaged environment through slices:
Figure 1 C/C ++ compilation process
The first is the pre-processor. If there are header files and macro expressions in the project, it will be responsible for including header files and translating all macro expressions.
Next, the compiler, instead of directly generating binary code, generates assembly code (. s), which is basically the basis of all modern unstructured languages.
Then, the assembler translates the assembly code into the target code (. O and. OBJ files, machine commands ).
Finally, the linker links all target files related to each other and the generated executable files or libraries.
In general, our code is first translated into assembly code, and then into machine commands (Binary Code ).
What is a macro?
In C/C ++, macros are pre-processing commands, which have a variety of application technologies, including pre-definition, keyword creation, and Conditional compilation. In general, these technologies are considered a bad practice in C ++, mainly because they may abuse the syntax change function provided by C ++, it is even possible that a non-standard language is created without your knowledge. Macros do not follow general source code compilation rules because they are processed through preprocessing rather than the compiler.
Compiling process of the hosting environment (C #/Java)
In the hosting environment, the compilation process is slightly different. We are familiar with the hosting languages C # And java. Next, we will take C # and Java as examples to introduce the compilation process in the hosting environment.
When we write code in our favorite IDE, the first thing that detects our code is IDE (lexical analysis). Then, compile it into the target file and link it to the dynamic/static library or executable file for re-check (syntax analysis). The last check is the runtime check. A common feature of the hosting environment is,The compiler does not generate binary code, but the intermediate metacode.Is called msil-Microsoft intermediate language in. Net or bytecode in Java.
After that, the JIT (Just In Time) compiler translates msil into binary code at runtime, which means that our code is parsed only when it is actually used, this allows us to pre-compile and optimize our code in CLR (runtime in public languages) to improve program performance, but increases the startup time of the program, we can also use native image generator to pre-compile our program to shorten the startup time, but there is no benefit from runtime optimization.
Figure 2 C # compilation process
. Net Framework adds an abstraction layer on Win32 core. It provides support for multiple languages, JIT optimization, automatic memory management, and improved security. Another complete solution is winrt, however, this involves another topic, which is not described in detail here.
Figure 3 Windows API
Advantages and disadvantages of JIT compilation
JIT compilation brings many benefits. The biggest one is in my opinion the performance advantage. It allows CLR (a universal language runtime plays the role of the javaser component) to only execute the required code, for example: suppose we have a very large WPF application. Instead of loading the entire program immediately, but when the CLR starts to execute, different parts of our code will translate the cost commands in an efficient way, because it can check the system JIT and generate Optimized Code, rather than following a predefined pattern. Unfortunately, a disadvantage is that the startup process is slow, which means it is not suitable for loading long packages.
The JIT alternative solution uses ngen
If Visual Studio is created by JIT, it will take several minutes to start. On the contrary, if it is compiled using native image generator, it will create a pure binary executable file, if you only consider the speed issue, it is definitely the right choice.
1.1.3 Summary
In an unmanaged environment, we need to know that the compilation process is divided into two phases: Compilation and connection. In the compilation stage, the source program (*. C ,*. CPP or *. h) convert to the target code (*. O or *. OBJ file). The specific process is the first three stages of the C/C ++ compilation process mentioned above. The link stage is the target code (OBJ file) converted from the previous step) link the code corresponding to the library function called in our program to form the corresponding executable file (exe file ).
In a hosted environment, the compilation process can be divided into lexical analysis, syntax analysis, intermediate code generation, code optimization, and target code generation. net or Java, they all generate intermediate code (msil or bytecode), then translate the optimized intermediate code into the target code, and finally the JIT translates the IL into machine code when the program is running.
Whether hosted or unmanaged languages, their compilation and compilation processes translate advanced languages into machine codes that computers can understand, since the compilation process involves a wide range of knowledge (compilation principles and hardware knowledge), and my own capabilities are limited, I can only briefly describe these processes. If you want to have a deep understanding of the compilation principles, I suggest you take a look at compilation principles.
Reference
[1] http://www.developingthefuture.net/compilation-process-and-jit-compiler/