Beispiel #1
0
        private static void Join(AALocalDecl decl1, AALocalDecl decl2, ControlFlowGraph cfg, Dictionary <AALocalDecl, ControlFlowGraph.Node> localNodes, SharedData data)
        {
            //Remove decl2 from cfg
            //decl2 can't be a formal, since formals was first in the list, and two formals wont be joined.
            ControlFlowGraph.Node cfgNode2 = localNodes[decl2];
            cfg.Remove(cfgNode2);
            //Remove decl2 from the ast
            cfgNode2.Statement.Parent().RemoveChild(cfgNode2.Statement);
            //Go through cfg and make live decl2 variables become live decl1 variables.));
            foreach (ControlFlowGraph.Node cfgNode in cfg.Nodes)
            {
                if (cfgNode.LiveVariables.Contains(decl2))
                {
                    cfgNode.LiveVariables.Remove(decl2);
                    cfgNode.LiveVariables.Add(decl1);
                }
            }
            //Rename all refferences to decl2 to be decl1
            LinkedList <ALocalLvalue> keys = new LinkedList <ALocalLvalue>();

            foreach (KeyValuePair <ALocalLvalue, AALocalDecl> pair in data.LocalLinks)
            {
                if (pair.Value == decl2)
                {
                    keys.AddLast(pair.Key);
                }
            }
            foreach (ALocalLvalue key in keys)
            {
                data.LocalLinks[key] = decl1;
            }
        }
 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;
                 }
             }
         }
     }
 }
Beispiel #3
0
        public override void InAALocalDecl(AALocalDecl node)
        {
            decls.Add(node);

            /*node.GetName().Text = nextName;
             * NextName();*/
        }
        public override void CaseASimpleInvokeExp(ASimpleInvokeExp node)
        {
            PExp  expNode = (PExp)node;
            PType type    = data.ExpTypes[expNode];

            if (type is APointerType)
            {
                type = new ANamedType(new TIdentifier("string"), null);
            }
            ALocalLvalue local = new ALocalLvalue(new TIdentifier("tempName", 0, 0));
            ALvalueExp   exp   = new ALvalueExp(local);
            PStm         stm   = Util.GetAncestor <PStm>(node);
            AABlock      block = (AABlock)stm.Parent();

            node.ReplaceBy(exp);
            AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                    Util.MakeClone(type, data),
                                                    new TIdentifier(varName, 0, 0), expNode);
            ALocalDeclStm newStm = new ALocalDeclStm(new TSemicolon(";"), localDecl);

            block.GetStatements().Insert(block.GetStatements().IndexOf(stm), newStm);
            NewStatements.Add(newStm);

            data.LvalueTypes[local] = type;
            data.ExpTypes[exp]      = type;
            data.LocalLinks[local]  = localDecl;
            //localDecl.Apply(this);
            exp.Apply(this);
            return;
        }
 public FixReturnsAndWhiles(AALocalDecl hasMethodReturnedVar, AALocalDecl methodReturnerVar, SharedData data, bool neededWhile)
 {
     this.hasMethodReturnedVar = hasMethodReturnedVar;
     this.methodReturnerVar    = methodReturnerVar;
     this.data        = data;
     this.neededWhile = neededWhile;
 }
Beispiel #6
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);
        }
Beispiel #7
0
        public VariableDescription(AALocalDecl localDecl, VariableTypes type)
        {
            Name = localDecl.GetName().Text;
            Type = Util.TypeToString(localDecl.GetType());
            switch (type)
            {
            case VariableTypes.LocalVariable:
                PlacementPrefix = "Local";
                break;

            case VariableTypes.Parameter:
                PlacementPrefix = "Parameter";
                break;

            case VariableTypes.StructVariable:
                PlacementPrefix = "Struct field";
                break;

            default:
                PlacementPrefix = "";
                break;
            }
            VariableType = type;
            Const        = localDecl.GetConst() != null;
            IsStatic     = localDecl.GetStatic() != null;
            Visibility   = localDecl.GetVisibilityModifier();
            realType     = (PType)localDecl.GetType().Clone();
            init         = localDecl.GetInit();
            Line         = localDecl.GetName().Line;
            Position     = TextPoint.FromCompilerCoords(localDecl.GetName());
        }
Beispiel #8
0
        public static bool Parse(ControlFlowGraph cfg, SharedData data)
        {
            List <AALocalDecl> usedLocals = GetUsedLocals.Parse(cfg.Method.GetBlock(), data);

            List <ControlFlowGraph.Node> modifications = new List <ControlFlowGraph.Node>();

            foreach (ControlFlowGraph.Node node in cfg.Nodes)
            {
                if (node.Statement is ALocalDeclStm)
                {
                    AALocalDecl decl = (AALocalDecl)((ALocalDeclStm)node.Statement).GetLocalDecl();
                    if (!usedLocals.Contains(decl))
                    {
                        modifications.Add(node);
                    }
                }
                else
                {
                    break;
                }
            }
            foreach (ControlFlowGraph.Node node in modifications)
            {
                cfg.Remove(node);
                node.Statement.Parent().RemoveChild(node.Statement);
            }
            return(modifications.Count > 0);
        }
Beispiel #9
0
        public override void CaseAThisArrayPropertyDecl(AThisArrayPropertyDecl node)
        {
            APropertyDecl replacer = new APropertyDecl(node.GetVisibilityModifier(), null, node.GetType(),
                                                       new TIdentifier("array property", node.GetToken().Line, node.GetToken().Pos),
                                                       node.GetGetter(), node.GetSetter());
            List <AALocalDecl> locals = new List <AALocalDecl>();

            if (replacer.GetGetter() != null)
            {
                AABlock     block = (AABlock)replacer.GetGetter();
                AALocalDecl local = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                    (PType)node.GetArgType().Clone(),
                                                    (TIdentifier)node.GetArgName().Clone(), null);
                block.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), local));
                locals.Add(local);
            }
            if (replacer.GetSetter() != null)
            {
                AABlock     block = (AABlock)replacer.GetSetter();
                AALocalDecl local = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                    (PType)node.GetArgType().Clone(),
                                                    (TIdentifier)node.GetArgName().Clone(), null);

                block.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), local));
                locals.Add(local);
            }
            node.ReplaceBy(replacer);
            replacer.Apply(this);
        }
Beispiel #10
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);
        }
            public static bool HasMoreThanOneUses(PStm statement, AALocalDecl decl, SharedData data, out ALocalLvalue lvalue)
            {
                GetUses uses = new GetUses(decl, data);

                statement.Apply(uses);
                lvalue = uses.lvalue;
                return(uses.count > 1);
            }
        public override void CaseALocalLvalue(ALocalLvalue node)
        {
            AALocalDecl decl = data.LocalLinks[node];

            if (!UsedLocals.Contains(decl))
            {
                UsedLocals.Add(decl);
            }
        }
Beispiel #13
0
 public LocalDecl(AALocalDecl decl, AMethodDecl parentMethod)
 {
     Decl         = decl;
     ParentMethod = parentMethod;
     if (Name.StartsWith("_"))
     {
         Name = "u" + Name;
     }
 }
Beispiel #14
0
        public override void CaseAALocalDecl(AALocalDecl node)
        {
            //It wont enter methods
            //Repeated fields in structs are syntax errors
            AStructDecl str = Util.GetAncestor <AStructDecl>(node);

            StructFields[str].Add(node);
            node.Parent().RemoveChild(node);
        }
        private int FoldInt(PLvalue lvalue, ref bool valid)
        {
            if (!valid)
            {
                return(-1);
            }

            if (lvalue is ALocalLvalue)
            {
                ALocalLvalue aLvalue = (ALocalLvalue)lvalue;
                AALocalDecl  decl    = data.LocalLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }
            if (lvalue is AFieldLvalue)
            {
                AFieldLvalue aLvalue = (AFieldLvalue)lvalue;
                AFieldDecl   decl    = data.FieldLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }
            if (lvalue is AStructFieldLvalue)
            {
                AStructFieldLvalue aLvalue = (AStructFieldLvalue)lvalue;
                AALocalDecl        decl    = data.StructMethodFieldLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }
            if (lvalue is AStructLvalue)
            {
                AStructLvalue aLvalue = (AStructLvalue)lvalue;
                AALocalDecl   decl    = data.StructFieldLinks[aLvalue];
                if (decl.GetConst() == null)
                {
                    valid = false;
                    return(-1);
                }
                return(FoldInt(decl.GetInit(), ref valid));
            }

            valid = false;
            return(-1);
        }
 public void Add(AALocalDecl local, bool read, bool written)
 {
     if (Locals.ContainsKey(local))
     {
         Locals[local].Union(read, written);
     }
     else
     {
         Locals[local] = new Values(read, written);
     }
 }
        public static bool Parse(ControlFlowGraph cfg, SharedData data, out bool redoLivenessAnalysis)
        {
            bool changed = false;

            redoLivenessAnalysis = false;
            Dictionary <ControlFlowGraph.Node, List <ASimpleInvokeExp> > Modifications = new Dictionary <ControlFlowGraph.Node, List <ASimpleInvokeExp> >();

            foreach (ControlFlowGraph.Node node in cfg.Nodes)
            {
                if (node.Expression is AAssignmentExp)
                {
                    AAssignmentExp exp = (AAssignmentExp)node.Expression;
                    if (exp.GetLvalue() is ALocalLvalue)
                    {
                        AALocalDecl decl = data.LocalLinks[(ALocalLvalue)exp.GetLvalue()];
                        //If the variable is not live at any successors, remove this assignment
                        bool inUse = false;
                        foreach (ControlFlowGraph.Node successor in node.Successors)
                        {
                            if (successor.LiveVariables.Contains(decl))
                            {
                                inUse = true;
                                break;
                            }
                        }
                        if (!inUse)
                        {
                            //Move method invokes out
                            GetMethodInvokes getter = new GetMethodInvokes();
                            exp.GetExp().Apply(getter);
                            //Might also have to redo because we removed a reference to a variable in the right side
                            //if (getter.Invokes.Count > 0)
                            redoLivenessAnalysis = true;
                            Modifications[node]  = getter.Invokes;
                            changed = true;
                        }
                    }
                }
            }
            foreach (KeyValuePair <ControlFlowGraph.Node, List <ASimpleInvokeExp> > pair in Modifications)
            {
                ControlFlowGraph.Node node = pair.Key;
                foreach (ASimpleInvokeExp invoke in pair.Value)
                {
                    AExpStm stm    = new AExpStm(new TSemicolon(";"), invoke);
                    AABlock pBlock = (AABlock)node.Statement.Parent();
                    pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node.Statement), stm);
                    cfg.Insert(node, stm);
                }
                cfg.Remove(node);
                node.Statement.Parent().RemoveChild(node.Statement);
            }
            return(changed);
        }
Beispiel #18
0
 public override void CaseAAssignmentExp(AAssignmentExp node)
 {
     currentLocal = null;
     node.GetLvalue().Apply(this);
     if (currentLocal != null)
     {
         AALocalDecl decl = data.LocalLinks[currentLocal];
         NeededRefs[Util.GetAncestor <AMethodDecl>(node)].Add(decl);
     }
     node.GetExp().Apply(this);
 }
 public override void CaseAALocalDecl(AALocalDecl node)
 {
     if (!processFieldsOnly && !processMethodsOnly && !processStructs)
     {
         if (!definedLocals.Contains(node))
         {
             definedLocals.Add(node);
             assignedToLocals[node] = new List <AAssignmentExp>();
         }
     }
     base.CaseAALocalDecl(node);
 }
Beispiel #20
0
        public override void CaseAALocalDecl(AALocalDecl node)
        {
            if (node.GetConst() == null)
            {
                return;
            }

            initialLocalDecl = node;

            if (IsConstant(node.GetInit()))
            {
                {
                    List <ALocalLvalue> lvalues = new List <ALocalLvalue>();
                    lvalues.AddRange(data.LocalLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (ALocalLvalue lvalue in lvalues)
                    {
                        PExp parent = (PExp)lvalue.Parent();
                        parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                    }
                }
                {
                    List <AStructLvalue> lvalues = new List <AStructLvalue>();
                    lvalues.AddRange(data.StructFieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (AStructLvalue lvalue in lvalues)
                    {
                        PExp parent = (PExp)lvalue.Parent();
                        parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                    }
                }
                {
                    List <AStructFieldLvalue> lvalues = new List <AStructFieldLvalue>();
                    lvalues.AddRange(
                        data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (AStructFieldLvalue lvalue in lvalues)
                    {
                        PExp parent = (PExp)lvalue.Parent();
                        parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                    }
                }
                if (node.Parent() is ALocalDeclStm)
                {
                    node.Parent().Parent().RemoveChild(node.Parent());
                }
                else
                {
                    node.Parent().RemoveChild(node);
                }
            }


            initialLocalDecl = null;
        }
Beispiel #21
0
 public override void CaseAALocalDecl(AALocalDecl node)
 {
     if (node.GetConst() != null)
     {
         Write("const ");
     }
     node.GetType().Apply(this);
     Write(" " + node.GetName().Text);
     if (node.GetInit() != null)
     {
         Write(" = ");
         node.GetInit().Apply(this);
     }
 }
Beispiel #22
0
        //Convert struct variables to a collection of local variables
        public override void CaseAALocalDecl(AALocalDecl node)
        {
            if (node.GetType() is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)node.GetType()) && Util.HasAncestor <PStm>(node))
            {
                //Can not have init - it would be bulk copy
                AStructDecl str = data.StructTypeLinks[(ANamedType)node.GetType()];
                Dictionary <AALocalDecl, AALocalDecl> variableMap = new Dictionary <AALocalDecl, AALocalDecl>();
                PStm    pStm   = (PStm)node.Parent();
                AABlock pBlock = (AABlock)pStm.Parent();
                foreach (AALocalDecl structLocal in str.GetLocals())
                {
                    AALocalDecl replacementLocal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                   Util.MakeClone(structLocal.GetType(), data),
                                                                   new TIdentifier(node.GetName().Text + "_" +
                                                                                   structLocal.GetName().Text), null);
                    pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), replacementLocal));


                    AALocalDecl baseLocal = structLocal;
                    if (data.EnheritanceLocalMap.ContainsKey(baseLocal))
                    {
                        baseLocal = data.EnheritanceLocalMap[baseLocal];
                    }
                    List <AALocalDecl> localsToAdd = new List <AALocalDecl>();
                    localsToAdd.AddRange(data.EnheritanceLocalMap.Where(pair => pair.Value == baseLocal).Select(pair => pair.Key));
                    localsToAdd.Add(baseLocal);
                    foreach (AALocalDecl localDecl in localsToAdd)
                    {
                        variableMap[localDecl] = replacementLocal;
                    }
                }
                List <ALocalLvalue> uses = new List <ALocalLvalue>();
                uses.AddRange(data.LocalLinks.Where(k => k.Value == node && Util.GetAncestor <AAProgram>(k.Key) != null).Select(k => k.Key));
                foreach (ALocalLvalue lvalue in uses)
                {
                    AStructLvalue structLocalRef = (AStructLvalue)lvalue.Parent().Parent();

                    AALocalDecl  replacementLocal  = variableMap[data.StructFieldLinks[structLocalRef]];
                    ALocalLvalue replacementLvalue = new ALocalLvalue(new TIdentifier(replacementLocal.GetName().Text));
                    data.LocalLinks[replacementLvalue]  = replacementLocal;
                    data.LvalueTypes[replacementLvalue] = replacementLocal.GetType();
                    structLocalRef.ReplaceBy(replacementLvalue);
                }
                foreach (AALocalDecl replacementLocal in variableMap.Select(k => k.Value))
                {
                    replacementLocal.Apply(this);
                }
            }
            base.CaseAALocalDecl(node);
        }
        public override void CaseALocalLvalue(ALocalLvalue node)
        {
            if (processFieldsOnly || processMethodsOnly || processStructs)
            {
                return;
            }

            AALocalDecl decl = finalTrans.data.LocalLinks[node];

            if (!usedLocals.Contains(decl))
            {
                usedLocals.Add(decl);
            }
            base.CaseALocalLvalue(node);
        }
Beispiel #24
0
        public static bool IsStaticContext(Node node)
        {
            //Must be in a struct/enrichment, and in a static method/property/localdecl
            if (HasAncestor <AStructDecl>(node) || HasAncestor <AEnrichmentDecl>(node))
            {
                AMethodDecl   pMethod    = GetAncestor <AMethodDecl>(node);
                APropertyDecl pProperty  = GetAncestor <APropertyDecl>(node);
                AALocalDecl   pLocalDecl = GetAncestor <AALocalDecl>(node);

                return(pMethod != null && pMethod.GetStatic() != null ||
                       pProperty != null && pProperty.GetStatic() != null ||
                       pLocalDecl != null && pLocalDecl.GetStatic() != null);
            }
            return(false);
        }
Beispiel #25
0
        public override void OutAALocalDecl(AALocalDecl node)
        {
            if (!Util.HasAncestor <AABlock>(node) && !Util.HasAncestor <AMethodDecl>(node))
            {
                //OutStructFieldDecl(node);
                return;
            }

            if (node.GetInit() != null)
            {
                return;
            }

            AABlock pBlock;
            int     insertIndex;
            PLvalue lvalue;

            if (Util.HasAncestor <AABlock>(node))
            {
                //A local variable
                pBlock      = Util.GetAncestor <AABlock>(node);
                insertIndex = pBlock.GetStatements().IndexOf(Util.GetAncestor <PStm>(node)) + 1;
                lvalue      = new ALocalLvalue(new TIdentifier(node.GetName().Text));
                data.LocalLinks[(ALocalLvalue)lvalue] = node;
                data.LvalueTypes[lvalue] = node.GetType();
            }
            else
            {
                //Parameter

                //Parameters will be set from the caller
                return;

                pBlock      = (AABlock)Util.GetAncestor <AMethodDecl>(node).GetBlock();
                insertIndex = 0;
                lvalue      = new ALocalLvalue(new TIdentifier(node.GetName().Text));
                data.LocalLinks[(ALocalLvalue)lvalue] = node;
                data.LvalueTypes[lvalue] = node.GetType();
            }
            AABlock block = new AABlock(new ArrayList(), new TRBrace("}"));

            MakeAssignments(block, node.GetType(), lvalue, true);

            if (block.GetStatements().Count != 0)
            {
                pBlock.GetStatements().Insert(insertIndex, new ABlockStm(new TLBrace("{"), block));
            }
        }
Beispiel #26
0
            public override void CaseAALocalDecl(AALocalDecl node)
            {
                //If parent is a methoddecl, we are a parameter
                if (node.Parent() is AMethodDecl)
                {
                    VariableDescription variable = new VariableDescription(node, VariableDescription.VariableTypes.Parameter);
                    Formals.Add(variable);
                }
                else
                {
                    VariableDescription variable = new VariableDescription(node, VariableDescription.VariableTypes.LocalVariable);
                    Locals.Add(variable);
                }

                base.CaseAALocalDecl(node);
            }
Beispiel #27
0
        public override void OutAEnumDecl(AEnumDecl node)
        {
            AStructDecl replacer = new AStructDecl(node.GetVisibilityModifier(), null, null, null, node.GetEndToken(),
                                                   node.GetName(), new ArrayList(), null, new ArrayList());



            int intVal = 0;

            //int min = int.MaxValue;
            //int max = int.MinValue;
            //List<TIdentifier> types = new List<TIdentifier>();
            foreach (AAEnumLocal value in node.GetValues())
            {
                AIntConstExp intConst;
                if (value.GetValue() != null)
                {
                    intConst = (AIntConstExp)value.GetValue();
                    intVal   = int.Parse(intConst.GetIntegerLiteral().Text) + 1;
                }
                else
                {
                    intConst = new AIntConstExp(new TIntegerLiteral(intVal.ToString(), value.GetName().Line, value.GetName().Pos));
                    intVal++;
                }
                //    min = Math.Min(intVal - 1, min);
                //    max = Math.Max(intVal - 1, max);
                TIdentifier typeIdentifier = new TIdentifier(replacer.GetName().Text, value.GetName().Line, value.GetName().Pos);
                // types.Add(typeIdentifier);
                AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(),
                                                        new TStatic("static", value.GetName().Line, value.GetName().Pos),
                                                        null, null,
                                                        new TConst("const", value.GetName().Line, value.GetName().Pos),
                                                        new ANamedType(typeIdentifier, null), value.GetName(), intConst);
                replacer.GetLocals().Add(localDecl);
            }

            /*   if (min < 0 || max > 255)
             *     foreach (TIdentifier identifier in types)
             *     {
             *         identifier.Text = "int";
             *     }*/
            node.ReplaceBy(replacer);

            replacer.Apply(this);
            replacer.GetName().Text = "enum " + replacer.GetName().Text;
        }
Beispiel #28
0
            private void MoveOut(PExp exp, PType type)
            {
                PStm    pStm   = Util.GetAncestor <PStm>(exp);
                AABlock pBlock = (AABlock)pStm.Parent();

                ALocalLvalue lvalue    = new ALocalLvalue(new TIdentifier("gppVar"));
                ALvalueExp   lvalueExp = new ALvalueExp(lvalue);

                exp.ReplaceBy(lvalueExp);
                AALocalDecl decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(type, data), new TIdentifier("gppVar"), exp);

                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), decl));

                data.LvalueTypes[lvalue]     =
                    data.ExpTypes[lvalueExp] = decl.GetType();
                data.LocalLinks[lvalue]      = decl;
            }
            public override void OutALocalLvalue(ALocalLvalue node)
            {
                if (currentMethod == null)
                {
                    return;
                }

                AALocalDecl decl = data.LocalLinks[node];

                Node n = node;

                while (true)
                {
                    n = Util.GetNearestAncestor(n.Parent(), typeof(AMethodDecl), typeof(AAssignmentExp), typeof(AArrayLvalue));
                    if (n is AMethodDecl)
                    {
                        break;
                    }
                    if (n is AAssignmentExp)
                    {
                        if (Util.IsAncestor(node, ((AAssignmentExp)n).GetLvalue()))
                        {
                            if (!WrittenLocals[currentMethod].Contains(decl))
                            {
                                WrittenLocals[currentMethod].Add(decl);
                            }
                            return;
                        }
                        break;
                    }
                    if (n is AArrayLvalue)
                    {
                        if (Util.IsAncestor(node, ((AArrayLvalue)n).GetBase()))
                        {
                            continue;
                        }
                        break;
                    }
                    break;
                }

                if (!ReadLocals[currentMethod].Contains(decl))
                {
                    ReadLocals[currentMethod].Add(decl);
                }
            }
Beispiel #30
0
        public override void CaseAAProgram(AAProgram node)
        {
            /*decls.AddRange(finalTrans.data.Structs.Select(str => str.Decl));
             * decls.AddRange(finalTrans.data.Methods.Select(str => str.Decl));
             * decls.AddRange(finalTrans.data.Fields.Select(str => str.Decl));
             * if (finalTrans.data.Locals.Count > 0)
             *  decls.AddRange(finalTrans.data.Locals.Select(str => str.Value).Aggregate(Aggregate));
             * if (finalTrans.data.Structs.Count > 0)
             *  decls.AddRange(finalTrans.data.StructFields.Values.Aggregate(Aggregate));*/
            base.CaseAAProgram(node);
            Random rand = new Random();

            while (decls.Count > 0)
            {
                int  i = rand.Next(decls.Count);
                Node n = decls[i];
                decls.RemoveAt(i);

                if (n is AFieldDecl)
                {
                    AFieldDecl an     = (AFieldDecl)n;
                    an.GetName().Text = nextName;
                }
                else if (n is AMethodDecl)
                {
                    AMethodDecl an = (AMethodDecl)n;
                    if (finalTrans.mainEntry == an || (an.GetTrigger() != null && finalTrans.data.HasUnknownTrigger))
                    {
                        continue;
                    }
                    an.GetName().Text = nextName;
                }
                else if (n is AALocalDecl)
                {
                    AALocalDecl an    = (AALocalDecl)n;
                    an.GetName().Text = nextName;
                }
                else if (n is AStructDecl)
                {
                    AStructDecl an    = (AStructDecl)n;
                    an.GetName().Text = nextName;
                }
                NextName();
            }
        }