InstructionEncoder Structure

Définition

Encode les instructions CIL (Common Intermediate Language).

public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
Héritage
InstructionEncoder

Exemples

Cet exemple montre comment émettre un corps de méthode à l’aide InstructionEncoderde :

// 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;
}

Remarques

La InstructionEncoder classe est utilisée pour émettre des instructions CIL qui composent un corps de méthode. Pour obtenir un exemple complet d’émission d’une méthode, consultez la documentation de classe MetadataBuilder .

Constructeurs

Nom Description
InstructionEncoder(BlobBuilder, ControlFlowBuilder)

Crée un encodeur soutenu par des générateurs de code et de flux de contrôle.

Propriétés

Nom Description
CodeBuilder

Générateur sous-jacent dans lequel les instructions encodées sont écrites.

ControlFlowBuilder

Générateur d’étiquettes de suivi, de branches et de gestionnaires d’exceptions.

Offset

Décalage de l’instruction encodée suivante.

Méthodes

Nom Description
Branch(ILOpCode, LabelHandle)

Encode une instruction de branche.

Call(EntityHandle)

Encode l’instruction call et son opérande.

Call(MemberReferenceHandle)

Encode l’instruction call et son opérande.

Call(MethodDefinitionHandle)

Encode l’instruction call et son opérande.

Call(MethodSpecificationHandle)

Encode l’instruction call et son opérande.

CallIndirect(StandaloneSignatureHandle)

Encode l’instruction calli et son opérande.

DefineLabel()

Définit une étiquette qui peut être utilisée ultérieurement pour marquer et faire référence à un emplacement dans le flux d’instructions.

LoadArgument(Int32)

Encode l’instruction de chargement des arguments.

LoadArgumentAddress(Int32)

Encode les instructions de chargement d’adresse d’argument.

LoadConstantI4(Int32)

Encode l’instruction Int32 de charge constante.

LoadConstantI8(Int64)

Encode l’instruction Int64 de charge constante.

LoadConstantR4(Single)

Encode l’instruction Single de charge constante.

LoadConstantR8(Double)

Encode l’instruction Double de charge constante.

LoadLocal(Int32)

Encode les instructions de chargement des variables locales.

LoadLocalAddress(Int32)

Encode l’instruction de chargement d’adresses de variable locale.

LoadString(UserStringHandle)

Encode l’instruction ldstr et son opérande.

MarkLabel(LabelHandle)

Associe l’étiquette spécifiée au décalage IL actuel.

OpCode(ILOpCode)

Encode le code op spécifié.

StoreArgument(Int32)

Encode les instructions de magasin d’arguments.

StoreLocal(Int32)

Encode les instructions de magasin de variables locales.

Switch(Int32)

Démarre l’encodage d’une instruction switch.

Token(EntityHandle)

Encode un jeton.

Token(Int32)

Encode un jeton.

S’applique à