You are in the right direction to get rid of "nop" s:
When you provide an additional argument for calling Emit, always check the MSDN for the correct type of argument.
For OpCodes.Ldc_I4_S, MSDN declares:
ldc.i4.s is a more efficient encoding for pushing integers from -128 to 127 onto the stack> evaluation.
The following Emit method overload may use the ldc.i4.s opcode:
ILGenerator.Emit (OpCode, byte)
Thus, the second part of your code will have unpredictable results (besides these pesky nop) at runtime, since you are trying to load "int8" on the stack, but providing "int32" or "short", Value:
else if (IsBetween(value, short.MinValue, short.MaxValue)) { gen.Emit(OpCodes.Ldc_I4_S, (short)value); } else { gen.Emit(OpCodes.Ldc_I4_S, value); }
You should use Ldc_I4 instead of Ldc_I4_S if you want to correctly load int32 / short (or something larger than a byte) onto the stack. So your code should look like the one shown above:
else { gen.Emit(OpCodes.Ldc_I4, value); }
This is a wild hunch, but the three new ones that were generated should probably do something with extra bytes from your int32
Hope this helps ...
T. fabre
source share