package com.hpctoday.fada;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.hpctoday.fada.pointers.IntegerPointer;
public class Statement implements Comparable<Statement>{
private int id; // !< The identifier of the statement.
private int first_stmt; // !< the ID of the first enclosed statement.
private int last_stmt; // !< the ID of the last enclosed statement.
private int first_else_stmt; // !< the ID of the first statement enclosed by the else-part, if th statement is an alternative
private int last_else_stmt; // !< the ID of the last statement enclosed by the else-part, if th statement is an alternative
private References references; // !< all references
private boolean is_main; // !< is the main program (a root node)
private List<Inequation> pragma_constraints;
// !\name If not a main program
private Statement parent;
private boolean is_assign; // !< is it an assignment ?
// !\name If assignment
private Assignment assign; // !< the assignment
// ! \name If control (not an assignment)
private Control control; // !< a reference to the control
private List<Statement> surrounded_statements; // !< List of surrounded statements.
// ! \name If alternative
private List<Statement> surrounded_statements_else_part; // !< List of statements surrounded by else branch
// !\name Setters
// ! \brief A setter for "is_assign"
private void SetStatementKind(boolean kind) {
is_assign = kind;
}
// ! \brief A setter for "parent"
private void SetParent(Statement __stmt) {
parent = __stmt;
};
// ! \brief A setter for "assign"
private void SetAssignment(Assignment __assign) {
assign = __assign;
}
// ! \brief A setter for "control"
private void SetControl(Control __ctl) {
control = __ctl;
}
// ! \brief A setter for "surrounded_statements"
private void SetSurrStmt(List<Statement> __stmts) {
surrounded_statements = __stmts;
}
// ! \brief A setter for "surrounded_statements_else_part"
private void SetElseSurrStmt(List<Statement> __stmts) {
surrounded_statements_else_part = __stmts;
}
private void SetProgram(boolean value) {
is_main = value;
}
@SuppressWarnings("unused")
private void Serialize(List<Assignment> __assign, List<Control> __control) {
if (IsAssignment()) {
__assign.add(GetAssignment());
return;
}
if (IsControl()) {
__control.add(GetControl());
for (Statement it : GetSurroundedStatements())
it.Serialize(__assign, __control);
if (GetControl().IsIfElse())
for (Statement it : GetElseSurroundedStatements())
it.Serialize(__assign, __control);
return;
}
if (IsProgram()) {
__control.add(GetControl());
for (Statement it : GetSurroundedStatements())
it.Serialize(__assign, __control);
return;
}
throw new RuntimeException("Statement::Serialize .... fatal error (inappropriate case)");
}
// !\name Advanced setters
private void Initialize() {
this.SetProgram(false);
this.SetStatementKind(true);
this.SetAssignment(null);
this.SetControl(null);
pragma_constraints = new ArrayList<Inequation>();
surrounded_statements = new ArrayList<Statement>();
surrounded_statements_else_part = new ArrayList<Statement>();
}
private void Set(Assignment assign) {
this.SetStatementKind(true);
this.SetAssignment(assign);
}
private void Set(Control __control, List<Statement> __surr) {
this.SetStatementKind(false);
this.SetControl(__control);
this.SetSurrStmt(__surr);
}
private void Set(Control __control, List<Statement> __surr, List<Statement> __surr_else) {
this.SetStatementKind(false);
this.SetControl(__control);
this.SetSurrStmt(__surr);
this.SetElseSurrStmt(__surr_else);
GetControl().SwitchToIfElse();
}
private void Set(List<Statement> __stmts) {
this.SetProgram(true);
this.SetSurrStmt(__stmts);
}
// private void Link2Parent(Statement __parent){}
@SuppressWarnings("unused")
private void BuildDomains(Condition __c) {
GetReferences().SetDomain(__c);
if (IsAssignment())
return;
Condition new_domain = new Condition(__c, Condition.Logical_Operator.FADA_AND, GetControl().GetRegularDomain());
for (Statement it : GetSurroundedStatements())
it.BuildDomains(new_domain);
if (IsControl() && GetControl().IsIfElse()) {
new_domain = new Condition(__c, Condition.Logical_Operator.FADA_AND, GetControl().GetRegularDomain().Negate());
for (Statement it : GetElseSurroundedStatements())
it.BuildDomains(new_domain);
return;
}
return;
}
// ~Statement();
public Statement() {
Initialize();
SetProgram(true);
}
public Statement(Assignment __assign) {
this.Initialize();
this.Set(__assign);
}
public Statement(List<Statement> __stmts) {
Initialize();
Set(__stmts);
}
public Statement(Control __control) {
Initialize();
this.SetStatementKind(false);
SetControl(__control);
}
public Statement(Control __control, List<Statement> __surr) {
this.Initialize();
this.Set(__control, __surr);
}
public Statement(Control __control, List<Statement> __surr, List<Statement> __surr_else) {
this.Initialize();
this.Set(__control, __surr, __surr_else);
}
public void Enclose(Statement stmt, boolean in_else_part) {
if (!in_else_part)
GetSurroundedStatements().add(stmt);
else if (IsControl()) {
GetControl().SwitchToIfElse();
GetElseSurroundedStatements().add(stmt);
} else {
throw new RuntimeException("Statement::Enclose ... inappropriate case");
}
}
public void Enclose(Assignment assign, boolean in_else_part) {
Enclose(new Statement(assign), in_else_part);
}
public Statement GetParent() {
return parent;
}
public boolean IsAssignment() {
return !is_main && is_assign;
}
public boolean IsControl() {
return !is_main && !is_assign;
}
public boolean IsProgram() {
return is_main;
}
public Assignment GetAssignment() {
return assign;
}
public Control GetControl() {
return control;
}
public List<Statement> GetSurroundedStatements() {
return surrounded_statements;
}
public List<Statement> GetElseSurroundedStatements() {
return surrounded_statements_else_part;
}
@Override
public String toString() {
return Print_str();
}
public String Print_str() {
return Print_str("");
}
public String Print_str(String tabs) {
if (this.IsAssignment())
return tabs + this.GetAssignment().Print_str();
String result;
if (this.IsControl())
result = "\n" + tabs + this.GetControl().Print_str();
else
result = "\n" + tabs + "main";
for (Statement it : GetSurroundedStatements())
result += "\n" + it.Print_str(tabs + "\t");
if (!this.GetElseSurroundedStatements().isEmpty()) {
for (Statement it : GetElseSurroundedStatements())
result += "\n" + it.Print_str(tabs + "\t");
}
return result;
}
// public void Print(){}
public String Regenerate_C_Code(String prefix) {
String indent = "\t";
if (this.IsAssignment()){
return prefix + this.GetAssignment().Generate_C_Code() + ";";
}
String result = "";
if (this.IsControl()) {
if (this.GetControl().IsWhileLoop())
result += prefix + "int " + this.GetControl().GetLoopCounter() + " = 0;";
result += prefix + this.GetControl().Generate_C_Code();
}
if (this.IsProgram())
result += prefix + "//main";
result += prefix + "{";
if (this.IsControl() && this.GetControl().IsWhileLoop())
result += prefix + indent + this.GetControl().GetLoopCounter() + "++;";
for (Statement it : GetSurroundedStatements())
result += it.Regenerate_C_Code(prefix + indent);
result += prefix + "}";
if (this.IsControl() && this.GetControl().IsIfElse()) {
result += prefix + "else";
result += prefix + indent + "{";
for (Statement it : GetElseSurroundedStatements())
result += it.Regenerate_C_Code(prefix + indent);
result += prefix + indent + "}";
}
return result;
}
// public String Generate_C_Test_Code(String prefix){return null;}
public void Link2Parent() {
if (this.IsProgram())
this.SetParent(null);
if (this.IsAssignment())
return;
for (Statement it : GetSurroundedStatements()) {
it.SetParent(this);
it.Link2Parent();
}
if (this.IsControl() && this.GetControl().IsIfElse())
for (Statement it : GetElseSurroundedStatements()) {
it.SetParent(this);
it.Link2Parent();
}
}
//public List<ElementaryDependence> GenerateOptimalScheduleForFlowDependenciesComputing(String array, int stmt) {}
// public List<ElementaryDependence> GenerateOptimalScheduleForFlowDependenciesComputing(String array, FADA_Index __i,int stmt, int
// deep){return null;}
public List<ElementaryDependence> GenerateDeepestOptimalScheduleForFlowDependenciesComputing(String array, FADA_Index __i,
int read_stmt, int deep) {
Statement __stmt = this;
Statement __ptr = __stmt;
Statement __ptr2;
List<ElementaryDependence> result = new ArrayList<ElementaryDependence>();
while ((__ptr.IsControl() && !__ptr.GetControl().IsLoop()) || (!__ptr.GetParent().IsProgram())) {
__ptr2 = __ptr.GetParent();
if (!__ptr2.IsControl()) {
throw new RuntimeException("Statement::GenerateOptimalScheduleForFlowDependenceComputing, fatal error (inappropriate case)");
}
if (__ptr2.GetControl().IsIfElse()) {
int position = __ptr2.GetSurroundedStatements().lastIndexOf(__ptr);
if (position != -1) {
for (int i = __ptr2.GetSurroundedStatements().size() - 1; i != position; --i) {
Statement it = __ptr2.GetSurroundedStatements().get(i);
List<ElementaryDependence> local = it.GenerateReversedConfigs(array, __i, read_stmt, deep);
for (ElementaryDependence itr : local)
result.add(itr);
}
}
}
if (__ptr.GetControl().IsLoop())
--deep;
__ptr = __ptr2;
}
if (deep != 0) {
throw new RuntimeException(
"Statement::GenerateOptimalScheduleForFlowDependenceComputing, fatal error( inappropriate value for 'deep')");
}
return result;
}
public List<ElementaryDependence> GenerateReversedConfigs(String array, FADA_Index __i, int read_stmt, int deep) {
List<ElementaryDependence> result = new ArrayList<ElementaryDependence>();
if (IsAssignment()) {
if (GetAssignment().GetLHSArray().equals(array)) {
ElementaryDependence dep = new ElementaryDependence(read_stmt, GetID(), deep, __i);
result.add(0, dep);
}
return result;
}
if (IsControl()) {
for (int i = GetSurroundedStatements().size() - 1; i >= 0; --i) {
Statement it = GetSurroundedStatements().get(i);
List<ElementaryDependence> local = it.GenerateReversedConfigs(array, __i, read_stmt, deep);
for (ElementaryDependence itr : local)
result.add(itr);
}
if (GetControl().IsIfElse()) {
for (int i = GetElseSurroundedStatements().size() - 1; i >= 0; --i) {
Statement it = GetElseSurroundedStatements().get(i);
List<ElementaryDependence> local = it.GenerateReversedConfigs(array, __i, read_stmt, deep);
for (ElementaryDependence itr : local)
result.add(itr);
}
}
return result;
}
if (IsProgram()) {
for (int i = GetSurroundedStatements().size() - 1; i >= 0; --i) {
Statement it = GetSurroundedStatements().get(i);
List<ElementaryDependence> local = it.GenerateReversedConfigs(array, __i, read_stmt, deep);
for (ElementaryDependence itr : local)
result.add(itr);
}
return result;
}
throw new RuntimeException("Statement::GenerateReversedConfigs, fatal error (unhandled case)");
}
//public Statement Find(int stmt) {}
public void SetID(int __id) {
id = __id;
}
public void SetReferences(References __stmt) {
references = __stmt;
}
public int GetID() {
return id;
}
public References GetReferences() {
return references;
}
//public List<ElementaryDependence> ComputeOptimalScheduleForFlowDependenciesEvaluation(int rank, String array, int deep) {}
public void SetEnclosed(int f, int l) {
first_stmt = f;
last_stmt = l;
}
public void GetEnclosed(IntegerPointer f, IntegerPointer l) {
f.setValue(first_stmt);
l.setValue(last_stmt);
}
public void SetElseEnclosed(int f, int l) {
first_else_stmt = f;
last_else_stmt = l;
}
public void GetElseEnclosed(IntegerPointer f, IntegerPointer l) {
f.setValue(first_else_stmt);
l.setValue(last_else_stmt);
}
public void ReferencedScalars(Set<String> __written, Set<String> __read) {
if (IsAssignment()) {
GetAssignment().ReferencedScalars(__written, __read);
return;
}
if (IsProgram()) {
for (Statement it : GetSurroundedStatements())
it.ReferencedScalars(__written, __read);
return;
}
if (IsControl()) {
if (GetControl().IsLoop())
__written.add(GetControl().GetLoopCounter());
GetControl().ReferencedScalars(__read);
for (Statement it : GetSurroundedStatements())
it.ReferencedScalars(__written, __read);
if (GetControl().IsIfElse())
for (Statement it : GetElseSurroundedStatements())
it.ReferencedScalars(__written, __read);
return;
}
throw new RuntimeException("Statement::GetReferencedScalars..... fadal error (inapropirate case)");
}
public void SingleCounterId(List<String> counters) {
if (IsAssignment())
return;
if (IsControl() && GetControl().IsLoop()) {
if (counters.contains(GetControl().GetCounter())) {
String new_name = Global.NewCounterName(GetControl().GetCounter(), counters);
Map<String, String> __mapping = new HashMap<String, String>();
__mapping.put(GetControl().GetCounter(), new_name);
GetControl().Substitute(__mapping);
for (Statement it : GetSurroundedStatements())
it.Substitute(__mapping);
counters.add(new_name);
}
counters.add(GetControl().GetCounter());
for (Statement it : GetSurroundedStatements())
it.SingleCounterId(counters);
return;
}
if (IsControl() && (GetControl().IsIf() || GetControl().IsIfElse())) {
for (Statement it : GetSurroundedStatements())
it.SingleCounterId(counters);
if (GetControl().IsIfElse())
for (Statement it : GetElseSurroundedStatements())
it.SingleCounterId(counters);
return;
}
throw new RuntimeException("Statement::SingleCounterId, Fatal error (inappropriate case)");
}
public void SingleCounterId() {
if (IsProgram()) {
List<String> __counters = new ArrayList<String>();
for (Statement it : GetSurroundedStatements())
it.SingleCounterId(__counters);
return;
}
throw new RuntimeException("Statement::SingleCounterId, fatal error (Given statement is not a function)");
}
public void Substitute(Map<String, String> __mapping) {
if (IsAssignment()) {
GetAssignment().Rename(__mapping);
return;
}
if (IsControl()) {
GetControl().Substitute(__mapping);
for (Statement it : GetSurroundedStatements())
it.Substitute(__mapping);
if (GetControl().IsIfElse())
for (Statement it : GetElseSurroundedStatements())
it.Substitute(__mapping);
return;
}
if (IsProgram()) {
for (Statement it : GetSurroundedStatements())
it.Substitute(__mapping);
return;
}
throw new RuntimeException("Statement::Substitute, fatal error (inappropriate case)");
}
public void NormalizeConditions(List<References> ref, List<String> param) {
if (IsAssignment())
return;
if (IsProgram()) {
for (Statement it : GetSurroundedStatements())
it.NormalizeConditions(ref, param);
return;
}
if (IsControl() && (GetControl().IsWhileLoop() || GetControl().IsIf() || GetControl().IsIfElse() || GetControl().IsForLoop())) {
List<String> loopcounters = ref.get(GetID()).GetCounters();
if (GetControl().IsLoop())
loopcounters.add(GetControl().GetLoopCounter());
GetControl().NormalizeConditions(GetID(), loopcounters, param);
for (Statement it : GetSurroundedStatements())
it.NormalizeConditions(ref, param);
if (GetControl().IsIfElse())
for (Statement it : GetElseSurroundedStatements())
it.NormalizeConditions(ref, param);
return;
}
throw new RuntimeException("Statement::NormalizeConditions, FATAL ERROR (inappropriate case)");
}
// inline void ComputeSources(Program pgm,Vector<String> param, boolean sa, boolean alphas);
// public String GenerateHTMLIndex(Vector<References> __references,String indent);
// public void GenerateHTMLIndex(String file_name,Vector<References> __references);
// public String ToHtml(String work_dir, String file_name_prefix, String indent,String def_frame,Options op);
public boolean IsSequentiallyBefore(int id1, int id2) {
if (id1 >= id2)
return false;
if (IsAssignment())
return false;
if (IsControl() && id1 == GetID())
return true;
if (IsControl() || IsProgram()) {
if (IsControl() && GetControl().IsIfElse()) {
if (IsInThenPart(id1) && IsInElsePart(id2)) {
// cout<<"\n Statement("<<id1<<") Is In Then Part, Where Statement("<<id2<<") in Else Part\n";
return false;
}
if (IsInThenPart(id1)) {
boolean found = false;
for (Statement it : GetSurroundedStatements()) {
if (!found) {
it.IsEnclosed(id1);
if (it.IsEnclosed(id2))
return it.IsSequentiallyBefore(id1, id2);
found = true;
} else {
if (it.IsEnclosed(id2))
return true;
}
}
throw new RuntimeException("Statement::IsSequentiallyBefore ... fatal error (inappropriate case)[1]]");
}
if (IsInElsePart(id1)) {
boolean found = false;
for (Statement it : GetElseSurroundedStatements()) {
if (!found) {
it.IsEnclosed(id1);
if (it.IsEnclosed(id2))
return it.IsSequentiallyBefore(id1, id2);
found = true;
} else {
if (it.IsEnclosed(id2))
return true;
}
}
throw new RuntimeException("Statement::IsSequentiallyBefore ... fatal error (inappropriate case)[2]");
}
throw new RuntimeException("Statement::IsSequentiallyBefore ... fatal error (inappropriate case)[3]");
} else {
boolean found = false;
for (Statement it : GetSurroundedStatements()) {
if (!found) {
if (it.IsEnclosed(id1)) {
if (it.IsEnclosed(id2))
return it.IsSequentiallyBefore(id1, id2);
found = true;
}
} else {
if (it.IsEnclosed(id2))
return true;
}
}
throw new RuntimeException("Statement::IsSequentiallyBefore ... fatal error (inappropriate case)[4]");
}
}
return false;
}
public boolean IsEnclosed(int id) {
IntegerPointer tf = new IntegerPointer();
IntegerPointer tl = new IntegerPointer();
GetEnclosed(tf, tl);
if (IsControl() && GetControl().IsIfElse()) {
IntegerPointer ef = new IntegerPointer();
IntegerPointer el = new IntegerPointer();
GetElseEnclosed(ef, el);
return id >= tf.getValue() && id <= el.getValue();
}
return id >= tf.getValue() && id <= tl.getValue();
}
public boolean IsInThenPart(int id) {
if (IsControl() && GetControl().IsIfElse()) {
IntegerPointer f = new IntegerPointer();
IntegerPointer l = new IntegerPointer();
GetEnclosed(f, l);
return id >= f.getValue() && id <= l.getValue();
}
throw new RuntimeException("Statement::IsInThenPart .... fatal error (inaproriate case)");
}
public boolean IsInElsePart(int id) {
if (IsControl() && GetControl().IsIfElse()) {
IntegerPointer f = new IntegerPointer();
IntegerPointer l = new IntegerPointer();
GetElseEnclosed(f, l);
return id >= f.getValue() && id <= l.getValue();
}
throw new RuntimeException("Statement::IsInElsePart .... fatal error (inaproriate case)");
}
// public String PrettyPrint(){return null;}
public List<Inequation> GetPragmaConstraints() {
return pragma_constraints;
}
public void AddPragmaConstraint(Inequation ineq) {
GetPragmaConstraints().add(ineq);
}
public void CollectIrregularLoopsProperties(LDemonstrator __ppts, List<String> counters) {
//Deep copy
List<String> enclosing_counters = new ArrayList<String>(counters);
if (IsAssignment())
return;
if (IsProgram()) {
List<String> empty = new ArrayList<String>();
for (Statement it : GetSurroundedStatements())
it.CollectIrregularLoopsProperties(__ppts, empty);
return;
}
if (IsControl() && GetControl().IsWhileLoop()) {
if (GetControl().GetCondition().IsLeaf())
__ppts.PUSHBACK(LogicalClause.LoopProperty(enclosing_counters, GetControl().GetCounter(), GetControl().GetCondition()
.GetInequation()));
}
if (IsControl() && GetControl().IsLoop()) {
enclosing_counters.add(GetControl().GetCounter());
for (Statement it : GetSurroundedStatements())
it.CollectIrregularLoopsProperties(__ppts, enclosing_counters);
return;
}
if (IsControl() && GetControl().IsIf()) {
for (Statement it : GetSurroundedStatements())
it.CollectIrregularLoopsProperties(__ppts, enclosing_counters);
return;
}
if (IsControl() && GetControl().IsIfElse()) {
for (Statement it : GetSurroundedStatements())
it.CollectIrregularLoopsProperties(__ppts, enclosing_counters);
for (Statement it : GetElseSurroundedStatements())
it.CollectIrregularLoopsProperties(__ppts, enclosing_counters);
return;
}
if (IsAssignment())
return;
return;
}
@Override
public int compareTo(Statement s) {
return this.Print_str().compareTo(s.Print_str());
}
}