Skip to content

Commit

Permalink
Implement compact file stacks in object files
Browse files Browse the repository at this point in the history
Gets rid of `open_memstream`, enabling Windows compatibility again
Also fixes #491 as a nice bonus!
  • Loading branch information
ISSOtm committed Sep 29, 2020
1 parent 676ac30 commit 30ccd5e
Show file tree
Hide file tree
Showing 22 changed files with 801 additions and 371 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ checkpatch:
# compilation and make the continous integration infrastructure return failure.

develop:
$Qenv $(MAKE) -j WARNFLAGS="-Werror -Wall -Wextra -Wpedantic \
$Qenv $(MAKE) -j WARNFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-type-limits \
-Wno-sign-compare -Wformat -Wformat-security -Wformat-overflow=2 \
-Wformat-truncation=1 -Wformat-y2k -Wswitch-enum -Wunused \
-Wuninitialized -Wunknown-pragmas -Wstrict-overflow=5 \
Expand Down
57 changes: 35 additions & 22 deletions include/asm/fstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,44 @@

#include "types.h"

struct MacroArgs;
struct FileStackNode {
struct FileStackNode *parent; /* Pointer to parent node, for error reporting */
/* Line at which the parent context was exited; meaningless for the root level */
uint32_t lineNo;

struct FileStackNode *next; /* Next node in the output linked list */
bool referenced; /* If referenced, don't free! */
uint32_t ID; /* Set only if referenced: ID within the object file, -1 if not output yet */

enum {
NODE_REPT,
NODE_FILE,
NODE_MACRO,
} type;
};

struct FileStackReptNode { /* NODE_REPT */
struct FileStackNode node;
uint32_t reptDepth;
/* WARNING: if changing this type, change overflow check in `fstk_Init` */
uint32_t iters[]; /* REPT iteration counts since last named node, in reverse depth order */
};

struct sContext {
struct LexerState *lexerState;
struct Symbol const *pMacro;
struct sContext *next;
char tzFileName[_MAX_PATH + 1];
struct MacroArgs *macroArgs;
uint32_t uniqueID;
int32_t nLine;
uint32_t nStatus;
char const *pREPTBlock;
uint32_t nREPTBlockCount;
uint32_t nREPTBlockSize;
int32_t nREPTBodyFirstLine;
int32_t nREPTBodyLastLine;
struct FileStackNamedNode { /* NODE_FILE, NODE_MACRO */
struct FileStackNode node;
char name[]; /* File name for files, file::macro name for macros */
};

extern size_t nMaxRecursionDepth;

struct MacroArgs;

void fstk_Dump(struct FileStackNode const *node, uint32_t lineNo);
void fstk_DumpCurrent(void);
struct FileStackNode *fstk_GetFileStack(void);
/* The lifetime of the returned chars is until reaching the end of that file */
char const *fstk_GetFileName(void);

void fstk_AddIncludePath(char const *s);
/**
* @param path The user-provided file name
Expand All @@ -53,14 +71,9 @@ bool fstk_FindFile(char const *path, char **fullPath, size_t *size);

bool yywrap(void);
void fstk_RunInclude(char const *path);
void fstk_RunMacro(char *macroName, struct MacroArgs *args);
void fstk_RunMacro(char const *macroName, struct MacroArgs *args);
void fstk_RunRept(uint32_t count, int32_t nReptLineNo, char *body, size_t size);

void fstk_Dump(void);
char *fstk_DumpToStr(void);
char const *fstk_GetFileName(void);
uint32_t fstk_GetLine(void);

void fstk_Init(char *mainPath, size_t maxRecursionDepth);
void fstk_Init(char const *mainPath, size_t maxRecursionDepth);

#endif /* RGBDS_ASM_FSTACK_H */
3 changes: 3 additions & 0 deletions include/asm/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static inline void lexer_SetGfxDigits(char const *digits)
gfxDigits = digits;
}

/*
* `path` is referenced, but not held onto..!
*/
struct LexerState *lexer_OpenFile(char const *path);
struct LexerState *lexer_OpenFileView(char *buf, size_t size, uint32_t lineNo);
void lexer_RestartRept(uint32_t lineNo);
Expand Down
2 changes: 2 additions & 0 deletions include/asm/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct Expression;
extern char *tzObjectname;
extern struct Section *pSectionList, *pCurrentSection;

void out_RegisterNode(struct FileStackNode *node);
void out_ReplaceNode(struct FileStackNode *node);
void out_SetFileName(char *s);
void out_CreatePatch(uint32_t type, struct Expression const *expr,
uint32_t ofs);
Expand Down
4 changes: 2 additions & 2 deletions include/asm/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ struct Symbol {
bool isBuiltin; /* Whether the symbol is a built-in */
struct Symbol const *scope;
struct Section *section;
char fileName[_MAX_PATH + 1]; /* File where the symbol was defined. */
uint32_t fileLine; /* Line where the symbol was defined. */
struct FileStackNode *src; /* Where the symbol was defined */
uint32_t fileLine; /* Line where the symbol was defined */

bool hasCallback;
union {
Expand Down
34 changes: 32 additions & 2 deletions include/link/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,45 @@ extern bool beVerbose;
extern bool isWRA0Mode;
extern bool disablePadding;

struct FileStackNode {
struct FileStackNode *parent;
/* Line at which the parent context was exited; meaningless for the root level */
uint32_t lineNo;

enum {
NODE_REPT,
NODE_FILE,
NODE_MACRO,
} type;
union {
char *name; /* NODE_FILE, NODE_MACRO */
struct { /* NODE_REPT */
uint32_t reptDepth;
uint32_t *iters;
};
};
};

/* Helper macro for printing verbose-mode messages */
#define verbosePrint(...) do { \
if (beVerbose) \
fprintf(stderr, __VA_ARGS__); \
} while (0)

void error(char const *fmt, ...);
/**
* Dump a file stack to stderr
* @param node The leaf node to dump the context of
*/
char const *dumpFileStack(struct FileStackNode const *node);

void warning(struct FileStackNode const *where, uint32_t lineNo,
char const *fmt, ...) format_(printf, 3, 4);

void error(struct FileStackNode const *where, uint32_t lineNo,
char const *fmt, ...) format_(printf, 3, 4);

noreturn_ void fatal(char const *fmt, ...);
noreturn_ void fatal(struct FileStackNode const *where, uint32_t lineNo,
char const *fmt, ...) format_(printf, 3, 4);

/**
* Opens a file if specified, and aborts on error.
Expand Down
9 changes: 8 additions & 1 deletion include/link/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
/**
* Read an object (.o) file, and add its info to the data structures.
* @param fileName A path to the object file to be read
* @param i The ID of the file
*/
void obj_ReadFile(char const *fileName);
void obj_ReadFile(char const *fileName, unsigned int i);

/**
* Perform validation on the object files' contents
Expand All @@ -27,6 +28,12 @@ void obj_DoSanityChecks(void);
*/
void obj_CheckAssertions(void);

/**
* Sets up object file reading
* @param nbFiles The number of object files that will be read
*/
void obj_Setup(unsigned int nbFiles);

/**
* `free`s all object memory that was allocated.
*/
Expand Down
4 changes: 3 additions & 1 deletion include/link/section.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "linkdefs.h"

struct FileStackNode;
struct Section;

struct AttachedSymbol {
Expand All @@ -27,7 +28,8 @@ struct AttachedSymbol {
};

struct Patch {
char *fileName;
struct FileStackNode const *src;
uint32_t lineNo;
int32_t offset;
uint32_t pcSectionID;
uint32_t pcOffset;
Expand Down
4 changes: 3 additions & 1 deletion include/link/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

#include "linkdefs.h"

struct FileStackNode;

struct Symbol {
/* Info contained in the object files */
char *name;
enum ExportLevel type;
char const *objFileName;
char *fileName;
struct FileStackNode const *src;
int32_t lineNo;
int32_t sectionID;
union {
Expand Down
2 changes: 1 addition & 1 deletion include/linkdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#define RGBDS_OBJECT_VERSION_STRING "RGB%1u"
#define RGBDS_OBJECT_VERSION_NUMBER 9U
#define RGBDS_OBJECT_REV 5U
#define RGBDS_OBJECT_REV 6U

enum AssertionType {
ASSERT_WARN,
Expand Down
Loading

0 comments on commit 30ccd5e

Please sign in to comment.