Ejemplo n.º 1
0
 public override void CaseAMethodDecl(AMethodDecl node)
 {
     if (node.GetInline() != null)
     {
         bool canAlwaysInline = true;
         foreach (KeyValuePair <ASimpleInvokeExp, AMethodDecl> pair in data.SimpleMethodLinks)
         {
             if (pair.Value == node && !Util.HasAncestor <AABlock>(pair.Key))
             {
                 canAlwaysInline = false;
                 break;
             }
         }
         if (canAlwaysInline)
         {
             node.Parent().RemoveChild(node);
             if (finalTrans.data.Methods.Any(item => item.Decl == node))
             {
                 finalTrans.data.Methods.Remove(finalTrans.data.Methods.First(item => item.Decl == node));
             }
         }
     }
     else
     {
         base.CaseAMethodDecl(node);
     }
 }
Ejemplo n.º 2
0
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            if (node.GetInline() == null)
            {
                return;
            }

            //Variables marked as out can be made into ref
            foreach (AALocalDecl formal in node.GetFormals())
            {
                if (formal.GetOut() != null)
                {
                    formal.SetRef(new TRef("ref"));
                    formal.SetOut(null);
                }
            }
            assignedToLocals.Clear();
            base.CaseAMethodDecl(node);

            foreach (AALocalDecl formal in node.GetFormals())
            {
                if (!assignedToLocals.Contains(formal))
                {
                    formal.SetRef(new TRef("ref"));
                }
            }
        }
Ejemplo n.º 3
0
        private static void Update(AMethodDecl method, List <AMethodDecl> parsedMethods, ModifyData modifyData, Dictionary <AMethodDecl, List <AMethodDecl> > usedMethods, Dictionary <AMethodDecl, List <AFieldDecl> > readFields, Dictionary <AMethodDecl, List <AFieldDecl> > writtenFields)
        {
            if (parsedMethods.Contains(method))
            {
                return;
            }

            parsedMethods.Add(method);

            foreach (AFieldDecl fieldDecl in readFields[method])
            {
                modifyData.Add(fieldDecl, true, false);
            }
            foreach (AFieldDecl fieldDecl in writtenFields[method])
            {
                modifyData.Add(fieldDecl, false, true);
            }


            foreach (AMethodDecl usedMethod in usedMethods[method])
            {
                ModifyData data = GetModifyData(usedMethod);
                if (data == null)
                {
                    Update(usedMethod, parsedMethods, modifyData, usedMethods, readFields, writtenFields);
                }
                else
                {
                    modifyData.Union(data);
                }
            }
        }
        public static bool Parse(AMethodDecl method, SharedData data)
        {
            RemoveSelfAssignments remover = new RemoveSelfAssignments(data);

            method.GetBlock().Apply(remover);
            return(remover.changedSomething);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        /*public override void InAMethodDecl(AMethodDecl node)
         * {
         *  AABlock block = (AABlock) node.GetBlock();
         *  if (block != null)
         *  {
         *      if (!data.Locals.ContainsKey(block))
         *          data.Locals.Add(block, new List<AALocalDecl>());
         *      foreach (AALocalDecl formal in node.GetFormals())
         *      {
         *          data.Locals[block].Add(formal);
         *      }
         *  }
         * }
         *
         * public override void InAConstructorDecl(AConstructorDecl node)
         * {
         *  AABlock block = (AABlock)node.GetBlock();
         *  if (block != null)
         *  {
         *      if (!data.Locals.ContainsKey(block))
         *          data.Locals.Add(block, new List<AALocalDecl>());
         *      foreach (AALocalDecl formal in node.GetFormals())
         *      {
         *          data.Locals[block].Add(formal);
         *      }
         *  }
         * }*/

        public override void OutAMethodDecl(AMethodDecl node)
        {
            AStructDecl     parentStruct     = Util.GetAncestor <AStructDecl>(node);
            AEnrichmentDecl parentEnrichment = Util.GetAncestor <AEnrichmentDecl>(node);

            if (parentStruct != null)
            {
                //Struct method
                data.StructMethods[parentStruct].Add(node);
            }
            else if (parentEnrichment == null)
            {//Global method
                //Dont care about abstract methods - will add them later
                if (node.GetBlock() != null || node.GetNative() != null)
                {
                    data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(currentSourceFile, node));
                    data.UserMethods.Add(node);
                }
                else if (node.GetDelegate() != null)
                {
                    data.Delegates.Add(new SharedData.DeclItem <AMethodDecl>(currentSourceFile, node));
                }
                else
                {
                    node.Parent().RemoveChild(node);
                    return;
                }
            }
            base.OutAMethodDecl(node);
        }
Ejemplo n.º 7
0
 public override void OutAMethodDecl(AMethodDecl node)
 {
     //If void return is missing, insert it.
     if (node.GetReturnType() is AVoidType && node.GetBlock() != null)
     {
         AABlock block        = (AABlock)node.GetBlock();
         bool    insertReturn = false;
         while (true)
         {
             if (block.GetStatements().Count == 0)
             {
                 insertReturn = true;
                 break;
             }
             PStm lastStm = (PStm)block.GetStatements()[block.GetStatements().Count - 1];
             if (lastStm is AVoidReturnStm)
             {
                 break;
             }
             if (lastStm is ABlockStm)
             {
                 block = (AABlock)((ABlockStm)block.GetStatements()[block.GetStatements().Count - 1]).GetBlock();
                 continue;
             }
             insertReturn = true;
             break;
         }
         if (insertReturn)
         {
             block.GetStatements().Add(new AVoidReturnStm(new TReturn("return", block.GetToken().Line, block.GetToken().Pos)));
         }
     }
     base.OutAMethodDecl(node);
 }
Ejemplo n.º 8
0
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            Write("\n");
            if (node.GetStatic() != null)
            {
                Write("static ");
            }
            if (node.GetNative() != null)
            {
                Write("native ");
            }
            node.GetReturnType().Apply(this);
            Write(" " + node.GetName().Text + "(");
            bool first = true;

            foreach (AALocalDecl formal in node.GetFormals())
            {
                if (!first)
                {
                    Write(", ");
                }
                formal.Apply(this);
                first = false;
            }
            if (node.GetBlock() != null)
            {
                Write(")\n");
                node.GetBlock().Apply(this);
            }
            else
            {
                Write(");\n\n");
            }
        }
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            if (processStructs)
            {
                return;
            }

            AMethodDecl decl = finalTrans.data.SimpleMethodLinks[node];

            if (!processFieldsOnly && unusedMethods.Contains(decl))
            {
                unusedMethods.Remove(decl);
            }

            AFieldDecl field = Util.GetAncestor <AFieldDecl>(node);

            if (!processMethodsOnly && field != null)
            {
                if (!fieldsWithMethodCalls.Contains(field))
                {
                    fieldsWithMethodCalls.Add(field);
                }
            }

            base.CaseASimpleInvokeExp(node);
        }
Ejemplo n.º 10
0
            public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
            {
                if (currentMethod == null)
                {
                    return;
                }

                AMethodDecl targetMethod = data.SimpleMethodLinks[node];

                if (node.Parent() is APointerLvalue || node.Parent() is AStructLvalue)
                {
                    //Move node out
                    MoveOut(node, targetMethod.GetReturnType());
                }


                //Only if it is not a library method
                if (Util.HasAncestor <AAProgram>(node))
                {
                    if (!dependancies[currentMethod].Contains(targetMethod))
                    {
                        dependancies[currentMethod].Add(targetMethod);
                    }
                }
            }
Ejemplo n.º 11
0
 public override void CaseAMethodDecl(AMethodDecl node)
 {
     currentMethod      = node;
     dependancies[node] = new List <AMethodDecl>();
     base.CaseAMethodDecl(node);
     currentMethod = null;
 }
Ejemplo n.º 12
0
        public static bool Parse(AMethodDecl method)
        {
            StatementRemover remover = new StatementRemover();

            method.Apply(remover);
            return(remover.changedSomething);
        }
Ejemplo n.º 13
0
        private static ModifyData GetModifyData(AMethodDecl method)
        {
            if (methodData.ContainsKey(method))
            {
                return(methodData[method]);
            }

            if (Util.GetAncestor <AAProgram>(method) == null)
            {
                ModifyData modifyData = new ModifyData();
                string     name       = method.GetName().Text.ToLower();
                if (name.Contains("datatable"))
                {
                    modifyData.DataTable.Reads  |= name.Contains("get");
                    modifyData.DataTable.Writes |= name.Contains("set") || name.Contains("remove") || name.Contains("clear");
                }
                else
                {
                    modifyData.GameData.Writes |= name.Contains("set") || name.Contains("create");
                    modifyData.GameData.Reads   = true;
                }
                if (name == "wait")
                {
                    modifyData.Waits = true;
                }
                methodData[method] = modifyData;
                return(modifyData);
            }
            return(null);
        }
Ejemplo n.º 14
0
 public override void CaseALocalLvalue(ALocalLvalue node)
 {
     if (Util.HasAncestor <AMethodDecl>(node) &&                                         //Is in a method
         data.LocalLinks[node].Parent() == Util.GetAncestor <AMethodDecl>(node))         //Is a link to a formal in that method
     {
         if (Util.HasAncestor <AAssignmentExp>(node) &&                                  //Is in an assignement
             Util.IsAncestor(node, Util.GetAncestor <AAssignmentExp>(node).GetLvalue())) //Is left side of the assignment
         {
             AssignedFormals.Add(data.LocalLinks[node]);
         }
         else if (Util.HasAncestor <ASimpleInvokeExp>(node))
         {
             ASimpleInvokeExp invoke = Util.GetAncestor <ASimpleInvokeExp>(node);
             AMethodDecl      method = data.SimpleMethodLinks[invoke];
             for (int i = 0; i < invoke.GetArgs().Count; i++)
             {
                 AALocalDecl formal = (AALocalDecl)method.GetFormals()[i];
                 if ((formal.GetRef() != null || formal.GetOut() != null) && Util.IsAncestor(node, (Node)invoke.GetArgs()[i]))
                 {
                     AssignedFormals.Add(data.LocalLinks[node]);
                     return;
                 }
             }
         }
     }
 }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
            public override void CaseAMethodDecl(AMethodDecl node)
            {
                End        = Start = TextPoint.FromCompilerCoords(node.GetName().Line, node.GetName().Pos);
                ReturnType = Util.TypeToString(node.GetReturnType());
                Name       = node.GetName().Text;

                base.CaseAMethodDecl(node);
            }
Ejemplo n.º 17
0
 public MethodDecl(AMethodDecl decl)
 {
     Decl = decl;
     if (Name.StartsWith("_"))
     {
         Name = "u" + Name;
     }
 }
Ejemplo n.º 18
0
 private string GetName(AMethodDecl method)
 {
     if (names.ContainsKey(method))
     {
         return(names[method]);
     }
     return(names[method] = NextName());
 }
Ejemplo n.º 19
0
        public override void InAMethodDecl(AMethodDecl node)
        {
            decls.Add(node);

            /*if (finalTrans.mainEntry == node || (node.GetTrigger() != null && finalTrans.data.HasUnknownTrigger))
             *  return;
             * node.GetName().Text = nextName;
             * NextName();*/
        }
Ejemplo n.º 20
0
 public override void InAMethodDecl(AMethodDecl node)
 {
     currentMethod       = node;
     UsedMethods[node]   = new List <AMethodDecl>();
     ReadFields[node]    = new List <AFieldDecl>();
     WrittenFields[node] = new List <AFieldDecl>();
     ReadLocals[node]    = new List <AALocalDecl>();
     WrittenLocals[node] = new List <AALocalDecl>();
 }
Ejemplo n.º 21
0
 public LocalDecl(AALocalDecl decl, AMethodDecl parentMethod)
 {
     Decl         = decl;
     ParentMethod = parentMethod;
     if (Name.StartsWith("_"))
     {
         Name = "u" + Name;
     }
 }
Ejemplo n.º 22
0
 //Rename trigger refferences
 public override void OutAMethodDecl(AMethodDecl node)
 {
     if (finalTrans.data.TriggerDeclarations.ContainsKey(node))
     {
         foreach (TStringLiteral stringLiteral in finalTrans.data.TriggerDeclarations[node])
         {
             stringLiteral.Text = "\"" + node.GetName().Text + "\"";
         }
     }
 }
Ejemplo n.º 23
0
        //Rename method invocations
        public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
        {
            AMethodDecl method = finalTrans.data.SimpleMethodLinks[node];

            if (method == null)
            {
                method = null;
            }
            node.GetName().Text = finalTrans.data.SimpleMethodLinks[node].GetName().Text;
        }
Ejemplo n.º 24
0
        public static ControlFlowGraph Create(AMethodDecl method)
        {
            ControlFlowGraph graph     = new ControlFlowGraph(method);
            CFGGenerator     generator = new CFGGenerator();

            method.GetBlock().Apply(generator);
            graph.Nodes = new List <Node>(generator.Nodes.Count);
            graph.Nodes.AddRange(generator.Nodes);
            return(graph);
        }
Ejemplo n.º 25
0
        public override void InAABlock(AABlock node)
        {
            AMethodDecl pMethod = Util.GetAncestor <AMethodDecl>(node);

            if (!data.Locals.ContainsKey(node))
            {
                data.Locals.Add(node, new List <AALocalDecl>());
            }

            base.InAABlock(node);
        }
Ejemplo n.º 26
0
            public override void OutAMethodDecl(AMethodDecl node)
            {
                MethodDescription method = new MethodDescription(node);

                if (inEnrichment)
                {
                    method.Name = "";
                    method.Decl = null;
                }
                Methods.Add(method);
            }
Ejemplo n.º 27
0
        public override void CaseADelegateExp(ADelegateExp node)
        {
            /* Replace delegate<Type>(method)
             * With
             *
             * "method"
             *
             * or
             *
             * "method:reciever"
             */
            AMethodDecl    method = finalTrans.data.DelegateCreationMethod[node];
            string         d      = GetName(method);
            PExp           replacer;
            APointerLvalue reciever = finalTrans.data.DelegateRecieveres[node];

            if (reciever != null)
            {
                d += ":";
                AStringConstExp leftSide = new AStringConstExp(new TStringLiteral("\"" + d + "\""));
                replacer = new ABinopExp(leftSide, new APlusBinop(new TPlus("+")), reciever.GetBase());
                finalTrans.data.ExpTypes[leftSide]     =
                    finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null);

                if (Util.IsIntPointer(reciever, data.LvalueTypes[reciever], data))
                {
                    ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                              new ArrayList()
                    {
                        ((ABinopExp)replacer).GetRight()
                    });
                    ((ABinopExp)replacer).SetRight(intToStringInvoke);

                    finalTrans.data.SimpleMethodLinks[intToStringInvoke] =
                        data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);
                    data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);
                }
            }
            else
            {
                replacer = new AStringConstExp(new TStringLiteral("\"" + d + "\""));
                finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null);
            }
            MoveMethodDeclsOut mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);

            node.Apply(mover);
            node.ReplaceBy(replacer);
            foreach (PStm stm in mover.NewStatements)
            {
                stm.Apply(this);
            }
        }
Ejemplo n.º 28
0
        public override void CaseAMethodDecl(AMethodDecl node)
        {
            if (data.AllowPrintouts)
            {
                Form1.Form.SetStatusText(prefix + " [" + ++methodNr + "/" + methodCount + " " + node.GetName().Text + "]");
            }
            if (node.GetName().Text.Contains("t2"))
            {
                node = node;
            }
            //Move locals to the start
            node.Apply(new MoveLocalsToStart(finalTrans.data));
            bool changes = true;

            while (changes)
            {
                changes = false;

                //Remove foo = foo
                RemoveSelfAssignments.Parse(node, data);

                //Create cfg
                ControlFlowGraph cfg = ControlFlowGraph.Create(node);
                RemoveDeadCode.Parse(cfg);
                LivenessAnalysis.CalculateLiveVariables(cfg, data);
                bool redoLivenessAnalysis;
                changes |= RemoveUnusedAssignments.Parse(cfg, data, out redoLivenessAnalysis);
                if (redoLivenessAnalysis)
                {
                    LivenessAnalysis.CalculateLiveVariables(cfg, data);
                }
                changes |= VariableJoiner.Parse(cfg, data);
                //This phase doesn't use liveness analysis
                changes |= RemoveUnusedLocals.Parse(cfg, data);


                while (true)
                {
                    bool changed = RemoveSingleUsedAssignments.Parse(cfg, data);
                    if (changed)
                    {
                        changes = true;
                        LivenessAnalysis.CalculateLiveVariables(cfg, data);
                        continue;
                    }
                    break;
                }


                changes |= StatementRemover.Parse(node);
            }
        }
Ejemplo n.º 29
0
            public override void OutASimpleInvokeExp(ASimpleInvokeExp node)
            {
                if (currentMethod == null)
                {
                    return;
                }
                AMethodDecl decl = data.SimpleMethodLinks[node];

                if (!UsedMethods[currentMethod].Contains(decl))
                {
                    UsedMethods[currentMethod].Add(decl);
                }
            }
Ejemplo n.º 30
0
 public GetDependancies(SharedData data, AMethodDecl currentMethod = null)
 {
     this.currentMethod = currentMethod;
     if (currentMethod != null)
     {
         UsedMethods[currentMethod]   = new List <AMethodDecl>();
         ReadFields[currentMethod]    = new List <AFieldDecl>();
         WrittenFields[currentMethod] = new List <AFieldDecl>();
         ReadLocals[currentMethod]    = new List <AALocalDecl>();
         WrittenLocals[currentMethod] = new List <AALocalDecl>();
     }
     this.data = data;
 }