IL "The Language of CLR" - A Platform for Cross-Language


1.     Summary 
This article introduces the Cross-Language capabilities of CLR environment (Common Language Runtime Environment) and its constituent language IL (Intermediate Language).

2.     Introduction 
Microsoft .NET framework brings one of the amazing aspects of development, called "Cross-Language Development". By Cross-Language development it means, cross language Inheritance, Cross-Language Debugging and Cross-Language Exception handling. More generally, we can visualize Cross-Language development as follows. The code written using C# (as a matter of fact any .NET compliant language) should be usable in another language such as VB. Later on these two code modules should be usable in, yet another language like JScript etc and so on.  In order to make the above said statement possible there should be a common run time environment, which can understand all these languages. 

This article introduces the Cross-Language capabilities of CLR (Common Language Runtime Environment) and describes how IL (Intermediate Language) becomes core of all .NET compliant languages. In particular, I discuss about how to program using Intermediate Language and Use ILASM (IL assembler). Further, I also deal with some examples, which demonstrate the cross language capabilities of .NET framework.  In the examples that follow i will be using IL (Intermediate Language), C#, Jscript, C++ and VB languages. 

One of the main design goals of .NET is to encourage cross - Language development. (Cross - Language Inheritance, Exception handling etc.) The advantage of doing so is, a developer can choose a language that best suits for delivering a given module / unit (each language has its own strengths) and still be able to integrate into a single application. The end result of that is Languages become equal. Even employers also feel more comfortable as they would have more resource and options at hand.  

Microsoft .NET framework also eliminates DLL hell and allows for side by side deployment of Components. This is so because registration information and state data are no longer stored in the registry where it can be difficult to establish and maintain.

In the section that follows, i will introduce the .NET framework architecture in brief and then move to different cross language implementation using different .NET compliant languages.        

3.     Microsoft .NET Architecture 

IL
- Intermediate Language
CLR Environment - Common Language runtime Environment
JIT - Just in time Compilation
VES - Virtual Execution System
BCL - Base Class Library
PE Portable Executable
GC Garbage Collector

Common Language Runtime
Common Language Runtime (CLR) manages the execution of code and provides different services like Garbage collection and support for Base Class Libraries etc. The main constituents of CLR are described below 

The common Language Runtime (CLR) a rich set of features for cross-language development and deployment. CLR supports both Object Oriented Languages as well as procedural languages. CLR provides security, garbage collection, cross language exception handling, cross language inheritance and so on. 

The Common Type System, support both Object Oriented Programming languages as well as procedural languages. Basically CTS provides rich type system that is intended to support wide range of languages.  

CLS (Common Language Specification) defines a subset of Common Type System, which all language compilers targeting CLR must adhere to. CLS is a subset of CTS.  

All compilers under .NET will generate Intermediate Language no matter what language is used to develop an application. In fact, CLR will not be aware of the language used to develop an application. All language compilers will generate a uniform, common language called Intermediate Language. For this reason IL can be called as The language of CLR A platform for cross language development. 

Just in Time Compiler converts the IL code back to a platform/device specific code. In .NET you have three types of JIT compilers.

         Pre-JIT (Compiles entire code into native code at one stretch)

         Ecno-JIT (Compiles code part by part freeing when required)

         Normal JIT (Compiles only that part of code when called and places in cache)

Type safety is ensured in this phase. In all, the role of a JIT compiler is to bring higher performance by placing the once compiled code in cache, so that when a next call is made to the same method/procedure it get executed at faster speed.

Virtual Execution System (VES) implements Common Type System. VES loads links and runs Portable Executable (PE) files. VES also ensures to load the information contained in metadata.

Metadata

         Describes and references the data types defined by the VOS type system.

         Lay out instances of classes in memory.

         Resolves method invocation

         Solves Versioning Problem (DLL hell) 

The following diagram depicts the .NET architecture

 

We know that Windows Operating System runs on the Intel family of microprocessor chips. In order to generate platform neutral code two things should happen.

         Hardware dependencies (like microprocessor instruction set etc) are to be eliminated

         Software dependencies (like using operating system nave API etc). are to be eliminated  

Some of the Languages Supported By .NET  

C#, C++, VB, Jscript, Cobol, Small Talk, Perl, Pascal, Phyton, Oberon, APL, Haskell, Mercury, Scheme, CAML and OZ. 

Once code gets compiled using any of the .NET compliant language compilers, it gets converted to IL (Intermediate Language) as seen from the above diagram. This code is not compiled to machine native code but to an intermediate form which don't contain any specific information about hardware nor software dependencies. When you run this code, since it is not in machine specific form, it fails to execute. So, in order to make IL code converted to machine specific code there must be a run time environment, which understands and converts the IL code to machine specific code. This is the role played by CLR along with many other functions like garbage collection etc as discussed above 

I am not going to talk about these issues in detail. However I would concentrate on implementation of cross language capabilities of .NET framework.

4.     Common Language features

All languages will be using, same Library called Base Class Library (BCL). However, the syntax used by these languages will remain same as that of the original language.

         No language under .NET, has its own library

         Garbage Collection is be the responsibility of CLR environment and not a language 

Let us now write some programs using different languages and test whether or not the same CLR would support them. In other words, the same environment should understand code written in different languages at run time. If the same environment can understand multiple languages at run time, that it self is a great proof, to say that it supports cross-language development. The following examples show you how to write code using different languages.

Later on I will also deal with how to write code in IL instead of using .NET languages. 

5.     CLR Examples 

In the following section I shall deal with three programs written using three different languages (C++, Csharp and VB).  

C++ Example 

This is a very simple example using C++. Instead if include directive it uses using directive and also will not be using native C++ languages library function but will be using Base Class Library functions which every language under .NET will be using as said. 

In order to run the following code, you need to copy .NET framework lib files into the folder you are using or alternatively set the environment variables accordingly so that you can go off with copying files into the working directory. 

In order to compile the code type CL./CLR cplus.cpp at the command prompt and press enter, it generates cplus.exe file. This file is called portable executable. In order to run cplus.exe, type cplus at the command prompt and press enter.  

// C++ Example
// CL/CLR cplus.cpp
# using <mscorlib.dll>
using namespace System;
void main()
{
Console::WriteLine("Hello i am C++");
}

Csharp Example 
Csharp was the language used to develop much part of the .NET framework architecture. At first glance it looks like Java code. Once you get used to this you will notice it otherwise. 

Csharp is the first Component oriented language in the market. A component 
1.      Should deliver PME (Property, Method and Events) model
2.      Should be able set both design and run time attributes
3.      Should be able to generate documentation preferably in XML 

// Csharp Example
//csc csharp.cs
using System;
class HelloCsharp
{
public static void Main()
{
Console.WriteLine("Hello I am, Csharp");
}
 
 VB Example 
 In .NET VB has a special role to play. Microsoft defines VB as VB for WEB. There will not be Visual InterDev shipped in VS.NET instead it merges with VB. VB in .NET has got full object oriented capabilities. It is a full-blown object oriented language now. 

VB Example
'vbc vb.txt
imports System
public Module ALL
public sub main()
Console.WriteLine("Hello I am, VB")
end sub
end Module


Similar Articles