void emitDecrement(jit_function_t function, jit_value_t ptr, ops *unit) { jit_value_t tmp = jit_insn_load_relative(function, ptr, 0, jit_type_ubyte); jit_value_t numbyte = jit_value_create_nint_constant(function, jit_type_ubyte, unit->count); tmp = jit_insn_sub(function, tmp, numbyte); tmp = jit_insn_convert(function, tmp, jit_type_ubyte, 0); jit_insn_store_relative(function, ptr, 0, tmp); }
void JitInstanceMemberFunction::compileVTableIndirectionFunction() { m_jit_virtual_indirection_function = jit_function_create((jit_context_t)m_jit_context.context, toJitSignature(m_eAbi, getSignature())); o_assert(m_jit_virtual_indirection_function.function); jit_context_build_start((jit_context_t)m_jit_context.context); size_t argCount = getSignature()->getParameterCount()+1; // parameters+this jit_value_t* args = o_allocate_n(argCount, jit_value_t); // The indirection has the same signature as the indirected one jit_value_t this_pointer = jit_value_get_param((jit_function_t)m_jit_virtual_indirection_function.function, 0); jit_value_t vtable_index = jit_value_create_nint_constant((jit_function_t)m_jit_virtual_indirection_function.function, jit_type_nuint, getInstanceMemberFunction()->getVirtualTableIndex()); jit_value_t vtable_array = jit_insn_load_relative ((jit_function_t)m_jit_virtual_indirection_function.function, this_pointer, 0, jit_type_void_ptr); jit_value_t vtable_pointer = jit_insn_load_elem((jit_function_t)m_jit_virtual_indirection_function.function, vtable_array, vtable_index, jit_type_void_ptr); size_t i = 0; for(;i<argCount;++i) { args[i] = jit_value_get_param((jit_function_t)m_jit_virtual_indirection_function.function, i); } jit_insn_call_indirect_vtable ((jit_function_t)m_jit_virtual_indirection_function.function, vtable_pointer, jit_function_get_signature((jit_function_t)m_jit_function.function), args, argCount, 0); int result = jit_function_compile((jit_function_t)m_jit_virtual_indirection_function.function); o_assert(result != 0); jit_context_build_end((jit_context_t)m_jit_context.context); o_deallocate_n(args, argCount, jit_value_t); }
void *LibJITFormula::emit (VariableExprAST *expr) { // Create a pointer temporary with the address stored in it jit_value_t pointer = jit_value_create_nint_constant (function, jit_type_void_ptr, (jit_nint)expr->pointer); // Create a load instruction for this address return jit_insn_load_relative (function, pointer, 0, jit_type_float64); }
/* * Get the length of a string. */ static ILJitValue _ILJitStringGetLength(ILJitFunction jitFunction, ILJitValue string) { ILJitValue len; len = jit_insn_load_relative(jitFunction, string, offsetof(System_String, length), _IL_JIT_TYPE_INT32); return len; }
/* Given a struct name, the name of a struct member, and a jit pointer, * to the struct, return a jit value of the member within the struct. */ static VALUE function_ruby_struct_member( VALUE self, VALUE struct_name, VALUE member_name, VALUE ptr_v) { jit_function_t function; jit_value_t ptr = get_value(ptr_v, "ptr"); jit_value_t result; struct Member_Info * member_info = get_member_info( struct_name, member_name); Data_Get_Struct(self, struct _jit_function, function); result = jit_insn_load_relative( function, ptr, member_info->offset, member_info->type); return Data_Wrap_Struct(rb_cValue, 0, 0, result); }
Compiler::value System_operations(Compiler& c) { jit_value_t jit_ops_ptr = jit_value_create_long_constant(c.F, jit_type_void_ptr, (long int) &c.vm->operations); return {jit_insn_load_relative(c.F, jit_ops_ptr, 0, jit_type_uint), Type::LONG}; }
jit_function_t bf_compile(jit_context_t cx, FILE *fp) { jit_type_t params[1], putchar_params[1], signature, putchar_sig, getchar_sig; jit_value_t ptr, uptr, ubyte, tmp; bf_loop_t loop = NULL; ubyte_ptr = jit_type_create_pointer(jit_type_ubyte, 1); params[0] = ubyte_ptr; putchar_params[0] = jit_type_ubyte; signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void, params, 1, 1); putchar_sig = jit_type_create_signature(jit_abi_cdecl, jit_type_ubyte, putchar_params, 1, 1); getchar_sig = jit_type_create_signature(jit_abi_cdecl, jit_type_ubyte, NULL, 0, 1); jit_function_t function = jit_function_create(cx, signature); ptr = jit_value_get_param(function, 0); uptr = jit_value_create_nint_constant(function, ubyte_ptr, 1); ubyte = jit_value_create_nint_constant(function, jit_type_ubyte, 1); ops *unit = (ops*)malloc(sizeof(ops)); unit->count = 0; int first = 1; while(!feof(fp)) { char c = fgetc(fp); if (first) { unit->token = c; first = 0; } switch(c) { case '>': OPTIMIZE_TOKEN('>') case '<': OPTIMIZE_TOKEN('<') case '+': OPTIMIZE_TOKEN('+') case '-': OPTIMIZE_TOKEN('-') case '.': emitOpcodes(function, ptr, unit); unit->token = '.'; tmp = jit_insn_load_relative(function, ptr, 0, jit_type_ubyte); jit_insn_call_native(function, "putchar", putchar, putchar_sig, &tmp, 1, JIT_CALL_NOTHROW); break; case ',': emitOpcodes(function, ptr, unit); unit->token = ','; jit_insn_call_native(function, "getchar", getchar, getchar_sig, NULL, 0, JIT_CALL_NOTHROW); jit_insn_store_relative(function, ptr, 0, tmp); break; case '[': emitOpcodes(function, ptr, unit); unit->token = '['; loop_start(function, &loop); tmp = jit_insn_load_relative(function, ptr, 0, jit_type_ubyte); jit_insn_branch_if_not(function, tmp, &loop->stop); break; case ']': emitOpcodes(function, ptr, unit); unit->token = ']'; loop_stop(function, &loop); break; } } jit_insn_return(function, NULL); jit_function_compile(function); return function; }
jit_value jit_function::insn_load_relative (const jit_value& value, jit_nint offset, jit_type_t type) { value_wrap(jit_insn_load_relative(func, value.raw(), offset, type)); }
jit_function_t GPerlJITCompiler::compile(JITParam *param) { GPerlVirtualMachineCode *pc = param->mtd; jit_context_t ctx = jit_context_create(); jit_context_build_start(ctx); int argc = param->argc; jit_type_t _params[argc]; for (int i = 0; i < argc; i++) { _params[i] = getJitType(param->arg_types[i]); } jit_type_t rtype = getJitType(param->return_type); jit_value_t curstack[32] = {0}; jit_value_t argstack[MAX_ARGSTACK_SIZE] = {0}; jit_type_t signature = jit_type_create_signature(jit_abi_fastcall, rtype, _params, argc, 0); jit_function_t func = jit_function_create(ctx, signature); jit_value_t _v[MAX_REG_SIZE] = {0}; GPerlJmpStack *jmp_stack = new GPerlJmpStack(); argc = 0; for (; pc->op != UNDEF; pc++) { if (jmp_stack->isJmp()) { GPerlJmpInfo *inf = jmp_stack->pop(); jit_insn_label(func, &inf->label); } switch (pc->op) { case LET: DBG_PL("COMPILE LET"); curstack[pc->dst] = _v[pc->src]; break; case MOV: DBG_PL("COMPILE MOV"); _v[pc->dst] = compileMOV(pc, &func); break; case ARRAY_ARGAT: { DBG_PL("COMPILE ARGAT"); jit_value_t arg = jit_value_get_param(func, pc->src); jit_nint offset = 48; jit_value_t list = jit_insn_load_relative(func, arg, offset, object_ptr_type); jit_value_t idx = jit_value_create_nint_constant(func, jit_type_int, pc->idx); jit_value_t elem = jit_insn_load_elem(func, list, idx, value_type); _v[pc->dst] = elem; break; } case vMOV: DBG_PL("COMPILE vMOV"); _v[pc->dst] = curstack[pc->src]; break; case gMOV: break; case ARGMOV: DBG_PL("COMPILE ARGMOV"); _v[pc->dst] = jit_value_get_param(func, pc->src); break; case ADD: DBG_PL("COMPILE ADD"); _v[pc->dst] = jit_insn_add(func, _v[pc->dst], _v[pc->src]); break; case SUB: DBG_PL("COMPILE SUB"); _v[pc->dst] = jit_insn_sub(func, _v[pc->dst], _v[pc->src]); break; case iADDC: { DBG_PL("COMPILE iADDC"); jit_value_t c = jit_value_create_nint_constant(func, jit_type_int, pc->v.ivalue); _v[pc->dst] = jit_insn_add(func, _v[pc->dst], c); break; } case iSUBC: { DBG_PL("COMPILE iSUBC"); jit_value_t c = jit_value_create_nint_constant(func, jit_type_int, pc->v.ivalue); _v[pc->dst] = jit_insn_sub(func, _v[pc->dst], c); break; } case IS: { DBG_PL("COMPILE IS"); jit_value_t c = jit_value_create_nint_constant(func, jit_type_int, 1); jit_value_t tmp = jit_insn_eq(func, _v[pc->dst], c); GPerlJmpInfo *inf = new GPerlJmpInfo(pc->jmp); jmp_stack->push(inf); jit_insn_branch_if_not(func, tmp, &inf->label); break; } case iJLC: { DBG_PL("COMPILE iJLC"); jit_value_t c = jit_value_create_nint_constant(func, jit_type_int, pc->v.ivalue); jit_value_t tmp = jit_insn_lt(func, _v[pc->dst], c); GPerlJmpInfo *inf = new GPerlJmpInfo(pc->jmp); jmp_stack->push(inf); jit_insn_branch_if_not(func, tmp, &inf->label); break; } case iJLEC: { DBG_PL("COMPILE iJLEC"); jit_value_t c = jit_value_create_nint_constant(func, jit_type_int, pc->v.ivalue); jit_value_t tmp = jit_insn_le(func, _v[pc->dst], c); GPerlJmpInfo *inf = new GPerlJmpInfo(pc->jmp); jmp_stack->push(inf); jit_insn_branch_if_not(func, tmp, &inf->label); break; } case JE: { DBG_PL("COMPILE JE"); jit_value_t tmp = jit_insn_eq(func, _v[pc->dst], _v[pc->src]); GPerlJmpInfo *inf = new GPerlJmpInfo(pc->jmp); jmp_stack->push(inf); jit_insn_branch_if_not(func, tmp, &inf->label); break; } case JLE: { DBG_PL("COMPILE JLE"); jit_value_t tmp = jit_insn_le(func, _v[pc->dst], _v[pc->src]); GPerlJmpInfo *inf = new GPerlJmpInfo(pc->jmp); jmp_stack->push(inf); jit_insn_branch_if_not(func, tmp, &inf->label); break; } case PUSH: DBG_PL("COMPILE PUSH"); argstack[pc->src] = _v[pc->dst]; argc++; break; case SELFCALL: { DBG_PL("COMPILE SELFCALL"); _v[pc->dst] = jit_insn_call(func, "", func, NULL, argstack, argc, 0);//JIT_CALL_TAIL); argc = 0; break; } case SELF_FASTCALL0: { DBG_PL("COMPILE SELF_FASTCALL0"); argstack[0] = _v[pc->arg0]; _v[pc->dst] = jit_insn_call(func, "", func, NULL, argstack, 1, 0);//JIT_CALL_TAIL); break; } case SELF_FASTCALL1: { DBG_PL("COMPILE SELF_FASTCALL1"); argstack[0] = _v[pc->arg0]; argstack[1] = _v[pc->arg1]; _v[pc->dst] = jit_insn_call(func, "", func, NULL, argstack, 2, 0);//JIT_CALL_TAIL); break; } case SELF_FASTCALL2: { DBG_PL("COMPILE SELF_FASTCALL2"); argstack[0] = _v[pc->arg0]; argstack[1] = _v[pc->arg1]; argstack[2] = _v[pc->arg2]; _v[pc->dst] = jit_insn_call(func, "", func, NULL, argstack, 3, 0);//JIT_CALL_TAIL); break; } case SELF_FASTCALL3: { DBG_PL("COMPILE SELF_FASTCALL3"); argstack[0] = _v[pc->arg0]; argstack[1] = _v[pc->arg1]; argstack[2] = _v[pc->arg2]; argstack[3] = _v[pc->arg3]; _v[pc->dst] = jit_insn_call(func, "", func, NULL, argstack, 4, 0);//JIT_CALL_TAIL); break; } case REF: { //int ret = 0; //if (TYPE_CHECK(v) > 1) { //GPerlObject *o = (GPerlObject *)getObject(v); //if (o->h.type == ArrayRef) { //ret = 1; //} //} //INT_init(reg[0], ret); break; } case RET: case JIT_COUNTDOWN_RET: DBG_PL("COMPILE RET"); _v[0] = _v[pc->src]; jit_insn_return(func, _v[0]); break; default: DBG_PL("COMPILE DEFALUT"); break; } } jit_function_compile(func); jit_context_build_end(ctx); return func; }