8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
1/26
CLR Basics:
The CLRs Execution Model
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
2/26
Agenda
CLR Execution Model
Compiling source code intoManaged modules
Combining Managed Modulesinto Assemblies
The Native Code Generator Tool:NGen.exe
Introducing the FrameworkClass Library
The Common Type System
Loading the Common LanguageRuntime
Executing your Assemblys code
IL and Verification
Unsafe Code
The Common LanguageSpecification
Interoperability withUnmanaged code
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
3/26
CLR Execution Model
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
4/26
Compiling Source Code into Managed Modules
Language compilers are syntax checkers and "correct code" analyzers.
Microsoft has created several language compilers (C++, C# ,Visual Basic,
JScript, J#,etc..) that produce code that targets CLR.
In addition to Microsoft, several other companies, colleges, and universitieshave created compilers that produce code to target the CLR.
Example : Compilers for Ada, APL, Caml, COBOL, Eiffel, Forth, Fortran, Haskell,
Lexico, LISP, LOGO,Lua, Mercury, ML, Mondrian, Oberon, Pascal, Perl, Php,Prolog, Python, RPG, Scheme, Smalltalk, and Tcl/Tk.
These compilers check the syntax and analyze the source code and generate aManaged Module.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
5/26
Compiling Source Code into Managed Modules
A managed module is a standard 32-bit Microsoft Windows portableexecutable (PE32) file or a standard 64-bit Windows portableexecutable (PE32+) file that requires the CLR to execute.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
6/26
Parts of a Managed Module
PE32 or PE32+ header
CLR header
Metadata or Metadata Table
Intermediate Language (IL) code
PE32 or PE32+ header
If the header uses the PE32 format, the file can run on a 32-bit or 64-bit version ofWindows.
If the header uses the PE32+ format, the file requires a 64-bit version of Windows torun.
Also indicates the type of file: GUI, CUI, or DLL, and contains a timestamp indicatingwhen the file was built.
CLR header
Contains the information that makes this a managed module.
Version of the CLR required, module's entry point method (Main method), and the
location/ size of the module's metadata, resources, strong name, some flags, andother less interesting stuff.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
7/26
Parts of a Managed Module
Metadata or Metadata Table
Tables that describe the types and members defined in your
source code
Tables that describe the types and members referenced by yoursource code.
Intermediate Language (IL) code
Code the compiler produced as it compiled the source code.
At run time, the CLR compiles the IL into native CPUinstructions.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
8/26
Parts of Managed Module
To get information about Managed Module , execute the followingcommand in VS command prompt;
Dumpbin / all > result.txt
The above command would result with result.txt file which contains
Also can use ildasm a tool to view the IL code and Metadatainformation. (Will discuss in next chapter.)
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
9/26
Combining Managed Modules into Assemblies
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
10/26
Combining Managed Modules into Assemblies
An assembly is a logical grouping of one or more modules or resourcefiles.
Also an assembly is the smallest unit of reuse, security andversioning.
Assembly is either an .exe file or .dll file.
For projects that have just one managed module and no resource (ordata) files, the assembly will be the managed module.
Manifest within assembly contains information about Managed module
in the assembly.
There are tools to create assembly with more than one managedmodule. (Discussed in next chapter.)
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
11/26
Loading the Common Language Runtime
Each assembly you build can be either an executable application or aDLL containing a set of types for use by an executable application.
.Net Framework needs to be installed in the host machine that triesto execute the exe or use the dll.
MSCorEE.dll file in the %SystemRoot%\system32 directory.
Several versions of the .NET Framework can be installed on a singlemachine.
A command-line utility called CLRVer.exe shows all of the CLRversions installed on a machine.
Version of CLR to be invoked by the assembly is mention in themanifest.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
12/26
Selecting Windows Version
In visual studio , under project's property pages, click the Build tab,and then selecting an option in the Platform Target list.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
13/26
Executing Your Assembly's Code
Usually, developers will program in a high-level language, such as C#,C++/CLI, or Visual Basic.
The compilers for these high-level languages produce IL.
IL code is converted to native CPU instructions by the CLR's JIT (just-- .
Lets see an example;
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
14/26
Executing Your Assembly's Code
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
15/26
Executing Your Assembly's Code
Just before the Main method executes, the CLR detects all of thetypes that are referenced by Main's code.
Then CLR allocates an internal data structure that is used to manage
access to the referenced types.
Here ,the Main method refers to a single type, Console, causing theCLR to allocate a single internal structure.
This internal data structure contains an entry for each methoddefined by the Console type. Each entry holds the address where themethod's implementation can be found.
This data structure will be used by the JIT compiler in the CLR.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
16/26
Executing Your Assembly's Code
When Main makes its first call to WriteLine, the JITCompiler is called.
When called, the JITCompiler knows what method is being called and whattype defines this method.
JIT compiler then searches the internal data structure to verify the structureand syntax of the method and finally compiles the IL code into native CPUinstructions.
The native CPU instructions are saved in a dynamically allocated block ofmemory.
Then, JITCompiler goes back to the entry for the called method in the type'sinternal data structure created by the CLR and replaces the reference that
called it in the first place with the address of the block of memory containingthe native CPU instructions it just compiled.
Finally, the JITCompiler function jumps to the code in the memory block.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
17/26
Executing Your Assembly's Code
Main now calls WriteLine a second time. This time, the code forWriteLine has already been verified and compiled. So the call goesdirectly to the block of memory, skipping the JITCompiler functionentirely. After the WriteLine method executes, it returns to Main.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
18/26
Executing Your Assembly's Code
A performance hit is incurred only the first time a method is called.
All subsequent calls to the method execute at the full speed of the
native code because verification and compilation to native code do notneed to be performed again.
The JIT compiler stores the native CPU instructions in dynamicmemory.
This means that the compiled code is discarded when the applicationterminates.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
19/26
The NGen.exe tool that ships with the .NET Framework can be used to compileIL code to native code when an application is installed on a user's machine.
The code gets compiled at install time, the CLR's JIT compiler does not haveto compile the IL code at run time, and this can improve the application's
performance.
Advantages :
Improving an application's startup time
The Native Code Generator Tool: NGen.exe
compiles the IL code at run time.
Reducing an application's working set
compiles the IL to native code and saves the output in a separate file.
This file can be memory-mapped into multiple process address spacessimultaneously, allowing the code to be shared; not every process/AppDomain needs its own copy of the code.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
20/26
The Native Code Generator Tool: NGen.exe
This new file is placed in a folder under the directory with a name like
C:\Windows\Assembly\NativeImages_v2.0.50727_32.
Whenever the CLR loads an assembly file, the CLR looks to see if acorresponding NGen'd native file exists. If a native file cannot be
, .
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
21/26
Framework Class Library
Also called as Base Class Library (BCL)
The FCL is a set of DLL assemblies that contain several thousandtype definitions in which each type exposes some functionality.
FCL is a set of reusable object-oriented classes that provide basicplatform functionality, such as ;
Data access classes of ADO.NET File system utility classes
networking classes (ex: DNS resolution) and
other network-related functionality etc.,
Developers can use the base classes directly or derive from theseclasses to provide customized functionality.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
22/26
Framework Class Library
All objects of BCL originate from class System
Some of the BCL are mentioned below;
System Root for all namespaces
System.Data Database classes
System.Text String manipulation
ys em. e
System.Windows.Forms Windows Forms
System.Web Web Forms
System.XML XML elements
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
23/26
The Common Type System
CTS defines how data types are going to be declared and managed in runtime.
In .Net Framework, System.Object is the common base type from where allthe other types are derived.
The types in .NET Framework are the base on which .NET applications,components, and controls are built.
The Common Type System performs the following functions:
,
type safety high performance code execution.
Provides an object-oriented model.
Standardizes the conventions that all the languages must follow.
Encapsulates data structures.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
24/26
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
25/26
Common Language Specification
CLS is a specification that defines the rules to support languageintegration in such a way that programs written in any language,can interoperate with one another.
The CLS is a subset CLR, as well as a set of rules that languageand compiler designers must follow. (if interoperability isneeded.)
CLS provides the ability to inherit classes written in one .NETlanguage in any other .NET language and cross-languagedebugging.
The rules defined by the CLS apply only to publicly exposed
features of a class.
8/10/2019 Chapter 1_CLR Basics - The CLRs Execution Model.pdf
26/26
Thank You