Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the possible OpCodes for the last instruction of a method?

Tags:

c#

.net

cil

opcode

il

In .net what are the possible OpCodes that can exist as the last instruction of a method.

At the moment I know that it can be

  • Ret http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.ret.aspx
  • Throw http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.throw.aspx

But is it possible for it to be any other opcodes? And if so what code (prefer c#) would produce them?

By "Last" I mean "the final OpCode defined in the method body"

like image 321
Simon Avatar asked Jul 28 '12 04:07

Simon


1 Answers

If throw can be the last opcode in a method, chances are that jmp also qualifies.

Also, if we consider a recursive method whose exit condition is not located at the end, the last opcode might be a call or tail.call instead of a ret.

Update: Well, no, it won't. As Marc Gravell rightfully points out in his comment, the documentation for tail.call says:

The stack must be empty except for the arguments being transferred by the following call. The instruction following the call instruction must be a ret.

Update 2: Unconditional branch opcodes like br and br.s may also be the last instructions of a method, if its exit point occurs earlier (thanks again Marc).

like image 192
3 revs Avatar answered Oct 12 '22 22:10

3 revs