summaryrefslogtreecommitdiff
path: root/src/Compile.hs
blob: 5c9d1a24a09343f15395326c7bfa60729446f892 (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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
module Compile (compile) where

import Control.Monad (forM_, when, replicateM)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Writer.CPS
import Data.Bifunctor (first)
import Data.Foldable (toList)
import Data.Functor.Const
import qualified Data.Functor.Product as Product
import Data.Functor.Product (Product)
import Data.List (foldl1', intersperse, intercalate)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import qualified Data.Set as Set
import Data.Set (Set)
import Data.Some
import qualified Data.Vector as V
import Foreign
import System.IO (hPutStrLn, stderr)

import Prelude hiding ((^))
import qualified Prelude

import Array
import AST
import AST.Pretty (ppTy)
import Compile.Exec
import Data
import Interpreter.Rep


{-
:m *Example Compile AST.UnMonoid
:seti -XOverloadedLabels -XGADTs
let array = arrayGenerate (ShNil `ShCons` 10) (\(IxNil `IxCons` i) -> fromIntegral i :: Double) in (($ SCons (Value array) SNil) =<<) $ compile knownEnv $ fromNamed $ lambda @(TArr N1 (TScal TF64)) #x $ body $ #x ! pair nil (round_ (#x ! pair nil 3))
(($ SNil) =<<) $ compile knownEnv $ fromNamed $ body $ build2 5 3 (#i :-> #j :-> 10 * #i + #j)
-}


-- In shape and index arrays, the innermost dimension is on the right (last index).


debug :: Bool
debug = toEnum 0

compile :: SList STy env -> Ex env t
        -> IO (SList Value env -> IO (Rep t))
compile = \env expr -> do
  let source = compileToString env expr
  when debug $ hPutStrLn stderr $ "Generated C source: <<<\n\x1B[2m" ++ source ++ "\x1B[0m>>>"
  lib <- buildKernel source ["kernel"]

  let arg_metrics = reverse (unSList metricsSTy env)
      (arg_offsets, result_offset) = computeStructOffsets arg_metrics
      result_type = typeOf expr
      result_size = sizeofSTy result_type

  return $ \val -> do
    allocaBytes (result_offset + result_size) $ \ptr -> do
      let args = zip (reverse (unSList Some (slistZip env val))) arg_offsets
      serialiseArguments args ptr $ do
        callKernelFun "kernel" lib ptr
        deserialise result_type ptr result_offset
  where
    serialiseArguments :: [(Some (Product STy Value), Int)] -> Ptr () -> IO r -> IO r
    serialiseArguments ((Some (Product.Pair t (Value arg)), off) : args) ptr k =
      serialise t arg ptr off $
        serialiseArguments args ptr k
    serialiseArguments _ _ k = k


data StructDecl = StructDecl
    String  -- ^ name
    String  -- ^ contents
    String  -- ^ comment
  deriving (Show)

data Stmt
  = SVarDecl Bool String String CExpr  -- ^ const, type, variable name, right-hand side
  | SVarDeclUninit String String  -- ^ type, variable name (no initialiser)
  | SAsg String CExpr  -- ^ variable name, right-hand side
  | SBlock (Bag Stmt)
  | SIf CExpr (Bag Stmt) (Bag Stmt)
  | SLoop String String CExpr CExpr (Bag Stmt)  -- ^ for (<type> <name> = <expr>; name < <expr>; name++) {<stmts>}
  | SVerbatim String  -- ^ no implicit ';', just printed as-is
  deriving (Show)

data CExpr
  = CELit String  -- ^ inserted as-is, assumed no parentheses needed
  | CEStruct String [(String, CExpr)]  -- ^ struct construction literal: `(name){.field=expr}`
  | CEProj CExpr String  -- ^ field projection: expr.field
  | CEPtrProj CExpr String  -- ^ field projection through pointer: expr->field
  | CEAddrOf CExpr  -- ^ &expr
  | CEIndex CExpr CExpr  -- ^ expr[expr]
  | CECall String [CExpr]  -- ^ function(arg1, ..., argn)
  | CEBinop CExpr String CExpr  -- ^ expr + expr
  | CEIf CExpr CExpr CExpr  -- ^ expr ? expr : expr
  | CECast String CExpr  -- ^ (<type)<expr>
  deriving (Show)

printStructDecl :: StructDecl -> ShowS
printStructDecl (StructDecl name contents comment) =
  showString "typedef struct { " . showString contents . showString " } " . showString name
  . showString ";" . (if null comment then id else showString ("  // " ++ comment))

printStmt :: Int -> Stmt -> ShowS
printStmt indent = \case
  SVarDecl cnst typ name rhs -> showString (typ ++ " " ++ (if cnst then "const " else "") ++ name ++ " = ") . printCExpr 0 rhs . showString ";"
  SVarDeclUninit typ name -> showString (typ ++ " " ++ name ++ ";")
  SAsg name rhs -> showString (name ++ " = ") . printCExpr 0 rhs . showString ";"
  SBlock stmts
    | null stmts -> showString "{}"
    | otherwise ->
        showString "{"
        . compose [showString ("\n" ++ replicate (2*indent+2) ' ') . printStmt (indent+1) stmt | stmt <- toList stmts]
        . showString ("\n" ++ replicate (2*indent) ' ' ++ "}")
  SIf cond b1 b2 ->
    showString "if (" . printCExpr 0 cond . showString ") "
    . printStmt indent (SBlock b1)
    . (if null b2 then id else showString " else " . printStmt indent (SBlock b2))
  SLoop typ name e1 e2 stmts ->
    showString ("for (" ++ typ ++ " " ++ name ++ " = ")
    . printCExpr 0 e1 . showString ("; " ++ name ++ " < ") . printCExpr 6 e2
    . showString ("; " ++ name ++ "++) ")
    . printStmt indent (SBlock stmts)
  SVerbatim s -> showString s

-- d values:
-- * 0: top level
-- * 1: in 1st or 2nd component of a ternary operator (technically same as top level, but readability)
-- * 2-...: various operators (see precTable)
-- * 80: address-of operator (&)
-- * 98: inside unknown operator
-- * 99: left of a field projection
-- Unlisted operators are conservatively written with full parentheses.
printCExpr :: Int -> CExpr -> ShowS
printCExpr d = \case
  CELit s -> showString s
  CEStruct name pairs ->
    showParen (d >= 99) $
      showString ("(" ++ name ++ "){")
      . compose (intersperse (showString ", ") [showString ("." ++ n ++ " = ") . printCExpr 0 e
                                               | (n, e) <- pairs])
      . showString "}"
  CEProj e name -> printCExpr 99 e . showString ("." ++ name)
  CEPtrProj e name -> printCExpr 99 e . showString ("->" ++ name)
  CEAddrOf e -> showParen (d > 80) $ showString "&" . printCExpr 80 e
  CEIndex e1 e2 -> printCExpr 99 e1 . showString "[" . printCExpr 0 e2 . showString "]"
  CECall n es ->
    showString (n ++ "(") . compose (intersperse (showString ", ") (map (printCExpr 0) es)) . showString ")"
  CEBinop e1 n e2 ->
    let mprec = Map.lookup n precTable
        p = maybe (-1) fst mprec  -- precedence of this operator
        (d1, d2) = maybe (98, 98) snd mprec  -- precedences for the arguments
    in showParen (d > p) $
         printCExpr d1 e1 . showString (" " ++ n ++ " ") . printCExpr d2 e2
  CEIf e1 e2 e3 ->
    showParen (d > 0) $
      printCExpr 1 e1 . showString " ? " . printCExpr 1 e2 . showString " : " . printCExpr 0 e3
  CECast typ e ->
    showParen (d > 98) $ showString ("(" ++ typ ++ ")") . printCExpr 98 e
  where
    precTable = Map.fromList
      [("||", (2, (2, 2)))
      ,("&&", (3, (3, 3)))
      ,("==", (4, (5, 5)))
      ,("!=", (4, (5, 5)))
      ,("<", (5, (6, 6)))  -- Note: this precedence is used in the printing of SLoop
      ,(">", (5, (6, 6)))
      ,("<=", (5, (6, 6)))
      ,(">=", (5, (6, 6)))
      ,("+", (6, (6, 7)))
      ,("-", (6, (6, 7)))
      ,("*", (7, (7, 8)))
      ,("/", (7, (7, 8)))
      ,("%", (7, (7, 8)))]

repTy :: Ty -> String
repTy (TScal st) = case st of
  TI32 -> "int32_t"
  TI64 -> "int64_t"
  TF32 -> "float"
  TF64 -> "double"
  TBool -> "uint8_t"
repTy t = genStructName t

repSTy :: STy t -> String
repSTy = repTy . unSTy

genStructName :: Ty -> String
genStructName = \t -> "ty_" ++ gen t where
  -- all tags start with a letter, so the array mangling is unambiguous.
  gen :: Ty -> String
  gen TNil = "n"
  gen (TPair a b) = 'P' : gen a ++ gen b
  gen (TEither a b) = 'E' : gen a ++ gen b
  gen (TMaybe t) = 'M' : gen t
  gen (TArr n t) = "A" ++ show (fromNat n) ++ gen t
  gen (TScal st) = case st of
    TI32 -> "i"
    TI64 -> "j"
    TF32 -> "f"
    TF64 -> "d"
    TBool -> "b"
  gen (TAccum t) = 'C' : gen t

-- | This function generates the actual struct declarations for each of the
-- types in our language. It thus implicitly "documents" the layout of the
-- types in the C translation.
genStruct :: String -> Ty -> [StructDecl]
genStruct name topty = case topty of
  TNil ->
    [StructDecl name "" com]
  TPair a b ->
    [StructDecl name (repTy a ++ " a; " ++ repTy b ++ " b;") com]
  TEither a b ->  -- 0 -> l, 1 -> r
    [StructDecl name ("uint8_t tag; union { " ++ repTy a ++ " l; " ++ repTy b ++ " r; };") com]
  TMaybe t ->  -- 0 -> nothing, 1 -> just
    [StructDecl name ("uint8_t tag; " ++ repTy t ++ " j;") com]
  TArr n t ->
    -- The buffer is trailed by a VLA for the actual array data.
    [StructDecl (name ++ "_buf") ("size_t sh[" ++ show (fromNat n) ++ "]; size_t refc; " ++ repTy t ++ " xs[];") ""
    ,StructDecl name (name ++ "_buf *buf;") com]
  TScal _ ->
    []
  TAccum t ->
    [StructDecl name (repTy t ++ " ac;") com]
  where
    com = ppTy 0 topty

-- State: already-generated (skippable) struct names
-- Writer: the structs in declaration order
genStructs :: Ty -> WriterT (Bag StructDecl) (State (Set String)) ()
genStructs ty = do
  let name = genStructName ty
  seen <- lift $ gets (name `Set.member`)

  if seen
    then pure ()
    else do
      -- already mark this struct as generated now, so we don't generate it
      -- twice (unnecessary because no recursive types, but y'know)
      lift $ modify (Set.insert name)

      case ty of
        TNil -> pure ()
        TPair a b -> genStructs a >> genStructs b
        TEither a b -> genStructs a >> genStructs b
        TMaybe t -> genStructs t
        TArr _ t -> genStructs t
        TScal _ -> pure ()
        TAccum t -> genStructs t

      tell (BList (genStruct name ty))

genAllStructs :: Foldable t => t Ty -> [StructDecl]
genAllStructs tys = toList $ evalState (execWriterT (mapM_ genStructs tys)) mempty

data CompState = CompState
  { csStructs :: Set Ty
  , csTopLevelDecls :: Bag String
  , csStmts :: Bag Stmt
  , csNextId :: Int }
  deriving (Show)

type CompM a = State CompState a

genId :: CompM Int
genId = state $ \s -> (csNextId s, s { csNextId = csNextId s + 1 })

genName' :: String -> CompM String
genName' prefix = (prefix ++) . show <$> genId

genName :: CompM String
genName = genName' "x"

emit :: Stmt -> CompM ()
emit stmt = modify $ \s -> s { csStmts = csStmts s <> pure stmt }

scope :: CompM a -> CompM (a, [Stmt])
scope m = do
  stmts <- state $ \s -> (csStmts s, s { csStmts = mempty })
  res <- m
  innerStmts <- state $ \s -> (csStmts s, s { csStmts = stmts })
  return (res, toList innerStmts)

emitStruct :: STy t -> CompM String
emitStruct ty = do
  let ty' = unSTy ty
  modify $ \s -> s { csStructs = Set.insert ty' (csStructs s) }
  return (genStructName ty')

emitTLD :: String -> CompM ()
emitTLD decl = modify $ \s -> s { csTopLevelDecls = csTopLevelDecls s <> pure decl }

nameEnv :: SList f env -> SList (Const String) env
nameEnv = flip evalState (0::Int) . slistMapA (\_ -> state $ \i -> (Const ("arg" ++ show i), i + 1))

compileToString :: SList STy env -> Ex env t -> String
compileToString env expr =
  let args = nameEnv env
      (res, s) = runState (compile' args expr) (CompState mempty mempty mempty 1)
      structs = genAllStructs (csStructs s <> Set.fromList (unSList unSTy env))

      (arg_pairs, arg_metrics) =
        unzip $ reverse (unSList (\(Product.Pair t (Const n)) -> ((n, repSTy t), metricsSTy t))
                                 (slistZip env args))
      (arg_offsets, result_offset') = computeStructOffsets arg_metrics
      result_offset = align (alignmentSTy (typeOf expr)) result_offset'
  in ($ "") $ compose
       [showString "#include <stdio.h>\n"
       ,showString "#include <stdint.h>\n"
       ,showString "#include <stdlib.h>\n"
       ,showString "#include <math.h>\n\n"
       ,compose $ map (\sd -> printStructDecl sd . showString "\n") structs
       ,showString "\n"
       ,compose [showString str . showString "\n\n" | str <- toList (csTopLevelDecls s)]
       ,showString $
          "static " ++ repSTy (typeOf expr) ++ " typed_kernel(" ++
            intercalate ", " (reverse (unSList (\(Product.Pair t (Const n)) -> repSTy t ++ " " ++ n) (slistZip env args))) ++
            ") {\n"
       ,compose $ map (\st -> showString "  " . printStmt 1 st . showString "\n") (toList (csStmts s))
       ,showString ("  return ") . printCExpr 0 res . showString ";\n}\n\n"
       ,showString "void kernel(void *data) {\n"
        -- Some code here assumes that we're on a 64-bit system, so let's check that
       ,showString "  if (sizeof(void*) != 8 || sizeof(size_t) != 8) { fprintf(stderr, \"Only 64-bit systems supported\\n\"); abort(); }\n"
       ,showString $ "  *(" ++ repSTy (typeOf expr) ++ "*)(data + " ++ show result_offset ++ ") = typed_kernel(" ++
                       concat (map (\((arg, typ), off, idx) ->
                                       "\n    *(" ++ typ ++ "*)(data + " ++ show off ++ ")"
                                       ++ (if idx < length arg_pairs - 1 then "," else "")
                                       ++ "  // " ++ arg)
                                   (zip3 arg_pairs arg_offsets [0::Int ..])) ++
                       "\n  );\n"
       ,showString "}\n"]

-- | Takes list of metrics (alignment, sizeof).
-- Returns (offsets, size of struct).
computeStructOffsets :: [(Int, Int)] -> ([Int], Int)
computeStructOffsets = go 0 0
  where
    go off maxal [(al, sz)] =
      ([off], align (max maxal al) (off + sz))
    go off maxal ((al, sz) : pairs@((al2,_):_)) =
      first (off :) $ go (align al2 (off + sz)) (max maxal al) pairs
    go _   _     [] = ([], 0)

-- | Assumes that this is called at the correct alignment.
serialise :: STy t -> Rep t -> Ptr () -> Int -> IO r -> IO r
serialise topty topval ptr off k =
  -- TODO: this code is quadratic in the depth of the type because of the alignment/sizeOf calls
  case (topty, topval) of
    (STNil, ()) -> k
    (STPair a b, (x, y)) ->
      serialise a x ptr off $
        serialise b y ptr (align (alignmentSTy b) (off + sizeofSTy a)) k
    (STEither a _, Left x) -> do
      pokeByteOff ptr off (0 :: Word8)  -- alignment of (a + b) is alignment of (union {a b})
      serialise a x ptr (off + alignmentSTy topty) k
    (STEither _ b, Right y) -> do
      pokeByteOff ptr off (1 :: Word8)
      serialise b y ptr (off + alignmentSTy topty) k
    (STMaybe _, Nothing) -> do
      pokeByteOff ptr off (0 :: Word8)
      k
    (STMaybe t, Just x) -> do
      pokeByteOff ptr off (1 :: Word8)
      serialise t x ptr (off + alignmentSTy t) k
    (STArr n t, Array sh vec) -> do
      let eltsz = sizeofSTy t
      allocaBytes (fromSNat n * 8 + 8 + shapeSize sh * eltsz) $ \bufptr -> do
        pokeByteOff ptr off bufptr

        pokeShape bufptr 0 n sh
        pokeByteOff @Word64 bufptr (8 * fromSNat n) (2 ^ 63)

        let off1 = fromSNat n * 8 + 8
            loop i
              | i == shapeSize sh = k
              | otherwise =
                  serialise t (vec V.! i) bufptr (off1 + i * eltsz) $
                    loop (i+1)
        loop 0
    (STScal sty, x) -> case sty of
      STI32 -> pokeByteOff ptr off (x :: Int32) >> k
      STI64 -> pokeByteOff ptr off (x :: Int64) >> k
      STF32 -> pokeByteOff ptr off (x :: Float) >> k
      STF64 -> pokeByteOff ptr off (x :: Double) >> k
      STBool -> pokeByteOff ptr off (fromIntegral (fromEnum x) :: Word8) >> k
    (STAccum{}, _) -> error "Cannot serialise accumulators"

-- | Assumes that this is called at the correct alignment.
deserialise :: STy t -> Ptr () -> Int -> IO (Rep t)
deserialise topty ptr off =
  -- TODO: this code is quadratic in the depth of the type because of the alignment/sizeOf calls
  case topty of
    STNil -> return ()
    STPair a b -> do
      x <- deserialise a ptr off
      y <- deserialise b ptr (align (alignmentSTy b) (off + sizeofSTy a))
      return (x, y)
    STEither a b -> do
      tag <- peekByteOff @Word8 ptr off
      if tag == 0  -- alignment of (a + b) is alignment of (union {a b})
        then Left <$> deserialise a ptr (off + alignmentSTy topty)
        else Right <$> deserialise b ptr (off + alignmentSTy topty)
    STMaybe t -> do
      tag <- peekByteOff @Word8 ptr off
      if tag == 0
        then return Nothing
        else Just <$> deserialise t ptr (off + alignmentSTy t)
    STArr n t -> do
      bufptr <- peekByteOff @(Ptr ()) ptr off
      sh <- peekShape bufptr 0 n
      refc <- peekByteOff @Word64 bufptr (8 * fromSNat n)
      let off1 = 8 * fromSNat n + 8
          eltsz = sizeofSTy t
      arr <- Array sh <$> V.generateM (shapeSize sh) (\i -> deserialise t bufptr (off1 + i * eltsz))
      when (refc < 2 ^ 62) $ free bufptr
      return arr
    STScal sty -> case sty of
      STI32 -> peekByteOff @Int32 ptr off
      STI64 -> peekByteOff @Int64 ptr off
      STF32 -> peekByteOff @Float ptr off
      STF64 -> peekByteOff @Double ptr off
      STBool -> toEnum . fromIntegral <$> peekByteOff @Word8 ptr off
    STAccum{} -> error "Cannot serialise accumulators"

align :: Int -> Int -> Int
align a off = (off + a - 1) `div` a * a

alignmentSTy :: STy t -> Int
alignmentSTy = fst . metricsSTy

sizeofSTy :: STy t -> Int
sizeofSTy = snd . metricsSTy

-- | Returns (alignment, sizeof)
metricsSTy :: STy t -> (Int, Int)
metricsSTy STNil = (1, 0)
metricsSTy (STPair a b) =
  let (a1, s1) = metricsSTy a
      (a2, s2) = metricsSTy b
  in (max a1 a2, align (max a1 a2) (s1 + s2))
metricsSTy (STEither a b) =
  let (a1, s1) = metricsSTy a
      (a2, s2) = metricsSTy b
  in (max a1 a2, max a1 a2 + max s1 s2)  -- the union after the tag byte is aligned
metricsSTy (STMaybe t) =
  let (a, s) = metricsSTy t
  in (a, a + s)  -- the union after the tag byte is aligned
metricsSTy (STArr _ _) = (8, 8)
metricsSTy (STScal sty) = case sty of
  STI32 -> (4, 4)
  STI64 -> (8, 8)
  STF32 -> (4, 4)
  STF64 -> (8, 8)
  STBool -> (1, 1)  -- compiled to uint8_t
metricsSTy (STAccum t) = metricsSTy t

pokeShape :: Ptr () -> Int -> SNat n -> Shape n -> IO ()
pokeShape ptr off = go . fromSNat
  where
    go :: Int -> Shape n -> IO ()
    go rank = \case
      ShNil -> return ()
      sh `ShCons` n -> do
        pokeByteOff ptr (off + (rank - 1) * 8) (fromIntegral n :: Int64)
        go (rank - 1) sh

peekShape :: Ptr () -> Int -> SNat n -> IO (Shape n)
peekShape ptr off = \case
  SZ -> return ShNil
  SS n -> ShCons <$> peekShape ptr off n
                 <*> (fromIntegral <$> peekByteOff @Int64 ptr (off + (fromSNat n) * 8))

compile' :: SList (Const String) env -> Ex env t -> CompM CExpr
compile' env = \case
  EVar _ t i -> do
    let Const var = slistIdx env i
    incrementVarAlways Increment t var
    return $ CELit var

  ELet _ rhs body -> do
    e <- compile' env rhs
    var <- genName
    emit $ SVarDecl True (repSTy (typeOf rhs)) var e
    rete <- compile' (Const var `SCons` env) body
    incrementVarAlways Decrement (typeOf rhs) var
    return rete

  EPair _ a b -> do
    name <- emitStruct (STPair (typeOf a) (typeOf b))
    e1 <- compile' env a
    e2 <- compile' env b
    return $ CEStruct name [("a", e1), ("b", e2)]

  EFst _ e -> do
    let STPair _ t2 = typeOf e
    e' <- compile' env e
    case incrementVar Decrement t2 of
      Nothing -> return $ CEProj e' "a"
      Just f -> do var <- genName
                   emit $ SVarDecl True (repSTy (typeOf e)) var e'
                   f (var ++ ".b")
                   return $ CEProj (CELit var) "a"

  ESnd _ e -> do
    let STPair t1 _ = typeOf e
    e' <- compile' env e
    case incrementVar Decrement t1 of
      Nothing -> return $ CEProj e' "b"
      Just f -> do var <- genName
                   emit $ SVarDecl True (repSTy (typeOf e)) var e'
                   f (var ++ ".a")
                   return $ CEProj (CELit var) "b"

  ENil _ -> do
    name <- emitStruct STNil
    return $ CEStruct name []

  EInl _ t e -> do
    name <- emitStruct (STEither (typeOf e) t)
    e1 <- compile' env e
    return $ CEStruct name [("tag", CELit "0"), ("l", e1)]

  EInr _ t e -> do
    name <- emitStruct (STEither t (typeOf e))
    e2 <- compile' env e
    return $ CEStruct name [("tag", CELit "1"), ("r", e2)]

  ECase _ (EOp _ OIf e) a b -> do
    e1 <- compile' env e
    (e2, stmts2) <- scope $ compile' (Const undefined `SCons` env) a  -- don't access that nil, stupid you
    (e3, stmts3) <- scope $ compile' (Const undefined `SCons` env) b
    retvar <- genName
    emit $ SVarDeclUninit (repSTy (typeOf a)) retvar
    emit $ SIf e1
             (BList stmts2 <> pure (SAsg retvar e2))
             (BList stmts3 <> pure (SAsg retvar e3))
    return (CELit retvar)

  ECase _ e a b -> do
    let STEither t1 t2 = typeOf e
    e1 <- compile' env e
    var <- genName
    -- I know those are not variable names, but it's fine, probably
    (e2, stmts2) <- scope $ compile' (Const (var ++ ".l") `SCons` env) a
    (e3, stmts3) <- scope $ compile' (Const (var ++ ".r") `SCons` env) b
    ((), stmtsRel1) <- scope $ incrementVarAlways Decrement t1 (var ++ ".l")
    ((), stmtsRel2) <- scope $ incrementVarAlways Decrement t2 (var ++ ".r")
    retvar <- genName
    emit $ SVarDeclUninit (repSTy (typeOf a)) retvar
    emit $ SBlock (pure (SVarDecl True (repSTy (typeOf e)) var e1)
                <> pure (SIf (CEBinop (CEProj (CELit var) "tag") "==" (CELit "0"))
                           (BList stmts2
                            <> BList stmtsRel1
                            <> pure (SAsg retvar e2))
                           (BList stmts3
                            <> BList stmtsRel2
                            <> pure (SAsg retvar e3))))
    return (CELit retvar)

  ENothing _ t -> do
    name <- emitStruct (STMaybe t)
    return $ CEStruct name [("tag", CELit "0")]

  EJust _ e -> do
    name <- emitStruct (STMaybe (typeOf e))
    e1 <- compile' env e
    return $ CEStruct name [("tag", CELit "1"), ("j", e1)]

  EMaybe _ a b e -> do
    let STMaybe t = typeOf e
    e1 <- compile' env e
    var <- genName
    (e2, stmts2) <- scope $ compile' env a
    (e3, stmts3) <- scope $ compile' (Const (var ++ ".j") `SCons` env) b
    ((), stmtsRel) <- scope $ incrementVarAlways Decrement t (var ++ ".j")
    retvar <- genName
    emit $ SVarDeclUninit (repSTy (typeOf a)) retvar
    emit $ SBlock (pure (SVarDecl True (repSTy (typeOf e)) var e1)
                <> pure (SIf (CEBinop (CEProj (CELit var) "tag") "==" (CELit "0"))
                           (BList stmts2
                            <> pure (SAsg retvar e2))
                           (BList stmts3
                            <> BList stmtsRel
                            <> pure (SAsg retvar e3))))
    return (CELit retvar)

  EConstArr _ n t (Array sh vec) -> do
    strname <- emitStruct (STArr n (STScal t))
    tldname <- genName' "carraybuf"
    -- Give it a refcount of _half_ the size_t max, so that it can be
    -- incremented and decremented at will and will "never" reach anything
    -- where something happens
    emitTLD $ "static " ++ strname ++ "_buf " ++ tldname ++ " = " ++
              "(" ++ strname ++ "_buf){.sh = {" ++ intercalate "," (map show (shapeToList sh)) ++ "}, " ++
              ".refc = (size_t)1<<63, .xs = {" ++ intercalate "," (map (compileScal False t) (toList vec)) ++ "}};"
    return (CEStruct strname [("buf", CEAddrOf (CELit tldname))])

  EBuild _ n esh efun -> do
    shname <- genName' "sh"
    emit . SVarDecl True (repSTy (typeOf esh)) shname =<< compile' env esh
    shsizename <- genName' "shsz"
    emit $ SVarDecl True "size_t" shsizename (compileShapeSize n shname)

    arrname <- allocArray "arr" n (typeOf efun) (CELit shsizename) (compileShapeTupIntoArray n shname)

    idxargname <- genName' "ix"
    (funretval, funstmts) <- scope $ compile' (Const idxargname `SCons` env) efun

    linivar <- genName' "li"
    ivars <- replicateM (fromSNat n) (genName' "i")
    emit $ SBlock $
      pure (SVarDecl False "size_t" linivar (CELit "0"))
      <> compose [pure . SLoop (repSTy tIx) ivar (CELit "0")
                               (CECast (repSTy tIx) (CEIndex (CELit (arrname ++ ".buf->sh")) (CELit (show dimidx))))
                 | (ivar, dimidx) <- zip ivars [0::Int ..]]
           (pure (SVarDecl True (repSTy (typeOf esh)) idxargname
                           (shapeTupFromLitVars n ivars))
            <> BList funstmts
            <> pure (SAsg (arrname ++ ".buf->xs[" ++ linivar ++ "++]") funretval))

    return (CELit arrname)

  -- EFold1Inner _ a b c -> error "TODO" -- EFold1Inner ext (compile' a) (compile' b) (compile' c)

  ESum1Inner _ e -> do
    let STArr (SS n) t = typeOf e
    e' <- compile' env e
    argname <- genName' "sumarg"
    emit $ SVarDecl True (repSTy (STArr (SS n) t)) argname e'

    shszname <- genName' "shsz"
    -- This n is one less than the shape of the thing we're querying, which is
    -- unexpected. But it's exactly what we want, so we do it anyway.
    emit $ SVarDecl True (repSTy tIx) shszname (compileArrShapeSize n argname)

    resname <- allocArray "sumres" n t (CELit shszname)
                  [CELit (argname ++ ".buf->sh[" ++ show i ++ "]") | i <- [0 .. fromSNat n - 1]]

    lenname <- genName' "n"
    emit $ SVarDecl True (repSTy tIx) lenname
                    (CELit (argname ++ ".buf->sh[" ++ show (fromSNat n) ++ "]"))

    ivar <- genName' "i"
    jvar <- genName' "j"
    accvar <- genName' "tot"
    emit $ SLoop (repSTy tIx) ivar (CELit "0") (CELit shszname) $ BList
             -- we have ScalIsNumeric, so it has 0 and (+) in C
             [SVarDecl False (repSTy t) accvar (CELit "0")
             ,SLoop (repSTy tIx) jvar (CELit "0") (CELit lenname) $
                pure $ SVerbatim $ accvar ++ " += " ++ argname ++ ".buf->xs[" ++ lenname ++ " * " ++ ivar ++ " + " ++ jvar ++ "];"
             ,SAsg (resname ++ ".buf->xs[" ++ ivar ++ "]") (CELit accvar)]

    return (CELit resname)

  EUnit _ e -> do
    e' <- compile' env e
    let typ = STArr SZ (typeOf e)
    strname <- emitStruct typ
    name <- genName
    emit $ SVarDecl True strname name (CEStruct strname
                  [("buf", CECall "malloc" [CELit (show (8 + sizeofSTy (typeOf e)))])])
    emit $ SAsg (name ++ ".buf->refc") (CELit "1")
    emit $ SAsg (name ++ ".buf->xs[0]") e'
    return (CELit name)

  EReplicate1Inner _ elen earg -> do
    let STArr n t = typeOf earg
    lenname <- genName' "replen"
    emit . SVarDecl True (repSTy tIx) lenname =<< compile' env elen
    argname <- genName' "reparg"
    emit . SVarDecl True (repSTy (typeOf earg)) argname =<< compile' env earg

    shszname <- genName' "shsz"
    emit $ SVarDecl True (repSTy tIx) shszname (compileArrShapeSize n argname)

    resname <- allocArray "rep" (SS n) t
                 (CEBinop (CELit shszname) "*" (CELit lenname))
                 ([CELit (argname ++ ".buf->sh[" ++ show i ++ "]") | i <- [0 .. fromSNat n - 1]]
                  ++ [CELit lenname])

    ivar <- genName' "i"
    jvar <- genName' "j"
    emit $ SLoop (repSTy tIx) ivar (CELit "0") (CELit shszname) $
             pure $ SLoop (repSTy tIx) jvar (CELit "0") (CELit lenname) $
               pure $ SAsg (resname ++ ".buf->xs[" ++ ivar ++ " * " ++ lenname ++ " + " ++ jvar ++ "]")
                           (CELit (argname ++ ".buf->xs[" ++ ivar ++ "]"))

    return (CELit resname)

  EMaximum1Inner _ e -> compileExtremum "max" "maximum1i" ">" env e

  EMinimum1Inner _ e -> compileExtremum "min" "minimum1i" "<" env e

  EConst _ t x -> return $ CELit $ compileScal True t x

  EIdx0 _ e -> do
    let STArr _ t = typeOf e
    e' <- compile' env e
    arrname <- genName
    emit $ SVarDecl True (repSTy (STArr SZ t)) arrname e'
    name <- genName
    emit $ SVarDecl True (repSTy t) name
              (CEIndex (CEPtrProj (CEProj (CELit arrname) "buf") "xs") (CELit "0"))
    incrementVarAlways Decrement (STArr SZ t) arrname
    return (CELit name)

  -- EIdx1 _ a b -> error "TODO" -- EIdx1 ext (compile' a) (compile' b)

  EIdx _ earr eidx -> do
    let STArr n t = typeOf earr
    arrname <- genName' "ixarr"
    idxname <- genName' "ixix"
    emit . SVarDecl True (repSTy (typeOf earr)) arrname =<< compile' env earr
    when (fromSNat n > 0) $ emit . SVarDecl True (repSTy (typeOf eidx)) idxname =<< compile' env eidx
    resname <- genName' "ixres"
    emit $ SVarDecl True (repSTy t) resname (CEIndex (CELit (arrname ++ ".buf->xs")) (toLinearIdx n arrname idxname))
    incrementVarAlways Decrement (STArr n t) arrname
    return (CELit resname)

  EShape _ e -> do
    let STArr n _ = typeOf e
        t = tTup (sreplicate n tIx)
    _ <- emitStruct t
    name <- genName
    emit . SVarDecl True (repSTy (typeOf e)) name =<< compile' env e
    resname <- genName
    emit $ SVarDecl True (repSTy t) resname (compileShapeQuery n name)
    incrementVarAlways Decrement (typeOf e) name
    return (CELit resname)

  EOp _ op (EPair _ e1 e2) -> do
    e1' <- compile' env e1
    e2' <- compile' env e2
    compileOpPair op e1' e2'

  EOp _ op e -> do
    e' <- compile' env e
    compileOpGeneral op e'

  ECustom _ t1 t2 _ earg _ _ e1 e2 -> do
    e1' <- compile' env e1
    name1 <- genName
    emit $ SVarDecl True (repSTy t1) name1 e1'
    e2' <- compile' env e2
    name2 <- genName
    emit $ SVarDecl True (repSTy t2) name2 e2'
    compile' (Const name2 `SCons` Const name1 `SCons` SNil) earg

  EWith _ t e1 e2 -> do
    e1' <- compile' env e1
    name1 <- genName
    emit $ SVarDecl True (repSTy (typeOf e1)) name1 e1'

    mcopy <- copyForWriting t name1
    accname <- genName' "accum"
    emit $ SVarDecl False (repSTy (STAccum t)) accname (maybe (CELit name1) id mcopy)

    e2' <- compile' (Const accname `SCons` env) e2

    return $ CEStruct (repSTy (STPair (typeOf e2) t)) [("a", e2'), ("b", CELit accname)]

  -- EAccum _ n a b e -> error "TODO" -- EAccum n (compile' a) (compile' b) (compile' e)

  EError _ t s -> do
    -- using 'show' here is wrong, but it's good enough for me.
    emit $ SVerbatim $ "fprintf(stderr, \"ERROR: %s\\n\", " ++ show s ++ "); exit(1);"
    case t of
      STScal _ -> return (CELit "0")
      _ -> do
        name <- emitStruct t
        return $ CEStruct name []

  EZero{} -> error "Compile: monoid operations should have been eliminated"
  EPlus{} -> error "Compile: monoid operations should have been eliminated"
  EOneHot{} -> error "Compile: monoid operations should have been eliminated"

  EFold1Inner{} -> error "Compile: not implemented: EFold1Inner"
  EIdx1{} -> error "Compile: not implemented: EIdx1"
  EAccum{} -> error "Compile: not implemented: EAccum"

data Increment = Increment | Decrement
  deriving (Show)

-- | Increment reference counts in the components of the given variable.
incrementVar :: Increment -> STy a -> Maybe (String -> CompM ())
incrementVar inc ty =
  let tree = makeArrayTree ty
  in case tree of ATNoop -> Nothing
                  _ -> Just $ \var -> incrementVar' inc var tree

incrementVarAlways :: Increment -> STy a -> String -> CompM ()
incrementVarAlways inc ty var = maybe (pure ()) ($ var) (incrementVar inc ty)

data ArrayTree = ATArray  -- ^ we've arrived at an array we need to decrement the refcount of
               | ATNoop  -- ^ don't do anything here
               | ATProj String ArrayTree  -- ^ descend one field deeper
               | ATCondTag ArrayTree ArrayTree  -- ^ if tag is 0, first; if 1, second
               | ATBoth ArrayTree ArrayTree  -- ^ do both these paths

smartATProj :: String -> ArrayTree -> ArrayTree
smartATProj _ ATNoop = ATNoop
smartATProj field t = ATProj field t

smartATCondTag :: ArrayTree -> ArrayTree -> ArrayTree
smartATCondTag ATNoop ATNoop = ATNoop
smartATCondTag t t' = ATCondTag t t'

smartATBoth :: ArrayTree -> ArrayTree -> ArrayTree
smartATBoth ATNoop t = t
smartATBoth t ATNoop = t
smartATBoth t t' = ATBoth t t'

makeArrayTree :: STy a -> ArrayTree
makeArrayTree STNil = ATNoop
makeArrayTree (STPair a b) = smartATBoth (smartATProj "a" (makeArrayTree a))
                                         (smartATProj "b" (makeArrayTree b))
makeArrayTree (STEither a b) = smartATCondTag (smartATProj "l" (makeArrayTree a))
                                              (smartATProj "r" (makeArrayTree b))
makeArrayTree (STMaybe t) = smartATCondTag ATNoop (makeArrayTree t)
makeArrayTree (STArr _ _) = ATArray
makeArrayTree (STScal _) = ATNoop
makeArrayTree (STAccum _) = ATNoop

incrementVar' :: Increment -> String -> ArrayTree -> CompM ()
incrementVar' inc path ATArray =
  case inc of
    Increment -> emit $ SVerbatim (path ++ ".buf->refc++;")
    Decrement ->
      emit $ SVerbatim $ "if (--" ++ path ++ ".buf->refc == 0) free(" ++ path ++ ".buf);"
incrementVar' _ _ ATNoop = pure ()
incrementVar' inc path (ATProj field t) = incrementVar' inc (path ++ "." ++ field) t
incrementVar' inc path (ATCondTag t1 t2) = do
  ((), stmts1) <- scope $ incrementVar' inc path t1
  ((), stmts2) <- scope $ incrementVar' inc path t2
  emit $ SIf (CEBinop (CELit (path ++ ".tag")) "==" (CELit "0")) (BList stmts1) (BList stmts2)
incrementVar' inc path (ATBoth t1 t2) = incrementVar' inc path t1 >> incrementVar' inc path t2

toLinearIdx :: SNat n -> String -> String -> CExpr
toLinearIdx SZ _ _ = CELit "0"
toLinearIdx (SS SZ) _ idxvar = CELit (idxvar ++ ".b")
toLinearIdx (SS n) arrvar idxvar =
  CEBinop (CEBinop (toLinearIdx n arrvar (idxvar ++ ".a"))
                   "*" (CEIndex (CELit (arrvar ++ ".buf->sh")) (CELit (show (fromSNat n)))))
          "+" (CELit (idxvar ++ ".b"))

-- fromLinearIdx :: SNat n -> String -> String -> CompM CExpr
-- fromLinearIdx SZ _ _ = return $ CEStruct (repSTy STNil) []
-- fromLinearIdx (SS n) arrvar idxvar = do
--   name <- genName
--   emit $ SVarDecl True (repSTy tIx) name (CEBinop (CELit idxvar) "/" (CELit (arrvar ++ ".buf->sh[" ++ show (fromSNat n) ++ "]")))
--   _

-- | The shape must have the outer dimension at the head (and the inner dimension on the right).
allocArray :: String -> SNat n -> STy t -> CExpr -> [CExpr] -> CompM String
allocArray nameBase rank eltty shsz shape = do
  when (length shape /= fromSNat rank) $
    error "allocArray: shape does not match rank"
  let arrty = STArr rank eltty
  strname <- emitStruct arrty
  arrname <- genName' nameBase
  emit $ SVarDecl True strname arrname $ CEStruct strname
            [("buf", CECall "malloc" [CEBinop (CELit (show (fromSNat rank * 8 + 8)))
                                              "+"
                                              (CEBinop shsz "*" (CELit (show (sizeofSTy eltty))))])]
  forM_ (zip shape [0::Int ..]) $ \(dim, i) ->
    emit $ SAsg (arrname ++ ".buf->sh[" ++ show i ++ "]") dim
  emit $ SAsg (arrname ++ ".buf->refc") (CELit "1")
  return arrname

compileShapeQuery :: SNat n -> String -> CExpr
compileShapeQuery SZ _ = CEStruct (repSTy STNil) []
compileShapeQuery (SS n) var =
  CEStruct (repSTy (tTup (sreplicate (SS n) tIx)))
    [("a", compileShapeQuery n var)
    ,("b", CEIndex (CELit (var ++ ".buf->sh")) (CELit (show (fromSNat n))))]

compileShapeSize :: SNat n -> String -> CExpr
compileShapeSize SZ _ = CELit "1"
compileShapeSize (SS SZ) var = CELit (var ++ ".b")
compileShapeSize (SS n) var = CEBinop (compileShapeSize n (var ++ ".a")) "*" (CELit (var ++ ".b"))

-- | Takes a variable name for the array, not the buffer.
compileArrShapeSize :: SNat n -> String -> CExpr
compileArrShapeSize SZ _ = CELit "1"
compileArrShapeSize n var =
  foldl1' (\a b -> CEBinop a "*" b) [CELit (var ++ ".buf->sh[" ++ show i ++ "]")
                                    | i <- [0 .. fromSNat n - 1]]

compileShapeTupIntoArray :: SNat n -> String -> [CExpr]
compileShapeTupIntoArray = \n var -> map CELit (toList (go n var))
  where
    go :: SNat n -> String -> Bag String
    go SZ _ = mempty
    go (SS n) var = go n (var ++ ".a") <> pure (var ++ ".b")

-- | Takes variable names with the innermost dimension on the right.
shapeTupFromLitVars :: SNat n -> [String] -> CExpr
shapeTupFromLitVars = \n -> go n . reverse
  where
    -- takes variables with the innermost dimension at the _head_
    go :: SNat n -> [String] -> CExpr
    go SZ [] = CEStruct (repSTy STNil) []
    go (SS n) (var : vars) = CEStruct (repSTy (tTup (sreplicate (SS n) tIx))) [("a", go n vars), ("b", CELit var)]
    go _ _ = error "shapeTupFromLitVars: SNat and list do not correspond"

compileOpGeneral :: SOp a b -> CExpr -> CompM CExpr
compileOpGeneral op e1 = do
  let unary cop = return @(State CompState) $ CECall cop [e1]
  let binary cop = do
        name <- genName
        emit $ SVarDecl True (repSTy (opt1 op)) name e1
        return $ CEBinop (CEProj (CELit name) "a") cop (CEProj (CELit name) "b")
  case op of
    OAdd _ -> binary "+"
    OMul _ -> binary "*"
    ONeg _ -> unary "-"
    OLt _ -> binary "<"
    OLe _ -> binary "<="
    OEq _ -> binary "=="
    ONot -> unary "!"
    OAnd -> binary "&&"
    OOr -> binary "||"
    OIf -> do
      name <- emitStruct (STEither STNil STNil)
      _ <- emitStruct STNil
      return $ CEIf e1 (CEStruct name [("tag", CELit "0")])
                       (CEStruct name [("tag", CELit "1")])
    ORound64 -> unary "(int64_t)round"  -- ew
    OToFl64 -> unary "(double)"
    ORecip _ -> return $ CEBinop (CELit "1.0") "/" e1
    OExp STF32 -> unary "expf"
    OExp STF64 -> unary "exp"
    OLog STF32 -> unary "logf"
    OLog STF64 -> unary "log"
    OIDiv _ -> binary "/"

compileOpPair :: SOp a b -> CExpr -> CExpr -> CompM CExpr
compileOpPair op e1 e2 = do
  let binary cop = return @(State CompState) $ CEBinop e1 cop e2
  case op of
    OAdd _ -> binary "+"
    OMul _ -> binary "*"
    OLt _ -> binary "<"
    OLe _ -> binary "<="
    OEq _ -> binary "=="
    OAnd -> binary "&&"
    OOr -> binary "||"
    OIDiv _ -> binary "/"
    _ -> error "compileOpPair: got unary operator"

-- | Bool: whether to ensure that the literal itself already has the appropriate type
compileScal :: Bool -> SScalTy t -> ScalRep t -> String
compileScal pedantic typ x = case typ of
  STI32 -> (if pedantic then "(int32_t)" else "") ++ show x
  STI64 -> (if pedantic then "(int64_t)" else "") ++ show x
  STF32 -> show x ++ "f"
  STF64 -> show x
  STBool -> if x then "1" else "0"

compileExtremum :: String -> String -> String -> SList (Const String) env -> Ex env (TArr (S n) t) -> CompM CExpr
compileExtremum nameBase opName operator env e = do
  let STArr (SS n) t = typeOf e
  e' <- compile' env e
  argname <- genName' (nameBase ++ "arg")
  emit $ SVarDecl True (repSTy (STArr (SS n) t)) argname e'

  shszname <- genName' "shsz"
  -- This n is one less than the shape of the thing we're querying, which is
  -- unexpected. But it's exactly what we want, so we do it anyway.
  emit $ SVarDecl True (repSTy tIx) shszname (compileArrShapeSize n argname)

  resname <- allocArray (nameBase ++ "res") n t (CELit shszname)
                [CELit (argname ++ ".buf->sh[" ++ show i ++ "]") | i <- [0 .. fromSNat n - 1]]

  lenname <- genName' "n"
  emit $ SVarDecl True (repSTy tIx) lenname
                  (CELit (argname ++ ".buf->sh[" ++ show (fromSNat n) ++ "]"))

  emit $ SVerbatim $ "if (" ++ lenname ++ " == 0) { fprintf(stderr, \"Empty array in " ++ opName ++ "\\n\"); abort(); }"

  ivar <- genName' "i"
  jvar <- genName' "j"
  xvar <- genName
  redvar <- genName' "red"  -- use "red", not "acc", to avoid confusion with accumulators
  emit $ SLoop (repSTy tIx) ivar (CELit "0") (CELit shszname) $ BList
           -- we have ScalIsNumeric, so it has 1 and (<) etc. in C
           [SVarDecl False (repSTy t) redvar (CELit (argname ++ ".buf->xs[" ++ lenname ++ " * " ++ ivar ++ "]"))
           ,SLoop (repSTy tIx) jvar (CELit "1") (CELit lenname) $ BList
              [SVarDecl True (repSTy t) xvar (CELit (argname ++ ".buf->xs[" ++ lenname ++ " * " ++ ivar ++ " + " ++ jvar ++ "]"))
              ,SAsg redvar $ CEIf (CEBinop (CELit xvar) operator (CELit redvar)) (CELit xvar) (CELit redvar)
              ]
           ,SAsg (resname ++ ".buf->xs[" ++ ivar ++ "]") (CELit redvar)]

  return (CELit resname)

-- | If this returns Nothing, there was nothing to copy because making a simple
-- value copy in C already makes it suitable to write to.
copyForWriting :: STy t -> String -> CompM (Maybe CExpr)
copyForWriting topty var = case topty of
  STNil -> return Nothing

  STPair a b -> do
    e1 <- copyForWriting a (var ++ ".a")
    e2 <- copyForWriting b (var ++ ".b")
    case (e1, e2) of
      (Nothing, Nothing) -> return Nothing
      _ -> return $ Just $ CEStruct (repSTy topty)
                             [("a", fromMaybe (CELit (var++".a")) e1)
                             ,("b", fromMaybe (CELit (var++".b")) e2)]

  STEither a b -> do
    (e1, stmts1) <- scope $ copyForWriting a (var ++ ".l")
    (e2, stmts2) <- scope $ copyForWriting b (var ++ ".r")
    case (e1, e2) of
      (Nothing, Nothing) -> return Nothing
      _ -> do
        name <- genName
        emit $ SVarDeclUninit (repSTy topty) name
        emit $ SIf (CEBinop (CELit var) "==" (CELit "0"))
                 (BList stmts1
                  <> pure (SAsg name (CEStruct (repSTy topty)
                                        [("tag", CELit "0"), ("l", fromMaybe (CELit (var++".l")) e1)])))
                 (BList stmts2
                  <> pure (SAsg name (CEStruct (repSTy topty)
                                        [("tag", CELit "1"), ("r", fromMaybe (CELit (var++".r")) e2)])))
        return (Just (CELit name))

  STMaybe t -> do
    (e1, stmts1) <- scope $ copyForWriting t (var ++ ".j")
    case e1 of
      Nothing -> return Nothing
      Just e1' -> do
        name <- genName
        emit $ SVarDeclUninit (repSTy topty) name
        emit $ SIf (CEBinop (CELit var) "==" (CELit "0"))
                 (pure (SAsg name (CEStruct (repSTy topty) [("tag", CELit "0")])))
                 (BList stmts1
                  <> pure (SAsg name (CEStruct (repSTy topty) [("tag", CELit "1"), ("j", e1')])))
        return (Just (CELit name))

  -- If there are no nested arrays, we know that a refcount of 1 means that the
  -- whole thing is owned. Nested arrays have their own refcount, so with
  -- nesting we'd have to check the refcounts of all the nested arrays _too_;
  -- at that point we might as well copy the whole thing. Furthermore, no
  -- sub-arrays means that the whole thing is flat, and we can just memcpy if
  -- necessary.
  STArr n t | not (hasArrays t) -> do
    name <- genName
    shszname <- genName' "shsz"
    emit $ SVarDeclUninit (repSTy (STArr n t)) name

    emit $ SIf (CEBinop (CELit (var ++ ".refc")) "==" (CELit "1"))
             (pure (SAsg name (CELit var)))
             (let shbytes = fromSNat n * 8
                  databytes = CEBinop (CELit shszname) "*" (CELit (show (sizeofSTy t)))
                  totalbytes = CEBinop (CELit (show (shbytes + 8))) "+" databytes
              in BList
               [SVarDecl True (repSTy tIx) shszname (compileArrShapeSize n var)
               ,SAsg name (CEStruct (repSTy (STArr n t)) [("buf", CECall "malloc" [totalbytes])])
               ,SVerbatim $ "memcpy(" ++ name ++ ".buf->sh, " ++ var ++ ".buf->sh, " ++
                                      show shbytes ++ ");"
               ,SAsg (name ++ ".buf->refc") (CELit "1")
               ,SVerbatim $ "memcpy(" ++ name ++ ".buf->xs, " ++ var ++ ".buf->xs, " ++
                                      printCExpr 0 databytes ")"])
    return (Just (CELit name))

  STArr n t -> do
    shszname <- genName' "shsz"
    emit $ SVarDecl True (repSTy tIx) shszname (compileArrShapeSize n var)

    let shbytes = fromSNat n * 8
        databytes = CEBinop (CELit shszname) "*" (CELit (show (sizeofSTy t)))
        totalbytes = CEBinop (CELit (show (shbytes + 8))) "+" databytes

    name <- genName
    emit $ SVarDecl False (repSTy (STArr n t)) name
             (CEStruct (repSTy (STArr n t)) [("buf", CECall "malloc" [totalbytes])])
    emit $ SVerbatim $ "memcpy(" ++ name ++ ".buf->sh, " ++ var ++ ".buf->sh, " ++
                                 show shbytes ++ ");"
    emit $ SAsg (name ++ ".buf->refc") (CELit "1")

    -- put the arrays in variables to cut short the not-quite-var chain
    dstvar <- genName' "cpydst"
    emit $ SVarDecl True (repSTy t ++ " *") dstvar (CELit (name ++ ".buf->xs"))
    srcvar <- genName' "cpysrc"
    emit $ SVarDecl True (repSTy t ++ " *") srcvar (CELit (var ++ ".buf->xs"))

    ivar <- genName' "i"

    (cpye, cpystmts) <- scope $ copyForWriting t (srcvar ++ "[" ++ ivar ++ "]")
    let cpye' = case cpye of
                  Just e -> e
                  Nothing -> error "copyForWriting: arrays cannot be copied as-is, bug"

    emit $ SLoop (repSTy tIx) ivar (CELit "0") (CELit shszname) $
             BList cpystmts
             <> pure (SAsg (dstvar ++ "[" ++ ivar ++ "]") cpye')

    return (Just (CELit name))

  STScal _ -> return Nothing

  STAccum _ -> error "Compile: Nested accumulators not supported"

compose :: Foldable t => t (a -> a) -> a -> a
compose = foldr (.) id

-- | Type-restricted.
(^) :: Num a => a -> Int -> a
(^) = (Prelude.^)