#include #include #include #include #include "finalize.h" #include "gen.h" #include "sym.h" struct SymTabNode { struct SymTabNode *next; struct Sym sym; }; static struct SymTabNode *symTab; static void cleanup(void) { for (struct SymTabNode *n = symTab, *next; n; n = next) { next = n->next; free(n); } } struct Sym * SymAdd(const struct UStr *identifier) { assert(identifier); static bool first = true; if (first) { first = false; finalizeRegister(cleanup); } struct Sym *found = SymFind(identifier); if (found) { return found; } struct SymTabNode *n = malloc(sizeof(*n)); if (!n) { fprintf(stderr, "SymAdd: out of memory\n"); finalizeExit(1); } // initialize list element *(const struct UStr **)(uintptr_t)&n->sym.identifier = identifier; n->sym.value = 0; // prepend to list n->next = symTab; symTab = n; return &n->sym; } struct Sym * SymFind(const struct UStr *identifier) { assert(identifier); for (struct SymTabNode *n = symTab; n; n = n->next) { if (n->sym.identifier == identifier) { return &n->sym; } } return 0; } void printSymtab(void) { genData(); for (const struct SymTabNode *n = symTab; n; n = n->next) { if (n->sym.value != 0) { genLabeledUInt(n->sym.identifier->cstr, n->sym.value); } } genBSS(); for (const struct SymTabNode *n = symTab; n; n = n->next) { if (n->sym.value == 0) { genLabeledUInt(n->sym.identifier->cstr, n->sym.value); } } }