Use automatic allocation for DS args

This commit is contained in:
Rangi42
2024-02-22 21:21:05 -05:00
committed by Sylvie
parent b1aa98b43d
commit 843f3394c8
4 changed files with 22 additions and 49 deletions

View File

@@ -88,11 +88,4 @@ struct AlignmentSpec {
uint16_t alignOfs;
};
#define INITIAL_DS_ARG_SIZE 2
struct DsArgList {
size_t nbArgs;
size_t capacity;
struct Expression *args;
};
#endif // RGBDS_ASM_LEXER_H

View File

@@ -74,7 +74,7 @@ void sect_AbsWordGroup(uint8_t const *s, size_t length);
void sect_AbsLongGroup(uint8_t const *s, size_t length);
void sect_Skip(uint32_t skip, bool ds);
void sect_RelByte(struct Expression *expr, uint32_t pcShift);
void sect_RelBytes(uint32_t n, struct Expression *exprs, size_t size);
void sect_RelBytes(uint32_t n, std::vector<struct Expression> &exprs);
void sect_RelWord(struct Expression *expr, uint32_t pcShift);
void sect_RelLong(struct Expression *expr, uint32_t pcShift);
void sect_PCRelByte(struct Expression *expr, uint32_t pcShift);

View File

@@ -352,39 +352,18 @@ static void compoundAssignment(const char *symName, enum RPNCommand op, int32_t
sym_AddVar(symName, newValue);
}
static void initDsArgList(struct DsArgList *args)
static void initDsArgList(std::vector<struct Expression> *&args)
{
args->nbArgs = 0;
args->capacity = INITIAL_DS_ARG_SIZE;
args->args = (struct Expression *)malloc(args->capacity * sizeof(*args->args));
if (!args->args)
fatalerror("Failed to allocate memory for ds arg list: %s\n",
strerror(errno));
}
static void appendDsArgList(struct DsArgList *args, const struct Expression *expr)
{
if (args->nbArgs == args->capacity) {
args->capacity = (args->capacity + 1) * 2;
args->args = (struct Expression *)realloc(args->args, args->capacity * sizeof(*args->args));
if (!args->args)
fatalerror("realloc error while resizing ds arg list: %s\n",
strerror(errno));
}
args->args[args->nbArgs++] = *expr;
}
static void freeDsArgList(struct DsArgList *args)
{
free(args->args);
args = new(std::nothrow) std::vector<struct Expression>();
if (!args)
fatalerror("Failed to allocate memory for ds arg list: %s\n", strerror(errno));
}
static void initPurgeArgList(std::vector<char *> *&args)
{
args = new(std::nothrow) std::vector<char *>();
if (!args)
fatalerror("Failed to allocate memory for purge arg list: %s\n",
strerror(errno));
fatalerror("Failed to allocate memory for purge arg list: %s\n", strerror(errno));
}
static void freePurgeArgList(std::vector<char *> *&args)
@@ -486,7 +465,7 @@ enum {
struct MacroArgs *macroArg;
enum AssertionType assertType;
struct AlignmentSpec alignSpec;
struct DsArgList dsArgs;
std::vector<struct Expression> *dsArgs;
std::vector<char *> *purgeArgs;
struct ForArgs forArgs;
struct StrFmtArgList strfmtArgs;
@@ -1175,8 +1154,8 @@ endu : T_POP_ENDU { sect_EndUnion(); }
ds : T_POP_DS uconst { sect_Skip($2, true); }
| T_POP_DS uconst T_COMMA ds_args trailing_comma {
sect_RelBytes($2, $4.args, $4.nbArgs);
freeDsArgList(&$4);
sect_RelBytes($2, *$4);
delete $4;
}
| T_POP_DS T_OP_ALIGN T_LBRACK align_spec T_RBRACK trailing_comma {
uint32_t n = sect_GetAlignBytes($4.alignment, $4.alignOfs);
@@ -1187,18 +1166,18 @@ ds : T_POP_DS uconst { sect_Skip($2, true); }
| T_POP_DS T_OP_ALIGN T_LBRACK align_spec T_RBRACK T_COMMA ds_args trailing_comma {
uint32_t n = sect_GetAlignBytes($4.alignment, $4.alignOfs);
sect_RelBytes(n, $7.args, $7.nbArgs);
sect_RelBytes(n, *$7);
sect_AlignPC($4.alignment, $4.alignOfs);
freeDsArgList(&$7);
delete $7;
}
;
ds_args : reloc_8bit {
initDsArgList(&$$);
appendDsArgList(&$$, &$1);
initDsArgList($$);
$$->push_back($1);
}
| ds_args T_COMMA reloc_8bit {
appendDsArgList(&$1, &$3);
$1->push_back($3);
$$ = $1;
}
;

View File

@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string>
#include <string.h>
#include <vector>
#include "asm/fstack.hpp"
#include "asm/main.hpp"
@@ -694,7 +695,7 @@ void sect_RelByte(struct Expression *expr, uint32_t pcShift)
// Output several copies of a relocatable byte. Checking will be done to see if
// it is an absolute value in disguise.
void sect_RelBytes(uint32_t n, struct Expression *exprs, size_t size)
void sect_RelBytes(uint32_t n, std::vector<struct Expression> &exprs)
{
if (!checkcodesection())
return;
@@ -702,18 +703,18 @@ void sect_RelBytes(uint32_t n, struct Expression *exprs, size_t size)
return;
for (uint32_t i = 0; i < n; i++) {
struct Expression *expr = &exprs[i % size];
struct Expression &expr = exprs[i % exprs.size()];
if (!rpn_isKnown(expr)) {
createPatch(PATCHTYPE_BYTE, expr, i);
if (!rpn_isKnown(&expr)) {
createPatch(PATCHTYPE_BYTE, &expr, i);
writebyte(0);
} else {
writebyte(expr->val);
writebyte(expr.val);
}
}
for (size_t i = 0; i < size; i++)
rpn_Free(&exprs[i]);
for (struct Expression &expr : exprs)
rpn_Free(&expr);
}
// Output a relocatable word. Checking will be done to see if