예제 #1
0
 private static bool IsAssertionField(Exprent exprent, string classname, string key
                                      , bool throwInIf)
 {
     if (throwInIf)
     {
         if (exprent.type == Exprent.Exprent_Function)
         {
             FunctionExprent fparam = (FunctionExprent)exprent;
             if (fparam.GetFuncType() == FunctionExprent.Function_Bool_Not && fparam.GetLstOperands
                     ()[0].type == Exprent.Exprent_Field)
             {
                 FieldExprent fdparam = (FieldExprent)fparam.GetLstOperands()[0];
                 return(classname.Equals(fdparam.GetClassname()) && key.Equals(InterpreterUtil.MakeUniqueKey
                                                                                   (fdparam.GetName(), fdparam.GetDescriptor().descriptorString)));
             }
         }
     }
     else if (exprent.type == Exprent.Exprent_Field)
     {
         FieldExprent fdparam = (FieldExprent)exprent;
         return(classname.Equals(fdparam.GetClassname()) && key.Equals(InterpreterUtil.MakeUniqueKey
                                                                           (fdparam.GetName(), fdparam.GetDescriptor().descriptorString)));
     }
     return(false);
 }
예제 #2
0
        private static bool IsExprentIndependent(Exprent exprent, MethodWrapper method)
        {
            List <Exprent> lst = exprent.GetAllExprents(true);

            lst.Add(exprent);
            foreach (Exprent expr in lst)
            {
                switch (expr.type)
                {
                case Exprent.Exprent_Var:
                {
                    VarVersionPair varPair = new VarVersionPair((VarExprent)expr);
                    if (!method.varproc.GetExternalVars().Contains(varPair))
                    {
                        string varName = method.varproc.GetVarName(varPair);
                        if (!varName.Equals("this") && !varName.EndsWith(".this"))
                        {
                            // FIXME: remove direct comparison with strings
                            return(false);
                        }
                    }
                    break;
                }

                case Exprent.Exprent_Field:
                {
                    return(false);
                }
                }
            }
            return(true);
        }
예제 #3
0
 private static void HideEmptySuper(ClassWrapper wrapper)
 {
     foreach (MethodWrapper method in wrapper.GetMethods())
     {
         if (ICodeConstants.Init_Name.Equals(method.methodStruct.GetName()) && method.root
             != null)
         {
             Statement firstData = Statements.FindFirstData(method.root);
             if (firstData == null || (firstData.GetExprents().Count == 0))
             {
                 return;
             }
             Exprent exprent = firstData.GetExprents()[0];
             if (exprent.type == Exprent.Exprent_Invocation)
             {
                 InvocationExprent invExpr = (InvocationExprent)exprent;
                 if (Statements.IsInvocationInitConstructor(invExpr, method, wrapper, false) && (invExpr
                                                                                                 .GetLstParameters().Count == 0))
                 {
                     firstData.GetExprents().RemoveAtReturningValue(0);
                 }
             }
         }
     }
 }
예제 #4
0
        private bool ReplaceInvocations(ClassesProcessor.ClassNode caller, MethodWrapper
                                        meth, Exprent exprent)
        {
            bool res = false;

            foreach (Exprent expr in exprent.GetAllExprents())
            {
                res |= ReplaceInvocations(caller, meth, expr);
            }
            while (true)
            {
                bool found = false;
                foreach (Exprent expr in exprent.GetAllExprents())
                {
                    if (expr.type == Exprent.Exprent_Invocation)
                    {
                        Exprent newexpr = ReplaceAccessExprent(caller, meth, (InvocationExprent)expr);
                        if (newexpr != null)
                        {
                            exprent.ReplaceExprent(expr, newexpr);
                            found = true;
                            res   = true;
                            break;
                        }
                    }
                }
                if (!found)
                {
                    break;
                }
            }
            return(res);
        }
 private static string IsClass14Invocation(Exprent exprent, ClassWrapper wrapper,
                                           MethodWrapper meth)
 {
     if (exprent.type == Exprent.Exprent_Function)
     {
         FunctionExprent fexpr = (FunctionExprent)exprent;
         if (fexpr.GetFuncType() == FunctionExprent.Function_Iif)
         {
             if (fexpr.GetLstOperands()[0].type == Exprent.Exprent_Function)
             {
                 FunctionExprent headexpr = (FunctionExprent)fexpr.GetLstOperands()[0];
                 if (headexpr.GetFuncType() == FunctionExprent.Function_Eq)
                 {
                     if (headexpr.GetLstOperands()[0].type == Exprent.Exprent_Field && headexpr.GetLstOperands
                             ()[1].type == Exprent.Exprent_Const && ((ConstExprent)headexpr.GetLstOperands()[
                                                                         1]).GetConstType().Equals(VarType.Vartype_Null))
                     {
                         FieldExprent field = (FieldExprent)headexpr.GetLstOperands()[0];
                         ClassesProcessor.ClassNode fieldnode = DecompilerContext.GetClassProcessor().GetMapRootClasses
                                                                    ().GetOrNull(field.GetClassname());
                         if (fieldnode != null && fieldnode.classStruct.qualifiedName.Equals(wrapper.GetClassStruct
                                                                                                 ().qualifiedName))
                         {
                             // source class
                             StructField fd = wrapper.GetClassStruct().GetField(field.GetName(), field.GetDescriptor
                                                                                    ().descriptorString);
                             // FIXME: can be null! why??
                             if (fd != null && fd.HasModifier(ICodeConstants.Acc_Static) && (fd.IsSynthetic() ||
                                                                                             DecompilerContext.GetOption(IFernflowerPreferences.Synthetic_Not_Set)))
                             {
                                 if (fexpr.GetLstOperands()[1].type == Exprent.Exprent_Assignment && fexpr.GetLstOperands
                                         ()[2].Equals(field))
                                 {
                                     AssignmentExprent asexpr = (AssignmentExprent)fexpr.GetLstOperands()[1];
                                     if (asexpr.GetLeft().Equals(field) && asexpr.GetRight().type == Exprent.Exprent_Invocation)
                                     {
                                         InvocationExprent invexpr = (InvocationExprent)asexpr.GetRight();
                                         if (invexpr.GetClassname().Equals(wrapper.GetClassStruct().qualifiedName) && invexpr
                                             .GetName().Equals(meth.methodStruct.GetName()) && invexpr.GetStringDescriptor().
                                             Equals(meth.methodStruct.GetDescriptor()))
                                         {
                                             if (invexpr.GetLstParameters()[0].type == Exprent.Exprent_Const)
                                             {
                                                 wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(fd.GetName(), fd.GetDescriptor
                                                                                                                  ()));
                                                 // hide synthetic field
                                                 return(((ConstExprent)invexpr.GetLstParameters()[0]).GetValue().ToString());
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(null);
 }
예제 #6
0
 private static bool IsQualifiedNewGetClass(Exprent first, Exprent second)
 {
     if (first.type == Exprent.Exprent_Invocation)
     {
         InvocationExprent invocation = (InvocationExprent)first;
         if (!invocation.IsStatic() && invocation.GetInstance().type == Exprent.Exprent_Var &&
             invocation.GetName().Equals("getClass") && invocation.GetStringDescriptor().
             Equals("()Ljava/lang/Class;"))
         {
             List <Exprent> lstExprents = second.GetAllExprents();
             lstExprents.Add(second);
             foreach (Exprent expr in lstExprents)
             {
                 if (expr.type == Exprent.Exprent_New)
                 {
                     NewExprent newExpr = (NewExprent)expr;
                     if (newExpr.GetConstructor() != null && !(newExpr.GetConstructor().GetLstParameters
                                                                   ().Count == 0) && newExpr.GetConstructor().GetLstParameters()[0].Equals(invocation
                                                                                                                                           .GetInstance()))
                     {
                         string classname = newExpr.GetNewType().value;
                         ClassesProcessor.ClassNode node = DecompilerContext.GetClassProcessor().GetMapRootClasses
                                                               ().GetOrNull(classname);
                         if (node != null && node.type != ClassesProcessor.ClassNode.Class_Root)
                         {
                             return(true);
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }
예제 #7
0
 private static bool IsStackAssignment(Exprent first, Exprent second)
 {
     if (first.type == Exprent.Exprent_Assignment && second.type == Exprent.Exprent_Assignment)
     {
         AssignmentExprent asf = (AssignmentExprent)first;
         AssignmentExprent ass = (AssignmentExprent)second;
         while (true)
         {
             if (asf.GetRight().Equals(ass.GetRight()))
             {
                 if ((asf.GetLeft().type == Exprent.Exprent_Var && ((VarExprent)asf.GetLeft()).IsStack
                          ()) && (ass.GetLeft().type != Exprent.Exprent_Var || !((VarExprent)ass.GetLeft()
                                                                                 ).IsStack()))
                 {
                     if (!ass.GetLeft().ContainsExprent(asf.GetLeft()))
                     {
                         asf.SetRight(ass);
                         return(true);
                     }
                 }
             }
             if (asf.GetRight().type == Exprent.Exprent_Assignment)
             {
                 asf = (AssignmentExprent)asf.GetRight();
             }
             else
             {
                 break;
             }
         }
     }
     return(false);
 }
예제 #8
0
        private static Exprent IsLambda(Exprent exprent, StructClass cl)
        {
            List <Exprent> lst = exprent.GetAllExprents();

            foreach (Exprent expr in lst)
            {
                Exprent ret = IsLambda(expr, cl);
                if (ret != null)
                {
                    exprent.ReplaceExprent(expr, ret);
                }
            }
            if (exprent.type == Exprent.Exprent_Invocation)
            {
                InvocationExprent @in = (InvocationExprent)exprent;
                if (@in.GetInvocationTyp() == InvocationExprent.Invoke_Dynamic)
                {
                    string lambda_class_name = cl.qualifiedName + @in.GetInvokeDynamicClassSuffix();
                    ClassesProcessor.ClassNode lambda_class = DecompilerContext.GetClassProcessor().GetMapRootClasses
                                                                  ().GetOrNull(lambda_class_name);
                    if (lambda_class != null)
                    {
                        // real lambda class found, replace invocation with an anonymous class
                        NewExprent newExpr = new NewExprent(new VarType(lambda_class_name, true), null, 0
                                                            , @in.bytecode);
                        newExpr.SetConstructor(@in);
                        // note: we don't set the instance to null with in.setInstance(null) like it is done for a common constructor invocation
                        // lambda can also be a reference to a virtual method (e.g. String x; ...(x::toString);)
                        // in this case instance will hold the corresponding object
                        return(newExpr);
                    }
                }
            }
            return(null);
        }
예제 #9
0
        private static Exprent IsSimpleConstructorInvocation(Exprent exprent)
        {
            List <Exprent> lst = exprent.GetAllExprents();

            foreach (Exprent expr in lst)
            {
                Exprent ret = IsSimpleConstructorInvocation(expr);
                if (ret != null)
                {
                    exprent.ReplaceExprent(expr, ret);
                }
            }
            if (exprent.type == Exprent.Exprent_Invocation)
            {
                InvocationExprent @in = (InvocationExprent)exprent;
                if (@in.GetFunctype() == InvocationExprent.Typ_Init && @in.GetInstance().type ==
                    Exprent.Exprent_New)
                {
                    NewExprent newExpr = (NewExprent)@in.GetInstance();
                    newExpr.SetConstructor(@in);
                    @in.SetInstance(null);
                    return(newExpr);
                }
            }
            return(null);
        }
예제 #10
0
 public override void ReplaceExprent(Exprent oldexpr, Exprent newexpr)
 {
     if (headexprent[0] == oldexpr)
     {
         headexprent[0] = newexpr;
     }
 }
        private static bool ReplaceInvocations(Exprent exprent, ClassWrapper wrapper, MethodWrapper
                                               meth)
        {
            bool res = false;

            while (true)
            {
                bool found = false;
                foreach (Exprent expr in exprent.GetAllExprents())
                {
                    string cl = IsClass14Invocation(expr, wrapper, meth);
                    if (cl != null)
                    {
                        exprent.ReplaceExprent(expr, new ConstExprent(VarType.Vartype_Class, cl.Replace('.'
                                                                                                        , '/'), expr.bytecode));
                        found = true;
                        res   = true;
                        break;
                    }
                    res |= ReplaceInvocations(expr, wrapper, meth);
                }
                if (!found)
                {
                    break;
                }
            }
            return(res);
        }
예제 #12
0
        public static void RemoveRedundantReturns(RootStatement root)
        {
            DummyExitStatement dummyExit = root.GetDummyExit();

            foreach (StatEdge edge in dummyExit.GetAllPredecessorEdges())
            {
                if (!edge.@explicit)
                {
                    Statement      source  = edge.GetSource();
                    List <Exprent> lstExpr = source.GetExprents();
                    if (lstExpr != null && !(lstExpr.Count == 0))
                    {
                        Exprent expr = lstExpr[lstExpr.Count - 1];
                        if (expr.type == Exprent.Exprent_Exit)
                        {
                            ExitExprent ex = (ExitExprent)expr;
                            if (ex.GetExitType() == ExitExprent.Exit_Return && ex.GetValue() == null)
                            {
                                // remove redundant return
                                dummyExit.AddBytecodeOffsets(ex.bytecode);
                                lstExpr.RemoveAtReturningValue(lstExpr.Count - 1);
                            }
                        }
                    }
                }
            }
        }
예제 #13
0
        private static StructField FindAssertionField(ClassesProcessor.ClassNode node)
        {
            ClassWrapper wrapper     = node.GetWrapper();
            bool         noSynthFlag = DecompilerContext.GetOption(IFernflowerPreferences.Synthetic_Not_Set
                                                                   );

            foreach (StructField fd in wrapper.GetClassStruct().GetFields())
            {
                string keyField = InterpreterUtil.MakeUniqueKey(fd.GetName(), fd.GetDescriptor());
                // initializer exists
                if (wrapper.GetStaticFieldInitializers().ContainsKey(keyField))
                {
                    // access flags set
                    if (fd.HasModifier(ICodeConstants.Acc_Static) && fd.HasModifier(ICodeConstants.Acc_Final
                                                                                    ) && (noSynthFlag || fd.IsSynthetic()))
                    {
                        // field type boolean
                        FieldDescriptor fdescr = FieldDescriptor.ParseDescriptor(fd.GetDescriptor());
                        if (VarType.Vartype_Boolean.Equals(fdescr.type))
                        {
                            Exprent initializer = wrapper.GetStaticFieldInitializers().GetWithKey(keyField);
                            if (initializer.type == Exprent.Exprent_Function)
                            {
                                FunctionExprent fexpr = (FunctionExprent)initializer;
                                if (fexpr.GetFuncType() == FunctionExprent.Function_Bool_Not && fexpr.GetLstOperands
                                        ()[0].type == Exprent.Exprent_Invocation)
                                {
                                    InvocationExprent invexpr = (InvocationExprent)fexpr.GetLstOperands()[0];
                                    if (invexpr.GetInstance() != null && invexpr.GetInstance().type == Exprent.Exprent_Const &&
                                        "desiredAssertionStatus".Equals(invexpr.GetName()) && "java/lang/Class".Equals
                                            (invexpr.GetClassname()) && (invexpr.GetLstParameters().Count == 0))
                                    {
                                        ConstExprent cexpr = (ConstExprent)invexpr.GetInstance();
                                        if (VarType.Vartype_Class.Equals(cexpr.GetConstType()))
                                        {
                                            ClassesProcessor.ClassNode nd = node;
                                            while (nd != null)
                                            {
                                                if (nd.GetWrapper().GetClassStruct().qualifiedName.Equals(cexpr.GetValue()))
                                                {
                                                    break;
                                                }
                                                nd = nd.parent;
                                            }
                                            if (nd != null)
                                            {
                                                // found enclosing class with the same name
                                                return(fd);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }
예제 #14
0
        public override TextBuffer ToJava(int indent, BytecodeMappingTracer tracer)
        {
            SwitchHelper.Simplify(this);
            TextBuffer buf = new TextBuffer();

            buf.Append(ExprProcessor.ListToJava(varDefinitions, indent, tracer));
            buf.Append(first.ToJava(indent, tracer));
            if (IsLabeled())
            {
                buf.AppendIndent(indent).Append("label").Append(this.id.ToString()).Append(":").AppendLineSeparator
                    ();
                tracer.IncrementCurrentSourceLine();
            }
            buf.AppendIndent(indent).Append(headexprent[0].ToJava(indent, tracer)).Append(" {"
                                                                                          ).AppendLineSeparator();
            tracer.IncrementCurrentSourceLine();
            VarType switch_type = headexprent[0].GetExprType();

            for (int i = 0; i < caseStatements.Count; i++)
            {
                Statement       stat   = caseStatements[i];
                List <StatEdge> edges  = caseEdges[i];
                List <Exprent>  values = caseValues[i];
                for (int j = 0; j < edges.Count; j++)
                {
                    if (edges[j] == default_edge)
                    {
                        buf.AppendIndent(indent).Append("default:").AppendLineSeparator();
                    }
                    else
                    {
                        buf.AppendIndent(indent).Append("case ");
                        Exprent value = values[j];
                        if (value is ConstExprent)
                        {
                            value = value.Copy();
                            ((ConstExprent)value).SetConstType(switch_type);
                        }
                        if (value is FieldExprent && ((FieldExprent)value).IsStatic())
                        {
                            // enum values
                            buf.Append(((FieldExprent)value).GetName());
                        }
                        else
                        {
                            buf.Append(value.ToJava(indent, tracer));
                        }
                        buf.Append(":").AppendLineSeparator();
                    }
                    tracer.IncrementCurrentSourceLine();
                }
                buf.Append(ExprProcessor.JmpWrapper(stat, indent + 1, false, tracer));
            }
            buf.AppendIndent(indent).Append("}").AppendLineSeparator();
            tracer.IncrementCurrentSourceLine();
            return(buf);
        }
예제 #15
0
 private static bool AddArrayInitializer(Exprent first, Exprent second)
 {
     if (first.type == Exprent.Exprent_Assignment)
     {
         AssignmentExprent @as = (AssignmentExprent)first;
         if (@as.GetRight().type == Exprent.Exprent_New && @as.GetLeft().type == Exprent.Exprent_Var)
         {
             NewExprent newExpr = (NewExprent)@as.GetRight();
             if (!(newExpr.GetLstArrayElements().Count == 0))
             {
                 VarExprent arrVar = (VarExprent)@as.GetLeft();
                 if (second.type == Exprent.Exprent_Assignment)
                 {
                     AssignmentExprent aas = (AssignmentExprent)second;
                     if (aas.GetLeft().type == Exprent.Exprent_Array)
                     {
                         ArrayExprent arrExpr = (ArrayExprent)aas.GetLeft();
                         if (arrExpr.GetArray().type == Exprent.Exprent_Var && arrVar.Equals(arrExpr.GetArray
                                                                                                 ()) && arrExpr.GetIndex().type == Exprent.Exprent_Const)
                         {
                             int constValue = ((ConstExprent)arrExpr.GetIndex()).GetIntValue();
                             if (constValue < newExpr.GetLstArrayElements().Count)
                             {
                                 Exprent init = newExpr.GetLstArrayElements()[constValue];
                                 if (init.type == Exprent.Exprent_Const)
                                 {
                                     ConstExprent cinit      = (ConstExprent)init;
                                     VarType      arrType    = newExpr.GetNewType().DecreaseArrayDim();
                                     ConstExprent defaultVal = ExprProcessor.GetDefaultArrayValue(arrType);
                                     if (cinit.Equals(defaultVal))
                                     {
                                         Exprent tempExpr = aas.GetRight();
                                         if (!tempExpr.ContainsExprent(arrVar))
                                         {
                                             newExpr.GetLstArrayElements()[constValue] = tempExpr;
                                             if (tempExpr.type == Exprent.Exprent_New)
                                             {
                                                 NewExprent tempNewExpr = (NewExprent)tempExpr;
                                                 int        dims        = newExpr.GetNewType().arrayDim;
                                                 if (dims > 1 && !(tempNewExpr.GetLstArrayElements().Count == 0))
                                                 {
                                                     tempNewExpr.SetDirectArrayInit(true);
                                                 }
                                             }
                                             return(true);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }
예제 #16
0
 private static bool IsMonitorExit(Exprent first)
 {
     if (first.type == Exprent.Exprent_Monitor)
     {
         MonitorExprent expr = (MonitorExprent)first;
         return(expr.GetMonType() == MonitorExprent.Monitor_Exit && expr.GetValue().type ==
                Exprent.Exprent_Var && !((VarExprent)expr.GetValue()).IsStack());
     }
     return(false);
 }
        private static void ProcessClassRec(ClassesProcessor.ClassNode node, IDictionary
                                            <ClassWrapper, MethodWrapper> mapClassMeths, HashSet <ClassWrapper> setFound)
        {
            ClassWrapper wrapper = node.GetWrapper();

            // search code
            foreach (MethodWrapper meth in wrapper.GetMethods())
            {
                RootStatement root = meth.root;
                if (root != null)
                {
                    DirectGraph graph = meth.GetOrBuildGraph();
                    graph.IterateExprents((Exprent exprent) => {
                        foreach (KeyValuePair <ClassWrapper, MethodWrapper> ent in mapClassMeths)
                        {
                            if (ReplaceInvocations(exprent, ent.Key, ent.Value))
                            {
                                setFound.Add(ent.Key);
                            }
                        }
                        return(0);
                    }
                                          );
                }
            }
            // search initializers
            for (int j = 0; j < 2; j++)
            {
                VBStyleCollection <Exprent, string> initializers = j == 0 ? wrapper.GetStaticFieldInitializers
                                                                       () : wrapper.GetDynamicFieldInitializers();
                for (int i = 0; i < initializers.Count; i++)
                {
                    foreach (KeyValuePair <ClassWrapper, MethodWrapper> ent in mapClassMeths)
                    {
                        Exprent exprent = initializers[i];
                        if (ReplaceInvocations(exprent, ent.Key, ent.Value))
                        {
                            setFound.Add(ent.Key);
                        }
                        string cl = IsClass14Invocation(exprent, ent.Key, ent.Value);
                        if (cl != null)
                        {
                            initializers[i] = new ConstExprent(VarType.Vartype_Class, cl.Replace('.', '/'), exprent
                                                               .bytecode);
                            setFound.Add(ent.Key);
                        }
                    }
                }
            }
            // iterate nested classes
            foreach (ClassesProcessor.ClassNode nd in node.nested)
            {
                ProcessClassRec(nd, mapClassMeths, setFound);
            }
        }
예제 #18
0
        private static Exprent IsReplaceableVar(Exprent exprent, Dictionary <VarVersionPair
                                                                             , Exprent> mapVarValues)
        {
            Exprent dest = null;

            if (exprent.type == Exprent.Exprent_Var)
            {
                VarExprent var = (VarExprent)exprent;
                dest = mapVarValues.GetOrNull(new VarVersionPair(var));
            }
            return(dest);
        }
예제 #19
0
 private static bool IsEnumArray(Exprent exprent)
 {
     if (exprent is ArrayExprent)
     {
         Exprent field = ((ArrayExprent)exprent).GetArray();
         Exprent index = ((ArrayExprent)exprent).GetIndex();
         return(field is FieldExprent && (((FieldExprent)field).GetName().StartsWith("$SwitchMap"
                                                                                     ) || (index is InvocationExprent && ((InvocationExprent)index).GetName().Equals(
                                                                                               "ordinal"))));
     }
     return(false);
 }
예제 #20
0
        public static void ClearEnum(ClassWrapper wrapper)
        {
            StructClass cl = wrapper.GetClassStruct();

            // hide values/valueOf methods and super() invocations
            foreach (MethodWrapper method in wrapper.GetMethods())
            {
                StructMethod mt         = method.methodStruct;
                string       name       = mt.GetName();
                string       descriptor = mt.GetDescriptor();
                if ("values".Equals(name))
                {
                    if (descriptor.Equals("()[L" + cl.qualifiedName + ";"))
                    {
                        wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(name, descriptor));
                    }
                }
                else if ("valueOf".Equals(name))
                {
                    if (descriptor.Equals("(Ljava/lang/String;)L" + cl.qualifiedName + ";"))
                    {
                        wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(name, descriptor));
                    }
                }
                else if (ICodeConstants.Init_Name.Equals(name))
                {
                    Statement firstData = Statements.FindFirstData(method.root);
                    if (firstData != null && !(firstData.GetExprents().Count == 0))
                    {
                        Exprent exprent = firstData.GetExprents()[0];
                        if (exprent.type == Exprent.Exprent_Invocation)
                        {
                            InvocationExprent invExpr = (InvocationExprent)exprent;
                            if (Statements.IsInvocationInitConstructor(invExpr, method, wrapper, false))
                            {
                                firstData.GetExprents().RemoveAtReturningValue(0);
                            }
                        }
                    }
                }
            }
            // hide synthetic fields of enum and it's constants
            foreach (StructField fd in cl.GetFields())
            {
                string descriptor = fd.GetDescriptor();
                if (fd.IsSynthetic() && descriptor.Equals("[L" + cl.qualifiedName + ";"))
                {
                    wrapper.GetHiddenMembers().Add(InterpreterUtil.MakeUniqueKey(fd.GetName(), descriptor
                                                                                 ));
                }
            }
        }
예제 #21
0
        private static Exprent CreateConcatExprent(List <Exprent> lstOperands, HashSet <int
                                                                                        > bytecode)
        {
            // build exprent to return
            Exprent func = lstOperands[0];

            for (int i = 1; i < lstOperands.Count; i++)
            {
                func = new FunctionExprent(FunctionExprent.Function_Str_Concat, Sharpen.Arrays.AsList
                                               (func, lstOperands[i]), bytecode);
            }
            return(func);
        }
예제 #22
0
        private static void SetExprentVersionsToNull(Exprent exprent)
        {
            List <Exprent> lst = exprent.GetAllExprents(true);

            lst.Add(exprent);
            foreach (Exprent expr in lst)
            {
                if (expr.type == Exprent.Exprent_Var)
                {
                    ((VarExprent)expr).SetVersion(0);
                }
            }
        }
예제 #23
0
        public static Exprent PropagateBoolNot(Exprent exprent)
        {
            if (exprent.type == Exprent.Exprent_Function)
            {
                FunctionExprent fexpr = (FunctionExprent)exprent;
                if (fexpr.GetFuncType() == FunctionExprent.Function_Bool_Not)
                {
                    Exprent param = fexpr.GetLstOperands()[0];
                    if (param.type == Exprent.Exprent_Function)
                    {
                        FunctionExprent fparam = (FunctionExprent)param;
                        int             ftype  = fparam.GetFuncType();
                        switch (ftype)
                        {
                        case FunctionExprent.Function_Bool_Not:
                        {
                            Exprent newexpr = fparam.GetLstOperands()[0];
                            Exprent retexpr = PropagateBoolNot(newexpr);
                            return(retexpr == null ? newexpr : retexpr);
                        }

                        case FunctionExprent.Function_Cadd:
                        case FunctionExprent.Function_Cor:
                        {
                            List <Exprent> operands = fparam.GetLstOperands();
                            for (int i = 0; i < operands.Count; i++)
                            {
                                Exprent newparam = new FunctionExprent(FunctionExprent.Function_Bool_Not, operands
                                                                       [i], operands[i].bytecode);
                                Exprent retparam = PropagateBoolNot(newparam);
                                operands[i] = retparam == null ? newparam : retparam;
                            }
                            goto case FunctionExprent.Function_Eq;
                        }

                        case FunctionExprent.Function_Eq:
                        case FunctionExprent.Function_Ne:
                        case FunctionExprent.Function_Lt:
                        case FunctionExprent.Function_Ge:
                        case FunctionExprent.Function_Gt:
                        case FunctionExprent.Function_Le:
                        {
                            fparam.SetFuncType(funcsnot[ftype - FunctionExprent.Function_Eq]);
                            return(fparam);
                        }
                        }
                    }
                }
            }
            return(null);
        }
예제 #24
0
 private void EliminateStaticAccess(ClassesProcessor.ClassNode node)
 {
     if (node.type == ClassesProcessor.ClassNode.Class_Lambda)
     {
         return;
     }
     foreach (MethodWrapper meth in node.GetWrapper().GetMethods())
     {
         if (meth.root != null)
         {
             bool                    replaced   = false;
             DirectGraph             graph      = meth.GetOrBuildGraph();
             HashSet <DirectNode>    setVisited = new HashSet <DirectNode>();
             LinkedList <DirectNode> stack      = new LinkedList <DirectNode>();
             stack.AddLast(graph.first);
             while (!(stack.Count == 0))
             {
                 // TODO: replace with interface iterator?
                 DirectNode nd = Sharpen.Collections.RemoveFirst(stack);
                 if (setVisited.Contains(nd))
                 {
                     continue;
                 }
                 setVisited.Add(nd);
                 for (int i = 0; i < nd.exprents.Count; i++)
                 {
                     Exprent exprent = nd.exprents[i];
                     replaced |= ReplaceInvocations(node, meth, exprent);
                     if (exprent.type == Exprent.Exprent_Invocation)
                     {
                         Exprent ret = ReplaceAccessExprent(node, meth, (InvocationExprent)exprent);
                         if (ret != null)
                         {
                             nd.exprents[i] = ret;
                             replaced       = true;
                         }
                     }
                 }
                 Sharpen.Collections.AddAll(stack, nd.succs);
             }
             if (replaced)
             {
                 ComputeMethodType(node, meth);
             }
         }
     }
     foreach (ClassesProcessor.ClassNode child in node.nested)
     {
         EliminateStaticAccess(child);
     }
 }
예제 #25
0
 private static bool IsTrivialStackAssignment(Exprent first)
 {
     if (first.type == Exprent.Exprent_Assignment)
     {
         AssignmentExprent asf = (AssignmentExprent)first;
         if (asf.GetLeft().type == Exprent.Exprent_Var && asf.GetRight().type == Exprent.Exprent_Var)
         {
             VarExprent left  = (VarExprent)asf.GetLeft();
             VarExprent right = (VarExprent)asf.GetRight();
             return(left.GetIndex() == right.GetIndex() && left.IsStack() && right.IsStack());
         }
     }
     return(false);
 }
예제 #26
0
 public override void ReplaceExprent(Exprent oldexpr, Exprent newexpr)
 {
     if (initExprent[0] == oldexpr)
     {
         initExprent[0] = newexpr;
     }
     if (conditionExprent[0] == oldexpr)
     {
         conditionExprent[0] = newexpr;
     }
     if (incExprent[0] == oldexpr)
     {
         incExprent[0] = newexpr;
     }
 }
예제 #27
0
        private static HashSet <VarVersionPair> GetAllVersions(Exprent exprent)
        {
            HashSet <VarVersionPair> res      = new HashSet <VarVersionPair>();
            List <Exprent>           listTemp = new List <Exprent>(exprent.GetAllExprents(true));

            listTemp.Add(exprent);
            foreach (Exprent expr in listTemp)
            {
                if (expr.type == Exprent.Exprent_Var)
                {
                    VarExprent var = (VarExprent)expr;
                    res.Add(new VarVersionPair(var));
                }
            }
            return(res);
        }
예제 #28
0
        private static void ReplaceSingleVar(Exprent parent, VarExprent var, Exprent dest
                                             , SSAUConstructorSparseEx ssau)
        {
            parent.ReplaceExprent(var, dest);
            // live sets
            SFormsFastMapDirect      livemap = ssau.GetLiveVarVersionsMap(new VarVersionPair(var));
            HashSet <VarVersionPair> setVars = GetAllVersions(dest);

            foreach (VarVersionPair varpaar in setVars)
            {
                VarVersionNode node      = ssau.GetSsuversions().nodes.GetWithKey(varpaar);
                var            toRemove  = new List <KeyValuePair <int, FastSparseSetFactory <int> .FastSparseSet <int> > >();
                var            entryList = node.live.EntryList();
                for (var index = 0; index < entryList.Count; index++)
                {
                    var itent = entryList[index];
                    KeyValuePair <int, FastSparseSetFactory <int> .FastSparseSet <int> > ent =
                        entryList.ElementAtOrDefault(index + 1);
                    if (ent.Value == null)
                    {
                        break;
                    }

                    int key = ent.Key;
                    if (!livemap.ContainsKey(key))
                    {
                        toRemove.Add(itent);
                    }
                    else
                    {
                        FastSparseSetFactory <int> .FastSparseSet <int> set = ent.Value;
                        set.Complement(livemap.Get(key));
                        if (set.IsEmpty())
                        {
                            toRemove.Add(itent);
                        }
                    }
                }

                foreach (var keyValuePair in toRemove)
                {
                    node.live.PutInternal(keyValuePair.Key, keyValuePair.Value, true);
                    // entryList.RemoveAll(c => c.Key == keyValuePair.Key);
                }
            }
        }
예제 #29
0
 private static bool SetDefinition(Exprent expr, int index)
 {
     if (expr.type == Exprent.Exprent_Assignment)
     {
         Exprent left = ((AssignmentExprent)expr).GetLeft();
         if (left.type == Exprent.Exprent_Var)
         {
             VarExprent var = (VarExprent)left;
             if (var.GetIndex() == index)
             {
                 var.SetDefinition(true);
                 return(true);
             }
         }
     }
     return(false);
 }
예제 #30
0
        private static void ExtractStaticInitializers(ClassWrapper wrapper, MethodWrapper
                                                      method)
        {
            RootStatement root      = method.root;
            StructClass   cl        = wrapper.GetClassStruct();
            Statement     firstData = Statements.FindFirstData(root);

            if (firstData != null)
            {
                bool inlineInitializers = cl.HasModifier(ICodeConstants.Acc_Interface) || cl.HasModifier
                                              (ICodeConstants.Acc_Enum);
                while (!(firstData.GetExprents().Count == 0))
                {
                    Exprent exprent = firstData.GetExprents()[0];
                    bool    found   = false;
                    if (exprent.type == Exprent.Exprent_Assignment)
                    {
                        AssignmentExprent assignExpr = (AssignmentExprent)exprent;
                        if (assignExpr.GetLeft().type == Exprent.Exprent_Field)
                        {
                            FieldExprent fExpr = (FieldExprent)assignExpr.GetLeft();
                            if (fExpr.IsStatic() && fExpr.GetClassname().Equals(cl.qualifiedName) && cl.HasField
                                    (fExpr.GetName(), fExpr.GetDescriptor().descriptorString))
                            {
                                // interfaces fields should always be initialized inline
                                if (inlineInitializers || IsExprentIndependent(assignExpr.GetRight(), method))
                                {
                                    string keyField = InterpreterUtil.MakeUniqueKey(fExpr.GetName(), fExpr.GetDescriptor
                                                                                        ().descriptorString);
                                    if (!wrapper.GetStaticFieldInitializers().ContainsKey(keyField))
                                    {
                                        wrapper.GetStaticFieldInitializers().AddWithKey(assignExpr.GetRight(), keyField);
                                        firstData.GetExprents().RemoveAtReturningValue(0);
                                        found = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!found)
                    {
                        break;
                    }
                }
            }
        }