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_Add(std::string const &mapping, uint8_t value);
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);
#endif // RGBDS_ASM_CHARMAP_HPP

View File

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

View File

@@ -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<uint8_t> const &s);
void sect_AbsWordString(std::vector<uint8_t> const &s);
void sect_AbsLongString(std::vector<uint8_t> 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<Expression> &exprs);

View File

@@ -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<uint8_t> &output) {
std::string_view inputView = input;
while (charmap_ConvertNext(inputView, &output))
std::vector<uint8_t> charmap_Convert(std::string const &input) {
std::vector<uint8_t> 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) {

View File

@@ -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(

View File

@@ -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<Symbol *> objectSymbols;
@@ -36,7 +36,6 @@ static std::deque<Assertion> assertions;
static std::deque<std::shared_ptr<FileStackNode>> 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 &sect, FILE *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) {
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 = "<stdout>";
objectFileName = "<stdout>";
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());
}

View File

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

View File

@@ -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<uint8_t> 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<uint8_t> 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<uint8_t> 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

View File

@@ -54,12 +54,11 @@ char const *printChar(int c) {
}
size_t readUTF8Char(std::vector<uint8_t> *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)