jit_value_t Match::Pattern::match(Compiler &c, jit_value_t v) const { jit_type_t args_types[2] = {LS_POINTER, LS_POINTER}; jit_type_t sig = jit_type_create_signature(jit_abi_cdecl, jit_type_sys_bool, args_types, 2, 1); if (interval) { jit_value_t ge = nullptr; if (begin) { auto b = begin->compile(c); if (begin->type.nature == Nature::VALUE) { ge = jit_insn_ge(c.F, v, b.v); } else { jit_value_t args[2] = { v, b.v }; ge = jit_insn_call_native(c.F, "", (void*) jit_greater_equal_, sig, args, 2, JIT_CALL_NOTHROW); c.insn_delete_temporary(b); } } jit_value_t lt = nullptr; if (end) { auto e = end->compile(c); if (end->type.nature == Nature::VALUE) { lt = jit_insn_lt(c.F, v, e.v); } else { jit_value_t args[2] = { v, e.v }; lt = jit_insn_call_native(c.F, "", (void*) jit_less_, sig, args, 2, JIT_CALL_NOTHROW); c.insn_delete_temporary(e); } } jit_type_free(sig); if (ge) { if (lt) { return jit_insn_and(c.F, ge, lt); } else { return ge; } } else { return lt; } } else { jit_value_t cond; auto p = begin->compile(c); if (begin->type.nature == Nature::VALUE) { cond = jit_insn_eq(c.F, v, p.v); } else { jit_value_t args[2] = { v, p.v }; cond = jit_insn_call_native(c.F, "", (void*) jit_equals_, sig, args, 2, JIT_CALL_NOTHROW); c.insn_delete_temporary(p); } jit_type_free(sig); return cond; } }
jit_value jit_function::insn_call_native (const char *name, void *native_func, jit_type_t signature, jit_value_t *args, unsigned int num_args, int flags) { value_wrap(jit_insn_call_native (func, name, native_func, signature, args, num_args, flags)); }
virtual jit_value_t jit_compile(jit_function_t func) { return jit_insn_call_native(func ,"native" , (void*)m_method , jit_type_create_signature(jit_abi_stdcall, jit_type_sys_bool, 0, 0, 1) , 0, 0, 0); }
void call_alignment(jit_function_t func, jit_value_t addr, int size, int store, uint32_t pc) { jit_value_t args[] = {addr, make_uint(size), make_uint(store), make_uint(pc)}; jit_insn_call_native(func, 0, (void *) alignment, sig_4, args, 4, 0); }
void call_overflow(jit_function_t func, jit_value_t a, jit_value_t b, int dir, uint32_t pc, uint32_t inst) { jit_value_t args[] = {a, b, make_uint(dir), make_uint(pc), make_uint(inst)}; jit_insn_call_native(func, 0, (void *) overflow, sig_5, args, 5, 0); }
void call_branch_block(jit_function_t func, block_t *block) { jit_value_t args[] = {make_ptr(block)}; jit_insn_call_native(func, 0, (void *) branch_block, sig_1_ptr, args, 1, 0); }
void *LibJITFormula::emit (CallExprAST *expr) { if (expr->function == "if") { // We're going to make a new temporary, and store a value to it depending // on what happens here. jit_value_t result = jit_value_create (function, jit_type_float64); // if (cond) jit_value_t cond = (jit_value_t)expr->args[0]->generate (this); if (!cond) return NULL; jit_value_t one = jit_value_create_float64_constant (function, jit_type_float64, 1.0); jit_value_t comparison = jit_insn_eq (function, cond, one); jit_label_t label_if = jit_label_undefined; jit_label_t label_end = jit_label_undefined; jit_insn_branch_if_not (function, comparison, &label_if); // (the if-value) jit_value_t t = (jit_value_t)expr->args[1]->generate (this); jit_insn_store (function, result, t); jit_insn_branch (function, &label_end); // The else branches to here... jit_insn_label (function, &label_if); // (the else-value) jit_value_t f = (jit_value_t)expr->args[2]->generate (this); jit_insn_store (function, result, f); jit_insn_label (function, &label_end); return result; } else if (expr->function == "sign") { // Same thing as the if-function above jit_value_t one = jit_value_create_float64_constant (function, jit_type_float64, 1.0); jit_value_t zero = jit_value_create_float64_constant (function, jit_type_float64, 0.0); jit_value_t minusone = jit_value_create_float64_constant (function, jit_type_float64, -1.0); jit_value_t arg = (jit_value_t)expr->args[0]->generate (this); jit_value_t result = jit_value_create (function, jit_type_float64); jit_label_t label_end = jit_label_undefined; jit_value_t positive = jit_insn_gt (function, arg, zero); jit_label_t label_positive = jit_label_undefined; jit_insn_branch_if_not (function, positive, &label_positive); jit_insn_store (function, result, one); jit_insn_branch (function, &label_end); jit_insn_label (function, &label_positive); jit_value_t negative = jit_insn_lt (function, arg, zero); jit_label_t label_negative = jit_label_undefined; jit_insn_branch_if_not (function, negative, &label_negative); jit_insn_store (function, result, minusone); jit_insn_branch (function, &label_end); jit_insn_label (function, &label_negative); jit_insn_store (function, result, zero); jit_insn_label (function, &label_end); return result; } else if (expr->function == "atan2") { // HACK: We have to put this here since it has two arguments jit_value_t args[2]; args[0] = (jit_value_t)expr->args[0]->generate (this); args[1] = (jit_value_t)expr->args[1]->generate (this); jit_type_t params[2]; params[0] = jit_type_float64; params[1] = jit_type_float64; jit_type_t signature; signature = jit_type_create_signature (jit_abi_cdecl, jit_type_float64, params, 2, 1); return jit_insn_call_native (function, "atan2", (void *)((double (*)(double, double))atan2), signature, args, 2, 0); } // Compile the arguments // HACK: we just assume for now that if() is the only thing with // more than one argument. jit_value_t arg = (jit_value_t)expr->args[0]->generate (this); // Create a native signature for our few native functions jit_type_t params[1]; params[0] = jit_type_float64; jit_type_t signature; signature = jit_type_create_signature (jit_abi_cdecl, jit_type_float64, params, 1, 1); // Several of the jit_insn_FUNC constructs below have been replaced // by jit_insn_call_native, because the intrinsic instructions seem to // give the wrong answers. if (expr->function == "sin") return jit_insn_call_native (function, "sin", (void *)((double (*)(double))sin), signature, &arg, 1, 0); else if (expr->function == "cos") return jit_insn_call_native (function, "cos", (void *)((double (*)(double))cos), signature, &arg, 1, 0); else if (expr->function == "tan") return jit_insn_call_native (function, "tan", (void *)((double (*)(double))tan), signature, &arg, 1, 0); else if (expr->function == "asin") return jit_insn_call_native (function, "asin", (void *)((double (*)(double))asin), signature, &arg, 1, 0); else if (expr->function == "acos") return jit_insn_call_native (function, "acos", (void *)((double (*)(double))acos), signature, &arg, 1, 0); else if (expr->function == "atan") return jit_insn_call_native (function, "atan", (void *)((double (*)(double))atan), signature, &arg, 1, 0); else if (expr->function == "sinh") return jit_insn_call_native (function, "sinh", (void *)((double (*)(double))sinh), signature, &arg, 1, 0); else if (expr->function == "cosh") return jit_insn_call_native (function, "cosh", (void *)((double (*)(double))cosh), signature, &arg, 1, 0); else if (expr->function == "tanh") return jit_insn_call_native (function, "tanh", (void *)((double (*)(double))tanh), signature, &arg, 1, 0); else if (expr->function == "asinh") return jit_insn_call_native (function, "asinh", (void *)((double (*)(double))asinh), signature, &arg, 1, 0); else if (expr->function == "acosh") return jit_insn_call_native (function, "acosh", (void *)((double (*)(double))acosh), signature, &arg, 1, 0); else if (expr->function == "atanh") return jit_insn_call_native (function, "atanh", (void *)((double (*)(double))atanh), signature, &arg, 1, 0); else if (expr->function == "log2") return jit_insn_call_native (function, "log2", (void *)((double (*)(double))log2), signature, &arg, 1, 0); else if (expr->function == "log" || expr->function == "log10") return jit_insn_call_native (function, "log10", (void *)((double (*)(double))log10), signature, &arg, 1, 0); else if (expr->function == "ln") return jit_insn_call_native (function, "log", (void *)((double (*)(double))log), signature, &arg, 1, 0); else if (expr->function == "exp") return jit_insn_call_native (function, "exp", (void *)((double (*)(double))exp), signature, &arg, 1, 0); else if (expr->function == "sqrt") return jit_insn_sqrt (function, arg); else if (expr->function == "abs") return jit_insn_abs (function, arg); else if (expr->function == "rint") return jit_insn_call_native (function, "rint", (void *)((double (*)(double))rint), signature, &arg, 1, 0); else return NULL; }
jit_value_t call_read_copcreg(jit_function_t func, int cop, int reg) { jit_value_t args[] = {make_uint(cop), make_uint(reg)}; return jit_insn_call_native(func, 0, (void *) read_copcreg, sig_2, args, 2, 0); }
void call_store_memory(jit_function_t func, int size, jit_value_t ptr, jit_value_t val, uint32_t pc) { jit_value_t args[] = {make_uint(size), ptr, val, make_uint(pc)}; jit_insn_call_native(func, 0, (void *) store_memory, sig_4, args, 4, 0); }
void call_absorb_muldiv_delay(jit_function_t func) { jit_insn_call_native(func, 0, (void *) absorb_muldiv_delay, sig_0, NULL, 0, 0); }
jit_value_t call_copfun(jit_function_t func, int cop, int cofun, uint32_t inst) { jit_value_t args[] = {make_uint(cop), make_uint(cofun), make_uint(inst)}; return jit_insn_call_native(func, 0, (void *) copfun, sig_3, args, 3, 0); }
int __cdecl main(int argc, char **argv) { jit_context_t context; jit_type_t params[5]; jit_type_t signature; jit_function_t function; void* _this; bool a = true; bool b = true; bool c = true; bool d = true; void *args[5]; //jit_int result;` MyClass* pMyClass = new MyClass; void*** pMyClassCasted = (void***)pMyClass; void ** vtable_ptr = *pMyClassCasted; std::cout<<vtable_ptr[0]<<std::endl; typedef void (*void_method)(); void** new_vtable = (void**)malloc(sizeof(void*)*2); *pMyClassCasted = new_vtable; memcpy(new_vtable, vtable_ptr, NativeVTableIndexInspector::getVirtualMethodCount<MySubClass>()*sizeof(void*)); auto method_ptr = &MyClass::doSmth; auto method_ptr2 = &MyClass::doSmthElse; //std::cout<< "index of MyClass::doSmth"<< NativeVTableIndexInspector::getIndexOf(&MyClass::doSmth) <<std::endl; /* Create a context to hold the JIT's primary state */ context = jit_context_create(); // DEBUGGER debugger = jit_debugger_create(context); jit_debugger_set_hook(context, debugger_hook); /* // Create threads pthread_t thread0; pthread_t thread1; pthread_create(&thread0, NULL, thread0_func, NULL); pthread_create(&thread1, NULL, thread1_func, NULL);*/ /* Lock the context while we construct the function */ jit_context_build_start(context); /* Build the function signature */ params[0] = jit_type_void_ptr; // this params[1] = jit_type_sys_bool; // a params[2] = jit_type_sys_bool; // b params[3] = jit_type_sys_bool; // c params[4] = jit_type_sys_bool; // d signature = jit_type_create_signature(jit_abi_thiscall, jit_type_void_ptr, params, 1, 1); /* Create the function object */ function = jit_function_create(context, signature); //Expression* expression = new Or(new And(new ArgumentAccess(0), new ArgumentAccess(1)) // , new And(new ArgumentAccess(2), new NativeMethodCall(native_method))); void* hacked_ptr = *reinterpret_cast<void**>(&method_ptr); signature = jit_type_create_signature(jit_abi_thiscall, jit_type_void, params, 1, 1); jit_value_t args_this = jit_value_create_nint_constant(function, jit_type_void_ptr, 0); jit_insn_call_native(function, "doSmth", hacked_ptr, signature, &args_this, 1, 0); jit_insn_mark_breakpoint (function, JIT_DEBUGGER_DATA1_LINE, 0); jit_insn_return(function, jit_value_get_param(function, 0)); jit_insn_mark_breakpoint (function, JIT_DEBUGGER_DATA1_LINE, 1); /* Unlock the context. It will be automatically locked for us when the on-demand compiler is called */ jit_context_build_end(context); /* Execute the function and print the result. This will arrange to call the on-demand compiler to build the function's body */ jit_function_compile(function); _this = (void*)10; void* result; args[0] = &pMyClass; args[1] = &a; args[2] = &b; args[3] = &c; args[4] = &d; /*__asm { mov ecx, pMyClass };*/ jit_function_apply(function, args, &result); printf("(a && b) || (c && call) = %d\n", result); new_vtable[1] = jit_function_to_vtable_pointer(function); result = pMyClass->doSmthElse(); printf("(a && b) || (c && call) = %d\n", result); /* Execute the function again, to demonstrate that the on-demand compiler is not invoked a second time */ /*arg1 = 13; arg2 = 5; arg3 = 7; args[0] = &arg1; args[1] = &arg2; args[2] = &arg3; jit_function_apply(function, args, &result); printf("mul_add(13, 5, 7) = %d\n", (int)result);*/ /* Execute the function a third time, after it is recompiled */ /*arg1 = 2; arg2 = 18; arg3 = -3; args[0] = &arg1; args[1] = &arg2; args[2] = &arg3; jit_function_apply(function, args, &result); printf("mul_add(2, 18, -3) = %d\n", (int)result);*/ /* Clean up */ jit_context_destroy(context); system("pause"); /* Finished */ return 0; }
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; }
void *LibJITFormula::emit (BinaryExprAST *expr) { // Deal with assign separately, we don't want to compile the LHS if (expr->op == "=") { VariableExprAST *var = dynamic_cast<VariableExprAST *>(expr->LHS); if (!var) return NULL; jit_value_t val = (jit_value_t)expr->RHS->generate (this); if (!val) return NULL; // Get a pointer for this variable jit_value_t pointer = jit_value_create_nint_constant (function, jit_type_void_ptr, (jit_nint)var->pointer); // Emit a store instruction jit_insn_store_relative (function, pointer, 0, val); // Return value return val; } // Generate both sides jit_value_t L = (jit_value_t)expr->LHS->generate (this); jit_value_t R = (jit_value_t)expr->RHS->generate (this); if (L == NULL || R == NULL) return NULL; if (expr->op == "<=") return jit_insn_le (function, L, R); else if (expr->op == ">=") return jit_insn_ge (function, L, R); else if (expr->op == "!=") return jit_insn_ne (function, L, R); else if (expr->op == "==") return jit_insn_eq (function, L, R); else if (expr->op == "<") return jit_insn_lt (function, L, R); else if (expr->op == ">") return jit_insn_gt (function, L, R); else if (expr->op == "+") return jit_insn_add (function, L, R); else if (expr->op == "-") return jit_insn_sub (function, L, R); else if (expr->op == "*") return jit_insn_mul (function, L, R); else if (expr->op == "/") return jit_insn_div (function, L, R); else if (expr->op == "^") { // jit_insn_pow seems to give the wrong results? jit_type_t params[2]; params[0] = jit_type_float64; params[1] = jit_type_float64; jit_type_t signature; signature = jit_type_create_signature (jit_abi_cdecl, jit_type_float64, params, 2, 1); jit_value_t args[2]; args[0] = L; args[1] = R; return jit_insn_call_native (function, "pow", (void *)((double (*)(double, double))pow), signature, args, 2, 0); } else return NULL; }
void call_timestamp_inc(jit_function_t func, uint32_t amount) { jit_value_t args[] = {make_uint(amount)}; jit_insn_call_native(func, 0, (void *) timestamp_inc, sig_1, args, 1, 0); }
void call_step(jit_function_t func, uint32_t arg) { jit_value_t args[] = {make_uint(arg)}; jit_insn_call_native(func, 0, (void *) step, sig_1, args, 1, 0); }
void call_mul_delay(jit_function_t func, jit_value_t a, jit_value_t b, int is_signed) { jit_value_t args[] = {a, b, make_uint(is_signed)}; jit_insn_call_native(func, 0, (void *) mul_delay, sig_3, args, 3, 0); }
jit_value_t call_signext(jit_function_t func, int size, jit_value_t val) { jit_value_t args[] = {make_uint(size), val}; return jit_insn_call_native(func, 0, (void *) signext, sig_2, args, 2, 0); }
void call_check_irq(jit_function_t func, uint32_t pc) { jit_value_t args[] = {make_uint(pc)}; jit_insn_call_native(func, 0, (void *) check_irq, sig_1, args, 1, 0); }
void call_break(jit_function_t func, uint32_t code, uint32_t pc, uint32_t inst) { jit_value_t args[] = {make_uint(code), make_uint(pc), make_uint(inst)}; jit_insn_call_native(func, 0, (void *) break_, sig_3, args, 3, 0); }
jit_value_t call_load_memory(jit_function_t func, int size, jit_value_t ptr, uint32_t pc) { jit_value_t args[] = {make_uint(size), ptr, make_uint(pc)}; return jit_insn_call_native(func, 0, (void *) load_memory, sig_3, args, 3, 0); }
void call_branch(jit_function_t func, jit_value_t val) { jit_value_t args[] = {val}; jit_insn_call_native(func, 0, (void *) branch, sig_1, args, 1, 0); }
void call_write_copcreg(jit_function_t func, int cop, int reg, jit_value_t val) { jit_value_t args[] = {make_uint(cop), make_uint(reg), val}; jit_insn_call_native(func, 0, (void *) write_copcreg, sig_3, args, 3, 0); }
/* * Inline function to create a new string with the given length */ static int _ILJitSystemStringNew(ILJITCoder *jitCoder, ILMethod *method, ILCoderMethodInfo *methodInfo, ILJitStackItem *args, ILInt32 numArgs) { ILJitFunction jitFunction = ILJitFunctionFromILMethod(method); ILClass *stringClass = ILMethod_Owner(method); ILJitValue newString; ILJitValue callArgs[2]; if(!jitFunction) { /* We need to layout the class first. */ if(!_LayoutClass(ILExecThreadCurrent(), stringClass)) { return 0; } if(!(jitFunction = ILJitFunctionFromILMethod(method))) { return 0; } } #if !defined(IL_CONFIG_REDUCE_CODE) && !defined(IL_WITHOUT_TOOLS) && defined(_IL_JIT_ENABLE_DEBUG) if(jitCoder->flags & IL_CODER_FLAG_STATS) { ILMutexLock(globalTraceMutex); fprintf(stdout, "Inline System.String::NewString\n"); ILMutexUnlock(globalTraceMutex); } #endif /* We call the alloc functions. */ /* They thow an out of memory exception so we don't need to care. */ callArgs[0] = jit_value_create_nint_constant(jitCoder->jitFunction, _IL_JIT_TYPE_VPTR, (jit_nint)stringClass); if(!callArgs[0]) { return 0; } callArgs[1] = _ILJitValueConvertImplicit(jitCoder->jitFunction, _ILJitStackItemValue(args[0]), _IL_JIT_TYPE_UINT32); if(!callArgs[1]) { return 0; } newString = jit_insn_call_native(jitCoder->jitFunction, "_ILJitStringAlloc", _ILJitStringAlloc, _ILJitSignature_ILJitStringAlloc, callArgs, 2, 0); if(!newString) { return 0; } _ILJitStackPushValue(jitCoder, newString); return 1; }