Example #1
0
        public SemanticValue Clone()
        {
            SemanticValue sv = new SemanticValue();

            sv.type       = type;
            sv.isConst    = isConst;
            sv.node       = node;
            sv.integerVal = integerVal;
            sv.floatVal   = floatVal;
            return(sv);
        }
Example #2
0
 private TypeInfo GetIntegerCalcResultType(SemanticValue v1, SemanticValue v2)
 {
     if (v1.Type == BasicTypeInfo.Int64Info || v2.Type == BasicTypeInfo.Int64Info)
     {
         return(BasicTypeInfo.Int64Info);
     }
     else if (v1.Type == BasicTypeInfo.UInt64Info || v2.Type == BasicTypeInfo.UInt64Info)
     {
         return(BasicTypeInfo.UInt64Info);
     }
     else if (v1.Type == BasicTypeInfo.Int32Info || v2.Type == BasicTypeInfo.Int32Info)
     {
         return(BasicTypeInfo.Int32Info);
     }
     else if (v1.Type == BasicTypeInfo.UInt32Info || v2.Type == BasicTypeInfo.UInt32Info)
     {
         return(BasicTypeInfo.UInt32Info);
     }
     else if (v1.Type == BasicTypeInfo.IntPtrInfo || v2.Type == BasicTypeInfo.IntPtrInfo)
     {
         return(BasicTypeInfo.IntPtrInfo);
     }
     else if (v1.Type == BasicTypeInfo.UIntPtrInfo || v2.Type == BasicTypeInfo.UIntPtrInfo)
     {
         return(BasicTypeInfo.UIntPtrInfo);
     }
     else if (v1.Type == BasicTypeInfo.Int16Info || v2.Type == BasicTypeInfo.Int16Info)
     {
         return(BasicTypeInfo.Int16Info);
     }
     else if (v1.Type == BasicTypeInfo.UInt16Info || v2.Type == BasicTypeInfo.UInt16Info)
     {
         return(BasicTypeInfo.UInt16Info);
     }
     else if (v1.Type == BasicTypeInfo.CharInfo || v2.Type == BasicTypeInfo.CharInfo)
     {
         return(BasicTypeInfo.CharInfo);
     }
     else if (v1.Type == BasicTypeInfo.SByteInfo || v2.Type == BasicTypeInfo.SByteInfo)
     {
         return(BasicTypeInfo.SByteInfo);
     }
     else if (v1.Type == BasicTypeInfo.ByteInfo || v2.Type == BasicTypeInfo.ByteInfo)
     {
         return(BasicTypeInfo.ByteInfo);
     }
     else
     {
         return(BasicTypeInfo.Int32Info);
     }
 }
Example #3
0
        private TypeInfo GetCompatibleType(params SemanticValue[] vals)
        {
            bool isReference = true;

            foreach (SemanticValue vv in vals)
            {
                if (!vv.Type.IsReference)
                {
                    isReference = false;
                    break;
                }
            }
            SemanticValue v = vals[0];

            if (isReference)//全部都是引用时不换名
            {
                return(v.Type);
            }
            TypeInfo t = v.Type;

            for (int i = 1; i < vals.Length; i++)
            {
                TypeInfo tt = vals[i].Type;
                if (t == BasicTypeInfo.ObjectInfo)//类型直接是System.Object的通常是nullptr,不用它作判断依据
                {
                    t = tt;
                    continue;
                }
                if (tt == BasicTypeInfo.ObjectInfo)//类型直接是System.Object的通常是nullptr,不用它作判断依据
                {
                    continue;
                }
                if (t.IsRef || tt.IsRef)
                {
                    if (t != tt)//类型不一致的使用System.Object作临时变量类型,这样不会倒致类型冲突,稍后变量的使用会通过转型成合适的类型
                    {
                        return(BasicTypeInfo.ObjectInfo);
                    }
                }
                else if (t.IsBoolean || tt.IsBoolean)
                {
                    t = BasicTypeInfo.BooleanInfo;
                }
                else if ((!t.IsSimple || t.IsCastSimple) && tt.IsSimple && !tt.IsCastSimple)
                {
                    t = tt;
                }
            }
            return(t);
        }
Example #4
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);
        }
Example #5
0
 private void CheckSemantic()
 {
     for (int i = 0; i < ilLines.Values.Count; i++)
     {
         IlIns il = ilLines.Values[i];
         if (flowGraph.IsNode(il.LabelValue))
         {
             FlowGraphNode node = flowGraph[il.LabelValue];
             if (node.SemanticStacks.Count > 0)
             {
                 //检查是否存在多个前导且存在传入的语义栈,这是需要处理跨块共用栈的情形,添加一个警告信息。
                 if (node.PrevNodes.Count > 1)
                 {
                     DAGNode enode = DAGNode.CreateLeaf(true, "warning ! semantic stack doesn't empty at joint !");
                     enode.IsComment = true;
                     il.Csharps.Add(enode);
                 }
                 //检查多个前导传入的语义栈的语义值数目是否一致,这表明程序或者分析出现了错误。
                 int  n     = -1;
                 bool error = false;
                 foreach (SemanticStack ss in node.SemanticStacks)
                 {
                     if (n <= 0)
                     {
                         n = ss.Count;
                     }
                     else if (ss.Count != n)
                     {
                         DAGNode enode = DAGNode.CreateLeaf(true, "error ! semantic stack doesn't balance at joint !");
                         enode.IsComment = true;
                         il.Csharps.Add(enode);
                         error = true;
                         break;
                     }
                 }
                 if (!error)
                 {
                     //对多个前导传入的语义栈里的语义值统一一个临时变量名。
                     TypeInfo[]        types = new TypeInfo[n];
                     string[]          tvars = new string[n];
                     SemanticValue[][] vals0 = new SemanticValue[node.SemanticStacks.Count][];
                     for (int ii = 0; ii < vals0.Length; ii++)
                     {
                         vals0[ii] = node.SemanticStacks[ii].ToArray();
                     }
                     SemanticValue[][] vals = new SemanticValue[n][];
                     for (int ii = 0; ii < vals.Length; ii++)
                     {
                         vals[ii] = new SemanticValue[node.SemanticStacks.Count];
                         for (int j = 0; j < vals[ii].Length; j++)
                         {
                             vals[ii][j] = vals0[j][ii];
                         }
                     }
                     int ix = 0;
                     for (; ix < n; ix++)
                     {
                         TypeInfo type = GetCompatibleType(vals[ix]);
                         if (type.IsReference)//仅对非引用值换名
                         {
                             types[ix] = type;
                             tvars[ix] = "";
                         }
                         else
                         {
                             types[ix] = type;
                             tvars[ix] = NewAlias(type);
                         }
                     }
                     foreach (SemanticStack ss in node.SemanticStacks)
                     {
                         ix = 0;
                         foreach (SemanticValue val in ss)
                         {
                             if (val.Node is ReferenceNode)
                             {
                                 if (!types[ix].IsReference)//仅对非引用值换名
                                 {
                                     val.Node.SetID(tvars[ix], types[ix]);
                                     AdjustConstForType(val.Node as ReferenceNode, types[ix]);
                                 }
                             }
                             else
                             {
                                 //error
                             }
                             ix++;
                         }
                     }
                 }
             }
         }
     }
 }
Example #6
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);
        }
Example #7
0
 private DAGNode BuildCastExpression(TypeInfo ti, SemanticValue v)
 {
     return(BuildCastExpression(ti, v, false));
 }
Example #8
0
 private DAGNode BuildArgumentCastExpression(TypeInfo ti, SemanticValue v)
 {
     return(BuildCastExpression(ti, v, true));
 }