Intermediate Language of C#

Types of IL (Intermediate Language)
– CIL (Common Intermediate Language)
– MSIL (Microsoft Intermediate Language)

View IL in Unity project on MonoDevelop
1. Open Edit References
Solution Window | References | Settings
2. Open dll file by Assembly Browser
Edit References | .Net Assembly | Browse

2 .NET Compilers
1) Intermediate Language Compiler => CIL (Common Intermediate Language)
2) JIT (just-in-time) Compiler => Machine Language (optimized for CPU)

Demonstration

int i = 456;
i = i + 1;

ldc.i4.1
ldloc.0
add
stloc.0

ldc.i4.1
Load the 4-byte signed integer constant 1 on the evaluation stack.
[Local variable locations] 456
[Evaluation stack] 1

ldloc.0
Load the variable in location 0 on the evaluation stack. The other value (1) on the stack is pushed down.
[Local variable locations] 456
[Evaluation stack] 456, 1

add
Add the top two numbers on the evaluation stack together, and replace them with the result.
[Local variable locations] 456
[Evaluation stack] 457

stloc.0
Remove the value at the top of the evaluation stack, and store it in location 0.
[Local variable locations] 457
[Evaluation stack]

Reference

box: box the top value on the stack
bne: branch if top 2 values on stack are not equal
call: call a static member of a class
callvirt: call an instance member of a class
ldelem/stelem: load and store array elements
newarr: create a new 1-dimensional array
newobj: create a new object on the heap
ret: return from method
throw: throw an exception
unbox: unbox the top reference on the stack

pop: Pop value from the stack.
dup: Duplicate the value on the top of the stack.
stsfld: Replace the value of field with val.
ldsfld: Push the value of field on the stack.

List of CIL instructions
https://en.wikipedia.org/wiki/List_of_CIL_instructions

ページトップへ