public override void CaseALocalDeclStm(ALocalDeclStm node) { AMethodDecl pMethod = Util.GetAncestor<AMethodDecl>(node); AALocalDecl decl = (AALocalDecl) node.GetLocalDecl(); if (decl.GetInit() == null) { ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(decl.GetName().Text)); data.LocalLinks[lvalue] = decl; data.LvalueTypes[lvalue] = decl.GetType(); List<PStm> statements = AssignDefault(lvalue); AABlock pBlock = (AABlock)node.Parent(); foreach (PStm statement in statements) { pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), statement); } pBlock.RemoveChild(node); } else { //Make an assignment expression before moving ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(decl.GetName().Text)); data.LvalueTypes[lvalue] = decl.GetType(); AAssignmentExp exp = new AAssignmentExp(new TAssign("="), lvalue, decl.GetInit()); AExpStm expStm = new AExpStm(new TSemicolon(";"), exp); node.ReplaceBy(expStm); data.LvalueTypes[lvalue] = decl.GetType(); data.ExpTypes[exp] = decl.GetType(); data.LocalLinks[lvalue] = decl; } AABlock block = (AABlock) pMethod.GetBlock(); block.GetStatements().Insert(0, node); }
public override void CaseAAssignmentExp(AAssignmentExp node) { currentLocal = null; node.GetLvalue().Apply(this); if (currentLocal != null) { AALocalDecl decl = data.LocalLinks[currentLocal]; NeededRefs[Util.GetAncestor<AMethodDecl>(node)].Add(decl); } node.GetExp().Apply(this); }
public override void CaseALocalLvalue(ALocalLvalue node) { if (setUses) { PStm parentStm = Util.GetAncestor<PStm>(node); AALocalDecl decl = finalTrans.data.LocalLinks[node]; AAssignmentExp assignment = Util.GetAncestor<AAssignmentExp>(node); bool isUsage = true; if (assignment != null) { if (Util.IsAncestor(node, assignment.GetLvalue())) { AArrayLvalue arrayLvalue = Util.GetAncestor<AArrayLvalue>(node); isUsage = false; while (arrayLvalue != null) { if (Util.IsAncestor(node, arrayLvalue.GetIndex())) { isUsage = true; break; } arrayLvalue = Util.GetAncestor<AArrayLvalue>(arrayLvalue.Parent()); } if (!isUsage) { //If we have a bulk copy here, it is really only a partial assignment, and so we should not add it as an assignment if (!Util.IsBulkCopy(decl.GetType())) assigns[parentStm] = finalTrans.data.LocalLinks[node]; } } } if (isUsage) { if (!uses[parentStm].Contains(decl)) uses[parentStm].Add(decl); } } if (fixRefferences) { AALocalDecl decl = finalTrans.data.LocalLinks[node]; while (renamedLocals.ContainsKey(decl)) { decl = renamedLocals[decl]; } finalTrans.data.LocalLinks[node] = decl; node.GetName().Text = decl.GetName().Text; } base.CaseALocalLvalue(node); }
//Convert struct variables to a collection of local variables public override void CaseAALocalDecl(AALocalDecl node) { if (node.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType) node.GetType()) && Util.HasAncestor<PStm>(node)) { //Can not have init - it would be bulk copy AStructDecl str = data.StructTypeLinks[(ANamedType) node.GetType()]; Dictionary<AALocalDecl, AALocalDecl> variableMap = new Dictionary<AALocalDecl, AALocalDecl>(); PStm pStm = (PStm) node.Parent(); AABlock pBlock = (AABlock) pStm.Parent(); foreach (AALocalDecl structLocal in str.GetLocals()) { AALocalDecl replacementLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(structLocal.GetType(), data), new TIdentifier(node.GetName().Text + "_" + structLocal.GetName().Text), null); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), replacementLocal)); AALocalDecl baseLocal = structLocal; if (data.EnheritanceLocalMap.ContainsKey(baseLocal)) baseLocal = data.EnheritanceLocalMap[baseLocal]; List<AALocalDecl> localsToAdd = new List<AALocalDecl>(); localsToAdd.AddRange(data.EnheritanceLocalMap.Where(pair => pair.Value == baseLocal).Select(pair => pair.Key)); localsToAdd.Add(baseLocal); foreach (AALocalDecl localDecl in localsToAdd) { variableMap[localDecl] = replacementLocal; } } List<ALocalLvalue> uses = new List<ALocalLvalue>(); uses.AddRange(data.LocalLinks.Where(k => k.Value == node && Util.GetAncestor<AAProgram>(k.Key) != null).Select(k => k.Key)); foreach (ALocalLvalue lvalue in uses) { AStructLvalue structLocalRef = (AStructLvalue) lvalue.Parent().Parent(); AALocalDecl replacementLocal = variableMap[data.StructFieldLinks[structLocalRef]]; ALocalLvalue replacementLvalue = new ALocalLvalue(new TIdentifier(replacementLocal.GetName().Text)); data.LocalLinks[replacementLvalue] = replacementLocal; data.LvalueTypes[replacementLvalue] = replacementLocal.GetType(); structLocalRef.ReplaceBy(replacementLvalue); } foreach (AALocalDecl replacementLocal in variableMap.Select(k => k.Value)) { replacementLocal.Apply(this); } } base.CaseAALocalDecl(node); }
public override void OutAALocalDecl(AALocalDecl node) { if (!Util.HasAncestor<AABlock>(node) && !Util.HasAncestor<AMethodDecl>(node)) { //OutStructFieldDecl(node); return; } if (node.GetInit() != null) return; AABlock pBlock; int insertIndex; PLvalue lvalue; if (Util.HasAncestor<AABlock>(node)) { //A local variable pBlock = Util.GetAncestor<AABlock>(node); insertIndex = pBlock.GetStatements().IndexOf(Util.GetAncestor<PStm>(node)) + 1; lvalue = new ALocalLvalue(new TIdentifier(node.GetName().Text)); data.LocalLinks[(ALocalLvalue) lvalue] = node; data.LvalueTypes[lvalue] = node.GetType(); } else { //Parameter //Parameters will be set from the caller return; pBlock = (AABlock) Util.GetAncestor<AMethodDecl>(node).GetBlock(); insertIndex = 0; lvalue = new ALocalLvalue(new TIdentifier(node.GetName().Text)); data.LocalLinks[(ALocalLvalue)lvalue] = node; data.LvalueTypes[lvalue] = node.GetType(); } AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); MakeAssignments(block, node.GetType(), lvalue, true); if (block.GetStatements().Count != 0) pBlock.GetStatements().Insert(insertIndex, new ABlockStm(new TLBrace("{"), block)); }
public override void CaseASimpleInvokeExp(ASimpleInvokeExp node) { AMethodDecl target = data.SimpleMethodLinks[node]; for (int i = 0; i < node.GetArgs().Count; i++) { PExp arg = (PExp)node.GetArgs()[i]; currentLocal = null; arg.Apply(this); if (currentLocal != null && target.GetFormals().Cast<AALocalDecl>().ToList()[i].GetRef() != null) { ALocalLvalue local = currentLocal; target.Apply(this); if (target.GetFormals().Cast<AALocalDecl>().ToList()[i].GetRef() != null) { AALocalDecl decl = data.LocalLinks[local]; NeededRefs[Util.GetAncestor<AMethodDecl>(node)].Add(decl); } } } }
public override void CaseASimpleInvokeExp(ASimpleInvokeExp node) { PExp expNode = (PExp)node; PType type = data.ExpTypes[expNode]; if (type is APointerType) type = new ANamedType(new TIdentifier("string"), null); ALocalLvalue local = new ALocalLvalue(new TIdentifier("tempName", 0, 0)); ALvalueExp exp = new ALvalueExp(local); PStm stm = Util.GetAncestor<PStm>(node); AABlock block = (AABlock)stm.Parent(); node.ReplaceBy(exp); AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(type, data), new TIdentifier(varName, 0, 0), expNode); ALocalDeclStm newStm = new ALocalDeclStm(new TSemicolon(";"), localDecl); block.GetStatements().Insert(block.GetStatements().IndexOf(stm), newStm); NewStatements.Add(newStm); data.LvalueTypes[local] = type; data.ExpTypes[exp] = type; data.LocalLinks[local] = localDecl; //localDecl.Apply(this); exp.Apply(this); return; }
public override void CaseALocalLvalue(ALocalLvalue node) { //Only if it is not what it is assigning to AAssignmentExp exp = Util.GetAncestor<AAssignmentExp>(node); if (exp != null && node == exp.GetLvalue()) return; UsedLocals.Add(data.LocalLinks[node]); }
public override void OutALocalLvalue(ALocalLvalue node) { if (currentMethod == null) return; AALocalDecl decl = data.LocalLinks[node]; Node n = node; while (true) { n = Util.GetNearestAncestor(n.Parent(), typeof(AMethodDecl), typeof(AAssignmentExp), typeof(AArrayLvalue)); if (n is AMethodDecl) break; if (n is AAssignmentExp) { if (Util.IsAncestor(node, ((AAssignmentExp)n).GetLvalue())) { if (!WrittenLocals[currentMethod].Contains(decl)) WrittenLocals[currentMethod].Add(decl); return; } break; } if (n is AArrayLvalue) { if (Util.IsAncestor(node, ((AArrayLvalue)n).GetBase())) continue; break; } break; } if (!ReadLocals[currentMethod].Contains(decl)) ReadLocals[currentMethod].Add(decl); }
public override void DefaultIn(Node 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 = currentCloneNode }; currentCloneNode.Apply(getChildTypeByIndex); currentCloneNode = getChildTypeByIndex.Child; //currentCloneNode should now be the clone corrosponding to node. /*finalTrans.data.ExpTypes finalTrans.data.FieldLinks finalTrans.data.LocalLinks finalTrans.data.Locals//Lets not forget about this one finalTrans.data.LvalueTypes finalTrans.data.SimpleMethodLinks finalTrans.data.StructFieldLinks finalTrans.data.StructMethodLinks finalTrans.data.StructTypeLinks*/ if (node is AABlock) data.Locals[(AABlock) currentCloneNode] = new List<AALocalDecl>(); if (node is PExp) data.ExpTypes[(PExp)currentCloneNode] = data.ExpTypes[(PExp)node]; if (node is AFieldLvalue) data.FieldLinks[(AFieldLvalue)currentCloneNode] = data.FieldLinks[(AFieldLvalue)node]; if (node is ALocalLvalue) { PLvalue replacer = Util.MakeClone(localMap[data.LocalLinks[(ALocalLvalue)node]], data); currentCloneNode.ReplaceBy(replacer); currentCloneNode = replacer; } if (node is AALocalDecl) { ALocalLvalue replacer = new ALocalLvalue(new TIdentifier("IwillGetRenamedLater")); data.LvalueTypes[replacer] = ((AALocalDecl)currentCloneNode).GetType(); data.LocalLinks[replacer] = (AALocalDecl)currentCloneNode; AABlock pBlock = Util.GetAncestor<AABlock>(currentCloneNode) ?? (AABlock) Util.GetAncestor<AMethodDecl>(currentCloneNode).GetBlock(); data.Locals[Util.GetAncestor<AABlock>(currentCloneNode)].Add((AALocalDecl) currentCloneNode); localMap.Add((AALocalDecl)node, replacer); } if (node is PLvalue) data.LvalueTypes[(PLvalue)currentCloneNode] = data.LvalueTypes[(PLvalue)node]; if (node is ASimpleInvokeExp) data.SimpleMethodLinks[(ASimpleInvokeExp)currentCloneNode] = data.SimpleMethodLinks[(ASimpleInvokeExp)node]; if (node is AStructLvalue) data.StructFieldLinks[(AStructLvalue)currentCloneNode] = data.StructFieldLinks[(AStructLvalue)node]; if (node is ANonstaticInvokeExp) data.StructMethodLinks[(ANonstaticInvokeExp)currentCloneNode] = data.StructMethodLinks[(ANonstaticInvokeExp)node]; if (node is ANamedType && data.StructTypeLinks.Keys.Contains(node)) data.StructTypeLinks[(ANamedType)currentCloneNode] = data.StructTypeLinks[(ANamedType)node]; if (extraCheck != null) extraCheck(node, currentCloneNode); if (node is PType && data.EnrichmentTypeLinks.ContainsKey((PType) node)) data.EnrichmentTypeLinks[(PType)currentCloneNode] = data.EnrichmentTypeLinks[(PType)node]; if (node is AStringConstExp) { if (data.StringsDontJoinRight.Contains(node)) data.StringsDontJoinRight.Add((AStringConstExp) currentCloneNode); } }
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, LocRM.GetString("Delegates_Text1"))); 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); }
public override void OutALocalLvalue(ALocalLvalue node) { currentDecl = new VariableDecl(null, data.LocalLinks[node], null); }
private AMethodDecl GetIntPointerMethod() { if (GetIntPointerPartMethod != null) return GetIntPointerPartMethod; /* * int GetIntPointerPart(string delegate) * { * return IntToString(GetPointerPart(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")); ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1); ASimpleInvokeExp getPointerPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetPointerPart"), new ArrayList() { delegateRef1Exp }); ASimpleInvokeExp StringToIntInvoke = new ASimpleInvokeExp(new TIdentifier("StringToInt"), new ArrayList() { getPointerPartInvoke }); GetIntPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() { delegateFormal }, new AABlock( new ArrayList() { new AValueReturnStm(new TReturn("return"), StringToIntInvoke) }, new TRBrace("}"))); sourceFile.GetDecl().Add(GetIntPointerPartMethod); data.Methods.Add(new SharedData.DeclItem<AMethodDecl>(sourceFile, GetIntPointerPartMethod)); finalTrans.data.LocalLinks[delegateRef1] = delegateFormal; finalTrans.data.LvalueTypes[delegateRef1] = finalTrans.data.ExpTypes[delegateRef1Exp] = finalTrans.data.ExpTypes[getPointerPartInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[StringToIntInvoke] = new ANamedType(new TIdentifier("int"), null); finalTrans.data.SimpleMethodLinks[getPointerPartInvoke] = GetStringPointerMethod(); finalTrans.data.SimpleMethodLinks[StringToIntInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == StringToIntInvoke.GetName().Text); return GetIntPointerPartMethod; }
public override void OutALocalLvalue(ALocalLvalue node) { node.GetName().Text = finalTrans.data.LocalLinks[node].GetName().Text; }
private AMethodDecl CreateStringDeobfuscator() { AASourceFile file = (AASourceFile) finalTrans.mainEntry.Parent(); //Create fields for the string constants AStringConstExp emptyStringConst = new AStringConstExp(new TStringLiteral("\"\"")); AFieldDecl emptyStringField = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"), new ANamedType(new TIdentifier("string"), null), new TIdentifier("fOobar"), emptyStringConst); file.GetDecl().Add(emptyStringField); data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, emptyStringField)); AFieldLvalue emptyStringRef1 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text)); AFieldLvalue emptyStringRef2 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text)); AFieldLvalue emptyStringRef3 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text)); ALvalueExp emptyStringRef1Exp = new ALvalueExp(emptyStringRef1); ALvalueExp emptyStringRef2Exp = new ALvalueExp(emptyStringRef2); ALvalueExp emptyStringRef3Exp = new ALvalueExp(emptyStringRef3); AStringConstExp colonStringConst = new AStringConstExp(new TStringLiteral("\":\"")); AFieldDecl colonStringField = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"), new ANamedType(new TIdentifier("string"), null), new TIdentifier("foObar"), colonStringConst); file.GetDecl().Add(colonStringField); data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, colonStringField)); AFieldLvalue colonStringRef = new AFieldLvalue(new TIdentifier(colonStringField.GetName().Text)); ALvalueExp colonStringRefExp = new ALvalueExp(colonStringRef); /* string output = ""; string ch; int length = StringLength(s); int phase1 = (length - 1)%3; int phase2 = (length - 1)%2; */ AALocalDecl stringParam = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("fo0bar"), emptyStringRef1Exp); ALocalLvalue stringParamRef1 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef2 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef3 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef4 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef5 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef6 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef7 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALocalLvalue stringParamRef8 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text)); ALvalueExp stringParamRef1Exp = new ALvalueExp(stringParamRef1); ALvalueExp stringParamRef2Exp = new ALvalueExp(stringParamRef2); ALvalueExp stringParamRef4Exp = new ALvalueExp(stringParamRef4); ALvalueExp stringParamRef5Exp = new ALvalueExp(stringParamRef5); ALvalueExp stringParamRef7Exp = new ALvalueExp(stringParamRef7); ALvalueExp stringParamRef8Exp = new ALvalueExp(stringParamRef8); AABlock methodBlock = new AABlock(new ArrayList(), new TRBrace("}")); AALocalDecl outputDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("foobar"), emptyStringRef1Exp); methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), outputDecl)); ALocalLvalue outputRef1 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef2 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef3 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef4 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef5 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef6 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef7 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef8 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef9 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef10 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef11 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef12 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALocalLvalue outputRef13 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text)); ALvalueExp outputRef2Exp = new ALvalueExp(outputRef2); ALvalueExp outputRef4Exp = new ALvalueExp(outputRef4); ALvalueExp outputRef5Exp = new ALvalueExp(outputRef5); ALvalueExp outputRef6Exp = new ALvalueExp(outputRef6); ALvalueExp outputRef7Exp = new ALvalueExp(outputRef7); ALvalueExp outputRef8Exp = new ALvalueExp(outputRef8); ALvalueExp outputRef10Exp = new ALvalueExp(outputRef10); ALvalueExp outputRef11Exp = new ALvalueExp(outputRef11); ALvalueExp outputRef12Exp = new ALvalueExp(outputRef12); ALvalueExp outputRef13Exp = new ALvalueExp(outputRef13); AALocalDecl chDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("f0obar"), null); methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), chDecl)); ALocalLvalue chRef1 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text)); ALocalLvalue chRef2 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text)); ALocalLvalue chRef3 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text)); ALocalLvalue chRef4 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text)); ALocalLvalue chRef5 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text)); ALvalueExp chRef3Exp = new ALvalueExp(chRef3); ALvalueExp chRef4Exp = new ALvalueExp(chRef4); ALvalueExp chRef5Exp = new ALvalueExp(chRef5); ASimpleInvokeExp stringLengthInvoke1 = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { stringParamRef1Exp }); AALocalDecl lengthDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("f0Obar"), stringLengthInvoke1); methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), lengthDecl)); ALocalLvalue lengthRef1 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef2 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef3 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef4 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef5 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef6 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef7 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALocalLvalue lengthRef8 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text)); ALvalueExp lengthRef1Exp = new ALvalueExp(lengthRef1); ALvalueExp lengthRef2Exp = new ALvalueExp(lengthRef2); ALvalueExp lengthRef3Exp = new ALvalueExp(lengthRef3); ALvalueExp lengthRef4Exp = new ALvalueExp(lengthRef4); ALvalueExp lengthRef5Exp = new ALvalueExp(lengthRef5); ALvalueExp lengthRef6Exp = new ALvalueExp(lengthRef6); ALvalueExp lengthRef7Exp = new ALvalueExp(lengthRef7); AIntConstExp intConstp1Init1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConstp1Init2 = new AIntConstExp(new TIntegerLiteral("3")); ABinopExp binopExpP1InitMinus = new ABinopExp(lengthRef1Exp, new AMinusBinop(new TMinus("-")), intConstp1Init1); ABinopExp binopExpP1InitMod = new ABinopExp(binopExpP1InitMinus, new AModuloBinop(new TMod("%")), intConstp1Init2); AALocalDecl phase1Decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("fO0bar"), binopExpP1InitMod); methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), phase1Decl)); ALocalLvalue phase1Ref1 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref2 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref3 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref4 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref5 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref6 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref7 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref8 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref9 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref10 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALocalLvalue phase1Ref11 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text)); ALvalueExp phase1Ref1Exp = new ALvalueExp(phase1Ref1); ALvalueExp phase1Ref2Exp = new ALvalueExp(phase1Ref2); ALvalueExp phase1Ref4Exp = new ALvalueExp(phase1Ref4); ALvalueExp phase1Ref5Exp = new ALvalueExp(phase1Ref5); ALvalueExp phase1Ref7Exp = new ALvalueExp(phase1Ref7); ALvalueExp phase1Ref9Exp = new ALvalueExp(phase1Ref9); ALvalueExp phase1Ref10Exp = new ALvalueExp(phase1Ref10); ALvalueExp phase1Ref11Exp = new ALvalueExp(phase1Ref11); AIntConstExp intConstp2Init1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConstp2Init2 = new AIntConstExp(new TIntegerLiteral("2")); ABinopExp binopExpP2InitMinus = new ABinopExp(lengthRef2Exp, new AMinusBinop(new TMinus("-")), intConstp2Init1); ABinopExp binopExpP2InitMod = new ABinopExp(binopExpP2InitMinus, new AModuloBinop(new TMod("%")), intConstp2Init2); AALocalDecl phase2Decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null), new TIdentifier("carl"), binopExpP2InitMod); methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), phase2Decl)); ALocalLvalue phase2Ref1 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref2 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref3 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref4 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref5 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref6 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref7 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref8 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALocalLvalue phase2Ref9 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text)); ALvalueExp phase2Ref1Exp = new ALvalueExp(phase2Ref1); ALvalueExp phase2Ref2Exp = new ALvalueExp(phase2Ref2); ALvalueExp phase2Ref4Exp = new ALvalueExp(phase2Ref4); ALvalueExp phase2Ref5Exp = new ALvalueExp(phase2Ref5); ALvalueExp phase2Ref7Exp = new ALvalueExp(phase2Ref7); ALvalueExp phase2Ref9Exp = new ALvalueExp(phase2Ref9); /* while(length > 0) { if(phase2 == 0) { ch = StringSub(s, 1, 1); s = StringReplace(s, "", 1, 1); } else { if(phase2 == 1) { ch = StringSub(s, length, length); s = StringReplace(s, "", length, length); } } if(phase1 == 0) { output = ch + output; } else { if(phase1 == 1) { output = StringSub(output, 1, (StringLength(output) + 1)/2) + ch + StringSub(output, (StringLength(output) + 1)/2 + 1, StringLength(output)); } else { output = output + ch; } } phase1 = phase1 - 1; if(phase1 < 0) { phase1 = phase1 + 3; } phase2 = phase2 - 1; if(phase2 < 0) { phase2 = phase2 + 2; } length = StringLength(s); } */ AABlock whileBlock = new AABlock(new ArrayList(), new TRBrace("}")); AIntConstExp intConstWhileCond = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binopWhileCond = new ABinopExp(lengthRef3Exp, new AGtBinop(new TGt(">")), intConstWhileCond); methodBlock.GetStatements().Add(new AWhileStm(new TLParen("("), binopWhileCond, new ABlockStm(new TLBrace("{"), whileBlock))); /* if(phase2 == 0) { ch = StringSub(s, 1, 1); s = StringReplace(s, "", 1, 1); } else { if(phase2 == 1) { ch = StringSub(s, length, length); s = StringReplace(s, "", length, length); } } */ AIntConstExp intConstIf1Cond = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binopIf1Cond = new ABinopExp(phase2Ref1Exp, new AEqBinop(new TEq("==")), intConstIf1Cond); AABlock thenBlock = new AABlock(); AABlock elseBlock = new AABlock(); whileBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binopIf1Cond, new ABlockStm(new TLBrace("{"), thenBlock), new ABlockStm(new TLBrace("{"), elseBlock))); //ch = StringSub(s, 1, 1); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("1")); ASimpleInvokeExp invokeStringSub1 = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() {stringParamRef2Exp, intConst1, intConst2}); AAssignmentExp assignment1 = new AAssignmentExp(new TAssign("="), chRef1, invokeStringSub1); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment1)); //s = StringReplace(s, "", 1, 1); AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst4 = new AIntConstExp(new TIntegerLiteral("1")); ASimpleInvokeExp invokeStringReplace1 = new ASimpleInvokeExp(new TIdentifier("StringReplace"), new ArrayList() { stringParamRef4Exp, emptyStringRef2Exp, intConst3, intConst4 }); AAssignmentExp assignment2 = new AAssignmentExp(new TAssign("="), stringParamRef3, invokeStringReplace1); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment2)); //if(phase2 == 1) AIntConstExp intConst5 = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop1 = new ABinopExp(phase2Ref2Exp, new AEqBinop(new TEq("==")), intConst5); thenBlock = new AABlock(); elseBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop1, new ABlockStm(new TLBrace("{"), thenBlock))); //ch = StringSub(s, length, length); ASimpleInvokeExp invokeStringSub2 = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { stringParamRef5Exp, lengthRef3Exp, lengthRef4Exp }); AAssignmentExp assignment3 = new AAssignmentExp(new TAssign("="), chRef2, invokeStringSub2); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment3)); //s = StringReplace(s, "", length, length); ASimpleInvokeExp invokeStringReplace2 = new ASimpleInvokeExp(new TIdentifier("StringReplace"), new ArrayList() { stringParamRef7Exp, emptyStringRef3Exp, lengthRef5Exp, lengthRef6Exp }); AAssignmentExp assignment4 = new AAssignmentExp(new TAssign("="), stringParamRef6, invokeStringReplace2); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment4)); //if(phase1 == 0) AIntConstExp intConst6 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop2 = new ABinopExp(phase1Ref1Exp, new AEqBinop(new TEq("==")), intConst6); thenBlock = new AABlock(); elseBlock = new AABlock(); whileBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binop2, new ABlockStm(new TLBrace("{"), thenBlock), new ABlockStm(new TLBrace("{"), elseBlock))); //output = ch + output; ABinopExp binop3 = new ABinopExp(chRef3Exp, new APlusBinop(new TPlus("+")), outputRef2Exp); AAssignmentExp assignment5 = new AAssignmentExp(new TAssign("="), outputRef1, binop3); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment5)); //if(phase1 == 1) AABlock cBlock = elseBlock; AIntConstExp intConst7 = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop4 = new ABinopExp(phase1Ref2Exp, new AEqBinop(new TEq("==")), intConst7); thenBlock = new AABlock(); elseBlock = new AABlock(); cBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binop4, new ABlockStm(new TLBrace("{"), thenBlock), new ABlockStm(new TLBrace("{"), elseBlock))); //output = StringSub(output, 1, (StringLength(output) + 1)/2) + ch + StringSub(output, (StringLength(output) + 1)/2 + 1, StringLength(output)); AIntConstExp intConst8 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst9 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst10 = new AIntConstExp(new TIntegerLiteral("2")); AIntConstExp intConst11 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst12 = new AIntConstExp(new TIntegerLiteral("2")); AIntConstExp intConst13 = new AIntConstExp(new TIntegerLiteral("1")); ASimpleInvokeExp invokeStringLength1 = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() {outputRef5Exp}); ABinopExp binop5 = new ABinopExp(invokeStringLength1, new APlusBinop(new TPlus("+")), intConst9); ABinopExp binop6 = new ABinopExp(binop5, new ADivideBinop(new TDiv("/")), intConst10); ASimpleInvokeExp invokeStringSub3 = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { outputRef4Exp, intConst8, binop6}); ABinopExp binop7 = new ABinopExp(invokeStringSub3, new APlusBinop(new TPlus("+")), chRef4Exp); ASimpleInvokeExp invokeStringLength2 = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { outputRef7Exp }); ABinopExp binop8 = new ABinopExp(invokeStringLength2, new APlusBinop(new TPlus("+")), intConst11); ABinopExp binop9 = new ABinopExp(binop8, new ADivideBinop(new TDiv("/")), intConst12); ABinopExp binop10 = new ABinopExp(binop9, new APlusBinop(new TPlus("+")), intConst13); ASimpleInvokeExp invokeStringLength3 = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { outputRef8Exp }); ASimpleInvokeExp invokeStringSub4 = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { outputRef6Exp, binop10, invokeStringLength3 }); ABinopExp binop11 = new ABinopExp(binop7, new APlusBinop(new TPlus("+")), invokeStringSub4); AAssignmentExp assignment6 = new AAssignmentExp(new TAssign("="), outputRef3, binop11); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment6)); //output = output + ch; ABinopExp binop12 = new ABinopExp(outputRef10Exp, new APlusBinop(new TPlus("+")), chRef5Exp); AAssignmentExp assignment7 = new AAssignmentExp(new TAssign("="), outputRef9, binop12); elseBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment7)); //phase1 = phase1 - 1; AIntConstExp intConst14 = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop13 = new ABinopExp(phase1Ref4Exp, new AMinusBinop(new TMinus("-")), intConst14); AAssignmentExp assignment8 = new AAssignmentExp(new TAssign("="), phase1Ref3, binop13); whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment8)); //if(phase1 < 0) AIntConstExp intConst15 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop14 = new ABinopExp(phase1Ref5Exp, new ALtBinop(new TLt("<")), intConst15); thenBlock = new AABlock(); whileBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop14, new ABlockStm(new TLBrace("{"), thenBlock))); //phase1 = phase1 + 3; AIntConstExp intConst16 = new AIntConstExp(new TIntegerLiteral("3")); ABinopExp binop15 = new ABinopExp(phase1Ref7Exp, new APlusBinop(new TPlus("+")), intConst16); AAssignmentExp assignment9 = new AAssignmentExp(new TAssign("="), phase1Ref6, binop15); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment9)); //phase2 = phase2 - 1; AIntConstExp intConst17 = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop16 = new ABinopExp(phase2Ref4Exp, new AMinusBinop(new TMinus("-")), intConst17); AAssignmentExp assignment10 = new AAssignmentExp(new TAssign("="), phase2Ref3, binop16); whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment10)); //if(phase2 < 0) AIntConstExp intConst18 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop17 = new ABinopExp(phase2Ref5Exp, new ALtBinop(new TLt("<")), intConst18); thenBlock = new AABlock(); whileBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop17, new ABlockStm(new TLBrace("{"), thenBlock))); //phase2 = phase2 + 2; AIntConstExp intConst19 = new AIntConstExp(new TIntegerLiteral("2")); ABinopExp binop18 = new ABinopExp(phase2Ref7Exp, new APlusBinop(new TPlus("+")), intConst19); AAssignmentExp assignment11 = new AAssignmentExp(new TAssign("="), phase2Ref6, binop18); thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment11)); //length = StringLength(s); ASimpleInvokeExp invokeStringLength4 = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { stringParamRef8Exp }); AAssignmentExp assignment12 = new AAssignmentExp(new TAssign("="), lengthRef8, invokeStringLength4); whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment12)); /* phase1 = StringFind(output, ":", false); phase2 = StringToInt(StringSub(output, 1, phase1 - 1)); return StringSub(output, phase1 + 1, phase2 + phase1); */ ABooleanConstExp boolConst1 = new ABooleanConstExp(new AFalseBool()); ASimpleInvokeExp invokeStringFind = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList() {outputRef11Exp, colonStringRefExp, boolConst1}); AAssignmentExp assignment13 = new AAssignmentExp(new TAssign("="), phase1Ref8, invokeStringFind); methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment13)); //phase2 = StringToInt(StringSub(output, 1, phase1 - 1)); AIntConstExp intConst20 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst21 = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop19 = new ABinopExp(phase1Ref9Exp, new AMinusBinop(new TMinus("-")), intConst21); ASimpleInvokeExp invokeStringSub5 = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { outputRef12Exp, intConst20, binop19}); ASimpleInvokeExp invokeStringToInt = new ASimpleInvokeExp(new TIdentifier("StringToInt"), new ArrayList() { invokeStringSub5 }); AAssignmentExp assignment14 = new AAssignmentExp(new TAssign("="), phase2Ref8, invokeStringToInt); methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment14)); //return StringSub(output, phase1 + 1, phase2 + phase1); AIntConstExp intConst22 = new AIntConstExp(new TIntegerLiteral("1")); ABinopExp binop20 = new ABinopExp(phase1Ref10Exp, new APlusBinop(new TPlus("+")), intConst22); ABinopExp binop21 = new ABinopExp(phase2Ref9Exp, new APlusBinop(new TPlus("+")), phase1Ref11Exp); ASimpleInvokeExp invokeStringSub6 = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList() { outputRef12Exp, binop20, binop21 }); methodBlock.GetStatements().Add(new AValueReturnStm(new TReturn("return"), invokeStringSub6)); AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("Galaxypp_Deobfuscate"), new ArrayList() {stringParam}, methodBlock); //Fix data refferences.. I got tired here. return method; }
public virtual void OutALocalLvalue(ALocalLvalue node) { DefaultOut(node); }
public override void CaseALocalLvalue(ALocalLvalue node) { InALocalLvalue(node); if (node.GetName() != null) { node.GetName().Apply(this); } OutALocalLvalue(node); }
public static PLvalue Link(AAName name, Node refNode, List<Node> list, SharedData data) { List<TIdentifier> identifierList = new List<TIdentifier>(); { int count = name.GetIdentifier().Count; if (count < list.Count) { for (int i = 0; i < list.Count - count; i++) { if (list[i] is AStructDecl) identifierList.Add(((AStructDecl)list[i]).GetName()); } for (int i = 0; i < count; i++) { TIdentifier iden = (TIdentifier)name.GetIdentifier()[i]; identifierList.Add(iden); } } else for (int i = count - list.Count; i < count; i++) { TIdentifier iden = (TIdentifier)name.GetIdentifier()[i]; identifierList.Add(iden); } } PLvalue baseLvalue = null; Node node = list[0]; list.RemoveAt(0); TIdentifier identifier = identifierList[0]; identifierList.RemoveAt(0); if (node is AALocalDecl) { AALocalDecl aNode = (AALocalDecl)node; if (node.Parent() is AStructDecl) {//Struct local //Make it this->var or this.var AStructDecl pStruct = Util.GetAncestor<AStructDecl>(node); if (pStruct.GetClassToken() != null || Util.HasAncestor<AConstructorDecl>(refNode) || Util.HasAncestor<ADeconstructorDecl>(refNode)) {//(*this). baseLvalue = new AThisLvalue(new TThis("this")); baseLvalue = new APointerLvalue(new TStar("*"), new ALvalueExp(baseLvalue)); baseLvalue = new AStructLvalue(new ALvalueExp(baseLvalue), new ADotDotType(new TDot(".")), identifier); data.StructFieldLinks[(AStructLvalue)baseLvalue] = aNode; } else {//struct. baseLvalue = new AStructFieldLvalue(identifier); data.StructMethodFieldLinks[(AStructFieldLvalue)baseLvalue] = aNode; } } else {//Method/constructor/deconstructor local ALocalLvalue replaceNode = new ALocalLvalue(identifier); data.LocalLinks[replaceNode] = aNode; baseLvalue = replaceNode; } } else if (node is APropertyDecl) { APropertyDecl aNode = (APropertyDecl)node; if (Util.HasAncestor<AStructDecl>(node)) {//Property in current struct AStructDecl pStruct = Util.GetAncestor<AStructDecl>(node); if (pStruct.GetClassToken() != null || Util.HasAncestor<AConstructorDecl>(refNode) || Util.HasAncestor<ADeconstructorDecl>(refNode)) {//(*this). baseLvalue = new AThisLvalue(new TThis("this")); baseLvalue = new APointerLvalue(new TStar("*"), new ALvalueExp(baseLvalue)); baseLvalue = new AStructLvalue(new ALvalueExp(baseLvalue), new ADotDotType(new TDot(".")), identifier); data.StructPropertyLinks[(AStructLvalue)baseLvalue] = aNode; } else {//struct. baseLvalue = new AStructFieldLvalue(identifier); data.StructMethodPropertyLinks[(AStructFieldLvalue)baseLvalue] = aNode; } } else {//Global property baseLvalue = new APropertyLvalue(identifier); data.PropertyLinks[(APropertyLvalue)baseLvalue] = aNode; } } else if (node is AFieldDecl) { baseLvalue = new AFieldLvalue(identifier); data.FieldLinks[(AFieldLvalue)baseLvalue] = (AFieldDecl)node; } else if (node is AStructDecl) { AStructDecl targetStruct = (AStructDecl)node; node = list[0]; list.RemoveAt(0); identifier = identifierList[0]; identifierList.RemoveAt(0); AStructFieldLvalue lvalue = new AStructFieldLvalue(identifier); if (node is AALocalDecl) data.StructMethodFieldLinks[lvalue] = (AALocalDecl)node; else data.StructMethodPropertyLinks[lvalue] = (APropertyDecl)node; baseLvalue = lvalue; } while (list.Count > 0) { node = list[0]; list.RemoveAt(0); identifier = identifierList[0]; identifierList.RemoveAt(0); baseLvalue = new AStructLvalue(new ALvalueExp(baseLvalue), new ADotDotType(new TDot(".")), identifier); if (node is AALocalDecl) {//Struct local data.StructFieldLinks[(AStructLvalue) baseLvalue] = (AALocalDecl) node; } else if (node is APropertyDecl) {//Struct property data.StructPropertyLinks[(AStructLvalue) baseLvalue] = (APropertyDecl) node; } //Don't link array length stuff } return baseLvalue; }
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; }
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 virtual void InALocalLvalue(ALocalLvalue node) { DefaultIn(node); }
public override void CaseASwitchStm(ASwitchStm node) { node.GetTest().Apply(this); AALocalDecl fallThroughDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null), new TIdentifier(MakeUniqueLocalName(node, "switchFallThrough")), new ABooleanConstExp(new AFalseBool())); AALocalDecl continueDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null), new TIdentifier(MakeUniqueLocalName(node, "switchContinue")), new ABooleanConstExp(new ATrueBool())); AALocalDecl testVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(data.ExpTypes[node.GetTest()], data), new TIdentifier(MakeUniqueLocalName(node, "switchTestVar")), node.GetTest()); AABlock bigBlock = new AABlock(); //AABlock previousBlock = bigBlock; if (node.GetCases().Count > 0) { List<SwitchCaseData> switchCaseDatas = new List<SwitchCaseData>(); //Join cases without a body for (int i = node.GetCases().Count - 1; i >= 0; i--) { ASwitchCaseStm caseStm = (ASwitchCaseStm) node.GetCases()[i]; SwitchCaseData caseData = new SwitchCaseData(); caseData.Block = (AABlock) caseStm.GetBlock(); if (caseStm.GetType() is ACaseSwitchCaseType) caseData.Tests.Add(((ACaseSwitchCaseType)caseStm.GetType()).GetExp()); else caseData.ContainsDefault = true; caseData.IsLast = switchCaseDatas.Count == 0; if (switchCaseDatas.Count == 0 || caseData.Block.GetStatements().Count > 0) { switchCaseDatas.Insert(0, caseData); continue; } switchCaseDatas[0].Tests.AddRange(caseData.Tests); } for (int i = switchCaseDatas.Count - 1; i >= 0; i--) { switchCaseDatas[i].ContainsFallthrough = CanFallthrough(switchCaseDatas[i].Block, out switchCaseDatas[i].HasBreaks, out switchCaseDatas[i].RequiresWhile); if (i == switchCaseDatas.Count - 1) continue; switchCaseDatas[i + 1].TargetForFallThrough = switchCaseDatas[i].ContainsFallthrough; switchCaseDatas[i].RequiresContinue = !switchCaseDatas[i].ContainsFallthrough && (switchCaseDatas[i + 1].RequiresContinue || switchCaseDatas[i + 1].ContainsFallthrough); } AABlock previousBlock = bigBlock; //Make code for specific case foreach (SwitchCaseData switchCase in switchCaseDatas) { List<PExp> tests = new List<PExp>(); AABlock nextBlock; if (switchCase.TargetForFallThrough) {//Add if (continueSwitch) {} ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(continueDecl.GetName().Text)); ALvalueExp test = new ALvalueExp(lvalue); nextBlock = new AABlock(); AIfThenStm ifStm = new AIfThenStm(new TLParen("("), test, new ABlockStm(new TLBrace("{"), nextBlock)); previousBlock.GetStatements().Add(ifStm); previousBlock = nextBlock; data.LocalLinks[lvalue] = continueDecl; data.LvalueTypes[lvalue] = data.ExpTypes[test] = continueDecl.GetType(); //First test in next if: if (fallThrough || ... lvalue = new ALocalLvalue(new TIdentifier(fallThroughDecl.GetName().Text)); test = new ALvalueExp(lvalue); tests.Add(test); data.LocalLinks[lvalue] = fallThroughDecl; data.LvalueTypes[lvalue] = data.ExpTypes[test] = fallThroughDecl.GetType(); } //Make code for the test in the if foreach (PExp exp in switchCase.Tests) { ALocalLvalue leftSide = new ALocalLvalue(new TIdentifier(testVar.GetName().Text)); ALvalueExp lvalueExp = new ALvalueExp(leftSide); ABinopExp test = new ABinopExp(lvalueExp, new AEqBinop(new TEq("==")), exp); tests.Add(test); data.LocalLinks[leftSide] = testVar; data.LvalueTypes[leftSide] = data.ExpTypes[lvalueExp] = testVar.GetType(); data.ExpTypes[test] = new ANamedType(new TIdentifier("bool"), null); } if (switchCase.ContainsDefault) { ABooleanConstExp test = new ABooleanConstExp(new ATrueBool()); tests.Add(test); data.ExpTypes[test] = new ANamedType(new TIdentifier("bool"), null); } PExp finalTest = tests[0]; tests.RemoveAt(0); foreach (PExp exp in tests) { finalTest = new ABinopExp(finalTest, new ALazyOrBinop(new TOrOr("||")), exp); data.ExpTypes[finalTest] = new ANamedType(new TIdentifier("bool"), null); } //Transform breaks into assignments //If we can fallthrough, and there are breaks, encase in a while stm AABlock testBlock = switchCase.Block; if (switchCase.RequiresWhile) { AABlock newBlock = new AABlock(); PExp whileTest = new ABooleanConstExp(new ATrueBool()); AWhileStm whileStm = new AWhileStm(new TLParen("("), whileTest, new ABlockStm(new TLBrace("{"), switchCase.Block)); newBlock.GetStatements().Add(whileStm); switchCase.Block = newBlock; } TransformBreaks(testBlock, switchCase, continueDecl, fallThroughDecl); if (switchCase.ContainsFallthrough && !switchCase.TargetForFallThrough) {//Add fallthrough = true; ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(fallThroughDecl.GetName().Text)); ABooleanConstExp rightSide = new ABooleanConstExp(new ATrueBool()); AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, rightSide); testBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); data.LocalLinks[lvalue] = fallThroughDecl; data.LvalueTypes[lvalue] = data.ExpTypes[rightSide] = data.ExpTypes[assignment] = fallThroughDecl.GetType(); } if (switchCase.RequiresWhile) {//Add break at the end of the while testBlock.GetStatements().Add(new ABreakStm(new TBreak("break"))); } //Make if PStm finalIfStm; if (finalTest is ABooleanConstExp) {//Final if is if(true). dont add it. finalIfStm = new ABlockStm(new TLBrace("{"), switchCase.Block); nextBlock = new AABlock(); } else if (switchCase.IsLast || switchCase.ContainsFallthrough) {//One armed if finalIfStm = new AIfThenStm(new TLParen("("), finalTest, new ABlockStm(new TLBrace("{"), switchCase.Block)); nextBlock = bigBlock; } else {//Two armed if nextBlock = new AABlock(); finalIfStm = new AIfThenElseStm(new TLParen("("), finalTest, new ABlockStm(new TLBrace("{"), switchCase.Block), new ABlockStm(new TLBrace("{"), nextBlock)); } previousBlock.GetStatements().Add(finalIfStm); previousBlock = nextBlock; } //If needed, add fallThroughDecl and continueDecl data.Locals.Add(bigBlock, new List<AALocalDecl>()); if (data.LocalLinks.Values.Contains(fallThroughDecl)) { bigBlock.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), fallThroughDecl)); data.Locals[bigBlock].Add(fallThroughDecl); } if (data.LocalLinks.Values.Contains(continueDecl)) { bigBlock.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), continueDecl)); data.Locals[bigBlock].Add(continueDecl); } bigBlock.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), testVar)); data.Locals[bigBlock].Add(testVar); node.ReplaceBy(new ABlockStm(new TLBrace("{"), bigBlock)); bigBlock.Apply(this); } }
public virtual void CaseALocalLvalue(ALocalLvalue node) { DefaultCase(node); }
public override void OutAIncDecExp(AIncDecExp node) { PIncDecOp op = node.GetIncDecOp(); if (!Util.HasAncestor<AABlock>(node)) { Token token = null; if (op is APostDecIncDecOp) token = ((APostDecIncDecOp) op).GetToken(); else if (op is APreDecIncDecOp) token = ((APreDecIncDecOp)op).GetToken(); else if (op is APostIncIncDecOp) token = ((APostIncIncDecOp)op).GetToken(); else if (op is APreIncIncDecOp) token = ((APreIncIncDecOp)op).GetToken(); errors.Add(new ErrorCollection.Error(token, LocRM.GetString("ErrorText114"))); throw new ParserException(token, "TypeChecking.OutAIncDecExp"); } bool plus = op is APreIncIncDecOp || op is APostIncIncDecOp; if (op is APreIncIncDecOp || op is APreDecIncDecOp || node.Parent() is AExpStm) {//++i, --i, i++; or i--; //Replace with assignment //<exp>++ => <exp> + 1 //(... foo = <exp> ...)++ => (... foo = <exp> ...) = (... foo ...) + 1 //(... foo++ ...)++ => (... foo++ ...) = (... foo ...) + 1 PLvalue clone = Util.MakeClone(node.GetLvalue(), data); clone.Apply(new AssignFixup(data)); PBinop binop; if (plus) { binop = new APlusBinop(new TPlus("+")); } else { binop = new AMinusBinop(new TMinus("-")); } ABinopExp addExp = new ABinopExp(new ALvalueExp(clone), binop, new AIntConstExp(new TIntegerLiteral("1"))); AAssignmentExp exp = new AAssignmentExp(new TAssign("="), node.GetLvalue(), addExp); node.ReplaceBy(exp); exp.Apply(this); return; } {//i++ or i-- //Make a new local so //int newLocal = i; //++i; //...newLocal...; PLvalue lvalueClone = Util.MakeClone(node.GetLvalue(), data); PExp exp = new ALvalueExp(Util.MakeClone(node.GetLvalue(), data)); data.ExpTypes[exp] = data.LvalueTypes[node.GetLvalue()]; AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(data.LvalueTypes[node.GetLvalue()], data), new TIdentifier("incDecVar"), exp); ALocalDeclStm localDeclStm = new ALocalDeclStm(new TSemicolon(";"), localDecl); node.SetIncDecOp(plus ? (PIncDecOp) new APreIncIncDecOp(new TPlusPlus("++")) : new APreDecIncDecOp(new TMinusMinus("--"))); ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(localDecl.GetName().Text)); exp = new ALvalueExp(lvalue); data.ExpTypes[exp] = data.LvalueTypes[lvalue] = data.LvalueTypes[node.GetLvalue()]; data.LocalLinks[lvalue] = localDecl; PStm pStm = Util.GetAncestor<PStm>(node); node.ReplaceBy(exp); PStm nodeStm = new AExpStm(new TSemicolon(";"), node); AABlock block = (AABlock) pStm.Parent(); block.GetStatements().Insert(block.GetStatements().IndexOf(pStm), localDeclStm); block.GetStatements().Insert(block.GetStatements().IndexOf(pStm), nodeStm); localDeclStm.Apply(this); nodeStm.Apply(this); exp.Apply(this); if (pStm is AWhileStm && Util.IsAncestor(exp, ((AWhileStm)pStm).GetCondition())) { AWhileStm aStm = (AWhileStm)pStm; //Insert // newLocal = i // ++i //Before each continue in the while, and at the end. //Add continue statement, if not present block = (AABlock)((ABlockStm)aStm.GetBody()).GetBlock(); if (block.GetStatements().Count == 0 || !(block.GetStatements()[block.GetStatements().Count - 1] is AContinueStm)) block.GetStatements().Add(new AContinueStm(new TContinue("continue"))); //Get all continue statements in the while ContinueFinder finder = new ContinueFinder(); block.Apply(finder); foreach (AContinueStm continueStm in finder.Continues) { PLvalue nodeLvalue1 = Util.MakeClone(lvalueClone, data); PExp nodeLvalue1Exp = new ALvalueExp(nodeLvalue1); PLvalue nodeLvalue2 = Util.MakeClone(lvalueClone, data); ALocalLvalue newLocalLvalue = new ALocalLvalue(new TIdentifier("newLocal")); data.LocalLinks[newLocalLvalue] = localDecl; AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLocalLvalue, nodeLvalue1Exp); PStm assignmentStm = new AExpStm(new TSemicolon(";"), assignment); AIncDecExp newIncDecExp = new AIncDecExp(nodeLvalue2, plus ? (PIncDecOp) new APreIncIncDecOp( new TPlusPlus("++")) : new APreDecIncDecOp( new TMinusMinus("--"))); PStm newIncDecExpStm = new AExpStm(new TSemicolon(";"), newIncDecExp); block = (AABlock)continueStm.Parent(); block.GetStatements().Insert(block.GetStatements().IndexOf(continueStm), assignmentStm); block.GetStatements().Insert(block.GetStatements().IndexOf(continueStm), newIncDecExpStm); assignment.Apply(this); newIncDecExp.Apply(this); } } return; } }
public override void OutALocalLvalue(ALocalLvalue node) { if (folding) { AALocalDecl local = data.LocalLinks[node]; if (local.GetConst() == null) { if (!isANewExp) { errors.Add( new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText61"), false), true); throw new ParserException(null, null); } } if (local.GetInit() == null)//An error will be given earlier throw new ParserException(null, null); local.GetInit().Apply(this); } }
public override void OutALocalLvalue(ALocalLvalue node) { AALocalDecl decl = data.LocalLinks[node]; PType type = data.LvalueTypes[node] = decl.GetType(); if (decl.GetOut() != null && !Util.IsBulkCopy(type) && !assignedToOutParams.Contains(decl) && !(node.Parent() is AAssignmentExp && node == ((AAssignmentExp)node.Parent()).GetLvalue())) { errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile, LocRM.GetString("ErrorText97") + node.GetName().Text + LocRM.GetString("ErrorText98"))); } }
private void MoveOut(PExp exp, PType type) { PStm pStm = Util.GetAncestor<PStm>(exp); AABlock pBlock = (AABlock)pStm.Parent(); ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier("gppVar")); ALvalueExp lvalueExp = new ALvalueExp(lvalue); exp.ReplaceBy(lvalueExp); AALocalDecl decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(type, data), new TIdentifier("gppVar"), exp); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), decl)); data.LvalueTypes[lvalue] = data.ExpTypes[lvalueExp] = decl.GetType(); data.LocalLinks[lvalue] = decl; }
private void TransformBreaks(PStm stm, SwitchCaseData switchCase, AALocalDecl continueDecl, AALocalDecl fallthroughDecl, ref int currI) { if (stm is ABreakStm) { AABlock pBlock = (AABlock) stm.Parent(); if (!switchCase.IsLast && (switchCase.RequiresContinue || switchCase.ContainsFallthrough)) { //Add continue = false; ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(continueDecl.GetName().Text)); ABooleanConstExp rightSide = new ABooleanConstExp(new AFalseBool()); AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, rightSide); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(stm), new AExpStm(new TSemicolon(";"), assignment)); data.LocalLinks[lvalue] = continueDecl; data.LvalueTypes[lvalue] = data.ExpTypes[rightSide] = data.ExpTypes[assignment] = continueDecl.GetType(); currI++; } if (!switchCase.IsLast && switchCase.TargetForFallThrough) { //Add fallthrough = false; ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(fallthroughDecl.GetName().Text)); ABooleanConstExp rightSide = new ABooleanConstExp(new AFalseBool()); AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, rightSide); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(stm), new AExpStm(new TSemicolon(";"), assignment)); data.LocalLinks[lvalue] = fallthroughDecl; data.LvalueTypes[lvalue] = data.ExpTypes[rightSide] = data.ExpTypes[assignment] = fallthroughDecl.GetType(); currI++; } if (!switchCase.RequiresWhile) {//Remove break stm.Parent().RemoveChild(stm); currI--; } } if (stm is ABlockStm) TransformBreaks((AABlock)((ABlockStm)stm).GetBlock(), switchCase, continueDecl, fallthroughDecl); if (stm is AIfThenStm) TransformBreaks(((AIfThenStm)stm).GetBody(), switchCase, continueDecl, fallthroughDecl, ref currI); if (stm is AIfThenElseStm) { TransformBreaks(((AIfThenElseStm)stm).GetThenBody(), switchCase, continueDecl, fallthroughDecl, ref currI); TransformBreaks(((AIfThenElseStm)stm).GetElseBody(), switchCase, continueDecl, fallthroughDecl, ref currI); } }
public override void OutALocalLvalue(ALocalLvalue node) { base.OutALocalLvalue(node); }
public override void CaseALocalLvalue(ALocalLvalue node) { if (!IsConst) return; AALocalDecl decl = data.LocalLinks[node]; if (decl.GetConst() == null) { IsConst = false; } }