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

View File

@ -511,7 +511,7 @@ split(Fn *fn, Blk *b)
idup(bn, curi, &insb[NIns]-curi);
curi = &insb[NIns];
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->link = b->link;
b->link = bn;

View File

@ -548,7 +548,7 @@ split(Fn *fn, Blk *b)
idup(bn, curi, &insb[NIns]-curi);
curi = &insb[NIns];
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->link = b->link;
b->link = bn;

21
parse.c
View File

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

2
rega.c
View File

@ -673,7 +673,7 @@ rega(Fn *fn)
b1->link = blist;
blist = b1;
fn->nblk++;
strf(b1->name, "%s_%s", b->name, s->name);
b1->name = strf("%s_%s", b->name, s->name);
stmov += &insb[NIns]-curi;
stblk += 1;
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);
}
void
strf(char str[NString], char *s, ...)
char *
strf(char *s, ...)
{
va_list ap;
int n;
char *p;
va_start(ap, s);
vsnprintf(str, NString, s, ap);
n = vsnprintf(NULL, 0, s, 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
@ -443,7 +450,7 @@ newtmp(char *prfx, int k, Fn *fn)
vgrow(&fn->tmp, fn->ntmp);
memset(&fn->tmp[t], 0, sizeof(Tmp));
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].slot = -1;
fn->tmp[t].nuse = +1;