Skip to content

Latest commit

 

History

History
52 lines (31 loc) · 3.11 KB

iii.4.27-stelem-type.md

File metadata and controls

52 lines (31 loc) · 3.11 KB

III.4.27 stelem.<type> – store an element of an array

Format Assembly Format Description
9C stelem.i1 Replace array element at index with the int8 value on the stack.
9D stelem.i2 Replace array element at index with the int16 value on the stack.
9E stelem.i4 Replace array element at index with the int32 value on the stack.
9F stelem.i8 Replace array element at index with the int64 value on the stack.
A0 stelem.r4 Replace array element at index with the float32 value on the stack.
A1 stelem.r8 Replace array element at index with the float64 value on the stack.
9B stelem.i Replace array element at index with the native int value on the stack.
A2 stelem.ref Replace array element at index with the ref value on the stack.

Stack Transition:

…, array, index, value → …,

Description:

The stelem.<type> instruction replaces the value of the element with zero-based index index (of type int32 or native int) in the one-dimensional array array with value. Arrays are objects and hence represented by a value of type O.

Storing into arrays that hold values smaller than 4 bytes whose intermediate type is int32 truncates the value as it moves from the stack to the array. Floating-point values are rounded from their native size (type F) to the size associated with the array. (See §III.1.1.1, Numeric data types.)

All variants, except stelem.ref, are equivalent to the stelem instruction (§III.4.26) with an appropriate typeTok.

Note that stelem.ref implicitly casts value to the element type of array before assigning the value to the array element. This cast can fail, even for verified code. Thus the stelem.ref instruction can throw the ArrayTypeMismatchException. This behavior differs from stelem.

[Note: for one-dimensional arrays that aren't zero-based and for multidimensional arrays, the array class provides a StoreElement method. end note]

Exceptions:

System.NullReferenceException is thrown if array is null.

System.IndexOutOfRangeException is thrown if index is negative, or larger than the bound of array.

System.ArrayTypeMismatchException is thrown if array doesn't hold elements of the required type.

Correctness:

Correct CIL requires that array be a zero-based, one-dimensional array, and that the type in the instruction is array-element-compatible-with its declared element type.

Verifiability:

Verification requires that:

  • the tracked type of array is T[], for some T;

  • for stelem.ref the tracked type of value is a reference type and is (array-element)compatible-with T;

  • for other instruction variants the tracked type of value is array-element-compatible-withI.8.7.1) the type in the instruction, and the type in the instruction is array-element-compatible-with T; and

  • the type of index is int32 or native int.