valarith.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  1. /* Perform arithmetic and other operations on values, for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "value.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "target.h"
  20. #include "language.h"
  21. #include "target-float.h"
  22. #include "infcall.h"
  23. #include "gdbsupport/byte-vector.h"
  24. #include "gdbarch.h"
  25. /* Forward declarations. */
  26. static struct value *value_subscripted_rvalue (struct value *array,
  27. LONGEST index,
  28. LONGEST lowerbound);
  29. /* Define whether or not the C operator '/' truncates towards zero for
  30. differently signed operands (truncation direction is undefined in C). */
  31. #ifndef TRUNCATION_TOWARDS_ZERO
  32. #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
  33. #endif
  34. /* Given a pointer, return the size of its target.
  35. If the pointer type is void *, then return 1.
  36. If the target type is incomplete, then error out.
  37. This isn't a general purpose function, but just a
  38. helper for value_ptradd. */
  39. static LONGEST
  40. find_size_for_pointer_math (struct type *ptr_type)
  41. {
  42. LONGEST sz = -1;
  43. struct type *ptr_target;
  44. gdb_assert (ptr_type->code () == TYPE_CODE_PTR);
  45. ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
  46. sz = type_length_units (ptr_target);
  47. if (sz == 0)
  48. {
  49. if (ptr_type->code () == TYPE_CODE_VOID)
  50. sz = 1;
  51. else
  52. {
  53. const char *name;
  54. name = ptr_target->name ();
  55. if (name == NULL)
  56. error (_("Cannot perform pointer math on incomplete types, "
  57. "try casting to a known type, or void *."));
  58. else
  59. error (_("Cannot perform pointer math on incomplete type \"%s\", "
  60. "try casting to a known type, or void *."), name);
  61. }
  62. }
  63. return sz;
  64. }
  65. /* Given a pointer ARG1 and an integral value ARG2, return the
  66. result of C-style pointer arithmetic ARG1 + ARG2. */
  67. struct value *
  68. value_ptradd (struct value *arg1, LONGEST arg2)
  69. {
  70. struct type *valptrtype;
  71. LONGEST sz;
  72. struct value *result;
  73. arg1 = coerce_array (arg1);
  74. valptrtype = check_typedef (value_type (arg1));
  75. sz = find_size_for_pointer_math (valptrtype);
  76. result = value_from_pointer (valptrtype,
  77. value_as_address (arg1) + sz * arg2);
  78. if (VALUE_LVAL (result) != lval_internalvar)
  79. set_value_component_location (result, arg1);
  80. return result;
  81. }
  82. /* Given two compatible pointer values ARG1 and ARG2, return the
  83. result of C-style pointer arithmetic ARG1 - ARG2. */
  84. LONGEST
  85. value_ptrdiff (struct value *arg1, struct value *arg2)
  86. {
  87. struct type *type1, *type2;
  88. LONGEST sz;
  89. arg1 = coerce_array (arg1);
  90. arg2 = coerce_array (arg2);
  91. type1 = check_typedef (value_type (arg1));
  92. type2 = check_typedef (value_type (arg2));
  93. gdb_assert (type1->code () == TYPE_CODE_PTR);
  94. gdb_assert (type2->code () == TYPE_CODE_PTR);
  95. if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
  96. != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
  97. error (_("First argument of `-' is a pointer and "
  98. "second argument is neither\n"
  99. "an integer nor a pointer of the same type."));
  100. sz = type_length_units (check_typedef (TYPE_TARGET_TYPE (type1)));
  101. if (sz == 0)
  102. {
  103. warning (_("Type size unknown, assuming 1. "
  104. "Try casting to a known type, or void *."));
  105. sz = 1;
  106. }
  107. return (value_as_long (arg1) - value_as_long (arg2)) / sz;
  108. }
  109. /* Return the value of ARRAY[IDX].
  110. ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
  111. current language supports C-style arrays, it may also be TYPE_CODE_PTR.
  112. See comments in value_coerce_array() for rationale for reason for
  113. doing lower bounds adjustment here rather than there.
  114. FIXME: Perhaps we should validate that the index is valid and if
  115. verbosity is set, warn about invalid indices (but still use them). */
  116. struct value *
  117. value_subscript (struct value *array, LONGEST index)
  118. {
  119. bool c_style = current_language->c_style_arrays_p ();
  120. struct type *tarray;
  121. array = coerce_ref (array);
  122. tarray = check_typedef (value_type (array));
  123. if (tarray->code () == TYPE_CODE_ARRAY
  124. || tarray->code () == TYPE_CODE_STRING)
  125. {
  126. struct type *range_type = tarray->index_type ();
  127. gdb::optional<LONGEST> lowerbound = get_discrete_low_bound (range_type);
  128. if (!lowerbound.has_value ())
  129. lowerbound = 0;
  130. if (VALUE_LVAL (array) != lval_memory)
  131. return value_subscripted_rvalue (array, index, *lowerbound);
  132. gdb::optional<LONGEST> upperbound
  133. = get_discrete_high_bound (range_type);
  134. if (!upperbound.has_value ())
  135. upperbound = -1;
  136. if (index >= *lowerbound && index <= *upperbound)
  137. return value_subscripted_rvalue (array, index, *lowerbound);
  138. if (!c_style)
  139. {
  140. /* Emit warning unless we have an array of unknown size.
  141. An array of unknown size has lowerbound 0 and upperbound -1. */
  142. if (*upperbound > -1)
  143. warning (_("array or string index out of range"));
  144. /* fall doing C stuff */
  145. c_style = true;
  146. }
  147. index -= *lowerbound;
  148. array = value_coerce_array (array);
  149. }
  150. if (c_style)
  151. return value_ind (value_ptradd (array, index));
  152. else
  153. error (_("not an array or string"));
  154. }
  155. /* Return the value of EXPR[IDX], expr an aggregate rvalue
  156. (eg, a vector register). This routine used to promote floats
  157. to doubles, but no longer does. */
  158. static struct value *
  159. value_subscripted_rvalue (struct value *array, LONGEST index,
  160. LONGEST lowerbound)
  161. {
  162. struct type *array_type = check_typedef (value_type (array));
  163. struct type *elt_type = TYPE_TARGET_TYPE (array_type);
  164. LONGEST elt_size = type_length_units (elt_type);
  165. /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
  166. in a byte. */
  167. LONGEST stride = array_type->bit_stride ();
  168. if (stride != 0)
  169. {
  170. struct gdbarch *arch = elt_type->arch ();
  171. int unit_size = gdbarch_addressable_memory_unit_size (arch);
  172. elt_size = stride / (unit_size * 8);
  173. }
  174. LONGEST elt_offs = elt_size * (index - lowerbound);
  175. bool array_upper_bound_undefined
  176. = array_type->bounds ()->high.kind () == PROP_UNDEFINED;
  177. if (index < lowerbound
  178. || (!array_upper_bound_undefined
  179. && elt_offs >= type_length_units (array_type))
  180. || (VALUE_LVAL (array) != lval_memory && array_upper_bound_undefined))
  181. {
  182. if (type_not_associated (array_type))
  183. error (_("no such vector element (vector not associated)"));
  184. else if (type_not_allocated (array_type))
  185. error (_("no such vector element (vector not allocated)"));
  186. else
  187. error (_("no such vector element"));
  188. }
  189. if (is_dynamic_type (elt_type))
  190. {
  191. CORE_ADDR address;
  192. address = value_address (array) + elt_offs;
  193. elt_type = resolve_dynamic_type (elt_type, {}, address);
  194. }
  195. return value_from_component (array, elt_type, elt_offs);
  196. }
  197. /* Check to see if either argument is a structure, or a reference to
  198. one. This is called so we know whether to go ahead with the normal
  199. binop or look for a user defined function instead.
  200. For now, we do not overload the `=' operator. */
  201. int
  202. binop_types_user_defined_p (enum exp_opcode op,
  203. struct type *type1, struct type *type2)
  204. {
  205. if (op == BINOP_ASSIGN)
  206. return 0;
  207. type1 = check_typedef (type1);
  208. if (TYPE_IS_REFERENCE (type1))
  209. type1 = check_typedef (TYPE_TARGET_TYPE (type1));
  210. type2 = check_typedef (type2);
  211. if (TYPE_IS_REFERENCE (type2))
  212. type2 = check_typedef (TYPE_TARGET_TYPE (type2));
  213. return (type1->code () == TYPE_CODE_STRUCT
  214. || type2->code () == TYPE_CODE_STRUCT);
  215. }
  216. /* Check to see if either argument is a structure, or a reference to
  217. one. This is called so we know whether to go ahead with the normal
  218. binop or look for a user defined function instead.
  219. For now, we do not overload the `=' operator. */
  220. int
  221. binop_user_defined_p (enum exp_opcode op,
  222. struct value *arg1, struct value *arg2)
  223. {
  224. return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
  225. }
  226. /* Check to see if argument is a structure. This is called so
  227. we know whether to go ahead with the normal unop or look for a
  228. user defined function instead.
  229. For now, we do not overload the `&' operator. */
  230. int
  231. unop_user_defined_p (enum exp_opcode op, struct value *arg1)
  232. {
  233. struct type *type1;
  234. if (op == UNOP_ADDR)
  235. return 0;
  236. type1 = check_typedef (value_type (arg1));
  237. if (TYPE_IS_REFERENCE (type1))
  238. type1 = check_typedef (TYPE_TARGET_TYPE (type1));
  239. return type1->code () == TYPE_CODE_STRUCT;
  240. }
  241. /* Try to find an operator named OPERATOR which takes NARGS arguments
  242. specified in ARGS. If the operator found is a static member operator
  243. *STATIC_MEMFUNP will be set to 1, and otherwise 0.
  244. The search if performed through find_overload_match which will handle
  245. member operators, non member operators, operators imported implicitly or
  246. explicitly, and perform correct overload resolution in all of the above
  247. situations or combinations thereof. */
  248. static struct value *
  249. value_user_defined_cpp_op (gdb::array_view<value *> args, char *oper,
  250. int *static_memfuncp, enum noside noside)
  251. {
  252. struct symbol *symp = NULL;
  253. struct value *valp = NULL;
  254. find_overload_match (args, oper, BOTH /* could be method */,
  255. &args[0] /* objp */,
  256. NULL /* pass NULL symbol since symbol is unknown */,
  257. &valp, &symp, static_memfuncp, 0, noside);
  258. if (valp)
  259. return valp;
  260. if (symp)
  261. {
  262. /* This is a non member function and does not
  263. expect a reference as its first argument
  264. rather the explicit structure. */
  265. args[0] = value_ind (args[0]);
  266. return value_of_variable (symp, 0);
  267. }
  268. error (_("Could not find %s."), oper);
  269. }
  270. /* Lookup user defined operator NAME. Return a value representing the
  271. function, otherwise return NULL. */
  272. static struct value *
  273. value_user_defined_op (struct value **argp, gdb::array_view<value *> args,
  274. char *name, int *static_memfuncp, enum noside noside)
  275. {
  276. struct value *result = NULL;
  277. if (current_language->la_language == language_cplus)
  278. {
  279. result = value_user_defined_cpp_op (args, name, static_memfuncp,
  280. noside);
  281. }
  282. else
  283. result = value_struct_elt (argp, args, name, static_memfuncp,
  284. "structure");
  285. return result;
  286. }
  287. /* We know either arg1 or arg2 is a structure, so try to find the right
  288. user defined function. Create an argument vector that calls
  289. arg1.operator @ (arg1,arg2) and return that value (where '@' is any
  290. binary operator which is legal for GNU C++).
  291. OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
  292. is the opcode saying how to modify it. Otherwise, OTHEROP is
  293. unused. */
  294. struct value *
  295. value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
  296. enum exp_opcode otherop, enum noside noside)
  297. {
  298. char *ptr;
  299. char tstr[13];
  300. int static_memfuncp;
  301. arg1 = coerce_ref (arg1);
  302. arg2 = coerce_ref (arg2);
  303. /* now we know that what we have to do is construct our
  304. arg vector and find the right function to call it with. */
  305. if (check_typedef (value_type (arg1))->code () != TYPE_CODE_STRUCT)
  306. error (_("Can't do that binary op on that type")); /* FIXME be explicit */
  307. value *argvec_storage[3];
  308. gdb::array_view<value *> argvec = argvec_storage;
  309. argvec[1] = value_addr (arg1);
  310. argvec[2] = arg2;
  311. /* Make the right function name up. */
  312. strcpy (tstr, "operator__");
  313. ptr = tstr + 8;
  314. switch (op)
  315. {
  316. case BINOP_ADD:
  317. strcpy (ptr, "+");
  318. break;
  319. case BINOP_SUB:
  320. strcpy (ptr, "-");
  321. break;
  322. case BINOP_MUL:
  323. strcpy (ptr, "*");
  324. break;
  325. case BINOP_DIV:
  326. strcpy (ptr, "/");
  327. break;
  328. case BINOP_REM:
  329. strcpy (ptr, "%");
  330. break;
  331. case BINOP_LSH:
  332. strcpy (ptr, "<<");
  333. break;
  334. case BINOP_RSH:
  335. strcpy (ptr, ">>");
  336. break;
  337. case BINOP_BITWISE_AND:
  338. strcpy (ptr, "&");
  339. break;
  340. case BINOP_BITWISE_IOR:
  341. strcpy (ptr, "|");
  342. break;
  343. case BINOP_BITWISE_XOR:
  344. strcpy (ptr, "^");
  345. break;
  346. case BINOP_LOGICAL_AND:
  347. strcpy (ptr, "&&");
  348. break;
  349. case BINOP_LOGICAL_OR:
  350. strcpy (ptr, "||");
  351. break;
  352. case BINOP_MIN:
  353. strcpy (ptr, "<?");
  354. break;
  355. case BINOP_MAX:
  356. strcpy (ptr, ">?");
  357. break;
  358. case BINOP_ASSIGN:
  359. strcpy (ptr, "=");
  360. break;
  361. case BINOP_ASSIGN_MODIFY:
  362. switch (otherop)
  363. {
  364. case BINOP_ADD:
  365. strcpy (ptr, "+=");
  366. break;
  367. case BINOP_SUB:
  368. strcpy (ptr, "-=");
  369. break;
  370. case BINOP_MUL:
  371. strcpy (ptr, "*=");
  372. break;
  373. case BINOP_DIV:
  374. strcpy (ptr, "/=");
  375. break;
  376. case BINOP_REM:
  377. strcpy (ptr, "%=");
  378. break;
  379. case BINOP_BITWISE_AND:
  380. strcpy (ptr, "&=");
  381. break;
  382. case BINOP_BITWISE_IOR:
  383. strcpy (ptr, "|=");
  384. break;
  385. case BINOP_BITWISE_XOR:
  386. strcpy (ptr, "^=");
  387. break;
  388. case BINOP_MOD: /* invalid */
  389. default:
  390. error (_("Invalid binary operation specified."));
  391. }
  392. break;
  393. case BINOP_SUBSCRIPT:
  394. strcpy (ptr, "[]");
  395. break;
  396. case BINOP_EQUAL:
  397. strcpy (ptr, "==");
  398. break;
  399. case BINOP_NOTEQUAL:
  400. strcpy (ptr, "!=");
  401. break;
  402. case BINOP_LESS:
  403. strcpy (ptr, "<");
  404. break;
  405. case BINOP_GTR:
  406. strcpy (ptr, ">");
  407. break;
  408. case BINOP_GEQ:
  409. strcpy (ptr, ">=");
  410. break;
  411. case BINOP_LEQ:
  412. strcpy (ptr, "<=");
  413. break;
  414. case BINOP_MOD: /* invalid */
  415. default:
  416. error (_("Invalid binary operation specified."));
  417. }
  418. argvec[0] = value_user_defined_op (&arg1, argvec.slice (1), tstr,
  419. &static_memfuncp, noside);
  420. if (argvec[0])
  421. {
  422. if (static_memfuncp)
  423. {
  424. argvec[1] = argvec[0];
  425. argvec = argvec.slice (1);
  426. }
  427. if (value_type (argvec[0])->code () == TYPE_CODE_XMETHOD)
  428. {
  429. /* Static xmethods are not supported yet. */
  430. gdb_assert (static_memfuncp == 0);
  431. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  432. {
  433. struct type *return_type
  434. = result_type_of_xmethod (argvec[0], argvec.slice (1));
  435. if (return_type == NULL)
  436. error (_("Xmethod is missing return type."));
  437. return value_zero (return_type, VALUE_LVAL (arg1));
  438. }
  439. return call_xmethod (argvec[0], argvec.slice (1));
  440. }
  441. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  442. {
  443. struct type *return_type;
  444. return_type
  445. = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
  446. return value_zero (return_type, VALUE_LVAL (arg1));
  447. }
  448. return call_function_by_hand (argvec[0], NULL,
  449. argvec.slice (1, 2 - static_memfuncp));
  450. }
  451. throw_error (NOT_FOUND_ERROR,
  452. _("member function %s not found"), tstr);
  453. }
  454. /* We know that arg1 is a structure, so try to find a unary user
  455. defined operator that matches the operator in question.
  456. Create an argument vector that calls arg1.operator @ (arg1)
  457. and return that value (where '@' is (almost) any unary operator which
  458. is legal for GNU C++). */
  459. struct value *
  460. value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
  461. {
  462. struct gdbarch *gdbarch = value_type (arg1)->arch ();
  463. char *ptr;
  464. char tstr[13], mangle_tstr[13];
  465. int static_memfuncp, nargs;
  466. arg1 = coerce_ref (arg1);
  467. /* now we know that what we have to do is construct our
  468. arg vector and find the right function to call it with. */
  469. if (check_typedef (value_type (arg1))->code () != TYPE_CODE_STRUCT)
  470. error (_("Can't do that unary op on that type")); /* FIXME be explicit */
  471. value *argvec_storage[3];
  472. gdb::array_view<value *> argvec = argvec_storage;
  473. argvec[1] = value_addr (arg1);
  474. argvec[2] = 0;
  475. nargs = 1;
  476. /* Make the right function name up. */
  477. strcpy (tstr, "operator__");
  478. ptr = tstr + 8;
  479. strcpy (mangle_tstr, "__");
  480. switch (op)
  481. {
  482. case UNOP_PREINCREMENT:
  483. strcpy (ptr, "++");
  484. break;
  485. case UNOP_PREDECREMENT:
  486. strcpy (ptr, "--");
  487. break;
  488. case UNOP_POSTINCREMENT:
  489. strcpy (ptr, "++");
  490. argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
  491. nargs ++;
  492. break;
  493. case UNOP_POSTDECREMENT:
  494. strcpy (ptr, "--");
  495. argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
  496. nargs ++;
  497. break;
  498. case UNOP_LOGICAL_NOT:
  499. strcpy (ptr, "!");
  500. break;
  501. case UNOP_COMPLEMENT:
  502. strcpy (ptr, "~");
  503. break;
  504. case UNOP_NEG:
  505. strcpy (ptr, "-");
  506. break;
  507. case UNOP_PLUS:
  508. strcpy (ptr, "+");
  509. break;
  510. case UNOP_IND:
  511. strcpy (ptr, "*");
  512. break;
  513. case STRUCTOP_PTR:
  514. strcpy (ptr, "->");
  515. break;
  516. default:
  517. error (_("Invalid unary operation specified."));
  518. }
  519. argvec[0] = value_user_defined_op (&arg1, argvec.slice (1, nargs), tstr,
  520. &static_memfuncp, noside);
  521. if (argvec[0])
  522. {
  523. if (static_memfuncp)
  524. {
  525. argvec[1] = argvec[0];
  526. argvec = argvec.slice (1);
  527. }
  528. if (value_type (argvec[0])->code () == TYPE_CODE_XMETHOD)
  529. {
  530. /* Static xmethods are not supported yet. */
  531. gdb_assert (static_memfuncp == 0);
  532. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  533. {
  534. struct type *return_type
  535. = result_type_of_xmethod (argvec[0], argvec[1]);
  536. if (return_type == NULL)
  537. error (_("Xmethod is missing return type."));
  538. return value_zero (return_type, VALUE_LVAL (arg1));
  539. }
  540. return call_xmethod (argvec[0], argvec[1]);
  541. }
  542. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  543. {
  544. struct type *return_type;
  545. return_type
  546. = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
  547. return value_zero (return_type, VALUE_LVAL (arg1));
  548. }
  549. return call_function_by_hand (argvec[0], NULL,
  550. argvec.slice (1, nargs));
  551. }
  552. throw_error (NOT_FOUND_ERROR,
  553. _("member function %s not found"), tstr);
  554. }
  555. /* Concatenate two values. One value must be an array; and the other
  556. value must either be an array with the same element type, or be of
  557. the array's element type. */
  558. struct value *
  559. value_concat (struct value *arg1, struct value *arg2)
  560. {
  561. struct type *type1 = check_typedef (value_type (arg1));
  562. struct type *type2 = check_typedef (value_type (arg2));
  563. if (type1->code () != TYPE_CODE_ARRAY && type2->code () != TYPE_CODE_ARRAY)
  564. error ("no array provided to concatenation");
  565. LONGEST low1, high1;
  566. struct type *elttype1 = type1;
  567. if (elttype1->code () == TYPE_CODE_ARRAY)
  568. {
  569. elttype1 = TYPE_TARGET_TYPE (elttype1);
  570. if (!get_array_bounds (type1, &low1, &high1))
  571. error (_("could not determine array bounds on left-hand-side of "
  572. "array concatenation"));
  573. }
  574. else
  575. {
  576. low1 = 0;
  577. high1 = 0;
  578. }
  579. LONGEST low2, high2;
  580. struct type *elttype2 = type2;
  581. if (elttype2->code () == TYPE_CODE_ARRAY)
  582. {
  583. elttype2 = TYPE_TARGET_TYPE (elttype2);
  584. if (!get_array_bounds (type2, &low2, &high2))
  585. error (_("could not determine array bounds on right-hand-side of "
  586. "array concatenation"));
  587. }
  588. else
  589. {
  590. low2 = 0;
  591. high2 = 0;
  592. }
  593. if (!types_equal (elttype1, elttype2))
  594. error (_("concatenation with different element types"));
  595. LONGEST lowbound = current_language->c_style_arrays_p () ? 0 : 1;
  596. LONGEST n_elts = (high1 - low1 + 1) + (high2 - low2 + 1);
  597. struct type *atype = lookup_array_range_type (elttype1,
  598. lowbound,
  599. lowbound + n_elts - 1);
  600. struct value *result = allocate_value (atype);
  601. gdb::array_view<gdb_byte> contents = value_contents_raw (result);
  602. gdb::array_view<const gdb_byte> lhs_contents = value_contents (arg1);
  603. gdb::array_view<const gdb_byte> rhs_contents = value_contents (arg2);
  604. gdb::copy (lhs_contents, contents.slice (0, lhs_contents.size ()));
  605. gdb::copy (rhs_contents, contents.slice (lhs_contents.size ()));
  606. return result;
  607. }
  608. /* Integer exponentiation: V1**V2, where both arguments are
  609. integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
  610. static LONGEST
  611. integer_pow (LONGEST v1, LONGEST v2)
  612. {
  613. if (v2 < 0)
  614. {
  615. if (v1 == 0)
  616. error (_("Attempt to raise 0 to negative power."));
  617. else
  618. return 0;
  619. }
  620. else
  621. {
  622. /* The Russian Peasant's Algorithm. */
  623. LONGEST v;
  624. v = 1;
  625. for (;;)
  626. {
  627. if (v2 & 1L)
  628. v *= v1;
  629. v2 >>= 1;
  630. if (v2 == 0)
  631. return v;
  632. v1 *= v1;
  633. }
  634. }
  635. }
  636. /* Obtain argument values for binary operation, converting from
  637. other types if one of them is not floating point. */
  638. static void
  639. value_args_as_target_float (struct value *arg1, struct value *arg2,
  640. gdb_byte *x, struct type **eff_type_x,
  641. gdb_byte *y, struct type **eff_type_y)
  642. {
  643. struct type *type1, *type2;
  644. type1 = check_typedef (value_type (arg1));
  645. type2 = check_typedef (value_type (arg2));
  646. /* At least one of the arguments must be of floating-point type. */
  647. gdb_assert (is_floating_type (type1) || is_floating_type (type2));
  648. if (is_floating_type (type1) && is_floating_type (type2)
  649. && type1->code () != type2->code ())
  650. /* The DFP extension to the C language does not allow mixing of
  651. * decimal float types with other float types in expressions
  652. * (see WDTR 24732, page 12). */
  653. error (_("Mixing decimal floating types with "
  654. "other floating types is not allowed."));
  655. /* Obtain value of arg1, converting from other types if necessary. */
  656. if (is_floating_type (type1))
  657. {
  658. *eff_type_x = type1;
  659. memcpy (x, value_contents (arg1).data (), TYPE_LENGTH (type1));
  660. }
  661. else if (is_integral_type (type1))
  662. {
  663. *eff_type_x = type2;
  664. if (type1->is_unsigned ())
  665. target_float_from_ulongest (x, *eff_type_x, value_as_long (arg1));
  666. else
  667. target_float_from_longest (x, *eff_type_x, value_as_long (arg1));
  668. }
  669. else
  670. error (_("Don't know how to convert from %s to %s."), type1->name (),
  671. type2->name ());
  672. /* Obtain value of arg2, converting from other types if necessary. */
  673. if (is_floating_type (type2))
  674. {
  675. *eff_type_y = type2;
  676. memcpy (y, value_contents (arg2).data (), TYPE_LENGTH (type2));
  677. }
  678. else if (is_integral_type (type2))
  679. {
  680. *eff_type_y = type1;
  681. if (type2->is_unsigned ())
  682. target_float_from_ulongest (y, *eff_type_y, value_as_long (arg2));
  683. else
  684. target_float_from_longest (y, *eff_type_y, value_as_long (arg2));
  685. }
  686. else
  687. error (_("Don't know how to convert from %s to %s."), type1->name (),
  688. type2->name ());
  689. }
  690. /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
  691. perform the binary operation OP on these two operands, and return
  692. the resulting value (also as a fixed point). */
  693. static struct value *
  694. fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  695. {
  696. struct type *type1 = check_typedef (value_type (arg1));
  697. struct type *type2 = check_typedef (value_type (arg2));
  698. const struct language_defn *language = current_language;
  699. struct gdbarch *gdbarch = type1->arch ();
  700. struct value *val;
  701. gdb_mpq v1, v2, res;
  702. gdb_assert (is_fixed_point_type (type1) || is_fixed_point_type (type2));
  703. if (op == BINOP_MUL || op == BINOP_DIV)
  704. {
  705. v1 = value_to_gdb_mpq (arg1);
  706. v2 = value_to_gdb_mpq (arg2);
  707. /* The code below uses TYPE1 for the result type, so make sure
  708. it is set properly. */
  709. if (!is_fixed_point_type (type1))
  710. type1 = type2;
  711. }
  712. else
  713. {
  714. if (!is_fixed_point_type (type1))
  715. {
  716. arg1 = value_cast (type2, arg1);
  717. type1 = type2;
  718. }
  719. if (!is_fixed_point_type (type2))
  720. {
  721. arg2 = value_cast (type1, arg2);
  722. type2 = type1;
  723. }
  724. v1.read_fixed_point (value_contents (arg1),
  725. type_byte_order (type1), type1->is_unsigned (),
  726. type1->fixed_point_scaling_factor ());
  727. v2.read_fixed_point (value_contents (arg2),
  728. type_byte_order (type2), type2->is_unsigned (),
  729. type2->fixed_point_scaling_factor ());
  730. }
  731. auto fixed_point_to_value = [type1] (const gdb_mpq &fp)
  732. {
  733. value *fp_val = allocate_value (type1);
  734. fp.write_fixed_point
  735. (value_contents_raw (fp_val),
  736. type_byte_order (type1),
  737. type1->is_unsigned (),
  738. type1->fixed_point_scaling_factor ());
  739. return fp_val;
  740. };
  741. switch (op)
  742. {
  743. case BINOP_ADD:
  744. mpq_add (res.val, v1.val, v2.val);
  745. val = fixed_point_to_value (res);
  746. break;
  747. case BINOP_SUB:
  748. mpq_sub (res.val, v1.val, v2.val);
  749. val = fixed_point_to_value (res);
  750. break;
  751. case BINOP_MIN:
  752. val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) < 0 ? v1 : v2);
  753. break;
  754. case BINOP_MAX:
  755. val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) > 0 ? v1 : v2);
  756. break;
  757. case BINOP_MUL:
  758. mpq_mul (res.val, v1.val, v2.val);
  759. val = fixed_point_to_value (res);
  760. break;
  761. case BINOP_DIV:
  762. if (mpq_sgn (v2.val) == 0)
  763. error (_("Division by zero"));
  764. mpq_div (res.val, v1.val, v2.val);
  765. val = fixed_point_to_value (res);
  766. break;
  767. case BINOP_EQUAL:
  768. val = value_from_ulongest (language_bool_type (language, gdbarch),
  769. mpq_cmp (v1.val, v2.val) == 0 ? 1 : 0);
  770. break;
  771. case BINOP_LESS:
  772. val = value_from_ulongest (language_bool_type (language, gdbarch),
  773. mpq_cmp (v1.val, v2.val) < 0 ? 1 : 0);
  774. break;
  775. default:
  776. error (_("Integer-only operation on fixed point number."));
  777. }
  778. return val;
  779. }
  780. /* A helper function that finds the type to use for a binary operation
  781. involving TYPE1 and TYPE2. */
  782. static struct type *
  783. promotion_type (struct type *type1, struct type *type2)
  784. {
  785. struct type *result_type;
  786. if (is_floating_type (type1) || is_floating_type (type2))
  787. {
  788. /* If only one type is floating-point, use its type.
  789. Otherwise use the bigger type. */
  790. if (!is_floating_type (type1))
  791. result_type = type2;
  792. else if (!is_floating_type (type2))
  793. result_type = type1;
  794. else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
  795. result_type = type2;
  796. else
  797. result_type = type1;
  798. }
  799. else
  800. {
  801. /* Integer types. */
  802. if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
  803. result_type = type1;
  804. else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
  805. result_type = type2;
  806. else if (type1->is_unsigned ())
  807. result_type = type1;
  808. else if (type2->is_unsigned ())
  809. result_type = type2;
  810. else
  811. result_type = type1;
  812. }
  813. return result_type;
  814. }
  815. static struct value *scalar_binop (struct value *arg1, struct value *arg2,
  816. enum exp_opcode op);
  817. /* Perform a binary operation on complex operands. */
  818. static struct value *
  819. complex_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  820. {
  821. struct type *arg1_type = check_typedef (value_type (arg1));
  822. struct type *arg2_type = check_typedef (value_type (arg2));
  823. struct value *arg1_real, *arg1_imag, *arg2_real, *arg2_imag;
  824. if (arg1_type->code () == TYPE_CODE_COMPLEX)
  825. {
  826. arg1_real = value_real_part (arg1);
  827. arg1_imag = value_imaginary_part (arg1);
  828. }
  829. else
  830. {
  831. arg1_real = arg1;
  832. arg1_imag = value_zero (arg1_type, not_lval);
  833. }
  834. if (arg2_type->code () == TYPE_CODE_COMPLEX)
  835. {
  836. arg2_real = value_real_part (arg2);
  837. arg2_imag = value_imaginary_part (arg2);
  838. }
  839. else
  840. {
  841. arg2_real = arg2;
  842. arg2_imag = value_zero (arg2_type, not_lval);
  843. }
  844. struct type *comp_type = promotion_type (value_type (arg1_real),
  845. value_type (arg2_real));
  846. if (!can_create_complex_type (comp_type))
  847. error (_("Argument to complex arithmetic operation not supported."));
  848. arg1_real = value_cast (comp_type, arg1_real);
  849. arg1_imag = value_cast (comp_type, arg1_imag);
  850. arg2_real = value_cast (comp_type, arg2_real);
  851. arg2_imag = value_cast (comp_type, arg2_imag);
  852. struct type *result_type = init_complex_type (nullptr, comp_type);
  853. struct value *result_real, *result_imag;
  854. switch (op)
  855. {
  856. case BINOP_ADD:
  857. case BINOP_SUB:
  858. result_real = scalar_binop (arg1_real, arg2_real, op);
  859. result_imag = scalar_binop (arg1_imag, arg2_imag, op);
  860. break;
  861. case BINOP_MUL:
  862. {
  863. struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
  864. struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
  865. result_real = scalar_binop (x1, x2, BINOP_SUB);
  866. x1 = scalar_binop (arg1_real, arg2_imag, op);
  867. x2 = scalar_binop (arg1_imag, arg2_real, op);
  868. result_imag = scalar_binop (x1, x2, BINOP_ADD);
  869. }
  870. break;
  871. case BINOP_DIV:
  872. {
  873. if (arg2_type->code () == TYPE_CODE_COMPLEX)
  874. {
  875. struct value *conjugate = value_complement (arg2);
  876. /* We have to reconstruct ARG1, in case the type was
  877. promoted. */
  878. arg1 = value_literal_complex (arg1_real, arg1_imag, result_type);
  879. struct value *numerator = scalar_binop (arg1, conjugate,
  880. BINOP_MUL);
  881. arg1_real = value_real_part (numerator);
  882. arg1_imag = value_imaginary_part (numerator);
  883. struct value *x1 = scalar_binop (arg2_real, arg2_real, BINOP_MUL);
  884. struct value *x2 = scalar_binop (arg2_imag, arg2_imag, BINOP_MUL);
  885. arg2_real = scalar_binop (x1, x2, BINOP_ADD);
  886. }
  887. result_real = scalar_binop (arg1_real, arg2_real, op);
  888. result_imag = scalar_binop (arg1_imag, arg2_real, op);
  889. }
  890. break;
  891. case BINOP_EQUAL:
  892. case BINOP_NOTEQUAL:
  893. {
  894. struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
  895. struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
  896. LONGEST v1 = value_as_long (x1);
  897. LONGEST v2 = value_as_long (x2);
  898. if (op == BINOP_EQUAL)
  899. v1 = v1 && v2;
  900. else
  901. v1 = v1 || v2;
  902. return value_from_longest (value_type (x1), v1);
  903. }
  904. break;
  905. default:
  906. error (_("Invalid binary operation on numbers."));
  907. }
  908. return value_literal_complex (result_real, result_imag, result_type);
  909. }
  910. /* Return the type's length in bits. */
  911. static int
  912. type_length_bits (type *type)
  913. {
  914. int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
  915. return unit_size * 8 * TYPE_LENGTH (type);
  916. }
  917. /* Check whether the RHS value of a shift is valid in C/C++ semantics.
  918. SHIFT_COUNT is the shift amount, SHIFT_COUNT_TYPE is the type of
  919. the shift count value, used to determine whether the type is
  920. signed, and RESULT_TYPE is the result type. This is used to avoid
  921. both negative and too-large shift amounts, which are undefined, and
  922. would crash a GDB built with UBSan. Depending on the current
  923. language, if the shift is not valid, this either warns and returns
  924. false, or errors out. Returns true if valid. */
  925. static bool
  926. check_valid_shift_count (int op, type *result_type,
  927. type *shift_count_type, ULONGEST shift_count)
  928. {
  929. if (!shift_count_type->is_unsigned () && (LONGEST) shift_count < 0)
  930. {
  931. auto error_or_warning = [] (const char *msg)
  932. {
  933. /* Shifts by a negative amount are always an error in Go. Other
  934. languages are more permissive and their compilers just warn or
  935. have modes to disable the errors. */
  936. if (current_language->la_language == language_go)
  937. error (("%s"), msg);
  938. else
  939. warning (("%s"), msg);
  940. };
  941. if (op == BINOP_RSH)
  942. error_or_warning (_("right shift count is negative"));
  943. else
  944. error_or_warning (_("left shift count is negative"));
  945. return false;
  946. }
  947. if (shift_count >= type_length_bits (result_type))
  948. {
  949. /* In Go, shifting by large amounts is defined. Be silent and
  950. still return false, as the caller's error path does the right
  951. thing for Go. */
  952. if (current_language->la_language != language_go)
  953. {
  954. if (op == BINOP_RSH)
  955. warning (_("right shift count >= width of type"));
  956. else
  957. warning (_("left shift count >= width of type"));
  958. }
  959. return false;
  960. }
  961. return true;
  962. }
  963. /* Perform a binary operation on two operands which have reasonable
  964. representations as integers or floats. This includes booleans,
  965. characters, integers, or floats.
  966. Does not support addition and subtraction on pointers;
  967. use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
  968. static struct value *
  969. scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  970. {
  971. struct value *val;
  972. struct type *type1, *type2, *result_type;
  973. arg1 = coerce_ref (arg1);
  974. arg2 = coerce_ref (arg2);
  975. type1 = check_typedef (value_type (arg1));
  976. type2 = check_typedef (value_type (arg2));
  977. if (type1->code () == TYPE_CODE_COMPLEX
  978. || type2->code () == TYPE_CODE_COMPLEX)
  979. return complex_binop (arg1, arg2, op);
  980. if ((!is_floating_value (arg1)
  981. && !is_integral_type (type1)
  982. && !is_fixed_point_type (type1))
  983. || (!is_floating_value (arg2)
  984. && !is_integral_type (type2)
  985. && !is_fixed_point_type (type2)))
  986. error (_("Argument to arithmetic operation not a number or boolean."));
  987. if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
  988. return fixed_point_binop (arg1, arg2, op);
  989. if (is_floating_type (type1) || is_floating_type (type2))
  990. {
  991. result_type = promotion_type (type1, type2);
  992. val = allocate_value (result_type);
  993. struct type *eff_type_v1, *eff_type_v2;
  994. gdb::byte_vector v1, v2;
  995. v1.resize (TYPE_LENGTH (result_type));
  996. v2.resize (TYPE_LENGTH (result_type));
  997. value_args_as_target_float (arg1, arg2,
  998. v1.data (), &eff_type_v1,
  999. v2.data (), &eff_type_v2);
  1000. target_float_binop (op, v1.data (), eff_type_v1,
  1001. v2.data (), eff_type_v2,
  1002. value_contents_raw (val).data (), result_type);
  1003. }
  1004. else if (type1->code () == TYPE_CODE_BOOL
  1005. || type2->code () == TYPE_CODE_BOOL)
  1006. {
  1007. LONGEST v1, v2, v = 0;
  1008. v1 = value_as_long (arg1);
  1009. v2 = value_as_long (arg2);
  1010. switch (op)
  1011. {
  1012. case BINOP_BITWISE_AND:
  1013. v = v1 & v2;
  1014. break;
  1015. case BINOP_BITWISE_IOR:
  1016. v = v1 | v2;
  1017. break;
  1018. case BINOP_BITWISE_XOR:
  1019. v = v1 ^ v2;
  1020. break;
  1021. case BINOP_EQUAL:
  1022. v = v1 == v2;
  1023. break;
  1024. case BINOP_NOTEQUAL:
  1025. v = v1 != v2;
  1026. break;
  1027. default:
  1028. error (_("Invalid operation on booleans."));
  1029. }
  1030. result_type = type1;
  1031. val = allocate_value (result_type);
  1032. store_signed_integer (value_contents_raw (val).data (),
  1033. TYPE_LENGTH (result_type),
  1034. type_byte_order (result_type),
  1035. v);
  1036. }
  1037. else
  1038. /* Integral operations here. */
  1039. {
  1040. /* Determine type length of the result, and if the operation should
  1041. be done unsigned. For exponentiation and shift operators,
  1042. use the length and type of the left operand. Otherwise,
  1043. use the signedness of the operand with the greater length.
  1044. If both operands are of equal length, use unsigned operation
  1045. if one of the operands is unsigned. */
  1046. if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
  1047. result_type = type1;
  1048. else
  1049. result_type = promotion_type (type1, type2);
  1050. if (result_type->is_unsigned ())
  1051. {
  1052. LONGEST v2_signed = value_as_long (arg2);
  1053. ULONGEST v1, v2, v = 0;
  1054. v1 = (ULONGEST) value_as_long (arg1);
  1055. v2 = (ULONGEST) v2_signed;
  1056. switch (op)
  1057. {
  1058. case BINOP_ADD:
  1059. v = v1 + v2;
  1060. break;
  1061. case BINOP_SUB:
  1062. v = v1 - v2;
  1063. break;
  1064. case BINOP_MUL:
  1065. v = v1 * v2;
  1066. break;
  1067. case BINOP_DIV:
  1068. case BINOP_INTDIV:
  1069. if (v2 != 0)
  1070. v = v1 / v2;
  1071. else
  1072. error (_("Division by zero"));
  1073. break;
  1074. case BINOP_EXP:
  1075. v = uinteger_pow (v1, v2_signed);
  1076. break;
  1077. case BINOP_REM:
  1078. if (v2 != 0)
  1079. v = v1 % v2;
  1080. else
  1081. error (_("Division by zero"));
  1082. break;
  1083. case BINOP_MOD:
  1084. /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
  1085. v1 mod 0 has a defined value, v1. */
  1086. if (v2 == 0)
  1087. {
  1088. v = v1;
  1089. }
  1090. else
  1091. {
  1092. v = v1 / v2;
  1093. /* Note floor(v1/v2) == v1/v2 for unsigned. */
  1094. v = v1 - (v2 * v);
  1095. }
  1096. break;
  1097. case BINOP_LSH:
  1098. if (!check_valid_shift_count (op, result_type, type2, v2))
  1099. v = 0;
  1100. else
  1101. v = v1 << v2;
  1102. break;
  1103. case BINOP_RSH:
  1104. if (!check_valid_shift_count (op, result_type, type2, v2))
  1105. v = 0;
  1106. else
  1107. v = v1 >> v2;
  1108. break;
  1109. case BINOP_BITWISE_AND:
  1110. v = v1 & v2;
  1111. break;
  1112. case BINOP_BITWISE_IOR:
  1113. v = v1 | v2;
  1114. break;
  1115. case BINOP_BITWISE_XOR:
  1116. v = v1 ^ v2;
  1117. break;
  1118. case BINOP_LOGICAL_AND:
  1119. v = v1 && v2;
  1120. break;
  1121. case BINOP_LOGICAL_OR:
  1122. v = v1 || v2;
  1123. break;
  1124. case BINOP_MIN:
  1125. v = v1 < v2 ? v1 : v2;
  1126. break;
  1127. case BINOP_MAX:
  1128. v = v1 > v2 ? v1 : v2;
  1129. break;
  1130. case BINOP_EQUAL:
  1131. v = v1 == v2;
  1132. break;
  1133. case BINOP_NOTEQUAL:
  1134. v = v1 != v2;
  1135. break;
  1136. case BINOP_LESS:
  1137. v = v1 < v2;
  1138. break;
  1139. case BINOP_GTR:
  1140. v = v1 > v2;
  1141. break;
  1142. case BINOP_LEQ:
  1143. v = v1 <= v2;
  1144. break;
  1145. case BINOP_GEQ:
  1146. v = v1 >= v2;
  1147. break;
  1148. default:
  1149. error (_("Invalid binary operation on numbers."));
  1150. }
  1151. val = allocate_value (result_type);
  1152. store_unsigned_integer (value_contents_raw (val).data (),
  1153. TYPE_LENGTH (value_type (val)),
  1154. type_byte_order (result_type),
  1155. v);
  1156. }
  1157. else
  1158. {
  1159. LONGEST v1, v2, v = 0;
  1160. v1 = value_as_long (arg1);
  1161. v2 = value_as_long (arg2);
  1162. switch (op)
  1163. {
  1164. case BINOP_ADD:
  1165. v = v1 + v2;
  1166. break;
  1167. case BINOP_SUB:
  1168. v = v1 - v2;
  1169. break;
  1170. case BINOP_MUL:
  1171. v = v1 * v2;
  1172. break;
  1173. case BINOP_DIV:
  1174. case BINOP_INTDIV:
  1175. if (v2 != 0)
  1176. v = v1 / v2;
  1177. else
  1178. error (_("Division by zero"));
  1179. break;
  1180. case BINOP_EXP:
  1181. v = integer_pow (v1, v2);
  1182. break;
  1183. case BINOP_REM:
  1184. if (v2 != 0)
  1185. v = v1 % v2;
  1186. else
  1187. error (_("Division by zero"));
  1188. break;
  1189. case BINOP_MOD:
  1190. /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
  1191. X mod 0 has a defined value, X. */
  1192. if (v2 == 0)
  1193. {
  1194. v = v1;
  1195. }
  1196. else
  1197. {
  1198. v = v1 / v2;
  1199. /* Compute floor. */
  1200. if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
  1201. {
  1202. v--;
  1203. }
  1204. v = v1 - (v2 * v);
  1205. }
  1206. break;
  1207. case BINOP_LSH:
  1208. if (!check_valid_shift_count (op, result_type, type2, v2))
  1209. v = 0;
  1210. else
  1211. {
  1212. /* Cast to unsigned to avoid undefined behavior on
  1213. signed shift overflow (unless C++20 or later),
  1214. which would crash GDB when built with UBSan.
  1215. Note we don't warn on left signed shift overflow,
  1216. because starting with C++20, that is actually
  1217. defined behavior. Also, note GDB assumes 2's
  1218. complement throughout. */
  1219. v = (ULONGEST) v1 << v2;
  1220. }
  1221. break;
  1222. case BINOP_RSH:
  1223. if (!check_valid_shift_count (op, result_type, type2, v2))
  1224. {
  1225. /* Pretend the too-large shift was decomposed in a
  1226. number of smaller shifts. An arithmetic signed
  1227. right shift of a negative number always yields -1
  1228. with such semantics. This is the right thing to
  1229. do for Go, and we might as well do it for
  1230. languages where it is undefined. Also, pretend a
  1231. shift by a negative number was a shift by the
  1232. negative number cast to unsigned, which is the
  1233. same as shifting by a too-large number. */
  1234. if (v1 < 0)
  1235. v = -1;
  1236. else
  1237. v = 0;
  1238. }
  1239. else
  1240. v = v1 >> v2;
  1241. break;
  1242. case BINOP_BITWISE_AND:
  1243. v = v1 & v2;
  1244. break;
  1245. case BINOP_BITWISE_IOR:
  1246. v = v1 | v2;
  1247. break;
  1248. case BINOP_BITWISE_XOR:
  1249. v = v1 ^ v2;
  1250. break;
  1251. case BINOP_LOGICAL_AND:
  1252. v = v1 && v2;
  1253. break;
  1254. case BINOP_LOGICAL_OR:
  1255. v = v1 || v2;
  1256. break;
  1257. case BINOP_MIN:
  1258. v = v1 < v2 ? v1 : v2;
  1259. break;
  1260. case BINOP_MAX:
  1261. v = v1 > v2 ? v1 : v2;
  1262. break;
  1263. case BINOP_EQUAL:
  1264. v = v1 == v2;
  1265. break;
  1266. case BINOP_NOTEQUAL:
  1267. v = v1 != v2;
  1268. break;
  1269. case BINOP_LESS:
  1270. v = v1 < v2;
  1271. break;
  1272. case BINOP_GTR:
  1273. v = v1 > v2;
  1274. break;
  1275. case BINOP_LEQ:
  1276. v = v1 <= v2;
  1277. break;
  1278. case BINOP_GEQ:
  1279. v = v1 >= v2;
  1280. break;
  1281. default:
  1282. error (_("Invalid binary operation on numbers."));
  1283. }
  1284. val = allocate_value (result_type);
  1285. store_signed_integer (value_contents_raw (val).data (),
  1286. TYPE_LENGTH (value_type (val)),
  1287. type_byte_order (result_type),
  1288. v);
  1289. }
  1290. }
  1291. return val;
  1292. }
  1293. /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
  1294. replicating SCALAR_VALUE for each element of the vector. Only scalar
  1295. types that can be cast to the type of one element of the vector are
  1296. acceptable. The newly created vector value is returned upon success,
  1297. otherwise an error is thrown. */
  1298. struct value *
  1299. value_vector_widen (struct value *scalar_value, struct type *vector_type)
  1300. {
  1301. /* Widen the scalar to a vector. */
  1302. struct type *eltype, *scalar_type;
  1303. struct value *elval;
  1304. LONGEST low_bound, high_bound;
  1305. int i;
  1306. vector_type = check_typedef (vector_type);
  1307. gdb_assert (vector_type->code () == TYPE_CODE_ARRAY
  1308. && vector_type->is_vector ());
  1309. if (!get_array_bounds (vector_type, &low_bound, &high_bound))
  1310. error (_("Could not determine the vector bounds"));
  1311. eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
  1312. elval = value_cast (eltype, scalar_value);
  1313. scalar_type = check_typedef (value_type (scalar_value));
  1314. /* If we reduced the length of the scalar then check we didn't loose any
  1315. important bits. */
  1316. if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
  1317. && !value_equal (elval, scalar_value))
  1318. error (_("conversion of scalar to vector involves truncation"));
  1319. value *val = allocate_value (vector_type);
  1320. gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
  1321. int elt_len = TYPE_LENGTH (eltype);
  1322. for (i = 0; i < high_bound - low_bound + 1; i++)
  1323. /* Duplicate the contents of elval into the destination vector. */
  1324. copy (value_contents_all (elval),
  1325. val_contents.slice (i * elt_len, elt_len));
  1326. return val;
  1327. }
  1328. /* Performs a binary operation on two vector operands by calling scalar_binop
  1329. for each pair of vector components. */
  1330. static struct value *
  1331. vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
  1332. {
  1333. struct type *type1, *type2, *eltype1, *eltype2;
  1334. int t1_is_vec, t2_is_vec, elsize, i;
  1335. LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
  1336. type1 = check_typedef (value_type (val1));
  1337. type2 = check_typedef (value_type (val2));
  1338. t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
  1339. && type1->is_vector ()) ? 1 : 0;
  1340. t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
  1341. && type2->is_vector ()) ? 1 : 0;
  1342. if (!t1_is_vec || !t2_is_vec)
  1343. error (_("Vector operations are only supported among vectors"));
  1344. if (!get_array_bounds (type1, &low_bound1, &high_bound1)
  1345. || !get_array_bounds (type2, &low_bound2, &high_bound2))
  1346. error (_("Could not determine the vector bounds"));
  1347. eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
  1348. eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
  1349. elsize = TYPE_LENGTH (eltype1);
  1350. if (eltype1->code () != eltype2->code ()
  1351. || elsize != TYPE_LENGTH (eltype2)
  1352. || eltype1->is_unsigned () != eltype2->is_unsigned ()
  1353. || low_bound1 != low_bound2 || high_bound1 != high_bound2)
  1354. error (_("Cannot perform operation on vectors with different types"));
  1355. value *val = allocate_value (type1);
  1356. gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
  1357. value *mark = value_mark ();
  1358. for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
  1359. {
  1360. value *tmp = value_binop (value_subscript (val1, i),
  1361. value_subscript (val2, i), op);
  1362. copy (value_contents_all (tmp),
  1363. val_contents.slice (i * elsize, elsize));
  1364. }
  1365. value_free_to_mark (mark);
  1366. return val;
  1367. }
  1368. /* Perform a binary operation on two operands. */
  1369. struct value *
  1370. value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  1371. {
  1372. struct value *val;
  1373. struct type *type1 = check_typedef (value_type (arg1));
  1374. struct type *type2 = check_typedef (value_type (arg2));
  1375. int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
  1376. && type1->is_vector ());
  1377. int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
  1378. && type2->is_vector ());
  1379. if (!t1_is_vec && !t2_is_vec)
  1380. val = scalar_binop (arg1, arg2, op);
  1381. else if (t1_is_vec && t2_is_vec)
  1382. val = vector_binop (arg1, arg2, op);
  1383. else
  1384. {
  1385. /* Widen the scalar operand to a vector. */
  1386. struct value **v = t1_is_vec ? &arg2 : &arg1;
  1387. struct type *t = t1_is_vec ? type2 : type1;
  1388. if (t->code () != TYPE_CODE_FLT
  1389. && t->code () != TYPE_CODE_DECFLOAT
  1390. && !is_integral_type (t))
  1391. error (_("Argument to operation not a number or boolean."));
  1392. /* Replicate the scalar value to make a vector value. */
  1393. *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
  1394. val = vector_binop (arg1, arg2, op);
  1395. }
  1396. return val;
  1397. }
  1398. /* See value.h. */
  1399. bool
  1400. value_logical_not (struct value *arg1)
  1401. {
  1402. int len;
  1403. const gdb_byte *p;
  1404. struct type *type1;
  1405. arg1 = coerce_array (arg1);
  1406. type1 = check_typedef (value_type (arg1));
  1407. if (is_floating_value (arg1))
  1408. return target_float_is_zero (value_contents (arg1).data (), type1);
  1409. len = TYPE_LENGTH (type1);
  1410. p = value_contents (arg1).data ();
  1411. while (--len >= 0)
  1412. {
  1413. if (*p++)
  1414. break;
  1415. }
  1416. return len < 0;
  1417. }
  1418. /* Perform a comparison on two string values (whose content are not
  1419. necessarily null terminated) based on their length. */
  1420. static int
  1421. value_strcmp (struct value *arg1, struct value *arg2)
  1422. {
  1423. int len1 = TYPE_LENGTH (value_type (arg1));
  1424. int len2 = TYPE_LENGTH (value_type (arg2));
  1425. const gdb_byte *s1 = value_contents (arg1).data ();
  1426. const gdb_byte *s2 = value_contents (arg2).data ();
  1427. int i, len = len1 < len2 ? len1 : len2;
  1428. for (i = 0; i < len; i++)
  1429. {
  1430. if (s1[i] < s2[i])
  1431. return -1;
  1432. else if (s1[i] > s2[i])
  1433. return 1;
  1434. else
  1435. continue;
  1436. }
  1437. if (len1 < len2)
  1438. return -1;
  1439. else if (len1 > len2)
  1440. return 1;
  1441. else
  1442. return 0;
  1443. }
  1444. /* Simulate the C operator == by returning a 1
  1445. iff ARG1 and ARG2 have equal contents. */
  1446. int
  1447. value_equal (struct value *arg1, struct value *arg2)
  1448. {
  1449. int len;
  1450. const gdb_byte *p1;
  1451. const gdb_byte *p2;
  1452. struct type *type1, *type2;
  1453. enum type_code code1;
  1454. enum type_code code2;
  1455. int is_int1, is_int2;
  1456. arg1 = coerce_array (arg1);
  1457. arg2 = coerce_array (arg2);
  1458. type1 = check_typedef (value_type (arg1));
  1459. type2 = check_typedef (value_type (arg2));
  1460. code1 = type1->code ();
  1461. code2 = type2->code ();
  1462. is_int1 = is_integral_type (type1);
  1463. is_int2 = is_integral_type (type2);
  1464. if (is_int1 && is_int2)
  1465. return longest_to_int (value_as_long (value_binop (arg1, arg2,
  1466. BINOP_EQUAL)));
  1467. else if ((is_floating_value (arg1) || is_int1)
  1468. && (is_floating_value (arg2) || is_int2))
  1469. {
  1470. struct type *eff_type_v1, *eff_type_v2;
  1471. gdb::byte_vector v1, v2;
  1472. v1.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
  1473. v2.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
  1474. value_args_as_target_float (arg1, arg2,
  1475. v1.data (), &eff_type_v1,
  1476. v2.data (), &eff_type_v2);
  1477. return target_float_compare (v1.data (), eff_type_v1,
  1478. v2.data (), eff_type_v2) == 0;
  1479. }
  1480. /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  1481. is bigger. */
  1482. else if (code1 == TYPE_CODE_PTR && is_int2)
  1483. return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
  1484. else if (code2 == TYPE_CODE_PTR && is_int1)
  1485. return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
  1486. else if (code1 == code2
  1487. && ((len = (int) TYPE_LENGTH (type1))
  1488. == (int) TYPE_LENGTH (type2)))
  1489. {
  1490. p1 = value_contents (arg1).data ();
  1491. p2 = value_contents (arg2).data ();
  1492. while (--len >= 0)
  1493. {
  1494. if (*p1++ != *p2++)
  1495. break;
  1496. }
  1497. return len < 0;
  1498. }
  1499. else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
  1500. {
  1501. return value_strcmp (arg1, arg2) == 0;
  1502. }
  1503. else
  1504. error (_("Invalid type combination in equality test."));
  1505. }
  1506. /* Compare values based on their raw contents. Useful for arrays since
  1507. value_equal coerces them to pointers, thus comparing just the address
  1508. of the array instead of its contents. */
  1509. int
  1510. value_equal_contents (struct value *arg1, struct value *arg2)
  1511. {
  1512. struct type *type1, *type2;
  1513. type1 = check_typedef (value_type (arg1));
  1514. type2 = check_typedef (value_type (arg2));
  1515. return (type1->code () == type2->code ()
  1516. && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
  1517. && memcmp (value_contents (arg1).data (),
  1518. value_contents (arg2).data (),
  1519. TYPE_LENGTH (type1)) == 0);
  1520. }
  1521. /* Simulate the C operator < by returning 1
  1522. iff ARG1's contents are less than ARG2's. */
  1523. int
  1524. value_less (struct value *arg1, struct value *arg2)
  1525. {
  1526. enum type_code code1;
  1527. enum type_code code2;
  1528. struct type *type1, *type2;
  1529. int is_int1, is_int2;
  1530. arg1 = coerce_array (arg1);
  1531. arg2 = coerce_array (arg2);
  1532. type1 = check_typedef (value_type (arg1));
  1533. type2 = check_typedef (value_type (arg2));
  1534. code1 = type1->code ();
  1535. code2 = type2->code ();
  1536. is_int1 = is_integral_type (type1);
  1537. is_int2 = is_integral_type (type2);
  1538. if ((is_int1 && is_int2)
  1539. || (is_fixed_point_type (type1) && is_fixed_point_type (type2)))
  1540. return longest_to_int (value_as_long (value_binop (arg1, arg2,
  1541. BINOP_LESS)));
  1542. else if ((is_floating_value (arg1) || is_int1)
  1543. && (is_floating_value (arg2) || is_int2))
  1544. {
  1545. struct type *eff_type_v1, *eff_type_v2;
  1546. gdb::byte_vector v1, v2;
  1547. v1.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
  1548. v2.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
  1549. value_args_as_target_float (arg1, arg2,
  1550. v1.data (), &eff_type_v1,
  1551. v2.data (), &eff_type_v2);
  1552. return target_float_compare (v1.data (), eff_type_v1,
  1553. v2.data (), eff_type_v2) == -1;
  1554. }
  1555. else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  1556. return value_as_address (arg1) < value_as_address (arg2);
  1557. /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  1558. is bigger. */
  1559. else if (code1 == TYPE_CODE_PTR && is_int2)
  1560. return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
  1561. else if (code2 == TYPE_CODE_PTR && is_int1)
  1562. return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
  1563. else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
  1564. return value_strcmp (arg1, arg2) < 0;
  1565. else
  1566. {
  1567. error (_("Invalid type combination in ordering comparison."));
  1568. return 0;
  1569. }
  1570. }
  1571. /* The unary operators +, - and ~. They free the argument ARG1. */
  1572. struct value *
  1573. value_pos (struct value *arg1)
  1574. {
  1575. struct type *type;
  1576. arg1 = coerce_ref (arg1);
  1577. type = check_typedef (value_type (arg1));
  1578. if (is_integral_type (type) || is_floating_value (arg1)
  1579. || (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  1580. || type->code () == TYPE_CODE_COMPLEX)
  1581. return value_from_contents (type, value_contents (arg1).data ());
  1582. else
  1583. error (_("Argument to positive operation not a number."));
  1584. }
  1585. struct value *
  1586. value_neg (struct value *arg1)
  1587. {
  1588. struct type *type;
  1589. arg1 = coerce_ref (arg1);
  1590. type = check_typedef (value_type (arg1));
  1591. if (is_integral_type (type) || is_floating_type (type))
  1592. return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
  1593. else if (is_fixed_point_type (type))
  1594. return value_binop (value_zero (type, not_lval), arg1, BINOP_SUB);
  1595. else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  1596. {
  1597. struct value *val = allocate_value (type);
  1598. struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  1599. int i;
  1600. LONGEST low_bound, high_bound;
  1601. if (!get_array_bounds (type, &low_bound, &high_bound))
  1602. error (_("Could not determine the vector bounds"));
  1603. gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
  1604. int elt_len = TYPE_LENGTH (eltype);
  1605. for (i = 0; i < high_bound - low_bound + 1; i++)
  1606. {
  1607. value *tmp = value_neg (value_subscript (arg1, i));
  1608. copy (value_contents_all (tmp),
  1609. val_contents.slice (i * elt_len, elt_len));
  1610. }
  1611. return val;
  1612. }
  1613. else if (type->code () == TYPE_CODE_COMPLEX)
  1614. {
  1615. struct value *real = value_real_part (arg1);
  1616. struct value *imag = value_imaginary_part (arg1);
  1617. real = value_neg (real);
  1618. imag = value_neg (imag);
  1619. return value_literal_complex (real, imag, type);
  1620. }
  1621. else
  1622. error (_("Argument to negate operation not a number."));
  1623. }
  1624. struct value *
  1625. value_complement (struct value *arg1)
  1626. {
  1627. struct type *type;
  1628. struct value *val;
  1629. arg1 = coerce_ref (arg1);
  1630. type = check_typedef (value_type (arg1));
  1631. if (is_integral_type (type))
  1632. val = value_from_longest (type, ~value_as_long (arg1));
  1633. else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  1634. {
  1635. struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  1636. int i;
  1637. LONGEST low_bound, high_bound;
  1638. if (!get_array_bounds (type, &low_bound, &high_bound))
  1639. error (_("Could not determine the vector bounds"));
  1640. val = allocate_value (type);
  1641. gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
  1642. int elt_len = TYPE_LENGTH (eltype);
  1643. for (i = 0; i < high_bound - low_bound + 1; i++)
  1644. {
  1645. value *tmp = value_complement (value_subscript (arg1, i));
  1646. copy (value_contents_all (tmp),
  1647. val_contents.slice (i * elt_len, elt_len));
  1648. }
  1649. }
  1650. else if (type->code () == TYPE_CODE_COMPLEX)
  1651. {
  1652. /* GCC has an extension that treats ~complex as the complex
  1653. conjugate. */
  1654. struct value *real = value_real_part (arg1);
  1655. struct value *imag = value_imaginary_part (arg1);
  1656. imag = value_neg (imag);
  1657. return value_literal_complex (real, imag, type);
  1658. }
  1659. else
  1660. error (_("Argument to complement operation not an integer, boolean."));
  1661. return val;
  1662. }
  1663. /* The INDEX'th bit of SET value whose value_type is TYPE,
  1664. and whose value_contents is valaddr.
  1665. Return -1 if out of range, -2 other error. */
  1666. int
  1667. value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
  1668. {
  1669. struct gdbarch *gdbarch = type->arch ();
  1670. LONGEST low_bound, high_bound;
  1671. LONGEST word;
  1672. unsigned rel_index;
  1673. struct type *range = type->index_type ();
  1674. if (!get_discrete_bounds (range, &low_bound, &high_bound))
  1675. return -2;
  1676. if (index < low_bound || index > high_bound)
  1677. return -1;
  1678. rel_index = index - low_bound;
  1679. word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
  1680. type_byte_order (type));
  1681. rel_index %= TARGET_CHAR_BIT;
  1682. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1683. rel_index = TARGET_CHAR_BIT - 1 - rel_index;
  1684. return (word >> rel_index) & 1;
  1685. }
  1686. int
  1687. value_in (struct value *element, struct value *set)
  1688. {
  1689. int member;
  1690. struct type *settype = check_typedef (value_type (set));
  1691. struct type *eltype = check_typedef (value_type (element));
  1692. if (eltype->code () == TYPE_CODE_RANGE)
  1693. eltype = TYPE_TARGET_TYPE (eltype);
  1694. if (settype->code () != TYPE_CODE_SET)
  1695. error (_("Second argument of 'IN' has wrong type"));
  1696. if (eltype->code () != TYPE_CODE_INT
  1697. && eltype->code () != TYPE_CODE_CHAR
  1698. && eltype->code () != TYPE_CODE_ENUM
  1699. && eltype->code () != TYPE_CODE_BOOL)
  1700. error (_("First argument of 'IN' has wrong type"));
  1701. member = value_bit_index (settype, value_contents (set).data (),
  1702. value_as_long (element));
  1703. if (member < 0)
  1704. error (_("First argument of 'IN' not in range"));
  1705. return member;
  1706. }