CLR Notes: 1. CLR execution model

Source: Internet
Author: User
Tags types of tables

Term: CLR: Common Language Runtime, with Runtime libraries used in different programming languages
Managed Module: Managed Module. A standard MS Window can be transplanted to the execution body file (32-bit PE32 or 64-bit PE32 +)
IL: Intermediate Language, also known as managed code (executed by CLR)
Metadata: metadata, a series of special data tables
Assembly: Assembly, abstract
JIT: just-in-time instant compilation, which compiles local CPU commands (Local Code)
FCL: Framework Class Library, Framework Class Library
CTS: Common Type System, which describes the definition and behavior of a Type.
CLI: Common Language Infrastructure, basic structure of a Common Language. This is a standard that MS submits to ECMA. It consists of CTS and other Framwork components.
CLS: Common Language Specfication, which specifies a minimum feature set in detail.

1.1 compile the source code into a managed Module
CLR compilation process: C # source code file -- C # compiler Compilation -- managed module (IL and metadata)

Each part of the hosting module:
1. PE32 or PE32 + Header
Indicates the file type, GUI/CUI/DLL, file generation time, run on 32-bit or 64-bit
2. CLR Header
CLR version, entry method, module metadata, resource, strong name
3. Metadata
Three types of tables
4. IL code

Metadata includes:
1. describes the content defined in the module, such as the class and its members.
2. pointed out the content referenced by the hosting module, such as the imported class and its members.
3. manifest describes the Assembly file, the public export type implemented by the files in the Assembly, and the resource/data file associated with the Assembly.
Metadata is always embedded in the same EXE/DLL as the code, and always synchronized with IL.

Metadata usage:
1. eliminates the dependency on the header/library file and reads it directly from the hosting module.
2. Smart awareness, parsing from metadata
3. Code verification. use metadata to ensure that the Code performs only security operations.
4. Positive and Negative serialization
5. The Spam collector traces the object's lifetime and object type.

1.2 merge managed modules into assembly
Assembly: one or more logical groups of managed modules/resource files. It is the smallest reuse, security, and Version Control Unit.
A single file assembly or multi-file assembly can be generated, which is determined by the compiler tool.
CLR works with the Assembly, not with the managed Module

1.3 load CLR
CLRVer command to view all CLR versions on the machine
The/plattform switch of csc determines which assembly to generate: AnyCPU, x86, x64, Itanium

1.4 execute Assembly code
ILAsm command to compile IL into Assembly; ILDasm to compile Assembly into IL.
The advanced language (C #) is only a subset of CLR, and IL allows access to all CLR functions.
JITCompiler function, also known as JIT Compiler (JITter)
When the method is executed for the first time, the CLR detects all types referenced by the Main code. Therefore, the CLR allocates an internal data structure to manage access to the reference type.
In this internal structure, each method has a corresponding record and address.
During the initialization of this structure, CLR sets each record as an undocumented function in CLR, namely, the JITCompiler function.
When the JITCompiler function is called, find the IL of the corresponding method, compile the local CPU command, save it to a dynamic memory block, and store the memory address in the internal structure, finally, the JITCompiler function jumps to the Code in the memory block and runs the code.
During the second execution of this method, the code in the memory block is directly executed without further compilation.

JIT saves the local code in the dynamic memory. Once the program is terminated, the local code will be discarded.

The csc command has two switches that will affect code optimization:/optimize,/debug

Switch Settings IL code Quality JIT local code Quality
/Optimize-,/debug- Not optimized Optimization Default settings
/Optimize-,/debug (+/full/pdbonly) Not optimized Not optimized VS2005 Degug status
/Optimize +,/debug (-/full/pdbonly) Optimization Optimization VS2005 Release status

When an unoptimized IL is generated, the NOP command is generated in the IL for debugging and breakpoint setting.

IL is stack-based. All commands are: press the operands to the stack, and the result is displayed from the stack.
IL has a security authentication mechanism to ensure that each line of IL code is correct and does not illegally access the memory. Each hosted EXE runs in its own AppDomain.

Insecure code: allows C # To directly operate on memory bytes for use during COM interoperability. csc uses the/unsafe switch to mark the inclusion of Insecure code. All methods use the unsafe keyword.
The PEVerify command checks all methods of the assembly and indicates the Unsafe code methods.

1.5 local code generator NGEN.exe
NGEN.exe pre-compiles IL to a hard disk file to speed up program startup and reduce the working set of the Program (all AppDomains that load the Assembly no longer copy their copies, because the Assembly has been compiled into a file and is shared with the code ).
Disadvantages:
Cannot protect IL Leakage
The generated file may be out of sync.
Because the preferred base address needs to be calculated in the file, while NGEN is well calculated in static mode, modifying the base address will slow down.
Poor execution performance. The code generated by NGEN is not as good as JIT.
If the file generated by NGEN cannot be used, JITCompiler is automatically loaded.

1.7 CTS
CTS rules:
1. A type can contain 0 or more members.
2. Type visualization and access rules for type members
3. defined management rules for inheritance, virtual methods, and object generation periods
4. All types are ultimately inherited from the predefined System. Object.

1.8 CLS
If the types and methods defined in C # Can Be Used in VB, no public/protected attribute outside CLS can be defined in C, the privated type and its members are not restricted.
C # there can be two methods that are case-insensitive -- they do not conform to CLS, so they cannot be public.

Use the [assembly: CLSComplant (true)] flag to tell the compiler to check the CLS compatibility of the assembly. I did a test here:

Using System;

[Assembly: CLSCompliant (true)]

Namespace ClassLibrary2
{
Public class Class1
{

Public void ()
{

}

Public void ()
{

}
}
}

Note: [assembly: CLSComplant (true)] must be written outside the namespace.
I have defined two different methods A and a. the compiler will warn that the syntax is not compatible with CLS. If the [assembly: CLSComplant (true)] Declaration is removed, there will be no such warning; if you change method a to private, no warning is given.
I used ILDasm to observe the dll and found that both methods A and a exist in IL, indicating that the syntax range of IL is greater than CLS.
In VB, I added a reference to this dll:

Imports ClassLibrary2

Module Module1Module Module1

Public Class TClass T
Public Function A () As Integer

Dim c1 As Class1 = New Class1 ()


End Function
End Class

End Module

It is found that there is no smart sensing of method A or method a after c1. this means that VB cannot identify the non-conforming CLS syntax. If you modify method a in dll to private or delete method a, you can intelligently perceive method A in VB.
It can be concluded that the CLS syntax is not applicable in other languages.

1.9 COM interoperability
Three interoperability scenarios:
1. The managed code can call the unmanaged functions contained in the DLL, such as Kernal32.dll and User32.dll.
2. Hosted code can use off-the-shelf COM components
3. unmanaged code can use managed types (ActiveX controls or shell extensions written in C)

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.