Common Intermediate Language: Difference between revisions

Content deleted Content added
Metadata: copy editing
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(38 intermediate revisions by 29 users not shown)
Line 3:
{{refimprove|date=November 2017}}
 
'''Common Intermediate Language''' ('''CIL'''), formerly called '''Microsoft Intermediate Language''' ('''MSIL''') or '''Intermediate Language''' ('''IL'''),<ref>{{cite web |url = https://docslearn.microsoft.com/en-us/dotnet/standard/managed-code |title = IntermediateWhat Languageis "managed code"?|publisher=Microsoft| date=19 April &2023 execution}}</ref> is the [[intermediate language]] binary instruction set defined within the [[Common Language Infrastructure]] (CLI) specification.<ref>{{cite web |url = https://www.ecma-international.org/publications-and-standards/filesstandards/ECMAecma-ST335/ECMA <!--335.pdf |page = 32 --> |title = ECMA-335 Common Language Infrastructure (CLI)}}</ref> CIL instructions are executed by a CLICIL-compatible runtime environment such as the [[Common Language Runtime]]. Languages which target the CLI compile to CIL. CIL is [[Object-oriented programming|object-oriented]], [[Stack machine|stack-based]] [[bytecode]]. Runtimes typically [[Just-in-time compilation|just-in-time]] compile CIL instructions into [[native code]].
 
CIL was originally known as Microsoft Intermediate Language (MSIL) during the beta releases of the .NET languages. Due to standardization of [[C Sharp (programming language)|C#]] and the CLI, the bytecode is now officially known as CIL.<ref>{{cite web
| url = http://www.interviewcity.com/2010/04/what-is-intermediate-languageilmsilcil.html
| title = What is Intermediate Language(IL)/MSIL/CIL in .NET
| accessdateaccess-date = 2011-02-17
| quote = CIL: ... When we compile [a]. NET project, it [is] not directly converted to binary code but to the intermediate language. When a project is run, every language of .NET programming is converted into binary code into CIL. Only some part of CIL that is required at run time is converted into binary code. DLL and EXE of .NET are also in CIL form.}}</ref> [[Windows Defender]] virus definitions continue to refer to binaries compiled with it as MSIL.<ref name="Defender">{{cite web |title=HackTool:MSIL/SkypeCracker |url=https://www.microsoft.com/en-us/wdsi/threats/malware-encyclopedia-description?Name=HackTool:MSIL/SkypeCracker&threatId=-2147221892 |publisher=Microsoft |accessdateaccess-date=26 November 2019}}</ref>
 
==General information==
During compilation of [[List of CLI languages|CLI programming languages]], the [[source code]] is translated into CIL code rather than into platform- or processor-specific [[object file|object code]]. CIL is a [[CPU]]- and platform-independent instruction set that can be executed in any environment supporting the Common Language Infrastructure, such as the [[Common Language Runtime|.NET runtime]] on [[Microsoft Windows|Windows]], or the [[cross-platform]] [[Mono (software)|Mono]] runtime. In theory, this eliminates the need to distribute different executable files for different platforms and CPU types. CIL code is verified for safety during runtime, providing better security and reliability than natively compiled executable files.<ref>{{cite book| url = https://books.google.com/books?id=VGT1_UJzjM0C&q=CIL+is+platform-independent&pg=PA15| title = Benefits of CIL| access-date = 2011-02-17| last1 = Troelsen| first1 = Andrew| date = 2009-05-02| publisher = Apress| isbn = 9781590598849}}</ref><ref>{{cite web|url=https://www.visualcplusdotnet.com/visualcplusdotnet1.html|title=Unmanaged, Managed Extensions for C++, Managed and .Net Framework|website=www.visualcplusdotnet.com|access-date=2020-07-07}}</ref>
<ref> {{cite book| url = https://books.google.com/books?id=VGT1_UJzjM0C&pg=PA15&lpg=PA15&dq=CIL+is+platformindependent#v=onepage&q=CIL%20is%20platform-independent&f=false| title = Benefits of CIL| access-date = 2011-02-17| last1 = Troelsen| first1 = Andrew| date = 2009-05-02}}</ref>
<ref> {{cite web|url=https://www.visualcplusdotnet.com/visualcplusdotnet1.html|title=Unmanaged, Managed Extensions for C++, Managed and .Net Framework|website=www.visualcplusdotnet.com|access-date=2020-07-07}}</ref>
 
The execution process looks like this:
Line 38 ⟶ 36:
 
==Computational model==
{{GOCEinuse|section=yes}}
<!-- {{Copy edit|section|date=June 2020}} starting copy edit 20 July 2020 -->
 
The Common Intermediate Language is object-oriented and [[stack-based]], which means that instruction parameters and results are kept on a single stack instead of in several registers or other memory locations, as in most [[programming language]]s.
 
Line 48 ⟶ 43:
</syntaxhighlight>
 
CouldCode in an [[intermediate language]] (IL) look like this, where 0 is eax and 1 is edx:
<syntaxhighlight lang="csharp">
ldloc.0 // push local variable 0 onto stack
Line 59 ⟶ 54:
 
===Object-oriented concepts===
CIL is designed to be object-oriented. YouOne may create objects, call methods, and use other types of members, such as fields.
 
Every [[Method (programming)|method]] needs (with some exceptions) to reside in a class. So does this static method:
<syntaxhighlight lang="csharp">
.class public Foo {
.method public static int32 Add(int32, int32) cil managed {
{
.method public static int32 Add(int32, int32) cil managed
{
.maxstack 2
ldarg.0 // load the first argument;
Line 76 ⟶ 69:
</syntaxhighlight>
 
The method FooAdd does not require any instance of Foo to be declared because it is declared as static, and it may then be used like this in C#:
<syntaxhighlight lang="csharp">
int r = Foo.Add(2, 3); // 5
Line 92 ⟶ 85:
An instance class contains at least one [[Constructor (object-oriented programming)|constructor]] and some [[Instance (computer science)|instance]] members. The following class has a set of methods representing actions of a Car-object.
<syntaxhighlight lang="csharp">
.class public Car {
.method public specialname rtspecialname instance void .ctor(int32, int32) cil managed {
{
.method public specialname rtspecialname instance void .ctor(int32, int32) cil managed
{
/* Constructor */
}
 
.method public void Move(int32) cil managed { /* Omitting implementation */ }
.method public void TurnRight() cil managed { /* Omitting implementation */ }
{
.method public void TurnLeft() cil managed { /* Omitting implementation */ }
.method public void Brake() cil managed { /* Omitting implementation */ }
}
 
.method public void TurnRight() cil managed
{
/* Omitting implementation */
}
 
.method public void TurnLeft() cil managed
{
/* Omitting implementation */
}
 
.method public void Brake() cil managed
{
/* Omitting implementation */
}
}
</syntaxhighlight>
Line 161 ⟶ 137:
 
==Example==
Below is a basic [["Hello, World!" program]] program written in CIL assembler. It will display the string "Hello, world!".
<syntaxhighlight lang="csharp">
.assembly Hello {}
Line 194 ⟶ 170:
</syntaxhighlight>
 
In CIL assembler syntax it looks like this:
<syntaxhighlight lang="cpp">
.method private hidebysig static void Main(string[] args) cil managed
Line 233 ⟶ 209:
</syntaxhighlight>
 
This is just a representation of how CIL looks like near the [[virtual machine]] (VM-) level. When compiled the methods are stored in tables and the instructions are stored as bytes inside the assembly, which is a [[Portable Executable]] (PE).
 
==Generation==
Line 243 ⟶ 219:
 
===Just-in-time compilation===
[[Just-in-time compilation]] (JIT) involves turning the byte-code into code immediately executable by the CPU. The conversion is performed gradually during the program's execution. JIT compilation provides environment-specific optimization, runtime [[type safety]], and assembly verification. To accomplish this, the JIT compiler examines the assembly metadata for any illegal accesses and handles violations appropriately.
 
===Ahead-of-time compilation===
Line 251 ⟶ 227:
 
==Pointer instructions - C++/CLI==
A notable difference from Java's bytecode is that CIL comes with {{code|ldind}}, {{code|stind}}, {{code|ldloca}}, and many call instructions which are enough for data/function pointers manipulation needed to compile C/C++ code into CIL.
 
<syntaxhighlight lang="cpp">
Line 317 ⟶ 293:
 
==See also==
*[[LLVM]]
*[[MLIR (software)]]
*[[List of CIL instructions]]
*[[List of CLI languages]]
Line 322 ⟶ 300:
==References==
{{Reflist}}
 
==Further reading==
* {{cite book|author-last=Bock|author-first=Jason|year=2002|title=CIL Programming: Under the Hood of .NET|publisher=Apress|isbn=978-1590590416}}
 
==External links==
*[httphttps://www.ecma-international.org/publications-and-standards/standards/Ecmaecma-335.htm/ Common Language Infrastructure (Standard ECMA-335)]
*[https://www.visualstudio.com/license-terms/ecma-c-common-language-infrastructure-standards/ “ECMA C# and Common Language Infrastructure Standards” on the Visual Studio website]
*[[wikibooks:Computer Programming/Hello world#CIL|Hello world program in CIL]]
Line 330 ⟶ 311:
 
{{Common Language Infrastructure}}
{{.NET}}
 
[[Category:Assembly languages]]
[[Category:Bytecodes]]
[[Category:Common Language Infrastructure]]