InstructionEncoder Struct

Definizione

Codifica le istruzioni common intermediate language (CIL).

public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
Ereditarietà
InstructionEncoder

Esempio

In questo esempio viene illustrato come generare un corpo del metodo usando 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;
}

Commenti

La InstructionEncoder classe viene usata per generare istruzioni CIL che costituiscono un corpo del metodo. Per un esempio completo di emissione di un metodo, vedere la documentazione della MetadataBuilder classe.

Costruttori

InstructionEncoder(BlobBuilder, ControlFlowBuilder)

Crea un codificatore supportato da generatori di codice e flusso di controllo.

Proprietà

CodeBuilder

Generatore sottostante in cui vengono scritte le istruzioni codificate.

ControlFlowBuilder

Generatore di etichette di rilevamento, rami e gestori di eccezioni.

Offset

Offset dell'istruzione codificata successiva.

Metodi

Branch(ILOpCode, LabelHandle)

Codifica un'istruzione branch.

Call(EntityHandle)

call Codifica l'istruzione e il relativo operando.

Call(MemberReferenceHandle)

call Codifica l'istruzione e il relativo operando.

Call(MethodDefinitionHandle)

call Codifica l'istruzione e il relativo operando.

Call(MethodSpecificationHandle)

call Codifica l'istruzione e il relativo operando.

CallIndirect(StandaloneSignatureHandle)

calli Codifica l'istruzione e il relativo operando.

DefineLabel()

Definisce un'etichetta che può essere usata in un secondo momento per contrassegnare e fare riferimento a una posizione nel flusso di istruzioni.

LoadArgument(Int32)

Codifica l'istruzione di caricamento degli argomenti.

LoadArgumentAddress(Int32)

Codifica l'istruzione di caricamento degli indirizzi degli argomenti.

LoadConstantI4(Int32)

Codifica l'istruzione di caricamento della costante Int32.

LoadConstantI8(Int64)

Codifica l'istruzione di caricamento della costante Int64.

LoadConstantR4(Single)

Codifica l'istruzione di caricamento della costante Single.

LoadConstantR8(Double)

Codifica l'istruzione di caricamento della costante Double.

LoadLocal(Int32)

Codifica l'istruzione di caricamento della variabile locale.

LoadLocalAddress(Int32)

Codifica l'istruzione di caricamento dell'indirizzo della variabile locale.

LoadString(UserStringHandle)

ldstr Codifica l'istruzione e il relativo operando.

MarkLabel(LabelHandle)

Associa l'etichetta specificata all'offset IL corrente.

OpCode(ILOpCode)

Codifica il codice op-code specificato.

StoreArgument(Int32)

Codifica l'istruzione di archiviazione dell'argomento.

StoreLocal(Int32)

Codifica l'istruzione di archiviazione della variabile locale.

Switch(Int32)

Avvia la codifica di un'istruzione switch.

Token(EntityHandle)

Codifica un token.

Token(Int32)

Codifica un token.

Si applica a