-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathTransCustom.hs
1695 lines (1454 loc) · 73.3 KB
/
TransCustom.hs
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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ConstraintKinds #-}
module Mir.TransCustom(customOps) where
import Data.Bits (shift)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Vector as V
import Control.Monad
import Control.Lens
import GHC.Stack
-- parameterized-utils
import qualified Data.Parameterized.Context as Ctx
import Data.Parameterized.Classes
import Data.Parameterized.NatRepr
import Data.Parameterized.Some
-- crucible
import qualified Lang.Crucible.Types as C
import qualified Lang.Crucible.CFG.Generator as G
import qualified Lang.Crucible.CFG.Expr as E
import qualified Lang.Crucible.Syntax as S
import qualified Lang.Crucible.CFG.Reg as R
import qualified Mir.DefId as M
import Mir.DefId (ExplodedDefId)
import Mir.Mir
import Mir.Generator hiding (customOps)
import Mir.Intrinsics
import Mir.TransTy
import Mir.Trans
--------------------------------------------------------------------------------------------------------------------------
-- * Primitives & other custom stuff
customOps = CustomOpMap customOpDefs fnPtrShimDef cloneShimDef cloneFromShimDef
customOpDefs :: Map ExplodedDefId CustomRHS
customOpDefs = Map.fromList $ [
slice_index_usize_get_unchecked
, slice_index_range_get_unchecked
, slice_index_usize_get_unchecked_mut
, slice_index_range_get_unchecked_mut
, slice_len
, const_slice_len
, mut_slice_len
-- core::intrinsics
, discriminant_value
, type_id
, mem_swap
, add_with_overflow
, sub_with_overflow
, mul_with_overflow
, wrapping_add
, wrapping_sub
, wrapping_mul
, saturating_add
, saturating_sub
, unchecked_add
, unchecked_sub
, unchecked_mul
, unchecked_div
, unchecked_rem
, unchecked_shl
, unchecked_shr
, ctlz
, ctlz_nonzero
, rotate_left
, rotate_right
, size_of
, min_align_of
, intrinsics_assume
, assert_inhabited
, unlikely
, mem_transmute
, mem_crucible_identity_transmute
, array_from_slice
, vector_new
, vector_replicate
, vector_len
, vector_push
, vector_push_front
, vector_pop
, vector_pop_front
, vector_as_slice
, vector_as_mut_slice
, vector_concat
, vector_split_at
, vector_copy_from_slice
, array_zeroed
, array_lookup
, array_update
, array_as_slice
, array_as_mut_slice
, any_new
, any_downcast
, ptr_offset
, ptr_offset_mut
, ptr_wrapping_offset
, ptr_wrapping_offset_mut
, ptr_offset_from
, ptr_offset_from_mut
, sub_ptr
, sub_ptr_mut
, ptr_compare_usize
, is_aligned_and_not_null
, ptr_slice_from_raw_parts
, ptr_slice_from_raw_parts_mut
, ptr_read
, ptr_write
, ptr_swap
, ptr_null
, ptr_null_mut
, intrinsics_copy
, intrinsics_copy_nonoverlapping
, exit
, abort
, panicking_begin_panic
, panicking_panic
, panicking_panic_fmt
, panicking_panicking
, allocate
, allocate_zeroed
, reallocate
, maybe_uninit_uninit
, ctpop
, integer_from_u8
, integer_from_i32
, integer_from_u64
, integer_as_u8
, integer_as_u64
, integer_shl
, integer_shr
, integer_bitand
, integer_bitor
, integer_add
, integer_sub
, integer_rem
, integer_eq
, integer_lt
] ++ bv_funcs ++ atomic_funcs
-----------------------------------------------------------------------------------------------------
-- ** Custom: Exit
exit :: (ExplodedDefId, CustomRHS)
exit = (["std", "process", "exit"], \s ->
Just (CustomOpExit $ \ops -> return "process::exit"))
abort :: (ExplodedDefId, CustomRHS)
abort = (["core", "intrinsics", "", "abort"], \s ->
Just (CustomOpExit $ \ops -> return "intrinsics::abort"))
panicking_begin_panic :: (ExplodedDefId, CustomRHS)
panicking_begin_panic = (["std", "panicking", "begin_panic"], \s -> Just $ CustomOpExit $ \ops -> do
fn <- expectFnContext
return $ "panicking::begin_panic, called from " <> M.idText (fn^.fname)
)
panicking_panic :: (ExplodedDefId, CustomRHS)
panicking_panic = (["core", "panicking", "panic"], \s -> Just $ CustomOpExit $ \ops -> do
fn <- expectFnContext
return $ "panicking::panic, called from " <> M.idText (fn^.fname)
)
panicking_panic_fmt :: (ExplodedDefId, CustomRHS)
panicking_panic_fmt = (["core", "panicking", "panic_fmt"], \s -> Just $ CustomOpExit $ \ops -> do
fn <- expectFnContext
return $ "panicking::panic_fmt, called from " <> M.idText (fn^.fname)
)
panicking_panicking :: (ExplodedDefId, CustomRHS)
panicking_panicking = (["std", "panicking", "panicking"], \_ -> Just $ CustomOp $ \_ _ -> do
return $ MirExp C.BoolRepr $ R.App $ E.BoolLit False)
-----------------------------------------------------------------------------------------------------
-- ** Custom: Vector
-- Methods for crucible::vector::Vector<T> (which has custom representation)
vector_new :: (ExplodedDefId, CustomRHS)
vector_new = ( ["crucible","vector","{impl}", "new"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ _ -> do
Some tpr <- tyToReprM t
return $ MirExp (C.VectorRepr tpr) (R.App $ E.VectorLit tpr V.empty)
_ -> Nothing
vector_replicate :: (ExplodedDefId, CustomRHS)
vector_replicate = ( ["crucible","vector","{impl}", "replicate"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp tpr eVal, MirExp UsizeRepr eLen] -> do
let eLenNat = R.App $ usizeToNat eLen
return $ MirExp (C.VectorRepr tpr) (R.App $ E.VectorReplicate tpr eLenNat eVal)
_ -> mirFail $ "bad arguments for Vector::replicate: " ++ show ops
_ -> Nothing
vector_len :: (ExplodedDefId, CustomRHS)
vector_len = ( ["crucible","vector","{impl}", "len"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr (C.VectorRepr tpr)) eRef] -> do
e <- readMirRef (C.VectorRepr tpr) eRef
return $ MirExp UsizeRepr (R.App $ vectorSizeUsize R.App e)
_ -> mirFail $ "bad arguments for Vector::len: " ++ show ops
_ -> Nothing
vector_push :: (ExplodedDefId, CustomRHS)
vector_push = ( ["crucible","vector","{impl}", "push"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.VectorRepr tpr) eVec, MirExp tpr' eItem]
| Just Refl <- testEquality tpr tpr' -> do
eSnoc <- vectorSnoc tpr eVec eItem
return $ MirExp (C.VectorRepr tpr) eSnoc
_ -> mirFail $ "bad arguments for Vector::push: " ++ show ops
_ -> Nothing
vector_push_front :: (ExplodedDefId, CustomRHS)
vector_push_front = ( ["crucible","vector","{impl}", "push_front"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.VectorRepr tpr) eVec, MirExp tpr' eItem]
| Just Refl <- testEquality tpr tpr' -> do
let eSnoc = R.App $ E.VectorCons tpr eItem eVec
return $ MirExp (C.VectorRepr tpr) eSnoc
_ -> mirFail $ "bad arguments for Vector::push_front: " ++ show ops
_ -> Nothing
vector_pop :: (ExplodedDefId, CustomRHS)
vector_pop = ( ["crucible","vector","{impl}", "pop"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.VectorRepr tpr) eVec] -> do
meInit <- MirExp (C.VectorRepr tpr) <$> vectorInit tpr eVec
-- `Option<T>` must exist because it appears in the return type.
meLast <- vectorLast tpr eVec >>= maybeToOption t tpr
vectorTy <- findExplodedAdtTy vectorExplodedDefId (Substs [t])
optionTy <- findExplodedAdtTy optionExplodedDefId (Substs [t])
buildTupleMaybeM [vectorTy, optionTy] [Just meInit, Just meLast]
_ -> mirFail $ "bad arguments for Vector::pop: " ++ show ops
_ -> Nothing
vector_pop_front :: (ExplodedDefId, CustomRHS)
vector_pop_front = ( ["crucible","vector","{impl}", "pop_front"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.VectorRepr tpr) eVec] -> do
-- `Option<T>` must exist because it appears in the return type.
meHead <- vectorHead tpr eVec >>= maybeToOption t tpr
meTail <- MirExp (C.VectorRepr tpr) <$> vectorTail tpr eVec
optionTy <- findExplodedAdtTy optionExplodedDefId (Substs [t])
vectorTy <- findExplodedAdtTy vectorExplodedDefId (Substs [t])
buildTupleMaybeM [optionTy, vectorTy] [Just meHead, Just meTail]
_ -> mirFail $ "bad arguments for Vector::pop_front: " ++ show ops
_ -> Nothing
vector_as_slice_impl :: CustomRHS
vector_as_slice_impl (Substs [t]) =
Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr (C.VectorRepr tpr)) e] -> do
-- This is similar to `&mut [T; n] -> &mut [T]` unsizing.
v <- readMirRef (C.VectorRepr tpr) e
let end = R.App $ vectorSizeUsize R.App v
e' <- mirRef_vectorAsMirVector tpr e
e'' <- subindexRef tpr e' (R.App $ usizeLit 0)
let tup = S.mkStruct
(Ctx.Empty Ctx.:> MirReferenceRepr tpr Ctx.:> knownRepr)
(Ctx.Empty Ctx.:> e'' Ctx.:> end)
return $ MirExp (MirSliceRepr tpr) tup
_ -> mirFail $ "bad arguments for Vector::as_slice: " ++ show ops
vector_as_slice_impl _ = Nothing
-- `&Vector<T>` and `&[T]` use the same representations as `&mut Vector<T>` and
-- `&mut [T]`, so we can use the same implementation.
vector_as_slice :: (ExplodedDefId, CustomRHS)
vector_as_slice = ( ["crucible","vector","{impl}", "as_slice"], vector_as_slice_impl )
vector_as_mut_slice :: (ExplodedDefId, CustomRHS)
vector_as_mut_slice = ( ["crucible","vector","{impl}", "as_mut_slice"], vector_as_slice_impl )
vector_concat :: (ExplodedDefId, CustomRHS)
vector_concat = ( ["crucible","vector","{impl}", "concat"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.VectorRepr tpr1) e1, MirExp (C.VectorRepr tpr2) e2]
| Just Refl <- testEquality tpr1 tpr2 -> do
MirExp (C.VectorRepr tpr1) <$> vectorConcat tpr1 e1 e2
_ -> mirFail $ "bad arguments for Vector::concat: " ++ show ops
_ -> Nothing
vector_split_at :: (ExplodedDefId, CustomRHS)
vector_split_at = ( ["crucible","vector","{impl}", "split_at"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.VectorRepr tpr) eVec, MirExp UsizeRepr eIdx] -> do
let eIdxNat = R.App $ usizeToNat eIdx
mePre <- MirExp (C.VectorRepr tpr) <$> vectorTake tpr eVec eIdxNat
meSuf <- MirExp (C.VectorRepr tpr) <$> vectorDrop tpr eVec eIdxNat
vectorTy <- findExplodedAdtTy vectorExplodedDefId (Substs [t])
buildTupleMaybeM [vectorTy, vectorTy] [Just mePre, Just meSuf]
_ -> mirFail $ "bad arguments for Vector::split_at: " ++ show ops
_ -> Nothing
vector_copy_from_slice :: (ExplodedDefId, CustomRHS)
vector_copy_from_slice = ( ["crucible","vector","{impl}", "copy_from_slice"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirSliceRepr tpr) e] -> do
let ptr = getSlicePtr e
let len = getSliceLen e
v <- vectorCopy tpr ptr len
return $ MirExp (C.VectorRepr tpr) v
_ -> mirFail $ "bad arguments for Vector::copy_from_slice: " ++ show ops
_ -> Nothing
-----------------------------------------------------------------------------------------------------
-- ** Custom: Array
-- Methods for crucible::array::Array<T> (which has custom representation)
array_zeroed :: (ExplodedDefId, CustomRHS)
array_zeroed = ( ["crucible","array","{impl}", "zeroed"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ _ -> tyToReprM t >>= \(Some tpr) -> case tpr of
C.BVRepr w -> do
let idxs = Ctx.Empty Ctx.:> BaseUsizeRepr
v <- arrayZeroed idxs w
return $ MirExp (C.SymbolicArrayRepr idxs (C.BaseBVRepr w)) v
_ -> mirFail $ "bad substs for Array::zeroed: " ++ show t
_ -> Nothing
array_lookup :: (ExplodedDefId, CustomRHS)
array_lookup = ( ["crucible","array","{impl}", "lookup"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[ MirExp (UsizeArrayRepr btr) eArr,
MirExp UsizeRepr eIdx ] -> do
let idx = E.BaseTerm BaseUsizeRepr eIdx
let idxs = Ctx.Empty Ctx.:> idx
return $ MirExp (C.baseToType btr) (R.App $ E.SymArrayLookup btr eArr idxs)
_ -> mirFail $ "bad arguments for Array::lookup: " ++ show ops
_ -> Nothing
array_update :: (ExplodedDefId, CustomRHS)
array_update = ( ["crucible","array","{impl}", "update"], ) $ \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[ MirExp arrTpr@(UsizeArrayRepr btr) eArr,
MirExp UsizeRepr eIdx,
MirExp (C.asBaseType -> C.AsBaseType btr') eVal ]
| Just Refl <- testEquality btr btr' -> do
let idx = E.BaseTerm BaseUsizeRepr eIdx
let idxs = Ctx.Empty Ctx.:> idx
return $ MirExp arrTpr (R.App $ E.SymArrayUpdate btr eArr idxs eVal)
_ -> mirFail $ "bad arguments for Array::lookup: " ++ show ops
_ -> Nothing
array_as_slice_impl :: CustomRHS
array_as_slice_impl (Substs [t]) =
Just $ CustomOp $ \_ ops -> case ops of
[ MirExp (MirReferenceRepr (UsizeArrayRepr btpr)) e,
MirExp UsizeRepr start,
MirExp UsizeRepr len ] -> do
e' <- mirRef_arrayAsMirVector btpr e
let tpr = C.baseToType btpr
ptr <- subindexRef tpr e' start
return $ MirExp (MirSliceRepr tpr) $ mkSlice tpr ptr len
_ -> mirFail $ "bad arguments for Array::as_slice: " ++ show ops
array_as_slice_impl _ = Nothing
array_as_slice :: (ExplodedDefId, CustomRHS)
array_as_slice = ( ["crucible","array","{impl}", "as_slice"], array_as_slice_impl )
array_as_mut_slice :: (ExplodedDefId, CustomRHS)
array_as_mut_slice = ( ["crucible","array","{impl}", "as_mut_slice"], array_as_slice_impl )
-----------------------------------------------------------------------------------------------------
-- ** Custom: Any
-- Methods for crucible::any::Any (which has custom representation)
any_new :: (ExplodedDefId, CustomRHS)
any_new = ( ["core", "crucible", "any", "{impl}", "new"], \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp tpr e] -> do
return $ MirExp C.AnyRepr $ R.App $ E.PackAny tpr e
_ -> mirFail $ "bad arguments for Any::new: " ++ show ops
_ -> Nothing
)
any_downcast :: (ExplodedDefId, CustomRHS)
any_downcast = ( ["core", "crucible", "any", "{impl}", "downcast"], \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp C.AnyRepr e] -> do
Some tpr <- tyToReprM t
let maybeVal = R.App $ E.UnpackAny tpr e
let errMsg = R.App $ E.StringLit $ fromString $
"failed to downcast Any as " ++ show tpr
let val = R.App $ E.FromJustValue tpr maybeVal errMsg
return $ MirExp tpr val
_ -> mirFail $ "bad arguments for Any::downcast: " ++ show ops
_ -> Nothing
)
-----------------------------------------------------------------------------------------------------
-- ** Custom: ptr
ptr_offset_impl :: CustomRHS
ptr_offset_impl = \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ref, MirExp IsizeRepr offset] ->
MirExp (MirReferenceRepr tpr) <$> mirRef_offset tpr ref offset
_ -> mirFail $ "bad arguments for ptr::offset: " ++ show ops
_ -> Nothing
ptr_offset :: (ExplodedDefId, CustomRHS)
ptr_offset = (["core", "ptr", "const_ptr", "{impl}", "offset"], ptr_offset_impl)
ptr_offset_mut :: (ExplodedDefId, CustomRHS)
ptr_offset_mut = (["core", "ptr", "mut_ptr", "{impl}", "offset"], ptr_offset_impl)
ptr_wrapping_offset_impl :: CustomRHS
ptr_wrapping_offset_impl = \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ref, MirExp IsizeRepr offset] ->
MirExp (MirReferenceRepr tpr) <$> mirRef_offsetWrap tpr ref offset
_ -> mirFail $ "bad arguments for ptr::wrapping_offset: " ++ show ops
_ -> Nothing
ptr_wrapping_offset :: (ExplodedDefId, CustomRHS)
ptr_wrapping_offset =
(["core", "ptr", "const_ptr", "{impl}", "wrapping_offset"], ptr_wrapping_offset_impl)
ptr_wrapping_offset_mut :: (ExplodedDefId, CustomRHS)
ptr_wrapping_offset_mut =
(["core", "ptr", "mut_ptr", "{impl}", "wrapping_offset"], ptr_wrapping_offset_impl)
ptr_offset_from_impl :: CustomRHS
ptr_offset_from_impl = \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr1) ref1, MirExp (MirReferenceRepr tpr2) ref2]
| Just Refl <- testEquality tpr1 tpr2 -> do
maybeOffset <- mirRef_tryOffsetFrom ref1 ref2
let errMsg = R.App $ E.StringLit $ fromString $
"tried to subtract pointers into different arrays"
let val = R.App $ E.FromJustValue IsizeRepr maybeOffset errMsg
return $ MirExp IsizeRepr val
_ -> mirFail $ "bad arguments for ptr::offset_from: " ++ show ops
_ -> Nothing
ptr_offset_from :: (ExplodedDefId, CustomRHS)
ptr_offset_from = (["core", "ptr", "const_ptr", "{impl}", "offset_from"], ptr_offset_from_impl)
ptr_offset_from_mut :: (ExplodedDefId, CustomRHS)
ptr_offset_from_mut = (["core", "ptr", "mut_ptr", "{impl}", "offset_from"], ptr_offset_from_impl)
sub_ptr :: (ExplodedDefId, CustomRHS)
sub_ptr = (["core", "ptr", "const_ptr", "{impl}", "sub_ptr"], ptr_offset_from_impl)
sub_ptr_mut :: (ExplodedDefId, CustomRHS)
sub_ptr_mut = (["core", "ptr", "mut_ptr", "{impl}", "sub_ptr"], ptr_offset_from_impl)
ptr_compare_usize :: (ExplodedDefId, CustomRHS)
ptr_compare_usize = (["core", "crucible", "ptr", "compare_usize"],
\substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ptr, MirExp UsizeRepr val] -> do
valAsPtr <- integerToMirRef tpr val
MirExp C.BoolRepr <$> mirRef_eq ptr valAsPtr
[MirExp (MirSliceRepr tpr) slice, MirExp UsizeRepr val] -> do
let ptr = getSlicePtr slice
valAsPtr <- integerToMirRef tpr val
MirExp C.BoolRepr <$> mirRef_eq ptr valAsPtr
-- TODO: `&dyn Tr` case (after defining MirDynRepr)
_ -> mirFail $ "bad arguments for ptr::compare_usize: " ++ show ops
_ -> Nothing)
is_aligned_and_not_null :: (ExplodedDefId, CustomRHS)
-- Not an actual intrinsic, so it's not in an `extern` block, so it doesn't
-- have the "" element in its path.
is_aligned_and_not_null = (["core", "intrinsics", "is_aligned_and_not_null"],
-- TODO (layout): correct behavior is to return `True` for all valid
-- references, and check `i != 0 && i % align_of::<T>() == 0` for
-- `MirReference_Integer i`. However, `align_of` is not implementable
-- until we start gathering layout information from mir-json.
\_substs -> Just $ CustomOp $ \_ _ -> return $ MirExp C.BoolRepr $ R.App $ E.BoolLit True)
ptr_slice_from_raw_parts_impl :: CustomRHS
ptr_slice_from_raw_parts_impl = \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ptr, MirExp UsizeRepr len] ->
return $ MirExp (MirSliceRepr tpr) (mkSlice tpr ptr len)
_ -> mirFail $ "bad arguments for ptr::slice_from_raw_parts: " ++ show ops
_ -> Nothing
ptr_slice_from_raw_parts :: (ExplodedDefId, CustomRHS)
ptr_slice_from_raw_parts =
( ["core", "ptr", "slice_from_raw_parts"]
, ptr_slice_from_raw_parts_impl)
ptr_slice_from_raw_parts_mut :: (ExplodedDefId, CustomRHS)
ptr_slice_from_raw_parts_mut =
( ["core", "ptr", "slice_from_raw_parts_mut"]
, ptr_slice_from_raw_parts_impl)
ptr_read :: (ExplodedDefId, CustomRHS)
ptr_read = ( ["core", "ptr", "read"], \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ptr] ->
MirExp tpr <$> readMirRef tpr ptr
_ -> mirFail $ "bad arguments for ptr::read: " ++ show ops
_ -> Nothing)
ptr_write :: (ExplodedDefId, CustomRHS)
ptr_write = ( ["core", "ptr", "write"], \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ptr, MirExp tpr' val]
| Just Refl <- testEquality tpr tpr' -> do
writeMirRef ptr val
return $ MirExp C.UnitRepr $ R.App E.EmptyApp
_ -> mirFail $ "bad arguments for ptr::write: " ++ show ops
_ -> Nothing)
ptr_swap :: (ExplodedDefId, CustomRHS)
ptr_swap = ( ["core", "ptr", "swap"], \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) ptr1, MirExp (MirReferenceRepr tpr') ptr2]
| Just Refl <- testEquality tpr tpr' -> do
x1 <- readMirRef tpr ptr1
x2 <- readMirRef tpr ptr2
writeMirRef ptr1 x2
writeMirRef ptr2 x1
return $ MirExp C.UnitRepr $ R.App E.EmptyApp
_ -> mirFail $ "bad arguments for ptr::swap: " ++ show ops
_ -> Nothing)
ptr_null :: (ExplodedDefId, CustomRHS)
ptr_null = ( ["core", "ptr", "null", "crucible_null_hook"]
, null_ptr_impl "ptr::null" )
ptr_null_mut :: (ExplodedDefId, CustomRHS)
ptr_null_mut = ( ["core", "ptr", "null_mut", "crucible_null_hook"]
, null_ptr_impl "ptr::null_mut" )
null_ptr_impl :: String -> CustomRHS
null_ptr_impl what substs = case substs of
Substs [ty] -> Just $ CustomOp $ \_ ops -> case ops of
[] -> do
Some tpr <- tyToReprM ty
ref <- integerToMirRef tpr $ R.App $ eBVLit knownNat 0
return $ MirExp (MirReferenceRepr tpr) ref
_ -> mirFail $ "expected no arguments for " ++ what ++ ", received: " ++ show ops
_ -> Nothing
intrinsics_copy :: (ExplodedDefId, CustomRHS)
intrinsics_copy = ( ["core", "intrinsics", "copy"], \substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) src,
MirExp (MirReferenceRepr tpr') dest,
MirExp UsizeRepr count]
| Just Refl <- testEquality tpr tpr' -> do
-- `copy` (as opposed to `copy_nonoverlapping`) must work
-- atomically even when the source and dest overlap. We do this by
-- taking a snapshot of the source, then copying the snapshot into
-- dest.
(srcVec, srcIdx) <- mirRef_peelIndex tpr src
srcSnapVec <- readMirRef (MirVectorRepr tpr) srcVec
srcSnapRoot <- constMirRef (MirVectorRepr tpr) srcSnapVec
srcSnap <- subindexRef tpr srcSnapRoot srcIdx
ptrCopy tpr srcSnap dest count
return $ MirExp C.UnitRepr $ R.App E.EmptyApp
_ -> mirFail $ "bad arguments for intrinsics::copy: " ++ show ops
_ -> Nothing)
intrinsics_copy_nonoverlapping :: (ExplodedDefId, CustomRHS)
intrinsics_copy_nonoverlapping = ( ["core", "intrinsics", "copy_nonoverlapping"],
\substs -> case substs of
Substs [_] -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (MirReferenceRepr tpr) src,
MirExp (MirReferenceRepr tpr') dest,
MirExp UsizeRepr count]
| Just Refl <- testEquality tpr tpr' ->
copyNonOverlapping tpr src dest count
_ -> mirFail $ "bad arguments for intrinsics::copy_nonoverlapping: " ++ show ops
_ -> Nothing)
-----------------------------------------------------------------------------------------------------
-- ** Custom: wrapping_mul
-- Note the naming: `overflowing` means `T -> T -> T`, with the output wrapped
-- mod 2^N. `with_overflow` means `T -> T -> (T, Bool)`, returning both the
-- wrapped output and an overflow flag.
makeOverflowingArith :: String -> BinOp -> CustomRHS
makeOverflowingArith name bop =
\_substs -> Just $ CustomOp $ \opTys ops -> case (opTys, ops) of
([TyUint _, TyUint _], [e1, e2]) ->
fst <$> evalBinOp bop (Just Unsigned) e1 e2
([TyInt _, TyInt _], [e1, e2]) ->
fst <$> evalBinOp bop (Just Signed) e1 e2
_ -> mirFail $ "bad arguments to " ++ name ++ ": " ++ show (opTys, ops)
wrapping_add :: (ExplodedDefId, CustomRHS)
wrapping_add =
( ["core","intrinsics", "", "wrapping_add"]
, makeOverflowingArith "wrapping_add" Add
)
wrapping_sub :: (ExplodedDefId, CustomRHS)
wrapping_sub =
( ["core","intrinsics", "", "wrapping_sub"]
, makeOverflowingArith "wrapping_sub" Sub
)
wrapping_mul :: (ExplodedDefId, CustomRHS)
wrapping_mul =
( ["core","intrinsics", "", "wrapping_mul"]
, makeOverflowingArith "wrapping_mul" Mul
)
overflowResult :: C.TypeRepr tp ->
R.Expr MIR s tp ->
R.Expr MIR s C.BoolType ->
MirGenerator h s ret (MirExp s)
overflowResult tpr value over = return $ buildTuple
[ MirExp (C.MaybeRepr tpr) $ R.App $ E.JustValue tpr value
, MirExp (C.MaybeRepr C.BoolRepr) $ R.App $ E.JustValue C.BoolRepr over
]
makeArithWithOverflow :: String -> Maybe Bool -> BinOp -> CustomRHS
makeArithWithOverflow name isSignedOverride bop =
\(Substs [t]) -> Just $ CustomOp $ \_opTys ops -> case ops of
[e1, e2] -> do
let arithType = fmap (\s -> if s then Signed else Unsigned) $ isSigned t
(result, overflow) <- evalBinOp bop arithType e1 e2
case result of
MirExp (C.BVRepr w) result' ->
overflowResult (C.BVRepr w) result' overflow
MirExp tpr _ -> mirFail $
"bad return values from evalBinOp " ++ show bop ++ ": " ++ show tpr
_ -> mirFail $ "bad arguments to " ++ name ++ ": " ++ show (t, ops)
where
isSigned _ | Just s <- isSignedOverride = Just s
isSigned (TyInt _) = Just True
isSigned (TyUint _) = Just False
-- Includes `Bv<_>` support so that `makeArithWithOverflow` can also be
-- used to implement `Bv::overflowing_add` etc.
isSigned (CTyBv _) = Just False
isSigned _ = Nothing
add_with_overflow :: (ExplodedDefId, CustomRHS)
add_with_overflow =
( ["core","intrinsics", "{extern}", "add_with_overflow"]
, makeArithWithOverflow "add_with_overflow" Nothing Add
)
sub_with_overflow :: (ExplodedDefId, CustomRHS)
sub_with_overflow =
( ["core","intrinsics", "{extern}", "sub_with_overflow"]
, makeArithWithOverflow "sub_with_overflow" Nothing Sub
)
mul_with_overflow :: (ExplodedDefId, CustomRHS)
mul_with_overflow =
( ["core","intrinsics", "{extern}", "mul_with_overflow"]
, makeArithWithOverflow "mul_with_overflow" Nothing Mul
)
saturatingResultBV :: (1 <= w) => NatRepr w ->
R.Expr MIR s (C.BVType w) ->
R.Expr MIR s (C.BVType w) ->
R.Expr MIR s C.BoolType ->
MirGenerator h s ret (MirExp s)
saturatingResultBV w satValue value over = return $ MirExp (C.BVRepr w) $
R.App $ E.BVIte over w satValue value
saturateValueUnsigned :: (1 <= w) => NatRepr w -> BinOp -> Maybe (R.Expr MIR s (C.BVType w))
saturateValueUnsigned w Add = Just $ R.App $ eBVLit w (shift 1 (fromInteger $ C.intValue w) - 1)
saturateValueUnsigned w Sub = Just $ R.App $ eBVLit w 0
saturateValueUnsigned w Mul = Just $ R.App $ eBVLit w (shift 1 (fromInteger $ C.intValue w) - 1)
saturateValueUnsigned w _ = Nothing
saturateValueSigned :: (1 <= w) => NatRepr w -> BinOp -> R.Expr MIR s C.BoolType -> Maybe (R.Expr MIR s (C.BVType w))
saturateValueSigned w op pos = case op of
Add -> Just $ R.App $ E.BVIte pos w maxVal minVal
Sub -> Just $ R.App $ E.BVIte pos w minVal maxVal
_ -> Nothing
where
bits = fromIntegral $ C.intValue w
maxVal = R.App $ eBVLit w ((1 `shift` (bits - 1)) - 1)
minVal = R.App $ eBVLit w (negate $ 1 `shift` (bits - 1))
makeSaturatingArith :: String -> BinOp -> CustomRHS
makeSaturatingArith name bop =
\_substs -> Just $ CustomOp $ \opTys ops -> case (opTys, ops) of
([TyUint _, TyUint _], [e1, e2]) -> do
(result, overflow) <- evalBinOp bop (Just Unsigned) e1 e2
case result of
MirExp (C.BVRepr w) result' -> do
satValue <- case saturateValueUnsigned w bop of
Just x -> return x
Nothing -> mirFail $ "not yet implemented: unsigned saturating " ++ show bop
saturatingResultBV w satValue result' overflow
MirExp tpr _ -> mirFail $
"bad return values from evalBinOp " ++ show bop ++ ": " ++ show tpr
([TyInt _, TyInt _], [e1, e2]) -> do
(result, overflow) <- evalBinOp bop (Just Signed) e1 e2
pos <- isPos e2
case result of
MirExp (C.BVRepr w) result' -> do
satValue <- case saturateValueSigned w bop pos of
Just x -> return x
Nothing -> mirFail $ "not yet implemented: signed saturating " ++ show bop
saturatingResultBV w satValue result' overflow
MirExp tpr _ -> mirFail $
"bad return values from evalBinOp " ++ show bop ++ ": " ++ show tpr
_ -> mirFail $ "bad arguments to " ++ name ++ ": " ++ show (opTys, ops)
where
isPos :: MirExp s -> MirGenerator h s ret (R.Expr MIR s C.BoolType)
isPos (MirExp (C.BVRepr w) e) = return $ R.App $ E.BVSle w (R.App $ eBVLit w 0) e
isPos (MirExp tpr _) = mirFail $ name ++ ": expected BVRepr, but got " ++ show tpr
saturating_add :: (ExplodedDefId, CustomRHS)
saturating_add =
( ["core","intrinsics", "{extern}", "saturating_add"]
, makeSaturatingArith "saturating_add" Add
)
saturating_sub :: (ExplodedDefId, CustomRHS)
saturating_sub =
( ["core","intrinsics", "{extern}", "saturating_sub"]
, makeSaturatingArith "saturating_sub" Sub
)
-- | Common implementation for `unchecked_add` and related intrinsics. These
-- all perform the normal arithmetic operation, but overflow is undefined
-- behavior.
makeUncheckedArith :: String -> BinOp -> CustomRHS
makeUncheckedArith name bop =
\_substs -> Just $ CustomOp $ \opTys ops -> case (opTys, ops) of
([TyUint _, TyUint _], [e1, e2]) -> do
(result, overflow) <- evalBinOp bop (Just Unsigned) e1 e2
G.assertExpr (R.App $ E.Not overflow) $
S.litExpr $ Text.pack $ "undefined behavior: " ++ name ++ " overflowed"
return result
([TyInt _, TyInt _], [e1, e2]) -> do
(result, overflow) <- evalBinOp bop (Just Signed) e1 e2
G.assertExpr (R.App $ E.Not overflow) $
S.litExpr $ Text.pack $ "undefined behavior: " ++ name ++ " overflowed"
return result
_ -> mirFail $ "bad arguments to " ++ name ++ ": " ++ show (opTys, ops)
unchecked_add :: (ExplodedDefId, CustomRHS)
unchecked_add =
( ["core","intrinsics", "{extern}", "unchecked_add"]
, makeUncheckedArith "unchecked_add" Add
)
unchecked_sub :: (ExplodedDefId, CustomRHS)
unchecked_sub =
( ["core","intrinsics", "{extern}", "unchecked_sub"]
, makeUncheckedArith "unchecked_sub" Sub
)
unchecked_mul :: (ExplodedDefId, CustomRHS)
unchecked_mul =
( ["core","intrinsics", "{extern}", "unchecked_mul"]
, makeUncheckedArith "unchecked_mul" Mul
)
unchecked_div :: (ExplodedDefId, CustomRHS)
unchecked_div =
( ["core","intrinsics", "{extern}", "unchecked_div"]
, makeUncheckedArith "unchecked_div" Div
)
unchecked_rem :: (ExplodedDefId, CustomRHS)
unchecked_rem =
( ["core","intrinsics", "{extern}", "unchecked_rem"]
, makeUncheckedArith "unchecked_rem" Rem
)
unchecked_shl :: (ExplodedDefId, CustomRHS)
unchecked_shl =
( ["core","intrinsics", "{extern}", "unchecked_shl"]
, makeUncheckedArith "unchecked_shl" Shl
)
unchecked_shr :: (ExplodedDefId, CustomRHS)
unchecked_shr =
( ["core","intrinsics", "{extern}", "unchecked_shr"]
, makeUncheckedArith "unchecked_shr" Shr
)
-- Build a "count leading zeros" implementation. The function will be
-- polymorphic, accepting bitvectors of any width. The `NatRepr` is the width
-- of the output, or `Nothing` to return a bitvector of the same width as the
-- input.
ctlz_impl :: Text -> Maybe (Some NatRepr) -> CustomRHS
ctlz_impl name optFixedWidth _substs = Just $ CustomOp $ \_optys ops -> case ops of
[MirExp (C.BVRepr w) v] -> case optFixedWidth of
Nothing ->
return $ MirExp (C.BVRepr w) $ S.app $ buildMux w w w v
Just (Some w')
| Just LeqProof <- isPosNat w' ->
return $ MirExp (C.BVRepr w') $ S.app $ buildMux w w w' v
| otherwise -> error $ "bad output width "++ show w' ++ " for ctlz_impl"
_ -> mirFail $ "BUG: invalid arguments to " ++ Text.unpack name ++ ": " ++ show ops
where
getBit :: (1 <= w, i + 1 <= w) =>
NatRepr w -> NatRepr i ->
R.Expr MIR s (C.BVType w) ->
E.App MIR (R.Expr MIR s) C.BoolType
getBit w i bv =
E.BVNonzero knownRepr $ R.App $
E.BVSelect i (knownNat @1) w $ bv
-- Build a mux tree that computes the number of leading zeros in `bv`,
-- assuming that all bits at positions >= i are already known to be zero.
-- The result is returned as a bitvector of width `w'`.
buildMux :: (1 <= w, i <= w, 1 <= w') =>
NatRepr w -> NatRepr i -> NatRepr w' ->
R.Expr MIR s (C.BVType w) ->
E.App MIR (R.Expr MIR s) (C.BVType w')
buildMux w i w' bv = case isZeroNat i of
ZeroNat ->
-- Bits 0..w are all known to be zero. There are `w` leading
-- zeros.
eBVLit w' $ intValue w
NonZeroNat
| i' <- predNat i
, LeqProof <- addIsLeq i' (knownNat @1)
, LeqProof <- leqTrans (leqProof i' i) (leqProof i w)
-- Bits i..w are known to be zero, so inspect bit `i-1` next.
-> E.BVIte (R.App $ getBit w i' bv) w'
(R.App $ eBVLit w' $ intValue w - intValue i)
(R.App $ buildMux w i' w' bv)
ctlz :: (ExplodedDefId, CustomRHS)
ctlz =
( ["core","intrinsics", "{extern}", "ctlz"]
, ctlz_impl "ctlz" Nothing )
ctlz_nonzero :: (ExplodedDefId, CustomRHS)
ctlz_nonzero =
( ["core","intrinsics", "{extern}", "ctlz_nonzero"]
, ctlz_impl "ctlz_nonzero" Nothing )
rotate_left :: (ExplodedDefId, CustomRHS)
rotate_left = ( ["core","intrinsics", "", "rotate_left"],
\_substs -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.BVRepr w) eVal, MirExp (C.BVRepr w') eAmt]
| Just Refl <- testEquality w w' -> do
return $ MirExp (C.BVRepr w) $ R.App $ E.BVRol w eVal eAmt
_ -> mirFail $ "invalid arguments for rotate_left")
rotate_right :: (ExplodedDefId, CustomRHS)
rotate_right = ( ["core","intrinsics", "", "rotate_right"],
\_substs -> Just $ CustomOp $ \_ ops -> case ops of
[MirExp (C.BVRepr w) eVal, MirExp (C.BVRepr w') eAmt]
| Just Refl <- testEquality w w' -> do
return $ MirExp (C.BVRepr w) $ R.App $ E.BVRor w eVal eAmt
_ -> mirFail $ "invalid arguments for rotate_right")
---------------------------------------------------------------------------------------
-- ** Custom ::intrinsics::discriminant_value
discriminant_value :: (ExplodedDefId, CustomRHS)
discriminant_value = (["core","intrinsics", "", "discriminant_value"],
\ _substs -> Just $ CustomOp $ \ opTys ops ->
case (opTys,ops) of
([TyRef (TyAdt nm _ _) Immut], [eRef]) -> do
adt <- findAdt nm
e <- derefExp eRef >>= readPlace
MirExp tp' e' <- enumDiscriminant adt e
case testEquality tp' IsizeRepr of
Just Refl ->
return $ MirExp (C.BVRepr (knownRepr :: NatRepr 64)) $
isizeToBv knownRepr R.App e'
Nothing ->
mirFail "unexpected non-isize discriminant"
_ -> mirFail $ "BUG: invalid arguments for discriminant_value")
type_id :: (ExplodedDefId, CustomRHS)
type_id = (["core","intrinsics", "", "type_id"],
\ _substs -> Just $ CustomOp $ \ opTys ops ->
-- TODO: keep a map from Ty to Word64, assigning IDs on first use of each type
return $ MirExp knownRepr $ R.App (eBVLit (knownRepr :: NatRepr 64) 0))
size_of :: (ExplodedDefId, CustomRHS)
size_of = (["core", "intrinsics", "", "size_of"], \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ _ ->
-- TODO: return the actual size, once mir-json exports size/layout info
return $ MirExp UsizeRepr $ R.App $ usizeLit 1
)
min_align_of :: (ExplodedDefId, CustomRHS)
min_align_of = (["core", "intrinsics", "", "min_align_of"], \substs -> case substs of
Substs [t] -> Just $ CustomOp $ \_ _ ->
-- TODO: return the actual alignment, once mir-json exports size/layout info
return $ MirExp UsizeRepr $ R.App $ usizeLit 1
)
-- mem::swap is used pervasively (both directly and via mem::replace), but it
-- has a nasty unsafe implementation, with lots of raw pointers and
-- reintepreting casts. Fortunately, it requires `T: Sized`, so it's almost
-- trivial to implement as a custom op.
mem_swap :: (ExplodedDefId, CustomRHS)
mem_swap = (["core","mem", "swap"],
\ _substs -> Just $ CustomOp $ \ opTys ops -> case ops of
[MirExp (MirReferenceRepr ty1) e1, MirExp (MirReferenceRepr ty2) e2]
| Just Refl <- testEquality ty1 ty2 -> do
val1 <- readMirRef ty1 e1
val2 <- readMirRef ty2 e2
writeMirRef e1 val2
writeMirRef e2 val1
return $ MirExp knownRepr $ R.App E.EmptyApp
_ -> mirFail $ "bad arguments to mem_swap: " ++ show (opTys, ops)
)
-- This is like normal mem::transmute, but requires source and target types to
-- have identical Crucible `TypeRepr`s.
mem_crucible_identity_transmute :: (ExplodedDefId, CustomRHS)
mem_crucible_identity_transmute = (["core","mem", "crucible_identity_transmute"],
\ substs -> case substs of
Substs [tyT, tyU] -> Just $ CustomOp $ \ _ ops -> case ops of
[e@(MirExp argTy _)] -> do
Some retTy <- tyToReprM tyU
case testEquality argTy retTy of
Just refl -> return e
Nothing -> mirFail $ "crucible_identity_transmute: types are not compatible: " ++
show (tyT, argTy) ++ " != " ++ show (tyU, retTy)
_ -> mirFail $ "bad arguments to mem_crucible_identity_transmute: "
++ show (tyT, tyU, ops)
_ -> Nothing
)
mem_transmute :: (ExplodedDefId, CustomRHS)
mem_transmute = (["core", "intrinsics", "{extern}", "transmute"],
\ substs -> case substs of
Substs [tyT, tyU] -> Just $ CustomOp $ \ _ ops -> case ops of
[e@(MirExp argTy _)] -> do
Some retTy <- tyToReprM tyU
case testEquality argTy retTy of
Just Refl -> return e
Nothing -> mirFail $
"representation mismatch in transmute: " ++ show argTy ++ " != " ++ show retTy
_ -> mirFail $ "bad arguments to transmute: "
++ show (tyT, tyU, ops)
_ -> Nothing)
intrinsics_assume :: (ExplodedDefId, CustomRHS)