public void Parse(AAProgram ast)
 {
     FindNullChecks finder = new FindNullChecks(data);
     ast.Apply(finder);
     ast.Apply(new Phase0(data));
     //ast.Apply(new Phase0(data));
     ast.Apply(new Phase2(data, finder.TypesWithIdentifierArray));
 }
        public static void Apply(AAProgram ast, FinalTransformations finalTrans)
        {
            //Build list of file dependacies
            Phase1 phase1 = new Phase1(finalTrans);
            ast.Apply(phase1);
            var dependancies = phase1.dependancies;
            if (dependancies.Keys.Count == 0) return;
            AASourceFile root = Util.GetAncestor<AASourceFile>(finalTrans.mainEntry) ??
                                dependancies.Keys.FirstOrDefault(file => !file.GetName().Text.Contains("\\")) ??
                                dependancies.Keys.First(file => true);

            //Remove files unreachable from root
            //On second thought, dont. there might be static refferences the other way which needs to be included
            /*{
                List<AASourceFile> reachable = GetReachable(root, dependancies);
                AASourceFile[] keys = new AASourceFile[dependancies.Count];
                dependancies.Keys.CopyTo(keys, 0);
                foreach (AASourceFile key in keys)
                {
                    if (!reachable.Contains(key))
                        dependancies.Remove(key);
                }
            }*/

            //Push common depancies up
            /*
             * root -> (item1 -> (item3), item2 -> (item4 -> (item3)))
             *
             * root -> (item3, item1, item2 -> (item4))
             */

            //Add unreachable to the root
            while (true)
            {
                List<AASourceFile> reachable = new List<AASourceFile>();
                GetReachable(root, dependancies, ref reachable);
                if (reachable.Count == dependancies.Count + (reachable.Contains(null) ? 1 : 0)) break;
                AASourceFile[] keys = new AASourceFile[dependancies.Count];
                dependancies.Keys.CopyTo(keys, 0);
                foreach (AASourceFile key in keys)
                {
                    if (!reachable.Contains(key))
                    {
                        AASourceFile k = key;
                        //See if you can find another unreachable file which need this file
                        Dictionary<AASourceFile, int> decendantCounts = new Dictionary<AASourceFile, int>();
                        decendantCounts.Add(k, CountDecendants(k, dependancies, new List<AASourceFile>()));
                        while (true)
                        {
                            AASourceFile file = null;
                            foreach (KeyValuePair<AASourceFile, List<AASourceFile>> dependancy in dependancies)
                            {
                                if (decendantCounts.ContainsKey(dependancy.Key))
                                    continue;
                                if (!dependancy.Value.Contains(k))
                                    continue;
                                file = dependancy.Key;
                                break;
                            }

                            //AASourceFile file = dependancies.FirstOrDefault(item => item.Value.Contains(k)).Key;
                            if (file == null) break;
                            decendantCounts.Add(file, CountDecendants(file, dependancies, new List<AASourceFile>()));
                            k = file;
                        }
                        foreach (KeyValuePair<AASourceFile, int> decendantItem in decendantCounts)
                        {
                            if (decendantItem.Value > decendantCounts[k])
                                k = decendantItem.Key;
                        }

                        dependancies[root].Add(k);
                        break;
                    }
                }
            }
            //It is moved down here because cycles are not removed in unreachable
            RemoveCycles(root, dependancies, new List<AASourceFile> { root });

            //Convert to tree to make it easier
            List<Item> allItems = new List<Item>();
            IncludeItem rootIncludeItem = MakeTree(root, dependancies, allItems, null);
            bool[] removed = new bool[allItems.Count];
            for (int i = 0; i < removed.Length; i++)
                removed[i] = false;
            int removedCount = 0;

            //Ensure that each include is only included one place
            for (int i = 0; i < allItems.Count; i++)
            {
                if (removed[i])
                    continue;

                IncludeItem item1 = (IncludeItem)allItems[i];
                for (int j = i + 1; j < allItems.Count; j++)
                {
                    if (removed[j])
                        continue;
                    IncludeItem item2 = (IncludeItem)allItems[j];

                    if (item1.Current == item2.Current)
                    {
                        List<Item> path1 = item1.Path;
                        List<Item> path2 = item2.Path;

                        for (int k = 0; k < Math.Min(path1.Count, path2.Count); k++)
                        {
                            if (path1[k] != path2[k])
                            {

                                int insertAt = Math.Min(path1[k - 1].Children.IndexOf(path1[k]),
                                                        path2[k - 1].Children.IndexOf(path2[k]));

                                item1.Parent.Children.Remove(item1);
                                LinkedList<IncludeItem> toRemove = new LinkedList<IncludeItem>();
                                toRemove.AddLast(item2);
                                while (toRemove.Count > 0)
                                {
                                    IncludeItem item = toRemove.First.Value;
                                    toRemove.RemoveFirst();
                                    item.Parent.Children.Remove(item);
                                    //allItems.Remove(item);
                                    removedCount++;
                                    removed[item.ListIndex] = true;
                                    foreach (IncludeItem child in item.Children)
                                    {
                                        toRemove.AddLast(child);
                                    }
                                }
                                //j--;

                                path1[k - 1].Children.Insert(insertAt, item1);
                                item1.Parent = path1[k - 1];

                                break;
                            }
                        }
                    }
                }
            }

            List<Item> newAllItems = new List<Item>(allItems.Count - removedCount);
            for (int i = 0; i < allItems.Count; i++)
                if (!removed[i])
                    newAllItems.Add(allItems[i]);
            allItems = newAllItems;

            //Move the null node to nr [0]
            foreach (IncludeItem item in allItems)
            {
                if (item.Current == null)
                {
                    int itemIndex = item.Parent.Children.IndexOf(item);
                    Item item0 = item.Parent.Children[0];
                    item.Parent.Children[0] = item;
                    item.Parent.Children[itemIndex] = item0;
                    break;
                }
            }

            //Insert method decls and move structs & fields up as needed
            ast.Apply(new Phase2(finalTrans, allItems));

            //Insert the headers in the files

            if (Options.Compiler.OneOutputFile)
            {
                //for (int i = 0; i < allItems.Count; i++)
                int i = 0;
                while (allItems.Count > 0)
                {
                    if (allItems[i] is IncludeItem)
                    {
                        IncludeItem includeItem = (IncludeItem) allItems[i];
                        //Dont want the standard lib
                        if (includeItem.Current == null)
                        {
                            i++;
                            continue;
                        }
                        //If it has children with children, then pick another first
                        if (includeItem.Children.Any(child => child.Children.Count > 0))
                        {
                            i++;
                            continue;
                        }
                        if (includeItem.Children.Count == 0)
                        {
                            if (includeItem.Parent == null)
                                break;
                            i++;
                            continue;
                        }
                        i = 0;
                        //Put all children into this
                        while (includeItem.Children.Count > 0)
                        {
                            int childNr = includeItem.Children.Count - 1;
                            allItems.Remove(includeItem.Children[childNr]);
                            if (includeItem.Children[childNr] is FieldItem)
                            {
                                FieldItem aItem = (FieldItem)includeItem.Children[childNr];
                                Node node = aItem.FieldDecl;
                                node.Parent().RemoveChild(node);
                                includeItem.Current.GetDecl().Insert(0, node);
                            }
                            else if (includeItem.Children[childNr] is StructItem)
                            {
                                StructItem aItem = (StructItem)includeItem.Children[childNr];
                                Node node = aItem.StructDecl;
                                node.Parent().RemoveChild(node);
                                includeItem.Current.GetDecl().Insert(0, node);
                            }
                            else if (includeItem.Children[childNr] is MethodDeclItem)
                            {
                                MethodDeclItem aItem = (MethodDeclItem)includeItem.Children[childNr];
                                AMethodDecl aNode = new AMethodDecl();
                                if (aItem.RealDecl.GetStatic() != null) aNode.SetStatic(new TStatic("static"));
                                aNode.SetReturnType(Util.MakeClone(aItem.RealDecl.GetReturnType(), finalTrans.data));
                                aNode.SetName(new TIdentifier(aItem.RealDecl.GetName().Text));
                                foreach (AALocalDecl formal in aItem.RealDecl.GetFormals())
                                {
                                    AALocalDecl clone = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(formal.GetType(), finalTrans.data), new TIdentifier(formal.GetName().Text), null);
                                    aNode.GetFormals().Add(clone);
                                }
                                includeItem.Current.GetDecl().Insert(0, aNode);
                            }
                            else if (includeItem.Children[childNr] is IncludeItem)
                            {
                                IncludeItem aChild = (IncludeItem)includeItem.Children[childNr];
                                if (aChild.Current == null)
                                {
                                    AIncludeDecl node = new AIncludeDecl(new TInclude("include"),
                                                        new TStringLiteral("\"TriggerLibs/NativeLib\""));
                                    includeItem.Current.GetDecl().Insert(0, node);
                                }
                                else
                                {
                                    PDecl[] decls = new PDecl[aChild.Current.GetDecl().Count];
                                    aChild.Current.GetDecl().CopyTo(decls, 0);
                                    for (int k = decls.Length - 1; k >= 0; k--)
                                    {
                                        includeItem.Current.GetDecl().Insert(0, decls[k]);
                                    }
                                    aChild.Current.Parent().RemoveChild(aChild.Current);
                                    //i = -1;
                                }
                            }
                            includeItem.Children.RemoveAt(childNr);
                        }
                    }
                }
            }
            else
                foreach (IncludeItem includeItem in allItems.OfType<IncludeItem>())
                {
                    for (int i = includeItem.Children.Count - 1; i >= 0; i--)
                    {
                        Node node;
                        if (includeItem.Children[i] is IncludeItem)
                        {
                            IncludeItem aItem = (IncludeItem) includeItem.Children[i];
                            node = new AIncludeDecl(new TInclude("include"),
                                                    new TStringLiteral("\"" + (aItem.Current == null ? "TriggerLibs/NativeLib" : aItem.Current.GetName().Text.Replace("\\", "/")) + "\""));
                            if (aItem.Current == null && finalTrans.mainEntry != null)
                            {
                                //Search for user defined initlib
                                bool foundInvoke = false;
                                foreach (ASimpleInvokeExp invokeExp in finalTrans.data.SimpleMethodLinks.Keys)
                                {
                                    if(invokeExp.GetName().Text == "libNtve_InitLib" && invokeExp.GetArgs().Count == 0)
                                    {
                                        /*finalTrans.errors.Add(new ErrorCollection.Error(invokeExp.GetName(),
                                                                                        Util.GetAncestor<AASourceFile>(
                                                                                            invokeExp),
                                                                                        "You are invoking libNtve_InitLib() yourself somewhere. It will not be auto inserted.",
                                                                                        true));*/
                                        foundInvoke = true;
                                        break;
                                    }
                                }

                                if (!foundInvoke)
                                {
                                    //Init the lib
                                    ASimpleInvokeExp initExp = new ASimpleInvokeExp();
                                    initExp.SetName(new TIdentifier("libNtve_InitLib"));
                                    finalTrans.data.ExpTypes[initExp] = new AVoidType(new TVoid("void"));
                                    foreach (AMethodDecl method in finalTrans.data.Libraries.Methods)
                                    {
                                        if (method.GetName().Text == "libNtve_InitLib" && method.GetFormals().Count == 0)
                                        {
                                            finalTrans.data.SimpleMethodLinks[initExp] = method;
                                        }
                                    }
                                    AABlock block = (AABlock) finalTrans.mainEntry.GetBlock();
                                    block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), initExp));
                                }
                            }
                        }
                        else if (includeItem.Children[i] is FieldItem)
                        {
                            FieldItem aItem = (FieldItem)includeItem.Children[i];
                            node = aItem.FieldDecl;
                            node.Parent().RemoveChild(node);
                        }
                        else if (includeItem.Children[i] is StructItem)
                        {
                            StructItem aItem = (StructItem)includeItem.Children[i];
                            node = aItem.StructDecl;
                            node.Parent().RemoveChild(node);
                        }
                        else if (includeItem.Children[i] is MethodDeclItem)
                        {
                            MethodDeclItem aItem = (MethodDeclItem)includeItem.Children[i];
                            AMethodDecl aNode = new AMethodDecl();
                            if (aItem.RealDecl.GetStatic() != null) aNode.SetStatic(new TStatic("static"));
                            aNode.SetReturnType(Util.MakeClone(aItem.RealDecl.GetReturnType(), finalTrans.data));
                            aNode.SetName(new TIdentifier(aItem.RealDecl.GetName().Text));
                            foreach (AALocalDecl formal in aItem.RealDecl.GetFormals())
                            {
                                AALocalDecl clone = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(formal.GetType(), finalTrans.data), new TIdentifier(formal.GetName().Text), null);
                                aNode.GetFormals().Add(clone);
                            }
                            node = aNode;
                        }
                        else
                            throw new Exception("FixIncludes.Apply: Unexpected item type");

                        includeItem.Current.GetDecl().Insert(0, node);
                    }
                }
        }
        public static void CalculateMethodModify(AAProgram ast, SharedData data, out int methodCount)
        {
            //Calculate what global variables all methods might modify.
            methodData.Clear();

            GetDependancies dependancies = new GetDependancies(data);
            ast.Apply(dependancies);
            methodCount = dependancies.UsedMethods.Count;

            while (dependancies.UsedMethods.Count > 0)
            {
                LinkedList<AMethodDecl> modified = new LinkedList<AMethodDecl>();
                foreach (var pair in dependancies.UsedMethods)
                {
                    AMethodDecl method = pair.Key;
                    List<AMethodDecl> usedMethods = pair.Value;

                    ModifyData modifyData = new ModifyData();
                    foreach (AFieldDecl fieldDecl in dependancies.ReadFields[method])
                    {
                        modifyData.Add(fieldDecl, true, false);
                    }
                    foreach (AFieldDecl fieldDecl in dependancies.WrittenFields[method])
                    {
                        modifyData.Add(fieldDecl, false, true);
                    }
                    bool skip = false;
                    foreach (AMethodDecl usedMethod in usedMethods)
                    {
                        ModifyData usedData = GetModifyData(usedMethod);
                        if (usedData == null)
                        {
                            skip = true;
                            break;
                        }
                        modifyData.Union(usedData);
                    }
                    if (skip)
                        continue;
                    methodData[method] = modifyData;
                    modified.AddLast(method);
                }
                foreach (AMethodDecl decl in modified)
                {
                    dependancies.UsedMethods.Remove(decl);
                    dependancies.ReadFields.Remove(decl);
                    dependancies.WrittenFields.Remove(decl);
                }
                if (modified.Count == 0)
                {//The rest is in a circular dependancy
                    foreach (var pair in dependancies.UsedMethods)
                    {
                        AMethodDecl method = pair.Key;

                        ModifyData modifyData = new ModifyData();

                        Update(method, new List<AMethodDecl>(), modifyData, dependancies.UsedMethods, dependancies.ReadFields, dependancies.WrittenFields);

                        methodData[method] = modifyData;
                        modified.AddLast(method);
                    }
                    dependancies.UsedMethods.Clear();
                }
            }
        }
Esempio n. 4
0
 public static void Parse(AAProgram ast, ErrorCollection errors, SharedData data)
 {
     ast.Apply(new Weeder(errors, data));
 }
 public static void Parse(AAProgram ast, ErrorCollection errors, SharedData data)
 {
     ast.Apply(new TypeChecking(errors, data));
 }
 public static void Parse(AAProgram ast, ErrorCollection errors, SharedData data)
 {
     ast.Apply(new EnviromentBuilding(errors, data));
 }
 public static void Parse(AAProgram ast, ErrorCollection errors, SharedData data, DirectoryInfo outputDir)
 {
     ast.Apply(new CodeGeneration(errors, data, outputDir));
 }
 public override void InAAProgram(AAProgram node)
 {
     node.Apply(new BlockFixer(data));
 }
        private void CompileThread()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Form1.Language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Form1.Language);
            try
            {
                if (!compilingFromCommandLine)
                    form.SetStatusText(LocRM.GetString("GC_Text3"));

                if (!compilingFromCommandLine)
                    ClearErrorWindow();
                //Build a tree with all sourcefiles
                AAProgram root = new AAProgram();
                ErrorCollection errors = new ErrorCollection();
                currentErrorCollection = errors;
                if (!compilingFromCommandLine)
                    errors.ErrorAdded += errors_ErrorAdded;
                bool addedDeobfuscator = false;
                SharedData sharedData = new SharedData();
                sharedData.AllowPrintouts = !compilingFromCommandLine;
                //Parse project files
                List<string> fileNames = new List<string>();
                List<string> sources = new List<string>();
                foreach (
                    FileItem sourceFile in Form1.GetSourceFiles(ProjectProperties.CurrentProjectPropperties.SrcFolder))
                {
                    if (sourceFile.Deactivated)
                        continue;
                    StreamReader reader = sourceFile.File.OpenText();

                    string filename = sourceFile.File.FullName;
                    //Remove c:/.../projectDir/src
                    filename = filename.Remove(0, (ProjectDir.FullName + "/src/").Length);
                    //Remove .galaxy++
                    filename = filename.Remove(filename.Length - ".galaxy++".Length);
                    fileNames.Add(filename);
                    sources.Add(reader.ReadToEnd());
                    reader.Close();
                    continue;

                    Parser parser = new Parser(new Lexer(reader));
                    try
                    {
                        Start start = parser.Parse();
                        AASourceFile sourceNode = (AASourceFile) start.GetPSourceFile();
                        reader.Close();
                        reader = sourceFile.File.OpenText();
                        int lineCount = 0;
                        while (reader.ReadLine() != null)
                        {
                            lineCount++;
                        }
                        reader.Close();
                        sharedData.LineCounts[sourceNode] = lineCount;

                        //Extract encryption function
                       /* {
                            AASourceFile file = (AASourceFile) start.GetPSourceFile();
                            if (file.GetDecl().Count > 0 && file.GetDecl()[0] is AMethodDecl)
                            {
                                AMethodDecl method = (AMethodDecl) file.GetDecl()[0];
                                if (method.GetName().Text == "Galaxy_pp_Deobfuscate")
                                {
                                    FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                    IFormatter formatter = new BinaryFormatter();
                                    Stream stream = dobfuscateFile.Open(FileMode.Create);
                                    formatter.Serialize(stream, method);
                                    stream.Close();
                                }
                            }
                        }*/

                        if (Options.Compiler.ObfuscateStrings)
                        {
                            HasStringConstExp checker = new HasStringConstExp();
                            start.Apply(checker);

                            if (!addedDeobfuscator /* && checker.HasStringConst*/)
                            {
                                FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                IFormatter formatter = new BinaryFormatter();
                                Stream stream = dobfuscateFile.Open(FileMode.Open);
                                AASourceFile file = (AASourceFile) start.GetPSourceFile();

                                AMethodDecl method = (AMethodDecl) formatter.Deserialize(stream);
                                sharedData.DeobfuscateMethod = method;
                                method.GetName().Line = 0;

                                HasStringConstExp checker2 = new HasStringConstExp();
                                method.Apply(checker2);
                                file.GetDecl().Insert(0, method);
                                stream.Close();

                                addedDeobfuscator = true;

                                foreach (AStringConstExp stringConstExp in checker2.List)
                                {
                                    int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                    AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const", line, 0),
                                                                      new ANamedType(new TIdentifier("string", line, 1),
                                                                                     null),
                                                                      new TIdentifier("Galaxy_pp_stringU" +
                                                                                      sharedData.UnobfuscatedStrings.
                                                                                          Count),
                                                                      null);
                                    //If the strings are the same - point them to same field
                                    bool newField = true;
                                    foreach (AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys)
                                    {
                                        if (stringConstExp.GetStringLiteral().Text ==
                                            oldStringConstExp.GetStringLiteral().Text)
                                        {
                                            field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                            newField = false;
                                            break;
                                        }
                                    }
                                    if (newField)
                                    {
                                        file.GetDecl().Insert(0, field);
                                        sharedData.ObfuscationFields.Add(field);
                                    }
                                    sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                }

                            }
                            foreach (AStringConstExp stringConstExp in checker.List)
                            {
                                int line = -sharedData.ObfuscatedStrings.Count - 1;
                                AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const", line, 0),
                                                                  new ANamedType(new TIdentifier("string", line, 1),
                                                                                 null),
                                                                  new TIdentifier("Galaxy_pp_stringO" +
                                                                                  sharedData.ObfuscatedStrings.Count),
                                                                  null);
                                //If the strings are the same - point them to same field
                                bool newField = true;
                                foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                {
                                    if (stringConstExp.GetStringLiteral().Text ==
                                        oldStringConstExp.GetStringLiteral().Text)
                                    {
                                        field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                        newField = false;
                                        break;
                                    }
                                }
                                if (newField)
                                {
                                    AASourceFile file = (AASourceFile) sharedData.DeobfuscateMethod.Parent();
                                    file.GetDecl().Insert(file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                          field);
                                    sharedData.ObfuscationFields.Add(field);
                                }
                                sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                            }
                        }

                        sourceNode.SetName(new TIdentifier(filename));
                        root.GetSourceFiles().Add(start.GetPSourceFile());

                    }
                    catch (ParserException err)
                    {
                        String errMsg = err.Message;
                        //Remove [...]
                        errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                        errors.Add(new ErrorCollection.Error(err.Token, filename, errMsg));
                    }
                    reader.Close();
                }
                //Parse project dialogs
                foreach (
                    DialogItem dialogItem in Form1.GetDialogsFiles(ProjectProperties.CurrentProjectPropperties.SrcFolder))
                {
                    if (dialogItem.Deactivated)
                        continue;
                   // List<string> fileNames = new List<string>();
                   // List<string> sources = new List<string>();

                    DialogData data;
                    if (dialogItem.OpenFileData != null)
                    {
                        data = dialogItem.OpenFileData;
                        data.Save(dialogItem.FullName);
                    }
                    else
                    {
                        data = DialogData.Load(dialogItem.FullName);
                        data.DialogItem = dialogItem;
                    }

                    string filename = dialogItem.FullName;
                    filename = filename.Remove(0, (ProjectDir.FullName + "/src/").Length);
                    filename = filename.Remove(filename.Length - ".Dialog".Length);

                    fileNames.Add(filename);
                    sources.Add(data.Code ?? "");

                    fileNames.Add(filename + ".Designer");
                    sources.Add(data.DesignerCode ?? "");

                    continue;

                    for (int i = 0; i < fileNames.Count; i++)
                    {
                        filename = fileNames[i];
                        StringReader reader = new StringReader(sources[i] ?? "");
                        Parser parser = new Parser(new Lexer(reader));
                        try
                        {
                            Start start = parser.Parse();
                            AASourceFile sourceNode = (AASourceFile) start.GetPSourceFile();
                            reader.Close();
                            reader.Dispose();
                            reader = new StringReader(sources[i] ?? "");
                            int lineCount = 0;
                            while (reader.ReadLine() != null)
                            {
                                lineCount++;
                            }
                            reader.Close();

                            sharedData.LineCounts[sourceNode] = lineCount;

                            if (Options.Compiler.ObfuscateStrings)
                            {
                                HasStringConstExp checker = new HasStringConstExp();
                                start.Apply(checker);

                                if (!addedDeobfuscator /* && checker.HasStringConst*/)
                                {
                                    FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                    IFormatter formatter = new BinaryFormatter();
                                    Stream stream = dobfuscateFile.Open(FileMode.Open);
                                    AASourceFile file = (AASourceFile) start.GetPSourceFile();

                                    AMethodDecl method = (AMethodDecl) formatter.Deserialize(stream);
                                    sharedData.DeobfuscateMethod = method;
                                    method.GetName().Line = 0;

                                    HasStringConstExp checker2 = new HasStringConstExp();
                                    method.Apply(checker2);
                                    file.GetDecl().Insert(0, method);
                                    stream.Close();

                                    addedDeobfuscator = true;

                                    foreach (AStringConstExp stringConstExp in checker2.List)
                                    {
                                        int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                        AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                          new TConst("const", line, 0),
                                                                          new ANamedType(
                                                                              new TIdentifier("string", line, 1),
                                                                              null),
                                                                          new TIdentifier("Galaxy_pp_stringU" +
                                                                                          sharedData.UnobfuscatedStrings
                                                                                              .
                                                                                              Count),
                                                                          null);
                                        //If the strings are the same - point them to same field
                                        bool newField = true;
                                        foreach (
                                            AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys)
                                        {
                                            if (stringConstExp.GetStringLiteral().Text ==
                                                oldStringConstExp.GetStringLiteral().Text)
                                            {
                                                field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                                newField = false;
                                                break;
                                            }
                                        }
                                        if (newField)
                                        {
                                            file.GetDecl().Insert(0, field);
                                            sharedData.ObfuscationFields.Add(field);
                                        }
                                        sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                    }

                                }
                                foreach (AStringConstExp stringConstExp in checker.List)
                                {
                                    int line = -sharedData.ObfuscatedStrings.Count - 1;
                                    AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                      new TConst("const", line, 0),
                                                                      new ANamedType(new TIdentifier("string", line, 1),
                                                                                     null),
                                                                      new TIdentifier("Galaxy_pp_stringO" +
                                                                                      sharedData.ObfuscatedStrings.Count),
                                                                      null);
                                    //If the strings are the same - point them to same field
                                    bool newField = true;
                                    foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                    {
                                        if (stringConstExp.GetStringLiteral().Text ==
                                            oldStringConstExp.GetStringLiteral().Text)
                                        {
                                            field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                            newField = false;
                                            break;
                                        }
                                    }
                                    if (newField)
                                    {
                                        AASourceFile file = (AASourceFile) sharedData.DeobfuscateMethod.Parent();
                                        file.GetDecl().Insert(file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                              field);
                                        sharedData.ObfuscationFields.Add(field);
                                    }
                                    sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                                }
                            }

                            sourceNode.SetName(new TIdentifier(filename));
                            root.GetSourceFiles().Add(start.GetPSourceFile());

                        }
                        catch (ParserException err)
                        {
                            String errMsg = err.Message;
                            //Remove [...]
                            errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                            errors.Add(new ErrorCollection.Error(err.Token, filename, errMsg));
                        }
                        reader.Close();
                    }
                }
               // Preprocessor.Parse(sources, errors);
                for (int i = 0; i < fileNames.Count; i++)
                {
                    string filename = fileNames[i];
                    StringReader reader = new StringReader(sources[i] ?? "");
                    Parser parser = new Parser(new Lexer(reader));
                    try
                    {
                        Start start = parser.Parse();
                        AASourceFile sourceNode = (AASourceFile)start.GetPSourceFile();
                        reader.Close();
                        reader.Dispose();
                        reader = new StringReader(sources[i] ?? "");
                        int lineCount = 0;
                        while (reader.ReadLine() != null)
                        {
                            lineCount++;
                        }
                        reader.Close();

                        sharedData.LineCounts[sourceNode] = lineCount;

                        //Extract encryption function
                         /*{
                             AASourceFile file = (AASourceFile) start.GetPSourceFile();
                             if (file.GetDecl().Count > 0 && file.GetDecl()[0] is AMethodDecl)
                             {
                                 AMethodDecl method = (AMethodDecl) file.GetDecl()[0];
                                 if (method.GetName().Text == "Galaxy_pp_Deobfuscate")
                                 {
                                     FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                     IFormatter formatter = new BinaryFormatter();
                                     Stream stream = dobfuscateFile.Open(FileMode.Create);
                                     formatter.Serialize(stream, method);
                                     stream.Close();
                                 }
                             }
                         }*/

                        if (Options.Compiler.ObfuscateStrings)
                        {
                            HasStringConstExp checker = new HasStringConstExp();
                            start.Apply(checker);

                            if (!addedDeobfuscator /* && checker.HasStringConst*/)
                            {
                                FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                IFormatter formatter = new BinaryFormatter();
                                Stream stream = dobfuscateFile.Open(FileMode.Open);
                                AASourceFile file = (AASourceFile)start.GetPSourceFile();

                                AMethodDecl method = (AMethodDecl)formatter.Deserialize(stream);
                                sharedData.DeobfuscateMethod = method;
                                method.GetName().Line = 0;

                                HasStringConstExp checker2 = new HasStringConstExp();
                                method.Apply(checker2);
                                file.GetDecl().Insert(0, method);
                                stream.Close();

                                addedDeobfuscator = true;

                                foreach (AStringConstExp stringConstExp in checker2.List)
                                {
                                    int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                    AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                      new TConst("const", line, 0),
                                                                      new ANamedType(
                                                                          new TIdentifier("string", line, 1),
                                                                          null),
                                                                      new TIdentifier("Galaxy_pp_stringU" +
                                                                                      sharedData.UnobfuscatedStrings
                                                                                          .
                                                                                          Count),
                                                                      null);
                                    //If the strings are the same - point them to same field
                                    bool newField = true;
                                    foreach (
                                        AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys)
                                    {
                                        if (stringConstExp.GetStringLiteral().Text ==
                                            oldStringConstExp.GetStringLiteral().Text)
                                        {
                                            field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                            newField = false;
                                            break;
                                        }
                                    }
                                    if (newField)
                                    {
                                        file.GetDecl().Insert(0, field);
                                        sharedData.ObfuscationFields.Add(field);
                                    }
                                    sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                }

                            }
                            foreach (AStringConstExp stringConstExp in checker.List)
                            {
                                int line = -sharedData.ObfuscatedStrings.Count - 1;
                                AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                  new TConst("const", line, 0),
                                                                  new ANamedType(new TIdentifier("string", line, 1),
                                                                                 null),
                                                                  new TIdentifier("Galaxy_pp_stringO" +
                                                                                  sharedData.ObfuscatedStrings.Count),
                                                                  null);
                                //If the strings are the same - point them to same field
                                bool newField = true;
                                foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                {
                                    if (stringConstExp.GetStringLiteral().Text ==
                                        oldStringConstExp.GetStringLiteral().Text)
                                    {
                                        field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                        newField = false;
                                        break;
                                    }
                                }
                                if (newField)
                                {
                                    AASourceFile file = (AASourceFile)sharedData.DeobfuscateMethod.Parent();
                                    file.GetDecl().Insert(file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                          field);
                                    sharedData.ObfuscationFields.Add(field);
                                }
                                sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                            }
                        }

                        sourceNode.SetName(new TIdentifier(filename));
                        root.GetSourceFiles().Add(start.GetPSourceFile());

                    }
                    catch (ParserException err)
                    {
                        String errMsg = err.Message;
                        //Remove [...]
                        errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                        errors.Add(new ErrorCollection.Error(err.Token, filename, errMsg));
                    }
                    reader.Close();
                }

                //Load libraries
                foreach (Library lib in ProjectProperties.CurrentProjectPropperties.Libraries)
                {
                    foreach (KeyValuePair<Library.File, string> sourceFile in lib.GetFiles())
                    {
                        StringReader sReader = new StringReader(sourceFile.Key.Text);
                        {
                            Parser parser = new Parser(new Lexer(sReader));

                            try
                            {
                                Start start = parser.Parse();
                                AASourceFile sourceNode = (AASourceFile) start.GetPSourceFile();
                                sReader.Close();
                                sReader.Dispose();
                                sReader = new StringReader(sourceFile.Key.Text);
                                int lineCount = 0;
                                while (sReader.ReadLine() != null)
                                {
                                    lineCount++;
                                }
                                sReader.Close();
                                sReader.Dispose();
                                sharedData.LineCounts[sourceNode] = lineCount;

                                if (Options.Compiler.ObfuscateStrings)
                                {
                                    HasStringConstExp checker = new HasStringConstExp();
                                    start.Apply(checker);

                                    if (!addedDeobfuscator /* && checker.HasStringConst*/)
                                    {
                                        FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                        IFormatter formatter = new BinaryFormatter();
                                        Stream stream = dobfuscateFile.Open(FileMode.Open);
                                        AASourceFile file = (AASourceFile) start.GetPSourceFile();

                                        AMethodDecl method = (AMethodDecl) formatter.Deserialize(stream);
                                        sharedData.DeobfuscateMethod = method;
                                        method.GetName().Line = 0;

                                        HasStringConstExp checker2 = new HasStringConstExp();
                                        method.Apply(checker2);
                                        file.GetDecl().Insert(0, method);
                                        stream.Close();

                                        addedDeobfuscator = true;

                                        foreach (AStringConstExp stringConstExp in checker2.List)
                                        {
                                            int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                            AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                              new TConst("const", line, 0),
                                                                              new ANamedType(
                                                                                  new TIdentifier("string", line, 1),
                                                                                  null),
                                                                              new TIdentifier("Galaxy_pp_stringU" +
                                                                                              sharedData.
                                                                                                  UnobfuscatedStrings
                                                                                                  .
                                                                                                  Count),
                                                                              null);
                                            //If the strings are the same - point them to same field
                                            bool newField = true;
                                            foreach (
                                                AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys
                                                )
                                            {
                                                if (stringConstExp.GetStringLiteral().Text ==
                                                    oldStringConstExp.GetStringLiteral().Text)
                                                {
                                                    field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                                    newField = false;
                                                    break;
                                                }
                                            }
                                            if (newField)
                                            {
                                                file.GetDecl().Insert(0, field);
                                                sharedData.ObfuscationFields.Add(field);
                                            }
                                            sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                        }

                                    }
                                    foreach (AStringConstExp stringConstExp in checker.List)
                                    {
                                        int line = -sharedData.ObfuscatedStrings.Count - 1;
                                        AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                          new TConst("const", line, 0),
                                                                          new ANamedType(
                                                                              new TIdentifier("string", line, 1),
                                                                              null),
                                                                          new TIdentifier("Galaxy_pp_stringO" +
                                                                                          sharedData.ObfuscatedStrings.
                                                                                              Count),
                                                                          null);
                                        //If the strings are the same - point them to same field
                                        bool newField = true;
                                        foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                        {
                                            if (stringConstExp.GetStringLiteral().Text ==
                                                oldStringConstExp.GetStringLiteral().Text)
                                            {
                                                field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                                newField = false;
                                                break;
                                            }
                                        }
                                        if (newField)
                                        {
                                            AASourceFile file = (AASourceFile) sharedData.DeobfuscateMethod.Parent();
                                            file.GetDecl().Insert(
                                                file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                field);
                                            sharedData.ObfuscationFields.Add(field);
                                        }
                                        sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                                    }
                                }

                                sourceNode.SetName(new TIdentifier(sourceFile.Value));
                                root.GetSourceFiles().Add(start.GetPSourceFile());

                            }
                            catch (ParserException err)
                            {
                                String errMsg = err.Message;
                                //Remove [...]
                                errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                                errors.Add(new ErrorCollection.Error(err.Token, sourceFile.Value, errMsg));
                            }
                        }
                    }
                }

                string rootFileName = "";
                DirectoryInfo outputDir = ProjectDir.CreateSubdirectory("output");
                try
                {
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text4"));
                    sharedData.Libraries = libraryData;
                    Weeder.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text5"));
                    if (!errors.HasErrors) EnviromentBuilding.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text6"));
                    if (!errors.HasErrors) EnviromentChecking.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text7"));
                    if (!errors.HasErrors) root.Apply(new LinkNamedTypes(errors, sharedData));

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text8"));
                    if (!errors.HasErrors) root.Apply(new FixGenerics(errors, sharedData));

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text9"));
                    if (!errors.HasErrors) root.Apply(new Enheritance(sharedData, errors));

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text10"));
                    if (!errors.HasErrors) TypeLinking.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text11"));
                    if (!errors.HasErrors) TypeChecking.Parse(root, errors, sharedData);
                    if (!errors.HasErrors) root.Apply(new MakeEnrichmentLinks(sharedData, errors));
                    if (!errors.HasErrors) root.Apply(new SetArrayIndexes(sharedData, errors));
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text12"));
                    if (!errors.HasErrors) FinalTransformations.Parse(root, errors, sharedData, out rootFileName);
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text13"));
                    if (!errors.HasErrors) CodeGeneration.Parse(root, errors, sharedData, outputDir);
                    if (!errors.HasErrors) GenerateBankPreloadFile.Generate(sharedData, outputDir);
                }
                catch (ParserException err)
                {

                }

                Compiling = false;

                if (!errors.HasErrors)
                {
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text14"));
                    ProjectProperties.CurrentProjectPropperties.RootFileName = rootFileName;
                    ProjectProperties.CurrentProjectPropperties.CompileStatus =
                        ProjectProperties.ECompileStatus.SuccessfullyCompiled;
                    if (CompilationSuccessfull != null)
                        CompilationSuccessfull();
                }
                else
                {
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text15"));
                    if (CompilationFailed != null)
                        CompilationFailed();
                }

            }
            #if DEBUG
            finally
            {

            }
            #else
            catch (Exception error)
            {
                Compiling = false;
                //Program.ErrorHandeler(this, new ThreadExceptionEventArgs(error));
                new ExceptionForm(error, true).ShowDialog();
                form.SetStatusText("Critical compile error");
                if (CompilationFailed != null)
                    CompilationFailed();
            }
            #endif
        }
 public LibraryData(AAProgram program, StreamWriter writer)
 {
     this.writer = writer;
     program.Apply(this);
 }
Esempio n. 11
0
 public override void CaseAAProgram(AAProgram node)
 {
     GetOldParameterTypes getOldParameterTypes = new GetOldParameterTypes();
     node.Apply(getOldParameterTypes);
     oldParameterTypes = getOldParameterTypes.OldParameterTypes;
     oldReturnTypes = getOldParameterTypes.OldReturnTypes;
     foreach (AASourceFile sourceFile in node.GetSourceFiles())
     {
         foreach (PDecl decl in sourceFile.GetDecl())
         {
             if (decl is AMethodDecl)
             {
                 GetUsedParameters getUses = new GetUsedParameters(data);
                 decl.Apply(getUses);
                 UsedParameters[(AMethodDecl) decl] = getUses.UsedParameters;
             }
         }
     }
      	            base.CaseAAProgram(node);
     while (mover.NewStatements.Count > 0)
     {
         List<PStm> stms = mover.NewStatements;
         mover = new MoveMethodDeclsOut("bulkCopyVar", data);
         foreach (PStm stm in stms)
         {
             stm.Apply(this);
         }
     }
 }
 public override void InAAProgram(AAProgram node)
 {
     if (Options.Compiler.AutomaticallyInlineShortMethods)
         node.Apply(new MarkShortMethodsAsInline(finalTrans, inlineConstructors));
     base.InAAProgram(node);
 }
        private void Apply(AAProgram ast)
        {
            int stage = 1;
            int totalStages = 31;

            List<ANamedType> deleteUs = new List<ANamedType>();
            foreach (KeyValuePair<ANamedType, AStructDecl> pair in data.StructTypeLinks)
            {
                ANamedType type = pair.Key;
                AStructDecl str = pair.Value;
                if (data.Enums.ContainsKey(str))
                {
                    type.SetName(new AAName(new ArrayList(){new TIdentifier(data.Enums[str] ? "int" : "byte")}));
                    deleteUs.Add(type);
                }
            }
            foreach (ANamedType type in deleteUs)
            {
                data.StructTypeLinks.Remove(type);
            }

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text2"));
            stage++;

            ast.Apply(new RemoveNamespaces());

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text3"));
            stage++;

            ast.Apply(new StaticStructMembers(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text4"));
            stage++;

            ast.Apply(new RemoveConstants(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text5"));
            stage++;

            ast.Apply(new MainEntryFinder(this));

            if (mainEntry == null)
            {
                //errors.Add(new ErrorCollection.Error("No entry point found (void InitMap(){...})", true));

                //Generate main entry
                AASourceFile file =
                    ast.GetSourceFiles().Cast<AASourceFile>().FirstOrDefault(
                        sourceFile => !Util.GetAncestor<AASourceFile>(sourceFile).GetName().Text.Contains("\\"));
                if (file == null)
                {
                    //Make default sourcefile
                    file = new AASourceFile();
                    file.SetName(new TIdentifier("MapScript"));
                    ast.GetSourceFiles().Add(file);
                    data.LineCounts[file] = 1;
                }
                mainEntry = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null, new AVoidType(new TVoid("void")), new TIdentifier("InitMap"), new ArrayList(), new AABlock());
                file.GetDecl().Add(mainEntry);
                data.Methods.Add(new SharedData.DeclItem<AMethodDecl>(file, mainEntry));
            }
            else if (Util.GetAncestor<AASourceFile>(mainEntry).GetName().Text.Contains("\\"))
                errors.Add(new ErrorCollection.Error(mainEntry.GetName(), Util.GetAncestor<AASourceFile>(mainEntry), LocRM.GetString("FT_Text6"), true));

            ((AABlock)mainEntry.GetBlock()).GetStatements().Insert(0, mainEntryFieldInitBlock);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text7"));
            stage++;

            ast.Apply(new StructInitializer(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text8"));
            stage++;

            ast.Apply(new TransformExpressionIfs(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text9"));
            stage++;

            ast.Apply(new RenameUnicode(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text10"));
            stage++;

            TransformProperties.Phase1.Parse(this);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text11"));
            stage++;

            ast.Apply(new RemoveDeadCode());

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text12"));
            stage++;

            ast.Apply(new TransformMethodDecls(this));

            if (errors.HasErrors)
                return;

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text13"));
            stage++;

            MakeInitializerInvokes();

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text14"));
            stage++;

            ast.Apply(new AssignFixup(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text15"));
            stage++;

            TransformProperties.Phase2.Parse(this);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text16"));
            stage++;

            if (data.Invokes.Count > 0)
                Invokes.Parse(this);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text17"));
            stage++;

            ast.Apply(new RemoveEmptyStructs(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text18"));
            stage++;

            ast.Apply(new Delegates(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text19"));
            stage++;

            //ast.Apply(new AddUnneededRef(data));
            ast.Apply(new FixInlineMethods(this, false));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text20"));
            stage++;

            ast.Apply(new RemoveUnnededRef(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text21"));
            stage++;

            ast.Apply(new SplitStructTests(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text22"));
            stage++;

            new Pointers(data).Parse(ast);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text23"));
            stage++;

            ast.Apply(new FixInlineMethods(this, true));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text24"));
            stage++;

            //Split local struct into primitives, to make optimizations easier
            ast.Apply(new StructSplitter(data));
            //ast.Apply(new BulkCopyFixup(this));
            //BulkCopyFixup.Parse(ast, this);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text25"));
            stage++;

            //Remove stupid assignments (assignments to a variable where that variable is not used before its next assignment
            ast.Apply(new RemoveUnusedVariables(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text26"));
            stage++;

            ast.Apply(new ConstantFolding(data));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text27"));
            stage++;

            //Assign fixup was here //Dahm grafiti painters
            //ast.Apply(new LivenessAnalysis(this));
            ast.Apply(new Optimizations.OptimizePhase(this, LocRM.GetString("FT_Text1") + (stage - 1) + " / " + totalStages + LocRM.GetString("FT_Text27")));

            ast.Apply(new FixByteArrayIndexes(data));

            if (Options.Compiler.MakeShortNames)
            {
                if (data.AllowPrintouts)
                    Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text28"));
                stage++;

                ast.Apply(new MakeShortNames(this));
            }
            else
            {
                if (data.AllowPrintouts)
                    Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text29"));
                stage++;

                //MakeUniqueNames.Parse(ast, this);
                ast.Apply(new MakeUniqueNamesV2());
            }

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text30"));
            stage++;
            //Remove uneeded blocks and check that names fit the decls
            ast.Apply(new RenameRefferences(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text31"));
            stage++;
            //Obfuscate strings
            ast.Apply(new ObfuscateStrings(this));

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text32"));
            stage++;
            MergeSameMethods.Parse(this);

            if (data.AllowPrintouts)
                Form1.Form.SetStatusText(LocRM.GetString("FT_Text1") + stage + " / " + totalStages + LocRM.GetString("FT_Text33"));
            stage++;
            //Insert includes, and move methods, structs and fields around so they are visible
            FixIncludes.Apply(ast, this);
            if (Options.Compiler.OneOutputFile)
                ((AASourceFile)mainEntry.Parent()).SetName(new TIdentifier("MapScript"));
        }