/*
Create the function object */
function
= jit_function_create(context, signature);
output
= fopen("output.txt", "w");
/*
Construct the function body */
x
= jit_value_get_param(function, 0);
y
= jit_value_get_param(function, 1);
z
= jit_value_get_param(function, 2);
i
= jit_value_create(function, jit_type_int);
j
= jit_value_create(function, jit_type_int);
zero
= jit_value_create_nint_constant(function, jit_type_int, 0);
one
= jit_value_create_nint_constant(function, jit_type_int, 1);
x1
= jit_insn_add(function, x, y); //
x1 = index into output part of data
i1
= jit_insn_dup(function, z);
// init value for outer loop counter
jit_insn_label(function,
&outer);
j
= jit_insn_dup(function, y);
// reset inner loop counter
jit_insn_label(function,
&inner);
j1
= jit_insn_dup(function, j);
//
decrement inner loop counter - but doesn't generate what I expected it
to :(
j
= jit_insn_sub(function, j1, one);
temp2
= jit_insn_load_elem(function, x, j, jit_type_int); // get the input element
temp1
= jit_insn_load_elem(function, x1, j, jit_type_int); // and the output
element
temp1
= jit_insn_add(function, temp1, temp2); // add them together
jit_insn_store_elem(function,
x1, j, temp1); // and store in output
jit_insn_branch_if(function,
j, &inner); // if j != 0 continue inner loop
i1
= jit_insn_dup(function, i);
//
decrement i (still no luck)
i
= jit_insn_sub(function, i1, one);
jit_insn_branch_if(function,
i, &outer); // if i + 0 continue outer loop
jit_insn_return(function,
zero);
jit_dump_function(output,
function, "before_compile");
/*
Compile the function */
jit_function_compile(function);
/*
Unlock the context */
jit_context_build_end(context);
jit_dump_function(output,
function, "after_compile");
/*
Execute the function and print the result */
fclose(output);
arg1
= 3;
arg2
= 10;
arg3
= 1000;
args[0]
= &data;
args[1]
= &arg2;
args[2]
= &arg3;
jit_function_apply(function,
args, &result);
/*
Clean up */
jit_context_destroy(context);
crashes inside jit_function_apply
and the two dumps generate following output: