From 2b9adf5d275433a09b1baac02cf3c7b83dfe2d39 Mon Sep 17 00:00:00 2001 From: ALEXks Date: Sat, 13 Apr 2024 18:14:28 +0300 Subject: [PATCH] improved IR and dead_code --- .../experts/Sapfor_2017/_src/CFGraph/IR.cpp | 38 +++++++++++++++++-- sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h | 7 ++-- .../_src/Transformations/dead_code.cpp | 5 ++- .../experts/Sapfor_2017/_src/Utils/version.h | 2 +- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp index fd59b00..5c8bd0c 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.cpp @@ -195,7 +195,7 @@ static SAPFOR::Argument* createConstArg(SgExpression* exp) else if (var == BOOL_VAL) newName = value->boolValue() ? "TRUE" : "FALSE"; else if (var == KEYWORD_VAL) - newName = string("%") + isSgKeywordValExp(exp)->value(); + newName = string("%") + "key_arg_" + isSgKeywordValExp(exp)->value(); else if (var == CONSTRUCTOR_REF) { type = CFG_ARG_TYPE::CONSTR_REF; @@ -395,7 +395,7 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& if (ex) { const int var = ex->variant(); - if ((var == VAR_REF || var == CONST_REF) && !ex->lhs() && !ex->rhs()) // обращение к переменной + if ((var == VAR_REF || var == CONST_REF || var == LABEL_REF) && !ex->lhs() && !ex->rhs()) // обращение к переменной { if (var == CONST_REF) { @@ -405,6 +405,14 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& else 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 return createArg(ex->symbol(), commonVars, func); } @@ -609,13 +617,15 @@ static SAPFOR::Argument* processExpression(SgExpression* ex, vector& } else if (var == CONSTRUCTOR_REF) return createConstArg(ex); + else if (var == SPEC_PAIR) + return processExpression(ex->rhs(), blocks, func, commonVars); else { __spf_print(1, "unknown expression '%s'\n", tag[ex->variant()]); printInternalError(convertFileName(__FILE__).c_str(), __LINE__); } } - + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); return NULL; } @@ -1131,8 +1141,8 @@ static SgStatement* processStatement(SgStatement* st, vector& blocks, else if (var == PRINT_STAT || var == WRITE_STAT || var == READ_STAT) { SgInputOutputStmt* io = isSgInputOutputStmt(st); + SgExpression* item = io->itemList(); - vector> items; vector tmp; @@ -1200,10 +1210,30 @@ static SgStatement* processStatement(SgStatement* st, vector& blocks, } else { + SgExpression* spec = io->specList(); + vector 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 args; for (auto& par : item) 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) { Instruction* instr = new Instruction(CFG_OP::PARAM, arg); diff --git a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h index e50a7dc..505a428 100644 --- a/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h +++ b/sapfor/experts/Sapfor_2017/_src/CFGraph/IR.h @@ -12,7 +12,7 @@ namespace SAPFOR { 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, JUMP, JUMP_IF, 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_MEM_TYPE { NONE_, COMMON_, SAVE_, LOCAL_, MODULE_, FUNC_RES_, FUNC_PARAM_, FILED_ }; - static std::vector CFG_OP_S = { "--", " = ", " => ", "LOAD ", "STORE ", "LOAD_REF ", "STORE_REF ", "REF ", "PARAM ", "RANGE ", "ENTRY ", "->", + static std::vector CFG_OP_S = { "--", " = ", " => ", "LOAD ", "STORE ", "LOAD_REF ", "STORE_REF ", "REF ", "PARAM ", "IO_PARAM ", "RANGE ", "ENTRY ", "->", " + ", " * ", " / ", " - ", " + ", "-", " ** ", " // ", "CAST ", "GOTO ", "IF_FALSE ", " >= ", " <= ", " > " , " < ", " == ", " != ", " eqv ", "CONTINUE", " || ", " && ", " ! ", @@ -228,7 +228,8 @@ namespace SAPFOR res = "LOAD " + resultVal + " " + arg1Val + CFG_OP_S[(int)operation] + arg2Val; break; case CFG_OP::REF: - case CFG_OP::PARAM: + case CFG_OP::PARAM: + case CFG_OP::IO_PARAM: res = CFG_OP_S[(int)operation] + arg1Val; break; case CFG_OP::CAST: diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp index 3971b8c..98b22a1 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/dead_code.cpp @@ -65,7 +65,8 @@ static void updateUseDefForInstruction(SAPFOR::BasicBlock* block, SAPFOR::Instru SAPFOR::CFG_OP::ENTRY, SAPFOR::CFG_OP::EXIT, 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()) @@ -245,7 +246,7 @@ public: bool forwardData(const map>& data) { bool inserted = false; - SAPFOR::BasicBlock* bb= getBlock(); + SAPFOR::BasicBlock* bb = getBlock(); set use, def; diff --git a/sapfor/experts/Sapfor_2017/_src/Utils/version.h b/sapfor/experts/Sapfor_2017/_src/Utils/version.h index 9492715..fb6d786 100644 --- a/sapfor/experts/Sapfor_2017/_src/Utils/version.h +++ b/sapfor/experts/Sapfor_2017/_src/Utils/version.h @@ -1,3 +1,3 @@ #pragma once -#define VERSION_SPF "2316" +#define VERSION_SPF "2317"