summaryrefslogtreecommitdiffstats
path: root/loc2c.c
blob: e7c0737155562473723642ecdc3c7c86aad3fcbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
#include <config.h>
#include <inttypes.h>
#include <stdbool.h>
#include <obstack.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <dwarf.h>
#include <elfutils/libdw.h>
#include <assert.h>
#include "loc2c.h"

#define N_(x) x

#define STACK_TYPE	"intptr_t"  /* Must be the signed type.  */
#define UTYPE		"uintptr_t" /* Must be the unsigned type.  */
#define SFORMAT		"%" PRId64 "L"
#define UFORMAT		"%" PRIu64 "UL"
#define AFORMAT		"%#" PRIx64 "UL"
#define STACKFMT	"s%u"

struct location
{
  struct location *next;

  void (*fail) (void *arg, const char *fmt, ...)
    __attribute__ ((noreturn, format (printf, 2, 3)));
  void *fail_arg;
  void (*emit_address) (void *fail_arg, struct obstack *, Dwarf_Addr);

  const Dwarf_Op *ops;
  size_t nops;

  Dwarf_Word byte_size;

  enum
    {
      loc_address, loc_register, loc_noncontiguous,
      loc_decl, loc_fragment, loc_final
    } type;
  struct location *frame_base;
  union
  {
    struct			/* loc_address, loc_fragment, loc_final */
    {
      const char *declare;	/* Temporary that needs declared.  */
      char *program;		/* C fragment, leaves address in s0.  */
      unsigned int stack_depth;	/* Temporaries "s0..<N>" used by it.  */
      bool used_deref;		/* Program uses "deref" macro.  */
    } address;
    unsigned int regno;		/* loc_register */
    struct location *pieces;	/* loc_noncontiguous */
  };
};

static struct location *
alloc_location (struct obstack *pool, struct location *origin)
{
  struct location *loc = obstack_alloc (pool, sizeof *loc);
  loc->fail = origin->fail;
  loc->fail_arg = origin->fail_arg;
  loc->emit_address = origin->emit_address;
  loc->byte_size = 0;
  loc->frame_base = NULL;
  return loc;
}

#define FAIL(loc, fmt, ...) \
  (*(loc)->fail) ((loc)->fail_arg, fmt, ## __VA_ARGS__)

static void
default_emit_address (void *fail_arg __attribute__ ((unused)),
		      struct obstack *pool, Dwarf_Addr address)
{
  obstack_printf (pool, AFORMAT, address);
}

/* Synthesize a new loc_address using the program on the obstack.  */
static struct location *
new_synthetic_loc (struct obstack *pool, struct location *origin, bool deref)
{
  obstack_1grow (pool, '\0');
  char *program = obstack_finish (pool);

  struct location *loc = alloc_location (pool, origin);
  loc->next = NULL;
  loc->byte_size = 0;
  loc->type = loc_address;
  loc->address.program = program;
  loc->address.stack_depth = 0;
  loc->address.declare = NULL;
  loc->address.used_deref = deref;

  if (origin->type == loc_register)
    {
      loc->ops = origin->ops;
      loc->nops = origin->nops;
    }
  else
    {
      loc->ops = NULL;
      loc->nops = 0;
    }

  return loc;
}


/* Die in the middle of an expression.  */
static struct location *
lose (struct location *loc,
      const char *failure, const Dwarf_Op *lexpr, size_t i)
{
  FAIL (loc, N_("%s in DWARF expression [%Zu] at %" PRIu64
		" (%#x: %" PRId64 ", %" PRId64 ")"),
	failure, i, lexpr[i].offset,
	lexpr[i].atom, lexpr[i].number, lexpr[i].number2);
  return NULL;
}

/* Translate a (constrained) DWARF expression into C code
   emitted to the obstack POOL.  INDENT is the number of indentation levels.
   ADDRBIAS is the difference between runtime and Dwarf info addresses.
   INPUT is null or an expression to be initially pushed on the stack.
   If NEED_FB is null, fail on DW_OP_fbreg, else set *NEED_FB to true
   and emit "frame_base" for it.  On success, set *MAX_STACK to the number
   of stack slots required.  On failure, set *LOSER to the index in EXPR
   of the operation we could not handle.

   Returns a failure message or null for success.  */

static const char *
translate (struct obstack *pool, int indent, Dwarf_Addr addrbias,
	   const Dwarf_Op *expr, const size_t len,
	   struct location *input,
	   bool *need_fb, size_t *loser,
	   struct location *loc)
{
  loc->ops = expr;
  loc->nops = len;

#define DIE(msg) return (*loser = i, N_(msg))

#define emit(fmt, ...) obstack_printf (pool, fmt, ## __VA_ARGS__)

  unsigned int stack_depth = 0, max_stack = 0;
  inline void deepen (void)
    {
      if (stack_depth == max_stack)
	++max_stack;
    }

#define POP(var)							      \
    if (stack_depth > 0)						      \
      --stack_depth;							      \
    else if (tos_register != -1)					      \
      fetch_tos_register ();						      \
    else								      \
      goto underflow;							      \
    int var = stack_depth
#define PUSH 		(deepen (), stack_depth++)
#define STACK(idx)	(stack_depth - 1 - (idx))

  /* Don't put stack operations in the arguments to this.  */
#define push(fmt, ...) \
  emit ("%*s" STACKFMT " = " fmt ";\n", indent * 2, "", PUSH, ## __VA_ARGS__)

  int tos_register = -1;
  inline void fetch_tos_register (void)
    {
      deepen ();
      emit ("%*s" STACKFMT " = fetch_register (%d);\n",
	    indent * 2, "", stack_depth, tos_register);
      tos_register = -1;
    }

  if (input != NULL)
    switch (input->type)
      {
      case loc_address:
	push ("addr");
	break;

      case loc_register:
	tos_register = input->regno;
	break;

      default:
	abort ();
	break;
      }

  size_t i;

  bool used_deref = false;
  inline const char *finish (struct location *piece)
    {
      if (stack_depth > 1)
	DIE ("multiple values left on stack");
      if (stack_depth == 1)
	{
	  obstack_1grow (pool, '\0');
	  char *program = obstack_finish (pool);
	  piece->type = loc_address;
	  piece->address.declare = NULL;
	  piece->address.program = program;
	  piece->address.stack_depth = max_stack;
	  piece->address.used_deref = used_deref;
	  used_deref = false;
	}
      else if (tos_register == -1)
	DIE ("stack underflow");
      else if (obstack_object_size (pool) != 0)
	DIE ("register value must stand alone in location expression");
      else
	{
	  piece->type = loc_register;
	  piece->regno = tos_register;
	}
      return NULL;
    }

  struct location *pieces = NULL, **tailpiece = &pieces;
  size_t piece_expr_start = 0;
  Dwarf_Word piece_total_bytes = 0;
  for (i = 0; i < len; ++i)
    {
      unsigned int reg;
      uint_fast8_t sp;
      Dwarf_Word value;

      switch (expr[i].atom)
	{
	  /* Basic stack operations.  */
	case DW_OP_nop:
	  break;

	case DW_OP_dup:
	  if (stack_depth < 1)
	    goto underflow;
	  else
	    {
	      unsigned int tos = STACK (0);
	      push (STACKFMT, tos);
	    }
	  break;

	case DW_OP_drop:
	  POP (ignore);
	  emit ("%*s/* drop " STACKFMT "*/\n", indent * 2, "", ignore);
	  break;

	case DW_OP_pick:
	  sp = expr[i].number;
	op_pick:
	  if (sp >= stack_depth)
	    goto underflow;
	  sp = STACK (sp);
	  push (STACKFMT, sp);
	  break;

	case DW_OP_over:
	  sp = 1;
	  goto op_pick;

	case DW_OP_swap:
	  if (stack_depth < 2)
	    goto underflow;
	  deepen ();		/* Use a temporary slot.  */
	  emit ("%*s"
		STACKFMT " = " STACKFMT ", "
		STACKFMT " = " STACKFMT ", "
		STACKFMT " = " STACKFMT ";\n",
		indent * 2, "",
		STACK (-1), STACK (0),
		STACK (0), STACK (1),
		STACK (1), STACK (-1));
	  break;

	case DW_OP_rot:
	  if (stack_depth < 3)
	    goto underflow;
	  deepen ();		/* Use a temporary slot.  */
	  emit ("%*s"
		STACKFMT " = " STACKFMT ", "
		STACKFMT " = " STACKFMT ", "
		STACKFMT " = " STACKFMT ", "
		STACKFMT " = " STACKFMT ";\n",
		indent * 2, "",
		STACK (-1), STACK (0),
		STACK (0), STACK (1),
		STACK (1), STACK (2),
		STACK (3), STACK (-1));
	  break;


	  /* Control flow operations.  */
	case DW_OP_skip:
	  {
	    Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
	    while (i + 1 < len && expr[i + 1].offset < target)
	      ++i;
	    if (expr[i + 1].offset != target)
	      DIE ("invalid skip target");
	    break;
	  }

	case DW_OP_bra:
	  DIE ("conditional branches not supported");
	  break;


	  /* Memory access.  */
	case DW_OP_deref:
	  {
	    POP (addr);
	    push ("deref (sizeof (void *), " STACKFMT ")", addr);
	    used_deref = true;
	  }
	  break;

	case DW_OP_deref_size:
	  {
	    POP (addr);
	    push ("deref (" UFORMAT ", " STACKFMT ")",
		  expr[i].number, addr);
	    used_deref = true;
	  }
	  break;

	case DW_OP_xderef:
	  {
	    POP (addr);
	    POP (as);
	    push ("xderef (sizeof (void *), " STACKFMT ", " STACKFMT ")",
		  addr, as);
	    used_deref = true;
	  }
	  break;

	case DW_OP_xderef_size:
	  {
	    POP (addr);
	    POP (as);
	    push ("xderef (" UFORMAT ", " STACKFMT ", " STACKFMT ")",
		  expr[i].number, addr, as);
	    used_deref = true;
	  }
	  break;

	  /* Constant-value operations.  */

	case DW_OP_addr:
	  emit ("%*s" STACKFMT " = ", indent * 2, "", PUSH);
	  (*loc->emit_address) (loc->fail_arg, pool,
				addrbias + expr[i].number);
	  emit (";\n");
	  break;

	case DW_OP_lit0 ... DW_OP_lit31:
	  value = expr[i].atom - DW_OP_lit0;
	  goto op_const;

	case DW_OP_const1u:
	case DW_OP_const1s:
	case DW_OP_const2u:
	case DW_OP_const2s:
	case DW_OP_const4u:
	case DW_OP_const4s:
	case DW_OP_const8u:
	case DW_OP_const8s:
	case DW_OP_constu:
	case DW_OP_consts:
	  value = expr[i].number;
	op_const:
	  push (SFORMAT, value);
	  break;

	  /* Arithmetic operations.  */
#define UNOP(dw_op, c_op)						      \
	case DW_OP_##dw_op:						      \
	  {								      \
	    POP (tos);							      \
	    push ("%s (" STACKFMT ")", #c_op, tos);			      \
	  }								      \
	  break
#define BINOP(dw_op, c_op)						      \
	case DW_OP_##dw_op:						      \
	  {								      \
	    POP (b);							      \
	    POP (a);							      \
	    push (STACKFMT " %s " STACKFMT, a, #c_op, b);		      \
	  }								      \
	  break

	  UNOP (abs, op_abs);
	  BINOP (and, &);
	  BINOP (div, /);
	  BINOP (minus, -);
	  BINOP (mod, %);
	  BINOP (mul, *);
	  UNOP (neg, -);
	  UNOP (not, ~);
	  BINOP (or, |);
	  BINOP (plus, +);
	  BINOP (shl, <<);
	  BINOP (shra, >>);
	  BINOP (xor, ^);

	  /* Comparisons are binary operators too.  */
	  BINOP (le, <=);
	  BINOP (ge, >=);
	  BINOP (eq, ==);
	  BINOP (lt, <);
	  BINOP (gt, >);
	  BINOP (ne, !=);

#undef	UNOP
#undef	BINOP

	case DW_OP_shr:
	  {
	    POP (b);
	    POP (a);
	    push ("(%s) " STACKFMT " >> (%s)" STACKFMT,
		  UTYPE, a, UTYPE, b);
	    break;
	  }

	case DW_OP_plus_uconst:
	  {
	    POP (x);
	    push (STACKFMT " + " UFORMAT, x, expr[i].number);
	  }
	  break;


	  /* Register-relative addressing.  */
	case DW_OP_breg0 ... DW_OP_breg31:
	  reg = expr[i].atom - DW_OP_breg0;
	  value = expr[i].number;
	  goto op_breg;

	case DW_OP_bregx:
	  reg = expr[i].number;
	  value = expr[i].number2;
	op_breg:
	  push ("fetch_register (%u) + " SFORMAT, reg, value);
	  break;

	case DW_OP_fbreg:
	  if (need_fb == NULL)
	    DIE ("DW_OP_fbreg from DW_AT_frame_base");
	  *need_fb = true;
	  push ("frame_base + " SFORMAT, expr[i].number);
	  break;

	  /* Direct register contents.  */
	case DW_OP_reg0 ... DW_OP_reg31:
	  reg = expr[i].atom - DW_OP_reg0;
	  goto op_reg;

	case DW_OP_regx:
	  reg = expr[i].number;
	op_reg:
	  tos_register = reg;
	  break;

	  /* Special magic.  */
	case DW_OP_piece:
	  if (stack_depth > 1)
	    /* If this ever happens we could copy the program.  */
	    DIE ("DW_OP_piece left multiple values on stack");
	  else
	    {
	      /* The obstack has a pending program for loc_address,
		 so we must finish that piece off before we can
		 allocate again.  */
	      struct location temp_piece =
		{
		  .fail = loc->fail,
		  .fail_arg = loc->fail_arg,
		  .emit_address = loc->emit_address,
		  .frame_base = NULL,
		  .ops = &expr[piece_expr_start],
		  .nops = i - piece_expr_start,
		};
	      const char *failure = finish (&temp_piece);
	      if (failure != NULL)
		return failure;

	      struct location *piece = obstack_alloc (pool, sizeof *piece);
	      *piece = temp_piece;

	      piece_expr_start = i + 1;

	      piece_total_bytes += piece->byte_size = expr[i].number;

	      *tailpiece = piece;
	      tailpiece = &piece->next;
	      piece->next = NULL;
	    }
	  break;

	case DW_OP_push_object_address:
	  DIE ("XXX DW_OP_push_object_address");
	  break;

	default:
	  DIE ("unrecognized operation");
	  break;
	}
    }

  if (pieces == NULL)
    return finish (loc);

  if (piece_expr_start != i)
    DIE ("extra operations after last DW_OP_piece");

  loc->type = loc_noncontiguous;
  loc->pieces = pieces;
  loc->byte_size = piece_total_bytes;

  return NULL;

 underflow:
  DIE ("stack underflow");

#undef emit
#undef push
#undef PUSH
#undef POP
#undef STACK
#undef DIE
}

/* Translate a location starting from an address or nothing.  */
static struct location *
location_from_address (struct obstack *pool,
		       void (*fail) (void *arg, const char *fmt, ...)
		         __attribute__ ((noreturn, format (printf, 2, 3))),
		       void *fail_arg,
		       void (*emit_address) (void *fail_arg,
					     struct obstack *, Dwarf_Addr),
		       int indent, Dwarf_Addr dwbias,
		       const Dwarf_Op *expr, size_t len, Dwarf_Addr address,
		       struct location **input, Dwarf_Attribute *fb_attr)
{
  struct location *loc = obstack_alloc (pool, sizeof *loc);
  loc->fail = *input == NULL ? fail : (*input)->fail;
  loc->fail_arg = *input == NULL ? fail_arg : (*input)->fail_arg;
  loc->emit_address = *input == NULL ? emit_address : (*input)->emit_address;
  loc->byte_size = 0;
  loc->frame_base = NULL;

  bool need_fb = false;
  size_t loser;
  const char *failure = translate (pool, indent + 1, dwbias, expr, len,
				   *input, &need_fb, &loser, loc);
  if (failure != NULL)
    return lose (loc, failure, expr, loser);

  loc->next = NULL;
  if (need_fb)
    {
      /* The main expression uses DW_OP_fbreg, so we need to compute
	 the DW_AT_frame_base attribute expression's value first.  */

      if (fb_attr == NULL)
	FAIL (loc, N_("required DW_AT_frame_base attribute not supplied"));

      Dwarf_Op *fb_expr;
      size_t fb_len;
      switch (dwarf_getlocation_addr (fb_attr, address - dwbias,
				      &fb_expr, &fb_len, 1))
	{
	case 1:			/* Should always happen.  */
	  if (fb_len == 0)
	    goto fb_inaccessible;
	  break;

	default:		/* Shouldn't happen.  */
	case -1:
	  FAIL (loc, N_("dwarf_getlocation_addr (form %#x): %s"),
		dwarf_whatform (fb_attr), dwarf_errmsg (-1));
	  return NULL;

	case 0:			/* Shouldn't happen.  */
	fb_inaccessible:
	  FAIL (loc, N_("DW_AT_frame_base not accessible at this address"));
	  return NULL;
	}

      loc->frame_base = alloc_location (pool, loc);
      failure = translate (pool, indent + 1, dwbias, fb_expr, fb_len, NULL,
			   NULL, &loser, loc->frame_base);
      if (failure != NULL)
	return lose (loc, failure, fb_expr, loser);
    }

  if (*input != NULL)
    (*input)->next = loc;
  *input = loc;

  return loc;
}

/* Translate a location starting from a non-address "on the top of the
   stack".  The *INPUT location is a register name or noncontiguous
   object specification, and this expression wants to find the "address"
   of an object relative to that "address".  */

static struct location *
location_relative (struct obstack *pool,
		   int indent, Dwarf_Addr dwbias,
		   const Dwarf_Op *expr, size_t len, Dwarf_Addr address,
		   struct location **input, Dwarf_Attribute *fb_attr)
{
  Dwarf_Sword *stack;
  unsigned int stack_depth = 0, max_stack = 0;
  inline void deepen (void)
    {
      if (stack_depth == max_stack)
	{
	  ++max_stack;
	  obstack_blank (pool, sizeof stack[0]);
	  stack = (void *) obstack_base (pool);
	}
    }

#define POP(var)							      \
    if (stack_depth > 0)						      \
      --stack_depth;							      \
    else								      \
      goto underflow;							      \
    int var = stack_depth
#define PUSH 		(deepen (), stack_depth++)
#define STACK(idx)	(stack_depth - 1 - (idx))
#define STACKWORD(idx)	stack[STACK (idx)]

  /* Don't put stack operations in the arguments to this.  */
#define push(value) (stack[PUSH] = (value))

  const char *failure = NULL;
#define DIE(msg) do { failure = N_(msg); goto fail; } while (0)

  struct location *head = NULL;
  size_t i;
  for (i = 0; i < len; ++i)
    {
      uint_fast8_t sp;
      Dwarf_Word value;

      switch (expr[i].atom)
	{
	  /* Basic stack operations.  */
	case DW_OP_nop:
	  break;

	case DW_OP_dup:
	  if (stack_depth < 1)
	    goto underflow;
	  else
	    {
	      unsigned int tos = STACK (0);
	      push (stack[tos]);
	    }
	  break;

	case DW_OP_drop:
	  if (stack_depth > 0)
	    --stack_depth;
	  else if (*input != NULL)
	    /* Mark that we have consumed the input.  */
	    *input = NULL;
	  else
	    /* Hits if cleared above, or if we had no input at all.  */
	    goto underflow;
	  break;

	case DW_OP_pick:
	  sp = expr[i].number;
	op_pick:
	  if (sp >= stack_depth)
	    goto underflow;
	  sp = STACK (sp);
	  push (stack[sp]);
	  break;

	case DW_OP_over:
	  sp = 1;
	  goto op_pick;

	case DW_OP_swap:
	  if (stack_depth < 2)
	    goto underflow;
	  deepen ();		/* Use a temporary slot.  */
	  STACKWORD (-1) = STACKWORD (0);
	  STACKWORD (0) = STACKWORD (1);
	  STACKWORD (1) = STACKWORD (-1);
	  break;

	case DW_OP_rot:
	  if (stack_depth < 3)
	    goto underflow;
	  deepen ();		/* Use a temporary slot.  */
	  STACKWORD (-1) = STACKWORD (0);
	  STACKWORD (0) = STACKWORD (1);
	  STACKWORD (2) = STACKWORD (2);
	  STACKWORD (2) = STACKWORD (-1);
	  break;


	  /* Control flow operations.  */
	case DW_OP_bra:
	  {
	    POP (taken);
	    if (stack[taken] == 0)
	      break;
	  }
	  /*FALLTHROUGH*/

	case DW_OP_skip:
	  {
	    Dwarf_Off target = expr[i].offset + 3 + expr[i].number;
	    while (i + 1 < len && expr[i + 1].offset < target)
	      ++i;
	    if (expr[i + 1].offset != target)
	      DIE ("invalid skip target");
	    break;
	  }

	  /* Memory access.  */
	case DW_OP_deref:
	case DW_OP_deref_size:
	case DW_OP_xderef:
	case DW_OP_xderef_size:

	  /* Register-relative addressing.  */
	case DW_OP_breg0 ... DW_OP_breg31:
	case DW_OP_bregx:
	case DW_OP_fbreg:

	  /* This started from a register, but now it's following a pointer.
	     So we can do the translation starting from address here.  */
	  return location_from_address (pool, NULL, NULL, NULL, indent, dwbias,
					expr, len, address, input, fb_attr);


	  /* Constant-value operations.  */
	case DW_OP_addr:
	  DIE ("static calculation depends on load-time address");
	  push (dwbias + expr[i].number);
	  break;

	case DW_OP_lit0 ... DW_OP_lit31:
	  value = expr[i].atom - DW_OP_lit0;
	  goto op_const;

	case DW_OP_const1u:
	case DW_OP_const1s:
	case DW_OP_const2u:
	case DW_OP_const2s:
	case DW_OP_const4u:
	case DW_OP_const4s:
	case DW_OP_const8u:
	case DW_OP_const8s:
	case DW_OP_constu:
	case DW_OP_consts:
	  value = expr[i].number;
	op_const:
	  push (value);
	  break;

	  /* Arithmetic operations.  */
#define UNOP(dw_op, c_op)						      \
	case DW_OP_##dw_op:						      \
	  {								      \
	    POP (tos);							      \
	    push (c_op (stack[tos])); 					      \
	  }								      \
	  break
#define BINOP(dw_op, c_op)						      \
	case DW_OP_##dw_op:						      \
	  {								      \
	    POP (b);							      \
	    POP (a);							      \
 	    push (stack[a] c_op stack[b]);				      \
	  }								      \
	  break

#define op_abs(x) (x < 0 ? -x : x)
	  UNOP (abs, op_abs);
	  BINOP (and, &);
	  BINOP (div, /);
	  BINOP (mod, %);
	  BINOP (mul, *);
	  UNOP (neg, -);
	  UNOP (not, ~);
	  BINOP (or, |);
	  BINOP (shl, <<);
	  BINOP (shra, >>);
	  BINOP (xor, ^);

	  /* Comparisons are binary operators too.  */
	  BINOP (le, <=);
	  BINOP (ge, >=);
	  BINOP (eq, ==);
	  BINOP (lt, <);
	  BINOP (gt, >);
	  BINOP (ne, !=);

#undef	UNOP
#undef	BINOP

	case DW_OP_shr:
	  {
	    POP (b);
	    POP (a);
	    push ((Dwarf_Word) stack[a] >> (Dwarf_Word) stack[b]);
	    break;
	  }

	  /* Simple addition we may be able to handle relative to
	     the starting register name.  */
	case DW_OP_minus:
	  {
	    POP (tos);
	    value = -stack[tos];
	    goto plus;
	  }
	case DW_OP_plus:
	  {
	    POP (tos);
	    value = stack[tos];
	    goto plus;
	  }
	case DW_OP_plus_uconst:
	  value = expr[i].number;
	plus:
	  if (stack_depth > 0)
	    {
	      /* It's just private diddling after all.  */
	      POP (a);
	      push (stack[a] + value);
	      break;
	    }
	  if (*input == NULL)
	    goto underflow;

	  /* This is the primary real-world case: the expression takes
	     the input address and adds a constant offset.  */

	  while ((*input)->type == loc_noncontiguous)
	    {
	      /* We are starting from a noncontiguous object (DW_OP_piece).
		 Find the piece we want.  */

	      struct location *piece = (*input)->pieces;
	      while (piece != NULL && value >= piece->byte_size)
		{
		  value -= piece->byte_size;
		  piece = piece->next;
		}
	      if (piece == NULL)
		DIE ("offset outside available pieces");

	      *input = piece;
	    }

	  switch ((*input)->type)
	    {
	    case loc_address:
	      {
		/* The piece we want is actually in memory.  Use the same
		   program to compute the address from the preceding input.  */

		struct location *loc = alloc_location (pool, *input);
		*loc = **input;
		if (head == NULL)
		  head = loc;
		(*input)->next = loc;
		if (value == 0)
		  {
		    /* The piece addresses exactly where we want to go.  */
		    loc->next = NULL;
		    *input = loc;
		  }
		else
		  {
		    /* Add a second fragment to offset the piece address.  */
		    obstack_printf (pool, "%*saddr += " SFORMAT "\n",
				    indent * 2, "", value);
		    *input = loc->next = new_synthetic_loc (pool, *input,
							    false);
		  }

		if (i + 1 < len)
		  {
		    /* This expression keeps going, but further
		       computations now have an address to start with.
		       So we can punt to the address computation generator.  */
		    loc = location_from_address (pool, NULL, NULL, NULL,
						 indent, dwbias,
						 &expr[i + 1], len - i - 1,
						 address, input, fb_attr);
		    if (loc == NULL)
		      return NULL;
		  }

		/* That's all she wrote.  */
		return head;
	      }

	    case loc_register:
	      // XXX

	    default:
	      abort ();
	    }
	  break;

	  /* Direct register contents.  */
	case DW_OP_reg0 ... DW_OP_reg31:
	case DW_OP_regx:
	  DIE ("register");
	  break;

	  /* Special magic.  */
	case DW_OP_piece:
	  DIE ("DW_OP_piece");
	  break;

	case DW_OP_push_object_address:
	  DIE ("XXX DW_OP_push_object_address");
	  break;

	default:
	  DIE ("unrecognized operation");
	  break;
	}
    }

  if (stack_depth > 1)
    DIE ("multiple values left on stack");

  if (stack_depth > 0)		/* stack_depth == 1 */
    {
      if (*input != NULL)
	DIE ("multiple values left on stack");

      /* Could handle this if it ever actually happened.  */
      DIE ("relative expression computed constant");
    }

  return head;

 underflow:
  if (*input == NULL)
    DIE ("stack underflow");
  else
    DIE ("cannot handle location expression");

 fail:
  return lose (*input, failure, expr, i);
}


/* Translate a C fragment for the location expression, using *INPUT
   as the starting location, begin from scratch if *INPUT is null.
   If DW_OP_fbreg is used, it may have a subfragment computing from
   the FB_ATTR location expression.

   On errors, call FAIL and never return.  On success, return the
   first fragment created, which is also chained onto (*INPUT)->next.
   *INPUT is then updated with the new tail of that chain.  */

struct location *
c_translate_location (struct obstack *pool,
		      void (*fail) (void *arg, const char *fmt, ...)
		        __attribute__ ((noreturn, format (printf, 2, 3))),
		      void *fail_arg,
		      void (*emit_address) (void *fail_arg,
					    struct obstack *, Dwarf_Addr),
		      int indent, Dwarf_Addr dwbias, Dwarf_Addr pc_address,
		      const Dwarf_Op *expr, size_t len,
		      struct location **input, Dwarf_Attribute *fb_attr)
{
  indent += 2;

  switch (*input == NULL ? loc_address : (*input)->type)
    {
    case loc_address:
      /* We have a previous address computation.
	 This expression will compute starting with that on the stack.  */
      return location_from_address (pool, fail, fail_arg,
				    emit_address ?: &default_emit_address,
				    indent, dwbias, expr, len, pc_address,
				    input, fb_attr);

    case loc_noncontiguous:
    case loc_register:
      /* The starting point is not an address computation, but a
	 register.  We can only handle limited computations from here.  */
      return location_relative (pool, indent, dwbias, expr, len, pc_address,
				input, fb_attr);

    default:
      abort ();
      break;
    }

  return NULL;
}

/* Emit "uintNN_t TARGET = ...;".  */
static bool
emit_base_fetch (struct obstack *pool, Dwarf_Word byte_size,
		 const char *target, bool decl, struct location *loc)
{
  if (decl)
    obstack_printf (pool, "uint%" PRIu64 "_t ", byte_size * 8);

  switch (loc->type)
    {
    case loc_address:
      if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
	obstack_printf (pool, "%s = deref (%" PRIu64 ", addr);",
			target, byte_size);
      else
	obstack_printf (pool, "%s = deref (sizeof %s, addr);",
			target, target);
      return true;

    case loc_register:
      obstack_printf (pool, "%s = fetch_register (%u);", target, loc->regno);
      break;

    case loc_noncontiguous:
      FAIL (loc, N_("noncontiguous location for base fetch"));
      break;

    default:
      abort ();
      break;
    }

  return false;
}

/* Emit "... = RVALUE;".  */
static bool
emit_base_store (struct obstack *pool, Dwarf_Word byte_size,
		 const char *rvalue, struct location *loc)
{
  switch (loc->type)
    {
    case loc_address:
      if (byte_size != 0 && byte_size != (Dwarf_Word) -1)
	obstack_printf (pool, "store_deref (%" PRIu64 ", addr, %s); ",
			byte_size, rvalue);
      else
	obstack_printf (pool, "store_deref (sizeof %s, addr, %s); ",
			rvalue, rvalue);
      return true;

    case loc_register:
      obstack_printf (pool, "store_register (%u, %s);", loc->regno, rvalue);
      break;

    case loc_noncontiguous:
      FAIL (loc, N_("noncontiguous location for base store"));
      break;

    default:
      abort ();
      break;
    }

  return false;
}


/* Slice up an object into pieces no larger than MAX_PIECE_BYTES,
   yielding a loc_noncontiguous location unless LOC is small enough.  */
static struct location *
discontiguify (struct obstack *pool, int indent, struct location *loc,
	       Dwarf_Word total_bytes, Dwarf_Word max_piece_bytes)
{
  inline bool pieces_small_enough (void)
    {
      if (loc->type != loc_noncontiguous)
	return (loc->byte_size ?: total_bytes) <= max_piece_bytes;
      for (struct location *p = loc->pieces; p != NULL; p = p->next)
	if (p->byte_size > max_piece_bytes)
	  return false;
      return true;
    }

  if (pieces_small_enough ())
    return loc;

  struct location *noncontig = alloc_location (pool, loc);
  noncontig->next = NULL;
  noncontig->type = loc_noncontiguous;
  noncontig->byte_size = total_bytes;
  noncontig->pieces = NULL;
  struct location **tailpiece = &noncontig->pieces;
  inline void add (struct location *piece)
    {
      *tailpiece = piece;
      tailpiece = &piece->next;
    }

  switch (loc->type)
    {
    case loc_address:
      {
	/* Synthesize a piece that sets "container_addr" to the computed
	   address of the whole object.  Each piece will refer to this.  */
	obstack_printf (pool, "%*scontainer_addr = addr;\n",
			indent++ * 2, "");
	loc->next = new_synthetic_loc (pool, loc, false);
	loc->next->byte_size = loc->byte_size;
	loc->next->type = loc_fragment;
	loc->next->address.declare = "container_addr";
	loc = loc->next;

	/* Synthesize pieces that just compute "container_addr + N".  */
	Dwarf_Word offset = 0;
	while (total_bytes - offset > 0)
	  {
	    Dwarf_Word size = total_bytes - offset;
	    if (size > max_piece_bytes)
	      size = max_piece_bytes;

	    obstack_printf (pool, "%*saddr = container_addr + " UFORMAT ";\n",
			    indent * 2, "", offset);
	    struct location *piece = new_synthetic_loc (pool, loc, false);
	    piece->byte_size = size;
	    add (piece);

	    offset += size;
	  }

	--indent;
	break;
      }

    case loc_register:
      FAIL (loc, N_("single register too big for fetch/store ???"));
      break;

    case loc_noncontiguous:
      /* Could be handled if it ever happened.  */
      FAIL (loc, N_("discontiguify of noncontiguous location not supported"));
      break;

    default:
      abort ();
      break;
    }

  loc->next = noncontig;
  return noncontig;
}

/* Make a fragment that declares a union such as:
    union {
      char bytes[8];
      struct {
        uint32_t p0;
        uint32_t p4;
      } pieces __attribute__ ((packed));
      uint64_t whole;
    } u;
*/
static void
declare_noncontig_union (struct obstack *pool, int indent,
			 struct location **input, struct location *loc)
{
  obstack_printf (pool, "%*sunion {\n", indent++ * 2, "");

  obstack_printf (pool, "%*schar bytes[%" PRIu64 "];\n",
		  indent * 2, "", loc->byte_size);

  obstack_printf (pool, "%*sstruct {\n", indent++ * 2, "");

  Dwarf_Word offset = 0;
  for (struct location *p = loc->pieces; p != NULL; p = p->next)
    {
      obstack_printf (pool, "%*suint%" PRIu64 "_t p%" PRIu64 ";\n",
		      indent * 2, "", p->byte_size * 8, offset);
      offset += p->byte_size;
    }

  obstack_printf (pool, "%*s} pieces __attribute__ ((packed));\n",
		  --indent * 2, "");

  obstack_printf (pool, "%*suint%" PRIu64 "_t whole;\n",
		  indent * 2, "", loc->byte_size * 8);

  obstack_printf (pool, "%*s} u;\n", --indent * 2, "");

  loc = new_synthetic_loc (pool, *input, false);
  loc->type = loc_decl;
  (*input)->next = loc;
  *input = loc;
}

/* Determine the byte size of a base type.  */
static Dwarf_Word
base_byte_size (Dwarf_Die *typedie, struct location *origin)
{
  assert (dwarf_tag (typedie) == DW_TAG_base_type ||
	  dwarf_tag (typedie) == DW_TAG_enumeration_type);

  Dwarf_Attribute attr_mem;
  Dwarf_Word size;
  if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) != NULL
      && dwarf_formudata (&attr_mem, &size) == 0)
    return size;

  FAIL (origin,
	 N_("cannot get byte_size attribute for type %s: %s"),
	 dwarf_diename (typedie) ?: "<anonymous>",
	 dwarf_errmsg (-1));
  return -1;
}

/* Fetch the bitfield parameters.  */
static void
get_bitfield (struct location *loc,
	      Dwarf_Die *die, Dwarf_Word *bit_offset, Dwarf_Word *bit_size)
{
  Dwarf_Attribute attr_mem;
  if (dwarf_attr_integrate (die, DW_AT_bit_offset, &attr_mem) == NULL
      || dwarf_formudata (&attr_mem, bit_offset) != 0
      || dwarf_attr_integrate (die, DW_AT_bit_size, &attr_mem) == NULL
      || dwarf_formudata (&attr_mem, bit_size) != 0)
    FAIL (loc, N_("cannot get bit field parameters: %s"), dwarf_errmsg (-1));
}

/* Translate a fragment to fetch the base-type value of BYTE_SIZE bytes
   at the *INPUT location and store it in lvalue TARGET.  */
static void
translate_base_fetch (struct obstack *pool, int indent, Dwarf_Word byte_size,
		      struct location **input, const char *target)
{
  bool deref = false;

  if ((*input)->type == loc_noncontiguous)
    {
      struct location *p = (*input)->pieces;

      declare_noncontig_union (pool, indent, input, *input);

      Dwarf_Word offset = 0;
      char piece[sizeof "u.pieces.p" + 20] = "u.pieces.p";
      while (p != NULL)
	{
	  struct location *newp = obstack_alloc (pool, sizeof *newp);
	  *newp = *p;
	  newp->next = NULL;
	  (*input)->next = newp;
	  *input = newp;

	  snprintf (&piece[sizeof "u.pieces.p" - 1], 20, "%" PRIu64, offset);
	  translate_base_fetch (pool, indent, p->byte_size, input, piece);
	  (*input)->type = loc_fragment;

	  offset += p->byte_size;
	  p = p->next;
	}

      obstack_printf (pool, "%*s%s = u.whole;\n", indent * 2, "", target);
    }
  else
    switch (byte_size)
      {
      case 0:			/* Special case, means address size.  */
      case 1:
      case 2:
      case 4:
      case 8:
	obstack_printf (pool, "%*s", indent * 2, "");
	deref = emit_base_fetch (pool, byte_size, target, false, *input);
	obstack_printf (pool, "\n");
	break;

      default:
	/* Could handle this generating call to memcpy equivalent.  */
	FAIL (*input, N_("fetch is larger than base integer types"));
	break;
      }

  struct location *loc = new_synthetic_loc (pool, *input, deref);
  loc->byte_size = byte_size;
  loc->type = loc_final;
  (*input)->next = loc;
  *input = loc;
}

/* Determine the maximum size of a base type, from some DIE in the CU.  */
static Dwarf_Word
max_fetch_size (struct location *loc, Dwarf_Die *die)
{
  Dwarf_Die cu_mem;
  uint8_t address_size;
  Dwarf_Die *cu = dwarf_diecu (die, &cu_mem, &address_size, NULL);
  if (cu == NULL)
    FAIL (loc, N_("cannot determine CU address size from %s: %s"),
	  dwarf_diename (die), dwarf_errmsg (-1));

  return address_size;
}

/* Translate a fragment to fetch the value of variable or member DIE
   at the *INPUT location and store it in lvalue TARGET.  */
void
c_translate_fetch (struct obstack *pool, int indent,
		   Dwarf_Addr dwbias __attribute__ ((unused)),
		   Dwarf_Die *die, Dwarf_Die *typedie,
		   struct location **input, const char *target)
{
  ++indent;

  Dwarf_Attribute size_attr;
  Dwarf_Word byte_size;
  if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL
      || dwarf_formudata (&size_attr, &byte_size) != 0)
    byte_size = base_byte_size (typedie, *input);

  *input = discontiguify (pool, indent, *input, byte_size,
			  max_fetch_size (*input, die));

  if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
    {
      /* This is a bit field.  Fetch the containing base type into a
	 temporary variable.  */

      translate_base_fetch (pool, indent, byte_size, input, "tmp");
      (*input)->type = loc_fragment;
      (*input)->address.declare = "tmp";

      Dwarf_Word bit_offset, bit_size;
      get_bitfield (*input, die, &bit_offset, &bit_size);

      obstack_printf (pool, "%*s"
		      "fetch_bitfield (%s, tmp, %" PRIu64 ", %" PRIu64 ");\n",
		      indent *2, "", target, bit_offset, bit_size);

      struct location *loc = new_synthetic_loc (pool, *input, false);
      loc->type = loc_final;
      (*input)->next = loc;
      *input = loc;
    }
  else
    translate_base_fetch (pool, indent, byte_size, input, target);
}

/* Translate a fragment to store RVALUE into the base-type value of
   BYTE_SIZE bytes at the *INPUT location.  */
static void
translate_base_store (struct obstack *pool, int indent, Dwarf_Word byte_size,
		      struct location **input, struct location *store_loc,
		      const char *rvalue)
{
  bool deref = false;

  if (store_loc->type == loc_noncontiguous)
    {
      declare_noncontig_union (pool, indent, input, store_loc);

      obstack_printf (pool, "%*su.whole = %s;\n", indent * 2, "", rvalue);
      struct location *loc = new_synthetic_loc (pool, *input, deref);
      loc->type = loc_fragment;
      (*input)->next = loc;
      *input = loc;

      Dwarf_Word offset = 0;
      char piece[sizeof "u.pieces.p" + 20] = "u.pieces.p";
      for (struct location *p = store_loc->pieces; p != NULL; p = p->next)
	{
	  struct location *newp = obstack_alloc (pool, sizeof *newp);
	  *newp = *p;
	  newp->next = NULL;
	  (*input)->next = newp;
	  *input = newp;

	  snprintf (&piece[sizeof "u.pieces.p" - 1], 20, "%" PRIu64, offset);
	  translate_base_store (pool, indent,
				p->byte_size, input, *input, piece);
	  (*input)->type = loc_fragment;

	  offset += p->byte_size;
	}

      (*input)->type = loc_final;
    }
  else
    {
      switch (byte_size)
	{
	case 1:
	case 2:
	case 4:
	case 8:
	  obstack_printf (pool, "%*s", indent * 2, "");
	  deref = emit_base_store (pool, byte_size, rvalue, store_loc);
	  obstack_printf (pool, "\n");
	  break;

	default:
	  /* Could handle this generating call to memcpy equivalent.  */
	  FAIL (*input, N_("store is larger than base integer types"));
	  break;
	}

      struct location *loc = new_synthetic_loc (pool, *input, deref);
      loc->type = loc_final;
      (*input)->next = loc;
      *input = loc;
    }
}

/* Translate a fragment to fetch the value of variable or member DIE
   at the *INPUT location and store it in rvalue RVALUE.  */

void
c_translate_store (struct obstack *pool, int indent,
		   Dwarf_Addr dwbias __attribute__ ((unused)),
		   Dwarf_Die *die, Dwarf_Die *typedie,
		   struct location **input, const char *rvalue)
{
  ++indent;

  Dwarf_Attribute size_attr;
  Dwarf_Word byte_size;
  if (dwarf_attr_integrate (die, DW_AT_byte_size, &size_attr) == NULL
      || dwarf_formudata (&size_attr, &byte_size) != 0)
    byte_size = base_byte_size (typedie, *input);

  *input = discontiguify (pool, indent, *input, byte_size,
			  max_fetch_size (*input, die));

  struct location *store_loc = *input;

  if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
    {
      /* This is a bit field.  Fetch the containing base type into a
	 temporary variable.  */

      translate_base_fetch (pool, indent, byte_size, input, "tmp");
      (*input)->type = loc_fragment;
      (*input)->address.declare = "tmp";

      Dwarf_Word bit_offset, bit_size;
      get_bitfield (*input, die, &bit_offset, &bit_size);

      obstack_printf (pool, "%*s"
		      "store_bitfield (tmp, %s, %" PRIu64 ", %" PRIu64 ");\n",
		      indent * 2, "", rvalue, bit_offset, bit_size);

      struct location *loc = new_synthetic_loc (pool, *input, false);
      loc->type = loc_fragment;
      (*input)->next = loc;
      *input = loc;

      /* We have mixed RVALUE into the bits in "tmp".
	 Now we'll store "tmp" back whence we fetched it.  */
      rvalue = "tmp";
    }

  translate_base_store (pool, indent, byte_size, input, store_loc, rvalue);
}

/* Translate a fragment to dereference the given pointer type,
   where *INPUT is the location of the pointer with that type.

   We chain on a loc_address program that yields this pointer value
   (i.e. the location of what it points to).  */

void
c_translate_pointer (struct obstack *pool, int indent,
		     Dwarf_Addr dwbias __attribute__ ((unused)),
		     Dwarf_Die *typedie, struct location **input)
{
  assert (dwarf_tag (typedie) == DW_TAG_pointer_type);

  Dwarf_Attribute attr_mem;
  Dwarf_Word byte_size;
  if (dwarf_attr_integrate (typedie, DW_AT_byte_size, &attr_mem) == NULL)
    byte_size = 0;
  else if (dwarf_formudata (&attr_mem, &byte_size) != 0)
    FAIL (*input,
	  N_("cannot get byte_size attribute for type %s: %s"),
	  dwarf_diename (typedie) ?: "<anonymous>",
	  dwarf_errmsg (-1));

  translate_base_fetch (pool, indent + 1, byte_size, input, "addr");
  (*input)->type = loc_address;
}


void
c_translate_addressof (struct obstack *pool, int indent,
		       Dwarf_Addr dwbias __attribute__ ((unused)),
		       Dwarf_Die *die,
		       Dwarf_Die *typedie __attribute__ ((unused)),
		       struct location **input, const char *target)
{
  ++indent;

  if (dwarf_hasattr_integrate (die, DW_AT_bit_offset))
    FAIL (*input, N_("cannot take the address of a bit field"));

  switch ((*input)->type)
    {
    case loc_address:
      obstack_printf (pool, "%*s%s = addr;\n", indent * 2, "", target);
      (*input)->next = new_synthetic_loc (pool, *input, false);
      (*input)->next->type = loc_final;
      break;

    case loc_register:
      FAIL (*input, N_("cannot take address of object in register"));
      break;
    case loc_noncontiguous:
      FAIL (*input, N_("cannot take address of noncontiguous object"));
      break;

    default:
      abort();
      break;
    }
}


/* Determine the element stride of an array type.  */
static Dwarf_Word
array_stride (Dwarf_Die *typedie, struct location *origin)
{
  Dwarf_Attribute attr_mem;
  if (dwarf_attr_integrate (typedie, DW_AT_stride_size, &attr_mem) != NULL)
    {
      Dwarf_Word stride;
      if (dwarf_formudata (&attr_mem, &stride) == 0)
	return stride;
      FAIL (origin, N_("cannot get stride_size attribute array type %s: %s"),
	    dwarf_diename (typedie) ?: "<anonymous>",
	    dwarf_errmsg (-1));
    }

  Dwarf_Die die_mem;
  if (dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem) == NULL
      || dwarf_formref_die (&attr_mem, &die_mem) == NULL)
    FAIL (origin, N_("cannot get element type of array type %s: %s"),
	  dwarf_diename (typedie) ?: "<anonymous>",
	  dwarf_errmsg (-1));

  if (dwarf_attr_integrate (&die_mem, DW_AT_byte_size, &attr_mem) != NULL)
    {
      Dwarf_Word stride;
      if (dwarf_formudata (&attr_mem, &stride) == 0)
	return stride;
      FAIL (origin,
	    N_("cannot get byte_size attribute for array element type %s: %s"),
	    dwarf_diename (&die_mem) ?: "<anonymous>",
	    dwarf_errmsg (-1));
    }

  FAIL (origin, N_("confused about array element size"));
  return 0;
}

void
c_translate_array (struct obstack *pool, int indent,
		   Dwarf_Addr dwbias __attribute__ ((unused)),
		   Dwarf_Die *typedie, struct location **input,
		   const char *idx, Dwarf_Word const_idx)
{
  assert (dwarf_tag (typedie) == DW_TAG_array_type);

  ++indent;

  Dwarf_Word stride = array_stride (typedie, *input);

  struct location *loc = *input;
  while (loc->type == loc_noncontiguous)
    {
      if (idx != NULL)
	FAIL (*input, N_("cannot dynamically index noncontiguous array"));
      else
	{
	  Dwarf_Word offset = const_idx * stride;
	  struct location *piece = loc->pieces;
	  while (piece != NULL && offset >= piece->byte_size)
	    {
	      offset -= piece->byte_size;
	      piece = piece->next;
	    }
	  if (piece == NULL)
	    FAIL (*input, N_("constant index is outside noncontiguous array"));
	  if (offset % stride != 0)
	    FAIL (*input, N_("noncontiguous array splits elements"));
	  const_idx = offset / stride;
	  loc = piece;
	}
    }

  switch (loc->type)
    {
    case loc_address:
      ++indent;
      if (idx != NULL)
	obstack_printf (pool, "%*saddr += %s * " UFORMAT ";\n",
			indent * 2, "", idx, stride);
      else
	obstack_printf (pool, "%*saddr += " UFORMAT " * " UFORMAT ";\n",
			indent * 2, "", const_idx, stride);
      loc = new_synthetic_loc (pool, loc, false);
      break;

    case loc_register:
      FAIL (*input, N_("cannot index array stored in a register"));
      break;

    default:
      abort();
      break;
    }

  (*input)->next = loc;
  *input = (*input)->next;
}


/* Emitting C code for finalized fragments.  */


#define emit(fmt, ...) fprintf (out, fmt, ## __VA_ARGS__)

/* Open a block with a comment giving the original DWARF expression.  */
static void
emit_header (FILE *out, struct location *loc, unsigned int hindent)
{
  if (loc->ops == NULL)
    emit ("%*s{ // synthesized\n", hindent * 2, "");
  else
    {
      emit ("%*s{ // DWARF expression:", hindent * 2, "");
      for (size_t i = 0; i < loc->nops; ++i)
	{
	  emit (" %#x", loc->ops[i].atom);
	  if (loc->ops[i].number2 == 0)
	    {
	      if (loc->ops[i].number != 0)
		emit ("(%" PRId64 ")", loc->ops[i].number);
	    }
	  else
	    emit ("(%" PRId64 ",%" PRId64 ")",
		  loc->ops[i].number, loc->ops[i].number2);
	}
      emit ("\n");
    }
}

/* Emit a code fragment to assign the target variable to a register value.  */
static void
emit_loc_register (FILE *out, struct location *loc, unsigned int indent,
		   const char *target)
{
  assert (loc->type == loc_register);

  emit ("%*s%s = fetch_register (%u);\n",
	indent * 2, "", target, loc->regno);
}

/* Emit a code fragment to assign the target variable to an address.  */
static void
emit_loc_address (FILE *out, struct location *loc, unsigned int indent,
		  const char *target)
{
  assert (loc->type == loc_address);

  if (loc->address.stack_depth == 0)
    /* Synthetic program.  */
    emit ("%s", loc->address.program);
  else
    {
      emit ("%*s{\n", indent * 2, "");
      emit ("%*s%s " STACKFMT, (indent + 1) * 2, "", STACK_TYPE, 0);
      for (unsigned int i = 1; i < loc->address.stack_depth; ++i)
	emit (", " STACKFMT, i);
      emit (";\n");

      emit ("%s%*s%s = " STACKFMT ";\n", loc->address.program,
	    (indent + 1) * 2, "", target, 0);
      emit ("%*s}\n", indent * 2, "");
    }
}

/* Emit a code fragment to declare the target variable and
   assign it to an address-sized value.  */
static void
emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
		const char *target, bool declare)
{
  if (declare)
    emit ("%*s%s %s;\n", indent * 2, "", STACK_TYPE, target);

  emit_header (out, loc, indent++);

  switch (loc->type)
    {
    default:
      abort ();
      break;

    case loc_register:
      emit_loc_register (out, loc, indent, target);
      break;

    case loc_address:
      emit_loc_address (out, loc, indent, target);
      break;
    }

  emit ("%*s}\n", --indent * 2, "");
}

bool
c_emit_location (FILE *out, struct location *loc, int indent)
{
  emit ("%*s{\n", indent * 2, "");

  bool declared_addr = false;
  for (struct location *l = loc; l != NULL; l = l->next)
    switch (l->type)
      {
      case loc_decl:
	emit ("%s", l->address.program);
	break;

      case loc_address:
	if (declared_addr)
	  break;
	declared_addr = true;
	l->address.declare = "addr";
      case loc_fragment:
      case loc_final:
	if (l->address.declare != NULL)
	  {
	    if (l->byte_size == 0 || l->byte_size == (Dwarf_Word) -1)
	      emit ("%*s%s %s;\n", (indent + 1) * 2, "",
		    STACK_TYPE, l->address.declare);
	    else
	      emit ("%*suint%" PRIu64 "_t %s;\n", (indent + 1) * 2, "",
		    l->byte_size * 8, l->address.declare);
	  }

      default:
	break;
      }

  bool deref = false;

  if (loc->frame_base != NULL)
    emit_loc_value (out, loc->frame_base, indent, "frame_base", true);

  for (; loc->next != NULL; loc = loc->next)
    switch (loc->type)
      {
      case loc_address:
	/* Emit the program fragment to calculate the address.  */
	emit_loc_value (out, loc, indent + 1, "addr", false);
	deref = deref || loc->address.used_deref;
	break;

      case loc_fragment:
	emit ("%s", loc->address.program);
	deref = deref || loc->address.used_deref;
	break;

      case loc_decl:
      case loc_register:
      case loc_noncontiguous:
	/* These don't produce any code directly.
	   The next address/final record incorporates the value.  */
	break;

      case loc_final:		/* Should be last in chain!  */
      default:
	abort ();
	break;
      }

  if (loc->type != loc_final)	/* Unfinished chain.  */
    abort ();

  emit ("%s%*s}\n", loc->address.program, indent * 2, "");

  return deref || loc->address.used_deref;
}

#undef emit