Esempio n. 1
0
        public void TestBuildManualCyclicPointer01()
        {
            int allocationSize = 1;

            // Allocate a pointer
            int ptr = core.Rmem.Heap.Allocate(allocationSize);

            // Pointer data that points to its own heap element
            int pointerData = ptr;

            // Build a pointer whose data points to its own heap element
            StackValue sv = StackValue.BuildPointer(pointerData);

            core.Rmem.Heap.IncRefCount(sv);
            core.Rmem.Heap.Heaplist[ptr].Stack[0] = sv;

            // Verify the heap contains a cycle
            Assert.IsTrue(ProtoCore.Utils.HeapUtils.IsHeapCyclic(core));
        }
Esempio n. 2
0
 public void PushStackFrame(int ptr, int classIndex, int funcIndex, int pc, int functionBlockDecl, int functionBlockCaller, StackFrameType callerType, StackFrameType type, int depth, int fp, List <StackValue> registers, int locsize, int executionStates)
 {
     // TODO Jun: Performance
     // Push frame should only require adjusting the frame index instead of pushing dummy elements
     PushFrame(locsize);
     Push(StackValue.BuildInt(fp));
     PushRegisters(registers);
     Push(StackValue.BuildInt(executionStates));
     Push(StackValue.BuildInt(0));
     Push(StackValue.BuildInt(depth));
     Push(StackValue.BuildFrameType((int)type));
     Push(StackValue.BuildFrameType((int)callerType));
     Push(StackValue.BuildBlockIndex(functionBlockCaller));
     Push(StackValue.BuildBlockIndex(functionBlockDecl));
     Push(StackValue.BuildInt(pc));
     Push(StackValue.BuildInt(funcIndex));
     Push(StackValue.BuildInt(classIndex));
     Push(StackValue.BuildPointer(ptr));
     FramePointer = Stack.Count;
 }
Esempio n. 3
0
        public void TestBuildManualCyclicPointer02()
        {
            int allocationSize = 1;

            // Allocate 2 pointers
            int ptr1 = core.Rmem.Heap.Allocate(allocationSize);
            int ptr2 = core.Rmem.Heap.Allocate(allocationSize);

            // Build a pointer whose data points to its own heap element
            StackValue svPtr1 = StackValue.BuildPointer(ptr2);

            core.Rmem.Heap.IncRefCount(svPtr1);
            core.Rmem.Heap.Heaplist[ptr1].Stack[0] = svPtr1;


            // Build a 2nd pointer that points to the first pointer
            StackValue svPtr2 = StackValue.BuildPointer(ptr1);

            core.Rmem.Heap.IncRefCount(svPtr2);
            core.Rmem.Heap.Heaplist[ptr2].Stack[0] = svPtr2;

            // Verify the heap contains a cycle
            Assert.IsTrue(ProtoCore.Utils.HeapUtils.IsHeapCyclic(core));
        }
        public StackValue Evaluate(List <StackValue> args, StackFrame stackFrame)
        {
            // Build the stackframe
            var runtimeCore = interpreter.runtime.RuntimeCore;

            int        classScopeCaller = stackFrame.ClassScope;
            int        returnAddr       = stackFrame.ReturnPC;
            int        blockDecl        = procNode.RuntimeIndex;
            int        blockCaller      = stackFrame.FunctionCallerBlock;
            int        framePointer     = runtimeCore.RuntimeMemory.FramePointer;
            StackValue thisPtr          = StackValue.BuildPointer(Constants.kInvalidIndex);

            // Functoion has variable input parameter. This case only happen
            // for FFI functions whose last parameter's type is (params T[]).
            // In this case, we need to convert argument list from
            //
            //    {a1, a2, ..., am, v1, v2, ..., vn}
            //
            // to
            //
            //    {a1, a2, ..., am, {v1, v2, ..., vn}}
            if (procNode.IsVarArg)
            {
                int paramCount = procNode.ArgumentInfos.Count;
                Validity.Assert(paramCount >= 1);

                int varParamCount = args.Count - (paramCount - 1);
                var varParams     = args.GetRange(paramCount - 1, varParamCount).ToArray();
                args.RemoveRange(paramCount - 1, varParamCount);

                try
                {
                    var packedParams = interpreter.runtime.rmem.Heap.AllocateArray(varParams);
                    args.Add(packedParams);
                }
                catch (RunOutOfMemoryException)
                {
                    interpreter.runtime.RuntimeCore.RuntimeStatus.LogWarning(WarningID.RunOutOfMemory, Resources.RunOutOfMemory);
                    return(StackValue.Null);
                }
            }

            bool isCallingMemberFunciton = procNode.ClassID != Constants.kInvalidIndex &&
                                           !procNode.IsConstructor &&
                                           !procNode.IsStatic;

            bool isValidThisPointer = true;

            if (isCallingMemberFunciton)
            {
                Validity.Assert(args.Count >= 1);
                thisPtr = args[0];
                if (thisPtr.IsArray)
                {
                    isValidThisPointer = ArrayUtils.GetFirstNonArrayStackValue(thisPtr, ref thisPtr, runtimeCore);
                }
                else
                {
                    args.RemoveAt(0);
                }
            }

            if (!isValidThisPointer || (!thisPtr.IsPointer && !thisPtr.IsArray))
            {
                runtimeCore.RuntimeStatus.LogWarning(WarningID.DereferencingNonPointer,
                                                     Resources.kDeferencingNonPointer);
                return(StackValue.Null);
            }

            var callerType = stackFrame.StackFrameType;

            interpreter.runtime.TX = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.BounceType.Implicit);

            StackValue svBlockDecl = StackValue.BuildBlockIndex(blockDecl);
            // interpreter.runtime.SX = svBlockDecl;

            List <StackValue> registers = interpreter.runtime.GetRegisters();
            var newStackFrame           = new StackFrame(thisPtr,
                                                         classScopeCaller,
                                                         1,
                                                         returnAddr,
                                                         blockDecl,
                                                         blockCaller,
                                                         callerType,
                                                         StackFrameType.Function,
                                                         0, // depth
                                                         framePointer,
                                                         svBlockDecl.BlockIndex,
                                                         registers,
                                                         0);

            bool isInDebugMode = runtimeCore.Options.IDEDebugMode &&
                                 runtimeCore.Options.RunMode != InterpreterMode.Expression;

            if (isInDebugMode)
            {
                runtimeCore.DebugProps.SetUpCallrForDebug(
                    runtimeCore,
                    interpreter.runtime,
                    procNode,
                    returnAddr - 1,
                    false,
                    callsite,
                    args,
                    new List <List <ProtoCore.ReplicationGuide> >(),
                    newStackFrame);
            }

            StackValue rx = callsite.JILDispatchViaNewInterpreter(
                new Runtime.Context(),
                args,
                new List <List <ProtoCore.ReplicationGuide> >(),
                null,
                newStackFrame,
                runtimeCore);

            if (isInDebugMode)
            {
                runtimeCore.DebugProps.RestoreCallrForNoBreak(runtimeCore, procNode);
            }

            return(rx);
        }
Esempio n. 5
0
        public StackValue Evaluate(List <StackValue> args, StackFrame stackFrame)
        {
            // Build the stackframe
            var runtimeCore = interpreter.runtime.Core;

            int        classScopeCaller = (int)stackFrame.GetAt(StackFrame.AbsoluteIndex.kClass).opdata;
            int        returnAddr       = (int)stackFrame.GetAt(StackFrame.AbsoluteIndex.kReturnAddress).opdata;
            int        blockDecl        = (int)procNode.runtimeIndex;
            int        blockCaller      = (int)stackFrame.GetAt(StackFrame.AbsoluteIndex.kFunctionCallerBlock).opdata;
            int        framePointer     = runtimeCore.Rmem.FramePointer;
            StackValue thisPtr          = StackValue.BuildPointer(-1);

            bool isCallingMemberFunciton = procNode.classScope != Constants.kInvalidIndex &&
                                           !procNode.isConstructor &&
                                           !procNode.isStatic;

            bool isValidThisPointer = true;

            if (isCallingMemberFunciton)
            {
                Validity.Assert(args.Count >= 1);
                thisPtr = args[0];
                if (thisPtr.IsArray)
                {
                    isValidThisPointer = ArrayUtils.GetFirstNonArrayStackValue(thisPtr, ref thisPtr, runtimeCore);
                }
                else
                {
                    args.RemoveAt(0);
                }
            }

            if (!isValidThisPointer || (!thisPtr.IsPointer && !thisPtr.IsArray))
            {
                runtimeCore.RuntimeStatus.LogWarning(WarningID.kDereferencingNonPointer,
                                                     WarningMessage.kDeferencingNonPointer);
                return(StackValue.Null);
            }

            var callerType = (StackFrameType)stackFrame.GetAt(StackFrame.AbsoluteIndex.kStackFrameType).opdata;

            interpreter.runtime.TX = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.BounceType.kImplicit);

            StackValue svBlockDecl = StackValue.BuildBlockIndex(blockDecl);

            interpreter.runtime.SX = svBlockDecl;

            var repGuides = new List <List <ProtoCore.ReplicationGuide> >();

            List <StackValue> registers = new List <StackValue>();

            interpreter.runtime.SaveRegisters(registers);
            var newStackFrame = new StackFrame(thisPtr,
                                               classScopeCaller,
                                               1,
                                               returnAddr,
                                               blockDecl,
                                               blockCaller,
                                               callerType,
                                               StackFrameType.kTypeFunction,
                                               0,   // depth
                                               framePointer,
                                               registers,
                                               null);

            bool isInDebugMode = runtimeCore.Options.IDEDebugMode &&
                                 runtimeCore.ExecMode != InterpreterMode.kExpressionInterpreter;

            if (isInDebugMode)
            {
                runtimeCore.DebugProps.SetUpCallrForDebug(runtimeCore,
                                                          interpreter.runtime,
                                                          procNode,
                                                          returnAddr - 1,
                                                          false,
                                                          callsite,
                                                          args,
                                                          repGuides,
                                                          newStackFrame);
            }

            StackValue rx = callsite.JILDispatchViaNewInterpreter(
                new Runtime.Context(),
                args,
                repGuides,
                newStackFrame,
                runtimeCore);

            if (isInDebugMode)
            {
                runtimeCore.DebugProps.RestoreCallrForNoBreak(runtimeCore, procNode);
            }

            return(rx);
        }