The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
SSAMap.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: SSAMap.cs //
6 // Author: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Description: //
8 // Implementation of a map to use in static single assignment algorithm. //
9 // -------------------------------------------------------------------------- //
10 // Create date: 07-04-2007 //
11 // Modification date: 17-05-2007 //
13 
14 using System;
15 using System.Collections.Generic;
16 using System.Text;
17 
18 using AST;
19 using ErrorManagement;
20 using TypeSystem;
21 
22 namespace Semantic.SSAAlgorithm
23 {
27  class SSAMap
28  {
29  #region Fields
30 
34  private List<Dictionary<string, SSAElement>> map;
35 
36  #endregion
37 
38  #region Properties
39 
43  public int ScopesCount
44  {
45  get { return this.map.Count; }
46  }
47 
53  private Dictionary<string, SSAElement> this[int i]
54  {
55  get
56  {
57  if ((i >= 0) && (i < this.map.Count))
58  return this.map[i];
59  return null;
60  }
61  }
62 
63  #endregion
64 
65  #region Constructors
66 
70  public SSAMap()
71  {
72  this.map = new List<Dictionary<string, SSAElement>>();
73  }
74 
75  #endregion
76 
77  #region SetScope()
78 
82  public void SetScope()
83  {
84  this.map.Add(new Dictionary<string, SSAElement>());
85  }
86 
87  #endregion
88 
89  #region ResetScope()
90 
94  public Dictionary<string, SSAElement> ResetScope()
95  {
96  Dictionary<string, SSAElement> aux = this.map[this.map.Count - 1];
97  this.map.RemoveAt(this.map.Count - 1);
98  return aux;
99  }
100 
101  #endregion
102 
103  #region AddNewVariable()
104 
113  public void AddNewVariable(string key, string type, Location loc)
114  {
115  if (!this.map[this.map.Count - 1].ContainsKey(key))
116  this.map[this.map.Count - 1].Add(key, new SSAElement(type, loc));
117  }
118 
119  #endregion
120 
121  #region Search()
122 
128  public int Search(string id)
129  {
130  int scope = this.map.Count - 1;
131 
132  while (scope >= 0)
133  {
134  if (this.map[scope].ContainsKey(id))
135  return this.map[scope][id].IndexSSA;
136  else
137  scope--;
138  }
139  return -1;
140  }
141 
142  #endregion
143 
144  #region Increment()
145 
151  public bool Increment(string id)
152  {
153  int scope = this.map.Count - 1;
154 
155  while (scope >= 0)
156  {
157  if (this.map[scope].ContainsKey(id))
158  {
159  this.map[scope][id].UpdateIndexSSA(this.map[scope][id].IndexSSA + 1);
160  return true;
161  }
162  else
163  scope--;
164  }
165 
166  return false;
167  }
168 
169  #endregion
170 
171  #region Clone()
172 
177  public SSAMap Clone()
178  {
179  SSAMap aux = new SSAMap();
180 
181  for (int i = 0; i < this.map.Count; i++)
182  {
183  aux.SetScope();
184 
185  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
186 
187  foreach (string key in keys)
188  {
189  aux.map[i].Add(key, this.map[i][key].Clone());
190  }
191  }
192 
193  return aux;
194  }
195 
196  #endregion
197 
198  #region ToString()
199 
204  public override string ToString()
205  {
206  StringBuilder aux = new StringBuilder();
207  for (int i = 0; i < this.map.Count; i++)
208  {
209  aux.AppendFormat("Scope {0}", i);
210  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
211  foreach (string key in keys)
212  aux.AppendFormat("\t{0}{1}", key, this.map[i][key].IndexSSA);
213  }
214  return aux.ToString();
215  }
216 
217  #endregion
218 
219  #region createNewExpression
220 
230  private SingleIdentifierExpression createNewExpression(SSAMap map, string key, int position, string filename, int line)
231  {
233 
234  //if ((position == 0) && (map[position][tmpName].IndexSSA == 0)) // use a real field
235  //{
236  // id = new SingleIdentifierExpression(tmpName.Substring(6, tmpName.Length - 6), filename, line, 0);
237  // id.IndexOfSSA = -1;
238  //}
239  //else
240  //{
241  id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
242  id.IndexOfSSA = map[position][key].IndexSSA;
243  //}
244  return id;
245  }
246 
247  #endregion
248 
249  #region GetMoveStatements
250 
251  #region List<MoveStatement> GetMoveStatementsForIf(SSAMap ifBlockMap, SSAMap elseBlockMap, string filename, int line)
252 
262  public List<MoveStatement> GetMoveStatementsForIf(SSAMap ifBlockMap, SSAMap elseBlockMap, string filename, int line)
263  {
264  List<MoveStatement> stat = new List<MoveStatement>();
265 
266  if ((this.map.Count == ifBlockMap.ScopesCount) && (this.map.Count == elseBlockMap.ScopesCount))
267  {
268  for (int i = 0; i < this.map.Count; i++)
269  {
270  if ((this.map[i].Count == ifBlockMap[i].Count) && (this.map[i].Count == elseBlockMap[i].Count))
271  {
272  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
273  foreach (string key in keys)
274  {
275  if (((this.map[i][key].IndexSSA == ifBlockMap[i][key].IndexSSA) && (this.map[i][key].IndexSSA != elseBlockMap[i][key].IndexSSA)) ||
276  ((ifBlockMap[i][key].IndexSSA == elseBlockMap[i][key].IndexSSA) && (this.map[i][key].IndexSSA != elseBlockMap[i][key].IndexSSA)))
277  {
278  SingleIdentifierExpression right = this.createNewExpression(this, key, i, filename, line);
279  SingleIdentifierExpression left = new SingleIdentifierExpression(key, new Location(filename, 0, 0));
280  left.IndexOfSSA = elseBlockMap[i][key].IndexSSA + 1;
281  stat.Add(new MoveStatement(left, right, filename, line));
282  }
283  }
284  }
285  }
286  }
287 
288  return stat;
289  }
290 
291  #endregion
292 
293  #region List<MoveStatement> GetMoveStatementsForSwitch(List<SSAMap> maps, string filename, int line)
294 
303  public List<MoveStatement> GetMoveStatementsForSwitch(List<SSAMap> maps, string filename, int line)
304  {
305  List<MoveStatement> stat = new List<MoveStatement>();
306  SSAMap finalMap = maps[maps.Count - 1];
307 
308  if (this.map.Count == finalMap.ScopesCount)
309  {
310  for (int i = 0; i < this.map.Count; i++)
311  {
312  if (this.map[i].Count == finalMap[i].Count)
313  {
314  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
315  foreach (string key in keys)
316  {
317  if (this.map[i][key].IndexSSA != finalMap[i][key].IndexSSA)
318  {
319  for (int j = 0; j < maps.Count; j++)
320  {
321  // No updates. I need to use the condition value
322  if (((j == 0) && (this.map[i][key].IndexSSA == maps[j][i][key].IndexSSA)) ||
323  ((j != 0) && (maps[j - 1][i][key].IndexSSA == maps[j][i][key].IndexSSA)))
324  {
325  SingleIdentifierExpression right = this.createNewExpression(this, key, i, filename, line);
326  SingleIdentifierExpression left = new SingleIdentifierExpression(key, new Location(filename, 0, 0));
327  left.IndexOfSSA = finalMap[i][key].IndexSSA + 1;
328  stat.Add(new MoveStatement(left, right, filename, line));
329  break;
330  }
331  }
332  }
333  }
334  }
335  }
336  }
337  return stat;
338  }
339 
340  #endregion
341 
342  #region List<MoveStatement> GetMoveStatements(SSAMap mapX, string filename, int line)
343 
352  public List<MoveStatement> GetMoveStatements(SSAMap mapX, string filename, int line)
353  {
354  List<MoveStatement> stat = new List<MoveStatement>();
355 
356  if (this.map.Count == mapX.ScopesCount)
357  {
358  for (int i = 0; i < this.map.Count; i++)
359  {
360  if (this.map[i].Count == mapX[i].Count)
361  {
362  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
363  foreach (string key in keys)
364  {
365  if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
366  {
367  SingleIdentifierExpression right = createNewExpression(this, key, i, filename, line);
368  SingleIdentifierExpression left = new SingleIdentifierExpression(key, new Location(filename, line, 0));
369  left.IndexOfSSA = mapX[i][key].IndexSSA + 1;
370  stat.Add(new MoveStatement(left, right, filename, line));
371  }
372  }
373  }
374  }
375  }
376 
377  return stat;
378  }
379 
380  #endregion
381 
382  #region List<MoveStatement> GetMoveStatements(SSAMap mapX, SSAMap mapY, string filename, int line)
383 
393  public List<MoveStatement> GetMoveStatements(SSAMap mapX, SSAMap mapY, string filename, int line)
394  {
395  List<MoveStatement> stat = new List<MoveStatement>();
396 
397  if (this.map.Count == mapX.ScopesCount)
398  {
399  for (int i = 0; i < this.map.Count; i++)
400  {
401  if (this.map[i].Count == mapX[i].Count)
402  {
403  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
404  foreach (string key in keys)
405  {
406  if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
407  {
408  SingleIdentifierExpression right = this.createNewExpression(mapX, key, i, filename, line);
409  SingleIdentifierExpression left = new SingleIdentifierExpression(key, new Location( filename, 0, 0));
410  left.IndexOfSSA = mapY[i][key].IndexSSA;
411  stat.Add(new MoveStatement(left, right, filename, line));
412  }
413  }
414  }
415  }
416  }
417 
418  return stat;
419  }
420 
421  #endregion
422 
423  #endregion
424 
425  #region GetThetaStatements
426 
427  #region List<ThetaStatement> GetThetaStatements(List<SSAMap> maps, ref SSAMap map3, bool defaultFound, string filename, int line)
428 
439  public List<ThetaStatement> GetThetaStatements(List<SSAMap> maps, ref SSAMap map3, bool defaultFound, string filename, int line)
440  {
441  List<ThetaStatement> stat = new List<ThetaStatement>();
442  bool useCondition = !defaultFound;
443 
444  if (this.map.Count == map3.ScopesCount)
445  {
446  for (int i = 0; i < this.map.Count; i++)
447  {
448  if (this.map[i].Count == map3[i].Count)
449  {
450  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
451  foreach (string key in keys)
452  {
453  if (this.map[i][key].IndexSSA != map3[i][key].IndexSSA)
454  {
455  List<SingleIdentifierExpression> list = new List<SingleIdentifierExpression>();
456 
457  for (int j = 0; j < maps.Count; j++)
458  {
459  if ((this.map.Count == maps[j].ScopesCount) && (this.map[i].Count == maps[j][i].Count))
460  {
461  if (((j == 0) && (this.map[i][key].IndexSSA != maps[j][i][key].IndexSSA)) ||
462  ((j != 0) && (maps[j - 1][i][key].IndexSSA != maps[j][i][key].IndexSSA)))
463  list.Add(createNewExpression(maps[j], key, i, filename, line));
464  else
465  useCondition = true;
466  }
467  }
468 
469  if (useCondition)
470  {
471  list.Add(createNewExpression(this, key, i, filename, line));
472  useCondition = !defaultFound;
473  }
474 
475  list.Add(createNewExpression(map3, key, i, filename, line));
476  SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
477  id.IndexOfSSA = map3[i][key].IndexSSA + 1;
478  stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
479 
480  map3[i][key].UpdateIndexSSA(map3[i][key].IndexSSA + 1);
481  }
482  }
483  }
484  }
485  }
486 
487  return stat;
488  }
489 
490  #endregion
491 
492  #region List<ThetaStatement> GetThetaStatements(SSAMap map2, ref SSAMap map3, string filename, int line)
493 
503  public List<ThetaStatement> GetThetaStatements(SSAMap map2, ref SSAMap map3, string filename, int line)
504  {
505  List<ThetaStatement> stat = new List<ThetaStatement>();
506 
507  if ((this.map.Count == map3.ScopesCount) && (this.map.Count == map2.ScopesCount))
508  {
509  for (int i = 0; i < this.map.Count; i++)
510  {
511  if ((this.map[i].Count == map3[i].Count) && (this.map[i].Count == map2[i].Count))
512  {
513  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
514  foreach (string key in keys)
515  {
516  if (this.map[i][key].IndexSSA != map3[i][key].IndexSSA)
517  {
518  List<SingleIdentifierExpression> list = new List<SingleIdentifierExpression>();
519  list.Add(createNewExpression(this, key, i, filename, line));
520 
521  if (this.map[i][key].IndexSSA != map2[i][key].IndexSSA)
522  list.Add(createNewExpression(map2, key, i, filename, line));
523 
524  list.Add(createNewExpression(map3, key, i, filename, line));
525  SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
526  id.IndexOfSSA = map3[i][key].IndexSSA + 1;
527  stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
528 
529  map3[i][key].UpdateIndexSSA(map3[i][key].IndexSSA + 1);
530  }
531  }
532  }
533  }
534  }
535 
536  return stat;
537  }
538 
539  #endregion
540 
541  #region List<ThetaStatement> GetThetaStatements(ref SSAMap map3, string filename, int line)
542 
551  public List<ThetaStatement> GetThetaStatements(ref SSAMap mapX, string filename, int line)
552  {
553  List<ThetaStatement> stat = new List<ThetaStatement>();
554 
555  if (this.map.Count == mapX.ScopesCount)
556  {
557  for (int i = 0; i < this.map.Count; i++)
558  {
559  if (this.map[i].Count == mapX[i].Count)
560  {
561  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
562  foreach (string key in keys)
563  {
564  if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
565  {
566  List<SingleIdentifierExpression> list = new List<SingleIdentifierExpression>();
567 
568  list.Add(createNewExpression(this, key, i, filename, line));
569  list.Add(createNewExpression(mapX, key, i, filename, line));
570  SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
571  id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
572  stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
573 
574  mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
575  }
576  }
577  }
578  }
579  }
580 
581  return stat;
582  }
583 
584  #endregion
585 
586  #region List<ThetaStatement> GetThetaStatements(ref SSAMap map3, SSAMap condMap, string filename, int line)
587 
597  public List<ThetaStatement> GetThetaStatements(ref SSAMap mapX, SSAMap condMap, string filename, int line)
598  {
599  List<ThetaStatement> stat = new List<ThetaStatement>();
600 
601  if ((this.map.Count == mapX.ScopesCount) && ((this.map.Count == condMap.ScopesCount)))
602  {
603  for (int i = 0; i < this.map.Count; i++)
604  {
605  if ((this.map[i].Count == mapX[i].Count) && (this.map[i].Count == condMap[i].Count))
606  {
607  Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
608  foreach (string key in keys)
609  {
610  List<SingleIdentifierExpression> list = new List<SingleIdentifierExpression>();
611  if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
612  {
613  list.Add(createNewExpression(this, key, i, filename, line));
614  list.Add(createNewExpression(mapX, key, i, filename, line));
615  SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
616  id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
617  stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
618  mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
619  }
620  else // this.map == mapX
621  {
622  if (condMap[i][key].IndexSSA != mapX[i][key].IndexSSA)
623  {
624  list.Add(createNewExpression(condMap, key, i, filename, line));
625  list.Add(createNewExpression(mapX, key, i, filename, line));
626  SingleIdentifierExpression id = new SingleIdentifierExpression(key, new Location(filename, line, 0));
627  id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
628  stat.Add(new ThetaStatement(id, list, new Location(filename, line, 0)));
629  mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
630  }
631  }
632  }
633  }
634  }
635  }
636 
637  return stat;
638  }
639 
640  #endregion
641  #endregion
642  }
643 }
void AddNewVariable(string key, string type, Location loc)
Adds a new identifier.
Definition: SSAMap.cs:113
void SetScope()
Adds a new scope
Definition: SSAMap.cs:82
SSAMap Clone()
Clones the current SSAMap
Definition: SSAMap.cs:177
List< ThetaStatement > GetThetaStatements(SSAMap map2, ref SSAMap map3, string filename, int line)
Compares the current map with both maps to create a list of ThetaStatement in base of their informati...
Definition: SSAMap.cs:503
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
List< MoveStatement > GetMoveStatements(SSAMap mapX, string filename, int line)
Compares the current map with the argument map to create a list of MoveStatement in base of their inf...
Definition: SSAMap.cs:352
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
List< ThetaStatement > GetThetaStatements(List< SSAMap > maps, ref SSAMap map3, bool defaultFound, string filename, int line)
Compares the current map with both maps to create a list of ThetaStatement in base of their informati...
Definition: SSAMap.cs:439
This class stores the information to use in SSA map and allows to create a new declarations.
Definition: SSAElement.cs:29
Encapsulates a identifier expression of our programming language.
List< ThetaStatement > GetThetaStatements(ref SSAMap mapX, string filename, int line)
Compares the current map with both maps to create a list of ThetaStatement in base of their informati...
Definition: SSAMap.cs:551
Encapsulates a Theta function to use in SSA algorithm.
Dictionary< string, SSAElement > ResetScope()
Removes the last scope
Definition: SSAMap.cs:94
bool Increment(string id)
Increments the value of the specified identifier.
Definition: SSAMap.cs:151
List< MoveStatement > GetMoveStatementsForSwitch(List< SSAMap > maps, string filename, int line)
Compares the current map with the argument map to create a list of MoveStatement in base of their inf...
Definition: SSAMap.cs:303
override string ToString()
Dumps the current state of the SSAMap.
Definition: SSAMap.cs:204
SSAMap()
Constructor of SSAMap.
Definition: SSAMap.cs:70
int ScopesCount
Gets the number of scopes in the SSAMap.
Definition: SSAMap.cs:44
List< ThetaStatement > GetThetaStatements(ref SSAMap mapX, SSAMap condMap, string filename, int line)
Compares the current map with both maps to create a list of ThetaStatement in base of their informati...
Definition: SSAMap.cs:597
Encapsulates a Move instruction to use in SSA algorithm
Implementation of a map to use in static single assignment algorithm.
Definition: SSAMap.cs:27
List< MoveStatement > GetMoveStatementsForIf(SSAMap ifBlockMap, SSAMap elseBlockMap, string filename, int line)
Compares the current map with the argument map to create a list of MoveStatement in base of their inf...
Definition: SSAMap.cs:262
List< MoveStatement > GetMoveStatements(SSAMap mapX, SSAMap mapY, string filename, int line)
Compares the current map with the argument map to create a list of MoveStatement in base of their inf...
Definition: SSAMap.cs:393
int Search(string id)
Searches the variable specified and returns its associated number.
Definition: SSAMap.cs:128