Implement syntax highlighting for RISC-V assembly language
MODIFY
RISC-V-sim.pro
RISC-V-sim.pro
@@ -26,7 +26,8 @@ SOURCES += src/main.cpp \
src/memoryview.cpp \
src/gotocombobox.cpp \
src/addressdialog.cpp \
- src/codeeditor.cpp
+ src/codeeditor.cpp \
+ src/asmhighlighter.cpp
HEADERS += \
src/parser.h \
@@ -50,7 +51,8 @@ HEADERS += \
src/gotocombobox.h \
src/addressdialog.h \
src/addressdialog.h \
- src/codeeditor.h
+ src/codeeditor.h \
+ src/asmhighlighter.h
FORMS += \
src/mainwindow.ui \

ADD
src/asmhighlighter.cpp
+#include "asmhighlighter.h"
+
+#include <QRegularExpressionMatchIterator>
+
+#include "defines.h"
+
+AsmHighlighter::AsmHighlighter(QTextDocument *parent)
+ : QSyntaxHighlighter(parent) {
+
+ HighlightingRule rule;
+
+ // Create rules for name-specific registers
+ regFormat.setForeground(QColor(Colors::FoundersRock));
+ QStringList registerPatterns;
+ registerPatterns << "\\bzero\\b"
+ << "\\bra\\b"
+ << "\\bsp\\b"
+ << "\\bgp\\b"
+ << "\\btp\\b"
+ << "\\bfp\\b";
+ for (const auto &pattern : registerPatterns) {
+ rule.pattern = QRegularExpression(pattern);
+ rule.format = regFormat;
+ m_highlightingRules.append(rule);
+ }
+
+ // Create match cases for instructions
+ QStringList instructionPatterns;
+ instructionPatterns << "\\bla\\b"
+ << "\\brd\\b"
+ << "\\blw\\b"
+ << "\\blh\\b"
+ << "\\blb\\b"
+ << "\\bsb\\b"
+ << "\\bsh\\b"
+ << "\\bsw\\b"
+ << "\\bnop\\b"
+ << "\\bli\\b"
+ << "\\bmv\\b"
+ << "\\bnot\\b"
+ << "\\bneg\\b"
+ << "\\bnegw\\b"
+ << "\\bsext.w\\b"
+ << "\\bseqz\\b"
+ << "\\bsnez\\b"
+ << "\\bsltz\\b"
+ << "\\bsgtz\\b"
+ << "\\bbegz\\b"
+ << "\\bbnez\\b"
+ << "\\bblez\\b"
+ << "\\bbgez\\b"
+ << "\\bbltz\\b"
+ << "\\bbgtz\\b"
+ << "\\bbgt\\b"
+ << "\\bble\\b"
+ << "\\bbgtu\\b"
+ << "\\bbleu\\b"
+ << "\\bj\\b"
+ << "\\bjal\\b"
+ << "\\bjr\\b"
+ << "\\bjalr\\b"
+ << "\\bret\\b"
+ << "\\bcall\\b"
+ << "\\btail\\b"
+ << "\\bfence\\b"
+ << "\\brdinstret\\b"
+ << "\\brdcycle\\b"
+ << "\\brdtime\\b"
+ << "\\bcsrr\\b"
+ << "\\bcsrw\\b"
+ << "\\bcsrs\\b"
+ << "\\bcsrc\\b"
+ << "\\bcsrwi\\b"
+ << "\\bcsrsi\\b"
+ << "\\bcsrci\\b"
+ << "\\bauipc\\b"
+ << "\\baddi\\b"
+ << "\\baddi\\b"
+ << "\\bxori\\b"
+ << "\\bsub\\b"
+ << "\\bsubw\\b"
+ << "\\baddiw\\b"
+ << "\\bsltiu\\b"
+ << "\\bsltu\\b"
+ << "\\bslt\\b"
+ << "\\bbeq\\b"
+ << "\\bbne\\b"
+ << "\\bbge\\b"
+ << "\\bblt\\b"
+ << "\\bbltu\\b"
+ << "\\bbgeu\\b"
+ << "\\bsrli\\b"
+ << "\\bslli\\b";
+ instrFormat.setForeground(QColor(Colors::BerkeleyBlue));
+ for (const auto &pattern : instructionPatterns) {
+ rule.pattern = QRegularExpression(pattern);
+ rule.format = instrFormat;
+ m_highlightingRules.append(rule);
+ }
+
+ // Create rules for registers that can easily be regex matched
+ // (saved, temporary and argument registers)
+ rule.pattern = QRegularExpression("\\b[(a|s|t)][0-9]{1,2}");
+ rule.format = regFormat;
+ m_highlightingRules.append(rule);
+
+ // Create immediate hightlighting rule
+ immFormat.setForeground(QColor(Qt::darkGreen));
+ rule.pattern = QRegularExpression("(?<![A-Za-z])[-+]?\\d+");
+ rule.format = immFormat;
+ m_highlightingRules.append(rule);
+
+ // Create comment highlighting rule
+ commentFormat.setForeground(QColor(Colors::Medalist));
+ rule.pattern = QRegularExpression("[#]+.*");
+ rule.format = commentFormat;
+ m_highlightingRules.append(rule);
+}
+
+void AsmHighlighter::highlightBlock(const QString &text) {
+ foreach (const HighlightingRule &rule, m_highlightingRules) {
+ QRegularExpressionMatchIterator matchIterator =
+ rule.pattern.globalMatch(text);
+ while (matchIterator.hasNext()) {
+ QRegularExpressionMatch match = matchIterator.next();
+ setFormat(match.capturedStart(), match.capturedLength(), rule.format);
+ }
+ }
+}

ADD
src/asmhighlighter.h
+#ifndef ASMHIGHLIGHTER_H
+#define ASMHIGHLIGHTER_H
+
+#include <QRegularExpression>
+#include <QSyntaxHighlighter>
+
+/* Class for highlighting RISC-V assembly code
+ Based on QT's rich text syntax highlighter example.
+ http://doc.qt.io/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html
+
+ Matches instruction names directly, and register aliases/true name.
+ Matches immediate values by regex*/
+
+class AsmHighlighter : public QSyntaxHighlighter {
+ Q_OBJECT
+public:
+ explicit AsmHighlighter(QTextDocument *parent = nullptr);
+
+ void highlightBlock(const QString &text);
+
+signals:
+
+private:
+ struct HighlightingRule {
+ QRegularExpression pattern;
+ QTextCharFormat format;
+ };
+
+ QVector<HighlightingRule> m_highlightingRules;
+
+ // Format type for each matching case
+ QTextCharFormat regFormat;
+ QTextCharFormat instrFormat;
+ QTextCharFormat immFormat;
+ QTextCharFormat commentFormat;
+
+public slots:
+};
+
+#endif // ASMHIGHLIGHTER_H

MODIFY
src/codeeditor.cpp
src/codeeditor.cpp
@@ -38,6 +38,11 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
// set event filter for catching scroll events
installEventFilter(this);
+
+ // Set syntax highlighter
+ m_highlighter = new AsmHighlighter(document());
+
+ setWordWrapMode(QTextOption::NoWrap);
}
int CodeEditor::lineNumberAreaWidth() {

MODIFY
src/codeeditor.h
src/codeeditor.h
#include <QScrollBar>
#include <QTimer>
+#include "asmhighlighter.h"
+
#include <set>
// Extended version of Qt's CodeEditor example
@@ -34,6 +36,7 @@ private slots:
void updateSidebar(const QRect &, int);
private:
+ AsmHighlighter *m_highlighter;
LineNumberArea *m_lineNumberArea;
BreakpointArea *m_breakpointArea;
int m_sidebarWidth;

Implement syntax highlighting for RISC-V assembly language