Esempio n. 1
0
        public object Recall(ObjectHashtable hash)
        {
            //hash is an (object -> int) mapping, the same that was used while memoizing
            object[] objs = new object[memo.Length];
            foreach(object obj in hash.Keys)
                objs[(int)hash[obj]] = obj;
            for(int i=0; i<memo.Length; i++)
            {
                object obj = objs[i];
                if(obj == null)
                    continue;
                Type type = memo[i].type;
                if(!Equals(type, obj.GetType()))
                    throw new MemoException();
                if(Equals(type,typeof(string)))
                    continue;//strings are immutable...
                if(type.IsPrimitive)
                {
                    //DmUtils call?...
                }
                else if(type.IsArray)
                    RecallArray(obj as Array, i, objs);
                else
                    RecallObject(obj, i, objs);
            }

            return objs[1];
        }
Esempio n. 2
0
        private void Ctor(object obj, ObjectHashtable hash)
        {
            if (hash.Count != 0)
            {
                throw new MemoException();                 //XZ??
            }
            ArrayList heap = new ArrayList();

            heap.Add(new MemoObject(null, null));
            hash[null] = 0;
            Memo(obj, heap, hash, true);
            memo = new MemoObject[heap.Count];
            heap.CopyTo(memo);
            CalculateHashCode();
        }
Esempio n. 3
0
        public void Recall(MemoState memo, ObjectHashtable hash)
        {
            StateDecomposition dec = memo.Recall(hash) as StateDecomposition;

            this.stack.Clear();
            for (int i = dec.Stack.Length - 1; i >= 0; i--)
            {
                this.stack.Push(dec.Stack[i]);
            }

            int j = 0;

            foreach (Variable v in this.pool.GetVariables())
            {
                this.pool[v].Val = dec.Pool[j++].Val;
            }
        }
Esempio n. 4
0
        public MemoState Memorize(out ObjectHashtable hash)
        {
            hash = new ObjectHashtable();

            Value[] stack = new Value[this.stack.Count];
            for (int i = 0; i < this.stack.Count; i++)
            {
                stack[i] = this.stack[i];
            }

            int j = 0;

            Location[] pool = new Location[this.pool.GetVariables().Count];
            foreach (Variable v in this.pool.GetVariables())
            {
                pool[j++] = this.pool[v];
            }

            return(new MemoState(new StateDecomposition(stack, pool), hash));
        }
Esempio n. 5
0
        public object Recall(ObjectHashtable hash)
        {
            //hash is an (object -> int) mapping, the same that was used while memoizing
            object[] objs = new object[memo.Length];
            foreach (object obj in hash.Keys)
            {
                objs[(int)hash[obj]] = obj;
            }
            for (int i = 0; i < memo.Length; i++)
            {
                object obj = objs[i];
                if (obj == null)
                {
                    continue;
                }
                Type type = memo[i].type;
                if (!Equals(type, obj.GetType()))
                {
                    throw new MemoException();
                }
                if (Equals(type, typeof(string)))
                {
                    continue;                    //strings are immutable...
                }
                if (type.IsPrimitive)
                {
                    //DmUtils call?...
                }
                else if (type.IsArray)
                {
                    RecallArray(obj as Array, i, objs);
                }
                else
                {
                    RecallObject(obj, i, objs);
                }
            }

            return(objs[1]);
        }
Esempio n. 6
0
 internal Data(MemoSpecState memo, ObjectHashtable objHash, PointerToNode ptrUpNode)
 {
     this.MemoSpecState = memo;
     this.ObjectHashtable = objHash;
     this.PointerToNode = ptrUpNode;
 }
Esempio n. 7
0
        private BTType checkAnnotatedMethodForInvoke(AnnotatedMethod aMethod)
        {
            MethodBase sMethod = aMethod.SourceMethod;
            if (sMethod.DeclaringType.ToString() == "System.Object" &&
                sMethod.Name == ".ctor" &&
                sMethod.GetParameters().Length == 0)
            {
                BTValue obj = aMethod.ParamVals[0].Val as BTValue;
                return obj.BTType;
            }
            else if (this.holder.WhiteList.Contains(sMethod))
            {
                ObjectHashtable hash = new ObjectHashtable();
                for (int i = 0; i < aMethod.ParamVals.Count; i++)
                    getAllFieldBTValue(aMethod.ParamVals[i].Val as ReferenceBTValue, 5, hash);
                if (aMethod.ReturnValue != null)
                    getAllFieldBTValue(aMethod.ReturnValue, 5, hash);

                foreach (ReferenceBTValue val in hash.Keys)
                    if (val.BTType == BTType.Dynamic)
                        goto P;

                return BTType.Static;
            }

              P:

            Creators crtrs = new Creators();
            for (int i = 0; i < aMethod.ParamVals.Count; i++)
                crtrs.AddCreators((aMethod.ParamVals[i].Val as ReferenceBTValue).Lift());
            ReferenceBTValue ret = aMethod.ReturnValue;
            if (ret != null)
                crtrs.AddCreators(ret.Lift());

            if (! crtrs.IsEmpty)
                throw new LiftException(crtrs);

            return BTType.Dynamic;
        }
Esempio n. 8
0
 public MemoState(object obj, ObjectHashtable hash)
 {
     Ctor(obj, hash);
 }
Esempio n. 9
0
 internal PtrComparer(ObjectHashtable objHash)
 {
     this.objHash = objHash;
 }
Esempio n. 10
0
        private static int Memo(object obj, ArrayList heap, ObjectHashtable hash, bool addToHash)
        {
            int myIndex = heap.Count;

            heap.Add(null);           //Reserve some space for this object
            if (addToHash)            //addToHash == false, when obj is a boxed representation of some non-boxed value
            {
                hash[obj] = myIndex;
            }
            Type type = obj.GetType();

            if (typeof(Pointer).IsAssignableFrom(type))
            {
                throw new MemoException();                 //some mannaged wrapper for unmannaged data...
            }
            if (type.IsPrimitive)
            {
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                case TypeCode.Byte:
                case TypeCode.SByte:
                case TypeCode.Char:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                    SetElement(heap, myIndex, new MemoObject(type, (obj as IConvertible).ToInt64(null)));
                    return(myIndex);

                case TypeCode.Single:
                case TypeCode.Double:
                    SetElement(heap, myIndex, new MemoObject(type, (obj as IConvertible).ToDouble(null)));
                    return(myIndex);

                default:                         //IntPtr? UIntPtr?
                    if (type.Equals(typeof(IntPtr)))
                    {
                        SetElement(heap, myIndex, new MemoObject(type, ((IntPtr)obj).ToInt64()));
                        return(myIndex);
                    }
                    else if (type.Equals(typeof(UIntPtr)))
                    {
                        SetElement(heap, myIndex, new MemoObject(type, (long)((UIntPtr)obj).ToUInt64()));
                        return(myIndex);
                    }
                    else
                    {
                        throw new MemoException();
                    }
                }        //switch
            }            //if(IsPrimitive)
            int[] fieldIndices;
            if (Equals(type, typeof(string)))
            {
                string str = obj as string;
                fieldIndices = new int[str.Length];
                for (int i = 0; i < str.Length; i++)
                {
                    char fieldValue = str[i];
                    fieldIndices[i] = Memo(fieldValue, heap, hash, false);
                }
            }
            else if (type.IsArray)
            {
                Array array = obj as Array;
                if (array.Rank > 1 || array.GetLowerBound(0) != 0)
                {
                    throw new MemoException();                     //not supported yet...
                }
                fieldIndices = new int[array.Length];
                bool doAddToHash = !type.GetElementType().IsValueType;
                for (int i = 0; i < array.Length; i++)
                {
                    object fieldValue = array.GetValue(i);
                    if (doAddToHash)
                    {
                        object fieldIndexBoxed = hash[fieldValue];
                        if (fieldIndexBoxed != null)
                        {
                            fieldIndices[i] = (int)fieldIndexBoxed;
                            continue;
                        }
                    }
                    fieldIndices[i] = Memo(fieldValue, heap, hash, doAddToHash);
                }
            }
            else
            {
                //Custom object
                FieldInfo[] fieldInfos = ReflectionUtils.GetAllFields(type);
                fieldIndices = new int[fieldInfos.Length];
                for (int i = 0; i < fieldInfos.Length; i++)
                {
                    bool   doAddToHash = !fieldInfos[i].FieldType.IsValueType;
                    object fieldValue  = fieldInfos[i].GetValue(obj);
                    if (doAddToHash)
                    {
                        object fieldIndexBoxed = hash[fieldValue];
                        if (fieldIndexBoxed != null)
                        {
                            fieldIndices[i] = (int)fieldIndexBoxed;
                            continue;
                        }
                    }
                    fieldIndices[i] = Memo(fieldValue, heap, hash, doAddToHash);
                }
            }
            SetElement(heap, myIndex, new MemoObject(type, fieldIndices));
            return(myIndex);
        }
Esempio n. 11
0
 internal void Recall(MemoSpecState memo, ObjectHashtable objHash)
 {
     this.state.Recall(memo.MemoState, objHash);
 }
Esempio n. 12
0
        internal PointerValue[] GetPointers(ObjectHashtable objHash)
        {
            ArrayList ptrs = new ArrayList();
            foreach (PointerValue ptr in this.hash.Keys)
                if (objHash.ContainsKey(ptr.GetHeapObject()))
                    ptrs.Add(ptr);
            ptrs.Sort(new PtrComparer(objHash));

            return ptrs.ToArray(typeof(PointerValue)) as PointerValue[];
        }
Esempio n. 13
0
 private void addCreator(BTValue val, ReferenceCreator crtr)
 {
     ObjectHashtable hash = new ObjectHashtable();
     AnnotatingVisitor.getAllBTValue(val, hash);
     foreach (BTValue v in hash.Keys)
         v.Creators[this].AddCreator(crtr);
 }
Esempio n. 14
0
 private void Ctor(object obj, ObjectHashtable hash)
 {
     if(hash.Count != 0)
         throw new MemoException(); //XZ??
     ArrayList heap = new ArrayList();
     heap.Add(new MemoObject(null,null));
     hash[null] = 0;
     Memo(obj, heap, hash, true);
     memo = new MemoObject[heap.Count];
     heap.CopyTo(memo);
     CalculateHashCode();
 }
Esempio n. 15
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);
            }
        }
Esempio n. 16
0
 private static int Memo(object obj, ArrayList heap, ObjectHashtable hash, bool addToHash)
 {
     int myIndex = heap.Count;
     heap.Add(null); //Reserve some space for this object
     if(addToHash) //addToHash == false, when obj is a boxed representation of some non-boxed value
         hash[obj] = myIndex;
     Type type = obj.GetType();
     if(typeof(Pointer).IsAssignableFrom(type))
         throw new MemoException(); //some mannaged wrapper for unmannaged data...
     if(type.IsPrimitive)
     {
         switch(Type.GetTypeCode(type))
         {
             case TypeCode.Boolean:
             case TypeCode.Byte:
             case TypeCode.SByte:
             case TypeCode.Char:
             case TypeCode.Int16:
             case TypeCode.UInt16:
             case TypeCode.Int32:
             case TypeCode.UInt32:
             case TypeCode.Int64:
             case TypeCode.UInt64:
                 SetElement(heap , myIndex , new MemoObject(type, (obj as IConvertible).ToInt64(null)));
                 return(myIndex);
             case TypeCode.Single:
             case TypeCode.Double:
                 SetElement(heap , myIndex , new MemoObject(type, (obj as IConvertible).ToDouble(null)));
                 return(myIndex);
             default: //IntPtr? UIntPtr?
                 if(type.Equals(typeof(IntPtr)))
                 {
                     SetElement(heap , myIndex , new MemoObject(type, ((IntPtr)obj).ToInt64()));
                     return(myIndex);
                 }
                 else if(type.Equals(typeof(UIntPtr)))
                 {
                     SetElement(heap , myIndex , new MemoObject(type, (long)((UIntPtr)obj).ToUInt64()));
                     return(myIndex);
                 }
                 else
                     throw new MemoException();
         }//switch
     }//if(IsPrimitive)
     int[] fieldIndices;
     if(Equals(type,typeof(string)))
     {
         string str = obj as string;
         fieldIndices = new int[str.Length];
         for(int i=0; i<str.Length; i++)
         {
             char fieldValue = str[i];
             fieldIndices[i] = Memo(fieldValue, heap, hash, false);
         }
     }
     else if(type.IsArray)
     {
         Array array = obj as Array;
         if(array.Rank > 1 || array.GetLowerBound(0) != 0)
             throw new MemoException(); //not supported yet...
         fieldIndices = new int[array.Length];
         bool doAddToHash = ! type.GetElementType().IsValueType;
         for(int i=0; i<array.Length; i++)
         {
             object fieldValue = array.GetValue(i);
             if(doAddToHash)
             {
                 object fieldIndexBoxed = hash[fieldValue];
                 if(fieldIndexBoxed != null)
                 {
                     fieldIndices[i] = (int)fieldIndexBoxed;
                     continue;
                 }
             }
             fieldIndices[i] = Memo(fieldValue, heap, hash, doAddToHash);
         }
     }
     else
     {
         //Custom object
         FieldInfo[] fieldInfos = ReflectionUtils.GetAllFields(type);
         fieldIndices = new int[fieldInfos.Length];
         for(int i=0; i<fieldInfos.Length; i++)
         {
             bool doAddToHash = ! fieldInfos[i].FieldType.IsValueType;
             object fieldValue = fieldInfos[i].GetValue(obj);
             if(doAddToHash)
             {
                 object fieldIndexBoxed = hash[fieldValue];
                 if(fieldIndexBoxed != null)
                 {
                     fieldIndices[i] = (int)fieldIndexBoxed;
                     continue;
                 }
             }
             fieldIndices[i] = Memo(fieldValue, heap, hash, doAddToHash);
         }
     }
     SetElement(heap , myIndex , new MemoObject(type, fieldIndices));
     return(myIndex);
 }
Esempio n. 17
0
 public MemoState(object obj, ObjectHashtable hash)
 {
     Ctor(obj, hash);
 }
Esempio n. 18
0
        internal MemoSpecState Memorize(VariablesHashtable varsHash, out ObjectHashtable objHash)
        {
            MemoState memo = this.state.Memorize(out objHash);
            PointerValue[] ptrs = varsHash.GetPointers(objHash);

            Variable[] vars = new Variable[ptrs.Length];
            for (int i=0; i < vars.Length; i++)
                vars[i] = varsHash[ptrs[i] as PointerValue] as Variable;

            return new MemoSpecState(memo, vars);
        }
Esempio n. 19
0
 private static void getAllFieldBTValue(BTValue val, int depth, ObjectHashtable hash)
 {
     if (! hash.Contains(val) && depth > 0)
     {
         hash[val] = true;
         if (val is ReferenceBTValue)
             foreach (BTValue v in (val as ReferenceBTValue).GetAllFieldBTValues())
                 AnnotatingVisitor.getAllFieldBTValue(v, depth-1, hash);
     }
 }