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

16
copy.c
View File

@ -388,14 +388,20 @@ phicopyref(Fn *fn, Blk *b, Phi *p)
{
Blk *d, **s;
Phi *p1;
Ref r;
uint n, c;
/* identical args */
for (n=0; n<p->narg-1; n++)
if (!req(p->arg[n], p->arg[n+1]))
break;
if (n == p->narg-1)
return p->arg[n];
r = R;
for (n=0; n<p->narg; n++)
if (!req(p->arg[n], p->to)) {
if (req(r, R))
r = p->arg[n];
else if (!req(p->arg[n], r))
break;
}
if (n == p->narg)
return r;
/* same as a previous phi */
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;
}
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
killsl(Ref r, Slice sl)
{
@ -431,6 +448,7 @@ loadopt(Fn *fn)
sz = loadsz(i);
sl = (Slice){i->arg[0], 0, sz, i->cls};
l = (Loc){LRoot, i-b->ins, b};
rebase(&sl);
i->arg[1] = def(sl, MASK(sz), b, i, &l);
}
qsort(ilog, nlog, sizeof ilog[0], icmp);

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

4
rega.c
View File

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