Example #1
0
        public virtual VarVersionNode CreateNode(VarVersionPair ver)
        {
            VarVersionNode node;

            nodes.AddWithKey(node = new VarVersionNode(ver.var, ver.version), ver);
            return(node);
        }
Example #2
0
        public override bool Equals(object o)
        {
            if (o == this)
            {
                return(true);
            }
            if (!(o is VarVersionPair))
            {
                return(false);
            }
            VarVersionPair paar = (VarVersionPair)o;

            return(var == paar.var && version == paar.version);
        }
Example #3
0
        private bool CheckTypeExprent(Exprent exprent)
        {
            foreach (Exprent expr in exprent.GetAllExprents())
            {
                if (!CheckTypeExprent(expr))
                {
                    return(false);
                }
            }
            if (exprent.type == Exprent.Exprent_Const)
            {
                ConstExprent constExpr = (ConstExprent)exprent;
                if (constExpr.GetConstType().typeFamily <= ICodeConstants.Type_Family_Integer)
                {
                    // boolean or integer
                    VarVersionPair pair = new VarVersionPair(constExpr.id, -1);
                    if (!mapExprentMinTypes.ContainsKey(pair))
                    {
                        Sharpen.Collections.Put(mapExprentMinTypes, pair, constExpr.GetConstType());
                    }
                }
            }
            CheckTypesResult result = exprent.CheckExprTypeBounds();
            bool             res    = true;

            if (result != null)
            {
                foreach (CheckTypesResult.ExprentTypePair entry in result.GetLstMaxTypeExprents())
                {
                    if (entry.type.typeFamily != ICodeConstants.Type_Family_Object)
                    {
                        ChangeExprentType(entry.exprent, entry.type, 1);
                    }
                }
                foreach (CheckTypesResult.ExprentTypePair entry in result.GetLstMinTypeExprents())
                {
                    res &= ChangeExprentType(entry.exprent, entry.type, 0);
                }
            }
            return(res);
        }
Example #4
0
 public virtual void SetVarName(VarVersionPair pair, string name)
 {
     Sharpen.Collections.Put(mapVarNames, pair, name);
 }
Example #5
0
 public virtual string GetVarName(VarVersionPair pair)
 {
     return(mapVarNames == null ? null : mapVarNames.GetOrNull(pair));
 }
Example #6
0
 public virtual void SetVarType(VarVersionPair pair, VarType type)
 {
     varVersions.SetVarType(pair, type);
 }
Example #7
0
 public virtual VarType GetVarType(VarVersionPair pair)
 {
     return(varVersions == null ? null : varVersions.GetVarType(pair));
 }
Example #8
0
        private bool ChangeExprentType(Exprent exprent, VarType newType, int minMax)
        {
            bool res = true;

            switch (exprent.type)
            {
            case Exprent.Exprent_Const:
            {
                ConstExprent constExpr = (ConstExprent)exprent;
                VarType      constType = constExpr.GetConstType();
                if (newType.typeFamily > ICodeConstants.Type_Family_Integer || constType.typeFamily
                    > ICodeConstants.Type_Family_Integer)
                {
                    return(true);
                }
                else if (newType.typeFamily == ICodeConstants.Type_Family_Integer)
                {
                    VarType minInteger = new ConstExprent((int)constExpr.GetValue(), false, null).GetConstType
                                             ();
                    if (minInteger.IsStrictSuperset(newType))
                    {
                        newType = minInteger;
                    }
                }
                goto case Exprent.Exprent_Var;
            }

            case Exprent.Exprent_Var:
            {
                VarVersionPair pair = null;
                if (exprent.type == Exprent.Exprent_Const)
                {
                    pair = new VarVersionPair(((ConstExprent)exprent).id, -1);
                }
                else if (exprent.type == Exprent.Exprent_Var)
                {
                    pair = new VarVersionPair((VarExprent)exprent);
                }
                if (minMax == 0)
                {
                    // min
                    VarType currentMinType = mapExprentMinTypes.GetOrNull(pair);
                    VarType newMinType;
                    if (currentMinType == null || newType.typeFamily > currentMinType.typeFamily)
                    {
                        newMinType = newType;
                    }
                    else if (newType.typeFamily < currentMinType.typeFamily)
                    {
                        return(true);
                    }
                    else
                    {
                        newMinType = VarType.GetCommonSupertype(currentMinType, newType);
                    }
                    Sharpen.Collections.Put(mapExprentMinTypes, pair, newMinType);
                    if (exprent.type == Exprent.Exprent_Const)
                    {
                        ((ConstExprent)exprent).SetConstType(newMinType);
                    }
                    if (currentMinType != null && (newMinType.typeFamily > currentMinType.typeFamily ||
                                                   newMinType.IsStrictSuperset(currentMinType)))
                    {
                        return(false);
                    }
                }
                else
                {
                    // max
                    VarType currentMaxType = mapExprentMaxTypes.GetOrNull(pair);
                    VarType newMaxType;
                    if (currentMaxType == null || newType.typeFamily < currentMaxType.typeFamily)
                    {
                        newMaxType = newType;
                    }
                    else if (newType.typeFamily > currentMaxType.typeFamily)
                    {
                        return(true);
                    }
                    else
                    {
                        newMaxType = VarType.GetCommonMinType(currentMaxType, newType);
                    }
                    Sharpen.Collections.Put(mapExprentMaxTypes, pair, newMaxType);
                }
                break;
            }

            case Exprent.Exprent_Assignment:
            {
                return(ChangeExprentType(((AssignmentExprent)exprent).GetRight(), newType, minMax
                                         ));
            }

            case Exprent.Exprent_Function:
            {
                FunctionExprent func = (FunctionExprent)exprent;
                switch (func.GetFuncType())
                {
                case FunctionExprent.Function_Iif:
                {
                    // FIXME:
                    res = ChangeExprentType(func.GetLstOperands()[1], newType, minMax) & ChangeExprentType
                              (func.GetLstOperands()[2], newType, minMax);
                    break;
                }

                case FunctionExprent.Function_And:
                case FunctionExprent.Function_Or:
                case FunctionExprent.Function_Xor:
                {
                    res = ChangeExprentType(func.GetLstOperands()[0], newType, minMax) & ChangeExprentType
                              (func.GetLstOperands()[1], newType, minMax);
                    break;
                }
                }
                break;
            }
            }
            return(res);
        }
Example #9
0
        public virtual int GetVarFinal(VarVersionPair pair)
        {
            int?fin = typeProcessor.GetMapFinalVars().GetOrNullable(pair);

            return(fin == null ? VarTypeProcessor.Var_Final : fin.Value);
        }
Example #10
0
 public virtual void SetVarType(VarVersionPair pair, VarType type)
 {
     typeProcessor.SetVarType(pair, type);
 }
Example #11
0
 public virtual VarType GetVarType(VarVersionPair pair)
 {
     return(typeProcessor.GetVarType(pair));
 }
Example #12
0
        private void SetNewVarIndices(VarTypeProcessor typeProcessor, DirectGraph graph,
                                      VarVersionsProcessor previousVersionsProcessor)
        {
            Dictionary <VarVersionPair, VarType> mapExprentMaxTypes = typeProcessor.GetMapExprentMaxTypes
                                                                          ();
            Dictionary <VarVersionPair, VarType> mapExprentMinTypes = typeProcessor.GetMapExprentMinTypes
                                                                          ();
            Dictionary <VarVersionPair, int> mapFinalVars = typeProcessor.GetMapFinalVars();
            CounterContainer counters = DecompilerContext.GetCounterContainer();
            Dictionary <VarVersionPair, int> mapVarPaar = new Dictionary <VarVersionPair, int>
                                                              ();
            Dictionary <int, int> mapOriginalVarIndices = new Dictionary <int, int>();

            // map var-version pairs on new var indexes
            foreach (VarVersionPair pair in new List <VarVersionPair>(mapExprentMinTypes.Keys))
            {
                if (pair.version >= 0)
                {
                    int newIndex = pair.version == 1 ? pair.var : counters.GetCounterAndIncrement(CounterContainer
                                                                                                  .Var_Counter);
                    VarVersionPair newVar = new VarVersionPair(newIndex, 0);
                    Sharpen.Collections.Put(mapExprentMinTypes, newVar, mapExprentMinTypes.GetOrNull(
                                                pair));
                    Sharpen.Collections.Put(mapExprentMaxTypes, newVar, mapExprentMaxTypes.GetOrNull(
                                                pair));
                    if (mapFinalVars.ContainsKey(pair))
                    {
                        Sharpen.Collections.Put(mapFinalVars, newVar, Sharpen.Collections.Remove(mapFinalVars
                                                                                                 , pair));
                    }
                    Sharpen.Collections.Put(mapVarPaar, pair, newIndex);
                    Sharpen.Collections.Put(mapOriginalVarIndices, newIndex, pair.var);
                }
            }
            // set new vars
            graph.IterateExprents((Exprent exprent) => {
                List <Exprent> lst = exprent.GetAllExprents(true);
                lst.Add(exprent);
                foreach (Exprent expr in lst)
                {
                    if (expr.type == Exprent.Exprent_Var)
                    {
                        VarExprent newVar = (VarExprent)expr;
                        int?newVarIndex   = mapVarPaar.GetOrNullable(new VarVersionPair(newVar));
                        if (newVarIndex != null)
                        {
                            newVar.SetIndex(newVarIndex.Value);
                            newVar.SetVersion(0);
                        }
                    }
                    else if (expr.type == Exprent.Exprent_Const)
                    {
                        VarType maxType = mapExprentMaxTypes.GetOrNull(new VarVersionPair(expr.id, -1));
                        if (maxType != null && maxType.Equals(VarType.Vartype_Char))
                        {
                            ((ConstExprent)expr).SetConstType(maxType);
                        }
                    }
                }
                return(0);
            }
                                  );
            if (previousVersionsProcessor != null)
            {
                Dictionary <int, int> oldIndices = previousVersionsProcessor.GetMapOriginalVarIndices
                                                       ();
                this.mapOriginalVarIndices = new Dictionary <int, int>(mapOriginalVarIndices.Count
                                                                       );
                foreach (KeyValuePair <int, int> entry in mapOriginalVarIndices)
                {
                    int value    = entry.Value;
                    int?oldValue = oldIndices.GetOrNullable(value);
                    value = oldValue != null ? oldValue.Value : value;
                    Sharpen.Collections.Put(this.mapOriginalVarIndices, entry.Key, value);
                }
            }
            else
            {
                this.mapOriginalVarIndices = mapOriginalVarIndices;
            }
        }
Example #13
0
        private static void SimpleMerge(VarTypeProcessor typeProcessor, DirectGraph graph
                                        , StructMethod mt)
        {
            Dictionary <VarVersionPair, VarType> mapExprentMaxTypes = typeProcessor.GetMapExprentMaxTypes
                                                                          ();
            Dictionary <VarVersionPair, VarType> mapExprentMinTypes = typeProcessor.GetMapExprentMinTypes
                                                                          ();
            Dictionary <int, HashSet <int> > mapVarVersions = new Dictionary <int, HashSet <int> >
                                                                  ();

            foreach (VarVersionPair pair in mapExprentMinTypes.Keys)
            {
                if (pair.version >= 0)
                {
                    // don't merge constants
                    mapVarVersions.ComputeIfAbsent(pair.var, (int k) => new HashSet <int>()).Add(pair.
                                                                                                 version);
                }
            }
            bool is_method_static = mt.HasModifier(ICodeConstants.Acc_Static);
            Dictionary <VarVersionPair, int> mapMergedVersions = new Dictionary <VarVersionPair
                                                                                 , int>();

            foreach (KeyValuePair <int, HashSet <int> > ent in mapVarVersions)
            {
                if (ent.Value.Count > 1)
                {
                    List <int> lstVersions = new List <int>(ent.Value);
                    lstVersions.Sort();
                    for (int i = 0; i < lstVersions.Count; i++)
                    {
                        VarVersionPair firstPair = new VarVersionPair(ent.Key, lstVersions[i]);
                        VarType        firstType = mapExprentMinTypes.GetOrNull(firstPair);
                        if (firstPair.var == 0 && firstPair.version == 1 && !is_method_static)
                        {
                            continue;
                        }
                        // don't merge 'this' variable
                        for (int j = i + 1; j < lstVersions.Count; j++)
                        {
                            VarVersionPair secondPair = new VarVersionPair(ent.Key, lstVersions[j]);
                            VarType        secondType = mapExprentMinTypes.GetOrNull(secondPair);
                            if (firstType.Equals(secondType) || (firstType.Equals(VarType.Vartype_Null) && secondType
                                                                 .type == ICodeConstants.Type_Object) || (secondType.Equals(VarType.Vartype_Null) &&
                                                                                                          firstType.type == ICodeConstants.Type_Object))
                            {
                                VarType firstMaxType  = mapExprentMaxTypes.GetOrNull(firstPair);
                                VarType secondMaxType = mapExprentMaxTypes.GetOrNull(secondPair);
                                VarType type          = firstMaxType == null ? secondMaxType : secondMaxType == null ? firstMaxType
                                                                         : VarType.GetCommonMinType(firstMaxType, secondMaxType);
                                Sharpen.Collections.Put(mapExprentMaxTypes, firstPair, type);
                                Sharpen.Collections.Put(mapMergedVersions, secondPair, firstPair.version);
                                Sharpen.Collections.Remove(mapExprentMaxTypes, secondPair);
                                Sharpen.Collections.Remove(mapExprentMinTypes, secondPair);
                                if (firstType.Equals(VarType.Vartype_Null))
                                {
                                    Sharpen.Collections.Put(mapExprentMinTypes, firstPair, secondType);
                                    firstType = secondType;
                                }
                                Sharpen.Collections.Put(typeProcessor.GetMapFinalVars(), firstPair, VarTypeProcessor
                                                        .Var_Non_Final);
                                lstVersions.RemoveAtReturningValue(j);
                                //noinspection AssignmentToForLoopParameter
                                j--;
                            }
                        }
                    }
                }
            }
            if (!(mapMergedVersions.Count == 0))
            {
                UpdateVersions(graph, mapMergedVersions);
            }
        }
Example #14
0
 public virtual VarType GetVarType(VarVersionPair pair)
 {
     return(mapExprentMinTypes.GetOrNull(pair));
 }
Example #15
0
 public virtual void SetVarType(VarVersionPair pair, VarType type)
 {
     Sharpen.Collections.Put(mapExprentMinTypes, pair, type);
 }
Example #16
0
 public virtual int GetVarFinal(VarVersionPair pair)
 {
     return(varVersions == null ? VarTypeProcessor.Var_Final : varVersions.GetVarFinal
                (pair));
 }
Example #17
0
 public virtual void SetVarFinal(VarVersionPair pair, int finalType)
 {
     varVersions.SetVarFinal(pair, finalType);
 }
Example #18
0
 public virtual void SetVarFinal(VarVersionPair pair, int finalType)
 {
     Sharpen.Collections.Put(typeProcessor.GetMapFinalVars(), pair, finalType);
 }