Use automatic allocation for assertions

This commit is contained in:
Rangi42
2024-02-22 14:26:12 -05:00
committed by Sylvie
parent 6e03504802
commit e022adf4a0
3 changed files with 19 additions and 35 deletions

View File

@@ -18,7 +18,7 @@ void out_RegisterNode(struct FileStackNode *node);
void out_ReplaceNode(struct FileStackNode *node); void out_ReplaceNode(struct FileStackNode *node);
void out_SetFileName(char *s); void out_SetFileName(char *s);
void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs, uint32_t pcShift); void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs, uint32_t pcShift);
bool out_CreateAssert(enum AssertionType type, struct Expression const *expr, void out_CreateAssert(enum AssertionType type, struct Expression const *expr,
char const *message, uint32_t ofs); char const *message, uint32_t ofs);
void out_WriteObject(void); void out_WriteObject(void);

View File

@@ -10,6 +10,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <string.h> #include <string.h>
#include <vector> #include <vector>
@@ -40,7 +41,7 @@ struct Patch {
struct Assertion { struct Assertion {
struct Patch *patch; struct Patch *patch;
struct Section *section; struct Section *section;
char *message; std::string message;
}; };
const char *objectName; const char *objectName;
@@ -50,7 +51,7 @@ std::deque<struct Section *> sectionList;
// List of symbols to put in the object file // List of symbols to put in the object file
static std::vector<struct Symbol *> objectSymbols; static std::vector<struct Symbol *> objectSymbols;
static std::deque<struct Assertion *> assertions; static std::deque<struct Assertion> assertions;
static std::deque<struct FileStackNode *> fileStackNodes; static std::deque<struct FileStackNode *> fileStackNodes;
@@ -352,37 +353,26 @@ void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs,
} }
// Creates an assert that will be written to the object file // Creates an assert that will be written to the object file
bool out_CreateAssert(enum AssertionType type, struct Expression const *expr, void out_CreateAssert(enum AssertionType type, struct Expression const *expr,
char const *message, uint32_t ofs) char const *message, uint32_t ofs)
{ {
struct Assertion *assertion = (struct Assertion *)malloc(sizeof(*assertion)); assertions.push_front({
.patch = allocpatch(type, expr, ofs),
if (!assertion) .section = NULL,
return false; .message = message,
});
assertion->patch = allocpatch(type, expr, ofs);
assertion->message = strdup(message);
if (!assertion->message) {
free(assertion);
return false;
}
assertions.push_front(assertion);
return true;
} }
static void writeassert(struct Assertion *assert, FILE *f) static void writeassert(struct Assertion &assert, FILE *f)
{ {
writepatch(assert->patch, f); writepatch(assert.patch, f);
putstring(assert->message, f); putstring(assert.message.c_str(), f);
} }
static void freeassert(struct Assertion *assert) static void freeassert(struct Assertion &assert)
{ {
free(assert->patch->rpn); free(assert.patch->rpn);
free(assert->patch); free(assert.patch);
free(assert);
} }
static void writeFileStackNode(struct FileStackNode const *node, FILE *f) static void writeFileStackNode(struct FileStackNode const *node, FILE *f)
@@ -456,7 +446,7 @@ void out_WriteObject(void)
putlong(assertions.size(), f); putlong(assertions.size(), f);
for (struct Assertion *assert : assertions) { for (struct Assertion &assert : assertions) {
writeassert(assert, f); writeassert(assert, f);
freeassert(assert); freeassert(assert);
} }

View File

@@ -1089,10 +1089,7 @@ assert_type : %empty { $$ = ASSERT_ERROR; }
assert : T_POP_ASSERT assert_type relocexpr { assert : T_POP_ASSERT assert_type relocexpr {
if (!rpn_isKnown(&$3)) { if (!rpn_isKnown(&$3)) {
if (!out_CreateAssert($2, &$3, "", out_CreateAssert($2, &$3, "", sect_GetOutputOffset());
sect_GetOutputOffset()))
error("Assertion creation failed: %s\n",
strerror(errno));
} else if ($3.val == 0) { } else if ($3.val == 0) {
failAssert($2); failAssert($2);
} }
@@ -1100,10 +1097,7 @@ assert : T_POP_ASSERT assert_type relocexpr {
} }
| T_POP_ASSERT assert_type relocexpr T_COMMA string { | T_POP_ASSERT assert_type relocexpr T_COMMA string {
if (!rpn_isKnown(&$3)) { if (!rpn_isKnown(&$3)) {
if (!out_CreateAssert($2, &$3, $5, out_CreateAssert($2, &$3, $5, sect_GetOutputOffset());
sect_GetOutputOffset()))
error("Assertion creation failed: %s\n",
strerror(errno));
} else if ($3.val == 0) { } else if ($3.val == 0) {
failAssertMsg($2, $5); failAssertMsg($2, $5);
} }