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); }
private DAGNode GetVarReference(string name, bool create) { if (varReferences.ContainsKey(name)) { return(varReferences[name]); } if (create) { DAGNode node = DAGNode.CreateLeaf(name); varReferences[name] = node; return(node); } else { return(null); } }
/// <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); }
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++; } } } } } } }
/// <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); }
static SemanticValue() { nullValue.Node = DAGNode.CreateLeaf("null_stack"); }