Exemple #1
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);
                    }
                }
            }
        }
Exemple #2
0
        private static bool ImplicitConvertToSingle(TypeCode sourceTypeCode, FleeIlGenerator ilg)
        {
            bool implicitConvertToSingle;

            switch (sourceTypeCode)
            {
            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                EmitConvert(ilg, OpCodes.Conv_R4);
                break;

            case TypeCode.UInt32:
            case TypeCode.UInt64:
                EmitConvert(ilg, OpCodes.Conv_R_Un);
                EmitConvert(ilg, OpCodes.Conv_R4);
                break;

            case TypeCode.Single:
                break;

            default:
                implicitConvertToSingle = false;
                return(implicitConvertToSingle);
            }
            implicitConvertToSingle = true;
            return(implicitConvertToSingle);
        }
Exemple #3
0
 public static void SyncFleeILGeneratorLabels(FleeIlGenerator source, FleeIlGenerator target)
 {
     while (source.LabelCount != target.LabelCount)
     {
         target.DefineLabel();
     }
 }
Exemple #4
0
        private static bool ImplicitConvertToInt64(TypeCode sourceTypeCode, FleeIlGenerator ilg)
        {
            bool implicitConvertToInt64;

            switch (sourceTypeCode)
            {
            case TypeCode.Char:
            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
                EmitConvert(ilg, OpCodes.Conv_U8);
                break;

            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
                EmitConvert(ilg, OpCodes.Conv_I8);
                break;

            case TypeCode.Int64:
                break;

            default:
                implicitConvertToInt64 = false;
                return(implicitConvertToInt64);
            }
            implicitConvertToInt64 = true;
            return(implicitConvertToInt64);
        }
Exemple #5
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);
            }
        }
Exemple #6
0
        private void EmitVariableLoad(FleeIlGenerator ilg)
        {
            var mi = VariableCollection.GetVariableLoadMethod(this.myVariableType);

            ilg.Emit(OpCodes.Ldstr, this.myName);
            this.EmitMethodCall(mi, ilg);
        }
Exemple #7
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);
                }
            }
        }
Exemple #8
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);
        }
Exemple #9
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);
        }
Exemple #10
0
        public void AddBranch(FleeIlGenerator ilg, Label target)
        {
            var startLoc = new IlLocation(ilg.Length);
            var bi       = new BranchInfo(startLoc, target);

            this.myBranchInfos.Add(bi);
        }
Exemple #11
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);
            }
        }
Exemple #12
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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #13
0
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            var binaryResultType   = ImplicitConverter.GetBinaryResultType(this.myLeftChild.ResultType, this.myRightChild.ResultType);
            var overloadedOperator = this.GetOverloadedCompareOperator();
            var flag = this.AreBothChildrenOfType(typeof(string));

            if (flag)
            {
                this.myLeftChild.Emit(ilg, services);
                this.myRightChild.Emit(ilg, services);
                EmitStringEquality(ilg, this.myOperation, services);
            }
            else
            {
                var flag2 = overloadedOperator != null;
                if (flag2)
                {
                    this.EmitOverloadedOperatorCall(overloadedOperator, ilg, services);
                }
                else
                {
                    var flag3 = binaryResultType != null;
                    if (flag3)
                    {
                        EmitChildWithConvert(this.myLeftChild, binaryResultType, ilg, services);
                        EmitChildWithConvert(this.myRightChild, binaryResultType, ilg, services);
                        this.EmitCompareOperation(ilg, this.myOperation);
                    }
                    else
                    {
                        var flag4 = this.AreBothChildrenOfType(typeof(bool));
                        if (flag4)
                        {
                            this.EmitRegular(ilg, services);
                        }
                        else
                        {
                            var flag5 = this.AreBothChildrenReferenceTypes();
                            if (flag5)
                            {
                                this.EmitRegular(ilg, services);
                            }
                            else
                            {
                                var flag6 = this.myLeftChild.ResultType.IsEnum & this.myRightChild.ResultType.IsEnum;
                                if (flag6)
                                {
                                    this.EmitRegular(ilg, services);
                                }
                                else
                                {
                                    Debug.Fail("unknown operand types");
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #14
0
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            this.myCastExpression.Emit(ilg, services);
            var sourceType = this.myCastExpression.ResultType;
            var destType   = this.myDestType;

            this.EmitCast(ilg, sourceType, destType, services);
        }
Exemple #15
0
        private static void MarkBranchTarget(ShortCircuitInfo info, Label target, FleeIlGenerator ilg)
        {
            var isTemp = ilg.IsTemp;

            if (isTemp)
            {
                info.Branches.MarkLabel(ilg, target);
            }
        }
Exemple #16
0
        private static void EmitConvert(FleeIlGenerator ilg, OpCode convertOpcode)
        {
            var flag = ilg != null;

            if (flag)
            {
                ilg.Emit(convertOpcode);
            }
        }
Exemple #17
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);
        }
Exemple #18
0
        public bool IsLongBranch(FleeIlGenerator ilg, Label target)
        {
            var startLoc = new IlLocation(ilg.Length);
            var bi       = new BranchInfo(startLoc, target);
            var index    = this.myBranchInfos.IndexOf(bi);

            bi = this.myBranchInfos[index];
            return(bi.IsLongBranch);
        }
Exemple #19
0
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            var flag = this.myPrevious != null;

            if (flag)
            {
                this.myPrevious.Emit(ilg, services);
            }
        }
Exemple #20
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);
        }
Exemple #21
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);
        }
Exemple #22
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);
        }
Exemple #23
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);
     }
 }
Exemple #24
0
 private static void EmitLogicalShortCircuit(FleeIlGenerator ilg, ShortCircuitInfo info, IServiceProvider services)
 {
     while (info.Operators.Count != 0)
     {
         var op          = (AndOrElement)info.Operators.Pop();
         var leftOperand = (ExpressionElement)info.Operands.Pop();
         EmitOperand(leftOperand, info, ilg, services);
         var i = GetShortCircuitLabel(op, info, ilg);
         EmitBranch(op, ilg, i, info);
     }
 }
        public override void Emit(FleeIlGenerator ilg, IServiceProvider services)
        {
            base.Emit(ilg, services);
            this.myElement.Emit(ilg, services);
            var isValueType = this.myElement.ResultType.IsValueType;

            if (isValueType)
            {
                EmitValueTypeLoadAddress(ilg, this.ResultType);
            }
        }
Exemple #26
0
 protected static void EmitLoad(bool value, FleeIlGenerator ilg)
 {
     if (value)
     {
         ilg.Emit(OpCodes.Ldc_I4_1);
     }
     else
     {
         ilg.Emit(OpCodes.Ldc_I4_0);
     }
 }
Exemple #27
0
        public Label GetLabel(object key, FleeIlGenerator ilg)
        {
            Label lbl;
            var   flag = !this.myKeyLabelMap.TryGetValue(RuntimeHelpers.GetObjectValue(key), out lbl);

            if (flag)
            {
                lbl = ilg.DefineLabel();
                this.myKeyLabelMap.Add(RuntimeHelpers.GetObjectValue(key), lbl);
            }
            return(lbl);
        }
Exemple #28
0
        private void DoEmitLogical(FleeIlGenerator ilg, IServiceProvider services)
        {
            var info    = new ShortCircuitInfo();
            var ilgTemp = this.CreateTempFleeIlGenerator(ilg);

            Utility.SyncFleeILGeneratorLabels(ilg, ilgTemp);
            this.EmitLogical(ilgTemp, info, services);
            info.ClearTempState();
            info.Branches.ComputeBranches();
            Utility.SyncFleeILGeneratorLabels(ilgTemp, ilg);
            this.EmitLogical(ilg, info, services);
        }
Exemple #29
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);
        }
Exemple #30
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);
        }