summaryrefslogtreecommitdiff
path: root/compiler/GHC/CmmToAsm/Wasm/FromCmm.hs
blob: 9a4c3f34c26580040110137a6d940dd35877d366 (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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Use camelCase" #-}
module GHC.CmmToAsm.Wasm.FromCmm
  ( alignmentFromWordType,
    globalInfoFromCmmGlobalReg,
    supportedCmmGlobalRegs,
    onCmmGroup,
  )
where

import Control.Monad
import qualified Data.ByteString as BS
import Data.Foldable
import Data.Functor
import qualified Data.IntSet as IS
import Data.Semigroup
import Data.String
import Data.Traversable
import Data.Type.Equality
import GHC.Cmm
import GHC.Cmm.BlockId
import GHC.Cmm.CLabel
import GHC.Cmm.Dataflow.Block
import GHC.Cmm.Dataflow.Label
import GHC.Cmm.InitFini
import GHC.CmmToAsm.Wasm.Types
import GHC.CmmToAsm.Wasm.Utils
import GHC.Float
import GHC.Platform
import GHC.Prelude
import GHC.StgToCmm.CgUtils
import GHC.Types.Basic
import GHC.Types.ForeignCall
import GHC.Types.Unique
import GHC.Types.Unique.FM
import GHC.Types.Unique.Map
import GHC.Types.Unique.Supply
import GHC.Utils.Outputable hiding ((<>))
import GHC.Utils.Panic
import GHC.Wasm.ControlFlow.FromCmm

-- | Calculate the wasm representation type from a 'CmmType'. This is
-- a lossy conversion, and sometimes we need to pass the original
-- 'CmmType' or at least its 'Width' around, so to properly add
-- subword truncation or extension logic.
someWasmTypeFromCmmType :: CmmType -> SomeWasmType
someWasmTypeFromCmmType t
  | isWord32 t = SomeWasmType TagI32
  | isWord64 t = SomeWasmType TagI64
  | t `cmmEqType` b16 = SomeWasmType TagI32
  | t `cmmEqType` b8 = SomeWasmType TagI32
  | isFloat64 t = SomeWasmType TagF64
  | isFloat32 t = SomeWasmType TagF32
  | otherwise =
      panic $
        "someWasmTypeFromCmmType: unsupported CmmType "
          <> showSDocOneLine defaultSDocContext (ppr t)

-- | Calculate the optional memory narrowing of a 'CmmLoad' or
-- 'CmmStore'.
wasmMemoryNarrowing :: WasmTypeTag t -> CmmType -> Maybe Int
wasmMemoryNarrowing ty ty_cmm = case (# ty, typeWidth ty_cmm #) of
  (# TagI32, W8 #) -> Just 8
  (# TagI32, W16 #) -> Just 16
  (# TagI32, W32 #) -> Nothing
  (# TagI64, W8 #) -> Just 8
  (# TagI64, W16 #) -> Just 16
  (# TagI64, W32 #) -> Just 32
  (# TagI64, W64 #) -> Nothing
  (# TagF32, W32 #) -> Nothing
  (# TagF64, W64 #) -> Nothing
  _ -> panic "wasmMemoryNarrowing: unreachable"

-- | Despite this is used by the WebAssembly native codegen, we use
-- 'pprCLabel' instead of 'pprAsmLabel' when emitting the textual
-- symbol name. Either one would work, but 'pprCLabel' makes the
-- output assembly code looks closer to the unregisterised codegen
-- output, which can be handy when using the unregisterised codegen as
-- a source of truth when debugging the native codegen.
symNameFromCLabel :: CLabel -> SymName
symNameFromCLabel lbl =
  fromString $
    showSDocOneLine defaultSDocContext {sdocStyle = PprCode} $
      pprCLabel genericPlatform lbl

-- | Calculate a symbol's visibility.
symVisibilityFromCLabel :: CLabel -> SymVisibility
symVisibilityFromCLabel lbl
  | externallyVisibleCLabel lbl = SymDefault
  | otherwise = SymStatic

-- | Calculate a symbol's kind, see haddock docs of 'SymKind' for more
-- explanation.
symKindFromCLabel :: CLabel -> SymKind
symKindFromCLabel lbl
  | isCFunctionLabel lbl = SymFunc
  | otherwise = SymData

-- | Calculate a data section's kind, see haddock docs of
-- 'DataSectionKind' for more explanation.
dataSectionKindFromCmmSection :: Section -> DataSectionKind
dataSectionKindFromCmmSection s = case sectionProtection s of
  ReadWriteSection -> SectionData
  _ -> SectionROData

-- | Calculate the natural alignment size given the platform word
-- type.
alignmentFromWordType :: WasmTypeTag w -> Alignment
alignmentFromWordType TagI32 = mkAlignment 4
alignmentFromWordType TagI64 = mkAlignment 8
alignmentFromWordType _ = panic "alignmentFromWordType: unreachable"

-- | Calculate a data section's alignment. As a conservative
-- optimization, a data section with a single CmmString/CmmFileEmbed
-- has no alignment requirement, otherwise we always align to the word
-- size to satisfy pointer tagging requirements and avoid unaligned
-- loads/stores.
alignmentFromCmmSection :: WasmTypeTag w -> [DataSectionContent] -> Alignment
alignmentFromCmmSection _ [DataASCII {}] = mkAlignment 1
alignmentFromCmmSection _ [DataIncBin {}] = mkAlignment 1
alignmentFromCmmSection t _ = alignmentFromWordType t

-- | Lower a 'CmmStatic'.
lower_CmmStatic :: CmmStatic -> WasmCodeGenM w DataSectionContent
lower_CmmStatic s = case s of
  CmmStaticLit (CmmInt i W8) -> pure $ DataI8 $ fromInteger $ narrowU W8 i
  CmmStaticLit (CmmInt i W16) -> pure $ DataI16 $ fromInteger $ narrowU W16 i
  CmmStaticLit (CmmInt i W32) -> pure $ DataI32 $ fromInteger $ narrowU W32 i
  CmmStaticLit (CmmInt i W64) -> pure $ DataI64 $ fromInteger $ narrowU W64 i
  CmmStaticLit (CmmFloat f W32) -> pure $ DataF32 $ fromRational f
  CmmStaticLit (CmmFloat d W64) -> pure $ DataF64 $ fromRational d
  CmmStaticLit (CmmLabel lbl) ->
    onAnySym lbl
      $> DataSym
        (symNameFromCLabel lbl)
        0
  CmmStaticLit (CmmLabelOff lbl o) ->
    onAnySym lbl
      $> DataSym
        (symNameFromCLabel lbl)
        o
  CmmUninitialised i -> pure $ DataSkip i
  CmmString b -> pure $ DataASCII b
  CmmFileEmbed f l -> pure $ DataIncBin f l
  _ -> panic "lower_CmmStatic: unreachable"

{-
Note [Register mapping on WebAssembly]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unlike typical ISAs, WebAssembly doesn't expose a fixed set of
registers. For now, we map each Cmm LocalReg to a wasm local, and each
Cmm GlobalReg to a wasm global. The wasm globals are defined in
rts/wasm/Wasm.S, and must be kept in sync with
'globalInfoFromCmmGlobalReg' and 'supportedCmmGlobalRegs' here.

There are some other Cmm GlobalRegs which are still represented by
StgRegTable fields instead of wasm globals (e.g. HpAlloc). It's cheap
to add wasm globals, but other parts of rts logic only work with the
StgRegTable fields, so we also need to instrument StgRun/StgReturn to
sync the wasm globals with the StgRegTable. It's not really worth the
trouble.

-}
globalInfoFromCmmGlobalReg :: WasmTypeTag w -> GlobalReg -> Maybe GlobalInfo
globalInfoFromCmmGlobalReg t reg = case reg of
  VanillaReg i
    | i >= 1 && i <= 10 -> Just (fromString $ "__R" <> show i, ty_word)
  FloatReg i
    | i >= 1 && i <= 6 ->
        Just (fromString $ "__F" <> show i, SomeWasmType TagF32)
  DoubleReg i
    | i >= 1 && i <= 6 ->
        Just (fromString $ "__D" <> show i, SomeWasmType TagF64)
  LongReg i
    | i == 1 -> Just (fromString $ "__L" <> show i, SomeWasmType TagI64)
  Sp -> Just ("__Sp", ty_word)
  SpLim -> Just ("__SpLim", ty_word)
  Hp -> Just ("__Hp", ty_word)
  HpLim -> Just ("__HpLim", ty_word)
  _ -> Nothing
  where
    ty_word = SomeWasmType t

supportedCmmGlobalRegs :: [GlobalReg]
supportedCmmGlobalRegs =
  [VanillaReg i | i <- [1 .. 10]]
    <> [FloatReg i | i <- [1 .. 6]]
    <> [DoubleReg i | i <- [1 .. 6]]
    <> [LongReg i | i <- [1 .. 1]]
    <> [Sp, SpLim, Hp, HpLim]

-- | Truncate a subword.
truncSubword :: Width -> WasmTypeTag t -> WasmExpr w t -> WasmExpr w t
truncSubword W8 ty (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmConst ty 0xFF `WasmConcat` WasmAnd ty
truncSubword W16 ty (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmConst ty 0xFFFF `WasmConcat` WasmAnd ty
truncSubword _ _ expr = expr

-- | Sign-extend a subword.
extendSubword :: Width -> WasmTypeTag t -> WasmExpr w t -> WasmExpr w t
extendSubword W8 TagI32 (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmI32Extend8S
extendSubword W16 TagI32 (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmI32Extend16S
extendSubword W8 TagI64 (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmI64Extend8S
extendSubword W16 TagI64 (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmI64Extend16S
extendSubword W32 TagI64 (WasmExpr instr) =
  WasmExpr $ instr `WasmConcat` WasmI64Extend32S
extendSubword _ _ expr = expr

-- | Lower an unary homogeneous operation.
lower_MO_Un_Homo ::
  ( forall pre t.
    WasmTypeTag t ->
    WasmInstr
      w
      (t : pre)
      (t : pre)
  ) ->
  CLabel ->
  CmmType ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Un_Homo op lbl t0 [x] = case someWasmTypeFromCmmType t0 of
  SomeWasmType ty -> do
    WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
    pure $
      SomeWasmExpr ty $
        WasmExpr $
          x_instr `WasmConcat` op ty
lower_MO_Un_Homo _ _ _ _ = panic "lower_MO_Un_Homo: unreachable"

-- | Lower a binary homogeneous operation. Homogeneous: result type is
-- the same with operand types.
lower_MO_Bin_Homo ::
  ( forall pre t.
    WasmTypeTag t ->
    WasmInstr
      w
      (t : t : pre)
      (t : pre)
  ) ->
  CLabel ->
  CmmType ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Bin_Homo op lbl t0 [x, y] = case someWasmTypeFromCmmType t0 of
  SomeWasmType ty -> do
    WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
    WasmExpr y_instr <- lower_CmmExpr_Typed lbl ty y
    pure $
      SomeWasmExpr ty $
        WasmExpr $
          x_instr `WasmConcat` y_instr `WasmConcat` op ty
lower_MO_Bin_Homo _ _ _ _ = panic "lower_MO_Bin_Homo: unreachable"

-- | Lower a binary homogeneous operation, and truncate the result if
-- it's a subword.
lower_MO_Bin_Homo_Trunc ::
  (forall pre t. WasmTypeTag t -> WasmInstr w (t : t : pre) (t : pre)) ->
  CLabel ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Bin_Homo_Trunc op lbl w0 [x, y] =
  case someWasmTypeFromCmmType (cmmBits w0) of
    SomeWasmType ty -> do
      WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
      WasmExpr y_instr <- lower_CmmExpr_Typed lbl ty y
      pure $
        SomeWasmExpr ty $
          truncSubword w0 ty $
            WasmExpr $
              x_instr `WasmConcat` y_instr `WasmConcat` op ty
lower_MO_Bin_Homo_Trunc _ _ _ _ = panic "lower_MO_Bin_Homo_Trunc: unreachable"

-- | Lower a binary homogeneous operation, first sign extending the
-- operands, then truncating the result.
lower_MO_Bin_Homo_Ext_Trunc ::
  (forall pre t. WasmTypeTag t -> WasmInstr w (t : t : pre) (t : pre)) ->
  CLabel ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Bin_Homo_Ext_Trunc op lbl w0 [x, y] =
  case someWasmTypeFromCmmType (cmmBits w0) of
    SomeWasmType ty -> do
      WasmExpr x_instr <-
        extendSubword w0 ty <$> lower_CmmExpr_Typed lbl ty x
      WasmExpr y_instr <-
        extendSubword w0 ty <$> lower_CmmExpr_Typed lbl ty y
      pure $
        SomeWasmExpr ty $
          truncSubword w0 ty $
            WasmExpr $
              x_instr `WasmConcat` y_instr `WasmConcat` op ty
lower_MO_Bin_Homo_Ext_Trunc _ _ _ _ =
  panic "lower_MO_Bin_Homo_Ext_Trunc: unreachable"

-- | Lower a relational binary operation, first sign extending the
-- operands. Relational: result type is a boolean (word type).
lower_MO_Bin_Rel_Ext ::
  (forall pre t. WasmTypeTag t -> WasmInstr w (t : t : pre) (w : pre)) ->
  CLabel ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Bin_Rel_Ext op lbl w0 [x, y] =
  case someWasmTypeFromCmmType (cmmBits w0) of
    SomeWasmType ty -> do
      WasmExpr x_instr <-
        extendSubword w0 ty <$> lower_CmmExpr_Typed lbl ty x
      WasmExpr y_instr <-
        extendSubword w0 ty <$> lower_CmmExpr_Typed lbl ty y
      ty_word <- wasmWordTypeM
      pure $
        SomeWasmExpr ty_word $
          WasmExpr $
            x_instr `WasmConcat` y_instr `WasmConcat` op ty
lower_MO_Bin_Rel_Ext _ _ _ _ = panic "lower_MO_Bin_Rel_Ext: unreachable"

-- | Lower a relational binary operation.
lower_MO_Bin_Rel ::
  ( forall pre t.
    WasmTypeTag t ->
    WasmInstr
      w
      (t : t : pre)
      (w : pre)
  ) ->
  CLabel ->
  CmmType ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Bin_Rel op lbl t0 [x, y] = case someWasmTypeFromCmmType t0 of
  SomeWasmType ty -> do
    WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
    WasmExpr y_instr <- lower_CmmExpr_Typed lbl ty y
    ty_word <- wasmWordTypeM
    pure $
      SomeWasmExpr ty_word $
        WasmExpr $
          x_instr `WasmConcat` y_instr `WasmConcat` op ty
lower_MO_Bin_Rel _ _ _ _ = panic "lower_MO_Bin_Rel: unreachable"

-- | Cast a shiftL/shiftR RHS to the same type as LHS. Because we may
-- have a 64-bit LHS and 32-bit RHS, but wasm shift operators are
-- homogeneous.
shiftRHSCast ::
  CLabel ->
  WasmTypeTag t ->
  CmmExpr ->
  WasmCodeGenM
    w
    (WasmExpr w t)
shiftRHSCast lbl t1 x = do
  SomeWasmExpr t0 (WasmExpr x_instr) <- lower_CmmExpr lbl x
  if
      | Just Refl <- t0 `testEquality` t1 -> pure $ WasmExpr x_instr
      | TagI32 <- t0,
        TagI64 <- t1 ->
          pure $ WasmExpr $ x_instr `WasmConcat` WasmI64ExtendI32 Unsigned
      | otherwise -> panic "shiftRHSCast: unreachable"

-- | Lower a 'MO_Shl' operation, truncating the result.
lower_MO_Shl ::
  CLabel ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_MO_Shl lbl w0 [x, y] = case someWasmTypeFromCmmType (cmmBits w0) of
  SomeWasmType ty -> do
    WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
    WasmExpr y_instr <- shiftRHSCast lbl ty y
    pure $
      SomeWasmExpr ty $
        truncSubword w0 ty $
          WasmExpr $
            x_instr `WasmConcat` y_instr `WasmConcat` WasmShl ty
lower_MO_Shl _ _ _ = panic "lower_MO_Shl: unreachable"

-- | Lower a 'MO_U_Shr' operation.
lower_MO_U_Shr ::
  CLabel ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_MO_U_Shr lbl w0 [x, y] = case someWasmTypeFromCmmType (cmmBits w0) of
  SomeWasmType ty -> do
    WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
    WasmExpr y_instr <- shiftRHSCast lbl ty y
    pure $
      SomeWasmExpr ty $
        WasmExpr $
          x_instr `WasmConcat` y_instr `WasmConcat` WasmShr Unsigned ty
lower_MO_U_Shr _ _ _ = panic "lower_MO_U_Shr: unreachable"

-- | Lower a 'MO_S_Shr' operation, first sign-extending the LHS, then
-- truncating the result.
lower_MO_S_Shr ::
  CLabel ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_MO_S_Shr lbl w0 [x, y] = case someWasmTypeFromCmmType (cmmBits w0) of
  SomeWasmType ty -> do
    WasmExpr x_instr <- extendSubword w0 ty <$> lower_CmmExpr_Typed lbl ty x
    WasmExpr y_instr <- shiftRHSCast lbl ty y
    pure $
      SomeWasmExpr ty $
        truncSubword w0 ty $
          WasmExpr $
            x_instr `WasmConcat` y_instr `WasmConcat` WasmShr Signed ty
lower_MO_S_Shr _ _ _ = panic "lower_MO_S_Shr: unreachable"

-- | Lower a 'MO_MulMayOflo' operation. It's translated to a ccall to
-- @hs_mulIntMayOflo@ function in @ghc-prim/cbits/mulIntMayOflo@,
-- otherwise it's quite non-trivial to implement as inline assembly.
lower_MO_MulMayOflo ::
  CLabel -> Width -> [CmmExpr] -> WasmCodeGenM w (SomeWasmExpr w)
lower_MO_MulMayOflo lbl w0 [x, y] = case someWasmTypeFromCmmType ty_cmm of
  SomeWasmType ty -> do
    WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
    WasmExpr y_instr <- lower_CmmExpr_Typed lbl ty y
    onFuncSym "hs_mulIntMayOflo" [ty_cmm, ty_cmm] [ty_cmm]
    pure $
      SomeWasmExpr ty $
        WasmExpr $
          x_instr
            `WasmConcat` y_instr
            `WasmConcat` WasmCCall "hs_mulIntMayOflo"
  where
    ty_cmm = cmmBits w0
lower_MO_MulMayOflo _ _ _ = panic "lower_MO_MulMayOflo: unreachable"

-- | Lower an unary conversion operation.
lower_MO_Un_Conv ::
  ( forall pre t0 t1.
    WasmTypeTag t0 ->
    WasmTypeTag t1 ->
    WasmInstr w (t0 : pre) (t1 : pre)
  ) ->
  CLabel ->
  CmmType ->
  CmmType ->
  [CmmExpr] ->
  WasmCodeGenM w (SomeWasmExpr w)
lower_MO_Un_Conv op lbl t0 t1 [x] =
  case (# someWasmTypeFromCmmType t0, someWasmTypeFromCmmType t1 #) of
    (# SomeWasmType ty0, SomeWasmType ty1 #) -> do
      WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty0 x
      pure $ SomeWasmExpr ty1 $ WasmExpr $ x_instr `WasmConcat` op ty0 ty1
lower_MO_Un_Conv _ _ _ _ _ = panic "lower_MO_Un_Conv: unreachable"

-- | Lower a 'MO_SS_Conv' operation.
lower_MO_SS_Conv ::
  CLabel ->
  Width ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_MO_SS_Conv lbl w0 w1 [x]
  | w0 == w1 = lower_CmmExpr lbl x
lower_MO_SS_Conv lbl w0 w1 [CmmLoad ptr _ align]
  | w0 < w1,
    w1 <= W32 = do
      (WasmExpr ptr_instr, o) <- lower_CmmExpr_Ptr lbl ptr
      pure $
        SomeWasmExpr TagI32 $
          truncSubword w1 TagI32 $
            WasmExpr $
              ptr_instr
                `WasmConcat` WasmLoad
                  TagI32
                  (wasmMemoryNarrowing TagI32 (cmmBits w0))
                  Signed
                  o
                  align
  | w0 > w1 =
      SomeWasmExpr TagI32
        <$> lower_CmmLoad_Typed
          lbl
          ptr
          TagI32
          (cmmBits w1)
          align
lower_MO_SS_Conv lbl w0 W64 [CmmLoad ptr _ align] = do
  (WasmExpr ptr_instr, o) <- lower_CmmExpr_Ptr lbl ptr
  pure $
    SomeWasmExpr TagI64 $
      WasmExpr $
        ptr_instr
          `WasmConcat` WasmLoad
            TagI64
            (wasmMemoryNarrowing TagI64 (cmmBits w0))
            Signed
            o
            align
lower_MO_SS_Conv lbl w0 w1 [x]
  | w0 < w1,
    w1 <= W32 = do
      x_expr <- lower_CmmExpr_Typed lbl TagI32 x
      pure $
        SomeWasmExpr TagI32 $
          truncSubword w1 TagI32 $
            extendSubword w0 TagI32 x_expr
  | W32 >= w0,
    w0 > w1 = do
      x_expr <- lower_CmmExpr_Typed lbl TagI32 x
      pure $ SomeWasmExpr TagI32 $ truncSubword w1 TagI32 x_expr
lower_MO_SS_Conv lbl W32 W64 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagI32 x
  pure $
    SomeWasmExpr TagI64 $
      WasmExpr $
        x_instr `WasmConcat` WasmI64ExtendI32 Signed
lower_MO_SS_Conv lbl w0 W64 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagI32 x
  pure $
    SomeWasmExpr TagI64 $
      extendSubword w0 TagI64 $
        WasmExpr $
          x_instr `WasmConcat` WasmI64ExtendI32 Unsigned
lower_MO_SS_Conv lbl W64 w1 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagI64 x
  pure $
    SomeWasmExpr TagI32 $
      truncSubword w1 TagI32 $
        WasmExpr $
          x_instr `WasmConcat` WasmI32WrapI64
lower_MO_SS_Conv _ _ _ _ = panic "lower_MO_SS_Conv: unreachable"

-- | Lower a 'MO_UU_Conv' operation.
lower_MO_UU_Conv ::
  CLabel ->
  Width ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_MO_UU_Conv lbl w0 w1 [CmmLoad ptr _ align] =
  case someWasmTypeFromCmmType (cmmBits w1) of
    SomeWasmType ty ->
      SomeWasmExpr ty
        <$> lower_CmmLoad_Typed
          lbl
          ptr
          ty
          (cmmBits (min w0 w1))
          align
lower_MO_UU_Conv lbl w0 w1 [x]
  | w0 == w1 = lower_CmmExpr lbl x
  | w0 < w1, w1 <= W32 = lower_CmmExpr lbl x
  | W32 >= w0,
    w0 > w1 = do
      x_expr <- lower_CmmExpr_Typed lbl TagI32 x
      pure $ SomeWasmExpr TagI32 $ truncSubword w1 TagI32 x_expr
lower_MO_UU_Conv lbl _ W64 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagI32 x
  pure $
    SomeWasmExpr TagI64 $
      WasmExpr $
        x_instr `WasmConcat` WasmI64ExtendI32 Unsigned
lower_MO_UU_Conv lbl W64 w1 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagI64 x
  pure $
    SomeWasmExpr TagI32 $
      truncSubword w1 TagI32 $
        WasmExpr $
          x_instr `WasmConcat` WasmI32WrapI64
lower_MO_UU_Conv _ _ _ _ = panic "lower_MO_UU_Conv: unreachable"

-- | Lower a 'MO_FF_Conv' operation.
lower_MO_FF_Conv ::
  CLabel ->
  Width ->
  Width ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_MO_FF_Conv lbl W32 W64 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagF32 x
  pure $
    SomeWasmExpr TagF64 $
      WasmExpr $
        x_instr `WasmConcat` WasmF64PromoteF32
lower_MO_FF_Conv lbl W64 W32 [x] = do
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl TagF64 x
  pure $
    SomeWasmExpr TagF32 $
      WasmExpr $
        x_instr `WasmConcat` WasmF32DemoteF64
lower_MO_FF_Conv _ _ _ _ = panic "lower_MO_FF_Conv: unreachable"

-- | Lower a 'CmmMachOp'.
lower_CmmMachOp ::
  CLabel ->
  MachOp ->
  [CmmExpr] ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_CmmMachOp lbl (MO_Add w0) xs = lower_MO_Bin_Homo_Trunc WasmAdd lbl w0 xs
lower_CmmMachOp lbl (MO_Sub w0) xs = lower_MO_Bin_Homo_Trunc WasmSub lbl w0 xs
lower_CmmMachOp lbl (MO_Eq w0) xs = lower_MO_Bin_Rel WasmEq lbl (cmmBits w0) xs
lower_CmmMachOp lbl (MO_Ne w0) xs = lower_MO_Bin_Rel WasmNe lbl (cmmBits w0) xs
lower_CmmMachOp lbl (MO_Mul w0) xs = lower_MO_Bin_Homo_Trunc WasmMul lbl w0 xs
lower_CmmMachOp lbl (MO_S_MulMayOflo w0) xs = lower_MO_MulMayOflo lbl w0 xs
lower_CmmMachOp lbl (MO_S_Quot w0) xs =
  lower_MO_Bin_Homo_Ext_Trunc
    (WasmDiv Signed)
    lbl
    w0
    xs
lower_CmmMachOp lbl (MO_S_Rem w0) xs =
  lower_MO_Bin_Homo_Ext_Trunc
    (WasmRem Signed)
    lbl
    w0
    xs
lower_CmmMachOp lbl (MO_S_Neg w0) [x] =
  lower_CmmMachOp
    lbl
    (MO_Sub w0)
    [CmmLit $ CmmInt 0 w0, x]
lower_CmmMachOp lbl (MO_U_Quot w0) xs =
  lower_MO_Bin_Homo
    (WasmDiv Unsigned)
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_U_Rem w0) xs =
  lower_MO_Bin_Homo
    (WasmRem Unsigned)
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_S_Ge w0) xs =
  lower_MO_Bin_Rel_Ext
    (WasmGe Signed)
    lbl
    w0
    xs
lower_CmmMachOp lbl (MO_S_Le w0) xs =
  lower_MO_Bin_Rel_Ext
    (WasmLe Signed)
    lbl
    w0
    xs
lower_CmmMachOp lbl (MO_S_Gt w0) xs =
  lower_MO_Bin_Rel_Ext
    (WasmGt Signed)
    lbl
    w0
    xs
lower_CmmMachOp lbl (MO_S_Lt w0) xs =
  lower_MO_Bin_Rel_Ext
    (WasmLt Signed)
    lbl
    w0
    xs
lower_CmmMachOp lbl (MO_U_Ge w0) xs =
  lower_MO_Bin_Rel
    (WasmGe Unsigned)
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_U_Le w0) xs =
  lower_MO_Bin_Rel
    (WasmLe Unsigned)
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_U_Gt w0) xs =
  lower_MO_Bin_Rel
    (WasmGt Unsigned)
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_U_Lt w0) xs =
  lower_MO_Bin_Rel
    (WasmLt Unsigned)
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_F_Add w0) xs =
  lower_MO_Bin_Homo
    WasmAdd
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Sub w0) xs =
  lower_MO_Bin_Homo
    WasmSub
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Neg w0) xs =
  lower_MO_Un_Homo
    WasmNeg
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Mul w0) xs =
  lower_MO_Bin_Homo
    WasmMul
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Quot w0) xs =
  lower_MO_Bin_Homo
    (WasmDiv Signed)
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Eq w0) xs =
  lower_MO_Bin_Rel
    WasmEq
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Ne w0) xs =
  lower_MO_Bin_Rel
    WasmNe
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Ge w0) xs =
  lower_MO_Bin_Rel
    (WasmGe Signed)
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Le w0) xs =
  lower_MO_Bin_Rel
    (WasmLe Signed)
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Gt w0) xs =
  lower_MO_Bin_Rel
    (WasmGt Signed)
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_F_Lt w0) xs =
  lower_MO_Bin_Rel
    (WasmLt Signed)
    lbl
    (cmmFloat w0)
    xs
lower_CmmMachOp lbl (MO_And w0) xs =
  lower_MO_Bin_Homo
    WasmAnd
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_Or w0) xs = lower_MO_Bin_Homo WasmOr lbl (cmmBits w0) xs
lower_CmmMachOp lbl (MO_Xor w0) xs =
  lower_MO_Bin_Homo
    WasmXor
    lbl
    (cmmBits w0)
    xs
lower_CmmMachOp lbl (MO_Not w0) [x] =
  lower_CmmMachOp
    lbl
    (MO_Xor w0)
    [x, CmmLit $ CmmInt (widthMax w0) w0]
lower_CmmMachOp lbl (MO_Shl w0) xs = lower_MO_Shl lbl w0 xs
lower_CmmMachOp lbl (MO_U_Shr w0) xs = lower_MO_U_Shr lbl w0 xs
lower_CmmMachOp lbl (MO_S_Shr w0) xs = lower_MO_S_Shr lbl w0 xs
lower_CmmMachOp lbl (MO_SF_Conv w0 w1) xs =
  lower_MO_Un_Conv
    (WasmConvert Signed)
    lbl
    (cmmBits w0)
    (cmmFloat w1)
    xs
lower_CmmMachOp lbl (MO_FS_Conv w0 w1) xs =
  lower_MO_Un_Conv
    (WasmTruncSat Signed)
    lbl
    (cmmFloat w0)
    (cmmBits w1)
    xs
lower_CmmMachOp lbl (MO_SS_Conv w0 w1) xs = lower_MO_SS_Conv lbl w0 w1 xs
lower_CmmMachOp lbl (MO_UU_Conv w0 w1) xs = lower_MO_UU_Conv lbl w0 w1 xs
lower_CmmMachOp lbl (MO_XX_Conv w0 w1) xs = lower_MO_UU_Conv lbl w0 w1 xs
lower_CmmMachOp lbl (MO_FF_Conv w0 w1) xs = lower_MO_FF_Conv lbl w0 w1 xs
lower_CmmMachOp _ mop _ =
  pprPanic "lower_CmmMachOp: unreachable" $
    vcat [ text "offending MachOp:" <+> pprMachOp mop ]

-- | Lower a 'CmmLit'. Note that we don't emit 'f32.const' or
-- 'f64.const' for the time being, and instead emit their relative bit
-- pattern as int literals, then use an reinterpret cast. This is
-- simpler than dealing with textual representation of floating point
-- values.
lower_CmmLit :: CmmLit -> WasmCodeGenM w (SomeWasmExpr w)
lower_CmmLit lit = do
  ty_word <- wasmWordTypeM
  case lit of
    CmmInt i w -> case someWasmTypeFromCmmType (cmmBits w) of
      SomeWasmType ty ->
        pure $
          SomeWasmExpr ty $
            WasmExpr $
              WasmConst ty $
                narrowU w i
    CmmFloat f W32 ->
      pure $
        SomeWasmExpr TagF32 $
          WasmExpr $
            WasmConst
              TagI32
              (toInteger $ castFloatToWord32 $ fromRational f)
              `WasmConcat` WasmReinterpret TagI32 TagF32
    CmmFloat f W64 ->
      pure $
        SomeWasmExpr TagF64 $
          WasmExpr $
            WasmConst
              TagI64
              (toInteger $ castDoubleToWord64 $ fromRational f)
              `WasmConcat` WasmReinterpret TagI64 TagF64
    CmmLabel lbl' -> do
      onAnySym lbl'
      let sym = symNameFromCLabel lbl'
      pure $ SomeWasmExpr ty_word $ WasmExpr $ WasmSymConst sym
    CmmLabelOff lbl' o -> do
      onAnySym lbl'
      let sym = symNameFromCLabel lbl'
      pure $
        SomeWasmExpr ty_word $
          WasmExpr $
            WasmSymConst sym
              `WasmConcat` WasmConst ty_word (toInteger o)
              `WasmConcat` WasmAdd ty_word
    CmmBlock bid -> lower_CmmLit $ CmmLabel $ infoTblLbl bid
    _ -> panic "lower_CmmLit: unreachable"

--  | Lower a 'CmmReg'. Some of the logic here wouldn't be needed if
--  we have run 'fixStgRegisters' on the wasm NCG's input Cmm, but we
--  haven't run it yet for certain reasons.
lower_CmmReg :: CLabel -> CmmReg -> WasmCodeGenM w (SomeWasmExpr w)
lower_CmmReg _ (CmmLocal reg) = do
  (reg_i, SomeWasmType ty) <- onCmmLocalReg reg
  pure $ SomeWasmExpr ty $ WasmExpr $ WasmLocalGet ty reg_i
lower_CmmReg lbl (CmmGlobal (GlobalRegUse greg reg_use_ty)) = do
  ty_word <- wasmWordTypeM
  ty_word_cmm <- wasmWordCmmTypeM
  case greg of
    EagerBlackholeInfo ->
      pure $
        SomeWasmExpr ty_word $
          WasmExpr $
            WasmSymConst "stg_EAGER_BLACKHOLE_info"
    GCEnter1 -> do
      onFuncSym "__stg_gc_enter_1" [] [ty_word_cmm]
      pure $ SomeWasmExpr ty_word $ WasmExpr $ WasmSymConst "__stg_gc_enter_1"
    GCFun -> do
      onFuncSym "__stg_gc_fun" [] [ty_word_cmm]
      pure $ SomeWasmExpr ty_word $ WasmExpr $ WasmSymConst "__stg_gc_fun"
    BaseReg -> do
      platform <- wasmPlatformM
      lower_CmmExpr lbl $ regTableOffset platform 0
    _other
      | Just (sym_global, SomeWasmType ty) <-
          globalInfoFromCmmGlobalReg ty_word greg ->
          pure $ SomeWasmExpr ty $ WasmExpr $ WasmGlobalGet ty sym_global
      | otherwise -> do
          platform <- wasmPlatformM
          case someWasmTypeFromCmmType reg_use_ty of
            SomeWasmType ty -> do
              (WasmExpr ptr_instr, o) <-
                lower_CmmExpr_Ptr lbl $
                  get_GlobalReg_addr platform greg
              pure $
                SomeWasmExpr ty $
                  WasmExpr $
                    ptr_instr
                      `WasmConcat` WasmLoad
                        ty
                        Nothing
                        Unsigned
                        o
                        NaturallyAligned

-- | Lower a 'CmmRegOff'.
lower_CmmRegOff :: CLabel -> CmmReg -> Int -> WasmCodeGenM w (SomeWasmExpr w)
lower_CmmRegOff lbl reg 0 = lower_CmmReg lbl reg
lower_CmmRegOff lbl reg o = do
  SomeWasmExpr ty (WasmExpr reg_instr) <- lower_CmmReg lbl reg
  pure $
    SomeWasmExpr ty $
      WasmExpr $
        reg_instr
          `WasmConcat` WasmConst
            ty
            (toInteger o)
          `WasmConcat` WasmAdd ty

-- | Lower a 'CmmLoad', passing in the expected wasm representation
-- type, and also the Cmm type (which contains width info needed for
-- memory narrowing).
--
-- The Cmm type system doesn't track signedness, so all 'CmmLoad's are
-- unsigned loads. However, as an optimization, we do emit signed
-- loads when a 'CmmLoad' result is immediately used as a 'MO_SS_Conv'
-- operand.
lower_CmmLoad_Typed ::
  CLabel ->
  CmmExpr ->
  WasmTypeTag t ->
  CmmType ->
  AlignmentSpec ->
  WasmCodeGenM w (WasmExpr w t)
lower_CmmLoad_Typed lbl ptr_expr ty ty_cmm align = do
  (WasmExpr ptr_instr, o) <- lower_CmmExpr_Ptr lbl ptr_expr
  pure $
    WasmExpr $
      ptr_instr
        `WasmConcat` WasmLoad
          ty
          (wasmMemoryNarrowing ty ty_cmm)
          Unsigned
          o
          align

-- | Lower a 'CmmLoad'.
lower_CmmLoad ::
  CLabel ->
  CmmExpr ->
  CmmType ->
  AlignmentSpec ->
  WasmCodeGenM
    w
    (SomeWasmExpr w)
lower_CmmLoad lbl ptr_expr ty_cmm align = case someWasmTypeFromCmmType ty_cmm of
  SomeWasmType ty ->
    SomeWasmExpr ty <$> lower_CmmLoad_Typed lbl ptr_expr ty ty_cmm align

-- | Lower a 'CmmExpr'.
lower_CmmExpr :: CLabel -> CmmExpr -> WasmCodeGenM w (SomeWasmExpr w)
lower_CmmExpr lbl expr = case expr of
  CmmLit lit -> lower_CmmLit lit
  CmmLoad ptr_expr ty_cmm align -> lower_CmmLoad lbl ptr_expr ty_cmm align
  CmmReg reg -> lower_CmmReg lbl reg
  CmmRegOff reg o -> lower_CmmRegOff lbl reg o
  CmmMachOp op xs -> lower_CmmMachOp lbl op xs
  _ -> panic "lower_CmmExpr: unreachable"

-- | Lower a 'CmmExpr', passing in the expected wasm representation
-- type.
lower_CmmExpr_Typed ::
  CLabel ->
  WasmTypeTag t ->
  CmmExpr ->
  WasmCodeGenM
    w
    (WasmExpr w t)
lower_CmmExpr_Typed lbl ty expr = do
  SomeWasmExpr ty' r <- lower_CmmExpr lbl expr
  if
      | Just Refl <- ty' `testEquality` ty -> pure r
      | otherwise -> panic "lower_CmmExpr_Typed: unreachable"

-- | Lower a 'CmmExpr' as a pointer, returning the pair of base
-- pointer and non-negative offset.
lower_CmmExpr_Ptr :: CLabel -> CmmExpr -> WasmCodeGenM w (WasmExpr w w, Int)
lower_CmmExpr_Ptr lbl ptr = do
  ty_word <- wasmWordTypeM
  case ptr of
    CmmLit (CmmLabelOff lbl o)
      | o >= 0 -> do
          instrs <-
            lower_CmmExpr_Typed
              lbl
              ty_word
              (CmmLit $ CmmLabel lbl)
          pure (instrs, o)
    CmmMachOp (MO_Add _) [base, CmmLit (CmmInt o _)]
      | o >= 0 -> do
          instrs <- lower_CmmExpr_Typed lbl ty_word base
          pure (instrs, fromInteger o)
    _ -> do
      instrs <- lower_CmmExpr_Typed lbl ty_word ptr
      pure (instrs, 0)

-- | Push a series of values onto the wasm value stack, returning the
-- result stack type.
type family
  WasmPushes (ts :: [WasmType]) (pre :: [WasmType]) ::
    [WasmType]
  where
  WasmPushes '[] pre = pre
  WasmPushes (t : ts) pre = WasmPushes ts (t : pre)

-- | Push the arguments onto the wasm value stack before a ccall.
data SomeWasmPreCCall w where
  SomeWasmPreCCall ::
    TypeList ts ->
    (forall pre. WasmInstr w pre (WasmPushes ts pre)) ->
    SomeWasmPreCCall w

-- | Pop the results into locals after a ccall.
data SomeWasmPostCCall w where
  SomeWasmPostCCall ::
    TypeList ts ->
    (forall post. WasmInstr w (WasmPushes ts post) post) ->
    SomeWasmPostCCall w

-- | Lower an unary homogeneous 'CallishMachOp' to a ccall.
lower_CMO_Un_Homo ::
  CLabel ->
  SymName ->
  [CmmFormal] ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_CMO_Un_Homo lbl op [reg] [x] = do
  (ri, SomeWasmType ty) <- onCmmLocalReg reg
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
  let ty_cmm = localRegType reg
  onFuncSym op [ty_cmm] [ty_cmm]
  pure $
    WasmStatements $
      x_instr `WasmConcat` WasmCCall op `WasmConcat` WasmLocalSet ty ri
lower_CMO_Un_Homo _ _ _ _ = panic "lower_CMO_Un_Homo: unreachable"

-- | Lower a binary homogeneous 'CallishMachOp' to a ccall.
lower_CMO_Bin_Homo ::
  CLabel ->
  SymName ->
  [CmmFormal] ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_CMO_Bin_Homo lbl op [reg] [x, y] = do
  (ri, SomeWasmType ty) <- onCmmLocalReg reg
  WasmExpr x_instr <- lower_CmmExpr_Typed lbl ty x
  WasmExpr y_instr <- lower_CmmExpr_Typed lbl ty y
  let ty_cmm = localRegType reg
  onFuncSym op [ty_cmm, ty_cmm] [ty_cmm]
  pure $
    WasmStatements $
      x_instr
        `WasmConcat` y_instr
        `WasmConcat` WasmCCall op
        `WasmConcat` WasmLocalSet ty ri
lower_CMO_Bin_Homo _ _ _ _ = panic "lower_CMO_Bin_Homo: unreachable"

-- | Lower a 'MO_UF_Conv' operation.
lower_MO_UF_Conv ::
  CLabel ->
  Width ->
  [CmmFormal] ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_MO_UF_Conv lbl W32 [reg] [x] = do
  ri <- onCmmLocalReg_Typed TagF32 reg
  SomeWasmExpr ty0 (WasmExpr x_instr) <- lower_CmmExpr lbl x
  pure $
    WasmStatements $
      x_instr
        `WasmConcat` WasmConvert Unsigned ty0 TagF32
        `WasmConcat` WasmLocalSet TagF32 ri
lower_MO_UF_Conv lbl W64 [reg] [x] = do
  ri <- onCmmLocalReg_Typed TagF64 reg
  SomeWasmExpr ty0 (WasmExpr x_instr) <- lower_CmmExpr lbl x
  pure $
    WasmStatements $
      x_instr
        `WasmConcat` WasmConvert Unsigned ty0 TagF64
        `WasmConcat` WasmLocalSet TagF64 ri
lower_MO_UF_Conv _ _ _ _ = panic "lower_MO_UF_Conv: unreachable"

-- | Lower a 'MO_Cmpxchg' operation to inline assembly. Currently we
-- target wasm without atomics and threads, so it's just lowered to
-- regular memory loads and stores.
lower_MO_Cmpxchg ::
  CLabel ->
  Width ->
  [CmmFormal] ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_MO_Cmpxchg lbl w0 [reg] [ptr, expected, new] =
  case someWasmTypeFromCmmType ty_cmm of
    SomeWasmType ty -> do
      reg_i <- onCmmLocalReg_Typed ty reg
      let narrowing = wasmMemoryNarrowing ty ty_cmm
      (WasmExpr ptr_instr, o) <- lower_CmmExpr_Ptr lbl ptr
      WasmExpr expected_instr <- lower_CmmExpr_Typed lbl ty expected
      WasmExpr new_instr <- lower_CmmExpr_Typed lbl ty new
      pure $
        WasmStatements $
          ptr_instr
            `WasmConcat` WasmLoad ty narrowing Unsigned o NaturallyAligned
            `WasmConcat` WasmLocalTee ty reg_i
            `WasmConcat` expected_instr
            `WasmConcat` WasmEq ty
            `WasmConcat` WasmCond
              ( ptr_instr
                  `WasmConcat` new_instr
                  `WasmConcat` WasmStore ty narrowing o NaturallyAligned
              )
  where
    ty_cmm = cmmBits w0
lower_MO_Cmpxchg _ _ _ _ = panic "lower_MO_Cmpxchg: unreachable"

-- | Lower a 'CallishMachOp'.
lower_CallishMachOp ::
  CLabel ->
  CallishMachOp ->
  [CmmFormal] ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_CallishMachOp lbl MO_F64_Pwr rs xs = lower_CMO_Bin_Homo lbl "pow" rs xs
lower_CallishMachOp lbl MO_F64_Sin rs xs = lower_CMO_Un_Homo lbl "sin" rs xs
lower_CallishMachOp lbl MO_F64_Cos rs xs = lower_CMO_Un_Homo lbl "cos" rs xs
lower_CallishMachOp lbl MO_F64_Tan rs xs = lower_CMO_Un_Homo lbl "tan" rs xs
lower_CallishMachOp lbl MO_F64_Sinh rs xs = lower_CMO_Un_Homo lbl "sinh" rs xs
lower_CallishMachOp lbl MO_F64_Cosh rs xs = lower_CMO_Un_Homo lbl "cosh" rs xs
lower_CallishMachOp lbl MO_F64_Tanh rs xs = lower_CMO_Un_Homo lbl "tanh" rs xs
lower_CallishMachOp lbl MO_F64_Asin rs xs = lower_CMO_Un_Homo lbl "asin" rs xs
lower_CallishMachOp lbl MO_F64_Acos rs xs = lower_CMO_Un_Homo lbl "acos" rs xs
lower_CallishMachOp lbl MO_F64_Atan rs xs = lower_CMO_Un_Homo lbl "atan" rs xs
lower_CallishMachOp lbl MO_F64_Asinh rs xs = lower_CMO_Un_Homo lbl "asinh" rs xs
lower_CallishMachOp lbl MO_F64_Acosh rs xs = lower_CMO_Un_Homo lbl "acosh" rs xs
lower_CallishMachOp lbl MO_F64_Atanh rs xs = lower_CMO_Un_Homo lbl "atanh" rs xs
lower_CallishMachOp lbl MO_F64_Log rs xs = lower_CMO_Un_Homo lbl "log" rs xs
lower_CallishMachOp lbl MO_F64_Log1P rs xs = lower_CMO_Un_Homo lbl "log1p" rs xs
lower_CallishMachOp lbl MO_F64_Exp rs xs = lower_CMO_Un_Homo lbl "exp" rs xs
lower_CallishMachOp lbl MO_F64_ExpM1 rs xs = lower_CMO_Un_Homo lbl "expm1" rs xs
lower_CallishMachOp lbl MO_F64_Fabs rs xs = lower_CMO_Un_Homo lbl "fabs" rs xs
lower_CallishMachOp lbl MO_F64_Sqrt rs xs = lower_CMO_Un_Homo lbl "sqrt" rs xs
lower_CallishMachOp lbl MO_F32_Pwr rs xs = lower_CMO_Bin_Homo lbl "powf" rs xs
lower_CallishMachOp lbl MO_F32_Sin rs xs = lower_CMO_Un_Homo lbl "sinf" rs xs
lower_CallishMachOp lbl MO_F32_Cos rs xs = lower_CMO_Un_Homo lbl "cosf" rs xs
lower_CallishMachOp lbl MO_F32_Tan rs xs = lower_CMO_Un_Homo lbl "tanf" rs xs
lower_CallishMachOp lbl MO_F32_Sinh rs xs = lower_CMO_Un_Homo lbl "sinhf" rs xs
lower_CallishMachOp lbl MO_F32_Cosh rs xs = lower_CMO_Un_Homo lbl "coshf" rs xs
lower_CallishMachOp lbl MO_F32_Tanh rs xs = lower_CMO_Un_Homo lbl "tanhf" rs xs
lower_CallishMachOp lbl MO_F32_Asin rs xs = lower_CMO_Un_Homo lbl "asinf" rs xs
lower_CallishMachOp lbl MO_F32_Acos rs xs = lower_CMO_Un_Homo lbl "acosf" rs xs
lower_CallishMachOp lbl MO_F32_Atan rs xs = lower_CMO_Un_Homo lbl "atanf" rs xs
lower_CallishMachOp lbl MO_F32_Asinh rs xs =
  lower_CMO_Un_Homo lbl "asinhf" rs xs
lower_CallishMachOp lbl MO_F32_Acosh rs xs =
  lower_CMO_Un_Homo lbl "acoshf" rs xs
lower_CallishMachOp lbl MO_F32_Atanh rs xs =
  lower_CMO_Un_Homo lbl "atanhf" rs xs
lower_CallishMachOp lbl MO_F32_Log rs xs = lower_CMO_Un_Homo lbl "logf" rs xs
lower_CallishMachOp lbl MO_F32_Log1P rs xs =
  lower_CMO_Un_Homo lbl "log1pf" rs xs
lower_CallishMachOp lbl MO_F32_Exp rs xs = lower_CMO_Un_Homo lbl "expf" rs xs
lower_CallishMachOp lbl MO_F32_ExpM1 rs xs =
  lower_CMO_Un_Homo lbl "expm1f" rs xs
lower_CallishMachOp lbl MO_F32_Fabs rs xs = lower_CMO_Un_Homo lbl "fabsf" rs xs
lower_CallishMachOp lbl MO_F32_Sqrt rs xs = lower_CMO_Un_Homo lbl "sqrtf" rs xs
lower_CallishMachOp lbl (MO_UF_Conv w0) rs xs = lower_MO_UF_Conv lbl w0 rs xs
lower_CallishMachOp _ MO_ReadBarrier _ _ = pure $ WasmStatements WasmNop
lower_CallishMachOp _ MO_WriteBarrier _ _ = pure $ WasmStatements WasmNop
lower_CallishMachOp _ MO_Touch _ _ = pure $ WasmStatements WasmNop
lower_CallishMachOp _ (MO_Prefetch_Data {}) _ _ = pure $ WasmStatements WasmNop
lower_CallishMachOp lbl (MO_Memcpy {}) [] xs = do
  ty_word_cmm <- wasmWordCmmTypeM
  lower_CmmUnsafeForeignCall_Drop lbl "memcpy" ty_word_cmm xs
lower_CallishMachOp lbl (MO_Memset {}) [] xs = do
  ty_word_cmm <- wasmWordCmmTypeM
  lower_CmmUnsafeForeignCall_Drop lbl "memset" ty_word_cmm xs
lower_CallishMachOp lbl (MO_Memmove {}) [] xs = do
  ty_word_cmm <- wasmWordCmmTypeM
  lower_CmmUnsafeForeignCall_Drop lbl "memmove" ty_word_cmm xs
lower_CallishMachOp lbl (MO_Memcmp {}) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left "memcmp")
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_PopCnt w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_popcnt" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_Pdep w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_pdep" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_Pext w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_pext" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_Clz w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_clz" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_Ctz w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_ctz" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_BSwap w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_bswap" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_BRev w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_bitrev" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_AtomicRMW w0 op) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    ( Left $
        fromString $
          ( case op of
              AMO_Add -> "hs_atomic_add"
              AMO_Sub -> "hs_atomic_sub"
              AMO_And -> "hs_atomic_and"
              AMO_Nand -> "hs_atomic_nand"
              AMO_Or -> "hs_atomic_or"
              AMO_Xor -> "hs_atomic_xor"
          )
            <> show (widthInBits w0)
    )
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl (MO_AtomicRead w0 _) [reg] [ptr] = do
  SomeWasmExpr ty (WasmExpr ret_instr) <-
    lower_CmmLoad
      lbl
      ptr
      (cmmBits w0)
      NaturallyAligned
  ri <- onCmmLocalReg_Typed ty reg
  pure $ WasmStatements $ ret_instr `WasmConcat` WasmLocalSet ty ri
lower_CallishMachOp lbl (MO_AtomicWrite _ _) [] [ptr, val] =
  lower_CmmStore lbl ptr val NaturallyAligned
lower_CallishMachOp lbl (MO_Cmpxchg w0) rs xs = lower_MO_Cmpxchg lbl w0 rs xs
lower_CallishMachOp lbl (MO_Xchg w0) rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left $ fromString $ "hs_xchg" <> show (widthInBits w0))
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl MO_SuspendThread rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left "suspendThread")
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp lbl MO_ResumeThread rs xs =
  lower_CmmUnsafeForeignCall
    lbl
    (Left "resumeThread")
    Nothing
    CmmMayReturn
    rs
    xs
lower_CallishMachOp _ _ _ _ = panic "lower_CallishMachOp: unreachable"

-- | Lower a ccall, but drop the result by assigning it to an unused
-- local. This is only used for lowering 'MO_Memcpy' and such, where
-- the libc functions do have a return value, but the corresponding
-- 'CallishMachOp' does not expect one.
lower_CmmUnsafeForeignCall_Drop ::
  CLabel ->
  SymName ->
  CmmType ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_CmmUnsafeForeignCall_Drop lbl sym_callee ret_cmm_ty arg_exprs = do
  ret_uniq <- getUniqueM
  let ret_local = LocalReg ret_uniq ret_cmm_ty
  lower_CmmUnsafeForeignCall
    lbl
    (Left sym_callee)
    Nothing
    CmmMayReturn
    [ret_local]
    arg_exprs

-- | Lower a 'CmmUnsafeForeignCall'. The target is 'Either' a symbol,
-- which translates to a direct @call@, or an expression, which
-- translates to a @call_indirect@. The callee function signature is
-- inferred from the passed in arguments here.
lower_CmmUnsafeForeignCall ::
  CLabel ->
  (Either SymName CmmExpr) ->
  Maybe
    ([ForeignHint], [ForeignHint]) ->
  CmmReturnInfo ->
  [CmmFormal] ->
  [CmmActual] ->
  WasmCodeGenM w (WasmStatements w)
lower_CmmUnsafeForeignCall lbl target mb_hints ret_info ret_locals arg_exprs = do
  platform <- wasmPlatformM
  SomeWasmPreCCall arg_tys args_instr <-
    foldrM
      ( \(arg_expr, arg_hint) (SomeWasmPreCCall acc_tys acc_instr) -> do
          SomeWasmExpr arg_ty arg_wasm_expr <- lower_CmmExpr lbl arg_expr
          let WasmExpr arg_instr = case arg_hint of
                SignedHint ->
                  extendSubword
                    (cmmExprWidth platform arg_expr)
                    arg_ty
                    arg_wasm_expr
                _ -> arg_wasm_expr
          pure $
            SomeWasmPreCCall (arg_ty `TypeListCons` acc_tys) $
              arg_instr `WasmConcat` acc_instr
      )
      (SomeWasmPreCCall TypeListNil WasmNop)
      arg_exprs_hints
  SomeWasmPostCCall ret_tys ret_instr <-
    foldrM
      ( \(reg, ret_hint) (SomeWasmPostCCall acc_tys acc_instr) -> do
          (reg_i, SomeWasmType reg_ty) <- onCmmLocalReg reg
          pure $
            SomeWasmPostCCall (reg_ty `TypeListCons` acc_tys) $
              case (# ret_hint, cmmRegWidth $ CmmLocal reg #) of
                (# SignedHint, W8 #) ->
                  acc_instr
                    `WasmConcat` WasmConst reg_ty 0xFF
                    `WasmConcat` WasmAnd reg_ty
                    `WasmConcat` WasmLocalSet reg_ty reg_i
                (# SignedHint, W16 #) ->
                  acc_instr
                    `WasmConcat` WasmConst reg_ty 0xFFFF
                    `WasmConcat` WasmAnd reg_ty
                    `WasmConcat` WasmLocalSet reg_ty reg_i
                _ -> acc_instr `WasmConcat` WasmLocalSet reg_ty reg_i
      )
      (SomeWasmPostCCall TypeListNil WasmNop)
      ret_locals_hints
  case target of
    Left sym_callee -> do
      platform <- wasmPlatformM
      let arg_cmm_tys = map (cmmExprType platform) arg_exprs
          ret_cmm_tys = map localRegType ret_locals
      onFuncSym sym_callee arg_cmm_tys ret_cmm_tys
      pure $
        WasmStatements $
          args_instr
            `WasmConcat` WasmCCall sym_callee
            `WasmConcat` ( case ret_info of
                             CmmMayReturn -> ret_instr
                             CmmNeverReturns -> WasmUnreachable
                         )
    Right fptr_callee -> do
      (WasmExpr instr_callee, _) <- lower_CmmExpr_Ptr lbl fptr_callee
      pure $
        WasmStatements $
          args_instr
            `WasmConcat` instr_callee
            `WasmConcat` WasmCCallIndirect arg_tys ret_tys
            `WasmConcat` ( case ret_info of
                             CmmMayReturn -> ret_instr
                             CmmNeverReturns -> WasmUnreachable
                         )
  where
    (# arg_exprs_hints, ret_locals_hints #) = case mb_hints of
      Just (arg_hints, ret_hints) ->
        (# zip arg_exprs arg_hints, zip ret_locals ret_hints #)
      _ -> (# map (,NoHint) arg_exprs, map (,NoHint) ret_locals #)

-- | Lower a 'CmmStore'.
lower_CmmStore ::
  CLabel ->
  CmmExpr ->
  CmmExpr ->
  AlignmentSpec ->
  WasmCodeGenM
    w
    (WasmStatements w)
lower_CmmStore lbl ptr val align = do
  platform <- wasmPlatformM
  (WasmExpr ptr_instr, o) <- lower_CmmExpr_Ptr lbl ptr
  let ty_cmm = cmmExprType platform val
  SomeWasmExpr ty (WasmExpr val_instr) <- lower_CmmExpr lbl val
  pure $
    WasmStatements $
      ptr_instr
        `WasmConcat` val_instr
        `WasmConcat` WasmStore ty (wasmMemoryNarrowing ty ty_cmm) o align

-- | Lower a single Cmm action.
lower_CmmAction :: CLabel -> CmmNode O O -> WasmCodeGenM w (WasmStatements w)
lower_CmmAction lbl act = do
  ty_word <- wasmWordTypeM
  platform <- wasmPlatformM
  case act of
    CmmComment {} -> pure $ WasmStatements WasmNop
    CmmTick {} -> pure $ WasmStatements WasmNop
    CmmUnwind {} -> pure $ WasmStatements WasmNop
    CmmAssign (CmmLocal reg) e -> do
      (i, SomeWasmType ty_reg) <- onCmmLocalReg reg
      WasmExpr instrs <- lower_CmmExpr_Typed lbl ty_reg e
      pure $ WasmStatements $ instrs `WasmConcat` WasmLocalSet ty_reg i
    CmmAssign (CmmGlobal (GlobalRegUse reg _)) e
      | BaseReg <- reg -> pure $ WasmStatements WasmNop
      | Just (sym_global, SomeWasmType ty_reg) <-
          globalInfoFromCmmGlobalReg ty_word reg -> do
          WasmExpr instrs <- lower_CmmExpr_Typed lbl ty_reg e
          pure $
            WasmStatements $
              instrs `WasmConcat` WasmGlobalSet ty_reg sym_global
      | otherwise -> do
          (WasmExpr ptr_instr, o) <-
            lower_CmmExpr_Ptr lbl $ get_GlobalReg_addr platform reg
          SomeWasmExpr ty_e (WasmExpr instrs) <- lower_CmmExpr lbl e
          pure $
            WasmStatements $
              ptr_instr
                `WasmConcat` instrs
                `WasmConcat` WasmStore ty_e Nothing o NaturallyAligned
    CmmStore ptr val align -> lower_CmmStore lbl ptr val align
    CmmUnsafeForeignCall
      ( ForeignTarget
          (CmmLit (CmmLabel lbl_callee))
          (ForeignConvention conv arg_hints ret_hints ret_info)
        )
      ret_locals
      arg_exprs
        | conv `elem` [CCallConv, CApiConv] ->
            lower_CmmUnsafeForeignCall
              lbl
              (Left $ symNameFromCLabel lbl_callee)
              (Just (arg_hints, ret_hints))
              ret_info
              ret_locals
              arg_exprs
    CmmUnsafeForeignCall
      (ForeignTarget target_expr (ForeignConvention conv arg_hints ret_hints ret_info))
      ret_locals
      arg_exprs
        | conv `elem` [CCallConv, CApiConv] ->
            lower_CmmUnsafeForeignCall
              lbl
              (Right target_expr)
              (Just (arg_hints, ret_hints))
              ret_info
              ret_locals
              arg_exprs
    CmmUnsafeForeignCall (PrimTarget op) ret_locals arg_exprs ->
      lower_CallishMachOp lbl op ret_locals arg_exprs
    _ -> panic "lower_CmmAction: unreachable"

-- | Lower a block of Cmm actions.
lower_CmmActions ::
  CLabel ->
  Label ->
  Block CmmNode O O ->
  WasmCodeGenM
    w
    (WasmStatements w)
lower_CmmActions lbl _ blk =
  foldlM
    ( \(WasmStatements acc) act ->
        (\(WasmStatements stmts) -> WasmStatements $ acc `WasmConcat` stmts)
          <$> lower_CmmAction lbl act
    )
    (WasmStatements WasmNop)
    acts
  where
    acts = blockToList blk

-- | Lower a 'CmmGraph'.
lower_CmmGraph :: CLabel -> CmmGraph -> WasmCodeGenM w (FuncBody w)
lower_CmmGraph lbl g = do
  ty_word <- wasmWordTypeM
  platform <- wasmPlatformM
  us <- getUniqueSupplyM
  body <-
    structuredControl
      platform
      us
      (\_ -> lower_CmmExpr_Typed lbl ty_word)
      (lower_CmmActions lbl)
      g
  locals <- wasmStateM $ \s ->
    (#
      map snd $ detEltsUFM $ localRegs s,
      s {localRegs = emptyUFM, localRegsCount = 0}
    #)
  pure FuncBody {funcLocals = locals, funcBody = wasmControlCast $ body}

-- | Invoked once for each 'CLabel' which indexes a 'CmmData' or
-- 'CmmProc'.
onTopSym :: CLabel -> WasmCodeGenM w ()
onTopSym lbl = case sym_vis of
  SymDefault -> wasmModifyM $ \s ->
    s
      { defaultSyms =
          IS.insert
            (getKey $ getUnique sym)
            $ defaultSyms s
      }
  _ -> pure ()
  where
    sym = symNameFromCLabel lbl

    sym_vis = symVisibilityFromCLabel lbl

-- | Invoked for each function 'CLabel' with known type (e.g. a
-- 'CmmProc', or callee of 'CmmUnsafeForeignCall').
onFuncSym :: SymName -> [CmmType] -> [CmmType] -> WasmCodeGenM w ()
onFuncSym sym arg_tys ret_tys = wasmModifyM $
  \s@WasmCodeGenState {..} ->
    s
      { funcTypes =
          addToUniqMap
            funcTypes
            sym
            ( map someWasmTypeFromCmmType arg_tys,
              map someWasmTypeFromCmmType ret_tys
            )
      }

-- | Invoked for all other 'CLabel's along the way, e.g. in
-- 'CmmStatic's or 'CmmExpr's.
onAnySym :: CLabel -> WasmCodeGenM w ()
onAnySym lbl = case sym_kind of
  SymFunc -> do
    ty_word <- wasmWordTypeM
    wasmModifyM $ \s@WasmCodeGenState {..} ->
      s {funcTypes = addToUniqMap_C const funcTypes sym ([], [SomeWasmType ty_word])}
  _ -> pure ()
  where
    sym = symNameFromCLabel lbl

    sym_kind = symKindFromCLabel lbl

-- | Invoked for each 'LocalReg', returning its wasm local id and
-- representation type.
onCmmLocalReg :: LocalReg -> WasmCodeGenM w LocalInfo
onCmmLocalReg reg = wasmStateM $ \s@WasmCodeGenState {..} ->
  let reg_info =
        (localRegsCount, someWasmTypeFromCmmType $ localRegType reg)
   in case addToUFM_L (\_ i _ -> i) reg reg_info localRegs of
        (Just i, _) -> (# i, s #)
        (_, localRegs') ->
          (#
            reg_info,
            s
              { localRegs = localRegs',
                localRegsCount =
                  localRegsCount + 1
              }
          #)

-- | Invoked for each 'LocalReg' with expected representation type,
-- only returning its wasm local id.
onCmmLocalReg_Typed :: WasmTypeTag t -> LocalReg -> WasmCodeGenM w Int
onCmmLocalReg_Typed ty reg = do
  (i, SomeWasmType ty') <- onCmmLocalReg reg
  if
      | Just Refl <- ty' `testEquality` ty -> pure i
      | otherwise -> panic "onCmmLocalReg_Typed: unreachable"

-- | Invoked for dtors. We don't bother to implement dtors yet;
-- there's no native @.fini_array@ support for wasm, and the way
-- @clang@ handles dtors is generating a ctor that calls @atexit()@
-- for dtors. Which makes some sense, but we don't need to do the same
-- thing yet.
onFini :: [SymName] -> WasmCodeGenM w ()
onFini syms = do
  let n_finis = length syms
  when (n_finis /= 0) $ panic "dtors unsupported by wasm32 NCG"

-- | Invoked for ctors and dtors.
onCmmInitFini :: InitOrFini -> [CLabel] -> WasmCodeGenM w ()
onCmmInitFini iof lbls = do
  for_ lbls $ \lbl -> onFuncSym (symNameFromCLabel lbl) [] []
  case iof of
    IsInitArray -> wasmModifyM $ \s -> s {ctors = syms <> ctors s}
    IsFiniArray -> onFini syms
  where
    syms = map symNameFromCLabel lbls

-- | Invoked for each data section.
onCmmData :: CLabel -> Section -> [CmmStatic] -> WasmCodeGenM w ()
onCmmData lbl s statics = do
  ty_word <- wasmWordTypeM
  onTopSym lbl
  cs <- for statics lower_CmmStatic
  let sym = symNameFromCLabel lbl
      sec =
        DataSection
          { dataSectionKind =
              dataSectionKindFromCmmSection s,
            dataSectionAlignment =
              alignmentFromCmmSection ty_word cs,
            dataSectionContents =
              case cs of
                [DataASCII buf] -> [DataASCII $ buf `BS.snoc` 0]
                [DataIncBin p l] -> [DataIncBin p l, DataI8 0]
                _ -> cs
          }
  wasmModifyM $ \s ->
    s
      { dataSections =
          addToUniqMap (dataSections s) sym sec
      }

-- | Invoked for each 'CmmProc'.
onCmmProc :: CLabel -> CmmGraph -> WasmCodeGenM w ()
onCmmProc lbl g = do
  ty_word <- wasmWordCmmTypeM
  onTopSym lbl
  onFuncSym sym [] [ty_word]
  body <- lower_CmmGraph lbl g
  wasmModifyM $ \s -> s {funcBodies = addToUniqMap (funcBodies s) sym body}
  where
    sym = symNameFromCLabel lbl

-- | Invoked for each 'RawCmmDecl'.
onCmmDecl :: RawCmmDecl -> WasmCodeGenM w ()
onCmmDecl decl
  | Just (iof, lbls) <- isInitOrFiniArray decl = onCmmInitFini iof lbls
onCmmDecl (CmmData s (CmmStaticsRaw lbl statics)) = onCmmData lbl s statics
onCmmDecl (CmmProc _ lbl _ g) = onCmmProc lbl g

-- | Invoked for each 'RawCmmGroup'.
onCmmGroup :: RawCmmGroup -> WasmCodeGenM w ()
onCmmGroup cmms = wasmStateM $ \s0 ->
  (# (), foldl' (\s cmm -> wasmExecM (onCmmDecl cmm) s) s0 cmms #)