Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are these opcodes for?

Using reflector I get the following output:

.method private hidebysig static class myModelTestarea.Foo Method() cil managed
{
  .maxstack 1
  .locals init ([0] class myModelTestarea.Foo CS$1$0000)
  L_0000: nop 
  L_0001: ldc.i4.0 
  L_0002: newarr object
  L_0007: call object myModelTestarea.Program::Resolve(object[])
  L_000c: castclass myModelTestarea.Foo
  L_0011: stloc.0 
  L_0012: br.s L_0014
  L_0014: ldloc.0 
  L_0015: ret 
}

for

private static Foo Method()
{
  return (Foo)Resolve();
}

private static object Resolve( params object[] args )
{
  return new Foo();
}

What do the lines 11-14 do? I call a function and get a result (line 7). I cast the result to the right returntype (line c) - why not return right now?

Somehow, the casted result is stored as a local variable - then there is an uncoditional jump to the next line, where the local variable is loaded again. Why?

In my opinion line 11-14 and the local variable can be omitted ... ?

like image 389
tanascius Avatar asked Jun 03 '09 12:06

tanascius


People also ask

What are opcodes used for?

An opcode identifies which basic computer operation in the instruction set is to be performed. It is used when writing machine code. It tells the computer to do something. Each machine language instruction typically has both an opcode and operands.

What is opcode and example?

Opcode definition A complete machine language instruction consists of an opcode and zero or more operands with which the specified operation is performed. Examples are “add memory location A to memory location B,” or “store the number five in memory location C.” “Add” and “Store” are the opcodes in these examples.

What are opcodes in assembly language?

The opcode is the instruction that is executed by the CPU and the operand is the data or memory location used to execute that instruction.

How many opcodes are there for each operation?

Usually an opcode will fit into a single memory access, and then the answer is 2^12. But a processor could implement a multi-cycle opcode decoding process to extend the number of possible opcodes beyond 2^12. The maximum number of instructions (containing opcodes) that the processor can directly address.


2 Answers

That looks like a DEBUG build, which leaves in extra IL to help the debugger. Try it again in RELEASE and it should look cleaner, with optimization etc.

.method private hidebysig static class program/Foo Method() cil managed
{
    .maxstack 8
    L_0000: ldc.i4.0 
    L_0001: newarr object
    L_0006: call object program::Resolve(object[])
    L_000b: castclass program/Foo
    L_0010: ret 
}
like image 114
Marc Gravell Avatar answered Sep 19 '22 23:09

Marc Gravell


Is this a debug build? It's possible that it's there for the sake of the debugger.

I've seen similar things in other places - it's almost always harmless though. Don't forget that most of the optimisation is done by the JIT, which can notice things like this easily enough. The only downside is that more IL hints to the JIT that the method shouldn't be inlined.

like image 42
Jon Skeet Avatar answered Sep 17 '22 23:09

Jon Skeet