mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Use automatic allocation for DS args
This commit is contained in:
@@ -88,11 +88,4 @@ struct AlignmentSpec {
|
|||||||
uint16_t alignOfs;
|
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
|
#endif // RGBDS_ASM_LEXER_H
|
||||||
|
|||||||
@@ -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_AbsLongGroup(uint8_t const *s, size_t length);
|
||||||
void sect_Skip(uint32_t skip, bool ds);
|
void sect_Skip(uint32_t skip, bool ds);
|
||||||
void sect_RelByte(struct Expression *expr, uint32_t pcShift);
|
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_RelWord(struct Expression *expr, uint32_t pcShift);
|
||||||
void sect_RelLong(struct Expression *expr, uint32_t pcShift);
|
void sect_RelLong(struct Expression *expr, uint32_t pcShift);
|
||||||
void sect_PCRelByte(struct Expression *expr, uint32_t pcShift);
|
void sect_PCRelByte(struct Expression *expr, uint32_t pcShift);
|
||||||
|
|||||||
@@ -352,39 +352,18 @@ static void compoundAssignment(const char *symName, enum RPNCommand op, int32_t
|
|||||||
sym_AddVar(symName, newValue);
|
sym_AddVar(symName, newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initDsArgList(struct DsArgList *args)
|
static void initDsArgList(std::vector<struct Expression> *&args)
|
||||||
{
|
{
|
||||||
args->nbArgs = 0;
|
args = new(std::nothrow) std::vector<struct Expression>();
|
||||||
args->capacity = INITIAL_DS_ARG_SIZE;
|
if (!args)
|
||||||
args->args = (struct Expression *)malloc(args->capacity * sizeof(*args->args));
|
fatalerror("Failed to allocate memory for ds arg list: %s\n", strerror(errno));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void initPurgeArgList(std::vector<char *> *&args)
|
static void initPurgeArgList(std::vector<char *> *&args)
|
||||||
{
|
{
|
||||||
args = new(std::nothrow) std::vector<char *>();
|
args = new(std::nothrow) std::vector<char *>();
|
||||||
if (!args)
|
if (!args)
|
||||||
fatalerror("Failed to allocate memory for purge arg list: %s\n",
|
fatalerror("Failed to allocate memory for purge arg list: %s\n", strerror(errno));
|
||||||
strerror(errno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void freePurgeArgList(std::vector<char *> *&args)
|
static void freePurgeArgList(std::vector<char *> *&args)
|
||||||
@@ -486,7 +465,7 @@ enum {
|
|||||||
struct MacroArgs *macroArg;
|
struct MacroArgs *macroArg;
|
||||||
enum AssertionType assertType;
|
enum AssertionType assertType;
|
||||||
struct AlignmentSpec alignSpec;
|
struct AlignmentSpec alignSpec;
|
||||||
struct DsArgList dsArgs;
|
std::vector<struct Expression> *dsArgs;
|
||||||
std::vector<char *> *purgeArgs;
|
std::vector<char *> *purgeArgs;
|
||||||
struct ForArgs forArgs;
|
struct ForArgs forArgs;
|
||||||
struct StrFmtArgList strfmtArgs;
|
struct StrFmtArgList strfmtArgs;
|
||||||
@@ -1175,8 +1154,8 @@ endu : T_POP_ENDU { sect_EndUnion(); }
|
|||||||
|
|
||||||
ds : T_POP_DS uconst { sect_Skip($2, true); }
|
ds : T_POP_DS uconst { sect_Skip($2, true); }
|
||||||
| T_POP_DS uconst T_COMMA ds_args trailing_comma {
|
| T_POP_DS uconst T_COMMA ds_args trailing_comma {
|
||||||
sect_RelBytes($2, $4.args, $4.nbArgs);
|
sect_RelBytes($2, *$4);
|
||||||
freeDsArgList(&$4);
|
delete $4;
|
||||||
}
|
}
|
||||||
| T_POP_DS T_OP_ALIGN T_LBRACK align_spec T_RBRACK trailing_comma {
|
| T_POP_DS T_OP_ALIGN T_LBRACK align_spec T_RBRACK trailing_comma {
|
||||||
uint32_t n = sect_GetAlignBytes($4.alignment, $4.alignOfs);
|
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 {
|
| 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);
|
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);
|
sect_AlignPC($4.alignment, $4.alignOfs);
|
||||||
freeDsArgList(&$7);
|
delete $7;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
ds_args : reloc_8bit {
|
ds_args : reloc_8bit {
|
||||||
initDsArgList(&$$);
|
initDsArgList($$);
|
||||||
appendDsArgList(&$$, &$1);
|
$$->push_back($1);
|
||||||
}
|
}
|
||||||
| ds_args T_COMMA reloc_8bit {
|
| ds_args T_COMMA reloc_8bit {
|
||||||
appendDsArgList(&$1, &$3);
|
$1->push_back($3);
|
||||||
$$ = $1;
|
$$ = $1;
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "asm/fstack.hpp"
|
#include "asm/fstack.hpp"
|
||||||
#include "asm/main.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
|
// Output several copies of a relocatable byte. Checking will be done to see if
|
||||||
// it is an absolute value in disguise.
|
// 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())
|
if (!checkcodesection())
|
||||||
return;
|
return;
|
||||||
@@ -702,18 +703,18 @@ void sect_RelBytes(uint32_t n, struct Expression *exprs, size_t size)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < n; i++) {
|
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)) {
|
if (!rpn_isKnown(&expr)) {
|
||||||
createPatch(PATCHTYPE_BYTE, expr, i);
|
createPatch(PATCHTYPE_BYTE, &expr, i);
|
||||||
writebyte(0);
|
writebyte(0);
|
||||||
} else {
|
} else {
|
||||||
writebyte(expr->val);
|
writebyte(expr.val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < size; i++)
|
for (struct Expression &expr : exprs)
|
||||||
rpn_Free(&exprs[i]);
|
rpn_Free(&expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output a relocatable word. Checking will be done to see if
|
// Output a relocatable word. Checking will be done to see if
|
||||||
|
|||||||
Reference in New Issue
Block a user