InstructionEncoder Struktura

Definice

Kóduje pokyny CIL (Common Intermediate Language).

public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
Dědičnost
InstructionEncoder

Příklady

Tento příklad ukazuje, jak generovat tělo metody pomocí 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;
}

Poznámky

Třída InstructionEncoder se používá ke generování instrukcí CIL, které tvoří tělo metody. Úplný příklad generování metody najdete v dokumentaci ke MetadataBuilder třídě.

Konstruktory

InstructionEncoder(BlobBuilder, ControlFlowBuilder)

Vytvoří kodér založený na tvůrcích kódu a toku řízení.

Vlastnosti

CodeBuilder

Základní tvůrce, do kterého se zapisují kódované instrukce.

ControlFlowBuilder

Popisky sledování, větve a obslužné rutiny výjimek v Tvůrci

Offset

Posun další kódované instrukce.

Metody

Branch(ILOpCode, LabelHandle)

Zakóduje instrukci větve.

Call(EntityHandle)

Kóduje call instrukce a její operand.

Call(MemberReferenceHandle)

Kóduje call instrukce a její operand.

Call(MethodDefinitionHandle)

Kóduje call instrukce a její operand.

Call(MethodSpecificationHandle)

Kóduje call instrukce a její operand.

CallIndirect(StandaloneSignatureHandle)

Kóduje calli instrukce a její operand.

DefineLabel()

Definuje popisek, který lze později použít k označení a odkazování na umístění v instrukčním streamu.

LoadArgument(Int32)

Kóduje instrukce načtení argumentu.

LoadArgumentAddress(Int32)

Kóduje instrukce načtení adresy argumentu.

LoadConstantI4(Int32)

Kóduje Int32 instrukce konstantního načítání.

LoadConstantI8(Int64)

Kóduje Int64 instrukce konstantního načítání.

LoadConstantR4(Single)

Kóduje Single instrukce konstantního načítání.

LoadConstantR8(Double)

Kóduje Double instrukce konstantního načítání.

LoadLocal(Int32)

Kóduje instrukce načtení místních proměnných.

LoadLocalAddress(Int32)

Kóduje instrukce načtení adresy místní proměnné.

LoadString(UserStringHandle)

Kóduje ldstr instrukce a její operand.

MarkLabel(LabelHandle)

Přidruží zadaný popisek k aktuálnímu posunu IL.

OpCode(ILOpCode)

Kóduje zadaný operační kód.

StoreArgument(Int32)

Kóduje instrukce úložiště argumentů.

StoreLocal(Int32)

Kóduje instrukce úložiště místních proměnných.

Switch(Int32)

Spustí kódování instrukce přepínače.

Token(EntityHandle)

Kóduje token.

Token(Int32)

Kóduje token.

Platí pro