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