]> sourceware.org Git - systemtap.git/blame - loc2c.c
Consolidate task_finder/vma tracker initialization.
[systemtap.git] / loc2c.c
CommitLineData
77de5e9e
GH
1#include <config.h>
2#include <inttypes.h>
3#include <stdbool.h>
4#include <obstack.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <error.h>
8#include <dwarf.h>
44049283 9#include <elfutils/libdw.h>
77de5e9e 10#include <assert.h>
377b204c 11#include "loc2c.h"
77de5e9e 12
55d8c5d4
FCE
13#include "config.h"
14
15#include <elfutils/libdw.h>
16#ifdef HAVE_ELFUTILS_VERSION_H
17#include <elfutils/version.h>
18#endif
19
20#if !defined(_ELFUTILS_PREREQ)
21// make a dummy PREREQ check for elfutils < 0.138
22#define _ELFUTILS_PREREQ(major, minor) (0 >= 1)
23#endif
24
bb5ba1d9
RM
25#if !_ELFUTILS_PREREQ (0,142)
26#define DW_TAG_rvalue_reference_type 0x42
27#define DW_OP_implicit_value 0x9e
28#define DW_OP_stack_value 0x9f
55d8c5d4
FCE
29#endif
30
e36387d7 31#define N_(x) x
77de5e9e 32
a5a674fa
FCE
33/* NB: PR10601 may make one suspect that intptr_t and uintptr_t aren't
34 right, for example on a 64-bit kernel targeting a 32-bit userspace
35 process. At least these types are always at least as wide as
36 userspace (since 64-bit userspace doesn't run on a 32-bit kernel).
37 So as long as deref() and {fetch,store}_register() widen/narrow
38 their underlying values to these, there should be no problem. */
39
77de5e9e
GH
40#define STACK_TYPE "intptr_t" /* Must be the signed type. */
41#define UTYPE "uintptr_t" /* Must be the unsigned type. */
42#define SFORMAT "%" PRId64 "L"
43#define UFORMAT "%" PRIu64 "UL"
44#define AFORMAT "%#" PRIx64 "UL"
45#define STACKFMT "s%u"
46
47struct location
48{
49 struct location *next;
50
e36387d7
RM
51 void (*fail) (void *arg, const char *fmt, ...)
52 __attribute__ ((noreturn, format (printf, 2, 3)));
53 void *fail_arg;
4b1ad75e 54 void (*emit_address) (void *fail_arg, struct obstack *, Dwarf_Addr);
e36387d7 55
20e4a32c 56 const Dwarf_Op *ops;
77de5e9e
GH
57 size_t nops;
58
59 Dwarf_Word byte_size;
60
ce48dbc7
RM
61 enum
62 {
45d7e325 63 loc_address, loc_register, loc_noncontiguous, loc_value,
707e2bec 64 loc_constant, loc_decl, loc_fragment, loc_final
ce48dbc7 65 } type;
9b9d9778 66 struct location *frame_base;
77de5e9e
GH
67 union
68 {
45d7e325 69 struct /* loc_address, loc_value, loc_fragment, loc_final */
77de5e9e 70 {
ce48dbc7 71 const char *declare; /* Temporary that needs declared. */
77de5e9e
GH
72 char *program; /* C fragment, leaves address in s0. */
73 unsigned int stack_depth; /* Temporaries "s0..<N>" used by it. */
77de5e9e
GH
74 bool used_deref; /* Program uses "deref" macro. */
75 } address;
5ddc2ead
RM
76 struct /* loc_register */
77 {
78 unsigned int regno;
79 Dwarf_Word offset;
80 } reg;
77de5e9e 81 struct location *pieces; /* loc_noncontiguous */
707e2bec 82 const void *constant_block; /* loc_constant */
77de5e9e
GH
83 };
84};
85
b25c01a1
RM
86/* Select the C type to use in the emitted code to represent slots in the
87 DWARF expression stack. For really proper semantics this should be the
88 target address size. */
89static const char *
90stack_slot_type (struct location *context __attribute__ ((unused)), bool sign)
91{
92 return sign ? STACK_TYPE : UTYPE;
93}
94
e36387d7
RM
95static struct location *
96alloc_location (struct obstack *pool, struct location *origin)
97{
98 struct location *loc = obstack_alloc (pool, sizeof *loc);
99 loc->fail = origin->fail;
100 loc->fail_arg = origin->fail_arg;
4b1ad75e 101 loc->emit_address = origin->emit_address;
ce48dbc7 102 loc->byte_size = 0;
9b9d9778 103 loc->frame_base = NULL;
8a77568a
RM
104 loc->ops = NULL;
105 loc->nops = 0;
e36387d7
RM
106 return loc;
107}
108
109#define FAIL(loc, fmt, ...) \
110 (*(loc)->fail) ((loc)->fail_arg, fmt, ## __VA_ARGS__)
4b1ad75e
RM
111
112static void
113default_emit_address (void *fail_arg __attribute__ ((unused)),
114 struct obstack *pool, Dwarf_Addr address)
115{
116 obstack_printf (pool, AFORMAT, address);
117}
77de5e9e 118\f
77de5e9e
GH
119/* Synthesize a new loc_address using the program on the obstack. */
120static struct location *
121new_synthetic_loc (struct obstack *pool, struct location *origin, bool deref)
122{
123 obstack_1grow (pool, '\0');
124 char *program = obstack_finish (pool);
125
e36387d7 126 struct location *loc = alloc_location (pool, origin);
77de5e9e
GH
127 loc->next = NULL;
128 loc->byte_size = 0;
129 loc->type = loc_address;
130 loc->address.program = program;
131 loc->address.stack_depth = 0;
ce48dbc7 132 loc->address.declare = NULL;
77de5e9e
GH
133 loc->address.used_deref = deref;
134
135 if (origin->type == loc_register)
136 {
137 loc->ops = origin->ops;
138 loc->nops = origin->nops;
139 }
140 else
141 {
142 loc->ops = NULL;
143 loc->nops = 0;
144 }
145
146 return loc;
147}
148
149
150/* Die in the middle of an expression. */
151static struct location *
e36387d7 152lose (struct location *loc,
20e4a32c 153 const char *failure, const Dwarf_Op *lexpr, size_t i)
77de5e9e 154{
e36387d7
RM
155 FAIL (loc, N_("%s in DWARF expression [%Zu] at %" PRIu64
156 " (%#x: %" PRId64 ", %" PRId64 ")"),
157 failure, i, lexpr[i].offset,
158 lexpr[i].atom, lexpr[i].number, lexpr[i].number2);
77de5e9e
GH
159 return NULL;
160}
161
162/* Translate a (constrained) DWARF expression into C code
163 emitted to the obstack POOL. INDENT is the number of indentation levels.
164 ADDRBIAS is the difference between runtime and Dwarf info addresses.
165 INPUT is null or an expression to be initially pushed on the stack.
166 If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
167 and emit "frame_base" for it. On success, set *MAX_STACK to the number
168 of stack slots required. On failure, set *LOSER to the index in EXPR
169 of the operation we could not handle.
170
171 Returns a failure message or null for success. */
172
173static const char *
174translate (struct obstack *pool, int indent, Dwarf_Addr addrbias,
24c7957b 175 Dwarf_Attribute *attr, const Dwarf_Op *expr, const size_t len,
77de5e9e
GH
176 struct location *input,
177 bool *need_fb, size_t *loser,
178 struct location *loc)
179{
180 loc->ops = expr;
181 loc->nops = len;
182
e36387d7 183#define DIE(msg) return (*loser = i, N_(msg))
77de5e9e
GH
184
185#define emit(fmt, ...) obstack_printf (pool, fmt, ## __VA_ARGS__)
186
56cfcfc6
RM
187 unsigned int stack_depth;
188 unsigned int max_stack = 0;
77de5e9e
GH
189 inline void deepen (void)
190 {
191 if (stack_depth == max_stack)
192 ++max_stack;
193 }
194
195#define POP(var) \
196 if (stack_depth > 0) \
197 --stack_depth; \
198 else if (tos_register != -1) \
199 fetch_tos_register (); \
200 else \
201 goto underflow; \
202 int var = stack_depth
203#define PUSH (deepen (), stack_depth++)
204#define STACK(idx) (stack_depth - 1 - (idx))
205
206 /* Don't put stack operations in the arguments to this. */
207#define push(fmt, ...) \
208 emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)
209
56cfcfc6 210 int tos_register;
77de5e9e
GH
211 inline void fetch_tos_register (void)
212 {
213 deepen ();
214 emit ("%*s" STACKFMT " = fetch_register (%d);\n",
215 indent * 2, "", stack_depth, tos_register);
216 tos_register = -1;
217 }
218
56cfcfc6
RM
219 bool tos_value;
220 Dwarf_Block implicit_value;
221 bool used_deref;
0c254456 222
56cfcfc6
RM
223 /* Initialize our state for handling each new piece. */
224 inline void reset ()
225 {
226 stack_depth = 0;
227 tos_register = -1;
228 tos_value = false;
229 implicit_value.data = NULL;
230 used_deref = false;
77de5e9e 231
56cfcfc6
RM
232 if (input != NULL)
233 switch (input->type)
234 {
235 case loc_address:
236 push ("addr");
237 break;
0c254456 238
56cfcfc6
RM
239 case loc_value:
240 push ("addr");
241 tos_value = true;
242 break;
77de5e9e 243
56cfcfc6
RM
244 case loc_register:
245 tos_register = input->reg.regno;
246 break;
77de5e9e 247
56cfcfc6
RM
248 default:
249 abort ();
250 break;
251 }
252 }
77de5e9e 253
56cfcfc6 254 size_t i;
77de5e9e
GH
255 inline const char *finish (struct location *piece)
256 {
f1604314 257 if (stack_depth >= 1)
77de5e9e 258 {
f1604314
RM
259 /* The top of stack has our value.
260 Other stack slots left don't matter. */
77de5e9e
GH
261 obstack_1grow (pool, '\0');
262 char *program = obstack_finish (pool);
707e2bec
RM
263 if (implicit_value.data == NULL)
264 {
265 piece->type = tos_value ? loc_value : loc_address;
266 piece->address.declare = NULL;
267 piece->address.program = program;
268 piece->address.stack_depth = max_stack;
269 piece->address.used_deref = used_deref;
270 }
271 else
272 {
273 piece->type = loc_constant;
274 piece->byte_size = implicit_value.length;
275 piece->constant_block = implicit_value.data;
276 }
77de5e9e
GH
277 }
278 else if (tos_register == -1)
279 DIE ("stack underflow");
280 else if (obstack_object_size (pool) != 0)
281 DIE ("register value must stand alone in location expression");
282 else
283 {
284 piece->type = loc_register;
5ddc2ead
RM
285 piece->reg.regno = tos_register;
286 piece->reg.offset = 0;
77de5e9e
GH
287 }
288 return NULL;
289 }
290
56cfcfc6 291 reset ();
77de5e9e
GH
292 struct location *pieces = NULL, **tailpiece = &pieces;
293 size_t piece_expr_start = 0;
ce48dbc7 294 Dwarf_Word piece_total_bytes = 0;
77de5e9e
GH
295 for (i = 0; i < len; ++i)
296 {
297 unsigned int reg;
298 uint_fast8_t sp;
299 Dwarf_Word value;
300
707e2bec
RM
301 inline bool more_ops (void)
302 {
303 return (expr[i].atom != DW_OP_nop
304 && expr[i].atom != DW_OP_piece
305 && expr[i].atom != DW_OP_bit_piece);
306 }
307
308 if (tos_value && more_ops ())
45d7e325
RM
309 DIE ("operations follow DW_OP_stack_value");
310
707e2bec
RM
311 if (implicit_value.data != NULL && more_ops ())
312 DIE ("operations follow DW_OP_implicit_value");
313
77de5e9e
GH
314 switch (expr[i].atom)
315 {
316 /* Basic stack operations. */
317 case DW_OP_nop:
318 break;
319
320 case DW_OP_dup:
321 if (stack_depth < 1)
322 goto underflow;
323 else
324 {
325 unsigned int tos = STACK (0);
326 push (STACKFMT, tos);
327 }
328 break;
329
330 case DW_OP_drop:
5cc50568
MW
331 {
332 POP (ignore);
333 emit ("%*s/* drop " STACKFMT "*/\n", indent * 2, "", ignore);
334 break;
335 }
77de5e9e
GH
336
337 case DW_OP_pick:
338 sp = expr[i].number;
339 op_pick:
340 if (sp >= stack_depth)
341 goto underflow;
342 sp = STACK (sp);
343 push (STACKFMT, sp);
344 break;
345
346 case DW_OP_over:
347 sp = 1;
348 goto op_pick;
349
350 case DW_OP_swap:
351 if (stack_depth < 2)
352 goto underflow;
353 deepen (); /* Use a temporary slot. */
354 emit ("%*s"
355 STACKFMT " = " STACKFMT ", "
356 STACKFMT " = " STACKFMT ", "
357 STACKFMT " = " STACKFMT ";\n",
358 indent * 2, "",
359 STACK (-1), STACK (0),
360 STACK (0), STACK (1),
361 STACK (1), STACK (-1));
362 break;
363
364 case DW_OP_rot:
365 if (stack_depth < 3)
366 goto underflow;
367 deepen (); /* Use a temporary slot. */
368 emit ("%*s"
369 STACKFMT " = " STACKFMT ", "
370 STACKFMT " = " STACKFMT ", "
371 STACKFMT " = " STACKFMT ", "
372 STACKFMT " = " STACKFMT ";\n",
373 indent * 2, "",
374 STACK (-1), STACK (0),
375 STACK (0), STACK (1),
376 STACK (1), STACK (2),
377 STACK (3), STACK (-1));
378 break;
379
380
381 /* Control flow operations. */
382 case DW_OP_skip:
383 {
384 Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
385 while (i + 1 < len && expr[i + 1].offset < target)
386 ++i;
387 if (expr[i + 1].offset != target)
388 DIE ("invalid skip target");
389 break;
390 }
391
392 case DW_OP_bra:
393 DIE ("conditional branches not supported");
394 break;
395
396
397 /* Memory access. */
398 case DW_OP_deref:
399 {
400 POP (addr);
401 push ("deref (sizeof (void *), " STACKFMT ")", addr);
9b9d9778 402 used_deref = true;
77de5e9e
GH
403 }
404 break;
405
406 case DW_OP_deref_size:
407 {
408 POP (addr);
409 push ("deref (" UFORMAT ", " STACKFMT ")",
410 expr[i].number, addr);
9b9d9778 411 used_deref = true;
77de5e9e
GH
412 }
413 break;
414
415 case DW_OP_xderef:
416 {
417 POP (addr);
418 POP (as);
419 push ("xderef (sizeof (void *), " STACKFMT ", " STACKFMT ")",
420 addr, as);
9b9d9778 421 used_deref = true;
77de5e9e
GH
422 }
423 break;
424
425 case DW_OP_xderef_size:
426 {
427 POP (addr);
428 POP (as);
429 push ("xderef (" UFORMAT ", " STACKFMT ", " STACKFMT ")",
430 expr[i].number, addr, as);
9b9d9778 431 used_deref = true;
77de5e9e
GH
432 }
433 break;
434
435 /* Constant-value operations. */
436
437 case DW_OP_addr:
4b1ad75e
RM
438 emit ("%*s" STACKFMT " = ", indent * 2, "", PUSH);
439 (*loc->emit_address) (loc->fail_arg, pool,
440 addrbias + expr[i].number);
441 emit (";\n");
77de5e9e
GH
442 break;
443
444 case DW_OP_lit0 ... DW_OP_lit31:
445 value = expr[i].atom - DW_OP_lit0;
446 goto op_const;
447
448 case DW_OP_const1u:
449 case DW_OP_const1s:
450 case DW_OP_const2u:
451 case DW_OP_const2s:
452 case DW_OP_const4u:
453 case DW_OP_const4s:
454 case DW_OP_const8u:
455 case DW_OP_const8s:
456 case DW_OP_constu:
457 case DW_OP_consts:
458 value = expr[i].number;
459 op_const:
460 push (SFORMAT, value);
461 break;
462
463 /* Arithmetic operations. */
464#define UNOP(dw_op, c_op) \
465 case DW_OP_##dw_op: \
466 { \
467 POP (tos); \
468 push ("%s (" STACKFMT ")", #c_op, tos); \
469 } \
470 break
471#define BINOP(dw_op, c_op) \
472 case DW_OP_##dw_op: \
473 { \
474 POP (b); \
475 POP (a); \
476 push (STACKFMT " %s " STACKFMT, a, #c_op, b); \
477 } \
478 break
479
480 UNOP (abs, op_abs);
481 BINOP (and, &);
77de5e9e
GH
482 BINOP (minus, -);
483 BINOP (mod, %);
484 BINOP (mul, *);
485 UNOP (neg, -);
486 UNOP (not, ~);
487 BINOP (or, |);
488 BINOP (plus, +);
489 BINOP (shl, <<);
5d580c21 490 BINOP (shr, >>);
77de5e9e
GH
491 BINOP (xor, ^);
492
493 /* Comparisons are binary operators too. */
494 BINOP (le, <=);
495 BINOP (ge, >=);
496 BINOP (eq, ==);
497 BINOP (lt, <);
498 BINOP (gt, >);
499 BINOP (ne, !=);
500
501#undef UNOP
502#undef BINOP
503
5d580c21 504 case DW_OP_shra:
77de5e9e
GH
505 {
506 POP (b);
507 POP (a);
508 push ("(%s) " STACKFMT " >> (%s)" STACKFMT,
b25c01a1
RM
509 stack_slot_type (loc, true), a,
510 stack_slot_type (loc, true), b);
77de5e9e
GH
511 break;
512 }
513
682d5372
RM
514 case DW_OP_div:
515 {
516 POP (b);
517 POP (a);
518 push ("(%s) " STACKFMT " / (%s)" STACKFMT,
519 stack_slot_type (loc, true), a,
520 stack_slot_type (loc, true), b);
521 break;
522 }
523
77de5e9e
GH
524 case DW_OP_plus_uconst:
525 {
526 POP (x);
527 push (STACKFMT " + " UFORMAT, x, expr[i].number);
528 }
529 break;
530
531
532 /* Register-relative addressing. */
533 case DW_OP_breg0 ... DW_OP_breg31:
534 reg = expr[i].atom - DW_OP_breg0;
535 value = expr[i].number;
536 goto op_breg;
537
538 case DW_OP_bregx:
539 reg = expr[i].number;
540 value = expr[i].number2;
541 op_breg:
542 push ("fetch_register (%u) + " SFORMAT, reg, value);
543 break;
544
545 case DW_OP_fbreg:
546 if (need_fb == NULL)
547 DIE ("DW_OP_fbreg from DW_AT_frame_base");
548 *need_fb = true;
549 push ("frame_base + " SFORMAT, expr[i].number);
550 break;
551
552 /* Direct register contents. */
553 case DW_OP_reg0 ... DW_OP_reg31:
554 reg = expr[i].atom - DW_OP_reg0;
555 goto op_reg;
556
557 case DW_OP_regx:
558 reg = expr[i].number;
559 op_reg:
560 tos_register = reg;
561 break;
562
563 /* Special magic. */
564 case DW_OP_piece:
565 if (stack_depth > 1)
566 /* If this ever happens we could copy the program. */
567 DIE ("DW_OP_piece left multiple values on stack");
568 else
569 {
9b9d9778
RM
570 /* The obstack has a pending program for loc_address,
571 so we must finish that piece off before we can
572 allocate again. */
573 struct location temp_piece =
574 {
575 .fail = loc->fail,
576 .fail_arg = loc->fail_arg,
577 .emit_address = loc->emit_address,
578 .frame_base = NULL,
579 .ops = &expr[piece_expr_start],
580 .nops = i - piece_expr_start,
581 };
582 const char *failure = finish (&temp_piece);
77de5e9e
GH
583 if (failure != NULL)
584 return failure;
585
9b9d9778
RM
586 struct location *piece = obstack_alloc (pool, sizeof *piece);
587 *piece = temp_piece;
588
77de5e9e
GH
589 piece_expr_start = i + 1;
590
ce48dbc7 591 piece_total_bytes += piece->byte_size = expr[i].number;
77de5e9e
GH
592
593 *tailpiece = piece;
594 tailpiece = &piece->next;
595 piece->next = NULL;
56cfcfc6
RM
596
597 /* Reset default conditions for handling the next piece. */
598 reset ();
77de5e9e
GH
599 }
600 break;
601
24c7957b 602 case DW_OP_stack_value:
45d7e325
RM
603 if (stack_depth > 1)
604 DIE ("DW_OP_stack_value left multiple values on stack");
605 else
606 {
607 /* Fetch a register to top of stack, or check for underflow.
608 Then mark the TOS as being a value. */
609 POP (tos);
610 assert (tos == 0);
611 PUSH;
612 tos_value = true;
613 }
24c7957b
MW
614 break;
615
616 case DW_OP_implicit_value:
bd39efb3 617 if (attr == NULL)
707e2bec
RM
618 DIE ("DW_OP_implicit_value used in invalid context"
619 " (no DWARF attribute, ABI return value location?)");
620
621 /* It's supposed to appear by itself, except for DW_OP_piece. */
622 if (stack_depth != 0)
623 DIE ("DW_OP_implicit_value follows stack operations");
bd39efb3
RM
624
625#if _ELFUTILS_PREREQ (0, 143)
707e2bec
RM
626 if (dwarf_getlocation_implicit_value (attr, (Dwarf_Op *) &expr[i],
627 &implicit_value) != 0)
628 DIE ("dwarf_getlocation_implicit_value failed");
bd39efb3 629
707e2bec
RM
630 /* Fake top of stack: implicit_value being set marks it. */
631 PUSH;
632 break;
bd39efb3 633#endif
707e2bec 634
bd39efb3
RM
635 DIE ("DW_OP_implicit_value not supported");
636 break;
24c7957b 637
00b01a99
MW
638 case DW_OP_call_frame_cfa:
639 // We pick this out when processing DW_AT_frame_base in
640 // so it really shouldn't turn up here.
641 if (need_fb == NULL)
642 DIE ("DW_OP_call_frame_cfa while processing frame base");
643 else
644 DIE ("DW_OP_call_frame_cfa not expected outside DW_AT_frame_base");
645 break;
646
77de5e9e
GH
647 case DW_OP_push_object_address:
648 DIE ("XXX DW_OP_push_object_address");
649 break;
650
651 default:
652 DIE ("unrecognized operation");
653 break;
654 }
655 }
656
657 if (pieces == NULL)
658 return finish (loc);
659
660 if (piece_expr_start != i)
661 DIE ("extra operations after last DW_OP_piece");
662
663 loc->type = loc_noncontiguous;
664 loc->pieces = pieces;
ce48dbc7 665 loc->byte_size = piece_total_bytes;
77de5e9e
GH
666
667 return NULL;
668
669 underflow:
670 DIE ("stack underflow");
671
672#undef emit
673#undef push
674#undef PUSH
675#undef POP
676#undef STACK
677#undef DIE
678}
679
680/* Translate a location starting from an address or nothing. */
681static struct location *
682location_from_address (struct obstack *pool,
e36387d7 683 void (*fail) (void *arg, const char *fmt, ...)
4b1ad75e 684 __attribute__ ((noreturn, format (printf, 2, 3))),
e36387d7 685 void *fail_arg,
4b1ad75e
RM
686 void (*emit_address) (void *fail_arg,
687 struct obstack *, Dwarf_Addr),
77de5e9e 688 int indent, Dwarf_Addr dwbias,
24c7957b 689 Dwarf_Attribute *attr,
20e4a32c 690 const Dwarf_Op *expr, size_t len, Dwarf_Addr address,
00b01a99
MW
691 struct location **input, Dwarf_Attribute *fb_attr,
692 const Dwarf_Op *cfa_ops)
77de5e9e 693{
e36387d7
RM
694 struct location *loc = obstack_alloc (pool, sizeof *loc);
695 loc->fail = *input == NULL ? fail : (*input)->fail;
696 loc->fail_arg = *input == NULL ? fail_arg : (*input)->fail_arg;
4b1ad75e 697 loc->emit_address = *input == NULL ? emit_address : (*input)->emit_address;
ce48dbc7 698 loc->byte_size = 0;
9b9d9778 699 loc->frame_base = NULL;
8a77568a
RM
700 loc->ops = NULL;
701 loc->nops = 0;
e36387d7 702
77de5e9e
GH
703 bool need_fb = false;
704 size_t loser;
24c7957b 705 const char *failure = translate (pool, indent + 1, dwbias, attr, expr, len,
77de5e9e
GH
706 *input, &need_fb, &loser, loc);
707 if (failure != NULL)
e36387d7 708 return lose (loc, failure, expr, loser);
77de5e9e
GH
709
710 loc->next = NULL;
711 if (need_fb)
712 {
713 /* The main expression uses DW_OP_fbreg, so we need to compute
714 the DW_AT_frame_base attribute expression's value first. */
715
b5bd7775
RM
716 if (fb_attr == NULL)
717 FAIL (loc, N_("required DW_AT_frame_base attribute not supplied"));
718
20e4a32c 719 Dwarf_Op *fb_expr;
77de5e9e 720 size_t fb_len;
20e4a32c
RM
721 switch (dwarf_getlocation_addr (fb_attr, address - dwbias,
722 &fb_expr, &fb_len, 1))
77de5e9e
GH
723 {
724 case 1: /* Should always happen. */
725 if (fb_len == 0)
726 goto fb_inaccessible;
727 break;
728
729 default: /* Shouldn't happen. */
730 case -1:
ab94852d 731 FAIL (loc, N_("dwarf_getlocation_addr (form %#x): %s"),
6174c5ec 732 dwarf_whatform (fb_attr), dwarf_errmsg (-1));
77de5e9e
GH
733 return NULL;
734
735 case 0: /* Shouldn't happen. */
736 fb_inaccessible:
2b3d9bba 737 FAIL (loc, N_("DW_AT_frame_base not accessible at this address"));
77de5e9e
GH
738 return NULL;
739 }
740
00b01a99
MW
741 // If it is DW_OP_call_frame_cfa then get cfi cfa ops.
742 const Dwarf_Op * fb_ops;
743 if (fb_len == 1 && fb_expr[0].atom == DW_OP_call_frame_cfa)
744 {
745 if (cfa_ops == NULL)
746 FAIL (loc, N_("No cfa_ops supplied, but needed by DW_OP_call_frame_cfa"));
747 fb_ops = cfa_ops;
748 }
749 else
750 fb_ops = fb_expr;
751
9b9d9778 752 loc->frame_base = alloc_location (pool, loc);
24c7957b 753 failure = translate (pool, indent + 1, dwbias, attr, fb_ops, fb_len, NULL,
9b9d9778 754 NULL, &loser, loc->frame_base);
77de5e9e 755 if (failure != NULL)
e36387d7 756 return lose (loc, failure, fb_expr, loser);
77de5e9e
GH
757 }
758
759 if (*input != NULL)
760 (*input)->next = loc;
761 *input = loc;
762
763 return loc;
764}
765
766/* Translate a location starting from a non-address "on the top of the
767 stack". The *INPUT location is a register name or noncontiguous
768 object specification, and this expression wants to find the "address"
24c7957b 769 of an object (or the actual value) relative to that "address". */
77de5e9e
GH
770
771static struct location *
772location_relative (struct obstack *pool,
24c7957b 773 int indent, Dwarf_Addr dwbias, Dwarf_Attribute *attr,
20e4a32c 774 const Dwarf_Op *expr, size_t len, Dwarf_Addr address,
00b01a99
MW
775 struct location **input, Dwarf_Attribute *fb_attr,
776 const Dwarf_Op *cfa_ops)
77de5e9e 777{
5cc50568 778 Dwarf_Sword *stack = NULL;
77de5e9e
GH
779 unsigned int stack_depth = 0, max_stack = 0;
780 inline void deepen (void)
781 {
782 if (stack_depth == max_stack)
783 {
784 ++max_stack;
785 obstack_blank (pool, sizeof stack[0]);
786 stack = (void *) obstack_base (pool);
787 }
788 }
789
790#define POP(var) \
791 if (stack_depth > 0) \
792 --stack_depth; \
793 else \
794 goto underflow; \
795 int var = stack_depth
796#define PUSH (deepen (), stack_depth++)
797#define STACK(idx) (stack_depth - 1 - (idx))
798#define STACKWORD(idx) stack[STACK (idx)]
799
800 /* Don't put stack operations in the arguments to this. */
801#define push(value) (stack[PUSH] = (value))
802
803 const char *failure = NULL;
e36387d7 804#define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
77de5e9e
GH
805
806 struct location *head = NULL;
807 size_t i;
808 for (i = 0; i < len; ++i)
809 {
810 uint_fast8_t sp;
811 Dwarf_Word value;
812
813 switch (expr[i].atom)
814 {
815 /* Basic stack operations. */
816 case DW_OP_nop:
817 break;
818
819 case DW_OP_dup:
820 if (stack_depth < 1)
821 goto underflow;
822 else
823 {
824 unsigned int tos = STACK (0);
825 push (stack[tos]);
826 }
827 break;
828
829 case DW_OP_drop:
830 if (stack_depth > 0)
831 --stack_depth;
832 else if (*input != NULL)
833 /* Mark that we have consumed the input. */
834 *input = NULL;
835 else
836 /* Hits if cleared above, or if we had no input at all. */
837 goto underflow;
838 break;
839
840 case DW_OP_pick:
841 sp = expr[i].number;
842 op_pick:
843 if (sp >= stack_depth)
844 goto underflow;
845 sp = STACK (sp);
846 push (stack[sp]);
847 break;
848
849 case DW_OP_over:
850 sp = 1;
851 goto op_pick;
852
853 case DW_OP_swap:
854 if (stack_depth < 2)
855 goto underflow;
856 deepen (); /* Use a temporary slot. */
857 STACKWORD (-1) = STACKWORD (0);
858 STACKWORD (0) = STACKWORD (1);
859 STACKWORD (1) = STACKWORD (-1);
860 break;
861
862 case DW_OP_rot:
863 if (stack_depth < 3)
864 goto underflow;
865 deepen (); /* Use a temporary slot. */
866 STACKWORD (-1) = STACKWORD (0);
867 STACKWORD (0) = STACKWORD (1);
868 STACKWORD (2) = STACKWORD (2);
869 STACKWORD (2) = STACKWORD (-1);
870 break;
871
872
873 /* Control flow operations. */
874 case DW_OP_bra:
875 {
876 POP (taken);
877 if (stack[taken] == 0)
878 break;
879 }
880 /*FALLTHROUGH*/
881
882 case DW_OP_skip:
883 {
884 Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
885 while (i + 1 < len && expr[i + 1].offset < target)
886 ++i;
887 if (expr[i + 1].offset != target)
888 DIE ("invalid skip target");
889 break;
890 }
891
892 /* Memory access. */
893 case DW_OP_deref:
894 case DW_OP_deref_size:
895 case DW_OP_xderef:
896 case DW_OP_xderef_size:
897
898 /* Register-relative addressing. */
899 case DW_OP_breg0 ... DW_OP_breg31:
900 case DW_OP_bregx:
901 case DW_OP_fbreg:
902
903 /* This started from a register, but now it's following a pointer.
904 So we can do the translation starting from address here. */
4b1ad75e 905 return location_from_address (pool, NULL, NULL, NULL, indent, dwbias,
24c7957b 906 attr, expr, len, address, input, fb_attr,
00b01a99 907 cfa_ops);
77de5e9e
GH
908
909
910 /* Constant-value operations. */
911 case DW_OP_addr:
4b1ad75e 912 DIE ("static calculation depends on load-time address");
77de5e9e
GH
913 push (dwbias + expr[i].number);
914 break;
915
916 case DW_OP_lit0 ... DW_OP_lit31:
917 value = expr[i].atom - DW_OP_lit0;
918 goto op_const;
919
920 case DW_OP_const1u:
921 case DW_OP_const1s:
922 case DW_OP_const2u:
923 case DW_OP_const2s:
924 case DW_OP_const4u:
925 case DW_OP_const4s:
926 case DW_OP_const8u:
927 case DW_OP_const8s:
928 case DW_OP_constu:
929 case DW_OP_consts:
930 value = expr[i].number;
931 op_const:
932 push (value);
933 break;
934
935 /* Arithmetic operations. */
936#define UNOP(dw_op, c_op) \
937 case DW_OP_##dw_op: \
938 { \
939 POP (tos); \
940 push (c_op (stack[tos])); \
941 } \
942 break
943#define BINOP(dw_op, c_op) \
944 case DW_OP_##dw_op: \
945 { \
946 POP (b); \
947 POP (a); \
948 push (stack[a] c_op stack[b]); \
949 } \
950 break
951
952#define op_abs(x) (x < 0 ? -x : x)
953 UNOP (abs, op_abs);
954 BINOP (and, &);
955 BINOP (div, /);
956 BINOP (mod, %);
957 BINOP (mul, *);
958 UNOP (neg, -);
959 UNOP (not, ~);
960 BINOP (or, |);
961 BINOP (shl, <<);
962 BINOP (shra, >>);
963 BINOP (xor, ^);
964
965 /* Comparisons are binary operators too. */
966 BINOP (le, <=);
967 BINOP (ge, >=);
968 BINOP (eq, ==);
969 BINOP (lt, <);
970 BINOP (gt, >);
971 BINOP (ne, !=);
972
973#undef UNOP
974#undef BINOP
975
976 case DW_OP_shr:
977 {
978 POP (b);
979 POP (a);
980 push ((Dwarf_Word) stack[a] >> (Dwarf_Word) stack[b]);
981 break;
982 }
983
984 /* Simple addition we may be able to handle relative to
985 the starting register name. */
986 case DW_OP_minus:
987 {
988 POP (tos);
989 value = -stack[tos];
990 goto plus;
991 }
992 case DW_OP_plus:
993 {
994 POP (tos);
995 value = stack[tos];
996 goto plus;
997 }
998 case DW_OP_plus_uconst:
999 value = expr[i].number;
1000 plus:
1001 if (stack_depth > 0)
1002 {
1003 /* It's just private diddling after all. */
1004 POP (a);
1005 push (stack[a] + value);
1006 break;
1007 }
1008 if (*input == NULL)
1009 goto underflow;
1010
1011 /* This is the primary real-world case: the expression takes
1012 the input address and adds a constant offset. */
1013
1014 while ((*input)->type == loc_noncontiguous)
1015 {
1016 /* We are starting from a noncontiguous object (DW_OP_piece).
1017 Find the piece we want. */
1018
1019 struct location *piece = (*input)->pieces;
1020 while (piece != NULL && value >= piece->byte_size)
1021 {
1022 value -= piece->byte_size;
1023 piece = piece->next;
1024 }
1025 if (piece == NULL)
1026 DIE ("offset outside available pieces");
1027
1028 *input = piece;
1029 }
1030
1031 switch ((*input)->type)
1032 {
1033 case loc_address:
1034 {
1035 /* The piece we want is actually in memory. Use the same
1036 program to compute the address from the preceding input. */
1037
e36387d7 1038 struct location *loc = alloc_location (pool, *input);
77de5e9e
GH
1039 *loc = **input;
1040 if (head == NULL)
1041 head = loc;
1042 (*input)->next = loc;
1043 if (value == 0)
1044 {
1045 /* The piece addresses exactly where we want to go. */
1046 loc->next = NULL;
1047 *input = loc;
1048 }
1049 else
1050 {
1051 /* Add a second fragment to offset the piece address. */
1052 obstack_printf (pool, "%*saddr += " SFORMAT "\n",
1053 indent * 2, "", value);
1054 *input = loc->next = new_synthetic_loc (pool, *input,
1055 false);
1056 }
1057
1058 if (i + 1 < len)
1059 {
1060 /* This expression keeps going, but further
1061 computations now have an address to start with.
1062 So we can punt to the address computation generator. */
4b1ad75e 1063 loc = location_from_address (pool, NULL, NULL, NULL,
24c7957b 1064 indent, dwbias, attr,
77de5e9e 1065 &expr[i + 1], len - i - 1,
00b01a99
MW
1066 address, input, fb_attr,
1067 cfa_ops);
77de5e9e
GH
1068 if (loc == NULL)
1069 return NULL;
1070 }
1071
1072 /* That's all she wrote. */
1073 return head;
1074 }
1075
1076 case loc_register:
5ddc2ead
RM
1077 /* This piece (or the whole struct) fits in a register. */
1078 (*input)->reg.offset += value;
1079 return head ?: *input;
77de5e9e 1080
707e2bec
RM
1081 case loc_constant:
1082 /* This piece has a constant value. */
1083 if (value >= (*input)->byte_size)
1084 DIE ("offset outside available constant block");
1085 (*input)->constant_block += value;
1086 (*input)->byte_size -= value;
1087 return head ?: *input;
1088
1089 case loc_value:
1090 /* The piece we want is part of a computed value! */
1091 /* XXX implement me! */
1092
77de5e9e
GH
1093 default:
1094 abort ();
1095 }
1096 break;
1097
1098 /* Direct register contents. */
1099 case DW_OP_reg0 ... DW_OP_reg31:
1100 case DW_OP_regx:
1101 DIE ("register");
1102 break;
1103
1104 /* Special magic. */
1105 case DW_OP_piece:
1106 DIE ("DW_OP_piece");
1107 break;
1108
1109 case DW_OP_push_object_address:
1110 DIE ("XXX DW_OP_push_object_address");
1111 break;
1112
1113 default:
1114 DIE ("unrecognized operation");
1115 break;
1116 }
1117 }
1118
1119 if (stack_depth > 1)
1120 DIE ("multiple values left on stack");
1121
1122 if (stack_depth > 0) /* stack_depth == 1 */
1123 {
1124 if (*input != NULL)
1125 DIE ("multiple values left on stack");
1126
1127 /* Could handle this if it ever actually happened. */
1128 DIE ("relative expression computed constant");
1129 }
1130
1131 return head;
1132
1133 underflow:
1134 if (*input == NULL)
1135 DIE ("stack underflow");
1136 else
1137 DIE ("cannot handle location expression");
1138
1139 fail:
e36387d7 1140 return lose (*input, failure, expr, i);
77de5e9e
GH
1141}
1142
1143
1144/* Translate a C fragment for the location expression, using *INPUT
1145 as the starting location, begin from scratch if *INPUT is null.
1146 If DW_OP_fbreg is used, it may have a subfragment computing from
1147 the FB_ATTR location expression.
1148
ce48dbc7 1149 On errors, call FAIL and never return. On success, return the
77de5e9e
GH
1150 first fragment created, which is also chained onto (*INPUT)->next.
1151 *INPUT is then updated with the new tail of that chain. */
1152
1153struct location *
1154c_translate_location (struct obstack *pool,
e36387d7
RM
1155 void (*fail) (void *arg, const char *fmt, ...)
1156 __attribute__ ((noreturn, format (printf, 2, 3))),
1157 void *fail_arg,
4b1ad75e
RM
1158 void (*emit_address) (void *fail_arg,
1159 struct obstack *, Dwarf_Addr),
d1531387 1160 int indent, Dwarf_Addr dwbias, Dwarf_Addr pc_address,
24c7957b 1161 Dwarf_Attribute *attr,
d1531387 1162 const Dwarf_Op *expr, size_t len,
00b01a99
MW
1163 struct location **input, Dwarf_Attribute *fb_attr,
1164 const Dwarf_Op *cfa_ops)
77de5e9e 1165{
d1531387 1166 indent += 2;
e141cc19 1167
77de5e9e
GH
1168 switch (*input == NULL ? loc_address : (*input)->type)
1169 {
1170 case loc_address:
1171 /* We have a previous address computation.
1172 This expression will compute starting with that on the stack. */
4b1ad75e
RM
1173 return location_from_address (pool, fail, fail_arg,
1174 emit_address ?: &default_emit_address,
24c7957b 1175 indent, dwbias, attr, expr, len, pc_address,
00b01a99 1176 input, fb_attr, cfa_ops);
77de5e9e
GH
1177
1178 case loc_noncontiguous:
1179 case loc_register:
45d7e325 1180 case loc_value:
707e2bec 1181 case loc_constant:
77de5e9e 1182 /* The starting point is not an address computation, but a
24c7957b
MW
1183 register or implicit value. We can only handle limited
1184 computations from here. */
1185 return location_relative (pool, indent, dwbias, attr, expr, len,
1186 pc_address, input, fb_attr, cfa_ops);
77de5e9e 1187
77de5e9e
GH
1188 default:
1189 abort ();
1190 break;
1191 }
1192
1193 return NULL;
1194}
1195
2e2a5d82
RM
1196/* Translate a DW_AT_const_value attribute as if it were a location of
1197 constant-value flavor. */
1198
1199struct location *
1200c_translate_constant (struct obstack *pool,
1201 void (*fail) (void *arg,
1202 const char *fmt, ...)
1203 __attribute__ ((noreturn,
1204 format (printf, 2, 3))),
1205 void *fail_arg,
1206 void (*emit_address) (void *fail_arg,
1207 struct obstack *,
1208 Dwarf_Addr),
1209 int indent, Dwarf_Addr dwbias, Dwarf_Attribute *attr)
1210{
1211 indent += 2;
1212
1213 struct location *loc = obstack_alloc (pool, sizeof *loc);
1214 loc->fail = fail;
1215 loc->fail_arg = fail_arg;
1216 loc->emit_address = emit_address ?: &default_emit_address;
1217 loc->byte_size = 0;
1218 loc->frame_base = NULL;
1219 loc->address.stack_depth = 0;
1220 loc->address.declare = NULL;
1221 loc->address.used_deref = false;
8a77568a
RM
1222 loc->ops = NULL;
1223 loc->nops = 0;
2e2a5d82
RM
1224
1225 switch (dwarf_whatform (attr))
1226 {
1227 case DW_FORM_addr:
1228 {
1229 Dwarf_Addr addr;
1230 if (dwarf_formaddr (attr, &addr) != 0)
1231 {
1232 FAIL (loc, N_("cannot get constant address: %s"),
1233 dwarf_errmsg (-1));
1234 return NULL;
1235 }
1236 loc->type = loc_value;
1237 obstack_printf (pool, "%*saddr = ", indent * 2, "");
1238 (*loc->emit_address) (loc->fail_arg, pool, dwbias + addr);
1239 obstack_grow (pool, ";\n", 3);
1240 loc->address.program = obstack_finish (pool);
1241 break;
1242 }
1243
1244 case DW_FORM_block:
1245 case DW_FORM_block1:
1246 case DW_FORM_block2:
1247 case DW_FORM_block4:
1248 {
1249 Dwarf_Block block;
1250 if (dwarf_formblock (attr, &block) != 0)
1251 {
1252 FAIL (loc, N_("cannot get constant block: %s"), dwarf_errmsg (-1));
1253 return NULL;
1254 }
1255 loc->type = loc_constant;
1256 loc->byte_size = block.length;
1257 loc->constant_block = block.data;
1258 break;
1259 }
1260
1261 case DW_FORM_string:
1262 case DW_FORM_strp:
1263 {
1264 const char *string = dwarf_formstring (attr);
1265 if (string == NULL)
1266 {
1267 FAIL (loc, N_("cannot get string constant: %s"), dwarf_errmsg (-1));
1268 return NULL;
1269 }
1270 loc->type = loc_constant;
1271 loc->byte_size = strlen (string) + 1;
1272 loc->constant_block = string;
1273 break;
1274 }
1275
1276 default:
1277 {
1278 Dwarf_Sword value;
1279 if (dwarf_formsdata (attr, &value) != 0)
1280 {
1281 FAIL (loc, N_("cannot get constant value: %s"), dwarf_errmsg (-1));
1282 return NULL;
1283 }
1284 loc->type = loc_value;
56cfcfc6 1285 obstack_printf (pool, "%*saddr = %" PRId64 "L;\n",
2e2a5d82
RM
1286 indent * 2, "", value);
1287 obstack_1grow (pool, '\0');
1288 loc->address.program = obstack_finish (pool);
1289 break;
1290 }
1291 }
1292
1293 return loc;
1294}
1295
1296
c4ce66a1
JS
1297/* Translate a C fragment for a direct argument VALUE. On errors, call FAIL,
1298 which should not return. Any later errors will use FAIL and FAIL_ARG from
1299 this translate call. On success, return the fragment created. */
1300struct location *
1301c_translate_argument (struct obstack *pool,
1302 void (*fail) (void *arg, const char *fmt, ...)
1303 __attribute__ ((noreturn, format (printf, 2, 3))),
1304 void *fail_arg,
1305 void (*emit_address) (void *fail_arg,
1306 struct obstack *, Dwarf_Addr),
1307 int indent, const char *value)
1308{
1309 indent += 2;
1310
2e2a5d82 1311 obstack_printf (pool, "%*saddr = %s;\n", indent * 2, "", value);
c4ce66a1
JS
1312 obstack_1grow (pool, '\0');
1313 char *program = obstack_finish (pool);
1314
1315 struct location *loc = obstack_alloc (pool, sizeof *loc);
1316 loc->next = NULL;
1317 loc->fail = fail;
1318 loc->fail_arg = fail_arg;
2e2a5d82 1319 loc->emit_address = emit_address ?: &default_emit_address;
c4ce66a1
JS
1320 loc->ops = NULL;
1321 loc->nops = 0;
1322 loc->byte_size = 0;
1323 loc->type = loc_address;
1324 loc->frame_base = NULL;
1325 loc->address.declare = NULL;
1326 loc->address.program = program;
1327 loc->address.stack_depth = 0;
1328 loc->address.used_deref = false;
1329
1330 return loc;
1331}
1332
1333
77de5e9e
GH
1334/* Emit "uintNN_t TARGET = ...;". */
1335static bool
1336emit_base_fetch (struct obstack *pool, Dwarf_Word byte_size,
e41b2484 1337 bool signed_p, const char *target, struct location *loc)
77de5e9e 1338{
83f1d2e5 1339 bool deref = false;
e41b2484 1340
5ddc2ead 1341 /* Emit size/signed coercion. */
83f1d2e5 1342 obstack_printf (pool, "{ ");
5ddc2ead 1343 obstack_printf (pool, "%sint%u_t value = ",
83f1d2e5 1344 (signed_p ? "" : "u"), (unsigned)(byte_size * 8));
77de5e9e
GH
1345
1346 switch (loc->type)
1347 {
45d7e325
RM
1348 case loc_value:
1349 obstack_printf (pool, "addr;");
1350 break;
1351
77de5e9e
GH
1352 case loc_address:
1353 if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
e41b2484 1354 obstack_printf (pool, "deref (%" PRIu64 ", addr);", byte_size);
77de5e9e 1355 else
e41b2484 1356 obstack_printf (pool, "deref (sizeof %s, addr);", target);
83f1d2e5
FCE
1357 deref = true;
1358 break;
77de5e9e
GH
1359
1360 case loc_register:
5ddc2ead
RM
1361 if (loc->reg.offset != 0)
1362 FAIL (loc, N_("cannot handle offset into register in fetch"));
1363 obstack_printf (pool, "fetch_register (%u);", loc->reg.regno);
77de5e9e
GH
1364 break;
1365
1366 case loc_noncontiguous:
ce48dbc7 1367 FAIL (loc, N_("noncontiguous location for base fetch"));
77de5e9e
GH
1368 break;
1369
1370 default:
1371 abort ();
1372 break;
1373 }
1374
56cfcfc6 1375 obstack_printf (pool, " %s = value; }", target);
83f1d2e5 1376 return deref;
77de5e9e
GH
1377}
1378
e141cc19
RM
1379/* Emit "... = RVALUE;". */
1380static bool
1381emit_base_store (struct obstack *pool, Dwarf_Word byte_size,
1382 const char *rvalue, struct location *loc)
1383{
1384 switch (loc->type)
1385 {
1386 case loc_address:
1387 if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
1388 obstack_printf (pool, "store_deref (%" PRIu64 ", addr, %s); ",
1389 byte_size, rvalue);
1390 else
1391 obstack_printf (pool, "store_deref (sizeof %s, addr, %s); ",
1392 rvalue, rvalue);
1393 return true;
1394
1395 case loc_register:
5ddc2ead
RM
1396 if (loc->reg.offset != 0)
1397 FAIL (loc, N_("cannot handle offset into register in store"));
1398 obstack_printf (pool, "store_register (%u, %s);", loc->reg.regno, rvalue);
e141cc19
RM
1399 break;
1400
1401 case loc_noncontiguous:
ce48dbc7 1402 FAIL (loc, N_("noncontiguous location for base store"));
e141cc19
RM
1403 break;
1404
45d7e325
RM
1405 case loc_value:
1406 FAIL (loc, N_("location is computed value, cannot store"));
1407 break;
1408
707e2bec
RM
1409 case loc_constant:
1410 FAIL (loc, N_("location is constant value, cannot store"));
1411 break;
1412
e141cc19
RM
1413 default:
1414 abort ();
1415 break;
1416 }
1417
1418 return false;
1419}
1420
77de5e9e 1421
ce48dbc7
RM
1422/* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1423 yielding a loc_noncontiguous location unless LOC is small enough. */
1424static struct location *
1425discontiguify (struct obstack *pool, int indent, struct location *loc,
1426 Dwarf_Word total_bytes, Dwarf_Word max_piece_bytes)
1427{
1428 inline bool pieces_small_enough (void)
1429 {
1430 if (loc->type != loc_noncontiguous)
1431 return (loc->byte_size ?: total_bytes) <= max_piece_bytes;
78f6bba6
FCE
1432 struct location *p;
1433 for (p = loc->pieces; p != NULL; p = p->next)
ce48dbc7
RM
1434 if (p->byte_size > max_piece_bytes)
1435 return false;
1436 return true;
1437 }
77de5e9e 1438
ce48dbc7
RM
1439 if (pieces_small_enough ())
1440 return loc;
1441
1442 struct location *noncontig = alloc_location (pool, loc);
1443 noncontig->next = NULL;
1444 noncontig->type = loc_noncontiguous;
1445 noncontig->byte_size = total_bytes;
1446 noncontig->pieces = NULL;
1447 struct location **tailpiece = &noncontig->pieces;
1448 inline void add (struct location *piece)
1449 {
1450 *tailpiece = piece;
1451 tailpiece = &piece->next;
1452 }
1453
1454 switch (loc->type)
1455 {
1456 case loc_address:
1457 {
1458 /* Synthesize a piece that sets "container_addr" to the computed
1459 address of the whole object. Each piece will refer to this. */
1460 obstack_printf (pool, "%*scontainer_addr = addr;\n",
1461 indent++ * 2, "");
1462 loc->next = new_synthetic_loc (pool, loc, false);
1463 loc->next->byte_size = loc->byte_size;
1464 loc->next->type = loc_fragment;
1465 loc->next->address.declare = "container_addr";
1466 loc = loc->next;
1467
1468 /* Synthesize pieces that just compute "container_addr + N". */
1469 Dwarf_Word offset = 0;
1470 while (total_bytes - offset > 0)
1471 {
1472 Dwarf_Word size = total_bytes - offset;
1473 if (size > max_piece_bytes)
1474 size = max_piece_bytes;
1475
01ed06fa 1476 obstack_printf (pool, "%*saddr = container_addr + " UFORMAT ";\n",
ce48dbc7
RM
1477 indent * 2, "", offset);
1478 struct location *piece = new_synthetic_loc (pool, loc, false);
1479 piece->byte_size = size;
1480 add (piece);
1481
1482 offset += size;
1483 }
1484
1485 --indent;
1486 break;
1487 }
1488
707e2bec
RM
1489 case loc_constant:
1490 {
1491 Dwarf_Word offset = 0;
1492 while (total_bytes - offset > 0)
1493 {
1494 Dwarf_Word size = total_bytes - offset;
1495 if (size > max_piece_bytes)
1496 size = max_piece_bytes;
1497
1498 struct location *piece = alloc_location (pool, loc);
1499 piece->next = NULL;
1500 piece->type = loc_constant;
1501 piece->byte_size = size;
1502 piece->constant_block = loc->constant_block + offset;
1503
1504 add (piece);
20561343
MW
1505
1506 offset += size;
707e2bec
RM
1507 }
1508
1509 break;
1510 }
1511
45d7e325
RM
1512 case loc_value:
1513 FAIL (loc, N_("stack value too big for fetch ???"));
1514 break;
1515
ce48dbc7
RM
1516 case loc_register:
1517 FAIL (loc, N_("single register too big for fetch/store ???"));
1518 break;
1519
1520 case loc_noncontiguous:
1521 /* Could be handled if it ever happened. */
1522 FAIL (loc, N_("discontiguify of noncontiguous location not supported"));
1523 break;
1524
1525 default:
1526 abort ();
1527 break;
1528 }
1529
1530 loc->next = noncontig;
1531 return noncontig;
1532}
1533
1534/* Make a fragment that declares a union such as:
1535 union {
1536 char bytes[8];
1537 struct {
1538 uint32_t p0;
1539 uint32_t p4;
1540 } pieces __attribute__ ((packed));
1541 uint64_t whole;
9fa29c52 1542 } u_pieces<depth>;
ce48dbc7
RM
1543*/
1544static void
1545declare_noncontig_union (struct obstack *pool, int indent,
9fa29c52
MW
1546 struct location **input, struct location *loc,
1547 int depth)
77de5e9e 1548{
9fa29c52
MW
1549 if (depth > 9)
1550 FAIL (loc, N_("declaring noncontig union for depth > 9, too many pieces"));
1551
ce48dbc7 1552 obstack_printf (pool, "%*sunion {\n", indent++ * 2, "");
377b204c 1553
ce48dbc7
RM
1554 obstack_printf (pool, "%*schar bytes[%" PRIu64 "];\n",
1555 indent * 2, "", loc->byte_size);
e141cc19 1556
707e2bec 1557 if (loc->type == loc_noncontiguous)
77de5e9e 1558 {
707e2bec
RM
1559 Dwarf_Word offset = 0;
1560 struct location *p;
5ec323b5
MW
1561 obstack_printf (pool, "%*sstruct {\n", indent++ * 2, "");
1562
707e2bec
RM
1563 for (p = loc->pieces; p != NULL; p = p->next)
1564 {
1565 obstack_printf (pool, "%*suint%" PRIu64 "_t p%" PRIu64 ";\n",
1566 indent * 2, "", p->byte_size * 8, offset);
1567 offset += p->byte_size;
1568 }
77de5e9e 1569
707e2bec
RM
1570 obstack_printf (pool, "%*s} pieces __attribute__ ((packed));\n",
1571 --indent * 2, "");
1572 }
77de5e9e 1573
ce48dbc7
RM
1574 obstack_printf (pool, "%*suint%" PRIu64 "_t whole;\n",
1575 indent * 2, "", loc->byte_size * 8);
1576
9fa29c52 1577 obstack_printf (pool, "%*s} u_pieces%d;\n", --indent * 2, "", depth);
ce48dbc7
RM
1578
1579 loc = new_synthetic_loc (pool, *input, false);
1580 loc->type = loc_decl;
77de5e9e
GH
1581 (*input)->next = loc;
1582 *input = loc;
1583}
1584
1585/* Determine the byte size of a base type. */
1586static Dwarf_Word
e36387d7 1587base_byte_size (Dwarf_Die *typedie, struct location *origin)
77de5e9e 1588{
e7a012f0
GH
1589 assert (dwarf_tag (typedie) == DW_TAG_base_type ||
1590 dwarf_tag (typedie) == DW_TAG_enumeration_type);
377b204c 1591
77de5e9e
GH
1592 Dwarf_Attribute attr_mem;
1593 Dwarf_Word size;
1594 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) != NULL
1595 && dwarf_formudata (&attr_mem, &size) == 0)
1596 return size;
1597
e36387d7
RM
1598 FAIL (origin,
1599 N_("cannot get byte_size attribute for type %s: %s"),
ce48dbc7 1600 dwarf_diename (typedie) ?: "<anonymous>",
77de5e9e
GH
1601 dwarf_errmsg (-1));
1602 return -1;
1603}
1604
83f1d2e5
FCE
1605static Dwarf_Word
1606base_encoding (Dwarf_Die *typedie, struct location *origin)
1607{
d6b38089
FCE
1608 if (! (dwarf_tag (typedie) == DW_TAG_base_type ||
1609 dwarf_tag (typedie) == DW_TAG_enumeration_type))
1610 return -1;
83f1d2e5
FCE
1611
1612 Dwarf_Attribute attr_mem;
1613 Dwarf_Word encoding;
1614 if (dwarf_attr_integrate (typedie, DW_AT_encoding, &attr_mem) != NULL
1615 && dwarf_formudata (&attr_mem, &encoding) == 0)
1616 return encoding;
1617
d6b38089
FCE
1618 (void) origin;
1619 /*
83f1d2e5
FCE
1620 FAIL (origin,
1621 N_("cannot get encoding attribute for type %s: %s"),
1622 dwarf_diename (typedie) ?: "<anonymous>",
1623 dwarf_errmsg (-1));
d6b38089 1624 */
83f1d2e5
FCE
1625 return -1;
1626}
1627
1628
1629
ce48dbc7
RM
1630/* Fetch the bitfield parameters. */
1631static void
1632get_bitfield (struct location *loc,
1633 Dwarf_Die *die, Dwarf_Word *bit_offset, Dwarf_Word *bit_size)
77de5e9e 1634{
77de5e9e
GH
1635 Dwarf_Attribute attr_mem;
1636 if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL
ce48dbc7 1637 || dwarf_formudata (&attr_mem, bit_offset) != 0
77de5e9e 1638 || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL
ce48dbc7 1639 || dwarf_formudata (&attr_mem, bit_size) != 0)
e36387d7 1640 FAIL (loc, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
77de5e9e
GH
1641}
1642
ce48dbc7 1643/* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
e141cc19 1644 at the *INPUT location and store it in lvalue TARGET. */
ce48dbc7 1645static void
707e2bec
RM
1646translate_base_fetch (struct obstack *pool, int indent,
1647 Dwarf_Word byte_size, bool signed_p,
9fa29c52
MW
1648 struct location **input, const char *target,
1649 int depth)
77de5e9e 1650{
ce48dbc7 1651 bool deref = false;
77de5e9e 1652
ce48dbc7
RM
1653 if ((*input)->type == loc_noncontiguous)
1654 {
1655 struct location *p = (*input)->pieces;
77de5e9e 1656
9fa29c52 1657 declare_noncontig_union (pool, indent, input, *input, depth);
ce48dbc7
RM
1658
1659 Dwarf_Word offset = 0;
9fa29c52
MW
1660 char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1661 piece[8] = (char) ('0' + depth);
1662 int pdepth = depth + 1;
ce48dbc7
RM
1663 while (p != NULL)
1664 {
1665 struct location *newp = obstack_alloc (pool, sizeof *newp);
1666 *newp = *p;
1667 newp->next = NULL;
1668 (*input)->next = newp;
1669 *input = newp;
1670
9fa29c52 1671 snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20,
73b5e906 1672 "%" PRIu64, offset);
e41b2484 1673 translate_base_fetch (pool, indent, p->byte_size, signed_p /* ? */,
9fa29c52 1674 input, piece, pdepth);
ce48dbc7
RM
1675 (*input)->type = loc_fragment;
1676
1677 offset += p->byte_size;
1678 p = p->next;
9fa29c52 1679 pdepth++;
ce48dbc7
RM
1680 }
1681
9fa29c52
MW
1682 obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2,
1683 "", target, depth);
707e2bec
RM
1684 }
1685 else if ((*input)->type == loc_constant)
1686 {
1687 const unsigned char *constant_block = (*input)->constant_block;
1688 const size_t byte_size = (*input)->byte_size;
1689 size_t i;
1690
9fa29c52 1691 declare_noncontig_union (pool, indent, input, *input, depth);
707e2bec
RM
1692
1693 for (i = 0; i < byte_size; ++i)
9fa29c52
MW
1694 obstack_printf (pool, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent * 2,
1695 "", depth, i, constant_block[i]);
707e2bec 1696
9fa29c52
MW
1697 obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2,
1698 "", target, depth);
ce48dbc7 1699 }
77de5e9e
GH
1700 else
1701 switch (byte_size)
1702 {
ce48dbc7 1703 case 0: /* Special case, means address size. */
77de5e9e
GH
1704 case 1:
1705 case 2:
1706 case 4:
1707 case 8:
1708 obstack_printf (pool, "%*s", indent * 2, "");
e41b2484 1709 deref = emit_base_fetch (pool, byte_size, signed_p, target, *input);
77de5e9e
GH
1710 obstack_printf (pool, "\n");
1711 break;
1712
1713 default:
1714 /* Could handle this generating call to memcpy equivalent. */
e36387d7 1715 FAIL (*input, N_("fetch is larger than base integer types"));
77de5e9e
GH
1716 break;
1717 }
1718
1719 struct location *loc = new_synthetic_loc (pool, *input, deref);
ce48dbc7 1720 loc->byte_size = byte_size;
77de5e9e
GH
1721 loc->type = loc_final;
1722 (*input)->next = loc;
1723 *input = loc;
1724}
1725
ce48dbc7
RM
1726/* Determine the maximum size of a base type, from some DIE in the CU. */
1727static Dwarf_Word
1728max_fetch_size (struct location *loc, Dwarf_Die *die)
1729{
1730 Dwarf_Die cu_mem;
1731 uint8_t address_size;
ce48dbc7
RM
1732 Dwarf_Die *cu = dwarf_diecu (die, &cu_mem, &address_size, NULL);
1733 if (cu == NULL)
1734 FAIL (loc, N_("cannot determine CU address size from %s: %s"),
1735 dwarf_diename (die), dwarf_errmsg (-1));
1736
1737 return address_size;
1738}
1739
1740/* Translate a fragment to fetch the value of variable or member DIE
1741 at the *INPUT location and store it in lvalue TARGET. */
1742void
1743c_translate_fetch (struct obstack *pool, int indent,
1744 Dwarf_Addr dwbias __attribute__ ((unused)),
1745 Dwarf_Die *die, Dwarf_Die *typedie,
1746 struct location **input, const char *target)
1747{
1748 ++indent;
1749
1750 Dwarf_Attribute size_attr;
1751 Dwarf_Word byte_size;
1752 if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL
1753 || dwarf_formudata (&size_attr, &byte_size) != 0)
1754 byte_size = base_byte_size (typedie, *input);
1755
e41b2484
FCE
1756 Dwarf_Attribute encoding_attr;
1757 Dwarf_Word encoding;
1758 if (dwarf_attr_integrate (die, DW_AT_encoding, &encoding_attr) == NULL
1759 || dwarf_formudata (&encoding_attr, &encoding) != 0)
83f1d2e5 1760 encoding = base_encoding (typedie, *input);
707e2bec 1761 bool signed_p = encoding == DW_ATE_signed || encoding == DW_ATE_signed_char;
e41b2484 1762
ce48dbc7
RM
1763 *input = discontiguify (pool, indent, *input, byte_size,
1764 max_fetch_size (*input, die));
1765
1766 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
1767 {
1768 /* This is a bit field. Fetch the containing base type into a
1769 temporary variable. */
1770
9fa29c52 1771 translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0);
ce48dbc7
RM
1772 (*input)->type = loc_fragment;
1773 (*input)->address.declare = "tmp";
1774
1775 Dwarf_Word bit_offset, bit_size;
1776 get_bitfield (*input, die, &bit_offset, &bit_size);
1777
1778 obstack_printf (pool, "%*s"
1779 "fetch_bitfield (%s, tmp, %" PRIu64 ", %" PRIu64 ");\n",
1780 indent *2, "", target, bit_offset, bit_size);
1781
1782 struct location *loc = new_synthetic_loc (pool, *input, false);
1783 loc->type = loc_final;
1784 (*input)->next = loc;
1785 *input = loc;
1786 }
1787 else
9fa29c52 1788 translate_base_fetch (pool, indent, byte_size, signed_p, input, target, 0);
ce48dbc7
RM
1789}
1790
1791/* Translate a fragment to store RVALUE into the base-type value of
1792 BYTE_SIZE bytes at the *INPUT location. */
1793static void
1794translate_base_store (struct obstack *pool, int indent, Dwarf_Word byte_size,
1795 struct location **input, struct location *store_loc,
9fa29c52 1796 const char *rvalue, int depth)
ce48dbc7
RM
1797{
1798 bool deref = false;
1799
1800 if (store_loc->type == loc_noncontiguous)
1801 {
9fa29c52 1802 declare_noncontig_union (pool, indent, input, store_loc, depth);
ce48dbc7 1803
9fa29c52
MW
1804 obstack_printf (pool, "%*su_pieces%d.whole = %s;\n", indent * 2,
1805 "", depth, rvalue);
ce48dbc7
RM
1806 struct location *loc = new_synthetic_loc (pool, *input, deref);
1807 loc->type = loc_fragment;
1808 (*input)->next = loc;
1809 *input = loc;
1810
1811 Dwarf_Word offset = 0;
9fa29c52
MW
1812 char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1813 piece[8] = (char) ('0' + depth);
78f6bba6 1814 struct location *p;
9fa29c52 1815 int pdepth = depth + 1;
78f6bba6 1816 for (p = store_loc->pieces; p != NULL; p = p->next)
b8da0ad1 1817 {
ce48dbc7
RM
1818 struct location *newp = obstack_alloc (pool, sizeof *newp);
1819 *newp = *p;
1820 newp->next = NULL;
1821 (*input)->next = newp;
1822 *input = newp;
1823
9fa29c52 1824 snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64,
73b5e906 1825 offset);
ce48dbc7 1826 translate_base_store (pool, indent,
9fa29c52 1827 p->byte_size, input, *input, piece, pdepth++);
ce48dbc7
RM
1828 (*input)->type = loc_fragment;
1829
1830 offset += p->byte_size;
1831 }
1832
1833 (*input)->type = loc_final;
1834 }
1835 else
1836 {
1837 switch (byte_size)
1838 {
1839 case 1:
1840 case 2:
1841 case 4:
1842 case 8:
1843 obstack_printf (pool, "%*s", indent * 2, "");
1844 deref = emit_base_store (pool, byte_size, rvalue, store_loc);
1845 obstack_printf (pool, "\n");
1846 break;
1847
1848 default:
1849 /* Could handle this generating call to memcpy equivalent. */
1850 FAIL (*input, N_("store is larger than base integer types"));
1851 break;
1852 }
1853
1854 struct location *loc = new_synthetic_loc (pool, *input, deref);
1855 loc->type = loc_final;
1856 (*input)->next = loc;
1857 *input = loc;
1858 }
1859}
1860
e141cc19
RM
1861/* Translate a fragment to fetch the value of variable or member DIE
1862 at the *INPUT location and store it in rvalue RVALUE. */
1863
1864void
1865c_translate_store (struct obstack *pool, int indent,
1866 Dwarf_Addr dwbias __attribute__ ((unused)),
377b204c 1867 Dwarf_Die *die, Dwarf_Die *typedie,
e141cc19
RM
1868 struct location **input, const char *rvalue)
1869{
1870 ++indent;
1871
1872 Dwarf_Attribute size_attr;
1873 Dwarf_Word byte_size;
1874 if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL
1875 || dwarf_formudata (&size_attr, &byte_size) != 0)
e36387d7 1876 byte_size = base_byte_size (typedie, *input);
e141cc19 1877
e41b2484
FCE
1878 Dwarf_Attribute encoding_attr;
1879 Dwarf_Word encoding;
1880 if (dwarf_attr_integrate (die, DW_AT_encoding, &encoding_attr) == NULL
1881 || dwarf_formudata (&encoding_attr, &encoding) != 0)
83f1d2e5 1882 encoding = base_encoding (typedie, *input);
5ddc2ead 1883 bool signed_p = (encoding == DW_ATE_signed
83f1d2e5 1884 || encoding == DW_ATE_signed_char);
e41b2484 1885
ce48dbc7
RM
1886 *input = discontiguify (pool, indent, *input, byte_size,
1887 max_fetch_size (*input, die));
1888
1889 struct location *store_loc = *input;
1890
e141cc19 1891 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
ce48dbc7
RM
1892 {
1893 /* This is a bit field. Fetch the containing base type into a
1894 temporary variable. */
e141cc19 1895
9fa29c52 1896 translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0);
ce48dbc7
RM
1897 (*input)->type = loc_fragment;
1898 (*input)->address.declare = "tmp";
e141cc19 1899
ce48dbc7
RM
1900 Dwarf_Word bit_offset, bit_size;
1901 get_bitfield (*input, die, &bit_offset, &bit_size);
1902
1903 obstack_printf (pool, "%*s"
1904 "store_bitfield (tmp, %s, %" PRIu64 ", %" PRIu64 ");\n",
1905 indent * 2, "", rvalue, bit_offset, bit_size);
1906
1907 struct location *loc = new_synthetic_loc (pool, *input, false);
1908 loc->type = loc_fragment;
1909 (*input)->next = loc;
1910 *input = loc;
1911
1912 /* We have mixed RVALUE into the bits in "tmp".
1913 Now we'll store "tmp" back whence we fetched it. */
1914 rvalue = "tmp";
1915 }
1916
9fa29c52 1917 translate_base_store (pool, indent, byte_size, input, store_loc, rvalue, 0);
ce48dbc7
RM
1918}
1919
1920/* Translate a fragment to dereference the given pointer type,
1921 where *INPUT is the location of the pointer with that type.
1922
1923 We chain on a loc_address program that yields this pointer value
1924 (i.e. the location of what it points to). */
1925
1926void
1927c_translate_pointer (struct obstack *pool, int indent,
1928 Dwarf_Addr dwbias __attribute__ ((unused)),
1929 Dwarf_Die *typedie, struct location **input)
1930{
9c119951
JS
1931 assert (dwarf_tag (typedie) == DW_TAG_pointer_type ||
1932 dwarf_tag (typedie) == DW_TAG_reference_type ||
1933 dwarf_tag (typedie) == DW_TAG_rvalue_reference_type);
ce48dbc7
RM
1934
1935 Dwarf_Attribute attr_mem;
1936 Dwarf_Word byte_size;
1937 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL)
1938 byte_size = 0;
1939 else if (dwarf_formudata (&attr_mem, &byte_size) != 0)
1940 FAIL (*input,
1941 N_("cannot get byte_size attribute for type %s: %s"),
1942 dwarf_diename (typedie) ?: "<anonymous>",
1943 dwarf_errmsg (-1));
1944
d6b38089 1945 bool signed_p = false; /* XXX: Does not matter? */
e41b2484 1946
9fa29c52 1947 translate_base_fetch (pool, indent + 1, byte_size, signed_p, input, "addr", 0);
ce48dbc7 1948 (*input)->type = loc_address;
e141cc19
RM
1949}
1950
1951
77de5e9e
GH
1952void
1953c_translate_addressof (struct obstack *pool, int indent,
1954 Dwarf_Addr dwbias __attribute__ ((unused)),
1955 Dwarf_Die *die,
f0df19a2 1956 Dwarf_Die *typedie __attribute__ ((unused)),
77de5e9e
GH
1957 struct location **input, const char *target)
1958{
1959 ++indent;
1960
1961 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
e36387d7 1962 FAIL (*input, N_("cannot take the address of a bit field"));
77de5e9e
GH
1963
1964 switch ((*input)->type)
1965 {
1966 case loc_address:
1967 obstack_printf (pool, "%*s%s = addr;\n", indent * 2, "", target);
1968 (*input)->next = new_synthetic_loc (pool, *input, false);
1969 (*input)->next->type = loc_final;
1970 break;
1971
1972 case loc_register:
e36387d7 1973 FAIL (*input, N_("cannot take address of object in register"));
77de5e9e
GH
1974 break;
1975 case loc_noncontiguous:
e36387d7 1976 FAIL (*input, N_("cannot take address of noncontiguous object"));
77de5e9e 1977 break;
45d7e325
RM
1978 case loc_value:
1979 FAIL (*input, N_("cannot take address of computed value"));
1980 break;
707e2bec
RM
1981 case loc_constant:
1982 FAIL (*input, N_("cannot take address of constant value"));
1983 break;
77de5e9e
GH
1984
1985 default:
1986 abort();
1987 break;
1988 }
1989}
1990
1991
b0be9bdb
FCE
1992/* Translate a fragment to write the given pointer value,
1993 where *INPUT is the location of the pointer with that type.
1994*/
1995
1996void
1997c_translate_pointer_store (struct obstack *pool, int indent,
1998 Dwarf_Addr dwbias __attribute__ ((unused)),
1999 Dwarf_Die *typedie, struct location **input,
2000 const char *rvalue)
2001{
2002 assert (dwarf_tag (typedie) == DW_TAG_pointer_type);
2003
2004 Dwarf_Attribute attr_mem;
2005 Dwarf_Word byte_size;
2006 if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL)
2007 byte_size = 0;
2008 else if (dwarf_formudata (&attr_mem, &byte_size) != 0)
2009 FAIL (*input,
2010 N_("cannot get byte_size attribute for type %s: %s"),
2011 dwarf_diename (typedie) ?: "<anonymous>",
2012 dwarf_errmsg (-1));
2013
2014 translate_base_store (pool, indent + 1, byte_size,
9fa29c52 2015 input, *input, rvalue, 0);
b0be9bdb
FCE
2016
2017 // XXX: what about multiple-location lvalues?
2018}
2019
f3b5366d 2020/* Determine the element stride of a pointer to a type. */
77de5e9e 2021static Dwarf_Word
f3b5366d 2022pointer_stride (Dwarf_Die *typedie, struct location *origin)
77de5e9e
GH
2023{
2024 Dwarf_Attribute attr_mem;
f3b5366d 2025 Dwarf_Die die_mem = *typedie;
28ad563d
JS
2026 int typetag = dwarf_tag(&die_mem);
2027 while (typetag == DW_TAG_typedef ||
f3b5366d
JS
2028 typetag == DW_TAG_const_type ||
2029 typetag == DW_TAG_volatile_type)
28ad563d
JS
2030 {
2031 if (dwarf_attr_integrate (&die_mem, DW_AT_type, &attr_mem) == NULL
f3b5366d
JS
2032 || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
2033 FAIL (origin, N_("cannot get inner type of type %s: %s"),
2034 dwarf_diename (&die_mem) ?: "<anonymous>",
2035 dwarf_errmsg (-1));
28ad563d
JS
2036 typetag = dwarf_tag(&die_mem);
2037 }
2038
77de5e9e
GH
2039 if (dwarf_attr_integrate (&die_mem, DW_AT_byte_size, &attr_mem) != NULL)
2040 {
2041 Dwarf_Word stride;
2042 if (dwarf_formudata (&attr_mem, &stride) == 0)
2043 return stride;
e36387d7
RM
2044 FAIL (origin,
2045 N_("cannot get byte_size attribute for array element type %s: %s"),
ce48dbc7 2046 dwarf_diename (&die_mem) ?: "<anonymous>",
e36387d7 2047 dwarf_errmsg (-1));
77de5e9e
GH
2048 }
2049
e36387d7 2050 FAIL (origin, N_("confused about array element size"));
77de5e9e
GH
2051 return 0;
2052}
2053
f3b5366d
JS
2054/* Determine the element stride of an array type. */
2055static Dwarf_Word
2056array_stride (Dwarf_Die *typedie, struct location *origin)
2057{
2058 Dwarf_Attribute attr_mem;
2059 if (dwarf_attr_integrate (typedie, DW_AT_byte_stride, &attr_mem) != NULL)
2060 {
2061 Dwarf_Word stride;
2062 if (dwarf_formudata (&attr_mem, &stride) == 0)
2063 return stride;
2064 FAIL (origin, N_("cannot get byte_stride attribute array type %s: %s"),
2065 dwarf_diename (typedie) ?: "<anonymous>",
2066 dwarf_errmsg (-1));
2067 }
2068
2069 Dwarf_Die die_mem;
2070 if (dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem) == NULL
2071 || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
2072 FAIL (origin, N_("cannot get element type of array type %s: %s"),
2073 dwarf_diename (typedie) ?: "<anonymous>",
2074 dwarf_errmsg (-1));
2075
2076 return pointer_stride (&die_mem, origin);
2077}
2078
77de5e9e
GH
2079void
2080c_translate_array (struct obstack *pool, int indent,
2081 Dwarf_Addr dwbias __attribute__ ((unused)),
2082 Dwarf_Die *typedie, struct location **input,
2083 const char *idx, Dwarf_Word const_idx)
2084{
d52761f8
JS
2085 assert (dwarf_tag (typedie) == DW_TAG_array_type ||
2086 dwarf_tag (typedie) == DW_TAG_pointer_type);
377b204c 2087
77de5e9e
GH
2088 ++indent;
2089
e36387d7 2090 Dwarf_Word stride = array_stride (typedie, *input);
77de5e9e
GH
2091
2092 struct location *loc = *input;
2093 while (loc->type == loc_noncontiguous)
2094 {
2095 if (idx != NULL)
e36387d7 2096 FAIL (*input, N_("cannot dynamically index noncontiguous array"));
77de5e9e
GH
2097 else
2098 {
2099 Dwarf_Word offset = const_idx * stride;
2100 struct location *piece = loc->pieces;
2101 while (piece != NULL && offset >= piece->byte_size)
2102 {
2103 offset -= piece->byte_size;
2104 piece = piece->next;
2105 }
2106 if (piece == NULL)
e36387d7 2107 FAIL (*input, N_("constant index is outside noncontiguous array"));
77de5e9e 2108 if (offset % stride != 0)
e36387d7 2109 FAIL (*input, N_("noncontiguous array splits elements"));
77de5e9e
GH
2110 const_idx = offset / stride;
2111 loc = piece;
2112 }
2113 }
2114
2115 switch (loc->type)
2116 {
2117 case loc_address:
2118 ++indent;
2119 if (idx != NULL)
2120 obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n",
2121 indent * 2, "", idx, stride);
2122 else
2123 obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n",
2124 indent * 2, "", const_idx, stride);
2125 loc = new_synthetic_loc (pool, loc, false);
2126 break;
2127
2128 case loc_register:
5ddc2ead
RM
2129 if (idx != NULL)
2130 FAIL (*input, N_("cannot index array stored in a register"));
2131 else if (const_idx > max_fetch_size (loc, typedie) / stride)
2132 FAIL (*input, N_("constant index is outside array held in register"));
2133 else
2134 {
2135 loc->reg.offset += const_idx * stride;
2136 return;
2137 }
77de5e9e
GH
2138 break;
2139
707e2bec
RM
2140 case loc_constant:
2141 if (idx != NULL)
2142 FAIL (*input, N_("cannot index into constant value"));
2143 else if (const_idx > loc->byte_size / stride)
2144 FAIL (*input, N_("constant index is outside constant array value"));
2145 else
2146 {
2147 loc->byte_size = stride;
2148 loc->constant_block += const_idx * stride;
2149 return;
2150 };
2151 break;
2152
45d7e325
RM
2153 case loc_value:
2154 FAIL (*input, N_("cannot index into computed value"));
2155 break;
2156
77de5e9e
GH
2157 default:
2158 abort();
2159 break;
2160 }
2161
2162 (*input)->next = loc;
2163 *input = (*input)->next;
2164}
2165
f3b5366d
JS
2166void
2167c_translate_array_pointer (struct obstack *pool, int indent,
2168 Dwarf_Die *typedie, struct location **input,
2169 const char *idx, Dwarf_Word const_idx)
2170{
2171 struct location *loc = *input;
2172 if (loc->type != loc_address)
2173 FAIL (*input, N_("cannot index noncontiguous array"));
2174
2175 Dwarf_Word stride = pointer_stride (typedie, *input);
2176
2177 indent += 2;
2178 if (idx != NULL)
2179 obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n",
2180 indent * 2, "", idx, stride);
2181 else
2182 obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n",
2183 indent * 2, "", const_idx, stride);
2184 loc = new_synthetic_loc (pool, loc, false);
2185
2186 (*input)->next = loc;
2187 *input = (*input)->next;
2188}
2189
77de5e9e
GH
2190\f
2191/* Emitting C code for finalized fragments. */
2192
2193
2194#define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2195
2196/* Open a block with a comment giving the original DWARF expression. */
2197static void
2198emit_header (FILE *out, struct location *loc, unsigned int hindent)
2199{
2200 if (loc->ops == NULL)
2201 emit ("%*s{ // synthesized\n", hindent * 2, "");
2202 else
2203 {
2204 emit ("%*s{ // DWARF expression:", hindent * 2, "");
78f6bba6
FCE
2205 size_t i;
2206 for (i = 0; i < loc->nops; ++i)
77de5e9e
GH
2207 {
2208 emit (" %#x", loc->ops[i].atom);
2209 if (loc->ops[i].number2 == 0)
2210 {
2211 if (loc->ops[i].number != 0)
2212 emit ("(%" PRId64 ")", loc->ops[i].number);
2213 }
2214 else
2215 emit ("(%" PRId64 ",%" PRId64 ")",
2216 loc->ops[i].number, loc->ops[i].number2);
2217 }
2218 emit ("\n");
2219 }
2220}
2221
2222/* Emit a code fragment to assign the target variable to a register value. */
2223static void
2224emit_loc_register (FILE *out, struct location *loc, unsigned int indent,
2225 const char *target)
2226{
2227 assert (loc->type == loc_register);
2228
5ddc2ead
RM
2229 if (loc->reg.offset != 0)
2230 FAIL (loc, N_("cannot handle offset into register in fetch"));
2231
77de5e9e 2232 emit ("%*s%s = fetch_register (%u);\n",
5ddc2ead 2233 indent * 2, "", target, loc->reg.regno);
77de5e9e
GH
2234}
2235
2236/* Emit a code fragment to assign the target variable to an address. */
2237static void
2238emit_loc_address (FILE *out, struct location *loc, unsigned int indent,
2239 const char *target)
2240{
0c254456 2241 assert (loc->type == loc_address || loc->type == loc_value);
77de5e9e
GH
2242
2243 if (loc->address.stack_depth == 0)
2244 /* Synthetic program. */
2245 emit ("%s", loc->address.program);
2246 else
2247 {
f1999557 2248 emit ("%*s{\n", indent * 2, "");
b25c01a1
RM
2249 emit ("%*s%s " STACKFMT, (indent + 1) * 2, "",
2250 stack_slot_type (loc, false), 0);
78f6bba6
FCE
2251 unsigned i;
2252 for (i = 1; i < loc->address.stack_depth; ++i)
77de5e9e
GH
2253 emit (", " STACKFMT, i);
2254 emit (";\n");
2255
2256 emit ("%s%*s%s = " STACKFMT ";\n", loc->address.program,
2257 (indent + 1) * 2, "", target, 0);
f1999557 2258 emit ("%*s}\n", indent * 2, "");
77de5e9e
GH
2259 }
2260}
2261
2262/* Emit a code fragment to declare the target variable and
2263 assign it to an address-sized value. */
2264static void
2265emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
85dfc5c8
RM
2266 const char *target, bool declare,
2267 bool *used_deref, unsigned int *max_stack)
77de5e9e
GH
2268{
2269 if (declare)
b25c01a1 2270 emit ("%*s%s %s;\n", indent * 2, "", stack_slot_type (loc, false), target);
77de5e9e 2271
e141cc19 2272 emit_header (out, loc, indent++);
77de5e9e
GH
2273
2274 switch (loc->type)
2275 {
2276 default:
2277 abort ();
2278 break;
2279
2280 case loc_register:
2281 emit_loc_register (out, loc, indent, target);
2282 break;
2283
2284 case loc_address:
0c254456 2285 case loc_value:
77de5e9e 2286 emit_loc_address (out, loc, indent, target);
85dfc5c8
RM
2287 *used_deref = *used_deref || loc->address.used_deref;
2288 if (loc->address.stack_depth > *max_stack)
2289 *max_stack = loc->address.stack_depth;
77de5e9e
GH
2290 break;
2291 }
2292
e141cc19 2293 emit ("%*s}\n", --indent * 2, "");
77de5e9e
GH
2294}
2295
2296bool
85dfc5c8
RM
2297c_emit_location (FILE *out, struct location *loc, int indent,
2298 unsigned int *max_stack)
77de5e9e
GH
2299{
2300 emit ("%*s{\n", indent * 2, "");
2301
ce48dbc7 2302 bool declared_addr = false;
78f6bba6
FCE
2303 struct location *l;
2304 for (l = loc; l != NULL; l = l->next)
ce48dbc7
RM
2305 switch (l->type)
2306 {
2307 case loc_decl:
2308 emit ("%s", l->address.program);
2309 break;
2310
2311 case loc_address:
0c254456 2312 case loc_value:
ce48dbc7
RM
2313 if (declared_addr)
2314 break;
2315 declared_addr = true;
2316 l->address.declare = "addr";
2317 case loc_fragment:
2318 case loc_final:
2319 if (l->address.declare != NULL)
2320 {
2321 if (l->byte_size == 0 || l->byte_size == (Dwarf_Word) -1)
2322 emit ("%*s%s %s;\n", (indent + 1) * 2, "",
b25c01a1 2323 stack_slot_type (l, false), l->address.declare);
ce48dbc7
RM
2324 else
2325 emit ("%*suint%" PRIu64 "_t %s;\n", (indent + 1) * 2, "",
2326 l->byte_size * 8, l->address.declare);
2327 }
2328
2329 default:
2330 break;
2331 }
2332
77de5e9e 2333 bool deref = false;
85dfc5c8 2334 *max_stack = 0;
9b9d9778
RM
2335
2336 if (loc->frame_base != NULL)
85dfc5c8
RM
2337 emit_loc_value (out, loc->frame_base, indent, "frame_base", true,
2338 &deref, max_stack);
9b9d9778 2339
ce48dbc7 2340 for (; loc->next != NULL; loc = loc->next)
77de5e9e
GH
2341 switch (loc->type)
2342 {
2343 case loc_address:
0c254456 2344 case loc_value:
77de5e9e 2345 /* Emit the program fragment to calculate the address. */
85dfc5c8 2346 emit_loc_value (out, loc, indent + 1, "addr", false, &deref, max_stack);
ce48dbc7
RM
2347 break;
2348
2349 case loc_fragment:
2350 emit ("%s", loc->address.program);
77de5e9e
GH
2351 deref = deref || loc->address.used_deref;
2352 break;
2353
ce48dbc7 2354 case loc_decl:
77de5e9e
GH
2355 case loc_register:
2356 case loc_noncontiguous:
707e2bec 2357 case loc_constant:
77de5e9e
GH
2358 /* These don't produce any code directly.
2359 The next address/final record incorporates the value. */
2360 break;
2361
2362 case loc_final: /* Should be last in chain! */
2363 default:
2364 abort ();
2365 break;
2366 }
2367
2368 if (loc->type != loc_final) /* Unfinished chain. */
2369 abort ();
2370
2371 emit ("%s%*s}\n", loc->address.program, indent * 2, "");
2372
85dfc5c8
RM
2373 if (loc->address.stack_depth > *max_stack)
2374 *max_stack = loc->address.stack_depth;
2375
ce48dbc7 2376 return deref || loc->address.used_deref;
77de5e9e
GH
2377}
2378
2379#undef emit
0c5f5812
JS
2380
2381/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.454117 seconds and 5 git commands to generate.