From a385be02219abc1fd751908e559286dc1a370e12 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Thu, 22 Aug 2019 12:18:33 +0200 Subject: Provide support for include directive in simple assembler. Signed-off-by: Pavel Pisa --- qtmips_asm/simpleasm.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- qtmips_asm/simpleasm.h | 2 ++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/qtmips_asm/simpleasm.cpp b/qtmips_asm/simpleasm.cpp index 0deea6f..ad720b9 100644 --- a/qtmips_asm/simpleasm.cpp +++ b/qtmips_asm/simpleasm.cpp @@ -34,6 +34,9 @@ ******************************************************************************/ #include +#include +#include +#include #include "simpleasm.h" @@ -122,8 +125,14 @@ bool SimpleAsm::process_line(QString line, QString filename, if (!final) ch = line.at(pos); if (!in_quotes) { - if (ch == '#') - final = true; + if (ch == '#') { + if (line.mid(pos).startsWith("#include")) { + if ((line.count() > pos + 8) && !line.at(pos + 8).isSpace()) + final = true; + } else { + final = true; + } + } if (ch == ';') final = true; if (ch == '/') { @@ -226,6 +235,49 @@ bool SimpleAsm::process_line(QString line, QString filename, return true; } + if (op == "#INCLUDE") { + bool res = true; + QString incname; + if ((operands.count() != 1) || operands.at(0).isEmpty()) { + error = "the single file has to be specified for include"; + emit report_message(messagetype::MSG_ERROR, filename, line_number, 0, error, ""); + error_occured = true; + if (error_ptr != nullptr) + *error_ptr = error; + return false; + } + incname = operands.at(0); + if (incname.at(0) == '"') + incname = incname.mid(1, incname.count() - 2); + QFileInfo fi(QFileInfo(filename).dir(), incname); + incname = fi.filePath(); + include_stack.append(filename); + if (include_stack.contains(incname)) { + error = QString("recursive include of file: \"%1\"").arg(incname); + res = false; + } else { + QFile incfile(incname); + if (!incfile.open(QFile::ReadOnly | QFile::Text)) { + error = QString("cannot open file: \"%1\"").arg(incname); + res = false; + } else for (int ln = 1; !incfile.atEnd(); ln++) { + QString line = incfile.readLine(); + if (!process_line(line, incname, ln, error_ptr)) { + res = false; + break; + } + } + } + if (!res) { + emit report_message(messagetype::MSG_ERROR, filename, line_number, 0, error, ""); + error_occured = true; + if (error_ptr != nullptr) + if (error_ptr->isEmpty()) + *error_ptr = error; + } + include_stack.removeLast(); + return res; + } if ((op == ".DATA") || (op == ".TEXT") || (op == ".GLOBL") || (op == ".END") || (op == ".ENT")) { @@ -388,6 +440,9 @@ bool SimpleAsm::process_line(QString line, QString filename, case '\\': ch = '\\'; break; + case '0': + ch = 0x00; + break; case 'r': ch = 0x0d; break; diff --git a/qtmips_asm/simpleasm.h b/qtmips_asm/simpleasm.h index 773aa39..f97904d 100644 --- a/qtmips_asm/simpleasm.h +++ b/qtmips_asm/simpleasm.h @@ -37,6 +37,7 @@ #define SIMPLEASM_H #include +#include #include "fixmatheval.h" #include "qtmipsmachine.h" #include "messagetype.h" @@ -72,6 +73,7 @@ public: int line_number = 0, QString *error_ptr = nullptr); bool finish(QString *error_ptr = nullptr); private: + QStringList include_stack; bool error_occured; bool fatal_occured; SymbolTableDb *symtab; -- cgit v1.2.3