Skip to content

Latest commit

 

History

History
13 lines (7 loc) · 1.88 KB

i.12.1.2-handling-of-short-integer-data-types.md

File metadata and controls

13 lines (7 loc) · 1.88 KB

I.12.1.2 Handling of short integer data types

The CLI defines an evaluation stack that contains either 4-byte or 8-byte integers; however, it also has a memory model that encompasses 1- and 2-byte integers. To be more precise, the following rules are part of the CLI model:

  • Loading from 1- or 2-byte locations (arguments, locals, fields, statics, pointers) expands to 4-byte values. For locations with a known type (e.g., local variables) the type being accessed determines whether the load sign-extends (signed locations) or zero-extends (unsigned locations). For pointer dereference (ldind.*), the instruction itself identifies the type of the location (e.g., ldind.u1 indicates an unsigned location, while ldind.i1 indicates a signed location).

  • Storing into a 1- or 2-byte location truncates to fit and will not generate an overflow error. Specific instructions (conv.ovf.*) can be used to test for overflow before storing.

  • Calling a method assigns values from the evaluation stack to the arguments for the method, hence it truncates just as any other store would when the argument is larger than the parameter.

  • Returning from a method assigns a value to an invisible return variable, so it also truncates as a store would when the type of the value returned is larger than the return type of the method. Since the value of this return variable is then placed on the evaluation stack, it is then sign-extended or zero-extended as would any other load. Note that this truncation followed by extending is not identical to simply leaving the computed value unchanged.

It is the responsibility of any translator from CIL to native machine instructions to make sure that these rules are faithfully modeled through the native conventions of the target machine. The CLI does not specify, for example, whether truncation of short integer arguments occurs at the call site or in the target method.