DSPAssembler: Make error code enum an enum class

Prevents the error codes from littering the surrounding scope.
This commit is contained in:
Lioncash 2018-03-25 18:00:17 -04:00
parent 5c83e18fbd
commit e42db3594b
2 changed files with 69 additions and 69 deletions

View File

@ -83,12 +83,12 @@ bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
m_output_buffer.shrink_to_fit(); m_output_buffer.shrink_to_fit();
last_error_str = "(no errors)"; last_error_str = "(no errors)";
last_error = ERR_OK; last_error = AssemblerError::OK;
return true; return true;
} }
void DSPAssembler::ShowError(err_t err_code, const char* extra_info) void DSPAssembler::ShowError(AssemblerError err_code, const char* extra_info)
{ {
if (!settings_.force) if (!settings_.force)
failed = true; failed = true;
@ -97,15 +97,16 @@ void DSPAssembler::ShowError(err_t err_code, const char* extra_info)
if (!extra_info) if (!extra_info)
extra_info = "-"; extra_info = "-";
const char* const error_string = err_string[static_cast<size_t>(err_code)];
if (m_current_param == 0) if (m_current_param == 0)
{ {
error += error += StringFromFormat("ERROR: %s Line: %u : %s\n", error_string, code_line, extra_info);
StringFromFormat("ERROR: %s Line: %u : %s\n", err_string[err_code], code_line, extra_info);
} }
else else
{ {
error += StringFromFormat("ERROR: %s Line: %u Param: %d : %s\n", err_string[err_code], error += StringFromFormat("ERROR: %s Line: %u Param: %d : %s\n", error_string, code_line,
code_line, m_current_param, extra_info); m_current_param, extra_info);
} }
last_error_str = std::move(error); last_error_str = std::move(error);
@ -146,7 +147,7 @@ s32 DSPAssembler::ParseValue(const char* str)
if (ptr[i] >= '0' && ptr[i] <= '9') if (ptr[i] >= '0' && ptr[i] <= '9')
val += ptr[i] - '0'; val += ptr[i] - '0';
else else
ShowError(ERR_INCORRECT_DEC, str); ShowError(AssemblerError::IncorrectDecimal, str);
} }
} }
else else
@ -164,7 +165,7 @@ s32 DSPAssembler::ParseValue(const char* str)
else if (ptr[i] >= '0' && ptr[i] <= '9') else if (ptr[i] >= '0' && ptr[i] <= '9')
val += (ptr[i] - '0'); val += (ptr[i] - '0');
else else
ShowError(ERR_INCORRECT_HEX, str); ShowError(AssemblerError::IncorrectHex, str);
} }
break; break;
case '\'': // binary case '\'': // binary
@ -174,7 +175,7 @@ s32 DSPAssembler::ParseValue(const char* str)
if (ptr[i] >= '0' && ptr[i] <= '1') if (ptr[i] >= '0' && ptr[i] <= '1')
val += ptr[i] - '0'; val += ptr[i] - '0';
else else
ShowError(ERR_INCORRECT_BIN, str); ShowError(AssemblerError::IncorrectBinary, str);
} }
break; break;
default: default:
@ -195,7 +196,7 @@ s32 DSPAssembler::ParseValue(const char* str)
if (ptr[i] >= '0' && ptr[i] <= '9') if (ptr[i] >= '0' && ptr[i] <= '9')
val += ptr[i] - '0'; val += ptr[i] - '0';
else else
ShowError(ERR_INCORRECT_DEC, str); ShowError(AssemblerError::IncorrectDecimal, str);
} }
} }
else // Everything else is a label. else // Everything else is a label.
@ -205,7 +206,7 @@ s32 DSPAssembler::ParseValue(const char* str)
if (labels.GetLabelValue(ptr, &value)) if (labels.GetLabelValue(ptr, &value))
return value; return value;
if (m_cur_pass == 2) if (m_cur_pass == 2)
ShowError(ERR_UNKNOWN_LABEL, str); ShowError(AssemblerError::UnknownLabel, str);
} }
} }
if (negative) if (negative)
@ -268,7 +269,7 @@ char* DSPAssembler::FindBrackets(char* src, char* dst)
} }
} }
if (count) if (count)
ShowError(ERR_NO_MATCHING_BRACKETS); ShowError(AssemblerError::NoMatchingBrackets);
return nullptr; return nullptr;
} }
@ -442,17 +443,17 @@ const opc_t* DSPAssembler::FindOpcode(std::string name, size_t par_count, Opcode
type == OpcodeType::Primary ? FindOpInfoByName(name) : FindExtOpInfoByName(name); type == OpcodeType::Primary ? FindOpInfoByName(name) : FindExtOpInfoByName(name);
if (!info) if (!info)
{ {
ShowError(ERR_UNKNOWN_OPCODE); ShowError(AssemblerError::UnknownOpcode);
return nullptr; return nullptr;
} }
if (par_count < info->param_count) if (par_count < info->param_count)
{ {
ShowError(ERR_NOT_ENOUGH_PARAMETERS); ShowError(AssemblerError::NotEnoughParameters);
} }
else if (par_count > info->param_count) else if (par_count > info->param_count)
{ {
ShowError(ERR_TOO_MANY_PARAMETERS); ShowError(AssemblerError::TooManyParameters);
} }
return info; return info;
@ -500,7 +501,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
fprintf(stderr, "(ext) "); fprintf(stderr, "(ext) ");
fprintf(stderr, "%s (param %zu)", cur_line.c_str(), current_param); fprintf(stderr, "%s (param %zu)", cur_line.c_str(), current_param);
ShowError(ERR_INVALID_REGISTER); ShowError(AssemblerError::InvalidRegister);
} }
break; break;
case P_PRG: case P_PRG:
@ -510,7 +511,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
fprintf(stderr, "(ext) "); fprintf(stderr, "(ext) ");
fprintf(stderr, "%s (param %zu)", cur_line.c_str(), current_param); fprintf(stderr, "%s (param %zu)", cur_line.c_str(), current_param);
ShowError(ERR_INVALID_REGISTER); ShowError(AssemblerError::InvalidRegister);
} }
break; break;
case P_ACC: case P_ACC:
@ -536,7 +537,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
} }
else else
{ {
ShowError(ERR_WRONG_PARAMETER_ACC); ShowError(AssemblerError::WrongParameterExpectedAccumulator);
} }
} }
break; break;
@ -562,7 +563,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
} }
else else
{ {
ShowError(ERR_WRONG_PARAMETER_ACC); ShowError(AssemblerError::WrongParameterExpectedAccumulator);
} }
} }
break; break;
@ -591,7 +592,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
} }
else else
{ {
ShowError(ERR_WRONG_PARAMETER_ACC); ShowError(AssemblerError::WrongParameterExpectedAccumulator);
} }
} }
break; break;
@ -610,25 +611,25 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
case P_REG: case P_REG:
if (type == OpcodeType::Extension) if (type == OpcodeType::Extension)
fprintf(stderr, "(ext) "); fprintf(stderr, "(ext) ");
ShowError(ERR_EXPECTED_PARAM_REG); ShowError(AssemblerError::ExpectedParamReg);
break; break;
case P_MEM: case P_MEM:
if (type == OpcodeType::Extension) if (type == OpcodeType::Extension)
fprintf(stderr, "(ext) "); fprintf(stderr, "(ext) ");
ShowError(ERR_EXPECTED_PARAM_MEM); ShowError(AssemblerError::ExpectedParamMem);
break; break;
case P_VAL: case P_VAL:
if (type == OpcodeType::Extension) if (type == OpcodeType::Extension)
fprintf(stderr, "(ext) "); fprintf(stderr, "(ext) ");
ShowError(ERR_EXPECTED_PARAM_VAL); ShowError(AssemblerError::ExpectedParamVal);
break; break;
case P_IMM: case P_IMM:
if (type == OpcodeType::Extension) if (type == OpcodeType::Extension)
fprintf(stderr, "(ext) "); fprintf(stderr, "(ext) ");
ShowError(ERR_EXPECTED_PARAM_IMM); ShowError(AssemblerError::ExpectedParamImm);
break; break;
} }
ShowError(ERR_WRONG_PARAMETER); ShowError(AssemblerError::WrongParameter);
break; break;
} }
else if ((opc->params[i].type & 3) != 0 && (par[i].type & 3) != 0) else if ((opc->params[i].type & 3) != 0 && (par[i].type & 3) != 0)
@ -641,7 +642,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
if (value == 7) // value 7 por sbclr/sbset if (value == 7) // value 7 por sbclr/sbset
{ {
fprintf(stderr, "Value must be from 0x0 to 0x%x\n", value); fprintf(stderr, "Value must be from 0x0 to 0x%x\n", value);
ShowError(ERR_OUT_RANGE_NUMBER); ShowError(AssemblerError::NumberOutOfRange);
} }
else if (opc->params[i].type == P_MEM) else if (opc->params[i].type == P_MEM)
{ {
@ -650,7 +651,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
else else
fprintf(stderr, "Address value must be from 0x0 to 0x%x\n", value); fprintf(stderr, "Address value must be from 0x0 to 0x%x\n", value);
ShowError(ERR_OUT_RANGE_NUMBER); ShowError(AssemblerError::NumberOutOfRange);
} }
else if ((int)par[i].val < -((value >> 1) + 1)) else if ((int)par[i].val < -((value >> 1) + 1))
{ {
@ -661,7 +662,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
fprintf(stderr, "Value must be from -0x%x to 0x%x or 0x0 to 0x%x, is %i\n", fprintf(stderr, "Value must be from -0x%x to 0x%x or 0x0 to 0x%x, is %i\n",
(value >> 1) + 1, value >> 1, value, par[i].val); (value >> 1) + 1, value >> 1, value, par[i].val);
ShowError(ERR_OUT_RANGE_NUMBER); ShowError(AssemblerError::NumberOutOfRange);
} }
} }
else else
@ -671,7 +672,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
if (par[i].val > (unsigned)value) if (par[i].val > (unsigned)value)
{ {
fprintf(stderr, "Value must be from 0x%x to 0x%x, is %i\n", valueu, value, par[i].val); fprintf(stderr, "Value must be from 0x%x to 0x%x, is %i\n", valueu, value, par[i].val);
ShowError(ERR_OUT_RANGE_NUMBER); ShowError(AssemblerError::NumberOutOfRange);
} }
} }
else if (opc->params[i].type == P_MEM) else if (opc->params[i].type == P_MEM)
@ -686,7 +687,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
par[i].val); par[i].val);
else else
fprintf(stderr, "Address value must be minor of 0x%x\n", value + 1); fprintf(stderr, "Address value must be minor of 0x%x\n", value + 1);
ShowError(ERR_OUT_RANGE_NUMBER); ShowError(AssemblerError::NumberOutOfRange);
} }
} }
else else
@ -700,7 +701,7 @@ bool DSPAssembler::VerifyParams(const opc_t* opc, param_t* par, size_t count, Op
par[i].val); par[i].val);
else else
fprintf(stderr, "Value must be minor of 0x%x, is %i\n", value + 1, par[i].val); fprintf(stderr, "Value must be minor of 0x%x, is %i\n", value + 1, par[i].val);
ShowError(ERR_OUT_RANGE_NUMBER); ShowError(AssemblerError::NumberOutOfRange);
} }
} }
} }
@ -931,7 +932,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
} }
else else
{ {
ShowError(ERR_EXPECTED_PARAM_STR); ShowError(AssemblerError::ExpectedParamStr);
} }
continue; continue;
} }
@ -941,7 +942,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
if (params[0].type == P_STR) if (params[0].type == P_STR)
include_dir = params[0].str; include_dir = params[0].str;
else else
ShowError(ERR_EXPECTED_PARAM_STR); ShowError(AssemblerError::ExpectedParamStr);
continue; continue;
} }
@ -954,7 +955,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
} }
else else
{ {
ShowError(ERR_EXPECTED_PARAM_VAL); ShowError(AssemblerError::ExpectedParamVal);
} }
continue; continue;
} }
@ -967,12 +968,12 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
{ {
std::string msg = StringFromFormat("WARNPC at 0x%04x, expected 0x%04x or less", std::string msg = StringFromFormat("WARNPC at 0x%04x, expected 0x%04x or less",
m_cur_addr, params[0].val); m_cur_addr, params[0].val);
ShowError(ERR_OUT_RANGE_PC, msg.c_str()); ShowError(AssemblerError::PCOutOfRange, msg.c_str());
} }
} }
else else
{ {
ShowError(ERR_EXPECTED_PARAM_VAL); ShowError(AssemblerError::ExpectedParamVal);
} }
continue; continue;
} }
@ -989,7 +990,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
m_cur_addr = segment_addr[cur_segment]; m_cur_addr = segment_addr[cur_segment];
} }
else else
ShowError(ERR_EXPECTED_PARAM_STR); ShowError(AssemblerError::ExpectedParamStr);
continue; continue;
} }
@ -1012,15 +1013,15 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
} }
else if (params_count_ext) else if (params_count_ext)
{ {
ShowError(ERR_EXT_PAR_NOT_EXT); ShowError(AssemblerError::ExtensionParamsOnNonExtendableOpcode);
} }
} }
else else
{ {
if (opcode_ext) if (opcode_ext)
ShowError(ERR_EXT_CANT_EXTEND_OPCODE); ShowError(AssemblerError::CantExtendOpcode);
if (params_count_ext) if (params_count_ext)
ShowError(ERR_EXT_PAR_NOT_EXT); ShowError(AssemblerError::ExtensionParamsOnNonExtendableOpcode);
} }
if (pass == 2) if (pass == 2)

View File

@ -18,32 +18,32 @@
namespace DSP namespace DSP
{ {
enum err_t enum class AssemblerError
{ {
ERR_OK = 0, OK,
ERR_UNKNOWN, Unknown,
ERR_UNKNOWN_OPCODE, UnknownOpcode,
ERR_NOT_ENOUGH_PARAMETERS, NotEnoughParameters,
ERR_TOO_MANY_PARAMETERS, TooManyParameters,
ERR_WRONG_PARAMETER, WrongParameter,
ERR_EXPECTED_PARAM_STR, ExpectedParamStr,
ERR_EXPECTED_PARAM_VAL, ExpectedParamVal,
ERR_EXPECTED_PARAM_REG, ExpectedParamReg,
ERR_EXPECTED_PARAM_MEM, ExpectedParamMem,
ERR_EXPECTED_PARAM_IMM, ExpectedParamImm,
ERR_INCORRECT_BIN, IncorrectBinary,
ERR_INCORRECT_HEX, IncorrectHex,
ERR_INCORRECT_DEC, IncorrectDecimal,
ERR_LABEL_EXISTS, LabelAlreadyExists,
ERR_UNKNOWN_LABEL, UnknownLabel,
ERR_NO_MATCHING_BRACKETS, NoMatchingBrackets,
ERR_EXT_CANT_EXTEND_OPCODE, CantExtendOpcode,
ERR_EXT_PAR_NOT_EXT, ExtensionParamsOnNonExtendableOpcode,
ERR_WRONG_PARAMETER_ACC, WrongParameterExpectedAccumulator,
ERR_WRONG_PARAMETER_MID_ACC, WrongParameterExpectedMidAccumulator,
ERR_INVALID_REGISTER, InvalidRegister,
ERR_OUT_RANGE_NUMBER, NumberOutOfRange,
ERR_OUT_RANGE_PC, PCOutOfRange,
}; };
// Unless you want labels to carry over between files, you probably // Unless you want labels to carry over between files, you probably
@ -62,7 +62,7 @@ public:
std::vector<int>* line_numbers = nullptr); std::vector<int>* line_numbers = nullptr);
std::string GetErrorString() const { return last_error_str; } std::string GetErrorString() const { return last_error_str; }
err_t GetError() const { return last_error; } AssemblerError GetError() const { return last_error; }
private: private:
struct param_t struct param_t
{ {
@ -94,8 +94,7 @@ private:
void InitPass(int pass); void InitPass(int pass);
bool AssemblePass(const std::string& text, int pass); bool AssemblePass(const std::string& text, int pass);
void ShowError(err_t err_code, const char* extra_info = nullptr); void ShowError(AssemblerError err_code, const char* extra_info = nullptr);
// void ShowWarning(err_t err_code, const char *extra_info = nullptr);
char* FindBrackets(char* src, char* dst); char* FindBrackets(char* src, char* dst);
const opc_t* FindOpcode(std::string name, size_t par_count, OpcodeType type); const opc_t* FindOpcode(std::string name, size_t par_count, OpcodeType type);
@ -116,7 +115,7 @@ private:
u32 code_line; u32 code_line;
bool failed; bool failed;
std::string last_error_str; std::string last_error_str;
err_t last_error; AssemblerError last_error;
typedef std::map<std::string, std::string> AliasMap; typedef std::map<std::string, std::string> AliasMap;
AliasMap aliases; AliasMap aliases;