| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
#include "libgccjit.h" |
| 5 |
|
| 6 |
#include "harness.h" |
| 7 |
|
| 8 |
/* Build various compound expressions, and verify that they have sane debug |
| 9 |
strings. */ |
| 10 |
void |
| 11 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 12 |
{ |
| 13 |
/* Make a singly-linked list type: |
| 14 |
struct node |
| 15 |
{ |
| 16 |
struct node *next; |
| 17 |
int value; |
| 18 |
}; |
| 19 |
*/ |
| 20 |
gcc_jit_type *t_int = |
| 21 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 22 |
gcc_jit_struct *t_node = |
| 23 |
gcc_jit_context_new_opaque_struct (ctxt, NULL, "node"); |
| 24 |
gcc_jit_type *t_node_ptr = |
| 25 |
gcc_jit_type_get_pointer (gcc_jit_struct_as_type (t_node)); |
| 26 |
gcc_jit_field *f_next = |
| 27 |
gcc_jit_context_new_field (ctxt, NULL, t_node_ptr, "next"); |
| 28 |
gcc_jit_field *f_value = |
| 29 |
gcc_jit_context_new_field (ctxt, NULL, t_int, "value"); |
| 30 |
gcc_jit_field *fields[] = {f_next, f_value}; |
| 31 |
gcc_jit_struct_set_fields (t_node, NULL, 2, fields); |
| 32 |
|
| 33 |
/* Create a dummy function so that we have locals/params to build |
| 34 |
expressions with. */ |
| 35 |
gcc_jit_type *t_void = |
| 36 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID); |
| 37 |
gcc_jit_function *fn = |
| 38 |
gcc_jit_context_new_function (ctxt, NULL, |
| 39 |
GCC_JIT_FUNCTION_EXPORTED, |
| 40 |
t_void, |
| 41 |
"test_debug_strings", |
| 42 |
0, NULL, 0); |
| 43 |
gcc_jit_rvalue *ptr = |
| 44 |
gcc_jit_lvalue_as_rvalue ( |
| 45 |
gcc_jit_function_new_local (fn, |
| 46 |
NULL, |
| 47 |
t_node_ptr, |
| 48 |
"ptr")); |
| 49 |
gcc_jit_rvalue *a = |
| 50 |
gcc_jit_lvalue_as_rvalue ( |
| 51 |
gcc_jit_function_new_local (fn, NULL, t_int, "a")); |
| 52 |
gcc_jit_rvalue *b = |
| 53 |
gcc_jit_lvalue_as_rvalue ( |
| 54 |
gcc_jit_function_new_local (fn, NULL, t_int, "b")); |
| 55 |
gcc_jit_rvalue *c = |
| 56 |
gcc_jit_lvalue_as_rvalue ( |
| 57 |
gcc_jit_function_new_local (fn, NULL, t_int, "c")); |
| 58 |
gcc_jit_rvalue *d = |
| 59 |
gcc_jit_lvalue_as_rvalue ( |
| 60 |
gcc_jit_function_new_local (fn, NULL, t_int, "d")); |
| 61 |
|
| 62 |
#define CHECK_RVALUE_DEBUG_STRING(RVALUE, EXPECTED) \ |
| 63 |
CHECK_STRING_VALUE ( \ |
| 64 |
gcc_jit_object_get_debug_string (gcc_jit_rvalue_as_object (RVALUE)), \ |
| 65 |
(EXPECTED)) |
| 66 |
|
| 67 |
#define CHECK_LVALUE_DEBUG_STRING(LVALUE, EXPECTED) \ |
| 68 |
CHECK_STRING_VALUE ( \ |
| 69 |
gcc_jit_object_get_debug_string (gcc_jit_lvalue_as_object (LVALUE)), \ |
| 70 |
(EXPECTED)) |
| 71 |
|
| 72 |
/* Verify various simple compound expressions. */ |
| 73 |
{ |
| 74 |
CHECK_RVALUE_DEBUG_STRING (ptr, "ptr"); |
| 75 |
|
| 76 |
gcc_jit_lvalue *deref = |
| 77 |
gcc_jit_rvalue_dereference_field (ptr, |
| 78 |
NULL, |
| 79 |
f_value); |
| 80 |
CHECK_LVALUE_DEBUG_STRING (deref, "ptr->value"); |
| 81 |
|
| 82 |
gcc_jit_rvalue *deref_as_rvalue = gcc_jit_lvalue_as_rvalue (deref); |
| 83 |
|
| 84 |
#define BINOP(OP, A, B) \ |
| 85 |
gcc_jit_context_new_binary_op (ctxt, NULL, \ |
| 86 |
GCC_JIT_BINARY_OP_##OP, t_int, (A), (B)) |
| 87 |
#define COMPARISON(OP, A, B) \ |
| 88 |
gcc_jit_context_new_comparison (ctxt, NULL, \ |
| 89 |
GCC_JIT_COMPARISON_##OP,(A), (B)) |
| 90 |
|
| 91 |
CHECK_RVALUE_DEBUG_STRING ( |
| 92 |
BINOP (PLUS, deref_as_rvalue, deref_as_rvalue), |
| 93 |
"ptr->value + ptr->value"); |
| 94 |
CHECK_RVALUE_DEBUG_STRING ( |
| 95 |
BINOP (MULT, deref_as_rvalue, deref_as_rvalue), |
| 96 |
"ptr->value * ptr->value"); |
| 97 |
|
| 98 |
/* Multiplication has higher precedence in C than addition, so this |
| 99 |
dump shouldn't contain parentheses. */ |
| 100 |
CHECK_RVALUE_DEBUG_STRING ( |
| 101 |
BINOP (PLUS, |
| 102 |
BINOP (MULT, a, b), |
| 103 |
BINOP (MULT, c, d)), |
| 104 |
"a * b + c * d"); |
| 105 |
|
| 106 |
/* ...but this one should. */ |
| 107 |
CHECK_RVALUE_DEBUG_STRING ( |
| 108 |
BINOP (MULT, |
| 109 |
BINOP (PLUS, a, b), |
| 110 |
BINOP (PLUS, c, d)), |
| 111 |
"(a + b) * (c + d)"); |
| 112 |
|
| 113 |
/* Equal precedences don't need parentheses. */ |
| 114 |
CHECK_RVALUE_DEBUG_STRING ( |
| 115 |
BINOP (MULT, |
| 116 |
BINOP (MULT, a, b), |
| 117 |
BINOP (MULT, c, d)), |
| 118 |
"a * b * c * d"); |
| 119 |
|
| 120 |
/* Comparisons and logical ops. */ |
| 121 |
CHECK_RVALUE_DEBUG_STRING ( |
| 122 |
COMPARISON (LT, a, b), |
| 123 |
"a < b"); |
| 124 |
|
| 125 |
CHECK_RVALUE_DEBUG_STRING ( |
| 126 |
BINOP (LOGICAL_AND, |
| 127 |
COMPARISON (LT, a, b), |
| 128 |
COMPARISON (GT, c, d)), |
| 129 |
"a < b && c > d"); |
| 130 |
|
| 131 |
CHECK_RVALUE_DEBUG_STRING ( |
| 132 |
BINOP (LOGICAL_AND, |
| 133 |
BINOP (LOGICAL_OR, |
| 134 |
COMPARISON (LT, a, b), |
| 135 |
COMPARISON (LT, a, c)), |
| 136 |
BINOP (LOGICAL_OR, |
| 137 |
COMPARISON (GT, d, b), |
| 138 |
COMPARISON (GT, d, c))), |
| 139 |
"(a < b || a < c) && (d > b || d > c)"); |
| 140 |
|
| 141 |
CHECK_RVALUE_DEBUG_STRING ( |
| 142 |
BINOP (LOGICAL_OR, |
| 143 |
BINOP (LOGICAL_AND, |
| 144 |
COMPARISON (LT, a, b), |
| 145 |
COMPARISON (LT, a, c)), |
| 146 |
BINOP (LOGICAL_AND, |
| 147 |
COMPARISON (GT, d, b), |
| 148 |
COMPARISON (GT, d, c))), |
| 149 |
"a < b && a < c || d > b && d > c"); |
| 150 |
|
| 151 |
#undef BINOP |
| 152 |
#undef COMPARISON |
| 153 |
} |
| 154 |
|
| 155 |
/* PR jit/66539 "Missing parentheses in jit dumps". |
| 156 |
Construct the equivalent of |
| 157 |
((cast)ptr->next)->next |
| 158 |
and verify that the appropriate parentheses appear in the debug |
| 159 |
string. */ |
| 160 |
{ |
| 161 |
/* "ptr->next". */ |
| 162 |
gcc_jit_lvalue *inner_deref = |
| 163 |
gcc_jit_rvalue_dereference_field (ptr, |
| 164 |
NULL, |
| 165 |
f_next); |
| 166 |
/* "((node *)ptr->next)"; the cast is redundant, purely |
| 167 |
to exercise dumping. */ |
| 168 |
gcc_jit_rvalue *test_cast = |
| 169 |
gcc_jit_context_new_cast (ctxt, NULL, |
| 170 |
gcc_jit_lvalue_as_rvalue (inner_deref), |
| 171 |
t_node_ptr); |
| 172 |
/* "((node *)ptr->next)->next". */ |
| 173 |
gcc_jit_lvalue *outer_deref = |
| 174 |
gcc_jit_rvalue_dereference_field (test_cast, /* gcc_jit_rvalue *ptr */ |
| 175 |
NULL, /* gcc_jit_location *loc */ |
| 176 |
f_next); /* gcc_jit_field *field */ |
| 177 |
CHECK_LVALUE_DEBUG_STRING (outer_deref, |
| 178 |
"((struct node *)ptr->next)->next"); |
| 179 |
} |
| 180 |
|
| 181 |
/* Check string literal escaping. */ |
| 182 |
{ |
| 183 |
CHECK_RVALUE_DEBUG_STRING |
| 184 |
(gcc_jit_context_new_string_literal (ctxt, ""), |
| 185 |
"\"\""); |
| 186 |
CHECK_RVALUE_DEBUG_STRING |
| 187 |
(gcc_jit_context_new_string_literal (ctxt, "foo"), |
| 188 |
"\"foo\""); |
| 189 |
CHECK_RVALUE_DEBUG_STRING |
| 190 |
(gcc_jit_context_new_string_literal (ctxt, "\""), |
| 191 |
"\"\\\"\""); |
| 192 |
CHECK_RVALUE_DEBUG_STRING |
| 193 |
(gcc_jit_context_new_string_literal (ctxt, "line 1\nline 2\n"), |
| 194 |
"\"line 1\\nline 2\\n\""); |
| 195 |
CHECK_RVALUE_DEBUG_STRING |
| 196 |
(gcc_jit_context_new_string_literal (ctxt, "foo\tbar"), |
| 197 |
"\"foo\\tbar\""); |
| 198 |
} |
| 199 |
|
| 200 |
#undef CHECK_RVALUE_DEBUG_STRING |
| 201 |
#undef CHECK_LVALUE_DEBUG_STRING |
| 202 |
} |
| 203 |
|
| 204 |
void |
| 205 |
verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) |
| 206 |
{ |
| 207 |
CHECK_NON_NULL (result); |
| 208 |
/* We don't actually build any functions above; |
| 209 |
nothing more to verify. */ |
| 210 |
} |