예제 #1
0
        // Expressions

        #region createMoveStatement

        private MoveStatement createMoveStatement(string id, SSAMap mapX, SSAMap mapY, string filename, int line)
        {
            SingleIdentifierExpression left = new SingleIdentifierExpression(id, new Location(filename, line, 0));

            left.IndexOfSSA = mapY.Search(id) + 1;
            SingleIdentifierExpression right = new SingleIdentifierExpression(id, new Location(filename, line, 0));

            right.IndexOfSSA = mapX.Search(id);
            return(new MoveStatement(left, right, filename, line));
        }
예제 #2
0
        /// <summary>
        /// Compares the current map with both maps to create a list of
        /// ThetaStatement in base of their information.
        /// </summary>
        /// <param name="maps">List of SSMap to compare.</param>
        /// <param name="map3">SSMap to compare and updates their values.</param>
        /// <param name="defaultFound">True if default case is found.</param>
        /// <param name="filename">File name.</param>
        /// <param name="line">Line to assign.</param>
        /// <returns>Returns the list of ThetaStatement.</returns>
        public List <ThetaStatement> GetThetaStatements(List <SSAMap> maps, ref SSAMap map3, bool defaultFound, string filename, int line)
        {
            List <ThetaStatement> stat = new List <ThetaStatement>();
            bool useCondition          = !defaultFound;

            if (this.map.Count == map3.ScopesCount)
            {
                for (int i = 0; i < this.map.Count; i++)
                {
                    if (this.map[i].Count == map3[i].Count)
                    {
                        Dictionary <string, SSAElement> .KeyCollection keys = this.map[i].Keys;
                        foreach (string key in keys)
                        {
                            if (this.map[i][key].IndexSSA != map3[i][key].IndexSSA)
                            {
                                List <SingleIdentifierExpression> list = new List <SingleIdentifierExpression>();

                                for (int j = 0; j < maps.Count; j++)
                                {
                                    if ((this.map.Count == maps[j].ScopesCount) && (this.map[i].Count == maps[j][i].Count))
                                    {
                                        if (((j == 0) && (this.map[i][key].IndexSSA != maps[j][i][key].IndexSSA)) ||
                                            ((j != 0) && (maps[j - 1][i][key].IndexSSA != maps[j][i][key].IndexSSA)))
                                        {
                                            list.Add(createNewExpression(maps[j], key, i, filename, line));
                                        }
                                        else
                                        {
                                            useCondition = true;
                                        }
                                    }
                                }

                                if (useCondition)
                                {
                                    list.Add(createNewExpression(this, key, i, filename, line));
                                    useCondition = !defaultFound;
                                }

                                list.Add(createNewExpression(map3, key, i, filename, line));
                                SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
                                id.IndexOfSSA = map3[i][key].IndexSSA + 1;
                                stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));

                                map3[i][key].UpdateIndexSSA(map3[i][key].IndexSSA + 1);
                            }
                        }
                    }
                }
            }

            return(stat);
        }
예제 #3
0
        /// <summary>
        /// Creates a new SingleIdentifierExpression to use in the new statement
        /// </summary>
        /// <param name="map">Map to use.</param>
        /// <param name="tmpName">Key to access to the specified position in the map.</param>
        /// <param name="position">Position to access in the map.</param>
        /// <param name="fileName">File name.</param>
        /// <param name="line">Line to assign.</param>
        /// <returns>Returns the SingleIdentifierExpression created.</returns>
        private SingleIdentifierExpression createNewExpression(SSAMap map, string key, int position, string filename, int line)
        {
            SingleIdentifierExpression id;

            //if ((position == 0) && (map[position][tmpName].IndexSSA == 0)) // use a real field
            //{
            //   id = new SingleIdentifierExpression(tmpName.Substring(6, tmpName.Length - 6), filename, line, 0);
            //   id.IndexOfSSA = -1;
            //}
            //else
            //{
            id            = new SingleIdentifierExpression(key, new Location(filename, line, 0));
            id.IndexOfSSA = map[position][key].IndexSSA;
            //}
            return(id);
        }
예제 #4
0
        public override Object Visit(WhileStatement node, Object obj)
        {
            Object aux  = null;
            SSAMap map1 = this.map.Clone();

            if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
            {
                node.Condition = (SingleIdentifierExpression)aux;
            }
            SSAMap map2 = this.map.Clone();

            this.map.SetScope();
            node.Statements.Accept(this, obj);
            this.addLocalVariable(this.map.ResetScope(), node.Statements);
            // map3 = this.map

            List <MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);

            if (mvSt.Count != 0)
            {
                node.InitWhile = mvSt;
            }

            List <ThetaStatement> thSt = map1.GetThetaStatements(map2, ref this.map, node.Location.FileName, node.Condition.Location.Line);

            if (thSt.Count != 0)
            {
                node.BeforeCondition = thSt;
            }

            mvSt = map1.GetMoveStatements(map2, this.map, node.Location.FileName, node.Condition.Location.Line);
            if (mvSt.Count != 0)
            {
                node.AfterCondition = mvSt;
            }

            SSAInfo info = new SSAInfo(null, null, map1, this.map);

            node.Condition.Accept(new VisitorSSA2(), info);

            info = new SSAInfo(this.map, null, map1, this.map);
            node.Statements.Accept(new VisitorSSA2(), info);

            return(null);
        }
예제 #5
0
        /// <summary>
        /// Clones the current SSAMap
        /// </summary>
        /// <returns>Returns the clone.</returns>
        public SSAMap Clone()
        {
            SSAMap aux = new SSAMap();

            for (int i = 0; i < this.map.Count; i++)
            {
                aux.SetScope();

                Dictionary <string, SSAElement> .KeyCollection keys = this.map[i].Keys;

                foreach (string key in keys)
                {
                    aux.map[i].Add(key, this.map[i][key].Clone());
                }
            }

            return(aux);
        }
예제 #6
0
        /// <summary>
        /// Compares the current map with both maps to create a list of
        /// ThetaStatement in base of their information.
        /// </summary>
        /// <param name="mapX">SSMap to compare.</param>
        /// <param name="condMap">Condition SSMap to use if this.map and mapX have the same value.</param>
        /// <param name="filename">File name.</param>
        /// <param name="line">Line to assign.</param>
        /// <returns>Returns the list of ThetaStatement.</returns>
        public List <ThetaStatement> GetThetaStatements(ref SSAMap mapX, SSAMap condMap, string filename, int line)
        {
            List <ThetaStatement> stat = new List <ThetaStatement>();

            if ((this.map.Count == mapX.ScopesCount) && ((this.map.Count == condMap.ScopesCount)))
            {
                for (int i = 0; i < this.map.Count; i++)
                {
                    if ((this.map[i].Count == mapX[i].Count) && (this.map[i].Count == condMap[i].Count))
                    {
                        Dictionary <string, SSAElement> .KeyCollection keys = this.map[i].Keys;
                        foreach (string key in keys)
                        {
                            List <SingleIdentifierExpression> list = new List <SingleIdentifierExpression>();
                            if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
                            {
                                list.Add(createNewExpression(this, key, i, filename, line));
                                list.Add(createNewExpression(mapX, key, i, filename, line));
                                SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
                                id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
                                stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
                                mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
                            }
                            else // this.map == mapX
                            {
                                if (condMap[i][key].IndexSSA != mapX[i][key].IndexSSA)
                                {
                                    list.Add(createNewExpression(condMap, key, i, filename, line));
                                    list.Add(createNewExpression(mapX, key, i, filename, line));
                                    SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
                                    id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
                                    stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
                                    mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
                                }
                            }
                        }
                    }
                }
            }

            return(stat);
        }
예제 #7
0
        /// <summary>
        /// Compares the current map with the argument map to create a list of
        /// MoveStatement in base of their information.
        /// </summary>
        /// <param name="maps">List of SSAMap.</param>
        /// <param name="filename">File name.</param>
        /// <param name="line">Line to assign.</param>
        /// <returns>Returns the list of MoveStatement.</returns>
        public List <MoveStatement> GetMoveStatementsForSwitch(List <SSAMap> maps, string filename, int line)
        {
            List <MoveStatement> stat = new List <MoveStatement>();
            SSAMap finalMap           = maps[maps.Count - 1];

            if (this.map.Count == finalMap.ScopesCount)
            {
                for (int i = 0; i < this.map.Count; i++)
                {
                    if (this.map[i].Count == finalMap[i].Count)
                    {
                        Dictionary <string, SSAElement> .KeyCollection keys = this.map[i].Keys;
                        foreach (string key in keys)
                        {
                            if (this.map[i][key].IndexSSA != finalMap[i][key].IndexSSA)
                            {
                                for (int j = 0; j < maps.Count; j++)
                                {
                                    // No updates. I need to use the condition value
                                    if (((j == 0) && (this.map[i][key].IndexSSA == maps[j][i][key].IndexSSA)) ||
                                        ((j != 0) && (maps[j - 1][i][key].IndexSSA == maps[j][i][key].IndexSSA)))
                                    {
                                        SingleIdentifierExpression right = this.createNewExpression(this, key, i, filename, line);
                                        SingleIdentifierExpression left  = new SingleIdentifierExpression(key, new Location(filename, 0, 0));
                                        left.IndexOfSSA = finalMap[i][key].IndexSSA + 1;
                                        stat.Add(new MoveStatement(left, right, filename, line));
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(stat);
        }
예제 #8
0
 /// <summary>
 /// Constructor of SSAInfo.
 /// </summary>
 public SSAInfo(SSAMap firstOpToMove, SSAMap secondOpToMove, SSAMap firstOpToUpdate, SSAMap secondOpToUpdate)
 {
     this.firstOperandToMove      = firstOpToMove;
     this.secondOperandToMove     = secondOpToMove;
     this.firstOperandToUpdateId  = firstOpToUpdate;
     this.secondOperandToUpdateId = secondOpToUpdate;
 }
예제 #9
0
        public override Object Visit(SwitchStatement node, Object obj)
        {
            Object        aux             = null;
            List <SSAMap> mapsSwitch      = new List <SSAMap>();
            int           indexDefaultMap = -1;

            if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
            {
                node.Condition = (SingleIdentifierExpression)aux;
            }
            SSAMap map0 = this.map.Clone();

            IList <SingleIdentifierExpression> previousCaseReferences = this.referencesUsedInCaseBody;

            for (int i = 0; i < node.SwitchBlockCount; i++)
            {
                // * Updates the current if statatement body being analyzed
                IList <SingleIdentifierExpression> caseReferences = new List <SingleIdentifierExpression>();
                node.ReferencesUsedCases[node.GetSwitchSectionElement(i).SwitchBlock] = caseReferences;
                IList <SingleIdentifierExpression> previousReferencesUsedInCaseBody = this.referencesUsedInCaseBody;
                this.referencesUsedInCaseBody = caseReferences;

                this.map.SetScope();
                node.GetSwitchSectionElement(i).Accept(this, false);
                if (node.GetSwitchSectionElement(i).IsDefaultCase())
                {
                    indexDefaultMap = i;
                }
                this.addLocalVariable(this.map.ResetScope(), node.GetSwitchSectionElement(i).SwitchBlock);
                mapsSwitch.Add(this.map.Clone());
            }
            this.referencesUsedInCaseBody = previousCaseReferences;

            // lastMap => this.map

            List <MoveStatement> mvSt;

            if (indexDefaultMap == -1)
            {
                mvSt = map0.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
            }
            else
            {
                mvSt = map0.GetMoveStatementsForSwitch(mapsSwitch, node.Location.FileName, node.Location.Line);
            }
            if (mvSt.Count != 0)
            {
                node.AfterCondition = mvSt;
            }


            for (int i = 0; i < node.SwitchBlockCount; i++)
            {
                SSAInfo info;
                if (i > 0)
                {
                    info = new SSAInfo(mapsSwitch[i], this.map, mapsSwitch[i - 1], map0);
                }
                else
                {
                    info = new SSAInfo(mapsSwitch[i], this.map, null, null);
                }
                node.GetSwitchSectionElement(i).Accept(new VisitorSSA2(), info);
            }

            List <ThetaStatement> thSt;

            if (indexDefaultMap != -1)
            {
                thSt = map0.GetThetaStatements(mapsSwitch, ref this.map, true, node.Location.FileName, node.GetSwitchSectionElement(node.SwitchBlockCount - 1).SwitchBlock.Location.Line);
            }
            else
            {
                thSt = map0.GetThetaStatements(mapsSwitch, ref this.map, false, node.Location.FileName, node.GetSwitchSectionElement(node.SwitchBlockCount - 1).SwitchBlock.Location.Line);
            }

            if (thSt.Count != 0)
            {
                node.ThetaStatements = thSt;
            }

            return(null);
        }
예제 #10
0
        public override Object Visit(IfElseStatement node, Object obj)
        {
            Object aux = null;

            if ((aux = node.Condition.Accept(this, false)) is SingleIdentifierExpression)
            {
                node.Condition = (SingleIdentifierExpression)aux;
            }
            SSAMap map1 = this.map.Clone();

            this.map.SetScope();

            // * Updates the current if statatement body being analyzed
            IList <SingleIdentifierExpression> previousReferencesUsedInIfElseBody = this.referencesUsedInIfElseBody;

            this.referencesUsedInIfElseBody = node.ReferencesUsedInTrueBranch;
            node.TrueBranch.Accept(this, obj);

            this.addLocalVariable(this.map.ResetScope(), node.TrueBranch);
            SSAMap map2 = this.map.Clone();

            this.map.SetScope();
            this.referencesUsedInIfElseBody = node.ReferencesUsedInFalseBranch;
            node.FalseBranch.Accept(this, obj);
            // * Updates the previous if statatement being analyzed
            this.referencesUsedInIfElseBody = previousReferencesUsedInIfElseBody;

            this.addLocalVariable(this.map.ResetScope(), node.FalseBranch);
            // map3 = this.map

            List <MoveStatement> mvSt = map1.GetMoveStatementsForIf(map2, this.map, node.Location.FileName, node.Condition.Location.Line);

            if (mvSt.Count != 0)
            {
                node.AfterCondition = mvSt;
            }

            SSAInfo info = new SSAInfo(map2, this.map, null, null);

            node.TrueBranch.Accept(new VisitorSSA2(), info);

            info = new SSAInfo(this.map, this.map, map2, map1);
            node.FalseBranch.Accept(new VisitorSSA2(), info);

            List <ThetaStatement> thSt;

            if (node.HaveElseBlock())
            {
                thSt = map2.GetThetaStatements(ref this.map, map1, node.Location.FileName, node.FalseBranch.Location.Line);
            }
            else
            {
                thSt = map1.GetThetaStatements(ref this.map, node.Location.FileName, node.FalseBranch.Location.Line);
            }

            if (thSt.Count != 0)
            {
                node.ThetaStatements = thSt;
            }

            return(null);
        }
예제 #11
0
 /// <summary>
 /// Constructor of VisitorSSA
 /// </summary>
 public VisitorSSA()
 {
     this.map        = new SSAMap();
     this.fieldsInfo = new Dictionary <string, FieldDeclaration>();
 }