opencl-lang.c 29 KB

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