User:Sundström/Drafts/Common Intermediate Language syntax
- Main article: Common Intermediate Language
This article describes the syntax of the Common Intermediate Language assembly language, abbr. as CIL.
Basics
[edit]Data types
[edit]CIL has three groups of built-in datatypes.
- Numerics types:
int32
,int64
,native int
,F
- Object reference:
O
- Pointer types:
native unsigned int
,&
Instruction set
[edit]- See also: List of CIL instructions
CIL bytecode has instructions for the following groups of tasks:
- Load and store
- Arithmetic
- Type conversion
- Object creation and manipulation
- Operand stack management (push / pop)
- Control transfer (branching)
- Method invocation and return
- Throwing exceptions
- Monitor-based concurrency
These are written inside of the body of a function declaration.
Modifier attributes
[edit]Member attributes instance
Declarations
[edit]Modules
[edit]Global functions
[edit]Classes
[edit]Methods
[edit]A method is a function or subroutin that belongs to a class. It contains a return type and possibly a parameter list is such is defined. A method in CIL can either be an instance member or a type member that is part of the class and not any specific object.
This is an instance method called Bar
and that takes one parameter. It takes the parameter does something to it
.method private instance void Bar(int32) cil managed
{
.maxstack 2
nop
ldarg.0
call instance class ConsoleApplication1.Token ConsoleApplication1.Parser::GetLookaheadToken()
ret
}
Constructors
[edit]A constructor, or initializer, is a method that is called when a type is about to get created or used for the first time. CIL supports both instance initializers and type initializers.
Initializers are simply ordinary methods that return void
but have special names.
Instance initializer
An instance initializer is what usually is called a constructor. As the name suggests it initialize an object instance with initial data. It is called when the newobj
instruction is called with it as its argument.
.method public void .ctor()
{
.maxstack 1
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ldstr ".ctor"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
In the example above the constructor of the base class System.Object::.ctor
is called first.
Destructors
[edit]Try-Catch blocks
[edit]Properties
[edit]Properties are a piece of syntactic sugar wraps the traditional accessor convention that what often would be getter and setter methods. In CIL they are treated as a special member that usually in a high-level language like C# is implemented with a field like syntax.
In the CIL the properties are declared as a separate structure. The signatures of the .get
and .set
methods reside inside the declaration. These are implemented separately like any other method but are treated specially at runtime.
//An instance property with both a getter and a setter.
.property instance int32 Age
{
.get instance int32 Person::get_Age()
.set instance void Person::set_Age(int32)
}
A property must have a getter but the setter can be left to make it read-only. Properties can be both static class members as well as instance members.
Inheritance
[edit]In CIL class can extend, or inherit, one class. If none is specified then it will implicitly inherit System.Object
which is the ultimate base class of all objects in the Common Language Infrastructure.
.class public auto ansi beforefieldinit Horse
extends Mammal
{
}
Implement interfaces
[edit]A class can implement multiple interfaces.
.class public auto ansi beforefieldinit Whale
extends Mammal implements ISeaDweller
{
}