Table of Contents

Class CilInstruction

Namespace
AsmResolver.PE.DotNet.Cil
Assembly
AsmResolver.PE.dll

Represents a single instruction in a managed CIL method body.

public class CilInstruction
Inheritance
CilInstruction
Inherited Members

Constructors

CilInstruction(CilOpCode)

Creates a new CIL instruction with no operand.

public CilInstruction(CilOpCode opCode)

Parameters

opCode CilOpCode

The operation to perform.

Remarks

This constructor does not do any verification on the correctness of the instruction.

CilInstruction(CilOpCode, object?)

Creates a new CIL instruction with an operand..

public CilInstruction(CilOpCode opCode, object? operand)

Parameters

opCode CilOpCode

The operation to perform.

operand object

The operand.

Remarks

This constructor does not do any verification on the correctness of the instruction.

CilInstruction(int, CilOpCode)

Creates a new CIL instruction with no operand.

public CilInstruction(int offset, CilOpCode opCode)

Parameters

offset int

The offset of the instruction, relative to the start of the method body's code.

opCode CilOpCode

The operation to perform.

Remarks

This constructor does not do any verification on the correctness of the instruction.

CilInstruction(int, CilOpCode, object?)

Creates a new CIL instruction with an operand..

public CilInstruction(int offset, CilOpCode opCode, object? operand)

Parameters

offset int

The offset of the instruction, relative to the start of the method body's code.

opCode CilOpCode

The operation to perform.

operand object

The operand.

Remarks

This constructor does not do any verification on the correctness of the instruction.

Properties

Offset

Gets or sets the offset to the start of the instruction, relative to the start of the code.

public int Offset { get; set; }

Property Value

int

OpCode

Gets or sets the operation to perform.

public CilOpCode OpCode { get; set; }

Property Value

CilOpCode

Operand

Gets or sets the operand of the instruction, if available.

public object? Operand { get; set; }

Property Value

object

Size

Gets the size in bytes of the CIL instruction.

public int Size { get; }

Property Value

int

Methods

CreateLabel()

Creates a new label to the current instruction.

public ICilLabel CreateLabel()

Returns

ICilLabel

The label.

CreateLdcI4(int)

Create a new instruction pushing the provided integer value, using the smallest possible operation code and operand size.

public static CilInstruction CreateLdcI4(int value)

Parameters

value int

The constant to push.

Returns

CilInstruction

The instruction.

Equals(CilInstruction)

Determines whether the provided instruction is considered equal to the current instruction.

protected bool Equals(CilInstruction other)

Parameters

other CilInstruction

The instruction to compare against.

Returns

bool

true if the instructions are equal, false otherwise.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

GetLdcI4Constant()

When this instruction is an ldc.i4 variant, gets the in32 constant that is being pushed onto the stack.

public int GetLdcI4Constant()

Returns

int

GetLdcI4OpCodeOperand(int)

Determines the smallest possible operation code and operand required to push the provided integer constant.

public static (CilOpCode code, object? operand) GetLdcI4OpCodeOperand(int value)

Parameters

value int

The constant to push.

Returns

(CilOpCode code, object operand)

The operation code and operand.

IsBranch()

Determines whether the instruction is a branching instruction (either conditional or unconditional).

public bool IsBranch()

Returns

bool

IsConditionalBranch()

Determines whether the instruction is a conditional branch instruction.

public bool IsConditionalBranch()

Returns

bool

IsLdarg()

Determines whether the instruction is using a variant of the ldarg opcodes.

public bool IsLdarg()

Returns

bool

IsLdcI4()

Determines whether the instruction is an instruction pushing an int32 constant onto the stack.

public bool IsLdcI4()

Returns

bool

IsLdloc()

Determines whether the instruction is using a variant of the ldloc opcodes.

public bool IsLdloc()

Returns

bool

IsStarg()

Determines whether the instruction is using a variant of the starg opcodes.

public bool IsStarg()

Returns

bool

IsStloc()

Determines whether the instruction is using a variant of the stloc opcodes.

public bool IsStloc()

Returns

bool

IsUnconditionalBranch()

Determines whether the instruction is an unconditional branch instruction.

public bool IsUnconditionalBranch()

Returns

bool

ReplaceWith(CilOpCode)

Replaces the operation code used by the instruction with a new one, and clears the operand.

public void ReplaceWith(CilOpCode opCode)

Parameters

opCode CilOpCode

The new operation code.

Remarks

This method may be useful when patching a method body, where reusing the instruction object is favourable. This can prevent breaking any references to the instruction (e.g. branch or exception handler targets).

ReplaceWith(CilOpCode, object?)

Replaces the operation code and operand used by the instruction with new ones.

public void ReplaceWith(CilOpCode opCode, object? operand)

Parameters

opCode CilOpCode

The new operation code.

operand object

The new operand.

Remarks

This method may be useful when patching a method body, where reusing the instruction object is favourable. This can prevent breaking any references to the instruction (e.g. branch or exception handler targets).

ReplaceWithNop()

Clears the operand and replaces the operation code with a Nop (No-Operation).

public void ReplaceWithNop()

Remarks

This method may be useful when patching a method body, where reusing the instruction object is favourable. This can prevent breaking any references to the instruction (e.g. branch or exception handler targets).

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.