01. Learning the implementation model of the CLR of the. NET CLR Base Series

Source: Internet
Author: User

. NET from code generation to execution, some of the middle-of-the-line processes are new concepts that are different from other technologies, so what is the process, what kind of mechanism, and what are the basic things that we do . Net of the east and the West can know. So, What is the process of the CLR's execution model?  

Compile the source code into a managed module --and merge the managed modules into assemblies-- load the common language runtime --Execute the assembly's code

Catalogue
  • compiling the source code into a managed module
  • to merge managed modules into assemblies
  • loading the common language runtime
  • code to execute the assembly
  • Local code generator:NGen.exe
  • Getting started with the Framwork class library
  • Common Type system
  • Common Language Specification (CLS)
  • Interoperability with unmanaged code

compiling the source code into a managed module

First of all ,. Net Development is to support multi-language selection or mixed language programming, code compilation is the use of compiler-oriented mechanism of the CLR, that is, regardless of the language written code, as long as the compiler is oriented to the CLR Can be.  

Common Language Runtime: The common language runtime, which can be used by a variety of programming languages. The core capabilities of the CLR, such as memory management, assembly loading, security, exception handling, and thread synchronization, can be used by all languages that target the CLR.

Managed Module : The managed module, which is the result of compiler compilation, is a managed module.

Il: Each CLR -oriented compiler generates IL(intermediate language) code. Also becomes managed code, because the CLR wants to manage its execution.

Native Code Compiler: Native-coded compiler. Generates code that targets a specific CPU architecture (X86, X64, IA64).

Metadata: Meta data. is a data table that describes the content defined in the module, such as types and members, and some data that describes what the module refers to, such as the imported type and its members. Metadata is always associated with a file that contains IL code and is always synchronized.

to merge managed modules into assemblies

Shows the process by which managed modules are merged into assemblies.

Assembly (Assembly) Understanding: Logical grouping of one or more module / resource files; The assembly is the smallest unit of reuse, security, and versioning.

from the display, it can be seenthat the Assembly (Assembly) contains a block of data named "Manifest" (manifest), which is another collection of metadata tables that describes the files that make up the assembly.

Another Place is AL.EXE: assembly connector

loading the common language runtime

Windows Check EXE file Header:

PE32: Requires 32-bit address space and can be run in 32-bit/64-bit address space

PE32+: Requires 64-bit address space

CPU architecture information to ensure that the current CPU is compliant

Create 32/64/wow64 bit process : based on exe file header

Loading MSCorEE.DLL: This file is in C:\Windows\SysWow64 or C:\Windwos\Sys

Look at the above process is still relatively clear.

code to execute the assembly

in order to execute a method, it must first take its IL translates the cost to the CPU instructions. This is the responsibility of the CLR's JIT(just-in-time or "instant") compiler. Never analyze how the process is.

  1. before Mian () executes,the CLR detects all the types referenced by the code of Main.
  2. allocates internal data structures for managing access to reference types, console type, and each method defined by the console type has a corresponding entry(record entry). Each entry contains an address that can be used to find the implementation of the method . when the structure is initialized, the The CLR sets each record entry to (pointing to) an jitcompiler function that is contained within the CLR, which is a function of
  3. The Color:black function is called when
  4. jitcompiler function call, Jitcompiler finds the Il

    --> validation il

    --> Compile the cost of the CPU instruction

    --> return to the record just now, and modify the reference to the Jitcompiler, point to the address of the memory block you just compiled

    -->jitcompiler function jump to the memory block just now execute

    --> back to Mian continue execution

      

  5. when the main function calls WriteLine for the second time

Local code generator:NGen. EXE

The Ngen.exe:.net Framework provides tools to compile the IL code when an application is installed on the target computer.

NGen.exe finally function:

    1. Speed up application start-up
    2. reduce the working set of the application (working set)

Location: similar to C:\Windows\Microsoft.NET\Framework\v4.0.30319

Command syntax:

NGen <action> [Options]

NGen/? | /help

Framework class library

FCL:the Framework Class Library is a generic term for a set of DLL assemblies.

Common Type System

CTS:Common type System, which describes the definition and behavior of a type.

Common Language Specification

CLS:Common Language specification

It was believed that programming in mixed languages forced Microsoft to develop a CLS. The problem that CLS solves is that objects created in different languages can communicate with each other. To create types that are easily accessible from other languages, you can select only those features that are supported by other languages from your own programming language.

This requires any compiler-generated type to be compatible with other "compliant The component generated by the CLS, CLR -oriented language, must support this minimal feature set.

tells the compiler to check syntax for CLS compatibility

[Assembly:clscompliant (true)]

Namespace Somelibrary

{

}

interoperability with unmanaged code

in order to cater to users of previous unmanaged code, Microsoft The CLR provides mechanisms that allow both managed and unmanaged code to be included in the application.

    1. managed code can invoke unmanaged functions in a DLL
    2. managed code can use an existing COM component (server)
    3. Managed types (servers) can be used by unmanaged code

This paper also mainly studies some of the architectural ideas and concepts of. NET platform design and should have a basic understanding of. NET in general.

01. Learning the implementation model of the CLR of the. NET CLR Base Series

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.