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
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go.h"
static Type* sw1(Node*, Type*);
static Type* sw2(Node*, Type*);
static Type* sw3(Node*, Type*);
static Node* curfn;
enum
{
Inone,
I2T,
I2T2,
I2I,
I2I2,
T2I,
I2Isame,
};
// can this code branch reach the end
// without an undcontitional RETURN
// this is hard, so it is conservative
int
walkret(Node *n)
{
loop:
if(n != N)
switch(n->op) {
case OLIST:
if(n->right == N) {
n = n->left;
goto loop;
}
n = n->right;
goto loop;
// at this point, we have the last
// statement of the function
case OGOTO:
case OPANIC:
case ORETURN:
return 0;
}
// all other statements
// will flow to the end
return 1;
}
void
walk(Node *fn)
{
char s[50];
curfn = fn;
if(debug['W']) {
snprint(s, sizeof(s), "\nbefore %S", curfn->nname->sym);
dump(s, curfn->nbody);
}
if(curfn->type->outtuple)
if(walkret(curfn->nbody))
yyerror("function ends without a return statement");
if(addtop != N)
fatal("addtop in walk");
walkstate(curfn->nbody);
if(debug['W']) {
snprint(s, sizeof(s), "after %S", curfn->nname->sym);
dump(s, curfn->nbody);
}
}
void
addtotop(Node *n)
{
Node *l;
while(addtop != N) {
l = addtop;
addtop = N;
walktype(l, Etop);
n->ninit = list(n->ninit, l);
}
}
void
gettype(Node *n, Node *a)
{
if(debug['W'])
dump("\nbefore gettype", n);
walktype(n, Erv);
if(a == N && addtop != N)
fatal("gettype: addtop");
addtotop(a);
if(debug['W'])
dump("after gettype", n);
}
void
walkstate(Node *n)
{
Node *more;
loop:
if(n == N)
return;
more = N;
switch(n->op) {
case OLIST:
walkstate(n->left);
more = n->right;
break;
default:
yyerror("walkstate: %O not a top level statement", n->op);
case OASOP:
case OAS:
case OCALLMETH:
case OCALLINTER:
case OCALL:
case OSEND:
case ORECV:
case OPRINT:
case OPRINTN:
case OPANIC:
case OPANICN:
case OFOR:
case OIF:
case OSWITCH:
case OSELECT:
case OEMPTY:
case OBREAK:
case OCONTINUE:
case OGOTO:
case OLABEL:
case OFALL:
case OXCASE:
case OCASE:
case OXFALL:
case ORETURN:
case OPROC:
case ODEFER:
walktype(n, Etop);
break;
}
addtotop(n);
if(more != N) {
n = more;
goto loop;
}
}
void
indir(Node *nl, Node *nr)
{
if(nr != N)
*nl = *nr;
}
void
implicitstar(Node **nn)
{
Type *t;
Node *n;
// insert implicit * if needed
n = *nn;
t = n->type;
if(t == T || !isptr[t->etype])
return;
t = t->type;
if(t == T)
return;
switch(t->etype) {
case TMAP:
case TSTRING:
case TARRAY:
break;
default:
return;
}
n = nod(OIND, n, N);
walktype(n, Elv);
*nn = n;
}
void
walktype(Node *n, int top)
{
Node *r, *l;
Type *t;
Sym *s;
int et, cl, cr;
int32 lno;
if(n == N)
return;
lno = setlineno(n);
/*
* walk the whole tree of the body of a function.
* the types expressions are calculated.
* compile-time constants are evaluated.
*/
loop:
if(n == N)
goto ret;
setlineno(n);
if(debug['w'] > 1 && top == Etop && n->op != OLIST)
dump("walk-before", n);
t = T;
et = Txxx;
switch(n->op) {
default:
fatal("walktype: switch 1 unknown op %N", n);
goto ret;
case OLIST:
case OKEY:
walktype(n->left, top);
n = n->right;
goto loop;
case OPRINT:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, prcompat(n->left, 0));
goto ret;
case OPRINTN:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, prcompat(n->left, 1));
goto ret;
case OPANIC:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, list(prcompat(n->left, 0), nodpanic(n->lineno)));
goto ret;
case OPANICN:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
indir(n, list(prcompat(n->left, 1), nodpanic(n->lineno)));
goto ret;
case OLITERAL:
if(top != Erv)
goto nottop;
n->addable = 1;
goto ret;
case ONONAME:
s = n->sym;
if(s->undef == 0) {
s->undef = 1;
yyerror("%S: undefined", s);
goto ret;
}
if(top == Etop)
goto nottop;
goto ret;
case ONAME:
if(top == Etop)
goto nottop;
n->addable = 1;
if(n->type == T) {
s = n->sym;
if(s->undef == 0) {
yyerror("walktype: %S undeclared", s);
s->undef = 1;
}
}
goto ret;
case OFOR:
if(top != Etop)
goto nottop;
walkstate(n->ninit);
walkbool(n->ntest);
walkstate(n->nincr);
walkstate(n->nbody);
goto ret;
case OSWITCH:
if(top != Etop)
goto nottop;
casebody(n->nbody);
if(n->ntest == N)
n->ntest = booltrue;
walkstate(n->ninit);
walktype(n->ntest, Erv);
walkstate(n->nbody);
// find common type
if(n->ntest->type == T)
n->ntest->type = walkswitch(n, sw1);
// if that fails pick a type
if(n->ntest->type == T)
n->ntest->type = walkswitch(n, sw2);
// set the type on all literals
if(n->ntest->type != T)
walkswitch(n, sw3);
walktype(n->ntest, Erv); // BOTCH is this right
walktype(n->nincr, Erv);
goto ret;
case OSELECT:
if(top != Etop)
goto nottop;
walkselect(n);
goto ret;
case OIF:
if(top != Etop)
goto nottop;
walkstate(n->ninit);
walkbool(n->ntest);
walkstate(n->nbody);
walkstate(n->nelse);
goto ret;
case ODEFER:
hasdefer = 1;
case OPROC:
if(top != Etop)
goto nottop;
walktype(n->left, Etop);
goto ret;
case OCALLMETH:
case OCALLINTER:
case OCALL:
if(top == Elv)
goto nottop;
if(n->type != T)
goto ret;
walktype(n->left, Erv);
if(n->left == N)
goto ret;
t = n->left->type;
if(t == T)
goto ret;
if(n->left->op == ODOTMETH)
n->op = OCALLMETH;
if(n->left->op == ODOTINTER)
n->op = OCALLINTER;
if(isptr[t->etype])
t = t->type;
if(t->etype != TFUNC) {
yyerror("call of a non-function %T", t);
goto ret;
}
dowidth(t);
n->type = *getoutarg(t);
switch(t->outtuple) {
case 0:
if(top == Erv) {
yyerror("function requires a return type");
n->type = types[TINT];
}
break;
case 1:
n->type = n->type->type->type;
break;
}
walktype(n->right, Erv);
switch(n->op) {
default:
fatal("walk: op: %O", n->op);
case OCALLINTER:
l = ascompatte(n->op, getinarg(t), &n->right, 0);
n->right = reorder1(l);
break;
case OCALL:
l = ascompatte(n->op, getinarg(t), &n->right, 0);
n->right = reorder1(l);
if(isselect(n)) {
// clear output bool - special prob with selectsend
r = ascompatte(n->op, getoutarg(t), &boolfalse, 0);
n->right = list(n->right, r);
}
break;
case OCALLMETH:
l = ascompatte(n->op, getinarg(t), &n->right, 0);
r = ascompatte(n->op, getthis(t), &n->left->left, 0);
l = list(r, l);
n->left->left = N;
ullmancalc(n->left);
n->right = reorder1(l);
break;
}
goto ret;
case OAS:
if(top != Etop)
goto nottop;
addtop = list(addtop, n->ninit);
n->ninit = N;
l = n->left;
r = n->right;
walktype(l, Elv);
if(l == N || r == N)
goto ret;
cl = listcount(l);
cr = listcount(r);
if(cl == cr) {
walktype(r, Erv);
l = ascompatee(n->op, &n->left, &n->right);
if(l != N)
indir(n, reorder3(l));
goto ret;
}
switch(r->op) {
case OCALLMETH:
case OCALLINTER:
case OCALL:
if(cr == 1) {
// a,b,... = fn()
walktype(r, Erv);
l = ascompatet(n->op, &n->left, &r->type, 0);
if(l != N)
indir(n, list(r, reorder2(l)));
goto ret;
}
break;
case OINDEX:
if(cl == 2 && cr == 1) {
// a,b = map[] - mapaccess2
walktype(r->left, Erv);
if(!istype(r->left->type, TMAP))
break;
l = mapop(n, top);
if(l == N)
break;
indir(n, l);
goto ret;
}
break;
case ORECV:
if(cl == 2 && cr == 1) {
// a,b = <chan - chanrecv2
walktype(r->left, Erv);
if(!istype(r->left->type, TCHAN))
break;
l = chanop(n, top);
if(l == N)
break;
indir(n, l);
goto ret;
}
break;
case OCONV:
if(cl == 2 && cr == 1) {
// a,b = i.(T)
walktype(r->left, Erv);
if(r->left == N)
break;
et = ifaceas1(r->type, r->left->type);
switch(et) {
case I2T:
et = I2T2;
break;
case I2Isame:
case I2I:
et = I2I2;
break;
default:
et = Inone;
break;
}
if(et == Inone)
break;
r = ifaceop(r->type, r->left, et);
l = ascompatet(n->op, &n->left, &r->type, 0);
if(l != N)
indir(n, list(r, reorder2(l)));
goto ret;
}
break;
}
switch(l->op) {
case OINDEX:
if(cl == 1 && cr == 2) {
// map[] = a,b - mapassign2
if(!istype(l->left->type, TMAP))
break;
l = mapop(n, top);
if(l == N)
break;
indir(n, l);
goto ret;
}
break;
}
if(l->diag == 0) {
l->diag = 1;
yyerror("assignment count mismatch: %d = %d", cl, cr);
}
goto ret;
case OBREAK:
case OCONTINUE:
case OGOTO:
case OLABEL:
if(top != Etop)
goto nottop;
goto ret;
case OXCASE:
if(top != Etop)
goto nottop;
yyerror("case statement out of place");
n->op = OCASE;
case OCASE:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
walkstate(n->right);
goto ret;
case OXFALL:
if(top != Etop)
goto nottop;
yyerror("fallthrough statement out of place");
n->op = OFALL;
case OFALL:
case OINDREG:
case OEMPTY:
goto ret;
case OCONV:
if(top == Etop)
goto nottop;
l = n->left;
if(l == N)
goto ret;
walktype(l, Erv);
t = n->type;
if(t == T)
goto ret;
convlit1(l, t, 1);
// nil conversion
if(eqtype(t, l->type, 0)) {
if(l->op != ONAME) {
indir(n, l);
n->type = t;
}
goto ret;
}
// simple fix-float
if(l->type != T)
if(isint[l->type->etype] || isfloat[l->type->etype])
if(isint[t->etype] || isfloat[t->etype]) {
evconst(n);
goto ret;
}
// to string
if(l->type != T)
if(istype(t, TSTRING)) {
et = l->type->etype;
if(isint[et]) {
indir(n, stringop(n, top));
goto ret;
}
if(et == TARRAY)
if(istype(l->type->type, TUINT8)) {
n->op = OARRAY;
indir(n, stringop(n, top));
goto ret;
}
}
// convert dynamic to static generated by ONEW/OMAKE
if(isfixedarray(t) && isslice(l->type))
goto ret;
// convert static array to dynamic array
if(isslice(t) && isfixedarray(l->type)) {
if(eqtype(t->type->type, l->type->type->type, 0)) {
indir(n, arrayop(n, Erv));
goto ret;
}
}
// interface assignment
et = ifaceas(n->type, l->type);
if(et != Inone) {
indir(n, ifaceop(n->type, l, et));
goto ret;
}
// convert to unsafe.pointer
if(isptrto(n->type, TANY)) {
if(isptr[l->type->etype])
goto ret;
if(l->type->etype == TUINTPTR)
goto ret;
}
// convert from unsafe.pointer
if(isptrto(l->type, TANY)) {
if(isptr[n->type->etype])
goto ret;
if(n->type->etype == TUINTPTR)
goto ret;
}
if(l->type != T)
yyerror("cannot convert %T to %T", l->type, t);
goto ret;
case OCOMP:
if(top == Etop)
goto nottop;
l = n->left;
if(l == N)
goto ret;
walktype(l, Erv);
t = n->type;
if(t == T)
goto ret;
// structure literal
if(t->etype == TSTRUCT) {
indir(n, structlit(n));
goto ret;
}
// array literal
if(t->etype == TARRAY) {
r = arraylit(n);
indir(n, r);
goto ret;
}
// map literal
if(t->etype == TMAP) {
r = maplit(n);
indir(n, r);
goto ret;
}
yyerror("bad composite literal %T", t);
goto ret;
case ORETURN:
if(top != Etop)
goto nottop;
walktype(n->left, Erv);
if(curfn->type->outnamed && n->left == N) {
// print("special return\n");
goto ret;
}
l = ascompatte(n->op, getoutarg(curfn->type), &n->left, 1);
if(l != N)
n->left = reorder4(l);
goto ret;
case ONOT:
if(top != Erv)
goto nottop;
walktype(n->left, Erv);
if(n->left == N || n->left->type == T)
goto ret;
et = n->left->type->etype;
break;
case OASOP:
if(top != Etop)
goto nottop;
walktype(n->left, Elv);
l = n->left;
if(l->op != OINDEX) {
if(n->etype == OLSH || n->etype == ORSH)
goto shft;
goto com;
}
if(!istype(l->left->type, TMAP))
goto com;
indir(n, mapop(n, top));
goto ret;
case OLSH:
case ORSH:
if(top != Erv)
goto nottop;
walktype(n->left, Erv);
shft:
walktype(n->right, Erv);
if(n->left == N || n->right == N)
goto ret;
evconst(n);
if(n->op == OLITERAL)
goto ret;
if(n->left->type == T)
convlit(n->left, types[TINT]);
if(n->right->type == T)
convlit(n->right, types[TUINT]);
if(n->left->type == T || n->right->type == T)
goto ret;
if(issigned[n->right->type->etype])
goto badt;
break;
case OMOD:
case OAND:
case OOR:
case OXOR:
case OANDAND:
case OOROR:
case OEQ:
case ONE:
case OLT:
case OLE:
case OGE:
case OGT:
case OADD:
case OSUB:
case OMUL:
case ODIV:
if(top != Erv)
goto nottop;
walktype(n->left, Erv);
com:
walktype(n->right, Erv);
if(n->left == N || n->right == N)
goto ret;
evconst(n);
if(n->op == OLITERAL)
goto ret;
convlit(n->left, n->right->type);
convlit(n->right, n->left->type);
if(n->left->type == T || n->right->type == T)
goto ret;
if(!eqtype(n->left->type, n->right->type, 0))
goto badt;
switch(n->op) {
case OEQ:
case ONE:
case OLT:
case OLE:
case OGE:
case OGT:
case OADD:
case OASOP:
if(istype(n->left->type, TSTRING)) {
indir(n, stringop(n, top));
goto ret;
}
}
break;
case OMINUS:
case OPLUS:
case OCOM:
if(top != Erv)
goto nottop;
walktype(n->left, Erv);
if(n->left == N)
goto ret;
evconst(n);
if(n->op == OLITERAL)
goto ret;
break;
case OLEN:
if(top != Erv)
goto nottop;
walktype(n->left, Erv);
implicitstar(&n->left);
evconst(n);
t = n->left->type;
if(t == T)
goto ret;
switch(t->etype) {
default:
goto badt;
case TSTRING:
case TMAP:
break;
case TARRAY:
if(t->bound >= 0)
nodconst(n, types[TINT], t->bound);
break;
}
n->type = types[TINT];
goto ret;
case OCAP:
if(top != Erv)
goto nottop;
walktype(n->left, Erv);
implicitstar(&n->left);
evconst(n);
t = n->left->type;
if(t == T)
goto ret;
switch(t->etype) {
default:
goto badt;
case TARRAY:
if(t->bound >= 0)
nodconst(n, types[TINT], t->bound);
break;
}
n->type = types[TINT];
goto ret;
case OINDEX:
if(top == Etop)
goto nottop;
walktype(n->left, Erv);
walktype(n->right, Erv);
if(n->left == N || n->right == N)
goto ret;
defaultlit(n->left);
implicitstar(&n->left);
t = n->left->type;
if(t == T)
goto ret;
switch(t->etype) {
default:
goto badt;
case TSTRING:
// right side must be an int
if(top != Erv)
goto nottop;
if(n->right->type == T) {
convlit(n->right, types[TINT]);
if(n->right->type == T)
break;
}
if(!isint[n->right->type->etype])
goto badt;
indir(n, stringop(n, top));
break;
case TMAP:
// right side must be map type
if(n->right->type == T) {
convlit(n->right, t->down);
if(n->right->type == T)
break;
}
if(!eqtype(n->right->type, t->down, 0))
goto badt;
n->type = t->type;
if(top == Erv)
indir(n, mapop(n, top));
break;
case TARRAY:
// right side must be an int
if(n->right->type == T) {
convlit(n->right, types[TINT]);
if(n->right->type == T)
break;
}
if(!isint[n->right->type->etype])
goto badt;
n->type = t->type;
break;
}
goto ret;
case OSEND:
if(top == Elv)
goto nottop;
walktype(n->left, Erv); // chan
walktype(n->right, Erv); // e
indir(n, chanop(n, top));
goto ret;
case ORECV:
if(top == Elv)
goto nottop;
if(n->right == N) {
walktype(n->left, Erv); // chan
indir(n, chanop(n, top)); // returns e blocking
goto ret;
}
walktype(n->left, Elv); // e
walktype(n->right, Erv); // chan
indir(n, chanop(n, top)); // returns bool non-blocking
goto ret;
case OSLICE:
if(top == Etop)
goto nottop;
walktype(n->left, top);
walktype(n->right, Erv);
if(n->left == N || n->right == N)
goto ret;
convlit(n->left, types[TSTRING]);
implicitstar(&n->left);
t = n->left->type;
if(t == T)
goto ret;
if(t->etype == TSTRING) {
indir(n, stringop(n, top));
goto ret;
}
if(t->etype == TARRAY) {
indir(n, arrayop(n, top));
goto ret;
}
badtype(OSLICE, n->left->type, T);
goto ret;
case ODOT:
case ODOTPTR:
case ODOTMETH:
case ODOTINTER:
if(top == Etop)
goto nottop;
walkdot(n);
goto ret;
case OADDR:
if(top != Erv)
goto nottop;
if(n->left->op == OCOMP && n->left->type != T)
if(n->left->type->etype == TSTRUCT) {
// turn &Point{1, 2} into allocation.
// initialize with
// nvar := new(*Point);
// *nvar = Point{1, 2};
// and replace expression with nvar
// TODO(rsc): might do a better job (fewer copies) later
Node *nnew, *nvar, *nas;
t = ptrto(n->left->type);
walktype(n->left, Elv);
if(n->left == N)
goto ret;
nvar = nod(0, N, N);
tempname(nvar, t);
nnew = nod(ONEW, N, N);
nnew->type = n->left->type;
nnew = newcompat(nnew);
nas = nod(OAS, nvar, nnew);
addtop = list(addtop, nas);
nas = nod(OAS, nod(OIND, nvar, N), n->left);
addtop = list(addtop, nas);
indir(n, nvar);
goto ret;
}
walktype(n->left, Elv);
if(n->left == N)
goto ret;
t = n->left->type;
if(t == T)
goto ret;
n->type = ptrto(t);
goto ret;
case OIND:
if(top == Etop)
goto nottop;
if(top == Elv) // even if n is lvalue, n->left is rvalue
top = Erv;
walktype(n->left, top);
if(n->left == N)
goto ret;
t = n->left->type;
if(t == T)
goto ret;
if(!isptr[t->etype])
goto badt;
n->type = t->type;
goto ret;
case OMAKE:
if(top != Erv)
goto nottop;
indir(n, makecompat(n));
goto ret;
case ONEW:
if(top != Erv)
goto nottop;
indir(n, newcompat(n));
goto ret;
}
/*
* ======== second switch ========
*/
switch(n->op) {
default:
fatal("walktype: switch 2 unknown op %N", n);
goto ret;
case OASOP:
break;
case ONOT:
case OANDAND:
case OOROR:
if(n->left->type == T)
goto ret;
et = n->left->type->etype;
if(et != TBOOL)
goto badt;
t = types[TBOOL];
break;
case OEQ:
case ONE:
if(n->left->type == T)
goto ret;
if(isslice(n->left->type)) {
t = types[TBOOL];
break;
}
et = n->left->type->etype;
if(!okforeq[et])
goto badt;
if(isinter(n->left->type)) {
indir(n, ifaceop(T, n, n->op));
goto ret;
}
t = types[TBOOL];
break;
case OLT:
case OLE:
case OGE:
case OGT:
if(n->left->type == T)
goto ret;
et = n->left->type->etype;
if(!okforadd[et] && et != TSTRING)
goto badt;
t = types[TBOOL];
break;
case OADD:
case OSUB:
case OMUL:
case ODIV:
case OPLUS:
if(n->left->type == T)
goto ret;
et = n->left->type->etype;
if(!okforadd[et])
goto badt;
break;
case OMINUS:
if(n->left->type == T)
goto ret;
et = n->left->type->etype;
if(!okforadd[et])
goto badt;
if(!isfloat[et])
break;
l = nod(OLITERAL, N, N);
l->val.u.fval = mal(sizeof(*l->val.u.fval));
l->val.ctype = CTFLT;
mpmovecflt(l->val.u.fval, 0.0);
l = nod(OSUB, l, n->left);
indir(n, l);
walktype(n, Erv);
goto ret;
case OLSH:
case ORSH:
case OAND:
case OOR:
case OXOR:
case OMOD:
case OCOM:
if(n->left->type == T)
goto ret;
et = n->left->type->etype;
if(!okforand[et])
goto badt;
break;
}
if(t == T)
t = n->left->type;
n->type = t;
goto ret;
nottop:
switch(top) {
default:
yyerror("didn't expect %O here", n->op);
break;
case Etop:
yyerror("operation %O not allowed in statement context", n->op);
break;
case Elv:
yyerror("operation %O not allowed in assignment context", n->op);
break;
case Erv:
yyerror("operation %O not allowed in expression context", n->op);
break;
}
goto ret;
badt:
if(n->right == N) {
if(n->left == N) {
badtype(n->op, T, T);
goto ret;
}
badtype(n->op, n->left->type, T);
goto ret;
}
badtype(n->op, n->left->type, n->right->type);
goto ret;
ret:
if(debug['w'] && top == Etop && n != N)
dump("walk", n);
ullmancalc(n);
lineno = lno;
}
void
walkbool(Node *n)
{
walktype(n, Erv);
addtotop(n);
if(n != N && n->type != T)
if(!eqtype(n->type, types[TBOOL], 0))
yyerror("IF and FOR require a boolean type");
}
/*
* return the first type
*/
Type*
sw1(Node *c, Type *place)
{
if(place == T)
return c->type;
return place;
}
/*
* return a suitable type
*/
Type*
sw2(Node *c, Type *place)
{
return types[TINT]; // botch
}
/*
* check that switch type
* is compat with all the cases
*/
Type*
sw3(Node *c, Type *place)
{
if(place == T)
return c->type;
if(c->type == T)
c->type = place;
convlit(c, place);
if(!ascompat(place, c->type))
badtype(OSWITCH, place, c->type);
return place;
}
Type*
walkswitch(Node *sw, Type*(*call)(Node*, Type*))
{
Node *n, *c;
Type *place;
place = call(sw->ntest, T);
setlineno(sw);
n = sw->nbody;
if(n->op == OLIST)
n = n->left;
if(n->op == OEMPTY)
return T;
for(; n!=N; n=n->right) {
if(n->op != OCASE)
fatal("walkswitch: not case %O\n", n->op);
for(c=n->left; c!=N; c=c->right) {
if(c->op != OLIST) {
setlineno(c);
place = call(c, place);
break;
}
setlineno(c);
place = call(c->left, place);
}
}
return place;
}
int
casebody(Node *n)
{
Node *oc, *ot, *t;
Iter save;
/*
* look to see if statements at top level have
* case labels attached to them. convert the illegal
* ops XFALL and XCASE into legal ops FALL and CASE.
* all unconverted ops will thus be caught as illegal
*/
oc = N; // last case statement
ot = N; // last statement (look for XFALL)
t = listfirst(&save, &n);
loop:
if(t == N) {
/* empty switch */
if(oc == N)
return 0;
return 1;
}
if(t->op == OXCASE) {
/* rewrite and link top level cases */
t->op = OCASE;
if(oc != N)
oc->right = t;
oc = t;
/* rewrite top fall that preceed case */
if(ot != N && ot->op == OXFALL)
ot->op = OFALL;
}
/* if first statement is not case */
if(oc == N)
return 0;
ot = t;
t = listnext(&save);
goto loop;
}
Node*
selcase(Node *n, Node *var)
{
Node *a, *r, *on, *c;
Type *t;
if(n->left == N)
goto dflt;
c = n->left;
if(c->op == ORECV)
goto recv;
walktype(c->left, Erv); // chan
walktype(c->right, Erv); // elem
t = fixchan(c->left->type);
if(t == T)
return N;
convlit(c->right, t->type);
if(!ascompat(t->type, c->right->type)) {
badtype(c->op, t->type, c->right->type);
return N;
}
// selectsend(sel *byte, hchan *chan any, elem any) (selected bool);
on = syslook("selectsend", 1);
argtype(on, t->type);
argtype(on, t->type);
a = c->right; // elem
r = a;
a = c->left; // chan
r = list(a, r);
a = var; // sel-var
r = list(a, r);
goto out;
recv:
if(c->right != N)
goto recv2;
walktype(c->left, Erv); // chan
t = fixchan(c->left->type);
if(t == T)
return N;
// selectrecv(sel *byte, hchan *chan any, elem *any) (selected bool);
on = syslook("selectrecv", 1);
argtype(on, t->type);
argtype(on, t->type);
a = c->left; // nil elem
a = nod(OLITERAL, N, N);
a->val.ctype = CTNIL;
r = a;
a = c->left; // chan
r = list(a, r);
a = var; // sel-var
r = list(a, r);
goto out;
recv2:
walktype(c->right, Erv); // chan
t = fixchan(c->right->type);
if(t == T)
return N;
walktype(c->left, Elv); // elem
convlit(c->left, t->type);
if(!ascompat(t->type, c->left->type)) {
badtype(c->op, t->type, c->left->type);
return N;
}
// selectrecv(sel *byte, hchan *chan any, elem *any) (selected bool);
on = syslook("selectrecv", 1);
argtype(on, t->type);
argtype(on, t->type);
a = c->left; // elem
a = nod(OADDR, a, N);
r = a;
a = c->right; // chan
r = list(a, r);
a = var; // sel-var
r = list(a, r);
goto out;
dflt:
// selectdefault(sel *byte);
on = syslook("selectdefault", 0);
a = var;
r = a; // sel-var
goto out;
out:
a = nod(OCALL, on, r);
r = nod(OIF, N, N);
r->ntest = a;
return r;
}
Node*
selectas(Node *name, Node *expr)
{
Node *a;
Type *t;
if(expr == N || expr->op != ORECV)
goto bad;
t = expr->left->type;
if(t == T)
goto bad;
if(t->etype != TCHAN)
goto bad;
a = old2new(name, t->type);
return a;
bad:
return name;
}
void
walkselect(Node *sel)
{
Iter iter;
Node *n, *oc, *on, *r;
Node *var, *bod, *res, *def;
int count, op;
int32 lno;
lno = setlineno(sel);
// generate sel-struct
var = nod(OXXX, N, N);
tempname(var, ptrto(types[TUINT8]));
n = listfirst(&iter, &sel->left);
if(n == N || n->op != OXCASE)
yyerror("first select statement must be a case");
count = 0; // number of cases
res = N; // entire select body
bod = N; // body of each case
oc = N; // last case
def = N; // default case
for(count=0; n!=N; n=listnext(&iter)) {
setlineno(n);
switch(n->op) {
default:
bod = list(bod, n);
break;
case OXCASE:
if(n->left == N) {
op = ORECV; // actual value not used
if(def != N)
yyerror("only one default select allowed");
def = n;
} else
op = n->left->op;
switch(op) {
default:
yyerror("select cases must be send, recv or default");
break;
case OAS:
// convert new syntax (a=recv(chan)) to (recv(a,chan))
if(n->left->right == N || n->left->right->op != ORECV) {
yyerror("select cases must be send, recv or default");
break;
}
n->left->right->right = n->left->right->left;
n->left->right->left = n->left->left;
n->left = n->left->right;
case OSEND:
case ORECV:
if(oc != N) {
bod = list(bod, nod(OBREAK, N, N));
oc->nbody = rev(bod);
}
oc = selcase(n, var);
res = list(res, oc);
break;
}
bod = N;
count++;
break;
}
}
if(oc != N) {
bod = list(bod, nod(OBREAK, N, N));
oc->nbody = rev(bod);
}
setlineno(sel);
// selectgo(sel *byte);
on = syslook("selectgo", 0);
r = nod(OCALL, on, var); // sel-var
res = list(res, r);
// newselect(size uint32) (sel *byte);
on = syslook("newselect", 0);
r = nod(OXXX, N, N);
nodconst(r, types[TINT], count); // count
r = nod(OCALL, on, r);
r = nod(OAS, var, r);
sel->ninit = r;
sel->nbody = rev(res);
sel->left = N;
walkstate(sel->ninit);
walkstate(sel->nbody);
//dump("sel", sel);
lineno = lno;
}
Type*
lookdot1(Sym *s, Type *t, Type *f)
{
Type *r;
r = T;
for(; f!=T; f=f->down) {
if(f->sym == S)
continue;
if(f->sym != s)
continue;
if(r != T) {
yyerror("ambiguous DOT reference %T.%S", t, s);
break;
}
r = f;
}
return r;
}
int
lookdot(Node *n, Type *t)
{
Type *f1, *f2, *tt;
int op;
Sym *s;
s = n->right->sym;
f1 = T;
if(t->etype == TSTRUCT || t->etype == TINTER)
f1 = lookdot1(s, t, t->type);
f2 = methtype(n->left->type);
if(f2 != T)
f2 = lookdot1(s, f2, f2->method);
if(f1 != T) {
if(f2 != T)
yyerror("ambiguous DOT reference %S as both field and method",
n->right->sym);
n->right = f1->nname; // substitute real name
n->xoffset = f1->width;
n->type = f1->type;
if(t->etype == TINTER)
n->op = ODOTINTER;
return 1;
}
if(f2 != T) {
tt = n->left->type;
if((op = methconv(tt)) != 0) {
switch(op) {
case OADDR:
walktype(n->left, Elv);
n->left = nod(OADDR, n->left, N);
n->left->type = ptrto(tt);
break;
case OIND:
n->left = nod(OIND, n->left, N);
n->left->type = tt->type;
break;
}
}
n->right = methodname(n->right, n->left->type);
n->xoffset = f2->width;
n->type = f2->type;
n->op = ODOTMETH;
return 1;
}
return 0;
}
void
walkdot(Node *n)
{
Type *t;
addtop = list(addtop, n->ninit);
n->ninit = N;
if(n->left == N || n->right == N)
return;
switch(n->op) {
case ODOTINTER:
case ODOTMETH:
return; // already done
}
walktype(n->left, Erv);
if(n->right->op != ONAME) {
yyerror("rhs of . must be a name");
return;
}
t = n->left->type;
if(t == T)
return;
// as a structure field or pointer to structure field
if(isptr[t->etype]) {
t = t->type;
if(t == T)
return;
n->op = ODOTPTR;
}
if(!lookdot(n, t))
yyerror("undefined DOT %S on %T", n->right->sym, n->left->type);
}
Node*
ascompatee(int op, Node **nl, Node **nr)
{
Node *l, *r, *nn, *a;
Iter savel, saver;
/*
* check assign expression list to
* a expression list. called in
* expr-list = expr-list
*/
l = listfirst(&savel, nl);
r = listfirst(&saver, nr);
nn = N;
loop:
if(l == N || r == N) {
if(l != r)
yyerror("error in shape across %O", op);
return rev(nn);
}
convlit(r, l->type);
if(!ascompat(l->type, r->type)) {
badtype(op, l->type, r->type);
return N;
}
a = nod(OAS, l, r);
a = convas(a);
nn = list(a, nn);
l = listnext(&savel);
r = listnext(&saver);
goto loop;
}
Node*
ascompatet(int op, Node **nl, Type **nr, int fp)
{
Node *l, *nn, *a;
Type *r;
Iter savel, saver;
/*
* check assign type list to
* a expression list. called in
* expr-list = func()
*/
l = listfirst(&savel, nl);
r = structfirst(&saver, nr);
nn = N;
loop:
if(l == N || r == T) {
if(l != N || r != T)
yyerror("error in shape across %O", op);
return rev(nn);
}
if(!ascompat(l->type, r->type)) {
badtype(op, l->type, r->type);
return N;
}
a = nod(OAS, l, nodarg(r, fp));
a = convas(a);
nn = list(a, nn);
l = listnext(&savel);
r = structnext(&saver);
goto loop;
}
/*
* make a tsig for the structure
* carrying the ... arguments
*/
Type*
sigtype(Type *st)
{
Dcl *x;
Sym *s;
Type *t;
static int sigdddgen;
dowidth(st);
sigdddgen++;
snprint(namebuf, sizeof(namebuf), "dsigddd_%d", sigdddgen);
s = lookup(namebuf);
t = newtype(s);
t = dodcltype(t);
updatetype(t, st);
t->local = 1;
// record internal type for signature generation
x = mal(sizeof(*x));
x->op = OTYPE;
x->dsym = s;
x->dtype = s->otype;
x->forw = signatlist;
x->block = block;
signatlist = x;
return s->otype;
}
/*
* package all the arguments that
* match a ... parameter into an
* automatic structure.
* then call the ... arg (interface)
* with a pointer to the structure
*/
Node*
mkdotargs(Node *r, Node *rr, Iter *saver, Node *nn, Type *l, int fp)
{
Type *t, *st, *ft;
Node *a, *n, *var;
Iter saven;
n = N; // list of assignments
st = typ(TSTRUCT); // generated structure
ft = T; // last field
while(r != N) {
defaultlit(r);
// generate the next structure field
t = typ(TFIELD);
t->type = r->type;
if(ft == T)
st->type = t;
else
ft->down = t;
ft = t;
a = nod(OAS, N, r);
n = list(n, a);
if(rr != N) {
r = rr;
rr = N;
} else
r = listnext(saver);
}
// make a named type for the struct
st = sigtype(st);
// now we have the size, make the struct
var = nod(OXXX, N, N);
tempname(var, st);
// assign the fields to the struct.
// use addtop so that reorder1 doesn't reorder
// these assignments after the interface conversion
// below.
n = rev(n);
r = listfirst(&saven, &n);
t = st->type;
while(r != N) {
r->left = nod(OXXX, N, N);
*r->left = *var;
r->left->type = r->right->type;
r->left->xoffset += t->width;
addtop = list(addtop, r);
r = listnext(&saven);
t = t->down;
}
// last thing is to put assignment
// of a pointer to the structure to
// the DDD parameter
a = nod(OAS, nodarg(l, fp), var);
nn = list(convas(a), nn);
return nn;
}
/*
* check assign expression list to
* a type list. called in
* return expr-list
* func(expr-list)
*/
Node*
ascompatte(int op, Type **nl, Node **nr, int fp)
{
Type *l, *ll;
Node *r, *rr, *nn, *a;
Iter savel, saver, peekl, peekr;
l = structfirst(&savel, nl);
r = listfirst(&saver, nr);
nn = N;
// 1 to many
peekl = savel;
peekr = saver;
if(l != T && r != N
&& structnext(&peekl) != T
&& listnext(&peekr) == N
&& eqtype(r->type, *nl, 0))
return convas(nod(OAS, nodarg(*nl, fp), r));
loop:
if(l != T && isddd(l->type)) {
// the ddd parameter must be last
ll = structnext(&savel);
if(ll != T)
yyerror("... must be last argument");
// special case --
// only if we are assigning a single ddd
// argument to a ddd parameter then it is
// passed thru unencapsulated
rr = listnext(&saver);
if(r != N && rr == N && isddd(r->type)) {
a = nod(OAS, nodarg(l, fp), r);
a = convas(a);
nn = list(a, nn);
return rev(nn);
}
// normal case -- make a structure of all
// remaining arguments and pass a pointer to
// it to the ddd parameter (empty interface)
nn = mkdotargs(r, rr, &saver, nn, l, fp);
return rev(nn);
}
if(l == T || r == N) {
if(l != T || r != N)
yyerror("error in shape across %O", op);
return rev(nn);
}
convlit(r, l->type);
if(!ascompat(l->type, r->type)) {
badtype(op, l->type, r->type);
return N;
}
a = nod(OAS, nodarg(l, fp), r);
a = convas(a);
nn = list(a, nn);
l = structnext(&savel);
r = listnext(&saver);
goto loop;
}
/*
* can we assign var of type src to var of type dst
*/
int
ascompat(Type *dst, Type *src)
{
if(eqtype(dst, src, 0))
return 1;
if(isslice(dst) && isfixedarray(src))
return 1;
if(isnilinter(dst) || isnilinter(src))
return 1;
if(isinter(dst) && isinter(src))
return 1;
if(isinter(dst) && methtype(src))
return 1;
if(isinter(src) && methtype(dst))
return 1;
return 0;
}
Node*
prcompat(Node *n, int fmt)
{
Node *l, *r;
Node *on;
Type *t;
Iter save;
int w, notfirst;
r = N;
l = listfirst(&save, &n);
notfirst = 0;
loop:
if(l == N) {
if(fmt) {
on = syslook("printnl", 0);
r = list(r, nod(OCALL, on, N));
}
walktype(r, Etop);
return r;
}
if(notfirst) {
on = syslook("printsp", 0);
r = list(r, nod(OCALL, on, N));
}
w = whatis(l);
switch(w) {
default:
if(l->type == T)
goto out;
if(isinter(l->type)) {
on = syslook("printinter", 1);
argtype(on, l->type); // any-1
break;
}
if(isptr[l->type->etype] || l->type->etype == TCHAN || l->type->etype == TMAP) {
on = syslook("printpointer", 1);
argtype(on, l->type); // any-1
break;
}
if(isslice(l->type)) {
on = syslook("printarray", 1);
argtype(on, l->type); // any-1
break;
}
badtype(OPRINT, l->type, T);
l = listnext(&save);
goto loop;
case Wlitint:
case Wtint:
on = syslook("printint", 0);
break;
case Wlitfloat:
case Wtfloat:
on = syslook("printfloat", 0);
break;
case Wlitbool:
case Wtbool:
on = syslook("printbool", 0);
break;
case Wlitstr:
case Wtstr:
on = syslook("printstring", 0);
break;
}
t = *getinarg(on->type);
if(t != nil)
t = t->type;
if(t != nil)
t = t->type;
if(!eqtype(t, l->type, 0)) {
l = nod(OCONV, l, N);
l->type = t;
}
r = list(r, nod(OCALL, on, l));
out:
notfirst = fmt;
l = listnext(&save);
goto loop;
}
Node*
nodpanic(int32 lineno)
{
Node *n, *on;
on = syslook("panicl", 0);
n = nodintconst(lineno);
n = nod(OCALL, on, n);
walktype(n, Etop);
return n;
}
Node*
makecompat(Node *n)
{
Type *t;
t = n->type;
if(t != T)
switch(t->etype) {
case TARRAY:
return arrayop(n, Erv);
case TMAP:
return mapop(n, Erv);
case TCHAN:
return chanop(n, Erv);
}
/*
* ken had code to malloc here,
* but rsc cut it out so that make(int)
* is diagnosed as an error (probably meant new).
* might come back once we know the
* language semantics for make(int).
*/
yyerror("cannot make(%T)", t);
return n;
}
Node*
newcompat(Node *n)
{
Node *r, *on;
Type *t;
t = n->type;
if(t != T && t->etype != TFUNC) {
if(n->left != N)
yyerror("cannot new(%T, expr)", t);
dowidth(t);
on = syslook("mal", 1);
argtype(on, t);
r = nodintconst(t->width);
r = nod(OCALL, on, r);
walktype(r, Erv);
return r;
}
yyerror("cannot new(%T)", t);
return n;
}
Node*
stringop(Node *n, int top)
{
Node *r, *c, *on;
switch(n->op) {
default:
fatal("stringop: unknown op %O", n->op);
case OEQ:
case ONE:
case OGE:
case OGT:
case OLE:
case OLT:
// sys_cmpstring(s1, s2) :: 0
on = syslook("cmpstring", 0);
r = list(n->left, n->right);
r = nod(OCALL, on, r);
c = nodintconst(0);
r = nod(n->op, r, c);
break;
case OADD:
// sys_catstring(s1, s2)
on = syslook("catstring", 0);
r = list(n->left, n->right);
r = nod(OCALL, on, r);
break;
case OASOP:
// sys_catstring(s1, s2)
switch(n->etype) {
default:
fatal("stringop: unknown op %O-%O", n->op, n->etype);
case OADD:
// s1 = sys_catstring(s1, s2)
if(n->etype != OADD)
fatal("stringop: not cat");
r = list(n->left, n->right);
on = syslook("catstring", 0);
r = nod(OCALL, on, r);
r = nod(OAS, n->left, r);
break;
}
break;
case OSLICE:
// sys_slicestring(s, lb, hb)
r = nod(OCONV, n->right->left, N);
r->type = types[TINT];
c = nod(OCONV, n->right->right, N);
c->type = types[TINT];
r = list(r, c);
r = list(n->left, r);
on = syslook("slicestring", 0);
r = nod(OCALL, on, r);
break;
case OINDEX:
// sys_indexstring(s, i)
r = nod(OCONV, n->right, N);
r->type = types[TINT];
r = list(n->left, r);
on = syslook("indexstring", 0);
r = nod(OCALL, on, r);
break;
case OCONV:
// sys_intstring(v)
r = nod(OCONV, n->left, N);
r->type = types[TINT64];
on = syslook("intstring", 0);
r = nod(OCALL, on, r);
break;
case OARRAY:
// arraystring([]byte) string;
r = n->left;
on = syslook("arraystring", 0);
r = nod(OCALL, on, r);
break;
}
walktype(r, top);
return r;
}
Type*
fixmap(Type *t)
{
if(t == T)
goto bad;
if(t->etype != TMAP)
goto bad;
if(t->down == T || t->type == T)
goto bad;
dowidth(t->down);
dowidth(t->type);
return t;
bad:
yyerror("not a map: %lT", t);
return T;
}
Type*
fixchan(Type *t)
{
if(t == T)
goto bad;
if(t->etype != TCHAN)
goto bad;
if(t->type == T)
goto bad;
dowidth(t->type);
return t;
bad:
yyerror("not a channel: %lT", t);
return T;
}
Node*
mapop(Node *n, int top)
{
Node *r, *a;
Type *t;
Node *on;
int cl, cr;
r = n;
switch(n->op) {
default:
fatal("mapop: unknown op %O", n->op);
case OMAKE:
if(top != Erv)
goto nottop;
// newmap(keysize int, valsize int,
// keyalg int, valalg int,
// hint int) (hmap map[any-1]any-2);
t = fixmap(n->type);
if(t == T)
break;
a = n->left; // hint
if(n->left == N)
a = nodintconst(0);
r = a;
a = nodintconst(algtype(t->type)); // val algorithm
r = list(a, r);
a = nodintconst(algtype(t->down)); // key algorithm
r = list(a, r);
a = nodintconst(t->type->width); // val width
r = list(a, r);
a = nodintconst(t->down->width); // key width
r = list(a, r);
on = syslook("newmap", 1);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
r = nod(OCALL, on, r);
walktype(r, top);
r->type = n->type;
break;
case OINDEX:
if(top != Erv)
goto nottop;
// mapaccess1(hmap map[any]any, key any) (val any);
t = fixmap(n->left->type);
if(t == T)
break;
convlit(n->right, t->down);
if(!eqtype(n->right->type, t->down, 0)) {
badtype(n->op, n->right->type, t->down);
break;
}
a = n->right; // key
r = a;
a = n->left; // map
r = list(a, r);
on = syslook("mapaccess1", 1);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
argtype(on, t->down); // any-3
argtype(on, t->type); // any-4
r = nod(OCALL, on, r);
walktype(r, Erv);
r->type = t->type;
break;
case OAS:
cl = listcount(n->left);
cr = listcount(n->right);
if(cl == 1 && cr == 2)
goto assign2;
if(cl == 2 && cr == 1)
goto access2;
if(cl != 1 || cr != 1)
goto shape;
// mapassign1(hmap map[any-1]any-2, key any-3, val any-4);
if(n->left->op != OINDEX)
goto shape;
t = fixmap(n->left->left->type);
if(t == T)
break;
a = n->right; // val
r = a;
a = n->left->right; // key
r = list(a, r);
a = n->left->left; // map
r = list(a, r);
on = syslook("mapassign1", 1);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
argtype(on, t->down); // any-3
argtype(on, t->type); // any-4
r = nod(OCALL, on, r);
walktype(r, Etop);
break;
assign2:
// mapassign2(hmap map[any]any, key any, val any, pres bool);
if(n->left->op != OINDEX)
goto shape;
t = fixmap(n->left->left->type);
if(t == T)
break;
a = n->right->right; // pres
r = a;
a = n->right->left; // val
r =list(a, r);
a = n->left->right; // key
r = list(a, r);
a = n->left->left; // map
r = list(a, r);
on = syslook("mapassign2", 1);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
argtype(on, t->down); // any-3
argtype(on, t->type); // any-4
r = nod(OCALL, on, r);
walktype(r, Etop);
break;
access2:
// mapaccess2(hmap map[any-1]any-2, key any-3) (val-4 any, pres bool);
//dump("access2", n);
if(n->right->op != OINDEX)
goto shape;
t = fixmap(n->right->left->type);
if(t == T)
break;
a = n->right->right; // key
r = a;
a = n->right->left; // map
r = list(a, r);
on = syslook("mapaccess2", 1);
argtype(on, t->down); // any-1
argtype(on, t->type); // any-2
argtype(on, t->down); // any-3
argtype(on, t->type); // any-4
n->right = nod(OCALL, on, r);
walktype(n, Etop);
r = n;
break;
case OASOP:
// rewrite map[index] op= right
// into tmpi := index; map[tmpi] = map[tmpi] op right
t = n->left->left->type;
a = nod(OXXX, N, N);
tempname(a, t->down); // tmpi
r = nod(OAS, a, n->left->right); // tmpi := index
n->left->right = a; // m[tmpi]
a = nod(OXXX, N, N);
indir(a, n->left); // copy of map[tmpi]
a = nod(n->etype, a, n->right); // m[tmpi] op right
a = nod(OAS, n->left, a); // map[tmpi] = map[tmpi] op right
r = nod(OLIST, r, a);
walktype(r, Etop);
break;
}
return r;
shape:
dump("shape", n);
fatal("mapop: cl=%d cr=%d, %O", top, n->op);
return N;
nottop:
yyerror("didn't expect %O here", n->op);
return N;
}
Node*
chanop(Node *n, int top)
{
Node *r, *a;
Type *t;
Node *on;
int cl, cr;
//dump("chanop", n);
r = n;
switch(n->op) {
default:
fatal("chanop: unknown op %O", n->op);
case OMAKE:
// newchan(elemsize int, elemalg int,
// hint int) (hmap *chan[any-1]);
t = fixchan(n->type);
if(t == T)
break;
if(n->left != N) {
// async buf size
a = nod(OCONV, n->left, N);
a->type = types[TINT];
} else
a = nodintconst(0);
r = a;
a = nodintconst(algtype(t->type)); // elem algorithm
r = list(a, r);
a = nodintconst(t->type->width); // elem width
r = list(a, r);
on = syslook("newchan", 1);
argtype(on, t->type); // any-1
r = nod(OCALL, on, r);
walktype(r, top);
r->type = n->type;
break;
case OAS:
cl = listcount(n->left);
cr = listcount(n->right);
if(cl != 2 || cr != 1 || n->right->op != ORECV)
goto shape;
// chanrecv2(hchan *chan any) (elem any, pres bool);
t = fixchan(n->right->left->type);
if(t == T)
break;
a = n->right->left; // chan
r = a;
on = syslook("chanrecv2", 1);
argtype(on, t->type); // any-1
argtype(on, t->type); // any-2
r = nod(OCALL, on, r);
n->right = r;
r = n;
walktype(r, Etop);
break;
case ORECV:
if(n->right != N)
goto recv2;
// chanrecv1(hchan *chan any) (elem any);
t = fixchan(n->left->type);
if(t == T)
break;
a = n->left; // chan
r = a;
on = syslook("chanrecv1", 1);
argtype(on, t->type); // any-1
argtype(on, t->type); // any-2
r = nod(OCALL, on, r);
walktype(r, Erv);
break;
recv2:
// chanrecv3(hchan *chan any, *elem any) (pres bool);
t = fixchan(n->right->type);
if(t == T)
break;
a = n->right; // chan
r = a;
a = n->left; // elem
if(a == N) {
a = nod(OLITERAL, N, N);
a->val.ctype = CTNIL;
} else
a = nod(OADDR, a, N);
on = syslook("chanrecv3", 1);
argtype(on, t->type); // any-1
argtype(on, t->type); // any-2
r = nod(OCALL, on, r);
n->right = r;
r = n;
walktype(r, Etop);
break;
case OSEND:
t = fixchan(n->left->type);
if(t == T)
break;
if(top != Etop)
goto send2;
// chansend1(hchan *chan any, elem any);
t = fixchan(n->left->type);
if(t == T)
break;
a = n->right; // e
r = a;
a = n->left; // chan
r = list(a, r);
on = syslook("chansend1", 1);
argtype(on, t->type); // any-1
argtype(on, t->type); // any-2
r = nod(OCALL, on, r);
walktype(r, Etop);
break;
send2:
// chansend2(hchan *chan any, val any) (pres bool);
t = fixchan(n->left->type);
if(t == T)
break;
a = n->right; // e
r = a;
a = n->left; // chan
r = list(a, r);
on = syslook("chansend2", 1);
argtype(on, t->type); // any-1
argtype(on, t->type); // any-2
r = nod(OCALL, on, r);
walktype(r, Etop);
break;
}
return r;
shape:
fatal("chanop: %O", n->op);
return N;
}
Type*
fixarray(Type *t)
{
if(t == T)
goto bad;
if(t->etype != TARRAY)
goto bad;
if(t->type == T)
goto bad;
dowidth(t);
return t;
bad:
yyerror("not an array: %lT", t);
return T;
}
Node*
arrayop(Node *n, int top)
{
Node *r, *a;
Type *t, *tl;
Node *on;
Iter save;
r = n;
switch(n->op) {
default:
fatal("darrayop: unknown op %O", n->op);
case OCONV:
// arrays2d(old *any, nel int) (ary []any)
t = fixarray(n->left->type);
tl = fixarray(n->type);
if(t == T || tl == T)
break;
a = nodintconst(t->bound); // nel
a = nod(OCONV, a, N);
a->type = types[TINT];
r = a;
a = nod(OADDR, n->left, N); // old
r = list(a, r);
on = syslook("arrays2d", 1);
argtype(on, t); // any-1
argtype(on, tl->type); // any-2
r = nod(OCALL, on, r);
walktype(r, top);
n->left = r;
return n;
case OAS:
// arrays2d(old *any, nel int) (ary []any)
t = fixarray(n->right->type);
tl = fixarray(n->left->type);
if(t == T || tl == T)
break;
a = nodintconst(t->bound); // nel
a = nod(OCONV, a, N);
a->type = types[TINT];
r = a;
a = nod(OADDR, n->right, N); // old
r = list(a, r);
on = syslook("arrays2d", 1);
argtype(on, t); // any-1
argtype(on, tl->type); // any-2
r = nod(OCALL, on, r);
walktype(r, top);
n->right = r;
return n;
case OMAKE:
// newarray(nel int, max int, width int) (ary []any)
t = fixarray(n->type);
if(t == T)
break;
a = nodintconst(t->type->width); // width
a = nod(OCONV, a, N);
a->type = types[TINT];
r = a;
a = listfirst(&save, &n->left); // max
a = listnext(&save);
if(a == N)
a = nodintconst(0);
a = nod(OCONV, a, N);
a->type = types[TINT];
r = list(a, r);
a = listfirst(&save, &n->left); // nel
if(a == N) {
if(t->bound < 0)
yyerror("new open array must have size");
a = nodintconst(t->bound);
}
a = nod(OCONV, a, N);
a->type = types[TINT];
r = list(a, r);
on = syslook("newarray", 1);
argtype(on, t->type); // any-1
r = nod(OCALL, on, r);
walktype(r, top);
r->type = t; // if t had a name, going through newarray lost it
break;
case OSLICE:
// arrayslices(old any, nel int, lb int, hb int, width int) (ary []any)
// arraysliced(old []any, lb int, hb int, width int) (ary []any)
t = fixarray(n->left->type);
if(t == T)
break;
a = nodintconst(t->type->width); // width
a = nod(OCONV, a, N);
a->type = types[TINT];
r = a;
a = nod(OCONV, n->right->right, N); // hb
a->type = types[TINT];
r = list(a, r);
a = nod(OCONV, n->right->left, N); // lb
a->type = types[TINT];
r = list(a, r);
if(t->bound >= 0) {
// static slice
a = nodintconst(t->bound); // nel
a = nod(OCONV, a, N);
a->type = types[TINT];
r = list(a, r);
a = nod(OADDR, n->left, N); // old
r = list(a, r);
on = syslook("arrayslices", 1);
argtype(on, t); // any-1
argtype(on, t->type); // any-2
} else {
// dynamic slice
a = n->left; // old
r = list(a, r);
on = syslook("arraysliced", 1);
argtype(on, t->type); // any-1
argtype(on, t->type); // any-2
}
r = nod(OCALL, on, r);
walktype(r, top);
break;
}
return r;
}
/*
* assigning src to dst involving interfaces?
* return op to use.
*/
int
ifaceas1(Type *dst, Type *src)
{
if(src == T || dst == T)
return Inone;
if(isinter(dst)) {
if(isinter(src)) {
if(eqtype(dst, src, 0))
return I2Isame;
return I2I;
}
if(isnilinter(dst))
return T2I;
ifacecheck(dst, src, lineno);
return T2I;
}
if(isinter(src)) {
if(isnilinter(src))
return I2T;
ifacecheck(dst, src, lineno);
return I2T;
}
return Inone;
}
/*
* treat convert T to T as noop
*/
int
ifaceas(Type *dst, Type *src)
{
int et;
et = ifaceas1(dst, src);
if(et == I2Isame)
et = Inone;
return et;
}
static char*
ifacename[] =
{
[I2T] = "ifaceI2T",
[I2T2] = "ifaceI2T2",
[I2I] = "ifaceI2I",
[I2I2] = "ifaceI2I2",
[I2Isame] = "ifaceI2Isame",
};
Node*
ifaceop(Type *tl, Node *n, int op)
{
Type *tr;
Node *r, *a, *on;
Sym *s;
tr = n->type;
switch(op) {
default:
fatal("ifaceop: unknown op %O\n", op);
case T2I:
// ifaceT2I(sigi *byte, sigt *byte, elem any) (ret any);
a = n; // elem
r = a;
s = signame(tr); // sigt
if(s == S)
fatal("ifaceop: signame-1 T2I: %lT", tr);
a = s->oname;
a = nod(OADDR, a, N);
r = list(a, r);
s = signame(tl); // sigi
if(s == S) {
fatal("ifaceop: signame-2 T2I: %lT", tl);
}
a = s->oname;
a = nod(OADDR, a, N);
r = list(a, r);
on = syslook("ifaceT2I", 1);
argtype(on, tr);
argtype(on, tl);
break;
case I2T:
case I2T2:
case I2I:
case I2I2:
// iface[IT]2[IT][2](sigt *byte, iface any) (ret any[, ok bool]);
a = n; // interface
r = a;
s = signame(tl); // sigi
if(s == S)
fatal("ifaceop: signame %d", op);
a = s->oname;
a = nod(OADDR, a, N);
r = list(a, r);
on = syslook(ifacename[op], 1);
argtype(on, tr);
argtype(on, tl);
break;
case OEQ:
case ONE:
// ifaceeq(i1 any-1, i2 any-2) (ret bool);
a = n->right; // i2
r = a;
a = n->left; // i1
r = list(a, r);
on = syslook("ifaceeq", 1);
argtype(on, n->right->type);
argtype(on, n->left->type);
r = nod(OCALL, on, r);
if(op == ONE)
r = nod(ONOT, r, N);
walktype(r, Erv);
return r;
}
r = nod(OCALL, on, r);
walktype(r, Erv);
return r;
}
Node*
convas(Node *n)
{
Node *l, *r;
Type *lt, *rt;
int et;
if(n->op != OAS)
fatal("convas: not OAS %O", n->op);
lt = T;
rt = T;
l = n->left;
r = n->right;
if(l == N || r == N)
goto out;
lt = l->type;
rt = r->type;
if(lt == T || rt == T)
goto out;
if(n->left->op == OINDEX)
if(istype(n->left->left->type, TMAP)) {
indir(n, mapop(n, Elv));
goto out;
}
if(n->left->op == OSEND)
if(n->left->type != T) {
indir(n, chanop(n, Elv));
goto out;
}
if(eqtype(lt, rt, 0))
goto out;
et = ifaceas(lt, rt);
if(et != Inone) {
n->right = ifaceop(lt, r, et);
goto out;
}
if(isslice(lt) && isfixedarray(rt)) {
if(!eqtype(lt->type->type, rt->type->type, 0))
goto bad;
indir(n, arrayop(n, Etop));
goto out;
}
if(ascompat(lt, rt))
goto out;
bad:
badtype(n->op, lt, rt);
out:
ullmancalc(n);
return n;
}
Node*
old2new(Node *n, Type *t)
{
Node *l;
if(n->op != ONAME && n->op != ONONAME) {
yyerror("left side of := must be a name");
return n;
}
l = newname(n->sym);
dodclvar(l, t);
return l;
}
Node*
colas(Node *nl, Node *nr)
{
Iter savel, saver;
Node *l, *r, *a, *n;
Type *t;
int cl, cr;
/* nl is an expression list.
* nr is an expression list.
* return a newname-list from
* types derived from the rhs.
*/
n = N;
cr = listcount(nr);
cl = listcount(nl);
if(cl != cr) {
if(cr == 1)
goto multi;
goto badt;
}
l = listfirst(&savel, &nl);
r = listfirst(&saver, &nr);
while(l != N) {
walktype(r, Erv);
defaultlit(r);
a = old2new(l, r->type);
n = list(n, a);
l = listnext(&savel);
r = listnext(&saver);
}
n = rev(n);
return n;
multi:
/*
* there is a list on the left
* and a mono on the right.
* go into the right to get
* individual types for the left.
*/
switch(nr->op) {
default:
goto badt;
case OCALLMETH:
case OCALLINTER:
case OCALL:
walktype(nr->left, Erv);
t = nr->left->type;
if(t != T && t->etype == tptr)
t = t->type;
if(t == T || t->etype != TFUNC)
goto badt;
if(t->outtuple != cl)
goto badt;
l = listfirst(&savel, &nl);
t = structfirst(&saver, getoutarg(t));
while(l != N) {
a = old2new(l, t->type);
n = list(n, a);
l = listnext(&savel);
t = structnext(&saver);
}
break;
case OINDEX:
// check if rhs is a map index.
// if so, types are valuetype,bool
if(cl != 2)
goto badt;
walktype(nr->left, Elv);
t = nr->left->type;
if(!istype(t, TMAP))
goto badt;
a = old2new(nl->left, t->type);
n = a;
a = old2new(nl->right, types[TBOOL]);
n = list(n, a);
break;
case OCONV:
// a,b := i.(T)
if(cl != 2)
goto badt;
walktype(nr->left, Erv);
if(!isinter(nr->left->type))
goto badt;
// a,b = iface
a = old2new(nl->left, nr->type);
n = a;
a = old2new(nl->right, types[TBOOL]);
n = list(n, a);
break;
case ORECV:
if(cl != 2)
goto badt;
walktype(nr->left, Erv);
t = nr->left->type;
if(!istype(t, TCHAN))
goto badt;
a = old2new(nl->left, t->type);
n = a;
a = old2new(nl->right, types[TBOOL]);
n = list(n, a);
break;
}
n = rev(n);
return n;
badt:
if(nl->diag == 0) {
nl->diag = 1;
yyerror("assignment count mismatch: %d = %d", cl, cr);
}
return nl;
}
/*
* rewrite a range statement
* k and v are names/new_names
* m is an array or map
* local is =/0 or :=/1
*/
Node*
dorange(Node *nn)
{
Node *k, *v, *m;
Node *n, *hk, *on, *r, *a;
Type *t, *th;
int local;
if(nn->op != ORANGE)
fatal("dorange not ORANGE");
implicitstar(&nn->right);
k = nn->left;
m = nn->right;
local = nn->etype;
v = N;
if(k->op == OLIST) {
v = k->right;
k = k->left;
}
n = nod(OFOR, N, N);
walktype(m, Erv);
t = m->type;
if(t == T)
goto out;
if(t->etype == TARRAY)
goto ary;
if(t->etype == TMAP)
goto map;
yyerror("range must be over map/array");
goto out;
ary:
hk = nod(OXXX, N, N); // hidden key
tempname(hk, types[TINT]); // maybe TINT32
n->ninit = nod(OAS, hk, literal(0));
n->ntest = nod(OLT, hk, nod(OLEN, m, N));
n->nincr = nod(OASOP, hk, literal(1));
n->nincr->etype = OADD;
if(local)
k = old2new(k, hk->type);
n->nbody = nod(OAS, k, hk);
if(v != N) {
if(local)
v = old2new(v, t->type);
n->nbody = list(n->nbody,
nod(OAS, v, nod(OINDEX, m, hk)) );
}
goto out;
map:
th = typ(TARRAY);
th->type = ptrto(types[TUINT8]);
th->bound = (sizeof(struct Hiter) + types[tptr]->width - 1) /
types[tptr]->width;
hk = nod(OXXX, N, N); // hidden iterator
tempname(hk, th); // hashmap hash_iter
on = syslook("mapiterinit", 1);
argtype(on, t->down);
argtype(on, t->type);
argtype(on, th);
r = nod(OADDR, hk, N);
r = list(m, r);
r = nod(OCALL, on, r);
n->ninit = r;
r = nod(OINDEX, hk, literal(0));
a = nod(OLITERAL, N, N);
a->val.ctype = CTNIL;
r = nod(ONE, r, a);
n->ntest = r;
on = syslook("mapiternext", 1);
argtype(on, th);
r = nod(OADDR, hk, N);
r = nod(OCALL, on, r);
n->nincr = r;
if(local)
k = old2new(k, t->down);
if(v == N) {
on = syslook("mapiter1", 1);
argtype(on, th);
argtype(on, t->down);
r = nod(OADDR, hk, N);
r = nod(OCALL, on, r);
n->nbody = nod(OAS, k, r);
goto out;
}
if(local)
v = old2new(v, t->type);
on = syslook("mapiter2", 1);
argtype(on, th);
argtype(on, t->down);
argtype(on, t->type);
r = nod(OADDR, hk, N);
r = nod(OCALL, on, r);
n->nbody = nod(OAS, nod(OLIST, k, v), r);
goto out;
out:
return n;
}
/*
* from ascompat[te]
* evaluating actual function arguments.
* f(a,b)
* if there is exactly one function expr,
* then it is done first. otherwise must
* make temp variables
*/
Node*
reorder1(Node *n)
{
Iter save;
Node *l, *r, *f, *a, *g;
int c, t;
l = listfirst(&save, &n);
c = 0; // function calls
t = 0; // total parameters
loop1:
if(l == N) {
if(c == 0 || t == 1)
return n;
goto pass2;
}
if(l->op == OLIST)
fatal("reorder1 OLIST");
t++;
ullmancalc(l);
if(l->ullman >= UINF)
c++;
l = listnext(&save);
goto loop1;
pass2:
l = listfirst(&save, &n);
g = N; // fncalls assigned to tempnames
f = N; // one fncall assigned to stack
r = N; // non fncalls and tempnames assigned to stack
loop2:
if(l == N) {
r = rev(r);
g = rev(g);
f = list(g, f);
r = list(f, r);
return r;
}
ullmancalc(l);
if(l->ullman < UINF) {
r = list(l, r);
goto more;
}
if(f == N) {
f = l;
goto more;
}
// make assignment of fncall to tempname
a = nod(OXXX, N, N);
tempname(a, l->right->type);
a = nod(OAS, a, l->right);
g = list(a, g);
// put normal arg assignment on list
// with fncall replaced by tempname
l->right = a->left;
r = list(l, r);
more:
l = listnext(&save);
goto loop2;
}
/*
* from ascompat[et]
* a,b = f()
* return of a multi.
* there can be no function calls at all,
* or they will over-write the return values.
*/
Node*
reorder2(Node *n)
{
Iter save;
Node *l;
int c;
l = listfirst(&save, &n);
c = 0;
loop1:
if(l == N) {
if(c > 0)
yyerror("reorder2: too many funcation calls evaluating parameters");
return n;
}
if(l->op == OLIST)
fatal("reorder2 OLIST");
ullmancalc(l);
if(l->ullman >= UINF)
c++;
l = listnext(&save);
goto loop1;
}
/*
* from ascompat[ee]
* a,b = c,d
* simultaneous assignment. there cannot
* be later use of an earlier lvalue.
*/
int
vmatch2(Node *l, Node *r)
{
loop:
/*
* isolate all right sides
*/
if(r == N)
return 0;
switch(r->op) {
case ONAME:
// match each right given left
if(l == r)
return 1;
case OLITERAL:
return 0;
}
if(vmatch2(l, r->right))
return 1;
r = r->left;
goto loop;
}
int
vmatch1(Node *l, Node *r)
{
loop:
/*
* isolate all left sides
*/
if(l == N)
return 0;
switch(l->op) {
case ONAME:
// match each left with all rights
return vmatch2(l, r);
case OLITERAL:
return 0;
}
if(vmatch1(l->right, r))
return 1;
l = l->left;
goto loop;
}
Node*
reorder3(Node *n)
{
Iter save1, save2;
Node *l1, *l2, *q, *r;
int c1, c2;
r = N;
l1 = listfirst(&save1, &n);
c1 = 0;
while(l1 != N) {
l2 = listfirst(&save2, &n);
c2 = 0;
while(l2 != N) {
if(c2 > c1) {
if(vmatch1(l1->left, l2->right)) {
q = nod(OXXX, N, N);
tempname(q, l1->right->type);
q = nod(OAS, l1->left, q);
l1->left = q->right;
r = list(r, q);
break;
}
}
l2 = listnext(&save2);
c2++;
}
l1 = listnext(&save1);
c1++;
}
if(r == N)
return n;
q = N;
l1 = listfirst(&save1, &n);
while(l1 != N) {
q = list(q, l1);
l1 = listnext(&save1);
}
r = rev(r);
l1 = listfirst(&save1, &r);
while(l1 != N) {
q = list(q, l1);
l1 = listnext(&save1);
}
q = rev(q);
//dump("res", q);
return q;
}
Node*
reorder4(Node *n)
{
/*
* from ascompat[te]
* return c,d
* return expression assigned to output
* parameters. there may be no problems.
*/
return n;
}
Node*
structlit(Node *n)
{
Iter savel, saver;
Type *l, *t;
Node *var, *r, *a;
t = n->type;
if(t->etype != TSTRUCT)
fatal("structlit: not struct");
var = nod(OXXX, N, N);
tempname(var, t);
l = structfirst(&savel, &n->type);
r = listfirst(&saver, &n->left);
if(r != N && r->op == OEMPTY)
r = N;
loop:
if(l == T || r == N) {
if(l != T)
yyerror("struct literal expect expr of type %T", l);
if(r != N)
yyerror("struct literal too many expressions");
return var;
}
// build list of var.field = expr
a = nod(ODOT, var, newname(l->sym));
a = nod(OAS, a, r);
addtop = list(addtop, a);
l = structnext(&savel);
r = listnext(&saver);
goto loop;
}
Node*
arraylit(Node *n)
{
Iter saver;
Type *t;
Node *var, *r, *a, *nnew;
int idx, ninit, b;
t = n->type;
if(t->etype != TARRAY)
fatal("arraylit: not array");
// count initializers
ninit = 0;
r = listfirst(&saver, &n->left);
if(r != N && r->op == OEMPTY)
r = N;
while(r != N) {
ninit++;
r = listnext(&saver);
}
b = t->bound;
if(b == -100) {
// flag for [...]
b = ninit;
t = shallow(t);
t->bound = b;
}
var = nod(OXXX, N, N);
tempname(var, t);
nnew = nil;
if(b < 0) {
// slice
nnew = nod(OMAKE, N, N);
nnew->type = t;
a = nod(OAS, var, nnew);
addtop = list(addtop, a);
} else {
// if entire array isnt initialized,
// then clear the array
if(ninit < b) {
a = nod(OAS, var, N);
addtop = list(addtop, a);
}
}
idx = 0;
r = listfirst(&saver, &n->left);
if(r != N && r->op == OEMPTY)
r = N;
while(r != N) {
// build list of var[c] = expr
a = nodintconst(idx);
a = nod(OINDEX, var, a);
a = nod(OAS, a, r);
addtop = list(addtop, a);
idx++;
r = listnext(&saver);
}
if(b < 0)
nnew->left = nodintconst(idx);
return var;
}
Node*
maplit(Node *n)
{
Iter saver;
Type *t;
Node *var, *r, *a;
t = n->type;
if(t->etype != TMAP)
fatal("maplit: not map");
var = nod(OXXX, N, N);
tempname(var, t);
a = nod(OMAKE, N, N);
a->type = t;
a = nod(OAS, var, a);
addtop = list(addtop, a);
r = listfirst(&saver, &n->left);
if(r != N && r->op == OEMPTY)
r = N;
loop:
if(r == N) {
return var;
}
if(r->op != OKEY) {
yyerror("map literal must have key:value pairs");
return var;
}
// build list of var[c] = expr
a = nod(OINDEX, var, r->left);
a = nod(OAS, a, r->right);
addtop = list(addtop, a);
r = listnext(&saver);
goto loop;
}
|