Merge pull request #7133 from lioncash/assembler
DSPAssembler: Minor changes
This commit is contained in:
commit
d8ba3034c3
|
@ -51,9 +51,7 @@ static const char* err_string[] = {"",
|
|||
"Number out of range",
|
||||
"Program counter out of range"};
|
||||
|
||||
DSPAssembler::DSPAssembler(const AssemblerSettings& settings)
|
||||
: m_cur_addr(0), m_cur_pass(0), m_current_param(0), settings_(settings)
|
||||
|
||||
DSPAssembler::DSPAssembler(const AssemblerSettings& settings) : m_settings(settings)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -69,10 +67,10 @@ bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
|
|||
return false;
|
||||
|
||||
// We now have the size of the output buffer
|
||||
if (m_totalSize <= 0)
|
||||
if (m_total_size <= 0)
|
||||
return false;
|
||||
|
||||
m_output_buffer.resize(m_totalSize);
|
||||
m_output_buffer.resize(m_total_size);
|
||||
|
||||
InitPass(2);
|
||||
if (!AssemblePass(text, 2))
|
||||
|
@ -82,18 +80,18 @@ bool DSPAssembler::Assemble(const std::string& text, std::vector<u16>& code,
|
|||
m_output_buffer.clear();
|
||||
m_output_buffer.shrink_to_fit();
|
||||
|
||||
last_error_str = "(no errors)";
|
||||
last_error = AssemblerError::OK;
|
||||
m_last_error_str = "(no errors)";
|
||||
m_last_error = AssemblerError::OK;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DSPAssembler::ShowError(AssemblerError err_code, const char* extra_info)
|
||||
{
|
||||
if (!settings_.force)
|
||||
failed = true;
|
||||
if (!m_settings.force)
|
||||
m_failed = true;
|
||||
|
||||
std::string error = StringFromFormat("%u : %s ", code_line, cur_line.c_str());
|
||||
std::string error = StringFromFormat("%u : %s ", m_code_line, m_cur_line.c_str());
|
||||
if (!extra_info)
|
||||
extra_info = "-";
|
||||
|
||||
|
@ -101,16 +99,16 @@ void DSPAssembler::ShowError(AssemblerError err_code, const char* extra_info)
|
|||
|
||||
if (m_current_param == 0)
|
||||
{
|
||||
error += StringFromFormat("ERROR: %s Line: %u : %s\n", error_string, code_line, extra_info);
|
||||
error += StringFromFormat("ERROR: %s Line: %u : %s\n", error_string, m_code_line, extra_info);
|
||||
}
|
||||
else
|
||||
{
|
||||
error += StringFromFormat("ERROR: %s Line: %u Param: %d : %s\n", error_string, code_line,
|
||||
error += StringFromFormat("ERROR: %s Line: %u Param: %d : %s\n", error_string, m_code_line,
|
||||
m_current_param, extra_info);
|
||||
}
|
||||
|
||||
last_error_str = std::move(error);
|
||||
last_error = err_code;
|
||||
m_last_error_str = std::move(error);
|
||||
m_last_error = err_code;
|
||||
}
|
||||
|
||||
static char* skip_spaces(char* ptr)
|
||||
|
@ -203,7 +201,7 @@ s32 DSPAssembler::ParseValue(const char* str)
|
|||
{
|
||||
// Lookup label
|
||||
u16 value;
|
||||
if (labels.GetLabelValue(ptr, &value))
|
||||
if (m_labels.GetLabelValue(ptr, &value))
|
||||
return value;
|
||||
if (m_cur_pass == 2)
|
||||
ShowError(AssemblerError::UnknownLabel, str);
|
||||
|
@ -335,7 +333,7 @@ u32 DSPAssembler::ParseExpression(const char* ptr)
|
|||
val = 0x10000 +
|
||||
(val &
|
||||
0xffff); // ATTENTION: avoid a terrible bug!!! number cannot write with '-' in sprintf
|
||||
fprintf(stderr, "WARNING: Number Underflow at Line: %d \n", code_line);
|
||||
fprintf(stderr, "WARNING: Number Underflow at Line: %d \n", m_code_line);
|
||||
}
|
||||
sprintf(d_buffer, "%d", val);
|
||||
}
|
||||
|
@ -435,8 +433,8 @@ const DSPOPCTemplate* DSPAssembler::FindOpcode(std::string name, size_t par_coun
|
|||
if (name[0] == 'C' && name[1] == 'W')
|
||||
return &cw;
|
||||
|
||||
const auto alias_iter = aliases.find(name);
|
||||
if (alias_iter != aliases.end())
|
||||
const auto alias_iter = m_aliases.find(name);
|
||||
if (alias_iter != m_aliases.end())
|
||||
name = alias_iter->second;
|
||||
|
||||
const DSPOPCTemplate* const info =
|
||||
|
@ -501,7 +499,7 @@ bool DSPAssembler::VerifyParams(const DSPOPCTemplate* opc, param_t* par, size_t
|
|||
if (type == OpcodeType::Extension)
|
||||
fprintf(stderr, "(ext) ");
|
||||
|
||||
fprintf(stderr, "%s (param %zu)", cur_line.c_str(), current_param);
|
||||
fprintf(stderr, "%s (param %zu)", m_cur_line.c_str(), current_param);
|
||||
ShowError(AssemblerError::InvalidRegister);
|
||||
}
|
||||
break;
|
||||
|
@ -511,7 +509,7 @@ bool DSPAssembler::VerifyParams(const DSPOPCTemplate* opc, param_t* par, size_t
|
|||
if (type == OpcodeType::Extension)
|
||||
fprintf(stderr, "(ext) ");
|
||||
|
||||
fprintf(stderr, "%s (param %zu)", cur_line.c_str(), current_param);
|
||||
fprintf(stderr, "%s (param %zu)", m_cur_line.c_str(), current_param);
|
||||
ShowError(AssemblerError::InvalidRegister);
|
||||
}
|
||||
break;
|
||||
|
@ -523,11 +521,11 @@ bool DSPAssembler::VerifyParams(const DSPOPCTemplate* opc, param_t* par, size_t
|
|||
|
||||
if (par[i].val >= 0x1e && par[i].val <= 0x1f)
|
||||
{
|
||||
fprintf(stderr, "%i : %s ", code_line, cur_line.c_str());
|
||||
fprintf(stderr, "%i : %s ", m_code_line, m_cur_line.c_str());
|
||||
fprintf(stderr,
|
||||
"WARNING: $ACM%d register used instead of $ACC%d register Line: %d "
|
||||
"Param: %zu Ext: %d\n",
|
||||
(par[i].val & 1), (par[i].val & 1), code_line, current_param,
|
||||
(par[i].val & 1), (par[i].val & 1), m_code_line, current_param,
|
||||
static_cast<int>(type));
|
||||
}
|
||||
else if (par[i].val >= 0x1c && par[i].val <= 0x1d)
|
||||
|
@ -535,7 +533,7 @@ bool DSPAssembler::VerifyParams(const DSPOPCTemplate* opc, param_t* par, size_t
|
|||
fprintf(
|
||||
stderr,
|
||||
"WARNING: $ACL%d register used instead of $ACC%d register Line: %d Param: %zu\n",
|
||||
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
||||
(par[i].val & 1), (par[i].val & 1), m_code_line, current_param);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -554,14 +552,14 @@ bool DSPAssembler::VerifyParams(const DSPOPCTemplate* opc, param_t* par, size_t
|
|||
fprintf(
|
||||
stderr,
|
||||
"WARNING: $ACL%d register used instead of $ACM%d register Line: %d Param: %zu\n",
|
||||
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
||||
(par[i].val & 1), (par[i].val & 1), m_code_line, current_param);
|
||||
}
|
||||
else if (par[i].val >= 0x20 && par[i].val <= 0x21)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING: $ACC%d register used instead of $ACM%d register Line: %d Param: %zu\n",
|
||||
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
||||
(par[i].val & 1), (par[i].val & 1), m_code_line, current_param);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -578,19 +576,19 @@ bool DSPAssembler::VerifyParams(const DSPOPCTemplate* opc, param_t* par, size_t
|
|||
|
||||
if (par[i].val >= 0x1e && par[i].val <= 0x1f)
|
||||
{
|
||||
fprintf(stderr, "%s ", cur_line.c_str());
|
||||
fprintf(stderr, "%s ", m_cur_line.c_str());
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING: $ACM%d register used instead of $ACL%d register Line: %d Param: %zu\n",
|
||||
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
||||
(par[i].val & 1), (par[i].val & 1), m_code_line, current_param);
|
||||
}
|
||||
else if (par[i].val >= 0x20 && par[i].val <= 0x21)
|
||||
{
|
||||
fprintf(stderr, "%s ", cur_line.c_str());
|
||||
fprintf(stderr, "%s ", m_cur_line.c_str());
|
||||
fprintf(
|
||||
stderr,
|
||||
"WARNING: $ACC%d register used instead of $ACL%d register Line: %d Param: %zu\n",
|
||||
(par[i].val & 1), (par[i].val & 1), code_line, current_param);
|
||||
(par[i].val & 1), (par[i].val & 1), m_code_line, current_param);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -731,23 +729,23 @@ void DSPAssembler::BuildCode(const DSPOPCTemplate* opc, param_t* par, u32 par_co
|
|||
|
||||
void DSPAssembler::InitPass(int pass)
|
||||
{
|
||||
failed = false;
|
||||
m_failed = false;
|
||||
if (pass == 1)
|
||||
{
|
||||
// Reset label table. Pre-populate with hw addresses and registers.
|
||||
labels.Clear();
|
||||
labels.RegisterDefaults();
|
||||
aliases.clear();
|
||||
aliases["S15"] = "SET15";
|
||||
aliases["S16"] = "SET16";
|
||||
aliases["S40"] = "SET40";
|
||||
m_labels.Clear();
|
||||
m_labels.RegisterDefaults();
|
||||
m_aliases.clear();
|
||||
m_aliases["S15"] = "SET15";
|
||||
m_aliases["S16"] = "SET16";
|
||||
m_aliases["S40"] = "SET40";
|
||||
}
|
||||
m_cur_addr = 0;
|
||||
m_totalSize = 0;
|
||||
cur_segment = SEGMENT_CODE;
|
||||
segment_addr[SEGMENT_CODE] = 0;
|
||||
segment_addr[SEGMENT_DATA] = 0;
|
||||
segment_addr[SEGMENT_OVERLAY] = 0;
|
||||
m_total_size = 0;
|
||||
m_cur_segment = SEGMENT_CODE;
|
||||
m_segment_addr[SEGMENT_CODE] = 0;
|
||||
m_segment_addr[SEGMENT_DATA] = 0;
|
||||
m_segment_addr[SEGMENT_OVERLAY] = 0;
|
||||
}
|
||||
|
||||
bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
||||
|
@ -756,22 +754,20 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
|
||||
std::istringstream fsrc(text);
|
||||
|
||||
// printf("%s: Pass %d\n", fname, pass);
|
||||
code_line = 0;
|
||||
m_code_line = 0;
|
||||
m_cur_pass = pass;
|
||||
|
||||
#define LINEBUF_SIZE 1024
|
||||
char line[LINEBUF_SIZE] = {0};
|
||||
while (!failed && !fsrc.fail() && !fsrc.eof())
|
||||
while (!m_failed && !fsrc.fail() && !fsrc.eof())
|
||||
{
|
||||
int opcode_size = 0;
|
||||
fsrc.getline(line, LINEBUF_SIZE);
|
||||
if (fsrc.fail())
|
||||
break;
|
||||
|
||||
cur_line = line;
|
||||
// printf("A: %s\n", line);
|
||||
code_line++;
|
||||
m_cur_line = line;
|
||||
m_code_line++;
|
||||
|
||||
param_t params[10] = {{0, P_NONE, nullptr}};
|
||||
param_t params_ext[10] = {{0, P_NONE, nullptr}};
|
||||
|
@ -897,7 +893,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
}
|
||||
}
|
||||
if (pass == 1)
|
||||
labels.RegisterLabel(label, lval);
|
||||
m_labels.RegisterLabel(label, lval);
|
||||
}
|
||||
|
||||
if (opcode == nullptr)
|
||||
|
@ -909,22 +905,22 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
if (params[0].type == P_STR)
|
||||
{
|
||||
std::string include_file_path;
|
||||
const u32 this_code_line = code_line;
|
||||
const u32 this_code_line = m_code_line;
|
||||
|
||||
if (include_dir.empty())
|
||||
if (m_include_dir.empty())
|
||||
{
|
||||
include_file_path = params[0].str;
|
||||
}
|
||||
else
|
||||
{
|
||||
include_file_path = include_dir + '/' + params[0].str;
|
||||
include_file_path = m_include_dir + '/' + params[0].str;
|
||||
}
|
||||
|
||||
std::string included_text;
|
||||
File::ReadFileToString(include_file_path, included_text);
|
||||
AssemblePass(included_text, pass);
|
||||
|
||||
code_line = this_code_line;
|
||||
m_code_line = this_code_line;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -936,7 +932,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
if (strcmp("INCDIR", opcode) == 0)
|
||||
{
|
||||
if (params[0].type == P_STR)
|
||||
include_dir = params[0].str;
|
||||
m_include_dir = params[0].str;
|
||||
else
|
||||
ShowError(AssemblerError::ExpectedParamStr);
|
||||
continue;
|
||||
|
@ -946,7 +942,7 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
{
|
||||
if (params[0].type == P_VAL)
|
||||
{
|
||||
m_totalSize = std::max(m_cur_addr, params[0].val);
|
||||
m_total_size = std::max(m_cur_addr, params[0].val);
|
||||
m_cur_addr = params[0].val;
|
||||
}
|
||||
else
|
||||
|
@ -978,12 +974,12 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
{
|
||||
if (params[0].type == P_STR)
|
||||
{
|
||||
segment_addr[cur_segment] = m_cur_addr;
|
||||
m_segment_addr[m_cur_segment] = m_cur_addr;
|
||||
if (strcmp("DATA", params[0].str) == 0)
|
||||
cur_segment = SEGMENT_DATA;
|
||||
m_cur_segment = SEGMENT_DATA;
|
||||
if (strcmp("CODE", params[0].str) == 0)
|
||||
cur_segment = SEGMENT_CODE;
|
||||
m_cur_addr = segment_addr[cur_segment];
|
||||
m_cur_segment = SEGMENT_CODE;
|
||||
m_cur_addr = m_segment_addr[m_cur_segment];
|
||||
}
|
||||
else
|
||||
ShowError(AssemblerError::ExpectedParamStr);
|
||||
|
@ -1030,9 +1026,9 @@ bool DSPAssembler::AssemblePass(const std::string& text, int pass)
|
|||
}
|
||||
|
||||
m_cur_addr += opcode_size;
|
||||
m_totalSize += opcode_size;
|
||||
m_total_size += opcode_size;
|
||||
};
|
||||
|
||||
return !failed;
|
||||
return !m_failed;
|
||||
}
|
||||
} // namespace DSP
|
||||
|
|
|
@ -61,8 +61,8 @@ public:
|
|||
bool Assemble(const std::string& text, std::vector<u16>& code,
|
||||
std::vector<int>* line_numbers = nullptr);
|
||||
|
||||
std::string GetErrorString() const { return last_error_str; }
|
||||
AssemblerError GetError() const { return last_error; }
|
||||
std::string GetErrorString() const { return m_last_error_str; }
|
||||
AssemblerError GetError() const { return m_last_error; }
|
||||
|
||||
private:
|
||||
struct param_t
|
||||
|
@ -104,26 +104,26 @@ private:
|
|||
|
||||
std::vector<u16> m_output_buffer;
|
||||
|
||||
std::string include_dir;
|
||||
std::string cur_line;
|
||||
std::string m_include_dir;
|
||||
std::string m_cur_line;
|
||||
|
||||
u32 m_cur_addr;
|
||||
int m_totalSize;
|
||||
u8 m_cur_pass;
|
||||
u32 m_cur_addr = 0;
|
||||
int m_total_size = 0;
|
||||
u8 m_cur_pass = 0;
|
||||
|
||||
LabelMap labels;
|
||||
LabelMap m_labels;
|
||||
|
||||
u32 code_line;
|
||||
bool failed;
|
||||
std::string last_error_str;
|
||||
AssemblerError last_error;
|
||||
u32 m_code_line = 0;
|
||||
bool m_failed = false;
|
||||
std::string m_last_error_str;
|
||||
AssemblerError m_last_error = AssemblerError::OK;
|
||||
|
||||
typedef std::map<std::string, std::string> AliasMap;
|
||||
AliasMap aliases;
|
||||
using AliasMap = std::map<std::string, std::string>;
|
||||
AliasMap m_aliases;
|
||||
|
||||
segment_t cur_segment;
|
||||
u32 segment_addr[SEGMENT_MAX];
|
||||
int m_current_param;
|
||||
const AssemblerSettings settings_;
|
||||
segment_t m_cur_segment = SEGMENT_CODE;
|
||||
u32 m_segment_addr[SEGMENT_MAX] = {};
|
||||
int m_current_param = 0;
|
||||
const AssemblerSettings m_settings;
|
||||
};
|
||||
} // namespace DSP
|
||||
|
|
Loading…
Reference in New Issue