1.1 Compiling source code into a managed code module
For example, create a source code file with any language that supports the CLR. Then use a corresponding compiler to check the syntax and parse the source code. The compiler compiles a managed module (Managed module), which is a portable execution file, which may be a 32-bit (PE32) file or a 64-bit (pe32+) file. Managed modules include intermediate languages and metadata that require the CLR to execute.
The Common language runtime (Common Language Runtime, CLR) is a runtime that is used by multiple programming languages. Code can be developed in any programming language, as long as the compiler is a CLR-oriented line.
The CLR can also be seen as a proxy for managing code at execution time, and managing code is the basic principle of the CLR. Code that can be managed is called managed (managed) code, which is otherwise called unmanaged code.
Memory management, assembly loading, security, exception handling, and thread synchronization in the core capabilities of the CLR can be used by all languages that target the CLR.
A managed module is a standard Windows PE file that requires a CLR environment to execute, including IL and metadata, as well as PE headers and CLR headers. :
- pe32/pe32+ header: Standard Windows PE file header. Contains information such as the file type (GUI, Cui, and so on), and when the file was created. If the file header is in PE32 format, this file can only be run on a 32-bit or 64-bit version of Windows, and if the file header is in pe32+ format, this file can only run on the 64-bit version of Windows.
- CLR Header: Contains some information that identifies a managed module. such as the CLR version number, the managed module entry point method (Main method), and the MethodDef metadata, and so on.
- Il code: is also the intermediate language. The intermediate code generated when the compiler compiles the source code, which, in the execution environment, is translated by the CLR's JIT editor into instructions that the CPU can recognize for CPU execution.
- Metadata is actually a collection of data tables that describe what is defined and referenced in a managed module. VS enables IntelliSense to benefit from the description of metadata.
The PE (portable Execute) file is a program file on the Microsoft Windows operating system, and EXE, DLL, OCX, SYS, com are all PE files.
The local code compiler (native code compiler) generates code that targets a specific CPU architecture (such as x86,x64). Instead, each CLR-oriented compiler generates IL code.
Intermediate Language Il(intermediate Language) code: code generated after the compiler compiles the source code (. exe or. dll file), but the program code compiled at this time is not machine code that the CPU can execute directly. At run time, the JIT editor of the CLR compiles the IL code to the cost of the CPU instruction.
CPU: Central processing Unit is a computer operation core and control core. Its function is mainly to explain the computer instruction and processing the data in the computer software.
DLL is a dynamic link library file, which is an executable file that acts as a library of shared functions. In Windows, many applications are not a complete executable, and they are split into relatively independent, dynamic-link libraries, DLL files, placed in the system. When we execute a program, the corresponding DLL file is called. Multiple applications can also access the contents of a single DLL copy in memory at the same time. DLLs help to share data and resources.
Dynamic linking differs from static linking in that dynamic linking allows executable modules (. dll files or. exe files) to contain only the information required to locate the executable code of the DLL function at run time. In a static link, the linker gets all the referenced functions from the static link library and places the library with the code in the executable file.
Advantages of using intermediate languages (cross-platform, cross-language):
- Platform agnostic, which is independent of the specific CPU.
- Just put. NET Framework is compiled into IL code, it is implemented. The interaction between languages in the NET Framework.
metadata (Metadata) is data that describes data (type information), which is often interpreted as a data-about-data, a binary block consisting of a set of tables. The metadata is compiled by the CLR compiler and saved in the Windows Portable Execution Body (PE) file, which is embedded in the Exe/dll file with the Il that it describes, and the IL and metadata are always synchronized.
Table of metadata main types:
- The definition table describes the type and member information defined in the current assembly.
- The reference table describes any external type and member information that is referenced by an internal type.
- The manifest table contains all the information that is required to compose the assembly, along with reference information to other assemblies.
Use of metadata:
- At compile time, the metadata eliminates the need for local C + + header and library files, because all information about the type/member referenced is included in the IL code file responsible for implementing the type/member. The compiler can read metadata directly from the managed module.
- Metadata is used in the CLR's code validation process to ensure that code performs only "type-safe" operations.
- VS uses meta data to help you write code. Its IntelliSense technology resolves metadata to indicate which methods, properties, events, and fields a type provides.
Metadata allows you to serialize the field of one object into one memory and send it to another machine. The state of the object is then deserialized and rebuilt on the remote machine. (Memory: A bridge that communicates with the CPU, and all programs in the computer run in memory, so the performance of the memory affects the computer very much.)
Metadata allows the garbage collector to track the lifetime of an object, the garbage collector can determine the type of any object, and know from the metadata which fields in that object refer to other objects.
1th. CLR Execution Model 1.1