public override void CaseAWhileStm(AWhileStm node) { /* * while(...){...} * -> * while(...){...} * if (hasMethodReturnedVar) * { * break; * } */ if (neededWhile) { ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(hasMethodReturnedVar.GetName().Text)); data.LvalueTypes[lvalue] = hasMethodReturnedVar.GetType(); data.LocalLinks[lvalue] = hasMethodReturnedVar; ALvalueExp exp = new ALvalueExp(lvalue); data.ExpTypes[exp] = hasMethodReturnedVar.GetType(); AABlock ifBlock = new AABlock(); ifBlock.GetStatements().Add(new ABreakStm(new TBreak("break"))); ABlockStm ifBlockStm = new ABlockStm(new TLBrace("{"), ifBlock); AIfThenStm ifStm = new AIfThenStm(new TLParen("("), exp, ifBlockStm); AABlock pBlock = (AABlock)node.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node) + 1, ifStm); } node.GetBody().Apply(this); }
/*public override void CaseAValueReturnStm(AValueReturnStm node) * { * GetNode(node); * PStm stm = GetNext(whileStm); * if (stm != null) * graphNode.AddSucc(GetNode(stm)); * }*/ public override void CaseAIfThenStm(AIfThenStm node) { //Create graph node Node graphNode = GetNode(node); //Process inner if node.GetBody().Apply(this); //Add successor and predessors PStm stm = GetFirst(node.GetBody()); if (stm != null) { graphNode.AddSucc(GetNode(stm)); } stm = GetNext(node); if (stm != null) { Node nextGraphNode = GetNode(stm); graphNode.AddSucc(nextGraphNode); List <PStm> stms = GetLast(node.GetBody()); foreach (PStm pStm in stms) { nextGraphNode.AddPred(GetNode(pStm)); } } }
public override void CaseAIfThenStm(AIfThenStm node) { Write("if("); node.GetCondition().Apply(this); Write(")\n"); node.GetBody().Apply(this); }
public override void OutAIfThenStm(AIfThenStm node) { bool value; if (IsConst(node.GetCondition(), out value)) { if (value) { node.ReplaceBy(node.GetBody()); } else { node.Parent().RemoveChild(node); } changedSomething = true; } }
//Returns true if execution cannot continue after the stm private bool removeDeadCode(PStm stm) { if (stm is ABreakStm || stm is AContinueStm || stm is AVoidReturnStm || stm is AValueReturnStm) { return(true); } if (stm is AIfThenStm) { AIfThenStm aStm = (AIfThenStm)stm; bool stopped = removeDeadCode(aStm.GetBody()); if (IsBoolConst(aStm.GetCondition(), true)) { return(stopped); } return(false); } if (stm is AIfThenElseStm) { AIfThenElseStm aStm = (AIfThenElseStm)stm; bool stopped1 = removeDeadCode(aStm.GetThenBody()); if (IsBoolConst(aStm.GetCondition(), true)) { return(stopped1); } bool stopped2 = removeDeadCode(aStm.GetElseBody()); if (IsBoolConst(aStm.GetCondition(), false)) { return(stopped2); } return(stopped1 && stopped2); } if (stm is AWhileStm) { AWhileStm aStm = (AWhileStm)stm; removeDeadCode(aStm.GetBody()); return(false); } if (stm is ABlockStm) { ABlockStm aStm = (ABlockStm)stm; return(removeDeadCode((AABlock)aStm.GetBlock())); } return(false); }
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); }
public override void CaseAIfThenStm(AIfThenStm node) { node.GetBody().Apply(this); }