diff options
author | Quentin Carbonneaux <quentin.carbonneaux@yale.edu> | 2015-10-06 20:42:54 -0400 |
---|---|---|
committer | Quentin Carbonneaux <quentin.carbonneaux@yale.edu> | 2015-10-06 20:42:54 -0400 |
commit | 99ad19546d983957d4bdb5596fc323a260d73fa6 (patch) | |
tree | abcb6fb12dc903d55a607d414f23d1cf7a58650c /lisc/util.c | |
parent | 1f618737993bd95cc94cdec0142d95935823a665 (diff) | |
download | roux-99ad19546d983957d4bdb5596fc323a260d73fa6.tar.gz |
use new vector functions instead of reallocs
Diffstat (limited to 'lisc/util.c')
-rw-r--r-- | lisc/util.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lisc/util.c b/lisc/util.c index e62fd1a..399c144 100644 --- a/lisc/util.c +++ b/lisc/util.c @@ -1,5 +1,23 @@ #include "lisc.h" +typedef struct Vec Vec; + +struct Vec { + ulong mag; + size_t esz; + ulong cap; + union { + long long ll; + long double ld; + void *ptr; + } align[]; +}; + +enum { + VMin = 2, + VMag = 0xcabba9e, +}; + Typ typ[NTyp]; Ins insb[NIns], *curi; @@ -80,3 +98,61 @@ icpy(Ins *d, Ins *s, ulong n) memcpy(d, s, n * sizeof(Ins)); return d + n; } + +void * +valloc(ulong len, size_t esz) +{ + ulong cap; + Vec *v; + + v = alloc(len * esz + sizeof(Vec)); + v->mag = VMag; + for (cap=VMin; cap<len; cap*=2) + ; + v->cap = cap; + v->esz = esz; + return v + 1; +} + +void +vgrow(void *vp, ulong len) +{ + Vec *v; + void *v1; + + v = *(Vec **)vp - 1; + assert(v+1 && v->mag == VMag); + if (v->cap >= len) + return; + v1 = valloc(len, v->esz); + memcpy(v1, v+1, v->cap * v->esz); + free(v); + *(Vec **)vp = v1; +} + +Ref +newtmp(char *prfx, Fn *fn) +{ + static int n; + int t; + + t = fn->ntmp++; + vgrow(&fn->tmp, fn->ntmp); + sprintf(fn->tmp[t].name, "%s%d", prfx, ++n); + fn->tmp[t].spill = -1; + return TMP(t); +} + +Ref +getcon(int64_t val, Fn *fn) +{ + int c; + + for (c=0; c<fn->ncon; c++) + if (fn->con[c].type == CNum && fn->con[c].val == val) + return CON(c); + fn->ncon++; + vgrow(&fn->con, fn->ncon); + fn->con[c] = (Con){.type = CNum, .val = val}; + return CON(c); +} |