diff options
| author | 2025-12-13 18:26:51 +0000 | |
|---|---|---|
| committer | 2026-02-16 19:11:24 +0000 | |
| commit | 40f9d989df2c1ff2f567656ccbdbfc0d97e34a77 (patch) | |
| tree | c3a0e69b2823adfe1d5ccf0f48d77b288e55740a /src/build/cmeta.h | |
| parent | f386f2a1fcae885967278710fe997c56e2183476 (diff) | |
| download | sst-40f9d989df2c1ff2f567656ccbdbfc0d97e34a77.tar.gz sst-40f9d989df2c1ff2f567656ccbdbfc0d97e34a77.zip | |
Switch cmeta from chibicc to a new homegrown lexer
This lexer is being written as a chunklet, not quite quite complete yet
as it lacks some functionality to make it generally useful for things,
but good enough for the gluegen use case now. So it's in the repo now
and we can go ahead and use it for this instead of having this
hacked-to-pieces third party thing that's nowhere near as efficient. In
the future the goal is to have a decently usable library for any sort of
C metaprogramming needs, not just within this project.
This design does the data-oriented thing of storing as little about each
token as possible (just 5 bytes), and re-lexing specific pieces only
when necessary. In some cases, full validation of correct syntax is
only possible through this secondary step. Since the use case is
metaprogramming and code-generation rather than the reimplementation of
Clang, this slight sloppiness in validation doesn't seem too bad.
It's been a while since I did a proper performance measurement of this
code but in some crude tests I did in the past the primary tokenisation
step was running at well over 500MB/s, which is fast enough for me. It's
possible that this version is a tiny bit slower due to the added
complexity of making UCNs work. UCNs, incidentally, are one of the
dumbest features of C by far. However, unlike trigraphs - which this
lexer does not handle - UCNs are still in the language, so it's kind of
sort of necessary to support them.
It's probably still possible to come up with a faster design with some
SIMD trickery, but the main loop is currently branchless and the lookup
tables are too big for the SIMD lookup things, so that seems kind of
hard. A separate SIMD path for whitespace or comment runs seems dubious
as it would introduce branch mispredictions everywhere. I also made
previous attempts to unroll the main loop and every attempt just made it
slower, so I guess code size is a significant factor.
Optimising the secondary tokenisation of identifiers (and later numerals
and string/character literals once those are handled) is still on the
cards, but since that happens less often, I don't know how much
difference it'll make.
At any rate, in a multithreaded context this thing would already come
pretty close to SSD speeds, if open-read-close syscall overhead doesn't
get in the way first. I imagine it's fast enough for anyone who hasn't
*already* written something faster.
Diffstat (limited to 'src/build/cmeta.h')
| -rw-r--r-- | src/build/cmeta.h | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/src/build/cmeta.h b/src/build/cmeta.h index e7c275d..c18adbb 100644 --- a/src/build/cmeta.h +++ b/src/build/cmeta.h @@ -17,33 +17,23 @@ #ifndef INC_CMETA_H #define INC_CMETA_H +#include "../chunklets/clex.h" /* XXX: only need this for struct, kinda leaky */ #include "../intdefs.h" #include "../os.h" -// XXX: leaking chibicc internals. won't matter after we do away with that -typedef struct Token Token; - -enum cmeta_item { - CMETA_ITEM_DEF_CVAR, // includes all min/max/unreg variants - CMETA_ITEM_DEF_CCMD, // includes plusminus/unreg variants - CMETA_ITEM_DEF_EVENT, // includes predicates +enum cmeta_itemtype { + CMETA_ITEM_DEF_CVAR, /* includes all min/max/unreg variants */ + CMETA_ITEM_DEF_CCMD, /* includes plusminus/unreg variants */ + CMETA_ITEM_DEF_EVENT, /* includes predicates */ CMETA_ITEM_HANDLE_EVENT, CMETA_ITEM_FEATURE, - CMETA_ITEM_REQUIRE, // includes all REQUIRE_*/REQUEST variants + CMETA_ITEM_REQUIRE, /* includes all REQUIRE_* and REQUEST variants */ CMETA_ITEM_GAMESPECIFIC, CMETA_ITEM_PREINIT, CMETA_ITEM_INIT, CMETA_ITEM_END }; -struct cmeta { - char *sbase; - u32 nitems; // number of interesting macros - //u32 *itemoffs; // file offsets of interesting macros (ONE DAY!) - Token **itemtoks; // crappy linked token structures, for the time being - u8 *itemtypes; // CMETA_ITEM_* enum values -}; - enum cmeta_flag_cvar { CMETA_CVAR_UNREG = 1, CMETA_CVAR_FEAT = 2, @@ -57,28 +47,37 @@ enum cmeta_flag_event { CMETA_EVENT_ISPREDICATE = 1 }; enum cmeta_flag_require { - CMETA_REQUIRE_OPTIONAL = 1, // i.e. REQUEST() macro, could be extended + CMETA_REQUIRE_OPTIONAL = 1, /* i.e. REQUEST() macro, could be extended */ CMETA_REQUIRE_GAMEDATA = 2, CMETA_REQUIRE_GLOBAL = 4 }; +struct cmeta { + char *sbase; + u32 nitems; /* number of interesting macros */ + //char pad[4]; + struct cmeta_item { + u8 type : 4; /* CMETA_ITEM_* enum value */ + u8 flags : 4; /* relevant flags (see above) */ + } *items; + u32 *itemtoks; /* internal */ + struct clex lexer; /* internal */ +}; + struct cmeta_slice { const char *s; int len; }; struct cmeta cmeta_loadfile(const os_char *path); -int cmeta_flags_cvar(const struct cmeta *cm, u32 i); -int cmeta_flags_ccmd(const struct cmeta *cm, u32 i); -int cmeta_flags_event(const struct cmeta *cm, u32 i); -int cmeta_flags_require(const struct cmeta *cm, u32 i); int cmeta_nparams(const struct cmeta *cm, u32 i); -struct cmeta_param_iter { Token *cur; }; +struct cmeta_param_iter { u32 i; }; struct cmeta_param_iter cmeta_param_iter_init(const struct cmeta *cm, u32 i); -struct cmeta_slice cmeta_param_iter(struct cmeta_param_iter *it); +struct cmeta_slice cmeta_param_iter(const struct cmeta *cm, + struct cmeta_param_iter *it); -#define cmeta_param_foreach(varname, cm, u32) \ +#define cmeta_param_foreach(varname, cm, i) \ switch (0) for (struct cmeta_slice varname; 0;) default: \ for (struct cmeta_param_iter _it = cmeta_param_iter_init(cm, i); \ - varname = cmeta_param_iter(&_it), varname.s;) \ + varname = cmeta_param_iter(cm, &_it), varname.s;) \ /* {...} */ u32 cmeta_line(const struct cmeta *cm, u32 i); |
