15 using System.Collections.Generic;
19 using ErrorManagement;
22 namespace Semantic.SSAAlgorithm
34 private List<Dictionary<string, SSAElement>> map;
43 public int ScopesCount
45 get {
return this.map.Count; }
53 private Dictionary<string, SSAElement>
this[
int i]
57 if ((i >= 0) && (i < this.map.Count))
72 this.map =
new List<Dictionary<string, SSAElement>>();
84 this.map.Add(
new Dictionary<string, SSAElement>());
96 Dictionary<string, SSAElement> aux = this.map[this.map.Count - 1];
97 this.map.RemoveAt(this.map.Count - 1);
103 #region AddNewVariable()
115 if (!this.map[this.map.Count - 1].ContainsKey(key))
116 this.map[this.map.Count - 1].Add(key,
new SSAElement(type, loc));
130 int scope = this.map.Count - 1;
134 if (this.map[scope].ContainsKey(
id))
135 return this.map[scope][id].IndexSSA;
153 int scope = this.map.Count - 1;
157 if (this.map[scope].ContainsKey(
id))
159 this.map[scope][id].UpdateIndexSSA(this.map[scope][id].IndexSSA + 1);
181 for (
int i = 0; i < this.map.Count; i++)
185 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
187 foreach (
string key
in keys)
189 aux.map[i].Add(key, this.map[i][key].Clone());
207 for (
int i = 0; i < this.map.Count; i++)
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);
214 return aux.ToString();
219 #region createNewExpression
242 id.IndexOfSSA = map[position][key].IndexSSA;
249 #region GetMoveStatements
251 #region List<MoveStatement> GetMoveStatementsForIf(SSAMap ifBlockMap, SSAMap elseBlockMap, string filename, int line)
264 List<MoveStatement> stat =
new List<MoveStatement>();
266 if ((this.map.Count == ifBlockMap.
ScopesCount) && (this.map.Count == elseBlockMap.ScopesCount))
268 for (
int i = 0; i < this.map.Count; i++)
270 if ((this.map[i].Count == ifBlockMap[i].Count) && (this.map[i].Count == elseBlockMap[i].Count))
272 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
273 foreach (
string key
in keys)
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)))
280 left.IndexOfSSA = elseBlockMap[i][key].IndexSSA + 1;
293 #region List<MoveStatement> GetMoveStatementsForSwitch(List<SSAMap> maps, string filename, int line)
305 List<MoveStatement> stat =
new List<MoveStatement>();
306 SSAMap finalMap = maps[maps.Count - 1];
310 for (
int i = 0; i < this.map.Count; i++)
312 if (this.map[i].Count == finalMap[i].Count)
314 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
315 foreach (
string key
in keys)
317 if (this.map[i][key].IndexSSA != finalMap[i][key].IndexSSA)
319 for (
int j = 0; j < maps.Count; j++)
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)))
327 left.IndexOfSSA = finalMap[i][key].IndexSSA + 1;
342 #region List<MoveStatement> GetMoveStatements(SSAMap mapX, string filename, int line)
354 List<MoveStatement> stat =
new List<MoveStatement>();
358 for (
int i = 0; i < this.map.Count; i++)
360 if (this.map[i].Count == mapX[i].Count)
362 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
363 foreach (
string key
in keys)
365 if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
369 left.IndexOfSSA = mapX[i][key].IndexSSA + 1;
382 #region List<MoveStatement> GetMoveStatements(SSAMap mapX, SSAMap mapY, string filename, int line)
395 List<MoveStatement> stat =
new List<MoveStatement>();
399 for (
int i = 0; i < this.map.Count; i++)
401 if (this.map[i].Count == mapX[i].Count)
403 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
404 foreach (
string key
in keys)
406 if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
410 left.IndexOfSSA = mapY[i][key].IndexSSA;
425 #region GetThetaStatements
427 #region List<ThetaStatement> GetThetaStatements(List<SSAMap> maps, ref SSAMap map3, bool defaultFound, string filename, int line)
441 List<ThetaStatement> stat =
new List<ThetaStatement>();
442 bool useCondition = !defaultFound;
446 for (
int i = 0; i < this.map.Count; i++)
448 if (this.map[i].Count == map3[i].Count)
450 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
451 foreach (
string key
in keys)
453 if (this.map[i][key].IndexSSA != map3[i][key].IndexSSA)
455 List<SingleIdentifierExpression> list =
new List<SingleIdentifierExpression>();
457 for (
int j = 0; j < maps.Count; j++)
459 if ((this.map.Count == maps[j].
ScopesCount) && (this.map[i].Count == maps[j][i].Count))
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));
471 list.Add(createNewExpression(
this, key, i, filename, line));
472 useCondition = !defaultFound;
475 list.Add(createNewExpression(map3, key, i, filename, line));
477 id.IndexOfSSA = map3[i][key].IndexSSA + 1;
480 map3[i][key].UpdateIndexSSA(map3[i][key].IndexSSA + 1);
492 #region List<ThetaStatement> GetThetaStatements(SSAMap map2, ref SSAMap map3, string filename, int line)
505 List<ThetaStatement> stat =
new List<ThetaStatement>();
507 if ((this.map.Count == map3.
ScopesCount) && (this.map.Count == map2.ScopesCount))
509 for (
int i = 0; i < this.map.Count; i++)
511 if ((this.map[i].Count == map3[i].Count) && (this.map[i].Count == map2[i].Count))
513 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
514 foreach (
string key
in keys)
516 if (this.map[i][key].IndexSSA != map3[i][key].IndexSSA)
518 List<SingleIdentifierExpression> list =
new List<SingleIdentifierExpression>();
519 list.Add(createNewExpression(
this, key, i, filename, line));
521 if (this.map[i][key].IndexSSA != map2[i][key].IndexSSA)
522 list.Add(createNewExpression(map2, key, i, filename, line));
524 list.Add(createNewExpression(map3, key, i, filename, line));
526 id.IndexOfSSA = map3[i][key].IndexSSA + 1;
529 map3[i][key].UpdateIndexSSA(map3[i][key].IndexSSA + 1);
541 #region List<ThetaStatement> GetThetaStatements(ref SSAMap map3, string filename, int line)
553 List<ThetaStatement> stat =
new List<ThetaStatement>();
557 for (
int i = 0; i < this.map.Count; i++)
559 if (this.map[i].Count == mapX[i].Count)
561 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
562 foreach (
string key
in keys)
564 if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
566 List<SingleIdentifierExpression> list =
new List<SingleIdentifierExpression>();
568 list.Add(createNewExpression(
this, key, i, filename, line));
569 list.Add(createNewExpression(mapX, key, i, filename, line));
571 id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
574 mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
586 #region List<ThetaStatement> GetThetaStatements(ref SSAMap map3, SSAMap condMap, string filename, int line)
599 List<ThetaStatement> stat =
new List<ThetaStatement>();
601 if ((this.map.Count == mapX.
ScopesCount) && ((this.map.Count == condMap.ScopesCount)))
603 for (
int i = 0; i < this.map.Count; i++)
605 if ((this.map[i].Count == mapX[i].Count) && (this.map[i].Count == condMap[i].Count))
607 Dictionary<string, SSAElement>.KeyCollection keys = this.map[i].Keys;
608 foreach (
string key
in keys)
610 List<SingleIdentifierExpression> list =
new List<SingleIdentifierExpression>();
611 if (this.map[i][key].IndexSSA != mapX[i][key].IndexSSA)
613 list.Add(createNewExpression(
this, key, i, filename, line));
614 list.Add(createNewExpression(mapX, key, i, filename, line));
616 id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
618 mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
622 if (condMap[i][key].IndexSSA != mapX[i][key].IndexSSA)
624 list.Add(createNewExpression(condMap, key, i, filename, line));
625 list.Add(createNewExpression(mapX, key, i, filename, line));
627 id.IndexOfSSA = mapX[i][key].IndexSSA + 1;
629 mapX[i][key].UpdateIndexSSA(mapX[i][key].IndexSSA + 1);
void AddNewVariable(string key, string type, Location loc)
Adds a new identifier.
void SetScope()
Adds a new scope
SSAMap Clone()
Clones the current SSAMap
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...
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.
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...
System.Text.StringBuilder StringBuilder
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...
This class stores the information to use in SSA map and allows to create a new declarations.
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...
Encapsulates a Theta function to use in SSA algorithm.
Dictionary< string, SSAElement > ResetScope()
Removes the last scope
bool Increment(string id)
Increments the value of the specified identifier.
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...
override string ToString()
Dumps the current state of the SSAMap.
SSAMap()
Constructor of SSAMap.
int ScopesCount
Gets the number of scopes in the SSAMap.
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...
Encapsulates a Move instruction to use in SSA algorithm
Implementation of a map to use in static single assignment algorithm.
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...
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...
int Search(string id)
Searches the variable specified and returns its associated number.