aboutsummaryrefslogtreecommitdiff
path: root/gcc/m2/gm2-gcc/m2expr.cc
blob: 42ea4fa9f5bd5873afcde79eb541c72f9f56c231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
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
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
/* m2expr.cc provides an interface to GCC expression trees.

Copyright (C) 2012-2025 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius@glam.ac.uk>.

This file is part of GNU Modula-2.

GNU Modula-2 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.

GNU Modula-2 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 GNU Modula-2; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "gcc-consolidation.h"

#include "../gm2-lang.h"
#include "../m2-tree.h"
#include "m2convert.h"

/* Prototypes.  */

#define m2expr_c
#include "m2assert.h"
#include "m2builtins.h"
#include "m2convert.h"
#include "m2decl.h"
#include "m2expr.h"
#include "m2options.h"
#include "m2range.h"
#include "m2statement.h"
#include "m2tree.h"
#include "m2treelib.h"
#include "m2type.h"
#include "m2linemap.h"
#include "math.h"

static void m2expr_checkRealOverflow (location_t location, enum tree_code code,
                                      tree result);
static tree checkWholeNegateOverflow (location_t location, tree i, tree lowest,
                                      tree min, tree max);
// static tree m2expr_Build4LogicalAnd (location_t location, tree a, tree b,
// tree c, tree d);
static tree m2expr_Build4LogicalOr (location_t location, tree a, tree b,
                                    tree c, tree d);
static tree m2expr_Build4TruthOrIf (location_t location, tree a, tree b,
                                    tree c, tree d);
static tree m2expr_Build4TruthAndIf (location_t location, tree a, tree b,
                                     tree c, tree d);

static int label_count = 0;
static GTY (()) tree set_full_complement;

/* Return an integer string using base 10 and no padding.  The string returned
   will have been malloc'd.  */

char *
m2expr_CSTIntToString (tree t)
{
  char val[100];

  snprintf (val, 100, HOST_WIDE_INT_PRINT_UNSIGNED, TREE_INT_CST_LOW (t));
  return xstrndup (val, 100);
}

/* Return the char representation of tree t.  */

char
m2expr_CSTIntToChar (tree t)
{
  return (char) (TREE_INT_CST_LOW (t));
}

/* CompareTrees returns -1 if e1 < e2, 0 if e1 == e2, and 1 if e1 > e2.  */

int
m2expr_CompareTrees (tree e1, tree e2)
{
  return tree_int_cst_compare (m2expr_FoldAndStrip (e1),
                               m2expr_FoldAndStrip (e2));
}

/* FoldAndStrip return expression, t, after it has been folded (if
   possible).  */

tree
m2expr_FoldAndStrip (tree t)
{
  if (t != NULL)
    {
      t = fold (t);
      if (TREE_CODE (t) == CONST_DECL)
        return m2expr_FoldAndStrip (DECL_INITIAL (t));
    }

  return t;
}

/* StringLength returns an unsigned int which is the length of, string.  */

unsigned int
m2expr_StringLength (tree string)
{
  return TREE_STRING_LENGTH (string);
}

/* BuildCondIfExpression returns a tree containing (condition) ? (left) : right.  */

tree
m2expr_BuildCondIfExpression (tree condition, tree type, tree left, tree right)
{
  return fold_build3 (COND_EXPR, type, condition, left, right);
}

/* CheckAddressToCardinal if op is a pointer convert it to the ADDRESS type.  */

static tree
CheckAddressToCardinal (location_t location, tree op)
{
  if (m2type_IsAddress (TREE_TYPE (op)))
    return m2convert_BuildConvert (location, m2type_GetCardinalAddressType (),
                                   op, false);
  return op;
}

/* BuildTruthAndIf return true if a && b.  Retain order left to right.  */

static tree
m2expr_BuildTruthAndIf (location_t location, tree a, tree b)
{
  return m2expr_build_binary_op (location, TRUTH_ANDIF_EXPR, a, b, false);
}

/* BuildTruthOrIf return true if a || b.  Retain order left to right.  */

static tree
m2expr_BuildTruthOrIf (location_t location, tree a, tree b)
{
  return m2expr_build_binary_op (location, TRUTH_ORIF_EXPR, a, b, false);
}

/* BuildTruthNotIf inverts the boolean value of expr and returns the result.  */

static tree
m2expr_BuildTruthNot (location_t location, tree expr)
{
  return m2expr_build_unary_op (location, TRUTH_NOT_EXPR, expr, false);
}

/* BuildPostInc builds a post increment tree, the second operand is
   always one.  */

static tree
m2expr_BuildPostInc (location_t location, tree op)
{
  return m2expr_BuildAdd (location, op, build_int_cst (TREE_TYPE (op), 1), false);
}

/* BuildPostDec builds a post decrement tree, the second operand is
   always one.  */

static tree
m2expr_BuildPostDec (location_t location, tree op)
{
  return m2expr_BuildSub (location, op, build_int_cst (TREE_TYPE (op), 1), false);
}

/* BuildAddCheck builds an addition tree.  */

tree
m2expr_BuildAddCheck (location_t location, tree op1, tree op2, tree lowest,
                      tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, PLUS_EXPR, op1, op2, false,
                                    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildAdd builds an addition tree.  */

tree
m2expr_BuildAdd (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, PLUS_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildSubCheck builds a subtraction tree.  */

tree
m2expr_BuildSubCheck (location_t location, tree op1, tree op2, tree lowest,
                      tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, MINUS_EXPR, op1, op2, false,
                                    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildSub builds a subtraction tree.  */

tree
m2expr_BuildSub (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, MINUS_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildDivTrunc builds a trunc division tree.  */

tree
m2expr_BuildDivTrunc (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, TRUNC_DIV_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildDivTruncCheck builds a trunc division tree.  */

tree
m2expr_BuildDivTruncCheck (location_t location, tree op1, tree op2, tree lowest,
			   tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, TRUNC_DIV_EXPR, op1, op2, false,
				    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildModTruncCheck builds a trunc modulus tree.  */

tree
m2expr_BuildModTruncCheck (location_t location, tree op1, tree op2, tree lowest,
			   tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, TRUNC_MOD_EXPR, op1, op2, false,
				    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildModTrunc builds a trunc modulus tree.  */

tree
m2expr_BuildModTrunc (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, TRUNC_MOD_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildModCeilCheck builds a ceil modulus tree.  */

tree
m2expr_BuildModCeilCheck (location_t location, tree op1, tree op2, tree lowest,
			  tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, CEIL_MOD_EXPR, op1, op2, false,
				    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildModFloorCheck builds a trunc modulus tree.  */

tree
m2expr_BuildModFloorCheck (location_t location, tree op1, tree op2, tree lowest,
			   tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, FLOOR_MOD_EXPR, op1, op2, false,
				    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildDivCeil builds a ceil division tree.  */

tree
m2expr_BuildDivCeil (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, CEIL_DIV_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildDivCeilCheck builds a check ceil division tree.  */

tree
m2expr_BuildDivCeilCheck (location_t location, tree op1, tree op2, tree lowest,
			  tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, CEIL_DIV_EXPR, op1, op2, false,
				    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildModCeil builds a ceil modulus tree.  */

tree
m2expr_BuildModCeil (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, CEIL_MOD_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildDivFloor builds a floor division tree.  */

tree
m2expr_BuildDivFloor (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, FLOOR_DIV_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildDivFloorCheck builds a check floor division tree.  */

tree
m2expr_BuildDivFloorCheck (location_t location, tree op1, tree op2, tree lowest,
			   tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, FLOOR_DIV_EXPR, op1, op2, false,
				    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* BuildRDiv builds a division tree (this should only be used for
   REAL and COMPLEX types and NEVER for integer based types).  */

tree
m2expr_BuildRDiv (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  t = m2expr_build_binary_op (location, RDIV_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildModFloor builds a modulus tree.  */

tree
m2expr_BuildModFloor (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op (location, FLOOR_MOD_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildLSL builds and returns tree (op1 << op2).  */

tree
m2expr_BuildLSL (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  t = m2expr_build_binary_op (location, LSHIFT_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildLSR builds and returns tree (op1 >> op2).  */

tree
m2expr_BuildLSR (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  t = m2expr_build_binary_op (location, RSHIFT_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* createUniqueLabel returns a unique label which has been alloc'ed.  */

static char *
createUniqueLabel (void)
{
  int size, i;
  char *label;

  label_count++;
  i = label_count;
  size = strlen (".LSHIFT") + 2;
  while (i > 0)
    {
      i /= 10;
      size++;
    }
  label = (char *)ggc_alloc_atomic (size);
  sprintf (label, ".LSHIFT%d", label_count);
  return label;
}

/* BuildLogicalShift builds the ISO Modula-2 SHIFT operator for a
   fundamental data type.  */

void
m2expr_BuildLogicalShift (location_t location, tree op1, tree op2, tree op3,
                          tree nBits ATTRIBUTE_UNUSED, bool needconvert)
{
  tree res;

  m2assert_AssertLocation (location);
  op2 = m2expr_FoldAndStrip (op2);
  op3 = m2expr_FoldAndStrip (op3);
  if (TREE_CODE (op3) == INTEGER_CST)
    {
      op2 = m2convert_ToWord (location, op2);
      if (tree_int_cst_sgn (op3) < 0)
        res = m2expr_BuildLSR (
            location, op2,
            m2convert_ToWord (location,
                              m2expr_BuildNegate (location, op3, needconvert)),
            needconvert);
      else
        res = m2expr_BuildLSL (location, op2, m2convert_ToWord (location, op3),
                               needconvert);
      res = m2convert_BuildConvert (
          location, m2tree_skip_type_decl (TREE_TYPE (op1)), res, false);
      m2statement_BuildAssignmentTree (location, op1, res);
    }
  else
    {
      char *labelElseName = createUniqueLabel ();
      char *labelEndName = createUniqueLabel ();
      tree is_less = m2expr_BuildLessThan (location,
                                           m2convert_ToInteger (location, op3),
                                           m2expr_GetIntegerZero (location));

      m2statement_DoJump (location, is_less, NULL, labelElseName);
      op2 = m2convert_ToWord (location, op2);
      op3 = m2convert_ToWord (location, op3);
      res = m2expr_BuildLSL (location, op2, op3, needconvert);
      res = m2convert_BuildConvert (
          location, m2tree_skip_type_decl (TREE_TYPE (op1)), res, false);
      m2statement_BuildAssignmentTree (location, op1, res);
      m2statement_BuildGoto (location, labelEndName);
      m2statement_DeclareLabel (location, labelElseName);
      res = m2expr_BuildLSR (location, op2,
                             m2expr_BuildNegate (location, op3, needconvert),
                             needconvert);
      res = m2convert_BuildConvert (
          location, m2tree_skip_type_decl (TREE_TYPE (op1)), res, false);
      m2statement_BuildAssignmentTree (location, op1, res);
      m2statement_DeclareLabel (location, labelEndName);
    }
}

/* BuildLRL builds and returns tree (op1 rotate left by op2 bits).  */

tree
m2expr_BuildLRL (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  t = m2expr_build_binary_op (location, LROTATE_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildLRR builds and returns tree (op1 rotate right by op2 bits).  */

tree
m2expr_BuildLRR (location_t location, tree op1, tree op2, bool needconvert)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  t = m2expr_build_binary_op (location, RROTATE_EXPR, op1, op2, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* m2expr_BuildMask returns a tree for the mask of a set of nBits.
   It assumes nBits is <= TSIZE (WORD).  */

tree
m2expr_BuildMask (location_t location, tree nBits, bool needconvert)
{
  tree mask = m2expr_BuildLSL (location, m2expr_GetIntegerOne (location),
                               nBits, needconvert);
  m2assert_AssertLocation (location);
  return m2expr_BuildSub (location, mask, m2expr_GetIntegerOne (location),
                          needconvert);
}

/* m2expr_BuildLRotate returns a tree in which op1 has been left
   rotated by nBits.  It assumes nBits is <= TSIZE (WORD).  */

tree
m2expr_BuildLRotate (location_t location, tree op1, tree nBits,
                     bool needconvert)
{
  tree t;

  op1 = m2expr_FoldAndStrip (op1);
  nBits = m2expr_FoldAndStrip (nBits);
  nBits = m2convert_BuildConvert (location, TREE_TYPE (op1), nBits, needconvert);  
  t = m2expr_build_binary_op (location, LROTATE_EXPR, op1, nBits, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* m2expr_BuildRRotate returns a tree in which op1 has been left
   rotated by nBits.  It assumes nBits is <= TSIZE (WORD).  */

tree
m2expr_BuildRRotate (location_t location, tree op1, tree nBits,
                     bool needconvert)
{
  tree t;

  op1 = m2expr_FoldAndStrip (op1);
  nBits = m2expr_FoldAndStrip (nBits);
  nBits = m2convert_BuildConvert (location, TREE_TYPE (op1), nBits, needconvert);
  t = m2expr_build_binary_op (location, RROTATE_EXPR, op1, nBits, needconvert);
  return m2expr_FoldAndStrip (t);
}

/* BuildLRLn builds and returns tree (op1 rotate left by op2 bits) it
   rotates a set of size, nBits.  */

tree
m2expr_BuildLRLn (location_t location, tree op1, tree op2, tree nBits,
                  bool needconvert)
{
  tree op2min;

  m2assert_AssertLocation (location);

  /* Ensure we wrap the rotate.  */

  op2min = m2expr_BuildModTrunc (
      location, m2convert_ToCardinal (location, op2),
      m2convert_ToCardinal (location, nBits), needconvert);

  /* Optimize if we are we going to rotate a TSIZE(BITSET) set.  */

  if (m2expr_CompareTrees (
          m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset ()), nBits)
      == 0)
    return m2expr_BuildLRotate (location, op1, op2min, needconvert);
  else
    {
      tree mask = m2expr_BuildMask (location, nBits, needconvert);
      tree left, right;

      /* Make absolutely sure there are no high order bits lying around.  */

      op1 = m2expr_BuildLogicalAnd (location, op1, mask, needconvert);
      left = m2expr_BuildLSL (location, op1, op2min, needconvert);
      left = m2expr_BuildLogicalAnd (location, left, mask, needconvert);
      right = m2expr_BuildLSR (
          location, op1,
          m2expr_BuildSub (location, m2convert_ToCardinal (location, nBits),
                           op2min, needconvert),
          needconvert);
      return m2expr_BuildLogicalOr (location, left, right, needconvert);
    }
}

/* BuildLRRn builds and returns tree (op1 rotate right by op2 bits).
   It rotates a set of size, nBits.  */

tree
m2expr_BuildLRRn (location_t location, tree op1, tree op2, tree nBits,
                  bool needconvert)
{
  tree op2min;

  m2assert_AssertLocation (location);

  /* Ensure we wrap the rotate.  */

  op2min = m2expr_BuildModTrunc (
      location, m2convert_ToCardinal (location, op2),
      m2convert_ToCardinal (location, nBits), needconvert);
  /* Optimize if we are we going to rotate a TSIZE(BITSET) set.  */

  if (m2expr_CompareTrees (
          m2decl_BuildIntegerConstant (m2decl_GetBitsPerBitset ()), nBits)
      == 0)
    return m2expr_BuildRRotate (location, op1, op2min, needconvert);
  else
    {
      tree mask = m2expr_BuildMask (location, nBits, needconvert);
      tree left, right;

      /* Make absolutely sure there are no high order bits lying around.  */

      op1 = m2expr_BuildLogicalAnd (location, op1, mask, needconvert);
      right = m2expr_BuildLSR (location, op1, op2min, needconvert);
      left = m2expr_BuildLSL (
          location, op1,
          m2expr_BuildSub (location, m2convert_ToCardinal (location, nBits),
                           op2min, needconvert),
          needconvert);
      left = m2expr_BuildLogicalAnd (location, left, mask, needconvert);
      return m2expr_BuildLogicalOr (location, left, right, needconvert);
    }
}

/* BuildLogicalRotate build the ISO Modula-2 ROTATE operator for a
   fundamental data type.  */

void
m2expr_BuildLogicalRotate (location_t location, tree op1, tree op2, tree op3,
                           tree nBits, bool needconvert)
{
  tree res;

  m2assert_AssertLocation (location);
  op2 = m2expr_FoldAndStrip (op2);
  op3 = m2expr_FoldAndStrip (op3);
  if (TREE_CODE (op3) == INTEGER_CST)
    {
      if (tree_int_cst_sgn (op3) < 0)
        res = m2expr_BuildLRRn (
            location, op2, m2expr_BuildNegate (location, op3, needconvert),
            nBits, needconvert);
      else
        res = m2expr_BuildLRLn (location, op2, op3, nBits, needconvert);
      m2statement_BuildAssignmentTree (location, op1, res);
    }
  else
    {
      char *labelElseName = createUniqueLabel ();
      char *labelEndName = createUniqueLabel ();
      tree rotateCount = m2convert_ToInteger (location, op3);
      tree is_less = m2expr_BuildLessThan (location, rotateCount,
                                           m2expr_GetIntegerZero (location));

      m2statement_DoJump (location, is_less, NULL, labelElseName);
      res = m2expr_BuildLRLn (location, op2, rotateCount, nBits, needconvert);
      m2statement_BuildAssignmentTree (location, op1, res);
      m2statement_BuildGoto (location, labelEndName);
      m2statement_DeclareLabel (location, labelElseName);
      rotateCount = m2expr_BuildNegate (location, rotateCount, needconvert);
      res = m2expr_BuildLRRn (location, op2, rotateCount, nBits, needconvert);
      m2statement_BuildAssignmentTree (location, op1, res);
      m2statement_DeclareLabel (location, labelEndName);
    }
}

/* buildUnboundedArrayOf construct an unbounded struct and returns
   the gcc tree.  The two fields of the structure are initialized to
   contentsPtr and high.  */

static tree
buildUnboundedArrayOf (tree unbounded, tree contentsPtr, tree high)
{
  tree fields = TYPE_FIELDS (unbounded);
  tree field_list = NULL_TREE;
  tree constructor;

  field_list = tree_cons (fields, contentsPtr, field_list);
  fields = TREE_CHAIN (fields);

  field_list = tree_cons (fields, high, field_list);

  constructor = build_constructor_from_list (unbounded, nreverse (field_list));
  TREE_CONSTANT (constructor) = 0;
  TREE_STATIC (constructor) = 0;

  return constructor;
}

/* BuildBinarySetDo if the size of the set is <= TSIZE(WORD) then op1
   := binop(op2, op3) else call m2rtsprocedure(op1, op2, op3).  */

void
m2expr_BuildBinarySetDo (location_t location, tree settype, tree op1, tree op2,
                         tree op3, void (*binop) (location_t, tree, tree, tree,
                                                  tree, bool),
                         bool is_op1lvalue, bool is_op2lvalue, bool is_op3lvalue,
                         tree nBits, tree unbounded, tree varproc,
                         tree leftproc, tree rightproc)
{
  tree size = m2expr_GetSizeOf (location, settype);
  bool is_const = false;
  bool is_left = false;

  m2assert_AssertLocation (location);

  ASSERT_BOOL (is_op1lvalue);
  ASSERT_BOOL (is_op2lvalue);
  ASSERT_BOOL (is_op3lvalue);

  if (m2expr_CompareTrees (
          size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
      <= 0)
    /* Small set size <= TSIZE(WORD).  */
    (*binop) (location,
              m2treelib_get_rvalue (location, op1, settype, is_op1lvalue),
              m2treelib_get_rvalue (location, op2, settype, is_op2lvalue),
              m2treelib_get_rvalue (location, op3, settype, is_op3lvalue),
              nBits, false);
  else
    {
      tree result;
      tree high = m2expr_BuildSub (
          location,
          m2convert_ToCardinal (
              location,
              m2expr_BuildDivTrunc (
                  location, size,
                  m2expr_GetSizeOf (location, m2type_GetBitsetType ()),
                  false)),
          m2expr_GetCardinalOne (location), false);

      /* If op3 is constant then make op3 positive and remember which
      direction we are shifting.  */

      op3 = m2tree_skip_const_decl (op3);
      if (TREE_CODE (op3) == INTEGER_CST)
        {
          is_const = true;
          if (tree_int_cst_sgn (op3) < 0)
            op3 = m2expr_BuildNegate (location, op3, false);
          else
            is_left = true;
          op3 = m2convert_BuildConvert (location, m2type_GetM2CardinalType (),
                                        op3, false);
        }

      /* These parameters must match the prototypes of the procedures:
	 ShiftLeft, ShiftRight, ShiftVal, RotateLeft, RotateRight, RotateVal
	 inside gm2-iso/SYSTEM.mod.  */

      /* Remember we must build the parameters in reverse.  */

      /* Parameter 4 amount.  */
      m2statement_BuildParam (
          location,
          m2convert_BuildConvert (
              location, m2type_GetM2IntegerType (),
              m2treelib_get_rvalue (location, op3,
                                    m2tree_skip_type_decl (TREE_TYPE (op3)),
                                    is_op3lvalue),
              false));

      /* Parameter 3 nBits.  */
      m2statement_BuildParam (
          location,
          m2convert_BuildConvert (location, m2type_GetM2CardinalType (),
                                  m2expr_FoldAndStrip (nBits), false));

      /* Parameter 2 destination set.  */
      m2statement_BuildParam (
          location,
          buildUnboundedArrayOf (
              unbounded,
              m2treelib_get_set_address (location, op1, is_op1lvalue), high));

      /* Parameter 1 source set.  */
      m2statement_BuildParam (
          location,
          buildUnboundedArrayOf (
              unbounded,
              m2treelib_get_set_address (location, op2, is_op2lvalue), high));

      /* Now call the appropriate procedure inside SYSTEM.mod.  */
      if (is_const)
        if (is_left)
          result = m2statement_BuildProcedureCallTree (location, leftproc,
                                                       NULL_TREE);
        else
          result = m2statement_BuildProcedureCallTree (location, rightproc,
                                                       NULL_TREE);
      else
        result = m2statement_BuildProcedureCallTree (location, varproc,
                                                     NULL_TREE);
      add_stmt (location, result);
    }
}

/* Print a warning if a constant expression had overflow in folding.
   Invoke this function on every expression that the language requires
   to be a constant expression.  */

void
m2expr_ConstantExpressionWarning (tree value)
{
  if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
       || TREE_CODE (value) == FIXED_CST || TREE_CODE (value) == VECTOR_CST
       || TREE_CODE (value) == COMPLEX_CST)
      && TREE_OVERFLOW (value))
    pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
}

/* TreeOverflow return true if the contant expression, t, has caused
   an overflow.  No error message or warning is emitted and no
   modification is made to, t.  */

bool
m2expr_TreeOverflow (tree t)
{
  if ((TREE_CODE (t) == INTEGER_CST
       || (TREE_CODE (t) == COMPLEX_CST
           && TREE_CODE (TREE_REALPART (t)) == INTEGER_CST))
      && TREE_OVERFLOW (t))
    return true;
  else if ((TREE_CODE (t) == REAL_CST
            || (TREE_CODE (t) == COMPLEX_CST
                && TREE_CODE (TREE_REALPART (t)) == REAL_CST))
           && TREE_OVERFLOW (t))
    return true;
  else
    return false;
}

/* RemoveOverflow if tree, t, is a constant expression it removes any
   overflow flag and returns, t.  */

tree
m2expr_RemoveOverflow (tree t)
{
  if (TREE_CODE (t) == INTEGER_CST
      || (TREE_CODE (t) == COMPLEX_CST
          && TREE_CODE (TREE_REALPART (t)) == INTEGER_CST))
    TREE_OVERFLOW (t) = 0;
  else if (TREE_CODE (t) == REAL_CST
           || (TREE_CODE (t) == COMPLEX_CST
               && TREE_CODE (TREE_REALPART (t)) == REAL_CST))
    TREE_OVERFLOW (t) = 0;
  return t;
}

/* BuildCoerce return a tree containing the expression, expr, after
   it has been coersed to, type.  */

tree
m2expr_BuildCoerce (location_t location, tree des, tree type, tree expr)
{
  tree copy = copy_node (expr);
  TREE_TYPE (copy) = type;

  m2assert_AssertLocation (location);

  return m2treelib_build_modify_expr (location, des, NOP_EXPR, copy);
}

/* BuildTrunc return an integer expression from a REAL or LONGREAL op1.  */

tree
m2expr_BuildTrunc (tree op1)
{
  return convert_to_integer (m2type_GetIntegerType (),
                             m2expr_FoldAndStrip (op1));
}

/* checkUnaryWholeOverflow decide if we can check this unary expression.  */

tree
m2expr_checkUnaryWholeOverflow (location_t location, enum tree_code code,
                                tree arg, tree lowest, tree min, tree max)
{
  if (M2Options_GetWholeValueCheck () && (min != NULL))
    {
      lowest = m2tree_skip_type_decl (lowest);
      arg = fold_convert_loc (location, lowest, arg);

      switch (code)
        {
        case NEGATE_EXPR:
          return checkWholeNegateOverflow (location, arg, lowest, min, max);
        default:
	  return NULL;
        }
    }
  return NULL;
}

/* build_unary_op return a unary tree node.  */

tree
m2expr_build_unary_op_check (location_t location, enum tree_code code,
                             tree arg, tree lowest, tree min, tree max)
{
  tree argtype = TREE_TYPE (arg);
  tree result;
  tree check = NULL;

  m2assert_AssertLocation (location);

  arg = m2expr_FoldAndStrip (arg);

  if ((TREE_CODE (argtype) != REAL_TYPE) && (min != NULL))
    check = m2expr_checkUnaryWholeOverflow (location, code, arg, lowest, min, max);

  result = build1 (code, argtype, arg);
  protected_set_expr_location (result, location);

  if (check != NULL)
    result = build2 (COMPOUND_EXPR, argtype, check, result);

  if (SCALAR_FLOAT_TYPE_P (argtype))
    m2expr_checkRealOverflow (location, code, result);

  return m2expr_FoldAndStrip (result);
}

/* build_unary_op return a unary tree node.  */

tree
m2expr_build_unary_op (location_t location, enum tree_code code, tree arg,
                       int flag ATTRIBUTE_UNUSED)
{
  tree argtype = TREE_TYPE (arg);
  tree result;

  m2assert_AssertLocation (location);

  arg = m2expr_FoldAndStrip (arg);
  result = build1 (code, argtype, arg);
  protected_set_expr_location (result, location);

  return m2expr_FoldAndStrip (result);
}

/* build_binary_op is a heavily pruned version of the one found in
   c-typeck.cc.  The Modula-2 expression rules are much more restricted
   than C.  */

tree
build_binary_op (location_t location, enum tree_code code, tree op1, tree op2,
                 int convert ATTRIBUTE_UNUSED)
{
  tree type1 = TREE_TYPE (op1);
  tree result;

  m2assert_AssertLocation (location);

  /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
  STRIP_TYPE_NOPS (op1);
  STRIP_TYPE_NOPS (op2);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  result = build2 (code, type1, op1, op2);
  protected_set_expr_location (result, location);

  return m2expr_FoldAndStrip (result);
}

/* BuildLessThanZero - returns a tree containing (< value 0).  It
   checks the min and max value to ensure that the test can be safely
   achieved and will short circuit the result otherwise.  */

tree
m2expr_BuildLessThanZero (location_t location, tree value, tree type, tree min,
                          tree max)
{
  if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) >= 0)
    /* min is greater than or equal to zero therefore value will always
       be >= 0.  */
    return m2type_GetBooleanFalse ();
  else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) == -1)
    /* max is less than zero therefore value will always be < 0.  */
    return m2type_GetBooleanTrue ();
  /* We now know 0 lies in the range min..max so we can safely cast
     zero to type.  */
  return m2expr_BuildLessThan (
      location, value,
      fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
}

/* BuildGreaterThanZero - returns a tree containing (> value 0).  It
   checks the min and max value to ensure that the test can be safely
   achieved and will short circuit the result otherwise.  */

tree
m2expr_BuildGreaterThanZero (location_t location, tree value, tree type,
                             tree min, tree max)
{
  if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) == 1)
    /* min is greater than zero therefore value will always be > 0.  */
    return m2type_GetBooleanTrue ();
  else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) <= 0)
    /* max is less than or equal to zero therefore value will always be
       <= 0.  */
    return m2type_GetBooleanFalse ();
  /* We now know 0 lies in the range min..max so we can safely cast
     zero to type.  */
  return m2expr_BuildGreaterThan (
      location, value,
      fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
}

/* BuildEqualToZero - returns a tree containing (= value 0).  It
   checks the min and max value to ensure that the test can be safely
   achieved and will short circuit the result otherwise.  */

tree
m2expr_BuildEqualToZero (location_t location, tree value, tree type, tree min,
                         tree max)
{
  if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) == 1)
    /* min is greater than zero therefore value will always be > 0.  */
    return m2type_GetBooleanFalse ();
  else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
    /* max is less than or equal to zero therefore value will always be <
       0.  */
    return m2type_GetBooleanFalse ();
  /* We now know 0 lies in the range min..max so we can safely cast
     zero to type.  */
  return m2expr_BuildEqualTo (
      location, value,
      fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
}

/* BuildNotEqualToZero - returns a tree containing (# value 0).  It
   checks the min and max value to ensure that the test can be safely
   achieved and will short circuit the result otherwise.  */

tree
m2expr_BuildNotEqualToZero (location_t location, tree value, tree type,
                            tree min, tree max)
{
  if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) == 1)
    /* min is greater than zero therefore value will always be true.  */
    return m2type_GetBooleanTrue ();
  else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
    /* max is less than or equal to zero therefore value will always be
       true.  */
    return m2type_GetBooleanTrue ();
  /* We now know 0 lies in the range min..max so we can safely cast
     zero to type.  */
  return m2expr_BuildNotEqualTo (
      location, value,
      fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
}


/* BuildGreaterThanOrEqualZero - returns a tree containing (>= value 0).  It
   checks the min and max value to ensure that the test can be safely
   achieved and will short circuit the result otherwise.  */

tree
m2expr_BuildGreaterThanOrEqualZero (location_t location, tree value, tree type,
				    tree min, tree max)
{
  if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) >= 0)
    /* min is greater than or equal to zero therefore value will always be >= 0.  */
    return m2type_GetBooleanTrue ();
  else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
    /* max is less than zero therefore value will always be < 0.  */
    return m2type_GetBooleanFalse ();
  /* We now know 0 lies in the range min..max so we can safely cast
     zero to type.  */
  return m2expr_BuildGreaterThan (
      location, value,
      fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
}


/* BuildLessThanOrEqualZero - returns a tree containing (<= value 0).  It
   checks the min and max value to ensure that the test can be safely
   achieved and will short circuit the result otherwise.  */

tree
m2expr_BuildLessThanOrEqualZero (location_t location, tree value, tree type,
				 tree min, tree max)
{
  if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) > 0)
    /* min is greater than zero therefore value will always be > 0.  */
    return m2type_GetBooleanFalse ();
  else if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) <= 0)
    /* max is less than or equal to zero therefore value will always be <= 0.  */
    return m2type_GetBooleanTrue ();
  /* We now know 0 lies in the range min..max so we can safely cast
     zero to type.  */
  return m2expr_BuildLessThanOrEqual (
      location, value,
      fold_convert_loc (location, type, m2expr_GetIntegerZero (location)));
}


/* get_current_function_name, return the name of the current function if
   it currently exists.  NULL is returned if we are not inside a function.  */

static const char *
get_current_function_name (void)
{
  if (current_function_decl != NULL
      && (DECL_NAME (current_function_decl) != NULL)
      && (IDENTIFIER_POINTER (DECL_NAME (current_function_decl)) != NULL))
    return IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
  return NULL;
}

/* checkWholeNegateOverflow - check to see whether -arg will overflow
   an integer.

PROCEDURE sneg (i: INTEGER) ;
BEGIN
   IF i = MIN(INTEGER)
   THEN
      'integer overflow'
   END
END sneg ;

general purpose subrange type, i, is currently legal, min is
   MIN(type) and max is MAX(type).

PROCEDURE sneg (i: type) ;
BEGIN
   max := MAX (type) ;
   min := MIN (type) ;
   (* cannot overflow if i is 0 *)
   IF (i#0) AND
     (* will overflow if entire range is positive.  *)
     ((min >= 0) OR
      (* will overflow if entire range is negative.  *)
      (max <= 0) OR
      (* c7 and c8 and c9 and c10 -> c17 more units positive.  *)
      ((min < 0) AND (max > 0) AND ((min + max) > 0) AND (i > -min)) OR
      (* c11 and c12 and c13 and c14 -> c18 more units negative.  *)
      ((min < 0) AND (max > 0) AND ((min + max) < 0) AND (i < -max)))
   THEN
      'type overflow'
   END
END sneg ; */

static tree
checkWholeNegateOverflow (location_t location,
			  tree i, tree type, tree min,
                          tree max)
{
  tree a1
      = m2expr_BuildNotEqualToZero (location, i, type, min, max); /* i # 0.  */
  tree c1 = m2expr_BuildGreaterThanZero (location, min, type, min,
                                         max); /* min > 0.  */
  tree c2 = m2expr_BuildEqualToZero (location, min, type, min,
                                     max); /* min == 0.  */
  tree c4 = m2expr_BuildLessThanZero (location, max, type, min,
                                      max); /* max < 0.  */
  tree c5 = m2expr_BuildEqualToZero (location, max, type, min,
                                     max); /* max == 0.  */
  tree c7 = m2expr_BuildLessThanZero (location, min, type, min,
                                      max); /* min < 0.  */
  tree c8 = m2expr_BuildGreaterThanZero (location, max, type, min,
                                         max); /* max > 0.  */
  tree c9 = m2expr_BuildGreaterThanZero (
      location, m2expr_BuildAdd (location, min, max, false), type, min,
      max); /* min + max > 0.  */
  tree c10 = m2expr_BuildGreaterThan (
      location, i, m2expr_BuildNegate (location, min, false)); /* i > -min.  */
  tree c11 = m2expr_BuildLessThanZero (
      location, m2expr_BuildAdd (location, min, max, false), type, min,
      max); /* min + max < 0.  */
  tree c12 = m2expr_BuildLessThan (
      location, i, m2expr_BuildNegate (location, max, false)); /* i < -max.  */

  tree b1 = m2expr_BuildTruthOrIf (location, c1, c2);
  tree b2 = m2expr_BuildTruthOrIf (location, c8, c5);
  tree o1 = m2expr_BuildTruthAndIf (location, b1, b2);

  tree b3 = m2expr_BuildTruthOrIf (location, c7, c2);
  tree b4 = m2expr_BuildTruthOrIf (location, c4, c5);
  tree o2 = m2expr_BuildTruthAndIf (location, b3, b4);

  tree o3 = m2expr_Build4TruthAndIf (location, c7, c8, c9, c10);
  tree o4 = m2expr_Build4TruthAndIf (location, c7, c8, c11, c12);

  tree a2 = m2expr_Build4TruthOrIf (location, o1, o2, o3, o4);
  tree condition
      = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, a1, a2));

  tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value unary minus will cause range overflow");
  return t;
}

/* checkWholeAddOverflow - check to see whether op1 + op2 will
   overflow an integer.

PROCEDURE sadd (i, j: INTEGER) ;
BEGIN
   IF ((j>0) AND (i > MAX(INTEGER)-j)) OR ((j<0) AND (i < MIN(INTEGER)-j))
   THEN
      'signed addition overflow'
   END
END sadd.  */

static tree
checkWholeAddOverflow (location_t location, tree i, tree j, tree lowest,
                       tree min, tree max)
{
  tree j_gt_zero = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
  tree i_gt_max_sub_j = m2expr_BuildGreaterThan (
      location, i, m2expr_BuildSub (location, max, j, false));
  tree j_lt_zero = m2expr_BuildLessThanZero (location, j, lowest, min, max);
  tree i_lt_min_sub_j = m2expr_BuildLessThan (location, i,
					      m2expr_BuildSub (location, min, j, false));
  tree lhs_or = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, j_gt_zero, i_gt_max_sub_j));
  tree rhs_or = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, j_lt_zero, i_lt_min_sub_j));
  tree condition
      = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, lhs_or, rhs_or));
  tree result = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value addition will cause a range overflow");
  return result;
}

/* checkWholeSubOverflow - check to see whether op1 - op2 will
   overflow an integer.

PROCEDURE ssub (i, j: INTEGER) ;
BEGIN
   IF ((j>0) AND (i < MIN(INTEGER)+j)) OR ((j<0) AND (i > MAX(INTEGER)+j))
   THEN
      'signed subtraction overflow'
   END
END ssub.  */

static tree
checkWholeSubOverflow (location_t location, tree i, tree j, tree lowest,
                       tree min, tree max)
{
  tree c1 = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
  tree c2 = m2expr_BuildLessThan (location, i,
                                  m2expr_BuildAdd (location, min, j, false));
  tree c3 = m2expr_BuildLessThanZero (location, j, lowest, min, max);
  tree c4 = m2expr_BuildGreaterThan (location, i,
				     m2expr_BuildAdd (location, max, j, false));
  tree c5 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, c1, c2));
  tree c6 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, c3, c4));
  tree condition
      = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, c5, c6));
  tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value subtraction will cause a range overflow");
  return t;
}

/* Build4TruthAndIf - return true if a && b && c && d.  Retain order left to
 * right.  */

static tree
m2expr_Build4TruthAndIf (location_t location, tree a, tree b, tree c, tree d)
{
  tree t1 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, a, b));
  tree t2 = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, t1, c));
  return m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, t2, d));
}

/* Build3TruthAndIf - return true if a && b && c.  Retain order left to right.
 */

static tree
m2expr_Build3TruthAndIf (location_t location, tree op1, tree op2, tree op3)
{
  tree t = m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, op1, op2));
  return m2expr_FoldAndStrip (m2expr_BuildTruthAndIf (location, t, op3));
}

/* Build3TruthOrIf - return true if a || b || c.  Retain order left to right.
 */

static tree
m2expr_Build3TruthOrIf (location_t location, tree op1, tree op2, tree op3)
{
  tree t = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, op1, op2));
  return m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, t, op3));
}

/* Build4TruthOrIf - return true if op1 || op2 || op3 || op4.  Retain order
   left to right.  */

static tree
m2expr_Build4TruthOrIf (location_t location, tree op1, tree op2, tree op3,
                        tree op4)
{
  tree t1 = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, op1, op2));
  tree t2 = m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, t1, op3));
  return m2expr_FoldAndStrip (m2expr_BuildTruthOrIf (location, t2, op4));
}

/* Build4LogicalOr - return true if op1 || op2 || op3 || op4.  */

static tree
m2expr_Build4LogicalOr (location_t location, tree op1, tree op2, tree op3,
                        tree op4)
{
  tree t1 = m2expr_FoldAndStrip (
      m2expr_BuildLogicalOr (location, op1, op2, false));
  tree t2
      = m2expr_FoldAndStrip (m2expr_BuildLogicalOr (location, t1, op3, false));
  return m2expr_FoldAndStrip (
      m2expr_BuildLogicalOr (location, t2, op4, false));
}

/* checkWholeMultOverflow - check to see whether i * j will overflow
   an integer.

PROCEDURE smult (lhs, rhs: INTEGER) ;
BEGIN
   IF ((lhs > 0) AND (rhs > 0) AND (lhs > max DIV rhs)) OR
      ((lhs > 0) AND (rhs < 0) AND (rhs < min DIV lhs)) OR
      ((lhs < 0) AND (rhs > 0) AND (lhs < min DIV rhs)) OR
      ((lhs < 0) AND (rhs < 0) AND (lhs < max DIV rhs))
   THEN
      error ('signed multiplication overflow')
   END
END smult ;

 if ((c1 && c3 && c4)
   || (c1 && c5 && c6)
   || (c2 && c3 && c7)
   || (c2 && c5 && c8))
   error ('signed subtraction overflow').  */

static tree
testWholeMultOverflow (location_t location, tree lhs, tree rhs,
		       tree lowest, tree min, tree max)
{
  tree c1 = m2expr_BuildGreaterThanZero (location, lhs, lowest, min, max);
  tree c2 = m2expr_BuildLessThanZero (location, lhs, lowest, min, max);

  tree c3 = m2expr_BuildGreaterThanZero (location, rhs, lowest, min, max);
  tree c4 = m2expr_BuildGreaterThan (
      location, lhs, m2expr_BuildDivTrunc (location, max, rhs, false));

  tree c5 = m2expr_BuildLessThanZero (location, rhs, lowest, min, max);
  tree c6 = m2expr_BuildLessThan (
      location, rhs, m2expr_BuildDivTrunc (location, min, lhs, false));
  tree c7 = m2expr_BuildLessThan (
      location, lhs, m2expr_BuildDivTrunc (location, min, rhs, false));
  tree c8 = m2expr_BuildLessThan (
      location, lhs, m2expr_BuildDivTrunc (location, max, rhs, false));

  tree c9 = m2expr_Build3TruthAndIf (location, c1, c3, c4);
  tree c10 = m2expr_Build3TruthAndIf (location, c1, c5, c6);
  tree c11 = m2expr_Build3TruthAndIf (location, c2, c3, c7);
  tree c12 = m2expr_Build3TruthAndIf (location, c2, c5, c8);

  tree condition = m2expr_Build4LogicalOr (location, c9, c10, c11, c12);
  return condition;
}


static tree
checkWholeMultOverflow (location_t location, tree i, tree j, tree lowest,
                        tree min, tree max)
{
  tree condition = testWholeMultOverflow (location, i, j, lowest, min, max);
  tree result = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
						    get_current_function_name (),
               "whole value multiplication will cause a range overflow");
  return result;
}


static tree
divMinUnderflow (location_t location, tree value, tree lowest, tree min, tree max)
{
  tree min2 = m2expr_BuildMult (location, min, min, false);
  tree rhs = m2expr_BuildGreaterThanOrEqual (location, value, min2);
  tree lhs = testWholeMultOverflow (location, min, min, lowest, min, max);
  return m2expr_BuildTruthAndIf (location, lhs, rhs);
}

/*
   divexpr - returns true if a DIV_TRUNC b will overflow.
 */

/* checkWholeDivOverflow - check to see whether i DIV_TRUNC j will overflow
   an integer.  The Modula-2 implementation of the GCC trees follows:

PROCEDURE divtruncexpr (a, b: INTEGER) : BOOLEAN ;
BEGIN
   (* Firstly catch division by 0.  *)
   RETURN ((b = 0) OR
           (* Case 2 range is always negative.  *)
           (* In which case a division will be illegal as result will be positive.  *)
           (max < 0) OR
           (* Case 1 both min / max are positive, check for underflow.  *)
           ((min >= 0) AND (max >= 0) AND (multMinOverflow (b) OR (a < b * min))) OR
           (* Case 1 both min / max are positive, check for overflow.  *)
           ((min >= 0) AND (max >= 0) AND (divMinUnderflow (a) OR (b > a DIV min))) OR
           (* Case 3 mixed range, need to check underflow.  *)
           ((min < 0) AND (max >= 0) AND (a < 0) AND (b < 0) AND (b >= a DIV min)) OR
           ((min < 0) AND (max >= 0) AND (a < 0) AND (b > 0) AND (b <= a DIV max)) OR
           ((min < 0) AND (max >= 0) AND (a >= 0) AND (b < 0) AND (a DIV b < min)))
END divtruncexpr ;

s1 -> a DIV min
s2 -> a DIV max
s3 -> a DIV b

b4 -> (min >= 0) AND (max >= 0)
b5 -> (min < 0) AND (max >= 0)
a_lt_b_mult_min -> (a < b * min)
b_mult_min_overflow -> testWholeMultOverflow (location, b, min, lowest, min, max)
b6 -> (b_mult_min_overflow OR a_lt_b_mult_min)
b_gt_s1 -> (b > s1)
a_div_min_overflow -> divMinUnderflow (location, a, min, lowest, min, max)
b7 -> (a_div_min_overflow OR b_gt_s1)
b8 -> (a < 0)
b9 -> (b < 0)
b10 -> (b > 0)
b11 -> (b >= s1)
b12 -> (b <= s2)
b13 -> (s3 < min)
b14 -> a >= 0

c1 -> (b = 0)
c2 -> (max < 0)
c3 -> (b4 AND b6)
c4 -> (b4 AND b7)
c5 -> (b5 AND b8 AND b9 AND b11)
c6 -> (b5 AND b8 AND b10 AND b12)
c7 -> (b5 AND b14 AND b9 AND b13)

 if (c1 || c2 || c3 || c4 || c5 || c6 || c7)
   error ('signed div trunc overflow').  */

static tree
checkWholeDivTruncOverflow (location_t location, tree i, tree j, tree lowest,
			    tree min, tree max)
{
  tree b4a = m2expr_BuildGreaterThanOrEqualZero (location, min, lowest, min, max);
  tree b4b = m2expr_BuildGreaterThanOrEqualZero (location, max, lowest, min, max);
  tree b4 = m2expr_BuildTruthAndIf (location, b4a, b4b);
  tree b5a = m2expr_BuildLessThanZero (location, min, lowest, min, max);
  tree b5 = m2expr_BuildTruthAndIf (location, b5a, b4b);
  tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
  tree c2 = m2expr_BuildLessThanZero (location, max, lowest, min, max);
  tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, m2expr_BuildMult (location, j, min, false));
  tree j_mult_min_overflow = testWholeMultOverflow (location, j, min, lowest, min, max);
  tree b6 = m2expr_BuildTruthOrIf (location, j_mult_min_overflow, i_lt_j_mult_min);
  tree c3 = m2expr_BuildTruthAndIf (location, b4, b6);
  tree s1 = m2expr_BuildDivTrunc (location, i, min, false);
  tree s2 = m2expr_BuildDivTrunc (location, i, max, false);
  tree s3 = m2expr_BuildDivTrunc (location, i, j, false);

  tree j_gt_s1 = m2expr_BuildGreaterThan (location, j, s1);
  tree i_div_min_overflow = divMinUnderflow (location, i, lowest, min, max);
  tree b7 = m2expr_BuildTruthOrIf (location, i_div_min_overflow, j_gt_s1);
  tree c4 = m2expr_BuildTruthAndIf (location, b4, b7);
  tree b8 = m2expr_BuildLessThanZero (location, i, lowest, min, max);
  tree b9 = m2expr_BuildLessThanZero (location, j, lowest, min, max);
  tree b10 = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
  tree b11 = m2expr_BuildGreaterThanOrEqual (location, j, s1);
  tree b12 = m2expr_BuildLessThanOrEqual (location, j, s2);
  tree b13 = m2expr_BuildLessThan (location, s3, min);
  tree b14 = m2expr_BuildGreaterThanOrEqualZero (location, i, lowest, min, max);
  tree c5 = m2expr_Build4TruthAndIf (location, b5, b8, b9, b11);
  tree c6 = m2expr_Build4TruthAndIf (location, b5, b8, b10, b12);
  tree c7 = m2expr_Build4TruthAndIf (location, b5, b14, b9, b13);
  tree c8 = m2expr_Build4TruthOrIf (location, c1, c2, c3, c4);
  tree condition = m2expr_Build4TruthOrIf (location, c5, c6, c7, c8);
  tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value truncated division will cause a range overflow");
  return t;
}

#if 0
(*
   divexpr - returns true if a DIV_CEIL b will overflow.
 *)

(* checkWholeDivCeilOverflow - check to see whether i DIV_CEIL j will overflow
   an integer.  *)

PROCEDURE divceilexpr (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN ((j = 0) OR     (* division by zero.  *)
           (maxT < 0) OR  (* both inputs are < 0 and max is < 0,
                             therefore error.  *)
           ((i # 0) AND   (* first operand is legally zero,
                             result is also legally zero.  *)
            divCeilOverflowCases (i, j)))
END divceilexpr ;


(*
   divCeilOverflowCases - precondition:  i, j are in range values.
                          postcondition:  true is returned if i divceil will
                                          result in an overflow/underflow.
*)

PROCEDURE divCeilOverflowCases (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN (((i > 0) AND (j > 0) AND divCeilOverflowPosPos (i, j)) OR
           ((i < 0) AND (j < 0) AND divCeilOverflowNegNeg (i, j)) OR
           ((i > 0) AND (j < 0) AND divCeilOverflowPosNeg (i, j)) OR
           ((i < 0) AND (j > 0) AND divCeilOverflowNegPos (i, j)))
END divCeilOverflowCases ;


(*
   divCeilOverflowPosPos - precondition:  i, j are legal and are both >= 0.
                           postcondition:  true is returned if i divceil will
                                           result in an overflow/underflow.
*)

PROCEDURE divCeilOverflowPosPos (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN (((i MOD j = 0) AND (i < j * minT)) OR
           (((i MOD j # 0) AND (i < j * minT + 1))))
END divCeilOverflowPosPos ;


(*
   divCeilOverflowNegNeg - precondition:  i, j are in range values and both < 0.
                           postcondition:  true is returned if i divceil will
                                           result in an overflow/underflow.
*)

PROCEDURE divCeilOverflowNegNeg (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN ((maxT <= 0) OR           (* signs will cause overflow.  *)
           (* check for underflow.  *)
           ((ABS (i) MOD ABS (j) = 0) AND (i >= j * minT)) OR
           ((ABS (i) MOD ABS (j) # 0) AND (i >= j * minT - 1)) OR
           (* check for overflow.  *)
           (((ABS (i) MOD maxT) = 0) AND (ABS (i) DIV maxT > ABS (j))) OR
           (((ABS (i) MOD maxT) # 0) AND (ABS (i) DIV maxT > ABS (j) + 1)))
END divCeilOverflowNegNeg ;


(*
   divCeilOverflowNegPos - precondition:  i, j are in range values.  i < 0, j >= 0.
                           postcondition:  true is returned if i divceil will
                                           result in an overflow/underflow.
*)

PROCEDURE divCeilOverflowNegPos (i, j: INTEGER) : BOOLEAN ;
BEGIN
   (* easier than might be initially expected.  We know minT < 0 and maxT > 0.
      We know the result will be negative and therefore we only need to test
      against minT.  *)
   RETURN (((ABS (i) MOD j = 0) AND (i < j * minT)) OR
           ((ABS (i) MOD j # 0) AND (i < j * minT - 1)))
END divCeilOverflowNegPos ;


(*
   divCeilOverflowPosNeg - precondition:  i, j are in range values.  i >= 0, j < 0.
                           postcondition:  true is returned if i divceil will
                                           result in an overflow/underflow.
*)

PROCEDURE divCeilOverflowPosNeg (i, j: INTEGER) : BOOLEAN ;
BEGIN
   (* easier than might be initially expected.  We know minT < 0 and maxT > 0.
      We know the result will be negative and therefore we only need to test
      against minT.  *)
   RETURN (((i MOD ABS (j) = 0) AND (i > j * minT)) OR
           ((i MOD ABS (j) # 0) AND (i > j * minT - 1)))
END divCeilOverflowPosNeg ;
#endif

/* divCeilOverflowPosPos, precondition:  lhs, rhs are legal and are both >= 0.
   Postcondition:  TRUE is returned if lhs divceil rhs will result
   in an overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN (((lhs MOD rhs = 0) AND (min >= 0) AND (lhs < rhs * min)) OR       (* check for underflow, no remainder.   *)
                                                  lhs_lt_rhs_mult_min
           (((lhs MOD rhs # 0) AND (lhs < rhs * min + 1))))   (* check for underflow with remainder.  *)
                                   ((lhs > min) AND (lhs - 1 > rhs * min))
                                                  lhs_gt_rhs_mult_min

   a -> (lhs MOD rhs = 0) AND (lhs < rhs * min)
   b -> (lhs MOD rhs # 0) AND (lhs < rhs * min + 1)
   RETURN a OR b.  */

static tree
divCeilOverflowPosPos (location_t location, tree i, tree j, tree lowest,
		       tree min, tree max)
{
  tree i_mod_j = m2expr_BuildModTrunc (location, i, j, false);
  tree i_mod_j_eq_zero = m2expr_BuildEqualToZero (location, i_mod_j, lowest, min, max);
  tree i_mod_j_ne_zero = m2expr_BuildNotEqualToZero (location, i_mod_j, lowest, min, max);
  tree j_min = m2expr_BuildMult (location, j, min, false);
  tree j_min_1 = m2expr_BuildAdd (location, j_min, m2expr_GetIntegerOne (location), false);
  tree i_lt_j_min = m2expr_BuildLessThan (location, i, j_min);
  tree i_lt_j_min_1 = m2expr_BuildLessThan (location, i, j_min_1);
  tree a = m2expr_BuildTruthAndIf (location, i_mod_j_eq_zero, i_lt_j_min);
  tree b = m2expr_BuildTruthAndIf (location, i_mod_j_ne_zero, i_lt_j_min_1);
  return m2expr_BuildTruthOrIf (location, a, b);
}


/* divCeilOverflowPosNeg precondition:  i, j are in range values and i >=0, j < 0.
   Postcondition:  TRUE is returned if i divceil j will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN (((i MOD ABS (j) = 0) AND (i > j * min)) OR
           ((i MOD ABS (j) # 0) AND (i > j * min - 1)))

   abs_j -> (ABS (j))
   i_mod_abs_j -> (i MOD abs_j)
   i_mod_abs_j_eq_0 -> (i_mod_abs_j = 0)
   i_mod_abs_j_ne_0 -> (i_mod_abs_j # 0)
   j_mult_min -> (j * min)
   j_mult_min_1 -> (j_mult_min - 1)
   i_gt_j_mult_min -> (i > j_mult_min)
   i_gt_j_mult_min_1 -> (i > j_mult_min_1)
   a -> (i_mod_abs_j_eq_0 AND i_gt_j_mult_min)
   b -> (i_mod_abs_j_ne_0 AND i_gt_j_mult_min_1)
   c -> (a OR b).  */

static tree
divCeilOverflowPosNeg (location_t location, tree i, tree j, tree lowest, tree min, tree max)
{
  tree abs_j = m2expr_BuildAbs (location, j);
  tree i_mod_abs_j = m2expr_BuildModFloor (location, i, abs_j, false);
  tree i_mod_abs_j_eq_0 = m2expr_BuildEqualToZero (location, i_mod_abs_j, lowest, min, max);
  tree i_mod_abs_j_ne_0 = m2expr_BuildNotEqualToZero (location, i_mod_abs_j, lowest, min, max);
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree j_mult_min_1 = m2expr_BuildPostDec (location, j_mult_min);
  tree i_gt_j_mult_min = m2expr_BuildGreaterThan (location, i, j_mult_min);
  tree i_gt_j_mult_min_1 = m2expr_BuildGreaterThan (location, i, j_mult_min_1);
  tree a = m2expr_BuildTruthAndIf (location, i_mod_abs_j_eq_0, i_gt_j_mult_min);
  tree b = m2expr_BuildTruthAndIf (location, i_mod_abs_j_ne_0, i_gt_j_mult_min_1);
  tree c = m2expr_BuildTruthOrIf (location, a, b);
  return c;
}


/* divCeilOverflowNegPos precondition:  i, j are in range values and i < 0, j >= 0.
   Postcondition:  TRUE is returned if i divceil j will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN (((ABS (i) MOD j = 0) AND (i < j * min)) OR
           ((ABS (i) MOD j # 0) AND (i < j * min - 1)))

   abs_i -> (ABS (i))
   abs_i_mod_j -> (abs_i MOD j)
   abs_i_mod_j_eq_0 -> (abs_i_mod_j = 0)
   abs_i_mod_j_ne_0 -> (abs_i_mod_j # 0)
   j_mult_min -> (j * min)
   j_mult_min_1 -> (j_mult_min - 1)
   i_lt_j_mult_min -> (i < j_mult_min)
   i_lt_j_mult_min_1 -> (i < j_mult_min_1)
   a = (abs_i_mod_j_eq_0 AND i_lt_j_mult_min)
   b = (abs_i_mod_j_ne_0 AND i_lt_j_mult_min_1)
   c -> (a OR b).  */

static tree
divCeilOverflowNegPos (location_t location, tree i, tree j, tree lowest, tree min, tree max)
{
  tree abs_i = m2expr_BuildAbs (location, i);
  tree abs_i_mod_j = m2expr_BuildModFloor (location, abs_i, j, false);
  tree abs_i_mod_j_eq_0 = m2expr_BuildEqualToZero (location, abs_i_mod_j, lowest, min, max);
  tree abs_i_mod_j_ne_0 = m2expr_BuildNotEqualToZero (location, abs_i_mod_j, lowest, min, max);
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree j_mult_min_1 = m2expr_BuildPostDec (location, j_mult_min);
  tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, j_mult_min);
  tree i_lt_j_mult_min_1 = m2expr_BuildLessThan (location, i, j_mult_min_1);
  tree a = m2expr_BuildTruthAndIf (location, abs_i_mod_j_eq_0, i_lt_j_mult_min);
  tree b = m2expr_BuildTruthAndIf (location, abs_i_mod_j_ne_0, i_lt_j_mult_min_1);
  tree c = m2expr_BuildTruthOrIf (location, a, b);
  return c;
}


/* divCeilOverflowNegNeg precondition:  i, j are in range values and both < 0.
   Postcondition:  TRUE is returned if i divceil j will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN ((max <= 0) OR           (* signs will cause overflow.  *)
           (* check for underflow.  *)
           ((ABS (i) MOD ABS (j) = 0) AND (i >= j * min)) OR
           ((ABS (i) MOD ABS (j) # 0) AND (i >= j * min - 1)) OR
           (* check for overflow.  *)
           (((ABS (i) MOD max) = 0) AND (ABS (i) DIV max > ABS (j))) OR
           (((ABS (i) MOD max) # 0) AND (ABS (i) DIV max > ABS (j) + 1)))

  max_lte_0 -> (max <= 0)
  abs_i -> (ABS (i))
  abs_j -> (ABS (j))
  abs_i_mod_abs_j -> (abs_i MOD abs_j)
  abs_i_mod_abs_j_eq_0 -> (abs_i_mod_abs_j = 0)
  abs_i_mod_abs_j_ne_0 -> (abs_i_mod_abs_j # 0)
  j_mult_min -> (j * min)
  j_mult_min_1 -> (j_mult_min - 1)
  i_ge_j_mult_min -> (i >= j_mult_min)
  i_ge_j_mult_min_1 -> (i >= j_mult_min_1)
  abs_i_mod_max -> (abs_i mod max)
  abs_i_div_max -> (abs_i DIVfloor max)
  abs_j_1 -> (abs_j + 1)
  abs_i_mod_max_eq_0 -> (abs_i_mod_max = 0)
  abs_i_mod_max_ne_0 -> (abs_i_mod_max # 0)
  abs_i_div_max_gt_abs_j -> (abs_i_div_max > abs_j)
  abs_i_div_max_gt_abs_j_1 -> (abs_i_div_max > abs_j_1)

  a -> (abs_i_mod_abs_j_eq_0 AND i_ge_j_mult_min)
  b -> (abs_i_mod_abs_j_ne_0 AND i_ge_j_mult_min_1)
  c -> (abs_i_mod_max_eq_0 AND abs_i_div_max_gt_abs_j)
  d -> (abs_i_mod_max_ne_0 AND abs_i_div_max_gt_abs_j_1)
  e -> (a OR b OR c OR d)
  return max_lte_0 OR e.  */

static tree
divCeilOverflowNegNeg (location_t location, tree i, tree j, tree lowest,
		       tree min, tree max)
{
  tree max_lte_0 = m2expr_BuildLessThanOrEqualZero (location, max, lowest, min, max);
  tree abs_i = m2expr_BuildAbs (location, i);
  tree abs_j = m2expr_BuildAbs (location, j);
  tree abs_i_mod_abs_j = m2expr_BuildModFloor (location, abs_i, abs_j, false);
  tree abs_i_mod_abs_j_eq_0 = m2expr_BuildEqualToZero (location, abs_i_mod_abs_j,
						       lowest, min, max);
  tree abs_i_mod_abs_j_ne_0 = m2expr_BuildNotEqualToZero (location, abs_i_mod_abs_j,
							  lowest, min, max);
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree j_mult_min_1 = m2expr_BuildPostDec (location, j_mult_min);
  tree i_ge_j_mult_min = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min);
  tree i_ge_j_mult_min_1 = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min_1);
  tree abs_i_mod_max = m2expr_BuildModFloor (location, abs_i, max, false);
  tree abs_i_div_max = m2expr_BuildDivFloor (location, abs_i, max, false);
  tree abs_j_1 = m2expr_BuildPostInc (location, abs_j);
  tree abs_i_mod_max_eq_0 = m2expr_BuildEqualToZero (location, abs_i_mod_max, lowest, min, max);
  tree abs_i_mod_max_ne_0 = m2expr_BuildNotEqualToZero (location, abs_i_mod_max, lowest, min, max);
  tree abs_i_div_max_gt_abs_j = m2expr_BuildGreaterThan (location,  abs_i_div_max, abs_j);
  tree abs_i_div_max_gt_abs_j_1 = m2expr_BuildGreaterThan (location, abs_i_div_max, abs_j_1);

  tree a = m2expr_BuildTruthAndIf (location, abs_i_mod_abs_j_eq_0, i_ge_j_mult_min);
  tree b = m2expr_BuildTruthAndIf (location, abs_i_mod_abs_j_ne_0, i_ge_j_mult_min_1);
  tree c = m2expr_BuildTruthAndIf (location, abs_i_mod_max_eq_0, abs_i_div_max_gt_abs_j);
  tree d = m2expr_BuildTruthAndIf (location, abs_i_mod_max_ne_0, abs_i_div_max_gt_abs_j_1);
  tree e = m2expr_Build4TruthOrIf (location, a, b, c, d);
  return m2expr_BuildTruthOrIf (location, max_lte_0, e);
}


/* divCeilOverflowCases, precondition:  i, j are in range values.
   Postcondition:  TRUE is returned if i divceil will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN (((i > 0) AND (j > 0) AND divCeilOverflowPosPos (i, j)) OR
           ((i < 0) AND (j < 0) AND divCeilOverflowNegNeg (i, j)) OR
           ((i > 0) AND (j < 0) AND divCeilOverflowPosNeg (i, j)) OR
           ((i < 0) AND (j > 0) AND divCeilOverflowNegPos (i, j)))

   a -> ((i > 0) AND (j > 0) AND divCeilOverflowPosPos (i, j))
   b -> ((i < 0) AND (j < 0) AND divCeilOverflowNegNeg (i, j))
   c -> ((i > 0) AND (j < 0) AND divCeilOverflowPosNeg (i, j))
   d -> ((i < 0) AND (j > 0) AND divCeilOverflowNegPos (i, j))

   RETURN a AND b AND c AND d.  */

static tree
divCeilOverflowCases (location_t location, tree i, tree j, tree lowest,
		      tree min, tree max)
{
  tree i_gt_zero = m2expr_BuildGreaterThanZero (location, i, lowest, min, max);
  tree j_gt_zero = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
  tree i_lt_zero = m2expr_BuildLessThanZero (location, i, lowest, min, max);
  tree j_lt_zero = m2expr_BuildLessThanZero (location, j, lowest, min, max);
  tree a = m2expr_Build3TruthAndIf (location, i_gt_zero, j_gt_zero,
				    divCeilOverflowPosPos (location, i, j, lowest, min, max));
  tree b = m2expr_Build3TruthAndIf (location, i_lt_zero, j_lt_zero,
				    divCeilOverflowNegNeg (location, i, j, lowest, min, max));
  tree c = m2expr_Build3TruthAndIf (location, i_gt_zero, j_lt_zero,
				    divCeilOverflowPosNeg (location, i, j, lowest, min, max));
  tree d = m2expr_Build3TruthAndIf (location, i_lt_zero, j_gt_zero,
				    divCeilOverflowNegPos (location, i, j, lowest, min, max));
  return m2expr_Build4TruthOrIf (location, a, b, c, d);
}


/* checkWholeDivCeilOverflow check to see whether i DIV_CEIL j will overflow
   an integer.  A handbuilt expression of trees implementing:

   RETURN ((j = 0) OR     (* division by zero.  *)
           (maxT < 0) OR  (* both inputs are < 0 and max is < 0,
                             therefore error.  *)
           ((i # 0) AND   (* first operand is legally zero,
                             result is also legally zero.  *)
            divCeilOverflowCases (i, j)))

   using the following subexpressions:

   j_eq_zero -> (j == 0)
   max_lt_zero -> (max < 0)
   i_ne_zero -> (i # 0).  */

static tree
checkWholeDivCeilOverflow (location_t location, tree i, tree j, tree lowest,
			   tree min, tree max)
{
  tree j_eq_zero = m2expr_BuildEqualToZero (location, j, lowest, min, max);
  tree max_lt_zero = m2expr_BuildLessThanZero (location, max, lowest, min, max);
  tree i_ne_zero = m2expr_BuildNotEqualToZero (location, i, lowest, min, max);
  tree j_lt_zero;
  tree rhs = m2expr_BuildTruthAndIf (location,
				     i_ne_zero,
				     divCeilOverflowCases (location,
							   i, j, lowest, min, max));

  if (M2Options_GetISO ())
    j_lt_zero = m2expr_FoldAndStrip (m2expr_BuildLessThanZero (location, j, lowest, min, max));
  else
    j_lt_zero = m2expr_GetIntegerZero (location);
  j_eq_zero = m2expr_FoldAndStrip (j_eq_zero);
  max_lt_zero = m2expr_FoldAndStrip (max_lt_zero);
  i_ne_zero = m2expr_FoldAndStrip (i_ne_zero);
  rhs = m2expr_FoldAndStrip (rhs);

  tree condition = m2expr_Build4TruthOrIf (location, j_eq_zero, max_lt_zero, rhs, j_lt_zero);
  tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value ceil division will cause a range overflow");
  return t;
}


/* checkWholeModTruncOverflow, the GCC tree.def defines TRUNC_MOD_EXPR to return
   the remainder which has the same sign as the dividend.  In ISO Modula-2 the
   divisor must never be negative (or zero).  The pseudo code for implementing these
   checks is given below:

   IF j = 0
   THEN
      RETURN TRUE   (* division by zero.  *)
   ELSIF j < 0
   THEN
      RETURN TRUE   (* modulus and division by negative (rhs) not allowed in ISO Modula-2.  *)
   ELSIF i = 0
   THEN
      RETURN FALSE  (* must be legal as result is same as operand.  *)
   ELSIF i > 0
   THEN
      (* test for:  i MOD j < minT  *)
      IF j > i
      THEN
         RETURN FALSE
      END ;
      RETURN i - ((i DIV j) * j) < minT
   ELSIF i < 0
   THEN
      (* the result will always be positive and less than i, given that j is less than zero
         we know that minT must be < 0 as well and therefore the result of i MOD j will
         never underflow.  *)
      RETURN FALSE
   END ;
   RETURN FALSE

   which can be converted into a large expression:

   RETURN (j = 0) OR ((j < 0) AND ISO) OR
          ((i # 0) AND (j <= i) AND (i - ((i DIVtrunc j) * j) < minT)

   and into GCC trees:

   c1 ->  (j = 0)
   c2 ->  (j < 0)  (* only called from ISO or PIM4 or -fpositive-mod-floor  *)
   c3 ->  (i # 0)
   c4 ->  (j <= i)
   c6 ->  (i DIVtrunc j)
   c7 ->  (i - (c6 * j))
   c5 ->  c7 < minT

   t -> (c1 OR c2 OR
        (c3 AND c4 AND c5)).  */

static tree
checkWholeModTruncOverflow (location_t location, tree i, tree j, tree lowest,
			    tree min, tree max)
{
  tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
  tree c2 = m2expr_BuildLessThanZero (location, j, lowest, min, max);
  tree c3 = m2expr_BuildNotEqualToZero (location, i, lowest, min, max);
  tree c4 = m2expr_BuildLessThanOrEqual (location, j, i);
  tree c6 = m2expr_BuildDivTrunc (location, i, j, false);
  tree c7 = m2expr_BuildSub (location, i, m2expr_BuildMult (location, c6, j, false), false);
  tree c5 = m2expr_BuildLessThan (location, c7, min);
  tree c8 = m2expr_Build3TruthAndIf (location, c3, c4, c5);
  tree condition = m2expr_Build3TruthOrIf (location, c1, c2, c8);
  tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value trunc modulus will cause a range overflow");
  return t;
}


/* checkWholeModCeilOverflow, the GCC tree.def defines CEIL_MOD_EXPR to return
   the remainder which has the same opposite of the divisor.  In gm2 this is
   only called when the divisor is negative.  The pseudo code for implementing
   these checks is given below:

   IF j = 0
   THEN
      RETURN TRUE   (* division by zero.  *)
   END ;
   t := i - j * divceil (i, j) ;
   printf ("t = %d, i = %d, j = %d, %d / %d = %d\n",
           t, i, j, i, j, divceil (i, j));
   RETURN NOT ((t >= minT) AND (t <= maxT))

   which can be converted into the expression:

   t := i - j * divceil (i, j) ;
   RETURN (j = 0) OR (NOT ((t >= minT) AND (t <= maxT)))

   and into GCC trees:

   c1 ->  (j = 0)
   c2 ->  (i - j)
   c3 ->  (i DIVceil j)
   t ->   (c2 * c3)
   c4 ->  (t >= minT)
   c5 ->  (t <= maxT)
   c6 ->  (c4 AND c5)
   c7 ->  (NOT c6)
   c8 ->  (c1 OR c7)
   return c8.  */

static tree
checkWholeModCeilOverflow (location_t location,
			   tree i, tree j, tree lowest,
			   tree min, tree max)
{
  tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
  tree c2 = m2expr_BuildSub (location, i, j, false);
  tree c3 = m2expr_BuildDivCeil (location, i, j, false);
  tree t  = m2expr_BuildMult (location, c2, c3, false);
  tree c4 = m2expr_BuildGreaterThanOrEqual (location, t, min);
  tree c5 = m2expr_BuildLessThanOrEqual (location, t, max);
  tree c6 = m2expr_BuildTruthAndIf (location, c4, c5);
  tree c7 = m2expr_BuildTruthNot (location, c6);
  tree condition = m2expr_BuildTruthOrIf (location, c1, c7);
  tree s = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value ceil modulus will cause a range overflow");
  return s;
}


/* checkWholeModFloorOverflow, the GCC tree.def defines FLOOR_MOD_EXPR to return
   the remainder which has the same sign as the divisor.  In gm2 this is
   only called when the divisor is positive.  The pseudo code for implementing
   these checks is given below:

   IF j = 0
   THEN
      RETURN TRUE   (* division by zero.  *)
   END ;
   t := i - j * divfloor (i, j) ;
   printf ("t = %d, i = %d, j = %d, %d / %d = %d\n",
           t, i, j, i, j, divfloor (i, j));
   RETURN NOT ((t >= minT) AND (t <= maxT))

   which can be converted into the expression:

   t := i - j * divfloor (i, j) ;
   RETURN (j = 0) OR (NOT ((t >= minT) AND (t <= maxT)))

   and into GCC trees:

   c1 ->  (j = 0)
   c2 ->  (i - j)
   c3 ->  (i DIVfloor j)
   t ->   (c2 * c3)
   c4 ->  (t >= minT)
   c5 ->  (t <= maxT)
   c6 ->  (c4 AND c5)
   c7 ->  (NOT c6)
   c8 ->  (c1 OR c7)
   return c8.  */

static tree
checkWholeModFloorOverflow (location_t location,
			    tree i, tree j, tree lowest,
			    tree min, tree max)
{
  tree c1 = m2expr_BuildEqualToZero (location, j, lowest, min, max);
  tree c2 = m2expr_BuildSub (location, i, j, false);
  tree c3 = m2expr_BuildDivFloor (location, i, j, false);
  tree t  = m2expr_BuildMult (location, c2, c3, false);
  tree c4 = m2expr_BuildGreaterThanOrEqual (location, t, min);
  tree c5 = m2expr_BuildLessThanOrEqual (location, t, max);
  tree c6 = m2expr_BuildTruthAndIf (location, c4, c5);
  tree c7 = m2expr_BuildTruthNot (location, c6);
  tree condition = m2expr_BuildTruthOrIf (location, c1, c7);
  tree s = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value floor modulus will cause a range overflow");
  return s;
}


#if 0
/*  The following is a Modula-2 implementation of the C tree node code
    this code has been hand translated into GCC trees.  */

(*
   divFloorOverflow2 - returns true if an overflow will occur
                       if i divfloor j is performed.
*)

PROCEDURE divFloorOverflow (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN ((j = 0) OR     (* division by zero.  *)
           (maxT < 0) OR  (* both inputs are < 0 and max is < 0,
                             therefore error.  *)
                          (* --fixme-- remember here to also check
                             if ISO M2 dialect and j < 0
                             which will also generate an error.  *)
           ((i # 0) AND   (* first operand is legally zero,
                             result is also legally zero.  *)
            divFloorOverflowCases (i, j)))
END divFloorOverflow ;


(*
   divFloorOverflowCases - precondition:  i, j are in range values.
                           postcondition:  true is returned if i divfloor will
                                           result in an overflow/underflow.
*)

PROCEDURE divFloorOverflowCases (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN (((i > 0) AND (j > 0) AND divFloorOverflowPosPos (i, j)) OR
           ((i < 0) AND (j < 0) AND divFloorOverflowNegNeg (i, j)) OR
           ((i > 0) AND (j < 0) AND divFloorOverflowPosNeg (i, j)) OR
           ((i < 0) AND (j > 0) AND divFloorOverflowNegPos (i, j)))
END divFloorOverflowCases ;


(*
   divFloorOverflowPosPos - precondition:  lhs, rhs are legal and are both >= 0.
                            postcondition:  true is returned if lhs divfloor rhs will
                                            result in an overflow/underflow.
*)

PROCEDURE divFloorOverflowPosPos (lhs, rhs: INTEGER) : BOOLEAN ;
BEGIN
   RETURN multMinOverflow (rhs) OR (lhs < rhs * min)
END divFloorOverflowPosPos ;


(*
   divFloorOverflowNegNeg - precondition:  i, j are in range values and both < 0.
                            postcondition:  true is returned if i divfloor will
                                            result in an overflow/underflow.
*)

PROCEDURE divFloorOverflowNegNeg (i, j: INTEGER) : BOOLEAN ;
BEGIN
   RETURN ((maxT <= 0) OR           (* signs will cause overflow.  *)
           (* check for underflow.  *)
           (i >= j * minT) OR
           (* check for overflow.  *)
           (ABS (i) DIV maxT > ABS (j)))
END divFloorOverflowNegNeg ;


(*
   divFloorOverflowNegPos - precondition:  i, j are in range values.  i < 0, j >= 0.
                            postcondition:  true is returned if i divfloor will
                                            result in an overflow/underflow.
*)

PROCEDURE divFloorOverflowNegPos (i, j: INTEGER) : BOOLEAN ;
BEGIN
   (* easier than might be initially expected.  We know minT < 0 and maxT > 0.
      We know the result will be negative and therefore we only need to test
      against minT.  *)
   RETURN i < j * minT
END divFloorOverflowNegPos ;


(*
   divFloorOverflowPosNeg - precondition:  i, j are in range values.  i >= 0, j < 0.
                           postcondition:  true is returned if i divfloor will
                                           result in an overflow/underflow.
*)

PROCEDURE divFloorOverflowPosNeg (i, j: INTEGER) : BOOLEAN ;
BEGIN
   (* easier than might be initially expected.  We know minT < 0 and maxT > 0.
      We know the result will be negative and therefore we only need to test
      against minT.  *)
   RETURN i >= j * minT - j  (* is safer than i > j * minT -1 *)
END divFloorOverflowPosNeg ;
#endif


/* divFloorOverflowPosPos, precondition:  i, j are legal and are both >= 0.
   Postcondition:  true is returned if i divfloor will result in an overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN i < j * min

   j_mult_min -> (j * min)
   RETURN i < j_mult_min.  */

static tree
divFloorOverflowPosPos (location_t location, tree i, tree j, tree min)
{
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, j_mult_min);
  return i_lt_j_mult_min;
}


/* divFloorOverflowNegNeg precondition:  i, j are in range values and both < 0.
   Postcondition:  true is returned if i divfloor j will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN ((maxT <= 0) OR           (* signs will cause overflow.  *)
           (* check for underflow.  *)
           (i >= j * min) OR
           (* check for overflow.  *)
           (ABS (i) DIV max > ABS (j)))

  max_lte_0 -> (max <= 0)
  abs_i -> (ABS (i))
  abs_j -> (ABS (j))
  j_mult_min -> (j * min)
  i_ge_j_mult_min -> (i >= j_mult_min)
  abs_i_div_max -> (abs_i divfloor max)
  abs_i_div_max_gt_abs_j -> (abs_i_div_max > abs_j)

  return max_lte_0 OR
         i_ge_j_mult_min OR
         abs_i_div_max_gt_abs_j.  */

static tree
divFloorOverflowNegNeg (location_t location, tree i, tree j, tree lowest,
			tree min, tree max)
{
  tree max_lte_0 = m2expr_BuildLessThanOrEqualZero (location, max, lowest, min, max);
  tree abs_i = m2expr_BuildAbs (location, i);
  tree abs_j = m2expr_BuildAbs (location, j);
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree i_ge_j_mult_min = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min);
  tree abs_i_div_max = m2expr_BuildDivFloor (location, abs_i, max, false);
  tree abs_i_div_max_gt_abs_j = m2expr_BuildGreaterThan (location,  abs_i_div_max, abs_j);

  return m2expr_Build3TruthOrIf (location, max_lte_0, i_ge_j_mult_min, abs_i_div_max_gt_abs_j);
}


/* divFloorOverflowPosNeg precondition:  i, j are in range values and i >=0, j < 0.
   Postcondition:  true is returned if i divfloor j will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN i >= j * min - j  (* is safer than i > j * min -1 *)

   j_mult_min -> (j * min)
   j_mult_min_sub_j -> (j_mult_min - j)
   i_ge_j_mult_min_sub_j -> (i >= j_mult_min_sub_j)

   return i_ge_j_mult_min_sub_j.  */

static tree
divFloorOverflowPosNeg (location_t location, tree i, tree j, tree min)
{
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree j_mult_min_sub_j = m2expr_BuildSub (location, j_mult_min, j, false);
  tree i_ge_j_mult_min_sub_j = m2expr_BuildGreaterThanOrEqual (location, i, j_mult_min_sub_j);
  return i_ge_j_mult_min_sub_j;
}


/* divFloorOverflowNegPos precondition:  i, j are in range values and i < 0, j > 0.
   Postcondition:  true is returned if i divfloor j will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN i < j * min

   j_mult_min -> (j * min)
   RETURN i < j_mult_min.  */

static tree
divFloorOverflowNegPos (location_t location, tree i, tree j, tree min)
{
  tree j_mult_min = m2expr_BuildMult (location, j, min, false);
  tree i_lt_j_mult_min = m2expr_BuildLessThan (location, i, j_mult_min);
  return i_lt_j_mult_min;
}


/* divFloorOverflowCases, precondition:  i, j are in range values.
   Postcondition:  true is returned if i divfloor will result in an
   overflow/underflow.

   A handbuilt expression of trees implementing:

   RETURN (((i > 0) AND (j > 0) AND divFloorOverflowPosPos (i, j)) OR
           ((i < 0) AND (j < 0) AND divFloorOverflowNegNeg (i, j)) OR
           ((i > 0) AND (j < 0) AND divFloorOverflowPosNeg (i, j)) OR
           ((i < 0) AND (j > 0) AND divFloorOverflowNegPos (i, j)))

   a -> ((i > 0) AND (j > 0) AND divFloorOverflowPosPos (i, j))
   b -> ((i < 0) AND (j < 0) AND divFloorOverflowNegNeg (i, j))
   c -> ((i > 0) AND (j < 0) AND divFloorOverflowPosNeg (i, j))
   d -> ((i < 0) AND (j > 0) AND divFloorOverflowNegPos (i, j))

   RETURN a AND b AND c AND d.  */

static tree
divFloorOverflowCases (location_t location, tree i, tree j, tree lowest,
		      tree min, tree max)
{
  tree i_gt_zero = m2expr_BuildGreaterThanZero (location, i, lowest, min, max);
  tree j_gt_zero = m2expr_BuildGreaterThanZero (location, j, lowest, min, max);
  tree i_lt_zero = m2expr_BuildLessThanZero (location, i, lowest, min, max);
  tree j_lt_zero = m2expr_BuildLessThanZero (location, j, lowest, min, max);
  tree a = m2expr_Build3TruthAndIf (location, i_gt_zero, j_gt_zero,
				    divFloorOverflowPosPos (location, i, j, min));
  tree b = m2expr_Build3TruthAndIf (location, i_lt_zero, j_lt_zero,
				    divFloorOverflowNegNeg (location, i, j, lowest, min, max));
  tree c = m2expr_Build3TruthAndIf (location, i_gt_zero, j_lt_zero,
				    divFloorOverflowPosNeg (location, i, j, min));
  tree d = m2expr_Build3TruthAndIf (location, i_lt_zero, j_gt_zero,
				    divFloorOverflowNegPos (location, i, j, min));
  return m2expr_Build4TruthOrIf (location, a, b, c, d);
}


/* checkWholeDivFloorOverflow check to see whether i DIV_FLOOR j will overflow
   an integer.  A handbuilt expression of trees implementing:

   RETURN ((j = 0) OR     (* division by zero.  *)
           (maxT < 0) OR  (* both inputs are < 0 and max is < 0,
                             therefore error.  *)
                          (* we also check
                             if ISO M2 dialect and j < 0
                             which will also generate an error.  *)
           ((i # 0) AND   (* first operand is legally zero,
                             result is also legally zero.  *)
            divFloorOverflowCases (i, j)))

  using the following subexpressions:

   j_eq_zero -> (j == 0)
   max_lt_zero -> (max < 0)
   i_ne_zero -> (i # 0).  */

static tree
checkWholeDivFloorOverflow (location_t location, tree i, tree j, tree lowest,
			    tree min, tree max)
{
  tree j_eq_zero = m2expr_BuildEqualToZero (location, j, lowest, min, max);
  tree max_lt_zero = m2expr_BuildLessThanZero (location, max, lowest, min, max);
  tree i_ne_zero = m2expr_BuildNotEqualToZero (location, i, lowest, min, max);
  tree j_lt_zero;
  tree rhs = m2expr_BuildTruthAndIf (location,
				     i_ne_zero,
				     divFloorOverflowCases (location,
							    i, j, lowest, min, max));

  if (M2Options_GetISO ())
    /* ISO Modula-2 raises an exception if the right hand operand is < 0.  */
    j_lt_zero = m2expr_FoldAndStrip (m2expr_BuildLessThanZero (location, j, lowest, min, max));
  else
    j_lt_zero = m2expr_GetIntegerZero (location);
  j_eq_zero = m2expr_FoldAndStrip (j_eq_zero);
  max_lt_zero = m2expr_FoldAndStrip (max_lt_zero);
  i_ne_zero = m2expr_FoldAndStrip (i_ne_zero);
  rhs = m2expr_FoldAndStrip (rhs);

  tree condition = m2expr_Build4TruthOrIf (location, j_eq_zero, max_lt_zero, rhs, j_lt_zero);
  tree t = M2Range_BuildIfCallWholeHandlerLoc (location, condition,
					       get_current_function_name (),
               "whole value floor division will cause a range overflow");
  return t;
}

/* checkWholeOverflow check to see if the binary operators will overflow
   ordinal types.  */

static tree
m2expr_checkWholeOverflow (location_t location, enum tree_code code, tree op1,
                           tree op2, tree lowest, tree min, tree max)
{
  if (M2Options_GetWholeValueCheck () && (min != NULL))
    {
      lowest = m2tree_skip_type_decl (lowest);
      op1 = fold_convert_loc (location, lowest, op1);
      op2 = fold_convert_loc (location, lowest, op2);

      switch (code)
        {
        case PLUS_EXPR:
          return checkWholeAddOverflow (location, op1, op2, lowest, min, max);
        case MINUS_EXPR:
          return checkWholeSubOverflow (location, op1, op2, lowest, min, max);
        case MULT_EXPR:
          return checkWholeMultOverflow (location, op1, op2, lowest, min, max);
	case TRUNC_DIV_EXPR:
	  return checkWholeDivTruncOverflow (location, op1, op2, lowest, min, max);
	case CEIL_DIV_EXPR:
	  return checkWholeDivCeilOverflow (location, op1, op2, lowest, min, max);
	case FLOOR_DIV_EXPR:
	  return checkWholeDivFloorOverflow (location, op1, op2, lowest, min, max);
	case TRUNC_MOD_EXPR:
	  return checkWholeModTruncOverflow (location, op1, op2, lowest, min, max);
	case CEIL_MOD_EXPR:
	  return checkWholeModCeilOverflow (location, op1, op2, lowest, min, max);
	case FLOOR_MOD_EXPR:
	  return checkWholeModFloorOverflow (location, op1, op2, lowest, min, max);
        default:
	  return NULL;
        }
    }
  return NULL;
}

/* checkRealOverflow if we have enabled real value checking then
   generate an overflow check appropriate to the tree code being used.  */

static void
m2expr_checkRealOverflow (location_t location, enum tree_code code,
                          tree result)
{
  if (M2Options_GetFloatValueCheck ())
    {
      tree condition = m2expr_BuildEqualTo (
          location, m2builtins_BuiltInIsfinite (location, result),
          m2expr_GetIntegerZero (location));
      switch (code)
        {
        case PLUS_EXPR:
          m2type_AddStatement (location,
                               M2Range_BuildIfCallRealHandlerLoc (
				   location, condition,
				   get_current_function_name (),
		   "floating point + has caused an overflow"));
          break;
        case MINUS_EXPR:
          m2type_AddStatement (location,
                               M2Range_BuildIfCallRealHandlerLoc (
                                   location, condition,
				   get_current_function_name (),
		   "floating point - has caused an overflow"));
          break;
        case RDIV_EXPR:
        case FLOOR_DIV_EXPR:
        case CEIL_DIV_EXPR:
        case TRUNC_DIV_EXPR:
          m2type_AddStatement (location,
                               M2Range_BuildIfCallRealHandlerLoc (
                                   location, condition,
				   get_current_function_name (),
		   "floating point / has caused an overflow"));
          break;
        case MULT_EXPR:
          m2type_AddStatement (location,
                               M2Range_BuildIfCallRealHandlerLoc (
                                   location, condition,
				   get_current_function_name (),
		   "floating point * has caused an overflow"));
          break;
        case NEGATE_EXPR:
          m2type_AddStatement (
              location, M2Range_BuildIfCallRealHandlerLoc (
                            location, condition,
			    get_current_function_name (),
		   "floating point unary - has caused an overflow"));
        default:
          break;
        }
    }
}

/* build_binary_op, a wrapper for the lower level build_binary_op
   above.  */

tree
m2expr_build_binary_op_check (location_t location, enum tree_code code,
                              tree op1, tree op2, bool needconvert, tree lowest,
                              tree min, tree max)
{
  tree type1, type2, result;
  tree check = NULL;

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  type1 = m2tree_skip_type_decl (TREE_TYPE (op1));
  type2 = m2tree_skip_type_decl (TREE_TYPE (op2));

  m2assert_AssertLocation (location);

  if (code == PLUS_EXPR)
    {
      if (POINTER_TYPE_P (type1))
        {
          op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
          return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op1),
                                  op1, op2);
        }
      else if (POINTER_TYPE_P (type2))
        {
          op1 = fold_convert_loc (location, sizetype, unshare_expr (op1));
          return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op2),
                                  op2, op1);
        }
    }
  if (code == MINUS_EXPR)
    {
      if (POINTER_TYPE_P (type1))
        {
          op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
          op2 = fold_build1_loc (location, NEGATE_EXPR, sizetype, op2);
          return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op1),
                                  op1, op2);
        }
      else if (POINTER_TYPE_P (type2))
        {
          op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
          op2 = fold_build1_loc (location, NEGATE_EXPR, sizetype, op2);
          op1 = fold_convert_loc (location, sizetype, unshare_expr (op1));
          return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op2),
                                  op2, op1);
        }
    }

  if ((code != LSHIFT_EXPR) && (code != RSHIFT_EXPR) && (code != LROTATE_EXPR)
      && (code == RROTATE_EXPR))
    if (type1 != type2)
      error_at (location, "not expecting different types to binary operator");

  if ((TREE_CODE (type1) != REAL_TYPE) && (min != NULL))
    check = m2expr_checkWholeOverflow (location, code, op1, op2, lowest, min, max);

  result = build_binary_op (location, code, op1, op2, needconvert);
  if (check != NULL)
    result = build2 (COMPOUND_EXPR, TREE_TYPE (result), check, result);

  if (SCALAR_FLOAT_TYPE_P (type1))
    m2expr_checkRealOverflow (location, code, result);
  return result;
}

/* build_binary_op, a wrapper for the lower level build_binary_op
   above.  */

tree
m2expr_build_binary_op (location_t location, enum tree_code code, tree op1,
                        tree op2, int convert)
{
  return m2expr_build_binary_op_check (location, code, op1, op2, convert, NULL,
                                       NULL, NULL);
}

/* BuildAddAddress return an expression op1+op2 where op1 is a
   pointer type and op2 is not a pointer type.  */

tree
m2expr_BuildAddAddress (location_t location, tree op1, tree op2)
{
  tree type1, type2;

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  type1 = m2tree_skip_type_decl (TREE_TYPE (op1));
  type2 = m2tree_skip_type_decl (TREE_TYPE (op2));

  m2assert_AssertLocation (location);
  ASSERT_CONDITION (POINTER_TYPE_P (type1));
  ASSERT_CONDITION (!POINTER_TYPE_P (type2));

  op2 = fold_convert_loc (location, sizetype, unshare_expr (op2));
  return fold_build2_loc (location, POINTER_PLUS_EXPR, TREE_TYPE (op1),
                          m2expr_FoldAndStrip (op1),
                          m2expr_FoldAndStrip (op2));
}

/* BuildNegateCheck builds a negate tree.  */

tree
m2expr_BuildNegateCheck (location_t location, tree arg, tree lowest, tree min,
                         tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  arg = m2expr_FoldAndStrip (arg);
  arg = CheckAddressToCardinal (location, arg);

  t = m2expr_build_unary_op_check (location, NEGATE_EXPR, arg, lowest, min,
                                   max);
  return m2expr_FoldAndStrip (t);
}

/* BuildNegate build a negate expression and returns the tree.  */

tree
m2expr_BuildNegate (location_t location, tree op1, bool needconvert)
{
  m2assert_AssertLocation (location);
  op1 = m2expr_FoldAndStrip (op1);
  op1 = CheckAddressToCardinal (location, op1);

  return m2expr_build_unary_op (location, NEGATE_EXPR, op1, needconvert);
}

/* BuildSetNegate build a set negate expression and returns the tree.  */

tree
m2expr_BuildSetNegate (location_t location, tree op1, bool needconvert)
{
  m2assert_AssertLocation (location);

  return m2expr_build_binary_op (
      location, BIT_XOR_EXPR,
      m2convert_BuildConvert (location, m2type_GetWordType (),
                              m2expr_FoldAndStrip (op1), false),
      set_full_complement, needconvert);
}

/* BuildMult build a multiplication tree.  */

tree
m2expr_BuildMult (location_t location, tree op1, tree op2, bool needconvert)
{
  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  m2assert_AssertLocation (location);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  return m2expr_build_binary_op (location, MULT_EXPR, op1, op2, needconvert);
}

/* BuildMultCheck builds a multiplication tree.  */

tree
m2expr_BuildMultCheck (location_t location, tree op1, tree op2, tree lowest,
                       tree min, tree max)
{
  tree t;

  m2assert_AssertLocation (location);

  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);

  op1 = CheckAddressToCardinal (location, op1);
  op2 = CheckAddressToCardinal (location, op2);

  t = m2expr_build_binary_op_check (location, MULT_EXPR, op1, op2, false,
                                    lowest, min, max);
  return m2expr_FoldAndStrip (t);
}

/* testLimits return the number of bits required to represent:
   min..max if it matches the, type.  Otherwise NULL_TREE is returned.  */

static tree
testLimits (location_t location, tree type, tree min, tree max)
{
  m2assert_AssertLocation (location);

  if ((m2expr_CompareTrees (TYPE_MAX_VALUE (type), max) == 0)
      && (m2expr_CompareTrees (TYPE_MIN_VALUE (type), min) == 0))
    return m2expr_BuildMult (location, m2expr_GetSizeOf (location, type),
                             m2decl_BuildIntegerConstant (BITS_PER_UNIT),
                             false);
  return NULL_TREE;
}

/* noBitsRequired return the number of bits required to contain, values.  */

static tree
noBitsRequired (tree values)
{
  int bits = tree_floor_log2 (values);

  return m2decl_BuildIntegerConstant (bits + 1);
}

/* getMax return the result of max (a, b).  */

static tree
getMax (tree a, tree b)
{
  if (m2expr_CompareTrees (a, b) > 0)
    return a;
  else
    return b;
}

/* calcNbits return the smallest number of bits required to
   represent: min..max.  */

tree
m2expr_calcNbits (location_t location, tree min, tree max)
{
  int negative = false;
  tree t = testLimits (location, m2type_GetIntegerType (), min, max);

  m2assert_AssertLocation (location);

  if (t == NULL)
    t = testLimits (location, m2type_GetCardinalType (), min, max);

  if (t == NULL)
    {
      if (m2expr_CompareTrees (min, m2expr_GetIntegerZero (location)) < 0)
        {
          min = m2expr_BuildAdd (location, min,
                                 m2expr_GetIntegerOne (location), false);
          min = fold (m2expr_BuildNegate (location, min, false));
          negative = true;
        }
      if (m2expr_CompareTrees (max, m2expr_GetIntegerZero (location)) < 0)
        {
          max = fold (m2expr_BuildNegate (location, max, false));
          negative = true;
        }
      t = noBitsRequired (getMax (min, max));
      if (negative)
        t = m2expr_BuildAdd (location, t, m2expr_GetIntegerOne (location),
                             false);
    }
  return t;
}

/* BuildTBitSize return the minimum number of bits to represent type.
   This function is called internally by cc1gm2 to calculate the bits
   size of a type and is used to position record fields.  */

tree
m2expr_BuildTBitSize (location_t location, tree type)
{
  enum tree_code code = TREE_CODE (type);
  tree min;
  tree max;
  m2assert_AssertLocation (location);

  switch (code)
    {

    case TYPE_DECL:
      return m2expr_BuildTBitSize (location, TREE_TYPE (type));
    case INTEGER_TYPE:
    case ENUMERAL_TYPE:
      max = m2convert_BuildConvert (location, m2type_GetIntegerType (),
                                    TYPE_MAX_VALUE (type), false);
      min = m2convert_BuildConvert (location, m2type_GetIntegerType (),
                                    TYPE_MIN_VALUE (type), false);
      return m2expr_calcNbits (location, min, max);
    case BOOLEAN_TYPE:
      return m2expr_GetIntegerOne (location);
    default:
      return m2expr_BuildMult (location, m2expr_GetSizeOf (location, type),
                               m2decl_BuildIntegerConstant (BITS_PER_UNIT),
                               false);
    }
}

/* BuildSystemTBitSize return the minimum number of bits to represent type.
   This function is called when evaluating SYSTEM.TBITSIZE.  */

tree
m2expr_BuildSystemTBitSize (location_t location, tree type)
{
  enum tree_code code = TREE_CODE (type);
  m2assert_AssertLocation (location);
  if (code == TYPE_DECL)
    return m2expr_BuildTBitSize (location, TREE_TYPE (type));
  return TYPE_SIZE (type);
}

/* BuildSize build a SIZE function expression and returns the tree.  */

tree
m2expr_BuildSize (location_t location, tree op1,
                  bool needconvert ATTRIBUTE_UNUSED)
{
  m2assert_AssertLocation (location);
  return m2expr_GetSizeOf (location, op1);
}

/* BuildAddr return an expression which calculates the address of op1
   and returns the tree.  If use_generic is true then create a generic
   pointer type.  */

tree
m2expr_BuildAddr (location_t location, tree op1, bool use_generic)
{
  tree type = m2tree_skip_type_decl (TREE_TYPE (op1));
  tree ptrType = build_pointer_type (type);
  tree result;

  m2assert_AssertLocation (location);

  if (!gm2_mark_addressable (op1))
    error_at (location, "cannot take the address of this expression");

  if (use_generic)
    result = build1 (ADDR_EXPR, m2type_GetPointerType (), op1);
  else
    result = build1 (ADDR_EXPR, ptrType, op1);
  protected_set_expr_location (result, location);
  return result;
}

/* BuildOffset1 build and return an expression containing the number
   of bytes the field is offset from the start of the record structure.
   This function is the same as the above, except that it derives the
   record from the field and then calls BuildOffset.  */

tree
m2expr_BuildOffset1 (location_t location, tree field,
                     bool needconvert ATTRIBUTE_UNUSED)
{
  m2assert_AssertLocation (location);
  return m2expr_BuildOffset (location, DECL_CONTEXT (field), field,
                             needconvert);
}

/* determinePenultimateField return the field associated with the
   DECL_CONTEXT (field) within a record or varient.  The record, is a
   record/varient but it maybe an outer nested record to the field that
   we are searching.  Ie:

   record = RECORD x: CARDINAL ; y: RECORD field: CARDINAL ; END END ;

   determinePenultimateField (record, field) returns, y.  We are
   assurred that the chain of records leading to field will be unique as
   they are built on the fly to implement varient records.  */

static tree
determinePenultimateField (tree record, tree field)
{
  tree fieldlist = TYPE_FIELDS (record);
  tree x, r;

  for (x = fieldlist; x; x = TREE_CHAIN (x))
    {
      if (DECL_CONTEXT (field) == TREE_TYPE (x))
        return x;
      switch (TREE_CODE (TREE_TYPE (x)))
        {
        case RECORD_TYPE:
        case UNION_TYPE:
          r = determinePenultimateField (TREE_TYPE (x), field);
          if (r != NULL)
            return r;
          break;
        default:
          break;
        }
    }
  return NULL_TREE;
}

/* BuildOffset builds an expression containing the number of bytes
the field is offset from the start of the record structure.  The
expression is returned.  */

tree
m2expr_BuildOffset (location_t location, tree record, tree field,
                    bool needconvert ATTRIBUTE_UNUSED)
{
  m2assert_AssertLocation (location);

  if (DECL_CONTEXT (field) == record)
    return m2convert_BuildConvert (
        location, m2type_GetIntegerType (),
        m2expr_BuildAdd (
            location, DECL_FIELD_OFFSET (field),
            m2expr_BuildDivTrunc (location, DECL_FIELD_BIT_OFFSET (field),
                                  m2decl_BuildIntegerConstant (BITS_PER_UNIT),
                                  false),
            false),
        false);
  else
    {
      tree r1 = DECL_CONTEXT (field);
      tree r2 = determinePenultimateField (record, field);
      return m2convert_BuildConvert (
          location, m2type_GetIntegerType (),
          m2expr_BuildAdd (
              location, m2expr_BuildOffset (location, r1, field, needconvert),
              m2expr_BuildOffset (location, record, r2, needconvert), false),
          false);
    }
}

/* BuildLogicalOrAddress build a logical or expressions and return the tree. */

tree
m2expr_BuildLogicalOrAddress (location_t location, tree op1, tree op2,
                              bool needconvert)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (location, BIT_IOR_EXPR, op1, op2,
                                 needconvert);
}

/* BuildLogicalOr build a logical or expressions and return the tree.  */

tree
m2expr_BuildLogicalOr (location_t location, tree op1, tree op2,
                       bool needconvert)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, BIT_IOR_EXPR,
      m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
      m2convert_BuildConvert (location, m2type_GetWordType (), op2, false),
      needconvert);
}

/* BuildLogicalAnd build a logical and expression and return the tree.  */

tree
m2expr_BuildLogicalAnd (location_t location, tree op1, tree op2,
                        bool needconvert)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, BIT_AND_EXPR,
      m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
      m2convert_BuildConvert (location, m2type_GetWordType (), op2, false),
      needconvert);
}

/* BuildSymmetricalDifference build a logical xor expression and return the
 * tree.  */

tree
m2expr_BuildSymmetricDifference (location_t location, tree op1, tree op2,
                                 bool needconvert)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, BIT_XOR_EXPR,
      m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
      m2convert_BuildConvert (location, m2type_GetWordType (), op2, false),
      needconvert);
}

/* BuildLogicalDifference build a logical difference expression and
return the tree.  (op1 and (not op2)).  */

tree
m2expr_BuildLogicalDifference (location_t location, tree op1, tree op2,
                               bool needconvert)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, BIT_AND_EXPR,
      m2convert_BuildConvert (location, m2type_GetWordType (), op1, false),
      m2expr_BuildSetNegate (location, op2, needconvert), needconvert);
}

/* base_type returns the base type of an ordinal subrange, or the
type itself if it is not a subrange.  */

static tree
base_type (tree type)
{
  if (type == error_mark_node)
    return error_mark_node;

  /* Check for ordinal subranges.  */
  if (m2tree_IsOrdinal (type) && TREE_TYPE (type))
    type = TREE_TYPE (type);
  return TYPE_MAIN_VARIANT (type);
}

/* boolean_enum_to_unsigned convert a BOOLEAN_TYPE, t, or
   ENUMERAL_TYPE to an unsigned type.  */

static tree
boolean_enum_to_unsigned (location_t location, tree t)
{
  tree type = TREE_TYPE (t);

  if (TREE_CODE (base_type (type)) == BOOLEAN_TYPE)
    return m2convert_BuildConvert (location, unsigned_type_node, t, false);
  else if (TREE_CODE (base_type (type)) == ENUMERAL_TYPE)
    return m2convert_BuildConvert (location, unsigned_type_node, t, false);
  else
    return t;
}

/* check_for_comparison check to see if, op, is of type, badType.  If
   so then it returns op after it has been cast to, goodType.  op will
   be an array so we take the address and cast the contents.  */

static tree
check_for_comparison (location_t location, tree op, tree badType,
                      tree goodType)
{
  m2assert_AssertLocation (location);
  if (m2tree_skip_type_decl (TREE_TYPE (op)) == badType)
    /* Cannot compare array contents in m2expr_build_binary_op.  */
    return m2expr_BuildIndirect (
        location, m2expr_BuildAddr (location, op, false), goodType);
  return op;
}

/* convert_for_comparison return a tree which can be used as an
   argument during a comparison.  */

static tree
convert_for_comparison (location_t location, tree op)
{
  m2assert_AssertLocation (location);
  op = boolean_enum_to_unsigned (location, op);

  op = check_for_comparison (location, op, m2type_GetISOWordType (),
                             m2type_GetWordType ());
  op = check_for_comparison (location, op, m2type_GetM2Word16 (),
                             m2type_GetM2Cardinal16 ());
  op = check_for_comparison (location, op, m2type_GetM2Word32 (),
                             m2type_GetM2Cardinal32 ());
  op = check_for_comparison (location, op, m2type_GetM2Word64 (),
                             m2type_GetM2Cardinal64 ());

  return op;
}

/* BuildLessThan return a tree which computes <.  */

tree
m2expr_BuildLessThan (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, LT_EXPR, boolean_enum_to_unsigned (location, op1),
      boolean_enum_to_unsigned (location, op2), true);
}

/* BuildGreaterThan return a tree which computes >.  */

tree
m2expr_BuildGreaterThan (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, GT_EXPR, boolean_enum_to_unsigned (location, op1),
      boolean_enum_to_unsigned (location, op2), true);
}

/* BuildLessThanOrEqual return a tree which computes <.  */

tree
m2expr_BuildLessThanOrEqual (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, LE_EXPR, boolean_enum_to_unsigned (location, op1),
      boolean_enum_to_unsigned (location, op2), true);
}

/* BuildGreaterThanOrEqual return a tree which computes >=.  */

tree
m2expr_BuildGreaterThanOrEqual (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (
      location, GE_EXPR, boolean_enum_to_unsigned (location, op1),
      boolean_enum_to_unsigned (location, op2), true);
}

/* BuildEqualTo return a tree which computes =.  */

tree
m2expr_BuildEqualTo (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (location, EQ_EXPR,
                                 convert_for_comparison (location, op1),
                                 convert_for_comparison (location, op2), true);
}

/* BuildEqualNotTo return a tree which computes #.  */

tree
m2expr_BuildNotEqualTo (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_build_binary_op (location, NE_EXPR,
                                 convert_for_comparison (location, op1),
                                 convert_for_comparison (location, op2), true);
}

/* BuildIsSuperset return a tree which computes:  op1 & op2 == op2.  */

tree
m2expr_BuildIsSuperset (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_BuildEqualTo (
      location, op2, m2expr_BuildLogicalAnd (location, op1, op2, false));
}

/* BuildIsNotSuperset return a tree which computes: op1 & op2 != op2.  */

tree
m2expr_BuildIsNotSuperset (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_BuildNotEqualTo (
      location, op2, m2expr_BuildLogicalAnd (location, op1, op2, false));
}

/* BuildIsSubset return a tree which computes:  op1 & op2 == op1.  */

tree
m2expr_BuildIsSubset (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_BuildEqualTo (
      location, op1, m2expr_BuildLogicalAnd (location, op1, op2, false));
}

/* BuildIsNotSubset return a tree which computes: op1 & op2 != op1.  */

tree
m2expr_BuildIsNotSubset (location_t location, tree op1, tree op2)
{
  m2assert_AssertLocation (location);
  return m2expr_BuildNotEqualTo (
      location, op1, m2expr_BuildLogicalAnd (location, op1, op2, false));
}

/* BuildIfConstInVar generates: if constel in varset then goto label.  */

void
m2expr_BuildIfConstInVar (location_t location, tree type, tree varset,
                          tree constel, bool is_lvalue, int fieldno,
                          char *label)
{
  tree size = m2expr_GetSizeOf (location, type);
  m2assert_AssertLocation (location);

  ASSERT_BOOL (is_lvalue);
  if (m2expr_CompareTrees (
          size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
      <= 0)
    /* Small set size <= TSIZE(WORD).  */
    m2treelib_do_jump_if_bit (
        location, NE_EXPR,
        m2treelib_get_rvalue (location, varset, type, is_lvalue), constel,
        label);
  else
    {
      tree fieldlist = TYPE_FIELDS (type);
      tree field;

      for (field = fieldlist; (field != NULL) && (fieldno > 0);
           field = TREE_CHAIN (field))
        fieldno--;

      m2treelib_do_jump_if_bit (
          location, NE_EXPR,
          m2treelib_get_set_field_rhs (location, varset, field), constel,
          label);
    }
}

/* BuildIfConstInVar generates: if not (constel in varset) then goto label.  */

void
m2expr_BuildIfNotConstInVar (location_t location, tree type, tree varset,
                             tree constel, bool is_lvalue, int fieldno,
                             char *label)
{
  tree size = m2expr_GetSizeOf (location, type);

  m2assert_AssertLocation (location);

  ASSERT_BOOL (is_lvalue);
  if (m2expr_CompareTrees (
          size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
      <= 0)
    /* Small set size <= TSIZE(WORD).  */
    m2treelib_do_jump_if_bit (
        location, EQ_EXPR,
        m2treelib_get_rvalue (location, varset, type, is_lvalue), constel,
        label);
  else
    {
      tree fieldlist = TYPE_FIELDS (type);
      tree field;

      for (field = fieldlist; (field != NULL) && (fieldno > 0);
           field = TREE_CHAIN (field))
        fieldno--;

      m2treelib_do_jump_if_bit (
          location, EQ_EXPR,
          m2treelib_get_set_field_rhs (location, varset, field), constel,
          label);
    }
}

/* BuildIfVarInVar generates: if varel in varset then goto label.  */

void
m2expr_BuildIfVarInVar (location_t location, tree type, tree varset,
                        tree varel, bool is_lvalue, tree low,
                        tree high ATTRIBUTE_UNUSED, char *label)
{
  tree size = m2expr_GetSizeOf (location, type);
  /* Calculate the index from the first bit, ie bit 0 represents low value.  */
  tree index = m2expr_BuildSub (
      location, m2convert_BuildConvert (location, m2type_GetIntegerType (),
                                        varel, false),
      m2convert_BuildConvert (location, m2type_GetIntegerType (), low, false),
      false);

  m2assert_AssertLocation (location);

  if (m2expr_CompareTrees (
          size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
      <= 0)
    /* Small set size <= TSIZE(WORD).  */
    m2treelib_do_jump_if_bit (
        location, NE_EXPR,
        m2treelib_get_rvalue (location, varset, type, is_lvalue), index,
        label);
  else
    {
      tree p1 = m2treelib_get_set_address (location, varset, is_lvalue);
      /* Which word do we need to fetch?  */
      tree word_index = m2expr_FoldAndStrip (m2expr_BuildDivTrunc (
          location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
          false));
      /* Calculate the bit in this word.  */
      tree offset_into_word = m2expr_FoldAndStrip (m2expr_BuildModTrunc (
          location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
          false));
      tree p2 = m2expr_FoldAndStrip (m2expr_BuildMult (
          location, word_index,
          m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT), false));

      /* Calculate the address of the word we are interested in.  */
      p1 = m2expr_BuildAddAddress (location,
                                   m2convert_convertToPtr (location, p1), p2);

      /* Fetch the word, extract the bit and test for != 0.  */
      m2treelib_do_jump_if_bit (
          location, NE_EXPR,
          m2expr_BuildIndirect (location, p1, m2type_GetBitsetType ()),
          offset_into_word, label);
    }
}

/* BuildIfNotVarInVar generates: if not (varel in varset) then goto label.  */

void
m2expr_BuildIfNotVarInVar (location_t location, tree type, tree varset,
                           tree varel, bool is_lvalue, tree low,
                           tree high ATTRIBUTE_UNUSED, char *label)
{
  tree size = m2expr_GetSizeOf (location, type);
  /* Calculate the index from the first bit, ie bit 0 represents low value.  */
  tree index = m2expr_BuildSub (
      location, m2convert_BuildConvert (location, m2type_GetIntegerType (),
                                        m2expr_FoldAndStrip (varel), false),
      m2convert_BuildConvert (location, m2type_GetIntegerType (),
                              m2expr_FoldAndStrip (low), false),
      false);

  index = m2expr_FoldAndStrip (index);
  m2assert_AssertLocation (location);

  if (m2expr_CompareTrees (
          size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
      <= 0)
    /* Small set size <= TSIZE(WORD).  */
    m2treelib_do_jump_if_bit (
        location, EQ_EXPR,
        m2treelib_get_rvalue (location, varset, type, is_lvalue), index,
        label);
  else
    {
      tree p1 = m2treelib_get_set_address (location, varset, is_lvalue);
      /* Calculate the index from the first bit.  */

      /* Which word do we need to fetch?  */
      tree word_index = m2expr_FoldAndStrip (m2expr_BuildDivTrunc (
          location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
          false));
      /* Calculate the bit in this word.  */
      tree offset_into_word = m2expr_FoldAndStrip (m2expr_BuildModTrunc (
          location, index, m2decl_BuildIntegerConstant (SET_WORD_SIZE),
          false));
      tree p2 = m2expr_FoldAndStrip (m2expr_BuildMult (
          location, word_index,
          m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT), false));

      /* Calculate the address of the word we are interested in.  */
      p1 = m2expr_BuildAddAddress (location, p1, p2);

      /* Fetch the word, extract the bit and test for == 0.  */
      m2treelib_do_jump_if_bit (
          location, EQ_EXPR,
          m2expr_BuildIndirect (location, p1, m2type_GetBitsetType ()),
          offset_into_word, label);
    }
}

/* BuildForeachWordInSetDoIfExpr foreach word in set, type, compute
   the expression, expr, and if true goto label.  */

void
m2expr_BuildForeachWordInSetDoIfExpr (location_t location, tree type, tree op1,
                                      tree op2, bool is_op1lvalue,
                                      bool is_op2lvalue, bool is_op1const,
                                      bool is_op2const,
                                      tree (*expr) (location_t, tree, tree),
                                      char *label)
{
  tree p1 = m2treelib_get_set_address_if_var (location, op1, is_op1lvalue,
                                              is_op1const);
  tree p2 = m2treelib_get_set_address_if_var (location, op2, is_op2lvalue,
                                              is_op2const);
  unsigned int fieldNo = 0;
  tree field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
  tree field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);

  m2assert_AssertLocation (location);
  ASSERT_CONDITION (TREE_CODE (TREE_TYPE (op1)) == RECORD_TYPE);
  ASSERT_CONDITION (TREE_CODE (TREE_TYPE (op2)) == RECORD_TYPE);

  while (field1 != NULL && field2 != NULL)
    {
      m2statement_DoJump (
          location,
          (*expr) (location,
                   m2treelib_get_set_value (location, p1, field1, is_op1const,
                                            is_op1lvalue, op1, fieldNo),
                   m2treelib_get_set_value (location, p2, field2, is_op2const,
                                            is_op2lvalue, op2, fieldNo)),
          NULL, label);
      fieldNo++;
      field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
      field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
    }
}

/* BuildIfInRangeGoto returns a tree containing if var is in the
   range low..high then goto label.  */

void
m2expr_BuildIfInRangeGoto (location_t location, tree var, tree low, tree high,
                           char *label)
{
  m2assert_AssertLocation (location);

  if (m2expr_CompareTrees (low, high) == 0)
    m2statement_DoJump (location, m2expr_BuildEqualTo (location, var, low),
                        NULL, label);
  else
    m2statement_DoJump (
        location,
        m2expr_build_binary_op (
            location, TRUTH_ANDIF_EXPR,
            m2expr_BuildGreaterThanOrEqual (location, var, low),
            m2expr_BuildLessThanOrEqual (location, var, high), false),
        NULL, label);
}

/* BuildIfNotInRangeGoto returns a tree containing if var is not in
   the range low..high then goto label.  */

void
m2expr_BuildIfNotInRangeGoto (location_t location, tree var, tree low,
                              tree high, char *label)
{
  m2assert_AssertLocation (location);

  if (m2expr_CompareTrees (low, high) == 0)
    m2statement_DoJump (location, m2expr_BuildNotEqualTo (location, var, low),
                        NULL, label);
  else
    m2statement_DoJump (
        location, m2expr_build_binary_op (
                      location, TRUTH_ORIF_EXPR,
                      m2expr_BuildLessThan (location, var, low),
                      m2expr_BuildGreaterThan (location, var, high), false),
        NULL, label);
}

/* BuildArray - returns a tree which accesses array[index] given,
   lowIndice.  */

tree
m2expr_BuildArray (location_t location, tree type, tree array, tree index,
                   tree low_indice)
{
  tree array_type = m2tree_skip_type_decl (TREE_TYPE (array));
  tree index_type = TYPE_DOMAIN (array_type);
  type = m2tree_skip_type_decl (type);
// ASSERT_CONDITION (low_indice == TYPE_MIN_VALUE (index_type));

  low_indice
      = m2convert_BuildConvert (location, index_type, low_indice, false);
  return build4_loc (location, ARRAY_REF, type, array, index, low_indice,
                     NULL_TREE);
}

/* BuildComponentRef - build a component reference tree which
   accesses record.field.  If field does not belong to record it
   calls BuildComponentRef on the penultimate field.  */

tree
m2expr_BuildComponentRef (location_t location, tree record, tree field)
{
  tree recordType = m2tree_skip_reference_type (
      m2tree_skip_type_decl (TREE_TYPE (record)));

  if (DECL_CONTEXT (field) == recordType)
    return build3 (COMPONENT_REF, TREE_TYPE (field), record, field, NULL_TREE);
  else
    {
      tree f = determinePenultimateField (recordType, field);
      return m2expr_BuildComponentRef (
          location, m2expr_BuildComponentRef (location, record, f), field);
    }
}

/* BuildIndirect - build: (*target) given that the object to be
   copied is of, type.  */

tree
m2expr_BuildIndirect (location_t location ATTRIBUTE_UNUSED, tree target,
                      tree type)
{
  /* Note that the second argument to build1 is:

     TYPE_QUALS is a list of modifiers such as const or volatile to apply
     to the pointer type, represented as identifiers.

     it also determines the type of arithmetic and size of the object to
     be indirectly moved.  */

  tree t1 = m2tree_skip_type_decl (type);
  tree t2 = build_pointer_type (t1);

  m2assert_AssertLocation (location);

  return build1 (INDIRECT_REF, t1,
                 m2convert_BuildConvert (location, t2, target, false));
}

/* IsTrue - returns true if, t, is known to be true.  */

bool
m2expr_IsTrue (tree t)
{
  return (m2expr_FoldAndStrip (t) == m2type_GetBooleanTrue ());
}

/* IsFalse - returns false if, t, is known to be false.  */

bool
m2expr_IsFalse (tree t)
{
  return (m2expr_FoldAndStrip (t) == m2type_GetBooleanFalse ());
}

/* AreConstantsEqual - maps onto tree.cc (tree_int_cst_equal).  It
   returns true if the value of e1 is the same as e2.  */

bool
m2expr_AreConstantsEqual (tree e1, tree e2)
{
  return tree_int_cst_equal (e1, e2) != 0;
}

/* AreRealOrComplexConstantsEqual - returns true if constants, e1 and
   e2 are equal according to IEEE rules.  This does not perform bit
   equivalence for example IEEE states that -0 == 0 and NaN != NaN.  */

bool
m2expr_AreRealOrComplexConstantsEqual (tree e1, tree e2)
{
  if (TREE_CODE (e1) == COMPLEX_CST)
    return (m2expr_AreRealOrComplexConstantsEqual (TREE_REALPART (e1),
                                                   TREE_REALPART (e2))
            && m2expr_AreRealOrComplexConstantsEqual (TREE_IMAGPART (e1),
                                                      TREE_IMAGPART (e2)));
  else
    return real_compare (EQ_EXPR, &TREE_REAL_CST (e1), &TREE_REAL_CST (e2));
}

/* DetermineSign, returns -1 if e<0 0 if e==0 1 if e>0
   an unsigned constant will never return -1.  */

int
m2expr_DetermineSign (tree e)
{
  return tree_int_cst_sgn (e);
}

/* Similar to build_int_2 () but allows you to specify the type of
   the integer constant that you are creating.  */

static tree
build_int_2_type (HOST_WIDE_INT low, HOST_WIDE_INT hi, tree type)
{
  tree value;
  HOST_WIDE_INT ival[3];

  ival[0] = low;
  ival[1] = hi;
  ival[2] = 0;

  widest_int wval = widest_int::from_array (ival, 3);
  value = wide_int_to_tree (type, wval);

  return value;
}

/* BuildCap - builds the Modula-2 function CAP(t) and returns the
   result in a gcc Tree.  */

tree
m2expr_BuildCap (location_t location, tree t)
{
  tree tt;
  tree out_of_range, less_than, greater_than, translated;

  m2assert_AssertLocation (location);

  t = fold (t);
  if (t == error_mark_node)
    return error_mark_node;

  tt = TREE_TYPE (t);

  t = fold (convert (m2type_GetM2CharType (), t));

  if (TREE_CODE (tt) == INTEGER_TYPE)
    {
      less_than = fold (m2expr_build_binary_op (
          location, LT_EXPR, t,
          build_int_2_type ('a', 0, m2type_GetM2CharType ()), 0));
      greater_than = fold (m2expr_build_binary_op (
          location, GT_EXPR, t,
          build_int_2_type ('z', 0, m2type_GetM2CharType ()), 0));
      out_of_range = fold (m2expr_build_binary_op (
          location, TRUTH_ORIF_EXPR, less_than, greater_than, 0));

      translated = fold (convert (
          m2type_GetM2CharType (),
          m2expr_build_binary_op (
              location, MINUS_EXPR, t,
              build_int_2_type ('a' - 'A', 0, m2type_GetM2CharType ()), 0)));

      return fold_build3 (COND_EXPR, m2type_GetM2CharType (), out_of_range, t,
                          translated);
    }

  error_at (location,
            "argument to CAP is not a constant or variable of type CHAR");
  return error_mark_node;
}

/* BuildDivM2 if iso or pim4 then all modulus results are positive
   and the results from the division are rounded to the floor otherwise
   use BuildDivTrunc.  */

tree
m2expr_BuildDivM2 (location_t location, tree op1, tree op2,
                   bool needsconvert)
{
  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);
  ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
  /* If iso or pim4 then build and return ((op2 < 0) ? (op1
     divceil op2) : (op1 divfloor op2)) otherwise use divtrunc.  */
  if (M2Options_GetPIM4 () || M2Options_GetISO ()
      || M2Options_GetPositiveModFloor ())
    return fold_build3 (
        COND_EXPR, TREE_TYPE (op1),
        m2expr_BuildLessThan (
            location, op2,
            m2convert_BuildConvert (location, TREE_TYPE (op2),
                                    m2expr_GetIntegerZero (location), false)),
        m2expr_BuildDivCeil (location, op1, op2, needsconvert),
        m2expr_BuildDivFloor (location, op1, op2, needsconvert));
  else
    return m2expr_BuildDivTrunc (location, op1, op2, needsconvert);
}

/* BuildDivM2Check - build and
   return ((op2 < 0) ? (op1 divtrunc op2) : (op1 divfloor op2))
   when -fiso, -fpim4 or -fpositive-mod-floor-div is present else
   return op1 div trunc op2.  Use the checking div equivalents.  */

tree
m2expr_BuildDivM2Check (location_t location, tree op1, tree op2,
			tree lowest, tree min, tree max)
{
  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);
  ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
  if (M2Options_GetISO ()
      || M2Options_GetPIM4 () || M2Options_GetPositiveModFloor ())
    return fold_build3 (
        COND_EXPR, TREE_TYPE (op1),
        m2expr_BuildLessThan (
            location, op2,
            m2convert_BuildConvert (location, TREE_TYPE (op2),
                                    m2expr_GetIntegerZero (location), false)),
        m2expr_BuildDivCeilCheck (location, op1, op2, lowest, min, max),
        m2expr_BuildDivFloorCheck (location, op1, op2, lowest, min, max));
  else
    return m2expr_BuildDivTruncCheck (location, op1, op2, lowest, min, max);
}

static
tree
m2expr_BuildISOModM2Check (location_t location,
			   tree op1, tree op2, tree lowest, tree min, tree max)
{
  tree cond = m2expr_BuildLessThan (location, op2,
				    m2convert_BuildConvert (location, TREE_TYPE (op2),
							    m2expr_GetIntegerZero (location), false));

  /* Return the result of the modulus.  */
  return fold_build3 (COND_EXPR, TREE_TYPE (op1), cond,
		      /* op2 < 0.  */
		      m2expr_BuildModCeilCheck (location, op1, op2, lowest, min, max),
		      /* op2 >= 0.  */
		      m2expr_BuildModFloorCheck (location, op1, op2, lowest, min, max));
}


/* BuildModM2Check if iso or pim4 then build and return ((op2 < 0) ? (op1
   modceil op2) :  (op1 modfloor op2)) otherwise use modtrunc.
   Use the checking mod equivalents.  */

tree
m2expr_BuildModM2Check (location_t location, tree op1, tree op2,
			tree lowest, tree min, tree max)
{
  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);
  ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
  if (M2Options_GetPIM4 () || M2Options_GetISO ()
      || M2Options_GetPositiveModFloor ())
    return m2expr_BuildISOModM2Check (location, op1, op2, lowest, min, max);
  else
    return m2expr_BuildModTruncCheck (location, op1, op2, lowest, min, max);
}

/* BuildModM2 if iso or pim4 then build and return ((op2 < 0) ? (op1
   modceil op2) : (op1 modfloor op2)) otherwise use modtrunc.  */

tree
m2expr_BuildModM2 (location_t location, tree op1, tree op2,
                   bool needsconvert)
{
  op1 = m2expr_FoldAndStrip (op1);
  op2 = m2expr_FoldAndStrip (op2);
  ASSERT_CONDITION (TREE_TYPE (op1) == TREE_TYPE (op2));
  if (M2Options_GetPIM4 () || M2Options_GetISO ()
      || M2Options_GetPositiveModFloor ())
    return fold_build3 (
        COND_EXPR, TREE_TYPE (op1),
        m2expr_BuildLessThan (
            location, op2,
            m2convert_BuildConvert (location, TREE_TYPE (op2),
                                    m2expr_GetIntegerZero (location), false)),
        m2expr_BuildModCeil (location, op1, op2, needsconvert),
        m2expr_BuildModFloor (location, op1, op2, needsconvert));
  else
    return m2expr_BuildModTrunc (location, op1, op2, needsconvert);
}

/* BuildAbs build the Modula-2 function ABS(t) and return the result
   in a gcc Tree.  */

tree
m2expr_BuildAbs (location_t location, tree t)
{
  m2assert_AssertLocation (location);

  return m2expr_build_unary_op (location, ABS_EXPR, t, 0);
}

/* BuildRe build an expression for the function RE.  */

tree
m2expr_BuildRe (tree op1)
{
  op1 = m2expr_FoldAndStrip (op1);
  if (TREE_CODE (op1) == COMPLEX_CST)
    return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
  else
    return build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
}

/* BuildIm build an expression for the function IM.  */

tree
m2expr_BuildIm (tree op1)
{
  op1 = m2expr_FoldAndStrip (op1);
  if (TREE_CODE (op1) == COMPLEX_CST)
    return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
  else
    return build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (op1)), op1);
}

/* BuildCmplx build an expression for the function CMPLX.  */

tree
m2expr_BuildCmplx (location_t location, tree type, tree real, tree imag)
{
  tree scalor;
  real = m2expr_FoldAndStrip (real);
  imag = m2expr_FoldAndStrip (imag);
  type = m2tree_skip_type_decl (type);
  scalor = TREE_TYPE (type);

  if (scalor != TREE_TYPE (real))
    real = m2convert_BuildConvert (location, scalor, real, false);
  if (scalor != TREE_TYPE (imag))
    imag = m2convert_BuildConvert (location, scalor, imag, false);

  if ((TREE_CODE (real) == REAL_CST) && (TREE_CODE (imag) == REAL_CST))
    return build_complex (type, real, imag);
  else
    return build2 (COMPLEX_EXPR, type, real, imag);
}

/* BuildBinaryForeachWordDo implements the large set operators.  Each
   word of the set can be calculated by binop.  This function runs along
   each word of the large set invoking the binop.  */

void
m2expr_BuildBinaryForeachWordDo (location_t location, tree type, tree op1,
                                 tree op2, tree op3,
                                 tree (*binop) (location_t, tree, tree, bool),
                                 bool is_op1lvalue, bool is_op2lvalue,
                                 bool is_op3lvalue, bool is_op1const,
                                 bool is_op2const, bool is_op3const)
{
  tree size = m2expr_GetSizeOf (location, type);

  m2assert_AssertLocation (location);

  ASSERT_BOOL (is_op1lvalue);
  ASSERT_BOOL (is_op2lvalue);
  ASSERT_BOOL (is_op3lvalue);
  ASSERT_BOOL (is_op1const);
  ASSERT_BOOL (is_op2const);
  ASSERT_BOOL (is_op3const);
  if (m2expr_CompareTrees (
          size, m2decl_BuildIntegerConstant (SET_WORD_SIZE / BITS_PER_UNIT))
      <= 0)
    /* Small set size <= TSIZE(WORD).  */
    m2statement_BuildAssignmentTree (
        location, m2treelib_get_rvalue (location, op1, type, is_op1lvalue),
        (*binop) (
            location, m2treelib_get_rvalue (location, op2, type, is_op2lvalue),
            m2treelib_get_rvalue (location, op3, type, is_op3lvalue), false));
  else
    {
      /* Large set size > TSIZE(WORD).  */

      tree p2 = m2treelib_get_set_address_if_var (location, op2, is_op2lvalue,
                                                  is_op2const);
      tree p3 = m2treelib_get_set_address_if_var (location, op3, is_op3lvalue,
                                                  is_op3const);
      unsigned int fieldNo = 0;
      tree field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
      tree field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
      tree field3 = m2treelib_get_field_no (type, op3, is_op3const, fieldNo);

      if (is_op1const)
	m2linemap_internal_error_at (
            location,
            "not expecting operand1 to be a constant set");

      while (field1 != NULL && field2 != NULL && field3 != NULL)
        {
          m2statement_BuildAssignmentTree (
              location, m2treelib_get_set_field_des (location, op1, field1),
              (*binop) (
                  location,
                  m2treelib_get_set_value (location, p2, field2, is_op2const,
                                           is_op2lvalue, op2, fieldNo),
                  m2treelib_get_set_value (location, p3, field3, is_op3const,
                                           is_op3lvalue, op3, fieldNo),
                  false));
          fieldNo++;
          field1 = m2treelib_get_field_no (type, op1, is_op1const, fieldNo);
          field2 = m2treelib_get_field_no (type, op2, is_op2const, fieldNo);
          field3 = m2treelib_get_field_no (type, op3, is_op3const, fieldNo);
        }
    }
}


/* OverflowZType returns true if the ZTYPE str will exceed the
   internal representation.  This routine is much faster (at
   least 2 orders of magnitude faster) than the char at a time overflow
   detection used in ToWideInt and so it should be
   used to filter out erroneously large constants before calling ToWideInt
   allowing a quick fail.  */

bool
m2expr_OverflowZType (location_t location, const char *str, unsigned int base,
		      bool issueError)
{
  int length = strlen (str);
  bool overflow = false;

  switch (base)
    {
    case 2:
      overflow = ((length -1) > WIDE_INT_MAX_PRECISION);
      break;
    case 8:
      overflow = (((length -1) * 3) > WIDE_INT_MAX_PRECISION);
      break;
    case 10:
      {
	int str_log10 = length;
	int bits_str = (int) (((float) (str_log10)) / log10f (2.0)) + 1;
	overflow = (bits_str > WIDE_INT_MAX_PRECISION);
      }
      break;
    case 16:
      overflow = (((length -1) * 4) > WIDE_INT_MAX_PRECISION);
      break;
    default:
      gcc_unreachable ();
    }
  if (issueError && overflow)
    error_at (location,
	      "constant literal %qs exceeds internal ZTYPE range", str);
  return overflow;
}


/* ToWideInt converts a ZTYPE str value into result.  */

static
bool
ToWideInt (location_t location, const char *str, unsigned int base,
	   widest_int &result, bool issueError)
{
  tree type = m2type_GetM2ZType ();
  unsigned int i = 0;
  wi::overflow_type overflow = wi::OVF_NONE;
  widest_int wbase = wi::to_widest (m2decl_BuildIntegerConstant (base));
  unsigned int digit = 0;
  result = wi::to_widest (m2decl_BuildIntegerConstant (0));
  bool base_specifier = false;

  while (((str[i] != (char)0) && (overflow == wi::OVF_NONE))
	 && (! base_specifier))
    {
      char ch = str[i];

      switch (base)
	{
	  /* GNU m2 extension allows 'A' to represent binary literals.  */
	case 2:
	  if (ch == 'A')
	    base_specifier = true;
	  else if ((ch < '0') || (ch > '1'))
	    {
	      if (issueError)
		error_at (location,
			  "constant literal %qs contains %qc, expected 0 or 1",
			  str, ch);
	      return true;
	    }
	  else
	    digit = (unsigned int) (ch - '0');
	  break;
	case 8:
	  /* An extension of 'B' indicates octal ZTYPE and 'C' octal character.  */
	  if ((ch == 'B') || (ch == 'C'))
	    base_specifier = true;
	  else if ((ch < '0') || (ch > '7'))
	    {
	      if (issueError)
		error_at (location,
			  "constant literal %qs contains %qc, expected %qs",
			  str, ch, "0..7");
	      return true;
	    }
	  else
	    digit = (unsigned int) (ch - '0');
	  break;
	case 10:
	  if ((ch < '0') || (ch > '9'))
	    {
	      if (issueError)
		error_at (location,
			  "constant literal %qs contains %qc, expected %qs",
			  str, ch, "0..9");
	      return true;
	    }
	  else
	    digit = (unsigned int) (ch - '0');
	  break;
	case 16:
	  /* An extension of 'H' indicates hexidecimal ZTYPE.  */
	  if (ch == 'H')
	    base_specifier = true;
	  else if ((ch >= '0') && (ch <= '9'))
	    digit = (unsigned int) (ch - '0');
	  else if ((ch >= 'A') && (ch <= 'F'))
	    digit = ((unsigned int) (ch - 'A')) + 10;
	  else
	    {
	      if (issueError)
		error_at (location,
			  "constant literal %qs contains %qc, expected %qs or %qs",
			  str, ch, "0..9", "A..F");
	      return true;
	    }
	  break;
	default:
	  gcc_unreachable ();
	}

      if (! base_specifier)
	{
	  widest_int wdigit = wi::to_widest (m2decl_BuildIntegerConstant (digit));
	  result = wi::umul (result, wbase, &overflow);
	  if (overflow == wi::OVF_NONE)
	    result = wi::add (result, wdigit, UNSIGNED, &overflow);
	}
      i++;
    }
  if (overflow == wi::OVF_NONE)
    {
      tree value = wide_int_to_tree (type, result);
      if (m2expr_TreeOverflow (value))
	{
	  if (issueError)
	    error_at (location,
		      "constant literal %qs exceeds internal ZTYPE range", str);
	  return true;
	}
      return false;
    }
  else
    {
      if (issueError)
	error_at (location,
		  "constant literal %qs exceeds internal ZTYPE range", str);
      return true;
    }
}


/* StrToWideInt return true if an overflow occurs when attempting to convert
   str to an unsigned ZTYPE the value is contained in the widest_int result.
   The value result is undefined if true is returned.  */

bool
m2expr_StrToWideInt (location_t location, const char *str, unsigned int base,
		     widest_int &result, bool issueError)
{
  if (m2expr_OverflowZType (location, str, base, issueError))
    return true;
  return ToWideInt (location, str, base, result, issueError);
}


/* GetSizeOfInBits return the number of bits used to contain, type.  */

tree
m2expr_GetSizeOfInBits (tree type)
{
  enum tree_code code = TREE_CODE (type);

  if (code == FUNCTION_TYPE)
    return m2expr_GetSizeOfInBits (ptr_type_node);

  if (code == VOID_TYPE)
    {
      error ("%qs applied to a void type", "sizeof");
      return size_one_node;
    }

  if (code == VAR_DECL)
    return m2expr_GetSizeOfInBits (TREE_TYPE (type));

  if (code == PARM_DECL)
    return m2expr_GetSizeOfInBits (TREE_TYPE (type));

  if (code == TYPE_DECL)
    return m2expr_GetSizeOfInBits (TREE_TYPE (type));

  if (code == COMPONENT_REF)
    return m2expr_GetSizeOfInBits (TREE_TYPE (type));

  if (code == ERROR_MARK)
    return size_one_node;

  if (!COMPLETE_TYPE_P (type))
    {
      error ("%qs applied to an incomplete type", "sizeof");
      return size_zero_node;
    }

  return m2decl_BuildIntegerConstant (TYPE_PRECISION (type));
}

/* GetSizeOf taken from c-typeck.cc (c_sizeof).  */

tree
m2expr_GetSizeOf (location_t location, tree type)
{
  enum tree_code code = TREE_CODE (type);
  m2assert_AssertLocation (location);

  if (code == FUNCTION_TYPE)
    return m2expr_GetSizeOf (location, m2type_GetPointerType ());

  if (code == VOID_TYPE)
    return size_one_node;

  if (code == VAR_DECL)
    return m2expr_GetSizeOf (location, TREE_TYPE (type));

  if (code == PARM_DECL)
    return m2expr_GetSizeOf (location, TREE_TYPE (type));

  if (code == TYPE_DECL)
    return m2expr_GetSizeOf (location, TREE_TYPE (type));

  if (code == ERROR_MARK)
    return size_one_node;

  if (code == CONSTRUCTOR)
    return m2expr_GetSizeOf (location, TREE_TYPE (type));

  if (code == FIELD_DECL)
    return m2expr_GetSizeOf (location, TREE_TYPE (type));

  if (code == COMPONENT_REF)
    return m2expr_GetSizeOf (location, TREE_TYPE (type));

  if (!COMPLETE_TYPE_P (type))
    {
      error_at (location, "%qs applied to an incomplete type", "SIZE");
      return size_zero_node;
    }

  /* Convert in case a char is more than one unit.  */
  return size_binop_loc (
      location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
      size_int (TYPE_PRECISION (char_type_node) / BITS_PER_UNIT));
}

tree
m2expr_GetIntegerZero (location_t location ATTRIBUTE_UNUSED)
{
  return integer_zero_node;
}

tree
m2expr_GetIntegerOne (location_t location ATTRIBUTE_UNUSED)
{
  return integer_one_node;
}

tree
m2expr_GetCardinalOne (location_t location)
{
  return m2convert_ToCardinal (location, integer_one_node);
}

tree
m2expr_GetCardinalZero (location_t location)
{
  return m2convert_ToCardinal (location, integer_zero_node);
}

tree
m2expr_GetWordZero (location_t location)
{
  return m2convert_ToWord (location, integer_zero_node);
}

tree
m2expr_GetWordOne (location_t location)
{
  return m2convert_ToWord (location, integer_one_node);
}

tree
m2expr_GetPointerZero (location_t location)
{
  return m2convert_convertToPtr (location, integer_zero_node);
}

tree
m2expr_GetPointerOne (location_t location)
{
  return m2convert_convertToPtr (location, integer_one_node);
}

/* build_set_full_complement return a word size value with all bits
set to one.  */

static tree
build_set_full_complement (location_t location)
{
  tree value = integer_zero_node;
  int i;

  m2assert_AssertLocation (location);

  for (i = 0; i < SET_WORD_SIZE; i++)
    {
      value = m2expr_BuildLogicalOr (
          location, value,
          m2expr_BuildLSL (
              location, m2expr_GetWordOne (location),
              m2convert_BuildConvert (location, m2type_GetWordType (),
                                      m2decl_BuildIntegerConstant (i), false),
              false),
          false);
    }
  return value;
}


/* GetCstInteger return the integer value of the cst tree.  */

int
m2expr_GetCstInteger (tree cst)
{
  return TREE_INT_CST_LOW (cst);
}


/* init initialise this module.  */

void
m2expr_init (location_t location)
{
  m2assert_AssertLocation (location);

  set_full_complement = build_set_full_complement (location);
}

#include "gt-m2-m2expr.h"