summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--all.h11
-rw-r--r--cfg.c2
-rw-r--r--fold.c2
-rw-r--r--load.c4
-rw-r--r--main.c5
-rw-r--r--parse.c10
-rw-r--r--ssa.c2
-rw-r--r--util.c14
8 files changed, 29 insertions, 21 deletions
diff --git a/all.h b/all.h
index d2763eb..7243e09 100644
--- a/all.h
+++ b/all.h
@@ -490,13 +490,14 @@ struct Dat {
 
 
 /* main.c */
-enum Asm {
-	Gasmacho,
-	Gaself,
-};
 extern char debug['Z'+1];
 
 /* util.c */
+typedef enum {
+	Pheap, /* free() necessary */
+	Pfn, /* discarded after processing the function */
+} Pool;
+
 extern Typ typ[NTyp];
 extern Ins insb[NIns], *curi;
 void die_(char *, char *, ...) __attribute__((noreturn));
@@ -507,7 +508,7 @@ void emit(int, int, Ref, Ref, Ref);
 void emiti(Ins);
 void idup(Ins **, Ins *, ulong);
 Ins *icpy(Ins *, Ins *, ulong);
-void *vnew(ulong, size_t, void *(size_t));
+void *vnew(ulong, size_t, Pool);
 void vfree(void *);
 void vgrow(void *, ulong);
 int clsmerge(short *, short);
diff --git a/cfg.c b/cfg.c
index bebf0fa..583629b 100644
--- a/cfg.c
+++ b/cfg.c
@@ -207,7 +207,7 @@ addfron(Blk *a, Blk *b)
 		if (a->fron[n] == b)
 			return;
 	if (!a->nfron)
-		a->fron = vnew(++a->nfron, sizeof a->fron[0], alloc);
+		a->fron = vnew(++a->nfron, sizeof a->fron[0], Pfn);
 	else
 		vgrow(&a->fron, ++a->nfron);
 	a->fron[a->nfron-1] = b;
diff --git a/fold.c b/fold.c
index a000ff4..110a23c 100644
--- a/fold.c
+++ b/fold.c
@@ -193,7 +193,7 @@ fold(Fn *fn)
 
 	val = emalloc(fn->ntmp * sizeof val[0]);
 	edge = emalloc(fn->nblk * sizeof edge[0]);
-	usewrk = vnew(0, sizeof usewrk[0], emalloc);
+	usewrk = vnew(0, sizeof usewrk[0], Pheap);
 
 	for (n=0; n<fn->ntmp; n++)
 		val[n] = Top;
diff --git a/load.c b/load.c
index 56204dc..be6b53d 100644
--- a/load.c
+++ b/load.c
@@ -336,7 +336,7 @@ loadopt(Fn *fn)
 	Loc l;
 
 	curf = fn;
-	ilog = vnew(0, sizeof ilog[0], emalloc);
+	ilog = vnew(0, sizeof ilog[0], Pheap);
 	nlog = 0;
 	inum = 0;
 	for (b=fn->start; b; b=b->link)
@@ -351,7 +351,7 @@ loadopt(Fn *fn)
 	qsort(ilog, nlog, sizeof ilog[0], icmp);
 	vgrow(&ilog, nlog+1);
 	ilog[nlog].bid = fn->nblk; /* add a sentinel */
-	ib = vnew(0, sizeof(Ins), emalloc);
+	ib = vnew(0, sizeof(Ins), Pheap);
 	for (ist=ilog, n=0; n<fn->nblk; ++n) {
 		b = fn->rpo[n];
 		for (; ist->bid == n && ist->isphi; ++ist) {
diff --git a/main.c b/main.c
index f8d367f..fd0b6e4 100644
--- a/main.c
+++ b/main.c
@@ -3,6 +3,11 @@
 #include <ctype.h>
 #include <getopt.h>
 
+enum Asm {
+	Gasmacho,
+	Gaself,
+};
+
 char debug['Z'+1] = {
 	['P'] = 0, /* parsing */
 	['A'] = 0, /* abi lowering */
diff --git a/parse.c b/parse.c
index a435754..f8fd705 100644
--- a/parse.c
+++ b/parse.c
@@ -308,7 +308,7 @@ lex()
 		return Tint;
 	}
 	if (c == '"') {
-		tokval.str = vnew(0, 1, alloc);
+		tokval.str = vnew(0, 1, Pfn);
 		esc = 0;
 		for (i=0;; i++) {
 			c = fgetc(inf);
@@ -810,8 +810,8 @@ parsefn(int export)
 	curf = alloc(sizeof *curf);
 	curf->ntmp = 0;
 	curf->ncon = 1; /* first constant must be 0 */
-	curf->tmp = vnew(curf->ntmp, sizeof curf->tmp[0], alloc);
-	curf->con = vnew(curf->ncon, sizeof curf->con[0], alloc);
+	curf->tmp = vnew(curf->ntmp, sizeof curf->tmp[0], Pfn);
+	curf->con = vnew(curf->ncon, sizeof curf->con[0], Pfn);
 	for (i=0; i<Tmp0; ++i)
 		newtmp(0, i < XMM0 ? Kl : Kd, curf);
 	curf->con[0].type = CBits;
@@ -836,7 +836,7 @@ parsefn(int export)
 		err("empty function");
 	if (curb->jmp.type == Jxxx)
 		err("last block misses jump");
-	curf->mem = vnew(0, sizeof curf->mem[0], alloc);
+	curf->mem = vnew(0, sizeof curf->mem[0], Pfn);
 	curf->nmem = 0;
 	curf->nblk = nblk;
 	curf->rpo = 0;
@@ -950,7 +950,7 @@ parsetyp()
 		return;
 	}
 	n = 0;
-	ty->seg = vnew(1, sizeof ty->seg[0], emalloc);
+	ty->seg = vnew(1, sizeof ty->seg[0], Pheap);
 	if (t == Tlbrace)
 		do {
 			if (t != Tlbrace)
diff --git a/ssa.c b/ssa.c
index a040484..c0c340c 100644
--- a/ssa.c
+++ b/ssa.c
@@ -52,7 +52,7 @@ filluse(Fn *fn)
 		tmp[t].phi = 0;
 		tmp[t].cls = 0;
 		if (tmp[t].use == 0)
-			tmp[t].use = vnew(0, sizeof(Use), alloc);
+			tmp[t].use = vnew(0, sizeof(Use), Pfn);
 	}
 	for (b=fn->start; b; b=b->link) {
 		for (p=b->phi; p; p=p->link) {
diff --git a/util.c b/util.c
index 380f139..9b73771 100644
--- a/util.c
+++ b/util.c
@@ -6,7 +6,7 @@ typedef struct Vec Vec;
 
 struct Vec {
 	ulong mag;
-	void *(*alloc)();
+	Pool pool;
 	size_t esz;
 	ulong cap;
 	union {
@@ -119,18 +119,20 @@ icpy(Ins *d, Ins *s, ulong n)
 }
 
 void *
-vnew(ulong len, size_t esz, void *alloc(size_t))
+vnew(ulong len, size_t esz, Pool pool)
 {
+	void *(*f)(size_t);
 	ulong cap;
 	Vec *v;
 
 	for (cap=VMin; cap<len; cap*=2)
 		;
-	v = alloc(cap * esz + sizeof(Vec));
+	f = pool == Pheap ? emalloc : alloc;
+	v = f(cap * esz + sizeof(Vec));
 	v->mag = VMag;
 	v->cap = cap;
 	v->esz = esz;
-	v->alloc = alloc;
+	v->pool = pool;
 	return v + 1;
 }
 
@@ -141,7 +143,7 @@ vfree(void *p)
 
 	v = (Vec *)p - 1;
 	assert(v->mag == VMag);
-	if (v->alloc == emalloc) {
+	if (v->pool == Pheap) {
 		v->mag = 0;
 		free(v);
 	}
@@ -157,7 +159,7 @@ vgrow(void *vp, ulong len)
 	assert(v+1 && v->mag == VMag);
 	if (v->cap >= len)
 		return;
-	v1 = vnew(len, v->esz, v->alloc);
+	v1 = vnew(len, v->esz, v->pool);
 	memcpy(v1, v+1, v->cap * v->esz);
 	vfree(v+1);
 	*(Vec **)vp = v1;