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
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
|
/* Internals of libgccjit: classes for recording calls made to the JIT API.
Copyright (C) 2013-2014 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/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "opts.h"
#include "tree.h"
#include "pretty-print.h"
#include <pthread.h>
#include "jit-common.h"
#include "jit-builtins.h"
#include "jit-recording.h"
#include "jit-playback.h"
namespace gcc {
namespace jit {
// class dump
dump::dump (recording::context &ctxt,
const char *filename,
bool update_locations)
: m_ctxt (ctxt),
m_filename (filename),
m_update_locations (update_locations),
m_line (0),
m_column (0)
{
m_file = fopen (filename, "w");
if (!m_file)
ctxt.add_error (NULL,
"error opening dump file %s for writing: %s",
filename,
xstrerror (errno));
}
dump::~dump ()
{
if (m_file)
{
int err = fclose (m_file);
if (err)
m_ctxt.add_error (NULL,
"error closing dump file %s: %s",
m_filename,
xstrerror (errno));
}
}
/* Write the given message to the dump, using printf-formatting
conventions, updating the line/column within the dump.
Emit an error on the context if a failure occurs. */
void
dump::write (const char *fmt, ...)
{
va_list ap;
char *buf = NULL;
/* If there was an error opening the file, we've already reported it.
Don't attempt further work. */
if (!m_file)
return;
va_start (ap, fmt);
vasprintf (&buf, fmt, ap);
va_end (ap);
if (!buf)
{
m_ctxt.add_error (NULL, "malloc failure writing to dumpfile %s",
m_filename);
return;
}
if (fwrite (buf, strlen (buf), 1, m_file) != 1)
m_ctxt.add_error (NULL, "error writing to dump file %s",
m_filename);
/* Update line/column: */
for (const char *ptr = buf; *ptr; ptr++)
{
if ('\n' == *ptr)
{
m_line++;
m_column = 0;
}
else
m_column++;
}
free (buf);
}
/* Construct a gcc::jit::recording::location instance for the current
location within the dump. */
recording::location *
dump::make_location () const
{
return m_ctxt.new_location (m_filename, m_line, m_column);
}
/**********************************************************************
Recording.
**********************************************************************/
/* Get the playback::location for the given recording::location,
handling a NULL input with a NULL output. */
playback::location *
recording::playback_location (replayer *r, recording::location *loc)
{
if (loc)
return loc->playback_location (r);
else
return NULL;
}
/* Get a const char * for the given recording::string
handling a NULL input with a NULL output. */
const char *
recording::playback_string (recording::string *str)
{
if (str)
return str->c_str ();
else
return NULL;
}
/* Get the playback::block for the given recording::block,
handling a NULL input with a NULL output. */
playback::block *
recording::playback_block (recording::block *b)
{
if (b)
return b->playback_block ();
else
return NULL;
}
/* Methods of cc::jit::recording::context. */
/* The constructor for gcc::jit::recording::context, used by
gcc_jit_context_acquire and gcc_jit_context_new_child_context. */
recording::context::context (context *parent_ctxt)
: m_parent_ctxt (parent_ctxt),
m_error_count (0),
m_first_error_str (NULL),
m_owns_first_error_str (false),
m_mementos (),
m_compound_types (),
m_functions (),
m_FILE_type (NULL),
m_builtins_manager(NULL)
{
if (parent_ctxt)
{
/* Inherit options from parent.
Note that the first memcpy means copying pointers to strings. */
memcpy (m_str_options,
parent_ctxt->m_str_options,
sizeof (m_str_options));
memcpy (m_int_options,
parent_ctxt->m_int_options,
sizeof (m_int_options));
memcpy (m_bool_options,
parent_ctxt->m_bool_options,
sizeof (m_bool_options));
}
else
{
memset (m_str_options, 0, sizeof (m_str_options));
memset (m_int_options, 0, sizeof (m_int_options));
memset (m_bool_options, 0, sizeof (m_bool_options));
}
memset (m_basic_types, 0, sizeof (m_basic_types));
}
/* The destructor for gcc::jit::recording::context, implicitly used by
gcc_jit_context_release. */
recording::context::~context ()
{
int i;
memento *m;
FOR_EACH_VEC_ELT (m_mementos, i, m)
{
delete m;
}
if (m_builtins_manager)
delete m_builtins_manager;
if (m_owns_first_error_str)
free (m_first_error_str);
}
/* Add the given mememto to the list of those tracked by this
gcc::jit::recording::context, so that e.g. it can be deleted
when this context is released. */
void
recording::context::record (memento *m)
{
gcc_assert (m);
m_mementos.safe_push (m);
}
/* Replay this context (and any parents) into the given replayer. */
void
recording::context::replay_into (replayer *r)
{
int i;
memento *m;
/* If we have a parent context, we must replay it. This will
recursively walk backwards up the historical tree, then replay things
forwards "in historical order", starting with the ultimate parent
context, until we reach the "this" context.
Note that we fully replay the parent, then fully replay the child,
which means that inter-context references can only exist from child
to parent, not the other way around.
All of this replaying is suboptimal - it would be better to do the
work for the parent context *once*, rather than replaying the parent
every time we replay each child. However, fixing this requires deep
surgery to lifetime-management: we'd need every context family tree
to have its own GC heap, and to initialize the GCC code to use that
heap (with a mutex on such a heap). */
if (m_parent_ctxt)
m_parent_ctxt->replay_into (r);
if (r->errors_occurred ())
return;
/* Replay this context's saved operations into r. */
FOR_EACH_VEC_ELT (m_mementos, i, m)
{
/* Disabled low-level debugging, here if we need it: print what
we're replaying.
Note that the calls to get_debug_string might lead to more
mementos being created for the strings.
This can also be used to exercise the debug_string
machinery. */
if (0)
printf ("context %p replaying (%p): %s\n",
(void *)this, (void *)m, m->get_debug_string ());
m->replay_into (r);
if (r->errors_occurred ())
return;
}
}
/* During a playback, we associate objects from the recording with
their counterparts during this playback.
For simplicity, we store this within the recording objects.
The following method cleans away these associations, to ensure that
we never have out-of-date associations lingering on subsequent
playbacks (the objects pointed to are GC-managed, but the
recording objects don't own refs to them). */
void
recording::context::disassociate_from_playback ()
{
int i;
memento *m;
if (m_parent_ctxt)
m_parent_ctxt->disassociate_from_playback ();
FOR_EACH_VEC_ELT (m_mementos, i, m)
{
m->set_playback_obj (NULL);
}
}
/* Create a recording::string instance and add it to this context's list
of mementos.
This creates a fresh copy of the given 0-terminated buffer. */
recording::string *
recording::context::new_string (const char *text)
{
if (!text)
return NULL;
recording::string *result = new string (this, text);
record (result);
return result;
}
/* Create a recording::location instance and add it to this context's
list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_location. */
recording::location *
recording::context::new_location (const char *filename,
int line,
int column)
{
recording::location *result =
new recording::location (this,
new_string (filename),
line, column);
record (result);
return result;
}
/* If we haven't seen this enum value yet, create a recording::type
instance and add it to this context's list of mementos.
If we have seen it before, reuse our cached value, so that repeated
calls on the context give the same object.
If we have a parent context, the cache is within the ultimate
ancestor context.
Implements the post-error-checking part of
gcc_jit_context_get_type. */
recording::type *
recording::context::get_type (enum gcc_jit_types kind)
{
if (!m_basic_types[kind])
{
if (m_parent_ctxt)
m_basic_types[kind] = m_parent_ctxt->get_type (kind);
else
{
recording::type *result = new memento_of_get_type (this, kind);
record (result);
m_basic_types[kind] = result;
}
}
return m_basic_types[kind];
}
/* Get a recording::type instance for the given size and signedness.
This is implemented in terms of recording::context::get_type
above.
Implements the post-error-checking part of
gcc_jit_context_get_int_type. */
recording::type *
recording::context::get_int_type (int num_bytes, int is_signed)
{
/* We can't use a switch here since some of the values are macros affected
by options; e.g. i386.h has
#define LONG_TYPE_SIZE (TARGET_X32 ? 32 : BITS_PER_WORD)
Compare with tree.c's make_or_reuse_type. Note that the _SIZE macros
are in bits, rather than bytes.
*/
const int num_bits = num_bytes * 8;
if (num_bits == INT_TYPE_SIZE)
return get_type (is_signed
? GCC_JIT_TYPE_INT
: GCC_JIT_TYPE_UNSIGNED_INT);
if (num_bits == CHAR_TYPE_SIZE)
return get_type (is_signed
? GCC_JIT_TYPE_SIGNED_CHAR
: GCC_JIT_TYPE_UNSIGNED_CHAR);
if (num_bits == SHORT_TYPE_SIZE)
return get_type (is_signed
? GCC_JIT_TYPE_SHORT
: GCC_JIT_TYPE_UNSIGNED_SHORT);
if (num_bits == LONG_TYPE_SIZE)
return get_type (is_signed
? GCC_JIT_TYPE_LONG
: GCC_JIT_TYPE_UNSIGNED_LONG);
if (num_bits == LONG_LONG_TYPE_SIZE)
return get_type (is_signed
? GCC_JIT_TYPE_LONG_LONG
: GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
/* Some other size, not corresponding to the C int types. */
/* To be written: support arbitrary other sizes, sharing by
memoizing at the recording::context level? */
gcc_unreachable ();
}
/* Create a recording::type instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_array_type. */
recording::type *
recording::context::new_array_type (recording::location *loc,
recording::type *element_type,
int num_elements)
{
if (struct_ *s = element_type->dyn_cast_struct ())
if (!s->get_fields ())
{
add_error (NULL,
"cannot create an array of type %s"
" until the fields have been set",
s->get_name ()->c_str ());
return NULL;
}
recording::type *result =
new recording::array_type (this, loc, element_type, num_elements);
record (result);
return result;
}
/* Create a recording::field instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_field. */
recording::field *
recording::context::new_field (recording::location *loc,
recording::type *type,
const char *name)
{
recording::field *result =
new recording::field (this, loc, type, new_string (name));
record (result);
return result;
}
/* Create a recording::struct_ instance and add it to this context's
list of mementos and list of compound types.
Implements the post-error-checking part of
gcc_jit_context_new_struct_type. */
recording::struct_ *
recording::context::new_struct_type (recording::location *loc,
const char *name)
{
recording::struct_ *result = new struct_ (this, loc, new_string (name));
record (result);
m_compound_types.safe_push (result);
return result;
}
/* Create a recording::union_ instance and add it to this context's
list of mementos and list of compound types.
Implements the first post-error-checking part of
gcc_jit_context_new_union_type. */
recording::union_ *
recording::context::new_union_type (recording::location *loc,
const char *name)
{
recording::union_ *result = new union_ (this, loc, new_string (name));
record (result);
m_compound_types.safe_push (result);
return result;
}
/* Create a recording::type instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_function_ptr_type. */
recording::type *
recording::context::new_function_ptr_type (recording::location *, /* unused loc */
recording::type *return_type,
int num_params,
recording::type **param_types,
int is_variadic)
{
recording::function_type *fn_type =
new function_type (this,
return_type,
num_params,
param_types,
is_variadic);
record (fn_type);
/* Return a pointer-type to the the function type. */
return fn_type->get_pointer ();
}
/* Create a recording::param instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_param. */
recording::param *
recording::context::new_param (recording::location *loc,
recording::type *type,
const char *name)
{
recording::param *result = new recording::param (this, loc, type, new_string (name));
record (result);
return result;
}
/* Create a recording::function instance and add it to this context's list
of mementos and list of functions.
Implements the post-error-checking part of
gcc_jit_context_new_function. */
recording::function *
recording::context::new_function (recording::location *loc,
enum gcc_jit_function_kind kind,
recording::type *return_type,
const char *name,
int num_params,
recording::param **params,
int is_variadic,
enum built_in_function builtin_id)
{
recording::function *result =
new recording::function (this,
loc, kind, return_type,
new_string (name),
num_params, params, is_variadic,
builtin_id);
record (result);
m_functions.safe_push (result);
return result;
}
/* Get a recording::function instance, which is lazily-created and added
to the context's lists of mementos.
Implements the post-error-checking part of
gcc_jit_context_get_builtin_function. */
recording::function *
recording::context::get_builtin_function (const char *name)
{
if (!m_builtins_manager)
m_builtins_manager = new builtins_manager (this);
return m_builtins_manager->get_builtin_function (name);
}
/* Create a recording::global instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_global. */
recording::lvalue *
recording::context::new_global (recording::location *loc,
recording::type *type,
const char *name)
{
recording::lvalue *result =
new recording::global (this, loc, type, new_string (name));
record (result);
return result;
}
/* Create a recording::memento_of_new_rvalue_from_int 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. */
recording::rvalue *
recording::context::new_rvalue_from_int (recording::type *type,
int value)
{
recording::rvalue *result =
new memento_of_new_rvalue_from_int (this, NULL, type, value);
record (result);
return result;
}
/* Create a recording::memento_of_new_rvalue_from_double instance and
add it to this context's list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_rvalue_from_double. */
recording::rvalue *
recording::context::new_rvalue_from_double (recording::type *type,
double value)
{
recording::rvalue *result =
new memento_of_new_rvalue_from_double (this, NULL, type, value);
record (result);
return result;
}
/* Create a recording::memento_of_new_rvalue_from_ptr instance and add
it to this context's list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_rvalue_from_ptr. */
recording::rvalue *
recording::context::new_rvalue_from_ptr (recording::type *type,
void *value)
{
recording::rvalue *result =
new memento_of_new_rvalue_from_ptr (this, NULL, type, value);
record (result);
return result;
}
/* Create a recording::memento_of_new_string_literal instance and add it
to this context's list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_string_literal. */
recording::rvalue *
recording::context::new_string_literal (const char *value)
{
recording::rvalue *result =
new memento_of_new_string_literal (this, NULL, new_string (value));
record (result);
return result;
}
/* Create a recording::unary_op instance and add it to this context's
list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_unary_op. */
recording::rvalue *
recording::context::new_unary_op (recording::location *loc,
enum gcc_jit_unary_op op,
recording::type *result_type,
recording::rvalue *a)
{
recording::rvalue *result =
new unary_op (this, loc, op, result_type, a);
record (result);
return result;
}
/* Create a recording::binary_op instance and add it to this context's
list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_binary_op. */
recording::rvalue *
recording::context::new_binary_op (recording::location *loc,
enum gcc_jit_binary_op op,
recording::type *result_type,
recording::rvalue *a,
recording::rvalue *b)
{
recording::rvalue *result =
new binary_op (this, loc, op, result_type, a, b);
record (result);
return result;
}
/* Create a recording::comparison instance and add it to this context's
list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_comparison. */
recording::rvalue *
recording::context::new_comparison (recording::location *loc,
enum gcc_jit_comparison op,
recording::rvalue *a,
recording::rvalue *b)
{
recording::rvalue *result = new comparison (this, loc, op, a, b);
record (result);
return result;
}
/* Create a recording::cast instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_cast. */
recording::rvalue *
recording::context::new_cast (recording::location *loc,
recording::rvalue *expr,
recording::type *type_)
{
recording::rvalue *result = new cast (this, loc, expr, type_);
record (result);
return result;
}
/* Create a recording::call instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_call. */
recording::rvalue *
recording::context::new_call (recording::location *loc,
function *func,
int numargs , recording::rvalue **args)
{
recording::rvalue *result = new call (this, loc, func, numargs, args);
record (result);
return result;
}
/* Create a recording::call_through_ptr instance and add it to this
context's list of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_call_through_ptr. */
recording::rvalue *
recording::context::new_call_through_ptr (recording::location *loc,
recording::rvalue *fn_ptr,
int numargs,
recording::rvalue **args)
{
recording::rvalue *result = new call_through_ptr (this, loc, fn_ptr, numargs, args);
record (result);
return result;
}
/* Create a recording::array_access instance and add it to this context's list
of mementos.
Implements the post-error-checking part of
gcc_jit_context_new_array_access. */
recording::lvalue *
recording::context::new_array_access (recording::location *loc,
recording::rvalue *ptr,
recording::rvalue *index)
{
recording::lvalue *result = new array_access (this, loc, ptr, index);
record (result);
return result;
}
/* Set the given string option for this context, or add an error if
it's not recognized.
Implements the post-error-checking part of
gcc_jit_context_set_str_option. */
void
recording::context::set_str_option (enum gcc_jit_str_option opt,
const char *value)
{
if (opt < 0 || opt >= GCC_JIT_NUM_STR_OPTIONS)
{
add_error (NULL,
"unrecognized (enum gcc_jit_str_option) value: %i", opt);
return;
}
m_str_options[opt] = value;
}
/* Set the given integer option for this context, or add an error if
it's not recognized.
Implements the post-error-checking part of
gcc_jit_context_set_int_option. */
void
recording::context::set_int_option (enum gcc_jit_int_option opt,
int value)
{
if (opt < 0 || opt >= GCC_JIT_NUM_INT_OPTIONS)
{
add_error (NULL,
"unrecognized (enum gcc_jit_int_option) value: %i", opt);
return;
}
m_int_options[opt] = value;
}
/* Set the given boolean option for this context, or add an error if
it's not recognized.
Implements the post-error-checking part of
gcc_jit_context_set_bool_option. */
void
recording::context::set_bool_option (enum gcc_jit_bool_option opt,
int value)
{
if (opt < 0 || opt >= GCC_JIT_NUM_BOOL_OPTIONS)
{
add_error (NULL,
"unrecognized (enum gcc_jit_bool_option) value: %i", opt);
return;
}
m_bool_options[opt] = value ? true : false;
}
/* This mutex guards gcc::jit::recording::context::compile, so that only
one thread can be accessing the bulk of GCC's state at once. */
static pthread_mutex_t jit_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Validate this context, and if it passes, compile it within a
mutex.
Implements the post-error-checking part of
gcc_jit_context_compile. */
result *
recording::context::compile ()
{
validate ();
if (errors_occurred ())
return NULL;
/* Acquire the big GCC mutex. */
pthread_mutex_lock (&jit_mutex);
gcc_assert (NULL == ::gcc::jit::active_playback_ctxt);
/* Set up a playback context. */
::gcc::jit::playback::context replayer (this);
::gcc::jit::active_playback_ctxt = &replayer;
result *result_obj = replayer.compile ();
/* Release the big GCC mutex. */
::gcc::jit::active_playback_ctxt = NULL;
pthread_mutex_unlock (&jit_mutex);
return result_obj;
}
/* Format the given error using printf's conventions, print
it to stderr, and add it to the context. */
void
recording::context::add_error (location *loc, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
add_error_va (loc, fmt, ap);
va_end (ap);
}
/* Format the given error using printf's conventions, print
it to stderr, and add it to the context. */
void
recording::context::add_error_va (location *loc, const char *fmt, va_list ap)
{
char *malloced_msg;
const char *errmsg;
bool has_ownership;
vasprintf (&malloced_msg, fmt, ap);
if (malloced_msg)
{
errmsg = malloced_msg;
has_ownership = true;
}
else
{
errmsg = "out of memory generating error message";
has_ownership = false;
}
const char *ctxt_progname =
get_str_option (GCC_JIT_STR_OPTION_PROGNAME);
if (!ctxt_progname)
ctxt_progname = "libgccjit.so";
if (loc)
fprintf (stderr, "%s: %s: error: %s\n",
ctxt_progname,
loc->get_debug_string (),
errmsg);
else
fprintf (stderr, "%s: error: %s\n",
ctxt_progname,
errmsg);
if (!m_error_count)
{
m_first_error_str = const_cast <char *> (errmsg);
m_owns_first_error_str = has_ownership;
}
else
if (has_ownership)
free (malloced_msg);
m_error_count++;
}
/* Get the message for the first error that occurred on this context, or
NULL if no errors have occurred on it.
Implements the post-error-checking part of
gcc_jit_context_get_first_error. */
const char *
recording::context::get_first_error () const
{
return m_first_error_str;
}
/* Lazily generate and record a recording::type representing an opaque
struct named "FILE".
For use if client code tries to dereference the result of
get_type (GCC_JIT_TYPE_FILE_PTR). */
recording::type *
recording::context::get_opaque_FILE_type ()
{
if (!m_FILE_type)
m_FILE_type = new_struct_type (NULL, "FILE");
return m_FILE_type;
}
/* Dump a C-like representation of the given context to the given path.
If UPDATE_LOCATIONS is true, update the locations within the
context's mementos to point to the dumpfile.
Implements the post-error-checking part of
gcc_jit_context_dump_to_file. */
void
recording::context::dump_to_file (const char *path, bool update_locations)
{
int i;
dump d (*this, path, update_locations);
/* Forward declaration of structs and unions. */
compound_type *st;
FOR_EACH_VEC_ELT (m_compound_types, i, st)
{
d.write ("%s;\n\n", st->get_debug_string ());
}
/* Content of structs, where set. */
FOR_EACH_VEC_ELT (m_compound_types, i, st)
if (st->get_fields ())
{
st->get_fields ()->write_to_dump (d);
d.write ("\n");
}
function *fn;
FOR_EACH_VEC_ELT (m_functions, i, fn)
{
fn->write_to_dump (d);
}
}
/* This is a pre-compilation check for the context (and any parents).
Detect errors within the context, adding errors if any are found. */
void
recording::context::validate ()
{
if (m_parent_ctxt)
m_parent_ctxt->validate ();
int i;
function *fn;
FOR_EACH_VEC_ELT (m_functions, i, fn)
fn->validate ();
}
/* The implementation of class gcc::jit::recording::memento. */
/* Get a (const char *) debug description of the given memento, by
calling the pure-virtual make_debug_string hook, caching the
result.
It is intended that this should only be called in debugging and
error-handling paths, so this doesn't need to be particularly
optimized. */
const char *
recording::memento::get_debug_string ()
{
if (!m_debug_string)
m_debug_string = make_debug_string ();
return m_debug_string->c_str ();
}
/* Default implementation of recording::memento::write_to_dump, writing
an indented form of the memento's debug string to the dump. */
void
recording::memento::write_to_dump (dump &d)
{
d.write(" %s\n", get_debug_string ());
}
/* The implementation of class gcc::jit::recording::string. */
/* Constructor for gcc::jit::recording::string::string, allocating a
copy of the given text using new char[]. */
recording::string::string (context *ctxt, const char *text)
: memento (ctxt)
{
m_len = strlen (text);
m_buffer = new char[m_len + 1];
strcpy (m_buffer, text);
}
/* Destructor for gcc::jit::recording::string::string. */
recording::string::~string ()
{
delete[] m_buffer;
}
/* Function for making gcc::jit::recording::string instances on a
context via printf-style formatting.
It is intended that this should only be called in debugging and
error-handling paths, so this doesn't need to be particularly
optimized, hence the double-copy of the string is acceptable. */
recording::string *
recording::string::from_printf (context *ctxt, const char *fmt, ...)
{
va_list ap;
char *buf = NULL;
recording::string *result;
va_start (ap, fmt);
vasprintf (&buf, fmt, ap);
va_end (ap);
if (!buf)
{
ctxt->add_error (NULL, "malloc failure");
return NULL;
}
result = ctxt->new_string (buf);
free (buf);
return result;
}
/* Implementation of recording::memento::make_debug_string for strings,
wrapping the given string in quotes and escaping as necessary. */
recording::string *
recording::string::make_debug_string ()
{
/* Hack to avoid infinite recursion into strings when logging all
mementos: don't re-escape strings: */
if (m_buffer[0] == '"')
return this;
/* Wrap in quotes and do escaping etc */
size_t sz = (1 /* opening quote */
+ (m_len * 2) /* each char might get escaped */
+ 1 /* closing quote */
+ 1); /* nil termintator */
char *tmp = new char[sz];
size_t len = 0;
#define APPEND(CH) do { gcc_assert (len < sz); tmp[len++] = (CH); } while (0)
APPEND('"'); /* opening quote */
for (size_t i = 0; i < m_len ; i++)
{
char ch = m_buffer[i];
if (ch == '\t' || ch == '\n' || ch == '\\' || ch == '"')
APPEND('\\');
APPEND(ch);
}
APPEND('"'); /* closing quote */
#undef APPEND
tmp[len] = '\0'; /* nil termintator */
string *result = m_ctxt->new_string (tmp);
delete[] tmp;
return result;
}
/* The implementation of class gcc::jit::recording::location. */
/* Implementation of recording::memento::replay_into for locations.
Create a new playback::location and store it into the
recording::location's m_playback_obj field. */
void
recording::location::replay_into (replayer *r)
{
m_playback_obj = r->new_location (this,
m_filename->c_str (),
m_line,
m_column);
}
/* Implementation of recording::memento::make_debug_string for locations,
turning them into the usual form:
FILENAME:LINE:COLUMN
like we do when emitting diagnostics. */
recording::string *
recording::location::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s:%i:%i",
m_filename->c_str (), m_line, m_column);
}
/* The implementation of class gcc::jit::recording::type. */
/* Given a type T, get the type T*.
If this doesn't already exist, generate a new memento_of_get_pointer
instance and add it to this type's context's list of mementos.
Otherwise, use the cached type.
Implements the post-error-checking part of
gcc_jit_type_get_pointer. */
recording::type *
recording::type::get_pointer ()
{
if (!m_pointer_to_this_type)
{
m_pointer_to_this_type = new memento_of_get_pointer (this);
m_ctxt->record (m_pointer_to_this_type);
}
return m_pointer_to_this_type;
}
/* Given a type T, get the type const T.
Implements the post-error-checking part of
gcc_jit_type_get_const. */
recording::type *
recording::type::get_const ()
{
recording::type *result = new memento_of_get_const (this);
m_ctxt->record (result);
return result;
}
/* Given a type T, get the type volatile T.
Implements the post-error-checking part of
gcc_jit_type_get_volatile. */
recording::type *
recording::type::get_volatile ()
{
recording::type *result = new memento_of_get_volatile (this);
m_ctxt->record (result);
return result;
}
/* Implementation of pure virtual hook recording::type::dereference for
recording::memento_of_get_type. */
recording::type *
recording::memento_of_get_type::dereference ()
{
switch (m_kind)
{
default: gcc_unreachable ();
case GCC_JIT_TYPE_VOID:
return NULL;
case GCC_JIT_TYPE_VOID_PTR:
return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
case GCC_JIT_TYPE_BOOL:
case GCC_JIT_TYPE_CHAR:
case GCC_JIT_TYPE_SIGNED_CHAR:
case GCC_JIT_TYPE_UNSIGNED_CHAR:
case GCC_JIT_TYPE_SHORT:
case GCC_JIT_TYPE_UNSIGNED_SHORT:
case GCC_JIT_TYPE_INT:
case GCC_JIT_TYPE_UNSIGNED_INT:
case GCC_JIT_TYPE_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG:
case GCC_JIT_TYPE_LONG_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
case GCC_JIT_TYPE_FLOAT:
case GCC_JIT_TYPE_DOUBLE:
case GCC_JIT_TYPE_LONG_DOUBLE:
/* Not a pointer: */
return NULL;
case GCC_JIT_TYPE_CONST_CHAR_PTR:
return m_ctxt->get_type (GCC_JIT_TYPE_CHAR)->get_const ();
case GCC_JIT_TYPE_SIZE_T:
/* Not a pointer: */
return NULL;
case GCC_JIT_TYPE_FILE_PTR:
/* Give the client code back an opaque "struct FILE". */
return m_ctxt->get_opaque_FILE_type ();
}
}
/* Implementation of pure virtual hook recording::type::is_int for
recording::memento_of_get_type. */
bool
recording::memento_of_get_type::is_int () const
{
switch (m_kind)
{
default: gcc_unreachable ();
case GCC_JIT_TYPE_VOID:
return false;
case GCC_JIT_TYPE_VOID_PTR:
return false;
case GCC_JIT_TYPE_BOOL:
return false;
case GCC_JIT_TYPE_CHAR:
case GCC_JIT_TYPE_SIGNED_CHAR:
case GCC_JIT_TYPE_UNSIGNED_CHAR:
case GCC_JIT_TYPE_SHORT:
case GCC_JIT_TYPE_UNSIGNED_SHORT:
case GCC_JIT_TYPE_INT:
case GCC_JIT_TYPE_UNSIGNED_INT:
case GCC_JIT_TYPE_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG:
case GCC_JIT_TYPE_LONG_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
return true;
case GCC_JIT_TYPE_FLOAT:
case GCC_JIT_TYPE_DOUBLE:
case GCC_JIT_TYPE_LONG_DOUBLE:
return false;
case GCC_JIT_TYPE_CONST_CHAR_PTR:
return false;
case GCC_JIT_TYPE_SIZE_T:
return true;
case GCC_JIT_TYPE_FILE_PTR:
return false;
}
}
/* Implementation of pure virtual hook recording::type::is_float for
recording::memento_of_get_type. */
bool
recording::memento_of_get_type::is_float () const
{
switch (m_kind)
{
default: gcc_unreachable ();
case GCC_JIT_TYPE_VOID:
return false;
case GCC_JIT_TYPE_VOID_PTR:
return false;
case GCC_JIT_TYPE_BOOL:
return false;
case GCC_JIT_TYPE_CHAR:
case GCC_JIT_TYPE_SIGNED_CHAR:
case GCC_JIT_TYPE_UNSIGNED_CHAR:
case GCC_JIT_TYPE_SHORT:
case GCC_JIT_TYPE_UNSIGNED_SHORT:
case GCC_JIT_TYPE_INT:
case GCC_JIT_TYPE_UNSIGNED_INT:
case GCC_JIT_TYPE_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG:
case GCC_JIT_TYPE_LONG_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
return false;
case GCC_JIT_TYPE_FLOAT:
case GCC_JIT_TYPE_DOUBLE:
case GCC_JIT_TYPE_LONG_DOUBLE:
return true;
case GCC_JIT_TYPE_CONST_CHAR_PTR:
return false;
case GCC_JIT_TYPE_SIZE_T:
return false;
case GCC_JIT_TYPE_FILE_PTR:
return false;
}
}
/* Implementation of pure virtual hook recording::type::is_bool for
recording::memento_of_get_type. */
bool
recording::memento_of_get_type::is_bool () const
{
switch (m_kind)
{
default: gcc_unreachable ();
case GCC_JIT_TYPE_VOID:
return false;
case GCC_JIT_TYPE_VOID_PTR:
return false;
case GCC_JIT_TYPE_BOOL:
return true;
case GCC_JIT_TYPE_CHAR:
case GCC_JIT_TYPE_SIGNED_CHAR:
case GCC_JIT_TYPE_UNSIGNED_CHAR:
case GCC_JIT_TYPE_SHORT:
case GCC_JIT_TYPE_UNSIGNED_SHORT:
case GCC_JIT_TYPE_INT:
case GCC_JIT_TYPE_UNSIGNED_INT:
case GCC_JIT_TYPE_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG:
case GCC_JIT_TYPE_LONG_LONG:
case GCC_JIT_TYPE_UNSIGNED_LONG_LONG:
return false;
case GCC_JIT_TYPE_FLOAT:
case GCC_JIT_TYPE_DOUBLE:
case GCC_JIT_TYPE_LONG_DOUBLE:
return false;
case GCC_JIT_TYPE_CONST_CHAR_PTR:
return false;
case GCC_JIT_TYPE_SIZE_T:
return false;
case GCC_JIT_TYPE_FILE_PTR:
return false;
}
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_get_type. */
void
recording::memento_of_get_type::replay_into (replayer *r)
{
set_playback_obj (r->get_type (m_kind));
}
/* The implementation of class gcc::jit::recording::memento_of_get_type. */
/* Descriptive strings for each of enum gcc_jit_types. */
static const char * const get_type_strings[] = {
"void", /* GCC_JIT_TYPE_VOID */
"void *", /* GCC_JIT_TYPE_VOID_PTR */
"bool", /* GCC_JIT_TYPE_BOOL */
"char", /* GCC_JIT_TYPE_CHAR */
"signed char", /* GCC_JIT_TYPE_SIGNED_CHAR */
"unsigned char", /* GCC_JIT_TYPE_UNSIGNED_CHAR */
"short", /* GCC_JIT_TYPE_SHORT */
"unsigned short", /* GCC_JIT_TYPE_UNSIGNED_SHORT */
"int", /* GCC_JIT_TYPE_INT */
"unsigned int", /* GCC_JIT_TYPE_UNSIGNED_INT */
"long", /* GCC_JIT_TYPE_LONG */
"unsigned long", /* GCC_JIT_TYPE_UNSIGNED_LONG, */
"long long", /* GCC_JIT_TYPE_LONG_LONG */
"unsigned long long", /* GCC_JIT_TYPE_UNSIGNED_LONG_LONG */
"float", /* GCC_JIT_TYPE_FLOAT */
"double", /* GCC_JIT_TYPE_DOUBLE */
"long double", /* GCC_JIT_TYPE_LONG_DOUBLE */
"const char *", /* GCC_JIT_TYPE_CONST_CHAR_PTR */
"size_t", /* GCC_JIT_TYPE_SIZE_T */
"FILE *" /* GCC_JIT_TYPE_FILE_PTR */
};
/* Implementation of recording::memento::make_debug_string for
results of get_type, using a simple table of type names. */
recording::string *
recording::memento_of_get_type::make_debug_string ()
{
return m_ctxt->new_string (get_type_strings[m_kind]);
}
/* The implementation of class gcc::jit::recording::memento_of_get_pointer. */
/* Override of default implementation of
recording::type::accepts_writes_from for get_pointer.
Require a pointer type, and allowing writes to
(const T *) from a (T*), but not the other way around. */
bool
recording::memento_of_get_pointer::accepts_writes_from (type *rtype)
{
/* Must be a pointer type: */
type *rtype_points_to = rtype->is_pointer ();
if (!rtype_points_to)
return false;
/* It's OK to assign to a (const T *) from a (T *). */
return m_other_type->unqualified ()
->accepts_writes_from (rtype_points_to);
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_get_pointer. */
void
recording::memento_of_get_pointer::replay_into (replayer *)
{
set_playback_obj (m_other_type->playback_type ()->get_pointer ());
}
/* Implementation of recording::memento::make_debug_string for
results of get_pointer, adding " *" to the underlying type,
with special-casing to handle function pointer types. */
recording::string *
recording::memento_of_get_pointer::make_debug_string ()
{
/* Special-case function pointer types, to put the "*" in parens between
the return type and the params (for one level of dereferencing, at
least). */
if (function_type *fn_type = m_other_type->dyn_cast_function_type ())
return fn_type->make_debug_string_with_ptr ();
return string::from_printf (m_ctxt,
"%s *", m_other_type->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::memento_of_get_const. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_get_const. */
void
recording::memento_of_get_const::replay_into (replayer *)
{
set_playback_obj (m_other_type->playback_type ()->get_const ());
}
/* Implementation of recording::memento::make_debug_string for
results of get_const, prepending "const ". */
recording::string *
recording::memento_of_get_const::make_debug_string ()
{
return string::from_printf (m_ctxt,
"const %s", m_other_type->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::memento_of_get_volatile. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_get_volatile. */
void
recording::memento_of_get_volatile::replay_into (replayer *)
{
set_playback_obj (m_other_type->playback_type ()->get_volatile ());
}
/* Implementation of recording::memento::make_debug_string for
results of get_volatile, prepending "volatile ". */
recording::string *
recording::memento_of_get_volatile::make_debug_string ()
{
return string::from_printf (m_ctxt,
"volatile %s", m_other_type->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::array_type */
/* Implementation of pure virtual hook recording::type::dereference for
recording::array_type. */
recording::type *
recording::array_type::dereference ()
{
return m_element_type;
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::array_type. */
void
recording::array_type::replay_into (replayer *r)
{
set_playback_obj (r->new_array_type (playback_location (r, m_loc),
m_element_type->playback_type (),
m_num_elements));
}
/* Implementation of recording::memento::make_debug_string for
results of new_array_type. */
recording::string *
recording::array_type::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s[%d]",
m_element_type->get_debug_string (),
m_num_elements);
}
/* The implementation of class gcc::jit::recording::function_type */
/* Constructor for gcc::jit::recording::function_type. */
recording::function_type::function_type (context *ctxt,
type *return_type,
int num_params,
type **param_types,
int is_variadic)
: type (ctxt),
m_return_type (return_type),
m_param_types (),
m_is_variadic (is_variadic)
{
for (int i = 0; i< num_params; i++)
m_param_types.safe_push (param_types[i]);
}
/* Implementation of pure virtual hook recording::type::dereference for
recording::function_type. */
recording::type *
recording::function_type::dereference ()
{
return NULL;
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::function_type. */
void
recording::function_type::replay_into (replayer *r)
{
/* Convert m_param_types to a vec of playback type. */
vec <playback::type *> param_types;
int i;
recording::type *type;
param_types.create (m_param_types.length ());
FOR_EACH_VEC_ELT (m_param_types, i, type)
param_types.safe_push (type->playback_type ());
set_playback_obj (r->new_function_type (m_return_type->playback_type (),
¶m_types,
m_is_variadic));
}
/* Special-casing for make_debug_string for get_pointer results for
handling (one level) of pointers to functions. */
recording::string *
recording::function_type::make_debug_string_with_ptr ()
{
return make_debug_string_with ("(*) ");
}
/* Implementation of recording::memento::make_debug_string for
results of new_function_type. */
recording::string *
recording::function_type::make_debug_string ()
{
return make_debug_string_with ("");
}
/* Build a debug string representation of the form:
RESULT_TYPE INSERT (PARAM_TYPES)
for use when handling 0 and 1 level of indirection to this
function type. */
recording::string *
recording::function_type::make_debug_string_with (const char *insert)
{
/* First, build a buffer for the arguments. */
/* Calculate length of said buffer. */
size_t sz = 1; /* nil terminator */
for (unsigned i = 0; i< m_param_types.length (); i++)
{
sz += strlen (m_param_types[i]->get_debug_string ());
sz += 2; /* ", " separator */
}
if (m_is_variadic)
sz += 5; /* ", ..." separator and ellipsis */
/* Now allocate and populate the buffer. */
char *argbuf = new char[sz];
size_t len = 0;
for (unsigned i = 0; i< m_param_types.length (); i++)
{
strcpy (argbuf + len, m_param_types[i]->get_debug_string ());
len += strlen (m_param_types[i]->get_debug_string ());
if (i + 1 < m_param_types.length ())
{
strcpy (argbuf + len, ", ");
len += 2;
}
}
if (m_is_variadic)
{
if (m_param_types.length ())
{
strcpy (argbuf + len, ", ");
len += 2;
}
strcpy (argbuf + len, "...");
len += 3;
}
argbuf[len] = '\0';
/* ...and use it to get the string for the call as a whole. */
string *result = string::from_printf (m_ctxt,
"%s %s(%s)",
m_return_type->get_debug_string (),
insert,
argbuf);
delete[] argbuf;
return result;
}
/* The implementation of class gcc::jit::recording::field. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::field. */
void
recording::field::replay_into (replayer *r)
{
set_playback_obj (r->new_field (playback_location (r, m_loc),
m_type->playback_type (),
playback_string (m_name)));
}
/* Override the default implementation of
recording::memento::write_to_dump. Dump each field
by dumping a line of the form:
TYPE NAME;
so that we can build up a struct/union field-byfield. */
void
recording::field::write_to_dump (dump &d)
{
d.write (" %s %s;\n",
m_type->get_debug_string (),
m_name->c_str ());
}
/* Implementation of recording::memento::make_debug_string for
results of new_field. */
recording::string *
recording::field::make_debug_string ()
{
return m_name;
}
/* The implementation of class gcc::jit::recording::compound_type */
/* The constructor for gcc::jit::recording::compound_type. */
recording::compound_type::compound_type (context *ctxt,
location *loc,
string *name)
: type (ctxt),
m_loc (loc),
m_name (name),
m_fields (NULL)
{
}
/* Set the fields of a compound type.
Implements the post-error-checking part of
gcc_jit_struct_set_fields, and is also used by
gcc_jit_context_new_union_type. */
void
recording::compound_type::set_fields (location *loc,
int num_fields,
field **field_array)
{
m_loc = loc;
gcc_assert (NULL == m_fields);
m_fields = new fields (this, num_fields, field_array);
m_ctxt->record (m_fields);
}
/* Implementation of pure virtual hook recording::type::dereference for
recording::compound_type. */
recording::type *
recording::compound_type::dereference ()
{
return NULL; /* not a pointer */
}
/* The implementation of class gcc::jit::recording::struct_. */
/* The constructor for gcc::jit::recording::struct_. */
recording::struct_::struct_ (context *ctxt,
location *loc,
string *name)
: compound_type (ctxt, loc, name)
{
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::struct_. */
void
recording::struct_::replay_into (replayer *r)
{
set_playback_obj (
r->new_compound_type (playback_location (r, get_loc ()),
get_name ()->c_str (),
true /* is_struct */));
}
/* Implementation of recording::memento::make_debug_string for
structs. */
recording::string *
recording::struct_::make_debug_string ()
{
return string::from_printf (m_ctxt,
"struct %s", get_name ()->c_str ());
}
/* The implementation of class gcc::jit::recording::union_. */
/* The constructor for gcc::jit::recording::union_. */
recording::union_::union_ (context *ctxt,
location *loc,
string *name)
: compound_type (ctxt, loc, name)
{
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::union_. */
void
recording::union_::replay_into (replayer *r)
{
set_playback_obj (
r->new_compound_type (playback_location (r, get_loc ()),
get_name ()->c_str (),
false /* is_struct */));
}
/* Implementation of recording::memento::make_debug_string for
unions. */
recording::string *
recording::union_::make_debug_string ()
{
return string::from_printf (m_ctxt,
"union %s", get_name ()->c_str ());
}
/* The implementation of class gcc::jit::recording::fields. */
/* The constructor for gcc::jit::recording::fields. */
recording::fields::fields (compound_type *struct_or_union,
int num_fields,
field **fields)
: memento (struct_or_union->m_ctxt),
m_struct_or_union (struct_or_union),
m_fields ()
{
for (int i = 0; i < num_fields; i++)
{
gcc_assert (fields[i]->get_container () == NULL);
fields[i]->set_container (m_struct_or_union);
m_fields.safe_push (fields[i]);
}
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::fields. */
void
recording::fields::replay_into (replayer *)
{
vec<playback::field *> playback_fields;
playback_fields.create (m_fields.length ());
for (unsigned i = 0; i < m_fields.length (); i++)
playback_fields.safe_push (m_fields[i]->playback_field ());
m_struct_or_union->playback_compound_type ()->set_fields (playback_fields);
}
/* Override the default implementation of
recording::memento::write_to_dump by writing a union/struct
declaration of this form:
struct/union NAME {
TYPE_1 NAME_1;
TYPE_2 NAME_2;
....
TYPE_N NAME_N;
};
to the dump. */
void
recording::fields::write_to_dump (dump &d)
{
int i;
field *f;
d.write ("%s\n{\n", m_struct_or_union->get_debug_string ());
FOR_EACH_VEC_ELT (m_fields, i, f)
f->write_to_dump (d);
d.write ("};\n");
}
/* Implementation of recording::memento::make_debug_string for
field tables. */
recording::string *
recording::fields::make_debug_string ()
{
return string::from_printf (m_ctxt,
"fields");
}
/* The implementation of class gcc::jit::recording::rvalue. */
/* Create a recording::access_field_rvalue instance and add it to
the rvalue's context's list of mementos.
Implements the post-error-checking part of
gcc_jit_rvalue_access_field. */
recording::rvalue *
recording::rvalue::access_field (recording::location *loc,
field *field)
{
recording::rvalue *result =
new access_field_rvalue (m_ctxt, loc, this, field);
m_ctxt->record (result);
return result;
}
/* Create a recording::dereference_field_rvalue instance and add it to
the rvalue's context's list of mementos.
Implements the post-error-checking part of
gcc_jit_rvalue_dereference_field. */
recording::lvalue *
recording::rvalue::dereference_field (recording::location *loc,
field *field)
{
recording::lvalue *result =
new dereference_field_rvalue (m_ctxt, loc, this, field);
m_ctxt->record (result);
return result;
}
/* Create a recording::dereference_rvalue instance and add it to the
rvalue's context's list of mementos.
Implements the post-error-checking part of
gcc_jit_rvalue_dereference. */
recording::lvalue *
recording::rvalue::dereference (recording::location *loc)
{
recording::lvalue *result =
new dereference_rvalue (m_ctxt, loc, this);
m_ctxt->record (result);
return result;
}
/* The implementation of class gcc::jit::recording::lvalue. */
/* Create a recording::new_access_field_of_lvalue instance and add it to
the lvalue's context's list of mementos.
Implements the post-error-checking part of
gcc_jit_lvalue_access_field. */
recording::lvalue *
recording::lvalue::access_field (recording::location *loc,
field *field)
{
recording::lvalue *result =
new access_field_of_lvalue (m_ctxt, loc, this, field);
m_ctxt->record (result);
return result;
}
/* Create a recording::get_address_of_lvalue instance and add it to
the lvalue's context's list of mementos.
Implements the post-error-checking part of
gcc_jit_lvalue_get_address. */
recording::rvalue *
recording::lvalue::get_address (recording::location *loc)
{
recording::rvalue *result =
new get_address_of_lvalue (m_ctxt, loc, this);
m_ctxt->record (result);
return result;
}
/* The implementation of class gcc::jit::recording::param. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::param. */
void
recording::param::replay_into (replayer *r)
{
set_playback_obj (r->new_param (playback_location (r, m_loc),
m_type->playback_type (),
m_name->c_str ()));
}
/* The implementation of class gcc::jit::recording::function. */
/* gcc::jit::recording::function's constructor. */
recording::function::function (context *ctxt,
recording::location *loc,
enum gcc_jit_function_kind kind,
type *return_type,
recording::string *name,
int num_params,
recording::param **params,
int is_variadic,
enum built_in_function builtin_id)
: memento (ctxt),
m_loc (loc),
m_kind (kind),
m_return_type (return_type),
m_name (name),
m_params (),
m_is_variadic (is_variadic),
m_builtin_id (builtin_id),
m_locals (),
m_blocks ()
{
for (int i = 0; i< num_params; i++)
m_params.safe_push (params[i]);
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::function. */
void
recording::function::replay_into (replayer *r)
{
/* Convert m_params to a vec of playback param. */
vec <playback::param *> params;
int i;
recording::param *param;
params.create (m_params.length ());
FOR_EACH_VEC_ELT (m_params, i, param)
params.safe_push (param->playback_param ());
set_playback_obj (r->new_function (playback_location (r, m_loc),
m_kind,
m_return_type->playback_type (),
m_name->c_str (),
¶ms,
m_is_variadic,
m_builtin_id));
}
/* Create a recording::local instance and add it to
the functions's context's list of mementos, and to the function's
list of locals.
Implements the post-error-checking part of
gcc_jit_function_new_local. */
recording::lvalue *
recording::function::new_local (recording::location *loc,
type *type,
const char *name)
{
local *result = new local (this, loc, type, new_string (name));
m_ctxt->record (result);
m_locals.safe_push (result);
return result;
}
/* Create a recording::block instance and add it to
the functions's context's list of mementos, and to the function's
list of blocks.
Implements the post-error-checking part of
gcc_jit_function_new_block. */
recording::block*
recording::function::new_block (const char *name)
{
gcc_assert (m_kind != GCC_JIT_FUNCTION_IMPORTED);
recording::block *result =
new recording::block (this, m_blocks.length (), new_string (name));
m_ctxt->record (result);
m_blocks.safe_push (result);
return result;
}
/* Override the default implementation of
recording::memento::write_to_dump by dumping a C-like
representation of the function; either like a prototype
for GCC_JIT_FUNCTION_IMPORTED, or like a full definition for
all other kinds of function. */
void
recording::function::write_to_dump (dump &d)
{
switch (m_kind)
{
default: gcc_unreachable ();
case GCC_JIT_FUNCTION_EXPORTED:
case GCC_JIT_FUNCTION_IMPORTED:
d.write ("extern ");
break;
case GCC_JIT_FUNCTION_INTERNAL:
d.write ("static ");
break;
case GCC_JIT_FUNCTION_ALWAYS_INLINE:
d.write ("static inline ");
break;
}
d.write ("%s\n", m_return_type->get_debug_string ());
if (d.update_locations ())
m_loc = d.make_location ();
d.write ("%s (", get_debug_string ());
int i;
recording::param *param;
FOR_EACH_VEC_ELT (m_params, i, param)
{
if (i > 0)
d.write (", ");
d.write ("%s %s",
param->get_type ()->get_debug_string (),
param->get_debug_string ());
}
d.write (")");
if (m_kind == GCC_JIT_FUNCTION_IMPORTED)
{
d.write ("; /* (imported) */\n\n");
}
else
{
int i;
local *var = NULL;
block *b;
d.write ("\n{\n");
/* Write locals: */
FOR_EACH_VEC_ELT (m_locals, i, var)
var->write_to_dump (d);
if (m_locals.length ())
d.write ("\n");
/* Write each block: */
FOR_EACH_VEC_ELT (m_blocks, i, b)
{
if (i > 0)
d.write ("\n");
b->write_to_dump (d);
}
d.write ("}\n\n");
}
}
/* Pre-compilation validation of a function, for those things we can't
check until the context is (supposedly) fully-populated. */
void
recording::function::validate ()
{
/* Complain about empty functions with non-void return type. */
if (m_kind != GCC_JIT_FUNCTION_IMPORTED
&& m_return_type != m_ctxt->get_type (GCC_JIT_TYPE_VOID))
if (0 == m_blocks.length ())
m_ctxt->add_error (m_loc,
"function %s returns non-void (type: %s)"
" but has no blocks",
get_debug_string (),
m_return_type->get_debug_string ());
/* Check that all blocks are terminated. */
int num_invalid_blocks = 0;
{
int i;
block *b;
FOR_EACH_VEC_ELT (m_blocks, i, b)
if (!b->validate ())
num_invalid_blocks++;
}
/* Check that all blocks are reachable. */
if (m_blocks.length () > 0 && 0 == num_invalid_blocks)
{
/* Iteratively walk the graph of blocks, marking their "m_is_reachable"
flag, starting at the initial block. */
auto_vec<block *> worklist (m_blocks.length ());
worklist.safe_push (m_blocks[0]);
while (worklist.length () > 0)
{
block *b = worklist.pop ();
b->m_is_reachable = true;
/* Add successor blocks that aren't yet marked to the worklist. */
/* We checked that each block has a terminating statement above . */
block *next1, *next2;
int n = b->get_successor_blocks (&next1, &next2);
switch (n)
{
default:
gcc_unreachable ();
case 2:
if (!next2->m_is_reachable)
worklist.safe_push (next2);
/* fallthrough */
case 1:
if (!next1->m_is_reachable)
worklist.safe_push (next1);
break;
case 0:
break;
}
}
/* Now complain about any blocks that haven't been marked. */
{
int i;
block *b;
FOR_EACH_VEC_ELT (m_blocks, i, b)
if (!b->m_is_reachable)
m_ctxt->add_error (b->get_loc (),
"unreachable block: %s",
b->get_debug_string ());
}
}
}
/* Implements the post-error-checking part of
gcc_jit_function_dump_to_dot. */
void
recording::function::dump_to_dot (const char *path)
{
FILE *fp = fopen (path, "w");
if (!fp)
return;
pretty_printer the_pp;
the_pp.buffer->stream = fp;
pretty_printer *pp = &the_pp;
pp_printf (pp,
"digraph %s {\n", get_debug_string ());
/* Blocks: */
{
int i;
block *b;
FOR_EACH_VEC_ELT (m_blocks, i, b)
b->dump_to_dot (pp);
}
/* Edges: */
{
int i;
block *b;
FOR_EACH_VEC_ELT (m_blocks, i, b)
b->dump_edges_to_dot (pp);
}
pp_printf (pp, "}\n");
pp_flush (pp);
fclose (fp);
}
/* Implementation of recording::memento::make_debug_string for
functions. */
recording::string *
recording::function::make_debug_string ()
{
return m_name;
}
/* The implementation of class gcc::jit::recording::block. */
/* Create a recording::eval instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking part of
gcc_jit_block_add_eval. */
void
recording::block::add_eval (recording::location *loc,
recording::rvalue *rvalue)
{
statement *result = new eval (this, loc, rvalue);
m_ctxt->record (result);
m_statements.safe_push (result);
}
/* Create a recording::assignment instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking part of
gcc_jit_block_add_assignment. */
void
recording::block::add_assignment (recording::location *loc,
recording::lvalue *lvalue,
recording::rvalue *rvalue)
{
statement *result = new assignment (this, loc, lvalue, rvalue);
m_ctxt->record (result);
m_statements.safe_push (result);
}
/* Create a recording::assignment_op instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking part of
gcc_jit_block_add_assignment_op. */
void
recording::block::add_assignment_op (recording::location *loc,
recording::lvalue *lvalue,
enum gcc_jit_binary_op op,
recording::rvalue *rvalue)
{
statement *result = new assignment_op (this, loc, lvalue, op, rvalue);
m_ctxt->record (result);
m_statements.safe_push (result);
}
/* Create a recording::comment instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking part of
gcc_jit_block_add_comment. */
void
recording::block::add_comment (recording::location *loc,
const char *text)
{
statement *result = new comment (this, loc, new_string (text));
m_ctxt->record (result);
m_statements.safe_push (result);
}
/* Create a recording::end_with_conditional instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking part of
gcc_jit_block_end_with_conditional. */
void
recording::block::end_with_conditional (recording::location *loc,
recording::rvalue *boolval,
recording::block *on_true,
recording::block *on_false)
{
statement *result = new conditional (this, loc, boolval, on_true, on_false);
m_ctxt->record (result);
m_statements.safe_push (result);
m_has_been_terminated = true;
}
/* Create a recording::end_with_jump instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking part of
gcc_jit_block_end_with_jump. */
void
recording::block::end_with_jump (recording::location *loc,
recording::block *target)
{
statement *result = new jump (this, loc, target);
m_ctxt->record (result);
m_statements.safe_push (result);
m_has_been_terminated = true;
}
/* Create a recording::end_with_return instance and add it to
the block's context's list of mementos, and to the block's
list of statements.
Implements the post-error-checking parts of
gcc_jit_block_end_with_return and
gcc_jit_block_end_with_void_return. */
void
recording::block::end_with_return (recording::location *loc,
recording::rvalue *rvalue)
{
/* This is used by both gcc_jit_function_add_return and
gcc_jit_function_add_void_return; rvalue will be non-NULL for
the former and NULL for the latter. */
statement *result = new return_ (this, loc, rvalue);
m_ctxt->record (result);
m_statements.safe_push (result);
m_has_been_terminated = true;
}
/* Override the default implementation of
recording::memento::write_to_dump for blocks by writing
an unindented block name as a label, followed by the indented
statements:
BLOCK_NAME:
STATEMENT_1;
STATEMENT_2;
...
STATEMENT_N; */
void
recording::block::write_to_dump (dump &d)
{
d.write ("%s:\n", get_debug_string ());
int i;
statement *s;
FOR_EACH_VEC_ELT (m_statements, i, s)
s->write_to_dump (d);
}
/* Validate a block by ensuring that it has been terminated. */
bool
recording::block::validate ()
{
if (!has_been_terminated ())
{
statement *stmt = get_last_statement ();
location *loc = stmt ? stmt->get_loc () : NULL;
m_func->get_context ()->add_error (loc,
"unterminated block in %s: %s",
m_func->get_debug_string (),
get_debug_string ());
return false;
}
return true;
}
/* Get the source-location of a block by using that of the first
statement within it, if any. */
recording::location *
recording::block::get_loc () const
{
recording::statement *stmt = get_first_statement ();
if (stmt)
return stmt->get_loc ();
else
return NULL;
}
/* Get the first statement within a block, if any. */
recording::statement *
recording::block::get_first_statement () const
{
if (m_statements.length ())
return m_statements[0];
else
return NULL;
}
/* Get the last statement within a block, if any. */
recording::statement *
recording::block::get_last_statement () const
{
if (m_statements.length ())
return m_statements[m_statements.length () - 1];
else
return NULL;
}
/* Assuming that this block has been terminated, get the number of
successor blocks, which will be 0, 1 or 2, for return, unconditional
jump, and conditional jump respectively.
NEXT1 and NEXT2 must be non-NULL. The first successor block (if any)
is written to NEXT1, and the second (if any) to NEXT2.
Used when validating functions, and when dumping dot representations
of them. */
int
recording::block::get_successor_blocks (block **next1, block **next2) const
{
gcc_assert (m_has_been_terminated);
gcc_assert (next1);
gcc_assert (next2);
statement *last_statement = get_last_statement ();
gcc_assert (last_statement);
return last_statement->get_successor_blocks (next1, next2);
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::block. */
void
recording::block::replay_into (replayer *)
{
set_playback_obj (m_func->playback_function ()
->new_block (playback_string (m_name)));
}
/* Implementation of recording::memento::make_debug_string for
blocks. */
recording::string *
recording::block::make_debug_string ()
{
if (m_name)
return m_name;
else
return string::from_printf (m_ctxt,
"<UNNAMED BLOCK %p>",
(void *)this);
}
/* Dump a block in graphviz form into PP, capturing the block name (if
any) and the statements. */
void
recording::block::dump_to_dot (pretty_printer *pp)
{
pp_printf (pp,
("\tblock_%d "
"[shape=record,style=filled,fillcolor=white,label=\"{"),
m_index);
pp_write_text_to_stream (pp);
if (m_name)
{
pp_string (pp, m_name->c_str ());
pp_string (pp, ":");
pp_newline (pp);
pp_write_text_as_dot_label_to_stream (pp, true /*for_record*/);
}
int i;
statement *s;
FOR_EACH_VEC_ELT (m_statements, i, s)
{
pp_string (pp, s->get_debug_string ());
pp_newline (pp);
pp_write_text_as_dot_label_to_stream (pp, true /*for_record*/);
}
pp_printf (pp,
"}\"];\n\n");
pp_flush (pp);
}
/* Dump the out-edges of the block in graphviz form into PP. */
void
recording::block::dump_edges_to_dot (pretty_printer *pp)
{
block *next[2];
int num_succs = get_successor_blocks (&next[0], &next[1]);
for (int i = 0; i < num_succs; i++)
pp_printf (pp,
"\tblock_%d:s -> block_%d:n;\n",
m_index, next[i]->m_index);
}
/* The implementation of class gcc::jit::recording::global. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::global. */
void
recording::global::replay_into (replayer *r)
{
set_playback_obj (r->new_global (playback_location (r, m_loc),
m_type->playback_type (),
playback_string (m_name)));
}
/* The implementation of class gcc::jit::recording::memento_of_new_rvalue_from_int. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_new_rvalue_from_int. */
void
recording::memento_of_new_rvalue_from_int::replay_into (replayer *r)
{
set_playback_obj (r->new_rvalue_from_int (m_type->playback_type (),
m_value));
}
/* Implementation of recording::memento::make_debug_string for
rvalue_from_int, rendering it as
(TYPE)LITERAL
e.g.
"(int)42". */
recording::string *
recording::memento_of_new_rvalue_from_int::make_debug_string ()
{
return string::from_printf (m_ctxt,
"(%s)%i",
m_type->get_debug_string (),
m_value);
}
/* The implementation of class gcc::jit::recording::memento_of_new_rvalue_from_double. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_new_rvalue_from_double. */
void
recording::memento_of_new_rvalue_from_double::replay_into (replayer *r)
{
set_playback_obj (r->new_rvalue_from_double (m_type->playback_type (),
m_value));
}
/* Implementation of recording::memento::make_debug_string for
rvalue_from_double, rendering it as
(TYPE)LITERAL
e.g.
"(float)42.0". */
recording::string *
recording::memento_of_new_rvalue_from_double::make_debug_string ()
{
return string::from_printf (m_ctxt,
"(%s)%f",
m_type->get_debug_string (),
m_value);
}
/* The implementation of class gcc::jit::recording::memento_of_new_rvalue_from_ptr. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_new_rvalue_from_ptr. */
void
recording::memento_of_new_rvalue_from_ptr::replay_into (replayer *r)
{
set_playback_obj (r->new_rvalue_from_ptr (m_type->playback_type (),
m_value));
}
/* Implementation of recording::memento::make_debug_string for
rvalue_from_ptr, rendering it as
(TYPE)HEX
e.g.
"(int *)0xdeadbeef"
Zero is rendered as NULL e.g.
"(int *)NULL". */
recording::string *
recording::memento_of_new_rvalue_from_ptr::make_debug_string ()
{
if (m_value != NULL)
return string::from_printf (m_ctxt,
"(%s)%p",
m_type->get_debug_string (), m_value);
else
return string::from_printf (m_ctxt,
"(%s)NULL",
m_type->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::memento_of_new_string_literal. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::memento_of_new_string_literal. */
void
recording::memento_of_new_string_literal::replay_into (replayer *r)
{
set_playback_obj (r->new_string_literal (m_value->c_str ()));
}
/* Implementation of recording::memento::make_debug_string for
string literals. */
recording::string *
recording::memento_of_new_string_literal::make_debug_string ()
{
return string::from_printf (m_ctxt,
m_value->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::unary_op. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::unary_op. */
void
recording::unary_op::replay_into (replayer *r)
{
set_playback_obj (r->new_unary_op (playback_location (r, m_loc),
m_op,
get_type ()->playback_type (),
m_a->playback_rvalue ()));
}
/* Implementation of recording::memento::make_debug_string for
unary ops. */
static const char * const unary_op_strings[] = {
"-", /* GCC_JIT_UNARY_OP_MINUS */
"~", /* GCC_JIT_UNARY_OP_BITWISE_NEGATE */
"!", /* GCC_JIT_UNARY_OP_LOGICAL_NEGATE */
};
recording::string *
recording::unary_op::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s(%s)",
unary_op_strings[m_op],
m_a->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::binary_op. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::binary_op. */
void
recording::binary_op::replay_into (replayer *r)
{
set_playback_obj (r->new_binary_op (playback_location (r, m_loc),
m_op,
get_type ()->playback_type (),
m_a->playback_rvalue (),
m_b->playback_rvalue ()));
}
/* Implementation of recording::memento::make_debug_string for
binary ops. */
static const char * const binary_op_strings[] = {
"+", /* GCC_JIT_BINARY_OP_PLUS */
"-", /* GCC_JIT_BINARY_OP_MINUS */
"*", /* GCC_JIT_BINARY_OP_MULT */
"/", /* GCC_JIT_BINARY_OP_DIVIDE */
"%", /* GCC_JIT_BINARY_OP_MODULO */
"&", /* GCC_JIT_BINARY_OP_BITWISE_AND */
"^", /* GCC_JIT_BINARY_OP_BITWISE_XOR */
"|", /* GCC_JIT_BINARY_OP_BITWISE_OR */
"&&", /* GCC_JIT_BINARY_OP_LOGICAL_AND */
"||", /* GCC_JIT_BINARY_OP_LOGICAL_OR */
"<<", /* GCC_JIT_BINARY_OP_LSHIFT */
">>", /* GCC_JIT_BINARY_OP_RSHIFT */
};
recording::string *
recording::binary_op::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s %s %s",
m_a->get_debug_string (),
binary_op_strings[m_op],
m_b->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::comparison. */
/* Implementation of recording::memento::make_debug_string for
comparisons. */
static const char * const comparison_strings[] =
{
"==", /* GCC_JIT_COMPARISON_EQ */
"!=", /* GCC_JIT_COMPARISON_NE */
"<", /* GCC_JIT_COMPARISON_LT */
"<=", /* GCC_JIT_COMPARISON_LE */
">", /* GCC_JIT_COMPARISON_GT */
">=", /* GCC_JIT_COMPARISON_GE */
};
recording::string *
recording::comparison::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s %s %s",
m_a->get_debug_string (),
comparison_strings[m_op],
m_b->get_debug_string ());
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::comparison. */
void
recording::comparison::replay_into (replayer *r)
{
set_playback_obj (r->new_comparison (playback_location (r, m_loc),
m_op,
m_a->playback_rvalue (),
m_b->playback_rvalue ()));
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::cast. */
void
recording::cast::replay_into (replayer *r)
{
set_playback_obj (r->new_cast (playback_location (r, m_loc),
m_rvalue->playback_rvalue (),
get_type ()->playback_type ()));
}
/* Implementation of recording::memento::make_debug_string for
casts. */
recording::string *
recording::cast::make_debug_string ()
{
return string::from_printf (m_ctxt,
"(%s)%s",
get_type ()->get_debug_string (),
m_rvalue->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::call. */
/* The constructor for gcc::jit::recording::call. */
recording::call::call (recording::context *ctxt,
recording::location *loc,
recording::function *func,
int numargs,
rvalue **args)
: rvalue (ctxt, loc, func->get_return_type ()),
m_func (func),
m_args ()
{
for (int i = 0; i< numargs; i++)
m_args.safe_push (args[i]);
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::call. */
void
recording::call::replay_into (replayer *r)
{
vec<playback::rvalue *> playback_args;
playback_args.create (m_args.length ());
for (unsigned i = 0; i< m_args.length (); i++)
playback_args.safe_push (m_args[i]->playback_rvalue ());
set_playback_obj (r->new_call (playback_location (r, m_loc),
m_func->playback_function (),
playback_args));
}
/* Implementation of recording::memento::make_debug_string for
function calls. */
recording::string *
recording::call::make_debug_string ()
{
/* First, build a buffer for the arguments. */
/* Calculate length of said buffer. */
size_t sz = 1; /* nil terminator */
for (unsigned i = 0; i< m_args.length (); i++)
{
sz += strlen (m_args[i]->get_debug_string ());
sz += 2; /* ", " separator */
}
/* Now allocate and populate the buffer. */
char *argbuf = new char[sz];
size_t len = 0;
for (unsigned i = 0; i< m_args.length (); i++)
{
strcpy (argbuf + len, m_args[i]->get_debug_string ());
len += strlen (m_args[i]->get_debug_string ());
if (i + 1 < m_args.length ())
{
strcpy (argbuf + len, ", ");
len += 2;
}
}
argbuf[len] = '\0';
/* ...and use it to get the string for the call as a whole. */
string *result = string::from_printf (m_ctxt,
"%s (%s)",
m_func->get_debug_string (),
argbuf);
delete[] argbuf;
return result;
}
/* The implementation of class gcc::jit::recording::call_through_ptr. */
/* The constructor for recording::call_through_ptr. */
recording::call_through_ptr::call_through_ptr (recording::context *ctxt,
recording::location *loc,
recording::rvalue *fn_ptr,
int numargs,
rvalue **args)
: rvalue (ctxt, loc,
fn_ptr->get_type ()->dereference ()
->as_a_function_type ()->get_return_type ()),
m_fn_ptr (fn_ptr),
m_args ()
{
for (int i = 0; i< numargs; i++)
m_args.safe_push (args[i]);
}
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::call_through_ptr. */
void
recording::call_through_ptr::replay_into (replayer *r)
{
vec<playback::rvalue *> playback_args;
playback_args.create (m_args.length ());
for (unsigned i = 0; i< m_args.length (); i++)
playback_args.safe_push (m_args[i]->playback_rvalue ());
set_playback_obj (r->new_call_through_ptr (playback_location (r, m_loc),
m_fn_ptr->playback_rvalue (),
playback_args));
}
/* Implementation of recording::memento::make_debug_string for
calls through function ptrs. */
recording::string *
recording::call_through_ptr::make_debug_string ()
{
/* First, build a buffer for the arguments. */
/* Calculate length of said buffer. */
size_t sz = 1; /* nil terminator */
for (unsigned i = 0; i< m_args.length (); i++)
{
sz += strlen (m_args[i]->get_debug_string ());
sz += 2; /* ", " separator */
}
/* Now allocate and populate the buffer. */
char *argbuf = new char[sz];
size_t len = 0;
for (unsigned i = 0; i< m_args.length (); i++)
{
strcpy (argbuf + len, m_args[i]->get_debug_string ());
len += strlen (m_args[i]->get_debug_string ());
if (i + 1 < m_args.length ())
{
strcpy (argbuf + len, ", ");
len += 2;
}
}
argbuf[len] = '\0';
/* ...and use it to get the string for the call as a whole. */
string *result = string::from_printf (m_ctxt,
"%s (%s)",
m_fn_ptr->get_debug_string (),
argbuf);
delete[] argbuf;
return result;
}
/* The implementation of class gcc::jit::recording::array_access. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::array_access. */
void
recording::array_access::replay_into (replayer *r)
{
set_playback_obj (
r->new_array_access (playback_location (r, m_loc),
m_ptr->playback_rvalue (),
m_index->playback_rvalue ()));
}
/* Implementation of recording::memento::make_debug_string for
array accesses. */
recording::string *
recording::array_access::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s[%s]",
m_ptr->get_debug_string (),
m_index->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::access_field_of_lvalue. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::access_field_of_lvalue. */
void
recording::access_field_of_lvalue::replay_into (replayer *r)
{
set_playback_obj (
m_lvalue->playback_lvalue ()
->access_field (playback_location (r, m_loc),
m_field->playback_field ()));
}
/* Implementation of recording::memento::make_debug_string for
accessing a field of an lvalue. */
recording::string *
recording::access_field_of_lvalue::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s.%s",
m_lvalue->get_debug_string (),
m_field->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::access_field_rvalue. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::access_field_rvalue. */
void
recording::access_field_rvalue::replay_into (replayer *r)
{
set_playback_obj (
m_rvalue->playback_rvalue ()
->access_field (playback_location (r, m_loc),
m_field->playback_field ()));
}
/* Implementation of recording::memento::make_debug_string for
accessing a field of an rvalue. */
recording::string *
recording::access_field_rvalue::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s.%s",
m_rvalue->get_debug_string (),
m_field->get_debug_string ());
}
/* The implementation of class
gcc::jit::recording::dereference_field_rvalue. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::dereference_field_rvalue. */
void
recording::dereference_field_rvalue::replay_into (replayer *r)
{
set_playback_obj (
m_rvalue->playback_rvalue ()->
dereference_field (playback_location (r, m_loc),
m_field->playback_field ()));
}
/* Implementation of recording::memento::make_debug_string for
dereferencing a field of an rvalue. */
recording::string *
recording::dereference_field_rvalue::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s->%s",
m_rvalue->get_debug_string (),
m_field->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::dereference_rvalue. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::dereference_rvalue. */
void
recording::dereference_rvalue::replay_into (replayer *r)
{
set_playback_obj (
m_rvalue->playback_rvalue ()->
dereference (playback_location (r, m_loc)));
}
/* Implementation of recording::memento::make_debug_string for
dereferencing an rvalue. */
recording::string *
recording::dereference_rvalue::make_debug_string ()
{
return string::from_printf (m_ctxt,
"*%s",
m_rvalue->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::get_address_of_lvalue. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::get_address_of_lvalue. */
void
recording::get_address_of_lvalue::replay_into (replayer *r)
{
set_playback_obj (
m_lvalue->playback_lvalue ()->
get_address (playback_location (r, m_loc)));
}
/* Implementation of recording::memento::make_debug_string for
getting the address of an lvalue. */
recording::string *
recording::get_address_of_lvalue::make_debug_string ()
{
return string::from_printf (m_ctxt,
"&%s",
m_lvalue->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::local. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::local. */
void
recording::local::replay_into (replayer *r)
{
set_playback_obj (
m_func->playback_function ()
->new_local (playback_location (r, m_loc),
m_type->playback_type (),
playback_string (m_name)));
}
/* Override the default implementation of
recording::memento::write_to_dump for locals by writing
TYPE NAME;
for use at the top of the function body as if it were a
declaration. */
void
recording::local::write_to_dump (dump &d)
{
if (d.update_locations ())
m_loc = d.make_location ();
d.write(" %s %s;\n",
m_type->get_debug_string (),
get_debug_string ());
}
/* The implementation of class gcc::jit::recording::statement. */
/* We poison the default implementation of
gcc::jit::recording::statement::get_successor_blocks
since this vfunc must only ever be called on terminator
statements. */
int
recording::statement::get_successor_blocks (block **/*out_next1*/,
block **/*out_next2*/) const
{
/* The base class implementation is for non-terminating statements,
and thus should never be called. */
gcc_unreachable ();
return 0;
}
/* Extend the default implementation of
recording::memento::write_to_dump for statements by (if requested)
updating the location of the statement to the current location in
the dumpfile. */
void
recording::statement::write_to_dump (dump &d)
{
memento::write_to_dump (d);
if (d.update_locations ())
m_loc = d.make_location ();
}
/* The implementation of class gcc::jit::recording::eval. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::eval. */
void
recording::eval::replay_into (replayer *r)
{
playback_block (get_block ())
->add_eval (playback_location (r),
m_rvalue->playback_rvalue ());
}
/* Implementation of recording::memento::make_debug_string for
an eval statement. */
recording::string *
recording::eval::make_debug_string ()
{
return string::from_printf (m_ctxt,
"(void)%s;",
m_rvalue->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::assignment. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::assignment. */
void
recording::assignment::replay_into (replayer *r)
{
playback_block (get_block ())
->add_assignment (playback_location (r),
m_lvalue->playback_lvalue (),
m_rvalue->playback_rvalue ());
}
/* Implementation of recording::memento::make_debug_string for
an assignment statement. */
recording::string *
recording::assignment::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s = %s;",
m_lvalue->get_debug_string (),
m_rvalue->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::assignment_op. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::assignment_op. */
void
recording::assignment_op::replay_into (replayer *r)
{
playback::type *result_type =
m_lvalue->playback_lvalue ()->get_type ();
playback::rvalue *binary_op =
r->new_binary_op (playback_location (r),
m_op,
result_type,
m_lvalue->playback_rvalue (),
m_rvalue->playback_rvalue ());
playback_block (get_block ())
->add_assignment (playback_location (r),
m_lvalue->playback_lvalue (),
binary_op);
}
/* Implementation of recording::memento::make_debug_string for
an assignment_op statement. */
recording::string *
recording::assignment_op::make_debug_string ()
{
return string::from_printf (m_ctxt,
"%s %s= %s;",
m_lvalue->get_debug_string (),
binary_op_strings[m_op],
m_rvalue->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::comment. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::comment. */
void
recording::comment::replay_into (replayer *r)
{
playback_block (get_block ())
->add_comment (playback_location (r),
m_text->c_str ());
}
/* Implementation of recording::memento::make_debug_string for
a comment "statement". */
recording::string *
recording::comment::make_debug_string ()
{
return string::from_printf (m_ctxt,
"/* %s */",
m_text->c_str ());
}
/* The implementation of class gcc::jit::recording::conditional. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::conditional. */
void
recording::conditional::replay_into (replayer *r)
{
playback_block (get_block ())
->add_conditional (playback_location (r),
m_boolval->playback_rvalue (),
playback_block (m_on_true),
playback_block (m_on_false));
}
/* Override the poisoned default implementation of
gcc::jit::recording::statement::get_successor_blocks
A conditional jump has 2 successor blocks. */
int
recording::conditional::get_successor_blocks (block **out_next1,
block **out_next2) const
{
*out_next1 = m_on_true;
*out_next2 = m_on_false;
return 2;
}
/* Implementation of recording::memento::make_debug_string for
a conditional jump statement. */
recording::string *
recording::conditional::make_debug_string ()
{
if (m_on_false)
return string::from_printf (m_ctxt,
"if (%s) goto %s; else goto %s;",
m_boolval->get_debug_string (),
m_on_true->get_debug_string (),
m_on_false->get_debug_string ());
else
return string::from_printf (m_ctxt,
"if (%s) goto %s;",
m_boolval->get_debug_string (),
m_on_true->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::jump. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::jump. */
void
recording::jump::replay_into (replayer *r)
{
playback_block (get_block ())
->add_jump (playback_location (r),
m_target->playback_block ());
}
/* Override the poisoned default implementation of
gcc::jit::recording::statement::get_successor_blocks
An unconditional jump has 1 successor block. */
int
recording::jump::get_successor_blocks (block **out_next1,
block **/*out_next2*/) const
{
*out_next1 = m_target;
return 1;
}
/* Implementation of recording::memento::make_debug_string for
a unconditional jump statement. */
recording::string *
recording::jump::make_debug_string ()
{
return string::from_printf (m_ctxt,
"goto %s;",
m_target->get_debug_string ());
}
/* The implementation of class gcc::jit::recording::return_. */
/* Implementation of pure virtual hook recording::memento::replay_into
for recording::return_. */
void
recording::return_::replay_into (replayer *r)
{
playback_block (get_block ())
->add_return (playback_location (r),
m_rvalue ? m_rvalue->playback_rvalue () : NULL);
}
/* Override the poisoned default implementation of
gcc::jit::recording::statement::get_successor_blocks
A return statement has no successor block. */
int
recording::return_::get_successor_blocks (block **/*out_next1*/,
block **/*out_next2*/) const
{
return 0;
}
/* Implementation of recording::memento::make_debug_string for
a return statement (covers both those with and without rvalues). */
recording::string *
recording::return_::make_debug_string ()
{
if (m_rvalue)
return string::from_printf (m_ctxt,
"return %s;",
m_rvalue->get_debug_string ());
else
return string::from_printf (m_ctxt,
"return;");
}
} // namespace gcc::jit
} // namespace gcc
|