The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
VisitorSSA2.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: VisitorSSA2.cs //
6 // Author: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Description: //
8 // This class makes the second part of static single assignment algorithm //
9 // in which every variable is assigned exactly once. //
10 // Inheritance: VisitorAdapter. //
11 // Implements Visitor pattern [Concrete Visitor]. //
12 // -------------------------------------------------------------------------- //
13 // Create date: 06-04-2007 //
14 // Modification date: 03-05-2007 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Text;
20 
21 using AST;
22 using Tools;
23 using ErrorManagement;
24 
25 namespace Semantic.SSAAlgorithm
26 {
36  {
37  #region Constructor
38 
42  public VisitorSSA2()
43  {
44  }
45 
46  #endregion
47 
48  // Declarations
49 
50  #region Visit(Definition node, Object obj)
51 
52  public override Object Visit(Definition node, Object obj)
53  {
54  node.Init.Accept(this, obj);
55  return null;
56  }
57 
58  #endregion
59 
60  #region Visit(ConstantDefinition node, Object obj)
61 
62  public override Object Visit(ConstantDefinition node, Object obj)
63  {
64  node.Init.Accept(this, obj);
65  return null;
66  }
67 
68  #endregion
69 
70  #region Visit(FieldDefinition node, Object obj)
71 
72  public override Object Visit(FieldDefinition node, Object obj)
73  {
74  node.Init.Accept(this, obj);
75  return null;
76  }
77 
78  #endregion
79 
80  #region Visit(ConstantFieldDefinition node, Object obj)
81 
82  public override Object Visit(ConstantFieldDefinition node, Object obj)
83  {
84  node.Init.Accept(this, obj);
85  return null;
86  }
87 
88  #endregion
89 
90  // Expressions
91 
92  #region createMoveStatement
93 
94  private MoveStatement createMoveStatement(string id, SSAMap mapX, SSAMap mapY, string filename, int line)
95  {
96  SingleIdentifierExpression left = new SingleIdentifierExpression(id, new Location(filename, line, 0));
97  left.IndexOfSSA = mapY.Search(id) + 1;
98  SingleIdentifierExpression right = new SingleIdentifierExpression(id, new Location(filename, line, 0));
99  right.IndexOfSSA = mapX.Search(id);
100  return new MoveStatement(left, right, filename,line);
101  }
102 
103  private MoveStatement createMoveStatement(string id, SSAMap mapX, string filename, int line)
104  {
105  SingleIdentifierExpression left = new SingleIdentifierExpression(id, new Location(filename, line, 0));
106  left.IndexOfSSA = mapX.Search(id);
107  SingleIdentifierExpression right = new SingleIdentifierExpression(id, new Location(filename, line, 0));
108  right.IndexOfSSA = mapX.Search(id) - 1;
109  return new MoveStatement(left, right, filename, line);
110  }
111 
112  #endregion
113 
114  #region Visit(AssignmentExpression node, Object obj)
115 
116  public override Object Visit(AssignmentExpression node, Object obj)
117  {
118  SSAMap mapX;
119  SSAMap mapY;
120 
121  if (obj is SSAInfo)
122  {
124  {
125  if ((mapX = ((SSAInfo)obj).FirstOperandToMove) != null)
126  {
127  if (mapX.Search(((SingleIdentifierExpression)node.FirstOperand).Identifier) != -1) // <-- Modified
128  {
129  if ((mapY = ((SSAInfo)obj).SecondOperandToMove) != null)
130  {
131  if (((SingleIdentifierExpression)node.FirstOperand).IndexOfSSA == mapX.Search(((SingleIdentifierExpression)node.FirstOperand).Identifier))
132  node.MoveStat = createMoveStatement(((SingleIdentifierExpression)node.FirstOperand).Identifier, mapX, mapY, node.Location.FileName, node.Location.Line);
133  }
134  else
135  {
136  if (((SingleIdentifierExpression)node.FirstOperand).IndexOfSSA == mapX.Search(((SingleIdentifierExpression)node.FirstOperand).Identifier) - 1)
137  node.MoveStat = createMoveStatement(((SingleIdentifierExpression)node.FirstOperand).Identifier, mapX, node.Location.FileName, node.Location.Line);
138  }
139  }
140  }
141  }
142  else
143  node.FirstOperand.Accept(this, obj);
144  node.SecondOperand.Accept(this, obj);
145  }
146  if (node.MoveStat != null)
147  node.MoveStat.Accept(this, obj);
148  return null;
149  }
150 
151  #endregion
152 
153  #region Visit(MoveStatement node, Object obj)
154 
155  public override Object Visit(MoveStatement node, Object obj)
156  {
157  SSAMap mapX;
158  SSAMap mapY;
159 
160  if (obj is SSAInfo)
161  {
162  if ((mapX = ((SSAInfo)obj).FirstOperandToMove) != null)
163  {
164  if (mapX.Search(node.LeftExp.Identifier) != -1) // <-- Modified
165  {
166  if ((mapY = ((SSAInfo)obj).SecondOperandToMove) != null)
167  {
168  if (node.LeftExp.IndexOfSSA == mapX.Search(node.LeftExp.Identifier))
169  node.MoveStat = createMoveStatement(node.LeftExp.Identifier, mapX, mapY, node.Location.FileName, node.Location.Line);
170  }
171  else
172  {
173  if (node.LeftExp.IndexOfSSA == mapX.Search(node.LeftExp.Identifier) - 1)
174  node.MoveStat = createMoveStatement(node.LeftExp.Identifier, mapX, node.Location.FileName, node.Location.Line);
175  }
176  }
177  }
178  }
179  node.RightExp.Accept(this, obj);
180 
181  if (node.MoveStat != null)
182  node.MoveStat.Accept(this, obj);
183 
184  return null;
185  }
186 
187  #endregion
188 
189  #region Visit(SingleIdentifierExpression node, Object obj)
190 
191  public override Object Visit(SingleIdentifierExpression node, Object obj)
192  {
193  SSAMap mapX;
194  SSAMap mapY;
195 
196  if (obj is SSAInfo)
197  {
198  if (((mapX = ((SSAInfo)obj).FirstOperandToUpdateId) != null) && ((mapY = ((SSAInfo)obj).SecondOperandToUpdateId) != null))
199  {
200  if (!(node.LeftExpression))
201  {
202  int iValueX;
203  int iValueY;
204 
205  if (((iValueX = mapX.Search(node.Identifier)) != -1) && ((iValueY = mapY.Search(node.Identifier)) != -1))
206  {
207  if (node.IndexOfSSA == iValueX)
208  {
209  if (iValueX != iValueY)
210  {
211  node.IndexOfSSA = iValueY;
212  }
213  }
214  }
215  }
216  }
217  }
218  return null;
219  }
220 
221  #endregion
222  }
223 }
This class stores the information to use in SSA algorithm
Definition: SSAInfo.cs:27
Encapsulates a definition of a concrete field.
This class encapsulates a location in a specific file. Implements an Inmutable pattern. So it can be used in any context, that is his internal fields never change.
Definition: Location.cs:24
Location Location
Definition: AstNode.cs:45
MoveStatement MoveStat
Gets or sets a move statement associated to the assignment expression.
Encapsulates a constant field definition.
Encapsulates a definition.
Definition: Definition.cs:36
Encapsulates a identifier expression of our programming language.
override Object Visit(Definition node, Object obj)
Definition: VisitorSSA2.cs:52
Abstract class to define different visits over the abstract syntax tree. It makes a deep walker...
int Line
Gets line where the item is located
Definition: Location.cs:58
This class makes the second part of static single assignment algorithm in which every variable is ass...
Definition: VisitorSSA2.cs:35
bool LeftExpression
Gets or sets true if the expression is allocated in the left part of an assignment, false otherwise.
Definition: Expression.cs:89
Encapsulates assignment binary expressions.
override Object Visit(FieldDefinition node, Object obj)
Definition: VisitorSSA2.cs:72
MoveStatement MoveStat
Gets or sets a move statement associated to the current move statement.
SingleIdentifierExpression LeftExp
Gets the left expression
int IndexOfSSA
Gets or sets the index associated with SSA algorithm
VisitorSSA2()
Constructor of VisitorSSA2
Definition: VisitorSSA2.cs:42
override Object Visit(ConstantFieldDefinition node, Object obj)
Definition: VisitorSSA2.cs:82
Encapsulates a constant definition.
override Object Visit(MoveStatement node, Object obj)
Definition: VisitorSSA2.cs:155
Encapsulates a Move instruction to use in SSA algorithm
override Object Visit(AssignmentExpression node, Object obj)
Definition: VisitorSSA2.cs:116
override Object Visit(SingleIdentifierExpression node, Object obj)
Definition: VisitorSSA2.cs:191
Expression FirstOperand
Gets the first operand of the binary expression
string Identifier
Gets the name name.
Implementation of a map to use in static single assignment algorithm.
Definition: SSAMap.cs:27
string FileName
Gets de name of the file
Definition: Location.cs:52
override Object Visit(ConstantDefinition node, Object obj)
Definition: VisitorSSA2.cs:62
int Search(string id)
Searches the variable specified and returns its associated number.
Definition: SSAMap.cs:128