Beispiel #1
0
 public IILGen Mark(IILLabel label)
 {
     _sourceCodeWriter.Indent--;
     _sourceCodeWriter.WriteLine(((ILLabelImpl)label).Name + ":");
     _sourceCodeWriter.Indent++;
     _ilGenerator.MarkLabel(((ILLabelImpl)label).Label);
     return(this);
 }
Beispiel #2
0
 public static IILGen BneUnS(this IILGen il, IILLabel targetLabel)
 {
     il.Emit(OpCodes.Bne_Un_S, targetLabel);
     return(il);
 }
Beispiel #3
0
 public static IILGen BrtrueS(this IILGen il, IILLabel targetLabel)
 {
     il.Emit(OpCodes.Brtrue_S, targetLabel);
     return(il);
 }
Beispiel #4
0
 public IILGen Mark(IILLabel label)
 {
     _instructions.Add(new MarkInst(label));
     return this;
 }
Beispiel #5
0
 public void ReplayTo(IILGen target)
 {
     Label = target.DefineLabel(_name);
 }
Beispiel #6
0
        public static void GenerateJumpIfEqual(IILGen ilGenerator, Type type, IILLabel jumpTo, Action <IILGen> loadLeft, Action <IILGen> loadRight)
        {
            if (type == typeof(sbyte) || type == typeof(byte) || type == typeof(short) || type == typeof(ushort) ||
                type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) ||
                type == typeof(float) || type == typeof(double) || type == typeof(bool) || type.IsEnum)
            {
                ilGenerator
                .Do(loadLeft)
                .Do(loadRight)
                .BeqS(jumpTo);
                return;
            }
            if (type.IsGenericType)
            {
                var genType = type.GetGenericTypeDefinition();
                if (genType == typeof(Nullable <>))
                {
                    var localLeft         = ilGenerator.DeclareLocal(type, "left");
                    var localRight        = ilGenerator.DeclareLocal(type, "right");
                    var hasValueMethod    = type.GetMethod("get_HasValue");
                    var getValueMethod    = type.GetMethod("GetValueOrDefault", Type.EmptyTypes);
                    var labelLeftHasValue = ilGenerator.DefineLabel("leftHasValue");
                    var labelDifferent    = ilGenerator.DefineLabel("different");
                    ilGenerator
                    .Do(loadLeft)
                    .Stloc(localLeft)
                    .Do(loadRight)
                    .Stloc(localRight)
                    .Ldloca(localLeft)
                    .Call(hasValueMethod)
                    .BrtrueS(labelLeftHasValue)
                    .Ldloca(localRight)
                    .Call(hasValueMethod)
                    .BrtrueS(labelDifferent)
                    .BrS(jumpTo)
                    .Mark(labelLeftHasValue)
                    .Ldloca(localRight)
                    .Call(hasValueMethod)
                    .BrfalseS(labelDifferent);
                    GenerateJumpIfEqual(
                        ilGenerator,
                        type.GetGenericArguments()[0],
                        jumpTo,
                        g => g.Ldloca(localLeft).Call(getValueMethod),
                        g => g.Ldloca(localRight).Call(getValueMethod));
                    ilGenerator.Mark(labelDifferent);
                    return;
                }
            }
            var equalsMethod = type.GetMethod("Equals", new[] { type, type })
                               ?? type.GetMethod("op_Equality", new[] { type, type });

            if (equalsMethod != null)
            {
                loadLeft(ilGenerator);
                loadRight(ilGenerator);
                ilGenerator
                .Call(equalsMethod)
                .BrtrueS(jumpTo);
                return;
            }
            throw new ArgumentOutOfRangeException(nameof(type), $"Don't know how to compare type {type}");
        }
Beispiel #7
0
 public IILGen Mark(IILLabel label)
 {
     _ilGenerator.MarkLabel(((ILLabelImpl)label).Label);
     return(this);
 }
Beispiel #8
0
 public MarkInst(IILLabel label)
 {
     _label = label;
 }
Beispiel #9
0
 public void ReplayTo(IILGen target)
 {
     Label = target.DefineLabel(_name);
 }
Beispiel #10
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _sourceCodeWriter.MarkAndWriteLine(_ilGenerator, $"{opCode} {((ILLabelImpl) ilLabel).Name}");
     _ilGenerator.Emit(opCode, ((ILLabelImpl)ilLabel).Label);
 }
Beispiel #11
0
 public IILGen Mark(IILLabel label)
 {
     _instructions.Add(new MarkInst(label));
     return(this);
 }
Beispiel #12
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _ilGenerator.Emit(opCode, ((ILLabelImpl)ilLabel).Label);
 }
Beispiel #13
0
 public IILGen Mark(IILLabel label)
 {
     _ilGenerator.MarkLabel(((ILLabelImpl)label).Label);
     return this;
 }
Beispiel #14
0
 public void FreeTemps()
 {
     Label = null;
 }
Beispiel #15
0
 public static IILGen Beq(this IILGen il, IILLabel targetLabel)
 {
     il.Emit(OpCodes.Beq, targetLabel);
     return(il);
 }
Beispiel #16
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _instructions.Add(new EmitILLabel(opCode, ilLabel));
 }
Beispiel #17
0
 public static IILGen BgeUn(this IILGen il, IILLabel targetLabel)
 {
     il.Emit(OpCodes.Bge_Un, targetLabel);
     return(il);
 }
Beispiel #18
0
 public EmitILLabel(OpCode opCode, IILLabel ilLabel)
 {
     _opCode  = opCode;
     _ilLabel = ilLabel;
 }
Beispiel #19
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _ilGenerator.Emit(opCode, ((ILLabelImpl)ilLabel).Label);
 }
Beispiel #20
0
 public void FreeTemps()
 {
     Label = null;
 }
Beispiel #21
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _instructions.Add(new EmitILLabel(opCode, ilLabel));
 }
Beispiel #22
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _sourceCodeWriter.MarkAndWriteLine(_ilGenerator, string.Format("{0} {1}", opCode, ((ILLabelImpl)ilLabel).Name));
     _ilGenerator.Emit(opCode, ((ILLabelImpl)ilLabel).Label);
 }
Beispiel #23
0
 public EmitILLabel(OpCode opCode, IILLabel ilLabel)
 {
     _opCode = opCode;
     _ilLabel = ilLabel;
 }
Beispiel #24
0
 public IILGen Mark(IILLabel label)
 {
     _sourceCodeWriter.Indent--;
     _sourceCodeWriter.WriteLine(((ILLabelImpl)label).Name + ":");
     _sourceCodeWriter.Indent++;
     _ilGenerator.MarkLabel(((ILLabelImpl)label).Label);
     return this;
 }
Beispiel #25
0
 public MarkInst(IILLabel label)
 {
     _label = label;
 }
Beispiel #26
0
 public void Emit(OpCode opCode, IILLabel ilLabel)
 {
     _sourceCodeWriter.MarkAndWriteLine(_ilGenerator, $"{opCode} {((ILLabelImpl) ilLabel).Name}");
     _ilGenerator.Emit(opCode, ((ILLabelImpl)ilLabel).Label);
 }