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; } }
static inline void php_jit_struct_free(zend_object *object) { php_jit_struct_t *pstruct = (php_jit_struct_t *) PHP_JIT_FETCH_STRUCT_O(object); zend_ulong nfield = 0; while (nfield < pstruct->nfields) { zval_ptr_dtor(&pstruct->zfields[nfield]); nfield++; } jit_type_free(pstruct->type); if (pstruct->zfields) { efree(pstruct->zfields); } if (pstruct->names) { zend_ulong nfield = 0; while (nfield < pstruct->nfields) { zend_string_release (pstruct->names[nfield]); nfield++; } efree(pstruct->names); } zend_object_std_dtor(&pstruct->std); }
void _jit_function_destroy(jit_function_t func) { if(!func) { return; } if(func->next) { func->next->prev = func->prev; } else { func->context->last_function = func->prev; } if(func->prev) { func->prev->next = func->next; } else { func->context->functions = func->next; } _jit_function_free_builder(func); jit_meta_destroy(&(func->meta)); jit_type_free(func->signature); jit_free(func); }
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; }
static inline void php_jit_type_free(zend_object *zobject) { php_jit_type_t *ptype = (php_jit_type_t *) PHP_JIT_FETCH_TYPE_O(zobject); if (ptype->copied) { jit_type_free(ptype->type); } zend_object_std_dtor(zobject); }
void _jit_value_free(void *_value) { jit_value_t value = (jit_value_t) _value; jit_type_free(value->type); if(value->free_address && value->address) { /* We need to free the memory for a large constant */ jit_free((void *) value->address); } }
/*@ * @deftypemethod jit_function void create () * Create this function if it doesn't already exist. This version will * call the virtual @code{create_signature()} method to obtain the * signature from the subclass. * @end deftypemethod @*/ void jit_function::create() { if(!func) { jit_type_t signature = create_signature(); create(signature); if(!func) { jit_type_free(signature); } } }
static void type_dealloc(PyJitType *self) { if (self->weakreflist) PyObject_ClearWeakRefs((PyObject *)self); if (self->type) { PYJIT_BEGIN_ALLOW_EXCEPTION if (pyjit_weak_cache_delitem(type_cache, (long)self->type) < 0) { PYJIT_TRACE("this shouldn't have happened"); abort(); } PYJIT_END_ALLOW_EXCEPTION jit_type_free(self->type); }
void _jit_function_destroy(jit_function_t func) { jit_context_t context; if(!func) { return; } context = func->context; if(func->next) { func->next->prev = func->prev; } else { context->last_function = func->prev; } if(func->prev) { func->prev->next = func->next; } else { context->functions = func->next; } _jit_function_free_builder(func); _jit_varint_free_data(func->bytecode_offset); jit_meta_destroy(&func->meta); jit_type_free(func->signature); _jit_memory_lock(context); #if !defined(JIT_BACKEND_INTERP) && (defined(jit_redirector_size) || defined(jit_indirector_size)) # if defined(jit_redirector_size) _jit_memory_free_trampoline(context, func->redirector); # else _jit_memory_free_trampoline(context, func->indirector); # endif #endif _jit_memory_free_function(context, func); _jit_memory_unlock(context); }
/*@ * @deftypefun jit_value_t jit_value_get_struct_pointer (jit_function_t @var{func}) * Get the value that contains the structure return pointer for * a function. If the function does not have a structure return pointer * (i.e. structures are returned in registers), then this returns NULL. * @end deftypefun @*/ jit_value_t jit_value_get_struct_pointer(jit_function_t func) { jit_type_t type; jit_value_t value; /* Ensure that we have a builder for this function */ if(!_jit_function_ensure_builder(func)) { return 0; } type = jit_type_remove_tags(jit_type_get_return(func->signature)); if(jit_type_is_struct(type) || jit_type_is_union(type)) { if(jit_type_return_via_pointer(type)) { if(!func->builder->struct_return) { type = jit_type_create_pointer(type, 1); if(!type) { return 0; } value = jit_value_create(func, type); func->builder->struct_return = value; if(value) { /* The value belongs to the entry block, no matter where it happens to be created */ value->block = func->builder->entry_block; value->is_parameter = 1; } jit_type_free(type); } return func->builder->struct_return; } } return 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 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); jit_type_free(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); jit_insn_call(function, "gcd", function, 0, temp_args, 2, JIT_CALL_TAIL); /* 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; jit_insn_call(function, "gcd", function, 0, temp_args, 2, JIT_CALL_TAIL); /* 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; }
// 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 */ }