Example #1
0
int main()
{
    jit_context_t ctx = jit_context_create();

    while (1)
    {
	func_t func;
	double x;
        int a;
        char buf[1024];
	printf("jitcalc> ");
        if (!fgets(buf, sizeof(buf), stdin))
            break;
	if (strchr("\r\n", buf[0]))
            continue;
	func = compile(ctx, buf, strlen(buf));

	for (a = 0; a < 3; ++a)
	{
	    x = func((double)a, (double)2, (double)7);
	    printf("func(%lf, 2, 7) = %lf\n", (double)a, x);
        }
    }

    jit_context_destroy(ctx);

    return 0;
}
Example #2
0
int main() {
  jit_context_t ctx;

  // Create a context to hold the JIT's primary stats
  ctx = jit_context_create();

  // Lock the context while we build and compile the function
  jit_context_build_start(ctx);

  // Build the function signature
  jit_function_t function;
  jit_type_t params[3];
  jit_type_t signature;
  params[0] = jit_type_int;
  params[1] = jit_type_int;
  params[2] = jit_type_int;
  signature =
      jit_type_create_signature(jit_abi_cdecl, jit_type_int, params, 3, 1);

  // Create the function object
  function = jit_function_create(ctx, signature);
  jit_type_free(signature);

  // Construct the function body
  jit_value_t x, y, z;
  x = jit_value_get_param(function, 0);
  y = jit_value_get_param(function, 1);
  z = jit_value_get_param(function, 2);
  jit_value_t temp1, temp2;
  temp1 = jit_insn_mul(function, x, y);
  temp2 = jit_insn_add(function, temp1, z);
  jit_insn_return(function, temp2);

  // Compile the function
  jit_function_compile(function);

  // Unlock the context
  jit_context_build_end(ctx);

  // Execute the function and print the result
  jit_int arg1, arg2, arg3;
  jit_int result;
  void *args[3];
  arg1 = 3;
  arg2 = 5;
  arg3 = 2;
  args[0] = &arg1;
  args[1] = &arg2;
  args[2] = &arg3;
  jit_function_apply(function, args, &result);
  printf("mul_add(3,5,2) = %d\n", (int)result);

  // Clean up
  jit_context_destroy(ctx);
  return 0;
}
/* End-of-global-destruction cleanup hook.
 * Actually installed in BOOT XS section. */
void
pj_global_state_final_cleanup(pTHX_ void *ptr)
{
  (void)ptr;
  
  PJ_DEBUG("pj_jit_final_cleanup after global destruction.\n");

  if (PJ_jit_context != NULL)
    jit_context_destroy(PJ_jit_context);
}
Example #4
0
int main(int argc, char const *argv[]) {
    jit_context_t cx = jit_context_create();

    char *data = calloc(50000, sizeof(char));
    jit_ptr arg1;
    void *args[1];

    FILE *fp = fopen(argv[1], "rb");
    jit_function_t function = bf_compile(cx, fp);
    fclose(fp);
    jit_context_build_end(cx);
    arg1 = data;
    args[0] = &arg1;
    jit_function_apply(function, args, NULL);
    jit_context_destroy(cx);
    return 0;
}
Example #5
0
int main(int argc, char **argv) {
	if (argc < 4) {
		LOGF("you must provide 3 arguments to function: F, X, LEN");
		return 0;
	}
	jit_context = jit_context_create();
	jit_context_build_start(jit_context);

	jit_function_t func = parse_function();
	jit_context_build_end(jit_context);
	
	jit_float64 result,
				f = atof(argv[1]);
	jit_nuint x = atoi(argv[2]),
			  len = atoi(argv[3]);
	void *args[3] = {&f, &x, &len};
	
	jit_function_apply(func, args, &result);
	LOGF("f(%f, %i, %i) = %f", f, x, len, result);

	jit_context_destroy(jit_context);

	return 0;
}
Example #6
0
int main(int argc, char **argv)
{
	jit_context_t context;
	jit_type_t params[2];
	jit_type_t signature;
	jit_function_t function;
	jit_value_t x, y;
	jit_value_t temp1, temp2;
	jit_value_t temp3, temp4;
	jit_value_t temp_args[2];
	jit_label_t label1 = jit_label_undefined;
	jit_label_t label2 = jit_label_undefined;
	jit_uint arg1, arg2;
	void *args[2];
	jit_uint result;

	/* Create a context to hold the JIT's primary state */
	context = jit_context_create();

	/* Lock the context while we build and compile the function */
	jit_context_build_start(context);

	/* Build the function signature */
	params[0] = jit_type_uint;
	params[1] = jit_type_uint;
	signature = jit_type_create_signature
		(jit_abi_cdecl, jit_type_uint, params, 2, 1);

	/* Create the function object */
	function = jit_function_create(context, signature);

	/* Check the condition "if(x == y)" */
	x = jit_value_get_param(function, 0);
	y = jit_value_get_param(function, 1);
	temp1 = jit_insn_eq(function, x, y);
	jit_insn_branch_if_not(function, temp1, &label1);

	/* Implement "return x" */
	jit_insn_return(function, x);

	/* Set "label1" at this position */
	jit_insn_label(function, &label1);

	/* Check the condition "if(x < y)" */
	temp2 = jit_insn_lt(function, x, y);
	jit_insn_branch_if_not(function, temp2, &label2);

	/* Implement "return gcd(x, y - x)" */
	temp_args[0] = x;
	temp_args[1] = jit_insn_sub(function, y, x);
	temp3 = jit_insn_call
		(function, "gcd", function, 0, temp_args, 2, 0);
	jit_insn_return(function, temp3);

	/* Set "label2" at this position */
	jit_insn_label(function, &label2);

	/* Implement "return gcd(x - y, y)" */
	temp_args[0] = jit_insn_sub(function, x, y);
	temp_args[1] = y;
	temp4 = jit_insn_call
		(function, "gcd", function, 0, temp_args, 2, 0);
	jit_insn_return(function, temp4);

	/* Compile the function */
	jit_function_compile(function);

	/* Unlock the context */
	jit_context_build_end(context);

	/* Execute the function and print the result */
	arg1 = 27;
	arg2 = 14;
	args[0] = &arg1;
	args[1] = &arg2;
	jit_function_apply(function, args, &result);
	printf("gcd(27, 14) = %u\n", (unsigned int)result);

	/* Clean up */
	jit_context_destroy(context);

	/* Finished */
	return 0;
}
Example #7
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;
}
Example #8
0
void function_destroy() {
	list_free(funclist, funclist);
	jit_context_build_end(jit_context);
	jit_context_destroy(jit_context);
}