cil

“Operation could destablize the runtime” and DynamicMethod with value types

余生颓废 提交于 2020-01-03 09:52:12
问题 I'm trying to generalize the following IL (from Reflector): .method private hidebysig instance void SetValue(valuetype Test.TestFixture/ValueSource& thing, string 'value') cil managed { .maxstack 8 L_0000: nop L_0001: ldarg.1 L_0002: ldarg.2 L_0003: call instance void Test.TestFixture/ValueSource::set_Value(string) L_0008: nop L_0009: ret } However, when I try and reproduce this IL with DynamicMethod: [Test] public void Test_with_DynamicMethod() { var sourceType = typeof(ValueSource);

How to force compiler to use ldc.i8 IL instruction instead of ldc.i4 plus conv.i8?

扶醉桌前 提交于 2020-01-02 05:27:11
问题 This is my C# code: const long pSize = 20; No matter if I use x64 or Any CPU to build it in Release mode, I get below MSIL instructions: IL_0010: ldc.i4.s 20 IL_0012: conv.i8 However, MSIL has ldc.i8 instruction. How to make compiler to use this? 回答1: MSIL has ldc.i8 instruction. How to make compiler to use this? You don't. That would be worse code. ldc.i4.s 20 conv.i8 is three bytes of code: two for the instructions and one for the constant. Whereas ldc.i8 20 is nine bytes: one for the

How to pin a generic Span<T> instance to work on it with Parallel.For?

a 夏天 提交于 2020-01-02 01:56:12
问题 I'm rewriting some of my extension methods using the new Span<T> type and I'm having trouble finding a way to properly pin a generic instance to be able to use parallel code to work on it. As an example, consider this extension method: public static unsafe void Fill<T>(this Span<T> span, [NotNull] Func<T> provider) where T : struct { int cores = Environment.ProcessorCount, batch = span.Length / cores, mod = span.Length % cores, sizeT = Unsafe.SizeOf<T>(); //fixed (void* p0 = &span

In IL code, why is there not a nop opcode in a given situation? Why is there a br.s opcode in a given situation?

早过忘川 提交于 2020-01-01 09:17:16
问题 Suppose I have the following code: public class Class1 { private Class2 obj; public void MethodA() { var class2 = new Class2(); class2.PropertyI = 2; obj = MethodB(class2); } public Class2 MethodB(Class2 class2) { return class2; } } public class Class2 { public int PropertyI { get; set; } } The generated IL code from compiling with Visual Studio 2010 as a .NET 2.0 assembly is the following: .method public hidebysig instance void MethodA() cil managed { .maxstack 3 .locals init ( [0] class

cecil: Instruction.Operand types corresponding to Instruction.OpCode.Code value

最后都变了- 提交于 2019-12-31 22:40:14
问题 Is there any documentation or is there a part of the cecil source code that I can consult to get a comprehensive view of which Operand types cecil will use for a given Code value? Eg: I can glean from MethodBodyRocks that Ldloc takes an Operand of type VariableDefinition , but I haven't been able to track down this down for some of the other instruction codes. 回答1: You can look at the definition of every OpCode in the OpCodes.cs file. E.g. for Ldloc you would see OperandType.InlineVar 回答2: To

Why are static classes considered “classes” and “reference types”?

不想你离开。 提交于 2019-12-31 08:57:30
问题 I’ve been pondering about the C# and CIL type system today and I’ve started to wonder why static classes are considered classes. There are many ways in which they are not really classes: A “normal” class can contain non-static members, a static class can’t. In this respect, a class is more similar to a struct than it is to a static class, and yet structs have a separate name. You can have a reference to an instance of a “normal” class, but not a static class (despite it being considered a

Why does C# -> CIL have a label on every instruction?

雨燕双飞 提交于 2019-12-31 02:44:10
问题 When using ILDASM.exe on a compiled C# program, it shows that there is a label for every instruction within methods. For example: IL_0001: ldc.i4.4 IL_0002: stloc.0 IL_0003: ldc.r8 12.34 IL_000c: stloc.1 IL_000d: ldc.r8 3.1415926535897931 IL_0016: stloc.2 IL_0017: ldstr "Ehsan" IL_001c: stloc.3 IL_001d: ret Why is this? Is it not inefficient to do this or does the CIL compiler optimize these labels itself? 回答1: Labels are not present in the compiled CIL. They are displayed for your

How is the iteration variable readonly?

北慕城南 提交于 2019-12-31 01:54:12
问题 In 8.8.4 of the C# specification, it provides this example: A foreach statement of the form foreach (V v in x) embedded-statement is then expanded to: { E e = ((C)(x)).GetEnumerator(); try { V v; while (e.MoveNext()) { v = (V)(T)e.Current; embedded-statement } } finally { … // Dispose e } } It also says: The iteration variable corresponds to a read-only local variable with a scope that extends over the embedded statement. The variable v is read-only in the embedded statement. How is the

Feeding an object literal to ILGenerator

空扰寡人 提交于 2019-12-30 09:39:12
问题 Food obj = ...; ILGenerator gen = (...).GetILGenerator(); gen.Emit( ?? obj ?? ); // replace this gen.Emit(OpCodes.Call, typeof(Person).GetMethod("Eat")); It's apparently not possible to cleanly push obj onto the evaluation stack, but I am open to ugly hacks which might compromise e.g. portability. ModuleBuilder.DefineInitializedData allows one to store a System.Byte[] in the .sdata. Any ideas? Edit: the generated method is being emitted as part of a new assembly. 回答1: object o = ...; Func

What is your recommendation for a good book on the .NET CLR and CIL? [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-30 04:44:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Do you know any good book about the workings of the CLR, the .NET Framework and CIL as opposed to any specific .NET language? 回答1: