本文整理汇总了C#中System.Reflection.Emit.ILGenerator.DefineLabel方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.DefineLabel方法的具体用法?C# ILGenerator.DefineLabel怎么用?C# ILGenerator.DefineLabel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.ILGenerator
的用法示例。
在下文中一共展示了ILGenerator.DefineLabel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Emit
public override void Emit(ILGenerator gen, bool labelSetAlready)
{
if (!labelSetAlready) MarkLabel(gen);
Label condition = gen.DefineLabel();
Label start = gen.DefineLabel();
// Initialize
init.Emit(gen, true);
// Branch to condition label
gen.Emit(OpCodes.Br, condition);
// Mark start of loop
gen.MarkLabel(start);
// Execute loop
stmt.Emit(gen);
// Update counter
update.Emit(gen);
gen.MarkLabel(condition);
comparison.Emit(gen);
gen.Emit(OpCodes.Brtrue, start);
}
示例2: Generate
public override void Generate(ILGenerator generator, Symbols s)
{
var Is_False = generator.DefineLabel();
var End = generator.DefineLabel();
LeftOperand.Generate(generator, s);
//Pregunto si hay un 0 en el tope d la pila
generator.Emit(OpCodes.Ldc_I4_0);
//Si es verdad salto para la etiqueta es falso
generator.Emit(OpCodes.Beq, Is_False);
RightOperand.Generate(generator, s);
//Pregunto si hay un 0 en el tope d la pila
generator.Emit(OpCodes.Ldc_I4_0);
//Si es verdad salto para la etiqueta es falso
generator.Emit(OpCodes.Beq, Is_False);
//Si hay un 1 en el tope es porque no salte para el label que me dice si hay algun false ,
//por lo q el primer operando es true y entonces es true el and
generator.Emit(OpCodes.Ldc_I4_1);
generator.Emit(OpCodes.Br, End);
generator.MarkLabel(Is_False);
generator.Emit(OpCodes.Ldc_I4_0);
generator.MarkLabel(End);
}
示例3: BuildUp
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="typeToBuild"></param>
/// <param name="existing"></param>
/// <param name="idToBuild"></param>
/// <param name="il"></param>
protected override void BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild,
ILGenerator il)
{
IConstructorChooserPolicy ctorChooser = GetConstructorChooser(context, typeToBuild, idToBuild);
ConstructorInfo ctor = ctorChooser.ChooseConstructor(typeToBuild);
Label existingObjectNotNull = il.DefineLabel();
Label done = il.DefineLabel();
// If existing object (arg 2) is null, call the constructor
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldnull);
il.Emit(OpCodes.Ceq);
il.Emit(OpCodes.Brfalse, existingObjectNotNull);
// resolve all constructor parameters...
if (ctor != null)
{
foreach (ParameterInfo parameter in ctor.GetParameters())
{
EmitResolveParameter(il, parameter);
FixupParameterType(il, parameter.ParameterType);
}
}
// invoke constructor, leaving the new object on the top of the stack...
EmitCallConstructor(il, ctor, typeToBuild);
// And skip around the else clause
il.Emit(OpCodes.Br_S, done);
// We have an existing object, just get it on top of the stack
il.MarkLabel(existingObjectNotNull);
il.Emit(OpCodes.Ldarg_2);
il.MarkLabel(done);
}
示例4: GenerateCode
public override void GenerateCode(ILGenerator codeGenerator, TypeBuilder typeBuilder, ModuleBuilder moduleBuilder)
{
Label forStart = codeGenerator.DefineLabel();
Label forEnd = codeGenerator.DefineLabel();
EndLabel = forEnd;
InitExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
UpperBoundExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
codeGenerator.MarkLabel(forStart);
codeGenerator.Emit(OpCodes.Dup);
codeGenerator.Emit(OpCodes.Ldsfld, InitExp.ForVariableBuilder);
codeGenerator.Emit(OpCodes.Blt, forEnd);
BodyExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
codeGenerator.Emit(OpCodes.Ldsfld, InitExp.ForVariableBuilder);
codeGenerator.Emit(OpCodes.Ldc_I4_1);
codeGenerator.Emit(OpCodes.Add);
codeGenerator.Emit(OpCodes.Stsfld, InitExp.ForVariableBuilder);
codeGenerator.Emit(OpCodes.Br, forStart);
codeGenerator.MarkLabel(forEnd);
codeGenerator.Emit(OpCodes.Pop);
InitExp.RestoreVariable(codeGenerator);
}
示例5: GenerateCode
public override void GenerateCode(ILGenerator codeGenerator, TypeBuilder typeBuilder, ModuleBuilder moduleBuilder)
{
LeftOperand.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
RightOperand.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
Label endLabel = codeGenerator.DefineLabel();
Label trueLabel = codeGenerator.DefineLabel();
if (LeftOperand.ReturnType == StringType.StringInstance)
{
codeGenerator.Emit(OpCodes.Call, typeof(String).GetMethod("CompareTo", new Type[] { typeof(string) }));
codeGenerator.Emit(OpCodes.Ldc_I4_0);
codeGenerator.Emit(OpCodes.Bne_Un, trueLabel);
}
else
codeGenerator.Emit(OpCodes.Bne_Un, trueLabel);
codeGenerator.Emit(OpCodes.Ldc_I4_0);
codeGenerator.Emit(OpCodes.Br, endLabel);
codeGenerator.MarkLabel(trueLabel);
codeGenerator.Emit(OpCodes.Ldc_I4_1);
codeGenerator.MarkLabel(endLabel);
}
示例6: GenerateWriterMethod
public void GenerateWriterMethod(Type obtype, CodeGenContext ctx, ILGenerator il)
{
var getTypeIDMethodInfo = typeof(Serializer).GetMethod("GetTypeID", BindingFlags.NonPublic | BindingFlags.Instance, null,
new Type[] { typeof(object) }, null);
var map = ctx.TypeMap;
// arg0: Serializer, arg1: Stream, arg2: object
var idLocal = il.DeclareLocal(typeof(ushort));
// get TypeID from object's Type
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Call, getTypeIDMethodInfo);
il.Emit(OpCodes.Stloc_S, idLocal);
// write typeID
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldloc_S, idLocal);
il.Emit(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(ushort)));
// +1 for 0 (null)
var jumpTable = new Label[map.Count + 1];
jumpTable[0] = il.DefineLabel();
foreach (var kvp in map)
jumpTable[kvp.Value.TypeID] = il.DefineLabel();
il.Emit(OpCodes.Ldloc_S, idLocal);
il.Emit(OpCodes.Switch, jumpTable);
il.Emit(OpCodes.Newobj, Helpers.ExceptionCtorInfo);
il.Emit(OpCodes.Throw);
/* null case */
il.MarkLabel(jumpTable[0]);
il.Emit(OpCodes.Ret);
/* cases for types */
foreach (var kvp in map)
{
var type = kvp.Key;
var data = kvp.Value;
il.MarkLabel(jumpTable[data.TypeID]);
if (data.NeedsInstanceParameter)
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);
il.Emit(OpCodes.Tailcall);
il.Emit(OpCodes.Call, data.WriterMethodInfo);
il.Emit(OpCodes.Ret);
}
}
示例7: MineLabels
void MineLabels(byte[] Bytes, ILGenerator Gen, Dictionary<int, Label[]> LabelOrigins, Dictionary<int, Label> LabelTargets)
{
for(int i = 0; i < Bytes.Length; i++)
{
OpCode Code = GetOpcode(Bytes, ref i);
int Start = i;
// Reference: http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.switch(VS.80).aspx
if(Code == OpCodes.Switch)
{
int Count = (int) BitHelper.ReadUnsignedInteger(Bytes, ref i);
// Jumps are relative to the first byte of the instruction following the switchmap
int Zero = i+Count*4+1;
List<Label> Labels = new List<Label>();
for(int j = 0; j < Count; j++)
{
int Absolute = Zero+BitHelper.ReadInteger(Bytes, ref i);
if(!LabelTargets.ContainsKey(Absolute))
{
Label At = Gen.DefineLabel();
LabelTargets.Add(Absolute, At);
Labels.Add(At);
}
// If there is a label already defined for this position, reuse that for this switchmap too
else Labels.Add(LabelTargets[Absolute]);
}
LabelOrigins.Add(Start, Labels.ToArray());
}
else if(Code.OperandType == OperandType.InlineBrTarget ||
Code.OperandType == OperandType.ShortInlineBrTarget)
{
int Zero, Target;
if(Code.OperandType == OperandType.InlineBrTarget)
{
Zero = i+5;
Target = Zero+BitHelper.ReadInteger(Bytes, ref i);
}
else
{
Zero = i+2;
Target = Zero+((sbyte)Bytes[++i]);
}
if(!LabelTargets.ContainsKey(Target))
{
Label At = Gen.DefineLabel();
LabelTargets.Add(Target, At);
LabelOrigins.Add(Start, new Label[] { At });
}
else LabelOrigins.Add(Start, new Label[] { LabelTargets[Target] });
}
else i += CodeArgumentSize(Code);
}
}
示例8: TranslateToIL
internal override void TranslateToIL(ILGenerator il, Type rtype){
Label else_label = il.DefineLabel();
Label endif_label = il.DefineLabel();
this.condition.TranslateToConditionalBranch(il, false, else_label, false);
this.operand1.TranslateToIL(il, rtype);
il.Emit(OpCodes.Br, endif_label);
il.MarkLabel(else_label);
this.operand2.TranslateToIL(il, rtype);
il.MarkLabel(endif_label);
}
示例9: EmitCalculateSize
public void EmitCalculateSize(ILGenerator il, Action<ILGenerator> emitLoad)
{
var valueIsNullOrEmptyLabel = il.DefineLabel();
var valueHasElementsLabel = il.DefineLabel();
var loopStartLabel = il.DefineLabel();
var loopConditionLabel = il.DefineLabel();
var endOfMethodLabel = il.DefineLabel();
var iVar = il.DeclareLocal(typeof(int)); // int i
var sumVar = il.DeclareLocal(typeof(int)); // int sum
var elemVar = il.DeclareLocal(type); // T elem
emitLoad(il); // if (!value)
il.Emit(OpCodes.Brfalse, valueIsNullOrEmptyLabel);// goto valueIsNullOrEmptyLabel
emitLoad(il); // if ((int)value.Length)
il.Emit(OpCodes.Ldlen); // goto valueHasElementsLabel
il.Emit(OpCodes.Conv_I4);
il.Emit(OpCodes.Brtrue, valueHasElementsLabel);
il.MarkLabel(valueIsNullOrEmptyLabel); // label valueIsNullOrEmptyLabel
il.Emit_Ldc_I4(sizeof(int)); // stack_0 = sizeof(int)
il.Emit(OpCodes.Br, endOfMethodLabel); // goto endOfMethodLabel
il.MarkLabel(valueHasElementsLabel); // label valueHasElementsLabel
il.Emit_Ldc_I4(0); // i = 0
il.Emit(OpCodes.Stloc, iVar);
il.Emit_Ldc_I4(sizeof(int)); // sum = sizeof(int)
il.Emit(OpCodes.Stloc, sumVar);
il.Emit(OpCodes.Br, loopConditionLabel); // goto loopConditionLabel
il.MarkLabel(loopStartLabel); // label loopStartLabel
il.Emit(OpCodes.Ldloc, sumVar); // stack_0 = sum
emitLoad(il); // elem = value[i]
il.Emit(OpCodes.Ldloc, iVar);
EmitLdelem(il);
il.Emit(OpCodes.Stloc, elemVar);
elementCodec.EmitCalculateSize(il, elemVar); // stack_1 = CalculateSize(elem)
il.Emit(OpCodes.Add); // sum = stack_0 + stack_1
il.Emit(OpCodes.Stloc, sumVar);
il.Emit(OpCodes.Ldloc, iVar); // i++
il.Emit_Ldc_I4(1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, iVar);
il.MarkLabel(loopConditionLabel); // label loopConditionLabel
il.Emit(OpCodes.Ldloc, iVar); // if (i < (int)value.Length)
emitLoad(il); // goto loopStartLabel
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Conv_I4);
il.Emit(OpCodes.Blt, loopStartLabel);
il.Emit(OpCodes.Ldloc, sumVar); // stack_0 = sum
il.MarkLabel(endOfMethodLabel); // label endOfMethodLabel
}
示例10: GenerateIL
public override void GenerateIL(ILGenerator il, SymbolTable st)
{
Label beginLoop = il.DefineLabel();
Label endLoop = il.DefineLabel();
il.MarkLabel(beginLoop);
var newTable = new SymbolTable(st, endLoop);
Block.GenerateIL(il, newTable);
il.Emit(OpCodes.Br, beginLoop);
il.MarkLabel(endLoop);
}
示例11: GenerateCode
public override void GenerateCode(ILGenerator codeGenerator, TypeBuilder typeBuilder, ModuleBuilder moduleBuilder)
{
Label thenLabel = codeGenerator.DefineLabel();
Label endLabel = codeGenerator.DefineLabel();
ConditionExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
codeGenerator.Emit(OpCodes.Brfalse, thenLabel);
ThenExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
codeGenerator.Emit(OpCodes.Br, endLabel);
codeGenerator.MarkLabel(thenLabel);
ElseExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
codeGenerator.MarkLabel(endLabel);
}
示例12: Generate
public override void Generate (ILGenerator gen)
{
Label falseLabel = gen.DefineLabel ();
Label endLabel = gen.DefineLabel ();
GenerateCondition (gen, falseLabel);
trueBlock.Generate (gen);
gen.Emit (OpCodes.Br, endLabel);
gen.MarkLabel(falseLabel);
falseBlock.Generate (gen);
gen.MarkLabel(endLabel);
}
示例13: WriteDeserializerArray
static void WriteDeserializerArray(TypeBuilder typeBuilder, ILGenerator il, Type type, int tag, MethodInfo setMethod,
int? itemLocalIndex = null)
{
var itemType = type.GetElementType();
var lengthLocal = il.DeclareLocal(typeof(int));
var arrayLocal = il.DeclareLocal(type);
var itemLocal = il.DeclareLocal(itemType);
var indexLocal = il.DeclareLocal(typeof(int));
var startLabel = il.DefineLabel();
var endLabel = il.DefineLabel();
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldc_I4, tag);
il.Emit(OpCodes.Call, GetCollectionLengthMethod);
il.Emit(OpCodes.Stloc, lengthLocal);
il.Emit(OpCodes.Ldloc, lengthLocal.LocalIndex);
il.Emit(OpCodes.Newarr, itemType);
il.Emit(OpCodes.Stloc, arrayLocal.LocalIndex);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Stloc, indexLocal.LocalIndex);
il.Emit(OpCodes.Br, startLabel);
il.MarkLabel(endLabel);
WriteDeserializerReadValue(typeBuilder, il, itemType, 1, itemLocal.LocalIndex);
il.Emit(OpCodes.Ldloc, arrayLocal.LocalIndex);
il.Emit(OpCodes.Ldloc, indexLocal.LocalIndex);
il.Emit(OpCodes.Ldloc, itemLocal.LocalIndex);
il.Emit(OpCodes.Stelem, itemType);
il.Emit(OpCodes.Ldloc, indexLocal.LocalIndex);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, indexLocal.LocalIndex);
il.MarkLabel(startLabel);
il.Emit(OpCodes.Ldloc, indexLocal.LocalIndex);
il.Emit(OpCodes.Ldloc, lengthLocal.LocalIndex);
il.Emit(OpCodes.Blt, endLabel);
if (itemLocalIndex == null) {
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldloc, arrayLocal.LocalIndex);
il.Emit(OpCodes.Callvirt, setMethod);
} else {
il.Emit(OpCodes.Ldloc, arrayLocal.LocalIndex);
il.Emit(OpCodes.Stloc, itemLocalIndex.Value);
}
}
示例14: GenerateSerializerSwitch
public static void GenerateSerializerSwitch(CodeGenContext ctx, ILGenerator il, IDictionary<Type, TypeData> map)
{
// arg0: Stream, arg1: object
var idLocal = il.DeclareLocal(typeof(ushort));
// get TypeID from object's Type
var getTypeIDMethod = typeof(Serializer).GetMethod("GetTypeID", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(object) }, null);
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Call, getTypeIDMethod, null);
il.Emit(OpCodes.Stloc_S, idLocal);
// write typeID
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc_S, idLocal);
il.EmitCall(OpCodes.Call, ctx.GetWriterMethodInfo(typeof(ushort)), null);
// +1 for 0 (null)
var jumpTable = new Label[map.Count + 1];
jumpTable[0] = il.DefineLabel();
foreach (var kvp in map)
jumpTable[kvp.Value.TypeID] = il.DefineLabel();
il.Emit(OpCodes.Ldloc_S, idLocal);
il.Emit(OpCodes.Switch, jumpTable);
ConstructorInfo exceptionCtor = typeof(Exception).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);
il.Emit(OpCodes.Newobj, exceptionCtor);
il.Emit(OpCodes.Throw);
/* null case */
il.MarkLabel(jumpTable[0]);
il.Emit(OpCodes.Ret);
/* cases for types */
foreach (var kvp in map)
{
var type = kvp.Key;
var data = kvp.Value;
il.MarkLabel(jumpTable[data.TypeID]);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(type.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, type);
il.EmitCall(OpCodes.Call, data.WriterMethodInfo, null);
il.Emit(OpCodes.Ret);
}
}
示例15: GenerateObjectToString
/// <summary>
/// Generates IL that pops the element on the top of the stack and
/// leaves a string in its place.
/// </summary>
/// <param name="generator"><see cref="ILGenerator"/> to write to.</param>
/// <param name="nullText">Text to leave if the element is null.</param>
public static void GenerateObjectToString(ILGenerator generator, string nullText)
{
// string theString = (element == null) ? "null" : element.ToString();
Label nullLabel = generator.DefineLabel();
Label afterTheStringLabel = generator.DefineLabel();
generator.Emit(OpCodes.Dup);
generator.Emit(OpCodes.Brfalse, nullLabel);
generator.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
generator.Emit(OpCodes.Br, afterTheStringLabel);
generator.MarkLabel(nullLabel);
generator.Emit(OpCodes.Pop);
generator.Emit(OpCodes.Ldstr, nullText);
generator.MarkLabel(afterTheStringLabel);
}