Demystifying CLR: Part I


Objective 

This is a fully theoretical article. In this article, I am going to explain fundamentals of CLR. How CLR executes codes written in different languages.  I am going to explain various components of MANAGED MODULES also. 

Note:  I have written this article on basis of my learning from the very nice book written by Jeffery Richter named CLR via C#. I suggest everyone to have a better understating on CLR read this book. Thanks to original author Jeffery.  I just tried to make his words simpler for native reader like me. 

Flow Chart of CLR execution model 

1.gif

CLR comes as part of .Net framework.  This is a run time that is useable by different and varied programming language. 

Features of CLR is as follows 
  1. Memory Management 
  2. Assembly Loading 
  3. Security 
  4. Exception Handling 
  5. Thread Synchronization 
Any language which targets its compiled output to CLR can avail above features.  

CLR can work on any language; condition is compiler of that language targets the compiled output to the CLR. Microsoft has created much language which compilers targets to CRL.

2.gif
 
Managed Module 

This is either 32 bit or 64 bit Microsoft Standard Portable Executable File Format.  These are the files required by CLR to execute. 

Compiler of all high level language does the task of syntax checking and language analysis and targets the compiled output to CLR. All languages outputs PE 32 or PE32+ format files and that will get executed by CLR to produce CPU instructions. 

3.gif
 
The above diagram describes why .Net frameworks are so called Language independent. It is due to CLR. CLR is common for all languages. 

Components of Managed Module 

4.gif
 
PE32 or PE32 + Header
  1. The standard of windows portable executable file format header is similar to common object filr format (COFF) headers. 

    5.gif
  2.  Header contains information about type of the file. That whether it is DLL, GIF etc.
  3. Header contains information about time stamp that when file built. 
  4. If the managed module is having only IL then PE 32 (+) header is ignored by CLR. 
  5. If the managed module contains CPU Instructions then PE 32 (+) header contains information about CPU instructions. 
CLR Header 
  1. This contains the information interpreted by CLR.
  2. This contains information about version of CLR needed to interpret the managed module. 
  3. This contains Flags information.
  4. CLR header contains MethodOF token which says what is entry point (main method) of managed module. 
IL Code 

Code generated by language compiler as compiled outputs. This IL code gets interpreted by CLR to produce CPU instructions. 

MetaData

Each managed modules contain Meta Data table. There are normally two types of Meta Data table. 

6.gif
 
Every compiler targeting the CLR must emit a full set of Meta data. Meta Data is set of tables that contain all the information about what defines in the module. This contains information about all the types and members used in the code. This also contains all the type and members referenced in the code.  Meta Data is always associated with the files contain the IL. Meta Data packaged in the same dll or exe file. 

Uses of Meta Data table 
  1. Meta Data removes the natives' code like header files. 
  2. Visual Studio uses Meta Data to perform work of IntelliSense. IntelliSense feature of visual studio uses Meta data tables to give suggestion while writing codes. 
  3. CLR code verification uses Meta Data tables to make sure code only performs type safe operations. 
  4. Meta Data performs work of serialization and deserialization. 
  5. Garbage collector reads life time of object from Meta data table. 
Conclusion 

In this article, I discussed how high level language compiles source code to a managed module. And CLR reads this managed module to give CPU instructions as output.  This was 1st part of series of article on CLR. Thanks for reading. I hope it was useful for you. 

Up Next
    Ebook Download
    View all
    Learn
    View all