aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cc.h4
-rw-r--r--decl.c8
-rw-r--r--map.c19
-rw-r--r--pp.c10
-rw-r--r--qbe.c8
-rw-r--r--scope.c28
-rw-r--r--util.h22
7 files changed, 46 insertions, 53 deletions
diff --git a/cc.h b/cc.h
index b75d511..4f72a68 100644
--- a/cc.h
+++ b/cc.h
@@ -297,8 +297,8 @@ struct decl {
};
struct scope {
- struct map *tags;
- struct map *decls;
+ struct map tags;
+ struct map decls;
struct block *breaklabel;
struct block *continuelabel;
struct switchcases *switchcases;
diff --git a/decl.c b/decl.c
index 34e61f4..975a8d5 100644
--- a/decl.c
+++ b/decl.c
@@ -1089,16 +1089,16 @@ decl(struct scope *s, struct func *f)
struct decl *
stringdecl(struct expr *expr)
{
- static struct map *strings;
+ static struct map strings;
struct mapkey key;
void **entry;
struct decl *d;
- if (!strings)
- strings = mkmap(64);
+ if (!strings.len)
+ mapinit(&strings, 64);
assert(expr->kind == EXPRSTRING);
mapkey(&key, expr->u.string.data, expr->u.string.size);
- entry = mapput(strings, &key);
+ entry = mapput(&strings, &key);
d = *entry;
if (!d) {
d = mkdecl(DECLOBJECT, expr->type, QUALNONE, LINKNONE);
diff --git a/map.c b/map.c
index 9464b38..2f34ae3 100644
--- a/map.c
+++ b/map.c
@@ -5,12 +5,6 @@
#include <string.h>
#include "util.h"
-struct map {
- size_t len, cap;
- struct mapkey *keys;
- void **vals;
-};
-
static uint64_t
hash(const void *ptr, size_t len)
{
@@ -31,31 +25,25 @@ mapkey(struct mapkey *k, const void *s, size_t n)
k->hash = hash(s, n);
}
-struct map *
-mkmap(size_t cap)
+void
+mapinit(struct map *h, size_t cap)
{
- struct map *h;
size_t i;
assert(!(cap & cap - 1));
- h = xmalloc(sizeof(*h));
h->len = 0;
h->cap = cap;
h->keys = xreallocarray(NULL, cap, sizeof(h->keys[0]));
h->vals = xreallocarray(NULL, cap, sizeof(h->vals[0]));
for (i = 0; i < cap; ++i)
h->keys[i].str = NULL;
-
- return h;
}
void
-delmap(struct map *h, void del(void *))
+mapfree(struct map *h, void del(void *))
{
size_t i;
- if (!h)
- return;
if (del) {
for (i = 0; i < h->cap; ++i) {
if (h->keys[i].str)
@@ -64,7 +52,6 @@ delmap(struct map *h, void del(void *))
}
free(h->keys);
free(h->vals);
- free(h);
}
static bool
diff --git a/pp.c b/pp.c
index 361cf69..09d1a06 100644
--- a/pp.c
+++ b/pp.c
@@ -51,14 +51,14 @@ struct frame {
enum ppflags ppflags;
static struct array ctx;
-static struct map *macros;
+static struct map macros;
/* number of macros currently undergoing expansion */
static size_t macrodepth;
void
ppinit(void)
{
- macros = mkmap(64);
+ mapinit(&macros, 64);
next();
}
@@ -112,7 +112,7 @@ macroget(char *name)
struct mapkey k;
mapkey(&k, name, strlen(name));
- return mapget(macros, &k);
+ return mapget(&macros, &k);
}
static void
@@ -272,7 +272,7 @@ define(void)
tok = *t;
mapkey(&k, m->name, strlen(m->name));
- entry = mapput(macros, &k);
+ entry = mapput(&macros, &k);
if (*entry && !macroequal(m, *entry))
error(&tok.loc, "redefinition of macro '%s'", m->name);
*entry = m;
@@ -288,7 +288,7 @@ undef(void)
name = tokencheck(&tok, TIDENT, "after #undef");
mapkey(&k, name, strlen(name));
- entry = mapput(macros, &k);
+ entry = mapput(&macros, &k);
m = *entry;
if (m) {
free(name);
diff --git a/qbe.c b/qbe.c
index 60e911f..8a19d46 100644
--- a/qbe.c
+++ b/qbe.c
@@ -89,7 +89,7 @@ struct func {
char *name;
struct type *type;
struct block *start, *end;
- struct map *gotos;
+ struct map gotos;
unsigned lastid;
};
@@ -481,8 +481,8 @@ mkfunc(struct decl *decl, char *name, struct type *t, struct scope *s)
f->name = name;
f->type = t;
f->start = f->end = mkblock("start");
- f->gotos = mkmap(8);
f->lastid = 0;
+ mapinit(&f->gotos, 8);
emittype(t->base);
/* allocate space for parameters */
@@ -528,7 +528,7 @@ delfunc(struct func *f)
free(b->insts.val);
free(b);
}
- delmap(f->gotos, free);
+ mapfree(&f->gotos, free);
free(f);
}
@@ -600,7 +600,7 @@ funcgoto(struct func *f, char *name)
struct mapkey key;
mapkey(&key, name, strlen(name));
- entry = mapput(f->gotos, &key);
+ entry = mapput(&f->gotos, &key);
g = *entry;
if (!g) {
g = xmalloc(sizeof(*g));
diff --git a/scope.c b/scope.c
index ab6e293..d48658b 100644
--- a/scope.c
+++ b/scope.c
@@ -44,8 +44,8 @@ mkscope(struct scope *parent)
struct scope *s;
s = xmalloc(sizeof(*s));
- s->decls = NULL;
- s->tags = NULL;
+ s->decls.len = 0;
+ s->tags.len = 0;
s->breaklabel = parent->breaklabel;
s->continuelabel = parent->continuelabel;
s->switchcases = parent->switchcases;
@@ -59,10 +59,10 @@ delscope(struct scope *s)
{
struct scope *parent = s->parent;
- if (s->decls)
- delmap(s->decls, NULL);
- if (s->tags)
- delmap(s->tags, NULL);
+ if (s->decls.len)
+ mapfree(&s->decls, NULL);
+ if (s->tags.len)
+ mapfree(&s->tags, NULL);
free(s);
return parent;
@@ -76,7 +76,7 @@ scopegetdecl(struct scope *s, const char *name, bool recurse)
mapkey(&k, name, strlen(name));
do {
- d = s->decls ? mapget(s->decls, &k) : NULL;
+ d = s->decls.len ? mapget(&s->decls, &k) : NULL;
s = s->parent;
} while (!d && s && recurse);
@@ -91,7 +91,7 @@ scopegettag(struct scope *s, const char *name, bool recurse)
mapkey(&k, name, strlen(name));
do {
- t = s->tags ? mapget(s->tags, &k) : NULL;
+ t = s->tags.len ? mapget(&s->tags, &k) : NULL;
s = s->parent;
} while (!t && s && recurse);
@@ -103,10 +103,10 @@ scopeputdecl(struct scope *s, const char *name, struct decl *d)
{
struct mapkey k;
- if (!s->decls)
- s->decls = mkmap(32);
+ if (!s->decls.len)
+ mapinit(&s->decls, 32);
mapkey(&k, name, strlen(name));
- *mapput(s->decls, &k) = d;
+ *mapput(&s->decls, &k) = d;
}
void
@@ -114,8 +114,8 @@ scopeputtag(struct scope *s, const char *name, struct type *t)
{
struct mapkey k;
- if (!s->tags)
- s->tags = mkmap(32);
+ if (!s->tags.len)
+ mapinit(&s->tags, 32);
mapkey(&k, name, strlen(name));
- *mapput(s->tags, &k) = t;
+ *mapput(&s->tags, &k) = t;
}
diff --git a/util.h b/util.h
index 615a4e4..4108075 100644
--- a/util.h
+++ b/util.h
@@ -7,12 +7,6 @@ struct array {
size_t len, cap;
};
-struct mapkey {
- uint64_t hash;
- const void *str;
- size_t len;
-};
-
struct treenode {
unsigned long long key;
void *child[2];
@@ -47,9 +41,21 @@ void *arraylast(struct array *, size_t);
/* map */
+struct map {
+ size_t len, cap;
+ struct mapkey *keys;
+ void **vals;
+};
+
+struct mapkey {
+ uint64_t hash;
+ const void *str;
+ size_t len;
+};
+
void mapkey(struct mapkey *, const void *, size_t);
-struct map *mkmap(size_t);
-void delmap(struct map *, void(void *));
+void mapinit(struct map *, size_t);
+void mapfree(struct map *, void(void *));
void **mapput(struct map *, struct mapkey *);
void *mapget(struct map *, struct mapkey *);