Example #1
0
        public virtual void Execute(MethodContext context, MethodState state, object localInx = null)
        {
            if (localInx == null || !((localInx is Int32) || (localInx is Byte)))
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Stloc, localInx);
            }

            Local local = context.Locals[(localInx is Int32) ? (Int32)localInx : (Int32)(Byte)localInx];

            var slot = new ESSlot()
            {
                TypeToken = local.Description.TypeToken,
                SType     = local.Description.SType
            };

            if (local.Description.TypeToken.IsPrimitive)
            {
                if (!IsPrimaryType(local.Description.TypeToken.PrimitiveType))
                {
                    slot.Val = GetUnaryOperation(local.Description.TypeToken.PrimitiveType, UnaryPrimitiveOpType.GetStackRep)(local.Val);
                }
                else
                {
                    slot.Val = local.Val;
                }
            }
            else
            {
                slot.Val = local.Val;
            }

            context.EvalStack.Push(slot);
        }
Example #2
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 1)
            {
                throw new InvalidStackSizeException("NotEngine: Evaluation stack should contain an element");
            }

            ESSlot slot = context.EvalStack.Pop();

            if (!slot.TypeToken.IsPrimitive)
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Not, slot);
            }

            var result = new ESSlot(ESSlotType.Val);

            if (!IsPrimaryType(slot.TypeToken.PrimitiveType))
            {
                PrimitiveType primitiveType = GetCommonPrimaryType(slot.TypeToken.PrimitiveType, slot.TypeToken.PrimitiveType);
                //object converted = GetConvertOperation(slot.TypeToken.PrimitiveType, primitiveType)(slot.Val);
                result.Val = GetUnaryOperation(primitiveType, UnaryPrimitiveOpType.Not)(slot.Val);
            }
            else
            {
                result.Val = GetUnaryOperation(slot.TypeToken.PrimitiveType, UnaryPrimitiveOpType.Not)(slot.Val);
            }

            result.TypeToken = slot.TypeToken;

            context.EvalStack.Push(result);
        }
        protected bool ConditionSatisfied(ESSlot slot1, ESSlot slot2, BinaryPrimitiveOpType binOperation)
        {
            Object        val1Converted  = null;
            Object        val2Converted  = null;
            PrimitiveType commonPrimType = default(PrimitiveType);

            if (slot1.TypeToken == slot2.TypeToken && IsPrimaryType(slot1.TypeToken.PrimitiveType))
            {
                val1Converted  = slot1.Val;
                val2Converted  = slot2.Val;
                commonPrimType = slot1.TypeToken.PrimitiveType;
            }
            else
            {
                //Get common primary type
                commonPrimType = GetCommonPrimaryType(slot1.TypeToken.PrimitiveType, slot2.TypeToken.PrimitiveType);
                //Convert slot vals
                val1Converted = slot1.Val;
                val2Converted = slot2.Val;
                //val1Converted = GetConvertOperation(slot1.TypeToken.PrimitiveType, commonPrimType)(slot1.Val);
                //val2Converted = GetConvertOperation(slot2.TypeToken.PrimitiveType, commonPrimType)(slot2.Val);
            }

            return((Boolean)GetBinaryOperation(commonPrimType, binOperation)(val1Converted, val2Converted));
        }
        protected ESSlot Compute(ESSlot slot1, ESSlot slot2, BinaryPrimitiveOpType binOperation)
        {
            ESSlot result = new ESSlot(ESSlotType.Val);

            Object        val1Converted  = null;
            Object        val2Converted  = null;
            ClassToken    resultToken    = null;
            PrimitiveType commonPrimType = default(PrimitiveType);

            if (slot1.TypeToken == slot2.TypeToken && IsPrimaryType(slot1.TypeToken.PrimitiveType))
            {
                val1Converted  = slot1.Val;
                val2Converted  = slot2.Val;
                resultToken    = slot1.TypeToken;
                commonPrimType = resultToken.PrimitiveType;
            }
            else
            {
                //Get common primary type
                commonPrimType = GetCommonPrimaryType(slot1.TypeToken.PrimitiveType, slot2.TypeToken.PrimitiveType);
                //Convert slot vals
                val1Converted = slot1.Val;
                val2Converted = slot2.Val;
                //val1Converted = GetConvertOperation(slot1.TypeToken.PrimitiveType, commonPrimType)(slot1.Val);
                //val2Converted = GetConvertOperation(slot2.TypeToken.PrimitiveType, commonPrimType)(slot2.Val);
                resultToken = GetTokenForPrimitive(commonPrimType);
            }

            result.Val       = GetBinaryOperation(commonPrimType, binOperation)(val1Converted, val2Converted);
            result.TypeToken = resultToken;
            return(result);
        }
Example #5
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            //Check ctor token validity
            var ctorTkn = (operand as MethodToken);

            if (ctorTkn == null)
            {
                throw new InvalidOperationException("Invalid constructor token");
            }

            //Check if object type is loaded into domain
            if (!context.TypeLoader.TypeIsLoaded(ctorTkn.Owner))
            {
                throw new InvalidOperationException("Type is not loaded into the domain");
            }
            TypeObject typeObj = context.TypesHeap.GetTypeObject(ctorTkn.Owner);

            //Allocate an object in heap
            var objRef = context.GCHeap.AllocObjRetAddr(typeObj);

            //Put 'this' to the evaluation stack
            ESSlot thisSlot = new ESSlot()
            {
                TypeToken = typeObj.Token,
                SType     = ESSlotType.HORef,
                Val       = objRef
            };

            context.EvalStack.Push(thisSlot);

            //Initiate ctor call
            state.CallMethod            = typeObj.VTable.GetMethod(ctorTkn);
            state.ExecutionInterruption = ExecutionInterruption.Call;
        }
        protected ESSlot Convert(Object initialValue, PrimitiveType initialType, PrimitiveType targetType)
        {
            ESSlot resultSlot = new ESSlot(ESSlotType.Val);
            //Finding common type
            PrimitiveType commonType = initialType;

            if (!IsPrimaryType(initialType))
            {
                commonType = PrimitiveType.Int32;
            }

            resultSlot.Val       = GetConvertOperation(commonType, targetType)(initialValue);
            resultSlot.TypeToken = GetTokenForPrimitive(targetType);
            return(resultSlot);
        }
Example #7
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 1)
            {
                throw new InvalidStackSizeException("Evaluation stack should contain an element");
            }

            ESSlot refSlot = context.EvalStack.Pop();

            if (refSlot.SType != ESSlotType.HORef || ((Int32)refSlot.Val) == GCHeapObj.NullObj.Addr)
            {
                throw new InvalidOperationException("Stack contains an invalid data");
            }

            //Get heap object by address
            GCHeapObj hObj = context.GCHeap.GetObj((Int32)refSlot.Val);

            var typeObject = context.TypesHeap.GetTypeObject(hObj.TypeToken);

            if (typeObject == null)
            {
                throw new InvalidOperationException("UnboxEngine: Type was not loaded");
            }
            else if (!typeObject.TypeDesc.IsValueType)
            {
                throw new InvalidOperationException("Reference type cannot be unboxed");
            }

            ESSlot valSlot = new ESSlot()
            {
                SType     = ESSlotType.Val,
                TypeToken = hObj.TypeToken
            };

            if (hObj.TypeToken.IsPrimitive)
            {
                valSlot.Val = GetUnaryOperation(hObj.TypeToken.PrimitiveType, UnaryPrimitiveOpType.GetStackRep)(hObj.Val);
            }
            else
            {
                valSlot.Val = hObj.Val;
            }

            context.EvalStack.Push(valSlot);
        }
Example #8
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 1)
            {
                throw new InvalidStackSizeException("Evaluation stack should contain at least one element");
            }

            ESSlot initSlot = context.EvalStack.Pop();

            if (!initSlot.TypeToken.IsPrimitive)
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Conv_R8, initSlot);
            }

            ESSlot resultSlot = Convert(initSlot.Val, initSlot.TypeToken.PrimitiveType, PrimitiveType.Double);

            context.EvalStack.Push(resultSlot);
        }
Example #9
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 2)
            {
                throw new InvalidStackSizeException("SubEngine: Evaluation stack should contain two elements to be subtracted");
            }

            ESSlot slot2 = context.EvalStack.Pop();
            ESSlot slot1 = context.EvalStack.Pop();

            if (!slot1.TypeToken.IsPrimitive || !slot2.TypeToken.IsPrimitive)
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Mul, slot1, slot2);
            }

            ESSlot result = Compute(slot1, slot2, BinaryPrimitiveOpType.Sub);

            context.EvalStack.Push(result);
        }
Example #10
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 1)
            {
                throw new InvalidStackSizeException("Evaluation stack should contain an element to be boxed");
            }

            ESSlot valueSlot = context.EvalStack.Pop();

            if (valueSlot.SType != ESSlotType.Val)
            {
                throw new InvalidOperationException("Stack contains an invalid data that cannot be boxed");
            }

            //Check if type is loaded
            if (!context.TypeLoader.TypeIsLoaded(valueSlot.TypeToken))
            {
                throw new InvalidOperationException("Type is not loaded into the domain");
            }

            if (valueSlot.TypeToken.IsPrimitive)
            {
                if (!IsPrimaryType(valueSlot.TypeToken.PrimitiveType))
                {
                    valueSlot.Val = GetUnaryOperation(valueSlot.TypeToken.PrimitiveType, UnaryPrimitiveOpType.GetStoreRep)(valueSlot.Val);
                }
            }

            TypeObject typeObj = context.TypesHeap.GetTypeObject(valueSlot.TypeToken);

            GCHeapObj hObj = context.GCHeap.AllocObj(typeObj);

            hObj.Val = valueSlot.Val;

            ESSlot refSlot = new ESSlot()
            {
                TypeToken = valueSlot.TypeToken,
                SType     = ESSlotType.HORef,
                Val       = hObj.Addr
            };

            context.EvalStack.Push(refSlot);
        }
Example #11
0
        public virtual void Execute(MethodContext context, MethodState state, object operand = null)
        {
            MtdArg arg  = context.Args[(Int32)operand];
            ESSlot slot = new ESSlot()
            {
                TypeToken = arg.Description.TypeToken,
                SType     = arg.Description.SType
            };

            if (arg.Description.TypeToken.IsPrimitive)
            {
                slot.Val = GetUnaryOperation(arg.Description.TypeToken.PrimitiveType, UnaryPrimitiveOpType.GetStackRep)(arg.Val);
            }
            else
            {
                slot.Val = arg.Val;
            }

            context.EvalStack.Push(slot);
        }
Example #12
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 2)
            {
                throw new InvalidStackSizeException("BgeEngine: Evaluation stack should contain two elements to be added");
            }

            ESSlot slot2 = context.EvalStack.Pop();
            ESSlot slot1 = context.EvalStack.Pop();

            if (!slot1.TypeToken.IsPrimitive || !slot2.TypeToken.IsPrimitive)
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Bge, slot1, slot2);
            }

            if (ConditionSatisfied(slot1, slot2, BinaryPrimitiveOpType.GreaterOrEqual))
            {
                state.JumpIndex = (Int32)operand;
            }
        }
Example #13
0
        public virtual void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 1)
            {
                throw new InvalidStackSizeException("Stack does not contain an element to be stored in locals");
            }

            ESSlot slot = context.EvalStack.Pop();

            if (operand == null || !((operand is Int32) || (operand is Byte)))
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Stloc, operand);
            }

            Local local = context.Locals[(operand is Int32) ? (Int32)operand : (Int32)(Byte)operand];


            if (local.Description.TypeToken.IsPrimitive)
            {
                Object val = slot.Val;
                if (slot.TypeToken.PrimitiveType != local.Description.TypeToken.PrimitiveType)
                {
                    val = GetConvertOperation(slot.TypeToken.PrimitiveType, local.Description.TypeToken.PrimitiveType)(val);
                }

                if (!IsPrimaryType(local.Description.TypeToken.PrimitiveType))
                {
                    val = GetUnaryOperation(local.Description.TypeToken.PrimitiveType, UnaryPrimitiveOpType.GetStoreRep)(val);
                }
                local.Val = val;
            }
            else
            {
                if (!context.TypeLoader.FirstIsOfTypeOrDerivedFromSecond(slot.TypeToken, local.Description.TypeToken))
                {
                    throw new InvalidLocalsValueException("StLocEngine: Stack element cannot be store in local due to type mismatch");
                }

                local.Val = slot.Val;
            }
        }
Example #14
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (context.EvalStack.Count < 2)
            {
                throw new InvalidStackSizeException("CgtEngine: Evaluation stack should contain two elements to be compared");
            }

            ESSlot slot2 = context.EvalStack.Pop();
            ESSlot slot1 = context.EvalStack.Pop();

            ESSlot result = new ESSlot(ESSlotType.Val, TempTypeLocator.Int32Desc.Metadata);

            if (!slot1.TypeToken.IsPrimitive || !slot2.TypeToken.IsPrimitive)
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Cgt, slot1, slot2);
            }

            result.Val = (Int32)(ConditionSatisfied(slot1, slot2, BinaryPrimitiveOpType.Greater) ? 1 : 0);

            context.EvalStack.Push(result);
        }
Example #15
0
        public virtual void Execute(MethodContext context, MethodState state, object operand = null)
        {
            if (operand == null)
            {
                throw new ArgumentNullException("Ldc_I4Engine: operand is null");
            }

            Int32 val;

            if (operand is Int32)
            {
                val = (Int32)operand;
            }
            else if (operand is UInt16)
            {
                val = (Int32)(UInt16)operand;
            }
            else if (operand is SByte)
            {
                val = (Int32)(SByte)operand;
            }
            else if (operand is Byte)
            {
                val = (Int32)(Byte)operand;
            }
            else
            {
                throw new OperandsNotSupportedByOperationException(ByteCode.Ldc_I4, operand);
            }

            var slot = new ESSlot()
            {
                TypeToken = TempTypeLocator.Int32Desc.Metadata,
                SType     = ESSlotType.Val,
                Val       = val
            };

            context.EvalStack.Push(slot);
        }
Example #16
0
        public void Execute(MethodContext context, MethodState state, object operand = null)
        {
            //Pop "this" from stack and check to null
            ESSlot objThisSlot = context.EvalStack.Peek();
            Int32  thisRefAddr = (Int32)objThisSlot.Val;

            if (thisRefAddr == GCHeapObj.NullObj.Addr)
            {
                throw new NullReferenceException("Object is null");
            }

            //Get right method
            var methodDef = (operand as MethodToken);

            if (methodDef == null)
            {
                throw new ArgumentException("Incorrect or null method definition");
            }

            GCHeapObj thisObject = context.GCHeap.GetObj((Int32)objThisSlot.Val);


            //if (typeObjHeap == GCHeapObj.NullObj)
            //    throw new NullReferenceException("Type object is null");

            TypeObject typeObj = context.TypesHeap.GetTypeObject(thisObject.TypeToken);

            MethodDesc method = typeObj.VTable.GetMethod(methodDef);

            if (method == null)
            {
                throw new InvalidOperationException("Method described by the provided metadata was not found in type's vtable");
            }

            state.CallMethod            = method;
            state.ExecutionInterruption = ExecutionInterruption.Call;
        }