22 using System.Collections.Generic;
27 using ErrorManagement;
29 namespace Semantic.SSAAlgorithm {
49 private Dictionary<string, FieldDeclaration> fieldsInfo;
55 IList<SingleIdentifierExpression> referencesUsedInIfElseBody;
61 IList<SingleIdentifierExpression> referencesUsedInCaseBody;
72 this.fieldsInfo =
new Dictionary<string, FieldDeclaration>();
77 #region addAssignmentStatements()
84 private void addAssignmentStatements(Dictionary<string, SSAElement> vars,
Block node) {
90 foreach (KeyValuePair<string, SSAElement> pair
in vars) {
91 if (pair.Value.IndexSSA > 0) {
96 op2.IndexOfSSA = pair.Value.IndexSSA;
104 #region addLocalVariable()
111 private void addLocalVariable(Dictionary<string, SSAElement> vars,
Statement node) {
115 statements = (Block)node;
116 foreach (KeyValuePair<string, SSAElement> pair
in vars) {
117 for (
int i = 0; i < pair.Value.IndexSSA; i++) {
118 type = TypeSystem.TypeTable.ObtainNewType(pair.Value.TypeId);
129 #region Visit(ClassDefinition node, Object obj)
136 this.fieldsInfo.Clear();
137 for (
int i = 0; i < node.MemberCount; i++) {
143 for (
int i = 0; i < node.MemberCount; i++) {
144 node.GetMemberElement(i).Accept(
this, obj);
153 #region Visit(IdDeclaration node, Object obj)
159 node.IdentifierExp.IndexOfSSA = -1;
165 #region Visit(Definition node, Object obj)
171 node.IdentifierExp.IndexOfSSA = -1;
172 node.Init.Accept(
this,
false);
178 #region Visit(ConstantDefinition node, Object obj)
184 node.IdentifierExp.IndexOfSSA = -1;
185 node.Init.Accept(
this,
false);
191 #region Visit(MethodDefinition node, Object obj)
204 for (
int i = 0; i < node.ParametersInfo.Count; i++) {
209 bool returnFound = (bool)node.
Body.
Accept(
this, obj);
210 this.addLocalVariable(this.map.ResetScope(), node.
Body);
219 this.map.ResetScope();
225 #region Visit(ConstructorDefinition node, Object obj)
238 for (
int i = 0; i < node.ParametersInfo.Count; i++) {
245 node.Initialization.Accept(
this, obj);
247 node.Body.Accept(
this, obj);
248 this.addLocalVariable(this.map.ResetScope(), node.
Body);
251 this.map.ResetScope();
259 #region Visit(AssignmentExpression node, Object obj)
263 node.SecondOperand.LeftExpression =
false;
265 node.SecondOperand = (SingleIdentifierExpression)aux;
266 node.FirstOperand.LeftExpression =
true;
268 node.FirstOperand = (SingleIdentifierExpression)aux;
271 SingleIdentifierExpression singleId=node.FirstOperand as SingleIdentifierExpression;
272 if (singleId!=null) {
273 if (this.referencesUsedInIfElseBody!=null)
274 this.referencesUsedInIfElseBody.Remove(singleId);
275 if (this.referencesUsedInCaseBody!=null)
276 this.referencesUsedInCaseBody.Remove(singleId);
283 #region Visit(SingleIdentifierExpression node, Object obj)
289 bool isMember =
true;
291 isMember = getInheritedAttributes<Boolean>(obj);
294 if (this.referencesUsedInIfElseBody != null && !this.referencesUsedInIfElseBody.Contains(node))
295 this.referencesUsedInIfElseBody.Add(node);
296 if (this.referencesUsedInCaseBody != null && !this.referencesUsedInCaseBody.Contains(node))
297 this.referencesUsedInCaseBody.Add(node);
302 this.map.Increment(node.Identifier);
305 if ((iValue = this.map.Search(node.
Identifier)) != -1) {
306 node.IndexOfSSA = iValue;
314 #region Visit(FieldAccessExpression node, Object obj)
319 node.Expression = (SingleIdentifierExpression)aux;
320 node.FieldName.Accept(
this,
true);
328 #region Visit(ArgumentExpression node, Object obj)
332 node.Argument.LeftExpression = node.LeftExpression;
334 node.Argument = (SingleIdentifierExpression)aux;
340 #region Visit(ArithmeticExpression node, Object obj)
344 node.FirstOperand.LeftExpression = node.LeftExpression;
345 node.SecondOperand.LeftExpression = node.LeftExpression;
347 node.FirstOperand = (SingleIdentifierExpression)aux;
349 node.SecondOperand = (SingleIdentifierExpression)aux;
355 #region Visit(ArrayAccessExpression node, Object obj)
360 node.FirstOperand.LeftExpression =
false;
361 node.SecondOperand.LeftExpression =
false;
363 node.FirstOperand = (SingleIdentifierExpression)aux;
365 node.SecondOperand = (SingleIdentifierExpression)aux;
366 node.FirstOperand.LeftExpression = node.LeftExpression;
372 #region Visit(BaseCallExpression node, Object obj)
375 node.Arguments.LeftExpression =
false;
376 node.Arguments.Accept(
this,
false);
382 #region Visit(BinaryExpression node, Object obj)
386 node.FirstOperand.LeftExpression = node.LeftExpression;
387 node.SecondOperand.LeftExpression = node.LeftExpression;
389 node.FirstOperand = (SingleIdentifierExpression)aux;
391 node.SecondOperand = (SingleIdentifierExpression)aux;
397 #region Visit(CastExpression node, Object obj)
401 node.Expression.LeftExpression = node.LeftExpression;
403 node.Expression = (SingleIdentifierExpression)aux;
409 #region Visit(CompoundExpression node, Object obj)
413 for (
int i = 0; i < node.ExpressionCount; i++) {
414 node.GetExpressionElement(i).LeftExpression = node.
LeftExpression;
423 #region Visit(InvocationExpression node, Object obj)
427 node.Identifier.LeftExpression = node.LeftExpression;
428 node.Arguments.LeftExpression =
false;
430 node.Identifier = (SingleIdentifierExpression)aux;
431 node.Arguments.Accept(
this,
false);
437 #region Visit(IsExpression node, Object obj)
441 node.Expression.LeftExpression = node.LeftExpression;
443 node.Expression = (SingleIdentifierExpression)aux;
449 #region Visit(LogicalExpression node, Object obj)
453 node.FirstOperand.LeftExpression = node.LeftExpression;
454 node.SecondOperand.LeftExpression = node.LeftExpression;
456 node.FirstOperand = (SingleIdentifierExpression)aux;
458 node.SecondOperand = (SingleIdentifierExpression)aux;
464 #region Visit(NewArrayExpression node, Object obj)
468 if (node.
Size != null) {
469 node.Size.LeftExpression = node.LeftExpression;
471 node.Size = (SingleIdentifierExpression)aux;
473 if (node.
Init != null) {
474 node.Init.LeftExpression = node.LeftExpression;
475 node.Init.Accept(
this,
false);
482 #region Visit(NewExpression node, Object obj)
485 node.Arguments.LeftExpression =
false;
486 node.Arguments.Accept(
this,
false);
492 #region Visit(QualifiedIdentifierExpression node, Object obj)
495 node.IdName.LeftExpression = node.LeftExpression;
496 node.IdExpression.LeftExpression = node.LeftExpression;
497 node.IdName.Accept(
this,
false);
498 node.IdExpression.Accept(
this,
true);
504 #region Visit(RelationalExpression node, Object obj)
508 node.FirstOperand.LeftExpression = node.LeftExpression;
509 node.SecondOperand.LeftExpression = node.LeftExpression;
511 node.FirstOperand = (SingleIdentifierExpression)aux;
513 node.SecondOperand = (SingleIdentifierExpression)aux;
519 #region Visit(TernaryExpression node, Object obj)
523 node.FirstOperand.LeftExpression = node.LeftExpression;
524 node.SecondOperand.LeftExpression = node.LeftExpression;
525 node.ThirdOperand.LeftExpression = node.LeftExpression;
527 node.FirstOperand = (SingleIdentifierExpression)aux;
529 node.SecondOperand = (SingleIdentifierExpression)aux;
531 node.ThirdOperand = (SingleIdentifierExpression)aux;
537 #region Visit(UnaryExpression node, Object obj)
542 node.Operand.LeftExpression = node.LeftExpression;
545 node.Operand = (SingleIdentifierExpression)aux;
554 #region Visit(Block node, Object obj)
557 public override Object
Visit(Block node, Object obj) {
558 bool returnFound =
false;
560 for (
int i = 0; i < node.StatementCount; i++) {
562 Object aux = node.GetStatementElement(i).Accept(
this, obj);
564 returnFound = (bool)aux;
571 #region Visit(ReturnStatement node, Object obj)
577 node.ReturnExpression = (SingleIdentifierExpression)aux;
583 #region Visit(IfElseStatement node, Object obj)
588 node.Condition = (SingleIdentifierExpression)aux;
589 SSAMap map1 = this.map.Clone();
593 IList<SingleIdentifierExpression> previousReferencesUsedInIfElseBody = this.referencesUsedInIfElseBody;
594 this.referencesUsedInIfElseBody = node.ReferencesUsedInTrueBranch;
595 node.TrueBranch.Accept(
this, obj);
597 this.addLocalVariable(this.map.ResetScope(), node.
TrueBranch);
598 SSAMap map2 = this.map.Clone();
600 this.referencesUsedInIfElseBody = node.ReferencesUsedInFalseBranch;
601 node.FalseBranch.Accept(
this, obj);
603 this.referencesUsedInIfElseBody = previousReferencesUsedInIfElseBody;
605 this.addLocalVariable(this.map.ResetScope(), node.
FalseBranch);
608 List<MoveStatement> mvSt = map1.GetMoveStatementsForIf(map2, this.map, node.Location.FileName, node.Condition.Location.Line);
610 node.AfterCondition = mvSt;
615 info =
new SSAInfo(this.map, this.map, map2, map1);
618 List<ThetaStatement> thSt;
622 thSt = map1.GetThetaStatements(ref this.map, node.Location.FileName, node.FalseBranch.Location.Line);
625 node.ThetaStatements = thSt;
632 #region Visit(WhileStatement node, Object obj)
636 SSAMap map1 = this.map.Clone();
638 node.Condition = (SingleIdentifierExpression)aux;
639 SSAMap map2 = this.map.Clone();
641 node.Statements.Accept(
this, obj);
642 this.addLocalVariable(this.map.ResetScope(), node.
Statements);
645 List<MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
647 node.InitWhile = mvSt;
649 List<ThetaStatement> thSt = map1.GetThetaStatements(map2, ref this.map, node.Location.FileName, node.Condition.Location.Line);
651 node.BeforeCondition = thSt;
653 mvSt = map1.GetMoveStatements(map2, this.map, node.Location.FileName, node.Condition.Location.Line);
655 node.AfterCondition = mvSt;
660 info =
new SSAInfo(this.map, null, map1, this.map);
668 #region Visit(ForStatement node, Object obj)
673 for (
int i = 0; i < node.InitializerCount; i++) {
674 node.GetInitializerElement(i).Accept(
this,
false);
676 SSAMap map1 = this.map.Clone();
679 node.Condition = (SingleIdentifierExpression)aux;
680 SSAMap map2 = this.map.Clone();
682 node.Statements.Accept(
this, obj);
683 SSAMap map3 = this.map.Clone();
685 for (
int i = 0; i < node.IteratorCount; i++) {
686 node.GetIteratorElement(i).Accept(
this,
false);
690 List<MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
692 node.AfterInit = mvSt;
694 List<ThetaStatement> thSt = map1.GetThetaStatements(map2, ref this.map, node.Location.FileName, node.Location.Line);
696 node.BeforeCondition = thSt;
698 mvSt = map1.GetMoveStatements(map2, this.map, node.Location.FileName, node.Location.Line);
700 node.AfterCondition = mvSt;
705 info =
new SSAInfo(this.map, null, map1, this.map);
708 info =
new SSAInfo(this.map, null, map1, this.map);
709 for (
int i = 0; i < node.IteratorCount; i++) {
710 node.GetIteratorElement(i).Accept(
new VisitorSSA2(), info);
713 this.addLocalVariable(this.map.ResetScope(), node.
AuxInitializer);
714 node.UpdateInitializer();
721 #region Visit(DoStatement node, Object obj)
725 SSAMap map1 = this.map.Clone();
727 node.Statements.Accept(
this, obj);
728 this.addLocalVariable(this.map.ResetScope(), node.
Statements);
729 SSAMap map2 = this.map.Clone();
731 node.Condition = (SingleIdentifierExpression)aux;
734 List<MoveStatement> mvSt = map1.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
738 List<ThetaStatement> thSt = map1.GetThetaStatements(ref this.map, node.Location.FileName, node.Statements.Location.Line);
740 node.BeforeBody = thSt;
745 info =
new SSAInfo(this.map, null, map1, this.map);
753 #region Visit(SwitchStatement node, Object obj)
757 List<SSAMap> mapsSwitch =
new List<SSAMap>();
758 int indexDefaultMap = -1;
761 node.Condition = (SingleIdentifierExpression)aux;
762 SSAMap map0 = this.map.Clone();
764 IList<SingleIdentifierExpression> previousCaseReferences = this.referencesUsedInCaseBody;
765 for (
int i = 0; i < node.SwitchBlockCount; i++) {
767 IList<SingleIdentifierExpression> caseReferences=
new List<SingleIdentifierExpression>();
768 node.ReferencesUsedCases[node.GetSwitchSectionElement(i).SwitchBlock] = caseReferences;
769 IList<SingleIdentifierExpression> previousReferencesUsedInCaseBody = this.referencesUsedInCaseBody;
770 this.referencesUsedInCaseBody = caseReferences;
773 node.GetSwitchSectionElement(i).Accept(
this,
false);
777 mapsSwitch.Add(this.map.Clone());
779 this.referencesUsedInCaseBody = previousCaseReferences;
783 List<MoveStatement> mvSt;
784 if (indexDefaultMap == -1)
785 mvSt = map0.GetMoveStatements(this.map, node.Location.FileName, node.Location.Line);
787 mvSt = map0.GetMoveStatementsForSwitch(mapsSwitch, node.Location.FileName, node.Location.Line);
789 node.AfterCondition = mvSt;
792 for (
int i = 0; i < node.SwitchBlockCount; i++) {
795 info =
new SSAInfo(mapsSwitch[i], this.map, mapsSwitch[i - 1], map0);
797 info =
new SSAInfo(mapsSwitch[i], this.map, null, null);
798 node.GetSwitchSectionElement(i).Accept(
new VisitorSSA2(), info);
801 List<ThetaStatement> thSt;
802 if (indexDefaultMap != -1)
803 thSt = map0.GetThetaStatements(mapsSwitch, ref this.map,
true, node.Location.FileName, node.GetSwitchSectionElement(node.SwitchBlockCount - 1).SwitchBlock.Location.Line);
805 thSt = map0.GetThetaStatements(mapsSwitch, ref this.map,
false, node.Location.FileName, node.GetSwitchSectionElement(node.SwitchBlockCount - 1).SwitchBlock.Location.Line);
808 node.ThetaStatements = thSt;
Encapsulates a Return statement of our programming languages.
Encapsulates a definition of a concrete method.
Statement Statements
Gets the block executed when the condition is true.
Encapsulates a binary expression of our programming language.
Expression Identifier
Gets the expression to invoke
Encapsulates the qualified name expression.
Encapsulates a Do statement of our programming language.
Expression FirstOperand
Gets the first operand of the ternary expression
Encapsulates the expression to access a field.
Encapsulates a block of statements.
override Object Visit(CompoundExpression node, Object obj)
override Object Visit(ArgumentExpression node, Object obj)
override Object Visit(IfElseStatement node, Object obj)
override Object Visit(TernaryExpression node, Object obj)
This class stores the information to use in SSA algorithm
override Object Visit(ConstructorDefinition node, Object obj)
bool HaveElseBlock()
Returns true if the statement has a else block. Otherwise, false.
Encapsulates a logical binary expression.
Statement Statements
Gets the block executed when the condition is true.
Encapsulates a invocation expression.
Encapsulates a cast expression.
Expression Condition
Gets the condition expression of For Statement.
override Object Visit(IdDeclaration node, Object obj)
Abstract class represents a programming language statement.
override Object Visit(Definition node, Object obj)
Block AuxInitializer
Gets the auxiliar block of variable declarations
override Object Visit(IsExpression node, Object obj)
Expression Operand
Gets the operand expression of the unary operation
override Object Visit(ArithmeticExpression node, Object obj)
Encapsulates a definition.
Encapsulates arithmetic binary expressions.
override Object Visit(ArrayAccessExpression node, Object obj)
Expression Condition
Gets the condition expression of If-Else statement.
SwitchSection GetSwitchSectionElement(int index)
Gets the element stored in the specified index.
Encapsulates a For statement of our programming languages.
Encapsulates a While statement of our programming languages.
void SetExpressionElement(int index, SingleIdentifierExpression exp)
Updates the value of the element stored in the specified index.
override Object Visit(FieldAccessExpression node, Object obj)
Encapsulates a Is expression.
Expression SecondOperand
Gets the second operand of the binary expression
override Object Visit(ForStatement node, Object obj)
Statement FalseBranch
Gets the block executed when the condition is false.
Statement TrueBranch
Gets the block executed when the condition is true.
Encapsulates a identifier expression of our programming language.
override Object Visit(Block node, Object obj)
Returns true if the last sentence is a return statement. Otherwise, false.
Expression Argument
Represents the option of the argument
Encapsulates a set of expressions.
VisitorSSA()
Constructor of VisitorSSA
List< Parameter > ParametersInfo
Gets the parameters information used to obtain its type
int Line
Gets line where the item is located
Encapsulates a relational binary expression.
This class makes the second part of static single assignment algorithm in which every variable is ass...
bool LeftExpression
Gets or sets true if the expression is allocated in the left part of an assignment, false otherwise.
Encapsulates assignment binary expressions.
override Object Visit(ReturnStatement node, Object obj)
override Object Visit(BinaryExpression node, Object obj)
CompoundExpression Init
Gets or sets the array initialization
Encapsulates a ternary expression of our programming language.
Expression Condition
Gets the condition expression of While Statement.
override Object Visit(ClassDefinition node, Object obj)
Encapsulates a If-Else statement of our programming language.
override Object Visit(ConstantDefinition node, Object obj)
Encapsulates a new array expression.
Encapsulates a definition of a concrete constructor.
Declaration GetMemberElement(int index)
Gets the element stored in the specified index.
override Object Visit(SwitchStatement node, Object obj)
override Object Visit(CastExpression node, Object obj)
Expression GetExpressionElement(int index)
Gets the element stored in the specified index.
abstract Object Accept(Visitor v, Object o)
Accept method of a concrete visitor.
Encapsulates a declaration.
Encapsulates a argument expression of our programming language.
Expression Condition
Gets the condition expression of Do Statement.
Encapsulates a 'this' expression.
override Object Visit(AssignmentExpression node, Object obj)
Expression Expression
Gets the expression.
Encapsulates a unary expression of our programming language.
override Object Visit(WhileStatement node, Object obj)
This class makes the static single assignment algorithm in which every variable is assigned exactly o...
Expression Size
Gets or sets the array size
InvocationExpression Initialization
Gets the base or this initialization of the constructor definition
Expression ReturnExpression
Gets the return expression.
override Object Visit(RelationalExpression node, Object obj)
override Object Visit(UnaryExpression node, Object obj)
Encapsulates a declaration of a concrete field.
override Object Visit(DoStatement node, Object obj)
Encapsulates a constant definition.
Encapsulates the array expression to access the concrete position.
override Object Visit(NewExpression node, Object obj)
override Object Accept(Visitor v, Object o)
Accept method of a concrete visitor.
Expression ThirdOperand
Gets the third operand of the ternary expression
override Object Visit(MethodDefinition node, Object obj)
string FullName
Gets or sets the nominal type of the declaration
Expression SecondOperand
Gets the second operand of the ternary expression
Expression FirstOperand
Gets the first operand of the binary expression
Expression Condition
Gets the condition expression of Switch Statement.
string Identifier
Gets the name associated to the declaration
override Object Visit(BaseCallExpression node, Object obj)
string Identifier
Gets the name name.
Implementation of a map to use in static single assignment algorithm.
Block Body
Gets the body of the method
override Object Visit(LogicalExpression node, Object obj)
AssignmentOperator
Assignment binary operators
Expression Expression
Gets the expression to access a field.
override Object Visit(QualifiedIdentifierExpression node, Object obj)
Encapsulates a invocation expression to base class.
string FileName
Gets de name of the file
Encapsulates a new expression.
Encapsulates a definition of a concrete class.
Expression Expression
Gets the expression to convert.
Encapsulates a Switch statement of our programming languages.
override Object Visit(NewArrayExpression node, Object obj)
override Object Visit(InvocationExpression node, Object obj)