Use automatic allocation for RPN reasons

This commit is contained in:
Rangi42
2024-03-07 09:58:45 -05:00
parent e5b7e65e91
commit 84bedc7bbe
2 changed files with 5 additions and 10 deletions

View File

@@ -13,7 +13,7 @@ struct Symbol;
struct Expression { struct Expression {
int32_t val; // If the expression's value is known, it's here int32_t val; // If the expression's value is known, it's here
std::string *reason; // Why the expression is not known, if it isn't std::string reason; // Why the expression is not known, if it isn't
bool isKnown; // Whether the expression's value is known at assembly time bool isKnown; // Whether the expression's value is known at assembly time
bool isSymbol; // Whether the expression represents a symbol suitable for const diffing bool isSymbol; // Whether the expression represents a symbol suitable for const diffing
std::vector<uint8_t> *rpn; // Bytes serializing the RPN expression std::vector<uint8_t> *rpn; // Bytes serializing the RPN expression

View File

@@ -26,7 +26,7 @@
// Init a RPN expression // Init a RPN expression
static void initExpression(Expression &expr) { static void initExpression(Expression &expr) {
expr.val = 0; expr.val = 0;
expr.reason = nullptr; expr.reason.clear();
expr.isKnown = true; expr.isKnown = true;
expr.isSymbol = false; expr.isSymbol = false;
expr.rpn = nullptr; expr.rpn = nullptr;
@@ -37,10 +37,8 @@ static void initExpression(Expression &expr) {
template<typename... Ts> template<typename... Ts>
static void makeUnknown(Expression &expr, Ts... parts) { static void makeUnknown(Expression &expr, Ts... parts) {
expr.isKnown = false; expr.isKnown = false;
expr.reason = new std::string(); expr.reason.clear();
if (!expr.reason) (expr.reason.append(parts), ...);
fatalerror("Failed to allocate RPN error string: %s\n", strerror(errno));
(expr.reason->append(parts), ...);
} }
static uint8_t *reserveSpace(Expression &expr, uint32_t size) { static uint8_t *reserveSpace(Expression &expr, uint32_t size) {
@@ -59,7 +57,6 @@ static uint8_t *reserveSpace(Expression &expr, uint32_t size) {
// Free the RPN expression // Free the RPN expression
void rpn_Free(Expression &expr) { void rpn_Free(Expression &expr) {
delete expr.rpn; delete expr.rpn;
delete expr.reason;
initExpression(expr); initExpression(expr);
} }
@@ -270,7 +267,7 @@ void rpn_CheckNBit(Expression const &expr, uint8_t n) {
int32_t Expression::getConstVal() const { int32_t Expression::getConstVal() const {
if (!isKnown) { if (!isKnown) {
error("Expected constant expression: %s\n", reason->c_str()); error("Expected constant expression: %s\n", reason.c_str());
return 0; return 0;
} }
return val; return val;
@@ -526,13 +523,11 @@ void rpn_BinaryOp(
// Use the other expression's un-const reason // Use the other expression's un-const reason
expr.reason = src2.reason; expr.reason = src2.reason;
delete src1.reason;
} else { } else {
// Otherwise just reuse its RPN buffer // Otherwise just reuse its RPN buffer
expr.rpnPatchSize = src1.rpnPatchSize; expr.rpnPatchSize = src1.rpnPatchSize;
expr.rpn = src1.rpn; expr.rpn = src1.rpn;
expr.reason = src1.reason; expr.reason = src1.reason;
delete src2.reason;
} }
// Now, merge the right expression into the left one // Now, merge the right expression into the left one