public override void CaseAIntConstExp(AIntConstExp node) { if (folding) { value = int.Parse(node.GetIntegerLiteral().Text); } }
public override void CaseAOctalConstExp(AOctalConstExp node) { int i = Convert.ToInt32(node.GetOctalLiteral().Text.Substring(1), 8); AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral(i.ToString(), node.GetOctalLiteral().Line, node.GetOctalLiteral().Pos)); node.ReplaceBy(intConst); intConst.Apply(this); }
public override void OutAIntConstExp(AIntConstExp node) { // check: valid 32 bit integer // annotate: type and value in current scope. long result = RuntimeEnvironment.ConvertToLong(node.GetValue()); PType type = RuntimeEnvironment.GetSmallestFit(result); Env.CurrentScope.Add(node, result, type); base.OutAIntConstExp(node); }
public override void OutAAProgram(AAProgram node) { foreach (KeyValuePair <AArrayLengthLvalue, AArrayTempType> pair in data.ArrayLengthTypes) { AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral(pair.Value.GetIntDim().Text)); data.ExpTypes[intConst] = new ANamedType(new TIdentifier("int"), null); ALvalueExp exp = Util.GetAncestor <ALvalueExp>(pair.Key); exp.ReplaceBy(intConst); } base.OutAAProgram(node); }
private void MakeAssignments(AABlock block, PType type, PLvalue leftSide, bool onEnhritedFields) { if (type is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)type)) { AStructDecl str = data.StructTypeLinks[(ANamedType)type]; foreach (AALocalDecl field in str.GetLocals().OfType <AALocalDecl>()) { if (!onEnhritedFields && data.EnheritanceLocalMap.ContainsKey(field)) { continue; } ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data)); data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide]; AStructLvalue newLeftSide = new AStructLvalue(lvalueExp, new ADotDotType(new TDot(".")), new TIdentifier(field.GetName().Text)); data.StructFieldLinks[newLeftSide] = field; data.LvalueTypes[newLeftSide] = field.GetType(); if (field.GetInit() != null) { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLeftSide, Util.MakeClone(field.GetInit(), data)); data.ExpTypes[assignment] = data.LvalueTypes[newLeftSide]; block.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } else { MakeAssignments(block, field.GetType(), newLeftSide, onEnhritedFields); } } } else if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++) { AIntConstExp index = new AIntConstExp(new TIntegerLiteral(i.ToString())); data.ExpTypes[index] = new ANamedType(new TIdentifier("int"), null); ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data)); data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide]; AArrayLvalue newLeftSide = new AArrayLvalue(new TLBracket("["), lvalueExp, index); data.LvalueTypes[newLeftSide] = aType.GetType(); MakeAssignments(block, aType.GetType(), newLeftSide, onEnhritedFields); } } }
public override void OutAEnumDecl(AEnumDecl node) { AStructDecl replacer = new AStructDecl(node.GetVisibilityModifier(), null, null, null, node.GetEndToken(), node.GetName(), new ArrayList(), null, new ArrayList()); int intVal = 0; //int min = int.MaxValue; //int max = int.MinValue; //List<TIdentifier> types = new List<TIdentifier>(); foreach (AAEnumLocal value in node.GetValues()) { AIntConstExp intConst; if (value.GetValue() != null) { intConst = (AIntConstExp)value.GetValue(); intVal = int.Parse(intConst.GetIntegerLiteral().Text) + 1; } else { intConst = new AIntConstExp(new TIntegerLiteral(intVal.ToString(), value.GetName().Line, value.GetName().Pos)); intVal++; } // min = Math.Min(intVal - 1, min); // max = Math.Max(intVal - 1, max); TIdentifier typeIdentifier = new TIdentifier(replacer.GetName().Text, value.GetName().Line, value.GetName().Pos); // types.Add(typeIdentifier); AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), new TStatic("static", value.GetName().Line, value.GetName().Pos), null, null, new TConst("const", value.GetName().Line, value.GetName().Pos), new ANamedType(typeIdentifier, null), value.GetName(), intConst); replacer.GetLocals().Add(localDecl); } /* if (min < 0 || max > 255) * foreach (TIdentifier identifier in types) * { * identifier.Text = "int"; * }*/ node.ReplaceBy(replacer); replacer.Apply(this); replacer.GetName().Text = "enum " + replacer.GetName().Text; }
public override void CaseAArrayLvalue(AArrayLvalue node) { bool containedLiteral = containsLiteral; containsLiteral = false; node.GetIndex().Apply(this); bool hasLiteral = containsLiteral; containsLiteral = containedLiteral; PType type = data.ExpTypes[node.GetIndex()]; if (!hasLiteral && type is ANamedType && ((ANamedType)type).IsPrimitive("byte")) { AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop = new ABinopExp(node.GetIndex(), new APlusBinop(new TPlus("+")), intConst); data.ExpTypes[intConst] = data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null); node.SetIndex(binop); } node.GetBase().Apply(this); }
private AMethodDecl GetMethodMethod() { if (GetMethodPartMethod != null) { return(GetMethodPartMethod); } /* * string GetMethodPart(string delegate) * { * int i = StringFind(delegate, ":", false); * if (i == -1) * { * return delegate; * } * return StringSub(delegate, 1, i - 1); * } */ AASourceFile sourceFile = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry); AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("delegate"), null); ALocalLvalue delegateRef1 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef2 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef3 = new ALocalLvalue(new TIdentifier("delegate")); ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1); ALvalueExp delegateRef2Exp = new ALvalueExp(delegateRef2); ALvalueExp delegateRef3Exp = new ALvalueExp(delegateRef3); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\":\"")); ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool()); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("-1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1")); ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList() { delegateRef1Exp, stringConst, booleanConst }); AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("i"), stringFindInvoke); ALocalLvalue iRef1 = new ALocalLvalue(new TIdentifier("i")); ALocalLvalue iRef2 = new ALocalLvalue(new TIdentifier("i")); ALvalueExp iRef1Exp = new ALvalueExp(iRef1); ALvalueExp iRef2Exp = new ALvalueExp(iRef2); ABinopExp binop1 = new ABinopExp(iRef1Exp, new AEqBinop(new TEq("==")), intConst1); AIfThenStm ifThen = new AIfThenStm(new TLParen("("), binop1, new ABlockStm(new TLBrace("{"), new AABlock( new ArrayList() { new AValueReturnStm(new TReturn("return"), delegateRef2Exp) }, new TRBrace("}")))); ABinopExp binop2 = new ABinopExp(iRef2Exp, new AMinusBinop(new TMinus("-")), intConst3); ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { delegateRef3Exp, intConst2, binop2 }); GetMethodPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("GetMethodPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() { delegateFormal }, new AABlock( new ArrayList() { new ALocalDeclStm(new TSemicolon(";"), iDecl), ifThen, new AValueReturnStm(new TReturn("return"), stringSubInvoke) }, new TRBrace("}"))); sourceFile.GetDecl().Add(GetMethodPartMethod); finalTrans.data.LocalLinks[delegateRef1] = finalTrans.data.LocalLinks[delegateRef2] = finalTrans.data.LocalLinks[delegateRef3] = delegateFormal; finalTrans.data.LocalLinks[iRef1] = finalTrans.data.LocalLinks[iRef2] = iDecl; finalTrans.data.LvalueTypes[delegateRef1] = finalTrans.data.LvalueTypes[delegateRef2] = finalTrans.data.LvalueTypes[delegateRef3] = finalTrans.data.ExpTypes[delegateRef1Exp] = finalTrans.data.ExpTypes[delegateRef2Exp] = finalTrans.data.ExpTypes[delegateRef3Exp] = finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.LvalueTypes[iRef1] = finalTrans.data.LvalueTypes[iRef2] = finalTrans.data.ExpTypes[iRef1Exp] = finalTrans.data.ExpTypes[iRef2Exp] = finalTrans.data.ExpTypes[stringFindInvoke] = finalTrans.data.ExpTypes[intConst1] = finalTrans.data.ExpTypes[intConst2] = finalTrans.data.ExpTypes[intConst3] = finalTrans.data.ExpTypes[binop2] = new ANamedType(new TIdentifier("int"), null); finalTrans.data.ExpTypes[booleanConst] = finalTrans.data.ExpTypes[binop1] = new ANamedType(new TIdentifier("bool"), null); finalTrans.data.SimpleMethodLinks[stringFindInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringSubInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text); return(GetMethodPartMethod); }
private AMethodDecl GetStringPointerMethod() { if (GetStringPointerPartMethod != null) { return(GetStringPointerPartMethod); } /* * string GetPointerPart(string delegate) * { * return StringSub(delegate, StringFind(delegate, ":", false) + 1, StringLength(delegate)); * } */ AASourceFile sourceFile = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry); AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("delegate"), null); ALocalLvalue delegateRef1 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef2 = new ALocalLvalue(new TIdentifier("delegate")); ALocalLvalue delegateRef3 = new ALocalLvalue(new TIdentifier("delegate")); ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1); ALvalueExp delegateRef2Exp = new ALvalueExp(delegateRef2); ALvalueExp delegateRef3Exp = new ALvalueExp(delegateRef3); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\":\"")); ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool()); ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList() { delegateRef2Exp, stringConst, booleanConst }); AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop = new ABinopExp(stringFindInvoke, new APlusBinop(new TPlus("+")), intConst); ASimpleInvokeExp stringLengthInvoke = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { delegateRef3Exp }); ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { delegateRef1Exp, binop, stringLengthInvoke }); GetStringPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() { delegateFormal }, new AABlock( new ArrayList() { new AValueReturnStm(new TReturn("return"), stringSubInvoke) }, new TRBrace("}"))); sourceFile.GetDecl().Add(GetStringPointerPartMethod); data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(sourceFile, GetStringPointerPartMethod)); finalTrans.data.LocalLinks[delegateRef1] = finalTrans.data.LocalLinks[delegateRef2] = finalTrans.data.LocalLinks[delegateRef3] = delegateFormal; finalTrans.data.LvalueTypes[delegateRef1] = finalTrans.data.LvalueTypes[delegateRef2] = finalTrans.data.LvalueTypes[delegateRef3] = finalTrans.data.ExpTypes[delegateRef1Exp] = finalTrans.data.ExpTypes[delegateRef2Exp] = finalTrans.data.ExpTypes[delegateRef3Exp] = finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[booleanConst] = new ANamedType(new TIdentifier("bool"), null); finalTrans.data.ExpTypes[intConst] = finalTrans.data.ExpTypes[binop] = finalTrans.data.ExpTypes[stringFindInvoke] = finalTrans.data.ExpTypes[stringLengthInvoke] = new ANamedType(new TIdentifier("int"), null); finalTrans.data.SimpleMethodLinks[stringFindInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringLengthInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringLengthInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringSubInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text); return(GetStringPointerPartMethod); }
public override void CaseAIntConstExp(AIntConstExp node) { Value += node.GetIntegerLiteral().Text; }
public override void CaseAIntConstExp(AIntConstExp node) { containsLiteral = true; }
public override void CaseABinopExp(ABinopExp node) { bool pushed = false; if (!(node.Parent() is ABinopExp)) { PushStack(); pushed = true; } try { bool isIntegerType = data.ExpTypes[node] is ANamedType && (((ANamedType)data.ExpTypes[node]).IsPrimitive("int") || ((ANamedType)data.ExpTypes[node]).IsPrimitive("byte")); if (isIntegerType) { if (node.GetBinop() is APlusBinop || node.GetBinop() is AMinusBinop) { node.GetLeft().Apply(this); if (!Util.HasAncestor <AAProgram>(node)) { return; } if (node.GetBinop() is AMinusBinop) { isNegativeRightSide = !isNegativeRightSide; } node.GetRight().Apply(this); if (node.GetBinop() is AMinusBinop) { isNegativeRightSide = !isNegativeRightSide; } if (!Util.HasAncestor <AAProgram>(node)) { return; } for (int i = 0; i < intConsts.Count; i++) { for (int j = i + 1; j < intConsts.Count; j++) { Pair <AIntConstExp, bool> const1 = intConsts[i]; Pair <AIntConstExp, bool> const2 = intConsts[j]; ABinopExp pBinop1 = (ABinopExp)const1.Car.Parent(); ABinopExp pBinop2 = (ABinopExp)const2.Car.Parent(); int a = int.Parse(const1.Car.GetIntegerLiteral().Text); int b = int.Parse(const2.Car.GetIntegerLiteral().Text); int c; if (const1.Cdr != const2.Cdr) { c = a - b; } else { c = a + b; } //Eliminate stuff like <exp> + -1 if (c < 0 && pBinop1.GetRight() == const1.Car) { c = -c; if (pBinop1.GetBinop() is AMinusBinop) { pBinop1.SetBinop(new APlusBinop(new TPlus("+"))); } else { pBinop1.SetBinop(new AMinusBinop(new TMinus("-"))); } const1.Cdr = !const1.Cdr; } const1.Car.GetIntegerLiteral().Text = c.ToString(); //Remove binop2 if (pBinop2.GetLeft() == const2.Car) { if (pBinop2.GetBinop() is AMinusBinop) { if (pBinop2.GetRight() is AIntConstExp) { AIntConstExp const3 = (AIntConstExp)pBinop2.GetRight(); const3.GetIntegerLiteral().Text = (-int.Parse(const3.GetIntegerLiteral().Text)).ToString(); pBinop2.ReplaceBy(const3); intConsts.Add(new Pair <AIntConstExp, bool>(const3, isNegativeRightSide)); } else { AUnopExp unop = new AUnopExp(new ANegateUnop(new TMinus("-")), pBinop2.GetRight()); data.ExpTypes[unop] = new ANamedType(new TIdentifier("int"), null); pBinop2.ReplaceBy(unop); } } else { pBinop2.ReplaceBy(pBinop2.GetRight()); } } else { pBinop2.ReplaceBy(pBinop2.GetLeft()); } intConsts.RemoveAt(j); j--; } } return; } } { PushStack(); node.GetLeft().Apply(this); PopStack(); PushStack(); node.GetRight().Apply(this); PopStack(); } if (isIntegerType && (node.GetBinop() is ATimesBinop || node.GetBinop() is ADivideBinop) && node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp) { AIntConstExp const1 = (AIntConstExp)node.GetLeft(); AIntConstExp const2 = (AIntConstExp)node.GetRight(); int a = int.Parse(const1.GetIntegerLiteral().Text); int b = int.Parse(const2.GetIntegerLiteral().Text); int c; if (node.GetBinop() is ATimesBinop || b != 0) { if (node.GetBinop() is ATimesBinop) { c = a * b; } else { c = a / b; } const1.GetIntegerLiteral().Text = c.ToString(); node.ReplaceBy(const1); const1.Apply(this); return; } } if (node.GetBinop() is AEqBinop || node.GetBinop() is ANeBinop) { if (node.GetLeft() is ABooleanConstExp && node.GetRight() is ABooleanConstExp) { bool b1 = ((ABooleanConstExp)node.GetLeft()).GetBool() is ATrueBool; bool b2 = ((ABooleanConstExp)node.GetRight()).GetBool() is ATrueBool; bool b3 = false; if (node.GetBinop() is AEqBinop) { b3 = b1 == b2; } else if (node.GetBinop() is ANeBinop) { b3 = b1 != b2; } ((ABooleanConstExp)node.GetLeft()).SetBool(b3 ? (PBool) new ATrueBool() : new AFalseBool()); node.ReplaceBy(node.GetLeft()); return; } else if (node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp) { AIntConstExp const1 = (AIntConstExp)node.GetLeft(); AIntConstExp const2 = (AIntConstExp)node.GetRight(); int a = int.Parse(const1.GetIntegerLiteral().Text); int b = int.Parse(const2.GetIntegerLiteral().Text); bool c = false; if (node.GetBinop() is AEqBinop) { c = a == b; } else if (node.GetBinop() is ANeBinop) { c = a != b; } ABooleanConstExp booleanExp = new ABooleanConstExp(c ? (PBool) new ATrueBool() : new AFalseBool()); data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null); node.ReplaceBy(booleanExp); return; } else if (node.GetLeft() is ANullExp && node.GetRight() is ANullExp) { ABooleanConstExp booleanExp = new ABooleanConstExp(node.GetBinop() is AEqBinop ? (PBool) new ATrueBool() : new AFalseBool()); data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null); node.ReplaceBy(booleanExp); return; } else if (node.GetLeft() is AStringConstExp && node.GetRight() is AStringConstExp) { AStringConstExp const1 = (AStringConstExp)node.GetLeft(); AStringConstExp const2 = (AStringConstExp)node.GetRight(); string a = const1.GetStringLiteral().Text; string b = const2.GetStringLiteral().Text; bool c = false; if (node.GetBinop() is AEqBinop) { c = a == b; } else if (node.GetBinop() is ANeBinop) { c = a != b; } ABooleanConstExp booleanExp = new ABooleanConstExp(c ? (PBool) new ATrueBool() : new AFalseBool()); data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null); node.ReplaceBy(booleanExp); return; } } if ((node.GetLeft() is ABooleanConstExp || node.GetRight() is ABooleanConstExp) && (node.GetBinop() is ALazyAndBinop || node.GetBinop() is ALazyOrBinop)) { ABooleanConstExp boolExp; PExp other; if (node.GetLeft() is ABooleanConstExp) { boolExp = (ABooleanConstExp)node.GetLeft(); other = node.GetRight(); } else { boolExp = (ABooleanConstExp)node.GetRight(); other = node.GetLeft(); } if (node.GetBinop() is ALazyAndBinop) { if (boolExp.GetBool() is ATrueBool) { //true && <exp> node.ReplaceBy(other); } else { //false && <exp> node.ReplaceBy(boolExp); } } else { if (boolExp.GetBool() is ATrueBool) { //true || <exp> node.ReplaceBy(boolExp); } else { //false || <exp> node.ReplaceBy(other); } } return; } } finally { if (pushed) { PopStack(); } } }
public override void DefaultIn(Node node) { if (!canMerge) { return; } if (node is AMethodDecl) { //First node - no need to fetch if (((AMethodDecl)node).GetFormals().Count != ((AMethodDecl)otherNode).GetFormals().Count) { canMerge = false; } return; } //Fetch corrosponding other node int index = 0; GetChildTypeIndex getChildTypeIndex = new GetChildTypeIndex() { Parent = node.Parent(), Child = node }; node.Parent().Apply(getChildTypeIndex); index = getChildTypeIndex.Index; GetChildTypeByIndex getChildTypeByIndex = new GetChildTypeByIndex() { Child = node, Index = index, Parent = otherNode }; otherNode.Apply(getChildTypeByIndex); otherNode = getChildTypeByIndex.Child; if (otherNode.GetType() != node.GetType()) { canMerge = false; return; } if (node is AALocalDecl) { locals.Add((AALocalDecl)node); otherLocals.Add((AALocalDecl)otherNode); return; } if (node is ANamedType) { ANamedType aNode = (ANamedType)node; ANamedType aOther = (ANamedType)otherNode; if (data.StructTypeLinks.ContainsKey(aNode) != data.StructTypeLinks.ContainsKey(aOther)) { canMerge = false; return; } if (data.StructTypeLinks.ContainsKey(aNode) && data.StructTypeLinks[aNode] != data.StructTypeLinks[aOther]) { canMerge = false; } if (!data.StructTypeLinks.ContainsKey(aNode) && aNode.IsSame(aOther, true))//aNode.GetName().Text != aOther.GetName().Text) { canMerge = false; } if (aNode.IsPrimitive() && !aOther.IsPrimitive(aNode.AsIdentifierString())) { canMerge = false; } return; } if (node is AABlock) { AABlock aNode = (AABlock)node; AABlock aOther = (AABlock)otherNode; if (aNode.GetStatements().Count != aOther.GetStatements().Count) { canMerge = false; } return; } if (node is AIntConstExp) { AIntConstExp aNode = (AIntConstExp)node; AIntConstExp aOther = (AIntConstExp)otherNode; if (aNode.GetIntegerLiteral().Text != aOther.GetIntegerLiteral().Text) { canMerge = false; } return; } if (node is AFixedConstExp) { AFixedConstExp aNode = (AFixedConstExp)node; AFixedConstExp aOther = (AFixedConstExp)otherNode; if (aNode.GetFixedLiteral().Text != aOther.GetFixedLiteral().Text) { canMerge = false; } return; } if (node is AStringConstExp) { AStringConstExp aNode = (AStringConstExp)node; AStringConstExp aOther = (AStringConstExp)otherNode; if (aNode.GetStringLiteral().Text != aOther.GetStringLiteral().Text) { canMerge = false; } return; } if (node is ACharConstExp) { ACharConstExp aNode = (ACharConstExp)node; ACharConstExp aOther = (ACharConstExp)otherNode; if (aNode.GetCharLiteral().Text != aOther.GetCharLiteral().Text) { canMerge = false; } return; } if (node is ASimpleInvokeExp) { ASimpleInvokeExp aNode = (ASimpleInvokeExp)node; ASimpleInvokeExp aOther = (ASimpleInvokeExp)otherNode; if (data.SimpleMethodLinks[aNode] != data.SimpleMethodLinks[aOther] && !(data.SimpleMethodLinks[aNode] == Util.GetAncestor <AMethodDecl>(aNode) && data.SimpleMethodLinks[aOther] == Util.GetAncestor <AMethodDecl>(aOther))) { canMerge = false; } return; } if (node is ALocalLvalue) { ALocalLvalue aNode = (ALocalLvalue)node; ALocalLvalue aOther = (ALocalLvalue)otherNode; if (locals.IndexOf(data.LocalLinks[aNode]) != otherLocals.IndexOf(data.LocalLinks[aOther])) { canMerge = false; } return; } if (node is AFieldLvalue) { AFieldLvalue aNode = (AFieldLvalue)node; AFieldLvalue aOther = (AFieldLvalue)otherNode; if (data.FieldLinks[aNode] != data.FieldLinks[aOther]) { canMerge = false; } return; } if (node is AStructLvalue) { AStructLvalue aNode = (AStructLvalue)node; AStructLvalue aOther = (AStructLvalue)otherNode; if (data.StructFieldLinks[aNode] != data.StructFieldLinks[aOther]) { canMerge = false; } return; } }
public override void OutAIntConstExp(AIntConstExp node) { value = int.Parse(node.GetIntegerLiteral().Text); base.OutAIntConstExp(node); }
public override void CaseAIntConstExp(AIntConstExp node) { Write(node.GetIntegerLiteral().Text); }
public override void CaseADelegateInvokeExp(ADelegateInvokeExp node) { //Build a list of the possible methods AASourceFile currentFile = Util.GetAncestor <AASourceFile>(node); List <AMethodDecl> methods = new List <AMethodDecl>(); ANamedType type = (ANamedType)finalTrans.data.ExpTypes[node.GetReceiver()]; AMethodDecl delegateMethod = finalTrans.data.DelegateTypeLinks[type]; foreach (KeyValuePair <ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod) { if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type)) { if (!methods.Contains(delegateCreationPair.Value)) { methods.Add(delegateCreationPair.Value); } } } MoveMethodDeclsOut mover; if (methods.Count == 0) { //Can only remove it if the return value is unused if (!(node.Parent() is AExpStm)) { finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(), currentFile, "No possible methods found for delegate invoke.")); throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp"); } mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); foreach (Node arg in node.GetArgs()) { arg.Apply(mover); } node.Parent().Parent().RemoveChild(node.Parent()); foreach (PStm stm in mover.NewStatements) { stm.Apply(this); } return; } if (methods.Count == 1) { ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()); while (node.GetArgs().Count > 0) { invoke.GetArgs().Add(node.GetArgs()[0]); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0]))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { node.GetReceiver() }); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = methods[0]; finalTrans.data.ExpTypes[invoke] = methods[0].GetReturnType(); node.ReplaceBy(invoke); return; } //Multiple methods. Make /* * <Methods moved out from reciever> * string delegate = GetMethodPart(<reciever>); * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... * else * { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate.")); * int i = 1/0; * return; * } * */ AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); node.GetReceiver().Apply(mover); AMethodDecl methodPartMethod = GetMethodMethod(); ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"), new ArrayList() { Util.MakeClone(node.GetReceiver(), finalTrans.data) }); finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod; finalTrans.data.ExpTypes[methodPartInvoke] = methodPartMethod.GetReturnType(); AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("methodPart"), methodPartInvoke); block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl)); //If the invoke's return value is used, get the lvalue PLvalue leftSide; if (node.Parent() is AALocalDecl) { leftSide = new ALocalLvalue(new TIdentifier("renameMe")); finalTrans.data.LocalLinks[(ALocalLvalue)leftSide] = (AALocalDecl)node.Parent(); finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null); PStm pStm = Util.GetAncestor <PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block)); node.Parent().RemoveChild(node); } else if (node.Parent() is AAssignmentExp) { AAssignmentExp assignExp = (AAssignmentExp)node.Parent(); leftSide = assignExp.GetLvalue(); leftSide.Apply(mover); PStm pStm = Util.GetAncestor <PStm>(node); pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else if (node.Parent() is AExpStm) { //No assignments needed leftSide = null; node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else { //Create a new local AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(delegateMethod.GetReturnType(), finalTrans.data), new TIdentifier("delegateVar"), null); ALocalLvalue leftSideLink = new ALocalLvalue(new TIdentifier("delegateVar")); ALvalueExp leftSideLinkExp = new ALvalueExp(leftSideLink); PStm pStm = Util.GetAncestor <PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block)); node.ReplaceBy(leftSideLinkExp); finalTrans.data.LocalLinks[leftSideLink] = leftSideDecl; finalTrans.data.LvalueTypes[leftSideLink] = finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType(); leftSide = leftSideLink; block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl)); } ABlockStm elseBranch; //Make final else branch /* { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate.")); * IntToString(1/0); * return; * } */ { AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp playerGroupInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList()); AFieldLvalue messageAreaLink = new AFieldLvalue(new TIdentifier("c_messageAreaDebug")); ALvalueExp messageAreaLinkExp = new ALvalueExp(messageAreaLink); AStringConstExp stringConst = new AStringConstExp( new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" + node.GetToken().Line + ", " + node.GetToken().Pos + "]: Got a null delegate.\"")); ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() { stringConst }); ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"), new ArrayList() { playerGroupInvoke, messageAreaLinkExp, stringToTextInvoke }); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2); ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList() { binop }); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke)); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke)); //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return"))); elseBranch = new ABlockStm(new TLBrace("{"), innerBlock); finalTrans.data.SimpleMethodLinks[playerGroupInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringToTextInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[displayMessageInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[intToStringInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text); finalTrans.data.FieldLinks[messageAreaLink] = finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text); finalTrans.data.ExpTypes[playerGroupInvoke] = finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType(); finalTrans.data.LvalueTypes[messageAreaLink] = finalTrans.data.ExpTypes[messageAreaLinkExp] = finalTrans.data.FieldLinks[messageAreaLink].GetType(); finalTrans.data.ExpTypes[stringToTextInvoke] = finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType(); finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[displayMessageInvoke] = new AVoidType(); finalTrans.data.ExpTypes[intConst1] = finalTrans.data.ExpTypes[intConst2] = finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null); } foreach (AMethodDecl method in methods) { /* * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... */ AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList()); for (int i = 0; i < node.GetArgs().Count; i++) { PExp arg = (PExp)node.GetArgs()[i]; invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data)); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { Util.MakeClone(node.GetReceiver(), data) }); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = method; finalTrans.data.ExpTypes[invoke] = method.GetReturnType(); if (leftSide == null) { innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke)); } else { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke); finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke]; innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } ALocalLvalue methodPartLink = new ALocalLvalue(new TIdentifier("methodPart")); ALvalueExp methodPartLinkExp = new ALvalueExp(methodPartLink); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\"")); finalTrans.data.LocalLinks[methodPartLink] = methodPartDecl; finalTrans.data.LvalueTypes[methodPartLink] = finalTrans.data.ExpTypes[methodPartLinkExp] = finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null); ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst); finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null); AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch); elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList() { ifThenElse }, new TRBrace("}"))); } block.GetStatements().Add(elseBranch); }
private List <PStm> AssignDefault(PLvalue lvalue) { List <PStm> returner = new List <PStm>(); PType type = data.LvalueTypes[lvalue]; PExp rightSide = null; if (type is ANamedType) { ANamedType aType = (ANamedType)type; if (aType.IsPrimitive("string"))//aType.GetName().Text == "string") { rightSide = new AStringConstExp(new TStringLiteral("\"\"")); data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("string"), null); } else if (aType.IsPrimitive(GalaxyKeywords.NullablePrimitives.words)) //GalaxyKeywords.NullablePrimitives.words.Contains(aType.GetName().Text)) { rightSide = new ANullExp(); data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("null"), null); } else if (aType.IsPrimitive(new [] { "int", "byte", "fixed" })) /*aType.GetName().Text == "int" || * aType.GetName().Text == "byte" || * aType.GetName().Text == "fixed")*/ { rightSide = new AIntConstExp(new TIntegerLiteral("0")); data.ExpTypes[rightSide] = type; } else if (aType.IsPrimitive("bool"))//aType.GetName().Text == "bool") { rightSide = new ABooleanConstExp(new AFalseBool()); data.ExpTypes[rightSide] = type; } else if (aType.IsPrimitive("color"))//aType.GetName().Text == "color") { PExp arg1 = new AIntConstExp(new TIntegerLiteral("0")); PExp arg2 = new AIntConstExp(new TIntegerLiteral("0")); PExp arg3 = new AIntConstExp(new TIntegerLiteral("0")); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Color"), new ArrayList() { arg1, arg2, arg3 }); rightSide = invoke; data.ExpTypes[rightSide] = type; data.ExpTypes[arg1] = data.ExpTypes[arg2] = data.ExpTypes[arg3] = new ANamedType(new TIdentifier("int"), null); data.SimpleMethodLinks[invoke] = data.Libraries.Methods.First(func => func.GetName().Text == invoke.GetName().Text); } else if (aType.IsPrimitive("char"))//aType.GetName().Text == "char") { //Dunno?! rightSide = new ACharConstExp(new TCharLiteral("'\0'")); data.ExpTypes[rightSide] = type; } else //Struct { AStructDecl str = data.StructTypeLinks[aType]; foreach (AALocalDecl localDecl in str.GetLocals()) { ALvalueExp reciever = new ALvalueExp(Util.MakeClone(lvalue, data)); AStructLvalue newLvalue = new AStructLvalue(reciever, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text)); data.StructFieldLinks[newLvalue] = localDecl; data.ExpTypes[reciever] = type; data.LvalueTypes[newLvalue] = localDecl.GetType(); returner.AddRange(AssignDefault(newLvalue)); } return(returner); } AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), rightSide); data.ExpTypes[assignment] = type; return(new List <PStm>() { new AExpStm(new TSemicolon(";"), assignment) }); } if (type is AArrayTempType) { AArrayTempType aType = (AArrayTempType)type; for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++) { ALvalueExp reciever = new ALvalueExp(Util.MakeClone(lvalue, data)); AArrayLvalue newLvalue = new AArrayLvalue(new TLBracket("["), reciever, new AIntConstExp(new TIntegerLiteral(i.ToString()))); data.ExpTypes[reciever] = type; data.LvalueTypes[newLvalue] = aType.GetType(); data.ExpTypes[newLvalue.GetIndex()] = new ANamedType(new TIdentifier("int"), null); returner.AddRange(AssignDefault(newLvalue)); } return(returner); } throw new Exception("Unexpected type. (LivenessAnalasys.AssignDefault), got " + type); }
public static bool ReturnsTheSame(PExp left, PExp right, SharedData data) { if (left.GetType() != right.GetType()) { return(false); } if (left is ABinopExp) { ABinopExp aLeft = (ABinopExp)left; ABinopExp aRight = (ABinopExp)right; if (aLeft.GetBinop().GetType() != aRight.GetBinop().GetType()) { return(false); } return(ReturnsTheSame(aLeft.GetLeft(), aRight.GetLeft(), data) && ReturnsTheSame(aLeft.GetRight(), aRight.GetRight(), data)); } if (left is AUnopExp) { AUnopExp aLeft = (AUnopExp)left; AUnopExp aRight = (AUnopExp)right; if (aLeft.GetUnop().GetType() != aRight.GetUnop().GetType()) { return(false); } return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data)); } if (left is AIntConstExp) { AIntConstExp aLeft = (AIntConstExp)left; AIntConstExp aRight = (AIntConstExp)right; return(int.Parse(aLeft.GetIntegerLiteral().Text) == int.Parse(aRight.GetIntegerLiteral().Text)); } if (left is AFixedConstExp) { AFixedConstExp aLeft = (AFixedConstExp)left; AFixedConstExp aRight = (AFixedConstExp)right; return(aLeft.GetFixedLiteral().Text == aRight.GetFixedLiteral().Text); } if (left is AStringConstExp) { AStringConstExp aLeft = (AStringConstExp)left; AStringConstExp aRight = (AStringConstExp)right; return(aLeft.GetStringLiteral().Text == aRight.GetStringLiteral().Text); } if (left is ACharConstExp) { ACharConstExp aLeft = (ACharConstExp)left; ACharConstExp aRight = (ACharConstExp)right; return(aLeft.GetCharLiteral().Text == aRight.GetCharLiteral().Text); } if (left is ABooleanConstExp) { ABooleanConstExp aLeft = (ABooleanConstExp)left; ABooleanConstExp aRight = (ABooleanConstExp)right; return(aLeft.GetBool().GetType() == aRight.GetBool().GetType()); } if (left is ASimpleInvokeExp) { //A method might not return the same thing each time it is called return(false); } if (left is ALvalueExp) { ALvalueExp aLeft = (ALvalueExp)left; ALvalueExp aRight = (ALvalueExp)right; return(ReturnsTheSame(aLeft.GetLvalue(), aRight.GetLvalue(), data)); } if (left is AParenExp) { AParenExp aLeft = (AParenExp)left; AParenExp aRight = (AParenExp)right; return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data)); } throw new Exception("Util.ReturnsTheSame. Unexpected type, got " + left.GetType()); }