improved pass

This commit is contained in:
ALEXks
2024-03-23 10:51:41 +03:00
parent f345741acc
commit c5866f2cf9
2 changed files with 75 additions and 71 deletions

View File

@@ -1,3 +1,8 @@
#include "../Utils/leak_detector.h"
#include <string>
#include <map>
#include "Utils/SgUtils.h" #include "Utils/SgUtils.h"
#include "set_implicit_none.h" #include "set_implicit_none.h"
@@ -5,18 +10,18 @@ using std::vector;
using std::map; using std::map;
using std::set; using std::set;
static const char commonIntLetters[6] = {'i', 'j', 'k', 'm', 'n', 'l'}; static const char commonIntLetters[6] = { 'i', 'j', 'k', 'm', 'n', 'l' };
static void InitTypes(map<char, SgType*>& types) static void InitTypes(map<char, SgType*>& types)
{ {
for (char letter = 'a'; letter <= 'z'; letter++) for (char letter = 'a'; letter <= 'z'; letter++)
types[letter] = NULL; types[letter] = 0;
} }
static void FillCommonTypes(map<char, SgType*>& types) static void FillCommonTypes(map<char, SgType*>& types)
{ {
for (char letter : commonIntLetters) for (char letter : commonIntLetters)
if (types[letter] == NULL) if (types[letter] == 0)
types[letter] = new SgType(T_INT); types[letter] = new SgType(T_INT);
for (auto letter : types) for (auto letter : types)
@@ -36,37 +41,47 @@ static void FindAllVars(SgExpression* expr, set<SgSymbol*>& allVars)
FindAllVars(expr->rhs(), allVars); FindAllVars(expr->rhs(), allVars);
} }
static char AddLettersToMap(SgExpression* expr, SgType* type, map<char, SgType*>& types) static char getValue(SgExpression* ex)
{ {
if (expr == NULL) char charVal = 0;
return NULL; if (ex && ex->variant() == CHAR_VAL)
charVal = isSgValueExp(ex)->charValue();
if (expr->variant() == CHAR_VAL) return charVal;
{
SgValueExp* val = isSgValueExp(expr);
return val->charValue();
}
char leftVal = AddLettersToMap(expr->lhs(), type, types);
char rightVal = AddLettersToMap(expr->rhs(), type, types);
if (expr->variant() == DDOT)
if (leftVal != NULL && rightVal != NULL)
for (char letter = leftVal; letter <= rightVal; letter++)
types[letter] = type;
if (expr->variant() == EXPR_LIST)
{
if (leftVal != NULL)
types[leftVal] = type;
if (rightVal != NULL)
types[rightVal] = type;
}
return NULL;
} }
static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, map<SgStatement*, map<char, SgType*>>& typesByFunctions) static void AddLettersToMap(SgExpression* expr, SgType* type, map<char, SgType*>& types)
{
while (expr)
{
if (expr->variant() != EXPR_LIST)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
SgExpression* val = expr->lhs();
if (val->variant() == DDOT)
{
char leftVal = getValue(val->lhs());
char rightVal = getValue(val->rhs());
if (leftVal == 0 || rightVal == 0)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
for (char letter = leftVal; letter <= rightVal; letter++)
types[letter] = type;
}
else
{
char charVal = getValue(val);
if (charVal == 0)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
types[charVal] = type;
}
expr = expr->rhs();
}
}
static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, const map<SgStatement*, map<char, SgType*>>& typesByFunctions)
{ {
set<SgSymbol*> allVars; set<SgSymbol*> allVars;
map<char, SgType*> types; map<char, SgType*> types;
@@ -75,26 +90,26 @@ static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, map<SgSta
InitTypes(types); InitTypes(types);
FillCommonTypes(types); FillCommonTypes(types);
if (typesByFunctions.find(function->controlParent()) != typesByFunctions.end()) auto cp = function->controlParent();
for (auto parentType : typesByFunctions[function->controlParent()]) if (isSgProgHedrStmt(cp))
types[parentType.first] = parentType.second; if (typesByFunctions.find(cp) != typesByFunctions.end())
for (auto& parentType : typesByFunctions.at(cp))
types[parentType.first] = parentType.second;
auto implicitNoneDeclaration = new SgStatement(IMPL_DECL);
auto hasImplicitNone = false; auto hasImplicitNone = false;
auto endOfFunc = function->lastNodeOfStmt();
for (SgStatement* statement = function; statement != NULL; statement = statement->lexNext()) for (auto st = function; st != endOfFunc; st = st->lexNext())
{ {
if (st->variant() == IMPL_DECL)
if (statement->variant() == IMPL_DECL)
{ {
SgImplicitStmt* implicitStatement = isSgImplicitStmt(statement); SgImplicitStmt* implicitStatement = isSgImplicitStmt(st);
if (implicitStatement != NULL) if (implicitStatement != NULL)
{ {
int numberOfTypes = implicitStatement->numberOfImplicitTypes(); const int numberOfTypes = implicitStatement->numberOfImplicitTypes();
if (numberOfTypes > 0) if (numberOfTypes > 0)
{ {
for (int j = 0; j < numberOfTypes; j++) for (int j = 0; j < numberOfTypes; ++j)
{ {
SgType* type = implicitStatement->implicitType(j); SgType* type = implicitStatement->implicitType(j);
SgExpression* lettersExpression = implicitStatement->implicitRangeList(j); SgExpression* lettersExpression = implicitStatement->implicitRangeList(j);
@@ -106,27 +121,21 @@ static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, map<SgSta
hasImplicitNone = true; hasImplicitNone = true;
} }
} }
else if (statement->variant() == CONTAINS_STMT || isSgExecutableStatement(statement) != NULL) else if (st->variant() == CONTAINS_STMT || isSgExecutableStatement(st) != NULL)
break; break;
} }
for (SgStatement* statement = function; for (auto st = function; st != endOfFunc && st->variant() != CONTAINS_STMT; st = st->lexNext())
statement != NULL && statement->variant() != CONTAINS_STMT; statement = statement->lexNext()) for (int i = 0; i < 3; ++i)
{ FindAllVars(st->expr(i), allVars);
for (int expressionNumber = 0; expressionNumber < 3; expressionNumber++)
FindAllVars(statement->expr(expressionNumber), allVars);
if (statement == function->lastExecutable()) for (auto& var : allVars)
break;
}
for (auto var : allVars)
{ {
vector<SgStatement*> _; vector<SgStatement*> _;
SgStatement* declaredInStatement = declaratedInStmt(var, &_, false); SgStatement* declaredInStatement = declaratedInStmt(var, &_, false);
if (declaredInStatement == NULL) if (declaredInStatement == NULL)
{ {
char c = var->identifier()[0]; const char c = var->identifier()[0];
if (types.find(c) != types.end()) if (types.find(c) != types.end())
var->setType(types[c]); var->setType(types[c]);
@@ -139,20 +148,21 @@ static map<char, SgType*> FunctionImplicitCheck(SgStatement* function, map<SgSta
if (!hasImplicitNone) if (!hasImplicitNone)
{ {
for (SgStatement* statement = function->lexNext(); for (auto st = function->lexNext();
statement != NULL && statement->variant() != CONTAINS_STMT && isSgExecutableStatement(statement) == NULL;) st != endOfFunc && st->variant() != CONTAINS_STMT && isSgExecutableStatement(st) == NULL;
)
{ {
if (statement->variant() == IMPL_DECL) if (st->variant() == IMPL_DECL)
{ {
auto tmpStatement = statement; auto tmpStatement = st;
statement = statement->lexNext(); st = st->lexNext();
tmpStatement->deleteStmt(); tmpStatement->deleteStmt();
} }
else else
statement = statement->lexNext(); st = st->lexNext();
} }
function->insertStmtAfter(*implicitNoneDeclaration, *function); function->insertStmtAfter(*new SgStatement(IMPL_DECL), *function);
} }
allVars.clear(); allVars.clear();
@@ -165,13 +175,11 @@ void ImplicitCheck(SgFile* file)
{ {
map<SgStatement*, map<char, SgType*>> typesByFunctions; map<SgStatement*, map<char, SgType*>> typesByFunctions;
for (int functionNumber = 0; functionNumber < file->numberOfFunctions(); functionNumber++) for (int func = 0; func < file->numberOfFunctions(); ++func)
{ {
SgStatement* function = file->functions(functionNumber); SgStatement* function = file->functions(func);
typesByFunctions[function] = FunctionImplicitCheck(function, typesByFunctions);
map<char, SgType*>& types = FunctionImplicitCheck(function, typesByFunctions);
typesByFunctions[function] = types;
} }
typesByFunctions.clear(); typesByFunctions.clear();
} }

View File

@@ -1,7 +1,3 @@
#pragma once #pragma once
#include "Utils/SgUtils.h"
#include <string>
#include <map>
void ImplicitCheck(SgFile* file); void ImplicitCheck(SgFile* file);