]> sourceware.org Git - systemtap.git/blob - loc2c.c
Swap DW_OP_shr and DW_OP_shra.
[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 (minus, -);
483 BINOP (mod, %);
484 BINOP (mul, *);
485 UNOP (neg, -);
486 UNOP (not, ~);
487 BINOP (or, |);
488 BINOP (plus, +);
489 BINOP (shl, <<);
490 BINOP (shr, >>);
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
504 case DW_OP_shra:
505 {
506 POP (b);
507 POP (a);
508 push ("(%s) " STACKFMT " >> (%s)" STACKFMT,
509 stack_slot_type (loc, true), a,
510 stack_slot_type (loc, true), b);
511 break;
512 }
513
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
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 {
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);
583 if (failure != NULL)
584 return failure;
585
586 struct location *piece = obstack_alloc (pool, sizeof *piece);
587 *piece = temp_piece;
588
589 piece_expr_start = i + 1;
590
591 piece_total_bytes += piece->byte_size = expr[i].number;
592
593 *tailpiece = piece;
594 tailpiece = &piece->next;
595 piece->next = NULL;
596
597 /* Reset default conditions for handling the next piece. */
598 reset ();
599 }
600 break;
601
602 case DW_OP_stack_value:
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 }
614 break;
615
616 case DW_OP_implicit_value:
617 if (attr == NULL)
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");
624
625 #if _ELFUTILS_PREREQ (0, 143)
626 if (dwarf_getlocation_implicit_value (attr, (Dwarf_Op *) &expr[i],
627 &implicit_value) != 0)
628 DIE ("dwarf_getlocation_implicit_value failed");
629
630 /* Fake top of stack: implicit_value being set marks it. */
631 PUSH;
632 break;
633 #endif
634
635 DIE ("DW_OP_implicit_value not supported");
636 break;
637
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
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;
665 loc->byte_size = piece_total_bytes;
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. */
681 static struct location *
682 location_from_address (struct obstack *pool,
683 void (*fail) (void *arg, const char *fmt, ...)
684 __attribute__ ((noreturn, format (printf, 2, 3))),
685 void *fail_arg,
686 void (*emit_address) (void *fail_arg,
687 struct obstack *, Dwarf_Addr),
688 int indent, Dwarf_Addr dwbias,
689 Dwarf_Attribute *attr,
690 const Dwarf_Op *expr, size_t len, Dwarf_Addr address,
691 struct location **input, Dwarf_Attribute *fb_attr,
692 const Dwarf_Op *cfa_ops)
693 {
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;
697 loc->emit_address = *input == NULL ? emit_address : (*input)->emit_address;
698 loc->byte_size = 0;
699 loc->frame_base = NULL;
700 loc->ops = NULL;
701 loc->nops = 0;
702
703 bool need_fb = false;
704 size_t loser;
705 const char *failure = translate (pool, indent + 1, dwbias, attr, expr, len,
706 *input, &need_fb, &loser, loc);
707 if (failure != NULL)
708 return lose (loc, failure, expr, loser);
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
716 if (fb_attr == NULL)
717 FAIL (loc, N_("required DW_AT_frame_base attribute not supplied"));
718
719 Dwarf_Op *fb_expr;
720 size_t fb_len;
721 switch (dwarf_getlocation_addr (fb_attr, address - dwbias,
722 &fb_expr, &fb_len, 1))
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:
731 FAIL (loc, N_("dwarf_getlocation_addr (form %#x): %s"),
732 dwarf_whatform (fb_attr), dwarf_errmsg (-1));
733 return NULL;
734
735 case 0: /* Shouldn't happen. */
736 fb_inaccessible:
737 FAIL (loc, N_("DW_AT_frame_base not accessible at this address"));
738 return NULL;
739 }
740
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
752 loc->frame_base = alloc_location (pool, loc);
753 failure = translate (pool, indent + 1, dwbias, attr, fb_ops, fb_len, NULL,
754 NULL, &loser, loc->frame_base);
755 if (failure != NULL)
756 return lose (loc, failure, fb_expr, loser);
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"
769 of an object (or the actual value) relative to that "address". */
770
771 static struct location *
772 location_relative (struct obstack *pool,
773 int indent, Dwarf_Addr dwbias, Dwarf_Attribute *attr,
774 const Dwarf_Op *expr, size_t len, Dwarf_Addr address,
775 struct location **input, Dwarf_Attribute *fb_attr,
776 const Dwarf_Op *cfa_ops)
777 {
778 Dwarf_Sword *stack = NULL;
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;
804 #define DIE(msg) do { failure = N_(msg); goto fail; } while (0)
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. */
905 return location_from_address (pool, NULL, NULL, NULL, indent, dwbias,
906 attr, expr, len, address, input, fb_attr,
907 cfa_ops);
908
909
910 /* Constant-value operations. */
911 case DW_OP_addr:
912 DIE ("static calculation depends on load-time address");
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
1038 struct location *loc = alloc_location (pool, *input);
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. */
1063 loc = location_from_address (pool, NULL, NULL, NULL,
1064 indent, dwbias, attr,
1065 &expr[i + 1], len - i - 1,
1066 address, input, fb_attr,
1067 cfa_ops);
1068 if (loc == NULL)
1069 return NULL;
1070 }
1071
1072 /* That's all she wrote. */
1073 return head;
1074 }
1075
1076 case loc_register:
1077 /* This piece (or the whole struct) fits in a register. */
1078 (*input)->reg.offset += value;
1079 return head ?: *input;
1080
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
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:
1140 return lose (*input, failure, expr, i);
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
1149 On errors, call FAIL and never return. On success, return the
1150 first fragment created, which is also chained onto (*INPUT)->next.
1151 *INPUT is then updated with the new tail of that chain. */
1152
1153 struct location *
1154 c_translate_location (struct obstack *pool,
1155 void (*fail) (void *arg, const char *fmt, ...)
1156 __attribute__ ((noreturn, format (printf, 2, 3))),
1157 void *fail_arg,
1158 void (*emit_address) (void *fail_arg,
1159 struct obstack *, Dwarf_Addr),
1160 int indent, Dwarf_Addr dwbias, Dwarf_Addr pc_address,
1161 Dwarf_Attribute *attr,
1162 const Dwarf_Op *expr, size_t len,
1163 struct location **input, Dwarf_Attribute *fb_attr,
1164 const Dwarf_Op *cfa_ops)
1165 {
1166 indent += 2;
1167
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. */
1173 return location_from_address (pool, fail, fail_arg,
1174 emit_address ?: &default_emit_address,
1175 indent, dwbias, attr, expr, len, pc_address,
1176 input, fb_attr, cfa_ops);
1177
1178 case loc_noncontiguous:
1179 case loc_register:
1180 case loc_value:
1181 case loc_constant:
1182 /* The starting point is not an address computation, but a
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);
1187
1188 default:
1189 abort ();
1190 break;
1191 }
1192
1193 return NULL;
1194 }
1195
1196 /* Translate a DW_AT_const_value attribute as if it were a location of
1197 constant-value flavor. */
1198
1199 struct location *
1200 c_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;
1222 loc->ops = NULL;
1223 loc->nops = 0;
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;
1285 obstack_printf (pool, "%*saddr = %" PRId64 "L;\n",
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
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. */
1300 struct location *
1301 c_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
1311 obstack_printf (pool, "%*saddr = %s;\n", indent * 2, "", value);
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;
1319 loc->emit_address = emit_address ?: &default_emit_address;
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
1334 /* Emit "uintNN_t TARGET = ...;". */
1335 static bool
1336 emit_base_fetch (struct obstack *pool, Dwarf_Word byte_size,
1337 bool signed_p, const char *target, struct location *loc)
1338 {
1339 bool deref = false;
1340
1341 /* Emit size/signed coercion. */
1342 obstack_printf (pool, "{ ");
1343 obstack_printf (pool, "%sint%u_t value = ",
1344 (signed_p ? "" : "u"), (unsigned)(byte_size * 8));
1345
1346 switch (loc->type)
1347 {
1348 case loc_value:
1349 obstack_printf (pool, "addr;");
1350 break;
1351
1352 case loc_address:
1353 if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
1354 obstack_printf (pool, "deref (%" PRIu64 ", addr);", byte_size);
1355 else
1356 obstack_printf (pool, "deref (sizeof %s, addr);", target);
1357 deref = true;
1358 break;
1359
1360 case loc_register:
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);
1364 break;
1365
1366 case loc_noncontiguous:
1367 FAIL (loc, N_("noncontiguous location for base fetch"));
1368 break;
1369
1370 default:
1371 abort ();
1372 break;
1373 }
1374
1375 obstack_printf (pool, " %s = value; }", target);
1376 return deref;
1377 }
1378
1379 /* Emit "... = RVALUE;". */
1380 static bool
1381 emit_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:
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);
1399 break;
1400
1401 case loc_noncontiguous:
1402 FAIL (loc, N_("noncontiguous location for base store"));
1403 break;
1404
1405 case loc_value:
1406 FAIL (loc, N_("location is computed value, cannot store"));
1407 break;
1408
1409 case loc_constant:
1410 FAIL (loc, N_("location is constant value, cannot store"));
1411 break;
1412
1413 default:
1414 abort ();
1415 break;
1416 }
1417
1418 return false;
1419 }
1420
1421
1422 /* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
1423 yielding a loc_noncontiguous location unless LOC is small enough. */
1424 static struct location *
1425 discontiguify (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;
1432 struct location *p;
1433 for (p = loc->pieces; p != NULL; p = p->next)
1434 if (p->byte_size > max_piece_bytes)
1435 return false;
1436 return true;
1437 }
1438
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
1476 obstack_printf (pool, "%*saddr = container_addr + " UFORMAT ";\n",
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
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);
1505
1506 offset += size;
1507 }
1508
1509 break;
1510 }
1511
1512 case loc_value:
1513 FAIL (loc, N_("stack value too big for fetch ???"));
1514 break;
1515
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;
1542 } u_pieces<depth>;
1543 */
1544 static void
1545 declare_noncontig_union (struct obstack *pool, int indent,
1546 struct location **input, struct location *loc,
1547 int depth)
1548 {
1549 if (depth > 9)
1550 FAIL (loc, N_("declaring noncontig union for depth > 9, too many pieces"));
1551
1552 obstack_printf (pool, "%*sunion {\n", indent++ * 2, "");
1553
1554 obstack_printf (pool, "%*schar bytes[%" PRIu64 "];\n",
1555 indent * 2, "", loc->byte_size);
1556
1557 if (loc->type == loc_noncontiguous)
1558 {
1559 Dwarf_Word offset = 0;
1560 struct location *p;
1561 obstack_printf (pool, "%*sstruct {\n", indent++ * 2, "");
1562
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 }
1569
1570 obstack_printf (pool, "%*s} pieces __attribute__ ((packed));\n",
1571 --indent * 2, "");
1572 }
1573
1574 obstack_printf (pool, "%*suint%" PRIu64 "_t whole;\n",
1575 indent * 2, "", loc->byte_size * 8);
1576
1577 obstack_printf (pool, "%*s} u_pieces%d;\n", --indent * 2, "", depth);
1578
1579 loc = new_synthetic_loc (pool, *input, false);
1580 loc->type = loc_decl;
1581 (*input)->next = loc;
1582 *input = loc;
1583 }
1584
1585 /* Determine the byte size of a base type. */
1586 static Dwarf_Word
1587 base_byte_size (Dwarf_Die *typedie, struct location *origin)
1588 {
1589 assert (dwarf_tag (typedie) == DW_TAG_base_type ||
1590 dwarf_tag (typedie) == DW_TAG_enumeration_type);
1591
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
1598 FAIL (origin,
1599 N_("cannot get byte_size attribute for type %s: %s"),
1600 dwarf_diename (typedie) ?: "<anonymous>",
1601 dwarf_errmsg (-1));
1602 return -1;
1603 }
1604
1605 static Dwarf_Word
1606 base_encoding (Dwarf_Die *typedie, struct location *origin)
1607 {
1608 if (! (dwarf_tag (typedie) == DW_TAG_base_type ||
1609 dwarf_tag (typedie) == DW_TAG_enumeration_type))
1610 return -1;
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
1618 (void) origin;
1619 /*
1620 FAIL (origin,
1621 N_("cannot get encoding attribute for type %s: %s"),
1622 dwarf_diename (typedie) ?: "<anonymous>",
1623 dwarf_errmsg (-1));
1624 */
1625 return -1;
1626 }
1627
1628
1629
1630 /* Fetch the bitfield parameters. */
1631 static void
1632 get_bitfield (struct location *loc,
1633 Dwarf_Die *die, Dwarf_Word *bit_offset, Dwarf_Word *bit_size)
1634 {
1635 Dwarf_Attribute attr_mem;
1636 if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL
1637 || dwarf_formudata (&attr_mem, bit_offset) != 0
1638 || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL
1639 || dwarf_formudata (&attr_mem, bit_size) != 0)
1640 FAIL (loc, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
1641 }
1642
1643 /* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
1644 at the *INPUT location and store it in lvalue TARGET. */
1645 static void
1646 translate_base_fetch (struct obstack *pool, int indent,
1647 Dwarf_Word byte_size, bool signed_p,
1648 struct location **input, const char *target,
1649 int depth)
1650 {
1651 bool deref = false;
1652
1653 if ((*input)->type == loc_noncontiguous)
1654 {
1655 struct location *p = (*input)->pieces;
1656
1657 declare_noncontig_union (pool, indent, input, *input, depth);
1658
1659 Dwarf_Word offset = 0;
1660 char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1661 piece[8] = (char) ('0' + depth);
1662 int pdepth = depth + 1;
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
1671 snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20,
1672 "%" PRIu64, offset);
1673 translate_base_fetch (pool, indent, p->byte_size, signed_p /* ? */,
1674 input, piece, pdepth);
1675 (*input)->type = loc_fragment;
1676
1677 offset += p->byte_size;
1678 p = p->next;
1679 pdepth++;
1680 }
1681
1682 obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2,
1683 "", target, depth);
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
1691 declare_noncontig_union (pool, indent, input, *input, depth);
1692
1693 for (i = 0; i < byte_size; ++i)
1694 obstack_printf (pool, "%*su_pieces%d.bytes[%zu] = %#x;\n", indent * 2,
1695 "", depth, i, constant_block[i]);
1696
1697 obstack_printf (pool, "%*s%s = u_pieces%d.whole;\n", indent * 2,
1698 "", target, depth);
1699 }
1700 else
1701 switch (byte_size)
1702 {
1703 case 0: /* Special case, means address size. */
1704 case 1:
1705 case 2:
1706 case 4:
1707 case 8:
1708 obstack_printf (pool, "%*s", indent * 2, "");
1709 deref = emit_base_fetch (pool, byte_size, signed_p, target, *input);
1710 obstack_printf (pool, "\n");
1711 break;
1712
1713 default:
1714 /* Could handle this generating call to memcpy equivalent. */
1715 FAIL (*input, N_("fetch is larger than base integer types"));
1716 break;
1717 }
1718
1719 struct location *loc = new_synthetic_loc (pool, *input, deref);
1720 loc->byte_size = byte_size;
1721 loc->type = loc_final;
1722 (*input)->next = loc;
1723 *input = loc;
1724 }
1725
1726 /* Determine the maximum size of a base type, from some DIE in the CU. */
1727 static Dwarf_Word
1728 max_fetch_size (struct location *loc, Dwarf_Die *die)
1729 {
1730 Dwarf_Die cu_mem;
1731 uint8_t address_size;
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. */
1742 void
1743 c_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
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)
1760 encoding = base_encoding (typedie, *input);
1761 bool signed_p = encoding == DW_ATE_signed || encoding == DW_ATE_signed_char;
1762
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
1771 translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0);
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
1788 translate_base_fetch (pool, indent, byte_size, signed_p, input, target, 0);
1789 }
1790
1791 /* Translate a fragment to store RVALUE into the base-type value of
1792 BYTE_SIZE bytes at the *INPUT location. */
1793 static void
1794 translate_base_store (struct obstack *pool, int indent, Dwarf_Word byte_size,
1795 struct location **input, struct location *store_loc,
1796 const char *rvalue, int depth)
1797 {
1798 bool deref = false;
1799
1800 if (store_loc->type == loc_noncontiguous)
1801 {
1802 declare_noncontig_union (pool, indent, input, store_loc, depth);
1803
1804 obstack_printf (pool, "%*su_pieces%d.whole = %s;\n", indent * 2,
1805 "", depth, rvalue);
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;
1812 char piece[sizeof "u_pieces?.pieces.p" + 20] = "u_pieces?.pieces.p";
1813 piece[8] = (char) ('0' + depth);
1814 struct location *p;
1815 int pdepth = depth + 1;
1816 for (p = store_loc->pieces; p != NULL; p = p->next)
1817 {
1818 struct location *newp = obstack_alloc (pool, sizeof *newp);
1819 *newp = *p;
1820 newp->next = NULL;
1821 (*input)->next = newp;
1822 *input = newp;
1823
1824 snprintf (&piece[sizeof "u_pieces?.pieces.p" - 1], 20, "%" PRIu64,
1825 offset);
1826 translate_base_store (pool, indent,
1827 p->byte_size, input, *input, piece, pdepth++);
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
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
1864 void
1865 c_translate_store (struct obstack *pool, int indent,
1866 Dwarf_Addr dwbias __attribute__ ((unused)),
1867 Dwarf_Die *die, Dwarf_Die *typedie,
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)
1876 byte_size = base_byte_size (typedie, *input);
1877
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)
1882 encoding = base_encoding (typedie, *input);
1883 bool signed_p = (encoding == DW_ATE_signed
1884 || encoding == DW_ATE_signed_char);
1885
1886 *input = discontiguify (pool, indent, *input, byte_size,
1887 max_fetch_size (*input, die));
1888
1889 struct location *store_loc = *input;
1890
1891 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
1892 {
1893 /* This is a bit field. Fetch the containing base type into a
1894 temporary variable. */
1895
1896 translate_base_fetch (pool, indent, byte_size, signed_p, input, "tmp", 0);
1897 (*input)->type = loc_fragment;
1898 (*input)->address.declare = "tmp";
1899
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
1917 translate_base_store (pool, indent, byte_size, input, store_loc, rvalue, 0);
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
1926 void
1927 c_translate_pointer (struct obstack *pool, int indent,
1928 Dwarf_Addr dwbias __attribute__ ((unused)),
1929 Dwarf_Die *typedie, struct location **input)
1930 {
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);
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
1945 bool signed_p = false; /* XXX: Does not matter? */
1946
1947 translate_base_fetch (pool, indent + 1, byte_size, signed_p, input, "addr", 0);
1948 (*input)->type = loc_address;
1949 }
1950
1951
1952 void
1953 c_translate_addressof (struct obstack *pool, int indent,
1954 Dwarf_Addr dwbias __attribute__ ((unused)),
1955 Dwarf_Die *die,
1956 Dwarf_Die *typedie __attribute__ ((unused)),
1957 struct location **input, const char *target)
1958 {
1959 ++indent;
1960
1961 if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
1962 FAIL (*input, N_("cannot take the address of a bit field"));
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:
1973 FAIL (*input, N_("cannot take address of object in register"));
1974 break;
1975 case loc_noncontiguous:
1976 FAIL (*input, N_("cannot take address of noncontiguous object"));
1977 break;
1978 case loc_value:
1979 FAIL (*input, N_("cannot take address of computed value"));
1980 break;
1981 case loc_constant:
1982 FAIL (*input, N_("cannot take address of constant value"));
1983 break;
1984
1985 default:
1986 abort();
1987 break;
1988 }
1989 }
1990
1991
1992 /* Translate a fragment to write the given pointer value,
1993 where *INPUT is the location of the pointer with that type.
1994 */
1995
1996 void
1997 c_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,
2015 input, *input, rvalue, 0);
2016
2017 // XXX: what about multiple-location lvalues?
2018 }
2019
2020 /* Determine the element stride of an array type. */
2021 static Dwarf_Word
2022 array_stride (Dwarf_Die *typedie, struct location *origin)
2023 {
2024 Dwarf_Attribute attr_mem;
2025 if (dwarf_attr_integrate (typedie, DW_AT_byte_stride, &attr_mem) != NULL)
2026 {
2027 Dwarf_Word stride;
2028 if (dwarf_formudata (&attr_mem, &stride) == 0)
2029 return stride;
2030 FAIL (origin, N_("cannot get byte_stride attribute array type %s: %s"),
2031 dwarf_diename (typedie) ?: "<anonymous>",
2032 dwarf_errmsg (-1));
2033 }
2034
2035 Dwarf_Die die_mem;
2036 if (dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem) == NULL
2037 || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
2038 FAIL (origin, N_("cannot get element type of array type %s: %s"),
2039 dwarf_diename (typedie) ?: "<anonymous>",
2040 dwarf_errmsg (-1));
2041
2042 int typetag = dwarf_tag(&die_mem);
2043 while (typetag == DW_TAG_typedef ||
2044 typetag == DW_TAG_const_type ||
2045 typetag == DW_TAG_volatile_type)
2046 {
2047 if (dwarf_attr_integrate (&die_mem, DW_AT_type, &attr_mem) == NULL
2048 || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
2049 FAIL (origin, N_("cannot get inner type of type %s: %s"),
2050 dwarf_diename (&die_mem) ?: "<anonymous>",
2051 dwarf_errmsg (-1));
2052 typetag = dwarf_tag(&die_mem);
2053 }
2054
2055 if (dwarf_attr_integrate (&die_mem, DW_AT_byte_size, &attr_mem) != NULL)
2056 {
2057 Dwarf_Word stride;
2058 if (dwarf_formudata (&attr_mem, &stride) == 0)
2059 return stride;
2060 FAIL (origin,
2061 N_("cannot get byte_size attribute for array element type %s: %s"),
2062 dwarf_diename (&die_mem) ?: "<anonymous>",
2063 dwarf_errmsg (-1));
2064 }
2065
2066 FAIL (origin, N_("confused about array element size"));
2067 return 0;
2068 }
2069
2070 void
2071 c_translate_array (struct obstack *pool, int indent,
2072 Dwarf_Addr dwbias __attribute__ ((unused)),
2073 Dwarf_Die *typedie, struct location **input,
2074 const char *idx, Dwarf_Word const_idx)
2075 {
2076 assert (dwarf_tag (typedie) == DW_TAG_array_type ||
2077 dwarf_tag (typedie) == DW_TAG_pointer_type);
2078
2079 ++indent;
2080
2081 Dwarf_Word stride = array_stride (typedie, *input);
2082
2083 struct location *loc = *input;
2084 while (loc->type == loc_noncontiguous)
2085 {
2086 if (idx != NULL)
2087 FAIL (*input, N_("cannot dynamically index noncontiguous array"));
2088 else
2089 {
2090 Dwarf_Word offset = const_idx * stride;
2091 struct location *piece = loc->pieces;
2092 while (piece != NULL && offset >= piece->byte_size)
2093 {
2094 offset -= piece->byte_size;
2095 piece = piece->next;
2096 }
2097 if (piece == NULL)
2098 FAIL (*input, N_("constant index is outside noncontiguous array"));
2099 if (offset % stride != 0)
2100 FAIL (*input, N_("noncontiguous array splits elements"));
2101 const_idx = offset / stride;
2102 loc = piece;
2103 }
2104 }
2105
2106 switch (loc->type)
2107 {
2108 case loc_address:
2109 ++indent;
2110 if (idx != NULL)
2111 obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n",
2112 indent * 2, "", idx, stride);
2113 else
2114 obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n",
2115 indent * 2, "", const_idx, stride);
2116 loc = new_synthetic_loc (pool, loc, false);
2117 break;
2118
2119 case loc_register:
2120 if (idx != NULL)
2121 FAIL (*input, N_("cannot index array stored in a register"));
2122 else if (const_idx > max_fetch_size (loc, typedie) / stride)
2123 FAIL (*input, N_("constant index is outside array held in register"));
2124 else
2125 {
2126 loc->reg.offset += const_idx * stride;
2127 return;
2128 }
2129 break;
2130
2131 case loc_constant:
2132 if (idx != NULL)
2133 FAIL (*input, N_("cannot index into constant value"));
2134 else if (const_idx > loc->byte_size / stride)
2135 FAIL (*input, N_("constant index is outside constant array value"));
2136 else
2137 {
2138 loc->byte_size = stride;
2139 loc->constant_block += const_idx * stride;
2140 return;
2141 };
2142 break;
2143
2144 case loc_value:
2145 FAIL (*input, N_("cannot index into computed value"));
2146 break;
2147
2148 default:
2149 abort();
2150 break;
2151 }
2152
2153 (*input)->next = loc;
2154 *input = (*input)->next;
2155 }
2156
2157 \f
2158 /* Emitting C code for finalized fragments. */
2159
2160
2161 #define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)
2162
2163 /* Open a block with a comment giving the original DWARF expression. */
2164 static void
2165 emit_header (FILE *out, struct location *loc, unsigned int hindent)
2166 {
2167 if (loc->ops == NULL)
2168 emit ("%*s{ // synthesized\n", hindent * 2, "");
2169 else
2170 {
2171 emit ("%*s{ // DWARF expression:", hindent * 2, "");
2172 size_t i;
2173 for (i = 0; i < loc->nops; ++i)
2174 {
2175 emit (" %#x", loc->ops[i].atom);
2176 if (loc->ops[i].number2 == 0)
2177 {
2178 if (loc->ops[i].number != 0)
2179 emit ("(%" PRId64 ")", loc->ops[i].number);
2180 }
2181 else
2182 emit ("(%" PRId64 ",%" PRId64 ")",
2183 loc->ops[i].number, loc->ops[i].number2);
2184 }
2185 emit ("\n");
2186 }
2187 }
2188
2189 /* Emit a code fragment to assign the target variable to a register value. */
2190 static void
2191 emit_loc_register (FILE *out, struct location *loc, unsigned int indent,
2192 const char *target)
2193 {
2194 assert (loc->type == loc_register);
2195
2196 if (loc->reg.offset != 0)
2197 FAIL (loc, N_("cannot handle offset into register in fetch"));
2198
2199 emit ("%*s%s = fetch_register (%u);\n",
2200 indent * 2, "", target, loc->reg.regno);
2201 }
2202
2203 /* Emit a code fragment to assign the target variable to an address. */
2204 static void
2205 emit_loc_address (FILE *out, struct location *loc, unsigned int indent,
2206 const char *target)
2207 {
2208 assert (loc->type == loc_address || loc->type == loc_value);
2209
2210 if (loc->address.stack_depth == 0)
2211 /* Synthetic program. */
2212 emit ("%s", loc->address.program);
2213 else
2214 {
2215 emit ("%*s{\n", indent * 2, "");
2216 emit ("%*s%s " STACKFMT, (indent + 1) * 2, "",
2217 stack_slot_type (loc, false), 0);
2218 unsigned i;
2219 for (i = 1; i < loc->address.stack_depth; ++i)
2220 emit (", " STACKFMT, i);
2221 emit (";\n");
2222
2223 emit ("%s%*s%s = " STACKFMT ";\n", loc->address.program,
2224 (indent + 1) * 2, "", target, 0);
2225 emit ("%*s}\n", indent * 2, "");
2226 }
2227 }
2228
2229 /* Emit a code fragment to declare the target variable and
2230 assign it to an address-sized value. */
2231 static void
2232 emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
2233 const char *target, bool declare,
2234 bool *used_deref, unsigned int *max_stack)
2235 {
2236 if (declare)
2237 emit ("%*s%s %s;\n", indent * 2, "", stack_slot_type (loc, false), target);
2238
2239 emit_header (out, loc, indent++);
2240
2241 switch (loc->type)
2242 {
2243 default:
2244 abort ();
2245 break;
2246
2247 case loc_register:
2248 emit_loc_register (out, loc, indent, target);
2249 break;
2250
2251 case loc_address:
2252 case loc_value:
2253 emit_loc_address (out, loc, indent, target);
2254 *used_deref = *used_deref || loc->address.used_deref;
2255 if (loc->address.stack_depth > *max_stack)
2256 *max_stack = loc->address.stack_depth;
2257 break;
2258 }
2259
2260 emit ("%*s}\n", --indent * 2, "");
2261 }
2262
2263 bool
2264 c_emit_location (FILE *out, struct location *loc, int indent,
2265 unsigned int *max_stack)
2266 {
2267 emit ("%*s{\n", indent * 2, "");
2268
2269 bool declared_addr = false;
2270 struct location *l;
2271 for (l = loc; l != NULL; l = l->next)
2272 switch (l->type)
2273 {
2274 case loc_decl:
2275 emit ("%s", l->address.program);
2276 break;
2277
2278 case loc_address:
2279 case loc_value:
2280 if (declared_addr)
2281 break;
2282 declared_addr = true;
2283 l->address.declare = "addr";
2284 case loc_fragment:
2285 case loc_final:
2286 if (l->address.declare != NULL)
2287 {
2288 if (l->byte_size == 0 || l->byte_size == (Dwarf_Word) -1)
2289 emit ("%*s%s %s;\n", (indent + 1) * 2, "",
2290 stack_slot_type (l, false), l->address.declare);
2291 else
2292 emit ("%*suint%" PRIu64 "_t %s;\n", (indent + 1) * 2, "",
2293 l->byte_size * 8, l->address.declare);
2294 }
2295
2296 default:
2297 break;
2298 }
2299
2300 bool deref = false;
2301 *max_stack = 0;
2302
2303 if (loc->frame_base != NULL)
2304 emit_loc_value (out, loc->frame_base, indent, "frame_base", true,
2305 &deref, max_stack);
2306
2307 for (; loc->next != NULL; loc = loc->next)
2308 switch (loc->type)
2309 {
2310 case loc_address:
2311 case loc_value:
2312 /* Emit the program fragment to calculate the address. */
2313 emit_loc_value (out, loc, indent + 1, "addr", false, &deref, max_stack);
2314 break;
2315
2316 case loc_fragment:
2317 emit ("%s", loc->address.program);
2318 deref = deref || loc->address.used_deref;
2319 break;
2320
2321 case loc_decl:
2322 case loc_register:
2323 case loc_noncontiguous:
2324 case loc_constant:
2325 /* These don't produce any code directly.
2326 The next address/final record incorporates the value. */
2327 break;
2328
2329 case loc_final: /* Should be last in chain! */
2330 default:
2331 abort ();
2332 break;
2333 }
2334
2335 if (loc->type != loc_final) /* Unfinished chain. */
2336 abort ();
2337
2338 emit ("%s%*s}\n", loc->address.program, indent * 2, "");
2339
2340 if (loc->address.stack_depth > *max_stack)
2341 *max_stack = loc->address.stack_depth;
2342
2343 return deref || loc->address.used_deref;
2344 }
2345
2346 #undef emit
2347
2348 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.139932 seconds and 6 git commands to generate.