111
|
1 #include <stdlib.h>
|
|
2 #include <stdio.h>
|
|
3 #include <string.h>
|
|
4
|
|
5 #include "libgccjit.h"
|
|
6
|
|
7 #include "harness.h"
|
|
8
|
|
9 void
|
|
10 create_code (gcc_jit_context *ctxt, void *user_data)
|
|
11 {
|
|
12 /* Let's try to inject the equivalent of:
|
|
13 int
|
|
14 test_switch (int x)
|
|
15 {
|
|
16 switch (x)
|
|
17 {
|
|
18 case 0 ... 5:
|
|
19 return 3;
|
|
20
|
|
21 case 5 ... 10:
|
|
22 return 4;
|
|
23
|
|
24 default:
|
|
25 return 10;
|
|
26 }
|
|
27 }
|
|
28 and verify that we get an error about the overlapping
|
|
29 ranges.
|
|
30 */
|
|
31 gcc_jit_type *t_int =
|
|
32 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
|
|
33 gcc_jit_type *return_type = t_int;
|
|
34 gcc_jit_param *x =
|
|
35 gcc_jit_context_new_param (ctxt, NULL, t_int, "x");
|
|
36 gcc_jit_param *params[1] = {x};
|
|
37 gcc_jit_function *func =
|
|
38 gcc_jit_context_new_function (ctxt, NULL,
|
|
39 GCC_JIT_FUNCTION_EXPORTED,
|
|
40 return_type,
|
|
41 "test_switch",
|
|
42 1, params, 0);
|
|
43
|
|
44 gcc_jit_block *b_initial =
|
|
45 gcc_jit_function_new_block (func, "initial");
|
|
46
|
|
47 gcc_jit_block *b_default =
|
|
48 gcc_jit_function_new_block (func, "default");
|
|
49 gcc_jit_block *b_case_0_5 =
|
|
50 gcc_jit_function_new_block (func, "case_0_5");
|
|
51 gcc_jit_block *b_case_5_10 =
|
|
52 gcc_jit_function_new_block (func, "case_5_10");
|
|
53
|
|
54 gcc_jit_case *cases[2] = {
|
|
55 gcc_jit_context_new_case (
|
|
56 ctxt,
|
|
57 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 0),
|
|
58 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5),
|
|
59 b_case_0_5),
|
|
60 gcc_jit_context_new_case (
|
|
61 ctxt,
|
|
62 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 5),
|
|
63 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10),
|
|
64 b_case_5_10)
|
|
65 };
|
|
66
|
|
67 gcc_jit_block_end_with_switch (
|
|
68 b_initial, NULL,
|
|
69 gcc_jit_param_as_rvalue (x),
|
|
70 b_default,
|
|
71 2,
|
|
72 cases);
|
|
73
|
|
74 gcc_jit_block_end_with_return (
|
|
75 b_case_0_5, NULL,
|
|
76 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 3));
|
|
77 gcc_jit_block_end_with_return (
|
|
78 b_case_5_10, NULL,
|
|
79 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 4));
|
|
80 gcc_jit_block_end_with_return (
|
|
81 b_default, NULL,
|
|
82 gcc_jit_context_new_rvalue_from_int (ctxt, t_int, 10));
|
|
83 }
|
|
84
|
|
85 void
|
|
86 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
|
|
87 {
|
|
88 CHECK_VALUE (result, NULL);
|
|
89
|
|
90 CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
|
|
91 "gcc_jit_block_end_with_switch:"
|
|
92 " duplicate (or overlapping) cases values:"
|
|
93 " case 1: case (int)5 ... (int)10: goto case_5_10;"
|
|
94 " overlaps case (int)0 ... (int)5: goto case_0_5;");
|
|
95 }
|