summaryrefslogtreecommitdiff
path: root/05/codegen.b
blob: b89ab51fbb756fa46a1feb2f5811f9838c55a4fd (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
; CALLING CONVENTION:
;  Here is the process for calling a function:
;     - the caller pushes the arguments on to the stack, from right to left
;     - the caller subtracts sizeof(return type) from rsp, rounded up to the nearest 8 bytes
;     - the caller calls the function
;     - the caller stores away the return value
;     - the caller adds (sizeof(return type) + sizeof arg0 + ... + sizeof argn) to rsp  - where each sizeof is rounded up to the nearest 8 bytes
; STACK LAYOUT:
;    arg n
;    ...
;    arg 0
;    return value   [rbp+16]
;    return address [rbp+8]
;    old rbp        [rbp]
;    local variables



global code_output
global codegen_second_pass ; = 0 on first global pass, 1 on second global pass
global functions_addresses ; ident list of addresses
global functions_labels ; ident list of ident lists of label addresses
global curr_function_labels ; ident list of labels for current function (written to in 1st pass, read from in 2nd pass)
global curr_function_return_type

global break_refs ; 0-terminated array of pointers to be filled in with the break offset
global continue_refs

; address of previous "case" label
global prev_case_addr


#define REG_RAX 0
#define REG_RBX 3
#define REG_RCX 1
#define REG_RDX 2
#define REG_RSP 4
#define REG_RBP 5
#define REG_RSI 6
#define REG_RDI 7

function emit_byte
	argument byte
	*1code_output = byte
	code_output += 1
	return

function emit_bytes
	argument bytes
	argument count
	memcpy(code_output, bytes, count)
	code_output += count
	return
	
function emit_word
	argument word
	*2code_output = word
	code_output += 2
	return

function emit_dword
	argument word
	*4code_output = word
	code_output += 4
	return

function emit_qword
	argument word
	*8code_output = word
	code_output += 8
	return

; e.g. emit_mov_reg(REG_RAX, REG_RBX)  emits  mov rax, rbx
function emit_mov_reg
	argument dest
	argument src
	local n
	
	if src ] 7 goto bad_reg
	if dest ] 7 goto bad_reg
	
	;48 89 (DEST|SRC<<3|0xc0)
	*2code_output = 0x8948
	code_output += 2
	n = 0xc0 | dest
	n |= src < 3
	*1code_output = n
	code_output += 1
	return
	:bad_reg
		die(.str_bad_reg)
	:str_bad_reg
		string Internal compiler error: bad register passed to emit_mov_reg.
		byte 0

function emit_mov_rax_imm64
	argument imm64
	if imm64 == 0 goto rax_imm64_0
	; 48 b8 IMM64
	*2code_output = 0xb848
	code_output += 2
	*8code_output = imm64
	code_output += 8
	return
	:rax_imm64_0
	emit_zero_rax()
	return

function emit_mov_rbx_imm64
	; 48 bb IMM64
	argument imm64
	*2code_output = 0xbb48
	code_output += 2
	*8code_output = imm64
	code_output += 8
	return

function emit_add_rax_imm32
	; 48 05 IMM32
	argument imm32
	*2code_output = 0x0548
	code_output += 2
	*4code_output = imm32
	code_output += 4
	return
	

function emit_zero_rax
	; 31 c0
	*2code_output = 0xc031
	code_output += 2
	return
	
function emit_zero_rdx
	; 31 d2
	*2code_output = 0xd231
	code_output += 2
	return
	
function emit_movsx_rax_al
	; 48 0f be c0
	*4code_output = 0xc0be0f48
	code_output += 4
	return

function emit_movsx_rax_ax
	; 48 0f bf c0
	*4code_output = 0xc0bf0f48
	code_output += 4
	return

function emit_movsx_rax_eax
	; 48 63 c0
	*2code_output = 0x6348
	code_output += 2
	*1code_output = 0xc0
	code_output += 1
	return

function emit_movzx_rax_al
	; 48 0f b6 c0
	*4code_output = 0xc0b60f48
	code_output += 4
	return

function emit_movzx_rax_ax
	; 48 0f b7 c0
	*4code_output = 0xc0b70f48
	code_output += 4
	return

function emit_mov_eax_eax
	; 89 c0
	*2code_output = 0xc089
	code_output += 2
	return

function emit_mov_al_byte_rbx
	; 8a 03
	*2code_output = 0x038a
	code_output += 2
	return

function emit_mov_byte_rbx_al
	; 88 03
	*2code_output = 0x0388
	code_output += 2
	return

function emit_mov_ax_word_rbx
	; 66 8b 03
	*2code_output = 0x8b66
	code_output += 2
	*1code_output = 0x03
	code_output += 1
	return

function emit_mov_word_rbx_ax
	; 66 89 03
	*2code_output = 0x8966
	code_output += 2
	*1code_output = 0x03
	code_output += 1
	return

function emit_mov_eax_dword_rbx
	; 8b 03
	*2code_output = 0x038b
	code_output += 2
	return

function emit_mov_dword_rbx_eax
	; 89 03
	*2code_output = 0x0389
	code_output += 2
	return

function emit_mov_rax_qword_rbx
	; 48 8b 03
	*2code_output = 0x8b48
	code_output += 2
	*1code_output = 0x03
	code_output += 1
	return

function emit_mov_qword_rbx_rax
	; 48 89 03
	*2code_output = 0x8948
	code_output += 2
	*1code_output = 0x03
	code_output += 1
	return

function emit_mov_qword_rsp_plus_imm32_rax
	argument imm32
	; 48 89 84 24 IMM32
	*4code_output = 0x24848948
	code_output += 4
	*4code_output = imm32
	code_output += 4
	return

function emit_mov_rax_qword_rsp_plus_imm32
	argument imm32
	; 48 8b 84 24 IMM32
	*4code_output = 0x24848b48
	code_output += 4
	*4code_output = imm32
	code_output += 4
	return

function emit_mov_rax_qword_rsp
	emit_mov_rax_qword_rsp_plus_imm32(0)
	return

function emit_mov_qword_rsp_rax
	emit_mov_qword_rsp_plus_imm32_rax(0)
	return

function emit_sub_rsp_imm32
	argument imm32
	;48 81 ec IMM32
	*2code_output = 0x8148
	code_output += 2
	*1code_output = 0xec
	code_output += 1
	*4code_output = imm32
	code_output += 4
	return

function emit_mov_qword_rsp_rbp
	; 48 89 2c 24
	*4code_output = 0x242c8948
	code_output += 4
	return

function emit_mov_rbp_qword_rsp
	; 48 8b 2c 24
	*4code_output = 0x242c8b48
	code_output += 4
	return

function emit_add_rsp_imm32
	argument imm32
	;48 81 c4 IMM32
	*2code_output = 0x8148
	code_output += 2
	*1code_output = 0xc4
	code_output += 1
	*4code_output = imm32
	code_output += 4
	return

function emit_ret
	*1code_output = 0xc3
	code_output += 1
	return

function emit_call_rax
	; ff d0
	*2code_output = 0xd0ff
	code_output += 2
	return

function emit_push_rax
	; 50
	*1code_output = 0x50
	code_output += 1
	return

function emit_pop_rax
	; 58
	*1code_output = 0x58
	code_output += 1
	return

function emit_syscall
	; 0f 05
	*2code_output = 0x050f
	code_output += 2
	return

function emit_lea_rax_rbp_plus_imm32
	; 48 8d 85 IMM32
	argument imm32
	*2code_output = 0x8d48
	code_output += 2
	*1code_output = 0x85
	code_output += 1
	*4code_output = imm32
	code_output += 4
	return

function emit_lea_rsp_rbp_plus_imm32
	; 48 8d a5 IMM32
	argument imm32
	*2code_output = 0x8d48
	code_output += 2
	*1code_output = 0xa5
	code_output += 1
	*4code_output = imm32
	code_output += 4
	return

function emit_mov_rax_qword_rbp_plus_imm32
	; 48 8b 85 IMM32
	argument imm32
	*2code_output = 0x8b48
	code_output += 2
	*1code_output = 0x85
	code_output += 1
	*4code_output = imm32
	code_output += 4
	return

	
function emit_rep_movsb
	; f3 a4
	*2code_output = 0xa4f3
	code_output += 2
	return

function emit_movsq
	; 48 a5
	*2code_output = 0xa548
	code_output += 2
	return

function emit_movq_rax_xmm0
	; 66 48 0f 7e c0
	*4code_output = 0x7e0f4866
	code_output += 4
	*1code_output = 0xc0
	code_output += 1
	return

function emit_movq_xmm0_rax
	; 66 48 0f 6e c0
	*4code_output = 0x6e0f4866
	code_output += 4
	*1code_output = 0xc0
	code_output += 1
	return

function emit_movq_xmm1_rax
	; 66 48 0f 6e c8
	*4code_output = 0x6e0f4866
	code_output += 4
	*1code_output = 0xc8
	code_output += 1
	return

function emit_movq_xmm1_xmm0
	; f3 0f 7e c8
	*4code_output = 0xc87e0ff3
	code_output += 4
	return

function emit_cvtss2sd_xmm0_xmm0
	; f3 0f 5a c0
	*4code_output = 0xc05a0ff3
	code_output += 4
	return

function emit_cvtsd2ss_xmm0_xmm0
	; f2 0f 5a c0
	*4code_output = 0xc05a0ff2
	code_output += 4
	return

function emit_cvttsd2si_rax_xmm0
	; f2 48 0f 2c c0
	*4code_output = 0x2c0f48f2
	code_output += 4
	*1code_output = 0xc0
	code_output += 1
	return

function emit_cvtsi2sd_xmm0_rax
	; f2 48 0f 2a c0
	*4code_output = 0x2a0f48f2
	code_output += 4
	*1code_output = 0xc0
	code_output += 1
	return

function emit_addsd_xmm0_xmm1
	*4code_output = 0xc1580ff2
	code_output += 4
	return

function emit_subsd_xmm0_xmm1
	*4code_output = 0xc15c0ff2
	code_output += 4
	return

function emit_mulsd_xmm0_xmm1
	*4code_output = 0xc1590ff2
	code_output += 4
	return

function emit_divsd_xmm0_xmm1
	*4code_output = 0xc15e0ff2
	code_output += 4
	return

function emit_neg_rax
	; 48 f7 d8
	*2code_output = 0xf748
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_not_rax
	; 48 f7 d0
	*2code_output = 0xf748
	code_output += 2
	*1code_output = 0xd0
	code_output += 1
	return

function emit_add_rax_rbx
	; 48 01 d8
	*2code_output = 0x0148
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_sub_rax_rbx
	; 48 29 d8
	*2code_output = 0x2948
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_imul_rbx
	; 48 f7 eb
	*2code_output = 0xf748
	code_output += 2
	*1code_output = 0xeb
	code_output += 1
	return

function emit_idiv_rbx
	; 48 f7 fb
	*2code_output = 0xf748
	code_output += 2
	*1code_output = 0xfb
	code_output += 1
	return

function emit_mul_rbx
	; 48 f7 e3
	*2code_output = 0xf748
	code_output += 2
	*1code_output = 0xe3
	code_output += 1
	return

function emit_div_rbx
	; 48 f7 f3
	*2code_output = 0xf748
	code_output += 2
	*1code_output = 0xf3
	code_output += 1
	return

function emit_and_rax_rbx
	; 48 21 d8
	*2code_output = 0x2148
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_or_rax_rbx
	; 48 09 d8
	*2code_output = 0x0948
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_xor_rax_rbx
	; 48 31 d8
	*2code_output = 0x3148
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_shl_rax_cl
	; 48 d3 e0
	*2code_output = 0xd348
	code_output += 2
	*1code_output = 0xe0
	code_output += 1
	return

function emit_shr_rax_cl
	; 48 d3 e8
	*2code_output = 0xd348
	code_output += 2
	*1code_output = 0xe8
	code_output += 1
	return

function emit_sar_rax_cl
	; 48 d3 f8
	*2code_output = 0xd348
	code_output += 2
	*1code_output = 0xf8
	code_output += 1
	return

function emit_cqo
	; 48 99
	*2code_output = 0x9948
	code_output += 2
	return

function emit_test_rax_rax
	; 48 85 c0
	*2code_output = 0x8548
	code_output += 2
	*1code_output = 0xc0
	code_output += 1
	return

function emit_jmp_rel32
	; e9 REL32
	argument rel32
	*1code_output = 0xe9
	code_output += 1
	*4code_output = rel32
	code_output += 4
	return

function emit_je_rel32
	; 0f 84 REL32
	argument rel32
	*2code_output = 0x840f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jne_rel32
	; 0f 85 REL32
	argument rel32
	*2code_output = 0x850f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jl_rel32
	; 0f 8c REL32
	argument rel32
	*2code_output = 0x8c0f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jg_rel32
	; 0f 8f REL32
	argument rel32
	*2code_output = 0x8f0f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jle_rel32
	; 0f 8e REL32
	argument rel32
	*2code_output = 0x8e0f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jge_rel32
	; 0f 8d REL32
	argument rel32
	*2code_output = 0x8d0f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jb_rel32
	; 0f 82 REL32
	argument rel32
	*2code_output = 0x820f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_ja_rel32
	; 0f 87 REL32
	argument rel32
	*2code_output = 0x870f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jbe_rel32
	; 0f 86 REL32
	argument rel32
	*2code_output = 0x860f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_jae_rel32
	; 0f 83 REL32
	argument rel32
	*2code_output = 0x830f
	code_output += 2
	*4code_output = rel32
	code_output += 4
	return

function emit_cmp_rax_rbx
	; 48 39 d8
	*2code_output = 0x3948
	code_output += 2
	*1code_output = 0xd8
	code_output += 1
	return

function emit_comisd_xmm0_xmm1
	; 66 0f 2f c1
	*4code_output = 0xc12f0f66
	code_output += 4
	return

#define COMPARISON_E 0x4
#define COMPARISON_NE 0x5
#define COMPARISON_L 0xc
#define COMPARISON_G 0xf
#define COMPARISON_LE 0xe
#define COMPARISON_GE 0xd
#define COMPARISON_B 0x2
#define COMPARISON_A 0x7
#define COMPARISON_BE 0x6
#define COMPARISON_AE 0x3

; emits two instructions:
;    setC al
;    movzx rax, al
function emit_setC_rax
	; 0f 9C c0 48 0f b6 c0
	argument comparison
	local a
	a = 0xc0b60f48c0900f
	a |= comparison < 8
	*8code_output = a
	code_output += 7
	return

; make sure you put the return value in the proper place before calling this
function generate_return
	emit_mov_reg(REG_RSP, REG_RBP)
	emit_mov_rbp_qword_rsp()
	emit_add_rsp_imm32(8)
	emit_ret()
	return

; copy sizeof(type) bytes, rounded up to the nearest 8, from rsi to rdi
function generate_copy_rsi_to_rdi_qwords
	argument type
	local n
	n = type_sizeof(type)
	
	n = round_up_to_8(n)
	if n == 8 goto rsi2rdi_qwords_simple
	; this is a struct or something, use  rep movsb
	emit_mov_rax_imm64(n)
	emit_mov_reg(REG_RCX, REG_RAX)
	emit_rep_movsb()
	return
	
	:rsi2rdi_qwords_simple
	; copy 8 bytes from rsi to rdi
	; this is a little "optimization" over rep movsb with rcx = 8, mainly it just makes debugging easier (otherwise you'd need 8 `stepi`s in gdb to skip over the instruction)
	emit_movsq()
	return
	

; cast whatever was just pushed onto the stack from from_type to to_type
; `statement` is used for errors
function generate_cast_top_of_stack
	argument statement
	argument from_type
	argument to_type
	local from
	local to
	local c
	local d
	
	from = types + from_type
	to = types + to_type
	
	if *1to == TYPE_VOID goto gen_cast_to_void
	if *1from == TYPE_VOID goto bad_gen_cast ; cast from void to something - that's bad
	if *1to == TYPE_ARRAY goto bad_gen_cast ; cast to array
	if *1from == TYPE_FUNCTION goto bad_gen_cast ; shouldn't happen
	if *1to == TYPE_FUNCTION goto bad_gen_cast ; shouldn't happen
	if *1to == TYPE_STRUCT goto gen_cast_to_struct
	if *1from == TYPE_STRUCT goto bad_gen_cast ; cast from struct to something else
	if *1to < TYPE_FLOAT goto gen_cast_to_integer
	if *1to == TYPE_POINTER goto gen_cast_to_integer ; pointers are basically integers
	
	; cast to float/double
	if *1from == TYPE_POINTER goto bad_gen_cast ; pointer to float/double
	if *1to == *1from goto return_0
	if *1from == TYPE_DOUBLE goto gen_cast_double_to_float
	if *1from == TYPE_FLOAT goto gen_cast_float_to_double
	; int to float/double
	if *1to == TYPE_FLOAT goto gen_cast_int_to_float
	if *1to == TYPE_DOUBLE goto gen_cast_int_to_double
	
	goto bad_gen_cast ; in theory we shouldn't get here
	
	:gen_cast_to_void
		; we need to handle rsp properly for stuff like:
		;  SomeLargeStruct s;
		;  (void)s;
		c = type_sizeof(from_type)
		c = round_up_to_8(c)
		c -= 8
		if c == 0 goto return_0 ; int to void cast or something; we don't care
		emit_add_rsp_imm32(c)
		return
	:gen_cast_to_integer
		if *1from == TYPE_ARRAY goto return_0 ;  e.g. (void *)"hello"
		if *1from == *1to goto return_0 ; casting from type to same type
		if *1from == TYPE_POINTER goto return_0 ; no need to do anything
		; cast float/double to integer
		if *1from == TYPE_FLOAT goto gen_cast_float_to_int
		if *1from == TYPE_DOUBLE goto gen_cast_double_to_int
		
		c = type_sizeof(*1from)
		d = type_sizeof(*1to)
		if d > c goto return_0 ; casting to bigger type, so we're good
		if d == 8 goto return_0 ; casting from unsigned/signed long to unsigned/signed long/pointer, we're good
		
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		
		; now sign/zero extend the lower part of rax to the whole of rax		
		if *1to == TYPE_CHAR goto gen_cast_integer_to_signed_char
		if *1to == TYPE_UNSIGNED_CHAR goto gen_cast_integer_to_unsigned_char
		if *1to == TYPE_SHORT goto gen_cast_integer_to_signed_short
		if *1to == TYPE_UNSIGNED_SHORT goto gen_cast_integer_to_unsigned_short
		if *1to == TYPE_INT goto gen_cast_integer_to_signed_int
		if *1to == TYPE_UNSIGNED_INT goto gen_cast_integer_to_unsigned_int
		
		goto bad_gen_cast ; in theory we shouldn't get here
		
		:int2int_cast_cont
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		return
		
		:gen_cast_integer_to_signed_char
			emit_movsx_rax_al()
			goto int2int_cast_cont
		:gen_cast_integer_to_unsigned_char
			emit_movzx_rax_al()
			goto int2int_cast_cont
		:gen_cast_integer_to_signed_short
			emit_movsx_rax_ax()
			goto int2int_cast_cont
		:gen_cast_integer_to_unsigned_short
			emit_movzx_rax_ax()
			goto int2int_cast_cont
		:gen_cast_integer_to_signed_int
			emit_movsx_rax_eax()
			goto int2int_cast_cont
		:gen_cast_integer_to_unsigned_int
			emit_mov_eax_eax()
			goto int2int_cast_cont
	:gen_cast_to_struct
		; this is necessary because we add an implicit cast for return values
		; so if we didn't have this, we wouldn't be able to return structs.
		if *1from != TYPE_STRUCT goto bad_gen_cast
		from += 1
		to += 1
		if *8from != *8to goto bad_gen_cast
		return ; no casting needed; these are the same type
	:gen_cast_double_to_float
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		; movq xmm0, rax
		emit_movq_xmm0_rax()
		; cvtsd2ss xmm0, xmm0
		emit_cvtsd2ss_xmm0_xmm0()
		; movq rax, xmm0
		emit_movq_rax_xmm0()
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		return
	:gen_cast_float_to_double
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		; movq xmm0, rax
		emit_movq_xmm0_rax()
		; cvtss2sd xmm0, xmm0
		emit_cvtss2sd_xmm0_xmm0()
		; movq rax, xmm0
		emit_movq_rax_xmm0()
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		return
	:gen_cast_int_to_float
		; to reduce # of instructions, we first convert int to double, then double to float
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		; cvtsi2sd xmm0, rax
		emit_cvtsi2sd_xmm0_rax()
		; cvtsd2ss xmm0, xmm0
		emit_cvtsd2ss_xmm0_xmm0()
		; movq rax, xmm0
		emit_movq_rax_xmm0()
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		; it shouldn't matter that there's junk at [rsp+4]
		return
	:gen_cast_int_to_double
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		; cvtsi2sd xmm0, rax
		emit_cvtsi2sd_xmm0_rax()
		; movq rax, xmm0
		emit_movq_rax_xmm0()
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		return
	:gen_cast_float_to_int
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		; movq xmm0, rax
		emit_movq_xmm0_rax()
		; convert float to double, then double to int
		; cvtss2sd xmm0, xmm0
		emit_cvtss2sd_xmm0_xmm0()
		; cvttsd2si rax, xmm0
		emit_cvttsd2si_rax_xmm0()
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		return
	:gen_cast_double_to_int
		; mov rax, [rsp]
		emit_mov_rax_qword_rsp()
		; movq xmm0, rax
		emit_movq_xmm0_rax()
		; cvttsd2si rax, xmm0
		emit_cvttsd2si_rax_xmm0()
		; mov [rsp], rax
		emit_mov_qword_rsp_rax()
		return
	
	:bad_gen_cast
		print_statement_location(statement)
		puts(.str_bad_gen_cast1)
		print_type(from_type)
		puts(.str_bad_gen_cast2)
		print_type(to_type)
		putc(10)
		exit(1)
	:str_bad_gen_cast1
		string : Error: Cannot convert type
		byte 32
		byte 0
	:str_bad_gen_cast2
		string  to type
		byte 32
		byte 0
	
; push expr, casted to to_type, onto the stack
; returns pointer to end of expr
function generate_push_expression_casted
	argument statement
	argument expr
	argument to_type
	
	local from_type
	
	from_type = expr + 4
	from_type = *4from_type
	
	expr = generate_push_expression(statement, expr)
	generate_cast_top_of_stack(statement, from_type, to_type)
	return expr

; if type is a pointer type, returns the size of the underlying type
; otherwise, returns 1
;   this is so that (int *)p + 5 adds 20 to p, instead of 5
function scale_rax_for_addition_with
	argument type
	local p
	p = types + type
	if *1p != TYPE_POINTER goto return_0
	
	local n
	p = type + 1
	n = type_sizeof(p)
	; now scale rax by n
	emit_mov_rbx_imm64(n)
	emit_mul_rbx()
	return

; pop the top two things off of the stack, and push their sum
; the things should both have type `out_type` on the stack, but their original types are given by type1,2
function generate_stack_add
	argument statement ; for errors (currently unused)
	argument type1 ; type of 1st operand
	argument type2 ; type of 2nd operand
	argument out_type
	local p
	
	p = types + out_type
	if *1p == TYPE_FLOAT goto generate_add_floats
	if *1p == TYPE_DOUBLE goto generate_add_doubles
	
	emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp]   (second operand)
	scale_rax_for_addition_with(type1) ; in case this is a pointer addition
	emit_mov_reg(REG_RSI, REG_RAX)       ; mov rsi, rax
	emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]  (first operand)
	scale_rax_for_addition_with(type2) ; in case this is a pointer addition
	emit_mov_reg(REG_RBX, REG_RSI)       ; mov rbx, rsi
	emit_add_rax_rbx()                   ; add rax, rbx
	emit_add_rsp_imm32(8)                ; add rsp, 8
	emit_mov_qword_rsp_rax()             ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, out_type)
	return
	
	:generate_add_floats
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_movq_xmm1_xmm0()                ; movq xmm1, xmm0
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_addsd_xmm0_xmm1()               ; addsd xmm0, xmm1
		emit_cvtsd2ss_xmm0_xmm0()            ; cvtsd2ss xmm0, xmm0
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax			
		return
		
	:generate_add_doubles
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm1_rax()                 ; movq xmm1, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_addsd_xmm0_xmm1()               ; addsd xmm0, xmm1
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax			
		return

; pop the top two things off of the stack, and push their difference
; the things should both have type `out_type` on the stack, but their original types are given by type1,2
function generate_stack_sub
	argument statement ; for errors
	argument type1 ; type of 1st operand
	argument type2 ; type of 2nd operand
	argument out_type
	local p
	
	p = types + out_type
	if *1p == TYPE_FLOAT goto generate_sub_floats
	if *1p == TYPE_DOUBLE goto generate_sub_doubles
	p = types + type2
	if *1p == TYPE_POINTER goto generate_sub_pointers
	
	emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp]   (second operand)
	scale_rax_for_addition_with(type1) ; in case this is a pointer - integer subtraction
	emit_mov_reg(REG_RSI, REG_RAX)       ; mov rsi, rax
	emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]  (first operand)
	emit_mov_reg(REG_RBX, REG_RSI)       ; mov rbx, rsi
	emit_sub_rax_rbx()                   ; sub rax, rbx
	emit_add_rsp_imm32(8)                ; add rsp, 8
	emit_mov_qword_rsp_rax()             ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, out_type)
	return
	
	:generate_sub_pointers
		; pointer difference - need to divide by object size
		local sz1
		local sz2
		p = types + type1
		if *1p != TYPE_POINTER goto bad_pointer_diff
		p = type1 + 1
		sz1 = type_sizeof(p)
		p = type2 + 1
		sz2 = type_sizeof(p)
		if sz1 != sz2 goto bad_pointer_diff
		
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp]   (second operand)
		emit_mov_reg(REG_RSI, REG_RAX)       ; mov rsi, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]  (first operand)
		emit_mov_reg(REG_RBX, REG_RSI)       ; mov rbx, rsi
		emit_sub_rax_rbx()                   ; sub rax, rbx
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_rbx_imm64(sz1)              ; mov rbx, (object size)
		emit_zero_rdx()                      ; xor edx, edx
		emit_div_rbx()                       ; div rbx
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax
		return
		
		:bad_pointer_diff
			statement_error(statement, .str_bad_pointer_diff)
		:str_bad_pointer_diff
			string Subtraction of incompatible pointer types.
			byte 0
	:generate_sub_floats
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_movq_xmm1_xmm0()                ; movq xmm1, xmm0
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_subsd_xmm0_xmm1()               ; subsd xmm0, xmm1
		emit_cvtsd2ss_xmm0_xmm0()            ; cvtsd2ss xmm0, xmm0
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax			
		return
		
	:generate_sub_doubles
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm1_rax()                 ; movq xmm1, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_subsd_xmm0_xmm1()               ; subsd xmm0, xmm1
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax			
		return

; pop the top two things off of the stack, and push their product
function generate_stack_mul
	argument statement ; for errors
	argument type
	local p
	p = types + type
	if *1p == TYPE_FLOAT goto generate_mul_floats
	if *1p == TYPE_DOUBLE goto generate_mul_doubles

	emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)       ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]  (first operand)
	emit_mul_rbx()                       ; mul rbx
	emit_add_rsp_imm32(8)                ; add rsp, 8
	emit_mov_qword_rsp_rax()             ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return
	
	:generate_mul_floats
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_movq_xmm1_xmm0()                ; movq xmm1, xmm0
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_mulsd_xmm0_xmm1()               ; mulsd xmm0, xmm1
		emit_cvtsd2ss_xmm0_xmm0()            ; cvtsd2ss xmm0, xmm0
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax
		return
	
	:generate_mul_doubles
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm1_rax()                 ; movq xmm1, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_mulsd_xmm0_xmm1()               ; mulsd xmm0, xmm1
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax			
		return

; pop the top two things off of the stack, and push their quotient
function generate_stack_div
	argument statement ; for errors
	argument type
	local p
	local c
	p = types + type
	if *1p == TYPE_FLOAT goto generate_div_floats
	if *1p == TYPE_DOUBLE goto generate_div_doubles

	emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)       ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]  (first operand)
	c = *1p & 1
	if c == 1 goto generate_div_signed
		emit_zero_rdx()              ; xor edx, edx
		emit_div_rbx()               ; div rbx
		goto generate_div_cont
	:generate_div_signed
		emit_cqo()                   ; cqo
		emit_idiv_rbx()              ; idiv rbx
	:generate_div_cont
	emit_add_rsp_imm32(8)                ; add rsp, 8
	emit_mov_qword_rsp_rax()             ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return
	
	:generate_div_floats
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_movq_xmm1_xmm0()                ; movq xmm1, xmm0
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_cvtss2sd_xmm0_xmm0()            ; cvtss2sd xmm0, xmm0
		emit_divsd_xmm0_xmm1()               ; divsd xmm0, xmm1
		emit_cvtsd2ss_xmm0_xmm0()            ; cvtsd2ss xmm0, xmm0
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax
		return
	
	:generate_div_doubles
		emit_mov_rax_qword_rsp_plus_imm32(0) ; mov rax, [rsp] (second operand)
		emit_movq_xmm1_rax()                 ; movq xmm1, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8] (first operand)
		emit_movq_xmm0_rax()                 ; movq xmm0, rax
		emit_divsd_xmm0_xmm1()               ; divsd xmm0, xmm1
		emit_movq_rax_xmm0()                 ; movq rax, xmm0
		emit_add_rsp_imm32(8)                ; add rsp, 8
		emit_mov_qword_rsp_rax()             ; mov [rsp], rax			
		return

; pop the top two things off of the stack, and push their remainder
function generate_stack_remainder
	argument statement ; for errors
	argument type
	local p
	local c
	p = types + type
	emit_mov_rax_qword_rsp_plus_imm32(0)   ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)         ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8]  (first operand)
	c = *1p & 1
	if c == 1 goto generate_remainder_signed
		emit_zero_rdx()                ; xor edx, edx
		emit_div_rbx()                 ; div rbx
		emit_mov_reg(REG_RAX, REG_RDX) ; mov rax, rdx
		goto generate_remainder_cont
	:generate_remainder_signed
		emit_cqo()                     ; cqo
		emit_idiv_rbx()                ; idiv rbx
		emit_mov_reg(REG_RAX, REG_RDX) ; mov rax, rdx
	:generate_remainder_cont
	emit_add_rsp_imm32(8)                  ; add rsp, 8
	emit_mov_qword_rsp_rax()               ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return
	
; pop the top two things off of the stack, and push their bitwise and
function generate_stack_bitwise_and
	argument statement ; for errors
	argument type
	emit_mov_rax_qword_rsp_plus_imm32(0)   ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)         ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8]  (first operand)
	emit_and_rax_rbx()                     ; and rax, rbx
	emit_add_rsp_imm32(8)                  ; add rsp, 8
	emit_mov_qword_rsp_rax()               ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return

; pop the top two things off of the stack, and push their bitwise or
function generate_stack_bitwise_or
	argument statement ; for errors
	argument type
	emit_mov_rax_qword_rsp_plus_imm32(0)   ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)         ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8]  (first operand)
	emit_or_rax_rbx()                     ; or rax, rbx
	emit_add_rsp_imm32(8)                  ; add rsp, 8
	emit_mov_qword_rsp_rax()               ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return

; pop the top two things off of the stack, and push their bitwise xor
function generate_stack_bitwise_xor
	argument statement ; for errors
	argument type
	emit_mov_rax_qword_rsp_plus_imm32(0)   ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)         ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8]  (first operand)
	emit_xor_rax_rbx()                     ; xor rax, rbx
	emit_add_rsp_imm32(8)                  ; add rsp, 8
	emit_mov_qword_rsp_rax()               ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return


function generate_stack_lshift
	argument statement ; for errors
	argument type
	emit_mov_rax_qword_rsp_plus_imm32(0)   ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RCX, REG_RAX)         ; mov rcx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8]  (first operand)
	emit_shl_rax_cl()                      ; shl rax, cl
	emit_add_rsp_imm32(8)                  ; add rsp, 8
	emit_mov_qword_rsp_rax()               ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return


function generate_stack_rshift
	argument statement ; for errors
	argument type
	local p
	local c
	p = types + type
	c = *1p & 1
	
	emit_mov_rax_qword_rsp_plus_imm32(0)   ; mov rax, [rsp]   (second operand)
	emit_mov_reg(REG_RCX, REG_RAX)         ; mov rcx, rax
	emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8]  (first operand)
	if c == 1 goto gen_rshift_signed
		emit_shr_rax_cl()              ; shr rax, cl
		goto gen_rshift_cont
	:gen_rshift_signed
		emit_sar_rax_cl()              ; sar rax, cl
	:gen_rshift_cont
	emit_add_rsp_imm32(8)                  ; add rsp, 8
	emit_mov_qword_rsp_rax()               ; mov [rsp], rax
	generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
	return

; pop a pointer off of the stack, then push the dereferenced value according to `type`
function generate_stack_dereference
	argument statement ; for errors
	argument type
	local p
	local size
	local c
	
	size = type_sizeof(type)
	if size == 1 goto gen_deref1
	if size == 2 goto gen_deref2
	if size == 4 goto gen_deref4
	if size == 8 goto gen_deref8
	
	emit_pop_rax()                  ; pop rax
	emit_mov_reg(REG_RSI, REG_RAX)  ; mov rsi, rax
	c = round_up_to_8(size)
	emit_sub_rsp_imm32(c)           ; sub rsp, (size)
	emit_mov_reg(REG_RDI, REG_RSP)  ; mov rdi, rsp
	emit_mov_rax_imm64(size)        ; mov rax, (size)
	emit_mov_reg(REG_RCX, REG_RAX)  ; mov rcx, rax
	emit_rep_movsb()                ; rep movsb
	return
	
	:gen_deref_cast
		emit_mov_qword_rsp_rax()
		p = types + type
		if *1p >= TYPE_LONG goto return_0
		generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
		return
	:gen_deref1
		emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
		emit_mov_al_byte_rbx()         ; mov al, [rbx]
		goto gen_deref_cast
	:gen_deref2
		emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
		emit_mov_ax_word_rbx()         ; mov ax, [rbx]
		goto gen_deref_cast
	:gen_deref4
		emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
		emit_mov_eax_dword_rbx()       ; mov eax, [rbx]
		goto gen_deref_cast
	:gen_deref8
		emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
		emit_mov_rax_qword_rbx()       ; mov rax, [rbx]
		goto gen_deref_cast

; pop address off of stack, pop value, set *address to value, then push value
function generate_stack_assign
	argument statement
	argument type
	local size
	size = type_sizeof(type)
	
	
	
	if size == 1 goto gen_assign1
	if size == 2 goto gen_assign2
	if size == 4 goto gen_assign4
	if size == 8 goto gen_assign8
	
	emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
	emit_mov_reg(REG_RDI, REG_RAX) ; mov rdi, rax
	emit_add_rsp_imm32(8)          ; add rsp, 8
	emit_mov_reg(REG_RSI, REG_RSP) ; mov rsi, rsp
	emit_mov_rax_imm64(size)       ; mov rax, (size)
	emit_mov_reg(REG_RCX, REG_RAX) ; mov rcx, rax
	emit_rep_movsb()               ; rep movsb
	return
	
	:gen_assign_ret
		emit_add_rsp_imm32(8) ; pop address
		return
	:gen_assign1
		emit_mov_rax_qword_rsp()             ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX)       ; mov rbx, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]
		emit_mov_byte_rbx_al()               ; mov [rbx], al
		goto gen_assign_ret
	:gen_assign2
		emit_mov_rax_qword_rsp()             ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX)       ; mov rbx, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]
		emit_mov_word_rbx_ax()               ; mov [rbx], ax
		goto gen_assign_ret
	:gen_assign4
		emit_mov_rax_qword_rsp()             ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX)       ; mov rbx, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]
		emit_mov_dword_rbx_eax()             ; mov [rbx], eax
		goto gen_assign_ret
	:gen_assign8
		emit_mov_rax_qword_rsp()             ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX)       ; mov rbx, rax
		emit_mov_rax_qword_rsp_plus_imm32(8) ; mov rax, [rsp+8]
		emit_mov_qword_rbx_rax()             ; mov [rbx], rax
		goto gen_assign_ret

; pop from the stack and compare with 0 (setting eflags appropriately)
function generate_stack_compare_against_zero
	argument statement
	argument type
	local p
	p = types + type
	if *1p == TYPE_FLOAT goto cmp_zero_float
	if *1p == TYPE_DOUBLE goto cmp_zero_double
	emit_add_rsp_imm32(8)                 ; add rsp, 8
	emit_mov_rax_qword_rsp_plus_imm32(-8) ; mov rax, [rsp-8]
	emit_test_rax_rax()                   ; test rax, rax
	return
	:cmp_zero_float
		generate_cast_top_of_stack(statement, TYPE_FLOAT, TYPE_DOUBLE) ; cast to double for comparison
	:cmp_zero_double	
		emit_add_rsp_imm32(8)                  ; add rsp, 8
		emit_zero_rax()                        ; xor eax, eax
		emit_movq_xmm1_rax()                   ; movq xmm1, rax
		emit_mov_rax_qword_rsp_plus_imm32(-8)  ; mov rax, [rsp-8]
		emit_movq_xmm0_rax()                   ; movq xmm0, rax
		emit_comisd_xmm0_xmm1()                ; comisd xmm0, xmm1
		return

; returns pointer to end of expr
function generate_push_address_of_expression
	argument statement ; for errors
	argument expr
	
	local c
	local d
	
	c = *1expr
	
	if c == EXPRESSION_GLOBAL_VARIABLE goto addrof_global_var
	if c == EXPRESSION_LOCAL_VARIABLE goto addrof_local_var
	if c == EXPRESSION_DEREFERENCE goto addrof_dereference
	if c == EXPRESSION_SUBSCRIPT goto addrof_subscript
	if c == EXPRESSION_DOT goto addrof_dot
	if c == EXPRESSION_ARROW goto addrof_arrow
	
	statement_error(statement, .str_bad_lvalue)
	:str_bad_lvalue
		string Bad l-value.
		byte 0
	:addrof_global_var
		expr += 8
		emit_mov_rax_imm64(*4expr)
		emit_push_rax()
		expr += 8
		return expr
	:addrof_local_var
		expr += 8
		emit_lea_rax_rbp_plus_imm32(*4expr)
		emit_push_rax()
		expr += 8
		return expr
	:addrof_dereference
		expr += 8
		return generate_push_expression(statement, expr)
	:addrof_subscript
		expr += 8
		c = expr + 4 ; type 1
		c = *4c
		expr = generate_push_expression(statement, expr)
		d = expr + 4 ; type 2
		d = *4d
		expr = generate_push_expression(statement, expr)
		generate_stack_add(statement, c, d, c)
		return expr
	:addrof_dot
		expr += 8
		expr = generate_push_address_of_expression(statement, expr)
		goto addrof_dot_cont
	:addrof_arrow
		expr += 8
		expr = generate_push_expression(statement, expr)
		:addrof_dot_cont
		emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
		emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
		emit_mov_rax_imm64(*4expr)     ; mov rax, (offset to member)
		emit_add_rax_rbx()             ; add rax, rbx
		emit_mov_qword_rsp_rax()       ; mov [rsp], rax
		expr += 8
		return expr

; pop the top two things off the stack of type `type` and compare them
function generate_stack_compare
	argument statement
	argument type
	local p
	p = types + type
	
	emit_add_rsp_imm32(16)                 ; add rsp, 16
	
	; NB: we do float comparisons as double comparisons (see function comparison_type)
	if *1p == TYPE_DOUBLE goto stack_compare_double
	if *1p > TYPE_UNSIGNED_LONG goto stack_compare_bad
	
	emit_mov_rax_qword_rsp_plus_imm32(-16) ; mov rax, [rsp-16] (second operand)
	emit_mov_reg(REG_RBX, REG_RAX)         ; mov rbx, rax
	emit_mov_rax_qword_rsp_plus_imm32(-8)  ; mov rax, [rsp-8] (first operand)
	emit_cmp_rax_rbx()                     ; cmp rax, rbx
	return
	
	:stack_compare_bad
		die(.str_stack_compare_bad)
		:str_stack_compare_bad
			string Bad types passed to generate_stack_compare()
			byte 0
	:stack_compare_double
		emit_mov_rax_qword_rsp_plus_imm32(-8)  ; mov rax, [rsp-8] (first operand)
		emit_movq_xmm0_rax()                   ; movq xmm0, rax
		emit_mov_rax_qword_rsp_plus_imm32(-16) ; mov rax, [rsp-16] (second operand)
		emit_movq_xmm1_rax()                   ; movq xmm1, rax
		emit_comisd_xmm0_xmm1()                ; comisd xmm0, xmm1
		return
	
; given that expr is a comparison expression, which type should both operands be converted to?
function comparison_type
	argument statement
	argument expr
	local type1
	local type2
	local t1
	local t2
	expr += 8
	
	type1 = expr + 4
	type1 = *4type1
	expr = expression_get_end(expr)
	type2 = expr + 4
	type2 = *4type2
	
	t1 = types + type1
	t1 = *1t1
	t2 = types + type2
	t2 = *1t2
	
	; do float comparisons as double comparisons to make things simpler
	if t1 == TYPE_FLOAT goto return_type_double
	if t2 == TYPE_FLOAT goto return_type_double
	
	if t1 == TYPE_POINTER goto return_type_unsigned_long
	if t2 == TYPE_POINTER goto return_type_unsigned_long
	if t1 == TYPE_ARRAY goto return_type_unsigned_long ; type1 decays to pointer
	if t2 == TYPE_ARRAY goto return_type_unsigned_long ; type2 decays to pointer
	return expr_binary_type_usual_conversions(statement, type1, type2)

; is this comparison expression a comparison between unsigned integers or floats?
function comparison_is_unsigned
	argument statement
	argument expr
	local type
	type = comparison_type(statement, expr)
	type += types
	type = *1type
	if type > TYPE_UNSIGNED_LONG goto return_1 ; double comparisons use setb/seta not setl/setg
	type &= 1
	if type == 0 goto return_1
	return 0
	
; `statement` is used for errors
; returns pointer to end of expression
function generate_push_expression
	argument statement
	argument expr
	local b
	local c
	local d
	local p
	local comparison
	local addr1
	local addr2
	local type
	
	type = expr + 4
	type = *4type
	
	c = *1expr
	
	if c == EXPRESSION_CONSTANT_INT goto generate_int
	if c == EXPRESSION_CONSTANT_FLOAT goto generate_float
	if c == EXPRESSION_GLOBAL_VARIABLE goto generate_global_variable
	if c == EXPRESSION_LOCAL_VARIABLE goto generate_local_variable
	if c == EXPRESSION_FUNCTION goto generate_function_addr
	if c == EXPRESSION_CAST goto generate_cast
	if c == EXPRESSION_UNARY_PLUS goto generate_cast ; the unary plus operator just casts to the promoted type
	if c == EXPRESSION_UNARY_MINUS goto generate_unary_minus
	if c == EXPRESSION_BITWISE_NOT goto generate_unary_bitwise_not
	if c == EXPRESSION_LOGICAL_NOT goto generate_unary_logical_not
	if c == EXPRESSION_ADD goto generate_add
	if c == EXPRESSION_SUB goto generate_sub
	if c == EXPRESSION_MUL goto generate_mul
	if c == EXPRESSION_DIV goto generate_div
	if c == EXPRESSION_REMAINDER goto generate_remainder
	if c == EXPRESSION_BITWISE_AND goto generate_bitwise_and
	if c == EXPRESSION_BITWISE_OR goto generate_bitwise_or
	if c == EXPRESSION_BITWISE_XOR goto generate_bitwise_xor
	if c == EXPRESSION_LSHIFT goto generate_lshift
	if c == EXPRESSION_RSHIFT goto generate_rshift
	if c == EXPRESSION_EQ goto generate_eq
	if c == EXPRESSION_NEQ goto generate_neq
	if c == EXPRESSION_LT goto generate_lt
	if c == EXPRESSION_GT goto generate_gt
	if c == EXPRESSION_LEQ goto generate_leq
	if c == EXPRESSION_GEQ goto generate_geq
	if c == EXPRESSION_ASSIGN goto generate_assign
	if c == EXPRESSION_ASSIGN_ADD goto generate_assign_add
	if c == EXPRESSION_ASSIGN_SUB goto generate_assign_sub
	if c == EXPRESSION_ASSIGN_MUL goto generate_assign_mul
	if c == EXPRESSION_ASSIGN_DIV goto generate_assign_div
	if c == EXPRESSION_ASSIGN_REMAINDER goto generate_assign_remainder
	if c == EXPRESSION_ASSIGN_LSHIFT goto generate_assign_lshift
	if c == EXPRESSION_ASSIGN_RSHIFT goto generate_assign_rshift
	if c == EXPRESSION_ASSIGN_AND goto generate_assign_and
	if c == EXPRESSION_ASSIGN_OR goto generate_assign_or
	if c == EXPRESSION_ASSIGN_XOR goto generate_assign_xor
	if c == EXPRESSION_POST_INCREMENT goto generate_post_increment
	if c == EXPRESSION_POST_DECREMENT goto generate_post_decrement
	if c == EXPRESSION_PRE_INCREMENT goto generate_pre_increment
	if c == EXPRESSION_PRE_DECREMENT goto generate_pre_decrement
	if c == EXPRESSION_DEREFERENCE goto generate_dereference
	if c == EXPRESSION_SUBSCRIPT goto generate_subscript
	if c == EXPRESSION_ADDRESS_OF goto generate_address_of
	if c == EXPRESSION_DOT goto generate_dot_or_arrow
	if c == EXPRESSION_ARROW goto generate_dot_or_arrow
	if c == EXPRESSION_COMMA goto generate_comma
	if c == EXPRESSION_CALL goto generate_call
	if c == EXPRESSION_LOGICAL_AND goto generate_logical_and
	if c == EXPRESSION_LOGICAL_OR goto generate_logical_or
	if c == EXPRESSION_CONDITIONAL goto generate_conditional
	
	putnln(c)	
	die(.str_genpushbadexpr)
	:str_genpushbadexpr
		string Internal compiler error: bad expression passed to generate_push_expression.
		byte 0
	:generate_cast
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		return expr
	:generate_function_addr
		d = 1 ; default to address of 1, not 0 because that will be optimized to xor eax, eax
		expr += 8
		if codegen_second_pass == 0 goto function_noaddr
		d = ident_list_lookup(functions_addresses, *8expr)
		if d == 0 goto no_such_function
		:function_noaddr
		expr += 8
		emit_mov_rax_imm64(d)
		emit_push_rax()
		return expr
		
		:no_such_function
			print_statement_location(statement)
			puts(.str_no_such_function)
			putsln(*8expr)
			exit(1)
			:str_no_such_function
				string : Error: No such function:
				byte 32
				byte 0
	:generate_unary_minus
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		p = types + type
		if *1p == TYPE_FLOAT goto generate_unary_minus_float
		if *1p == TYPE_DOUBLE goto generate_unary_minus_double
		; it's just an integer
		emit_mov_rax_qword_rsp() ; mov rax, [rsp]
		emit_neg_rax()           ; neg rax
		emit_mov_qword_rsp_rax() ; mov [rsp], rax
		return expr
		; "negate(x) copies a floating-point operand x to a destination in the same format, reversing the sign bit." IEEE 754 § 5.5.1
		:generate_unary_minus_float
			c = 1 < 31 ; sign bit for floats
			goto generate_unary_minus_floating
		:generate_unary_minus_double
			c = 1 < 63 ; sign bit for doubles
			:generate_unary_minus_floating
			emit_mov_rax_qword_rsp()       ; mov rax, [rsp]
			emit_mov_reg(REG_RBX, REG_RAX) ; mov rbx, rax
			emit_mov_rax_imm64(c)          ; mov rax, (sign bit)
			emit_xor_rax_rbx()             ; xor rax, rbx
			emit_mov_qword_rsp_rax()       ; mov [rsp], rax
			return expr
	:generate_unary_bitwise_not
		expr += 8
		expr = generate_push_expression(statement, expr) ; we'll cast after we take the bitwise not.
		emit_mov_rax_qword_rsp()  ; mov rax, [rsp]
		emit_not_rax()            ; not rax
		emit_mov_qword_rsp_rax()  ; mov [rsp], rax
		generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
		return expr
	:generate_address_of
		expr += 8
		expr = generate_push_address_of_expression(statement, expr)
		return expr
	:generate_assign
		expr += 8
		b = type_is_array(type)
		if b != 0 goto assign_array
		p = expression_get_end(expr)
		c = p
		; it makes things a lot easier if we push the rhs of the assignment first -- also
		;      *f() = g()
		;   it might be required to call g first? (something something sequence points)
		p = generate_push_expression_casted(statement, p, type)
		d = generate_push_address_of_expression(statement, expr)
		if c != d goto exprend_wrong
		expr = p
		generate_stack_assign(statement, type)
		return expr
		:assign_array
			statement_error(statement, .str_assign_array)
		:str_assign_array
			string Assigning to array.
			byte 0
	
	
	
		:exprend_wrong
			die(.str_exprend_wrong)
		:str_exprend_wrong
			string Internal compiler error: expression_get_end disagrees with generate_push_expression.
			byte 0
	:generate_assign_add
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (addend)
		emit_push_rax()                       ; push rax
		generate_stack_add(statement, type, type, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, addend)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_sub
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_sub(statement, type, type, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_mul
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_mul(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_div
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_div(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_remainder
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_remainder(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_and
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_bitwise_and(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_or
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_bitwise_or(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_xor
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_bitwise_xor(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_lshift
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_lshift(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_assign_rshift
		expr += 8
		p = expression_get_end(expr)
		p = generate_push_expression_casted(statement, p, type)
		generate_push_address_of_expression(statement, expr)
		expr = p
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (address)
		emit_push_rax()                       ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(16) ; mov rax, [rsp+16] (2nd operand)
		emit_push_rax()                       ; push rax
		generate_stack_rshift(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)  ; mov rax, [rsp+8] (address)
		emit_push_rax()                       ; push rax
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()              ; mov rax, [rsp] (result)
		emit_add_rsp_imm32(16)                ; add rsp, 16 (pop address, 2nd operand)
		emit_mov_qword_rsp_rax()              ; mov [rsp], rax
		return expr
	:generate_post_increment
		expr += 8
		expr = generate_push_address_of_expression(statement, expr)
		
		p = types + type
		if *1p == TYPE_FLOAT goto post_increment_float
		if *1p == TYPE_DOUBLE goto post_increment_double
		
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (address)
		emit_push_rax()                        ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_imm64(1)                  ; mov rax, 1
		scale_rax_for_addition_with(type) ; in case this is a pointer increment
		emit_mov_reg(REG_RBX, REG_RAX)
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (value)
		emit_add_rax_rbx()                     ; add rax, rbx
		emit_push_rax()                        ; push rax
		emit_mov_rax_qword_rsp_plus_imm32(16)  ; mov rax, [rsp+16] (address)
		emit_push_rax()
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8] (old value)
		emit_mov_qword_rsp_plus_imm32_rax(16)  ; mov [rsp+16], rax
		emit_add_rsp_imm32(16)                 ; add rsp, 16
		return expr
		
		:post_increment_float
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_eax_dword_rbx()        ; mov eax, [rbx]
			emit_mov_reg(REG_RSI, REG_RAX)  ; mov rsi, rax (save old value)
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_cvtss2sd_xmm0_xmm0()       ; cvtss2sd xmm0, xmm0
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_addsd_xmm0_xmm1()          ; addsd xmm0, xmm1
			emit_cvtsd2ss_xmm0_xmm0()       ; cvtsd2ss xmm0, xmm0
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_dword_rbx_eax()        ; mov [rbx], eax
			emit_mov_reg(REG_RAX, REG_RSI)  ; mov rax, rsi (old value)
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
		:post_increment_double
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_rax_qword_rbx()        ; mov rax, [rbx]
			emit_mov_reg(REG_RSI, REG_RAX)  ; mov rsi, rax (save old value)
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_addsd_xmm0_xmm1()          ; addsd xmm0, xmm1
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_qword_rbx_rax()        ; mov [rbx], rax
			emit_mov_reg(REG_RAX, REG_RSI)  ; mov rax, rsi (old value)
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
	:generate_post_decrement
		expr += 8
		expr = generate_push_address_of_expression(statement, expr)
		
		p = types + type
		if *1p == TYPE_FLOAT goto post_decrement_float
		if *1p == TYPE_DOUBLE goto post_decrement_double
		
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (address)
		emit_push_rax()                        ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_imm64(1)                  ; mov rax, 1
		scale_rax_for_addition_with(type) ; in case this is a pointer decrement
		emit_mov_reg(REG_RBX, REG_RAX)
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (value)
		emit_sub_rax_rbx()                     ; sub rax, rbx
		emit_push_rax()                        ; push rax
		emit_mov_rax_qword_rsp_plus_imm32(16)  ; mov rax, [rsp+16] (address)
		emit_push_rax()
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp_plus_imm32(8)   ; mov rax, [rsp+8] (old value)
		emit_mov_qword_rsp_plus_imm32_rax(16)  ; mov [rsp+16], rax
		emit_add_rsp_imm32(16)                 ; add rsp, 16
		return expr
		
		:post_decrement_float
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_eax_dword_rbx()        ; mov eax, [rbx]
			emit_mov_reg(REG_RSI, REG_RAX)  ; mov rsi, rax (save old value)
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_cvtss2sd_xmm0_xmm0()       ; cvtss2sd xmm0, xmm0
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_subsd_xmm0_xmm1()          ; subsd xmm0, xmm1
			emit_cvtsd2ss_xmm0_xmm0()       ; cvtsd2ss xmm0, xmm0
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_dword_rbx_eax()        ; mov [rbx], eax
			emit_mov_reg(REG_RAX, REG_RSI)  ; mov rax, rsi (old value)
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
		:post_decrement_double
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_rax_qword_rbx()        ; mov rax, [rbx]
			emit_mov_reg(REG_RSI, REG_RAX)  ; mov rsi, rax (save old value)
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_subsd_xmm0_xmm1()          ; subsd xmm0, xmm1
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_qword_rbx_rax()        ; mov [rbx], rax
			emit_mov_reg(REG_RAX, REG_RSI)  ; mov rax, rsi (old value)
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
	:generate_pre_increment
		expr += 8
		expr = generate_push_address_of_expression(statement, expr)
		
		p = types + type
		if *1p == TYPE_FLOAT goto pre_increment_float
		if *1p == TYPE_DOUBLE goto pre_increment_double
		
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (address)
		emit_push_rax()                        ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_imm64(1)                  ; mov rax, 1
		scale_rax_for_addition_with(type) ; in case this is a pointer increment
		emit_mov_reg(REG_RBX, REG_RAX)
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (value)
		emit_add_rax_rbx()                     ; add rax, rbx
		emit_push_rax()                        ; push rax
		emit_mov_rax_qword_rsp_plus_imm32(16)  ; mov rax, [rsp+16] (address)
		emit_push_rax()
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (value)
		emit_mov_qword_rsp_plus_imm32_rax(16)  ; mov [rsp+16], rax
		emit_add_rsp_imm32(16)                 ; add rsp, 16
		return expr
		
		:pre_increment_float
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_eax_dword_rbx()        ; mov eax, [rbx]
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_cvtss2sd_xmm0_xmm0()       ; cvtss2sd xmm0, xmm0
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_addsd_xmm0_xmm1()          ; addsd xmm0, xmm1
			emit_cvtsd2ss_xmm0_xmm0()       ; cvtsd2ss xmm0, xmm0
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_dword_rbx_eax()        ; mov [rbx], eax
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
		:pre_increment_double
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_rax_qword_rbx()        ; mov rax, [rbx]
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_addsd_xmm0_xmm1()          ; addsd xmm0, xmm1
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_qword_rbx_rax()        ; mov [rbx], rax
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
			
	:generate_pre_decrement
		expr += 8
		expr = generate_push_address_of_expression(statement, expr)
		
		p = types + type
		if *1p == TYPE_FLOAT goto pre_decrement_float
		if *1p == TYPE_DOUBLE goto pre_decrement_double
		
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (address)
		emit_push_rax()                        ; push rax
		generate_stack_dereference(statement, type)
		emit_mov_rax_imm64(1)                  ; mov rax, 1
		scale_rax_for_addition_with(type) ; in case this is a pointer decrement
		emit_mov_reg(REG_RBX, REG_RAX)
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (value)
		emit_sub_rax_rbx()                     ; sub rax, rbx
		emit_push_rax()                        ; push rax
		emit_mov_rax_qword_rsp_plus_imm32(16)  ; mov rax, [rsp+16] (address)
		emit_push_rax()
		generate_stack_assign(statement, type)
		emit_mov_rax_qword_rsp()               ; mov rax, [rsp] (value)
		emit_mov_qword_rsp_plus_imm32_rax(16)  ; mov [rsp+16], rax
		emit_add_rsp_imm32(16)                 ; add rsp, 16
		return expr
		
		:pre_decrement_float
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_eax_dword_rbx()        ; mov eax, [rbx]
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_cvtss2sd_xmm0_xmm0()       ; cvtss2sd xmm0, xmm0
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_subsd_xmm0_xmm1()          ; subsd xmm0, xmm1
			emit_cvtsd2ss_xmm0_xmm0()       ; cvtsd2ss xmm0, xmm0
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_dword_rbx_eax()        ; mov [rbx], eax
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
		:pre_decrement_double
			emit_mov_rax_qword_rsp()        ; mov rax, [rsp] (address)
			emit_mov_reg(REG_RBX, REG_RAX)  ; mov rbx, rax
			emit_mov_rax_qword_rbx()        ; mov rax, [rbx]
			emit_movq_xmm0_rax()            ; movq xmm0, rax
			emit_mov_rax_imm64(0x3ff0000000000000) ; mov rax, 1.0
			emit_movq_xmm1_rax()            ; movq xmm1, rax
			emit_subsd_xmm0_xmm1()          ; subsd xmm0, xmm1
			emit_movq_rax_xmm0()            ; mov rax, xmm0
			emit_mov_qword_rbx_rax()        ; mov [rbx], rax
			emit_mov_qword_rsp_rax()        ; mov [rsp], rax
			return expr
			
	:generate_add
		expr += 8
		c = expr + 4 ; type of 1st operand
		expr = generate_push_expression_casted(statement, expr, type)
		d = expr + 4 ; type of 2nd operand
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_add(statement, *4c, *4d, type)
		return expr
	:generate_sub
		expr += 8
		c = expr + 4 ; type of 1st operand
		expr = generate_push_expression_casted(statement, expr, type)
		d = expr + 4 ; type of 2nd operand
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_sub(statement, *4c, *4d, type)
		return expr
	:generate_mul
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_mul(statement, type)
		return expr
	:generate_div
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_div(statement, type)
		return expr
	:generate_remainder
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_remainder(statement, type)
		return expr
	:generate_bitwise_and
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_bitwise_and(statement, type)
		return expr
	:generate_bitwise_or
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_bitwise_or(statement, type)
		return expr
	:generate_bitwise_xor
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_bitwise_xor(statement, type)
		return expr
	:generate_lshift
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_lshift(statement, type)
		return expr
	:generate_rshift
		expr += 8
		expr = generate_push_expression_casted(statement, expr, type)
		expr = generate_push_expression_casted(statement, expr, type)
		generate_stack_rshift(statement, type)
		return expr
	:generate_unary_logical_not
		expr += 8
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		generate_stack_compare_against_zero(statement, *4p)
		emit_setC_rax(COMPARISON_E)   ; sete al ; movzx rax, al
		emit_push_rax()               ; push rax
		return expr
	:generate_eq
		comparison = COMPARISON_E
		goto generate_comparison
	:generate_neq
		comparison = COMPARISON_NE
		goto generate_comparison
	:generate_lt
		b = comparison_is_unsigned(statement, expr)
		if b != 0 goto comparison_b
			comparison = COMPARISON_L
			goto generate_comparison
		:comparison_b
			comparison = COMPARISON_B
			goto generate_comparison
	:generate_leq
		b = comparison_is_unsigned(statement, expr)
		if b != 0 goto comparison_be
			comparison = COMPARISON_LE
			goto generate_comparison
		:comparison_be
			comparison = COMPARISON_BE
			goto generate_comparison
	:generate_gt
		b = comparison_is_unsigned(statement, expr)
		if b != 0 goto comparison_a
			comparison = COMPARISON_G
			goto generate_comparison
		:comparison_a
			comparison = COMPARISON_A
			goto generate_comparison
	:generate_geq
		b = comparison_is_unsigned(statement, expr)
		if b != 0 goto comparison_ae
			comparison = COMPARISON_GE
			goto generate_comparison
		:comparison_ae
			comparison = COMPARISON_AE
			goto generate_comparison
	:generate_comparison
		c = comparison_type(statement, expr)
		expr += 8
		expr = generate_push_expression_casted(statement, expr, c)
		expr = generate_push_expression_casted(statement, expr, c)
		generate_stack_compare(statement, c)
		emit_setC_rax(comparison)
		emit_push_rax()
		return expr
	:generate_conditional
		expr += 8
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		generate_stack_compare_against_zero(statement, *4p)
		emit_je_rel32(0) ; temporary je +0 (correct offset will be filled in)
		addr1 = code_output
		expr = generate_push_expression_casted(statement, expr, type)
		emit_jmp_rel32(0) ; temporary jmp +0 (correct offset will be filled in)
		addr2 = code_output
		; fill in jump offset
		d = addr2 - addr1
		addr1 -= 4
		*4addr1 = d
		
		addr1 = code_output
		expr = generate_push_expression_casted(statement, expr, type)
		addr2 = code_output
		; fill in jump offset
		d = addr2 - addr1
		addr1 -= 4
		*4addr1 = d
		return expr
	
	:generate_logical_and
		expr += 8
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		generate_stack_compare_against_zero(statement, *4p)
		emit_je_rel32(0) ; temporary je +0; offset will be filled in later
		addr1 = code_output
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		generate_stack_compare_against_zero(statement, *4p)
		
		emit_je_rel32(15)         ; je +15 (10 bytes for mov rax, 1; 5 bytes for jmp +2)
		emit_mov_rax_imm64(1)     ; mov rax, 1
		emit_jmp_rel32(2)         ; jmp +2 (skip xor rax, rax)
		addr2 = code_output
		emit_zero_rax()           ; xor rax, rax
		emit_push_rax()           ; push rax
		
		; fill in jump offset
		d = addr2 - addr1
		addr1 -= 4
		*4addr1 = d
		return expr
	
	:generate_logical_or
		expr += 8
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		generate_stack_compare_against_zero(statement, *4p)
		emit_jne_rel32(0) ; temporary jne +0; offset will be filled in later
		addr1 = code_output
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		generate_stack_compare_against_zero(statement, *4p)
		
		emit_jne_rel32(7)         ; jne +7 (2 bytes for xor eax, eax; 5 bytes for jmp +10)
		emit_zero_rax()           ; xor eax, eax
		emit_jmp_rel32(10)        ; jmp +10 (skip mov rax, 1)
		addr2 = code_output
		emit_mov_rax_imm64(1)     ; mov rax, 1
		emit_push_rax()           ; push rax
		
		; fill in jump offset
		d = addr2 - addr1
		addr1 -= 4
		*4addr1 = d
		return expr
	
	:generate_global_variable
		expr += 8
		d = *4expr ; address
		expr += 4
		b = *4expr ; is array?
		expr += 4
		if b != 0 goto global_var_array
		c = type_sizeof(type)
		if c > 8 goto global_var_large
		emit_mov_rbx_imm64(d)    ; mov rbx, (address)
		emit_mov_rax_qword_rbx() ; mov rax, [rbx]
		emit_push_rax()          ; push rax
		p = types + type
		if *1p < TYPE_LONG goto global_var_needs_cast
		return expr
		:global_var_needs_cast
			; we need to sign extend 8/16/32-bit signed global variables to 64 bits
			generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
			return expr
		:global_var_large
			c = round_up_to_8(c)
			emit_sub_rsp_imm32(c)          ; sub rsp, (size)
			emit_mov_reg(REG_RDI, REG_RSP) ; mov rdi, rsp
			emit_mov_rax_imm64(d)          ; mov rax, (address)
			emit_mov_reg(REG_RSI, REG_RAX) ; mov rsi, rax
			emit_mov_rax_imm64(c)          ; mov rax, (size)
			emit_mov_reg(REG_RCX, REG_RAX) ; mov rcx, rax
			emit_rep_movsb()               ; rep movsb
			return expr
		:global_var_array
			; just push the address of the array
			emit_mov_rax_imm64(d) ; mov rax, (address)
			emit_push_rax()       ; push rax
			return expr
	:generate_dereference
		expr += 8
		p = expr + 4
		expr = generate_push_expression(statement, expr)
		p = types + *4p
		if *2p == TYPE2_FUNCTION_POINTER goto deref_function_pointer
		generate_stack_dereference(statement, type)
		:deref_function_pointer  ; dereferencing a function pointer does NOTHING
		return expr
	:generate_subscript
		expr += 8
		c = expr + 4 ; type 1
		c = *4c
		expr = generate_push_expression(statement, expr)
		d = expr + 4 ; type 2
		d = *4d
		expr = generate_push_expression(statement, expr)
		generate_stack_add(statement, c, d, c)
		generate_stack_dereference(statement, type)
		return expr
	:generate_dot_or_arrow
		; @NONSTANDARD: we require that the 1st operand to . be an lvalue
		;   e.g.   int thing = function_which_returns_struct().x;
		; is not allowed
		p = expr + 8
		p = expression_get_end(p)
		p += 4
		expr = generate_push_address_of_expression(statement, expr)
		if *4p == 1 goto member_array
		generate_stack_dereference(statement, type)
		:member_array
		return expr
	:generate_local_variable
		expr += 8
		d = sign_extend_32_to_64(*4expr) ; rbp offset
		expr += 4
		b = *4expr ; is array?
		expr += 4
		if b != 0 goto local_var_array
		c = type_sizeof(type)
		if c > 8 goto local_var_large
		emit_mov_rax_qword_rbp_plus_imm32(d) ; mov rax, [rbp+X]
		emit_push_rax()                      ; push rax
		p = types + type
		if *1p < TYPE_LONG goto local_var_needs_cast
		return expr
		:local_var_needs_cast
			generate_cast_top_of_stack(statement, TYPE_UNSIGNED_LONG, type)
			return expr
		:local_var_large
			c = round_up_to_8(c)
			emit_sub_rsp_imm32(c)          ; sub rsp, (size)
			emit_mov_reg(REG_RDI, REG_RSP) ; mov rdi, rsp
			emit_lea_rax_rbp_plus_imm32(d) ; lea rax, [rbp+X]
			emit_mov_reg(REG_RSI, REG_RAX) ; mov rsi, rax
			emit_mov_rax_imm64(c)          ; mov rax, (size)
			emit_mov_reg(REG_RCX, REG_RAX) ; mov rcx, rax
			emit_rep_movsb()               ; rep movsb
			return expr
		:local_var_array
			; push address of array instead of array
			emit_lea_rax_rbp_plus_imm32(d) ; lea rax, [rbp+X]
			emit_push_rax()                ; push rax
			return expr
	:generate_float
		expr += 8
		emit_mov_rax_imm64(*8expr)
		emit_push_rax()
		generate_cast_top_of_stack(statement, TYPE_DOUBLE, type)
		expr += 8
		return expr
	:generate_int
		expr += 8
		emit_mov_rax_imm64(*8expr)
		emit_push_rax()
		expr += 8
		return expr
	:generate_comma
		expr += 8
		c = expr + 4 ; type of 1st expression
		c = *4c
		expr = generate_push_expression(statement, expr)
		c = type_sizeof(c)
		c = round_up_to_8(c)
		emit_add_rsp_imm32(c) ; add rsp, (size of expression value on stack)
		expr = generate_push_expression(statement, expr)
		return expr
	:generate_call
		expr += 8
		local expr_arg_ptrs
		expr_arg_ptrs = malloc(4000)
		local arg_idx
		local call_function
		local return_val_size
		local args_size
		
		return_val_size = type_sizeof(type)
		return_val_size = round_up_to_8(return_val_size)
		
		call_function = expr
		expr = expression_get_end(expr)
		
		args_size = 0
		
		; we have to do a bit of work because the arguments are stored
		;  left to right, but pushed right to left
		arg_idx = 0
		p = expr_arg_ptrs
		:find_call_args_loop
			if *1expr == 0 goto find_call_args_loop_end
			*8p = expr
			
			c = expr + 4
			c = type_sizeof(*4c)
			c = round_up_to_8(c)
			args_size += c
			
			expr = expression_get_end(expr)
			p += 8
			arg_idx += 1
			goto find_call_args_loop
		:find_call_args_loop_end
		expr += 8
		
		:push_args_loop
			if arg_idx == 0 goto push_args_loop_end
			p -= 8
			generate_push_expression(statement, *8p)
			arg_idx -= 1
			goto push_args_loop
		:push_args_loop_end
		free(expr_arg_ptrs)
		
		; create space on stack for return value
		emit_sub_rsp_imm32(return_val_size)
		; put function in rax
		generate_push_expression(statement, call_function)
		emit_mov_rax_qword_rsp()
		emit_add_rsp_imm32(8)
		; call
		emit_call_rax()
		
		if return_val_size > 8 goto generate_big_return
		emit_mov_rax_qword_rsp()      ; mov rax, [rsp]
		emit_add_rsp_imm32(args_size) ; add rsp, (size of arguments on stack)
		emit_mov_qword_rsp_rax()      ; mov [rsp], rax
		return expr
		
		:generate_big_return
		; now we need to copy the return value to the proper place in the stack
		; this is kind of annoying because that might overlap with the current position.
		; so, we'll push a copy of the return value, then copy that over to the proper place.
		local copy_offset
		
		; first copy
		copy_offset = 0 - return_val_size
		emit_mov_reg(REG_RSI, REG_RSP)
		emit_mov_reg(REG_RAX, REG_RSP)
		emit_add_rax_imm32(copy_offset)
		emit_mov_reg(REG_RDI, REG_RAX)
		generate_copy_rsi_to_rdi_qwords(type)
		
		; second copy
		emit_mov_reg(REG_RAX, REG_RSP)
		emit_add_rax_imm32(copy_offset)
		emit_mov_reg(REG_RSI, REG_RAX)
		emit_mov_reg(REG_RAX, REG_RSP)
		emit_add_rax_imm32(args_size)
		emit_mov_reg(REG_RDI, REG_RAX)
		generate_copy_rsi_to_rdi_qwords(type)
		
		emit_add_rsp_imm32(args_size)
		return

; fill in break/continue jump addresses
function handle_refs
	argument p_refs
	argument prev_refs
	argument ref_addr
	local refs
	local p
	local addr
	local d
	
	refs = *8p_refs
	p = refs
	:handle_refs_loop
		if *8p == 0 goto handle_refs_loop_end
		addr = *8p
		p += 8
		d = ref_addr - addr
		addr -= 4
		*4addr = d
		goto handle_refs_loop
	:handle_refs_loop_end
	
	free(refs)
	*8p_refs = prev_refs
	return

function add_ref
	argument refs
	argument addr
	:add_ref_loop
		if *8refs == 0 goto add_ref_loop_end
		refs += 8
		goto add_ref_loop
	:add_ref_loop_end
	*8refs = addr
	return

function generate_statement
	argument statement
	local dat1
	local dat2
	local dat3
	local dat4
	local addr0
	local addr1
	local addr2
	local prev_continue_refs
	local prev_break_refs
	local n
	local p
	local c
	local d
	
	prev_continue_refs = continue_refs
	prev_break_refs = break_refs
	
	dat1 = statement + 8
	dat1 = *8dat1
	dat2 = statement + 16
	dat2 = *8dat2
	dat3 = statement + 24
	dat3 = *8dat3
	dat4 = statement + 32
	dat4 = *8dat4
	
	c = *1statement
	
	if c == STATEMENT_NOOP goto return_0
	if c == STATEMENT_BLOCK goto gen_block
	if c == STATEMENT_RETURN goto gen_return
	if c == STATEMENT_LOCAL_DECLARATION goto gen_local_decl
	if c == STATEMENT_EXPRESSION goto gen_stmt_expr
	if c == STATEMENT_IF goto gen_stmt_if
	if c == STATEMENT_WHILE goto gen_stmt_while
	if c == STATEMENT_DO goto gen_stmt_do
	if c == STATEMENT_FOR goto gen_stmt_for
	if c == STATEMENT_CONTINUE goto gen_stmt_continue
	if c == STATEMENT_BREAK goto gen_stmt_break
	if c == STATEMENT_LABEL goto gen_stmt_label
	if c == STATEMENT_GOTO goto gen_stmt_goto
	if c == STATEMENT_SWITCH goto gen_stmt_switch
	if c == STATEMENT_CASE goto gen_stmt_case
	if c == STATEMENT_DEFAULT goto gen_stmt_default
	
	die(.str_badgenstmt)
	:str_badgenstmt
		string Bad statement passed to generate_statement.
		byte 0
	:gen_block
		:gen_block_loop
			if *1dat1 == 0 goto gen_block_loop_end
			generate_statement(dat1)
			dat1 += 40
			goto gen_block_loop
		:gen_block_loop_end
		return
	:gen_return
		if dat1 == 0 goto gen_return_noexpr
		generate_push_expression_casted(statement, dat1, curr_function_return_type)
		; copy sizeof(return expression) rounded up to 8 bytes from [rsp] to [rbp+16]
		emit_mov_reg(REG_RSI, REG_RSP)
		emit_lea_rax_rbp_plus_imm32(16)
		emit_mov_reg(REG_RDI, REG_RAX)
		generate_copy_rsi_to_rdi_qwords(curr_function_return_type)
		
		:gen_return_noexpr
		generate_return()
		return
	:gen_local_decl
		dat1 = 0 - dat1
		
		if dat3 != 0 goto gen_local_decl_initializer
		if dat4 != 0 goto gen_local_decl_data_initializer
		return
		
		:gen_local_decl_initializer
			c = type_sizeof(dat2)
			c = round_up_to_8(c)
			
			; push the expression
			generate_push_expression_casted(statement, dat3, dat2)
			; copy it to where the variable's supposed to be
			emit_mov_reg(REG_RSI, REG_RSP)           ; mov rsi, rsp
			emit_lea_rax_rbp_plus_imm32(dat1)        ; lea rax, [rbp+X]
			emit_mov_reg(REG_RDI, REG_RAX)           ; mov rdi, rax
			generate_copy_rsi_to_rdi_qwords(dat2) 
			; pop the expression
			emit_add_rsp_imm32(c)                    ; add rsp, (size)
			return
		:gen_local_decl_data_initializer
			emit_mov_rax_imm64(dat4)          ; mov rax, (data address)
			emit_mov_reg(REG_RSI, REG_RAX)    ; mov rsi, rax
			emit_lea_rax_rbp_plus_imm32(dat1) ; lea rax, [rbp+X]
			emit_mov_reg(REG_RDI, REG_RAX)    ; mov rdi, rax
			generate_copy_rsi_to_rdi_qwords(dat2)
			return
	:gen_stmt_expr
		generate_push_expression_casted(statement, dat1, TYPE_VOID)
		; since we casted to void, it'll always be 8 bytes on the stack
		emit_add_rsp_imm32(8)
		return
	:gen_stmt_if
		p = dat1 + 4
		generate_push_expression(statement, dat1)
		generate_stack_compare_against_zero(statement, *4p)
		emit_je_rel32(0)    ; je +0 (temporary)
		addr1 = code_output
		generate_statement(dat2) ; "if" branch
		emit_jmp_rel32(0)   ; jmp +0 (temporary)
		addr2 = code_output
		; fill in je
		d = addr2 - addr1
		addr1 -= 4
		*4addr1 = d
		addr1 = addr2
		if dat3 == 0 goto gen_if_no_else
			generate_statement(dat3) ; "else" branch
		:gen_if_no_else
		addr2 = code_output
		; fill in jmp
		d = addr2 - addr1
		addr1 -= 4
		*4addr1 = d
		return
	:gen_stmt_while
		continue_refs = malloc(8000)
		break_refs = malloc(8000)
		
		addr0 = code_output
		p = dat1 + 4
		generate_push_expression(statement, dat1)
		generate_stack_compare_against_zero(statement, *4p)
		emit_je_rel32(0)    ; je +0 (temporary)
		addr1 = code_output
		generate_statement(dat2)
		emit_jmp_rel32(0)   ; jmp +0 (temporary)
		addr2 = code_output
		
		handle_refs(&continue_refs, prev_continue_refs, addr0)
		handle_refs(&break_refs, prev_break_refs, addr2)
		
		; fill in je
		d = addr2 - addr1
		p = addr1 - 4
		*4p = d
		; fill in jmp
		d = addr0 - addr2
		p = addr2 - 4
		*4p = d
		return
	:gen_stmt_do
		continue_refs = malloc(8000)
		break_refs = malloc(8000)
		
		addr0 = code_output
		generate_statement(dat1)
		p = dat2 + 4
		generate_push_expression(statement, dat2)
		generate_stack_compare_against_zero(statement, *4p)
		emit_jne_rel32(0) ; jne +0 (temorary)
		addr1 = code_output
		
		handle_refs(&continue_refs, prev_continue_refs, addr0)
		handle_refs(&break_refs, prev_break_refs, addr1)
		
		d = addr0 - addr1
		addr1 -= 4
		*4addr1 = d
		return
	:gen_stmt_for
		continue_refs = malloc(8000)
		break_refs = malloc(8000)
		
		if dat1 == 0 goto gen_for_no_expr1
			generate_push_expression_casted(statement, dat1, TYPE_VOID)
			emit_add_rsp_imm32(8) ; void is stored as 8 bytes
		:gen_for_no_expr1
		
		addr0 = code_output
		p = dat2 + 4
		if dat2 == 0 goto gen_for_no_expr2
			generate_push_expression(statement, dat2)
			generate_stack_compare_against_zero(statement, *4p)
			emit_je_rel32(0) ; je +0 (temporary)
		:gen_for_no_dat2_cont
		addr1 = code_output
		generate_statement(dat4) ; body
		handle_refs(&continue_refs, prev_continue_refs, code_output)
		if dat3 == 0 goto gen_for_no_expr3
			generate_push_expression_casted(statement, dat3, TYPE_VOID)
			emit_add_rsp_imm32(8) ; void is stored as 8 bytes
		:gen_for_no_expr3
		emit_jmp_rel32(0) ; jmp +0 (temporary)
		addr2 = code_output
		handle_refs(&break_refs, prev_break_refs, addr2)
		
		; fill in je
		d = addr2 - addr1
		p = addr1 - 4
		*4p = d
		
		; fill in jmp
		d = addr0 - addr2
		p = addr2 - 4
		*4p = d
		return
		:gen_for_no_expr2
			; we need to have a fake jump to be filled in here
			; so let's make a jump that'll never happen
			emit_zero_rax()     ; xor eax, eax
			emit_test_rax_rax() ; test rax, rax
			emit_jne_rel32(0)   ; jne +0 (temporary)
			goto gen_for_no_dat2_cont
	:gen_stmt_continue
		if continue_refs == 0 goto continue_outside_of_loop
		emit_jmp_rel32(0) ; jmp +0 (temporary)
		add_ref(continue_refs, code_output)
		return
		:continue_outside_of_loop
			statement_error(statement, .str_continue_outside_of_loop)
		:str_continue_outside_of_loop
			string continue statement not inside loop.
			byte 0
	:gen_stmt_break
		if break_refs == 0 goto break_outside_of_loop
		emit_jmp_rel32(0) ; jmp +0 (temporary)
		add_ref(break_refs, code_output)
		return
		:break_outside_of_loop
			statement_error(statement, .str_break_outside_of_loop)
		:str_break_outside_of_loop
			string break statement not inside loop or switch.
			byte 0
	:gen_stmt_label
		if codegen_second_pass != 0 goto gen_label_pass2
			ident_list_add(curr_function_labels, dat1, code_output)
			return
		:gen_label_pass2
			addr1 = ident_list_lookup(curr_function_labels, dat1)
			if addr1 != code_output goto bad_label_addr
			return
		:bad_label_addr
			die(.str_bad_label_addr)
		:str_bad_label_addr
			string Internal compiler error: Label address on 2nd pass doesn't match 1st pass.
			byte 0
	:gen_stmt_goto
		if codegen_second_pass == 0 goto gen_goto_pass1
		addr0 = ident_list_lookup(curr_function_labels, dat1)
		if addr0 == 0 goto bad_label
		d = addr0 - code_output
		d -= 5 ; subtract length of jmp instruction
		emit_jmp_rel32(d)
		return
		:gen_goto_pass1
			emit_jmp_rel32(0) ; we don't know the address of the label; just use 0
			return
		:bad_label
			statement_error(statement, .str_bad_label)
		:str_bad_label
			string Unrecognized label.
			byte 0
	:gen_stmt_switch
		break_refs = malloc(8000)
		
		local old_prev_case_addr
		old_prev_case_addr = prev_case_addr
		
		generate_push_expression_casted(statement, dat1, TYPE_UNSIGNED_LONG)
		emit_pop_rax()                    ; pop rax
		emit_mov_reg(REG_RBX, REG_RAX)    ; mov rbx, rax
		
		emit_jmp_rel32(0)                 ; jmp +0 (temporary)
		prev_case_addr = code_output
		
		generate_statement(dat2)
		
		addr1 = code_output
		handle_refs(&break_refs, prev_break_refs, addr1)
		
		; fill in last jump, if any
		if prev_case_addr == 0 goto switch_had_default
			d = addr1 - prev_case_addr
			p = prev_case_addr - 4
			*4p = d
		:switch_had_default
		prev_case_addr = old_prev_case_addr
		return
	:gen_stmt_case
		if prev_case_addr == 0 goto gen_bad_case
		emit_jmp_rel32(0) ; jump over the part where we deal with this case label
		addr0 = code_output
		
		; fill in previous jump
		d = addr0 - prev_case_addr
		p = prev_case_addr - 4
		*4p = d
		
		emit_mov_rax_imm64(dat1)
		emit_cmp_rax_rbx()
		emit_jne_rel32(0)
		prev_case_addr = code_output
		
		addr1 = code_output
		; fill in jump over comparison
		d = addr1 - addr0
		p = addr0 - 4
		*4p = d
		return
		
		:gen_bad_case
			; @NONSTANDARD: we don't allow `case X:` after `default:`
			; it's very annoying that the C standard allows that
			statement_error(statement, .str_gen_bad_case)
		:str_gen_bad_case
			string Either case outside of switch, or case following default.
			byte 0
	:gen_stmt_default
		addr0 = code_output
		
		; fill in previous jump
		d = addr0 - prev_case_addr
		p = prev_case_addr - 4
		*4p = d
		
		prev_case_addr = 0
		return
	
function generate_function
	argument function_name
	argument function_statement
	local function_type
	local out0
	local n_stack_bytes
	
	debug_putsln(function_name)
	
	function_type = ident_list_lookup(function_types, function_name)
	
	curr_function_return_type = functype_return_type(function_type)
	
	if codegen_second_pass != 0 goto genf_second_pass
		curr_function_labels = ident_list_create(4000) ; ~ 200 labels per function should be plenty
		ident_list_add(functions_labels, function_name, curr_function_labels)
		goto genf_cont
	:genf_second_pass
		curr_function_labels = ident_list_lookup(functions_labels, function_name)
	:genf_cont
	
	; prologue
	emit_sub_rsp_imm32(8)
	emit_mov_qword_rsp_rbp()
	emit_mov_reg(REG_RBP, REG_RSP)
	
	n_stack_bytes = ident_list_lookup(functions_required_stack_space, function_name)
	emit_sub_rsp_imm32(n_stack_bytes)
	
	generate_statement(function_statement)
	
	; implicit return at end of function
	generate_return()
	
	return

function generate_functions
	local addr
	local c
	local p
	local function_name
	
	function_name = function_statements
	
	:genfunctions_loop
		if *1function_name == 0 goto genfunctions_loop_end
		addr = code_output - output_file_data ; address of this function
		if codegen_second_pass != 0 goto genfs_check_addr
			; first pass; record address of function
			ident_list_add(functions_addresses, function_name, addr)
			goto genfs_cont
		:genfs_check_addr
			c = ident_list_lookup(functions_addresses, function_name)
			if c != addr goto function_addr_mismatch
			goto genfs_cont
		:genfs_cont
		p = memchr(function_name, 0)
		p += 1
		generate_function(function_name, *8p)
		function_name = p + 8
		goto genfunctions_loop
	:genfunctions_loop_end
	return
	
	:function_addr_mismatch
		; address of function on 2nd pass doesn't line up with 1st pass
		puts(.str_function_addr_mismatch)
		putsln(function_name)
		exit(1)
	:str_function_addr_mismatch
		string Function address on first pass doesn't match 2nd pass:
		byte 32
		byte 0

; emit ELF header and code.
function generate_code
	
	code_output = output_file_data
	emit_qword(0x00010102464c457f) ; elf identifier, 64-bit little endian, ELF version 1
	emit_qword(0) ; reserved
	emit_word(2) ; executable file
	emit_word(0x3e) ; architecture x86-64
	emit_dword(1) ; ELF version 1
	emit_qword(ENTRY_ADDR) ; entry point
	emit_qword(0x40) ; program header table offset
	emit_qword(0) ; section header table offset
	emit_dword(0) ; flags
	emit_word(0x40) ; size of header
	emit_word(0x38) ; size of program header
	emit_word(3) ; # of program headers = 3 (code, rwdata, rodata)
	emit_word(0) ; size of section header
	emit_word(0) ; # of section headers
	emit_word(0) ; index of .shstrtab

	; from /usr/include/elf.h:
	;#define PF_X		(1 << 0)	/* Segment is executable */
	;#define PF_W		(1 << 1)	/* Segment is writable */
	;#define PF_R		(1 << 2)	/* Segment is readable */
	
	; program header 1 (code)
	emit_dword(1) ; loadable segment
	emit_dword(1) ; execute only
	emit_qword(ENTRY_ADDR) ; offset in file
	emit_qword(ENTRY_ADDR) ; virtual address
	emit_qword(0) ; physical address
	emit_qword(TOTAL_CODE_SIZE) ; size in executable file
	emit_qword(TOTAL_CODE_SIZE) ; size when loaded into memory
	emit_qword(4096) ; alignment
	
	; program header 2 (rodata)
	emit_dword(1) ; loadable segment
	emit_dword(4) ; read only
	emit_qword(RODATA_ADDR) ; offset in file
	emit_qword(RODATA_ADDR) ; virtual address
	emit_qword(0) ; physical address
	emit_qword(RODATA_SIZE) ; size in executable file
	emit_qword(RODATA_SIZE) ; size when loaded into memory
	emit_qword(4096) ; alignment
	
	; program header 3 (rwdata)
	emit_dword(1) ; loadable segment
	emit_dword(7) ; read/write/execute  (this needs to be executable for `syscall` to be implementable)
	emit_qword(RWDATA_ADDR) ; offset in file
	emit_qword(RWDATA_ADDR) ; virtual address
	emit_qword(0) ; physical address
	emit_qword(RWDATA_SIZE) ; size in executable file
	emit_qword(RWDATA_SIZE) ; size when loaded into memory
	emit_qword(4096) ; alignment
	
	
	
	local p_func
	local end_addr
	code_output = output_file_data + FUNCTIONS_ADDR
	codegen_second_pass = 0
	debug_puts(.str_first_pass)
	generate_functions()
	end_addr = code_output - output_file_data
	if end_addr ] FUNCTIONS_END goto too_much_code
	code_output = output_file_data + FUNCTIONS_ADDR
	codegen_second_pass = 1
	debug_puts(.str_second_pass)
	generate_functions()
	; generate code at the entry point of the executable
	local main_addr
	main_addr = ident_list_lookup(functions_addresses, .str__main)
	if main_addr == 0 goto no_main_function
	
	; on entry, we will have:
	;   argc = *rsp
	;   argv = rsp + 8
	code_output = output_file_data + ENTRY_ADDR
	; add rsp, 8
	emit_add_rsp_imm32(8)
	; mov rax, rsp  (set rax to argv)
	emit_mov_reg(REG_RAX, REG_RSP)
	; sub rsp, 32  (undo add rsp, 8 from before and add space for argv, argc, return value)
	emit_sub_rsp_imm32(32)
	; mov [rsp+16], rax  (put argv in the right place)
	emit_mov_qword_rsp_plus_imm32_rax(16)
	; mov rax, [rsp+24]  (set rax to argc)
	emit_mov_rax_qword_rsp_plus_imm32(24)
	; mov [rsp+8], rax   (put argc in the right place)
	emit_mov_qword_rsp_plus_imm32_rax(8)
	; mov rax, main
	emit_mov_rax_imm64(main_addr)
	; call rax
	emit_call_rax()
	; mov rax, [rsp]
	emit_mov_rax_qword_rsp()
	; mov rdi, rax
	emit_mov_reg(REG_RDI, REG_RAX)
	; mov rax, 0x3c (SYS_exit)
	emit_mov_rax_imm64(0x3c)
	; syscall
	emit_syscall()
		
	return
	:no_main_function
	die(.str_no_main_function)
	:str_no_main_function
		string Error: No _main function.
		byte 0
	:too_much_code
		die(.str_too_much_code)
	:str_too_much_code
		string Too much code for executable.
		byte 0
	:str_first_pass
		string First codegen pass...
		byte 0xa
		byte 0
	:str_second_pass
		string Second codegen pass...
		byte 0xa
		byte 0