InstructionEncoder 结构

定义

编码公共中间语言 (CIL) 指令。

public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
继承
InstructionEncoder

示例

此示例演示如何使用 InstructionEncoder

// The following code emits a method body similar to this C# code:

/*public static double CalcRectangleArea(double length, double width)
{
    if (length < 0.0)
    {
        throw new ArgumentOutOfRangeException("length");
    }

    if (width < 0.0)
    {
        throw new ArgumentOutOfRangeException("width");
    }

    return length * width;
}*/

private static InstructionEncoder EmitMethodBody(MetadataBuilder metadata, AssemblyReferenceHandle corlibAssemblyRef)
{
    var codeBuilder = new BlobBuilder();
    var encoder = new InstructionEncoder(codeBuilder, new ControlFlowBuilder());

    // Get a reference to the System.ArgumentOutOfRangeException type
    TypeReferenceHandle typeRefHandle = metadata.AddTypeReference(
    corlibAssemblyRef,
    metadata.GetOrAddString("System"),
    metadata.GetOrAddString("ArgumentOutOfRangeException"));

    // Signature: .ctor(string)
    var ctorSignature = new BlobBuilder();

    new BlobEncoder(ctorSignature).
        MethodSignature(isInstanceMethod: true).
        Parameters(1, returnType => returnType.Void(), parameters => parameters.AddParameter().Type().String());

    BlobHandle ctorBlobIndex = metadata.GetOrAddBlob(ctorSignature);

    // Get a reference to the System.ArgumentOutOfRangeException constructor
    MemberReferenceHandle ctorMemberRef = metadata.AddMemberReference(
        typeRefHandle,
        metadata.GetOrAddString(".ctor"),
        ctorBlobIndex);

    LabelHandle label1 = encoder.DefineLabel();
    LabelHandle label2 = encoder.DefineLabel();

    // ldarg.0
    encoder.OpCode(ILOpCode.Ldarg_0);

    // ldc.r8 0
    encoder.LoadConstantR8(0);

    // bge.un.s LABEL1
    encoder.Branch(ILOpCode.Bge_un_s, label1);

    // ldstr "length"
    encoder.LoadString(metadata.GetOrAddUserString("length"));

    // newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string)
    encoder.OpCode(ILOpCode.Newobj);
    encoder.Token(ctorMemberRef);

    // throw
    encoder.OpCode(ILOpCode.Throw);

    // LABEL1: ldarg.1
    encoder.MarkLabel(label1);
    encoder.OpCode(ILOpCode.Ldarg_1);

    // ldc.r8 0
    encoder.LoadConstantR8(0);

    // bge.un.s LABEL2
    encoder.Branch(ILOpCode.Bge_un_s, label2);

    // ldstr "width"
    encoder.LoadString(metadata.GetOrAddUserString("width"));

    // newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string)
    encoder.OpCode(ILOpCode.Newobj);
    encoder.Token(ctorMemberRef);

    // throw
    encoder.OpCode(ILOpCode.Throw);

    // LABEL2: ldarg.0
    encoder.MarkLabel(label2);
    encoder.OpCode(ILOpCode.Ldarg_0);

    // ldarg.1
    encoder.OpCode(ILOpCode.Ldarg_1);

    // mul
    encoder.OpCode(ILOpCode.Mul);

    // ret
    encoder.OpCode(ILOpCode.Ret);

    return encoder;
}

注解

InstructionEncoder 类用于发出构成方法主体的 CIL 指令。 有关发出方法的完整示例,请参阅 MetadataBuilder 类文档。

构造函数

名称 说明
InstructionEncoder(BlobBuilder, ControlFlowBuilder)

创建由代码和控制流生成器支持的编码器。

属性

名称 说明
CodeBuilder

将编码指令写入到的基础生成器。

ControlFlowBuilder

生成器跟踪标签、分支和异常处理程序。

Offset

下一个编码指令的偏移量。

方法

名称 说明
Branch(ILOpCode, LabelHandle)

对分支指令进行编码。

Call(EntityHandle)

call 指令及其操作数进行编码。

Call(MemberReferenceHandle)

call 指令及其操作数进行编码。

Call(MethodDefinitionHandle)

call 指令及其操作数进行编码。

Call(MethodSpecificationHandle)

call 指令及其操作数进行编码。

CallIndirect(StandaloneSignatureHandle)

calli 指令及其操作数进行编码。

DefineLabel()

定义一个标签,该标签稍后可用于标记和引用指令流中的位置。

LoadArgument(Int32)

对参数加载指令进行编码。

LoadArgumentAddress(Int32)

对参数地址加载指令进行编码。

LoadConstantI4(Int32)

Int32 常量加载指令进行编码。

LoadConstantI8(Int64)

Int64 常量加载指令进行编码。

LoadConstantR4(Single)

Single 常量加载指令进行编码。

LoadConstantR8(Double)

Double 常量加载指令进行编码。

LoadLocal(Int32)

对本地变量加载指令进行编码。

LoadLocalAddress(Int32)

编码本地变量地址加载指令。

LoadString(UserStringHandle)

ldstr 指令及其操作数进行编码。

MarkLabel(LabelHandle)

将指定的标签与当前的 IL 偏移量相关联。

OpCode(ILOpCode)

对指定的操作代码进行编码。

StoreArgument(Int32)

编码参数存储指令。

StoreLocal(Int32)

对局部变量存储指令进行编码。

Switch(Int32)

开始对开关指令进行编码。

Token(EntityHandle)

对令牌进行编码。

Token(Int32)

对令牌进行编码。

适用于