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