Beispiel #1
0
        private static void EmitTerminals(ShortCircuitInfo info, FleeIlGenerator ilg, Label endLabel)
        {
            var flag = info.Branches.HasLabel(RuntimeHelpers.GetObjectValue(ourFalseTerminalKey));

            if (flag)
            {
                var falseLabel = info.Branches.FindLabel(RuntimeHelpers.GetObjectValue(ourFalseTerminalKey));
                ilg.MarkLabel(falseLabel);
                MarkBranchTarget(info, falseLabel, ilg);
                ilg.Emit(OpCodes.Ldc_I4_0);
                var flag2 = info.Branches.HasLabel(RuntimeHelpers.GetObjectValue(ourTrueTerminalKey));
                if (flag2)
                {
                    ilg.Emit(OpCodes.Br_S, endLabel);
                }
            }
            var flag3 = info.Branches.HasLabel(RuntimeHelpers.GetObjectValue(ourTrueTerminalKey));

            if (flag3)
            {
                var trueLabel = info.Branches.FindLabel(RuntimeHelpers.GetObjectValue(ourTrueTerminalKey));
                ilg.MarkLabel(trueLabel);
                MarkBranchTarget(info, trueLabel, ilg);
                ilg.Emit(OpCodes.Ldc_I4_1);
            }
        }
Beispiel #2
0
        private void EmitOptimizedPower(FleeIlGenerator ilg, bool emitOverflow, bool unsigned)
        {
            var right = (Int32LiteralElement)this.myRightChild;
            var flag  = right.Value == 0;

            if (flag)
            {
                ilg.Emit(OpCodes.Pop);
                LiteralElement.EmitLoad(1, ilg);
                ImplicitConverter.EmitImplicitNumericConvert(typeof(int), this.myLeftChild.ResultType, ilg);
            }
            else
            {
                var flag2 = right.Value == 1;
                if (!flag2)
                {
                    var num = right.Value - 1;
                    for (var i = 1; i <= num; i++)
                    {
                        ilg.Emit(OpCodes.Dup);
                    }
                    var num2 = right.Value - 1;
                    for (var j = 1; j <= num2; j++)
                    {
                        this.EmitMultiply(ilg, emitOverflow, unsigned);
                    }
                }
            }
        }
Beispiel #3
0
        public static void EmitLoadLocal(FleeIlGenerator ilg, int index)
        {
            Debug.Assert(index >= 0, "Invalid index");
            var flag = (index >= 0) & (index <= 3);

            if (flag)
            {
                switch (index)
                {
                case 0:
                    ilg.Emit(OpCodes.Ldloc_0);
                    break;

                case 1:
                    ilg.Emit(OpCodes.Ldloc_1);
                    break;

                case 2:
                    ilg.Emit(OpCodes.Ldloc_2);
                    break;

                case 3:
                    ilg.Emit(OpCodes.Ldloc_3);
                    break;
                }
            }
            else
            {
                Debug.Assert(index < 256, "local index too large");
                ilg.Emit(OpCodes.Ldloc_S, (byte)index);
            }
        }
Beispiel #4
0
        private void EmitCast(FleeIlGenerator ilg, Type sourceType, Type destType, IServiceProvider services)
        {
            var explicitOperator = this.GetExplictOverloadedOperator(sourceType, destType);
            var flag             = sourceType == destType;

            if (!flag)
            {
                var flag2 = explicitOperator != null;
                if (flag2)
                {
                    ilg.Emit(OpCodes.Call, explicitOperator);
                }
                else
                {
                    var flag3 = sourceType.IsEnum | destType.IsEnum;
                    if (flag3)
                    {
                        this.EmitEnumCast(ilg, sourceType, destType, services);
                    }
                    else
                    {
                        var flag4 = ImplicitConverter.EmitImplicitConvert(sourceType, destType, ilg);
                        if (!flag4)
                        {
                            var flag5 = IsCastableNumericType(sourceType) & IsCastableNumericType(destType);
                            if (flag5)
                            {
                                EmitExplicitNumericCast(ilg, sourceType, destType, services);
                            }
                            else
                            {
                                var isValueType = sourceType.IsValueType;
                                if (isValueType)
                                {
                                    Debug.Assert(!destType.IsValueType, "expecting reference type");
                                    ilg.Emit(OpCodes.Box, sourceType);
                                }
                                else
                                {
                                    var isValueType2 = destType.IsValueType;
                                    if (isValueType2)
                                    {
                                        ilg.Emit(OpCodes.Unbox_Any, destType);
                                    }
                                    else
                                    {
                                        var flag6 = !destType.IsAssignableFrom(sourceType);
                                        if (flag6)
                                        {
                                            ilg.Emit(OpCodes.Castclass, destType);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #5
0
 protected static void EmitLoad(bool value, FleeIlGenerator ilg)
 {
     if (value)
     {
         ilg.Emit(OpCodes.Ldc_I4_1);
     }
     else
     {
         ilg.Emit(OpCodes.Ldc_I4_0);
     }
 }
Beispiel #6
0
 private void EmitMultiply(FleeIlGenerator ilg, bool emitOverflow, bool unsigned)
 {
     if (emitOverflow)
     {
         ilg.Emit(unsigned ? OpCodes.Mul_Ovf_Un : OpCodes.Mul_Ovf);
     }
     else
     {
         ilg.Emit(OpCodes.Mul);
     }
 }
Beispiel #7
0
        public static void EmitLoadLocalAddress(FleeIlGenerator ilg, int index)
        {
            Debug.Assert(index >= 0, "Invalid index");
            var flag = index <= 255;

            if (flag)
            {
                ilg.Emit(OpCodes.Ldloca_S, (byte)index);
            }
            else
            {
                ilg.Emit(OpCodes.Ldloca, index);
            }
        }
Beispiel #8
0
 public static void EmitLoad(int value, FleeIlGenerator ilg)
 {
     if ((value >= -1) & (value <= 8))
     {
         EmitSuperShort(value, ilg);
     }
     else if ((value >= sbyte.MinValue) & (value <= sbyte.MaxValue))
     {
         ilg.Emit(OpCodes.Ldc_I4_S, Convert.ToSByte(value));
     }
     else
     {
         ilg.Emit(OpCodes.Ldc_I4, value);
     }
 }
Beispiel #9
0
        private static void EmitBranchToTrueTerminal(FleeIlGenerator ilg, Label trueTerminal, BranchManager bm)
        {
            var isTemp = ilg.IsTemp;

            if (isTemp)
            {
                bm.AddBranch(ilg, trueTerminal);
                ilg.Emit(OpCodes.Brtrue_S, trueTerminal);
            }
            else
            {
                var flag = !bm.IsLongBranch(ilg, trueTerminal);
                ilg.Emit(flag ? OpCodes.Brtrue_S : OpCodes.Brtrue, trueTerminal);
            }
        }
Beispiel #10
0
        private void EmitVariableLoad(FleeIlGenerator ilg)
        {
            var mi = VariableCollection.GetVariableLoadMethod(this.myVariableType);

            ilg.Emit(OpCodes.Ldstr, this.myName);
            this.EmitMethodCall(mi, ilg);
        }
Beispiel #11
0
        protected static void EmitValueTypeLoadAddress(FleeIlGenerator ilg, Type targetType)
        {
            var index = ilg.GetTempLocalIndex(targetType);

            Utility.EmitStoreLocal(ilg, index);
            ilg.Emit(OpCodes.Ldloca_S, (byte)index);
        }
Beispiel #12
0
        private void EmitShift(FleeIlGenerator ilg)
        {
            var op = new OpCode();

            switch (Type.GetTypeCode(this.myLeftChild.ResultType))
            {
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            {
                var flag = this.myOperation == ShiftOperation.LeftShift;
                op = flag ? OpCodes.Shl : OpCodes.Shr;
                break;
            }

            case TypeCode.UInt32:
            case TypeCode.UInt64:
            {
                var flag2 = this.myOperation == ShiftOperation.LeftShift;
                op = flag2 ? OpCodes.Shl : OpCodes.Shr_Un;
                break;
            }

            default:
                Debug.Assert(false, "unknown left shift operand");
                break;
            }
            ilg.Emit(op);
        }
Beispiel #13
0
        private static void EmitLdfld(FieldInfo fi, bool indirect, FleeIlGenerator ilg)
        {
            var isStatic = fi.IsStatic;

            if (isStatic)
            {
                ilg.Emit(indirect ? OpCodes.Ldsflda : OpCodes.Ldsfld, fi);
            }
            else if (indirect)
            {
                ilg.Emit(OpCodes.Ldflda, fi);
            }
            else
            {
                ilg.Emit(OpCodes.Ldfld, fi);
            }
        }
Beispiel #14
0
        protected void EmitLoadOwner(FleeIlGenerator ilg)
        {
            ilg.Emit(OpCodes.Ldarg_0);
            var ownerType = this.myOptions.OwnerType;
            var flag      = !ownerType.IsValueType;

            if (!flag)
            {
                ilg.Emit(OpCodes.Unbox, ownerType);
                ilg.Emit(OpCodes.Ldobj, ownerType);
                var requiresAddress = this.RequiresAddress;
                if (requiresAddress)
                {
                    EmitValueTypeLoadAddress(ilg, ownerType);
                }
            }
        }
Beispiel #15
0
        private static void EmitBranch(AndOrElement op, FleeIlGenerator ilg, Label target, ShortCircuitInfo info)
        {
            var isTemp = ilg.IsTemp;

            if (isTemp)
            {
                info.Branches.AddBranch(ilg, target);
                var shortBranch = GetBranchOpcode(op, false);
                ilg.Emit(shortBranch, target);
            }
            else
            {
                var longBranch = info.Branches.IsLongBranch(ilg, target);
                var brOpcode   = GetBranchOpcode(op, longBranch);
                ilg.Emit(brOpcode, target);
            }
        }
Beispiel #16
0
 protected static void EmitLoad(long value, FleeIlGenerator ilg)
 {
     if ((value >= int.MinValue) & (value <= int.MaxValue))
     {
         EmitLoad(Convert.ToInt32(value), ilg);
         ilg.Emit(OpCodes.Conv_I8);
     }
     else if ((value >= 0) & (value <= uint.MaxValue))
     {
         EmitLoad(Convert.ToInt32(value), ilg);
         ilg.Emit(OpCodes.Conv_U8);
     }
     else
     {
         ilg.Emit(OpCodes.Ldc_I8, value);
     }
 }
Beispiel #17
0
 private static void EmitBitwiseOperation(FleeIlGenerator ilg, AndOrOperation op)
 {
     if (op != AndOrOperation.And)
     {
         if (op != AndOrOperation.Or)
         {
             Debug.Fail("Unknown op type");
         }
         else
         {
             ilg.Emit(OpCodes.Or);
         }
     }
     else
     {
         ilg.Emit(OpCodes.And);
     }
 }
Beispiel #18
0
        private void EmitOnDemandFunction(ExpressionElement[] elements, FleeIlGenerator ilg, IServiceProvider services)
        {
            EmitLoadVariables(ilg);
            ilg.Emit(OpCodes.Ldstr, this.myName);
            EmitElementArrayLoad(elements, typeof(object), ilg, services);
            var mi = VariableCollection.GetFunctionInvokeMethod(this.myOnDemandFunctionReturnType);

            this.EmitMethodCall(mi, ilg);
        }
Beispiel #19
0
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            var resultType = this.ResultType;

            this.myChild.Emit(ilg, services);
            ImplicitConverter.EmitImplicitConvert(this.myChild.ResultType, resultType, ilg);
            var mi   = Utility.GetSimpleOverloadedOperator("UnaryNegation", resultType, resultType);
            var flag = mi == null;

            if (flag)
            {
                ilg.Emit(OpCodes.Neg);
            }
            else
            {
                ilg.Emit(OpCodes.Call, mi);
            }
        }
Beispiel #20
0
        private static void EmitConvert(FleeIlGenerator ilg, OpCode convertOpcode)
        {
            var flag = ilg != null;

            if (flag)
            {
                ilg.Emit(convertOpcode);
            }
        }
Beispiel #21
0
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            var resultType = this.ResultType;

            this.myLeftChild.Emit(ilg, services);
            ImplicitConverter.EmitImplicitConvert(this.myLeftChild.ResultType, resultType, ilg);
            this.myRightChild.Emit(ilg, services);
            ImplicitConverter.EmitImplicitConvert(this.myRightChild.ResultType, resultType, ilg);
            ilg.Emit(OpCodes.Xor);
        }
Beispiel #22
0
        private void EmitListIn(FleeIlGenerator ilg, IServiceProvider services, BranchManager bm)
        {
            var ce           = new CompareElement();
            var endLabel     = bm.FindLabel("endLabel");
            var trueTerminal = bm.FindLabel("trueTerminal");
            var lb           = ilg.DeclareLocal(this.myOperand.ResultType);
            var targetIndex  = lb.LocalIndex;

            this.myOperand.Emit(ilg, services);
            Utility.EmitStoreLocal(ilg, targetIndex);
            var targetShim = new LocalBasedElement(this.myOperand, targetIndex);

            this.myArguments.ForEach(argumentElement =>
            {
                ce.Initialize(targetShim, argumentElement, LogicalCompareOperation.Equal);
                ce.Emit(ilg, services);
                EmitBranchToTrueTerminal(ilg, trueTerminal, bm);
            });
            //try
            //{
            //    var enumerator = this.myArguments.GetEnumerator();
            //    while (enumerator.MoveNext())
            //    {
            //        var argumentElement = enumerator.Current;
            //        ce.Initialize(targetShim, argumentElement, LogicalCompareOperation.Equal);
            //        ce.Emit(ilg, services);
            //        EmitBranchToTrueTerminal(ilg, trueTerminal, bm);
            //    }
            //}
            //finally
            //{
            //    List<ExpressionElement>.Enumerator enumerator;
            //    ((IDisposable)enumerator).Dispose();
            //}

            ilg.Emit(OpCodes.Ldc_I4_0);
            ilg.Emit(OpCodes.Br_S, endLabel);
            bm.MarkLabel(ilg, trueTerminal);
            ilg.MarkLabel(trueTerminal);
            ilg.Emit(OpCodes.Ldc_I4_1);
            bm.MarkLabel(ilg, endLabel);
            ilg.MarkLabel(endLabel);
        }
Beispiel #23
0
        private void EmitCollectionIn(FleeIlGenerator ilg, IServiceProvider services)
        {
            var mi = this.GetCollectionContainsMethod();
            var p  = mi.GetParameters()[0];

            this.myTargetCollectionElement.Emit(ilg, services);
            this.myOperand.Emit(ilg, services);
            ImplicitConverter.EmitImplicitConvert(this.myOperand.ResultType, p.ParameterType, ilg);
            ilg.Emit(OpCodes.Callvirt, mi);
        }
Beispiel #24
0
        protected void EmitOverloadedOperatorCall(MethodInfo method, FleeIlGenerator ilg, IServiceProvider services)
        {
            var @params = method.GetParameters();
            var pLeft   = @params[0];
            var pRight  = @params[1];

            EmitChildWithConvert(this.myLeftChild, pLeft.ParameterType, ilg, services);
            EmitChildWithConvert(this.myRightChild, pRight.ParameterType, ilg, services);
            ilg.Emit(OpCodes.Call, method);
        }
Beispiel #25
0
        private static void EmitValueTypeMethodCall(MethodInfo mi, FleeIlGenerator ilg)
        {
            var isStatic = mi.IsStatic;

            if (isStatic)
            {
                ilg.Emit(OpCodes.Call, mi);
            }
            else
            {
                var flag = mi.DeclaringType != mi.ReflectedType;
                if (flag)
                {
                    var flag2 = IsGetTypeMethod(mi);
                    if (flag2)
                    {
                        ilg.Emit(OpCodes.Box, mi.ReflectedType);
                        ilg.Emit(OpCodes.Call, mi);
                    }
                    else
                    {
                        ilg.Emit(OpCodes.Constrained, mi.ReflectedType);
                        ilg.Emit(OpCodes.Callvirt, mi);
                    }
                }
                else
                {
                    ilg.Emit(OpCodes.Call, mi);
                }
            }
        }
Beispiel #26
0
        private static void EmitSuperShort(int value, FleeIlGenerator ilg)
        {
            var ldcOpcode = default(OpCode);

            switch (value)
            {
            case -1:
                ldcOpcode = OpCodes.Ldc_I4_M1;
                break;

            case 0:
                ldcOpcode = OpCodes.Ldc_I4_0;
                break;

            case 1:
                ldcOpcode = OpCodes.Ldc_I4_1;
                break;

            case 2:
                ldcOpcode = OpCodes.Ldc_I4_2;
                break;

            case 3:
                ldcOpcode = OpCodes.Ldc_I4_3;
                break;

            case 4:
                ldcOpcode = OpCodes.Ldc_I4_4;
                break;

            case 5:
                ldcOpcode = OpCodes.Ldc_I4_5;
                break;

            case 6:
                ldcOpcode = OpCodes.Ldc_I4_6;
                break;

            case 7:
                ldcOpcode = OpCodes.Ldc_I4_7;
                break;

            case 8:
                ldcOpcode = OpCodes.Ldc_I4_8;
                break;

            default:
                Debug.Assert(false, "value out of range");
                break;
            }
            ilg.Emit(ldcOpcode);
        }
Beispiel #27
0
        private void EmitVirtualPropertyLoad(FleeIlGenerator ilg)
        {
            var index = ilg.GetTempLocalIndex(this.myPrevious.ResultType);

            Utility.EmitStoreLocal(ilg, index);
            EmitLoadVariables(ilg);
            ilg.Emit(OpCodes.Ldstr, this.myName);
            Utility.EmitLoadLocal(ilg, index);
            ImplicitConverter.EmitImplicitConvert(this.myPrevious.ResultType, typeof(object), ilg);
            var mi = VariableCollection.GetVirtualPropertyLoadMethod(this.ResultType);

            this.EmitMethodCall(mi, ilg);
        }
Beispiel #28
0
        private static void EmitStringEquality(FleeIlGenerator ilg, LogicalCompareOperation op, IServiceProvider services)
        {
            var options = (ExpressionOptions)services.GetService(typeof(ExpressionOptions));
            var ic      = new Int32LiteralElement((int)options.StringComparison);

            ic.Emit(ilg, services);
            var mi = typeof(string).GetMethod("Equals", BindingFlags.Static | BindingFlags.Public, null, new[]
            {
                typeof(string),
                typeof(string),
                typeof(StringComparison)
            }, null);

            ilg.Emit(OpCodes.Call, mi);
            var flag = op == LogicalCompareOperation.NotEqual;

            if (flag)
            {
                ilg.Emit(OpCodes.Ldc_I4_0);
                ilg.Emit(OpCodes.Ceq);
            }
        }
Beispiel #29
0
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            this.myChild.Emit(ilg, services);
            ImplicitConverter.EmitImplicitConvert(this.myChild.ResultType, this.myResultType, ilg);
            var options = (ExpressionOptions)services.GetService(typeof(ExpressionOptions));
            var flag    = !options.IsGeneric;

            if (flag)
            {
                ImplicitConverter.EmitImplicitConvert(this.myResultType, typeof(object), ilg);
            }
            ilg.Emit(OpCodes.Ret);
        }
Beispiel #30
0
        private void EmitShiftCount(FleeIlGenerator ilg, IServiceProvider services)
        {
            this.myRightChild.Emit(ilg, services);
            var tc = Type.GetTypeCode(this.myLeftChild.ResultType);

            if (tc - TypeCode.SByte > 5)
            {
                if (tc - TypeCode.Int64 > 1)
                {
                    Debug.Assert(false, "unknown left shift operand");
                }
                else
                {
                    ilg.Emit(OpCodes.Ldc_I4_S, 63);
                }
            }
            else
            {
                ilg.Emit(OpCodes.Ldc_I4_S, 31);
            }
            ilg.Emit(OpCodes.And);
        }