Implement .equ directive for constant definitions
MODIFY
src/assembler/assembler.h
src/assembler/assembler.h
@@ -275,6 +275,7 @@ public:
// Per default, emit to .text until otherwise specified
setCurrentSegment(".text");
+ m_symbolMap.clear();
// Tokenize each source line and separate symbol from remainder of tokens
runPass(tokenizedLines, SourceProgram, pass0, programLines);
@@ -283,10 +284,8 @@ public:
runPass(expandedLines, SourceProgram, pass1, tokenizedLines);
/** Assemble. During assembly, we generate:
- * - symbolMap: Recoding the offset locations in the program of lines adorned with symbols
* - linkageMap: Recording offsets of instructions which require linkage with symbols
*/
- m_symbolMap.clear();
LinkRequests needsLinkage;
runPass(program, Program, pass2, expandedLines, needsLinkage);

MODIFY
src/assembler/gnudirectives.cpp
src/assembler/gnudirectives.cpp
@@ -19,6 +19,7 @@ DirectiveVec gnuDirectives() {
add_directive(directives, twoByteDirective());
add_directive(directives, fourByteDirective());
add_directive(directives, longDirective());
+ add_directive(directives, equDirective());
add_directive(directives, dataDirective());
add_directive(directives, textDirective());
@@ -151,5 +152,27 @@ Directive zeroDirective() {
return Directive(".zero", zeroFunctor);
}
+Directive equDirective() {
+ auto equFunctor = [](const AssemblerBase* assembler, const TokenizedSrcLine& line) -> HandleDirectiveRes {
+ if (line.tokens.length() != 2) {
+ return {Error(line.sourceLine, "Invalid number of arguments (expected 2)")};
+ }
+ bool ok;
+ int value = getImmediate(line.tokens.at(1), ok);
+ if (!ok) {
+ return {Error(line.sourceLine, "Invalid argument")};
+ }
+
+ auto err = assembler->addSymbol(line, line.tokens.at(0), value);
+ if (err) {
+ return err.value();
+ }
+
+ return HandleDirectiveRes(std::nullopt);
+ };
+ return Directive(".equ", equFunctor,
+ true /* Constants should be made available during ie. pseudo instruction expansion */);
+}
+
} // namespace Assembler
} // namespace Ripes

MODIFY
src/assembler/gnudirectives.h
src/assembler/gnudirectives.h
@@ -29,6 +29,7 @@ Directive longDirective();
Directive textDirective();
Directive dataDirective();
Directive bssDirective();
+Directive equDirective();
} // namespace Assembler
} // namespace Ripes

Implement .equ directive for constant definitions