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)

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

適用対象