| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "libgccjit++.h" |
| 6 |
|
| 7 |
#include "harness.h" |
| 8 |
|
| 9 |
/* Quote from here in docs/cp/topics/functions.rst. */ |
| 10 |
|
| 11 |
void |
| 12 |
create_code (gcc_jit_context *c_ctxt, void *user_data) |
| 13 |
{ |
| 14 |
/* Let's try to inject the equivalent of: |
| 15 |
int |
| 16 |
test_switch (int x) |
| 17 |
{ |
| 18 |
switch (x) |
| 19 |
{ |
| 20 |
case 0 ... 5: |
| 21 |
return 3; |
| 22 |
|
| 23 |
case 25 ... 27: |
| 24 |
return 4; |
| 25 |
|
| 26 |
case -42 ... -17: |
| 27 |
return 83; |
| 28 |
|
| 29 |
case 40: |
| 30 |
return 8; |
| 31 |
|
| 32 |
default: |
| 33 |
return 10; |
| 34 |
} |
| 35 |
} |
| 36 |
*/ |
| 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; |
| 42 |
params.push_back (x); |
| 43 |
gccjit::function func = ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED, |
| 44 |
return_type, |
| 45 |
"test_switch", |
| 46 |
params, 0); |
| 47 |
|
| 48 |
gccjit::block b_initial = func.new_block ("initial"); |
| 49 |
|
| 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"); |
| 55 |
|
| 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), |
| 59 |
b_case_0_5)); |
| 60 |
cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 25), |
| 61 |
ctxt.new_rvalue (t_int, 27), |
| 62 |
b_case_25_27)); |
| 63 |
cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, -42), |
| 64 |
ctxt.new_rvalue (t_int, -17), |
| 65 |
b_case_m42_m17)); |
| 66 |
cases.push_back (ctxt.new_case (ctxt.new_rvalue (t_int, 40), |
| 67 |
ctxt.new_rvalue (t_int, 40), |
| 68 |
b_case_40)); |
| 69 |
b_initial.end_with_switch (x, |
| 70 |
b_default, |
| 71 |
cases); |
| 72 |
|
| 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)); |
| 78 |
} |
| 79 |
|
| 80 |
/* Quote up to here in docs/cp/topics/functions.rst. */ |
| 81 |
|
| 82 |
static int |
| 83 |
c_test_switch (int x) |
| 84 |
{ |
| 85 |
switch (x) |
| 86 |
{ |
| 87 |
case 0 ... 5: |
| 88 |
return 3; |
| 89 |
case 25 ... 27: |
| 90 |
return 4; |
| 91 |
case -42 ... -17: |
| 92 |
return 83; |
| 93 |
case 40: |
| 94 |
return 8; |
| 95 |
default: |
| 96 |
return 10; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 102 |
{ |
| 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); |
| 108 |
|
| 109 |
int i; |
| 110 |
|
| 111 |
for (i = -255; i < 255; i++) |
| 112 |
{ |
| 113 |
int val = test_switch (i); |
| 114 |
int exp = c_test_switch (i); |
| 115 |
if (val != exp) |
| 116 |
fail ("test_switch (%i) returned: %i; expected; %i", i, val, exp); |
| 117 |
} |
| 118 |
} |