From 4e0f794c23d5b5d06296b943241b1961b935f15d Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Fri, 26 Jul 2024 20:12:51 -0400 Subject: [PATCH] More refactoring and renaming --- include/asm/charmap.hpp | 2 +- include/asm/output.hpp | 2 +- include/asm/section.hpp | 6 +++--- src/asm/charmap.cpp | 7 ++++--- src/asm/main.cpp | 4 ++-- src/asm/output.cpp | 29 ++++++++++------------------- src/asm/parser.y | 22 +++++++--------------- src/asm/section.cpp | 24 ++++++++++++------------ src/util.cpp | 5 ++--- 9 files changed, 42 insertions(+), 59 deletions(-) diff --git a/include/asm/charmap.hpp b/include/asm/charmap.hpp index 5f61d3df..767ea00a 100644 --- a/include/asm/charmap.hpp +++ b/include/asm/charmap.hpp @@ -16,7 +16,7 @@ void charmap_Push(); void charmap_Pop(); void charmap_Add(std::string const &mapping, uint8_t value); bool charmap_HasChar(std::string const &input); -void charmap_Convert(std::string const &input, std::vector &output); +std::vector charmap_Convert(std::string const &input); size_t charmap_ConvertNext(std::string_view &input, std::vector *output); #endif // RGBDS_ASM_CHARMAP_HPP diff --git a/include/asm/output.hpp b/include/asm/output.hpp index cacd1ef2..2e161c87 100644 --- a/include/asm/output.hpp +++ b/include/asm/output.hpp @@ -12,7 +12,7 @@ struct Expression; struct FileStackNode; -extern std::string objectName; +extern std::string objectFileName; void out_RegisterNode(std::shared_ptr node); void out_SetFileName(std::string const &name); diff --git a/include/asm/section.hpp b/include/asm/section.hpp index a51e6d38..0f1c5368 100644 --- a/include/asm/section.hpp +++ b/include/asm/section.hpp @@ -84,9 +84,9 @@ void sect_EndUnion(); void sect_CheckUnionClosed(); void sect_AbsByte(uint8_t b); -void sect_AbsByteGroup(uint8_t const *s, size_t length); -void sect_AbsWordGroup(uint8_t const *s, size_t length); -void sect_AbsLongGroup(uint8_t const *s, size_t length); +void sect_AbsByteString(std::vector const &s); +void sect_AbsWordString(std::vector const &s); +void sect_AbsLongString(std::vector const &s); void sect_Skip(uint32_t skip, bool ds); void sect_RelByte(Expression &expr, uint32_t pcShift); void sect_RelBytes(uint32_t n, std::vector &exprs); diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index e0d4ee43..adec7323 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -127,10 +127,11 @@ bool charmap_HasChar(std::string const &input) { return charmap.nodes[nodeIdx].isTerminal; } -void charmap_Convert(std::string const &input, std::vector &output) { - std::string_view inputView = input; - while (charmap_ConvertNext(inputView, &output)) +std::vector charmap_Convert(std::string const &input) { + std::vector output; + for (std::string_view inputView = input; charmap_ConvertNext(inputView, &output);) ; + return output; } size_t charmap_ConvertNext(std::string_view &input, std::vector *output) { diff --git a/src/asm/main.cpp b/src/asm/main.cpp index 6187d48f..02ef2950 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -287,8 +287,8 @@ int main(int argc, char *argv[]) { } } - if (targetFileName.empty() && !objectName.empty()) - targetFileName = objectName; + if (targetFileName.empty() && !objectFileName.empty()) + targetFileName = objectFileName; if (argc == musl_optind) { fputs( diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 59ac3cc2..9f76d909 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -27,7 +27,7 @@ struct Assertion { std::string message; }; -std::string objectName; +std::string objectFileName; // List of symbols to put in the object file static std::vector objectSymbols; @@ -36,7 +36,6 @@ static std::deque assertions; static std::deque> fileStackNodes; -// Write a long to a file (little-endian) static void putlong(uint32_t n, FILE *file) { uint8_t bytes[] = { (uint8_t)n, @@ -47,7 +46,6 @@ static void putlong(uint32_t n, FILE *file) { fwrite(bytes, 1, sizeof(bytes), file); } -// Write a NUL-terminated string to a file static void putstring(std::string const &s, FILE *file) { fputs(s.c_str(), file); putc('\0', file); @@ -72,7 +70,6 @@ static uint32_t getSectIDIfAny(Section *sect) { fatalerror("Unknown section '%s'\n", sect->name.c_str()); } -// Write a patch to a file static void writepatch(Patch const &patch, FILE *file) { assume(patch.src->ID != (uint32_t)-1); putlong(patch.src->ID, file); @@ -85,7 +82,6 @@ static void writepatch(Patch const &patch, FILE *file) { fwrite(patch.rpn.data(), 1, patch.rpn.size(), file); } -// Write a section to a file static void writesection(Section const §, FILE *file) { putstring(sect.name, file); @@ -110,7 +106,6 @@ static void writesection(Section const §, FILE *file) { } } -// Write a symbol to a file static void writesymbol(Symbol const &sym, FILE *file) { putstring(sym.name, file); if (!sym.isDefined()) { @@ -258,7 +253,6 @@ static void initpatch(Patch &patch, uint32_t type, Expression const &expr, uint3 } } -// Create a new patch (includes the rpn expr) void out_CreatePatch(uint32_t type, Expression const &expr, uint32_t ofs, uint32_t pcShift) { // Add the patch to the list Patch &patch = currentSection->patches.emplace_front(); @@ -271,7 +265,6 @@ void out_CreatePatch(uint32_t type, Expression const &expr, uint32_t ofs, uint32 patch.pcOffset -= pcShift; } -// Creates an assert that will be written to the object file void out_CreateAssert( AssertionType type, Expression const &expr, std::string const &message, uint32_t ofs ) { @@ -302,20 +295,19 @@ static void writeFileStackNode(FileStackNode const &node, FILE *file) { } } -// Write an object file void out_WriteObject() { - if (objectName.empty()) + if (objectFileName.empty()) return; FILE *file; - if (objectName != "-") { - file = fopen(objectName.c_str(), "wb"); + if (objectFileName != "-") { + file = fopen(objectFileName.c_str(), "wb"); } else { - objectName = ""; + objectFileName = ""; file = fdopen(STDOUT_FILENO, "wb"); } if (!file) - err("Failed to open object file '%s'", objectName.c_str()); + err("Failed to open object file '%s'", objectFileName.c_str()); Defer closeFile{[&] { fclose(file); }}; // Also write symbols that weren't written above @@ -355,11 +347,10 @@ void out_WriteObject() { writeassert(assert, file); } -// Set the object filename void out_SetFileName(std::string const &name) { - if (!objectName.empty()) - warnx("Overriding output filename %s", objectName.c_str()); - objectName = name; + if (!objectFileName.empty()) + warnx("Overriding output filename %s", objectFileName.c_str()); + objectFileName = name; if (verbose) - printf("Output filename %s\n", objectName.c_str()); + printf("Output filename %s\n", objectFileName.c_str()); } diff --git a/src/asm/parser.y b/src/asm/parser.y index fceb0f31..c1b66f6a 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -1170,10 +1170,8 @@ constlist_8bit_entry: sect_RelByte($1, 0); } | string { - std::vector output; - - charmap_Convert($1, output); - sect_AbsByteGroup(output.data(), output.size()); + std::vector output = charmap_Convert($1); + sect_AbsByteString(output); } ; @@ -1187,10 +1185,8 @@ constlist_16bit_entry: sect_RelWord($1, 0); } | string { - std::vector output; - - charmap_Convert($1, output); - sect_AbsWordGroup(output.data(), output.size()); + std::vector output = charmap_Convert($1); + sect_AbsWordString(output); } ; @@ -1204,10 +1200,8 @@ constlist_32bit_entry: sect_RelLong($1, 0); } | string { - std::vector output; - - charmap_Convert($1, output); - sect_AbsLongGroup(output.data(), output.size()); + std::vector output = charmap_Convert($1); + sect_AbsLongString(output); } ; @@ -1256,9 +1250,7 @@ relocexpr: $$ = std::move($1); } | string { - std::vector output; - - charmap_Convert($1, output); + std::vector output = charmap_Convert($1); $$.makeNumber(str2int2(output)); } ; diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 38278f2b..4100b02f 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -671,34 +671,34 @@ void sect_AbsByte(uint8_t b) { writebyte(b); } -void sect_AbsByteGroup(uint8_t const *s, size_t length) { +void sect_AbsByteString(std::vector const &s) { if (!checkcodesection()) return; - if (!reserveSpace(length)) + if (!reserveSpace(s.size())) return; - while (length--) - writebyte(*s++); + for (uint8_t v : s) + writebyte(v); } -void sect_AbsWordGroup(uint8_t const *s, size_t length) { +void sect_AbsWordString(std::vector const &s) { if (!checkcodesection()) return; - if (!reserveSpace(length * 2)) + if (!reserveSpace(s.size() * 2)) return; - while (length--) - writeword(*s++); + for (uint8_t v : s) + writeword(v); } -void sect_AbsLongGroup(uint8_t const *s, size_t length) { +void sect_AbsLongString(std::vector const &s) { if (!checkcodesection()) return; - if (!reserveSpace(length * 4)) + if (!reserveSpace(s.size() * 4)) return; - while (length--) - writelong(*s++); + for (uint8_t v : s) + writelong(v); } // Skip this many bytes diff --git a/src/util.cpp b/src/util.cpp index 722af449..ca1071e7 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -54,12 +54,11 @@ char const *printChar(int c) { } size_t readUTF8Char(std::vector *dest, char const *src) { - uint32_t state = 0; - uint32_t codep; + uint32_t state = 0, codepoint; size_t i = 0; for (;;) { - if (decode(&state, &codep, src[i]) == 1) + if (decode(&state, &codepoint, src[i]) == 1) return 0; if (dest)