diff options
author | comex | 2015-01-11 15:33:25 -0500 |
---|---|---|
committer | comex | 2015-01-11 15:33:25 -0500 |
commit | 4743027a22ae4e622d7e78f878eff307d02ac373 (patch) | |
tree | de3c834fa1812bf194948b466074c7626b878227 /lib/dis.h | |
parent | yay? (diff) | |
download | substitute-4743027a22ae4e622d7e78f878eff307d02ac373.tar.gz |
C++ is just a giant pain in the ass
Diffstat (limited to '')
-rw-r--r-- | lib/dis.h | 63 |
1 files changed, 43 insertions, 20 deletions
@@ -1,50 +1,62 @@ #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 { - int inpos, outpos, len; + int8_t inpos, outpos, len; }; struct bitslice { - int nruns; - const struct bitslice_run *runs; + int8_t nruns; + struct bitslice_run runs[6]; }; struct dis_data_operand { - struct bitslice n; bool out; + const struct bitslice *n; }; -static inline int sext(unsigned val, int bits) { +static inline CONSTEXPR int sext(unsigned val, int bits) { return val & (1 << (bits - 1)) ? ((int)val - (1 << bits)) : (int)val; } -static inline unsigned bs_get(struct bitslice bs, unsigned op) { +static inline CONSTEXPR unsigned bs_get(const 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 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]; +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]; 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 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]; +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]; inr.outpos -= lo; if(inr.outpos < 0) { inr.len += inr.outpos; @@ -54,10 +66,21 @@ static inline struct bitslice bs_slice_(struct bitslice bs, struct bitslice_run if(inr.outpos + inr.len > size) inr.len = size - inr.outpos; if(inr.len > 0) - runs[nruns++] = (struct bitslice_run) {inr.inpos, inr.outpos, inr.len}; + obs.runs[obs.nruns++] = inr; } - return (struct bitslice) {nruns, runs}; + return obs; } -#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 |