]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/opencl-lang.c
libsframe: fix error code in sframe_decode
[thirdparty/binutils-gdb.git] / gdb / opencl-lang.c
CommitLineData
f4b8a18d 1/* OpenCL language support for GDB, the GNU debugger.
d01e8234 2 Copyright (C) 2010-2025 Free Software Foundation, Inc.
f4b8a18d
KW
3
4 Contributed by Ken Werner <ken.werner@de.ibm.com>.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
f4b8a18d
KW
21#include "gdbtypes.h"
22#include "symtab.h"
23#include "expression.h"
24#include "parser-defs.h"
f4b8a18d 25#include "language.h"
a53b64ea 26#include "varobj.h"
f4b8a18d 27#include "c-lang.h"
0d12e84c 28#include "gdbarch.h"
e9677704 29#include "c-exp.h"
f4b8a18d 30
f4b8a18d
KW
31/* Returns the corresponding OpenCL vector type from the given type code,
32 the length of the element type, the unsigned flag and the amount of
33 elements (N). */
34
35static struct type *
36lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
37 unsigned int el_length, unsigned int flag_unsigned,
38 int n)
39{
f4b8a18d 40 unsigned int length;
f4b8a18d
KW
41
42 /* Check if n describes a valid OpenCL vector size (2, 3, 4, 8, 16). */
43 if (n != 2 && n != 3 && n != 4 && n != 8 && n != 16)
44 error (_("Invalid OpenCL vector size: %d"), n);
45
46 /* Triple vectors have the size of a quad vector. */
47 length = (n == 3) ? el_length * 4 : el_length * n;
48
cbbcd7a7 49 auto filter = [&] (struct type *type)
7bea47f0
AB
50 {
51 LONGEST lowb, highb;
52
53 return (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
54 && get_array_bounds (type, &lowb, &highb)
27710edb
SM
55 && type->target_type ()->code () == code
56 && type->target_type ()->is_unsigned () == flag_unsigned
df86565b
SM
57 && type->target_type ()->length () == el_length
58 && type->length () == length
7bea47f0
AB
59 && highb - lowb + 1 == n);
60 };
61 const struct language_defn *lang = language_def (language_opencl);
62 return language_lookup_primitive_type (lang, gdbarch, filter);
f4b8a18d
KW
63}
64
65/* Returns nonzero if the array ARR contains duplicates within
66 the first N elements. */
67
68static int
69array_has_dups (int *arr, int n)
70{
71 int i, j;
72
73 for (i = 0; i < n; i++)
74 {
75 for (j = i + 1; j < n; j++)
dda83cd7
SM
76 {
77 if (arr[i] == arr[j])
78 return 1;
79 }
f4b8a18d
KW
80 }
81
82 return 0;
83}
84
85/* The OpenCL component access syntax allows to create lvalues referring to
86 selected elements of an original OpenCL vector in arbitrary order. This
87 structure holds the information to describe such lvalues. */
88
89struct lval_closure
90{
91 /* Reference count. */
92 int refc;
93 /* The number of indices. */
94 int n;
95 /* The element indices themselves. */
96 int *indices;
97 /* A pointer to the original value. */
98 struct value *val;
99};
100
101/* Allocates an instance of struct lval_closure. */
102
103static struct lval_closure *
104allocate_lval_closure (int *indices, int n, struct value *val)
105{
41bf6aca 106 struct lval_closure *c = XCNEW (struct lval_closure);
f4b8a18d
KW
107
108 c->refc = 1;
109 c->n = n;
fc270c35 110 c->indices = XCNEWVEC (int, n);
f4b8a18d 111 memcpy (c->indices, indices, n * sizeof (int));
cdf3de17 112 val->incref (); /* Increment the reference counter of the value. */
f4b8a18d
KW
113 c->val = val;
114
115 return c;
116}
117
118static void
119lval_func_read (struct value *v)
120{
b9f74d54 121 struct lval_closure *c = (struct lval_closure *) v->computed_closure ();
d0c97917
TT
122 struct type *type = check_typedef (v->type ());
123 struct type *eltype = check_typedef (c->val->type ())->target_type ();
76675c4d 124 LONGEST offset = v->offset ();
df86565b 125 LONGEST elsize = eltype->length ();
f4b8a18d
KW
126 int n, i, j = 0;
127 LONGEST lowb = 0;
128 LONGEST highb = 0;
129
78134374 130 if (type->code () == TYPE_CODE_ARRAY
f4b8a18d
KW
131 && !get_array_bounds (type, &lowb, &highb))
132 error (_("Could not determine the vector bounds"));
133
134 /* Assume elsize aligned offset. */
135 gdb_assert (offset % elsize == 0);
136 offset /= elsize;
137 n = offset + highb - lowb + 1;
138 gdb_assert (n <= c->n);
139
140 for (i = offset; i < n; i++)
bbe912ba 141 memcpy (v->contents_raw ().data () + j++ * elsize,
efaf1ae0 142 c->val->contents ().data () + c->indices[i] * elsize,
f4b8a18d
KW
143 elsize);
144}
145
146static void
147lval_func_write (struct value *v, struct value *fromval)
148{
65558ca5
TT
149 scoped_value_mark mark;
150
b9f74d54 151 struct lval_closure *c = (struct lval_closure *) v->computed_closure ();
d0c97917
TT
152 struct type *type = check_typedef (v->type ());
153 struct type *eltype = check_typedef (c->val->type ())->target_type ();
76675c4d 154 LONGEST offset = v->offset ();
df86565b 155 LONGEST elsize = eltype->length ();
f4b8a18d
KW
156 int n, i, j = 0;
157 LONGEST lowb = 0;
158 LONGEST highb = 0;
159
78134374 160 if (type->code () == TYPE_CODE_ARRAY
f4b8a18d
KW
161 && !get_array_bounds (type, &lowb, &highb))
162 error (_("Could not determine the vector bounds"));
163
164 /* Assume elsize aligned offset. */
165 gdb_assert (offset % elsize == 0);
166 offset /= elsize;
167 n = offset + highb - lowb + 1;
168
169 /* Since accesses to the fourth component of a triple vector is undefined we
170 just skip writes to the fourth element. Imagine something like this:
171 int3 i3 = (int3)(0, 1, 2);
172 i3.hi.hi = 5;
173 In this case n would be 4 (offset=12/4 + 1) while c->n would be 3. */
174 if (n > c->n)
175 n = c->n;
176
177 for (i = offset; i < n; i++)
178 {
317c3ed9 179 struct value *from_elm_val = value::allocate (eltype);
f4b8a18d
KW
180 struct value *to_elm_val = value_subscript (c->val, c->indices[i]);
181
bbe912ba 182 memcpy (from_elm_val->contents_writeable ().data (),
efaf1ae0 183 fromval->contents ().data () + j++ * elsize,
f4b8a18d
KW
184 elsize);
185 value_assign (to_elm_val, from_elm_val);
186 }
f4b8a18d
KW
187}
188
19124154 189/* Return true if bits in V from OFFSET and LENGTH represent a
8cf6f0b1
TT
190 synthetic pointer. */
191
19124154 192static bool
8cf6f0b1 193lval_func_check_synthetic_pointer (const struct value *v,
6b850546 194 LONGEST offset, int length)
8cf6f0b1 195{
b9f74d54 196 struct lval_closure *c = (struct lval_closure *) v->computed_closure ();
8cf6f0b1
TT
197 /* Size of the target type in bits. */
198 int elsize =
d0c97917 199 check_typedef (c->val->type ())->target_type ()->length () * 8;
8cf6f0b1
TT
200 int startrest = offset % elsize;
201 int start = offset / elsize;
202 int endrest = (offset + length) % elsize;
203 int end = (offset + length) / elsize;
204 int i;
205
206 if (endrest)
207 end++;
208
209 if (end > c->n)
19124154 210 return false;
8cf6f0b1
TT
211
212 for (i = start; i < end; i++)
213 {
8f9a01ee
MS
214 int comp_offset = (i == start) ? startrest : 0;
215 int comp_length = (i == end) ? endrest : elsize;
8cf6f0b1 216
e989e637
TT
217 if (!c->val->bits_synthetic_pointer (c->indices[i] * elsize + comp_offset,
218 comp_length))
19124154 219 return false;
8cf6f0b1
TT
220 }
221
19124154 222 return true;
8cf6f0b1
TT
223}
224
f4b8a18d
KW
225static void *
226lval_func_copy_closure (const struct value *v)
227{
b9f74d54 228 struct lval_closure *c = (struct lval_closure *) v->computed_closure ();
f4b8a18d
KW
229
230 ++c->refc;
231
232 return c;
233}
234
235static void
236lval_func_free_closure (struct value *v)
237{
b9f74d54 238 struct lval_closure *c = (struct lval_closure *) v->computed_closure ();
f4b8a18d
KW
239
240 --c->refc;
241
242 if (c->refc == 0)
243 {
cdf3de17 244 c->val->decref (); /* Decrement the reference counter of the value. */
f4b8a18d
KW
245 xfree (c->indices);
246 xfree (c);
f4b8a18d
KW
247 }
248}
249
c8f2448a 250static const struct lval_funcs opencl_value_funcs =
f4b8a18d
KW
251 {
252 lval_func_read,
253 lval_func_write,
a519e8ff 254 nullptr,
a471c594
JK
255 NULL, /* indirect */
256 NULL, /* coerce_ref */
8cf6f0b1 257 lval_func_check_synthetic_pointer,
f4b8a18d
KW
258 lval_func_copy_closure,
259 lval_func_free_closure
260 };
261
262/* Creates a sub-vector from VAL. The elements are selected by the indices of
263 an array with the length of N. Supported values for NOSIDE are
264 EVAL_NORMAL and EVAL_AVOID_SIDE_EFFECTS. */
265
266static struct value *
267create_value (struct gdbarch *gdbarch, struct value *val, enum noside noside,
268 int *indices, int n)
269{
d0c97917 270 struct type *type = check_typedef (val->type ());
27710edb 271 struct type *elm_type = type->target_type ();
f4b8a18d
KW
272 struct value *ret;
273
274 /* Check if a single component of a vector is requested which means
275 the resulting type is a (primitive) scalar type. */
276 if (n == 1)
277 {
278 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 279 ret = value::zero (elm_type, not_lval);
f4b8a18d 280 else
dda83cd7 281 ret = value_subscript (val, indices[0]);
f4b8a18d
KW
282 }
283 else
284 {
285 /* Multiple components of the vector are requested which means the
286 resulting type is a vector as well. */
287 struct type *dst_type =
78134374 288 lookup_opencl_vector_type (gdbarch, elm_type->code (),
df86565b 289 elm_type->length (),
c6d940a9 290 elm_type->is_unsigned (), n);
f4b8a18d
KW
291
292 if (dst_type == NULL)
293 dst_type = init_vector_type (elm_type, n);
294
295 make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type), dst_type, NULL);
296
297 if (noside == EVAL_AVOID_SIDE_EFFECTS)
317c3ed9 298 ret = value::allocate (dst_type);
f4b8a18d
KW
299 else
300 {
301 /* Check whether to create a lvalue or not. */
736355f2 302 if (val->lval () != not_lval && !array_has_dups (indices, n))
f4b8a18d
KW
303 {
304 struct lval_closure *c = allocate_lval_closure (indices, n, val);
b64e2602 305 ret = value::allocate_computed (dst_type, &opencl_value_funcs, c);
f4b8a18d
KW
306 }
307 else
308 {
309 int i;
310
317c3ed9 311 ret = value::allocate (dst_type);
f4b8a18d
KW
312
313 /* Copy src val contents into the destination value. */
314 for (i = 0; i < n; i++)
bbe912ba 315 memcpy (ret->contents_writeable ().data ()
df86565b 316 + (i * elm_type->length ()),
efaf1ae0 317 val->contents ().data ()
df86565b
SM
318 + (indices[i] * elm_type->length ()),
319 elm_type->length ());
f4b8a18d
KW
320 }
321 }
322 }
323 return ret;
324}
325
326/* OpenCL vector component access. */
327
328static struct value *
749065b7
TT
329opencl_component_ref (struct expression *exp, struct value *val,
330 const char *comps, enum noside noside)
f4b8a18d
KW
331{
332 LONGEST lowb, highb;
333 int src_len;
334 struct value *v;
335 int indices[16], i;
336 int dst_len;
337
d0c97917 338 if (!get_array_bounds (check_typedef (val->type ()), &lowb, &highb))
f4b8a18d
KW
339 error (_("Could not determine the vector bounds"));
340
341 src_len = highb - lowb + 1;
342
343 /* Throw an error if the amount of array elements does not fit a
344 valid OpenCL vector size (2, 3, 4, 8, 16). */
345 if (src_len != 2 && src_len != 3 && src_len != 4 && src_len != 8
346 && src_len != 16)
347 error (_("Invalid OpenCL vector size"));
348
349 if (strcmp (comps, "lo") == 0 )
350 {
351 dst_len = (src_len == 3) ? 2 : src_len / 2;
352
353 for (i = 0; i < dst_len; i++)
354 indices[i] = i;
355 }
356 else if (strcmp (comps, "hi") == 0)
357 {
358 dst_len = (src_len == 3) ? 2 : src_len / 2;
359
360 for (i = 0; i < dst_len; i++)
361 indices[i] = dst_len + i;
362 }
363 else if (strcmp (comps, "even") == 0)
364 {
365 dst_len = (src_len == 3) ? 2 : src_len / 2;
366
367 for (i = 0; i < dst_len; i++)
368 indices[i] = i*2;
369 }
370 else if (strcmp (comps, "odd") == 0)
371 {
372 dst_len = (src_len == 3) ? 2 : src_len / 2;
373
374 for (i = 0; i < dst_len; i++)
dda83cd7 375 indices[i] = i*2+1;
f4b8a18d
KW
376 }
377 else if (strncasecmp (comps, "s", 1) == 0)
378 {
379#define HEXCHAR_TO_INT(C) ((C >= '0' && C <= '9') ? \
dda83cd7
SM
380 C-'0' : ((C >= 'A' && C <= 'F') ? \
381 C-'A'+10 : ((C >= 'a' && C <= 'f') ? \
382 C-'a'+10 : -1)))
f4b8a18d
KW
383
384 dst_len = strlen (comps);
385 /* Skip the s/S-prefix. */
386 dst_len--;
387
388 for (i = 0; i < dst_len; i++)
389 {
390 indices[i] = HEXCHAR_TO_INT(comps[i+1]);
391 /* Check if the requested component is invalid or exceeds
392 the vector. */
393 if (indices[i] < 0 || indices[i] >= src_len)
394 error (_("Invalid OpenCL vector component accessor %s"), comps);
395 }
396 }
397 else
398 {
399 dst_len = strlen (comps);
400
401 for (i = 0; i < dst_len; i++)
402 {
403 /* x, y, z, w */
404 switch (comps[i])
405 {
406 case 'x':
407 indices[i] = 0;
408 break;
409 case 'y':
410 indices[i] = 1;
411 break;
412 case 'z':
413 if (src_len < 3)
414 error (_("Invalid OpenCL vector component accessor %s"), comps);
415 indices[i] = 2;
416 break;
417 case 'w':
418 if (src_len < 4)
419 error (_("Invalid OpenCL vector component accessor %s"), comps);
420 indices[i] = 3;
421 break;
422 default:
423 error (_("Invalid OpenCL vector component accessor %s"), comps);
424 break;
425 }
426 }
427 }
428
429 /* Throw an error if the amount of requested components does not
430 result in a valid length (1, 2, 3, 4, 8, 16). */
431 if (dst_len != 1 && dst_len != 2 && dst_len != 3 && dst_len != 4
432 && dst_len != 8 && dst_len != 16)
433 error (_("Invalid OpenCL vector component accessor %s"), comps);
434
435 v = create_value (exp->gdbarch, val, noside, indices, dst_len);
436
437 return v;
438}
439
440/* Perform the unary logical not (!) operation. */
441
2492ba36
TT
442struct value *
443opencl_logical_not (struct type *expect_type, struct expression *exp,
444 enum noside noside, enum exp_opcode op,
445 struct value *arg)
f4b8a18d 446{
d0c97917 447 struct type *type = check_typedef (arg->type ());
f4b8a18d
KW
448 struct type *rettype;
449 struct value *ret;
450
bd63c870 451 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
f4b8a18d 452 {
27710edb 453 struct type *eltype = check_typedef (type->target_type ());
f4b8a18d
KW
454 LONGEST lowb, highb;
455 int i;
456
457 if (!get_array_bounds (type, &lowb, &highb))
458 error (_("Could not determine the vector bounds"));
459
460 /* Determine the resulting type of the operation and allocate the
461 value. */
462 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
df86565b 463 eltype->length (), 0,
f4b8a18d 464 highb - lowb + 1);
317c3ed9 465 ret = value::allocate (rettype);
f4b8a18d
KW
466
467 for (i = 0; i < highb - lowb + 1; i++)
468 {
469 /* For vector types, the unary operator shall return a 0 if the
470 value of its operand compares unequal to 0, and -1 (i.e. all bits
471 set) if the value of its operand compares equal to 0. */
472 int tmp = value_logical_not (value_subscript (arg, i)) ? -1 : 0;
bbe912ba 473 memset ((ret->contents_writeable ().data ()
df86565b
SM
474 + i * eltype->length ()),
475 tmp, eltype->length ());
f4b8a18d
KW
476 }
477 }
478 else
479 {
480 rettype = language_bool_type (exp->language_defn, exp->gdbarch);
481 ret = value_from_longest (rettype, value_logical_not (arg));
482 }
483
484 return ret;
485}
486
487/* Perform a relational operation on two scalar operands. */
488
489static int
490scalar_relop (struct value *val1, struct value *val2, enum exp_opcode op)
491{
492 int ret;
493
494 switch (op)
495 {
496 case BINOP_EQUAL:
497 ret = value_equal (val1, val2);
498 break;
499 case BINOP_NOTEQUAL:
500 ret = !value_equal (val1, val2);
501 break;
502 case BINOP_LESS:
503 ret = value_less (val1, val2);
504 break;
505 case BINOP_GTR:
506 ret = value_less (val2, val1);
507 break;
508 case BINOP_GEQ:
509 ret = value_less (val2, val1) || value_equal (val1, val2);
510 break;
511 case BINOP_LEQ:
512 ret = value_less (val1, val2) || value_equal (val1, val2);
513 break;
514 case BINOP_LOGICAL_AND:
515 ret = !value_logical_not (val1) && !value_logical_not (val2);
516 break;
517 case BINOP_LOGICAL_OR:
518 ret = !value_logical_not (val1) || !value_logical_not (val2);
519 break;
520 default:
521 error (_("Attempt to perform an unsupported operation"));
522 break;
523 }
524 return ret;
525}
526
527/* Perform a relational operation on two vector operands. */
528
529static struct value *
530vector_relop (struct expression *exp, struct value *val1, struct value *val2,
531 enum exp_opcode op)
532{
533 struct value *ret;
534 struct type *type1, *type2, *eltype1, *eltype2, *rettype;
535 int t1_is_vec, t2_is_vec, i;
536 LONGEST lowb1, lowb2, highb1, highb2;
537
d0c97917
TT
538 type1 = check_typedef (val1->type ());
539 type2 = check_typedef (val2->type ());
f4b8a18d 540
bd63c870
SM
541 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ());
542 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ());
f4b8a18d
KW
543
544 if (!t1_is_vec || !t2_is_vec)
545 error (_("Vector operations are not supported on scalar types"));
546
27710edb
SM
547 eltype1 = check_typedef (type1->target_type ());
548 eltype2 = check_typedef (type2->target_type ());
f4b8a18d
KW
549
550 if (!get_array_bounds (type1,&lowb1, &highb1)
551 || !get_array_bounds (type2, &lowb2, &highb2))
552 error (_("Could not determine the vector bounds"));
553
554 /* Check whether the vector types are compatible. */
78134374 555 if (eltype1->code () != eltype2->code ()
df86565b 556 || eltype1->length () != eltype2->length ()
c6d940a9 557 || eltype1->is_unsigned () != eltype2->is_unsigned ()
f4b8a18d
KW
558 || lowb1 != lowb2 || highb1 != highb2)
559 error (_("Cannot perform operation on vectors with different types"));
560
561 /* Determine the resulting type of the operation and allocate the value. */
562 rettype = lookup_opencl_vector_type (exp->gdbarch, TYPE_CODE_INT,
df86565b 563 eltype1->length (), 0,
f4b8a18d 564 highb1 - lowb1 + 1);
317c3ed9 565 ret = value::allocate (rettype);
f4b8a18d
KW
566
567 for (i = 0; i < highb1 - lowb1 + 1; i++)
568 {
569 /* For vector types, the relational, equality and logical operators shall
570 return 0 if the specified relation is false and -1 (i.e. all bits set)
571 if the specified relation is true. */
572 int tmp = scalar_relop (value_subscript (val1, i),
573 value_subscript (val2, i), op) ? -1 : 0;
bbe912ba 574 memset ((ret->contents_writeable ().data ()
df86565b
SM
575 + i * eltype1->length ()),
576 tmp, eltype1->length ());
f4b8a18d
KW
577 }
578
579 return ret;
580}
581
8954db33
AB
582/* Perform a cast of ARG into TYPE. There's sadly a lot of duplication in
583 here from valops.c:value_cast, opencl is different only in the
ac51afb5 584 behavior of scalar to vector casting. As far as possibly we're going
8954db33
AB
585 to try and delegate back to the standard value_cast function. */
586
e9677704 587struct value *
8954db33
AB
588opencl_value_cast (struct type *type, struct value *arg)
589{
d0c97917 590 if (type != arg->type ())
8954db33
AB
591 {
592 /* Casting scalar to vector is a special case for OpenCL, scalar
593 is cast to element type of vector then replicated into each
594 element of the vector. First though, we need to work out if
595 this is a scalar to vector cast; code lifted from
596 valops.c:value_cast. */
597 enum type_code code1, code2;
598 struct type *to_type;
599 int scalar;
600
601 to_type = check_typedef (type);
602
78134374 603 code1 = to_type->code ();
d0c97917 604 code2 = check_typedef (arg->type ())->code ();
8954db33
AB
605
606 if (code2 == TYPE_CODE_REF)
d0c97917 607 code2 = check_typedef (coerce_ref(arg)->type ())->code ();
8954db33
AB
608
609 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL
610 || code2 == TYPE_CODE_CHAR || code2 == TYPE_CODE_FLT
611 || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
612 || code2 == TYPE_CODE_RANGE);
613
bd63c870 614 if (code1 == TYPE_CODE_ARRAY && to_type->is_vector () && scalar)
8954db33
AB
615 {
616 struct type *eltype;
617
618 /* Cast to the element type of the vector here as
619 value_vector_widen will error if the scalar value is
620 truncated by the cast. To avoid the error, cast (and
621 possibly truncate) here. */
27710edb 622 eltype = check_typedef (to_type->target_type ());
8954db33
AB
623 arg = value_cast (eltype, arg);
624
625 return value_vector_widen (arg, type);
626 }
627 else
628 /* Standard cast handler. */
629 arg = value_cast (type, arg);
630 }
631 return arg;
632}
633
f4b8a18d
KW
634/* Perform a relational operation on two operands. */
635
a88c3c8d
TT
636struct value *
637opencl_relop (struct type *expect_type, struct expression *exp,
638 enum noside noside, enum exp_opcode op,
639 struct value *arg1, struct value *arg2)
f4b8a18d
KW
640{
641 struct value *val;
d0c97917
TT
642 struct type *type1 = check_typedef (arg1->type ());
643 struct type *type2 = check_typedef (arg2->type ());
78134374 644 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
bd63c870 645 && type1->is_vector ());
78134374 646 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
bd63c870 647 && type2->is_vector ());
f4b8a18d
KW
648
649 if (!t1_is_vec && !t2_is_vec)
650 {
651 int tmp = scalar_relop (arg1, arg2, op);
652 struct type *type =
653 language_bool_type (exp->language_defn, exp->gdbarch);
654
655 val = value_from_longest (type, tmp);
656 }
657 else if (t1_is_vec && t2_is_vec)
658 {
659 val = vector_relop (exp, arg1, arg2, op);
660 }
661 else
662 {
663 /* Widen the scalar operand to a vector. */
664 struct value **v = t1_is_vec ? &arg2 : &arg1;
665 struct type *t = t1_is_vec ? type2 : type1;
666
78134374 667 if (t->code () != TYPE_CODE_FLT && !is_integral_type (t))
f4b8a18d
KW
668 error (_("Argument to operation not a number or boolean."));
669
8954db33 670 *v = opencl_value_cast (t1_is_vec ? type1 : type2, *v);
f4b8a18d
KW
671 val = vector_relop (exp, arg1, arg2, op);
672 }
673
674 return val;
675}
676
3634f669
TT
677/* A helper function for BINOP_ASSIGN. */
678
a88c3c8d 679struct value *
3634f669 680eval_opencl_assign (struct type *expect_type, struct expression *exp,
a88c3c8d 681 enum noside noside, enum exp_opcode op,
3634f669
TT
682 struct value *arg1, struct value *arg2)
683{
0b2b0b82 684 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3634f669
TT
685 return arg1;
686
d0c97917 687 struct type *type1 = arg1->type ();
4b53ca88 688 if (arg1->deprecated_modifiable ()
736355f2 689 && arg1->lval () != lval_internalvar)
3634f669
TT
690 arg2 = opencl_value_cast (type1, arg2);
691
692 return value_assign (arg1, arg2);
693}
694
33b79214
TT
695namespace expr
696{
697
698value *
699opencl_structop_operation::evaluate (struct type *expect_type,
700 struct expression *exp,
701 enum noside noside)
702{
703 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
d0c97917 704 struct type *type1 = check_typedef (arg1->type ());
33b79214
TT
705
706 if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
707 return opencl_component_ref (exp, arg1, std::get<1> (m_storage).c_str (),
708 noside);
709 else
710 {
158cc4fe 711 struct value *v = value_struct_elt (&arg1, {},
33b79214
TT
712 std::get<1> (m_storage).c_str (),
713 NULL, "structure");
714
715 if (noside == EVAL_AVOID_SIDE_EFFECTS)
736355f2 716 v = value::zero (v->type (), v->lval ());
33b79214
TT
717 return v;
718 }
719}
720
944fd3b8
TT
721value *
722opencl_logical_binop_operation::evaluate (struct type *expect_type,
723 struct expression *exp,
724 enum noside noside)
725{
726 enum exp_opcode op = std::get<0> (m_storage);
727 value *arg1 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
728
729 /* For scalar operations we need to avoid evaluating operands
730 unnecessarily. However, for vector operations we always need to
731 evaluate both operands. Unfortunately we only know which of the
732 two cases apply after we know the type of the second operand.
733 Therefore we evaluate it once using EVAL_AVOID_SIDE_EFFECTS. */
734 value *arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp,
735 EVAL_AVOID_SIDE_EFFECTS);
d0c97917
TT
736 struct type *type1 = check_typedef (arg1->type ());
737 struct type *type2 = check_typedef (arg2->type ());
944fd3b8
TT
738
739 if ((type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
740 || (type2->code () == TYPE_CODE_ARRAY && type2->is_vector ()))
741 {
742 arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
743
744 return opencl_relop (nullptr, exp, noside, op, arg1, arg2);
745 }
746 else
747 {
748 /* For scalar built-in types, only evaluate the right
749 hand operand if the left hand operand compares
750 unequal(&&)/equal(||) to 0. */
7ebaa5f7 751 bool tmp = value_logical_not (arg1);
944fd3b8
TT
752
753 if (op == BINOP_LOGICAL_OR)
754 tmp = !tmp;
755
756 if (!tmp)
757 {
758 arg2 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
759 tmp = value_logical_not (arg2);
760 if (op == BINOP_LOGICAL_OR)
761 tmp = !tmp;
762 }
763
764 type1 = language_bool_type (exp->language_defn, exp->gdbarch);
765 return value_from_longest (type1, tmp);
766 }
767}
768
cf12b17f
TT
769value *
770opencl_ternop_cond_operation::evaluate (struct type *expect_type,
771 struct expression *exp,
772 enum noside noside)
773{
774 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
d0c97917 775 struct type *type1 = check_typedef (arg1->type ());
cf12b17f
TT
776 if (type1->code () == TYPE_CODE_ARRAY && type1->is_vector ())
777 {
778 struct value *arg2, *arg3, *tmp, *ret;
779 struct type *eltype2, *type2, *type3, *eltype3;
780 int t2_is_vec, t3_is_vec, i;
781 LONGEST lowb1, lowb2, lowb3, highb1, highb2, highb3;
782
783 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
784 arg3 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
d0c97917
TT
785 type2 = check_typedef (arg2->type ());
786 type3 = check_typedef (arg3->type ());
cf12b17f
TT
787 t2_is_vec
788 = type2->code () == TYPE_CODE_ARRAY && type2->is_vector ();
789 t3_is_vec
790 = type3->code () == TYPE_CODE_ARRAY && type3->is_vector ();
791
792 /* Widen the scalar operand to a vector if necessary. */
793 if (t2_is_vec || !t3_is_vec)
794 {
795 arg3 = opencl_value_cast (type2, arg3);
d0c97917 796 type3 = arg3->type ();
cf12b17f
TT
797 }
798 else if (!t2_is_vec || t3_is_vec)
799 {
800 arg2 = opencl_value_cast (type3, arg2);
d0c97917 801 type2 = arg2->type ();
cf12b17f
TT
802 }
803 else if (!t2_is_vec || !t3_is_vec)
804 {
805 /* Throw an error if arg2 or arg3 aren't vectors. */
806 error (_("\
807Cannot perform conditional operation on incompatible types"));
808 }
809
27710edb
SM
810 eltype2 = check_typedef (type2->target_type ());
811 eltype3 = check_typedef (type3->target_type ());
cf12b17f
TT
812
813 if (!get_array_bounds (type1, &lowb1, &highb1)
814 || !get_array_bounds (type2, &lowb2, &highb2)
815 || !get_array_bounds (type3, &lowb3, &highb3))
816 error (_("Could not determine the vector bounds"));
817
818 /* Throw an error if the types of arg2 or arg3 are incompatible. */
819 if (eltype2->code () != eltype3->code ()
df86565b 820 || eltype2->length () != eltype3->length ()
cf12b17f
TT
821 || eltype2->is_unsigned () != eltype3->is_unsigned ()
822 || lowb2 != lowb3 || highb2 != highb3)
823 error (_("\
824Cannot perform operation on vectors with different types"));
825
826 /* Throw an error if the sizes of arg1 and arg2/arg3 differ. */
827 if (lowb1 != lowb2 || lowb1 != lowb3
828 || highb1 != highb2 || highb1 != highb3)
829 error (_("\
830Cannot perform conditional operation on vectors with different sizes"));
831
317c3ed9 832 ret = value::allocate (type2);
cf12b17f
TT
833
834 for (i = 0; i < highb1 - lowb1 + 1; i++)
835 {
836 tmp = value_logical_not (value_subscript (arg1, i)) ?
837 value_subscript (arg3, i) : value_subscript (arg2, i);
bbe912ba 838 memcpy (ret->contents_writeable ().data () +
efaf1ae0 839 i * eltype2->length (), tmp->contents_all ().data (),
df86565b 840 eltype2->length ());
cf12b17f
TT
841 }
842
843 return ret;
844 }
845 else
846 {
847 if (value_logical_not (arg1))
848 return std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
849 else
850 return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
851 }
852}
853
33b79214
TT
854} /* namespace expr */
855
0874fd07
AB
856/* Class representing the OpenCL language. */
857
858class opencl_language : public language_defn
859{
860public:
861 opencl_language ()
0e25e767 862 : language_defn (language_opencl)
0874fd07 863 { /* Nothing. */ }
1fb314aa 864
6f7664a9
AB
865 /* See language.h. */
866
867 const char *name () const override
868 { return "opencl"; }
869
870 /* See language.h. */
871
872 const char *natural_name () const override
873 { return "OpenCL C"; }
874
1fb314aa
AB
875 /* See language.h. */
876 void language_arch_info (struct gdbarch *gdbarch,
877 struct language_arch_info *lai) const override
878 {
7bea47f0
AB
879 /* Helper function to allow shorter lines below. */
880 auto add = [&] (struct type * t) -> struct type *
881 {
882 lai->add_primitive_type (t);
883 return t;
884 };
1fb314aa 885
7bea47f0
AB
886/* Helper macro to create strings. */
887#define OCL_STRING(S) #S
888
889/* This macro allocates and assigns the type struct pointers
890 for the vector types. */
891#define BUILD_OCL_VTYPES(TYPE, ELEMENT_TYPE) \
892 do \
893 { \
894 struct type *tmp; \
895 tmp = add (init_vector_type (ELEMENT_TYPE, 2)); \
896 tmp->set_name (OCL_STRING(TYPE ## 2)); \
897 tmp = add (init_vector_type (ELEMENT_TYPE, 3)); \
898 tmp->set_name (OCL_STRING(TYPE ## 3)); \
df86565b 899 tmp->set_length (4 * (ELEMENT_TYPE)->length ()); \
7bea47f0
AB
900 tmp = add (init_vector_type (ELEMENT_TYPE, 4)); \
901 tmp->set_name (OCL_STRING(TYPE ## 4)); \
902 tmp = add (init_vector_type (ELEMENT_TYPE, 8)); \
903 tmp->set_name (OCL_STRING(TYPE ## 8)); \
904 tmp = init_vector_type (ELEMENT_TYPE, 16); \
905 tmp->set_name (OCL_STRING(TYPE ## 16)); \
906 } \
907 while (false)
908
909 struct type *el_type, *char_type, *int_type;
910
2d39ccd3
TT
911 type_allocator alloc (gdbarch);
912 char_type = el_type = add (init_integer_type (alloc, 8, 0, "char"));
7bea47f0 913 BUILD_OCL_VTYPES (char, el_type);
2d39ccd3 914 el_type = add (init_integer_type (alloc, 8, 1, "uchar"));
7bea47f0 915 BUILD_OCL_VTYPES (uchar, el_type);
2d39ccd3 916 el_type = add (init_integer_type (alloc, 16, 0, "short"));
7bea47f0 917 BUILD_OCL_VTYPES (short, el_type);
2d39ccd3 918 el_type = add (init_integer_type (alloc, 16, 1, "ushort"));
7bea47f0 919 BUILD_OCL_VTYPES (ushort, el_type);
2d39ccd3 920 int_type = el_type = add (init_integer_type (alloc, 32, 0, "int"));
7bea47f0 921 BUILD_OCL_VTYPES (int, el_type);
2d39ccd3 922 el_type = add (init_integer_type (alloc, 32, 1, "uint"));
7bea47f0 923 BUILD_OCL_VTYPES (uint, el_type);
2d39ccd3 924 el_type = add (init_integer_type (alloc, 64, 0, "long"));
7bea47f0 925 BUILD_OCL_VTYPES (long, el_type);
2d39ccd3 926 el_type = add (init_integer_type (alloc, 64, 1, "ulong"));
7bea47f0 927 BUILD_OCL_VTYPES (ulong, el_type);
77c5f496 928 el_type = add (init_float_type (alloc, 16, "half", floatformats_ieee_half));
7bea47f0 929 BUILD_OCL_VTYPES (half, el_type);
77c5f496 930 el_type = add (init_float_type (alloc, 32, "float", floatformats_ieee_single));
7bea47f0 931 BUILD_OCL_VTYPES (float, el_type);
77c5f496 932 el_type = add (init_float_type (alloc, 64, "double", floatformats_ieee_double));
7bea47f0
AB
933 BUILD_OCL_VTYPES (double, el_type);
934
46c04ea3 935 add (init_boolean_type (alloc, 8, 1, "bool"));
2d39ccd3
TT
936 add (init_integer_type (alloc, 8, 1, "unsigned char"));
937 add (init_integer_type (alloc, 16, 1, "unsigned short"));
938 add (init_integer_type (alloc, 32, 1, "unsigned int"));
939 add (init_integer_type (alloc, 64, 1, "unsigned long"));
940 add (init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 1, "size_t"));
941 add (init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 0, "ptrdiff_t"));
942 add (init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 0, "intptr_t"));
943 add (init_integer_type (alloc, gdbarch_ptr_bit (gdbarch), 1, "uintptr_t"));
95751990 944 add (builtin_type (gdbarch)->builtin_void);
1fb314aa
AB
945
946 /* Type of elements of strings. */
7bea47f0 947 lai->set_string_char_type (char_type);
1fb314aa
AB
948
949 /* Specifies the return type of logical and relational operations. */
7bea47f0 950 lai->set_bool_type (int_type, "int");
1fb314aa 951 }
fbfb0a46
AB
952
953 /* See language.h. */
954
97e20099
TT
955 bool can_print_type_offsets () const override
956 {
957 return true;
958 }
959
960 /* See language.h. */
961
fbfb0a46
AB
962 void print_type (struct type *type, const char *varstring,
963 struct ui_file *stream, int show, int level,
964 const struct type_print_options *flags) const override
965 {
966 /* We nearly always defer to C type printing, except that vector types
967 are considered primitive in OpenCL, and should always be printed
968 using their TYPE_NAME. */
969 if (show > 0)
970 {
971 type = check_typedef (type);
bd63c870 972 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
fbfb0a46
AB
973 && type->name () != NULL)
974 show = 0;
975 }
976
1c6fbf42 977 c_print_type (type, varstring, stream, show, level, la_language, flags);
fbfb0a46 978 }
1ac14a04
AB
979
980 /* See language.h. */
981
982 enum macro_expansion macro_expansion () const override
983 { return macro_expansion_c; }
0874fd07
AB
984};
985
986/* Single instance of the OpenCL language class. */
987
988static opencl_language opencl_language_defn;