aboutsummaryrefslogtreecommitdiff
path: root/lib/dis.h
diff options
context:
space:
mode:
authorcomex2015-01-11 19:39:23 -0500
committercomex2015-01-11 19:39:23 -0500
commit9850809890076f13bca3d5e6e0fcc050ad647c8d (patch)
treebe432adb27ab9d5d5a6e323fbacc05d03fcbace6 /lib/dis.h
parentC++ is just a giant pain in the ass (diff)
downloadsubstitute-9850809890076f13bca3d5e6e0fcc050ad647c8d.tar.gz
Revert "C++ is just a giant pain in the ass"
This reverts commit 4743027a22ae4e622d7e78f878eff307d02ac373.
Diffstat (limited to 'lib/dis.h')
-rw-r--r--lib/dis.h63
1 files changed, 20 insertions, 43 deletions
diff --git a/lib/dis.h b/lib/dis.h
index 9a87064..a45e5d0 100644
--- a/lib/dis.h
+++ b/lib/dis.h
@@ -1,62 +1,50 @@
#pragma once
#include <stdbool.h>
-#include <stdint.h>
#define UNUSED __attribute__((unused))
-#define INLINE __attribute__((always_inline)) inline
-#ifdef __cplusplus
-#define CONSTEXPR constexpr
-#else
-#define CONSTEXPR
-#endif
struct bitslice_run {
- int8_t inpos, outpos, len;
+ int inpos, outpos, len;
};
struct bitslice {
- int8_t nruns;
- struct bitslice_run runs[6];
+ int nruns;
+ const struct bitslice_run *runs;
};
struct dis_data_operand {
+ struct bitslice n;
bool out;
- const struct bitslice *n;
};
-static inline CONSTEXPR int sext(unsigned val, int bits) {
+static inline int sext(unsigned val, int bits) {
return val & (1 << (bits - 1)) ? ((int)val - (1 << bits)) : (int)val;
}
-static inline CONSTEXPR unsigned bs_get(const struct bitslice *bs, unsigned op) {
+static inline unsigned bs_get(struct bitslice bs, unsigned op) {
unsigned ret = 0;
- for(int i = 0; i < bs->nruns; i++) {
- const struct bitslice_run *run = &bs->runs[i];
+ for(int i = 0; i < bs.nruns; i++) {
+ const struct bitslice_run *run = &bs.runs[i];
unsigned val = (op >> run->inpos) & ((1 << run->len) - 1);
ret |= val << run->outpos;
}
return ret;
}
-static inline CONSTEXPR unsigned bs_set(const struct bitslice *bs, unsigned new_, unsigned op) {
- for(int i = 0; i < bs->nruns; i++) {
- const struct bitslice_run *run = &bs->runs[i];
+static inline unsigned bs_set(struct bitslice bs, unsigned new, unsigned op) {
+ for(int i = 0; i < bs.nruns; i++) {
+ const struct bitslice_run *run = &bs.runs[i];
unsigned mask = (1 << run->len) - 1;
- unsigned val = (new_ >> run->outpos) & mask;
+ unsigned val = (new >> run->outpos) & mask;
op = (op & ~(mask << run->inpos)) | (val << run->inpos);
}
return op;
}
-static inline CONSTEXPR struct bitslice bs_slice(const struct bitslice *bs, int lo, int size) {
- struct bitslice obs
- #ifdef __cplusplus
- {}
- #endif
- ;
- obs.nruns = 0;
- for(int i = 0; i < bs->nruns; i++) {
- struct bitslice_run inr = bs->runs[i];
+static inline struct bitslice bs_slice_(struct bitslice bs, struct bitslice_run *runs, int lo, int size) {
+ int nruns = 0;
+ for(int i = 0; i < bs.nruns; i++) {
+ struct bitslice_run inr = bs.runs[i];
inr.outpos -= lo;
if(inr.outpos < 0) {
inr.len += inr.outpos;
@@ -66,21 +54,10 @@ static inline CONSTEXPR struct bitslice bs_slice(const struct bitslice *bs, int
if(inr.outpos + inr.len > size)
inr.len = size - inr.outpos;
if(inr.len > 0)
- obs.runs[obs.nruns++] = inr;
+ runs[nruns++] = (struct bitslice_run) {inr.inpos, inr.outpos, inr.len};
}
- return obs;
+ return (struct bitslice) {nruns, runs};
}
+#define bs_slice(bs, lo, size) \
+ bs_slice_(bs, alloca((bs).nruns * sizeof(struct bitslice_run)), lo, size)
-
-#ifdef __cplusplus
-#define staticify(ty, ...) [&](){ constexpr static ty bs = __VA_ARGS__; return &bs; }()
-
-#define r(nn) staticify(struct dis_data_operand, {.n = nn, .out = false})
-#define rs(nn, l, s) staticify(struct dis_data_operand, {.n = staticify(struct bitslice, bs_slice(nn, l, s)), .out = false})
-#define rout(nn) staticify(struct dis_data_operand, {.n = nn, .out = true})
-#define rsout(nn, l, s) staticify(struct dis_data_operand, {.n = staticify(struct bitslice, bs_slice(nn, l, s)), .out = true})
-
-#define data(...) return P(data)<__VA_ARGS__>(ctx);
-typedef const struct bitslice *BSP;
-
-#endif