Beispiel #1
0
        private DAGNode AdjustGoto(IlIns il, DAGNode node, bool isDecompile)
        {
            string label = AdjustGoto(il, isDecompile);

            if (label != null)
            {
                node.IsComment = true;
                il.Csharps.Add(node);
                if (node is UnaryNode)
                {
                    DAGNode newNode = DAGNode.CreateUnary(true, "goto", DAGNode.CreateLeaf(label));
                    return(newNode);
                }
                else if (node is BinaryNode)
                {
                    DAGNode newNode = DAGNode.CreateBinary(true, "if", node.NextNodes[0], DAGNode.CreateLeaf(label));
                    if (node.OtherOperators.Count > 0)
                    {
                        newNode.OtherOperators.Add(node.OtherOperators[0]);
                    }
                    return(newNode);
                }
                else if (node is TernaryNode)
                {
                    DAGNode newNode = DAGNode.CreateTernary(true, "if", node.NextNodes[0], node.NextNodes[1], DAGNode.CreateLeaf(label));
                    newNode.OtherOperators.Add(node.OtherOperators[0]);
                    return(newNode);
                }
            }
            return(node);
        }
Beispiel #2
0
        /// <summary>
        /// 构造将值强制转为指定类型的表达式。
        /// </summary>
        /// <param name="type"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        private DAGNode BuildCastExpression(TypeInfo ti, SemanticValue _v, bool forArg)
        {
            SemanticValue v    = _v.Clone();
            DAGNode       node = _v.Node;

            if (v.Type == BasicTypeInfo.IntPtrInfo)
            {
                v.Node = DAGNode.CreateUnary("ToInt32", _v.Node);
            }
            else if (v.Type == BasicTypeInfo.UIntPtrInfo)
            {
                v.Node = DAGNode.CreateUnary("ToUInt32", _v.Node);
            }

            if (v.IsConst && ti.IsBoolean)
            {
                node = DAGNode.CreateLeaf(v.ConstToString(ti));
            }
            else if (v.IsConst && v.IntegerVal == 0 && ti.IsRef)
            {
                node = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(ti.TypeDefClause), DAGNode.CreateLeaf("nullptr"));
            }
            else if (v.IsConst && ti.IsSimple)
            {
                node = DAGNode.CreateLeaf(v.ConstToString(ti));
                if (forArg)
                {
                    node = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(ti.TypeDefClause), node);
                }
            }
            else if (ti != v.Type)
            {
                node = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(ti.TypeDefClause), v.Node);
            }
            return(node);
        }
Beispiel #3
0
        /// <summary>
        /// 为两个值构造强制转换表达式,使得两个值可以进行比较操作。
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <param name="node1"></param>
        /// <param name="node2"></param>
        /// <returns>
        /// true  --- 可以生成判大小的比较表达式
        /// false --- 不可以生成判大小的比较表达式,需要转为判等表达式(此情形为有一个操作数为0,另一个为对象引用)
        /// </returns>
        private bool BuildCastExpression(SemanticValue _v1, SemanticValue _v2, out DAGNode node1, out DAGNode node2)
        {
            bool ret = true;

            node1 = _v1.Node;
            node2 = _v2.Node;
            SemanticValue v1 = _v1.Clone();
            SemanticValue v2 = _v2.Clone();

            if (v1.Type == BasicTypeInfo.IntPtrInfo)
            {
                v1.Node = DAGNode.CreateUnary("ToInt32", _v1.Node);
            }
            else if (v1.Type == BasicTypeInfo.UIntPtrInfo)
            {
                v1.Node = DAGNode.CreateUnary("ToUInt32", _v1.Node);
            }
            if (v2.Type == BasicTypeInfo.IntPtrInfo)
            {
                v2.Node = DAGNode.CreateUnary("ToInt32", _v2.Node);
            }
            else if (v2.Type == BasicTypeInfo.UIntPtrInfo)
            {
                v2.Node = DAGNode.CreateUnary("ToUInt32", _v2.Node);
            }

            if (v1.Type.IsBoolean && v2.IsConst)
            {
                node2 = DAGNode.CreateLeaf(v2.ConstToString(v1.Type));
            }
            else if (v2.Type.IsBoolean && v1.IsConst)
            {
                node1 = DAGNode.CreateLeaf(v1.ConstToString(v2.Type));
            }
            else if (v1.Type.IsRef && v2.IsConst && v2.IntegerVal == 0)
            {
                node2 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v1.Type.TypeDefClause), DAGNode.CreateLeaf("nullptr"));
                ret   = false;
            }
            else if (v2.Type.IsRef && v1.IsConst && v1.IntegerVal == 0)
            {
                node1 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v2.Type.TypeDefClause), DAGNode.CreateLeaf("nullptr"));
                ret   = false;
            }
            else if (v1.Type != v2.Type)
            {
                if (v1.Type.IsSimple && v2.Type.IsSimple)//都是基础类型变量
                {
                    if (v1.IsConst)
                    {
                        node1 = DAGNode.CreateLeaf(v1.ConstToString(v2.Type));
                    }
                    else if (v2.IsConst)
                    {
                        node2 = DAGNode.CreateLeaf(v2.ConstToString(v1.Type));
                    }
                    else if (v1.Type.IsCastSimple && !v2.Type.IsCastSimple)
                    {
                        node1 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v2.Type.TypeClause), v1.Node);
                    }
                    else
                    {
                        node2 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v1.Type.TypeClause), v2.Node);
                    }
                }
                else if (v2.Type.IsSimple)//第二个操作数是基础变量,将操作数1强制转为操作数2的类型
                {
                    if (v1.Type.IsRef && v1.IsConst && v1.IntegerVal == 0)
                    {
                        node1 = DAGNode.CreateLeaf("0");
                    }
                    else
                    {
                        node1 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v2.Type.TypeDefClause), v1.Node);
                    }
                }
                else if (v1.Type.IsSimple)//第一个操作数是基础变量,将操作数2强制转为操作数1的类型
                {
                    if (v2.Type.IsRef && v2.IsConst && v2.IntegerVal == 0)
                    {
                        node2 = DAGNode.CreateLeaf("0");
                    }
                    else
                    {
                        node2 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v1.Type.TypeDefClause), v2.Node);
                    }
                }
                else//都不是基础变量
                {
                    node2 = DAGNode.CreateBinary("()", DAGNode.CreateLeaf(v1.Type.TypeDefClause), v2.Node);
                }
            }
            return(ret);
        }