void _jit_epilog(jit_state_t *_jit) { assert(_jitc->function); assert(_jitc->function->epilog->next == NULL); jit_link(_jitc->function->epilog); _jitc->function = NULL; }
void _jit_prolog(jit_state_t *_jit) { jit_int32_t offset; if (_jitc->function) jit_epilog(); assert(jit_regset_cmp_ui(&_jitc->regarg, 0) == 0); jit_regset_set_ui(&_jitc->regsav, 0); offset = _jitc->functions.offset; if (offset >= _jitc->functions.length) { jit_realloc((jit_pointer_t *)&_jitc->functions.ptr, _jitc->functions.length * sizeof(jit_function_t), (_jitc->functions.length + 16) * sizeof(jit_function_t)); _jitc->functions.length += 16; } _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++; _jitc->function->self.size = stack_framesize; _jitc->function->self.argi = _jitc->function->self.argf = _jitc->function->self.aoff = _jitc->function->self.alen = 0; /* float conversion */ _jitc->function->self.aoff = -8; _jitc->function->self.call = jit_call_default; jit_alloc((jit_pointer_t *)&_jitc->function->regoff, _jitc->reglen * sizeof(jit_int32_t)); _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog); jit_link(_jitc->function->prolog); _jitc->function->prolog->w.w = offset; _jitc->function->epilog = jit_new_node_no_link(jit_code_epilog); /* u: label value * v: offset in blocks vector * w: offset in functions vector */ _jitc->function->epilog->w.w = offset; jit_regset_new(&_jitc->function->regset); }
jit_pointer_t compile(const std::vector<Oper>& ops, jit_word_t *memory, const bool flush = true) { jit_prolog(); jit_movi(JIT_V0, reinterpret_cast<jit_word_t>(memory)); jit_movi(JIT_V1, 0); std::stack<Loop> loops; jit_node_t* start = jit_note(__FILE__, __LINE__); for ( size_t n=0; n<ops.size(); ++n ) { switch ( ops[n].code ) { case '<': jit_str(JIT_V0, JIT_V1); jit_subi(JIT_V0, JIT_V0, ops[n].count * sizeof(jit_word_t)); jit_ldr(JIT_V1, JIT_V0); break; case '>': jit_str(JIT_V0, JIT_V1); jit_addi(JIT_V0, JIT_V0, ops[n].count * sizeof(jit_word_t)); jit_ldr(JIT_V1, JIT_V0); break; case 'z': jit_movi(JIT_V1, 0); break; case '+': jit_addi(JIT_V1, JIT_V1, ops[n].count); break; case '-': jit_subi(JIT_V1, JIT_V1, ops[n].count); break; case '.': jit_prepare(); jit_pushargr(JIT_V1); jit_finishi(reinterpret_cast<jit_pointer_t>(putchar)); if ( flush ) { jit_prepare(); jit_pushargi(reinterpret_cast<jit_word_t>(stdout)); jit_finishi(reinterpret_cast<jit_pointer_t>(fflush)); } break; case ',': jit_prepare(); jit_finishi(reinterpret_cast<jit_pointer_t>(getchar)); jit_retval(JIT_V1); break; case '[': { Loop loop; loop.end = jit_forward(); jit_node_t *j = jit_beqi(JIT_V1, 0); jit_patch_at(j, loop.end); loop.body = jit_label(); loops.push(loop); } break; case ']': { Loop loop = loops.top(); jit_node_t *j = jit_bnei(JIT_V1, 0); jit_patch_at(j, loop.body); jit_link(loop.end); loops.pop(); break; } default: break; } } jit_node_t* stop = jit_note(__FILE__, __LINE__); jit_ret(); jit_epilog(); jit_pointer_t r = jit_emit(); fprintf(stderr, "compiled to %zu bytes\n", (char*)jit_address(stop) - (char*)jit_address(start)); return r; }