Skip to content

Commit

Permalink
Port some cleanup from PR #847
Browse files Browse the repository at this point in the history
- Update some whitespace formatting
- Factor out some functions
- Free data after outputting to an object file
  • Loading branch information
Rangi42 committed Apr 28, 2021
1 parent b4814b0 commit bba5321
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 188 deletions.
4 changes: 3 additions & 1 deletion include/asm/rpn.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Expression {
/*
* Determines if an expression is known at assembly time
*/
static inline bool rpn_isKnown(const struct Expression *expr)
static inline bool rpn_isKnown(struct Expression const *expr)
{
return expr->isKnown;
}
Expand Down Expand Up @@ -64,5 +64,7 @@ void rpn_StartOfSection(struct Expression *expr, char const *sectionName);
void rpn_Free(struct Expression *expr);
void rpn_CheckHRAM(struct Expression *expr, const struct Expression *src);
void rpn_CheckRST(struct Expression *expr, const struct Expression *src);
void rpn_CheckNBit(struct Expression const *expr, uint8_t n);
int32_t rpn_GetConstVal(struct Expression const *expr);

#endif /* RGBDS_ASM_RPN_H */
6 changes: 3 additions & 3 deletions include/linkdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ enum RPNCommand {
RPN_MUL = 0x02,
RPN_DIV = 0x03,
RPN_MOD = 0x04,
RPN_UNSUB = 0x05,
RPN_UNSUB = 0x05, // FIXME: should be renamed to "NEG" for consistency
RPN_EXP = 0x06,

RPN_OR = 0x10,
RPN_AND = 0x11,
RPN_XOR = 0x12,
RPN_UNNOT = 0x13,
RPN_UNNOT = 0x13, // FIXME: should be renamed to "NOT" for consistency

RPN_LOGAND = 0x21,
RPN_LOGOR = 0x22,
RPN_LOGUNNOT = 0x23,
RPN_LOGUNNOT = 0x23, // FIXME: should be renamed to "LOGNOT" for consistency

RPN_LOGEQ = 0x30,
RPN_LOGNE = 0x31,
Expand Down
36 changes: 33 additions & 3 deletions src/asm/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ static void writesection(struct Section const *sect, FILE *f)
}
}

static void freesection(struct Section const *sect)
{
if (sect_HasData(sect->type)) {
struct Patch *patch = sect->patches;

while (patch != NULL) {
struct Patch *next = patch->next;

free(patch->rpn);
free(patch);
patch = next;
}
}
}

/*
* Write a symbol to a file
*/
Expand Down Expand Up @@ -468,6 +483,13 @@ static void writeassert(struct Assertion *assert, FILE *f)
putstring(assert->message, f);
}

static void freeassert(struct Assertion *assert)
{
free(assert->patch->rpn);
free(assert->patch);
free(assert);
}

static void writeFileStackNode(struct FileStackNode const *node, FILE *f)
{
putlong(node->parent ? node->parent->ID : -1, f);
Expand Down Expand Up @@ -530,13 +552,21 @@ void out_WriteObject(void)
for (struct Symbol const *sym = objectSymbols; sym; sym = sym->next)
writesymbol(sym, f);

for (struct Section *sect = sectionList; sect; sect = sect->next)
for (struct Section *sect = sectionList; sect; sect = sect->next) {
writesection(sect, f);
freesection(sect);
}

putlong(countAsserts(), f);
for (struct Assertion *assert = assertions; assert;
assert = assert->next)
struct Assertion *assert = assertions;

while (assert != NULL) {
struct Assertion *next = assert->next;

writeassert(assert, f);
freeassert(assert);
assert = next;
}

fclose(f);
}
Expand Down
Loading

0 comments on commit bba5321

Please sign in to comment.