Refactor structs to use methods instead of functions (#1322)

This commit is contained in:
Sylvie
2024-03-01 13:11:45 -05:00
committed by GitHub
parent e14ba664ea
commit 1ac3c0262f
18 changed files with 364 additions and 446 deletions

View File

@@ -15,7 +15,7 @@ enum FormatState {
FORMAT_INVALID, // got unexpected character
};
struct FormatSpec {
class FormatSpec {
enum FormatState state;
int sign;
bool prefix;
@@ -26,15 +26,16 @@ struct FormatSpec {
size_t fracWidth;
int type;
bool valid;
public:
bool isEmpty() const { return !state; }
bool isValid() const { return valid || state == FORMAT_DONE; }
bool isFinished() const { return state >= FORMAT_DONE;}
void useCharacter(int c);
void finishCharacters();
void printString(char *buf, size_t bufLen, char const *value);
void printNumber(char *buf, size_t bufLen, uint32_t value);
};
FormatSpec fmt_NewSpec();
bool fmt_IsEmpty(FormatSpec const *fmt);
bool fmt_IsValid(FormatSpec const *fmt);
bool fmt_IsFinished(FormatSpec const *fmt);
void fmt_UseCharacter(FormatSpec *fmt, int c);
void fmt_FinishCharacters(FormatSpec *fmt);
void fmt_PrintString(char *buf, size_t bufLen, FormatSpec const *fmt, char const *value);
void fmt_PrintNumber(char *buf, size_t bufLen, FormatSpec const *fmt, uint32_t value);
#endif // RGBDS_FORMAT_SPEC_H

View File

@@ -36,6 +36,8 @@ struct FileStackNode {
// File name for files, file::macro name for macros
std::string &name();
std::string const &name() const;
void dump(uint32_t curLineNo) const;
};
#define DEFAULT_MAX_DEPTH 64
@@ -43,7 +45,6 @@ extern size_t maxRecursionDepth;
struct MacroArgs;
void fstk_Dump(FileStackNode const *node, uint32_t lineNo);
void fstk_DumpCurrent();
FileStackNode *fstk_GetFileStack();
// The lifetime of the returned chars is until reaching the end of that file

View File

@@ -5,18 +5,22 @@
#include <stdint.h>
#include <stdlib.h>
#include <vector>
#include "asm/warning.hpp"
#include "helpers.hpp"
struct MacroArgs;
struct MacroArgs {
unsigned int shift;
std::vector<char *> args;
void append(char *s);
void clear();
};
MacroArgs *macro_GetCurrentArgs();
MacroArgs *macro_NewArgs();
void macro_AppendArg(MacroArgs *args, char *s);
void macro_UseNewArgs(MacroArgs *args);
void macro_FreeArgs(MacroArgs *args);
char const *macro_GetArg(uint32_t i);
char const *macro_GetAllArgs();

View File

@@ -13,29 +13,19 @@ struct Symbol;
struct Expression {
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
bool isKnown; // Whether the expression's value is known
bool isSymbol; // Whether the expression represents a symbol
bool isKnown; // Whether the expression's value is known at assembly time
bool isSymbol; // Whether the expression represents a symbol suitable for const diffing
std::vector<uint8_t> *rpn; // Bytes serializing the RPN expression
uint32_t rpnPatchSize; // Size the expression will take in the object file
int32_t getConstVal() const;
Symbol const *symbolOf() const;
bool isDiffConstant(Symbol const *symName) const;
};
// Determines if an expression is known at assembly time
static inline bool rpn_isKnown(Expression const *expr)
{
return expr->isKnown;
}
// Determines if an expression is a symbol suitable for const diffing
static inline bool rpn_isSymbol(const Expression *expr)
{
return expr->isSymbol;
}
void rpn_Symbol(Expression *expr, char const *symName);
void rpn_Number(Expression *expr, uint32_t i);
void rpn_LOGNOT(Expression *expr, const Expression *src);
Symbol const *rpn_SymbolOf(Expression const *expr);
bool rpn_IsDiffConstant(Expression const *src, Symbol const *symName);
void rpn_BinaryOp(enum RPNCommand op, Expression *expr, const Expression *src1, const Expression *src2);
void rpn_HIGH(Expression *expr, const Expression *src);
void rpn_LOW(Expression *expr, const Expression *src);
@@ -49,10 +39,11 @@ void rpn_SizeOfSection(Expression *expr, char const *sectionName);
void rpn_StartOfSection(Expression *expr, char const *sectionName);
void rpn_SizeOfSectionType(Expression *expr, enum SectionType type);
void rpn_StartOfSectionType(Expression *expr, enum SectionType type);
void rpn_Free(Expression *expr);
void rpn_CheckHRAM(Expression *expr, const Expression *src);
void rpn_CheckRST(Expression *expr, const Expression *src);
void rpn_CheckNBit(Expression const *expr, uint8_t n);
int32_t rpn_GetConstVal(Expression const *expr);
#endif // RGBDS_ASM_RPN_H

View File

@@ -8,7 +8,6 @@
#include <vector>
#include "linkdefs.hpp"
#include "platform.hpp" // NONNULL
extern uint8_t fillByte;
@@ -39,6 +38,8 @@ struct Section {
uint16_t alignOfs;
std::deque<Patch> patches;
std::vector<uint8_t> data;
bool isSizeKnown() const;
};
struct SectionSpec {
@@ -85,6 +86,4 @@ void sect_EndSection();
void sect_PushSection();
void sect_PopSection();
bool sect_IsSizeKnown(Section const NONNULL(name));
#endif // RGBDS_SECTION_H

View File

@@ -29,6 +29,9 @@ struct strValue {
char *value;
};
struct Symbol; // For the `sym_IsPC` forward declaration
bool sym_IsPC(Symbol const *sym); // For the inline `getSection` method
struct Symbol {
char name[MAXSYMLEN + 1];
enum SymbolType type;
@@ -40,7 +43,7 @@ struct Symbol {
bool hasCallback;
union {
// If sym_IsNumeric
// If isNumeric()
int32_t value;
int32_t (*numCallback)(); // If hasCallback
// For SYM_MACRO
@@ -51,61 +54,28 @@ struct Symbol {
};
uint32_t ID; // ID of the symbol in the object file (-1 if none)
};
bool sym_IsPC(Symbol const *sym);
bool isDefined() const { return type != SYM_REF; }
bool isNumeric() const { return type == SYM_LABEL || type == SYM_EQU || type == SYM_VAR; }
bool isLabel() const { return type == SYM_LABEL || type == SYM_REF; }
static inline bool sym_IsDefined(Symbol const *sym)
{
return sym->type != SYM_REF;
}
static inline Section *sym_GetSection(Symbol const *sym)
{
return sym_IsPC(sym) ? sect_GetSymbolSection() : sym->section;
}
static inline bool sym_IsConstant(Symbol const *sym)
{
if (sym->type == SYM_LABEL) {
Section const *sect = sym_GetSection(sym);
return sect && sect->org != (uint32_t)-1;
bool isConstant() const {
if (type == SYM_LABEL) {
Section const *sect = getSection();
return sect && sect->org != (uint32_t)-1;
}
return type == SYM_EQU || type == SYM_VAR;
}
return sym->type == SYM_EQU || sym->type == SYM_VAR;
}
static inline bool sym_IsNumeric(Symbol const *sym)
{
return sym->type == SYM_LABEL || sym->type == SYM_EQU || sym->type == SYM_VAR;
}
Section *getSection() const { return sym_IsPC(this) ? sect_GetSymbolSection() : section; }
static inline bool sym_IsLabel(Symbol const *sym)
{
return sym->type == SYM_LABEL || sym->type == SYM_REF;
}
static inline bool sym_IsLocal(Symbol const *sym)
{
return sym_IsLabel(sym) && strchr(sym->name, '.');
}
static inline bool sym_IsExported(Symbol const *sym)
{
return sym->isExported;
}
// Get a string equate's value
static inline char const *sym_GetStringValue(Symbol const *sym)
{
if (sym->hasCallback)
return sym->strCallback();
return sym->equs.value;
}
int32_t getValue() const;
uint32_t getConstantValue() const;
char const *getStringValue() const { return hasCallback ? strCallback() : equs.value; }
};
void sym_ForEach(void (*func)(Symbol *));
int32_t sym_GetValue(Symbol const *sym);
void sym_SetExportAll(bool set);
Symbol *sym_AddLocalLabel(char const *symName);
Symbol *sym_AddLabel(char const *symName);
@@ -116,7 +86,6 @@ Symbol *sym_AddEqu(char const *symName, int32_t value);
Symbol *sym_RedefEqu(char const *symName, int32_t value);
Symbol *sym_AddVar(char const *symName, int32_t value);
uint32_t sym_GetPCValue();
uint32_t sym_GetConstantSymValue(Symbol const *sym);
uint32_t sym_GetConstantValue(char const *symName);
// Find a symbol by exact name, bypassing expansion checks
Symbol *sym_FindExactSymbol(char const *symName);