improved IR and dead_code

This commit is contained in:
ALEXks
2024-04-13 18:14:28 +03:00
parent b1eb7608ba
commit 2b9adf5d27
4 changed files with 42 additions and 10 deletions

View File

@@ -195,7 +195,7 @@ static SAPFOR::Argument* createConstArg(SgExpression* exp)
else if (var == BOOL_VAL) else if (var == BOOL_VAL)
newName = value->boolValue() ? "TRUE" : "FALSE"; newName = value->boolValue() ? "TRUE" : "FALSE";
else if (var == KEYWORD_VAL) else if (var == KEYWORD_VAL)
newName = string("%") + isSgKeywordValExp(exp)->value(); newName = string("%") + "key_arg_" + isSgKeywordValExp(exp)->value();
else if (var == CONSTRUCTOR_REF) else if (var == CONSTRUCTOR_REF)
{ {
type = CFG_ARG_TYPE::CONSTR_REF; type = CFG_ARG_TYPE::CONSTR_REF;
@@ -395,7 +395,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector<IR_Block*>&
if (ex) if (ex)
{ {
const int var = ex->variant(); const int var = ex->variant();
if ((var == VAR_REF || var == CONST_REF) && !ex->lhs() && !ex->rhs()) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> if ((var == VAR_REF || var == CONST_REF || var == LABEL_REF) && !ex->lhs() && !ex->rhs()) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{ {
if (var == CONST_REF) if (var == CONST_REF)
{ {
@@ -405,6 +405,14 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector<IR_Block*>&
else else
return createArg(ex->symbol(), commonVars, func); return createArg(ex->symbol(), commonVars, func);
} }
else if (var == LABEL_REF)
{
auto labRef = isSgLabelRefExp(ex);
if (labRef == NULL)
printInternalError(convertFileName(__FILE__).c_str(), __LINE__);
return createConstArg(labRef->label()->getLabNumber());
}
else else
return createArg(ex->symbol(), commonVars, func); return createArg(ex->symbol(), commonVars, func);
} }
@@ -609,6 +617,8 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector<IR_Block*>&
} }
else if (var == CONSTRUCTOR_REF) else if (var == CONSTRUCTOR_REF)
return createConstArg(ex); return createConstArg(ex);
else if (var == SPEC_PAIR)
return processExpression(ex->rhs(), blocks, func, commonVars);
else else
{ {
__spf_print(1, "unknown expression '%s'\n", tag[ex->variant()]); __spf_print(1, "unknown expression '%s'\n", tag[ex->variant()]);
@@ -1131,8 +1141,8 @@ static SgStatement* processStatement(SgStatement* st, vector<IR_Block*>& blocks,
else if (var == PRINT_STAT || var == WRITE_STAT || var == READ_STAT) else if (var == PRINT_STAT || var == WRITE_STAT || var == READ_STAT)
{ {
SgInputOutputStmt* io = isSgInputOutputStmt(st); SgInputOutputStmt* io = isSgInputOutputStmt(st);
SgExpression* item = io->itemList();
SgExpression* item = io->itemList();
vector<vector<SgExpression*>> items; vector<vector<SgExpression*>> items;
vector<SgExpression*> tmp; vector<SgExpression*> tmp;
@@ -1200,10 +1210,30 @@ static SgStatement* processStatement(SgStatement* st, vector<IR_Block*>& blocks,
} }
else else
{ {
SgExpression* spec = io->specList();
vector<SAPFOR::Argument*> specArgs;
if (spec->variant() == SPEC_PAIR)
specArgs.push_back(processExpression(spec, blocks, func, commonVars));
else
{
while (spec)
{
specArgs.push_back(processExpression(spec->lhs(), blocks, func, commonVars));
spec = spec->rhs();
}
}
vector<SAPFOR::Argument*> args; vector<SAPFOR::Argument*> args;
for (auto& par : item) for (auto& par : item)
args.push_back(processExpression(par, blocks, func, commonVars)); args.push_back(processExpression(par, blocks, func, commonVars));
for (auto& arg : specArgs)
{
Instruction* instr = new Instruction(CFG_OP::IO_PARAM, arg);
blocks.push_back(new IR_Block(instr));
}
for (auto& arg : args) for (auto& arg : args)
{ {
Instruction* instr = new Instruction(CFG_OP::PARAM, arg); Instruction* instr = new Instruction(CFG_OP::PARAM, arg);

View File

@@ -12,7 +12,7 @@ namespace SAPFOR
{ {
struct CFG_Settings; struct CFG_Settings;
enum class CFG_OP { NONE, ASSIGN, POINTER_ASS, LOAD, STORE, REC_REF_LOAD, REC_REF_STORE, REF, PARAM, RANGE, ENTRY, REC_REF, enum class CFG_OP { NONE, ASSIGN, POINTER_ASS, LOAD, STORE, REC_REF_LOAD, REC_REF_STORE, REF, PARAM, IO_PARAM, RANGE, ENTRY, REC_REF,
ADD, MULT, DIV, SUBT, UN_ADD, UN_MINUS, POW, CONCAT, CAST, ADD, MULT, DIV, SUBT, UN_ADD, UN_MINUS, POW, CONCAT, CAST,
JUMP, JUMP_IF, JUMP, JUMP_IF,
GE, LE, GT, LT, EQ, NEQV, EQV, EMPTY, OR, AND, NOT, GE, LE, GT, LT, EQ, NEQV, EQV, EMPTY, OR, AND, NOT,
@@ -22,7 +22,7 @@ namespace SAPFOR
enum class CFG_ARG_TYPE { NONE, REG, VAR, ARRAY, CONST, FUNC, LAB, INSTR, CONST_STR, RECORD, CONSTR_REF }; enum class CFG_ARG_TYPE { NONE, REG, VAR, ARRAY, CONST, FUNC, LAB, INSTR, CONST_STR, RECORD, CONSTR_REF };
enum class CFG_MEM_TYPE { NONE_, COMMON_, SAVE_, LOCAL_, MODULE_, FUNC_RES_, FUNC_PARAM_, FILED_ }; enum class CFG_MEM_TYPE { NONE_, COMMON_, SAVE_, LOCAL_, MODULE_, FUNC_RES_, FUNC_PARAM_, FILED_ };
static std::vector<std::string> CFG_OP_S = { "--", " = ", " => ", "LOAD ", "STORE ", "LOAD_REF ", "STORE_REF ", "REF ", "PARAM ", "RANGE ", "ENTRY ", "->", static std::vector<std::string> CFG_OP_S = { "--", " = ", " => ", "LOAD ", "STORE ", "LOAD_REF ", "STORE_REF ", "REF ", "PARAM ", "IO_PARAM ", "RANGE ", "ENTRY ", "->",
" + ", " * ", " / ", " - ", " + ", "-", " ** ", " // ", "CAST ", " + ", " * ", " / ", " - ", " + ", "-", " ** ", " // ", "CAST ",
"GOTO ", "IF_FALSE ", "GOTO ", "IF_FALSE ",
" >= ", " <= ", " > " , " < ", " == ", " != ", " eqv ", "CONTINUE", " || ", " && ", " ! ", " >= ", " <= ", " > " , " < ", " == ", " != ", " eqv ", "CONTINUE", " || ", " && ", " ! ",
@@ -229,6 +229,7 @@ namespace SAPFOR
break; break;
case CFG_OP::REF: case CFG_OP::REF:
case CFG_OP::PARAM: case CFG_OP::PARAM:
case CFG_OP::IO_PARAM:
res = CFG_OP_S[(int)operation] + arg1Val; res = CFG_OP_S[(int)operation] + arg1Val;
break; break;
case CFG_OP::CAST: case CFG_OP::CAST:

View File

@@ -65,7 +65,8 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru
SAPFOR::CFG_OP::ENTRY, SAPFOR::CFG_OP::ENTRY,
SAPFOR::CFG_OP::EXIT, SAPFOR::CFG_OP::EXIT,
SAPFOR::CFG_OP::DVM_DIR, SAPFOR::CFG_OP::DVM_DIR,
SAPFOR::CFG_OP::SPF_DIR SAPFOR::CFG_OP::SPF_DIR,
SAPFOR::CFG_OP::IO_PARAM
}; };
if (always_useful.find(instr_operation) != always_useful.end()) if (always_useful.find(instr_operation) != always_useful.end())

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define VERSION_SPF "2316" #define VERSION_SPF "2317"