Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 459
Keith Avatar asked Aug 27 '11 02:08

Keith


2 Answers

To add to poupou's answer, OpCodes.cs shows which OperandType gets assigned for each instruction Code. Using this OperandType you can consult CodeReader.ReadOperand to see how these OperandTypes are used to determine which concrete object type is constructed. Also note that CodeReader.ReadCode uses CodeReader.ResolveBranches to transform some operands from instruction offsets into Instruction objects before returning.

I created this table which was a lot more convenient then digging through the source every time (anything not covered in this table should have an InlineNone OperandType):

Instruction.OpCode.Code|Instruction.OpCode.OperandType|Instruction.Operand class
Ldarg_S                |ShortInlineArg                |ParameterDefinition
Ldarga_S               |ShortInlineArg                |ParameterDefinition
Starg_S                |ShortInlineArg                |ParameterDefinition
Ldloc_S                |ShortInlineVar                |VariableDefinition
Ldloca_S               |ShortInlineVar                |VariableDefinition
Stloc_S                |ShortInlineVar                |VariableDefinition
Ldc_I4_S               |ShortInlineI                  |sbyte <===== NOTE: special case
Ldc_I4                 |InlineI                       |int32
Ldc_I8                 |InlineI8                      |int64
Ldc_R4                 |ShortInlineR                  |single
Ldc_R8                 |InlineR                       |float (64 bit)
Jmp                    |InlineMethod                  |MethodReference
Call                   |InlineMethod                  |MethodReference
Calli                  |InlineSig                     |CallSite
Br_S                   |ShortInlineBrTarget           |Instruction
Brfalse_S              |ShortInlineBrTarget           |Instruction
Brtrue_S               |ShortInlineBrTarget           |Instruction
Beq_S                  |ShortInlineBrTarget           |Instruction
Bge_S                  |ShortInlineBrTarget           |Instruction
Bgt_S                  |ShortInlineBrTarget           |Instruction
Ble_S                  |ShortInlineBrTarget           |Instruction
Blt_S                  |ShortInlineBrTarget           |Instruction
Bne_Un_S               |ShortInlineBrTarget           |Instruction
Bge_Un_S               |ShortInlineBrTarget           |Instruction
Bgt_Un_S               |ShortInlineBrTarget           |Instruction
Ble_Un_S               |ShortInlineBrTarget           |Instruction
Blt_Un_S               |ShortInlineBrTarget           |Instruction
Br                     |InlineBrTarget                |Instruction
Brfalse                |InlineBrTarget                |Instruction
Brtrue                 |InlineBrTarget                |Instruction
Beq                    |InlineBrTarget                |Instruction
Bge                    |InlineBrTarget                |Instruction
Bgt                    |InlineBrTarget                |Instruction
Ble                    |InlineBrTarget                |Instruction
Blt                    |InlineBrTarget                |Instruction
Bne_Un                 |InlineBrTarget                |Instruction
Bge_Un                 |InlineBrTarget                |Instruction
Bgt_Un                 |InlineBrTarget                |Instruction
Ble_Un                 |InlineBrTarget                |Instruction
Blt_Un                 |InlineBrTarget                |Instruction
Switch                 |InlineSwitch                  |Instruction array
Callvirt               |InlineMethod                  |MethodReference
Cpobj                  |InlineType                    |TypeReference
Ldobj                  |InlineType                    |TypeReference
Ldstr                  |InlineString                  |string
Newobj                 |InlineMethod                  |MethodReference
Castclass              |InlineType                    |TypeReference
Isinst                 |InlineType                    |TypeReference
Unbox                  |InlineType                    |TypeReference
Ldfld                  |InlineField                   |FieldReference
Ldflda                 |InlineField                   |FieldReference
Stfld                  |InlineField                   |FieldReference
Ldsfld                 |InlineField                   |FieldReference
Ldsflda                |InlineField                   |FieldReference
Stsfld                 |InlineField                   |FieldReference
Stobj                  |InlineType                    |TypeReference
Box                    |InlineType                    |TypeReference
Newarr                 |InlineType                    |TypeReference
Ldelema                |InlineType                    |TypeReference
Ldelem_Any             |InlineType                    |TypeReference
Stelem_Any             |InlineType                    |TypeReference
Unbox_Any              |InlineType                    |TypeReference
Refanyval              |InlineType                    |TypeReference
Mkrefany               |InlineType                    |TypeReference
Ldtoken                |InlineTok                     |IMetadataTokenProvider
Leave                  |InlineBrTarget                |Instruction
Leave_S                |ShortInlineBrTarget           |Instruction
Ldftn                  |InlineMethod                  |MethodReference
Ldvirtftn              |InlineMethod                  |MethodReference
Ldarg                  |InlineArg                     |ParameterDefinition
Ldarga                 |InlineArg                     |ParameterDefinition
Starg                  |InlineArg                     |ParameterDefinition
Ldloc                  |InlineVar                     |VariableDefinition
Ldloca                 |InlineVar                     |VariableDefinition
Stloc                  |InlineVar                     |VariableDefinition
Unaligned              |ShortInlineI                  |byte
Initobj                |InlineType                    |TypeReference
Constrained            |InlineType                    |TypeReference
No                     |ShortInlineI                  |byte
Sizeof                 |InlineType                    |TypeReference
like image 196
Keith Avatar answered Sep 24 '22 16:09

Keith


You can look at the definition of every OpCode in the OpCodes.cs file.

E.g. for Ldloc you would see OperandType.InlineVar

like image 40
poupou Avatar answered Sep 20 '22 16:09

poupou