compile-loc2c.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /* Convert a DWARF location expression to C
  2. Copyright (C) 2014-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 "dwarf2.h"
  16. #include "objfiles.h"
  17. #include "dwarf2/expr.h"
  18. #include "dwarf2/loc.h"
  19. #include "dwarf2/read.h"
  20. #include "ui-file.h"
  21. #include "utils.h"
  22. #include "compile-internal.h"
  23. #include "compile-c.h"
  24. #include "compile.h"
  25. #include "block.h"
  26. #include "dwarf2/frame.h"
  27. #include "gdbsupport/gdb_vecs.h"
  28. #include "value.h"
  29. #include "gdbarch.h"
  30. /* Information about a given instruction. */
  31. struct insn_info
  32. {
  33. /* Stack depth at entry. */
  34. unsigned int depth;
  35. /* Whether this instruction has been visited. */
  36. unsigned int visited : 1;
  37. /* Whether this instruction needs a label. */
  38. unsigned int label : 1;
  39. /* Whether this instruction is DW_OP_GNU_push_tls_address or
  40. DW_OP_form_tls_address. This is a hack until we can add a
  41. feature to glibc to let us properly generate code for TLS. */
  42. unsigned int is_tls : 1;
  43. };
  44. /* A helper function for compute_stack_depth that does the work. This
  45. examines the DWARF expression starting from START and computes
  46. stack effects.
  47. NEED_TEMPVAR is an out parameter which is set if this expression
  48. needs a special temporary variable to be emitted (see the code
  49. generator).
  50. INFO is a vector of insn_info objects, indexed by offset from the
  51. start of the DWARF expression.
  52. TO_DO is a list of bytecodes which must be examined; it may be
  53. added to by this function.
  54. BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
  55. OP_PTR and OP_END are the bounds of the DWARF expression. */
  56. static void
  57. compute_stack_depth_worker (int start, int *need_tempvar,
  58. std::vector<struct insn_info> *info,
  59. std::vector<int> *to_do,
  60. enum bfd_endian byte_order, unsigned int addr_size,
  61. const gdb_byte *op_ptr, const gdb_byte *op_end)
  62. {
  63. const gdb_byte * const base = op_ptr;
  64. int stack_depth;
  65. op_ptr += start;
  66. gdb_assert ((*info)[start].visited);
  67. stack_depth = (*info)[start].depth;
  68. while (op_ptr < op_end)
  69. {
  70. enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
  71. uint64_t reg;
  72. int64_t offset;
  73. int ndx = op_ptr - base;
  74. #define SET_CHECK_DEPTH(WHERE) \
  75. if ((*info)[WHERE].visited) \
  76. { \
  77. if ((*info)[WHERE].depth != stack_depth) \
  78. error (_("inconsistent stack depths")); \
  79. } \
  80. else \
  81. { \
  82. /* Stack depth not set, so set it. */ \
  83. (*info)[WHERE].visited = 1; \
  84. (*info)[WHERE].depth = stack_depth; \
  85. }
  86. SET_CHECK_DEPTH (ndx);
  87. ++op_ptr;
  88. switch (op)
  89. {
  90. case DW_OP_lit0:
  91. case DW_OP_lit1:
  92. case DW_OP_lit2:
  93. case DW_OP_lit3:
  94. case DW_OP_lit4:
  95. case DW_OP_lit5:
  96. case DW_OP_lit6:
  97. case DW_OP_lit7:
  98. case DW_OP_lit8:
  99. case DW_OP_lit9:
  100. case DW_OP_lit10:
  101. case DW_OP_lit11:
  102. case DW_OP_lit12:
  103. case DW_OP_lit13:
  104. case DW_OP_lit14:
  105. case DW_OP_lit15:
  106. case DW_OP_lit16:
  107. case DW_OP_lit17:
  108. case DW_OP_lit18:
  109. case DW_OP_lit19:
  110. case DW_OP_lit20:
  111. case DW_OP_lit21:
  112. case DW_OP_lit22:
  113. case DW_OP_lit23:
  114. case DW_OP_lit24:
  115. case DW_OP_lit25:
  116. case DW_OP_lit26:
  117. case DW_OP_lit27:
  118. case DW_OP_lit28:
  119. case DW_OP_lit29:
  120. case DW_OP_lit30:
  121. case DW_OP_lit31:
  122. ++stack_depth;
  123. break;
  124. case DW_OP_addr:
  125. op_ptr += addr_size;
  126. ++stack_depth;
  127. break;
  128. case DW_OP_const1u:
  129. case DW_OP_const1s:
  130. op_ptr += 1;
  131. ++stack_depth;
  132. break;
  133. case DW_OP_const2u:
  134. case DW_OP_const2s:
  135. op_ptr += 2;
  136. ++stack_depth;
  137. break;
  138. case DW_OP_const4u:
  139. case DW_OP_const4s:
  140. op_ptr += 4;
  141. ++stack_depth;
  142. break;
  143. case DW_OP_const8u:
  144. case DW_OP_const8s:
  145. op_ptr += 8;
  146. ++stack_depth;
  147. break;
  148. case DW_OP_constu:
  149. case DW_OP_consts:
  150. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  151. ++stack_depth;
  152. break;
  153. case DW_OP_reg0:
  154. case DW_OP_reg1:
  155. case DW_OP_reg2:
  156. case DW_OP_reg3:
  157. case DW_OP_reg4:
  158. case DW_OP_reg5:
  159. case DW_OP_reg6:
  160. case DW_OP_reg7:
  161. case DW_OP_reg8:
  162. case DW_OP_reg9:
  163. case DW_OP_reg10:
  164. case DW_OP_reg11:
  165. case DW_OP_reg12:
  166. case DW_OP_reg13:
  167. case DW_OP_reg14:
  168. case DW_OP_reg15:
  169. case DW_OP_reg16:
  170. case DW_OP_reg17:
  171. case DW_OP_reg18:
  172. case DW_OP_reg19:
  173. case DW_OP_reg20:
  174. case DW_OP_reg21:
  175. case DW_OP_reg22:
  176. case DW_OP_reg23:
  177. case DW_OP_reg24:
  178. case DW_OP_reg25:
  179. case DW_OP_reg26:
  180. case DW_OP_reg27:
  181. case DW_OP_reg28:
  182. case DW_OP_reg29:
  183. case DW_OP_reg30:
  184. case DW_OP_reg31:
  185. ++stack_depth;
  186. break;
  187. case DW_OP_regx:
  188. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  189. ++stack_depth;
  190. break;
  191. case DW_OP_breg0:
  192. case DW_OP_breg1:
  193. case DW_OP_breg2:
  194. case DW_OP_breg3:
  195. case DW_OP_breg4:
  196. case DW_OP_breg5:
  197. case DW_OP_breg6:
  198. case DW_OP_breg7:
  199. case DW_OP_breg8:
  200. case DW_OP_breg9:
  201. case DW_OP_breg10:
  202. case DW_OP_breg11:
  203. case DW_OP_breg12:
  204. case DW_OP_breg13:
  205. case DW_OP_breg14:
  206. case DW_OP_breg15:
  207. case DW_OP_breg16:
  208. case DW_OP_breg17:
  209. case DW_OP_breg18:
  210. case DW_OP_breg19:
  211. case DW_OP_breg20:
  212. case DW_OP_breg21:
  213. case DW_OP_breg22:
  214. case DW_OP_breg23:
  215. case DW_OP_breg24:
  216. case DW_OP_breg25:
  217. case DW_OP_breg26:
  218. case DW_OP_breg27:
  219. case DW_OP_breg28:
  220. case DW_OP_breg29:
  221. case DW_OP_breg30:
  222. case DW_OP_breg31:
  223. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  224. ++stack_depth;
  225. break;
  226. case DW_OP_bregx:
  227. {
  228. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  229. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  230. ++stack_depth;
  231. }
  232. break;
  233. case DW_OP_fbreg:
  234. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  235. ++stack_depth;
  236. break;
  237. case DW_OP_dup:
  238. ++stack_depth;
  239. break;
  240. case DW_OP_drop:
  241. --stack_depth;
  242. break;
  243. case DW_OP_pick:
  244. ++op_ptr;
  245. ++stack_depth;
  246. break;
  247. case DW_OP_rot:
  248. case DW_OP_swap:
  249. *need_tempvar = 1;
  250. break;
  251. case DW_OP_over:
  252. ++stack_depth;
  253. break;
  254. case DW_OP_abs:
  255. case DW_OP_neg:
  256. case DW_OP_not:
  257. case DW_OP_deref:
  258. break;
  259. case DW_OP_deref_size:
  260. ++op_ptr;
  261. break;
  262. case DW_OP_plus_uconst:
  263. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  264. break;
  265. case DW_OP_div:
  266. case DW_OP_shra:
  267. case DW_OP_and:
  268. case DW_OP_minus:
  269. case DW_OP_mod:
  270. case DW_OP_mul:
  271. case DW_OP_or:
  272. case DW_OP_plus:
  273. case DW_OP_shl:
  274. case DW_OP_shr:
  275. case DW_OP_xor:
  276. case DW_OP_le:
  277. case DW_OP_ge:
  278. case DW_OP_eq:
  279. case DW_OP_lt:
  280. case DW_OP_gt:
  281. case DW_OP_ne:
  282. --stack_depth;
  283. break;
  284. case DW_OP_call_frame_cfa:
  285. ++stack_depth;
  286. break;
  287. case DW_OP_GNU_push_tls_address:
  288. case DW_OP_form_tls_address:
  289. (*info)[ndx].is_tls = 1;
  290. break;
  291. case DW_OP_skip:
  292. offset = extract_signed_integer (op_ptr, 2, byte_order);
  293. op_ptr += 2;
  294. offset = op_ptr + offset - base;
  295. /* If the destination has not been seen yet, add it to the
  296. to-do list. */
  297. if (!(*info)[offset].visited)
  298. to_do->push_back (offset);
  299. SET_CHECK_DEPTH (offset);
  300. (*info)[offset].label = 1;
  301. /* We're done with this line of code. */
  302. return;
  303. case DW_OP_bra:
  304. offset = extract_signed_integer (op_ptr, 2, byte_order);
  305. op_ptr += 2;
  306. offset = op_ptr + offset - base;
  307. --stack_depth;
  308. /* If the destination has not been seen yet, add it to the
  309. to-do list. */
  310. if (!(*info)[offset].visited)
  311. to_do->push_back (offset);
  312. SET_CHECK_DEPTH (offset);
  313. (*info)[offset].label = 1;
  314. break;
  315. case DW_OP_nop:
  316. break;
  317. default:
  318. error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
  319. }
  320. }
  321. gdb_assert (op_ptr == op_end);
  322. #undef SET_CHECK_DEPTH
  323. }
  324. /* Compute the maximum needed stack depth of a DWARF expression, and
  325. some other information as well.
  326. BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
  327. NEED_TEMPVAR is an out parameter which is set if this expression
  328. needs a special temporary variable to be emitted (see the code
  329. generator).
  330. IS_TLS is an out parameter which is set if this expression refers
  331. to a TLS variable.
  332. OP_PTR and OP_END are the bounds of the DWARF expression.
  333. INITIAL_DEPTH is the initial depth of the DWARF expression stack.
  334. INFO is an array of insn_info objects, indexed by offset from the
  335. start of the DWARF expression.
  336. This returns the maximum stack depth. */
  337. static int
  338. compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
  339. int *need_tempvar, int *is_tls,
  340. const gdb_byte *op_ptr, const gdb_byte *op_end,
  341. int initial_depth,
  342. std::vector<struct insn_info> *info)
  343. {
  344. std::vector<int> to_do;
  345. int stack_depth, i;
  346. info->resize (op_end - op_ptr);
  347. to_do.push_back (0);
  348. (*info)[0].depth = initial_depth;
  349. (*info)[0].visited = 1;
  350. while (!to_do.empty ())
  351. {
  352. int ndx = to_do.back ();
  353. to_do.pop_back ();
  354. compute_stack_depth_worker (ndx, need_tempvar, info, &to_do,
  355. byte_order, addr_size,
  356. op_ptr, op_end);
  357. }
  358. stack_depth = 0;
  359. *is_tls = 0;
  360. for (i = 0; i < op_end - op_ptr; ++i)
  361. {
  362. if ((*info)[i].depth > stack_depth)
  363. stack_depth = (*info)[i].depth;
  364. if ((*info)[i].is_tls)
  365. *is_tls = 1;
  366. }
  367. return stack_depth + 1;
  368. }
  369. #define GCC_UINTPTR "__gdb_uintptr"
  370. #define GCC_INTPTR "__gdb_intptr"
  371. /* Emit code to push a constant. */
  372. static void
  373. push (int indent, string_file *stream, ULONGEST l)
  374. {
  375. gdb_printf (stream,
  376. "%*s__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
  377. indent, "", hex_string (l));
  378. }
  379. /* Emit code to push an arbitrary expression. This works like
  380. printf. */
  381. static void pushf (int indent, string_file *stream, const char *format, ...)
  382. ATTRIBUTE_PRINTF (3, 4);
  383. static void
  384. pushf (int indent, string_file *stream, const char *format, ...)
  385. {
  386. va_list args;
  387. gdb_printf (stream, "%*s__gdb_stack[__gdb_tos + 1] = ", indent, "");
  388. va_start (args, format);
  389. stream->vprintf (format, args);
  390. va_end (args);
  391. stream->puts (";\n");
  392. gdb_printf (stream, "%*s++__gdb_tos;\n", indent, "");
  393. }
  394. /* Emit code for a unary expression -- one which operates in-place on
  395. the top-of-stack. This works like printf. */
  396. static void unary (int indent, string_file *stream, const char *format, ...)
  397. ATTRIBUTE_PRINTF (3, 4);
  398. static void
  399. unary (int indent, string_file *stream, const char *format, ...)
  400. {
  401. va_list args;
  402. gdb_printf (stream, "%*s__gdb_stack[__gdb_tos] = ", indent, "");
  403. va_start (args, format);
  404. stream->vprintf (format, args);
  405. va_end (args);
  406. stream->puts (";\n");
  407. }
  408. /* Emit code for a unary expression -- one which uses the top two
  409. stack items, popping the topmost one. This works like printf. */
  410. static void binary (int indent, string_file *stream, const char *format, ...)
  411. ATTRIBUTE_PRINTF (3, 4);
  412. static void
  413. binary (int indent, string_file *stream, const char *format, ...)
  414. {
  415. va_list args;
  416. gdb_printf (stream, "%*s__gdb_stack[__gdb_tos - 1] = ", indent, "");
  417. va_start (args, format);
  418. stream->vprintf (format, args);
  419. va_end (args);
  420. stream->puts (";\n");
  421. gdb_printf (stream, "%*s--__gdb_tos;\n", indent, "");
  422. }
  423. /* Print the name of a label given its "SCOPE", an arbitrary integer
  424. used for uniqueness, and its TARGET, the bytecode offset
  425. corresponding to the label's point of definition. */
  426. static void
  427. print_label (string_file *stream, unsigned int scope, int target)
  428. {
  429. stream->printf ("__label_%u_%s", scope, pulongest (target));
  430. }
  431. /* Note that a register was used. */
  432. static void
  433. note_register (int regnum, std::vector<bool> &registers_used)
  434. {
  435. gdb_assert (regnum >= 0);
  436. /* If the expression uses a cooked register, then we currently can't
  437. compile it. We would need a gdbarch method to handle this
  438. situation. */
  439. if (regnum >= registers_used.size ())
  440. error (_("Expression uses \"cooked\" register and cannot be compiled."));
  441. registers_used[regnum] = true;
  442. }
  443. /* Emit code that pushes a register's address on the stack.
  444. REGISTERS_USED is an out parameter which is updated to note which
  445. register was needed by this expression. */
  446. static void
  447. pushf_register_address (int indent, string_file *stream,
  448. std::vector<bool> &registers_used,
  449. struct gdbarch *gdbarch, int regnum)
  450. {
  451. std::string regname = compile_register_name_mangled (gdbarch, regnum);
  452. note_register (regnum, registers_used);
  453. pushf (indent, stream,
  454. "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
  455. regname.c_str ());
  456. }
  457. /* Emit code that pushes a register's value on the stack.
  458. REGISTERS_USED is an out parameter which is updated to note which
  459. register was needed by this expression. OFFSET is added to the
  460. register's value before it is pushed. */
  461. static void
  462. pushf_register (int indent, string_file *stream,
  463. std::vector<bool> &registers_used,
  464. struct gdbarch *gdbarch, int regnum, uint64_t offset)
  465. {
  466. std::string regname = compile_register_name_mangled (gdbarch, regnum);
  467. note_register (regnum, registers_used);
  468. if (offset == 0)
  469. pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
  470. regname.c_str ());
  471. else
  472. pushf (indent, stream,
  473. COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
  474. regname.c_str (), hex_string (offset));
  475. }
  476. /* Compile a DWARF expression to C code.
  477. INDENT is the indentation level to use.
  478. STREAM is the stream where the code should be written.
  479. TYPE_NAME names the type of the result of the DWARF expression.
  480. For locations this is "void *" but for array bounds it will be an
  481. integer type.
  482. RESULT_NAME is the name of a variable in the resulting C code. The
  483. result of the expression will be assigned to this variable.
  484. SYM is the symbol corresponding to this expression.
  485. PC is the location at which the expression is being evaluated.
  486. ARCH is the architecture to use.
  487. REGISTERS_USED is an out parameter which is updated to note which
  488. registers were needed by this expression.
  489. ADDR_SIZE is the DWARF address size to use.
  490. OPT_PTR and OP_END are the bounds of the DWARF expression.
  491. If non-NULL, INITIAL points to an initial value to write to the
  492. stack. If NULL, no initial value is written.
  493. PER_CU is the per-CU object used for looking up various other
  494. things. */
  495. static void
  496. do_compile_dwarf_expr_to_c (int indent, string_file *stream,
  497. const char *type_name,
  498. const char *result_name,
  499. struct symbol *sym, CORE_ADDR pc,
  500. struct gdbarch *arch,
  501. std::vector<bool> &registers_used,
  502. unsigned int addr_size,
  503. const gdb_byte *op_ptr, const gdb_byte *op_end,
  504. CORE_ADDR *initial,
  505. dwarf2_per_cu_data *per_cu,
  506. dwarf2_per_objfile *per_objfile)
  507. {
  508. /* We keep a counter so that labels and other objects we create have
  509. unique names. */
  510. static unsigned int scope;
  511. enum bfd_endian byte_order = gdbarch_byte_order (arch);
  512. const gdb_byte * const base = op_ptr;
  513. int need_tempvar = 0;
  514. int is_tls = 0;
  515. std::vector<struct insn_info> info;
  516. int stack_depth;
  517. ++scope;
  518. gdb_printf (stream, "%*s__attribute__ ((unused)) %s %s;\n",
  519. indent, "", type_name, result_name);
  520. gdb_printf (stream, "%*s{\n", indent, "");
  521. indent += 2;
  522. stack_depth = compute_stack_depth (byte_order, addr_size,
  523. &need_tempvar, &is_tls,
  524. op_ptr, op_end, initial != NULL,
  525. &info);
  526. /* This is a hack until we can add a feature to glibc to let us
  527. properly generate code for TLS. You might think we could emit
  528. the address in the ordinary course of translating
  529. DW_OP_GNU_push_tls_address, but since the operand appears on the
  530. stack, it is relatively hard to find, and the idea of calling
  531. target_translate_tls_address with OFFSET==0 and then adding the
  532. offset by hand seemed too hackish. */
  533. if (is_tls)
  534. {
  535. struct frame_info *frame = get_selected_frame (NULL);
  536. struct value *val;
  537. if (frame == NULL)
  538. error (_("Symbol \"%s\" cannot be used because "
  539. "there is no selected frame"),
  540. sym->print_name ());
  541. val = read_var_value (sym, NULL, frame);
  542. if (VALUE_LVAL (val) != lval_memory)
  543. error (_("Symbol \"%s\" cannot be used for compilation evaluation "
  544. "as its address has not been found."),
  545. sym->print_name ());
  546. warning (_("Symbol \"%s\" is thread-local and currently can only "
  547. "be referenced from the current thread in "
  548. "compiled code."),
  549. sym->print_name ());
  550. gdb_printf (stream, "%*s%s = %s;\n",
  551. indent, "", result_name,
  552. core_addr_to_string (value_address (val)));
  553. gdb_printf (stream, "%*s}\n", indent - 2, "");
  554. return;
  555. }
  556. gdb_printf (stream, "%*s" GCC_UINTPTR " __gdb_stack[%d];\n",
  557. indent, "", stack_depth);
  558. if (need_tempvar)
  559. gdb_printf (stream, "%*s" GCC_UINTPTR " __gdb_tmp;\n", indent, "");
  560. gdb_printf (stream, "%*sint __gdb_tos = -1;\n", indent, "");
  561. if (initial != NULL)
  562. pushf (indent, stream, "%s", core_addr_to_string (*initial));
  563. while (op_ptr < op_end)
  564. {
  565. enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
  566. uint64_t uoffset, reg;
  567. int64_t offset;
  568. stream->printf ("%*s", indent - 2, "");
  569. if (info[op_ptr - base].label)
  570. {
  571. print_label (stream, scope, op_ptr - base);
  572. stream->puts (":;");
  573. }
  574. stream->printf ("/* %s */\n", get_DW_OP_name (op));
  575. /* This is handy for debugging the generated code:
  576. gdb_printf (stream, "if (__gdb_tos != %d) abort ();\n",
  577. (int) info[op_ptr - base].depth - 1);
  578. */
  579. ++op_ptr;
  580. switch (op)
  581. {
  582. case DW_OP_lit0:
  583. case DW_OP_lit1:
  584. case DW_OP_lit2:
  585. case DW_OP_lit3:
  586. case DW_OP_lit4:
  587. case DW_OP_lit5:
  588. case DW_OP_lit6:
  589. case DW_OP_lit7:
  590. case DW_OP_lit8:
  591. case DW_OP_lit9:
  592. case DW_OP_lit10:
  593. case DW_OP_lit11:
  594. case DW_OP_lit12:
  595. case DW_OP_lit13:
  596. case DW_OP_lit14:
  597. case DW_OP_lit15:
  598. case DW_OP_lit16:
  599. case DW_OP_lit17:
  600. case DW_OP_lit18:
  601. case DW_OP_lit19:
  602. case DW_OP_lit20:
  603. case DW_OP_lit21:
  604. case DW_OP_lit22:
  605. case DW_OP_lit23:
  606. case DW_OP_lit24:
  607. case DW_OP_lit25:
  608. case DW_OP_lit26:
  609. case DW_OP_lit27:
  610. case DW_OP_lit28:
  611. case DW_OP_lit29:
  612. case DW_OP_lit30:
  613. case DW_OP_lit31:
  614. push (indent, stream, op - DW_OP_lit0);
  615. break;
  616. case DW_OP_addr:
  617. uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
  618. op_ptr += addr_size;
  619. /* Some versions of GCC emit DW_OP_addr before
  620. DW_OP_GNU_push_tls_address. In this case the value is an
  621. index, not an address. We don't support things like
  622. branching between the address and the TLS op. */
  623. if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
  624. uoffset += per_objfile->objfile->text_section_offset ();
  625. push (indent, stream, uoffset);
  626. break;
  627. case DW_OP_const1u:
  628. push (indent, stream,
  629. extract_unsigned_integer (op_ptr, 1, byte_order));
  630. op_ptr += 1;
  631. break;
  632. case DW_OP_const1s:
  633. push (indent, stream,
  634. extract_signed_integer (op_ptr, 1, byte_order));
  635. op_ptr += 1;
  636. break;
  637. case DW_OP_const2u:
  638. push (indent, stream,
  639. extract_unsigned_integer (op_ptr, 2, byte_order));
  640. op_ptr += 2;
  641. break;
  642. case DW_OP_const2s:
  643. push (indent, stream,
  644. extract_signed_integer (op_ptr, 2, byte_order));
  645. op_ptr += 2;
  646. break;
  647. case DW_OP_const4u:
  648. push (indent, stream,
  649. extract_unsigned_integer (op_ptr, 4, byte_order));
  650. op_ptr += 4;
  651. break;
  652. case DW_OP_const4s:
  653. push (indent, stream,
  654. extract_signed_integer (op_ptr, 4, byte_order));
  655. op_ptr += 4;
  656. break;
  657. case DW_OP_const8u:
  658. push (indent, stream,
  659. extract_unsigned_integer (op_ptr, 8, byte_order));
  660. op_ptr += 8;
  661. break;
  662. case DW_OP_const8s:
  663. push (indent, stream,
  664. extract_signed_integer (op_ptr, 8, byte_order));
  665. op_ptr += 8;
  666. break;
  667. case DW_OP_constu:
  668. op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  669. push (indent, stream, uoffset);
  670. break;
  671. case DW_OP_consts:
  672. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  673. push (indent, stream, offset);
  674. break;
  675. case DW_OP_reg0:
  676. case DW_OP_reg1:
  677. case DW_OP_reg2:
  678. case DW_OP_reg3:
  679. case DW_OP_reg4:
  680. case DW_OP_reg5:
  681. case DW_OP_reg6:
  682. case DW_OP_reg7:
  683. case DW_OP_reg8:
  684. case DW_OP_reg9:
  685. case DW_OP_reg10:
  686. case DW_OP_reg11:
  687. case DW_OP_reg12:
  688. case DW_OP_reg13:
  689. case DW_OP_reg14:
  690. case DW_OP_reg15:
  691. case DW_OP_reg16:
  692. case DW_OP_reg17:
  693. case DW_OP_reg18:
  694. case DW_OP_reg19:
  695. case DW_OP_reg20:
  696. case DW_OP_reg21:
  697. case DW_OP_reg22:
  698. case DW_OP_reg23:
  699. case DW_OP_reg24:
  700. case DW_OP_reg25:
  701. case DW_OP_reg26:
  702. case DW_OP_reg27:
  703. case DW_OP_reg28:
  704. case DW_OP_reg29:
  705. case DW_OP_reg30:
  706. case DW_OP_reg31:
  707. dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  708. pushf_register_address (indent, stream, registers_used, arch,
  709. dwarf_reg_to_regnum_or_error
  710. (arch, op - DW_OP_reg0));
  711. break;
  712. case DW_OP_regx:
  713. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  714. dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  715. pushf_register_address (indent, stream, registers_used, arch,
  716. dwarf_reg_to_regnum_or_error (arch, reg));
  717. break;
  718. case DW_OP_breg0:
  719. case DW_OP_breg1:
  720. case DW_OP_breg2:
  721. case DW_OP_breg3:
  722. case DW_OP_breg4:
  723. case DW_OP_breg5:
  724. case DW_OP_breg6:
  725. case DW_OP_breg7:
  726. case DW_OP_breg8:
  727. case DW_OP_breg9:
  728. case DW_OP_breg10:
  729. case DW_OP_breg11:
  730. case DW_OP_breg12:
  731. case DW_OP_breg13:
  732. case DW_OP_breg14:
  733. case DW_OP_breg15:
  734. case DW_OP_breg16:
  735. case DW_OP_breg17:
  736. case DW_OP_breg18:
  737. case DW_OP_breg19:
  738. case DW_OP_breg20:
  739. case DW_OP_breg21:
  740. case DW_OP_breg22:
  741. case DW_OP_breg23:
  742. case DW_OP_breg24:
  743. case DW_OP_breg25:
  744. case DW_OP_breg26:
  745. case DW_OP_breg27:
  746. case DW_OP_breg28:
  747. case DW_OP_breg29:
  748. case DW_OP_breg30:
  749. case DW_OP_breg31:
  750. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  751. pushf_register (indent, stream, registers_used, arch,
  752. dwarf_reg_to_regnum_or_error (arch,
  753. op - DW_OP_breg0),
  754. offset);
  755. break;
  756. case DW_OP_bregx:
  757. {
  758. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  759. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  760. pushf_register (indent, stream, registers_used, arch,
  761. dwarf_reg_to_regnum_or_error (arch, reg), offset);
  762. }
  763. break;
  764. case DW_OP_fbreg:
  765. {
  766. const gdb_byte *datastart;
  767. size_t datalen;
  768. const struct block *b;
  769. struct symbol *framefunc;
  770. char fb_name[50];
  771. b = block_for_pc (pc);
  772. if (!b)
  773. error (_("No block found for address"));
  774. framefunc = block_linkage_function (b);
  775. if (!framefunc)
  776. error (_("No function found for block"));
  777. func_get_frame_base_dwarf_block (framefunc, pc,
  778. &datastart, &datalen);
  779. op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  780. /* Generate a unique-enough name, in case the frame base
  781. is computed multiple times in this expression. */
  782. xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
  783. (long) (op_ptr - base));
  784. do_compile_dwarf_expr_to_c (indent, stream,
  785. GCC_UINTPTR, fb_name,
  786. sym, pc,
  787. arch, registers_used, addr_size,
  788. datastart, datastart + datalen,
  789. NULL, per_cu, per_objfile);
  790. pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
  791. }
  792. break;
  793. case DW_OP_dup:
  794. pushf (indent, stream, "__gdb_stack[__gdb_tos]");
  795. break;
  796. case DW_OP_drop:
  797. gdb_printf (stream, "%*s--__gdb_tos;\n", indent, "");
  798. break;
  799. case DW_OP_pick:
  800. offset = *op_ptr++;
  801. pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
  802. plongest (offset));
  803. break;
  804. case DW_OP_swap:
  805. gdb_printf (stream,
  806. "%*s__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n",
  807. indent, "");
  808. gdb_printf (stream,
  809. "%*s__gdb_stack[__gdb_tos - 1] = "
  810. "__gdb_stack[__gdb_tos];\n",
  811. indent, "");
  812. gdb_printf (stream, ("%*s__gdb_stack[__gdb_tos] = "
  813. "__gdb_tmp;\n"),
  814. indent, "");
  815. break;
  816. case DW_OP_over:
  817. pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
  818. break;
  819. case DW_OP_rot:
  820. gdb_printf (stream, ("%*s__gdb_tmp = "
  821. "__gdb_stack[__gdb_tos];\n"),
  822. indent, "");
  823. gdb_printf (stream,
  824. "%*s__gdb_stack[__gdb_tos] = "
  825. "__gdb_stack[__gdb_tos - 1];\n",
  826. indent, "");
  827. gdb_printf (stream,
  828. "%*s__gdb_stack[__gdb_tos - 1] = "
  829. "__gdb_stack[__gdb_tos -2];\n",
  830. indent, "");
  831. gdb_printf (stream, "%*s__gdb_stack[__gdb_tos - 2] = "
  832. "__gdb_tmp;\n",
  833. indent, "");
  834. break;
  835. case DW_OP_deref:
  836. case DW_OP_deref_size:
  837. {
  838. int size;
  839. const char *mode;
  840. if (op == DW_OP_deref_size)
  841. size = *op_ptr++;
  842. else
  843. size = addr_size;
  844. mode = c_get_mode_for_size (size);
  845. if (mode == NULL)
  846. error (_("Unsupported size %d in %s"),
  847. size, get_DW_OP_name (op));
  848. /* Cast to a pointer of the desired type, then
  849. dereference. */
  850. gdb_printf (stream,
  851. "%*s__gdb_stack[__gdb_tos] = "
  852. "*((__gdb_int_%s *) "
  853. "__gdb_stack[__gdb_tos]);\n",
  854. indent, "", mode);
  855. }
  856. break;
  857. case DW_OP_abs:
  858. unary (indent, stream,
  859. "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
  860. "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
  861. break;
  862. case DW_OP_neg:
  863. unary (indent, stream, "-__gdb_stack[__gdb_tos]");
  864. break;
  865. case DW_OP_not:
  866. unary (indent, stream, "~__gdb_stack[__gdb_tos]");
  867. break;
  868. case DW_OP_plus_uconst:
  869. op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  870. unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
  871. hex_string (reg));
  872. break;
  873. case DW_OP_div:
  874. binary (indent, stream, ("((" GCC_INTPTR
  875. ") __gdb_stack[__gdb_tos-1]) / (("
  876. GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
  877. break;
  878. case DW_OP_shra:
  879. binary (indent, stream,
  880. "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
  881. "__gdb_stack[__gdb_tos]");
  882. break;
  883. #define BINARY(OP) \
  884. binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
  885. " __gdb_stack[__gdb_tos]"); \
  886. break
  887. case DW_OP_and:
  888. BINARY (&);
  889. case DW_OP_minus:
  890. BINARY (-);
  891. case DW_OP_mod:
  892. BINARY (%);
  893. case DW_OP_mul:
  894. BINARY (*);
  895. case DW_OP_or:
  896. BINARY (|);
  897. case DW_OP_plus:
  898. BINARY (+);
  899. case DW_OP_shl:
  900. BINARY (<<);
  901. case DW_OP_shr:
  902. BINARY (>>);
  903. case DW_OP_xor:
  904. BINARY (^);
  905. #undef BINARY
  906. #define COMPARE(OP) \
  907. binary (indent, stream, \
  908. "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
  909. " ((" GCC_INTPTR \
  910. ") __gdb_stack[__gdb_tos]))"); \
  911. break
  912. case DW_OP_le:
  913. COMPARE (<=);
  914. case DW_OP_ge:
  915. COMPARE (>=);
  916. case DW_OP_eq:
  917. COMPARE (==);
  918. case DW_OP_lt:
  919. COMPARE (<);
  920. case DW_OP_gt:
  921. COMPARE (>);
  922. case DW_OP_ne:
  923. COMPARE (!=);
  924. #undef COMPARE
  925. case DW_OP_call_frame_cfa:
  926. {
  927. int regnum;
  928. CORE_ADDR text_offset;
  929. LONGEST off;
  930. const gdb_byte *cfa_start, *cfa_end;
  931. if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
  932. &regnum, &off,
  933. &text_offset, &cfa_start, &cfa_end))
  934. {
  935. /* Register. */
  936. pushf_register (indent, stream, registers_used, arch, regnum,
  937. off);
  938. }
  939. else
  940. {
  941. /* Another expression. */
  942. char cfa_name[50];
  943. /* Generate a unique-enough name, in case the CFA is
  944. computed multiple times in this expression. */
  945. xsnprintf (cfa_name, sizeof (cfa_name),
  946. "__cfa_%ld", (long) (op_ptr - base));
  947. do_compile_dwarf_expr_to_c (indent, stream,
  948. GCC_UINTPTR, cfa_name,
  949. sym, pc, arch, registers_used,
  950. addr_size,
  951. cfa_start, cfa_end,
  952. &text_offset, per_cu, per_objfile);
  953. pushf (indent, stream, "%s", cfa_name);
  954. }
  955. }
  956. break;
  957. case DW_OP_skip:
  958. offset = extract_signed_integer (op_ptr, 2, byte_order);
  959. op_ptr += 2;
  960. gdb_printf (stream, "%*sgoto ", indent, "");
  961. print_label (stream, scope, op_ptr + offset - base);
  962. stream->puts (";\n");
  963. break;
  964. case DW_OP_bra:
  965. offset = extract_signed_integer (op_ptr, 2, byte_order);
  966. op_ptr += 2;
  967. gdb_printf (stream,
  968. "%*sif ((( " GCC_INTPTR
  969. ") __gdb_stack[__gdb_tos--]) != 0) goto ",
  970. indent, "");
  971. print_label (stream, scope, op_ptr + offset - base);
  972. stream->puts (";\n");
  973. break;
  974. case DW_OP_nop:
  975. break;
  976. default:
  977. error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
  978. }
  979. }
  980. gdb_printf (stream, "%*s%s = __gdb_stack[__gdb_tos];\n",
  981. indent, "", result_name);
  982. gdb_printf (stream, "%*s}\n", indent - 2, "");
  983. }
  984. /* See compile.h. */
  985. void
  986. compile_dwarf_expr_to_c (string_file *stream, const char *result_name,
  987. struct symbol *sym, CORE_ADDR pc,
  988. struct gdbarch *arch,
  989. std::vector<bool> &registers_used,
  990. unsigned int addr_size,
  991. const gdb_byte *op_ptr, const gdb_byte *op_end,
  992. dwarf2_per_cu_data *per_cu,
  993. dwarf2_per_objfile *per_objfile)
  994. {
  995. do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
  996. arch, registers_used, addr_size, op_ptr, op_end,
  997. NULL, per_cu, per_objfile);
  998. }
  999. /* See compile.h. */
  1000. void
  1001. compile_dwarf_bounds_to_c (string_file *stream,
  1002. const char *result_name,
  1003. const struct dynamic_prop *prop,
  1004. struct symbol *sym, CORE_ADDR pc,
  1005. struct gdbarch *arch,
  1006. std::vector<bool> &registers_used,
  1007. unsigned int addr_size,
  1008. const gdb_byte *op_ptr, const gdb_byte *op_end,
  1009. dwarf2_per_cu_data *per_cu,
  1010. dwarf2_per_objfile *per_objfile)
  1011. {
  1012. do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
  1013. sym, pc, arch, registers_used,
  1014. addr_size, op_ptr, op_end, NULL, per_cu,
  1015. per_objfile);
  1016. }