aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/antcc.h128
-rw-r--r--src/c.c1
-rw-r--r--src/c_lex.c1
-rw-r--r--src/c_type.c1
-rw-r--r--src/ir.c1
-rw-r--r--src/ir_cse.c1
-rw-r--r--src/ir_mem2reg.c1
-rw-r--r--src/ir_regalloc.c1
-rw-r--r--src/u_bits.h76
-rw-r--r--src/u_hash.h23
-rw-r--r--src/u_io.c1
-rw-r--r--src/u_mem.c18
12 files changed, 136 insertions, 117 deletions
diff --git a/src/antcc.h b/src/antcc.h
index a24aa3d..2e6c2d7 100644
--- a/src/antcc.h
+++ b/src/antcc.h
@@ -53,60 +53,46 @@ void _assertfmt(const char *file, int line, const char *func, const char *expr);
#define assert(x) (void)(!(x) ? _assertfmt(__FILE__,__LINE__,__func__,#x), *(volatile int *)0 : 0)
#endif
-static inline size_t
-hashs(size_t h, const char *s)
-{
- while (*s) h = (uchar)*s++ + h*65599;
- return h;
-}
-static inline size_t
-hashb(size_t h, const void *d, size_t n)
-{
- const uchar *b = d;
- while (n--) h = *b++ + h*65599;
- return h;
-}
-static inline size_t
-ptrhash(const void *p) {
- return (size_t)p * 2654435761u;
-}
-static inline uint
-popcnt(uvlong x) {
#if HAS_BUILTIN(popcountll)
- return x ? __builtin_popcountll(x) : 0;
+#define popcnt(x) __builtin_popcountll(x)
#else
+static inline uint
+popcnt(uvlong x) {
uint n = 0;
while (x) n += x&1, x >>= 1;
return n;
-#endif
}
+#endif
+
static inline bool
ispo2(uvlong x) {
return (x != 0) & ((x & (x - 1)) == 0);
}
-static inline uint
-ilog2(uvlong x) { /* assumes x is a power of 2 */
+
#if HAS_BUILTIN(ctzll)
- return __builtin_ctzll(x);
+#define ilog2(x) __builtin_ctzll(x)
#else
+static inline uint
+ilog2(uvlong x) { /* assumes x is a power of 2 */
uint n = 0;
while (x >>= 1) ++n;
return n;
-#endif
}
+#endif
+
+#if HAS_BUILTIN(ctzll)
+#define lowestsetbit(x) __builtin_ctzll(x)
+#else
static inline uint
lowestsetbit(uvlong x)
{
-#if HAS_BUILTIN(ctzll)
- return __builtin_ctzll(x);
-#else
int i = 0;
for (uvlong mask = 1;; ++i, mask <<= 1)
if (x & mask)
break;
return i;
-#endif
}
+#endif
#define aisprint(c) in_range(c, ' ', '~')
#define aisdigit(c) in_range(c, '0', '9')
@@ -187,13 +173,10 @@ void targ_init(const char *);
/*********/
/* libc *alloc wrappers */
-void *xmalloc(size_t n, const char *);
-void *xcalloc(size_t n, const char *);
-void *xrealloc(void *, size_t n, const char *);
+void *xmalloc(size_t);
+void *xcalloc(size_t);
+void *xrealloc(void *, size_t);
void free(void *);
-#define xmalloc(n) xmalloc(n, __func__)
-#define xcalloc(n) xcalloc(n, __func__)
-#define xrealloc(p,n) xrealloc(p, n, __func__)
/* string interning */
typedef const struct internstr {char c;} *internstr;
@@ -312,84 +295,11 @@ extern char pmap_tombstone_[];
for (size_t _i = 0; _i < (m)->mb.N && ((kx) = (m)->mb.k[_i], (pvx) = &(m)->v[_i], 1); ++_i) \
if (kx && kx != pmap_tombstone_)
-/** bitset **/
+/** bitset (u_bits.h) **/
struct bitset { size_t u; };
enum { BSNBIT = 8 * sizeof(struct bitset) };
#define BSSIZE(nbit) ((nbit)/BSNBIT + ((nbit)%BSNBIT != 0))
-static inline bool
-bstest(const struct bitset *bs, uint i)
-{
- return bs[i/BSNBIT].u >> i%BSNBIT & 1;
-}
-
-static inline void
-bsset(struct bitset *bs, uint i)
-{
- bs[i/BSNBIT].u |= 1ull << i%BSNBIT;
-}
-
-static inline void
-bsclr(struct bitset *bs, uint i)
-{
- bs[i/BSNBIT].u &= ~(1ull << i%BSNBIT);
-}
-
-static inline void
-bszero(struct bitset bs[/*siz*/], uint siz)
-{
- memset(bs, 0, siz * sizeof *bs);
-}
-
-static inline void
-bscopy(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz)
-{
- while (siz--) dst++->u = src++->u;
-}
-
-static inline void
-bsunion(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz)
-{
- while (siz--) dst++->u |= src++->u;
-}
-
-static inline uint
-bscount(struct bitset bs[/*siz*/], uint siz)
-{
- uint n = 0;
- while (siz--) n += popcnt(bs++->u);
- return n;
-}
-
-static inline bool
-bsiter(uint *i, struct bitset bs[/*siz*/], uint siz)
-{
- uint k = *i/BSNBIT, j = *i%BSNBIT;
- if (k >= siz) return 0;
- size_t t = bs[k].u & ~(((size_t)1 << j) - 1);
- while (!t) {
- if (++k >= siz) return 0;
- t = bs[k].u;
- }
- *i = k*BSNBIT + lowestsetbit(t);
- return 1;
-}
-#define bs_each(T, var, bs, siz) for (T (var) = 0; bsiter(&(var), (bs), (siz)); ++(var))
-
-static inline bool
-bsiterzr(uint *i, struct bitset bs[/*siz*/], uint siz)
-{
- uint k = *i/BSNBIT, j = *i%BSNBIT;
- if (k >= siz) return 0;
- size_t t = ~bs[k].u & ~(((size_t)1 << j) - 1);
- while (!t) {
- if (++k >= siz) return 0;
- t = ~bs[k].u;
- }
- *i = k*BSNBIT + lowestsetbit(t);
- return 1;
-}
-
/********/
/** IO **/
/********/
diff --git a/src/c.c b/src/c.c
index 80ddc74..4d994e3 100644
--- a/src/c.c
+++ b/src/c.c
@@ -1,6 +1,7 @@
#include "c.h"
#include "c_lex.h"
#include "u_endian.h"
+#include "u_bits.h"
#include "ir.h"
#include "obj.h"
diff --git a/src/c_lex.c b/src/c_lex.c
index d3529c0..8b707fe 100644
--- a/src/c_lex.c
+++ b/src/c_lex.c
@@ -1,5 +1,6 @@
#include "c_lex.h"
#include "version.h"
+#include "u_bits.h"
#include <string.h>
#include <stdlib.h>
diff --git a/src/c_type.c b/src/c_type.c
index 59d890b..30fad03 100644
--- a/src/c_type.c
+++ b/src/c_type.c
@@ -1,4 +1,5 @@
#include "c_type.h"
+#include "u_hash.h"
struct typedata typedata[1<<13];
internstr ttypenames[1<<10];
diff --git a/src/ir.c b/src/ir.c
index 840ba0b..17e5712 100644
--- a/src/ir.c
+++ b/src/ir.c
@@ -1,5 +1,6 @@
#include "ir.h"
#include "obj.h"
+#include "u_hash.h"
uchar type2cls[NTYPETAG];
uchar cls2siz[] = { [KI32] = 4, [KI64] = 8, [KF32] = 4, [KF64] = 8 };
diff --git a/src/ir_cse.c b/src/ir_cse.c
index 23c5cf9..62976a2 100644
--- a/src/ir_cse.c
+++ b/src/ir_cse.c
@@ -1,4 +1,5 @@
#include "ir.h"
+#include "u_hash.h"
static inline bool
pure(const struct instr *ins)
diff --git a/src/ir_mem2reg.c b/src/ir_mem2reg.c
index 7a5874c..e684e62 100644
--- a/src/ir_mem2reg.c
+++ b/src/ir_mem2reg.c
@@ -1,4 +1,5 @@
#include "ir.h"
+#include "u_bits.h"
#include <stdlib.h> /* qsort */
static const uchar loadszcls[] = {
diff --git a/src/ir_regalloc.c b/src/ir_regalloc.c
index 1200c77..fbe80ca 100644
--- a/src/ir_regalloc.c
+++ b/src/ir_regalloc.c
@@ -1,4 +1,5 @@
#include "ir.h"
+#include "u_bits.h"
/** Implements linear scan register allocation **/
/* Some references:
diff --git a/src/u_bits.h b/src/u_bits.h
new file mode 100644
index 0000000..a4cef85
--- /dev/null
+++ b/src/u_bits.h
@@ -0,0 +1,76 @@
+#pragma once
+
+#include "antcc.h"
+
+static inline bool
+bstest(const struct bitset *bs, uint i)
+{
+ return bs[i/BSNBIT].u >> i%BSNBIT & 1;
+}
+
+static inline void
+bsset(struct bitset *bs, uint i)
+{
+ bs[i/BSNBIT].u |= 1ull << i%BSNBIT;
+}
+
+static inline void
+bsclr(struct bitset *bs, uint i)
+{
+ bs[i/BSNBIT].u &= ~(1ull << i%BSNBIT);
+}
+
+static inline void
+bszero(struct bitset bs[/*siz*/], uint siz)
+{
+ memset(bs, 0, siz * sizeof *bs);
+}
+
+static inline void
+bscopy(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz)
+{
+ while (siz--) dst++->u = src++->u;
+}
+
+static inline void
+bsunion(struct bitset dst[/*siz*/], const struct bitset src[/*siz*/], uint siz)
+{
+ while (siz--) dst++->u |= src++->u;
+}
+
+static inline uint
+bscount(struct bitset bs[/*siz*/], uint siz)
+{
+ uint n = 0;
+ while (siz--) n += popcnt(bs++->u);
+ return n;
+}
+
+static inline bool
+bsiter(uint *i, struct bitset bs[/*siz*/], uint siz)
+{
+ uint k = *i/BSNBIT, j = *i%BSNBIT;
+ if (k >= siz) return 0;
+ size_t t = bs[k].u & ~(((size_t)1 << j) - 1);
+ while (!t) {
+ if (++k >= siz) return 0;
+ t = bs[k].u;
+ }
+ *i = k*BSNBIT + lowestsetbit(t);
+ return 1;
+}
+#define bs_each(T, var, bs, siz) for (T (var) = 0; bsiter(&(var), (bs), (siz)); ++(var))
+
+static inline bool
+bsiterzr(uint *i, struct bitset bs[/*siz*/], uint siz)
+{
+ uint k = *i/BSNBIT, j = *i%BSNBIT;
+ if (k >= siz) return 0;
+ size_t t = ~bs[k].u & ~(((size_t)1 << j) - 1);
+ while (!t) {
+ if (++k >= siz) return 0;
+ t = ~bs[k].u;
+ }
+ *i = k*BSNBIT + lowestsetbit(t);
+ return 1;
+}
diff --git a/src/u_hash.h b/src/u_hash.h
new file mode 100644
index 0000000..2b54ae6
--- /dev/null
+++ b/src/u_hash.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <stddef.h>
+
+static inline size_t
+hashs(size_t h, const char *s)
+{
+ while (*s) h = (unsigned char)*s++ + h*65599;
+ return h;
+}
+
+static inline size_t
+hashb(size_t h, const void *d, size_t n)
+{
+ const unsigned char *b = d;
+ while (n--) h = *b++ + h*65599;
+ return h;
+}
+
+static inline size_t
+ptrhash(const void *p) {
+ return (size_t)p * 2654435761u;
+}
diff --git a/src/u_io.c b/src/u_io.c
index ac5bfdb..bd70baa 100644
--- a/src/u_io.c
+++ b/src/u_io.c
@@ -1,4 +1,5 @@
#include "c_lex.h"
+#include "u_hash.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
diff --git a/src/u_mem.c b/src/u_mem.c
index 9495a5f..b1abd2d 100644
--- a/src/u_mem.c
+++ b/src/u_mem.c
@@ -1,38 +1,40 @@
#include "antcc.h"
+#include "u_hash.h"
+#include "u_bits.h"
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
static void
-allocerr(const char *f)
+allocerr(void)
{
- efmt("%s: %s\n", f, strerror(errno));
+ efmt("antcc fatal: %s\n", strerror(errno));
ioflush(&bstdout);
ioflush(&bstderr);
abort();
}
void *
-(xmalloc)(size_t n, const char *f)
+xmalloc(size_t n)
{
void *p = malloc(n);
- if (!p) allocerr(f);
+ if (!p) allocerr();
return p;
}
void *
-(xcalloc)(size_t n, const char *f)
+xcalloc(size_t n)
{
void *p = calloc(n, 1);
- if (!p) allocerr(f);
+ if (!p) allocerr();
return p;
}
void *
-(xrealloc)(void *p, size_t n, const char *f)
+xrealloc(void *p, size_t n)
{
p = p ? realloc(p, n) : malloc(n);
- if (!p) allocerr(f);
+ if (!p) allocerr();
return p;
}