A Practical Guide to MSIL - struct Generation and Manipulation


A struct is a value type used to organize a group of related information variables into a single variable entity. All structures inherit from the System.ValueType class and are therefore a value type, that is, the struct instance is allocated on the thread's stack (stack), which itself stores the value, not a pointer to the value.


1. Declaring a struct
is the same as declaring a class, using the DefineType method of ModuleBuilder, but requires that the second parameter is TypeAttributes.Sealed, and the third parameter is typeof(ValueType).
Example:
var typeBuilder = moduleBuilder.DefineType(
"MyStruct,
TypeAttributes.Public ,
typeof(ValueType)
);

Second, the operation fields, methods, and attributes
are consistent with the members generated in the class.


Three, struct as a parameter related operations

1. The loading struct
is consistent with the general loading parameters, and the Ldarg_X class instruction is used

2. Load the fields of the struct
Use the Ldarga or Ldarga_S instruction to load the parameter address onto the stack, and then use the Ldfld instruction to load the field.
Ldarga_S is a short form instruction for Ldarga


3. Use the Ldarga or Ldarga_S instruction to load the parameter address onto the stack for the field saved to the struct , and then use the Sdfld instruction to load the field.

4. The method of calling struct
Use the Ldarga or Ldarga_S instruction to load the parameter address onto the stack, and then use the Call class instruction to call the method.

5. Save the struct
and the general save parameters are the same, use the Starg_X class instruction

Four, struct as a local variable related operations

1. Loading struct
is the same as loading local variables in general, using Ldloc_X class instructions

2. Load the fields of the struct.
Use the Ldloca or Ldloca_S instruction to load the parameter address onto the stack, and then use the Ldfld instruction to load the field.
Ldloca_S is the short form instruction of Ldloca


3. Use the Ldloca or Ldloca_S instruction to load the parameter address onto the stack for the field saved to the struct , and then use the Sdfld instruction to load the field.

4. The method of calling struct
Use the Ldloca or Ldloca_S instruction to load the parameter address onto the stack, and then use the Call class instruction to call the method.

5. Saving struct
is the same as generally saving local variables, using Stloc_X class instructions

Four, struct as a field related operations

1. The loading struct
is consistent with the general loading field, first generate and load this, and then use the Ldfld class instruction

2. Load the field of struct
First generate and load this, then use the Ldflda or Ldflda_S instruction to load the struct field address, and then use the Ldfld instruction to load the field.
Ldflda_S is a short form instruction for Ldflda

3. The fields saved to the struct first generate
and load this, then use the Ldflda or Ldflda_S instruction to load the struct field address onto the stack, and then use the Sdfld instruction to load the field.

4. The method of calling struct first generates
and loads this, then uses the Ldflda or Ldflda_S instruction to load the struct field address onto the stack, and then uses the Call class instruction to call the method.

5. Save the struct
and the general save fields are consistent, use the Stfld class instruction

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324572103&siteId=291194637