commit e30fab31e1935dc5a4154a0dab9cc61c8708953c
parent cb4d738c36072b33a1068c79c6f24372a7b6d6e2
Author: Quentin Carbonneaux <quentin.carbonneaux@yale.edu>
Date: Mon, 5 Oct 2015 17:23:54 -0400
factor some utility functions/data in util.c
Diffstat:
7 files changed, 97 insertions(+), 101 deletions(-)
diff --git a/lisc/Makefile b/lisc/Makefile
@@ -1,5 +1,5 @@
BIN = lisc
-OBJ = main.o parse.o ssa.o live.o isel.o spill.o rega.o emit.o
+OBJ = main.o util.o parse.o ssa.o live.o isel.o spill.o rega.o emit.o
CFLAGS = -Wall -Wextra -std=c99 -g -pedantic
diff --git a/lisc/isel.c b/lisc/isel.c
@@ -13,16 +13,6 @@
* constant allocX in the first basic block)
*/
-extern Ins insb[NIns], *curi; /* shared work buffer */
-
-static void
-emit(int op, int w, Ref to, Ref arg0, Ref arg1)
-{
- if (curi == insb)
- diag("isel: too many instructions");
- *--curi = (Ins){op, w, to, {arg0, arg1}};
-}
-
static Ref
newtmp(Fn *fn)
{
@@ -190,7 +180,7 @@ sel(Ins i, Fn *fn)
}
n = 0;
Emit:
- emit(i.op, w, i.to, i.arg[0], i.arg[1]);
+ emiti(i);
while (n--) {
/* load constants that do not fit in
* a 32bit signed integer into a
diff --git a/lisc/lisc.h b/lisc/lisc.h
@@ -18,8 +18,6 @@ typedef struct Con Con;
typedef struct Fn Fn;
typedef struct Typ Typ;
-typedef enum { U, F, T } B3;
-
enum Reg {
RXX,
@@ -270,12 +268,18 @@ struct Typ {
extern char debug['Z'+1];
void dumpts(Bits *, Tmp *, FILE *);
-/* parse.c */
+/* util.c */
extern Typ typ[NTyp];
-extern OpDesc opdesc[NOp];
+extern Ins insb[NIns], *curi;
void diag(char *);
void *alloc(size_t);
-Blk *blocka(void);
+Blk *blocka();
+void emit(int, int, Ref, Ref, Ref);
+void emiti(Ins);
+int bcnt(Bits *);
+
+/* parse.c */
+extern OpDesc opdesc[NOp];
Fn *parse(FILE *);
void printfn(Fn *, FILE *);
@@ -299,7 +303,6 @@ ulong callclb(Ins, int *);
void isel(Fn *);
/* spill.c */
-int bcnt(Bits *);
void fillcost(Fn *);
void spill(Fn *);
diff --git a/lisc/parse.c b/lisc/parse.c
@@ -8,8 +8,6 @@ enum {
NCon = 256,
};
-Ins insb[NIns], *curi;
-
OpDesc opdesc[NOp] = {
/* NAME NM */
[OAdd] = { "add", 2 },
@@ -55,9 +53,6 @@ OpDesc opdesc[NOp] = {
#undef X
};
-Typ typ[NTyp];
-static int ntyp;
-
typedef enum {
PXXX,
PLbl,
@@ -117,31 +112,9 @@ static Blk *curb;
static Blk **blink;
static int nblk;
static int rcls;
+static int ntyp;
-void *
-alloc(size_t n)
-{
- void *p;
-
- /* todo, export in util.c */
- if (n == 0)
- return 0;
- p = calloc(1, n);
- if (!p)
- abort();
- return p;
-}
-
-void
-diag(char *s)
-{
- /* todo, export in util.c */
- fputs(s, stderr);
- fputc('\n', stderr);
- abort();
-}
-
static void
err(char *s)
{
@@ -318,18 +291,6 @@ expect(int t)
err(buf);
}
-Blk *
-blocka()
-{
- static Blk zblock;
- Blk *b;
-
- b = alloc(sizeof *b);
- *b = zblock;
- b->id = nblk++;
- return b;
-}
-
static Ref
tmpref(char *v, int use)
{
@@ -452,6 +413,7 @@ findblk(char *name)
err("too many blocks");
if (!bmap[i]) {
bmap[i] = blocka();
+ nblk++;
strcpy(bmap[i]->name, name);
}
return bmap[i];
diff --git a/lisc/rega.c b/lisc/rega.c
@@ -13,7 +13,6 @@ struct RMap {
int n;
};
-extern Ins insb[NIns], *curi;
static ulong regu; /* registers used */
static Tmp *tmp; /* function temporaries */
static struct {
diff --git a/lisc/spill.c b/lisc/spill.c
@@ -111,35 +111,6 @@ fillcost(Fn *fn)
}
}
-int
-bcnt(Bits *b) /* glad I can pull it :) */
-{
- const uint64_t m1 = 0x5555555555555555;
- const uint64_t m2 = 0x3333333333333333;
- const uint64_t m3 = 0x0f0f0f0f0f0f0f0f;
- const uint64_t m4 = 0x00ff00ff00ff00ff;
- const uint64_t m5 = 0x0000ffff0000ffff;
- const uint64_t m6 = 0x00000000ffffffff;
- uint64_t tmp;
- int z, i;
-
- i = 0;
- for (z=0; z<BITS; z++) {
- tmp = b->t[z];
- if (!tmp)
- continue;
- tmp = (tmp&m1) + (tmp>> 1&m1);
- tmp = (tmp&m2) + (tmp>> 2&m2);
- tmp = (tmp&m3) + (tmp>> 4&m3);
- tmp = (tmp&m4) + (tmp>> 8&m4);
- tmp = (tmp&m5) + (tmp>>16&m5);
- tmp = (tmp&m6) + (tmp>>32&m6);
- i += tmp;
- }
- return i;
-}
-
-extern Ins insb[NIns], *curi; /* shared work buffer */
static Bits *f; /* temps to prioritize in registers (for tcmp1) */
static Tmp *tmp; /* current temporaries (for tcmpX) */
static int ntmp; /* current # of temps (for limit) */
@@ -198,20 +169,12 @@ slot(int t)
}
static void
-emit(Ins i)
-{
- if (curi == insb)
- diag("spill: too many instructions");
- *--curi = i;
-}
-
-static void
store(Ref r, int s)
{
if (tmp[r.val].wide)
- emit((Ins){OStorel, 0, R, {r, SLOT(s)}});
+ emit(OStorel, 0, R, r, SLOT(s));
else
- emit((Ins){OStorew, 0, R, {r, SLOT(s)}});
+ emit(OStorew, 0, R, r, SLOT(s));
}
static int
@@ -248,7 +211,7 @@ limit(Bits *b, int k, Bits *fst)
if (curi) {
t = tarr[i];
w = tmp[t].wide;
- emit((Ins){OLoad, w, TMP(t), {slot(t)}});
+ emit(OLoad, w, TMP(t), slot(t), R);
}
}
return t;
@@ -304,7 +267,7 @@ dopm(Blk *b, Ins *i, Bits *v)
} else
limit(v, NReg, 0);
do
- emit(*--i1);
+ emiti(*--i1);
while (i1 != i);
return i;
}
@@ -423,7 +386,7 @@ spill(Fn *fn)
j -= setloc(&i->arg[1], &v, &w);
if (s != -1)
store(i->to, s);
- emit(*i);
+ emiti(*i);
}
for (p=b->phi; p; p=p->link) {
diff --git a/lisc/util.c b/lisc/util.c
@@ -0,0 +1,79 @@
+#include "lisc.h"
+
+Typ typ[NTyp];
+int ntyp;
+Ins insb[NIns], *curi;
+
+void
+diag(char *s)
+{
+ fputs(s, stderr);
+ fputc('\n', stderr);
+ abort();
+}
+
+void *
+alloc(size_t n)
+{
+ void *p;
+
+ if (n == 0)
+ return 0;
+ p = calloc(1, n);
+ if (!p)
+ abort();
+ return p;
+}
+
+Blk *
+blocka()
+{
+ static Blk z;
+ Blk *b;
+
+ b = alloc(sizeof *b);
+ *b = z;
+ return b;
+}
+
+void
+emit(int op, int w, Ref to, Ref arg0, Ref arg1)
+{
+ if (curi == insb)
+ diag("emit: too many instructions");
+ *--curi = (Ins){op, w, to, {arg0, arg1}};
+}
+
+void
+emiti(Ins i)
+{
+ emit(i.op, i.wide, i.to, i.arg[0], i.arg[1]);
+}
+
+int
+bcnt(Bits *b)
+{
+ const uint64_t m1 = 0x5555555555555555;
+ const uint64_t m2 = 0x3333333333333333;
+ const uint64_t m3 = 0x0f0f0f0f0f0f0f0f;
+ const uint64_t m4 = 0x00ff00ff00ff00ff;
+ const uint64_t m5 = 0x0000ffff0000ffff;
+ const uint64_t m6 = 0x00000000ffffffff;
+ uint64_t tmp;
+ int z, i;
+
+ i = 0;
+ for (z=0; z<BITS; z++) {
+ tmp = b->t[z];
+ if (!tmp)
+ continue;
+ tmp = (tmp&m1) + (tmp>> 1&m1);
+ tmp = (tmp&m2) + (tmp>> 2&m2);
+ tmp = (tmp&m3) + (tmp>> 4&m3);
+ tmp = (tmp&m4) + (tmp>> 8&m4);
+ tmp = (tmp&m5) + (tmp>>16&m5);
+ tmp = (tmp&m6) + (tmp>>32&m6);
+ i += tmp;
+ }
+ return i;
+}