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 JitInstanceMemberFunction::compileThisAdjustementThunk( size_t a_uiThisOffset ) const
{
    o_assert(m_ThisAdjustmentThunks.find(a_uiThisOffset) == m_ThisAdjustmentThunks.end());

    size_t argCount = getSignature()->getParameterCount()+1; // parameters+this
    jit_value_t* args = o_allocate_n(argCount, jit_value_t); 

    // The fixing function has the same signature as the indirected one
    jit_function_t func = jit_function_create((jit_context_t)m_jit_context.context, jit_function_get_signature((jit_function_t)m_jit_function.function));

    jit_value_t new_this = jit_insn_add(func, jit_value_get_param(func, 0), jit_value_create_nint_constant(func, jit_type_nint, -((int)a_uiThisOffset)));

    args[0] = new_this;

    size_t i = 1;
    for(;i<argCount;++i)
    {
        args[i] = jit_value_get_param(func, i);
    }
    string name = "[thunk]:"+m_pSubroutine->getQualifiedDecoratedName()+":adjustor{"+boost::lexical_cast<string>(a_uiThisOffset)+"}";
    jit_insn_call(func
                , name.c_str()
                , (jit_function_t)m_jit_function.function
                , jit_function_get_signature((jit_function_t)m_jit_function.function)
                , args
                , argCount
                , 0);

    int result = jit_function_compile(func);
    o_assert(result != 0);

    o_deallocate_n(args, argCount, jit_value_t);

    m_ThisAdjustmentThunks[a_uiThisOffset] = func;
}
Example #3
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;
}
/*
 * Create the slots for the fixed parameters of the current function.
 * Returns 0 on error else 1.
 */
static int _ILJitParamsCreate(ILJITCoder *coder)
{
	ILJitType signature = jit_function_get_signature(coder->jitFunction);

	if(signature)
	{
		ILJitLocalSlot *param = 0;
		ILUInt32 numParams = jit_type_num_params(signature);

#ifdef IL_JIT_THREAD_IN_SIGNATURE
		/* We don't include the ILExecThread in the params. */
		if(numParams > 1)
		{
			ILInt32 current = 0;

			/* Allocate the locals for the parameters */
			_ILJitLocalsAlloc(coder->jitParams, numParams - 1);

			for(current = 1; current < numParams; ++current)
			{
				param = _ILJitParamGet(coder, current - 1);

				param->value = jit_value_get_param(coder->jitFunction, current);
				param->flags = 0;
				param->refValue = 0;
			}
			coder->jitParams.numSlots = numParams - 1;
		}
#else
		if(numParams > 0)
		{
			ILInt32 current = 0;

			/* Allocate the locals for the parameters */
			_ILJitLocalsAlloc(coder->jitParams, numParams);

			for(current = 0; current < numParams; ++current)
			{
				param = _ILJitParamGet(coder, current);

				param->value = jit_value_get_param(coder->jitFunction, current);
				param->flags = 0;
				param->refValue = 0;
			}
			coder->jitParams.numSlots = numParams;
		}
#endif
		else
		{
			coder->jitParams.numSlots = 0;
		}
		return 1;
	}
	return 0;
}
Example #5
0
Compiler::value VariableValue::compile(Compiler& c) const {

	// std::cout << "Compile var " << name << " " << version << std::endl;
	// cout << "compile vv " << name << " : " << type << "(" << (int) scope << ")" << endl;
	// cout << "req type : " << req_type << endl;

	if (scope == VarScope::CAPTURE) {
		return c.insn_get_capture(capture_index, type);
	}

	jit_value_t v;
	if (scope == VarScope::INTERNAL) {
		v = c.vm->internals.at(name);
	} else if (scope == VarScope::LOCAL) {
		auto f = dynamic_cast<Function*>(var->value);
		if (has_version && f) {
			return f->compile_version(c, version);
		}
		v = c.get_var(name).v;
	} else { /* if (scope == VarScope::PARAMETER) */
		v = jit_value_get_param(c.F, 1 + var->index); // 1 offset for function ptr
	}

	if (var->type.nature != Nature::UNKNOWN and var->type.nature != Nature::POINTER and type.nature == Nature::POINTER) {
		return {VM::value_to_pointer(c.F, v, var->type), type};
	}
	if (var->type.raw_type == RawType::INTEGER and type.raw_type == RawType::REAL) {
		return {VM::int_to_real(c.F, v), type};
	}
	return {v, type};
}
Example #6
0
jit_function_t parse_function() {
	jit_type_t params[3] = {jit_type_float64, jit_type_nuint, jit_type_nuint},
			   signature = jit_type_create_signature(jit_abi_cdecl, jit_type_float64, params, 3, 1);
	jit_function_t func = jit_function_create(jit_context, signature);
	const_rate = jit_value_create_float64_constant(func, jit_type_float64, (double)RATE),
	const_pi = jit_value_create_float64_constant(func, jit_type_float64, (double)M_PI),

	val_freq = jit_value_get_param(func, 0);
	val_sample = jit_value_get_param(func, 1);
	val_length = jit_value_get_param(func, 2);
	
	jit_insn_return(func, parse_recursive(func));
	jit_function_compile(func);

	return func;
}
Example #7
0
int compile_mul_add(jit_function_t function)
{
	jit_value_t x, y, z;
	jit_value_t temp1, temp2;
    
	printf("Compiling mul_add on demand\n");
    
	x = jit_value_get_param(function, 0);
	y = jit_value_get_param(function, 1);
	z = jit_value_get_param(function, 2);
    
	temp1 = jit_insn_mul(function, x, y);
	temp2 = jit_insn_add(function, temp1, z);
    
	jit_insn_return(function, temp2);
	return 1;
}
jit_function_t create_function() {
	jit_function_t func = jit_function_create(context, block_sig);
	state = jit_value_get_param(func, 0);
	_ReadAbsorb = jit_value_get_param(func, 1);
	_ReadAbsorbWhich = jit_value_get_param(func, 2);
	_ReadFudge = jit_value_get_param(func, 3);
	LDWhich = jit_value_get_param(func, 4);
	LDValue = jit_value_get_param(func, 5);
	LDAbsorb = jit_value_get_param(func, 6);
	return func;
}
Example #9
0
Compiler::value VariableValue::compile_l(Compiler& c) const {

	if (scope == VarScope::CAPTURE) {
		return c.insn_address_of(c.insn_get_capture(capture_index, type));
	}

	jit_value_t v;
	// No internal values here
	if (scope == VarScope::LOCAL) {
		v = c.get_var(name).v;
	} else { /* if (scope == VarScope::PARAMETER) */
		v = jit_value_get_param(c.F, 1 + var->index); // 1 offset for function ptr
	}
	return c.insn_address_of({v, type});
}
Example #10
0
int
pj_tree_jit(jit_context_t context, pj_term_t *term, jit_function_t *outfun, pj_basic_type *funtype)
{
  unsigned int i;
  jit_function_t function;
  jit_type_t *params;
  jit_type_t signature;

  jit_context_build_start(context);

  /* Get the "function type" which is the type that will be used for the
   * return value as well as all arguments. Double trumps ints. */
  *funtype = pj_tree_determine_funtype(term);

  /* Extract all variable occurrances from the AST */
  pj_variable_t **vars;
  unsigned int nvars;
  pj_tree_extract_vars(term, &vars, &nvars);
  printf("Found %i variable occurrances in tree.\n", nvars);

  /* Naive assumption: the maximum ivar is the total number if distinct arguments (-1) */
  unsigned int max_var = 0;
  for (i = 0; i < nvars; ++i) {
    if (max_var < (unsigned int)vars[i]->ivar)
      max_var = vars[i]->ivar;
  }
  printf("Found %i distinct variables in tree.\n", 1+max_var);
  nvars = max_var+1;
  free(vars);

  /* Setup libjit func signature */
  params = (jit_type_t *)malloc(nvars*sizeof(jit_type_t));
  for (i = 0; i < nvars; ++i) {
    params[i] = (*funtype == pj_int_type ? jit_type_sys_int : jit_type_sys_double);
  }
  signature = jit_type_create_signature(
    jit_abi_cdecl,
    (*funtype == pj_int_type ? jit_type_sys_int : jit_type_sys_double),
    params,
    nvars,
    1
  );
  function = jit_function_create(context, signature);

  /* Setup libjit values for func params */
  jit_value_t *var_values;
  var_values = (jit_value_t *)malloc(nvars*sizeof(jit_value_t));
  for (i = 0; i < nvars; ++i) {
    var_values[i] = jit_value_get_param(function, i);
  }

  /* Recursively emit instructions for JIT and final return */
  jit_value_t rv = pj_jit_internal(function, var_values, nvars, term);
  jit_insn_return(function, rv);

  /* Make it so! */
  /* jit_function_set_optimization_level(function, jit_function_get_max_optimization_level()); */
  jit_function_compile(function);
  jit_context_build_end(context);

  *outfun = function;
  return 0;
}
Example #11
0
 virtual jit_value_t jit_compile(jit_function_t func) 
 {
     return jit_value_get_param(func, m_index+1); // +1 to skip "this" pointer
 }
Example #12
0
File: jit.cpp Project: goccy/gperl
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;
}
Example #13
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;
}
Example #14
0
/*@
 * @deftypefun int _jit_create_entry_insns (jit_function_t @var{func})
 * Create instructions in the entry block to initialize the
 * registers and frame offsets that contain the parameters.
 * Returns zero if out of memory.
 *
 * This function is called when a builder is initialized.  It should
 * scan the signature and decide which register or frame position
 * contains each of the parameters and then call either
 * @code{jit_insn_incoming_reg} or @code{jit_insn_incoming_frame_posn}
 * to notify @code{libjit} of the location.
 * @end deftypefun
@*/
int _jit_create_entry_insns(jit_function_t func)
{
	jit_type_t signature = func->signature;
	jit_type_t type;
	jit_nint offset;
	jit_value_t value;
	unsigned int num_params;
	unsigned int param;

	/* Reset the frame size for this function */
	func->builder->frame_size = 0;

	/* The starting parameter offset.  We use negative offsets to indicate
	   an offset into the "args" block, and positive offsets to indicate
	   an offset into the "frame" block.  The negative values will be
	   flipped when we output the argument opcodes for interpretation */
	offset = -1;

	/* If the function is nested, then we need two extra parameters
	   to pass the pointer to the parent's local variables and arguments */
	if(func->nested_parent)
	{
		offset -= 2;
	}

	/* Allocate the structure return pointer */
	value = jit_value_get_struct_pointer(func);
	if(value)
	{
		if(!jit_insn_incoming_frame_posn(func, value, offset))
		{
			return 0;
		}
		--offset;
	}

	/* Allocate the parameter offsets */
	num_params = jit_type_num_params(signature);
	for(param = 0; param < num_params; ++param)
	{
		value = jit_value_get_param(func, param);
		if(!value)
		{
			continue;
		}

		type = jit_type_remove_tags(jit_value_get_type(value));
		switch(type->kind)
		{
		case JIT_TYPE_SBYTE:
		case JIT_TYPE_UBYTE:
			if(!jit_insn_incoming_frame_posn(func, value,
							 offset - _jit_int_lowest_byte()))
			{
				return 0;
			}
			--offset;
			break;

		case JIT_TYPE_SHORT:
		case JIT_TYPE_USHORT:
			if(!jit_insn_incoming_frame_posn(func, value,
							 offset - _jit_int_lowest_short()))
			{
				return 0;
			}
			--offset;
			break;

		case JIT_TYPE_INT:
		case JIT_TYPE_UINT:
		case JIT_TYPE_NINT:
		case JIT_TYPE_NUINT:
		case JIT_TYPE_SIGNATURE:
		case JIT_TYPE_PTR:
		case JIT_TYPE_LONG:
		case JIT_TYPE_ULONG:
		case JIT_TYPE_FLOAT32:
		case JIT_TYPE_FLOAT64:
		case JIT_TYPE_NFLOAT:
			if(!jit_insn_incoming_frame_posn(func, value, offset))
			{
				return 0;
			}
			--offset;
			break;

		case JIT_TYPE_STRUCT:
		case JIT_TYPE_UNION:
			if(!jit_insn_incoming_frame_posn(func, value, offset))
			{
				return 0;
			}
			offset -= JIT_NUM_ITEMS_IN_STRUCT(jit_type_get_size(type));
			break;
		}
	}
	return 1;
}
Example #15
0
/*@
 * @deftypemethod jit_function jit_value get_param (unsigned int @var{param})
 * Get the value that corresponds to parameter @var{param}.
 * @end deftypemethod
@*/
jit_value jit_function::get_param(unsigned int param)
{
	value_wrap(jit_value_get_param(func, param));
}
Example #16
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 #17
0
 virtual jit_value_t jit_compile(jit_function_t func) 
 {
     return jit_value_get_param(func, 0);
 }
Example #18
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 #19
0
// LIBJIT
void vm_cpu_4(uint32_t newPC,int opt, int size)
{
	int i;
	PC = newPC;
	nPC = 4;
	RF[0] = 0; //Register $zero must always be zero
	RF[31] = 1; //Return default (if the program does not set to zero, should put error)
	uint32_t HI = 0, LO = 0;  
	uint32_t offset = 4;
	uint8_t halted = 0;
	
	uint32_t instr;
	uint8_t op;
	uint8_t rs;
	uint8_t rt;
	uint8_t rd;
	int16_t immediate;
	uint32_t address;
	
	uint8_t shamt;
	uint8_t funct;
	
	uint64_t mult; 
	
	/*lib jit variables */
	
	jit_context_t context;
	jit_type_t signature;
        jit_function_t function;
	jit_type_t params[VM_MEMORY_SZ+2];
	jit_int result;
	
	jit_value_t constant_sum;
	jit_value_t constant_while;
	jit_value_t v_it;
	jit_value_t constant_update;
	jit_value_t compare;
	jit_value_t reg[32];		/* Reg */
	jit_value_t mem[VM_MEMORY_SZ];	/* Memory */
	jit_label_t labels[10]; /* Labs for jumping :D */
	jit_value_t sum, t_sum;
	void *args[VM_MEMORY_SZ+2];	/* Args */
	jit_int arg_uint[VM_MEMORY_SZ+2];

	/* 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);
	
	for(i=0; i<(VM_MEMORY_SZ+2); i++) {
	    params[i] = jit_type_int;
	}
		
	signature = jit_type_create_signature(jit_abi_cdecl, jit_type_int, params, VM_MEMORY_SZ+2, 1);
	
	/* Create the function object */
	function = jit_function_create(context, signature);
	jit_type_free(signature);
	
	// Read memory and start registers
	for(i=0; i<VM_MEMORY_SZ; i++) {
	  //printf("%d\n",i);
	  mem[i] = jit_value_get_param(function, i);
	}
	reg[0] = jit_value_get_param(function, VM_MEMORY_SZ);
	reg[31] = jit_value_get_param(function, VM_MEMORY_SZ+1);

	/*int verify = 0 - 1;
	for (i=1; i<VM_MEMORY_SZ; i++) {
	    if((i%2)==0) {
		verify = verify + (i);
	    }
	    else {
		verify = verify - i;
	    }
	}
	printf("verify %d\n", verify); */

	int l_index;
				
	// Only doing the micro benchmark, for analysis 
	// Addiu
#define loopSize 10000 
#define smallerLoopSize 1000
#define opPerLoop 100
	// v_it = 0 ; constant_while = loopSize ; constant_update = 1
	v_it = jit_value_create(function, jit_type_uint);
	constant_update = jit_value_create_nint_constant(function, jit_type_int, (int)0);
	jit_insn_store(function, v_it, constant_update);
	
	reg[2] = jit_value_create(function, jit_type_uint);
	constant_while = jit_value_create_nint_constant(function, jit_type_int, 0);
	jit_insn_store(function, reg[2], constant_while);
	reg[3] = jit_value_create(function, jit_type_uint);
	constant_while = jit_value_create_nint_constant(function, jit_type_int, 1);
	jit_insn_store(function, reg[3], constant_while);
	
	
	// do while (v_it < constant_while) {
	jit_insn_label(function, &labels[0]);
	
	if (opt == 0) {
	  constant_update = jit_insn_add(function, reg[2], reg[3]);
	}
	else if(opt == 1) {
	  constant_update = jit_insn_xor(function, reg[2], reg[3]);
	}
	else if(opt == 2) {
	  constant_update = jit_insn_load(function, mem[0]);
	}
	for (l_index = 1; l_index < opPerLoop; l_index++) {
	  if (opt == 0) {
	    constant_update = jit_insn_add(function, constant_update, reg[3]);
	  } 
	  else if(opt == 1) {
	    constant_update = jit_insn_xor(function, constant_update, reg[3]);
	  }
	  else if(opt == 2) {
	    constant_update = jit_insn_load(function, mem[l_index % 5]);
	  }
	}
	
	jit_insn_store(function, reg[2], constant_update);
	
	// do while
	constant_update = jit_value_create_nint_constant(function, jit_type_uint, 1);
	constant_sum = jit_insn_add(function, v_it, constant_update);
	jit_insn_store(function, v_it, constant_sum);
	
	if(size > 0) {
	  constant_while = jit_value_create_nint_constant(function, jit_type_uint, loopSize);
	}
	else {
	  constant_while = jit_value_create_nint_constant(function, jit_type_uint, smallerLoopSize);
	}
	compare = jit_insn_gt(function, constant_while, v_it);
	jit_insn_branch_if(function, compare, &labels[0]);
	
	
	// Return 
	//jit_insn_return(function, reg[2]);
	jit_insn_return(function, reg[2]);
	
	// START OF FINAL PART
	
	/* Compile the function */
	jit_function_compile(function);

	/* Unlock the context */
	jit_context_build_end(context);
	
	// Put memory and first registers
	for (i=0; i<VM_MEMORY_SZ; i++) {
	  arg_uint[i] = (int) VM_memory[i];
	  //arg_uint[i] = (int)i;
	  args[i] = &(arg_uint[i]);
	}
	arg_uint[VM_MEMORY_SZ] = 0;
	args[VM_MEMORY_SZ] = &(arg_uint[VM_MEMORY_SZ]);
	arg_uint[VM_MEMORY_SZ+1] = 1;
	args[VM_MEMORY_SZ+1] = &(arg_uint[VM_MEMORY_SZ+1]);
	
	jit_function_apply(function, args, &result);
	//printf("%d\n", result);
	
	return;
	
	/* end lib jit variables */
	
}