switch to tmpfile so it works on musl

This commit is contained in:
sam 2024-07-24 21:01:29 +12:00
parent e154997862
commit d2f4528797
2 changed files with 89 additions and 87 deletions

View file

@ -14,7 +14,7 @@ OBJ = $(COMMOBJ) $(AMD64OBJ) $(ARM64OBJ) $(RV64OBJ)
SRCALL = $(OBJ:.o=.c) SRCALL = $(OBJ:.o=.c)
CC = cc CC = cc
AR = ar AR = ar
CFLAGS = -std=c99 -g -Wall -Wextra -Wpedantic CFLAGS = -std=c99 -g -Wall -Wextra -Wpedantic
libqbe.a: $(OBJ) libqbe.a: $(OBJ)

174
libqbe.c
View file

@ -6,16 +6,16 @@
Target T; Target T;
char debug['Z'+1] = { char debug['Z'+1] = {
['P'] = 0, /* parsing */ ['P'] = 0, /* parsing */
['M'] = 0, /* memory optimization */ ['M'] = 0, /* memory optimization */
['N'] = 0, /* ssa construction */ ['N'] = 0, /* ssa construction */
['C'] = 0, /* copy elimination */ ['C'] = 0, /* copy elimination */
['F'] = 0, /* constant folding */ ['F'] = 0, /* constant folding */
['A'] = 0, /* abi lowering */ ['A'] = 0, /* abi lowering */
['I'] = 0, /* instruction selection */ ['I'] = 0, /* instruction selection */
['L'] = 0, /* liveness */ ['L'] = 0, /* liveness */
['S'] = 0, /* spilling */ ['S'] = 0, /* spilling */
['R'] = 0, /* reg. allocation */ ['R'] = 0, /* reg. allocation */
}; };
static FILE *outf; static FILE *outf;
@ -24,104 +24,106 @@ static int dbg;
static void static void
data(Dat *d) data(Dat *d)
{ {
if (dbg) if (dbg)
return; return;
emitdat(d, outf); emitdat(d, outf);
if (d->type == DEnd) { if (d->type == DEnd) {
fputs("/* end data */\n\n", outf); fputs("/* end data */\n\n", outf);
freeall(); freeall();
} }
} }
static void static void
func(Fn *fn) func(Fn *fn)
{ {
uint n; uint n;
if (dbg) if (dbg)
fprintf(stderr, "**** Function %s ****", fn->name); fprintf(stderr, "**** Function %s ****", fn->name);
if (debug['P']) { if (debug['P']) {
fprintf(stderr, "\n> After parsing:\n"); fprintf(stderr, "\n> After parsing:\n");
printfn(fn, stderr); printfn(fn, stderr);
} }
T.abi0(fn); T.abi0(fn);
fillrpo(fn); fillrpo(fn);
fillpreds(fn); fillpreds(fn);
filluse(fn); filluse(fn);
promote(fn); promote(fn);
filluse(fn); filluse(fn);
ssa(fn); ssa(fn);
filluse(fn); filluse(fn);
ssacheck(fn); ssacheck(fn);
fillalias(fn); fillalias(fn);
loadopt(fn); loadopt(fn);
filluse(fn); filluse(fn);
fillalias(fn); fillalias(fn);
coalesce(fn); coalesce(fn);
filluse(fn); filluse(fn);
ssacheck(fn); ssacheck(fn);
copy(fn); copy(fn);
filluse(fn); filluse(fn);
fold(fn); fold(fn);
T.abi1(fn); T.abi1(fn);
simpl(fn); simpl(fn);
fillpreds(fn); fillpreds(fn);
filluse(fn); filluse(fn);
T.isel(fn); T.isel(fn);
fillrpo(fn); fillrpo(fn);
filllive(fn); filllive(fn);
fillloop(fn); fillloop(fn);
fillcost(fn); fillcost(fn);
spill(fn); spill(fn);
rega(fn); rega(fn);
fillrpo(fn); fillrpo(fn);
simpljmp(fn); simpljmp(fn);
fillpreds(fn); fillpreds(fn);
fillrpo(fn); fillrpo(fn);
assert(fn->rpo[0] == fn->start); assert(fn->rpo[0] == fn->start);
for (n=0;; n++) for (n=0;; n++)
if (n == fn->nblk-1) { if (n == fn->nblk-1) {
fn->rpo[n]->link = 0; fn->rpo[n]->link = 0;
break; break;
} else } else
fn->rpo[n]->link = fn->rpo[n+1]; fn->rpo[n]->link = fn->rpo[n+1];
if (!dbg) { if (!dbg) {
T.emitfn(fn, outf); T.emitfn(fn, outf);
fprintf(outf, "/* end function %s */\n\n", fn->name); fprintf(outf, "/* end function %s */\n\n", fn->name);
} else } else
fprintf(stderr, "\n"); fprintf(stderr, "\n");
freeall(); freeall();
} }
static void static void
dbgfile(char *fn) dbgfile(char *fn)
{ {
emitdbgfile(fn, outf); emitdbgfile(fn, outf);
} }
const char* const char*
qbe_emit(Target target, const char* ssa) qbe_emit(Target target, const char* ssa)
{ {
T = target; T = target;
FILE *inf = fmemopen((void*)ssa, strlen(ssa), "r"); FILE *inf = tmpfile();
outf = tmpfile(); fputs(ssa, inf);
char* f = "-"; rewind(inf);
outf = tmpfile();
char* f = "-";
parse(inf, f, dbgfile, data, func); parse(inf, f, dbgfile, data, func);
fclose(inf); fclose(inf);
if (!dbg) if (!dbg)
T.emitfin(outf); T.emitfin(outf);
fseek(outf, 0, SEEK_END); fseek(outf, 0, SEEK_END);
long size = ftell(outf); long size = ftell(outf);
fseek(outf, 0, SEEK_SET); fseek(outf, 0, SEEK_SET);
char* buf = malloc(size + 1); char* buf = malloc(size + 1);
fread(buf, size, 1, outf); fread(buf, size, 1, outf);
buf[size] = '\0'; buf[size] = '\0';
fclose(outf); fclose(outf);
return buf; return buf;
} }