From 302e24671942051d70707586cf8c605a5815edac Mon Sep 17 00:00:00 2001 From: lemon Date: Mon, 15 Dec 2025 22:39:52 +0100 Subject: create distinct interned string type Interned strings are used pervasively, so it's a good idea to add a layer of type safety to differentiate them from general cstrs and avoid potential bugs from comparing non-interned and interned strings. Not that that's happened so far that I can remember, but it could. I'm 90% sure it's legal to alias `struct {char c;}` pointers with `char` pointers. This specific typedef gives type safety but with a simple one-way `internstr -> const char *` typecast (with `&istr->c`). Converting the other way around is more intentional: a straight up cast `(internstr)cstr` which sticks out as unchecked and probably wrong, or calling the intern(cstr) function, which is the right way. --- x86_64/emit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'x86_64') diff --git a/x86_64/emit.c b/x86_64/emit.c index 3c72712..8a1673d 100644 --- a/x86_64/emit.c +++ b/x86_64/emit.c @@ -251,7 +251,7 @@ opermatch(enum operpat pat, struct oper oper) static bool usebp; /* use RBP? */ static int rbpoff; -static const char *curfnsym; +static internstr curfnsym; static uchar *fnstart; /* Given an instruction description table, find the first entry that matches @@ -334,7 +334,7 @@ encode(uchar **pcode, const struct desc *tab, int ntab, enum irclass k, struct o static uchar offs[NOPERENC] = { [EN_MI8] = 1, [EN_MI16] = 2, [EN_MI32] = 4 }; uint addr; int disp = mem.disp - 4 - offs[en->operenc]; - const char *sym = xcon2sym(mem.con); + internstr sym = xcon2sym(mem.con); B(/*mod 0*/ (reg & 7) << 3 | RBP); if (objhassym(sym, &addr) == Stext) { I32(addr - (*pcode - objout.textbegin) + disp); @@ -433,7 +433,7 @@ encode(uchar **pcode, const struct desc *tab, int ntab, enum irclass k, struct o if (rex) B(0x40 | rex); D(opc, nopc); assert(dst.t == OSYM); - const char *sym = xcon2sym(dst.con); + internstr sym = xcon2sym(dst.con); uint addr; if (sym == curfnsym) { I32(fnstart - *pcode - 4); -- cgit v1.2.3