summaryrefslogtreecommitdiff
path: root/il.fun
blob: 1ca3172a76a35b8c8d6b691e3edc2efa40e2b0a4 (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
functor IL(P: PARSER) = struct

  structure P = P
  structure PP = P.P
  structure D = P.D

  datatype vregClass = VR4 | VR8

  type vreg = int
  type label = int

  datatype setArg = SaVReg of vreg | SaConst of word |
    SaAddr of P.nid * word

  datatype accessClass = AC1 | AC2 | AC4 | AC8

  datatype cmpOp =
    Cmpeq | Cmpneq |
    Cmpul | Cmpug | Cmpule | Cmpuge |
    Cmpsl | Cmpsg | Cmpsle | Cmpsge

  datatype irIns =
      IrSet of vreg * setArg
    | IrAdd of vreg * vreg * vreg
    | IrSub of vreg * vreg * vreg
    | IrMul of vreg * vreg * vreg
    | IrIMul of vreg * vreg * vreg
    | IrDiv of vreg * vreg * vreg
    | IrIDiv of vreg * vreg * vreg
    | IrMod of vreg * vreg * vreg
    | IrIMod of vreg * vreg * vreg
    | IrShr of vreg * vreg * vreg
    | IrShl of vreg * vreg * vreg
    | IrSar of vreg * vreg * vreg
    | IrAnd of vreg * vreg * vreg
    | IrOr of vreg * vreg * vreg
    | IrXor of vreg * vreg * vreg

    | IrCmp of cmpOp * vreg * vreg * vreg

    | IrExtZero of vreg * vreg * accessClass
    | IrExtSign of vreg * vreg * accessClass
    | IrLoad of vreg * vreg * accessClass (* %1 <- [%2] *)
    | IrStore of vreg * vreg * accessClass (* [%1] <- %2 *)

    | IrJmpc of cmpOp * vreg * vreg * label
    | IrJz of vreg * label
    | IrJnz of vreg * label
    | IrJmp of label

    | IrRet of vreg option
    | IrAlloc of vreg * word * int option
    | IrCopy of vreg * label * word
    | IrFcall of vreg * vreg * vreg list

    | IrNopLabel of label
    | IrNop of string

  datatype ev = Reg of vreg | Addr of vreg

  datatype regType =
    RtReg |
    RtRem |
    RtConst of word |
    RtAddrConst of int * word

  type regInfo = {
    class: vregClass,
    use: int list,
    defs: int list,
    t: regType,
    canFold: bool
  }

  datatype funcType = FtLeaf | FtNonLeaf

  datatype scopeInfo =
    SiLoop of { breakL: label, contL: label, startL: label, endL: label } |
    SiIf

  datatype localCtx = Lctx of {
    fname: int,
    localVars: { onStack: bool, t: P.ctype, name: int } vector,
    paramNum: int,
    ft: funcType ref,

    vregs: regInfo D.t,
    ops: (irIns option * (label * label) option) D.t,

    scopes: scopeInfo list ref,
    labels: (int option * int) D.t
  }

  datatype funcInfo = Fi of {
    name: int,
    paramNum: int,
    localBound: int,
    t: funcType,
    vregs: regInfo D.t,
    ops: (irIns option * (label * label) option) D.t,
    labels: int option D.t
  }

  datatype ctx = Ctx of {
    objs: P.objDef list,
    objsZI: P.objDef list,
    extSyms: P.nid list,
    globSyms: P.nid list,
    funcInfos: funcInfo list,
    strlits: int list
  }

  val debugFile = ref NONE

  local
    fun output s =
    let
      val outstream = !debugFile
    in
      case outstream of
        NONE => ()
      | SOME outstream => TextIO.output (outstream, s)
    end

    val ctx = ((false, makePrintfBase output),
      fn (_: bool * ((string -> unit) * (unit -> unit))) => ())
  in
    fun dprintf g = Fold.fold ctx g
  end

  fun updateLctx (Lctx ctx) = fn z =>
  let
    fun from fname localVars paramNum ft vregs ops scopes labels =
      { fname, localVars, paramNum, ft, vregs, ops, scopes, labels }
    fun to f { fname, localVars, paramNum, ft, vregs, ops, scopes, labels } =
      f fname localVars paramNum ft vregs ops scopes labels
  in
    FRU.makeUpdate7 (from, from, to) ctx (fn (a, f) => z (a, Lctx o f))
  end

  fun isLocal (Lctx { localVars, ... }) id = id < Vector.length localVars

  fun typeAccessClass t =
    case P.sizeOfType t of
      0w1 => AC1
    | 0w2 => AC2
    | 0w4 => AC4
    | 0w8 => AC8
    | _ => raise Unreachable

  fun ac2word AC1 = 0w1
    | ac2word AC2 = 0w2
    | ac2word AC4 = 0w4
    | ac2word AC8 = 0w8

  fun getRegType vregs r = #t $ D.get vregs r

  fun setupLocalVars localVars =
  let
    val len = Vector.length localVars

    fun setup idx acc =
      if idx = len then
        Vector.fromList (rev acc)
      else
        let
          val { onStack, t, name, ... } = Vector.sub (localVars, idx)
        in
          setup (idx + 1) ({ onStack, t, name } :: acc)
        end
  in
    setup 0 []
  end

  fun getClassForType t =
    case Word.compare (P.sizeOfType t, 0w8) of
      GREATER => raise Unreachable
    | EQUAL => VR8
    | LESS => VR4

  fun getClassSize VR4 = 0w4
    | getClassSize VR8 = 0w8

  fun setupVregs localVars paramNum =
  let
    val lvlen = Vector.length localVars
    val len = lvlen + paramNum
    val vregs = D.create len

    val () = dprintf `"local + copies: " I len `"\n" %

    fun loop idx =
      if idx = len then
        ()
      else
        if idx < lvlen then
          let
            val { t, onStack, ... } = Vector.sub (localVars, idx)
            val class = if onStack then VR8 else getClassForType t
            val defs = if idx < paramNum then [0] else []
          in
            D.push vregs
              { class, defs, use = [], t = RtReg, canFold = true };
            loop (idx + 1)
          end
        else
          let
            val { class, ... } = D.get vregs (idx - lvlen)
          in
            D.push vregs
              { class, defs = [], use = [], t = RtReg, canFold = true };
            loop (idx + 1)
          end
    val () = loop 0
  in
    vregs
  end

  fun getInsInfo op' =
  let
    fun tr (rd, rs1, rs2) = { defs = [rd], use = [rs1, rs2] }
    fun de (rd, rs1) = { defs = [rd], use = [rs1] }
    fun set reg arg =
      { defs = [reg], use = case arg of SaVReg rs => [rs] | _ => [] }
  in
    case op' of
      IrAdd t => tr t
    | IrSub t => tr t
    | IrMul t => tr t
    | IrIMul t => tr t
    | IrDiv t => tr t
    | IrIDiv t => tr t
    | IrMod t => tr t
    | IrIMod t => tr t
    | IrShl t => tr t
    | IrShr t => tr t
    | IrSar t => tr t
    | IrAnd t => tr t
    | IrOr t => tr t
    | IrXor t => tr t
    | IrCmp (_, vr1, vr2, vr3) => tr (vr1, vr2, vr3)

    | IrExtZero (rd, rs, _) => de (rd, rs)
    | IrExtSign (rd, rs, _) => de (rd ,rs)

    | IrSet (reg, arg) => set reg arg

    | IrLoad (r1, r2, _) => de (r1, r2)
    | IrStore (r1, r2, _) => { defs = [], use = [r1, r2] }
    | IrJmp _ => { defs = [], use = [] }
    | IrJmpc (_, r1, r2, _) => { defs = [], use = [r1, r2] }
    | IrJz (r, _) => { defs = [], use = [r] }
    | IrJnz (r, _) => { defs = [], use = [r] }
    | IrNopLabel _  | IrNop _  => { defs = [], use = [] }
    | IrRet v => { defs = [], use = case v of SOME r => [r] | _ => [] }
    | IrAlloc (r, _, _) => { defs = [r], use = [] }
    | IrCopy (r, _, _) => { defs = [], use = [r] }
    | IrFcall (rd, f, args) =>
        { defs = if rd = ~1 then [] else [rd], use = f :: args }
  end

  fun updateDefsUse (Lctx { vregs, ... }) defs use pos =
  let
    fun updateDef vr =
    let
      val { class, defs, use, t, canFold } = D.get vregs vr
    in
      D.set vregs vr { class, defs = pos :: defs, use, t, canFold }
    end
    fun updateUse vr =
    let
      val { class, defs, use, t, canFold } = D.get vregs vr
    in
      D.set vregs vr { class, defs, use = pos :: use, t, canFold }
    end
  in
    List.app updateDef defs;
    List.app updateUse use
  end

  fun getOutermostLoopIfNeeded scopes =
  let
    fun skipToIf [] = NONE
      | skipToIf (SiLoop _ :: tail) = skipToIf tail
      | skipToIf (SiIf :: tail) = SOME tail

    fun tryGetFirstLoop (SiIf :: tail) = tryGetFirstLoop tail
      | tryGetFirstLoop (SiLoop { startL, endL, ... } :: _) =
        SOME (startL, endL)
      | tryGetFirstLoop [] = NONE
  in
    case skipToIf scopes of
      NONE => NONE
    | SOME tail => tryGetFirstLoop (rev tail)
  end

  fun ctxPutOp (C as Lctx { ops, labels, scopes, ... }) op' =
  let
    val { defs, use } = getInsInfo op'
    val insPos = D.length ops
    val () = updateDefsUse C defs use insPos

    val li = getOutermostLoopIfNeeded (!scopes)
    val () = D.push ops (SOME op', li)

    fun setPos (NONE, use) = (SOME insPos, use)
      | setPos (SOME _, _) = raise Unreachable

    fun inc (v, use) = (v, use + 1)
  in
    case op' of
      IrNopLabel lid => D.update labels setPos lid
    | IrJmp lid | IrJnz (_, lid) | IrJz (_, lid) => D.update labels inc lid
    | _ => ()
  end

  fun copyArgs (C as Lctx { localVars, paramNum, ... }) =
  let
    val lvlen = Vector.length localVars

    fun loop idx =
      if idx = paramNum then
        ()
      else
        let
          val () = ctxPutOp C (IrSet (idx + lvlen, SaVReg idx))
        in
          loop (idx + 1)
        end
  in
    loop 0
  end

  fun getLabel (Lctx { labels, ... }) = D.pushAndGetId labels (NONE, 0)

  fun createLocalCtx fname localVars paramNum =
  let
    val localVars = setupLocalVars localVars
    val vregs = setupVregs localVars paramNum
    val labels = D.create0 ()

    val ctx = Lctx {
      fname,
      localVars, paramNum, ft = ref FtLeaf,
      vregs, ops = D.create0 (),
      scopes = ref [], labels
    }
    val () = ctxPutOp ctx (IrNop "")
    val _ = getLabel ctx (* label before ret *)
    val () = copyArgs ctx
  in
    ctx
  end

  fun getNewVR class (Lctx { vregs, ... }) canFold =
  let
    val id = D.pushAndGetId vregs
      { class, defs = [], use = [], t = RtReg, canFold }
  in
    id
  end

  fun getNewVReg class C = getNewVR class C true
  fun getNewVRegFuncArg class C = getNewVR class C false

  val getNew4 = getNewVReg VR4
  val getNew8 = getNewVReg VR8

  fun newConst (Lctx { vregs, ... }) class w =
  let
    val w = P.extz w (if class = VR4 then 0w4 else 0w8)

    val id = D.pushAndGetId vregs { class, defs = [], use = [],
      t = RtConst w, canFold = true }
  in
    id
  end

  fun getClass (Lctx { vregs, ... }) id = #class $ D.get vregs id

  fun pv (Reg v) out = Printf out `"Reg %" I v %
    | pv (Addr v) out = Printf out `"Addr %" I v %

  fun convConst ctx (w, t) =
  let
    val class =
      case Word.compare (P.sizeOfType t, 0w8) of
        GREATER => raise Unreachable
      | EQUAL => VR8
      | LESS => VR4

    val v = getNewVReg class ctx
    val () = ctxPutOp ctx (IrSet (v, SaConst w))
  in
    Reg v
  end

  fun convGLconst ctx id =
  let
    val v = getNew8 ctx
  in
    ctxPutOp ctx (IrSet (v, SaAddr (id, 0w0)));
    Addr v
  end

  fun convStrlit ctx id t =
  let
    val v = getNew8 ctx
  in
    ctxPutOp ctx (IrSet (v, SaAddr (id, 0w0)));
    (if P.isArray t then Addr else Reg) v
  end

  fun convId ctx (P.Gid id) = convGLconst ctx id
    | convId (Lctx { localVars, paramNum, ... }) (P.Lid id) =
      if id < paramNum then (* function parameter *)
        (Reg (id + Vector.length localVars))
      else
        let
          val onStack = #onStack $ Vector.sub (localVars, id)
        in
          (if onStack then Addr else Reg) id
        end

  fun getOffset t field = #1 $ valOf $ P.getFieldInfo t field

  fun computeFieldFromVReg ctx v offset =
  let
    val vOff = newConst ctx VR8 offset
    val vRes = getNew8 ctx
    val () = ctxPutOp ctx (IrAdd (vRes, v, vOff))
  in
    Addr vRes
  end

  fun convFieldAccessByV ctx ea field: ev =
  let
    val v: ev = convExpr ctx ea
    val offset = getOffset (P.getT ea) field
  in
    case v of
      Addr v => computeFieldFromVReg ctx v offset
    | Reg _ => raise Unreachable
  end

  and convFieldAccesByP ctx ea field =
  let
    val v = convExpr ctx ea

    val offset = getOffset (P.pointsTo $ P.getT ea)field
  in
    case v of
      Reg v => computeFieldFromVReg ctx v offset
    | Addr v =>
      let
        val vl = getNew8 ctx
        val () = ctxPutOp ctx (IrLoad (vl, v, AC8))
      in
        computeFieldFromVReg ctx vl offset
      end
  end

  and convSizeOfType ctx t: ev =
  let
    val w = P.sizeOfType t
    val v = newConst ctx VR8 w
  in
    Reg v
  end

  and getSingleOffset t =
    if P.isPointer t then
      P.sizeOfType (P.pointsTo t)
    else
      0w1

  and convPre op' ctx (v, t) =
    case v of
      Reg v =>
      let
        val class = getClass ctx v
        val v1 = newConst ctx class (getSingleOffset t)
      in
        ctxPutOp ctx (op' (v, v, v1));
        Reg v
      end
    | Addr v =>
      let
        val class = getClassForType t
        val aClass = typeAccessClass t
        val vl = getNewVReg class ctx
        val v1 = newConst ctx class (getSingleOffset t)
      in
        ctxPutOp ctx (IrLoad (vl, v, aClass));
        ctxPutOp ctx (op' (vl, vl, v1));
        ctxPutOp ctx (IrStore (v, vl, aClass));
        Reg vl
      end

  and convPost op' ctx (v, t) =
    case v of
      Reg v =>
        let
          val class = getClass ctx v
          val vOld = getNewVReg class ctx
          val v1 = newConst ctx class (getSingleOffset t)
        in
          ctxPutOp ctx (IrSet (vOld, SaVReg v));
          ctxPutOp ctx (op' (v, v, v1));
          Reg vOld
        end
    | Addr v =>
      let
        val class = getClassForType t
        val aClass = typeAccessClass t
        val vl = getNewVReg class ctx
        val vOld = getNewVReg class ctx
        val v1 = newConst ctx class (getSingleOffset t)
      in
        ctxPutOp ctx (IrLoad (vl, v, aClass));
        ctxPutOp ctx (IrSet (vOld, SaVReg vl));
        ctxPutOp ctx (op' (vl, vl, v1));
        ctxPutOp ctx (IrStore (v, vl, aClass));
        Reg vOld
      end

  and convAddr v =
    case v of
      Reg _ => raise Unreachable
    | Addr v => Reg v

  and convDeref ctx v =
    case v of
      Reg v => Addr v
    | Addr v =>
      let
        val vD = getNew8 ctx
        val () = ctxPutOp ctx (IrLoad (vD, v, AC8))
      in
        Addr vD
      end

  and convPos v = v

  and prepIsZero ctx vDest vSrc =
  let
    val vc = newConst ctx (getClass ctx vSrc) 0w0
  in
    ctxPutOp ctx (IrCmp (Cmpeq, vDest, vSrc, vc))
  end

  and prepNeg ctx vDest vSrc =
  let
    val vc = newConst ctx (getClass ctx vSrc) 0w0
  in
    ctxPutOp ctx (IrSub (vDest, vc, vSrc))
  end

  and prepComp ctx vDest vSrc =
  let
    val vc = newConst ctx (getClass ctx vSrc) (Word.~ 0w1)
  in
    ctxPutOp ctx (IrXor (vDest, vSrc, vc))
  end

  and convSimpleUnop fop ctx (v, t) =
    case v of
      Reg v =>
      let
        val vNew = getNewVReg (getClass ctx v) ctx
      in
        fop ctx vNew v;
        Reg vNew
      end
    | Addr v =>
      let
        val aClass = typeAccessClass t
        val class = getClassForType t
        val vl = getNewVReg class ctx
      in
        ctxPutOp ctx (IrLoad (vl, v, aClass));
        fop ctx vl vl;
        ctxPutOp ctx (IrStore (v, vl, aClass));
        Reg vl
      end

  and convCastScalar ctx v fromT toT =
    case Word.compare (P.sizeOfType toT, P.sizeOfType fromT) of
      EQUAL => v
    | LESS => (
        case v of
          Reg v =>
            let
              val vNew = getNew4 ctx
            in
              ctxPutOp ctx (IrSet (vNew, SaVReg v));
              Reg vNew
            end
        | Addr v => Addr v
    )
    | GREATER =>
      let
        val op' = if P.isSigned fromT then IrExtSign else IrExtZero
        val aClass = typeAccessClass fromT
        val toTClass = getClassForType toT

        val v =
            case v of
              Addr v =>
              let
                val vl = getNew4 ctx
              in
                ctxPutOp ctx (IrLoad (vl, v, aClass));
                vl
              end
            | Reg v => v
        val vNew = getNewVReg toTClass ctx
      in
        ctxPutOp ctx (op' (vNew, v, aClass));
        Reg vNew
      end

  and convCast _ (v, _, P.void_t) = v
    | convCast ctx (v, fromT, toT) =
      if P.isArray fromT then
        let
          val elT = P.elementType fromT
          val () =
            if toT <> P.pointer_t (1, elT) then raise Unreachable else ()
        in
          case v of
             Addr v => Reg v
           | _ => raise Unreachable
        end
      else if P.isFunc fromT then
        let
          val () =
            if toT <> P.pointer_t (1, fromT) then raise Unreachable else ()
        in
          case v of
            Addr v => Reg v
          | _ => raise Unreachable
        end
      else
        convCastScalar ctx v fromT toT

  and convUnop ctx unop ea t: ev =
  let
    val v: ev = convExpr ctx ea
    val subT = P.getT ea
  in
    case unop of
      P.UnopPreInc => convPre IrAdd ctx (v, subT)
    | P.UnopPreDec => convPre IrSub ctx (v, subT)
    | P.UnopPostInc => convPost IrAdd ctx (v, subT)
    | P.UnopPostDec => convPost IrSub ctx (v, subT)
    | P.UnopSizeof => convSizeOfType ctx subT
    | P.UnopAddr => convAddr v
    | P.UnopDeref => convDeref ctx v
    | P.UnopPos => convPos v
    | P.UnopNeg => convSimpleUnop prepNeg ctx (v, subT)
    | P.UnopComp => convSimpleUnop prepComp ctx (v, subT)
    | P.UnopLogNeg => convSimpleUnop prepIsZero ctx (v, subT)
    | P.UnopCast => convCast ctx (v, subT, t)
  end

  and loadIfNeeded ctx t vLeft =
    case vLeft of
      Reg v => v
    | Addr v =>
      let
        val aClass = typeAccessClass t
        val class = getClassForType t
        val vl = getNewVReg class ctx
      in
        ctxPutOp ctx (IrLoad (vl, v, aClass));
        vl
      end

  and binopPrepOpers ctx t vLeft vRight =
  let
    val vLeft = loadIfNeeded ctx t vLeft
    val vRight = loadIfNeeded ctx t vRight
    val vNew = getNewVReg (getClass ctx vLeft) ctx
  in
    (vNew, vLeft, vRight)
  end

  and convSimple op' ctx t vLeft vRight =
  let
    val (vNew, vLeft, vRight) = binopPrepOpers ctx t vLeft vRight
  in
    ctxPutOp ctx (op' (vNew, vLeft, vRight));
    Reg vNew
  end

  and shiftPointer ctx op' (leftT, vLeft) (rightT, vRight) =
    let
      val v =
        case convCast ctx (Reg vRight, rightT, P.ulong_t) of
          Reg v => v
        | Addr _ => raise Unreachable

      val mulVal = P.sizeOfType $ P.pointsTo leftT
      val multiplier = newConst ctx VR8 mulVal

      val vm = getNew8 ctx
      val vRes = getNew8 ctx
    in
      ctxPutOp ctx (IrMul (vm, v, multiplier));
      ctxPutOp ctx (op' (vRes, vLeft, vm));
      Reg vRes
    end

  and convSum ctx (leftT, vLeft) (rightT, vRight) =
  let
    val vLeft = loadIfNeeded ctx leftT vLeft
    val vRight = loadIfNeeded ctx rightT vRight
  in
    if P.isPointer leftT then
      shiftPointer ctx IrAdd (leftT, vLeft) (rightT, vRight)
    else
      let
        val vNew = getNewVReg (getClass ctx vLeft) ctx
      in
        ctxPutOp ctx (IrAdd (vNew, vLeft, vRight));
        Reg vNew
      end
  end

  and convSub ctx (leftT, vLeft) (rightT, vRight) =
  let
    val vLeft = loadIfNeeded ctx leftT vLeft
    val vRight = loadIfNeeded ctx rightT vRight
  in
    if P.isPointer leftT then
      if P.isPointer rightT then
        let
          val vDiff = getNew8 ctx
          val vRes = getNew8 ctx
          val divider = newConst ctx VR8 (P.sizeOfType (P.pointsTo leftT))
        in
          ctxPutOp ctx (IrSub (vDiff, vLeft, vRight));
          ctxPutOp ctx (IrDiv (vRes, vDiff, divider));
          Reg vRes
        end
      else
        shiftPointer ctx IrSub (leftT, vLeft) (rightT, vRight)
    else
      let
        val vNew = getNewVReg (getClass ctx vLeft) ctx
      in
        ctxPutOp ctx (IrSub (vNew, vLeft, vRight));
        Reg vNew
      end
  end

  and convSimpleAssignment ctx leftT vLeft vRight =
  let
    val vRight = loadIfNeeded ctx leftT vRight
  in
    case vLeft of
      Reg v => (
          ctxPutOp ctx (IrSet (v, SaVReg vRight));
          Reg v
      )
    | Addr v => (
      ctxPutOp ctx (IrStore (v, vRight, typeAccessClass leftT));
      Reg vRight
    )
  end

  and convSubscript ctx (leftT, vLeft) (rightT, vRight) =
  let
    val vLeft = loadIfNeeded ctx leftT vLeft
    val vRight = loadIfNeeded ctx rightT vRight

    val res =
      case shiftPointer ctx IrAdd (leftT, vLeft) (rightT, vRight) of
        Reg v => v
      | Addr _ => raise Unreachable
  in
    Addr res
  end

  and convCompAssign (op', op2) ctx (leftT, vLeft) (rightT, vRight) =
  let
    val vRight = loadIfNeeded ctx rightT vRight
    val leftT = P.resolveType leftT
    val rightT = P.resolveType rightT

    val commonType = P.commonType leftT rightT

    fun convIfNeeded ctx t v =
      if t <> commonType then
        case convCast ctx (Reg v, t, commonType) of
          Reg v => v
        | Addr _ => raise Unreachable
      else
        v

    fun apply ctx leftV =
    let
      val vLeft = convIfNeeded ctx leftT leftV
      val vRight = convIfNeeded ctx rightT vRight

      val op' = if P.isSigned commonType then op2 else op'
      val vRight =
        if P.isPointer leftT then
          let
            val mul = newConst ctx VR8 (P.sizeOfType (P.pointsTo leftT))
            val vm = getNew8 ctx
          in
            ctxPutOp ctx (IrMul (vm, vRight, mul));
            vm
          end
        else
          vRight
    in
      ctxPutOp ctx (op' (vLeft, vLeft, vRight));
      vLeft
    end
  in
    case vLeft of
      Reg v =>
      let
        val vLeft = apply ctx v
      in
        if leftT <> commonType then
          ctxPutOp ctx (IrSet (v, SaVReg vLeft))
        else
          ();
        Reg v
      end
    | Addr v =>
      let
        val class = getClassForType leftT
        val aClass = typeAccessClass leftT
        val vl = getNewVReg class ctx
        val () = ctxPutOp ctx (IrLoad (vl, v, aClass))

        val vLeft = apply ctx vl
      in
        ctxPutOp ctx (IrStore (v, vLeft, aClass));
        Reg vLeft
      end
  end

  and convComma _ _ (_, vRight) = vRight

  and getLabelPair ctx =
  let
    val l1 = getLabel ctx
    val l2 = getLabel ctx
  in
    (l1, l2)
  end

  and genLogPart ctx ea =
  let
    val t = P.getT ea
    val v = convExpr ctx ea
    val v = loadIfNeeded ctx t v
    val v =
      if P.typeRank t < P.typeRank P.int_t then
        case convCast ctx (Reg v, t, P.int_t) of
          Reg v => v
        | Addr _ => raise Unreachable
      else
        v
  in
    v
  end

  and convLogOr ctx left right =
  let
    val vLeft = genLogPart ctx left

    val (elseLabel, endLabel) = getLabelPair ctx

    val vRes = getNew4 ctx
    val () = ctxPutOp ctx (IrJz (vLeft, elseLabel))
    val () = ctxPutOp ctx (IrSet (vRes, SaConst 0w1))
    val () = ctxPutOp ctx (IrJmp endLabel)
    val () = ctxPutOp ctx (IrNopLabel elseLabel)

    val vRight = genLogPart ctx right
    val vC = newConst ctx (getClass ctx vRight) 0w0
    val () = ctxPutOp ctx (IrCmp (Cmpneq, vRes, vRight, vC))
    val () = ctxPutOp ctx (IrNopLabel endLabel);
  in
    Reg vRes
  end

  and convLogAnd ctx left right =
  let
    val vLeft = genLogPart ctx left
    val (falseLabel, endLabel) = getLabelPair ctx
    val () = ctxPutOp ctx (IrJz (vLeft, falseLabel))
    val vRight = genLogPart ctx right
    val vRes = getNew4 ctx
    val vC = newConst ctx (getClass ctx vRight) 0w0
  in
    ctxPutOp ctx (IrCmp (Cmpneq, vRes, vRight, vC));
    ctxPutOp ctx (IrJmp endLabel);
    ctxPutOp ctx (IrNopLabel (falseLabel));
    ctxPutOp ctx (IrSet (vRes, SaConst 0w0));
    ctxPutOp ctx (IrNopLabel endLabel);
    Reg vRes
  end

  and convBinop ctx binop left right =
  let
    val leftT = P.getT left
    val rightT = P.getT right

    fun chs opS opU = if P.isSigned leftT then opS else opU

    fun apply f =
    let
      val vLeft = convExpr ctx left
      val vRight = convExpr ctx right
    in
      f ctx (leftT, vLeft) (rightT, vRight)
    end

    fun commonWrapper f ctx (leftT, vLeft) (_, vRight) =
      f ctx leftT vLeft vRight

    val convSimple = fn op' => apply $ commonWrapper (convSimple op')
    val convCompAssign = fn opp => apply $ convCompAssign opp

    fun cmpw cmop (r1, r2, r3) = IrCmp (cmop, r1, r2, r3)
  in
    case binop of
      P.BR P.BrMul => convSimple (chs IrIMul IrMul)
    | P.BR P.BrDiv => convSimple (chs IrIDiv IrDiv)
    | P.BR P.BrMod => convSimple (chs IrIMod IrMod)
    | P.BR P.BrShiftLeft => convSimple IrShl
    | P.BR P.BrShiftRight => convSimple (chs IrSar IrShr)
    | P.BR P.BrBitAnd => convSimple IrAnd
    | P.BR P.BrBitOr => convSimple IrOr
    | P.BR P.BrBitXor => convSimple IrXor
    | P.BR P.BrEqual => convSimple (cmpw Cmpeq)
    | P.BR P.BrNotEqual => convSimple (cmpw Cmpneq)
    | P.BR P.BrGreater => convSimple (chs (cmpw Cmpsg) (cmpw Cmpug))
    | P.BR P.BrLess => convSimple (chs (cmpw Cmpsl) (cmpw Cmpul))
    | P.BR P.BrGreaterEqual => convSimple (chs (cmpw Cmpsge) (cmpw Cmpuge))
    | P.BR P.BrLessEqual => convSimple (chs (cmpw Cmpsle) (cmpw Cmpule))

    | P.BR P.BrAssign => apply $ commonWrapper convSimpleAssignment
    | P.BR P.BrBitAndAssign => convCompAssign (IrAnd, IrAnd)
    | P.BR P.BrBitOrAssign => convCompAssign (IrOr, IrOr)
    | P.BR P.BrBitXorAssign => convCompAssign (IrXor, IrXor)

    | P.BR P.BrLeftShiftAssign => convCompAssign (IrShl, IrShl)
    | P.BR P.BrRightShiftAssign => convCompAssign (IrShr, IrSar)
    | P.BR P.BrDivAssign => convCompAssign (IrDiv, IrIDiv)
    | P.BR P.BrMulAssign => convCompAssign (IrMul, IrIMul)
    | P.BR P.BrModAssign => convCompAssign (IrMod, IrIMod)

    | P.BR P.BrSubscript => apply convSubscript
    | P.BR P.BrComma => apply convComma

    | P.BR P.BrSum => apply convSum
    | P.BR P.BrSub => apply convSub
    | P.BR P.BrSumAssign => convCompAssign (IrAdd, IrAdd)
    | P.BR P.BrSubAssign => convCompAssign (IrSub, IrSub)

    | P.BR P.BrLogOr => convLogOr ctx left right
    | P.BR P.BrLogAnd => convLogAnd ctx left right
    | P.BinopTernaryIncomplete _ => raise Unreachable
  end

  and convTernary ctx cond left right =
  let
    val leftT = P.getT left

    val cond = genLogPart ctx cond
    val (elseLabel, endLabel) = getLabelPair ctx
    val () = ctxPutOp ctx (IrJz (cond, elseLabel))
    val vRes = getNewVReg (getClassForType leftT) ctx
    val vLeft = convExpr ctx left
    val vLeft = loadIfNeeded ctx leftT vLeft
    val () = ctxPutOp ctx (IrSet (vRes, SaVReg vLeft))
    val () = ctxPutOp ctx (IrJmp endLabel)
    val () = ctxPutOp ctx (IrNopLabel elseLabel)
    val vRight = convExpr ctx right
    val vRight = loadIfNeeded ctx leftT vRight
    val () = ctxPutOp ctx (IrSet (vRes, SaVReg vRight))
    val () = ctxPutOp ctx (IrNopLabel endLabel)
  in
    Reg vRes
  end

  and convFuncCall ctx func args =
  let
    val t = P.getT func
    val vFunc = convExpr ctx func
    val vFunc = loadIfNeeded ctx t vFunc
    val vFunc =
      case (getClass ctx vFunc) of
        VR4 =>
        let
          val v = getNew8 ctx
        in
          ctxPutOp ctx (IrSet (v, SaVReg vFunc));
          v
        end
      | VR8 => vFunc

    fun genArgs [] acc =
      let
        fun loop _ [] acc2 = rev acc2
          | loop idx (vArg :: acc) acc2 =
          let
            val arg = getNewVRegFuncArg (getClass ctx vArg) ctx
          in
            ctxPutOp ctx (IrSet (arg, SaVReg vArg));
            loop (idx + 1) acc (arg :: acc2)
          end

        val () = ctxPutOp ctx (IrNop "here")
      in
        loop 0 (rev acc) []
      end
      | genArgs (arg :: args) acc =
      let
        val vArg = convExpr ctx arg
        val vArg = loadIfNeeded ctx (P.getT arg) vArg
      in
        genArgs args (vArg :: acc)
      end

    val args = genArgs args []

    val rt = #1 $ P.funcParts (P.pointsTo t)
    val vRes =
      case P.resolveType rt of
          P.void_t => ~1
        | t =>
          let
            val class = getClassForType t
            val vRes = getNewVReg class ctx
          in
            vRes
          end
    val Lctx { ft, ... } = ctx
  in
    ft := FtNonLeaf;
    ctxPutOp ctx (IrFcall (vRes, vFunc, args));
    Reg vRes
  end

  and convExpr ctx ea =
  let
    val P.EA (e, _, _, t) = ea
  in
    case e of
      P.Eid (_, loc) => convId ctx (valOf loc)
    | P.Econst (_, P.Ninteger w) => convConst ctx (w, t)
    | P.Econst (_, _) => raise Unreachable
    | P.Estrlit id => convStrlit ctx id t
    | P.EmemberByV (ea, field) => convFieldAccessByV ctx ea field
    | P.EmemberByP (ea, field) => convFieldAccesByP ctx ea field
    | P.EsizeofType t => convSizeOfType ctx t
    | P.Eunop (unop, ea) => convUnop ctx unop ea t
    | P.Ebinop (binop, left, right) => convBinop ctx binop left right
    | P.Eternary (cond, left, right) => convTernary ctx cond left right
    | P.EfuncCall (func, args) => convFuncCall ctx func args
  end

  fun wrapTo8 v =
  let
    open Word
  in
    (v + 0w7) div 0w8 * 0w8
  end

  fun convIni (C as Lctx { localVars, ... }) (id, NONE) =
  let
    val size = P.sizeOfType $ #t $ Vector.sub (localVars, id)
  in
    ctxPutOp C (IrAlloc (id, wrapTo8 size, NONE))
  end
    | convIni (C as Lctx { localVars, ... }) (id, SOME (P.CiniExpr ea)) =
    let
      val t = P.getT ea
      val v = convExpr C ea
      val v = loadIfNeeded C t v

    in
      if #onStack $ Vector.sub (localVars, id) then (
        ctxPutOp C (IrAlloc (id, 0w8, NONE));
        ctxPutOp C (IrStore (id, v, typeAccessClass t))
      ) else
        ctxPutOp C (IrSet (id, SaVReg v))
    end
    | convIni ctx (id, SOME (P.CiniLayout lid)) =
    let
      val size = wrapTo8 $ P.getLayoutSize lid
    in
      ctxPutOp ctx (IrAlloc (id, size, NONE));
      ctxPutOp ctx (IrCopy (id, lid, size))
    end

  fun convReturn ctx ea =
    case ea of
      SOME ea =>
      let
        val v = convExpr ctx ea
        val v = loadIfNeeded ctx (P.getT ea) v
      in
        ctxPutOp ctx (IrRet $ SOME v)
      end
   | NONE => ctxPutOp ctx (IrRet NONE)

  fun beginIfScope (Lctx { scopes, ... }) = scopes := SiIf :: !scopes
  fun endIfScope (Lctx { scopes, ... }) =
    case hd (!scopes) of
      SiLoop _ => raise Unreachable
    | SiIf => scopes := tl (!scopes)

  fun convIf ctx (cond, thenPart, elsePart) =
  let
    val () = beginIfScope ctx
    val v = genLogPart ctx cond
    val (elseL, endL) = getLabelPair ctx
  in
    ctxPutOp ctx (IrJz (v, elseL));
    convStmt ctx thenPart;

    if isSome elsePart then ctxPutOp ctx (IrJmp endL) else ();
    ctxPutOp ctx (IrNopLabel elseL);
    case elsePart of
      SOME elsePart => (
        convStmt ctx elsePart;
        ctxPutOp ctx (IrNopLabel endL)
      )
    | NONE => ();
    endIfScope ctx
  end

  and getLabelsWhile (C as Lctx { scopes, ... }) =
  let
    val (startL, endL) = getLabelPair C
    val scope = SiLoop { startL, endL, contL = startL, breakL = endL }
  in
    scopes := scope :: !scopes;
    { startL, endL }
  end

  and ctxLoopExit (Lctx { scopes, ... }) =
  let
    val top = hd $ !scopes
  in
    case top of
      SiLoop _ => scopes := tl (!scopes)
    | _ => raise Unreachable
  end

  and convWhile ctx (cond, body) =
  let
    val { startL, endL } = getLabelsWhile ctx
    val () = ctxPutOp ctx (IrNopLabel startL)
    val cond = genLogPart ctx cond
  in
    ctxPutOp ctx (IrJz (cond, endL));
    convStmt ctx body;
    ctxPutOp ctx (IrJmp startL);
    ctxPutOp ctx (IrNopLabel endL);
    ctxLoopExit ctx
  end

  and getLabelsDoWhile (C as Lctx { scopes, ... }) =
  let
    val startL = getLabel C
    val (contL, endL) = getLabelPair C
    val scope = SiLoop { startL, endL, contL, breakL = endL }
  in
    scopes := scope :: !scopes;
    { startL, contL, endL }
  end

  and convDoWhile ctx (body, cond) =
  let
    val { startL, contL, endL } = getLabelsDoWhile ctx
    val () = ctxPutOp ctx (IrNopLabel startL)
    val () = convStmt ctx body
    val () = ctxPutOp ctx (IrNopLabel contL)
    val cond = genLogPart ctx cond
  in
    ctxPutOp ctx (IrJnz (cond, startL));
    ctxPutOp ctx (IrNopLabel endL);
    ctxLoopExit ctx
  end

  and convBreakOrCont isBreak (C as Lctx { scopes, ... }) =
  let
    fun getFirstLoopInfo [] = raise Unreachable
      | getFirstLoopInfo (SiLoop { breakL, contL, ... } :: _) =
       (breakL, contL)
      | getFirstLoopInfo (SiIf :: tail) = getFirstLoopInfo tail

    val (break, continue) = getFirstLoopInfo $ !scopes
    val label = if isBreak then break else continue
  in
    ctxPutOp C (IrJmp label)
  end

  and getLabelsFor (C as Lctx { scopes, ... }) =
  let
    val startL = getLabel C
    val (endL, contL) = getLabelPair C
    val scope = SiLoop { startL, endL, contL, breakL = endL }
  in
    scopes := scope :: !scopes;
    { startL, contL, endL }
  end

  and convFor ctx (pre, cond, post, stmt) =
  let
    val () =
      case pre of
        NONE => ()
      | SOME ea => ignore $ convExpr ctx ea
    val { startL, contL, endL } = getLabelsFor ctx

    val () = ctxPutOp ctx (IrNopLabel startL)
    val () =
      case cond of
        NONE => ()
      | SOME cond =>
        let
          val cond = genLogPart ctx cond
        in
          ctxPutOp ctx (IrJz (cond, endL))
        end
    val () = convStmt ctx stmt
    val () = ctxPutOp ctx (IrNopLabel contL)
    val () =
      case post of
        NONE => ()
      | SOME post => ignore $ convExpr ctx post
    val () = ctxPutOp ctx (IrJmp startL)
    val () = ctxPutOp ctx (IrNopLabel endL)
  in
    ctxLoopExit ctx
  end

  and convStmt ctx stmt: unit =
    case stmt of
      P.StmtExpr ea => ignore $ convExpr ctx ea
    | P.StmtCompound (inis, stmts) => (
      List.app (fn ini => convIni ctx ini) inis;
      List.app (fn stmt => convStmt ctx stmt) stmts
    )
    | P.StmtIf t => convIf ctx t
    | P.StmtReturn ea => convReturn ctx ea
    | P.StmtFor quad => convFor ctx quad
    | P.StmtWhile pair => convWhile ctx pair
    | P.StmtDoWhile pair => convDoWhile ctx pair
    | P.StmtBreak => convBreakOrCont true ctx
    | P.StmtContinue => convBreakOrCont false ctx
    | P.StmtNone => ()

  val Pl = fn z =>
  let
    fun f l out = Printf out `".L" I l %
  in
    bind A1 f
  end z

  val Pac = fn z =>
  let
    fun f ac out =
      Printf out `(
        case ac of
          AC1 => "byte"
        | AC2 => "word"
        | AC4 => "dword"
        | AC8 => "qword") %
  in
    bind A1 f
  end z

  val Pt = fn z =>
  let
    fun f ctx id out =
    let
      val c = getClass ctx id
    in
      Printf out `(case c of VR4 => "w4" | VR8 => "w8") %
    end
  in
    bind A2 f
  end z

  fun pwc class w out =
  let
    val (sign, w) =
      case class of
        VR4 =>
          if w < Word.<< (0w1, 0w31) then
            ("+", w)
          else
            ("-", Word.~ (P.exts w 0w4))
      | VR8 =>
          if w < Word.<< (0w1, 0w63) then
            ("+", w)
          else
            ("-", Word.~ w)
    in
      Printf out `sign W w %
    end

  val Pwc = fn z => bind A2 pwc z

  fun preg (C as Lctx { vregs, ... }) id out =
  let
    val rt = getRegType vregs id
  in
    case rt of
      RtReg => Printf out `"%" I id %
    | RtRem => raise Unreachable
    | RtConst w => Printf out Pwc (getClass C id) w %
    | RtAddrConst (id, w) => Printf out `"$" PP.? id Pwc VR8 w %
  end
  val Preg = fn z => bind A2 preg z

  fun printOpSet ctx reg arg =
  let
    val () = dprintf `"\t" Preg ctx reg `" " Pt ctx reg `" = " %
  in
    case arg of
      SaVReg reg => dprintf Preg ctx reg %
    | SaConst w => dprintf Pwc (getClass ctx reg) w %
    | SaAddr (id, w) => dprintf PP.? id Pwc VR8 w %
  end

  fun printOp ctx (idx, (SOME op', li)) =
  let
    fun printTail NONE = dprintf `"\n" %
      | printTail (SOME (startL, endL)) =
        case op' of
          IrNopLabel _ => dprintf `"\n" %
        | _ => dprintf `" ; (l" I startL `", l" I endL `")\n" %

    val () = dprintf Ip 4 idx `":" %
    val () =
      case op' of
        IrNopLabel _ => ()
      | _ => dprintf `"\t" %
    fun pt (reg1, reg2, reg3) op' =
      dprintf `"\t" Preg ctx reg1 `" " Pt ctx reg1 `" = "
          `op' `" " Preg ctx reg2 `", " Preg ctx reg3 %

    fun pe (reg1, reg2, aClass) op' =
      dprintf `"\t" Preg ctx reg1 `" " Pt ctx reg1 `" = "
          `op' `" " Pac aClass `" " Preg ctx reg2 %

    fun pj (r, l) op' = dprintf `"\t" `op' `" " Preg ctx r `", " Pl l %

    fun printRet NONE = dprintf `"\tret" %
      | printRet (SOME reg) =
        dprintf `"\tret " Pt ctx reg `" " Preg ctx reg %

    fun printAlloc (r, size, off) =
    let
      val () = dprintf `"\t" Preg ctx r `" = alloc " W size  %
    in
      case off of
        SOME off => dprintf `" [rbp-" I off `"]" %
      | NONE => ()
    end

    fun printCopy (to, from, size) =
      dprintf `"\tcopy " Preg ctx to `", I." I from `", " W size %

    fun printFcall (ret, f, args) =
    let
      val () = dprintf `"\t" %
      val () =
        if ret <> ~1 then
          dprintf Preg ctx ret `" " Pt ctx ret `" = " %
        else
          ()
    in
      dprintf `"fcall " Preg ctx f `"" Plist (preg ctx) args (", ", true, 0) %
    end

    fun printLabel (Lctx { labels, ... }) lid =
    let
      val (labelPos, use) = D.get labels lid
      val () = if valOf labelPos <> idx then raise Unreachable else ()
    in
        dprintf `"@" Pl lid `"(" I use `"):" %
    end

    fun cmpOpStr cmpop =
      case cmpop of
        Cmpeq => "cmpeq"
      | Cmpneq => "cmpneq"
      | Cmpul => "cmpul"
      | Cmpug => "cmpug"
      | Cmpule => "cmpule"
      | Cmpuge => "cmpuge"
      | Cmpsl => "cmpsl"
      | Cmpsg => "cmpsg"
      | Cmpsle => "cmpsle"
      | Cmpsge => "cmpsge"

    fun pjc (op', r1, r2, lid) =
    let
      val opRepr = cmpOpStr op'
      val opRepr = "jmp" ^ String.extract (opRepr, 3, NONE)
    in
      dprintf `"\t" `opRepr `" "
        Preg ctx r1 `", " Preg ctx r2 `", " Pl lid %
    end
  in
    case op' of
      IrSet (reg, arg) => printOpSet ctx reg arg
    | IrAdd t => pt t "add"
    | IrSub t => pt t "sub"
    | IrMul t => pt t "mul"
    | IrIMul t => pt t "imul"
    | IrDiv t => pt t "div"
    | IrIDiv t => pt t "idiv"
    | IrMod t => pt t "mod"
    | IrIMod t => pt t "imod"
    | IrShl t => pt t "shl"
    | IrShr t => pt t "shr"
    | IrSar t => pt t "sar"
    | IrAnd t => pt t "and"
    | IrOr t => pt t "or"
    | IrXor t => pt t "xor"

    | IrCmp (cmpOp, vr1, vr2, vr3) => pt (vr1, vr2, vr3) (cmpOpStr cmpOp)

    | IrExtZero t => pe t "extz"
    | IrExtSign t => pe t "exts"

    | IrLoad (r1, r2, ac) =>
      dprintf `"\t" Preg ctx r1 `" = " Pac ac `" [" Preg ctx r2 `"]" %
    | IrStore (r1, r2, ac) =>
      dprintf `"\t" Pac ac `" [" Preg ctx r1 `"] <- " Preg ctx r2 %
    | IrJmp l => dprintf `"\tjmp " Pl l %
    | IrJmpc q => pjc q
    | IrJz p => pj p "jz"
    | IrJnz p => pj p "jnz"
    | IrNopLabel l => printLabel ctx l
    | IrNop s => dprintf `"\t; " `s %
    | IrRet v => printRet v
    | IrAlloc p => printAlloc p
    | IrCopy t => printCopy t
    | IrFcall t => printFcall t
    ;
    printTail li
  end
   | printOp _ (_, (NONE, _)) = ()

  fun printIns (C as Lctx { ops, ... }) =
    D.appi (printOp C) ops

  fun printVar idx { class, defs, use, t, canFold = _ } =
  let
    val c = if class = VR4 then "w4" else "w8"

    val () = dprintf `"%" I idx `" " `c `"\t"
          `": defs = " Plist i defs (", ", true, 0)
          `", uses = " Plist i use (", ", true, 0) %
  in
    case t of
     RtReg => dprintf `" regular" %
   | RtRem => dprintf `" removed" %
   | RtConst w => dprintf `" const " Pwc class w %
   | RtAddrConst (id, w) =>
       dprintf `" addr const " PP.? id Pwc class w %
   ;
   dprintf `"\n" %
  end

  fun printVars (Lctx { vregs, ... }) =
  let
    fun loop idx =
      if idx = D.length vregs then
        ()
      else (
        printVar idx (D.get vregs idx);
        loop (idx + 1)
      )
  in
    loop 0
  end

  fun constAdd vregs ops vid v insId =
  let
    val { class, defs, use, ... } = D.get vregs vid
    val v =
      case v of
        RtConst w =>
        let
          val w = P.extz w (getClassSize class)
        in
          RtConst w
        end
      | RtAddrConst p => RtAddrConst p
      | RtReg | RtRem => raise Unreachable
    val () = D.set vregs vid { class, defs, use, t = v, canFold = true }

    fun f (SOME _, li) = (NONE, li)
      | f (NONE, _) = raise Unreachable
  in
    dprintf `"%" I vid `", " %;
    D.update ops f insId
  end

  fun getFirstConstants
    (Lctx { vregs, ops, localVars, paramNum, ... }) =
  let
    fun loop vid acc =
      if vid = D.length vregs then
        rev acc
      else
        let
          val { defs, t, ... } = D.get vregs vid

          fun addConstFromSet arg def =
          let
            val v =
              case arg of
                SaConst w => RtConst w
              | SaAddr p => RtAddrConst p
              | _ => raise Unreachable
          in
            constAdd vregs ops vid v def
          end
        in
          case (t, defs) of
            (RtReg, [def]) =>
            let
              val ins = valOf o #1 $ D.get ops def
            in
              case ins of
                IrSet(_, arg as SaConst _ | arg as SaAddr _) =>
                let
                  val () = addConstFromSet arg def
                in
                  loop (vid + 1) (vid :: acc)
                end
              | _ => loop (vid + 1) acc
            end
          | (RtReg, _) => loop (vid + 1) acc
          | (RtConst _, _) => loop (vid + 1) (vid :: acc)
          | _ => raise Unreachable
        end
  in
    loop (Vector.length localVars + paramNum) []
  end

  datatype ext = ExtSign | ExtZero

  fun getCS vregs r =
  let
    val { class, ... } = D.get vregs r
  in
    getClassSize class
  end

  fun evalPrep supThird vregs (rd, rs1, rs2) ext =
  let
    val getC = getCS vregs

    val () =
      if getC rs1 <> getC rs2 then
        raise Unreachable
      else
        if not supThird andalso getC rd <> getC rs1 then
          raise Unreachable
        else
          ()

    val rt1 = getRegType vregs rs1
    val rt2 = getRegType vregs rs2
    val size = getC rd

  in
    case (rt1, rt2) of
      (RtConst w1, RtConst w2) => (
        case ext of
          ExtZero => SOME (P.extz w1 size, P.extz w2 size)
        | ExtSign => SOME (P.exts w1 size, P.exts w2 size)
      )
    | _ => NONE
  end

  fun evalSimple supThird vregs ext triple op' =
    case evalPrep supThird vregs triple ext of
      SOME wp => RtConst (op' wp)
    | NONE => RtReg

  fun evalSet vregs (rd, SaVReg rs) =
  let
    val rt = getRegType vregs rs
    val { canFold, ... } = D.get vregs rd

    val fromSize = getCS vregs rs
    val toSize = getCS vregs rd
  in
    if canFold = false then
      RtReg
    else
      case rt of
        RtConst w => RtConst $ P.extz w fromSize
      | RtAddrConst p =>
          if toSize = 0w8 then
            RtAddrConst p
          else
            RtReg
      | _ => raise Unreachable
  end
    | evalSet _ _ = raise Unreachable

  fun evalExt vregs ext (_, rs, aClass) =
  let
    val rt = getRegType vregs rs
    val ext = if ext = ExtZero then P.extz else P.exts
  in
    case rt of
      RtConst w => RtConst $ ext w (ac2word aClass)
    | RtAddrConst p =>
        if aClass = AC8 then
          RtAddrConst p
        else
          RtReg
    | _ => raise Unreachable
  end

  fun evalAddSub op' vregs (rd, rs1, rs2) =
  let
    val getC = getCS vregs
    val () =
      if getC rs1 <> getC rs2 orelse getC rd <> getC rs1 then
        raise Unreachable
      else
        ()
    val size = getC rd

    val rt1 = getRegType vregs rs1
    val rt2 = getRegType vregs rs2
  in
    case (rt1, rt2) of
      (RtConst w1, RtConst w2) => RtConst $ P.extz (op' (w1, w2)) size
    | (RtAddrConst (id, w1), RtConst w2) => RtAddrConst (id, op' (w1, w2))
    | (RtRem, _) | (_, RtRem) => raise Unreachable
    | _ => RtReg
  end

  fun eval' ins vregs =
  let
    open Word

    val es = evalSimple false vregs
    val esu = es ExtZero
    val ess = es ExtSign

    fun opCommon conv op' (w1, w2) =
    let
      val i1 = word64Toint64 w1
      val i2 = word64Toint64 w2
    in
      conv (op' (i1, i2))
    end

    val sop = opCommon int64Toword64

    fun bop op' wp =
    let
      val res = op' wp
    in
      if res then 0w1 else 0w0
    end

    val sbop = opCommon (fn true => 0w1 | false => 0w0)

    fun eq wp = case compare wp of EQUAL => 0w1 | _ => 0w0
    fun neq wp = case compare wp of EQUAL => 0w0 | _ => 0w1

    fun ecmp (cmpOp, vr1, vr2, vr3) =
    let
      val t = (vr1, vr2, vr3)
    in
      case cmpOp of
        Cmpeq => evalSimple true vregs ExtZero t eq
      | Cmpneq => evalSimple true vregs ExtZero t neq
      | Cmpul => esu t (bop Word.<)
      | Cmpug => esu t (bop Word.>)
      | Cmpule => esu t (bop Word.<=)
      | Cmpuge => esu t (bop Word.>=)
      | Cmpsl => ess t (sbop Int64.<)
      | Cmpsg => ess t (sbop Int64.>)
      | Cmpsle => ess t (sbop Int64.<=)
      | Cmpsge => ess t (sbop Int64.>=)
    end
  in
    case ins of
      IrAdd t => evalAddSub Word.+ vregs t
    | IrSub t => evalAddSub Word.- vregs t

    | IrAnd t => esu t Word.andb
    | IrOr t => esu t Word.orb
    | IrXor t => esu t Word.xorb
    | IrMul t => esu t Word.*
    | IrIMul t => ess t (sop Int64.*)
    | IrDiv t => esu t Word.div
    | IrIDiv t => ess t (sop Int64.div)
    | IrMod t => esu t Word.mod
    | IrIMod t => esu t (sop Int64.mod)
    | IrShl t => esu t Word.<<
    | IrShr t => esu t Word.>>
    | IrSar t => ess t Word.~>>

    | IrCmp q => ecmp q

    | IrSet t => evalSet vregs t
    | IrExtZero p => evalExt vregs ExtZero p
    | IrExtSign p => evalExt vregs ExtSign p

    | IrAlloc _ | IrLoad _ | IrStore _ | IrRet _ | IrFcall _
    | IrCopy _ | IrJz _ | IrJnz _  => RtReg
    | IrJmpc _ | IrJmp _ | IrNop _ | IrNopLabel _ => raise Unreachable
  end

  fun eval (SOME ins) vregs =
  let
    val res = eval' ins vregs
  in
    case res of
      RtConst w =>
      let
        val { defs, ...  } = getInsInfo ins
        val { class, ... } = D.get vregs (hd defs)
        val w = P.extz w (getClassSize class)
      in
        RtConst w
      end
    | RtRem => raise Unreachable
    | _ => res
  end
   | eval NONE _ = RtReg

  fun defines (SOME ins) =
  let
    val { defs, ... } = getInsInfo ins
  in
    hd defs
  end
    | defines NONE = raise Unreachable

  fun propagate [] _ _ = ()
    | propagate (v :: vs) vregs ops =
    let
      val { use, ... } = D.get vregs v

      (*
      val () = printfn `"Took from worklist: " I v  %
      val () = printfn `"usage: " Plist i use (", ", true, 0) %
      *)

      fun loop (insId :: tail) acc =
      let
        val (ins, li) = D.get ops insId

        (*
        val () = printfn `"v: " I v `", Ins: " I insId %
        *)
      in
        case eval ins vregs of
          RtReg => loop tail acc
        | RtRem => raise Unreachable
        | v =>
        let
          val vd = defines ins
          val { defs, ... } = D.get vregs vd
          val newConst =
            case defs of
              [_] => (constAdd vregs ops vd v insId; SOME vd)
            | _ =>
              let
                val vl =
                  case v of
                    RtConst w => SaConst w
                  | RtAddrConst p => SaAddr p
                  | _ => raise Unreachable
                val ins = IrSet (vd, vl)
              in
                D.set ops insId (SOME ins, li);
                NONE
              end
        in
          loop tail (case newConst of SOME vc => vc :: acc | NONE => acc)
        end
      end
        | loop [] acc = acc

      val newConst = loop use []
    in
      propagate (List.revAppend (newConst, vs)) vregs ops
    end

  fun constPropagate (C as Lctx { vregs, ops, ... }) =
  let
    val () = dprintf `"constants: " %
    val worklist = getFirstConstants C
  in
    propagate worklist vregs ops;
    dprintf `"\n" %
  end

  fun changeDest rd ins =
  let
    fun tr (_, rs1, rs2) = (rd, rs1, rs2)
  in
    case ins of
      IrAdd t => IrAdd (tr t)
    | IrSub t => IrSub (tr t)
    | IrMul t => IrMul (tr t)
    | IrIMul t => IrIMul (tr t)
    | IrDiv t => IrDiv (tr t)
    | IrIDiv t => IrIDiv (tr t)
    | IrMod t => IrMod (tr t)
    | IrIMod t => IrIMod (tr t)

    | IrCmp (cmpOp, _, vr2, vr3) => IrCmp (cmpOp, rd, vr2, vr3)

    | IrAnd t => IrAnd (tr t)
    | IrOr t => IrOr (tr t)
    | IrXor t => IrXor (tr t)
    | IrExtSign t => IrExtSign (tr t)
    | IrExtZero t => IrExtZero (tr t)
    | IrLoad (_, rs, am) => IrLoad (rd, rs, am)
    | IrShl t => IrShl (tr t)
    | IrShr t => IrShr (tr t)
    | IrSar t => IrSar (tr t)
    | IrFcall (_, f, args) => IrFcall (rd, f, args)
    | IrRet (SOME _) => IrRet (SOME rd)
    | IrRet NONE => raise Unreachable
    | IrSet (_, arg) => IrSet (rd, arg)
    | IrNop _ | IrNopLabel _ | IrAlloc _ | IrCopy _ | IrJmp _ | IrJz _
    | IrJmpc _ | IrJnz _ | IrStore _ => raise Unreachable
  end

  fun removeVR vregs vr =
  let
    val { class, ... } = D.get vregs vr
  in
    D.set vregs vr
      { defs = [], use = [], class, t = RtRem, canFold = false }
  end

  fun changeIns ops idx ins =
  let
    fun f (SOME _, v) = (ins, v)
      | f (NONE, _) = raise Unreachable
  in
    D.update ops f idx
  end

  fun changeEl from to (x :: xs) acc =
    changeEl from to xs $ (if x = from then to else x) :: acc
    | changeEl _ _ [] acc = rev acc

  fun changeDef vregs vr from to =
  let
    val { defs, use, class, t, canFold } = D.get vregs vr
  in
    D.set vregs vr { defs = changeEl from to defs [], use, class, t,
    canFold }
  end

  fun changeUse vregs vr from to =
  let
    val { defs, use, class, t, canFold } = D.get vregs vr
  in
    D.set vregs vr { defs, use = changeEl from to use [], class, t,
      canFold }
  end

  fun singleDefUse vregs vr =
  let
    val { defs, use, ... } = D.get vregs vr
  in
    case (defs, use) of
      ([d], [_]) => SOME d
    | _ => NONE
  end

  fun fuseSet (Lctx { vregs, localVars, paramNum, ops, ... })
    (idx, (SOME (IrSet (rd, SaVReg rs)), _))
  =
    if getCS vregs rd <> getCS vregs rs then
      ()
    else (
      case singleDefUse vregs rs of
        NONE => ()
      | SOME d => (
        if d = idx - 1 andalso rs >= Vector.length localVars + paramNum
        then
          let
            val () = dprintf `"fusing instruction " I d `" with " I idx `"\n" %
            val () = removeVR vregs rs

            val ins = valOf o #1 $ D.get ops (idx - 1)
            val ir = changeDest rd ins

            val () = changeIns ops (idx - 1) (SOME ir)
            val () = changeIns ops idx NONE
          in
            changeDef vregs rd idx (idx - 1)
          end
        else
          ()
       )
     )
    | fuseSet _ _ = ()

  fun convCmpOp op' false = op'
    | convCmpOp op' true =
      case op' of
        Cmpeq => Cmpneq
      | Cmpneq => Cmpeq
      | Cmpul => Cmpuge
      | Cmpug => Cmpule
      | Cmpule => Cmpug
      | Cmpuge => Cmpul
      | Cmpsl => Cmpsge
      | Cmpsg => Cmpsle
      | Cmpsle => Cmpsg
      | Cmpsge => Cmpsl

  fun fuseJmpCommon (Lctx { ops, vregs, ... }) idx rs lid isRev =
    case singleDefUse vregs rs of
      NONE => ()
    | SOME d => (
      case #1 $ D.get ops d of
        SOME (IrCmp (op', _, r1, r2)) =>
          let
            val () = dprintf `"fusing instruction " I d `" with " I idx %
            val () = removeVR vregs rs
            val () = changeUse vregs r1 d idx
            val () = changeUse vregs r2 d idx
            val ins = IrJmpc (convCmpOp op' isRev, r1, r2, lid)
          in
            changeIns ops d NONE;
            changeIns ops idx (SOME ins)
          end
      | _ => ()
    )

  fun fuseJmpc C (idx, (ins, _)) =
    case ins of
      SOME (IrJnz (rs, lid)) => fuseJmpCommon C idx rs lid false
    | SOME (IrJz (rs, lid)) => fuseJmpCommon C idx rs lid true
    | _ => ()

  fun log2 0w0 = NONE
    | log2 w =
    let
      open Word

      fun log 0w0 = raise Unreachable
        | log 0w1 = 0w0
        | log w = 0w1 + log (>> (w, 0w1))

    in
      if andb (w, w - 0w1) = 0w0 then
        SOME $ log w
      else
        NONE
    end

  fun lowerMul' (C as Lctx { vregs, ops, ... }) idx (rd, rs1, rs2) =
  let
    val { class, ... } = D.get vregs rd
    val { t = t1, ... } = D.get vregs rs1
    val { t = t2, ... } = D.get vregs rs2

    fun tryLower rs v =
      case log2 v of
        SOME 0w0 => changeIns ops idx (SOME $ IrSet (rd, SaVReg rs))
      | SOME log =>
          let
            val log = newConst C class log
          in
            changeIns ops idx (SOME (IrShl (rd, rs, log)))
          end
      | NONE => ()
  in
    case (t1, t2) of
      (RtConst v, _) => tryLower rs2 v
    | (_, RtConst v) => tryLower rs1 v
    | _ => ()
  end

  fun lowerMul C (idx, (ins, _)) =
    case ins of
      SOME (IrMul t) => lowerMul' C idx t
    | SOME (IrIMul t) => lowerMul' C idx t
    | _ => ()

  fun lowerDiv (C as Lctx { vregs, ops, ... })
    (idx, (SOME (IrDiv (rd, rs1, rs2)), _))
  =
  let
    val { class, ... } = D.get vregs rd
    val { t = t1, ... } = D.get vregs rs1
    val { t = t2, ... } = D.get vregs rs2
  in
    case (t1, t2) of
      (_, RtConst c) => (
        case log2 c of
          NONE => ()
        | SOME 0w0 => changeIns ops idx (SOME $ IrSet (rd, SaVReg rs1))
        | SOME log =>
          let
            val log = newConst C class log
          in
            changeIns ops idx (SOME $ IrShr (rd, rs1, log))
          end
      )
    | _ => ()
  end
    | lowerDiv _ _ = ()

  fun peephole (C as Lctx { ops, ... }) = (
    D.appi (fuseSet C) ops;
    D.appi (fuseJmpc C) ops;
    D.appi (lowerMul C) ops;
    D.appi (lowerDiv C) ops
  )

  fun removeUnusedLabels (Lctx { ops, labels, ... }) =
  let
    val () = dprintf `"removing labels: " %
    fun rem (insId, (op', _)) =
      case op' of
        SOME (IrNopLabel lid) =>
        let
          val (_, usage) = D.get labels lid
        in
          if usage = 0 andalso lid <> 0 then (
            dprintf `"L" I lid `", " %;
            D.set ops insId (NONE, NONE)
          ) else
            ()
        end
      | _ => ()

    fun loop idx =
      if idx = D.length ops then
        ()
      else (
        rem (idx, D.get ops idx);
        loop (idx + 1)
      )
  in
    loop 0;
    dprintf `"\n" %
  end

  fun removeUnusedVars (Lctx { fname, vregs, localVars, ... }) =
  let
    fun die' idx =
    let
      val varName = #name $ Vector.sub (localVars, idx)
    in
      die 1 PP.? fname `": " PP.? varName
        `": variable is used uninitialized" %
    end
    fun loop idx =
      if idx = D.length vregs then
        ()
      else
        let
          val { defs, use, t, class, canFold } = D.get vregs idx
          val t =
            if t = RtReg andalso defs = [] then
              case use of
                [] => RtRem
              | _ =>
                if idx < Vector.length localVars then
                  die' idx
                else
                  raise Unreachable
            else
              t
        in
          D.set vregs idx { defs, use, t, class, canFold };
          loop (idx + 1)
        end
  in
    loop 0
  end

  fun translateFn ({ localVars, stmt, paramNum, name, ... }: P.funcInfo) =
  let
    val () = dprintf `"\n\nfunction " PP.? name `"\n\n" %

    val ctx = createLocalCtx name localVars paramNum
    val () = convStmt ctx stmt

    val () = ctxPutOp ctx (IrNopLabel 0)

    val () = printIns ctx
    (*
    val () = printVars ctx
    *)

    val () = dprintf `"\nconstant propagation\n\n" %
    val () = constPropagate ctx
    (*
    val () = printVars ctx
    val () = printIns ctx
    *)

    val () = dprintf `"\nmisc il optimizations\n\n" %

    val () = removeUnusedLabels ctx
    val () = removeUnusedVars ctx
    val () = peephole ctx

    val () = printVars ctx
    val () = printIns ctx

    (*
    val () = dprintf `"\nvariables\n\n" %
    val () = printVars ctx
    *)

    val Lctx { vregs, ops, labels, ft, ... } = ctx
  in
    Fi {
      name, localBound = Vector.length localVars + paramNum,
      t = !ft,
      paramNum, vregs, ops,
      labels = D.copy labels (fn (v, _) => v)
    }
  end

  fun createCtx ({ ext, glob, objsZI, objs, funcs, strlits }) debugFileName =
  let
    val () =
      case debugFileName of
        NONE => ()
      | SOME fname => debugFile := SOME (TextIO.openOut fname)

    val fis = List.map (fn func => translateFn func) funcs
  in
    Ctx { objs, objsZI, extSyms = ext, globSyms = glob,
      funcInfos = fis, strlits }
  end
end