#include #include #include #include "do_jit.h" #define NTARGETS 4 struct ejit_label targets[NTARGETS]; int main(int argc, char *argv[]) { (void)argv; bool do_jit = argc > 1; if (do_jit) { printf("... skipping for now ...\n"); return 0; } struct ejit_operand operands[1] = { EJIT_OPERAND_GPR(0, EJIT_TYPE(long)) }; struct ejit_func *f = ejit_create_func(EJIT_TYPE(long), 1, operands); struct ejit_reloc default_target = ejit_bgei_u(f, EJIT_GPR(0), NTARGETS); ejit_lshi(f, EJIT_GPR(0), EJIT_GPR(0), sizeof(struct ejit_label) == 4 ? 2 : 3); #warning \ "Label rewriting is not currently working so this will fail the jit test" ejit_ldxi_label(f, EJIT_GPR(1), EJIT_GPR(0), (uintptr_t)targets); ejit_jmpr(f, EJIT_GPR(1)); struct ejit_reloc tails[NTARGETS]; for (size_t i = 0; i < NTARGETS; i++) { targets[i] = ejit_label(f); ejit_movi(f, EJIT_GPR(0), i * i); tails[i] = ejit_jmp(f); } struct ejit_label default_label = ejit_label(f); ejit_patch(f, default_target, default_label); ejit_movi(f, EJIT_GPR(0), 42); struct ejit_label dst = ejit_label(f); for (int i = 0; i < NTARGETS; i++) { ejit_patch(f, tails[i], dst); } ejit_retr(f, EJIT_GPR(0)); ejit_select_compile_func(f, 2, 0, EJIT_USE64(long), do_jit, true); for (int i = -2; i < ((int) NTARGETS) + 2; i++) { if (i < 0) { assert(erfi1(f, EJIT_ARG(i, long)) == 42); } else if (i < NTARGETS) { assert(erfi1(f, EJIT_ARG(i, long)) == i * i); } else { assert(erfi1(f, EJIT_ARG(i, long)) == 42); } } ejit_destroy_func(f); }