11 create_code (gcc_jit_context
*ctxt
, void *user_data
)
13 /* Let's try to inject the equivalent of:
15 popcount (unsigned int x)
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
);
31 gcc_jit_param
*param_x
=
32 gcc_jit_context_new_param (
36 gcc_jit_param
*params
[1] = {param_x
};
37 gcc_jit_function
*func
=
38 gcc_jit_context_new_function (ctxt
,
40 GCC_JIT_FUNCTION_EXPORTED
,
45 gcc_jit_lvalue
*x
= gcc_jit_param_as_lvalue (param_x
);
46 gcc_jit_rvalue
*x_rvalue
= gcc_jit_lvalue_as_rvalue (x
);
48 gcc_jit_function_new_local (func
, NULL
, int_type
, "i");
49 gcc_jit_rvalue
*zero
= gcc_jit_context_zero (ctxt
, int_type
);
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");
56 gcc_jit_block_add_assignment (initial
, NULL
, i
, zero
);
57 gcc_jit_block_end_with_jump (initial
, NULL
, while_block
);
59 gcc_jit_block
*after
=
60 gcc_jit_function_new_block (func
, "after");
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
);
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
);
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
);
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
);
77 gcc_jit_block_end_with_return(after
, NULL
, gcc_jit_lvalue_as_rvalue (i
));
81 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
83 CHECK_NON_NULL (result
);