Use std::vector for fstack REPT nodes

This commit is contained in:
Rangi42
2024-02-24 10:59:22 -05:00
committed by Sylvie
parent 53343d2fa6
commit b207bff157
3 changed files with 14 additions and 17 deletions

View File

@@ -6,13 +6,14 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <vector>
#include "helpers.hpp" #include "helpers.hpp"
#include "linkdefs.hpp" #include "linkdefs.hpp"
// Variables related to CLI options // Variables related to CLI options
extern bool isDmgMode; extern bool isDmgMode;
extern char *linkerScriptName; extern char *linkerScriptName;
extern char const *mapFileName; extern char const *mapFileName;
extern bool noSymInMap; extern bool noSymInMap;
extern char const *symFileName; extern char const *symFileName;
@@ -27,12 +28,6 @@ extern bool beVerbose;
extern bool isWRA0Mode; extern bool isWRA0Mode;
extern bool disablePadding; extern bool disablePadding;
// Only used in an anonymous union by `struct FileStackNode`
struct reptNodeData {
uint32_t depth;
uint32_t *iters;
};
struct FileStackNode { struct FileStackNode {
struct FileStackNode *parent; struct FileStackNode *parent;
// Line at which the parent context was exited; meaningless for the root level // Line at which the parent context was exited; meaningless for the root level
@@ -41,7 +36,7 @@ struct FileStackNode {
enum FileStackNodeType type; enum FileStackNodeType type;
union { union {
char *name; // NODE_FILE, NODE_MACRO char *name; // NODE_FILE, NODE_MACRO
struct reptNodeData rept; // NODE_REPT std::vector<uint32_t> *iters; // NODE_REPT
}; };
}; };

View File

@@ -60,8 +60,8 @@ char const *dumpFileStack(struct FileStackNode const *node)
lastName = node->name; lastName = node->name;
fprintf(stderr, "(%" PRIu32 ") -> %s", node->lineNo, lastName); fprintf(stderr, "(%" PRIu32 ") -> %s", node->lineNo, lastName);
if (node->type == NODE_REPT) { if (node->type == NODE_REPT) {
for (uint32_t i = 0; i < node->rept.depth; i++) for (uint32_t iter : *node->iters)
fprintf(stderr, "::REPT~%" PRIu32, node->rept.iters[i]); fprintf(stderr, "::REPT~%" PRIu32, iter);
} }
} else { } else {
assert(node->type != NODE_REPT); assert(node->type != NODE_REPT);

View File

@@ -8,6 +8,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <vector>
#include "link/assign.hpp" #include "link/assign.hpp"
#include "link/main.hpp" #include "link/main.hpp"
@@ -181,16 +182,17 @@ static void readFileStackNode(FILE *file, struct FileStackNode fileNodes[], uint
"%s: Cannot read node #%" PRIu32 "'s file name: %s", fileName, i); "%s: Cannot read node #%" PRIu32 "'s file name: %s", fileName, i);
break; break;
uint32_t depth;
case NODE_REPT: case NODE_REPT:
tryReadlong(fileNodes[i].rept.depth, file, tryReadlong(depth, file,
"%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, i); "%s: Cannot read node #%" PRIu32 "'s rept depth: %s", fileName, i);
fileNodes[i].rept.iters = fileNodes[i].iters = new(std::nothrow) std::vector<uint32_t>();
(uint32_t *)malloc(sizeof(*fileNodes[i].rept.iters) * fileNodes[i].rept.depth); if (!fileNodes[i].iters)
if (!fileNodes[i].rept.iters)
fatal(NULL, 0, "%s: Failed to alloc node #%" PRIu32 "'s iters: %s", fatal(NULL, 0, "%s: Failed to alloc node #%" PRIu32 "'s iters: %s",
fileName, i, strerror(errno)); fileName, i, strerror(errno));
for (uint32_t k = 0; k < fileNodes[i].rept.depth; k++) fileNodes[i].iters->resize(depth);
tryReadlong(fileNodes[i].rept.iters[k], file, for (uint32_t k = 0; k < depth; k++)
tryReadlong((*fileNodes[i].iters)[k], file,
"%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s", "%s: Cannot read node #%" PRIu32 "'s iter #%" PRIu32 ": %s",
fileName, i, k); fileName, i, k);
if (!fileNodes[i].parent) if (!fileNodes[i].parent)
@@ -651,7 +653,7 @@ static void freeNode(struct FileStackNode *node)
{ {
if (node) { if (node) {
if (node->type == NODE_REPT) if (node->type == NODE_REPT)
free(node->rept.iters); delete node->iters;
else else
free(node->name); free(node->name);
} }