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)

トークンをエンコードします。

適用対象