Remove NString=80 limit

The parser now uses a vector to get tokens into, similar to how quoted strings
are handled. This buffer is PFn-scoped, so function and data parsing can use
it. Type parsing has to copy its names to the global heap.

While this avoids the "identifier too long" error, it of course calls malloc
more often, so could potentially have a small negative performance impact on
parsing.
This commit is contained in:
Scott Graham 2026-02-17 11:49:39 -08:00 committed by Quentin Carbonneaux
parent 6fa30d5e22
commit fbe3cea201
6 changed files with 31 additions and 22 deletions

11
all.h
View File

@ -34,7 +34,6 @@ typedef struct Lnk Lnk;
typedef struct Target Target; typedef struct Target Target;
enum { enum {
NString = 80,
NIns = 1 << 20, NIns = 1 << 20,
NAlign = 3, NAlign = 3,
NField = 32, NField = 32,
@ -274,7 +273,7 @@ struct Blk {
BSet in[1], out[1], gen[1]; BSet in[1], out[1], gen[1];
int nlive[2]; int nlive[2];
int loop; int loop;
char name[NString]; char *name;
}; };
struct Use { struct Use {
@ -336,7 +335,7 @@ struct Alias {
}; };
struct Tmp { struct Tmp {
char name[NString]; char *name;
Ins *def; Ins *def;
Use *use; Use *use;
uint ndef, nuse; uint ndef, nuse;
@ -415,12 +414,12 @@ struct Fn {
char vararg; char vararg;
char dynalloc; char dynalloc;
char leaf; char leaf;
char name[NString]; char *name;
Lnk lnk; Lnk lnk;
}; };
struct Typ { struct Typ {
char name[NString]; char *name;
char isdark; char isdark;
char isunion; char isunion;
int align; int align;
@ -490,7 +489,7 @@ void vfree(void *);
void vgrow(void *, ulong); void vgrow(void *, ulong);
void addins(Ins **, uint *, Ins *); void addins(Ins **, uint *, Ins *);
void addbins(Ins **, uint *, Blk *); void addbins(Ins **, uint *, Blk *);
void strf(char[NString], char *, ...); char *strf(char *, ...);
uint32_t intern(char *); uint32_t intern(char *);
char *str(uint32_t); char *str(uint32_t);
int argcls(Ins *, int); int argcls(Ins *, int);

View File

@ -511,7 +511,7 @@ split(Fn *fn, Blk *b)
idup(bn, curi, &insb[NIns]-curi); idup(bn, curi, &insb[NIns]-curi);
curi = &insb[NIns]; curi = &insb[NIns];
bn->visit = ++b->visit; bn->visit = ++b->visit;
strf(bn->name, "%s.%d", b->name, b->visit); bn->name = strf("%s.%d", b->name, b->visit);
bn->loop = b->loop; bn->loop = b->loop;
bn->link = b->link; bn->link = b->link;
b->link = bn; b->link = bn;

View File

@ -548,7 +548,7 @@ split(Fn *fn, Blk *b)
idup(bn, curi, &insb[NIns]-curi); idup(bn, curi, &insb[NIns]-curi);
curi = &insb[NIns]; curi = &insb[NIns];
bn->visit = ++b->visit; bn->visit = ++b->visit;
strf(bn->name, "%s.%d", b->name, b->visit); bn->name = strf("%s.%d", b->name, b->visit);
bn->loop = b->loop; bn->loop = b->loop;
bn->link = b->link; bn->link = b->link;
b->link = bn; b->link = bn;

21
parse.c
View File

@ -235,7 +235,7 @@ getint()
static int static int
lex() lex()
{ {
static char tok[NString]; char *tok;
int c, i, esc; int c, i, esc;
int t; int t;
@ -321,10 +321,10 @@ lex()
Alpha: Alpha:
if (!isalpha(c) && c != '.' && c != '_') if (!isalpha(c) && c != '.' && c != '_')
err("invalid character %c (%d)", c, c); err("invalid character %c (%d)", c, c);
tok = vnew(2, 1, PFn);
i = 0; i = 0;
do { do {
if (i >= NString-1) vgrow(&tok, i+2);
err("identifier too long");
tok[i++] = c; tok[i++] = c;
c = fgetc(inf); c = fgetc(inf);
} while (isalpha(c) || c == '$' || c == '.' || c == '_' || isdigit(c)); } while (isalpha(c) || c == '$' || c == '.' || c == '_' || isdigit(c));
@ -421,7 +421,7 @@ tmpref(char *v)
t = curf->ntmp; t = curf->ntmp;
tmph[i] = t; tmph[i] = t;
newtmp(0, Kx, curf); newtmp(0, Kx, curf);
strcpy(curf->tmp[t].name, v); curf->tmp[t].name = v;
return TMP(t); return TMP(t);
} }
@ -594,7 +594,7 @@ findblk(char *name)
return b; return b;
b = newblk(); b = newblk();
b->id = nblk++; b->id = nblk++;
strcpy(b->name, name); b->name = name;
b->dlink = blkh[h]; b->dlink = blkh[h];
blkh[h] = b; blkh[h] = b;
return b; return b;
@ -940,7 +940,7 @@ parsefn(Lnk *lnk)
rcls = K0; rcls = K0;
if (next() != Tglo) if (next() != Tglo)
err("function name expected"); err("function name expected");
strncpy(curf->name, tokval.str, NString-1); curf->name = tokval.str;
curf->vararg = parserefl(0); curf->vararg = parserefl(0);
if (nextnl() != Tlbrace) if (nextnl() != Tlbrace)
err("function body must start with {"); err("function body must start with {");
@ -1050,6 +1050,7 @@ parsetyp()
ty->size = 0; ty->size = 0;
if (nextnl() != Ttyp || nextnl() != Teq) if (nextnl() != Ttyp || nextnl() != Teq)
err("type name and then = expected"); err("type name and then = expected");
ty->name = emalloc(strlen(tokval.str)+1);
strcpy(ty->name, tokval.str); strcpy(ty->name, tokval.str);
t = nextnl(); t = nextnl();
if (t == Talign) { if (t == Talign) {
@ -1115,13 +1116,13 @@ parsedatstr(Dat *d)
static void static void
parsedat(void cb(Dat *), Lnk *lnk) parsedat(void cb(Dat *), Lnk *lnk)
{ {
char name[NString] = {0}; char *name;
int t; int t;
Dat d; Dat d;
if (nextnl() != Tglo || nextnl() != Teq) if (nextnl() != Tglo || nextnl() != Teq)
err("data name, then = expected"); err("data name, then = expected");
strncpy(name, tokval.str, NString-1); name = tokval.str;
t = nextnl(); t = nextnl();
lnk->align = 8; lnk->align = 8;
if (t == Talign) { if (t == Talign) {
@ -1251,9 +1252,11 @@ parse(FILE *f, char *path, void dbgfile(char *), void data(Dat *), void func(Fn
parsetyp(); parsetyp();
break; break;
case Teof: case Teof:
for (n=0; n<ntyp; n++) for (n=0; n<ntyp; n++) {
free(typ[n].name);
if (typ[n].nunion) if (typ[n].nunion)
vfree(typ[n].fields); vfree(typ[n].fields);
}
vfree(typ); vfree(typ);
return; return;
} }

2
rega.c
View File

@ -673,7 +673,7 @@ rega(Fn *fn)
b1->link = blist; b1->link = blist;
blist = b1; blist = b1;
fn->nblk++; fn->nblk++;
strf(b1->name, "%s_%s", b->name, s->name); b1->name = strf("%s_%s", b->name, s->name);
stmov += &insb[NIns]-curi; stmov += &insb[NIns]-curi;
stblk += 1; stblk += 1;
idup(b1, curi, &insb[NIns]-curi); idup(b1, curi, &insb[NIns]-curi);

15
util.c
View File

@ -172,14 +172,21 @@ addbins(Ins **pvins, uint *pnins, Blk *b)
addins(pvins, pnins, i); addins(pvins, pnins, i);
} }
void char *
strf(char str[NString], char *s, ...) strf(char *s, ...)
{ {
va_list ap; va_list ap;
int n;
char *p;
va_start(ap, s); va_start(ap, s);
vsnprintf(str, NString, s, ap); n = vsnprintf(NULL, 0, s, ap);
va_end(ap); va_end(ap);
p = alloc(n + 1);
va_start(ap, s);
vsnprintf(p, n + 1, s, ap);
va_end(ap);
return p;
} }
uint32_t uint32_t
@ -443,7 +450,7 @@ newtmp(char *prfx, int k, Fn *fn)
vgrow(&fn->tmp, fn->ntmp); vgrow(&fn->tmp, fn->ntmp);
memset(&fn->tmp[t], 0, sizeof(Tmp)); memset(&fn->tmp[t], 0, sizeof(Tmp));
if (prfx) if (prfx)
strf(fn->tmp[t].name, "%s.%d", prfx, ++n); fn->tmp[t].name = strf("%s.%d", prfx, ++n);
fn->tmp[t].cls = k; fn->tmp[t].cls = k;
fn->tmp[t].slot = -1; fn->tmp[t].slot = -1;
fn->tmp[t].nuse = +1; fn->tmp[t].nuse = +1;