More refactoring and renaming

This commit is contained in:
Rangi42
2024-07-26 20:12:51 -04:00
parent 6a65cbc9ed
commit 4e0f794c23
9 changed files with 42 additions and 59 deletions

View File

@@ -16,7 +16,7 @@ void charmap_Push();
void charmap_Pop(); void charmap_Pop();
void charmap_Add(std::string const &mapping, uint8_t value); void charmap_Add(std::string const &mapping, uint8_t value);
bool charmap_HasChar(std::string const &input); bool charmap_HasChar(std::string const &input);
void charmap_Convert(std::string const &input, std::vector<uint8_t> &output); std::vector<uint8_t> charmap_Convert(std::string const &input);
size_t charmap_ConvertNext(std::string_view &input, std::vector<uint8_t> *output); size_t charmap_ConvertNext(std::string_view &input, std::vector<uint8_t> *output);
#endif // RGBDS_ASM_CHARMAP_HPP #endif // RGBDS_ASM_CHARMAP_HPP

View File

@@ -12,7 +12,7 @@
struct Expression; struct Expression;
struct FileStackNode; struct FileStackNode;
extern std::string objectName; extern std::string objectFileName;
void out_RegisterNode(std::shared_ptr<FileStackNode> node); void out_RegisterNode(std::shared_ptr<FileStackNode> node);
void out_SetFileName(std::string const &name); void out_SetFileName(std::string const &name);

View File

@@ -84,9 +84,9 @@ void sect_EndUnion();
void sect_CheckUnionClosed(); void sect_CheckUnionClosed();
void sect_AbsByte(uint8_t b); void sect_AbsByte(uint8_t b);
void sect_AbsByteGroup(uint8_t const *s, size_t length); void sect_AbsByteString(std::vector<uint8_t> const &s);
void sect_AbsWordGroup(uint8_t const *s, size_t length); void sect_AbsWordString(std::vector<uint8_t> const &s);
void sect_AbsLongGroup(uint8_t const *s, size_t length); void sect_AbsLongString(std::vector<uint8_t> const &s);
void sect_Skip(uint32_t skip, bool ds); void sect_Skip(uint32_t skip, bool ds);
void sect_RelByte(Expression &expr, uint32_t pcShift); void sect_RelByte(Expression &expr, uint32_t pcShift);
void sect_RelBytes(uint32_t n, std::vector<Expression> &exprs); void sect_RelBytes(uint32_t n, std::vector<Expression> &exprs);

View File

@@ -127,10 +127,11 @@ bool charmap_HasChar(std::string const &input) {
return charmap.nodes[nodeIdx].isTerminal; return charmap.nodes[nodeIdx].isTerminal;
} }
void charmap_Convert(std::string const &input, std::vector<uint8_t> &output) { std::vector<uint8_t> charmap_Convert(std::string const &input) {
std::string_view inputView = input; std::vector<uint8_t> output;
while (charmap_ConvertNext(inputView, &output)) for (std::string_view inputView = input; charmap_ConvertNext(inputView, &output);)
; ;
return output;
} }
size_t charmap_ConvertNext(std::string_view &input, std::vector<uint8_t> *output) { size_t charmap_ConvertNext(std::string_view &input, std::vector<uint8_t> *output) {

View File

@@ -287,8 +287,8 @@ int main(int argc, char *argv[]) {
} }
} }
if (targetFileName.empty() && !objectName.empty()) if (targetFileName.empty() && !objectFileName.empty())
targetFileName = objectName; targetFileName = objectFileName;
if (argc == musl_optind) { if (argc == musl_optind) {
fputs( fputs(

View File

@@ -27,7 +27,7 @@ struct Assertion {
std::string message; std::string message;
}; };
std::string objectName; std::string objectFileName;
// List of symbols to put in the object file // List of symbols to put in the object file
static std::vector<Symbol *> objectSymbols; static std::vector<Symbol *> objectSymbols;
@@ -36,7 +36,6 @@ static std::deque<Assertion> assertions;
static std::deque<std::shared_ptr<FileStackNode>> fileStackNodes; static std::deque<std::shared_ptr<FileStackNode>> fileStackNodes;
// Write a long to a file (little-endian)
static void putlong(uint32_t n, FILE *file) { static void putlong(uint32_t n, FILE *file) {
uint8_t bytes[] = { uint8_t bytes[] = {
(uint8_t)n, (uint8_t)n,
@@ -47,7 +46,6 @@ static void putlong(uint32_t n, FILE *file) {
fwrite(bytes, 1, sizeof(bytes), file); fwrite(bytes, 1, sizeof(bytes), file);
} }
// Write a NUL-terminated string to a file
static void putstring(std::string const &s, FILE *file) { static void putstring(std::string const &s, FILE *file) {
fputs(s.c_str(), file); fputs(s.c_str(), file);
putc('\0', file); putc('\0', file);
@@ -72,7 +70,6 @@ static uint32_t getSectIDIfAny(Section *sect) {
fatalerror("Unknown section '%s'\n", sect->name.c_str()); fatalerror("Unknown section '%s'\n", sect->name.c_str());
} }
// Write a patch to a file
static void writepatch(Patch const &patch, FILE *file) { static void writepatch(Patch const &patch, FILE *file) {
assume(patch.src->ID != (uint32_t)-1); assume(patch.src->ID != (uint32_t)-1);
putlong(patch.src->ID, file); 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); fwrite(patch.rpn.data(), 1, patch.rpn.size(), file);
} }
// Write a section to a file
static void writesection(Section const &sect, FILE *file) { static void writesection(Section const &sect, FILE *file) {
putstring(sect.name, file); putstring(sect.name, file);
@@ -110,7 +106,6 @@ static void writesection(Section const &sect, FILE *file) {
} }
} }
// Write a symbol to a file
static void writesymbol(Symbol const &sym, FILE *file) { static void writesymbol(Symbol const &sym, FILE *file) {
putstring(sym.name, file); putstring(sym.name, file);
if (!sym.isDefined()) { 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) { void out_CreatePatch(uint32_t type, Expression const &expr, uint32_t ofs, uint32_t pcShift) {
// Add the patch to the list // Add the patch to the list
Patch &patch = currentSection->patches.emplace_front(); 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; patch.pcOffset -= pcShift;
} }
// Creates an assert that will be written to the object file
void out_CreateAssert( void out_CreateAssert(
AssertionType type, Expression const &expr, std::string const &message, uint32_t ofs 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() { void out_WriteObject() {
if (objectName.empty()) if (objectFileName.empty())
return; return;
FILE *file; FILE *file;
if (objectName != "-") { if (objectFileName != "-") {
file = fopen(objectName.c_str(), "wb"); file = fopen(objectFileName.c_str(), "wb");
} else { } else {
objectName = "<stdout>"; objectFileName = "<stdout>";
file = fdopen(STDOUT_FILENO, "wb"); file = fdopen(STDOUT_FILENO, "wb");
} }
if (!file) 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); }}; Defer closeFile{[&] { fclose(file); }};
// Also write symbols that weren't written above // Also write symbols that weren't written above
@@ -355,11 +347,10 @@ void out_WriteObject() {
writeassert(assert, file); writeassert(assert, file);
} }
// Set the object filename
void out_SetFileName(std::string const &name) { void out_SetFileName(std::string const &name) {
if (!objectName.empty()) if (!objectFileName.empty())
warnx("Overriding output filename %s", objectName.c_str()); warnx("Overriding output filename %s", objectFileName.c_str());
objectName = name; objectFileName = name;
if (verbose) if (verbose)
printf("Output filename %s\n", objectName.c_str()); printf("Output filename %s\n", objectFileName.c_str());
} }

View File

@@ -1170,10 +1170,8 @@ constlist_8bit_entry:
sect_RelByte($1, 0); sect_RelByte($1, 0);
} }
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output = charmap_Convert($1);
sect_AbsByteString(output);
charmap_Convert($1, output);
sect_AbsByteGroup(output.data(), output.size());
} }
; ;
@@ -1187,10 +1185,8 @@ constlist_16bit_entry:
sect_RelWord($1, 0); sect_RelWord($1, 0);
} }
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output = charmap_Convert($1);
sect_AbsWordString(output);
charmap_Convert($1, output);
sect_AbsWordGroup(output.data(), output.size());
} }
; ;
@@ -1204,10 +1200,8 @@ constlist_32bit_entry:
sect_RelLong($1, 0); sect_RelLong($1, 0);
} }
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output = charmap_Convert($1);
sect_AbsLongString(output);
charmap_Convert($1, output);
sect_AbsLongGroup(output.data(), output.size());
} }
; ;
@@ -1256,9 +1250,7 @@ relocexpr:
$$ = std::move($1); $$ = std::move($1);
} }
| string { | string {
std::vector<uint8_t> output; std::vector<uint8_t> output = charmap_Convert($1);
charmap_Convert($1, output);
$$.makeNumber(str2int2(output)); $$.makeNumber(str2int2(output));
} }
; ;

View File

@@ -671,34 +671,34 @@ void sect_AbsByte(uint8_t b) {
writebyte(b); writebyte(b);
} }
void sect_AbsByteGroup(uint8_t const *s, size_t length) { void sect_AbsByteString(std::vector<uint8_t> const &s) {
if (!checkcodesection()) if (!checkcodesection())
return; return;
if (!reserveSpace(length)) if (!reserveSpace(s.size()))
return; return;
while (length--) for (uint8_t v : s)
writebyte(*s++); writebyte(v);
} }
void sect_AbsWordGroup(uint8_t const *s, size_t length) { void sect_AbsWordString(std::vector<uint8_t> const &s) {
if (!checkcodesection()) if (!checkcodesection())
return; return;
if (!reserveSpace(length * 2)) if (!reserveSpace(s.size() * 2))
return; return;
while (length--) for (uint8_t v : s)
writeword(*s++); writeword(v);
} }
void sect_AbsLongGroup(uint8_t const *s, size_t length) { void sect_AbsLongString(std::vector<uint8_t> const &s) {
if (!checkcodesection()) if (!checkcodesection())
return; return;
if (!reserveSpace(length * 4)) if (!reserveSpace(s.size() * 4))
return; return;
while (length--) for (uint8_t v : s)
writelong(*s++); writelong(v);
} }
// Skip this many bytes // Skip this many bytes

View File

@@ -54,12 +54,11 @@ char const *printChar(int c) {
} }
size_t readUTF8Char(std::vector<uint8_t> *dest, char const *src) { size_t readUTF8Char(std::vector<uint8_t> *dest, char const *src) {
uint32_t state = 0; uint32_t state = 0, codepoint;
uint32_t codep;
size_t i = 0; size_t i = 0;
for (;;) { for (;;) {
if (decode(&state, &codep, src[i]) == 1) if (decode(&state, &codepoint, src[i]) == 1)
return 0; return 0;
if (dest) if (dest)