static jit_value_t pj_jit_internal_op(jit_function_t function, jit_value_t *var_values, int nvars, pj_op_t *op) { jit_value_t tmp1, tmp2, rv; tmp1 = pj_jit_internal(function, var_values, nvars, op->op1); if (op->op2 != NULL) tmp2 = pj_jit_internal(function, var_values, nvars, op->op2); switch (op->optype) { case pj_unop_negate: rv = jit_insn_neg(function, tmp1); break; case pj_unop_sin: rv = jit_insn_sin(function, tmp1); break; case pj_unop_cos: rv = jit_insn_cos(function, tmp1); break; case pj_unop_abs: rv = jit_insn_abs(function, tmp1); break; case pj_unop_sqrt: rv = jit_insn_sqrt(function, tmp1); break; case pj_unop_log: rv = jit_insn_log(function, tmp1); break; case pj_unop_exp: rv = jit_insn_exp(function, tmp1); break; case pj_unop_not: rv = jit_insn_not(function, tmp1); break; case pj_unop_not_bool: rv = jit_insn_to_not_bool(function, tmp1); break; case pj_binop_add: rv = jit_insn_add(function, tmp1, tmp2); break; case pj_binop_subtract: rv = jit_insn_sub(function, tmp1, tmp2); break; case pj_binop_multiply: rv = jit_insn_mul(function, tmp1, tmp2); break; case pj_binop_divide: rv = jit_insn_div(function, tmp1, tmp2); break; case pj_binop_modulo: rv = jit_insn_rem(function, tmp1, tmp2); /* FIXME should this use jit_insn_rem_ieee? */ break; case pj_binop_atan2: rv = jit_insn_atan2(function, tmp1, tmp2); break; case pj_binop_left_shift: rv = jit_insn_shl(function, tmp1, tmp2); break; case pj_binop_right_shift: rv = jit_insn_shr(function, tmp1, tmp2); break; case pj_binop_and: rv = jit_insn_and(function, tmp1, tmp2); break; case pj_binop_or: rv = jit_insn_or(function, tmp1, tmp2); break; case pj_binop_xor: rv = jit_insn_xor(function, tmp1, tmp2); break; case pj_binop_eq: rv = jit_insn_eq(function, tmp1, tmp2); break; case pj_binop_ne: rv = jit_insn_ne(function, tmp1, tmp2); break; case pj_binop_lt: rv = jit_insn_lt(function, tmp1, tmp2); break; case pj_binop_le: rv = jit_insn_le(function, tmp1, tmp2); break; case pj_binop_gt: rv = jit_insn_gt(function, tmp1, tmp2); break; case pj_binop_ge: rv = jit_insn_ge(function, tmp1, tmp2); break; default: abort(); } return rv; }
/* * Output a comparision between the 2 top most values on the evaluation stack. * The result value is returned. */ static ILJitValue OutputCompare(ILJITCoder *coder, int opcode, ILJitValue *value1, ILJitValue *value2) { switch(opcode) { case IL_OP_PREFIX + IL_PREFIX_OP_CEQ: case IL_OP_BEQ: { /* Test two values for equality */ AdjustMixedBinary(coder, 0, value1, value2); return jit_insn_eq(coder->jitFunction, *value1, *value2); } break; case IL_OP_BNE_UN: { /* Test two unsigned values for inequality */ AdjustMixedBinary(coder, 1, value1, value2); return jit_insn_ne(coder->jitFunction, *value1, *value2); } break; case IL_OP_PREFIX + IL_PREFIX_OP_CGT: case IL_OP_BGT: { /* Test two signed values for greater than */ AdjustMixedBinary(coder, 0, value1, value2); return jit_insn_gt(coder->jitFunction, *value1, *value2); } break; case IL_OP_PREFIX + IL_PREFIX_OP_CGT_UN: case IL_OP_BGT_UN: { /* Test two unsigned values for greater than */ AdjustMixedBinary(coder, 1, value1, value2); return jit_insn_gt(coder->jitFunction, *value1, *value2); } break; case IL_OP_BGE: { /* Test two signed values for greater than or equal */ AdjustMixedBinary(coder, 0, value1, value2); return jit_insn_ge(coder->jitFunction, *value1, *value2); } break; case IL_OP_BGE_UN: { /* Test two unsigned values for greater than or equal */ AdjustMixedBinary(coder, 1, value1, value2); return jit_insn_ge(coder->jitFunction, *value1, *value2); } break; case IL_OP_PREFIX + IL_PREFIX_OP_CLT: case IL_OP_BLT: { /* Test two signed values for less than */ AdjustMixedBinary(coder, 0, value1, value2); return jit_insn_lt(coder->jitFunction, *value1, *value2); } break; case IL_OP_PREFIX + IL_PREFIX_OP_CLT_UN: case IL_OP_BLT_UN: { /* Test two unsigned values for less than */ AdjustMixedBinary(coder, 1, value1, value2); return jit_insn_lt(coder->jitFunction, *value1, *value2); } break; case IL_OP_BLE: { /* Test two signed values for less than or equal */ AdjustMixedBinary(coder, 0, value1, value2); return jit_insn_le(coder->jitFunction, *value1, *value2); } break; case IL_OP_BLE_UN: { /* Test two unsigned values for less than or equal */ AdjustMixedBinary(coder, 1, value1, value2); return jit_insn_le(coder->jitFunction, *value1, *value2); } break; } return 0; }
static jit_value_t parse_recursive(jit_function_t func) { jit_value_t arg1, arg2, result; jit_function_t func1; double val; char *t = token(); // Somebody, do something with this! // It's awful monkeycoding, but I'm too lazy to rewrite it :3 if (STREQ(t, "F")) result = val_freq; else if (STREQ(t, "X")) result = jit_insn_convert(func, val_sample, jit_type_float64, 0); else if (STREQ(t, "LEN")) result = jit_insn_convert(func, val_length, jit_type_float64, 0); else if (STREQ(t, "RATE")) result = const_rate; else if (STREQ(t, "PI")) result = const_pi; else if (STREQ(t, "+")) result = jit_insn_add(func, parse_recursive(func), parse_recursive(func)); else if (STREQ(t, "-")) { arg1 = parse_recursive(func); arg2 = parse_recursive(func); result = jit_insn_sub(func, arg1, arg2); } else if (STREQ(t, "*")) result = jit_insn_mul(func, parse_recursive(func), parse_recursive(func)); else if (STREQ(t, "/")) { arg1 = parse_recursive(func); arg2 = parse_recursive(func); result = jit_insn_div(func, arg1, arg2); } else if (STREQ(t, "%")) { arg1 = parse_recursive(func); arg2 = parse_recursive(func); result = jit_insn_rem(func, arg1, arg2); } else if (STREQ(t, ">")) { arg1 = parse_recursive(func); arg2 = parse_recursive(func); result = jit_insn_gt(func, arg1, arg2); } else if (STREQ(t, "<")) { arg1 = parse_recursive(func); arg2 = parse_recursive(func); result = jit_insn_lt(func, arg1, arg2); } else if (STREQ(t, "if")) { jit_value_t tmpval = jit_value_create(func, jit_type_float64); jit_label_t lb_false = jit_label_undefined, lb_end = jit_label_undefined; jit_insn_branch_if_not(func, jit_insn_to_bool(func, parse_recursive(func)), &lb_false); jit_insn_store(func, tmpval, parse_recursive(func)); jit_insn_branch(func, &lb_end); jit_insn_label(func, &lb_false); jit_insn_store(func, tmpval, parse_recursive(func)); jit_insn_label(func, &lb_end); result = jit_insn_load(func, tmpval); } else if (STREQ(t, "sin")) result = jit_insn_sin(func, parse_recursive(func)); else if (sscanf(t, "%lf", &val) == 1) result = jit_value_create_float64_constant(func, jit_type_float64, val); else if ((func1 = get_function_by_name(t)) != NULL) { arg1 = parse_recursive(func); arg2 = parse_recursive(func); jit_value_t args[3] = {arg1, arg2, val_length}; result = jit_insn_call( func, t, func1, NULL, args, 3, 0); } else { LOGF("Unexpected token '%s'", t); result = NULL; } free(t); return result; }
jit_value jit_function::insn_gt (const jit_value& value1, const jit_value& value2) { value_wrap(jit_insn_gt(func, value1.raw(), value2.raw())); }
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 *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; }
// 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 */ }