- Managed module: Standard 32 or 64 bit Windows portable excutable PE32 or PE32+ file.
Compilers compile source code into managed module.
Content:
PE32/PE32+ header: The Windows version; Type of files(GUI, DLL...); Timestamp of building
CLR Header: version of CLR; some flags; MethodDef metadata token(main method); location/size of metadata; resources; strong name;
Metadata: table1: types and memebers in source code
table 2: types and memebers in references
IL code
metadata and IL code are in the same place. Metadata is created by IL code.
- Assembly: a logical grouping of one or more modules or resource files; the smallest unit of reuse.
Compilers turn managed module into an assembly.
C# compiler emits a managed module that contains a manifest which indicates that the assembly consists of just the one file. So for projects that have only
- Difference:
CLR only runs assembly
Assembly could have more than one managed modules
Application Run:
1. Windows examine the PE32/PE32+ header
2. Create a 32 or 64 process
3. Load x86, x64 or ARM version of MSCorEE.dll into the process space
4. The main thread of the process called a method of MSCorEE.dll to initialize the CLR, load the application file and call its entry Main method.
Method executes
1. Before the Main method executes, CLR detects all the types that are referenced by Main
2. CLR allocates an internal data structure to maintain access to all the referenced types
This data structure contains an entry to each methods that defined by the referenced type.
Each entry holds the address where the method's implementation can be found
When initialize this structure, CLR sets each entry to an internal, undocumented function(JITCompiler) contained inside the CLR itself.
3. When Main makes its first call, JITCompiler function is called.
4. JITCompiler function verifies and compiles a method's IL code into native CPU instructions (JIT: Just In Time)
JITCompiler function knows the called method and its type, and then JITCompiler searches the defining assembly's metadata for the IL code of the called method.
The native CPU instructions are saved in a dynamically allocated block of memory.
5. JITCompiler function goes back to the entry for the called method and replaces the reference with the address of the dynamically allocated memory.
6. JITCompiler jumps to the code in the memory block.
7. When this code returns, it returns to the code in Main and continues.
8. When the same method is called the second time, the call goes directly to the memory block and skips the JITCompiler function.
performance hit only happens in the first call
JIT optimizes the code
IL is stack-based, typeless.
While compiling IL code into native CPU instructions, CLR performs a process named Verification which examines the IL code and makes sure everything is safe.
The managed module's metadata includes all of the methods and types information used by the verification process.
Multiple managed applications can be run in a single Windows virtual address space.
NGen.exe: compile IL code into native CPU structions
It can be used when an application is installed on a user's machine.
Managed Profile Guided Optimization tool (MPGO.exe): This tool analyzes the execution of your application to see what it needs at startup.
FCL: Framework Class Library
CTS: Common Type System
CLS: Common Language Specification--interlanguage operation