Ejemplo n.º 1
0
        protected override void VisitStoreElement(StoreElement downNode, object o)
        {
            StructValue          idx = this.state.Stack.Pop() as StructValue;
            ObjectReferenceValue arr = this.state.Stack.Pop() as ObjectReferenceValue;
            PointerValue         ptr = new PointerToElementValue(arr.Obj as Array, SpecializingVisitor.toInt(idx));

            this.storeVar(downNode, ptr, o);
        }
Ejemplo n.º 2
0
        protected override void DispatchNode(Node downNode, object o)
        {
            Data          data      = o as Data;
            MemoSpecState memo      = data.MemoSpecState;
            PointerToNode ptrUpNode = data.PointerToNode;

            this.state.Recall(memo, data.ObjectHashtable);

            BTType btType = Annotation.GetNodeBTType(downNode);

            if (btType == BTType.Static)
            {
                Node nextDownNode = this.state.InterpretNode(downNode);
                this.AddTask(nextDownNode, ptrUpNode);
            }
            else
            {
                Variable[] vars1;
                Node       upNode;
                Variable[] vars2;
                this.getUpNode(downNode, memo, out vars1, out upNode, out vars2);
                if (upNode != null)
                {
                    SpecializingVisitor.createSubstitution(vars2, vars1, ptrUpNode, upNode);
                }
                else if (btType == BTType.eXclusive)
                {
                    this.CallVisitorMethod(downNode, data);
                }
                else
                {
                    upNode = ptrUpNode.Node = downNode.Clone();
                    for (int i = 0; i < downNode.NextArray.Count; i++)
                    {
                        this.AddTask(downNode.NextArray[i], new PointerToNode(upNode, i));
                    }
                }
            }

            if (ptrUpNode.Node != null)
            {
                this.setUpAndDownNode(downNode, memo, ptrUpNode.Node);
            }
        }
Ejemplo n.º 3
0
        protected override void VisitNewObject(NewObject downNode, object o)
        {
            PointerToNode   ptrUpNode = (o as Data).PointerToNode;
            AnnotatedMethod method    = Annotation.GetAnnotatedMethod(downNode);

            Node      upNode;
            ArrayList list = new ArrayList();

            for (int i = method.ParamVals.Count - 1; i > 0; i--)
            {
                if (Annotation.GetValueBTType(method.ParamVals[i].Val) == BTType.Static)
                {
                    list.Add(this.state.Stack.Pop());
                }
            }
            if (Annotation.GetValueBTType(method.ParamVals[0].Val) == BTType.Static)
            {
                Type objtype                  = downNode.Constructor.DeclaringType;
                ObjectReferenceValue obj      = new ObjectReferenceValue(FormatterServices.GetUninitializedObject(objtype));
                FieldInfo[]          fldInfos = ReflectionUtils.GetAllFields(objtype);
                BTType[]             btTypes  = Annotation.GetObjectFieldBTTypes(downNode);
                for (int i = 0; i < fldInfos.Length; i++)
                {
                    if (btTypes[i] == BTType.Dynamic)
                    {
                        Variable varUp = this.mbbUp.Variables.CreateVar(fldInfos[i].FieldType, VariableKind.Local);
                        this.varsHash[new PointerToObjectFieldValue(obj.Obj, fldInfos[i])] = varUp;
                        ptrUpNode = SpecializingVisitor.initVariable(varUp, ptrUpNode);
                    }
                }
                list.Add(obj);
                this.state.Stack.Push(obj);
                upNode = new CallMethod(downNode.Constructor, false, false);
            }
            else
            {
                upNode = new NewObject(downNode.Constructor);
            }
            list.Reverse();
            Value[] args = list.ToArray(typeof(Value)) as Value[];

            this.callMethod(downNode, method, ptrUpNode, upNode, args);
        }
Ejemplo n.º 4
0
        protected override void VisitNewArray(NewArray downNode, object o)
        {
            PointerToNode        ptrUpNode = (o as Data).PointerToNode;
            StructValue          idx       = this.state.Stack.Pop() as StructValue;
            Array                arr       = Array.CreateInstance(downNode.Type, SpecializingVisitor.toInt(idx));
            ObjectReferenceValue obj       = new ObjectReferenceValue(arr);

            if (Annotation.GetArrayElementsBTType(downNode) == BTType.Dynamic)
            {
                for (int i = 0; i < SpecializingVisitor.toInt(idx); i++)
                {
                    Variable varUp = this.mbbUp.Variables.CreateVar(downNode.Type, VariableKind.Local);
                    this.varsHash[new PointerToElementValue(arr, i)] = varUp;
                    ptrUpNode = SpecializingVisitor.initVariable(varUp, ptrUpNode);
                }
            }

            this.state.Stack.Push(obj);
            this.AddTask(downNode.Next, ptrUpNode);
        }
Ejemplo n.º 5
0
        protected override void VisitLift(Lift downNode, object o)
        {
            PointerToNode ptrUpNode = (o as Data).PointerToNode;
            LiftTask      task      = downNode.Task;

            if (task is StackLiftTask)
            {
                int depth = (task as StackLiftTask).Depth;
                ptrUpNode = new PointerToNode(ptrUpNode.Node = SpecializingVisitor.liftValue(this.state.Stack[depth]));
                this.state.Stack.RemoveAt(depth);
            }
            else
            {
                Variable var = (task as VariableLiftTask).Variable;
                ptrUpNode = new PointerToNode(ptrUpNode.Node = SpecializingVisitor.liftValue(this.state.Pool[var].Val));
                PointerValue ptr = new PointerToLocationValue(this.state.Pool[var]);
                ptrUpNode = new PointerToNode(ptrUpNode.Node = new StoreVar(this.varsHash[ptr]));
                ptr.SetZeroValue();
            }
            this.AddTask(downNode.Next, ptrUpNode);
        }
Ejemplo n.º 6
0
        internal static void SpecializeMethod(ResidualAssemblyHolder holder, ResidualMethod method)
        {
            Value[] args = method.Arguments;
            PointerValue[] ptrs = method.Pointers;
            MethodBodyBlock mbbDown = holder.AnnotatedHolder[method.AnnotatedMethod];
            MethodBodyBlock mbbUp = new MethodBodyBlock(mbbDown.ReturnType);
            holder.AddMethod(method, mbbUp);

            SpecState state = new SpecState(mbbDown.Variables.Count);
            VariablesHashtable varsHash = new VariablesHashtable();

            int varCount = 0;
            int argCount = 0;
            foreach (Variable varDown in mbbDown.Variables.ParameterMapper)
            {
                state.Pool[varDown] = new Location(varDown.Type);
                Variable varUp;
                if (Annotation.GetValueBTType(method.AnnotatedMethod.ParamVals[varCount++].Val) == BTType.Static)
                {
                    state.Pool[varDown].Val = args[argCount++];
                    varUp = mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                }
                else
                    varUp = mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Parameter);
                varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;
            }
            foreach (Variable varDown in mbbDown.Variables)
                if (! state.Pool.ContainsVar(varDown))
                {
                    state.Pool[varDown] = new Location(varDown.Type);
                    Variable varUp = mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                    varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;
                }

            PointerToNode ptrUpNode = new PointerToNode(mbbUp);
            Node dummyUp = new DummyNode(mbbUp);
            Node upNode = dummyUp;
            for (int i = 0; i < ptrs.Length; i++)
            {
                Type ptrType = ptrs[i].Type;
                Type type = ptrType.GetElementType();
                Variable newVar1 = mbbUp.Variables.CreateVar(ptrType, VariableKind.Parameter);
                Variable newVar2 = mbbUp.Variables.CreateVar(type, VariableKind.Local);
                varsHash[ptrs[i]] = newVar2;

                ptrUpNode = new PointerToNode(ptrUpNode.Node = new LoadVar(newVar1));
                ptrUpNode = new PointerToNode(ptrUpNode.Node = new LoadIndirect(type));
                ptrUpNode = new PointerToNode(ptrUpNode.Node = new StoreVar(newVar2));

                upNode = upNode.Next = new LoadVar(newVar1);
                upNode = upNode.Next = new LoadVar(newVar2);
                upNode = upNode.Next = new StoreIndirect(type);
            }
            upNode.Next = new Leave();
            upNode = dummyUp.Next;
            dummyUp.RemoveFromGraph();

            GraphProcessor graphProc = new GraphProcessor();
            SpecializingVisitor visitor = new SpecializingVisitor(graphProc, holder, mbbUp, state, varsHash);
            visitor.AddTask(mbbDown.Next, ptrUpNode);
            graphProc.Process();
            visitor.SetLastNode(upNode);
        }
Ejemplo n.º 7
0
        private void callMethod(Node downNode, AnnotatedMethod method, PointerToNode ptrUpNode, Node upNode, Value[] args)
        {
            if (method.SourceMethod.IsDefined(typeof(InlineAttribute), false) && ! (upNode is NewObject))
            {
                MethodBodyBlock mbbDown = this.holder.AnnotatedHolder[method];
                SpecState state = new SpecState(mbbDown.Variables.Count);

                int varCount = 0;
                int argCount = 0;
                foreach (Variable varDown in mbbDown.Variables.ParameterMapper)
                {
                    state.Pool[varDown] = new Location(varDown.Type);
                    Variable varUp = this.mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                    this.varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;

                    if (Annotation.GetValueBTType(method.ParamVals[varCount++].Val) == BTType.Static)
                    {
                        state.Pool[varDown].Val = args[argCount];
                        state.Stack.Push(args[argCount++]);
                    }
                    else
                    {
                        Node upNext = new StoreVar(varUp);
                        Node upPrevNext = ptrUpNode.Node;
                        ptrUpNode.Node = upNext;
                        upNext.Next = upPrevNext;
                    }
                }
                while (ptrUpNode.Node != null)
                    ptrUpNode = new PointerToNode(ptrUpNode.Node);
                foreach (Variable varDown in mbbDown.Variables)
                    if (! state.Pool.ContainsVar(varDown))
                    {
                        state.Pool[varDown] = new Location(varDown.Type);
                        Variable varUp = this.mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                        this.varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;
                        ptrUpNode = SpecializingVisitor.initVariable(varUp, ptrUpNode);
                    }

                int depth = state.Stack.Count + 1;

                GraphProcessor graphProc = new GraphProcessor();
                SpecializingVisitor visitor = new SpecializingVisitor(graphProc, this.holder, this.mbbUp, state, this.varsHash);
                visitor.AddTask(mbbDown.Next, ptrUpNode);
                graphProc.Process();

                foreach (Data newData in visitor.exitData)
                {
                    state.Recall(newData.MemoSpecState, newData.ObjectHashtable);
                    if (state.Stack.Count == depth)
                        this.state.Stack.Push(state.Stack.Pop());
                    this.AddTask(downNode.Next, newData.PointerToNode);
                }
            }
            else
            {
                ObjectHashtable objHash = new ObjectHashtable();
                MemoState memoArgs = new MemoState(args, objHash);
                PointerValue[] ptrs = this.varsHash.GetPointers(objHash);

                for (int i = 0; i < ptrs.Length; i++)
                    ptrUpNode = new PointerToNode(ptrUpNode.Node = new LoadVarAddr(this.varsHash[ptrs[i]]));
                ptrUpNode = new PointerToNode(ptrUpNode.Node = upNode);

                ResidualMethod callMethod = new ResidualMethod(method, memoArgs, args, ptrs);
                Specialization.SetResidualMethod(upNode, callMethod);
                this.holder.SpecializeMethod(callMethod);

                this.AddTask(downNode.Next, ptrUpNode);
            }
        }
Ejemplo n.º 8
0
        internal static void SpecializeMethod(ResidualAssemblyHolder holder, ResidualMethod method)
        {
            Value[]         args    = method.Arguments;
            PointerValue[]  ptrs    = method.Pointers;
            MethodBodyBlock mbbDown = holder.AnnotatedHolder[method.AnnotatedMethod];
            MethodBodyBlock mbbUp   = new MethodBodyBlock(mbbDown.ReturnType);

            holder.AddMethod(method, mbbUp);

            SpecState          state    = new SpecState(mbbDown.Variables.Count);
            VariablesHashtable varsHash = new VariablesHashtable();

            int varCount = 0;
            int argCount = 0;

            foreach (Variable varDown in mbbDown.Variables.ParameterMapper)
            {
                state.Pool[varDown] = new Location(varDown.Type);
                Variable varUp;
                if (Annotation.GetValueBTType(method.AnnotatedMethod.ParamVals[varCount++].Val) == BTType.Static)
                {
                    state.Pool[varDown].Val = args[argCount++];
                    varUp = mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                }
                else
                {
                    varUp = mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Parameter);
                }
                varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;
            }
            foreach (Variable varDown in mbbDown.Variables)
            {
                if (!state.Pool.ContainsVar(varDown))
                {
                    state.Pool[varDown] = new Location(varDown.Type);
                    Variable varUp = mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                    varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;
                }
            }

            PointerToNode ptrUpNode = new PointerToNode(mbbUp);
            Node          dummyUp   = new DummyNode(mbbUp);
            Node          upNode    = dummyUp;

            for (int i = 0; i < ptrs.Length; i++)
            {
                Type     ptrType = ptrs[i].Type;
                Type     type    = ptrType.GetElementType();
                Variable newVar1 = mbbUp.Variables.CreateVar(ptrType, VariableKind.Parameter);
                Variable newVar2 = mbbUp.Variables.CreateVar(type, VariableKind.Local);
                varsHash[ptrs[i]] = newVar2;

                ptrUpNode = new PointerToNode(ptrUpNode.Node = new LoadVar(newVar1));
                ptrUpNode = new PointerToNode(ptrUpNode.Node = new LoadIndirect(type));
                ptrUpNode = new PointerToNode(ptrUpNode.Node = new StoreVar(newVar2));

                upNode = upNode.Next = new LoadVar(newVar1);
                upNode = upNode.Next = new LoadVar(newVar2);
                upNode = upNode.Next = new StoreIndirect(type);
            }
            upNode.Next = new Leave();
            upNode      = dummyUp.Next;
            dummyUp.RemoveFromGraph();

            GraphProcessor      graphProc = new GraphProcessor();
            SpecializingVisitor visitor   = new SpecializingVisitor(graphProc, holder, mbbUp, state, varsHash);

            visitor.AddTask(mbbDown.Next, ptrUpNode);
            graphProc.Process();
            visitor.SetLastNode(upNode);
        }
Ejemplo n.º 9
0
        private void callMethod(Node downNode, AnnotatedMethod method, PointerToNode ptrUpNode, Node upNode, Value[] args)
        {
            if (method.SourceMethod.IsDefined(typeof(InlineAttribute), false) && !(upNode is NewObject))
            {
                MethodBodyBlock mbbDown = this.holder.AnnotatedHolder[method];
                SpecState       state   = new SpecState(mbbDown.Variables.Count);

                int varCount = 0;
                int argCount = 0;
                foreach (Variable varDown in mbbDown.Variables.ParameterMapper)
                {
                    state.Pool[varDown] = new Location(varDown.Type);
                    Variable varUp = this.mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                    this.varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;

                    if (Annotation.GetValueBTType(method.ParamVals[varCount++].Val) == BTType.Static)
                    {
                        state.Pool[varDown].Val = args[argCount];
                        state.Stack.Push(args[argCount++]);
                    }
                    else
                    {
                        Node upNext     = new StoreVar(varUp);
                        Node upPrevNext = ptrUpNode.Node;
                        ptrUpNode.Node = upNext;
                        upNext.Next    = upPrevNext;
                    }
                }
                while (ptrUpNode.Node != null)
                {
                    ptrUpNode = new PointerToNode(ptrUpNode.Node);
                }
                foreach (Variable varDown in mbbDown.Variables)
                {
                    if (!state.Pool.ContainsVar(varDown))
                    {
                        state.Pool[varDown] = new Location(varDown.Type);
                        Variable varUp = this.mbbUp.Variables.CreateVar(varDown.Type, VariableKind.Local);
                        this.varsHash[new PointerToLocationValue(state.Pool[varDown])] = varUp;
                        ptrUpNode = SpecializingVisitor.initVariable(varUp, ptrUpNode);
                    }
                }

                int depth = state.Stack.Count + 1;

                GraphProcessor      graphProc = new GraphProcessor();
                SpecializingVisitor visitor   = new SpecializingVisitor(graphProc, this.holder, this.mbbUp, state, this.varsHash);
                visitor.AddTask(mbbDown.Next, ptrUpNode);
                graphProc.Process();

                foreach (Data newData in visitor.exitData)
                {
                    state.Recall(newData.MemoSpecState, newData.ObjectHashtable);
                    if (state.Stack.Count == depth)
                    {
                        this.state.Stack.Push(state.Stack.Pop());
                    }
                    this.AddTask(downNode.Next, newData.PointerToNode);
                }
            }
            else
            {
                ObjectHashtable objHash  = new ObjectHashtable();
                MemoState       memoArgs = new MemoState(args, objHash);
                PointerValue[]  ptrs     = this.varsHash.GetPointers(objHash);

                for (int i = 0; i < ptrs.Length; i++)
                {
                    ptrUpNode = new PointerToNode(ptrUpNode.Node = new LoadVarAddr(this.varsHash[ptrs[i]]));
                }
                ptrUpNode = new PointerToNode(ptrUpNode.Node = upNode);

                ResidualMethod callMethod = new ResidualMethod(method, memoArgs, args, ptrs);
                Specialization.SetResidualMethod(upNode, callMethod);
                this.holder.SpecializeMethod(callMethod);

                this.AddTask(downNode.Next, ptrUpNode);
            }
        }