Example #1
0
        /// <summary>
        /// Ensures that dispose method node is created for this empty type.
        /// </summary>
        /// <param name="module">Reference module</param>
        public void EnsureDisposeMethod(CLRDLLModule module)
        {
            foreach (var item in ClassNode.funclist)
            {
                if (CoreUtils.IsDisposeMethod(item.Name))
                {
                    return; //Dispose method is already present.
                }
            }
            bool resetModule = false;

            if (Module == null)
            {
                Module      = module;
                resetModule = true;
            }
            AssociativeNode        node = ParseMethod(mDisposeMethod);
            FunctionDefinitionNode func = node as FunctionDefinitionNode;

            if (func != null)
            {
                func.Name            = ProtoCore.DSDefinitions.Keyword.Dispose;
                func.IsStatic        = false;
                func.IsAutoGenerated = true;
                ClassNode.funclist.Add(func);
            }
            if (resetModule)
            {
                Module = null;
            }
        }
Example #2
0
        private static bool TryGetFunctionDescriptor(
            AssociativeNode associativeNode, string asmPath,
            string className,
            out FunctionDescriptor descriptor)
        {
            descriptor = null;
            string name = associativeNode.Name;

            if (CoreUtils.IsSetter(name) ||
                CoreUtils.IsDisposeMethod(name) ||
                CoreUtils.StartsWithDoubleUnderscores(name))
            {
                return(false);
            }

            FunctionDescriptorParams functionParams = null;

            switch (associativeNode.Kind)
            {
            case AstKind.Constructor:
                functionParams = FunctionParamsFromConstructor(associativeNode as ConstructorDefinitionNode, asmPath, className);
                break;

            case AstKind.FunctionDefintion:
                functionParams = FunctionParamsFromFunction(associativeNode as FunctionDefinitionNode, asmPath, className);
                break;

            default:
                break;
            }

            descriptor = new FunctionDescriptor(functionParams);
            return(true);
        }
Example #3
0
        /// <summary>
        /// Retrieves an existing instance of a callsite associated with a UID
        /// It creates a new callsite if non was found
        /// </summary>
        /// <param name="core"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public CallSite GetCallSite(GraphNode graphNode,
                                    int classScope,
                                    string methodName,
                                    Executable executable,
                                    int runningBlock,
                                    Options options,
                                    RuntimeStatus runtimeStatus
                                    )
        {
            Validity.Assert(null != executable.FunctionTable);
            CallSite csInstance = null;

            // TODO Jun: Currently generates a new callsite for imperative and
            // internally generated functions.
            // Fix the issues that cause the cache to go out of sync when
            // attempting to cache internal functions. This may require a
            // secondary callsite cache for internal functions so they dont
            // clash with the graphNode UID key
            var  language           = executable.instrStreamList[runningBlock].language;
            bool isImperative       = language == Language.kImperative;
            bool isInternalFunction = CoreUtils.IsInternalFunction(methodName);

            if (isInternalFunction || isImperative)
            {
                csInstance = new CallSite(classScope,
                                          methodName,
                                          executable.FunctionTable,
                                          options.ExecutionMode);
            }
            else if (!CallsiteCache.TryGetValue(graphNode.CallsiteIdentifier, out csInstance))
            {
                // Attempt to retrieve a preloaded callsite data (optional).
                var traceData = GetAndRemoveTraceDataForNode(graphNode.guid);

                csInstance = new CallSite(classScope,
                                          methodName,
                                          executable.FunctionTable,
                                          options.ExecutionMode,
                                          traceData);

                CallsiteCache[graphNode.CallsiteIdentifier] = csInstance;
                CallSiteToNodeMap[csInstance.CallSiteID]    = graphNode.guid;
                ASTToCallSiteMap[graphNode.AstID]           = csInstance;
            }

            if (graphNode != null && !CoreUtils.IsDisposeMethod(methodName))
            {
                csInstance.UpdateCallSite(classScope, methodName);
                if (options.IsDeltaExecution)
                {
                    runtimeStatus.ClearWarningForExpression(graphNode.exprUID);
                }
            }

            return(csInstance);
        }
Example #4
0
        //this is incomplete todo: implement
        public override List <FFIFunctionPointer> GetFunctionPointers(string className, string name)
        {
            CLRModuleType type = null;

            if (mTypes.TryGetValue(className, out type))
            {
                return(type.GetFunctionPointers(name));
            }

            if (CoreUtils.IsDisposeMethod(name))
            {
                List <FFIFunctionPointer> pointers = new List <FFIFunctionPointer>();
                pointers.Add(new DisposeFunctionPointer(this, CLRModuleType.DisposeMethod, CLRModuleType.GetProtoCoreType(CLRModuleType.DisposeMethod.ReturnType, this)));
                return(pointers);
            }

            throw new KeyNotFoundException(string.Format("Function definition for {0}.{1}, not found", className, name));
        }
Example #5
0
 public ProcedureNode GetDisposeMethod()
 {
     if (!hasCachedDisposeMethod)
     {
         hasCachedDisposeMethod = true;
         if (vtable == null)
         {
             disposeMethod = null;
         }
         else
         {
             foreach (ProcedureNode procNode in vtable.procList)
             {
                 if (CoreUtils.IsDisposeMethod(procNode.name) && procNode.argInfoList.Count == 0)
                 {
                     disposeMethod = procNode;
                     break;
                 }
             }
         }
     }
     return(disposeMethod);
 }
Example #6
0
 public ProcedureNode GetDisposeMethod()
 {
     if (!hasCachedDisposeMethod)
     {
         hasCachedDisposeMethod = true;
         if (ProcTable == null)
         {
             disposeMethod = null;
         }
         else
         {
             foreach (ProcedureNode procNode in ProcTable.Procedures)
             {
                 if (CoreUtils.IsDisposeMethod(procNode.Name) && procNode.ArgumentInfos.Count == 0)
                 {
                     disposeMethod = procNode;
                     break;
                 }
             }
         }
     }
     return disposeMethod;
 }
Example #7
0
        private void RegisterFunctionPointer(string functionName, MemberInfo method, List <ProtoCore.Type> argTypes, ProtoCore.Type retype)
        {
            List <FFIFunctionPointer> pointers = GetFunctionPointers(functionName);
            FFIFunctionPointer        f        = null;

            if (CoreUtils.IsDisposeMethod(functionName))
            {
                f = new DisposeFunctionPointer(Module, method, retype);
            }
            else if (CoreUtils.IsGetter(functionName))
            {
                f = new GetterFunctionPointer(Module, functionName, method, retype);
            }
            else
            {
                f = new CLRFFIFunctionPointer(Module, functionName, method, argTypes, retype);
            }

            if (!pointers.Contains(f))
            {
                pointers.Add(f);
            }
        }
Example #8
0
        public override StackValue Execute(ProtoCore.Runtime.Context c, List <StackValue> formalParameters, ProtoCore.DSASM.StackFrame stackFrame, Core core)
        {
            ProtoCore.DSASM.Interpreter interpreter  = new ProtoCore.DSASM.Interpreter(core, true);
            ProtoCore.DSASM.Executive   oldDSASMExec = null;
            if (core.CurrentExecutive != null)
            {
                oldDSASMExec = core.CurrentExecutive.CurrentDSASMExec;
                core.CurrentExecutive.CurrentDSASMExec = interpreter.runtime;
            }

            // Assert for the block type
            activation.globs = core.DSExecutable.runtimeSymbols[core.RunningBlock].GetGlobalSize();

            //
            // Comment Jun:
            // Storing execution states is relevant only if the current scope is a function,
            // as this mechanism is used to keep track of maintining execution states of recursive calls
            // This mechanism should also be ignored if the function call is non-recursive as it does not need to maintains state in that case
            int execStateSize = procedureNode.GraphNodeList.Count;

            stackFrame.ExecutionStateSize = execStateSize;
            for (int n = execStateSize - 1; n >= 0; --n)
            {
                AssociativeGraph.GraphNode gnode = procedureNode.GraphNodeList[n];
                interpreter.Push(StackValue.BuildBoolean(gnode.isDirty));
            }

            // Push Params
            formalParameters.Reverse();
            for (int i = 0; i < formalParameters.Count; i++)
            {
                interpreter.Push(formalParameters[i]);
            }

            StackValue svThisPtr   = stackFrame.ThisPtr;
            StackValue svBlockDecl = StackValue.BuildBlockIndex(stackFrame.FunctionBlock);

            // Jun: Make sure we have no empty or unaligned frame data
            Validity.Assert(DSASM.StackFrame.kStackFrameSize == stackFrame.Frame.Length);

            // Setup the stack frame data
            //int thisPtr = (int)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kThisPtr).opdata;
            int ci           = activation.classIndex;
            int fi           = activation.funcIndex;
            int returnAddr   = stackFrame.ReturnPC;
            int blockDecl    = stackFrame.FunctionBlock;
            int blockCaller  = stackFrame.FunctionCallerBlock;
            int framePointer = core.Rmem.FramePointer;
            int locals       = activation.locals;


            // Update the running block to tell the execution engine which set of instruction to execute
            // TODO(Jun/Jiong): Considering store the orig block id to stack frame
            int origRunningBlock = core.RunningBlock;

            core.RunningBlock = (int)svBlockDecl.opdata;

            // Set SX register
            interpreter.runtime.SX = svBlockDecl;

            StackFrameType callerType = stackFrame.CallerStackFrameType;

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

            StackValue svCallConvention;
            bool       isDispose = CoreUtils.IsDisposeMethod(procedureNode.name);

            bool explicitCall = !c.IsReplicating && !c.IsImplicitCall && !isDispose;

            if (explicitCall)
            {
                svCallConvention = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.CallType.kExplicit);
            }
            else
            {
                svCallConvention = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.CallType.kImplicit);
            }

            stackFrame.TX          = svCallConvention;
            interpreter.runtime.TX = svCallConvention;

            // Set SX register
            stackFrame.SX          = svBlockDecl;
            interpreter.runtime.SX = svBlockDecl;

            // TODO Jun:
            // The stackframe carries the current set of registers
            // Determine if this can be done even for the non explicit call implementation
            registers.AddRange(stackFrame.GetRegisters());


            // Comment Jun: the depth is always 0 for a function call as we are reseting this for each function call
            // This is only incremented for every language block bounce
            int depth = stackFrame.Depth;

            DSASM.StackFrameType type = stackFrame.StackFrameType;
            Validity.Assert(depth == 0);
            Validity.Assert(type == DSASM.StackFrameType.kTypeFunction);

            core.Rmem.PushStackFrame(svThisPtr, ci, fi, returnAddr, blockDecl, blockCaller, callerType, type, depth, framePointer, registers, locals, execStateSize);


            StackValue svRet;

            if (explicitCall)
            {
                svRet = ProtoCore.DSASM.StackValue.BuildExplicitCall(activation.pc);
            }
            else
            {
                svRet             = interpreter.Run(core.RunningBlock, activation.pc, Language.kInvalid, core.Breakpoints);
                core.RunningBlock = origRunningBlock;
            }

            if (core.CurrentExecutive != null)
            {
                core.CurrentExecutive.CurrentDSASMExec = oldDSASMExec;
            }
            return(svRet); //DSASM.Mirror.ExecutionMirror.Unpack(svRet, core.heap, core);
        }
Example #9
0
        private void ImportProcedure(string library, ProcedureNode proc)
        {
            string procName = proc.name;

            if (proc.isAutoGeneratedThisProc ||
                CoreUtils.IsSetter(procName) ||
                CoreUtils.IsDisposeMethod(procName) ||
                CoreUtils.StartsWithDoubleUnderscores(procName))
            {
                return;
            }

            string           obsoleteMessage = "";
            int              classScope      = proc.classScope;
            string           className       = string.Empty;
            MethodAttributes methodAttribute = proc.MethodAttribute;
            ClassAttributes  classAttribute  = null;

            if (classScope != Constants.kGlobalScope)
            {
                var classNode = LibraryManagementCore.ClassTable.ClassNodes[classScope];

                classAttribute = classNode.ClassAttributes;
                className      = classNode.name;
            }

            // MethodAttribute's HiddenInLibrary has higher priority than
            // ClassAttribute's HiddenInLibrary
            var isVisible             = true;
            var canUpdatePeriodically = false;

            if (methodAttribute != null)
            {
                isVisible             = !methodAttribute.HiddenInLibrary;
                canUpdatePeriodically = methodAttribute.CanUpdatePeriodically;
            }
            else
            {
                if (classAttribute != null)
                {
                    isVisible = !classAttribute.HiddenInLibrary;
                }
            }

            FunctionType type;

            if (classScope == Constants.kGlobalScope)
            {
                type = FunctionType.GenericFunction;
            }
            else
            {
                if (CoreUtils.IsGetter(procName))
                {
                    type = proc.isStatic
                        ? FunctionType.StaticProperty
                        : FunctionType.InstanceProperty;

                    string property;
                    if (CoreUtils.TryGetPropertyName(procName, out property))
                    {
                        procName = property;
                    }
                }
                else
                {
                    if (proc.isConstructor)
                    {
                        type = FunctionType.Constructor;
                    }
                    else if (proc.isStatic)
                    {
                        type = FunctionType.StaticMethod;
                    }
                    else
                    {
                        type = FunctionType.InstanceMethod;
                    }
                }
            }

            List <TypedParameter> arguments = proc.argInfoList.Zip(
                proc.argTypeList,
                (arg, argType) =>
            {
                AssociativeNode defaultArgumentNode;
                // Default argument specified by DefaultArgumentAttribute
                // takes higher priority
                if (!TryGetDefaultArgumentFromAttribute(arg, out defaultArgumentNode) &&
                    arg.IsDefault)
                {
                    var binaryExpr = arg.DefaultExpression as BinaryExpressionNode;
                    if (binaryExpr != null)
                    {
                        defaultArgumentNode = binaryExpr.RightNode;
                    }
                }

                return(new TypedParameter(arg.Name, argType, defaultArgumentNode));
            }).ToList();

            IEnumerable <string> returnKeys = null;

            if (proc.MethodAttribute != null)
            {
                if (proc.MethodAttribute.ReturnKeys != null)
                {
                    returnKeys = proc.MethodAttribute.ReturnKeys;
                }
                if (proc.MethodAttribute.IsObsolete)
                {
                    obsoleteMessage = proc.MethodAttribute.ObsoleteMessage;
                }
            }

            var function = new FunctionDescriptor(new FunctionDescriptorParams
            {
                Assembly              = library,
                ClassName             = className,
                FunctionName          = procName,
                Parameters            = arguments,
                ReturnType            = proc.returntype,
                FunctionType          = type,
                IsVisibleInLibrary    = isVisible,
                ReturnKeys            = returnKeys,
                PathManager           = pathManager,
                IsVarArg              = proc.isVarArg,
                ObsoleteMsg           = obsoleteMessage,
                CanUpdatePeriodically = canUpdatePeriodically,
                IsBuiltIn             = pathManager.PreloadedLibraries.Contains(library)
            });

            AddImportedFunctions(library, new[] { function });
        }
Example #10
0
        public override StackValue Execute(ProtoCore.Runtime.Context c, List <StackValue> formalParameters, ProtoCore.DSASM.StackFrame stackFrame, Core core)
        {
            ProtoCore.DSASM.Interpreter interpreter  = new ProtoCore.DSASM.Interpreter(core, true);
            ProtoCore.DSASM.Executive   oldDSASMExec = null;
            if (core.CurrentExecutive != null)
            {
                oldDSASMExec = core.CurrentExecutive.CurrentDSASMExec;
                core.CurrentExecutive.CurrentDSASMExec = interpreter.runtime;
            }

            // Assert for the block type
            activation.globs = core.DSExecutable.runtimeSymbols[core.RunningBlock].GetGlobalSize();

            // Push Execution states
            int execStateSize = 0;

            if (null != stackFrame.ExecutionStates)
            {
                execStateSize = stackFrame.ExecutionStates.Length;

                // ExecutionStates are in lexical order
                // Push them in reverse order (similar to args) so they can be retrieved in sequence
                // Retrieveing the executing states occur on function return
                for (int n = execStateSize - 1; n >= 0; --n)
                {
                    StackValue svState = stackFrame.ExecutionStates[n];
                    Validity.Assert(svState.IsBoolean);
                    interpreter.Push(svState);
                }
            }

            // Push Params
            formalParameters.Reverse();
            for (int i = 0; i < formalParameters.Count; i++)
            {
                interpreter.Push(formalParameters[i]);
            }

            StackValue svThisPtr   = stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kThisPtr);
            StackValue svBlockDecl = stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kFunctionBlock);

            // Jun: Make sure we have no empty or unaligned frame data
            Validity.Assert(DSASM.StackFrame.kStackFrameSize == stackFrame.Frame.Length);

            // Setup the stack frame data
            //int thisPtr = (int)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kThisPtr).opdata;
            int ci           = activation.classIndex;
            int fi           = activation.funcIndex;
            int returnAddr   = (int)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kReturnAddress).opdata;
            int blockDecl    = (int)svBlockDecl.opdata;
            int blockCaller  = (int)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kFunctionCallerBlock).opdata;
            int framePointer = core.Rmem.FramePointer;
            int locals       = activation.locals;


            // Update the running block to tell the execution engine which set of instruction to execute
            // TODO(Jun/Jiong): Considering store the orig block id to stack frame
            int origRunningBlock = core.RunningBlock;

            core.RunningBlock = (int)svBlockDecl.opdata;

            // Set SX register
            interpreter.runtime.SX = svBlockDecl;

            DSASM.StackFrameType callerType = (DSASM.StackFrameType)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kCallerStackFrameType).opdata;

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

            StackValue svCallConvention;
            bool       isDispose = CoreUtils.IsDisposeMethod(procedureNode.name);

            bool explicitCall = !c.IsReplicating && !c.IsImplicitCall && !isDispose;

            if (explicitCall)
            {
                svCallConvention = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.CallType.kExplicit);
            }
            else
            {
                svCallConvention = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.CallType.kImplicit);
            }

            stackFrame.SetAt(DSASM.StackFrame.AbsoluteIndex.kRegisterTX, svCallConvention);
            interpreter.runtime.TX = svCallConvention;

            // Set SX register
            stackFrame.SetAt(DSASM.StackFrame.AbsoluteIndex.kRegisterSX, svBlockDecl);
            interpreter.runtime.SX = svBlockDecl;

            // TODO Jun:
            // The stackframe carries the current set of registers
            // Determine if this can be done even for the non explicit call implementation
            registers.AddRange(stackFrame.GetRegisters());


            // Comment Jun: the depth is always 0 for a function call as we are reseting this for each function call
            // This is only incremented for every language block bounce
            int depth = (int)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kStackFrameDepth).opdata;

            DSASM.StackFrameType type = (DSASM.StackFrameType)stackFrame.GetAt(DSASM.StackFrame.AbsoluteIndex.kStackFrameType).opdata;
            Validity.Assert(depth == 0);
            Validity.Assert(type == DSASM.StackFrameType.kTypeFunction);

            core.Rmem.PushStackFrame(svThisPtr, ci, fi, returnAddr, blockDecl, blockCaller, callerType, type, depth, framePointer, registers, locals, execStateSize);


            StackValue svRet;

            if (explicitCall)
            {
                svRet = ProtoCore.DSASM.StackValue.BuildExplicitCall(activation.pc);
            }
            else
            {
                if (core.ExecMode != DSASM.InterpreterMode.kExpressionInterpreter && core.Options.IDEDebugMode)
                {
                    svRet = interpreter.Run(core.Breakpoints, core.RunningBlock, activation.pc, Language.kInvalid);
                }
                else
                {
                    svRet = interpreter.Run(core.RunningBlock, activation.pc, Language.kInvalid);
                }
                core.RunningBlock = origRunningBlock;
            }

            if (core.CurrentExecutive != null)
            {
                core.CurrentExecutive.CurrentDSASMExec = oldDSASMExec;
            }
            return(svRet); //DSASM.Mirror.ExecutionMirror.Unpack(svRet, core.heap, core);
        }
Example #11
0
        private void ImportProcedure(string library, ProcedureNode proc)
        {
            string procName = proc.Name;

            if (proc.IsAutoGeneratedThisProc ||
                // There could be DS functions that have private access
                // that shouldn't be imported into the Library
                proc.AccessModifier == AccessModifier.Private ||
                CoreUtils.IsSetter(procName) ||
                CoreUtils.IsDisposeMethod(procName) ||
                CoreUtils.StartsWithDoubleUnderscores(procName))
            {
                return;
            }

            string           obsoleteMessage = "";
            int              classScope      = proc.ClassID;
            string           className       = string.Empty;
            MethodAttributes methodAttribute = proc.MethodAttribute;
            ClassAttributes  classAttribute  = null;

            if (classScope != Constants.kGlobalScope)
            {
                var classNode = LibraryManagementCore.ClassTable.ClassNodes[classScope];

                classAttribute = classNode.ClassAttributes;
                className      = classNode.Name;
            }

            // MethodAttribute's HiddenInLibrary has higher priority than
            // ClassAttribute's HiddenInLibrary
            var isVisible             = true;
            var canUpdatePeriodically = false;

            if (methodAttribute != null)
            {
                isVisible             = !methodAttribute.HiddenInLibrary;
                canUpdatePeriodically = methodAttribute.CanUpdatePeriodically;
            }
            else
            {
                if (classAttribute != null)
                {
                    isVisible = !classAttribute.HiddenInLibrary;
                }
            }

            FunctionType type;

            if (classScope == Constants.kGlobalScope)
            {
                type = FunctionType.GenericFunction;
            }
            else
            {
                if (CoreUtils.IsGetter(procName))
                {
                    type = proc.IsStatic
                        ? FunctionType.StaticProperty
                        : FunctionType.InstanceProperty;

                    string property;
                    if (CoreUtils.TryGetPropertyName(procName, out property))
                    {
                        procName = property;
                    }
                }
                else
                {
                    if (proc.IsConstructor)
                    {
                        type = FunctionType.Constructor;
                    }
                    else if (proc.IsStatic)
                    {
                        type = FunctionType.StaticMethod;
                    }
                    else
                    {
                        type = FunctionType.InstanceMethod;
                    }
                }
            }

            List <TypedParameter> arguments = proc.ArgumentInfos.Zip(
                proc.ArgumentTypes,
                (arg, argType) =>
            {
                AssociativeNode defaultArgumentNode;
                // Default argument specified by DefaultArgumentAttribute
                // takes higher priority
                if (!TryGetDefaultArgumentFromAttribute(arg, out defaultArgumentNode) &&
                    arg.IsDefault)
                {
                    var binaryExpr = arg.DefaultExpression as BinaryExpressionNode;
                    if (binaryExpr != null)
                    {
                        defaultArgumentNode = binaryExpr.RightNode;
                    }
                }
                string shortName = null;
                if (defaultArgumentNode != null)
                {
                    shortName           = defaultArgumentNode.ToString();
                    var rewriter        = new ElementRewriter(LibraryManagementCore.ClassTable, LibraryManagementCore.BuildStatus.LogSymbolConflictWarning);
                    defaultArgumentNode = defaultArgumentNode.Accept(rewriter);
                }
                return(new TypedParameter(arg.Name, argType, defaultArgumentNode, shortName));
            }).ToList();

            bool isLacingDisabled           = false;
            IEnumerable <string> returnKeys = null;

            if (proc.MethodAttribute != null)
            {
                if (proc.MethodAttribute.ReturnKeys != null)
                {
                    returnKeys = proc.MethodAttribute.ReturnKeys;
                }
                if (proc.MethodAttribute.IsObsolete)
                {
                    obsoleteMessage = proc.MethodAttribute.ObsoleteMessage;
                }
                isLacingDisabled = proc.MethodAttribute.IsLacingDisabled;
            }

            var function = new FunctionDescriptor(new FunctionDescriptorParams
            {
                Assembly              = library,
                ClassName             = className,
                FunctionName          = procName,
                Parameters            = arguments,
                ReturnType            = proc.ReturnType,
                FunctionType          = type,
                IsVisibleInLibrary    = isVisible,
                ReturnKeys            = returnKeys,
                PathManager           = pathManager,
                IsVarArg              = proc.IsVarArg,
                ObsoleteMsg           = obsoleteMessage,
                CanUpdatePeriodically = canUpdatePeriodically,
                IsBuiltIn             = pathManager.PreloadedLibraries.Contains(library),
                IsPackageMember       = packagedLibraries.Contains(library),
                IsLacingDisabled      = isLacingDisabled
            });

            AddImportedFunctions(library, new[] { function });
        }
Example #12
0
        public void SetUpCallrForDebug(RuntimeCore runtimeCore, DSASM.Executive exec, ProcedureNode fNode, int pc, bool isBaseCall = false,
                                       CallSite callsite = null, List <StackValue> arguments         = null, List <List <ReplicationGuide> > replicationGuides = null, StackFrame stackFrame = null,
                                       List <StackValue> dotCallDimensions = null, bool hasDebugInfo = false, bool isMember = false, StackValue?thisPtr = null)
        {
            //ProtoCore.DSASM.Executive exec = core.CurrentExecutive.CurrentDSASMExec;

            DebugFrame debugFrame = new DebugFrame();

            debugFrame.IsBaseCall       = isBaseCall;
            debugFrame.Arguments        = arguments;
            debugFrame.IsMemberFunction = isMember;
            debugFrame.ThisPtr          = thisPtr;
            debugFrame.HasDebugInfo     = hasDebugInfo;

            if (CoreUtils.IsDisposeMethod(fNode.Name))
            {
                debugFrame.IsDisposeCall = true;
                ReturnPCFromDispose      = DebugEntryPC;
            }

            if (RunMode == Runmode.StepNext)
            {
                debugFrame.FunctionStepOver = true;
            }

            bool isReplicating      = false;
            bool isExternalFunction = false;

            // callsite is set to null for a base class constructor call in CALL
            if (callsite == null)
            {
                isReplicating      = false;
                isExternalFunction = false;

                SetUpCallr(ref debugFrame, isReplicating, isExternalFunction, exec);
                DebugStackFrame.Push(debugFrame);

                return;
            }

            // Comment Jun: A dot call does not replicate and  must be handled immediately
            if (fNode.Name == Constants.kDotMethodName)
            {
                isReplicating                = false;
                isExternalFunction           = false;
                debugFrame.IsDotCall         = true;
                debugFrame.DotCallDimensions = dotCallDimensions;

                SetUpCallr(ref debugFrame, isReplicating, isExternalFunction, exec);
                DebugStackFrame.Push(debugFrame);

                return;
            }

            List <List <ReplicationInstruction> > replicationTrials;
            bool willReplicate = callsite.WillCallReplicate(new Context(), arguments, replicationGuides, stackFrame, runtimeCore, out replicationTrials);

            // the inline conditional built-in is handled separately as 'WillCallReplicate' is always true in this case
            if (fNode.Name.Equals(Constants.kInlineConditionalMethodName))
            {
                // The inline conditional built-in is created only for associative blocks and needs to be handled separately as below
                InstructionStream istream = runtimeCore.DSExecutable.instrStreamList[CurrentBlockId];
                Validity.Assert(istream.language == Language.Associative);
                {
                    runtimeCore.DebugProps.InlineConditionOptions.isInlineConditional = true;
                    runtimeCore.DebugProps.InlineConditionOptions.startPc             = pc;

                    runtimeCore.DebugProps.InlineConditionOptions.endPc = FindEndPCForAssocGraphNode(pc, istream, fNode, exec.Properties.executingGraphNode, runtimeCore.Options.ExecuteSSA);


                    runtimeCore.DebugProps.InlineConditionOptions.instructionStream = runtimeCore.RunningBlock;
                    debugFrame.IsInlineConditional = true;
                }

                // no replication case
                if (willReplicate && replicationTrials.Count == 1)
                {
                    runtimeCore.DebugProps.InlineConditionOptions.ActiveBreakPoints.AddRange(runtimeCore.Breakpoints);

                    isReplicating      = false;
                    isExternalFunction = false;
                }
                else // an inline conditional call that replicates
                {
                    // Clear all breakpoints for outermost replicated call
                    if (!DebugStackFrameContains(StackFrameFlagOptions.IsReplicating))
                    {
                        ActiveBreakPoints.AddRange(runtimeCore.Breakpoints);
                        runtimeCore.Breakpoints.Clear();
                    }
                    isExternalFunction = false;
                    isReplicating      = true;
                }
                SetUpCallr(ref debugFrame, isReplicating, isExternalFunction, exec, 0);

                DebugStackFrame.Push(debugFrame);

                return;
            }
            // Prevent breaking inside a function that is external except for dot calls
            // by clearing all breakpoints from outermost external function call
            // This check takes precedence over the replication check
            else if (fNode.IsExternal && fNode.Name != Constants.kDotMethodName)
            {
                // Clear all breakpoints
                if (!DebugStackFrameContains(StackFrameFlagOptions.IsExternalFunction) && fNode.Name != Constants.kFunctionRangeExpression)
                {
                    ActiveBreakPoints.AddRange(runtimeCore.Breakpoints);
                    runtimeCore.Breakpoints.Clear();
                }

                isExternalFunction = true;
                isReplicating      = false;
            }
            // Find if function call will replicate or not and if so
            // prevent stepping in by removing all breakpoints from outermost replicated call
            else if (willReplicate)
            {
                // Clear all breakpoints for outermost replicated call
                if (!DebugStackFrameContains(StackFrameFlagOptions.IsReplicating))
                {
                    ActiveBreakPoints.AddRange(runtimeCore.Breakpoints);
                    runtimeCore.Breakpoints.Clear();
                }

                isReplicating      = true;
                isExternalFunction = false;
            }
            // For all other function calls
            else
            {
                isReplicating      = false;
                isExternalFunction = false;
            }

            SetUpCallr(ref debugFrame, isReplicating, isExternalFunction, exec);
            DebugStackFrame.Push(debugFrame);
        }
Example #13
0
        private void ImportProcedure(string library, ProcedureNode proc)
        {
            string procName = proc.name;

            if (proc.isAutoGeneratedThisProc ||
                CoreUtils.IsSetter(procName) ||
                CoreUtils.IsDisposeMethod(procName) ||
                CoreUtils.StartsWithDoubleUnderscores(procName))
            {
                return;
            }

            int              classScope      = proc.classScope;
            string           className       = string.Empty;
            MethodAttributes methodAttribute = proc.MethodAttribute;
            ClassAttributes  classAttribute  = null;

            if (classScope != ProtoCore.DSASM.Constants.kGlobalScope)
            {
                var classNode = GraphUtilities.GetCore().ClassTable.ClassNodes[classScope];

                classAttribute = classNode.ClassAttributes;
                className      = classNode.name;
            }

            // MethodAttribute's HiddenInLibrary has higher priority than
            // ClassAttribute's HiddenInLibrary
            bool isVisible = true;

            if (methodAttribute != null)
            {
                isVisible = !methodAttribute.HiddenInLibrary;
            }
            else
            {
                if (classAttribute != null)
                {
                    isVisible = !classAttribute.HiddenInLibrary;
                }
            }

            FunctionType type;

            if (classScope == ProtoCore.DSASM.Constants.kGlobalScope)
            {
                type = FunctionType.GenericFunction;
            }
            else
            {
                if (CoreUtils.IsGetter(procName))
                {
                    type = proc.isStatic
                        ? FunctionType.StaticProperty
                        : FunctionType.InstanceProperty;

                    string property;
                    if (CoreUtils.TryGetPropertyName(procName, out property))
                    {
                        procName = property;
                    }
                }
                else
                {
                    if (proc.isConstructor)
                    {
                        type = FunctionType.Constructor;
                    }
                    else if (proc.isStatic)
                    {
                        type = FunctionType.StaticMethod;
                    }
                    else
                    {
                        type = FunctionType.InstanceMethod;
                    }
                }
            }

            IEnumerable <TypedParameter> arguments = proc.argInfoList.Zip(
                proc.argTypeList,
                (arg, argType) =>
            {
                object defaultValue = null;
                if (arg.IsDefault)
                {
                    var binaryExpr = arg.DefaultExpression as BinaryExpressionNode;
                    if (binaryExpr != null)
                    {
                        AssociativeNode vnode = binaryExpr.RightNode;
                        if (vnode is IntNode)
                        {
                            defaultValue = (vnode as IntNode).Value;
                        }
                        else if (vnode is DoubleNode)
                        {
                            defaultValue = (vnode as DoubleNode).Value;
                        }
                        else if (vnode is BooleanNode)
                        {
                            defaultValue = (vnode as BooleanNode).Value;
                        }
                        else if (vnode is StringNode)
                        {
                            defaultValue = (vnode as StringNode).value;
                        }
                    }
                }

                return(new TypedParameter(arg.Name, argType.ToString(), defaultValue));
            });

            IEnumerable <string> returnKeys = null;

            if (proc.MethodAttribute != null && proc.MethodAttribute.ReturnKeys != null)
            {
                returnKeys = proc.MethodAttribute.ReturnKeys;
            }

            var function = new FunctionDescriptor(
                library,
                className,
                procName,
                arguments,
                proc.returntype.ToString(),
                type,
                isVisible,
                returnKeys,
                proc.isVarArg);

            AddImportedFunctions(library, new[] { function });
        }
Example #14
0
        /// <summary>
        /// Retrieves an existing instance of a callsite associated with a UID
        /// It creates a new callsite if non was found
        /// </summary>
        /// <param name="core"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public CallSite GetCallSite(int classScope, string methodName, Executable executable, RuntimeCore runtimeCore)
        {
            Validity.Assert(null != executable.FunctionTable);
            CallSite csInstance   = null;
            var      graphNode    = executable.ExecutingGraphnode;
            var      topGraphNode = graphNode;

            // If it is a nested function call, append all callsite ids
            List <string> callsiteIdentifiers = new List <string>();

            foreach (var prop in runtimeCore.InterpreterProps)
            {
                if (prop != null && prop.executingGraphNode != null && graphNode != prop.executingGraphNode)
                {
                    topGraphNode = prop.executingGraphNode;
                    if (!string.IsNullOrEmpty(topGraphNode.CallsiteIdentifier))
                    {
                        callsiteIdentifiers.Add(topGraphNode.CallsiteIdentifier);
                    }
                }
            }
            if (graphNode != null)
            {
                callsiteIdentifiers.Add(graphNode.CallsiteIdentifier);
            }
            var callsiteID = string.Join(";", callsiteIdentifiers.ToArray());

            // TODO Jun: Currently generates a new callsite for imperative and
            // internally generated functions.
            // Fix the issues that cause the cache to go out of sync when
            // attempting to cache internal functions. This may require a
            // secondary callsite cache for internal functions so they dont
            // clash with the graphNode UID key
            var  language           = executable.instrStreamList[runtimeCore.RunningBlock].language;
            bool isImperative       = language == Language.Imperative;
            bool isInternalFunction = CoreUtils.IsInternalFunction(methodName);

            if (isInternalFunction || isImperative)
            {
                csInstance = new CallSite(classScope,
                                          methodName,
                                          executable.FunctionTable,
                                          runtimeCore.Options.ExecutionMode);
            }
            else if (!CallsiteCache.TryGetValue(callsiteID, out csInstance))
            {
                // Attempt to retrieve a preloaded callsite data (optional).
                var traceData = GetAndRemoveTraceDataForNode(topGraphNode.guid, callsiteID);

                csInstance = new CallSite(classScope,
                                          methodName,
                                          executable.FunctionTable,
                                          runtimeCore.Options.ExecutionMode,
                                          traceData);

                CallsiteCache[callsiteID] = csInstance;
                CallSiteToNodeMap[csInstance.CallSiteID] = topGraphNode.guid;
            }

            if (graphNode != null && !CoreUtils.IsDisposeMethod(methodName))
            {
                csInstance.UpdateCallSite(classScope, methodName);
                if (runtimeCore.Options.IsDeltaExecution)
                {
                    runtimeCore.RuntimeStatus.ClearWarningForExpression(graphNode.exprUID);
                }
            }

            return(csInstance);
        }
Example #15
0
        private void ImportProcedure(string library, ClassNode classNode, ProcedureNode proc)
        {
            if (proc.isAutoGeneratedThisProc)
            {
                return;
            }

            bool isVisibleInLibrary = !(null != proc.MethodAttribute && proc.MethodAttribute.HiddenInLibrary);

            //If the class is Hidden all methods are hidden.
            if (null != classNode.ClassAttributes && classNode.ClassAttributes.HiddenInLibrary)
            {
                isVisibleInLibrary = null != proc.MethodAttribute && !proc.MethodAttribute.HiddenInLibrary;
                //But if a particular method is not hidden, then this method is visible
            }

            string procName = proc.name;

            if (CoreUtils.IsSetter(procName) || CoreUtils.IsDisposeMethod(procName) ||
                CoreUtils.StartsWithDoubleUnderscores(procName))
            {
                return;
            }

            FunctionType type;

            if (CoreUtils.IsGetter(procName))
            {
                type = proc.isStatic ? FunctionType.StaticProperty : FunctionType.InstanceProperty;

                string property;
                if (CoreUtils.TryGetPropertyName(procName, out property))
                {
                    procName = property;
                }
            }
            else
            {
                if (proc.isConstructor)
                {
                    type = FunctionType.Constructor;
                }
                else if (proc.isStatic)
                {
                    type = FunctionType.StaticMethod;
                }
                else
                {
                    type = FunctionType.InstanceMethod;
                }
            }

            IEnumerable <TypedParameter> arguments = proc.argInfoList.Zip(
                proc.argTypeList,
                (arg, argType) =>
            {
                object defaultValue = null;
                if (arg.IsDefault)
                {
                    var binaryExpr = arg.DefaultExpression as BinaryExpressionNode;
                    if (binaryExpr != null)
                    {
                        AssociativeNode vnode = binaryExpr.RightNode;
                        if (vnode is IntNode)
                        {
                            defaultValue = (vnode as IntNode).Value;
                        }
                        else if (vnode is DoubleNode)
                        {
                            defaultValue = (vnode as DoubleNode).Value;
                        }
                        else if (vnode is BooleanNode)
                        {
                            defaultValue = (vnode as BooleanNode).Value;
                        }
                        else if (vnode is StringNode)
                        {
                            defaultValue = (vnode as StringNode).value;
                        }
                    }
                }

                return(new TypedParameter(arg.Name, argType.ToString(), defaultValue));
            });

            IEnumerable <string> returnKeys = null;

            if (proc.MethodAttribute != null && proc.MethodAttribute.ReturnKeys != null)
            {
                returnKeys = proc.MethodAttribute.ReturnKeys;
            }

            var function = new FunctionDescriptor(
                library,
                classNode.name,
                procName,
                arguments,
                proc.returntype.ToString(),
                type,
                isVisibleInLibrary,
                returnKeys,
                proc.isVarArg);

            AddImportedFunctions(library, new[] { function });
        }