Compare commits

...

6 Commits

Author SHA1 Message Date
Scott Graham
fbe3cea201 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.
2026-05-07 08:33:02 +02:00
Quentin Carbonneaux
6fa30d5e22 fix offset range check 2026-05-07 08:32:30 +02:00
Quentin Carbonneaux
a450a08e03 space nit 2026-05-06 22:21:00 +02:00
Quentin Carbonneaux
ade9f31efb recognize more phis as copies
Not only phi(%a, %a, ...) is a copy
but any argument can also be a self
reference.
2026-05-06 22:14:45 +02:00
Quentin Carbonneaux
1366e70856 reduce NPm to 64 2026-05-06 22:13:10 +02:00
Quentin Carbonneaux
3cfca4b2f2 reduce temp creation in loadopt() 2026-05-06 22:12:40 +02:00
8 changed files with 61 additions and 28 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;

16
copy.c
View File

@ -388,14 +388,20 @@ phicopyref(Fn *fn, Blk *b, Phi *p)
{ {
Blk *d, **s; Blk *d, **s;
Phi *p1; Phi *p1;
Ref r;
uint n, c; uint n, c;
/* identical args */ /* identical args */
for (n=0; n<p->narg-1; n++) r = R;
if (!req(p->arg[n], p->arg[n+1])) for (n=0; n<p->narg; n++)
break; if (!req(p->arg[n], p->to)) {
if (n == p->narg-1) if (req(r, R))
return p->arg[n]; r = p->arg[n];
else if (!req(p->arg[n], r))
break;
}
if (n == p->narg)
return r;
/* same as a previous phi */ /* same as a previous phi */
for (p1=b->phi; p1!=p; p1=p1->link) { for (p1=b->phi; p1!=p; p1=p1->link) {

18
load.c
View File

@ -166,6 +166,23 @@ load(Slice sl, bits msk, Loc *l)
return r; return r;
} }
static void
rebase(Slice *sl)
{
Alias *a;
if (rtype(sl->ref) != RTmp)
return;
a = &curf->tmp[sl->ref.val].alias;
if (a->offset == (short)a->offset)
if (a->type == ALoc
|| a->type == AEsc
|| a->type == AUnk) {
sl->ref = TMP(a->base);
sl->off = a->offset;
}
}
static int static int
killsl(Ref r, Slice sl) killsl(Ref r, Slice sl)
{ {
@ -431,6 +448,7 @@ loadopt(Fn *fn)
sz = loadsz(i); sz = loadsz(i);
sl = (Slice){i->arg[0], 0, sz, i->cls}; sl = (Slice){i->arg[0], 0, sz, i->cls};
l = (Loc){LRoot, i-b->ins, b}; l = (Loc){LRoot, i-b->ins, b};
rebase(&sl);
i->arg[1] = def(sl, MASK(sz), b, i, &l); i->arg[1] = def(sl, MASK(sz), b, i, &l);
} }
qsort(ilog, nlog, sizeof ilog[0], icmp); qsort(ilog, nlog, sizeof ilog[0], icmp);

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;
} }

4
rega.c
View File

@ -16,7 +16,7 @@ struct RMap {
}; };
enum { enum {
NPm = 128, /* max copies in a parallel move */ NPm = 64, /* max copies in a parallel move */
}; };
static bits regu; /* registers used */ static bits regu; /* registers used */
@ -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;