attempt to build new ast

This commit is contained in:
Egor Mayorov
2025-10-08 23:14:19 +03:00
parent f7005a2735
commit 2746d072d4

View File

@@ -290,22 +290,88 @@ vector<SgStatement*> scheduleOperations(const map<SgStatement*, set<SgStatement*
return executionOrder;
}
void buildNewAST(SgStatement* loop, vector<SgStatement*>& newBody)
static bool buildNewAST(SgStatement* loop, vector<SgStatement*>& newBody)
{
SgStatement* endDo = loop->lastNodeOfStmt();
SgStatement* st = loop;
int lineNum = loop -> lineNumber() + 1;
for (int i = 0; i < newBody.size(); i++)
{
st -> setLexNext(*newBody[i]);
st = st -> lexNext();
st -> setlineNumber(lineNum);
lineNum++;
if (!loop) {return false;}
if (newBody.empty()) {return true;}
if (loop->variant() != FOR_NODE) {return false;}
SgStatement* loopStart = loop->lexNext();
SgStatement* loopEnd = loop->lastNodeOfStmt();
if (!loopStart || !loopEnd) {return false;}
for (SgStatement* stmt : newBody) {
if (stmt && stmt != loop && stmt != loopEnd) {
SgStatement* current = loopStart;
bool found = false;
while (current && current != loopEnd->lexNext()) {
if (current == stmt) {
found = true;
break;
}
current = current->lexNext();
}
if (!found) {return false;}
}
st -> setLexNext(*endDo);
}
vector<SgStatement*> extractedStatements;
vector<char*> savedComments;
vector<int> savedLineNumbers;
for (SgStatement* stmt : newBody) {
if (stmt && stmt != loop && stmt != loopEnd) {
savedComments.push_back(stmt->comments() ? strdup(stmt->comments()) : nullptr);
savedLineNumbers.push_back(stmt->lineNumber());
SgStatement* extracted = stmt->extractStmt();
if (extracted) {extractedStatements.push_back(extracted);}
}
}
SgStatement* currentPos = loop;
int lineCounter = loop->lineNumber() + 1;
for (size_t i = 0; i < extractedStatements.size(); i++) {
SgStatement* stmt = extractedStatements[i];
if (stmt) {
if (i < savedComments.size() && savedComments[i]) {
stmt->setComments(savedComments[i]);
}
stmt->setlineNumber(lineCounter++);
currentPos->insertStmtAfter(*stmt, *loop);
currentPos = stmt;
}
}
for (char* comment : savedComments) {
if (comment) {
free(comment);
}
}
if (currentPos && currentPos->lexNext() != loopEnd) {
currentPos->setLexNext(*loopEnd);
}
return true;
}
static bool validateNewOrder(SgStatement* loop, const vector<SgStatement*>& newOrder)
{
if (!loop || newOrder.empty()) {
return true;
}
unordered_set<SgStatement*> seen;
for (SgStatement* stmt : newOrder) {
if (stmt && stmt != loop && stmt != loop->lastNodeOfStmt()) {
if (seen.count(stmt)) {
return false;
}
seen.insert(stmt);
}
}
return true;
}
void runSwapOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*>>& loopGraph, std::map<FuncInfo*, std::vector<SAPFOR::BasicBlock*>>& FullIR, int& countOfTransform)
{
@@ -339,7 +405,10 @@ void runSwapOperators(SgFile *file, std::map<std::string, std::vector<LoopGraph*
for (auto v: new_order)
if (v -> lineNumber() > firstLine)
cout << v -> lineNumber() << endl;
if (validateNewOrder(loopForAnalyze.first, new_order)) {
buildNewAST(loopForAnalyze.first, new_order);
}
st = loopForAnalyze.first -> lexNext();
while (st != loopForAnalyze.first -> lastNodeOfStmt())
{