Ejemplo n.º 1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private static void EmitCallTarget(ILGenerator il, IOperand target)
        {
            target.EmitTarget(il);

            if (target.OperandType.IsValueType)
            {
                if (target is ICanEmitAddress)
                {
                    target.EmitAddress(il);
                }
                else
                {
                    target.EmitLoad(il);

                    var temp = il.DeclareLocal(target.OperandType);
                    il.Emit(OpCodes.Stloc, temp);
                    il.Emit(OpCodes.Ldloca, (short)temp.LocalIndex);
                }

                il.Emit(OpCodes.Constrained, target.OperandType);
            }
            else
            {
                target.EmitLoad(il);
            }
        }
Ejemplo n.º 2
0
            //-------------------------------------------------------------------------------------------------------------------------------------------------

            private void EmitIncrement(ILGenerator il, IOperand <T> operand)
            {
                var overloads = TypeOperators.GetOperators(operand.OperandType);

                operand.EmitLoad(il);

                if (m_Position == UnaryOperatorPosition.Postfix && m_ShouldLeaveValueOnStack)
                {
                    il.Emit(OpCodes.Dup);
                }

                var overloadedOperator = (m_Positive ? overloads.OpIncrement : overloads.OpDecrement);

                if (overloadedOperator != null)
                {
                    il.Emit(OpCodes.Call, overloadedOperator);
                }
                else
                {
                    Helpers.EmitConvertible(il, 1);
                    Helpers.EmitCast(il, typeof(int), operand.OperandType);

                    il.Emit(m_Positive ? OpCodes.Add : OpCodes.Sub);
                }

                if (m_Position == UnaryOperatorPosition.Prefix && m_ShouldLeaveValueOnStack)
                {
                    il.Emit(OpCodes.Dup);
                }
            }
Ejemplo n.º 3
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private void EmitFullStatement(ILGenerator il)
        {
            var afterIfBlock   = il.DefineLabel();
            var afterElseBlock = (m_ElseBlock.Count > 0 ? il.DefineLabel() : new Label());

            m_Condition.EmitTarget(il);
            m_Condition.EmitLoad(il);

            il.Emit(OpCodes.Brfalse, afterIfBlock);

            foreach (var statement in m_ThenBlock)
            {
                statement.Emit(il);
            }

            if (m_ElseBlock.Count > 0)
            {
                il.Emit(OpCodes.Br, afterElseBlock);
            }

            il.MarkLabel(afterIfBlock);

            if (m_ElseBlock.Count > 0)
            {
                foreach (var statement in m_ElseBlock)
                {
                    statement.Emit(il);
                }

                il.MarkLabel(afterElseBlock);
            }

            il.Emit(OpCodes.Nop);
        }
Ejemplo n.º 4
0
            public void Emit(ILGenerator il, IOperand <int> lengthOperand)
            {
                lengthOperand.EmitTarget(il);
                lengthOperand.EmitLoad(il);

                il.Emit(OpCodes.Newarr, TypeTemplate.Resolve <TElement>());
            }
Ejemplo n.º 5
0
            public void Emit(ILGenerator il, IOperand <T> left, IOperand <Type> right)
            {
                var typeConstant = (right as Constant <Type>);

                if (object.ReferenceEquals(typeConstant, null))
                {
                    throw new NotSupportedException("Cast type must be a constant type known in advance.");
                }

                var fromType = left.OperandType;
                var castType = TypeTemplate.Resolve(typeConstant.Value);

                left.EmitTarget(il);
                left.EmitLoad(il);

                if (fromType.IsValueType)
                {
                    il.Emit(OpCodes.Box, fromType);
                }
                else
                {
                    il.Emit(OpCodes.Isinst, castType);

                    if (castType.IsNullableValueType())
                    {
                        il.Emit(OpCodes.Unbox_Any, castType);
                    }
                }
            }
Ejemplo n.º 6
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        #region StatementBase Members

        public override void Emit(ILGenerator il)
        {
            m_LoopNextLabel = il.DefineLabel();
            m_LoopEndLabel  = il.DefineLabel();
            var conditionLabel = il.DefineLabel();

            foreach (var statement in m_PreconditionBlock)
            {
                statement.Emit(il);
            }

            il.MarkLabel(conditionLabel);

            m_Condition.EmitTarget(il);
            m_Condition.EmitLoad(il);

            il.Emit(OpCodes.Brfalse, m_LoopEndLabel);

            foreach (var statement in m_BodyBlock)
            {
                statement.Emit(il);
            }

            il.MarkLabel(m_LoopNextLabel);

            foreach (var statement in m_NextBlock)
            {
                statement.Emit(il);
            }

            il.Emit(OpCodes.Br, conditionLabel);

            il.MarkLabel(m_LoopEndLabel);
            il.Emit(OpCodes.Nop);
        }
Ejemplo n.º 7
0
            public void Emit(ILGenerator il, IOperand <T> operand)
            {
                operand.EmitTarget(il);
                operand.EmitLoad(il);

                il.Emit(OpCodes.Not);
            }
Ejemplo n.º 8
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        protected override void OnEmitTarget(ILGenerator il)
        {
            if (m_Target != null)
            {
                m_Target.EmitTarget(il);
                m_Target.EmitLoad(il);
            }
        }
Ejemplo n.º 9
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        protected override void OnEmitTarget(ILGenerator il)
        {
            m_Array.EmitTarget(il);
            m_Array.EmitLoad(il);

            m_Index.EmitTarget(il);
            m_Index.EmitLoad(il);
        }
Ejemplo n.º 10
0
            public void Emit(ILGenerator il, IOperand <bool> operand)
            {
                operand.EmitTarget(il);
                operand.EmitLoad(il);

                il.Emit(OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Ceq);
            }
Ejemplo n.º 11
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        protected override void OnEmitLoad(ILGenerator il)
        {
            m_Target.EmitTarget(il);
            m_Target.EmitLoad(il);

            il.Emit(OpCodes.Ldftn, m_Method);
            il.Emit(OpCodes.Newobj, m_Constructor);
        }
Ejemplo n.º 12
0
            public void Emit(ILGenerator il, IOperand <bool> left, IOperand <bool> right)
            {
                left.EmitTarget(il);
                left.EmitLoad(il);

                right.EmitTarget(il);
                right.EmitLoad(il);

                il.Emit(OpCodes.Xor);
            }
Ejemplo n.º 13
0
            public void Emit(ILGenerator il, IOperand <T> left, IOperand <Type> right)
            {
                var typeConstant = (right as Constant <Type>);

                if (object.ReferenceEquals(typeConstant, null))
                {
                    throw new NotSupportedException("Cast type must be a constant type known in advance.");
                }

                left.EmitTarget(il);
                left.EmitLoad(il);

                Helpers.EmitCast(il, left.OperandType, typeConstant.Value);
            }
Ejemplo n.º 14
0
            public void Emit(ILGenerator il, IOperand <T> operand)
            {
                var overloads = TypeOperators.GetOperators(operand.OperandType);

                if (overloads.OpUnaryPlus != null)
                {
                    Helpers.EmitCall(il, null, overloads.OpUnaryPlus, operand);
                }
                else
                {
                    operand.EmitTarget(il);
                    operand.EmitLoad(il);

                    // by default this operator does nothing to the operand
                }
            }
Ejemplo n.º 15
0
            public void Emit(ILGenerator il, IOperand <T> operand)
            {
                var overloads = TypeOperators.GetOperators(operand.OperandType);

                if (overloads.OpNegation != null)
                {
                    Helpers.EmitCall(il, null, overloads.OpNegation, operand);
                }
                else
                {
                    operand.EmitTarget(il);
                    operand.EmitLoad(il);

                    il.Emit(OpCodes.Neg);
                }
            }
Ejemplo n.º 16
0
            public void Emit(ILGenerator il, IOperand <T> left, IOperand <T> right)
            {
                var endLabel = il.DefineLabel();

                left.EmitTarget(il);
                left.EmitLoad(il);

                il.Emit(OpCodes.Dup);
                il.Emit(OpCodes.Brtrue_S, endLabel);

                il.Emit(OpCodes.Pop);

                right.EmitTarget(il);
                right.EmitLoad(il);

                il.MarkLabel(endLabel);
                il.Emit(OpCodes.Nop);
            }
Ejemplo n.º 17
0
            //-------------------------------------------------------------------------------------------------------------------------------------------------

            public virtual void Emit(ILGenerator il, IOperand <TLeft> left, IOperand <TRight> right)
            {
                var typeOverloads = TypeOperators.GetOperators(left.OperandType);
                var overload      = m_OverloadSelector(typeOverloads);

                if (overload != null)
                {
                    m_Overloaded = true;
                    Helpers.EmitCall(il, null, overload, left, right);
                }
                else
                {
                    left.EmitTarget(il);
                    left.EmitLoad(il);

                    right.EmitTarget(il);
                    right.EmitLoad(il);

                    il.Emit(m_Instruction);
                }
            }
Ejemplo n.º 18
0
            public void Emit(ILGenerator il, IOperand <bool> left, IOperand <bool> right)
            {
                var trueLabel = il.DefineLabel();
                var endLabel  = il.DefineLabel();

                left.EmitTarget(il);
                left.EmitLoad(il);

                il.Emit(OpCodes.Brtrue, trueLabel);

                right.EmitTarget(il);
                right.EmitLoad(il);

                il.Emit(OpCodes.Br, endLabel);

                il.MarkLabel(trueLabel);
                il.Emit(OpCodes.Ldc_I4_1);
                il.Emit(OpCodes.Br, endLabel);

                il.MarkLabel(endLabel);
                il.Emit(OpCodes.Nop);
            }
Ejemplo n.º 19
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        protected override void OnEmitLoad(ILGenerator il)
        {
            var falseLabel = il.DefineLabel();
            var endLabel   = il.DefineLabel();

            m_Condition.EmitTarget(il);
            m_Condition.EmitLoad(il);

            il.Emit(OpCodes.Brfalse, falseLabel);

            m_OnTrue.EmitTarget(il);
            m_OnTrue.EmitLoad(il);

            il.Emit(OpCodes.Br, endLabel);
            il.MarkLabel(falseLabel);

            m_OnFalse.EmitTarget(il);
            m_OnFalse.EmitLoad(il);

            il.MarkLabel(endLabel);
            il.Emit(OpCodes.Nop);
        }
Ejemplo n.º 20
0
            //-------------------------------------------------------------------------------------------------------------------------------------------------

            public void Emit(ILGenerator il, IOperand <T> left, IOperand <T> right)
            {
                var nonPostfix = (left as INonPostfixNotation);

                left.EmitTarget(il);

                if (nonPostfix != null)
                {
                    nonPostfix.RightSide = right;
                }
                else
                {
                    right.EmitTarget(il);
                    right.EmitLoad(il);
                }

                left.EmitStore(il);

                if (m_ForceLeaveFalueOnStack)
                {
                    left.EmitTarget(il);
                    left.EmitLoad(il);
                }
            }
Ejemplo n.º 21
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        #region StatementBase Members

        public override void Emit(ILGenerator il)
        {
            m_LoopStartLabel = il.DefineLabel();
            m_LoopEndLabel   = il.DefineLabel();

            var nextIterationLabel = il.DefineLabel();

            il.MarkLabel(nextIterationLabel);

            foreach (var statement in m_BodyBlock)
            {
                statement.Emit(il);
            }

            il.MarkLabel(m_LoopStartLabel);

            m_Condition.EmitTarget(il);
            m_Condition.EmitLoad(il);
            il.Emit(OpCodes.Brtrue, nextIterationLabel);

            il.MarkLabel(m_LoopEndLabel);

            il.Emit(OpCodes.Nop);
        }