CIL instructions - valid "parameters"

81 views
Skip to first unread message

Gábor Kozár

unread,
Jun 26, 2011, 11:19:57 AM6/26/11
to mono-...@googlegroups.com
Hey,

I'm writing a stack analyzer using Mono.Cecil: ideally it should be able to tell me what is on the evaluation stack at the given instruction of a method body.
For this, I need to be able to tell the type of the values pushed to / pulled from the evaluation stack by the instructions.

This is mostly fine, however, there are some things around the native CIL types that are confusing.

For example, when I write this in C#:

return true;

this is the CIL emitted:

ldc.i4.1
ret

So apparently, the int32 value of '1' is bool 'true' as far as CIL is concerned. So what are the rules here? Are there any other such 'implicit conversions' I should be aware of?

Another thing that's not quite clear for me are the 'add', 'div', 'rem', 'clt' etc. instructions, specifically, the values they use. Do they work only on Int32? What happens if they get Int16 or Int64?

Is there a site or a tutorial or a book, or whatever that can teach me CIL? I did some searches, but only found some very basic examples - nothing too useful.

Thank you very much!

Alex

unread,
Jun 26, 2011, 12:12:40 PM6/26/11
to mono-...@googlegroups.com
Hello,

>So apparently, the int32 value of '1' is bool 'true' as far as CIL is concerned. So what are the rules here? Are there any other such 'implicit conversions' I should be aware of?

The only other case I can think of is 'char'.

>Another thing that's not quite clear for me are the 'add', 'div', 'rem', 'clt' etc. instructions, specifically, the values they use. Do they work only on Int32? What happens if they get Int16 or Int64?

In CIL, types are not encoded in instructions, but rather in local
variables, parameters, fields, etc. So, the instructions you mention
operate on both integers and floats (don't misunderstand; they're
still type-safe).

>Is there a site or a tutorial or a book, or whatever that can teach me CIL? I did some searches, but only found some very basic examples - nothing too useful.

I would recommend Expert .NET 2.0 IL Assembler and generally ECMA 335
Partition II and III.

Regards,
Alex

> --
> --
> mono-cecil

Gábor Kozár

unread,
Jun 27, 2011, 10:17:49 AM6/27/11
to mono-...@googlegroups.com
So char, the integer types and bool are all interchangable?
How about the 'ceq' instruction? Does it only work on the types above (lets call them 'numeric types'), or does it have some magic way of comparing other types?

Thanks for the book recommendation, I have ordered it! :)

2011/6/26 Alex <xtzg...@gmail.com>
--
--
mono-cecil

Alex

unread,
Jun 27, 2011, 12:44:47 PM6/27/11
to mono-...@googlegroups.com
Hi,

>So char, the integer types and bool are all interchangable?

Technically, yes. The CLR treats bools and chars as 32-bit integers
(even if a char really is only 16 bits and a bool 8 bits).

>How about the 'ceq' instruction? Does it only work on the types above (lets call them 'numeric types'), or does it have some magic way of comparing other types?

All such instructions only operate on primitive types that the VES
knows how to compare. Object.Equals(), IEquatable<T>, etc are all
independent of CIL and must be called manually.

Regards,
Alex

> --
> --
> mono-cecil

Gábor Kozár

unread,
Jun 27, 2011, 12:56:33 PM6/27/11
to mono-...@googlegroups.com
Thank you very much Alex, you've been a great deal of help!

Just a final stupid question: what is VES?

2011/6/27 Alex <xtzg...@gmail.com>
--
--
mono-cecil

Alex

unread,
Jun 27, 2011, 1:04:51 PM6/27/11
to mono-...@googlegroups.com
Hi,

Just a fancy ECMA 335 abbreviation for Virtual Execution System (i.e.
the runtime). :)

Regards,
Alex

> --
> --
> mono-cecil

Gábor Kozár

unread,
Jun 27, 2011, 1:11:35 PM6/27/11
to mono-...@googlegroups.com
Oh, all right. :)
Thank you very much once again!

2011/6/27 Alex <xtzg...@gmail.com>
--
--
mono-cecil

Reply all
Reply to author
Forward
0 new messages