jit: add switch statements
[official-gcc.git] / gcc / testsuite / jit.dg / test-switch.cc
blob862f7a8f72b51d64cbb9756d861c58e1377fd417
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "libgccjit++.h"
7 #include "harness.h"
9 /* Quote from here in docs/cp/topics/functions.rst. */
11 void
12 create_code (gcc_jit_context *c_ctxt, void *user_data)
14 /* Let's try to inject the equivalent of:
15 int
16 test_switch (int x)
18 switch (x)
20 case 0 ... 5:
21 return 3;
23 case 25 ... 27:
24 return 4;
26 case -42 ... -17:
27 return 83;
29 case 40:
30 return 8;
32 default:
33 return 10;
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);
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),
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);
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. */
82 static int
83 c_test_switch (int x)
85 switch (x)
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;
100 void
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);
109 int i;
111 for (i = -255; i < 255; i++)
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);