| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
#include "libgccjit.h" |
| 7 |
|
| 8 |
#include "harness.h" |
| 9 |
|
| 10 |
void |
| 11 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 12 |
{ |
| 13 |
/* Let's try to inject the equivalent of: |
| 14 |
int |
| 15 |
popcount (unsigned int x) |
| 16 |
{ |
| 17 |
int i = 0; |
| 18 |
while (x) |
| 19 |
{ |
| 20 |
x &= x - 1; |
| 21 |
++i; |
| 22 |
} |
| 23 |
return i; |
| 24 |
} |
| 25 |
*/ |
| 26 |
gcc_jit_type *int_type = |
| 27 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 28 |
gcc_jit_type *uint_type = |
| 29 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UNSIGNED_INT); |
| 30 |
|
| 31 |
gcc_jit_param *param_x = |
| 32 |
gcc_jit_context_new_param ( |
| 33 |
ctxt, |
| 34 |
NULL, |
| 35 |
uint_type, "x"); |
| 36 |
gcc_jit_param *params[1] = {param_x}; |
| 37 |
gcc_jit_function *func = |
| 38 |
gcc_jit_context_new_function (ctxt, |
| 39 |
NULL, |
| 40 |
GCC_JIT_FUNCTION_EXPORTED, |
| 41 |
int_type, |
| 42 |
"popcount", |
| 43 |
1, params, 0); |
| 44 |
|
| 45 |
gcc_jit_lvalue *x = gcc_jit_param_as_lvalue (param_x); |
| 46 |
gcc_jit_rvalue *x_rvalue = gcc_jit_lvalue_as_rvalue (x); |
| 47 |
gcc_jit_lvalue *i = |
| 48 |
gcc_jit_function_new_local (func, NULL, int_type, "i"); |
| 49 |
gcc_jit_rvalue *zero = gcc_jit_context_zero (ctxt, int_type); |
| 50 |
|
| 51 |
gcc_jit_block *initial = |
| 52 |
gcc_jit_function_new_block (func, "initial"); |
| 53 |
gcc_jit_block *while_block = |
| 54 |
gcc_jit_function_new_block (func, "while"); |
| 55 |
|
| 56 |
gcc_jit_block_add_assignment (initial, NULL, i, zero); |
| 57 |
gcc_jit_block_end_with_jump (initial, NULL, while_block); |
| 58 |
|
| 59 |
gcc_jit_block *after = |
| 60 |
gcc_jit_function_new_block (func, "after"); |
| 61 |
|
| 62 |
gcc_jit_block *while_body = |
| 63 |
gcc_jit_function_new_block (func, "while_body"); |
| 64 |
gcc_jit_rvalue *uzero = gcc_jit_context_zero (ctxt, uint_type); |
| 65 |
gcc_jit_rvalue *cmp = |
| 66 |
gcc_jit_context_new_comparison (ctxt, NULL, GCC_JIT_COMPARISON_NE, x_rvalue, uzero); |
| 67 |
gcc_jit_block_end_with_conditional (while_block, NULL, cmp, while_body, after); |
| 68 |
|
| 69 |
gcc_jit_rvalue *uone = gcc_jit_context_one (ctxt, uint_type); |
| 70 |
gcc_jit_rvalue *sub = gcc_jit_context_new_binary_op (ctxt, NULL, GCC_JIT_BINARY_OP_MINUS, uint_type, x_rvalue, uone); |
| 71 |
gcc_jit_block_add_assignment_op (while_body, NULL, x, GCC_JIT_BINARY_OP_BITWISE_AND, sub); |
| 72 |
|
| 73 |
gcc_jit_rvalue *one = gcc_jit_context_one (ctxt, int_type); |
| 74 |
gcc_jit_block_add_assignment_op (while_body, NULL, i, GCC_JIT_BINARY_OP_PLUS, one); |
| 75 |
gcc_jit_block_end_with_jump (while_body, NULL, while_block); |
| 76 |
|
| 77 |
gcc_jit_block_end_with_return(after, NULL, gcc_jit_lvalue_as_rvalue (i)); |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 82 |
{ |
| 83 |
CHECK_NON_NULL (result); |
| 84 |
} |