Search code examples
c#cil

C#/CIL: type of native int


I am writing some tools to help validate IL that is emitted at runtime. A part of this validation involves maintaining a Stack<Type> as OpCodes are emitted so that future OpCodes that utilize these stack elements can be validated as using the proper types. I am confused as to how to handle the ldind.i opcode, however.

The Microsoft documentation states:

The ldind.i instruction indirectly loads a native int value from the specified address (of type native int, &, or *) onto the stack as a native int.

In C#, native int is not defined, and I am confused as to what type most accurately represents this data. How can I determine what its size is, and which C# type should be used to represent it? I am concerned it will vary by system hardware.


Solution

  • To my mind, you'd be better off looking at how the VES is defined and using a dedicated enum to model the types on the stack rather than C# visible types. Otherwise you're in for a rude surprise when we get to the floating point type.

    From MS Partition I.pdf1, Section 12.1:

    The CLI model uses an evaluation stack [...] However, the CLI supports only a subset of these types in its operations upon values stored on its evaluation stack—int32, int64, and native int. In addition, the CLI supports an internal data type to represent floating-point values on the internal evaluation stack. The size of the internal data type is implementation-dependent.

    So those, as well as things like references are the things you should track, and I'd recommend you do that with an explicit model of the VES Stack using its terms.


    1ECMA C# and Common Language Infrastructure Standards