aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcomex2015-01-11 15:33:25 -0500
committercomex2015-01-11 15:33:25 -0500
commit4743027a22ae4e622d7e78f878eff307d02ac373 (patch)
treede3c834fa1812bf194948b466074c7626b878227
parentyay? (diff)
downloadsubstitute-4743027a22ae4e622d7e78f878eff307d02ac373.tar.gz
C++ is just a giant pain in the ass
-rw-r--r--Makefile32
-rw-r--r--lib/dis-arm.inc.h100
-rw-r--r--lib/dis.h63
-rw-r--r--test/test-dis.c8
-rw-r--r--test/test-tdarm-simple.cpp (renamed from test/test-tdarm-simple.c)8
5 files changed, 113 insertions, 98 deletions
diff --git a/Makefile b/Makefile
index e4ea709..18fdc70 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,9 @@
# todo
CC := clang
CXX := clang++
-CFLAGS := -O3 -Wall -Wextra -Werror -arch x86_64
+CFLAGS := -O3 -Wall -Wextra -Werror -arch x86_64
override CC := $(CC) $(CFLAGS)
-override CXX := $(CXX) $(CFLAGS)
+override CXX := $(CXX) $(CFLAGS) -fno-exceptions -fno-asynchronous-unwind-tables
IMAON2 := /Users/comex/c/imaon2
GEN_JS := node $(IMAON2)/tables/gen.js
@@ -16,27 +16,25 @@ all: \
out/test-dis \
out/test-tdarm-simple
-out:
- mkdir out
+$(shell mkdir -p out)
-out/%.o: lib/%.c Makefile out
+HEADERS := lib/*.h generated/transform-dis-arm.inc.h
+out/%.o: lib/%.c Makefile $(HEADERS)
$(CC) -fvisibility=hidden -std=c11 -c -o $@ $<
LIB_OBJS := out/find-syms.o out/substrate-compat.o
-HEADERS := lib/*.h generated/*.h
-out/libsubstitute.dylib: $(LIB_OBJS) lib/*.h out
+out/libsubstitute.dylib: $(LIB_OBJS)
$(CC) -dynamiclib -fvisibility=hidden -o $@ $(LIB_OBJS)
-out/test-tdarm-simple: test/test-tdarm-simple.c $(HEADERS) Makefile
- $(CC) -std=c11 -o $@ $< -Ilib
-out/test-dis: test/test-dis.c $(HEADERS) Makefile
- $(CC) -std=c11 -o $@ $< -Ilib
-out/test-%: test/test-%.c Makefile $(HEADERS) out/libsubstitute.dylib
- $(CC) -std=c89 -o $@ $< -Ilib -Lout -lsubstitute
-out/test-%-cpp: test/test-%.c Makefile $(HEADERS) out/libsubstitute.dylib
- $(CXX) -x c++ -std=c++98 -o $@ $< -Ilib -Lout -lsubstitute
-out/test-%: test/test-%.cpp Makefile $(HEADERS) out/libsubstitute.dylib
- $(CXX) -std=c++11 -o $@ $< -Ilib -Isubstrate -Lout -lsubstitute
+define define_test
+out/test-$(1): test/test-$(2).c* $(HEADERS) Makefile out/libsubstitute.dylib
+ $(3) -o $$@ $$< -Ilib -Isubstrate -Lout -lsubstitute
+endef
+$(eval $(call define_test,tdarm-simple,tdarm-simple,$(CXX) -std=c++1y))
+$(eval $(call define_test,dis,dis,$(CC) -std=c11))
+$(eval $(call define_test,find-syms,find-syms,$(CC) -std=c89))
+$(eval $(call define_test,find-syms-cpp,find-syms,$(CXX) -x c++ -std=c++98))
+$(eval $(call define_test,substrate,substrate,$(CXX) -std=c++98))
generated: Makefile
rm -rf generated
diff --git a/lib/dis-arm.inc.h b/lib/dis-arm.inc.h
index fe5c45b..1986132 100644
--- a/lib/dis-arm.inc.h
+++ b/lib/dis-arm.inc.h
@@ -1,147 +1,139 @@
#include "dis.h"
-#define r(nn) {.n = nn, .out = false}
-#define rs(nn, l, s) {.n = bs_slice(nn, l, s), .out = false}
-#define rout(nn) {.n = nn, .out = true}
-#define rsout(nn, l, s) {.n = bs_slice(nn, l, s), .out = true}
-#define data(...) \
- struct dis_data_operand ops[] = {__VA_ARGS__}; \
- return P(data)(ctx, ops, sizeof(ops)/sizeof(*ops));
-
-static tdis_ret P(GPRPairOp_Rt_addr_offset_none_addr_S_2_STLEXD)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) {
+template<BSP Rt, BSP addr> static INLINE tdis_ret P(GPRPairOp_Rt_addr_offset_none_addr_S_2_STLEXD)(tdis_ctx ctx) {
data(r(Rt), r(addr));
}
-static tdis_ret P(GPR_Rm_unk_Rd_1_MOVr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) {
+template<BSP Rm, BSP Rd> static INLINE tdis_ret P(GPR_Rm_unk_Rd_1_MOVr)(tdis_ctx ctx) {
data(rout(Rd), r(Rm));
}
-static tdis_ret P(GPR_Rn_GPR_Rm_unk_Rd_1_ADDrr)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd, struct bitslice Rn) {
+template<BSP Rm, BSP Rd, BSP Rn> static INLINE tdis_ret P(GPR_Rn_GPR_Rm_unk_Rd_1_ADDrr)(tdis_ctx ctx) {
data(rout(Rd), r(Rm), r(Rn));
}
-static tdis_ret P(GPR_Rn_so_reg_imm_shift_unk_Rd_1_ADDrsi)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rd, struct bitslice Rn) {
+template<BSP shift, BSP Rd, BSP Rn> static INLINE tdis_ret P(GPR_Rn_so_reg_imm_shift_unk_Rd_1_ADDrsi)(tdis_ctx ctx) {
data(rs(shift, 0, 4), r(Rn), rout(Rd));
}
-static tdis_ret P(GPR_Rn_so_reg_reg_shift_unk_Rd_1_ADDrsr)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rd, struct bitslice Rn) {
+template<BSP shift, BSP Rd, BSP Rn> static INLINE tdis_ret P(GPR_Rn_so_reg_reg_shift_unk_Rd_1_ADDrsr)(tdis_ctx ctx) {
data(rs(shift, 0, 4), rs(shift, 8, 4), r(Rn), rout(Rd));
}
-static tdis_ret P(GPR_Rn_unk_Rd_1_ADDri)(tdis_ctx ctx, struct bitslice Rd, struct bitslice Rn) {
+template<BSP Rd, BSP Rn> static INLINE tdis_ret P(GPR_Rn_unk_Rd_1_ADDri)(tdis_ctx ctx) {
data(rout(Rd), r(Rn));
}
-static tdis_ret P(GPR_Rt_4_MCR)(tdis_ctx ctx, struct bitslice Rt) {
+template<BSP Rt> static INLINE tdis_ret P(GPR_Rt_4_MCR)(tdis_ctx ctx) {
data(r(Rt));
}
-static tdis_ret P(GPR_Rt_addr_offset_none_addr_S_10_STL)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) {
+template<BSP Rt, BSP addr> static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_S_10_STL)(tdis_ctx ctx) {
data(r(addr), rout(Rt));
}
-static tdis_ret P(GPR_Rt_addr_offset_none_addr_am2offset_imm_offset_S_4_STRBT_POST_IMM)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<UNUSED BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_am2offset_imm_offset_S_4_STRBT_POST_IMM)(tdis_ctx ctx) {
data(r(addr), r(Rt));
}
-static tdis_ret P(GPR_Rt_addr_offset_none_addr_am2offset_reg_offset_S_4_STRBT_POST_REG)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_am2offset_reg_offset_S_4_STRBT_POST_REG)(tdis_ctx ctx) {
data(r(addr), rs(offset, 0, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_addr_offset_none_addr_am3offset_offset_S_2_STRD_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_am3offset_offset_S_2_STRD_POST)(tdis_ctx ctx) {
data(r(addr), rs(offset, 0, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_addr_offset_none_addr_postidx_imm8_offset_S_1_STRHTi)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<UNUSED BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(GPR_Rt_addr_offset_none_addr_postidx_imm8_offset_S_1_STRHTi)(tdis_ctx ctx) {
data(r(addr), r(Rt));
}
-static tdis_ret P(GPR_Rt_addrmode3_addr_S_2_STRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(GPR_Rt_addrmode3_addr_S_2_STRD)(tdis_ctx ctx) {
data(rs(addr, 9, 4), rs(addr, 0, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_addrmode3_pre_addr_S_2_STRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(GPR_Rt_addrmode3_pre_addr_S_2_STRD_PRE)(tdis_ctx ctx) {
data(rs(addr, 9, 4), rs(addr, 0, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_addrmode_imm12_addr_S_1_STRi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(GPR_Rt_addrmode_imm12_addr_S_1_STRi12)(tdis_ctx ctx) {
data(rs(addr, 13, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_addrmode_imm12_pre_addr_S_2_STRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(GPR_Rt_addrmode_imm12_pre_addr_S_2_STRB_PRE_IMM)(tdis_ctx ctx) {
data(rs(addr, 13, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_ldst_so_reg_addr_S_2_STRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(GPR_Rt_ldst_so_reg_addr_S_2_STRB_PRE_REG)(tdis_ctx ctx) {
data(rs(addr, 0, 4), rs(addr, 13, 4), r(Rt));
}
-static tdis_ret P(GPR_Rt_ldst_so_reg_shift_S_1_STRrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) {
+template<BSP shift, BSP Rt> static INLINE tdis_ret P(GPR_Rt_ldst_so_reg_shift_S_1_STRrs)(tdis_ctx ctx) {
data(rs(shift, 0, 4), rs(shift, 13, 4), r(Rt));
}
-static tdis_ret P(GPRnopc_Rt_4_MCRR)(tdis_ctx ctx, UNUSED struct bitslice Rt) {
- // need Rt2 but whatever
+template<UNUSED BSP Rt> static INLINE tdis_ret P(GPRnopc_Rt_4_MCRR)(tdis_ctx ctx) {
+ /* need Rt2 but whatever */
return P(unidentified)(ctx);
}
-static tdis_ret P(GPRnopc_Rt_addrmode_imm12_addr_S_1_STRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(GPRnopc_Rt_addrmode_imm12_addr_S_1_STRBi12)(tdis_ctx ctx) {
data(rs(addr, 13, 4), r(Rt));
}
-static tdis_ret P(GPRnopc_Rt_ldst_so_reg_shift_S_1_STRBrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) {
+template<BSP shift, BSP Rt> static INLINE tdis_ret P(GPRnopc_Rt_ldst_so_reg_shift_S_1_STRBrs)(tdis_ctx ctx) {
data(rs(shift, 13, 4), rs(shift, 0, 4), r(Rt));
}
-static tdis_ret P(addr_offset_none_addr_4_LDC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) {
+template<BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_4_LDC2L_OPTION)(tdis_ctx ctx) {
data(r(addr));
}
-static tdis_ret P(addr_offset_none_addr_S_4_STC2L_OPTION)(tdis_ctx ctx, struct bitslice addr) {
+template<BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_S_4_STC2L_OPTION)(tdis_ctx ctx) {
data(r(addr));
}
-static tdis_ret P(addr_offset_none_addr_am2offset_imm_offset_unk_Rt_4_LDRBT_POST_IMM)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<UNUSED BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_am2offset_imm_offset_unk_Rt_4_LDRBT_POST_IMM)(tdis_ctx ctx) {
data(r(addr), rout(Rt));
}
-static tdis_ret P(addr_offset_none_addr_am2offset_reg_offset_unk_Rt_4_LDRBT_POST_REG)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_am2offset_reg_offset_unk_Rt_4_LDRBT_POST_REG)(tdis_ctx ctx) {
data(r(addr), rs(offset, 0, 4), rout(Rt));
}
-static tdis_ret P(addr_offset_none_addr_am3offset_offset_unk_Rt_4_LDRD_POST)(tdis_ctx ctx, struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_am3offset_offset_unk_Rt_4_LDRD_POST)(tdis_ctx ctx) {
data(r(addr), rs(offset, 0, 4), rout(Rt));
}
-static tdis_ret P(addr_offset_none_addr_postidx_imm8_offset_unk_Rt_3_LDRHTi)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice Rt, struct bitslice addr) {
+template<UNUSED BSP offset, BSP Rt, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_postidx_imm8_offset_unk_Rt_3_LDRHTi)(tdis_ctx ctx) {
data(r(addr), rout(Rt));
}
-static tdis_ret P(addr_offset_none_addr_postidx_imm8s4_offset_4_LDC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) {
+template<UNUSED BSP offset, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_postidx_imm8s4_offset_4_LDC2L_POST)(tdis_ctx ctx) {
data(r(addr));
}
-static tdis_ret P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_STC2L_POST)(tdis_ctx ctx, UNUSED struct bitslice offset, struct bitslice addr) {
+template<UNUSED BSP offset, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_postidx_imm8s4_offset_S_4_STC2L_POST)(tdis_ctx ctx) {
data(r(addr));
}
-static tdis_ret P(addr_offset_none_addr_unk_Rt_16_LDA)(tdis_ctx ctx, struct bitslice Rt, struct bitslice addr) {
+template<BSP Rt, BSP addr> static INLINE tdis_ret P(addr_offset_none_addr_unk_Rt_16_LDA)(tdis_ctx ctx) {
data(r(addr), rout(Rt));
}
-static tdis_ret P(addrmode3_addr_unk_Rt_4_LDRD)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(addrmode3_addr_unk_Rt_4_LDRD)(tdis_ctx ctx) {
data(rsout(addr, 9, 4), rs(addr, 0, 4), r(Rt));
}
-static tdis_ret P(addrmode3_pre_addr_unk_Rt_4_LDRD_PRE)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(addrmode3_pre_addr_unk_Rt_4_LDRD_PRE)(tdis_ctx ctx) {
data(rsout(addr, 9, 4), rs(addr, 0, 4), r(Rt));
}
-static tdis_ret P(addrmode5_addr_8_LDC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) {
+template<BSP addr> static INLINE tdis_ret P(addrmode5_addr_8_LDC2L_OFFSET)(tdis_ctx ctx) {
data(rsout(addr, 9, 4));
}
-static tdis_ret P(addrmode5_addr_S_4_STC2L_OFFSET)(tdis_ctx ctx, struct bitslice addr) {
+template<BSP addr> static INLINE tdis_ret P(addrmode5_addr_S_4_STC2L_OFFSET)(tdis_ctx ctx) {
data(rs(addr, 9, 4));
}
-static tdis_ret P(addrmode5_pre_addr_4_LDC2L_PRE)(tdis_ctx ctx, struct bitslice addr) {
+template<BSP addr> static INLINE tdis_ret P(addrmode5_pre_addr_4_LDC2L_PRE)(tdis_ctx ctx) {
data(rs(addr, 9, 4));
}
-static tdis_ret P(addrmode5_pre_addr_S_4_STC2L_PRE)(tdis_ctx ctx, struct bitslice addr) {
+template<BSP addr> static INLINE tdis_ret P(addrmode5_pre_addr_S_4_STC2L_PRE)(tdis_ctx ctx) {
data(rs(addr, 9, 4));
}
-static tdis_ret P(addrmode_imm12_addr_unk_Rt_2_LDRBi12)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(addrmode_imm12_addr_unk_Rt_2_LDRBi12)(tdis_ctx ctx) {
data(rs(addr, 13, 4), rout(Rt));
}
-static tdis_ret P(addrmode_imm12_pre_addr_unk_Rt_2_LDRB_PRE_IMM)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(addrmode_imm12_pre_addr_unk_Rt_2_LDRB_PRE_IMM)(tdis_ctx ctx) {
data(rs(addr, 13, 4), rout(Rt));
}
-static tdis_ret P(adrlabel_label_1_ADR)(tdis_ctx ctx, struct bitslice label) {
+template<BSP label> static INLINE tdis_ret P(adrlabel_label_1_ADR)(tdis_ctx ctx) {
return P(adr)(ctx, ctx->pc + 8 + bs_get(label, ctx->op));
}
-static tdis_ret P(br_target_target_B_1_Bcc)(tdis_ctx ctx, struct bitslice target) {
+template<BSP target> static INLINE tdis_ret P(br_target_target_B_1_Bcc)(tdis_ctx ctx) {
return P(branch)(ctx, ctx->pc + 8 + sext(bs_get(target, ctx->op), 24));
}
-static tdis_ret P(ldst_so_reg_addr_unk_Rt_2_LDRB_PRE_REG)(tdis_ctx ctx, struct bitslice addr, struct bitslice Rt) {
+template<BSP addr, BSP Rt> static INLINE tdis_ret P(ldst_so_reg_addr_unk_Rt_2_LDRB_PRE_REG)(tdis_ctx ctx) {
data(rs(addr, 0, 4), rs(addr, 13, 4), rout(Rt));
}
-static tdis_ret P(ldst_so_reg_shift_unk_Rt_2_LDRBrs)(tdis_ctx ctx, struct bitslice shift, struct bitslice Rt) {
+template<BSP shift, BSP Rt> static INLINE tdis_ret P(ldst_so_reg_shift_unk_Rt_2_LDRBrs)(tdis_ctx ctx) {
data(rs(shift, 0, 4), rs(shift, 13, 4), rout(Rt));
}
-static tdis_ret P(tcGPR_Rm_unk_Rd_1_MOVr_TC)(tdis_ctx ctx, struct bitslice Rm, struct bitslice Rd) {
+template<BSP Rm, BSP Rd> static INLINE tdis_ret P(tcGPR_Rm_unk_Rd_1_MOVr_TC)(tdis_ctx ctx) {
data(rout(Rd), r(Rm));
}
-static tdis_ret P(unk_Rd_5_MOVTi16)(tdis_ctx ctx, struct bitslice Rd) {
+template<BSP Rd> static INLINE tdis_ret P(unk_Rd_5_MOVTi16)(tdis_ctx ctx) {
data(rout(Rd));
}
-static inline tdis_ret P(unk_Rt_13_MRC)(tdis_ctx ctx, struct bitslice Rt) {
+template<BSP Rt> static INLINE tdis_ret P(unk_Rt_13_MRC)(tdis_ctx ctx) {
data(rout(Rt));
}
diff --git a/lib/dis.h b/lib/dis.h
index a45e5d0..9a87064 100644
--- a/lib/dis.h
+++ b/lib/dis.h
@@ -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
diff --git a/test/test-dis.c b/test/test-dis.c
index 127b0b4..8b4a2b3 100644
--- a/test/test-dis.c
+++ b/test/test-dis.c
@@ -1,13 +1,13 @@
#include <stdio.h>
#include "dis.h"
unsigned f(unsigned x) {
- struct bitslice addr = {.nruns = 4, .runs = (struct bitslice_run[]) {{0,0,4}, {5,5,7}, {16,13,4}, {23,12,1}}};
- return bs_get(addr, x);
+ struct bitslice addr = {.nruns = 4, .runs = {{0,0,4}, {5,5,7}, {16,13,4}, {23,12,1}}};
+ return bs_get(&addr, x);
}
unsigned fs(unsigned val, unsigned op) {
- struct bitslice addr = {.nruns = 4, .runs = (struct bitslice_run[]) {{0,0,4}, {5,5,7}, {16,13,4}, {23,12,1}}};
- return bs_set(addr, val, op);
+ struct bitslice addr = {.nruns = 4, .runs = {{0,0,4}, {5,5,7}, {16,13,4}, {23,12,1}}};
+ return bs_set(&addr, val, op);
}
int main() {
diff --git a/test/test-tdarm-simple.c b/test/test-tdarm-simple.cpp
index 99a5d4a..d590ba8 100644
--- a/test/test-tdarm-simple.c
+++ b/test/test-tdarm-simple.cpp
@@ -11,13 +11,15 @@ typedef struct tc {
} *tdis_ctx;
#define P(x) P_##x
+__attribute__((noinline))
static void P_data(struct tc *ctx, struct dis_data_operand *ops, size_t nops) {
printf("data: %08x\n", ctx->op);
for(size_t i = 0; i < nops; i++) {
- unsigned val = bs_get(ops[i].n, ctx->op);
+ const struct bitslice *n = ops[i].n;
+ unsigned val = bs_get(n, ctx->op);
printf(" {");
- for(int j = 0; j < ops[i].n.nruns; j++) {
- struct bitslice_run run = ops[i].n.runs[j];
+ for(int j = 0; j < n->nruns; j++) {
+ struct bitslice_run run = n->runs[j];
printf(" %d:%d:%d", run.inpos, run.outpos, run.len);
}
printf(" } =>\n");