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
Data types
- Numerics types:
int32
,int64
,native int
,F
- Object reference:
O
- Pointer types:
native unsigned int
,&
Instruction set
- 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
Member attributes instance
Declarations
Modules
Global functions
Classes
Methods
.method private () cil managed
{
.maxstack 2
.locals init (
[0] class ConsoleApplication1.Token token,
[1] valuetype System.
nop
ldarg.0
call instance class ConsoleApplication1.Token ConsoleApplication1.Parser::GetLookaheadToken()
ret
}
Constructors
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
Try-Catch blocks
Properties
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
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
A class can implement multiple interfaces.
.class public auto ansi beforefieldinit Whale
extends Mammal implements ISeaDweller
{
}