Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does 'code size' in MSIL mean

Tags:

.net

cil

When you see a line in the IL like:

// Code size       25 (0x19)

what does nit actually mean? Is it talking about the (bytes of) memory usage? All I was able to gather was that it is one more than the number of IL lines. Below is the full IL.

.method private hidebysig static void  Execute(string y) cil managed
{
  // Code size       25 (0x19)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldstr      "string"
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  nop
  IL_000c:  ldarg.0
  IL_000d:  callvirt   instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
  IL_0012:  call       void [mscorlib]System.Console::WriteLine(object)
  IL_0017:  nop
  IL_0018:  ret
} //

When I look at ECMA_335 documentation, it defines code size as: Size of the code (text) section, or the sum of all code sections if there are multiple sections. (huh?)

like image 647
Arun Avatar asked Jun 30 '11 03:06

Arun


1 Answers

Not all IL opcodes are the same size, and the encoding of the arguments to different opcodes also take up different amounts of space. The "code size" is the size of all opcodes plus the sizes of their operands.

For instance, the nop opcode is a single byte (as seen by OpCodes.Nop.Size), and doesn't take any operand (as seen by OpCodes.Nop.OperandType), so the first line requires a single byte (which is why the second line is labeled with IL_0001).

Likewise, ldstr is also a single byte, but the string argument is represented in IL by a string table offset, which takes 4 bytes (importantly, the contents of the string don't factor into this calculation), so your second IL instruction takes 5 bytes total (and the third line gets labeled with IL_0006).

And so on.

like image 54
kvb Avatar answered Jan 25 '23 12:01

kvb