From 94095c7890f491d7023b1a69b3f088be8c05e898 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Wed, 17 Jul 2019 21:03:36 +0200 Subject: Enable CLI version to use simple assembler. Signed-off-by: Pavel Pisa --- qtmips_cli/main.cpp | 49 ++++++++++++++++++++++++++++-- qtmips_cli/msgreport.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++ qtmips_cli/msgreport.h | 58 ++++++++++++++++++++++++++++++++++++ qtmips_cli/qtmips_cli.pro | 6 ++-- qtmips_gui/mainwindow.cpp | 2 +- 5 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 qtmips_cli/msgreport.cpp create mode 100644 qtmips_cli/msgreport.h diff --git a/qtmips_cli/main.cpp b/qtmips_cli/main.cpp index 82296ed..08696b4 100644 --- a/qtmips_cli/main.cpp +++ b/qtmips_cli/main.cpp @@ -35,11 +35,15 @@ #include #include +#include +#include #include #include #include #include "tracer.h" #include "reporter.h" +#include "msgreport.h" +#include "simpleasm.h" using namespace machine; using namespace std; @@ -49,9 +53,10 @@ void create_parser(QCommandLineParser &p) { p.addHelpOption(); p.addVersionOption(); - p.addPositionalArgument("FILE", "Input ELF executable file"); + p.addPositionalArgument("FILE", "Input ELF executable file or assembler source"); // p.addOptions({}); available only from Qt 5.4+ + p.addOption({"asm", "Treat provided file argument as assembler source."}); p.addOption({"pipelined", "Configure CPU to use five stage pipeline."}); p.addOption({"no-delay-slot", "Disable jump delay slot."}); p.addOption({{"trace-fetch", "tr-fetch"}, "Trace fetched instruction (for both pipelined and not core)."}); @@ -315,18 +320,50 @@ void load_ranges(QtMipsMachine &machine, const QStringList &ranges) { } } +bool assemble(QtMipsMachine &machine, MsgReport &msgrep, QString filename) { + SymbolTableDb symtab(machine.symbol_table_rw(true)); + machine::MemoryAccess *mem = machine.physical_address_space_rw(); + if (mem == nullptr) { + return false; + } + machine.cache_sync(); + SimpleAsm sasm; + + sasm.connect(&sasm, SIGNAL(report_message(messagetype::Type,QString,int,int,QString,QString)), + &msgrep, SLOT(report_message(messagetype::Type,QString,int,int,QString,QString))); + + sasm.setup(mem, &symtab, 0x80020000); + + QFile input_file(filename); + if (!input_file.open(QIODevice::ReadOnly)) { + cout << "cannot open filename " << filename.toLocal8Bit().data() << endl; + return false; + } + + QTextStream in(&input_file); + for (int ln = 1; !in.atEnd(); ln++) { + QString line = in.readLine(); + sasm.process_line(line, filename, ln); + } + input_file.close(); + + return sasm.finish(); +} + int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); app.setApplicationName("qtmips_cli"); - app.setApplicationVersion("0.1"); + app.setApplicationVersion("0.7"); QCommandLineParser p; create_parser(p); p.process(app); + bool asm_source = p.isSet("asm"); + MachineConfig cc; configure_machine(p, cc); - QtMipsMachine machine(cc, true); + QtMipsMachine machine(cc, !asm_source, !asm_source); Tracer tr(&machine); configure_tracer(p, tr); @@ -334,6 +371,12 @@ int main(int argc, char *argv[]) { Reporter r(&app, &machine); configure_reporter(p, r, machine.symbol_table()); + if (asm_source) { + MsgReport msgrep(&app); + if (!assemble(machine, msgrep, p.positionalArguments()[0])) + exit(1); + } + load_ranges(machine, p.values("load-range")); machine.play(); diff --git a/qtmips_cli/msgreport.cpp b/qtmips_cli/msgreport.cpp new file mode 100644 index 0000000..bd5f478 --- /dev/null +++ b/qtmips_cli/msgreport.cpp @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#include "msgreport.h" +#include +#include +#include +#include + +using namespace std; + +MsgReport::MsgReport(QCoreApplication *app) : Super(app) { + +} + +void MsgReport::report_message(messagetype::Type type, QString file, + int line, int column, QString text, QString hint) { + (void)hint; + + QString typestr = "error"; + switch (type) { + case messagetype::ERROR: + typestr = "error"; + break; + case messagetype::WARNING: + typestr = "warning"; + break; + case messagetype::INFO: + typestr = "info"; + break; + default: + return; + } + + cout << file.toLocal8Bit().data() << ":"; + if (line != 0) + cout << line << ":"; + if (column != 0) + cout << column << ":"; + + cout << typestr.toLocal8Bit().data() << ":"; + cout << text.toLocal8Bit().data(); + cout << endl; +} diff --git a/qtmips_cli/msgreport.h b/qtmips_cli/msgreport.h new file mode 100644 index 0000000..b1b8f70 --- /dev/null +++ b/qtmips_cli/msgreport.h @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#ifndef MSGREPORT_H +#define MSGREPORT_H + +#include +#include +#include +#include +#include "messagetype.h" + +class MsgReport : public QObject { + Q_OBJECT + + using Super = QObject; + +public: + MsgReport(QCoreApplication *app); + +public slots: + void report_message(messagetype::Type type, QString file, int line, int column, QString text, QString hint); + +}; + +#endif // MSGREPORT_H diff --git a/qtmips_cli/qtmips_cli.pro b/qtmips_cli/qtmips_cli.pro index 6cc1909..ea692ec 100644 --- a/qtmips_cli/qtmips_cli.pro +++ b/qtmips_cli/qtmips_cli.pro @@ -33,8 +33,10 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ tracer.cpp \ - reporter.cpp + reporter.cpp \ + msgreport.cpp HEADERS += \ tracer.h \ - reporter.h + reporter.h \ + msgreport.h diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp index 19299c0..a201b24 100644 --- a/qtmips_gui/mainwindow.cpp +++ b/qtmips_gui/mainwindow.cpp @@ -701,7 +701,7 @@ void MainWindow::message_selected(messagetype::Type type, QString file, int line void MainWindow::compile_source() { SymbolTableDb symtab(machine->symbol_table_rw(true)); - bool error_occured; + bool error_occured = false; if (current_srceditor == nullptr) return; if (machine == nullptr) { -- cgit v1.2.3