#include #include #include "../deps/ejit/include/ejit/ejit.h" static struct ejit_func *compile(const char *program) { struct ejit_func *func = ejit_create_func(EJIT_INT32, 0, NULL); if (!func) { fprintf(stderr, "failed allocating func\n"); return NULL; } /* initialize accumulator */ ejit_movi(func, EJIT_GPR(0), 0); /* load constant 2 into register for multiplication/division since ejit * doesn't have muli/divi instructions */ ejit_movi(func, EJIT_GPR(1), 2); char c = 0; while ((c = *program++)) switch (c) { case '+': ejit_addi(func, EJIT_GPR(0), EJIT_GPR(0), 1); break; case '-': ejit_subi(func, EJIT_GPR(0), EJIT_GPR(0), 1); break; case '*': ejit_mulr(func, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1)); break; case '/': ejit_divr(func, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1)); break; default: break; } /* return accumulator */ ejit_retr(func, EJIT_GPR(0)); /* we use GPRs 0 and 1, so 2 in total, no FPRs, and we don't use 64 bit * values (explicitly at least) */ ejit_compile_func(func, 2, 0, false); return func; } static int32_t run(struct ejit_func *func) { return ejit_run_func(func, 0, NULL); } static void destroy(struct ejit_func *func) { ejit_destroy_func(func); } int main() { const char *program = "+ + * - /"; struct ejit_func *func = compile(program); if (!func) { fprintf(stderr, "failed to compile program\n"); return -1; } int32_t result = run(func); printf("%" PRIi32 "\n", result); destroy(func); }