summaryrefslogtreecommitdiff
path: root/gcc/jit/jit-recording.h
blob: 846d65cb20291e3dc2684815da121bda8a730be6 (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
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
/* Internals of libgccjit: classes for recording calls made to the JIT API.
   Copyright (C) 2013-2022 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifndef JIT_RECORDING_H
#define JIT_RECORDING_H

#include "jit-common.h"
#include "jit-logging.h"

class timer;

namespace gcc {

namespace jit {

extern const char * const unary_op_reproducer_strings[];
extern const char * const binary_op_reproducer_strings[];

class result;
class dump;
class reproducer;

/**********************************************************************
 Recording.
 **********************************************************************/

namespace recording {

playback::location *
playback_location (replayer *r, location *loc);

const char *
playback_string (string *str);

playback::block *
playback_block (block *b);

/* A recording of a call to gcc_jit_context_enable_dump.  */
struct requested_dump
{
  const char *m_dumpname;
  char **m_out_ptr;
};

/* A JIT-compilation context.  */
class context : public log_user
{
public:
  context (context *parent_ctxt);
  ~context ();

  builtins_manager *
  get_builtins_manager ();

  void record (memento *m);
  void replay_into (replayer *r);
  void disassociate_from_playback ();

  string *
  new_string (const char *text, bool escaped = false);

  location *
  new_location (const char *filename,
		int line,
		int column,
		bool created_by_user);

  type *
  get_type (enum gcc_jit_types type);

  type *
  get_int_type (int num_bytes, int is_signed);

  type *
  new_array_type (location *loc,
		  type *element_type,
		  int num_elements);

  field *
  new_field (location *loc,
	     type *type,
	     const char *name);

  field *
  new_bitfield (location *loc,
                type *type,
                int width,
                const char *name);

  struct_ *
  new_struct_type (location *loc,
		   const char *name);

  union_ *
  new_union_type (location *loc,
		  const char *name);

  function_type *
  new_function_type (type *return_type,
		     int num_params,
		     type **param_types,
		     int is_variadic);

  type *
  new_function_ptr_type (location *loc,
			 type *return_type,
			 int num_params,
			 type **param_types,
			 int is_variadic);

  param *
  new_param (location *loc,
	     type *type,
	     const char *name);

  function *
  new_function (location *loc,
		enum gcc_jit_function_kind kind,
		type *return_type,
		const char *name,
		int num_params,
		param **params,
		int is_variadic,
		enum built_in_function builtin_id);

  function *
  get_builtin_function (const char *name);

  lvalue *
  new_global (location *loc,
	      enum gcc_jit_global_kind kind,
	      type *type,
	      const char *name);

  rvalue *
  new_ctor (location *loc,
	    type *type,
	    size_t num_values,
	    field **fields,
	    rvalue **values);

  void
  new_global_init_rvalue (lvalue *variable,
			  rvalue *init);

  template <typename HOST_TYPE>
  rvalue *
  new_rvalue_from_const (type *type,
			 HOST_TYPE value);

  rvalue *
  new_string_literal (const char *value);

  rvalue *
  new_rvalue_from_vector (location *loc,
			  vector_type *type,
			  rvalue **elements);

  rvalue *
  new_unary_op (location *loc,
		enum gcc_jit_unary_op op,
		type *result_type,
		rvalue *a);

  rvalue *
  new_binary_op (location *loc,
		 enum gcc_jit_binary_op op,
		 type *result_type,
		 rvalue *a, rvalue *b);

  rvalue *
  new_comparison (location *loc,
		  enum gcc_jit_comparison op,
		  rvalue *a, rvalue *b);

  rvalue *
  new_call (location *loc,
	    function *func,
	    int numargs, rvalue **args);

  rvalue *
  new_call_through_ptr (location *loc,
			rvalue *fn_ptr,
			int numargs, rvalue **args);

  rvalue *
  new_cast (location *loc,
	    rvalue *expr,
	    type *type_);

  lvalue *
  new_array_access (location *loc,
		    rvalue *ptr,
		    rvalue *index);

  case_ *
  new_case (rvalue *min_value,
	    rvalue *max_value,
	    block *block);

  void
  set_str_option (enum gcc_jit_str_option opt,
		  const char *value);

  void
  set_int_option (enum gcc_jit_int_option opt,
		  int value);

  void
  set_bool_option (enum gcc_jit_bool_option opt,
		   int value);

  void
  set_inner_bool_option (enum inner_bool_option inner_opt,
			 int value);

  void
  add_command_line_option (const char *optname);

  void
  append_command_line_options (vec <char *> *argvec);

  void
  add_driver_option (const char *optname);

  void
  append_driver_options (auto_string_vec *argvec);

  void
  enable_dump (const char *dumpname,
	       char **out_ptr);

  const char *
  get_str_option (enum gcc_jit_str_option opt) const
  {
    return m_str_options[opt];
  }

  int
  get_int_option (enum gcc_jit_int_option opt) const
  {
    return m_int_options[opt];
  }

  int
  get_bool_option (enum gcc_jit_bool_option opt) const
  {
    return m_bool_options[opt];
  }

  int
  get_inner_bool_option (enum inner_bool_option opt) const
  {
    return m_inner_bool_options[opt];
  }

  result *
  compile ();

  void
  compile_to_file (enum gcc_jit_output_kind output_kind,
		   const char *output_path);

  void
  add_error (location *loc, const char *fmt, ...)
      GNU_PRINTF(3, 4);

  void
  add_error_va (location *loc, const char *fmt, va_list ap)
      GNU_PRINTF(3, 0);

  const char *
  get_first_error () const;

  const char *
  get_last_error () const;

  bool errors_occurred () const
  {
    if (m_parent_ctxt)
      if (m_parent_ctxt->errors_occurred ())
	return true;
    return m_error_count;
  }

  type *get_opaque_FILE_type ();

  void dump_to_file (const char *path, bool update_locations);

  void dump_reproducer_to_file (const char *path);

  void
  get_all_requested_dumps (vec <recording::requested_dump> *out);

  void set_timer (timer *t) { m_timer = t; }
  timer *get_timer () const { return m_timer; }

  void add_top_level_asm (location *loc, const char *asm_stmts);

private:
  void log_all_options () const;
  void log_str_option (enum gcc_jit_str_option opt) const;
  void log_int_option (enum gcc_jit_int_option opt) const;
  void log_bool_option (enum gcc_jit_bool_option opt) const;
  void log_inner_bool_option (enum inner_bool_option opt) const;

  void validate ();

private:
  context *m_parent_ctxt;

  /* The ultimate ancestor of the contexts within a family tree of
     contexts.  This has itself as its own m_toplevel_ctxt.  */
  context *m_toplevel_ctxt;

  timer *m_timer;

  int m_error_count;

  char *m_first_error_str;
  bool m_owns_first_error_str;

  char *m_last_error_str;
  bool m_owns_last_error_str;

  char *m_str_options[GCC_JIT_NUM_STR_OPTIONS];
  int m_int_options[GCC_JIT_NUM_INT_OPTIONS];
  bool m_bool_options[GCC_JIT_NUM_BOOL_OPTIONS];
  bool m_inner_bool_options[NUM_INNER_BOOL_OPTIONS];
  auto_vec <char *> m_command_line_options;
  auto_vec <char *> m_driver_options;

  /* Dumpfiles that were requested via gcc_jit_context_enable_dump.  */
  auto_vec<requested_dump> m_requested_dumps;

  /* Recorded API usage.  */
  auto_vec<memento *> m_mementos;

  /* Specific recordings, for use by dump_to_file.  */
  auto_vec<compound_type *> m_compound_types;
  auto_vec<global *> m_globals;
  auto_vec<function *> m_functions;
  auto_vec<top_level_asm *> m_top_level_asms;

  type *m_basic_types[NUM_GCC_JIT_TYPES];
  type *m_FILE_type;

  builtins_manager *m_builtins_manager; // lazily created
};


/* An object with lifetime managed by the context i.e.
   it lives until the context is released, at which
   point it itself is cleaned up.  */

class memento
{
public:
  virtual ~memento () {}

  /* Hook for replaying this.  */
  virtual void replay_into (replayer *r) = 0;

  void set_playback_obj (void *obj) { m_playback_obj = obj; }


  /* Get the context that owns this object.

     Implements the post-error-checking part of
     gcc_jit_object_get_context.  */
  context *get_context () { return m_ctxt; }

  memento *
  as_object () { return this; }

  /* Debugging hook, for use in generating error messages etc.
     Implements the post-error-checking part of
     gcc_jit_object_get_debug_string.  */
  const char *
  get_debug_string ();

  virtual void write_to_dump (dump &d);
  virtual void write_reproducer (reproducer &r) = 0;
  virtual location *dyn_cast_location () { return NULL; }

protected:
  memento (context *ctxt)
  : m_ctxt (ctxt),
    m_playback_obj (NULL),
    m_debug_string (NULL)
  {
    gcc_assert (ctxt);
  }

  string *new_string (const char *text) { return m_ctxt->new_string (text); }

private:
  virtual string * make_debug_string () = 0;

public:
  context *m_ctxt;

protected:
  void *m_playback_obj;

private:
  string *m_debug_string;
};

/* or just use std::string? */
class string : public memento
{
public:
  string (context *ctxt, const char *text, bool escaped);
  ~string ();

  const char *c_str () { return m_buffer; }

  static string * from_printf (context *ctxt, const char *fmt, ...)
    GNU_PRINTF(2, 3);

  void replay_into (replayer *) FINAL OVERRIDE {}

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  size_t m_len;
  char *m_buffer;

  /* Flag to track if this string is the result of string::make_debug_string,
     to avoid infinite recursion when logging all mementos: don't re-escape
     such strings.  */
  bool m_escaped;
};

class location : public memento
{
public:
  location (context *ctxt, string *filename, int line, int column,
	    bool created_by_user)
  : memento (ctxt),
    m_filename (filename),
    m_line (line),
    m_column (column),
    m_created_by_user (created_by_user)
 {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  playback::location *
  playback_location (replayer *r)
  {
    /* Normally during playback, we can walk forwards through the list of
       recording objects, playing them back.  The ordering of recording
       ensures that everything that a recording object refers to has
       already been played back, so we can simply look up the relevant
       m_playback_obj.

       Locations are an exception, due to the "write_to_dump" method of
       recording::statement.  This method can set a new location on a
       statement after the statement is created, and thus the location
       appears in the context's memento list *after* the statement that
       refers to it.

       In such circumstances, the statement is replayed *before* the location,
       when the latter doesn't yet have a playback object.

       Hence we need to ensure that locations have playback objects.  */
    if (!m_playback_obj)
      {
	replay_into (r);
      }
    gcc_assert (m_playback_obj);
    return static_cast <playback::location *> (m_playback_obj);
  }

  location *dyn_cast_location () FINAL OVERRIDE { return this; }
  bool created_by_user () const { return m_created_by_user; }

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  string *m_filename;
  int m_line;
  int m_column;
  bool m_created_by_user;
};

class type : public memento
{
public:
  type *get_pointer ();
  type *get_const ();
  type *get_volatile ();
  type *get_aligned (size_t alignment_in_bytes);
  type *get_vector (size_t num_units);

  /* Get the type obtained when dereferencing this type.

     This will return NULL if it's not valid to dereference this type.
     The caller is responsible for setting an error.  */
  virtual type *dereference () = 0;
  /* Get the type size in bytes.

     This is implemented only for memento_of_get_type and
     memento_of_get_pointer as it is used for initializing globals of
     these types.  */
  virtual size_t get_size () { gcc_unreachable (); }

  /* Dynamic casts.  */
  virtual function_type *dyn_cast_function_type () { return NULL; }
  virtual function_type *as_a_function_type() { gcc_unreachable (); return NULL; }
  virtual struct_ *dyn_cast_struct () { return NULL; }
  virtual vector_type *dyn_cast_vector_type () { return NULL; }

  /* Is it typesafe to copy to this type from rtype?  */
  virtual bool accepts_writes_from (type *rtype)
  {
    gcc_assert (rtype);
    return this->unqualified ()->is_same_type_as (rtype->unqualified ());
  }

  virtual bool is_same_type_as (type *other)
  {
    return this == other;
  }

  /* Strip off "const" etc */
  virtual type *unqualified ()
  {
    return this;
  }

  virtual bool is_int () const = 0;
  virtual bool is_float () const = 0;
  virtual bool is_bool () const = 0;
  virtual type *is_pointer () = 0;
  virtual type *is_volatile () { return NULL; }
  virtual type *is_const () { return NULL; }
  virtual type *is_array () = 0;
  virtual struct_ *is_struct () { return NULL; }
  virtual bool is_union () const { return false; }
  virtual bool is_void () const { return false; }
  virtual vector_type *is_vector () { return NULL; }
  virtual bool has_known_size () const { return true; }

  bool is_numeric () const
  {
    return is_int () || is_float () || is_bool ();
  }

  playback::type *
  playback_type ()
  {
    return static_cast <playback::type *> (m_playback_obj);
  }

  virtual const char *access_as_type (reproducer &r);

protected:
  type (context *ctxt)
    : memento (ctxt),
    m_pointer_to_this_type (NULL)
  {}

private:
  type *m_pointer_to_this_type;
};

/* Result of "gcc_jit_context_get_type".  */
class memento_of_get_type : public type
{
public:
  memento_of_get_type (context *ctxt,
		       enum gcc_jit_types kind)
  : type (ctxt),
    m_kind (kind) {}

  type *dereference () FINAL OVERRIDE;

  size_t get_size () FINAL OVERRIDE;

  bool accepts_writes_from (type *rtype) FINAL OVERRIDE
  {
    if (m_kind == GCC_JIT_TYPE_VOID_PTR)
      if (rtype->is_pointer ())
	{
	  /* LHS (this) is type (void *), and the RHS is a pointer:
	     accept it:  */
	  return true;
	}

    return type::accepts_writes_from (rtype);
  }

  bool is_int () const FINAL OVERRIDE;
  bool is_float () const FINAL OVERRIDE;
  bool is_bool () const FINAL OVERRIDE;
  type *is_pointer () FINAL OVERRIDE { return dereference (); }
  type *is_array () FINAL OVERRIDE { return NULL; }
  bool is_void () const FINAL OVERRIDE { return m_kind == GCC_JIT_TYPE_VOID; }

public:
  void replay_into (replayer *r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  enum gcc_jit_types m_kind;
};

/* Result of "gcc_jit_type_get_pointer".  */
class memento_of_get_pointer : public type
{
public:
  memento_of_get_pointer (type *other_type)
  : type (other_type->m_ctxt),
    m_other_type (other_type) {}

  type *dereference () FINAL OVERRIDE { return m_other_type; }

  size_t get_size () FINAL OVERRIDE;

  bool accepts_writes_from (type *rtype) FINAL OVERRIDE;

  void replay_into (replayer *r) FINAL OVERRIDE;

  bool is_int () const FINAL OVERRIDE { return false; }
  bool is_float () const FINAL OVERRIDE { return false; }
  bool is_bool () const FINAL OVERRIDE { return false; }
  type *is_pointer () FINAL OVERRIDE { return m_other_type; }
  type *is_array () FINAL OVERRIDE { return NULL; }

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  type *m_other_type;
};

/* A decorated version of a type, for get_const, get_volatile,
   get_aligned, and get_vector.  */

class decorated_type : public type
{
public:
  decorated_type (type *other_type)
  : type (other_type->m_ctxt),
    m_other_type (other_type) {}

  type *dereference () FINAL OVERRIDE { return m_other_type->dereference (); }

  bool is_int () const FINAL OVERRIDE { return m_other_type->is_int (); }
  bool is_float () const FINAL OVERRIDE { return m_other_type->is_float (); }
  bool is_bool () const FINAL OVERRIDE { return m_other_type->is_bool (); }
  type *is_pointer () FINAL OVERRIDE { return m_other_type->is_pointer (); }
  type *is_array () FINAL OVERRIDE { return m_other_type->is_array (); }
  struct_ *is_struct () FINAL OVERRIDE { return m_other_type->is_struct (); }

protected:
  type *m_other_type;
};

/* Result of "gcc_jit_type_get_const".  */
class memento_of_get_const : public decorated_type
{
public:
  memento_of_get_const (type *other_type)
  : decorated_type (other_type) {}

  bool accepts_writes_from (type */*rtype*/) FINAL OVERRIDE
  {
    /* Can't write to a "const".  */
    return false;
  }

  /* Strip off the "const", giving the underlying type.  */
  type *unqualified () FINAL OVERRIDE { return m_other_type; }

  virtual bool is_same_type_as (type *other)
  {
    if (!other->is_const ())
      return false;
    return m_other_type->is_same_type_as (other->is_const ());
  }

  virtual type *is_const () { return m_other_type; }

  void replay_into (replayer *) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
};

/* Result of "gcc_jit_type_get_volatile".  */
class memento_of_get_volatile : public decorated_type
{
public:
  memento_of_get_volatile (type *other_type)
  : decorated_type (other_type) {}

  virtual bool is_same_type_as (type *other)
  {
    if (!other->is_volatile ())
      return false;
    return m_other_type->is_same_type_as (other->is_volatile ());
  }

  /* Strip off the "volatile", giving the underlying type.  */
  type *unqualified () FINAL OVERRIDE { return m_other_type; }

  virtual type *is_volatile () { return m_other_type; }

  void replay_into (replayer *) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
};

/* Result of "gcc_jit_type_get_aligned".  */
class memento_of_get_aligned : public decorated_type
{
public:
  memento_of_get_aligned (type *other_type, size_t alignment_in_bytes)
  : decorated_type (other_type),
    m_alignment_in_bytes (alignment_in_bytes) {}

  /* Strip off the alignment, giving the underlying type.  */
  type *unqualified () FINAL OVERRIDE { return m_other_type; }

  void replay_into (replayer *) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  size_t m_alignment_in_bytes;
};

/* Result of "gcc_jit_type_get_vector".  */
class vector_type : public decorated_type
{
public:
  vector_type (type *other_type, size_t num_units)
  : decorated_type (other_type),
    m_num_units (num_units) {}

  size_t get_num_units () const { return m_num_units; }

  vector_type *dyn_cast_vector_type () FINAL OVERRIDE { return this; }

  type *get_element_type () { return m_other_type; }

  void replay_into (replayer *) FINAL OVERRIDE;

  vector_type *is_vector () FINAL OVERRIDE { return this; }

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  size_t m_num_units;
};

class array_type : public type
{
 public:
  array_type (context *ctxt,
	      location *loc,
	      type *element_type,
	      int num_elements)
  : type (ctxt),
    m_loc (loc),
    m_element_type (element_type),
    m_num_elements (num_elements)
  {}

  type *dereference () FINAL OVERRIDE;

  bool is_int () const FINAL OVERRIDE { return false; }
  bool is_float () const FINAL OVERRIDE { return false; }
  bool is_bool () const FINAL OVERRIDE { return false; }
  type *is_pointer () FINAL OVERRIDE { return NULL; }
  type *is_array () FINAL OVERRIDE { return m_element_type; }
  int num_elements () { return m_num_elements; }

  void replay_into (replayer *) FINAL OVERRIDE;

 private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

 private:
  location *m_loc;
  type *m_element_type;
  int m_num_elements;
};

class function_type : public type
{
public:
  function_type (context *ctxt,
		 type *return_type,
		 int num_params,
		 type **param_types,
		 int is_variadic);

  type *dereference () FINAL OVERRIDE;
  function_type *dyn_cast_function_type () FINAL OVERRIDE { return this; }
  function_type *as_a_function_type () FINAL OVERRIDE { return this; }

  bool is_same_type_as (type *other) FINAL OVERRIDE;

  bool is_int () const FINAL OVERRIDE { return false; }
  bool is_float () const FINAL OVERRIDE { return false; }
  bool is_bool () const FINAL OVERRIDE { return false; }
  type *is_pointer () FINAL OVERRIDE { return NULL; }
  type *is_array () FINAL OVERRIDE { return NULL; }

  void replay_into (replayer *) FINAL OVERRIDE;

  type * get_return_type () const { return m_return_type; }
  const vec<type *> &get_param_types () const { return m_param_types; }
  int is_variadic () const { return m_is_variadic; }

  string * make_debug_string_with_ptr ();

  void
  write_deferred_reproducer (reproducer &r,
			     memento *ptr_type);

 private:
  string * make_debug_string () FINAL OVERRIDE;
  string * make_debug_string_with (const char *);
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  type *m_return_type;
  auto_vec<type *> m_param_types;
  int m_is_variadic;
};

class field : public memento
{
public:
  field (context *ctxt,
	 location *loc,
	 type *type,
	 string *name)
  : memento (ctxt),
    m_loc (loc),
    m_type (type),
    m_name (name),
    m_container (NULL)
  {}

  type * get_type () const { return m_type; }

  compound_type * get_container () const { return m_container; }
  void set_container (compound_type *c) { m_container = c; }

  void replay_into (replayer *) OVERRIDE;

  void write_to_dump (dump &d) OVERRIDE;

  playback::field *
  playback_field () const
  {
    return static_cast <playback::field *> (m_playback_obj);
  }

private:
  string * make_debug_string () OVERRIDE;
  void write_reproducer (reproducer &r) OVERRIDE;

protected:
  location *m_loc;
  type *m_type;
  string *m_name;
  compound_type *m_container;
};


class bitfield : public field
{
public:
  bitfield (context *ctxt,
	    location *loc,
	    type *type,
	    int width,
	    string *name)
    : field (ctxt, loc, type, name),
      m_width (width)
  {}

  void replay_into (replayer *) FINAL OVERRIDE;

  void write_to_dump (dump &d) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  int m_width;
};

/* Base class for struct_ and union_ */
class compound_type : public type
{
public:
  compound_type (context *ctxt,
		 location *loc,
		 string *name);

  string *get_name () const { return m_name; }
  location *get_loc () const { return m_loc; }
  fields * get_fields () { return m_fields; }

  void
  set_fields (location *loc,
	      int num_fields,
	      field **fields);

  type *dereference () FINAL OVERRIDE;

  bool is_int () const FINAL OVERRIDE { return false; }
  bool is_float () const FINAL OVERRIDE { return false; }
  bool is_bool () const FINAL OVERRIDE { return false; }
  type *is_pointer () FINAL OVERRIDE { return NULL; }
  type *is_array () FINAL OVERRIDE { return NULL; }

  bool has_known_size () const FINAL OVERRIDE { return m_fields != NULL; }

  playback::compound_type *
  playback_compound_type ()
  {
    return static_cast <playback::compound_type *> (m_playback_obj);
  }

private:
  location *m_loc;
  string *m_name;
  fields *m_fields;
};

class struct_ : public compound_type
{
public:
  struct_ (context *ctxt,
	   location *loc,
	   string *name);

  struct_ *dyn_cast_struct () FINAL OVERRIDE { return this; }

  type *
  as_type () { return this; }

  void replay_into (replayer *r) FINAL OVERRIDE;

  const char *access_as_type (reproducer &r) FINAL OVERRIDE;

  struct_ *is_struct () FINAL OVERRIDE { return this; }

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
};

// memento of struct_::set_fields
class fields : public memento
{
public:
  fields (compound_type *struct_or_union,
	  int num_fields,
	  field **fields);

  void replay_into (replayer *r) FINAL OVERRIDE;

  void write_to_dump (dump &d) FINAL OVERRIDE;

  int length () const { return m_fields.length (); }
  field *get_field (int i) const { return m_fields[i]; }

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  compound_type *m_struct_or_union;
  auto_vec<field *> m_fields;
};

class union_ : public compound_type
{
public:
  union_ (context *ctxt,
	  location *loc,
	  string *name);

  void replay_into (replayer *r) FINAL OVERRIDE;

  virtual bool is_union () const FINAL OVERRIDE { return true; }

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
};

/* An abstract base class for operations that visit all rvalues within an
   expression tree.
   Currently the only implementation is class rvalue_usage_validator within
   jit-recording.cc.  */

class rvalue_visitor
{
 public:
  virtual ~rvalue_visitor () {}
  virtual void visit (rvalue *rvalue) = 0;
};

/* When generating debug strings for rvalues we mimic C, so we need to
   mimic C's precedence levels when handling compound expressions.
   These are in order from strongest precedence to weakest.  */
enum precedence
{
  PRECEDENCE_PRIMARY,
  PRECEDENCE_POSTFIX,
  PRECEDENCE_UNARY,
  PRECEDENCE_CAST,
  PRECEDENCE_MULTIPLICATIVE,
  PRECEDENCE_ADDITIVE,
  PRECEDENCE_SHIFT,
  PRECEDENCE_RELATIONAL,
  PRECEDENCE_EQUALITY,
  PRECEDENCE_BITWISE_AND,
  PRECEDENCE_BITWISE_XOR,
  PRECEDENCE_BITWISE_IOR,
  PRECEDENCE_LOGICAL_AND,
  PRECEDENCE_LOGICAL_OR
};

class rvalue : public memento
{
public:
  rvalue (context *ctxt,
	  location *loc,
	  type *type_)
  : memento (ctxt),
    m_loc (loc),
    m_type (type_),
    m_scope (NULL),
    m_parenthesized_string (NULL)
  {
    gcc_assert (type_);
  }

  location * get_loc () const { return m_loc; }

  /* Get the recording::type of this rvalue.

     Implements the post-error-checking part of
     gcc_jit_rvalue_get_type.  */
  type * get_type () const { return m_type; }

  playback::rvalue *
  playback_rvalue () const
  {
    return static_cast <playback::rvalue *> (m_playback_obj);
  }
  rvalue *
  access_field (location *loc,
		field *field);

  lvalue *
  dereference_field (location *loc,
		     field *field);

  lvalue *
  dereference (location *loc);

  void
  verify_valid_within_stmt (const char *api_funcname, statement *s);

  virtual void visit_children (rvalue_visitor *v) = 0;

  void set_scope (function *scope);
  function *get_scope () const { return m_scope; }

  /* Dynamic casts.  */
  virtual param *dyn_cast_param () { return NULL; }
  virtual base_call *dyn_cast_base_call () { return NULL; }

  virtual const char *access_as_rvalue (reproducer &r);

  /* Get the debug string, wrapped in parentheses.  */
  const char *
  get_debug_string_parens (enum precedence outer_prec);

  virtual bool is_constant () const { return false; }
  virtual bool get_wide_int (wide_int *) const { return false; }

private:
  virtual enum precedence get_precedence () const = 0;

protected:
  location *m_loc;
  type *m_type;

 private:
  function *m_scope; /* NULL for globals, non-NULL for locals/params */
  string *m_parenthesized_string;
};

class lvalue : public rvalue
{
public:
  lvalue (context *ctxt,
	  location *loc,
	  type *type_)
    : rvalue (ctxt, loc, type_),
    m_tls_model (GCC_JIT_TLS_MODEL_NONE),
    m_link_section (NULL)
    {}

  playback::lvalue *
  playback_lvalue () const
  {
    return static_cast <playback::lvalue *> (m_playback_obj);
  }

  lvalue *
  access_field (location *loc,
		field *field);

  rvalue *
  get_address (location *loc);

  rvalue *
  as_rvalue () { return this; }

  const char *access_as_rvalue (reproducer &r) OVERRIDE;
  virtual const char *access_as_lvalue (reproducer &r);
  virtual bool is_global () const { return false; }
  void set_tls_model (enum gcc_jit_tls_model model);
  void set_link_section (const char *name);

protected:
  enum gcc_jit_tls_model m_tls_model;
  string *m_link_section;
};

class param : public lvalue
{
public:
  param (context *ctxt,
	 location *loc,
	 type *type,
	 string *name)
    : lvalue (ctxt, loc, type),
    m_name (name) {}

  lvalue *
  as_lvalue () { return this; }

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE {}

  playback::param *
  playback_param () const
  {
    return static_cast <playback::param *> (m_playback_obj);
  }

  param *dyn_cast_param () FINAL OVERRIDE { return this; }

  const char *access_as_rvalue (reproducer &r) FINAL OVERRIDE;
  const char *access_as_lvalue (reproducer &r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE { return m_name; }
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

private:
  string *m_name;
};

class function : public memento
{
public:
  function (context *ctxt,
	    location *loc,
	    enum gcc_jit_function_kind kind,
	    type *return_type,
	    string *name,
	    int num_params,
	    param **params,
	    int is_variadic,
	    enum built_in_function builtin_id);

  void replay_into (replayer *r) FINAL OVERRIDE;

  playback::function *
  playback_function () const
  {
    return static_cast <playback::function *> (m_playback_obj);
  }

  enum gcc_jit_function_kind get_kind () const { return m_kind; }

  lvalue *
  new_local (location *loc,
	     type *type,
	     const char *name);

  block*
  new_block (const char *name);

  location *get_loc () const { return m_loc; }
  type *get_return_type () const { return m_return_type; }
  string * get_name () const { return m_name; }
  const vec<param *> &get_params () const { return m_params; }

  /* Get the given param by index.
     Implements the post-error-checking part of
     gcc_jit_function_get_param.  */
  param *get_param (int i) const { return m_params[i]; }

  bool is_variadic () const { return m_is_variadic; }

  void write_to_dump (dump &d) FINAL OVERRIDE;

  void validate ();

  void dump_to_dot (const char *path);

  rvalue *get_address (location *loc);

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  location *m_loc;
  enum gcc_jit_function_kind m_kind;
  type *m_return_type;
  string *m_name;
  auto_vec<param *> m_params;
  int m_is_variadic;
  enum built_in_function m_builtin_id;
  auto_vec<local *> m_locals;
  auto_vec<block *> m_blocks;
  type *m_fn_ptr_type;
};

class block : public memento
{
public:
  block (function *func, int index, string *name)
  : memento (func->m_ctxt),
    m_func (func),
    m_index (index),
    m_name (name),
    m_statements (),
    m_has_been_terminated (false),
    m_is_reachable (false)
  {
  }

  /* Get the recording::function containing this block.
     Implements the post-error-checking part of
     gcc_jit_block_get_function.  */
  function *get_function () { return m_func; }

  bool has_been_terminated () { return m_has_been_terminated; }
  bool is_reachable () { return m_is_reachable; }

  statement *
  add_eval (location *loc,
	    rvalue *rvalue);

  statement *
  add_assignment (location *loc,
		  lvalue *lvalue,
		  rvalue *rvalue);

  statement *
  add_assignment_op (location *loc,
		     lvalue *lvalue,
		     enum gcc_jit_binary_op op,
		     rvalue *rvalue);

  statement *
  add_comment (location *loc,
	       const char *text);

  extended_asm *
  add_extended_asm (location *loc,
		    const char *asm_template);

  statement *
  end_with_conditional (location *loc,
			rvalue *boolval,
			block *on_true,
			block *on_false);

  statement *
  end_with_jump (location *loc,
		 block *target);

  statement *
  end_with_return (location *loc,
		   rvalue *rvalue);

  statement *
  end_with_switch (location *loc,
		   rvalue *expr,
		   block *default_block,
		   int num_cases,
		   case_ **cases);

  extended_asm *
  end_with_extended_asm_goto (location *loc,
			      const char *asm_template,
			      int num_goto_blocks,
			      block **goto_blocks,
			      block *fallthrough_block);

  playback::block *
  playback_block () const
  {
    return static_cast <playback::block *> (m_playback_obj);
  }

  void write_to_dump (dump &d) FINAL OVERRIDE;

  bool validate ();

  location *get_loc () const;

  statement *get_first_statement () const;
  statement *get_last_statement () const;

  vec <block *> get_successor_blocks () const;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

  void replay_into (replayer *r) FINAL OVERRIDE;

  void dump_to_dot (pretty_printer *pp);
  void dump_edges_to_dot (pretty_printer *pp);

private:
  function *m_func;
  int m_index;
  string *m_name;
  auto_vec<statement *> m_statements;
  bool m_has_been_terminated;
  bool m_is_reachable;

  friend class function;
};

class global : public lvalue
{
public:
  global (context *ctxt,
	  location *loc,
	  enum gcc_jit_global_kind kind,
	  type *type,
	  string *name)
  : lvalue (ctxt, loc, type),
    m_kind (kind),
    m_name (name)
  {
    m_initializer = NULL;
    m_initializer_num_bytes = 0;
  }
  ~global ()
  {
    free (m_initializer);
  }

  void replay_into (replayer *) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE {}

  void write_to_dump (dump &d) FINAL OVERRIDE;

  bool is_global () const FINAL OVERRIDE { return true; }

  void
  set_initializer (const void *initializer,
                   size_t num_bytes)
  {
    if (m_initializer)
      free (m_initializer);
    m_initializer = xmalloc (num_bytes);
    memcpy (m_initializer, initializer, num_bytes);
    m_initializer_num_bytes = num_bytes;
  }

  void set_flags (int flag_fields)
  {
    m_flags = (enum global_var_flags)(m_flags | flag_fields);
  }
  /* Returns true if any of the flags in the argument is set.  */
  bool test_flags_anyof (int flag_fields) const
  {
    return m_flags & flag_fields;
  }

  enum gcc_jit_global_kind get_kind () const
  {
    return m_kind;
  }

  void set_rvalue_init (rvalue *val) { m_rvalue_init = val; }

private:
  string * make_debug_string () FINAL OVERRIDE { return m_name; }
  template <typename T>
  void write_initializer_reproducer (const char *id, reproducer &r);
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

private:
  enum gcc_jit_global_kind m_kind;
  enum global_var_flags m_flags = GLOBAL_VAR_FLAGS_NONE;
  string *m_name;
  void *m_initializer;
  rvalue *m_rvalue_init = nullptr; /* Only needed for write_dump.  */
  size_t m_initializer_num_bytes;
};

template <typename HOST_TYPE>
class memento_of_new_rvalue_from_const : public rvalue
{
public:
  memento_of_new_rvalue_from_const (context *ctxt,
				    location *loc,
				    type *type,
				    HOST_TYPE value)
  : rvalue (ctxt, loc, type),
    m_value (value) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE {}

  bool is_constant () const FINAL OVERRIDE { return true; }

  bool get_wide_int (wide_int *out) const FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

private:
  HOST_TYPE m_value;
};

class memento_of_new_string_literal : public rvalue
{
public:
  memento_of_new_string_literal (context *ctxt,
				 location *loc,
				 string *value)
  : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_CONST_CHAR_PTR)),
    m_value (value) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE {}

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

private:
  string *m_value;
};

class memento_of_new_rvalue_from_vector : public rvalue
{
public:
  memento_of_new_rvalue_from_vector (context *ctxt,
				     location *loc,
				     vector_type *type,
				     rvalue **elements);

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

private:
  vector_type *m_vector_type;
  auto_vec<rvalue *> m_elements;
};

class ctor : public rvalue
{
public:
  ctor (context *ctxt,
	location *loc,
	type *type)
  : rvalue (ctxt, loc, type)
  { }

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

public:
  auto_vec<field *> m_fields;
  auto_vec<rvalue *> m_values;
};

class unary_op : public rvalue
{
public:
  unary_op (context *ctxt,
	    location *loc,
	    enum gcc_jit_unary_op op,
	    type *result_type,
	    rvalue *a)
  : rvalue (ctxt, loc, result_type),
    m_op (op),
    m_a (a)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_UNARY;
  }

private:
  enum gcc_jit_unary_op m_op;
  rvalue *m_a;
};

class binary_op : public rvalue
{
public:
  binary_op (context *ctxt,
	     location *loc,
	     enum gcc_jit_binary_op op,
	     type *result_type,
	     rvalue *a, rvalue *b)
  : rvalue (ctxt, loc, result_type),
    m_op (op),
    m_a (a),
    m_b (b) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE;

private:
  enum gcc_jit_binary_op m_op;
  rvalue *m_a;
  rvalue *m_b;
};

class comparison : public rvalue
{
public:
  comparison (context *ctxt,
	      location *loc,
	      enum gcc_jit_comparison op,
	      rvalue *a, rvalue *b)
  : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_BOOL)),
    m_op (op),
    m_a (a),
    m_b (b)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE;

private:
  enum gcc_jit_comparison m_op;
  rvalue *m_a;
  rvalue *m_b;
};

class cast : public rvalue
{
public:
  cast (context *ctxt,
	location *loc,
	rvalue *a,
	type *type_)
  : rvalue (ctxt, loc, type_),
    m_rvalue (a)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_CAST;
  }

private:
  rvalue *m_rvalue;
};

class base_call : public rvalue
{
 public:
  base_call (context *ctxt,
	     location *loc,
	     type *type_,
	     int numargs,
	     rvalue **args);

  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_POSTFIX;
  }

  base_call *dyn_cast_base_call () FINAL OVERRIDE { return this; }

  void set_require_tail_call (bool require_tail_call)
  {
    m_require_tail_call = require_tail_call;
  }

 protected:
  void write_reproducer_tail_call (reproducer &r, const char *id);

 protected:
  auto_vec<rvalue *> m_args;
  bool m_require_tail_call;
};

class call : public base_call
{
public:
  call (context *ctxt,
	location *loc,
	function *func,
	int numargs,
	rvalue **args);

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  function *m_func;
};

class call_through_ptr : public base_call
{
public:
  call_through_ptr (context *ctxt,
		    location *loc,
		    rvalue *fn_ptr,
		    int numargs,
		    rvalue **args);

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  rvalue *m_fn_ptr;
};

class array_access : public lvalue
{
public:
  array_access (context *ctxt,
		location *loc,
		rvalue *ptr,
		rvalue *index)
  : lvalue (ctxt, loc, ptr->get_type ()->dereference ()),
    m_ptr (ptr),
    m_index (index)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_POSTFIX;
  }

private:
  rvalue *m_ptr;
  rvalue *m_index;
};

class access_field_of_lvalue : public lvalue
{
public:
  access_field_of_lvalue (context *ctxt,
			  location *loc,
			  lvalue *val,
			  field *field)
  : lvalue (ctxt, loc, field->get_type ()),
    m_lvalue (val),
    m_field (field)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_POSTFIX;
  }

private:
  lvalue *m_lvalue;
  field *m_field;
};

class access_field_rvalue : public rvalue
{
public:
  access_field_rvalue (context *ctxt,
		       location *loc,
		       rvalue *val,
		       field *field)
  : rvalue (ctxt, loc, field->get_type ()),
    m_rvalue (val),
    m_field (field)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_POSTFIX;
  }

private:
  rvalue *m_rvalue;
  field *m_field;
};

class dereference_field_rvalue : public lvalue
{
public:
  dereference_field_rvalue (context *ctxt,
			    location *loc,
			    rvalue *val,
			    field *field)
  : lvalue (ctxt, loc, field->get_type ()),
    m_rvalue (val),
    m_field (field)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_POSTFIX;
  }

private:
  rvalue *m_rvalue;
  field *m_field;
};

class dereference_rvalue : public lvalue
{
public:
  dereference_rvalue (context *ctxt,
		      location *loc,
		      rvalue *val)
  : lvalue (ctxt, loc, val->get_type ()->dereference ()),
    m_rvalue (val) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_UNARY;
  }

private:
  rvalue *m_rvalue;
};

class get_address_of_lvalue : public rvalue
{
public:
  get_address_of_lvalue (context *ctxt,
			 location *loc,
			 lvalue *val)
  : rvalue (ctxt, loc, val->get_type ()->get_pointer ()),
    m_lvalue (val)
  {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_UNARY;
  }

private:
  lvalue *m_lvalue;
};

class function_pointer : public rvalue
{
public:
  function_pointer (context *ctxt,
		    location *loc,
		    function *fn,
		    type *type)
  : rvalue (ctxt, loc, type),
    m_fn (fn) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *v) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_UNARY;
  }

private:
  function *m_fn;
};

class local : public lvalue
{
public:
  local (function *func, location *loc, type *type_, string *name)
    : lvalue (func->m_ctxt, loc, type_),
    m_func (func),
    m_name (name)
  {
    set_scope (func);
  }

  void replay_into (replayer *r) FINAL OVERRIDE;

  void visit_children (rvalue_visitor *) FINAL OVERRIDE {}

  void write_to_dump (dump &d) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE { return m_name; }
  void write_reproducer (reproducer &r) FINAL OVERRIDE;
  enum precedence get_precedence () const FINAL OVERRIDE
  {
    return PRECEDENCE_PRIMARY;
  }

private:
  function *m_func;
  string *m_name;
};

class statement : public memento
{
public:
  virtual vec <block *> get_successor_blocks () const;

  void write_to_dump (dump &d) FINAL OVERRIDE;

  block *get_block () const { return m_block; }
  location *get_loc () const { return m_loc; }

protected:
  statement (block *b, location *loc)
  : memento (b->m_ctxt),
    m_block (b),
    m_loc (loc) {}

  playback::location *
  playback_location (replayer *r) const
  {
    return ::gcc::jit::recording::playback_location (r, m_loc);
  }

private:
  block *m_block;
  location *m_loc;
};

class eval : public statement
{
public:
  eval (block *b,
	location *loc,
	rvalue *rvalue)
  : statement (b, loc),
    m_rvalue (rvalue) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  rvalue *m_rvalue;
};

class assignment : public statement
{
public:
  assignment (block *b,
	      location *loc,
	      lvalue *lvalue,
	      rvalue *rvalue)
  : statement (b, loc),
    m_lvalue (lvalue),
    m_rvalue (rvalue) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  lvalue *m_lvalue;
  rvalue *m_rvalue;
};

class assignment_op : public statement
{
public:
  assignment_op (block *b,
		 location *loc,
		 lvalue *lvalue,
		 enum gcc_jit_binary_op op,
		 rvalue *rvalue)
  : statement (b, loc),
    m_lvalue (lvalue),
    m_op (op),
    m_rvalue (rvalue) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  lvalue *m_lvalue;
  enum gcc_jit_binary_op m_op;
  rvalue *m_rvalue;
};

class comment : public statement
{
public:
  comment (block *b,
	   location *loc,
	   string *text)
  : statement (b, loc),
    m_text (text) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  string *m_text;
};

class conditional : public statement
{
public:
  conditional (block *b,
	       location *loc,
	       rvalue *boolval,
	       block *on_true,
	       block *on_false)
  : statement (b, loc),
    m_boolval (boolval),
    m_on_true (on_true),
    m_on_false (on_false) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  vec <block *> get_successor_blocks () const FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  rvalue *m_boolval;
  block *m_on_true;
  block *m_on_false;
};

class jump : public statement
{
public:
  jump (block *b,
	location *loc,
	block *target)
  : statement (b, loc),
    m_target (target) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  vec <block *> get_successor_blocks () const FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  block *m_target;
};

class return_ : public statement
{
public:
  return_ (block *b,
	   location *loc,
	   rvalue *rvalue)
  : statement (b, loc),
    m_rvalue (rvalue) {}

  void replay_into (replayer *r) FINAL OVERRIDE;

  vec <block *> get_successor_blocks () const FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  rvalue *m_rvalue;
};

class case_ : public memento
{
 public:
  case_ (context *ctxt,
	 rvalue *min_value,
	 rvalue *max_value,
	 block *dest_block)
  : memento (ctxt),
    m_min_value (min_value),
    m_max_value (max_value),
    m_dest_block (dest_block)
  {}

  rvalue *get_min_value () const { return m_min_value; }
  rvalue *get_max_value () const { return m_max_value; }
  block *get_dest_block () const { return m_dest_block; }

  void replay_into (replayer *) FINAL OVERRIDE { /* empty */ }

  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;

 private:
  rvalue *m_min_value;
  rvalue *m_max_value;
  block *m_dest_block;
};

class switch_ : public statement
{
public:
  switch_ (block *b,
	   location *loc,
	   rvalue *expr,
	   block *default_block,
	   int num_cases,
	   case_ **cases);

  void replay_into (replayer *r) FINAL OVERRIDE;

  vec <block *> get_successor_blocks () const FINAL OVERRIDE;

private:
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  rvalue *m_expr;
  block *m_default_block;
  auto_vec <case_ *> m_cases;
};

class asm_operand : public memento
{
public:
  asm_operand (extended_asm *ext_asm,
	       string *asm_symbolic_name,
	       string *constraint);

  const char *get_symbolic_name () const
  {
    if (m_asm_symbolic_name)
      return m_asm_symbolic_name->c_str ();
    else
      return NULL;
  }

  const char *get_constraint () const
  {
    return m_constraint->c_str ();
  }

  virtual void print (pretty_printer *pp) const;

private:
  string * make_debug_string () FINAL OVERRIDE;

protected:
  extended_asm *m_ext_asm;
  string *m_asm_symbolic_name;
  string *m_constraint;
};

class output_asm_operand : public asm_operand
{
public:
  output_asm_operand (extended_asm *ext_asm,
		      string *asm_symbolic_name,
		      string *constraint,
		      lvalue *dest)
  : asm_operand (ext_asm, asm_symbolic_name, constraint),
    m_dest (dest)
  {}

  lvalue *get_lvalue () const { return m_dest; }

  void replay_into (replayer *) FINAL OVERRIDE {}

  void print (pretty_printer *pp) const FINAL OVERRIDE;

private:
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  lvalue *m_dest;
};

class input_asm_operand : public asm_operand
{
public:
  input_asm_operand (extended_asm *ext_asm,
		     string *asm_symbolic_name,
		     string *constraint,
		     rvalue *src)
  : asm_operand (ext_asm, asm_symbolic_name, constraint),
    m_src (src)
  {}

  rvalue *get_rvalue () const { return m_src; }

  void replay_into (replayer *) FINAL OVERRIDE {}

  void print (pretty_printer *pp) const FINAL OVERRIDE;

private:
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  rvalue *m_src;
};

/* Abstract base class for extended_asm statements.  */

class extended_asm : public statement
{
public:
  extended_asm (block *b,
		location *loc,
		string *asm_template)
  : statement (b, loc),
    m_asm_template (asm_template),
    m_is_volatile (false),
    m_is_inline (false)
  {}

  void set_volatile_flag (bool flag) { m_is_volatile = flag; }
  void set_inline_flag (bool flag) { m_is_inline = flag; }

  void add_output_operand (const char *asm_symbolic_name,
			   const char *constraint,
			   lvalue *dest);
  void add_input_operand (const char *asm_symbolic_name,
			  const char *constraint,
			  rvalue *src);
  void add_clobber (const char *victim);

  void replay_into (replayer *r) OVERRIDE;

  string *get_asm_template () const { return m_asm_template; }

  virtual bool is_goto () const = 0;
  virtual void maybe_print_gotos (pretty_printer *) const = 0;

protected:
  void write_flags (reproducer &r);
  void write_clobbers (reproducer &r);

private:
  string * make_debug_string () FINAL OVERRIDE;
  virtual void maybe_populate_playback_blocks
    (auto_vec <playback::block *> *out) = 0;

protected:
  string *m_asm_template;
  bool m_is_volatile;
  bool m_is_inline;
  auto_vec<output_asm_operand *> m_output_ops;
  auto_vec<input_asm_operand *> m_input_ops;
  auto_vec<string *> m_clobbers;
};

/* An extended_asm that's not a goto, as created by
   gcc_jit_block_add_extended_asm. */

class extended_asm_simple : public extended_asm
{
public:
  extended_asm_simple (block *b,
		       location *loc,
		       string *asm_template)
  : extended_asm (b, loc, asm_template)
  {}

  void write_reproducer (reproducer &r) OVERRIDE;
  bool is_goto () const FINAL OVERRIDE { return false; }
  void maybe_print_gotos (pretty_printer *) const FINAL OVERRIDE {}

private:
  void maybe_populate_playback_blocks
    (auto_vec <playback::block *> *) FINAL OVERRIDE
  {}
};

/* An extended_asm that's a asm goto, as created by
   gcc_jit_block_end_with_extended_asm_goto.  */

class extended_asm_goto : public extended_asm
{
public:
  extended_asm_goto (block *b,
		     location *loc,
		     string *asm_template,
		     int num_goto_blocks,
		     block **goto_blocks,
		     block *fallthrough_block);

  void replay_into (replayer *r) FINAL OVERRIDE;
  void write_reproducer (reproducer &r) OVERRIDE;

  vec <block *> get_successor_blocks () const FINAL OVERRIDE;

  bool is_goto () const FINAL OVERRIDE { return true; }
  void maybe_print_gotos (pretty_printer *) const FINAL OVERRIDE;

private:
  void maybe_populate_playback_blocks
    (auto_vec <playback::block *> *out) FINAL OVERRIDE;

private:
  auto_vec <block *> m_goto_blocks;
  block *m_fallthrough_block;
};

/* A group of top-level asm statements, as created by
   gcc_jit_context_add_top_level_asm.  */

class top_level_asm : public memento
{
public:
  top_level_asm (context *ctxt, location *loc, string *asm_stmts);

  void write_to_dump (dump &d) FINAL OVERRIDE;

private:
  void replay_into (replayer *r) FINAL OVERRIDE;
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  location *m_loc;
  string *m_asm_stmts;
};

class global_init_rvalue : public memento
{
public:
  global_init_rvalue (context *ctxt, lvalue *variable, rvalue *init) :
    memento (ctxt), m_variable (variable), m_init (init) {};

  void write_to_dump (dump &d) FINAL OVERRIDE;

private:
  void replay_into (replayer *r) FINAL OVERRIDE;
  string * make_debug_string () FINAL OVERRIDE;
  void write_reproducer (reproducer &r) FINAL OVERRIDE;

private:
  lvalue *m_variable;
  rvalue *m_init;
};

} // namespace gcc::jit::recording

/* Create a recording::memento_of_new_rvalue_from_const instance and add
   it to this context's list of mementos.

   Implements the post-error-checking part of
   gcc_jit_context_new_rvalue_from_{int|long|double|ptr}.  */

template <typename HOST_TYPE>
recording::rvalue *
recording::context::new_rvalue_from_const (recording::type *type,
					   HOST_TYPE value)
{
  recording::rvalue *result =
    new memento_of_new_rvalue_from_const <HOST_TYPE> (this, NULL, type, value);
  record (result);
  return result;
}

/* Don't call this directly.  Call types_kinda_same.  */
bool
types_kinda_same_internal (recording::type *a,
			   recording::type *b);

/* Strip all qualifiers and count pointer depth, returning true
   if the types and pointer depth are the same, otherwise false.

   For array and vector types the number of element also
   has to match, aswell as the element types themself.  */
static inline bool
types_kinda_same (recording::type *a, recording::type *b)
{
  /* Handle trivial case here, to allow for inlining.  */
  return a == b || types_kinda_same_internal (a, b);
}

} // namespace gcc::jit

} // namespace gcc

#endif /* JIT_RECORDING_H */