InstructionEncoder Struct
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Encodes Common Intermediate Language (CIL) instructions.
public value class InstructionEncoder
public readonly struct InstructionEncoder
public struct InstructionEncoder
type InstructionEncoder = struct
Public Structure InstructionEncoder
- Inheritance
This example shows how to emit a method body using 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;
}
The InstructionEncoder class is used to emit CIL instructions that make up a method body. For a complete example of emitting a method, see the MetadataBuilder class documentation.
Instruction |
Creates an encoder backed by code and control-flow builders. |
Code |
Underlying builder where encoded instructions are written to. |
Control |
Builder tracking labels, branches and exception handlers. |
Offset |
Offset of the next encoded instruction. |
Branch(ILOp |
Encodes a branch instruction. |
Call(Entity |
Encodes |
Call(Member |
Encodes |
Call(Method |
Encodes |
Call(Method |
Encodes |
Call |
Encodes |
Define |
Defines a label that can later be used to mark and refer to a location in the instruction stream. |
Load |
Encodes argument load instruction. |
Load |
Encodes argument address load instruction. |
Load |
Encodes Int32 constant load instruction. |
Load |
Encodes Int64 constant load instruction. |
Load |
Encodes Single constant load instruction. |
Load |
Encodes Double constant load instruction. |
Load |
Encodes local variable load instruction. |
Load |
Encodes local variable address load instruction. |
Load |
Encodes |
Mark |
Associates specified label with the current IL offset. |
Op |
Encodes specified op-code. |
Store |
Encodes argument store instruction. |
Store |
Encodes local variable store instruction. |
Switch(Int32) |
Starts encoding a switch instruction. |
Token(Entity |
Encodes a token. |
Token(Int32) |
Encodes a token. |
Product | Versions |
---|---|
.NET | Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 (package-provided), 8, 9 (package-provided), 9 |
.NET Framework | 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided) |
.NET Standard | 2.0 (package-provided) |
.NET feedback
.NET is an open source project. Select a link to provide feedback: