Using Br_S OpCode to point to next instruction usi

2020-04-29 16:37发布

问题:

I am experimenting with parsing IL in order to emit a method. I have gotten the IL code of a method in a string[] where each string is an IL instruction. I am looping over this array and adding OpCodes using an ILGenerator:

        foreach (string ins in instructions) //string representations of IL          
        {
            string opCode = ins.Split(':').ElementAt(1);

            // other conditions omitted

            if (opCode.Contains("br.s"))
            {
                Label targetInstruction = ilGenerator.DefineLabel();

                ilGenerator.MarkLabel(targetInstruction);

                ilGenerator.Emit(OpCodes.Br_S, targetInstruction); 
            }

Here is the IL that I need to reproduce:

Source IL:
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret

And here is what I am getting as output:

Target IL:
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: stloc.0
IL_0007: br.s IL_0007   // this is wrong -- needs to point to IL_0009
IL_0009: ldloc.0
IL_000a: ret

As you can see the br.s call is pointing to itself which of course causes an infinite loop. How do I get it to point to the following instruction as in the source? This has to do with using Reflection.Emit.Label but I'm not sure how it works.

EDIT By the way the IL seen above is for this simple method,

    public string HelloWorld()
    {
            return "Hello, World!";
    }

回答1:

This code:

ilGenerator.MarkLabel(targetInstruction);
ilGenerator.Emit(OpCodes.Br_S, targetInstruction); 

clearly says "mark the label here" and then you add the instruction at the point where you marked the label.

If this is not what you want, why are you doing it?

MarkLabel marks the current position, which means the position of the next instruction you output, as the target of the label.

In this case, to get "what you want", simply reverse those two lines, output the branch instruction before marking the label.

I placed "what you want" in quotes since I don't understand the point of that branch instruction. The machine will happily "move" to the next instruction all by itself, there is no need to add "branch to the next instruction" instructions for this to happen.



回答2:

You need to place the ilGenerator.MarkLabel() call immediately before emitting the opcode you want to jump to. You are placing it before the branch, which means it will branch to itself, effectively creating an infinite loop. But as Lasse says, if you emit the IL correctly it will be a no-op.

Interestingly, the entire method could easily be:

ldstr "Hello, World!"
ret

Whatever compiler emitted the original code needs to have its author LARTed.



回答3:

Calling the MarkLabel() method on your ILGenerator can be used to mark a branch point, followed by Emit(OpCodes.Br_S, [label]) to branch to the point.

I assume whatever API you used to spy the IL instructions of the the Hello World method was done in Debug mode, as the added nop and branch instructions are added to help ensure the debugger covers every step.

In a DynamicMethod, there is no need to attach a debugger, and depending on platform, running it with the extra instructions in Release Mode could result in an InvalidProgramException.

The "Hello World" method requires only 2 instructions (And it's quite intuitive)

Ldstr "Hello, World!"
Ret