5 #include "libgccjit++.h"
9 /* Quote from here in docs/cp/topics/functions.rst. */
12 create_code (gcc_jit_context
*c_ctxt
, void *user_data
)
14 /* Let's try to inject the equivalent of:
37 gccjit::context
ctxt (c_ctxt
);
38 gccjit::type t_int
= ctxt
.get_type (GCC_JIT_TYPE_INT
);
39 gccjit::type return_type
= t_int
;
40 gccjit::param x
= ctxt
.new_param (t_int
, "x");
41 std::vector
<gccjit::param
> params
;
43 gccjit::function func
= ctxt
.new_function (GCC_JIT_FUNCTION_EXPORTED
,
48 gccjit::block b_initial
= func
.new_block ("initial");
50 gccjit::block b_default
= func
.new_block ("default");
51 gccjit::block b_case_0_5
= func
.new_block ("case_0_5");
52 gccjit::block b_case_25_27
= func
.new_block ("case_25_27");
53 gccjit::block b_case_m42_m17
= func
.new_block ("case_m42_m17");
54 gccjit::block b_case_40
= func
.new_block ("case_40");
56 std::vector
<gccjit::case_
> cases
;
57 cases
.push_back (ctxt
.new_case (ctxt
.new_rvalue (t_int
, 0),
58 ctxt
.new_rvalue (t_int
, 5),
60 cases
.push_back (ctxt
.new_case (ctxt
.new_rvalue (t_int
, 25),
61 ctxt
.new_rvalue (t_int
, 27),
63 cases
.push_back (ctxt
.new_case (ctxt
.new_rvalue (t_int
, -42),
64 ctxt
.new_rvalue (t_int
, -17),
66 cases
.push_back (ctxt
.new_case (ctxt
.new_rvalue (t_int
, 40),
67 ctxt
.new_rvalue (t_int
, 40),
69 b_initial
.end_with_switch (x
,
73 b_case_0_5
.end_with_return (ctxt
.new_rvalue (t_int
, 3));
74 b_case_25_27
.end_with_return (ctxt
.new_rvalue (t_int
, 4));
75 b_case_m42_m17
.end_with_return (ctxt
.new_rvalue (t_int
, 83));
76 b_case_40
.end_with_return (ctxt
.new_rvalue (t_int
, 8));
77 b_default
.end_with_return (ctxt
.new_rvalue (t_int
, 10));
80 /* Quote up to here in docs/cp/topics/functions.rst. */
101 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
103 typedef int (*test_switch_type
) (int);
104 CHECK_NON_NULL (result
);
105 test_switch_type test_switch
=
106 (test_switch_type
)gcc_jit_result_get_code (result
, "test_switch");
107 CHECK_NON_NULL (test_switch
);
111 for (i
= -255; i
< 255; i
++)
113 int val
= test_switch (i
);
114 int exp
= c_test_switch (i
);
116 fail ("test_switch (%i) returned: %i; expected; %i", i
, val
, exp
);