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

View File

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