CLR Reading Notes-Chapter 1 CLR execution model

Source: Internet
Author: User

Refer to Jianqiang Bao notes: http://www.cnblogs.com/Jax/archive/2008/04/05/1047032.html

1.1 compile the source code into a managed Module

1. What type of application or component (File) is generated );

(1) CLR (Common Language Runtime): runtime in the public language; runtime that can be used by multiple languages; --- a runtime environment

What is CLR... Reference: http://baike.baidu.com/view/605055.htm

(A program running under CLR monitoring is a "managed" code that does not run under Clr and runs directly on bare metal applications or components are "unmanaged" code)

(2) Core CLR functions can be used by all CLR-oriented languages, such as exception reporting and thread creation...

(3) the CLR does not care about the language in use;

(4) compiler: the compiler can be considered as a syntax checker...

The compiler generatesManaged module ,--Is a standard 32/64-bit portable execution file. They all need CLR to execute

Components of the hosting module:

@ 1 pe32/pe32 + header: standard Windows PE Header;

@ 2 CLR header: contains information about making this module A managed module;

@ 3 metadata: Each managed module contains metadata tables. (what is metadata? : Http://baike.baidu.com/view/107838.htm)

Metadata-simply put, it is a set of data tables. Some tables describe the content defined in the module, and some tables describe the content referenced by the hosting module. Metadata is a superset of some old technologies.

Metadata is always associated with the Il file. In fact, metadata is always embedded in the same EXE/dll as the code.

@ 4 IL (intermediate language code) code: the code generated by the compiler to compile the source code. during runtime, the CLR compiles the Il command at a cost. The IL code is sometimes called a hosted code, because CLR needs to manage its execution

(5) functions of metadata:

@ 1 eliminates the need for local C/C ++ headers and library files. The compiler can directly read metadata from the hosted module;

@ 2 smart notification ...;

@ 3 ensure that only "type security" operations are performed;

@ 4 Positive and Negative serialization;

@ 5 allow the Garbage Collector to track the lifetime of an object ;....

1.2 merge managed modules into assembly

CLR actually works with the Assembly. In the CLR world, the Assembly is equivalent to a "component ";

1. Assembly: @ 1 is a group of one or more modules/resource files. @ 2 is the minimum unit for reuse, security, and version control;

2. some managed heap and resource files are handed over to a tool (ex: C # compiler) to generate a separate pe32 (+) file to indicate the logical grouping of files; this pe32 (+) the file contains a file named "list"

Data block. The list represents the file set in the Assembly ..

3. By default, the compiler converts the generated managed modules into an assembly. The C # compiler generates a managed module that contains a list, which indicates that the Assembly is composed of only one file;

1.3 loading public Language Runtime

1. clrver command to view all CLR versions on the machine;

2. The CSC/Plattform switch determines what the Assembly looks like: anycpu, x86, x64, itanium;

1.4 run the Assembly Code

The advanced language (C #) is only a subset of CLR, and IL allows access to all CLR functions;

1. The hosted Assembly also contains metadata and IL (Il is a language unrelated to the CPU), which can be considered as an object-oriented machine language;

2. to execute a method, you must first convert its Il to the local CPU command. --> This is the responsibility of the clr jit (just-in-time, "instant") compiler;

3. events that occur when a method is called for the first time:

    

@ 1 inBefore the main () method is executedBecause main () references a console type, CLR will allocate an internal structure. When initializing this structure, CLR willEach record itemAre set to point to include

Jitcompiler function in CLR;

Before the main () method is executed:CLR detects all types referenced by the main () Code, which causes the CLR to allocate an internal data structure to manage access to the referenced types;

Each record item:In this internal data structure, each method defined by the console type has a corresponding record item, each record item contains an address, and the implementation of the method can be found based on this address;

@ 2 when the main () method calls writeline for the first time, the jitcompiler function will be called. The jitcompiler function is responsible for compiling the Il of a method to the local CPU command. When jitcompiler is called, it knows

Which method is called and what type is used to define the method?

@ 3 jitcompiler finds the Il of the called method in the program that defines this type;

@ 4 jitcompiler verifies the Il code and compiles the Il code with Local CPU commands (local CPU commands are saved to a dynamically allocatedMemory blockMedium)

@ 5 jipcompiler returns the internal data structure created by CLR for the type, finds the record corresponding to the called method, modifies the original reference to jitcompiler, and points itMemory blockAddress,

@ 6 jitcompiler jump to the Code in the memory block. These codes are exactly the implementation of the writeline method. After the code is executed, it will return to main again.

When writeline is called for the second time, because the writeline has been verified and compiled, the code in the memory block is executed directly, and the jitcompiler function is skipped completely. After writeline is executed, the system returns the result again.

Main method;

4. A method may cause some performance loss only when it is called for the first time.

5. two switches affect code optimization:/optimize and/debug. default Time:/optimize-/debug-,/optimize-generate unoptimized code During C # compilation, which will contain many NOP commands and

Many branch commands allow me to set breakpoints when debugging code...

6. The code is compiled in two phases: @ 1 the compiler generates the source code il; @ 2clr JIT then generates the local CPU Command...

7. The performance of the hosted application actually exceeds that of the unmanaged application,... n for many reasons...

1.4.1 Il and Verification

1. The Il is stack-based, and it does not provide instructions for operation registers;

2. the biggest advantage of Il: Application robustness and security... when the Il command is compiled to the local CPU, the CLR will execute a verification process, which will check whether advanced Il is safe;

3. In Windows, each process has a virtual address space. Each process is put into an independent address space for robustness and security. One process cannot interfere with another process;

In Windows, a process requires a large amount of operating system resources. Too many processes may damage performance and restrict available resources;

@ 1 verify the managed code to ensure that the Code accesses the correct memory @ 2 run multiple applications in a process

CLR provides the ability to cherish multiple hosted applications in a process. Each hosted application is executed in its own appdomain. by default, each hosted EXE file is independent of it.

In the address space, there is only one appdomain (application domain) to run this address );

1.4.2 Insecure code

1. C # Safe code generated by the compiler by default. however, the compiler also allows writing Insecure code-Insecure code allows direct operations on memory addresses and the bytes at these addresses.

It is usually used only when it is used for interoperation with non-hosted code, or when it is used to improve algorithms with extremely high efficiency requirements;

2. Major Risks: @ 1 destroys the data structure and threatens security. @ 2 may even cause new security vulnerabilities;

3. require that all insecure C code be compiled with the unsafe keyword JIT: Check @ 1 permission; check whether the @ 2 flag is set;

1.6 framework class library

1. the. NET Framework contains the framework class library (framework class library FCL). FCL is a group of DLL files;

1.7 General Type System

1. CLR is completely based on the type. Through the type, different programming languages can communicate. The type is the foundation of CLR, So Microsoft has developed this formal specification: general type system -- cts

Common Type System, which describes the type definition and behavior;

2. CTS rules:

@ 1 A type can contain 0 or more members;

@ 2 Specify type Visibility rules and member access rules;

@ 3 defines rules for type inheritance, virtual methods, and object lifetime;

@ 4 all types are inherited from the predefined system. object.

1.8 public language specifications

1. The common language specification Cls (Common Language Specification) defines in detail a minimum set of functions that must be supported by all languages,

2. In CLR: each member of a type is either a field or a method... the source code is finally compiled as a field or method.

1.9 interoperability with unmanaged code

1. The managed code can call the unmanaged code in the DLL;

2. Hosting code can use existing COM components;

3. The managed type can be used for unmanaged code;

--- I am a cainiao .... haha... it's a bit of a book copy feeling, the first time I write Reading Notes. I have finished reading this chapter in about two weeks. I don't know much about it every day. I just got back from work and went online to do other work... (⊙ o ⊙ )! Wood has perseverance !!! This should be done... hey... continue to cheer, take your own notes... welcome to make a brick.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

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.