Common Intermediate Language

Common Intermediate Language (CIL), formerly called Microsoft Intermediate Language (MSIL) or Intermediate Language (IL),[1] is the intermediate language binary instruction set defined within the Common Language Infrastructure (CLI) specification.[2] CIL instructions are executed by a CIL-compatible runtime environment such as the Common Language Runtime. Languages which target the CLI compile to CIL. CIL is object-oriented, stack-based bytecode. Runtimes typically 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# and the CLI, the bytecode is now officially known as CIL.[3] Windows Defender virus definitions continue to refer to binaries compiled with it as MSIL.[4]

General information

During compilation of CLI programming languages, the source code is translated into CIL code rather than into platform- or processor-specific 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 .NET runtime on Windows, or the cross-platform 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.[5][6]

The execution process looks like this:

  1. Source code is converted to CIL bytecode and a CLI assembly is created.
  2. Upon execution of a CIL assembly, its code is passed through the runtime's JIT compiler to generate native code. Ahead-of-time compilation may also be used, which eliminates this step, but at the cost of executable-file portability.
  3. The computer's processor executes the native code.

Instructions

CIL bytecode has instructions for the following groups of tasks:

Computational model

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 languages.

Code that adds two numbers in x86 assembly language, where eax and edx specify two different general-purpose registers:

add eax, edx

Code in an intermediate language (IL), where 0 is eax and 1 is edx:

ldloc.0    // push local variable 0 onto stack
ldloc.1    // push local variable 1 onto stack
add        // pop and add the top two stack items then push the result onto the stack
stloc.0    // pop and store the top stack item to local variable 0

In the latter example, the values of the two registers, eax and edx, are first pushed on the stack. When the add-instruction is called the operands are "popped", or retrieved, and the result is "pushed", or stored, on the stack. The resulting value is then popped from the stack and stored in eax.

Object-oriented concepts

CIL is designed to be object-oriented. One may create objects, call methods, and use other types of members, such as fields.

Every method needs (with some exceptions) to reside in a class. So does this static method:

.class public Foo {
    .method public static int32 Add(int32, int32) cil managed {
        .maxstack 2
        ldarg.0 // load the first argument;
        ldarg.1 // load the second argument;
        add     // add them;
        ret     // return the result;
    }
}

The method Add 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#:

int r = Foo.Add(2, 3);    // 5

In CIL it would look like this:

ldc.i4.2
ldc.i4.3
call int32 Foo::Add(int32, int32)
stloc.0

Instance classes

An instance class contains at least one constructor and some instance members. The following class has a set of methods representing actions of a Car-object.

.class public Car {
    .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 */ }
}

Creating objects

In C# class instances are created like this:

Car myCar = new Car(1, 4); 
Car yourCar = new Car(1, 3);

And those statements are roughly the same as these instructions in CIL:

ldc.i4.1
ldc.i4.4
newobj instance void Car::.ctor(int, int)
stloc.0    // myCar = new Car(1, 4);
ldc.i4.1
ldc.i4.3
newobj instance void Car::.ctor(int, int)
stloc.1    // yourCar = new Car(1, 3);

Invoking instance methods

Instance methods are invoked in C# as the one that follows:

myCar.Move(3);

As invoked in CIL:

ldloc.0    // Load the object "myCar" on the stack
ldc.i4.3
call instance void Car::Move(int32)

Metadata

The Common Language Infrastructure (CLI) records information about compiled classes as metadata. Like the type library in the Component Object Model, this enables applications to support and discover the interfaces, classes, types, methods, and fields in the assembly. The process of reading such metadata is called "reflection".

Metadata can be data in the form of "attributes". Attributes can be customized by extending the Attribute class. This is a powerful feature. It allows the creator of the class the ability to adorn it with extra information that consumers of the class can use in various meaningful ways, depending on the application domain.

Example

Below is a basic "Hello, World!" program written in CIL assembler. It will display the string "Hello, world!".

.assembly Hello {}
.assembly extern mscorlib {}
.method static void Main()
{
    .entrypoint
    .maxstack 1
    ldstr "Hello, world!"
    call void [mscorlib]System.Console::WriteLine(string)
    ret
}

The following code is more complex in number of opcodes.

This code can also be compared with the corresponding code in the article about Java bytecode.

static void Main(string[] args)
{
    for (int i = 2; i < 1000; i++)
    {
        for (int j = 2; j < i; j++)
        {
             if (i % j == 0)
                 goto outer;
        }
        Console.WriteLine(i);
        outer:;
    }
}

In CIL assembler syntax it looks like this:

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack  2
    .locals init (int32 V_0,
                  int32 V_1)

              ldc.i4.2
              stloc.0
              br.s       IL_001f
    IL_0004:  ldc.i4.2
              stloc.1
              br.s       IL_0011
    IL_0008:  ldloc.0
              ldloc.1
              rem
              brfalse.s  IL_001b
              ldloc.1
              ldc.i4.1
              add
              stloc.1
    IL_0011:  ldloc.1
              ldloc.0
              blt.s      IL_0008
              ldloc.0
              call       void [mscorlib]System.Console::WriteLine(int32)
    IL_001b:  ldloc.0
              ldc.i4.1
              add
              stloc.0
    IL_001f:  ldloc.0
              ldc.i4     0x3e8
              blt.s      IL_0004
              ret
}

This is just a representation of how CIL looks 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

A CIL assembly and instructions are generated by either a compiler or a utility called the IL Assembler (ILAsm) that is shipped with the execution environment.

Assembled CIL can also be disassembled into code again using the IL Disassembler (ILDASM). There are other tools such as .NET Reflector that can decompile CIL into a high-level language (e. g. C# or Visual Basic). This makes CIL a very easy target for reverse engineering. This trait is shared with Java bytecode. However, there are tools that can obfuscate the code, and do it so that the code cannot be easily readable but still be runnable.

Execution

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

CLI-compatible execution environments also come with the option to do an Ahead-of-time compilation (AOT) of an assembly to make it execute faster by removing the JIT process at runtime.

In the .NET Framework there is a special tool called the Native Image Generator (NGEN) that performs the AOT. A different approach for AOT is CoreRT that allows the compilation of .Net Core code to a single executable with no dependency on a runtime. In Mono there is also an option to do an AOT.

Pointer instructions - C++/CLI

A notable difference from Java's bytecode is that CIL comes with ldind, stind, ldloca, and many call instructions which are enough for data/function pointers manipulation needed to compile C/C++ code into CIL.

class A {
   public: virtual void __stdcall meth() {}
};
void test_pointer_operations(int param) {
	int k = 0;
	int * ptr = &k;
	*ptr = 1;
	ptr = &param;
	*ptr = 2;
	A a;
	A * ptra = &a;
	ptra->meth();
}

The corresponding code in CIL can be rendered as this:

.method assembly static void modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) 
        test_pointer_operations(int32 param) cil managed
{
  .vtentry 1 : 1
  // Code size       44 (0x2c)
  .maxstack  2
  .locals ([0] int32* ptr,
           [1] valuetype A* V_1,
           [2] valuetype A* a,
           [3] int32 k)
// k = 0;
  IL_0000:  ldc.i4.0 
  IL_0001:  stloc.3
// ptr = &k;
  IL_0002:  ldloca.s   k // load local's address instruction
  IL_0004:  stloc.0
// *ptr = 1;
  IL_0005:  ldloc.0
  IL_0006:  ldc.i4.1
  IL_0007:  stind.i4 // indirection instruction
// ptr = &param
  IL_0008:  ldarga.s   param // load parameter's address instruction
  IL_000a:  stloc.0
// *ptr = 2
  IL_000b:  ldloc.0
  IL_000c:  ldc.i4.2
  IL_000d:  stind.i4
// a = new A;
  IL_000e:  ldloca.s   a
  IL_0010:  call       valuetype A* modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall) 'A.{ctor}'(valuetype A* modopt([mscorlib]System.Runtime.CompilerServices.IsConst) modopt([mscorlib]System.Runtime.CompilerServices.IsConst))
  IL_0015:  pop
// ptra = &a;
  IL_0016:  ldloca.s   a
  IL_0018:  stloc.1
// ptra->meth();
  IL_0019:  ldloc.1
  IL_001a:  dup
  IL_001b:  ldind.i4 // reading the VMT for virtual call
  IL_001c:  ldind.i4
  IL_001d:  calli      unmanaged stdcall void modopt([mscorlib]System.Runtime.CompilerServices.CallConvStdcall)(native int)
  IL_0022:  ret
} // end of method 'Global Functions'::test_pointer_operations

See also

References

  1. ^ "Intermediate Language & execution". 19 April 2023.
  2. ^ "ECMA-335 Common Language Infrastructure (CLI)".
  3. ^ "What is Intermediate Language(IL)/MSIL/CIL in .NET". Retrieved 2011-02-17. 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.
  4. ^ "HackTool:MSIL/SkypeCracker". Microsoft. Retrieved 26 November 2019.
  5. ^ Troelsen, Andrew (2009-05-02). Benefits of CIL. Apress. ISBN 9781590598849. Retrieved 2011-02-17.
  6. ^ "Unmanaged, Managed Extensions for C++, Managed and .Net Framework". www.visualcplusdotnet.com. Retrieved 2020-07-07.

Further reading

  • Bock, Jason (2002). CIL Programming: Under the Hood of .NET. Apress. ISBN 978-1590590416.

Read other articles:

竹塹城護城河 新竹縣(1876年-1895年),是清朝在臺灣所設的行政區劃之一,係1876年1月16日(清光緒元年12月20日)沈葆楨奏請新設臺北府,設立時將淡水廳分割成淡水縣、基隆廳與新竹縣而來[1],而原淡水廳治竹塹位於此縣境內,故沿用為縣治[2]。 新竹縣剛成立時,北以頭重溪(屬社子溪水系)土牛溝與淡水縣為界,南以大甲溪與彰化縣為界,相當於今天臺灣桃

 

Cet article est une ébauche concernant une localité de Castille-et-León. Vous pouvez partager vos connaissances en l’améliorant (comment ?) selon les recommandations des projets correspondants. Pour les articles homonymes, voir San Silvestre. Encina de San Silvestre Héraldique Drapeau Église paroissiale Saint-Michel Administration Pays Espagne Communauté autonome Castille-et-León Province Province de Salamanque Comarque Tierra de Ledesma District judic. Salamanque Maire Mandat ...

 

2003 Russian filmCrazy Day or The Marriage of FigaroDVD cover with Philipp Kirkorov, Lolita Milyavskaya and Boris KhoshnyanskyDirected bySemen GorovStarringSofia RotaruPhilipp KirkorovDistributed byNTV, Inter, Melorama ProductionRelease dates 31 December 2003 (2003-12-31) (Russia) 1 January 2004 (2004-01-01) (Ukraine) Running time131 minutesCountriesRussiaUkraineLanguageRussian Crazy Day or The Marriage of Figaro (Russian: Безумный день, или

Ini adalah daftar katedral di Uzbekistan diurutkan berdasarkan denominasi. Katedral Hati Kudus Yesus, Tashkent Katolik Katedral Gereja Katolik di Uzbekistan:[1] Katedral Hati Kudus Yesus, Tashkent Lihat juga Gereja Katolik Roma Gereja Katolik di Uzbekistan Daftar katedral Administrasi Apostolik Uzbekistan Referensi ^ GCatholic.org: Katedral Uzbekistan lbsDaftar katedral di AsiaNegaraberdaulat Afganistan Arab Saudi Armenia1 Azerbaijan1 Bahrain Bangladesh Bhutan Brunei Filipina Georgia1...

 

Роман Весна Особисті дані Повне ім'я Роман Костянтинович Весна Народження 5 листопада 1947(1947-11-05) (76 років)   Винники, Львівська область Громадянство  СРСР Позиція півзахисник Професіональні клуби* Роки Клуб І (г) 1965 «Карпати» (Львів) 1 (0) 1968—1970 «Авангард» (Тернопіл...

 

City in Florida, United StatesOakland Park, FloridaCityCity of Oakland ParkOakland Park's City Hall in July 2008 FlagSealMotto: Engaged·Inspired·United: A City on the Move[1]Location of Oakland Park in Broward County, FloridaOakland Park, FloridaCoordinates: 26°10′35″N 80°08′40″W / 26.17639°N 80.14444°W / 26.17639; -80.14444Country United StatesState FloridaCountyBrowardSettled (Colohatchee Settlement)Late 1800s–Early 1900s[2&...

مايكل رابابورت   معلومات شخصية الميلاد 20 مارس 1970 (53 سنة)[1]  نيويورك،  ومانهاتن  الإقامة لوس أنجلوسنيويوركمانهاتن  مواطنة الولايات المتحدة  عضو في ساج أفترا  الحياة العملية المدرسة الأم ثانوية صالة إيراسموس  [لغات أخرى]‏  المهنة ممثل تلفزيوني...

 

هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (يناير 2016) يفتقر محتوى هذه المقالة إلى الاستشهاد بمصادر. فضلاً، ساهم في تطوير هذه المقالة من خلال إضافة مصادر موثوق بها. أي معلومات غير موثقة يمكن التشكيك بها وإزالتها. ...

 

الأرمن في روسيامعلومات عامةالشتات شتات أرمني التعداد الكليالتعداد 1,182,388 (إحصاء 2010)[1]2,000,000[2]—2,500,000 (تقديرات غير رسمية)[3]0.8%-1.7% من إجمالي عدد سكان روسيامناطق الوجود المميزةالبلد روسيا موسكو، كراسنودار كراي، ستافروبول كراي، روستوف أوبلاستاللغات الروسية، الأرمن...

Untuk kegunaan lain, lihat Manggala (disambiguasi). Soto manggala adalah soto khas Kota Pangkalan Bun, Kalimantan Tengah. Soto ini terbuat dari bahan singkong atau ketela. Soto ini dinamakan soto manggala karena rakyat Pangkalan Bun menyebut singkong dengan nama manggala. Cara memakan soto ini juga berbeda; soto ini tidak dimakan dengan nasi, melainkan dengan singkong. Daging yang digunakan dalam soto ini juga berbeda, mereka menggunakan kaki ayam. Karena saking uniknya, makanan ini juga semp...

 

Peta Holokaus: kamp pemusnahan Nazi (ditandai dengan gambar tengkorak) di wilayah Polandia, 1942. Kamp pemusnahan atau kamp kematian adalah kamp yang dirancang dan dibangun oleh Jerman Nazi selama Perang Dunia II (1939–45) untuk memusnahkan jutaan orang Yahudi, Slavia, Rom, komunis, dan kelompok-kelompok lain yang dianggap sebagai Untermenschen (bangsa rendahan). Mereka dibunuh dengan menggunakan gas atau kondisi kerja yang ekstrem dan kelaparan.[1][2] Gagasan pemusnahan mas...

 

Die Ägäische Platte, auch Ägäische Mikroplatte oder Hellenische Platte ist eine Kontinentalplatte im südöstlichen Europa. Im Norden grenzt sie an die Eurasische Platte, im Osten an die Anatolische Platte, im Süden an die Afrikanische Platte, im Westen an die Apulische Platte. Auf der Ägäischen Platte liegen im Süden Griechenlands die Peloponnes, die Halbinsel Attika, die Inseln Euböa, Kreta, Santorin und Therasia sowie die südlichen Ionischen Inseln bis Lefkada im Norden, die Kykl...

التدابير المتبعة مثل التباعد الاجتماعي والبقاء في المنزل وغسل اليدين يتيح مزيدًا من الوقت لزيادة قدرة الرعاية الصحية للتعامل بشكل أفضل مع المريض، حيث يمكن استخدام الوقت من خلال تسطيح المنحنى لرفع القدرة على الرعاية الصحية لتلبية الطلب المتزايد بشكل أفضل. الاحتمالات البد...

 

AlexandrosRaja HellenesBerkuasa11 Juni 1917 – 25 Oktober 1920[a]PendahuluKonstantinos IPenerusKonstantinos IInformasi pribadiKelahiran(1893-08-01)1 Agustus 1893Istana Tatoi, YunaniKematian25 Oktober 1920(1920-10-25) (umur 27)Athena, YunaniPemakaman29 Oktober 1920Pemakaman Kerajaan, Istana Tatoi, YunaniWangsaWangsa Schleswig-Holstein-Sonderburg-GlücksburgAyahKonstantinus I dari YunaniIbuSophia dari PrusiaPasanganAspasia ManosAnakAleksandra, Ratu YugoslaviaAgamaOrtodoks Yunani A...

 

1921 film ShatteredWerner Krauss in 'Shattered'Directed byLupu PickWritten byCarl MayerLupu PickProduced byLupu PickStarringWerner KraussEdith PoscaHermine Straßmann-WittCinematographyFriedrich WeinmannMusic byGiuseppe BecceAlexander SchirmannProductioncompanyRex-Film GmbHDistributed byFilm Arts GuildRelease date 27 May 1921 (1921-05-27) Running time50 minutesCountryGermanLanguageSilent film Shattered (German: Scherben) is a 1921 German silent Kammerspielfilm directed by Lupu ...

Biografi ini memerlukan lebih banyak catatan kaki untuk pemastian. Bantulah untuk menambahkan referensi atau sumber tepercaya. Materi kontroversial atau trivial yang sumbernya tidak memadai atau tidak bisa dipercaya harus segera dihapus, khususnya jika berpotensi memfitnah.Cari sumber: Pakubuwana XIII – berita · surat kabar · buku · cendekiawan · JSTOR (Pelajari cara dan kapan saatnya untuk menghapus pesan templat ini) Pakubuwana XIIIꦦꦑꦸꦨꦸꦮ...

 

Гран-прі Бахрейну 2021 Formula 1 Gulf Air Bahrain Grand Prix 2021 1-й з 23 етапів сезону 2021/ Гонка №1036 ←  • Гран-прі Емілії-Романьї 2021 → Дата 28 березня 2021 року Місце Сахір, Бахрейн Траса Сахір Довжина кола 5.412 км Дистанція 308.238 км (56 кіл) Погода Поул 1:28.997 Макс Ферстаппен Red Bull Racing Найшв...

 

  关于与「張強 (國民大會代表)」標題相近或相同的条目,請見「张强」。 張強个人资料出生?年?月?日国籍 大清(?年–1911年)  中華民國(1912年–?年) 中華民國(?年–?年) 張強(?—?),字毅夫,浙江温州人,中华民国政治人物,曾任中華民國第一届國民大會代表、浙江省議會議長等职。毕业于黄埔军校,曾任教於東吳大學。曾于中共建政后...

Tabriz Ancient names: Davrezh, Tavrezh, TavrezTop:Maqbaratol Shoara Tomb, Middle left:Saat Tower, Middle right:Statue of Bagher Khan in Tabriz Constitution House, Bottom:View of Shah-goli Park and Dadgostari area LambangNegara IranProvinsiEast Azerbaijan ProvinceEstablished dateN/APemerintahan • wali kotaAlireza Navin • dewan kotaZahra EftekhariLuas • Kota324 km2 (125 sq mi) • Luas perkotaan2.356 km2 (910 sq mi)Keti...

 

Short story by Ernest Hemingway set in Africa Illustration by Dean Cornwell in September 1936's Cosmopolitan accompanied by the first publication of the story The Short Happy Life of Francis Macomber is a short story by Ernest Hemingway. Set in Africa, it was published in the September 1936 issue of Cosmopolitan magazine concurrently with The Snows of Kilimanjaro. The story was eventually adapted to the screen as the Zoltan Korda film The Macomber Affair (1947). Synopsis The Short Happy Life ...

 

Strategi Solo vs Squad di Free Fire: Cara Menang Mudah!