/
Expr.hs
1576 lines (1342 loc) · 52.6 KB
/
Expr.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
{- |
Module : Lang.Crucible.CFG.Expr
Description : Expression syntax definitions
Copyright : (c) Galois, Inc 2014-2016
License : BSD3
Maintainer : Joe Hendrix <jhendrix@galois.com>
Define the syntax of Crucible expressions. Expressions represent
side-effect free computations that result in terms. The same
expression language is used both for registerized CFGs ("Lang.Crucible.CFG.Reg")
and for the core SSA-form CFGs ("Lang.Crucible.CFG.Core").
Evaluation of expressions is defined in module "Lang.Crucible.Simulator.Evaluation".
-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- This option is here because, without it, GHC takes an extremely
-- long time (forever?) to compile this module with profiling enabled.
-- The SpecConstr optimization appears to be the culprit, and this
-- option disables it. Perhaps we only need to disable this
-- optimization on profiling builds?
{-# OPTIONS_GHC -fno-spec-constr #-}
module Lang.Crucible.CFG.Expr
( -- * App
App(..)
, mapApp
, foldApp
, traverseApp
, pattern BoolEq
, pattern IntEq
, pattern RealEq
, pattern BVEq
, pattern BoolIte
, pattern IntIte
, pattern RealIte
, pattern BVIte
-- * Base terms
, BaseTerm(..)
, module Lang.Crucible.CFG.Extension
, RoundingMode(..)
, testVector
, compareVector
) where
import Control.Monad.Identity
import Control.Monad.State.Strict
import qualified Data.BitVector.Sized as BV
import Data.Kind (Type)
import Data.Vector (Vector)
import Numeric.Natural
import Prettyprinter
import qualified Data.Vector as V
import qualified GHC.Float as F
import Data.Parameterized.Classes
import qualified Data.Parameterized.Context as Ctx
import qualified Data.Parameterized.TH.GADT as U
import Data.Parameterized.TraversableFC
import What4.Interface (RoundingMode(..),StringLiteral(..), stringLiteralInfo)
import What4.InterpretedFloatingPoint (X86_80Val(..))
import Lang.Crucible.CFG.Extension
import Lang.Crucible.FunctionHandle
import Lang.Crucible.Types
import Lang.Crucible.Utils.PrettyPrint
import qualified Lang.Crucible.Utils.Structural as U
------------------------------------------------------------------------
-- BaseTerm
-- | Base terms represent the subset of expressions
-- of base types, packaged together with a run-time
-- representation of their type.
data BaseTerm (f :: CrucibleType -> Type) tp
= BaseTerm { baseTermType :: !(BaseTypeRepr tp)
, baseTermVal :: !(f (BaseToType tp))
}
instance TestEqualityFC BaseTerm where
testEqualityFC testF (BaseTerm _ x) (BaseTerm _ y) = do
Refl <- testF x y
return Refl
instance TestEquality f => TestEquality (BaseTerm f) where
testEquality = testEqualityFC testEquality
instance OrdFC BaseTerm where
compareFC cmpF (BaseTerm _ x) (BaseTerm _ y) = do
case cmpF x y of
LTF -> LTF
GTF -> GTF
EQF -> EQF
instance OrdF f => OrdF (BaseTerm f) where
compareF = compareFC compareF
instance FunctorFC BaseTerm where
fmapFC = fmapFCDefault
instance FoldableFC BaseTerm where
foldMapFC = foldMapFCDefault
instance TraversableFC BaseTerm where
traverseFC f (BaseTerm tp x) = BaseTerm tp <$> f x
------------------------------------------------------------------------
-- App
-- | Equality on booleans
pattern BoolEq :: () => (tp ~ BoolType) => f BoolType -> f BoolType -> App ext f tp
pattern BoolEq x y = BaseIsEq BaseBoolRepr x y
-- | Equality on integers
pattern IntEq :: () => (tp ~ BoolType) => f IntegerType -> f IntegerType -> App ext f tp
pattern IntEq x y = BaseIsEq BaseIntegerRepr x y
-- | Equality on real numbers.
pattern RealEq :: () => (tp ~ BoolType) => f RealValType -> f RealValType -> App ext f tp
pattern RealEq x y = BaseIsEq BaseRealRepr x y
-- | Equality on bitvectors
pattern BVEq :: () => (1 <= w, tp ~ BoolType) => NatRepr w -> f (BVType w) -> f (BVType w) -> App ext f tp
pattern BVEq w x y = BaseIsEq (BaseBVRepr w) x y
-- | Return first or second value depending on condition.
pattern BoolIte :: () => (tp ~ BoolType) => f BoolType -> f tp -> f tp -> App ext f tp
pattern BoolIte c x y = BaseIte BaseBoolRepr c x y
-- | Return first or second value depending on condition.
pattern IntIte :: () => (tp ~ IntegerType) => f BoolType -> f tp -> f tp -> App ext f tp
pattern IntIte c x y = BaseIte BaseIntegerRepr c x y
-- | Return first or second number depending on condition.
pattern RealIte :: () => (tp ~ RealValType) => f BoolType -> f tp -> f tp -> App ext f tp
pattern RealIte c x y = BaseIte BaseRealRepr c x y
-- | Return first or second value depending on condition.
pattern BVIte :: () => (1 <= w, tp ~ BVType w) => f BoolType -> NatRepr w -> f tp -> f tp -> App ext f tp
pattern BVIte c w x y = BaseIte (BaseBVRepr w) c x y
-- | The main Crucible expression datastructure, defined as a
-- multisorted algebra. Type @'App' ext f tp@ encodes the top-level
-- application of a Crucible expression. The parameter @ext@ is used
-- to indicate which syntax extension is being used via the
-- @ExprExtension@ type family. The type parameter @tp@ is a
-- type index that indicates the Crucible type of the values denoted
-- by the given expression form. Parameter @f@ is used everywhere a
-- recursive sub-expression would go. Uses of the 'App' type will
-- tie the knot through this parameter.
data App (ext :: Type) (f :: CrucibleType -> Type) (tp :: CrucibleType) where
----------------------------------------------------------------------
-- Syntax Extension
ExtensionApp :: !(ExprExtension ext f tp) -> App ext f tp
----------------------------------------------------------------------
-- Polymorphic
-- | Return true if two base types are equal.
BaseIsEq :: !(BaseTypeRepr tp)
-> !(f (BaseToType tp))
-> !(f (BaseToType tp))
-> App ext f BoolType
-- | Select one or other
BaseIte :: !(BaseTypeRepr tp)
-> !(f BoolType)
-> !(f (BaseToType tp))
-> !(f (BaseToType tp))
-> App ext f (BaseToType tp)
----------------------------------------------------------------------
-- ()
EmptyApp :: App ext f UnitType
----------------------------------------------------------------------
-- Any
-- Build an ANY type package.
PackAny :: !(TypeRepr tp)
-> !(f tp)
-> App ext f AnyType
-- Attempt to open an ANY type. Return the contained
-- value if it has the given type; otherwise return Nothing.
UnpackAny :: !(TypeRepr tp)
-> !(f AnyType)
-> App ext f (MaybeType tp)
---------------------------------------------------------------------
-- Bool
BoolLit :: !Bool -> App ext f BoolType
Not :: !(f BoolType)
-> App ext f BoolType
And :: !(f BoolType)
-> !(f BoolType)
-> App ext f BoolType
Or :: !(f BoolType)
-> !(f BoolType)
-> App ext f BoolType
-- Exclusive or of Boolean values.
BoolXor :: !(f BoolType)
-> !(f BoolType)
-> App ext f BoolType
----------------------------------------------------------------------
-- Nat
-- @NatLit n@ returns the value n.
NatLit :: !Natural -> App ext f NatType
-- Equality for natural numbers
NatEq :: !(f NatType) -> !(f NatType) -> App ext f BoolType
-- If/Then/Else on natural numbers
NatIte :: !(f BoolType) -> !(f NatType) -> !(f NatType) -> App ext f NatType
-- Less than on natural numbers.
NatLt :: !(f NatType) -> !(f NatType) -> App ext f BoolType
-- Less than or equal on natural numbers.
NatLe :: !(f NatType) -> !(f NatType) -> App ext f BoolType
-- Add two natural numbers.
NatAdd :: !(f NatType) -> !(f NatType) -> App ext f NatType
-- @NatSub x y@ equals @x - y@.
-- The result is undefined if the @x@ is less than @y@.
NatSub :: !(f NatType) -> !(f NatType) -> App ext f NatType
-- Multiply two natural numbers.
NatMul :: !(f NatType) -> !(f NatType) -> App ext f NatType
-- Divide two natural numbers. Undefined if the divisor is 0.
NatDiv :: !(f NatType) -> !(f NatType) -> App ext f NatType
-- Modular reduction on natural numbers. Undefined if the modulus is 0.
NatMod :: !(f NatType) -> !(f NatType) -> App ext f NatType
----------------------------------------------------------------------
-- Integer
-- Create a singleton real array from a numeric literal.
IntLit :: !Integer -> App ext f IntegerType
-- Less-than test on integers
IntLt :: !(f IntegerType) -> !(f IntegerType) -> App ext f BoolType
-- Less-than-or-equal test on integers
IntLe :: !(f IntegerType) -> !(f IntegerType) -> App ext f BoolType
-- Negation of an integer value
IntNeg :: !(f IntegerType) -> App ext f IntegerType
-- Add two integers.
IntAdd :: !(f IntegerType) -> !(f IntegerType) -> App ext f IntegerType
-- Subtract one integer from another.
IntSub :: !(f IntegerType) -> !(f IntegerType) -> App ext f IntegerType
-- Multiply two integers.
IntMul :: !(f IntegerType) -> !(f IntegerType) -> App ext f IntegerType
-- Divide two integers. Undefined if the divisor is 0.
IntDiv :: !(f IntegerType) -> !(f IntegerType) -> App ext f IntegerType
-- Modular reduction on integers. Undefined if the modulus is 0.
IntMod :: !(f IntegerType) -> !(f IntegerType) -> App ext f IntegerType
-- Integer absolute value
IntAbs :: !(f IntegerType) -> App ext f IntegerType
----------------------------------------------------------------------
-- RealVal
-- A real constant
RationalLit :: !Rational -> App ext f RealValType
RealLt :: !(f RealValType) -> !(f RealValType) -> App ext f BoolType
RealLe :: !(f RealValType) -> !(f RealValType) -> App ext f BoolType
-- Negate a real number
RealNeg :: !(f RealValType) -> App ext f RealValType
-- Add two natural numbers.
RealAdd :: !(f RealValType) -> !(f RealValType) -> App ext f RealValType
-- Subtract one number from another.
RealSub :: !(f RealValType) -> !(f RealValType) -> App ext f RealValType
-- Multiple two numbers.
RealMul :: !(f RealValType) -> !(f RealValType) -> App ext f RealValType
-- Divide two numbers.
RealDiv :: !(f RealValType) -> !(f RealValType) -> App ext f RealValType
-- Compute the "real modulus", which is @x - y * floor(x ./ y)@ when
-- @y@ is not zero and @x@ when @y@ is zero.
RealMod :: !(f RealValType) -> !(f RealValType) -> App ext f RealValType
-- Return true if real value is integer.
RealIsInteger :: !(f RealValType) -> App ext f BoolType
----------------------------------------------------------------------
-- Float
-- | Generate an "undefined" float value. The semantics of this construct are
-- still under discussion, see crucible#366.
FloatUndef :: !(FloatInfoRepr fi) -> App ext f (FloatType fi)
-- Floating point constants
FloatLit :: !Float -> App ext f (FloatType SingleFloat)
DoubleLit :: !Double -> App ext f (FloatType DoubleFloat)
X86_80Lit :: !X86_80Val -> App ext f (FloatType X86_80Float)
FloatNaN :: !(FloatInfoRepr fi) -> App ext f (FloatType fi)
FloatPInf :: !(FloatInfoRepr fi) -> App ext f (FloatType fi)
FloatNInf :: !(FloatInfoRepr fi) -> App ext f (FloatType fi)
FloatPZero :: !(FloatInfoRepr fi) -> App ext f (FloatType fi)
FloatNZero :: !(FloatInfoRepr fi) -> App ext f (FloatType fi)
-- Arithmetic operations
FloatNeg
:: !(FloatInfoRepr fi)
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatAbs
:: !(FloatInfoRepr fi)
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatSqrt
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatAdd
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatSub
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatMul
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatDiv
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
-- Foating-point remainder of the two operands
FloatRem
:: !(FloatInfoRepr fi)
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatMin
:: !(FloatInfoRepr fi)
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatMax
:: !(FloatInfoRepr fi)
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
FloatFMA
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
-- Comparison operations
FloatEq :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatFpEq :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatGt :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatGe :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatLt :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatLe :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatNe :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatFpApart :: !(f (FloatType fi)) -> !(f (FloatType fi)) -> App ext f BoolType
FloatIte
:: !(FloatInfoRepr fi)
-> !(f BoolType)
-> !(f (FloatType fi))
-> !(f (FloatType fi))
-> App ext f (FloatType fi)
-- Conversion operations
FloatCast
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (FloatType fi'))
-> App ext f (FloatType fi)
FloatFromBinary
:: !(FloatInfoRepr fi)
-> !(f (BVType (FloatInfoToBitWidth fi)))
-> App ext f (FloatType fi)
FloatToBinary
:: (1 <= FloatInfoToBitWidth fi)
=> !(FloatInfoRepr fi)
-> !(f (FloatType fi))
-> App ext f (BVType (FloatInfoToBitWidth fi))
FloatFromBV
:: (1 <= w)
=> !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (BVType w))
-> App ext f (FloatType fi)
FloatFromSBV
:: (1 <= w)
=> !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f (BVType w))
-> App ext f (FloatType fi)
FloatFromReal
:: !(FloatInfoRepr fi)
-> !RoundingMode
-> !(f RealValType)
-> App ext f (FloatType fi)
FloatToBV
:: (1 <= w)
=> !(NatRepr w)
-> !RoundingMode
-> !(f (FloatType fi))
-> App ext f (BVType w)
FloatToSBV
:: (1 <= w)
=> !(NatRepr w)
-> !RoundingMode
-> !(f (FloatType fi))
-> App ext f (BVType w)
FloatToReal :: !(f (FloatType fi)) -> App ext f RealValType
-- Classification operations
FloatIsNaN :: !(f (FloatType fi)) -> App ext f BoolType
FloatIsInfinite :: !(f (FloatType fi)) -> App ext f BoolType
FloatIsZero :: !(f (FloatType fi)) -> App ext f BoolType
FloatIsPositive :: !(f (FloatType fi)) -> App ext f BoolType
FloatIsNegative :: !(f (FloatType fi)) -> App ext f BoolType
FloatIsSubnormal :: !(f (FloatType fi)) -> App ext f BoolType
FloatIsNormal :: !(f (FloatType fi)) -> App ext f BoolType
----------------------------------------------------------------------
-- Maybe
JustValue :: !(TypeRepr tp)
-> !(f tp)
-> App ext f (MaybeType tp)
NothingValue :: !(TypeRepr tp) -> App ext f (MaybeType tp)
-- This is a partial operation with given a maybe value returns the
-- value if is defined and otherwise fails with the given error message.
--
-- This operation should be used instead of pattern matching on a maybe
-- when you do not want an explicit error message being printed, but rather
-- want to assert that the value is defined.
FromJustValue :: !(TypeRepr tp)
-> !(f (MaybeType tp))
-> !(f (StringType Unicode))
-> App ext f tp
----------------------------------------------------------------------
-- Recursive Types
RollRecursive :: IsRecursiveType nm
=> !(SymbolRepr nm)
-> !(CtxRepr ctx)
-> !(f (UnrollType nm ctx))
-> App ext f (RecursiveType nm ctx)
UnrollRecursive
:: IsRecursiveType nm
=> !(SymbolRepr nm)
-> !(CtxRepr ctx)
-> !(f (RecursiveType nm ctx))
-> App ext f (UnrollType nm ctx)
----------------------------------------------------------------------
-- Sequences
-- Create an empty sequence
SequenceNil :: !(TypeRepr tp) -> App ext f (SequenceType tp)
-- Add a new value to the front of a sequence
SequenceCons :: !(TypeRepr tp)
-> !(f tp)
-> !(f (SequenceType tp))
-> App ext f (SequenceType tp)
-- Append two sequences
SequenceAppend :: !(TypeRepr tp)
-> !(f (SequenceType tp))
-> !(f (SequenceType tp))
-> App ext f (SequenceType tp)
-- Test if a sequence is nil
SequenceIsNil :: !(TypeRepr tp)
-> !(f (SequenceType tp))
-> App ext f BoolType
-- Return the length of a sequence
SequenceLength :: !(TypeRepr tp)
-> !(f (SequenceType tp))
-> App ext f NatType
-- Return the head of a sesquence, if it is non-nil.
SequenceHead :: !(TypeRepr tp)
-> !(f (SequenceType tp))
-> App ext f (MaybeType tp)
-- Return the tail of a sequence, if it is non-nil.
SequenceTail :: !(TypeRepr tp)
-> !(f (SequenceType tp))
-> App ext f (MaybeType (SequenceType tp))
-- Deconstruct a sequence. Return nothing if nil,
-- return the head and tail if non-nil.
SequenceUncons :: !(TypeRepr tp)
-> !(f (SequenceType tp))
-> App ext f (MaybeType (StructType (EmptyCtx ::> tp ::> SequenceType tp)))
----------------------------------------------------------------------
-- Vector
-- Vector literal.
VectorLit :: !(TypeRepr tp) -> !(Vector (f tp)) -> App ext f (VectorType tp)
-- Create an vector of constants.
VectorReplicate :: !(TypeRepr tp)
-> !(f NatType)
-> !(f tp)
-> App ext f (VectorType tp)
-- Return true if vector is empty.
VectorIsEmpty :: !(f (VectorType tp))
-> App ext f BoolType
-- Size of vector
VectorSize :: !(f (VectorType tp)) -> App ext f NatType
-- Return value stored in given entry.
VectorGetEntry :: !(TypeRepr tp)
-> !(f (VectorType tp))
-> !(f NatType)
-> App ext f tp
-- Update vector at given entry.
VectorSetEntry :: !(TypeRepr tp)
-> !(f (VectorType tp))
-> !(f NatType)
-> !(f tp)
-> App ext f (VectorType tp)
-- Cons an element onto the front of the vector
VectorCons :: !(TypeRepr tp)
-> !(f tp)
-> !(f (VectorType tp))
-> App ext f (VectorType tp)
----------------------------------------------------------------------
-- Handle
HandleLit :: !(FnHandle args ret)
-> App ext f (FunctionHandleType args ret)
-- Create a closure that captures the last argument.
Closure :: !(CtxRepr args)
-> !(TypeRepr ret)
-> !(f (FunctionHandleType (args::>tp) ret))
-> !(TypeRepr tp)
-> !(f tp)
-> App ext f (FunctionHandleType args ret)
----------------------------------------------------------------------
-- Conversions
-- @NatToInteger@ convert a natural number to an integer.
NatToInteger :: !(f NatType) -> App ext f IntegerType
-- @IntegerToReal@ convert an integer to a real.
IntegerToReal :: !(f IntegerType) -> App ext f RealValType
-- @RealRound@ rounds the real number value toward the nearest integer.
-- Ties are rounded away from 0.
RealRound :: !(f RealValType) -> App ext f IntegerType
-- @RealRound@ computes the largest integer less-or-equal to the given real number.
RealFloor :: !(f RealValType) -> App ext f IntegerType
-- @RealCeil@ computes the smallest integer greater-or-equal to the given real number.
RealCeil :: !(f RealValType) -> App ext f IntegerType
-- @IntegerToBV@ converts an integer value to a bitvector. This operations computes
-- the unique bitvector whose value is congruent to the input value modulo @2^w@.
IntegerToBV :: (1 <= w) => NatRepr w -> !(f IntegerType) -> App ext f (BVType w)
-- @RealToNat@ convert a non-negative real integer to natural number.
-- This is partial, and requires that the input be a non-negative real
-- integer.
RealToNat :: !(f RealValType) -> App ext f NatType
----------------------------------------------------------------------
-- ComplexReal
-- Create complex number from two real numbers.
Complex :: !(f RealValType) -> !(f RealValType) -> App ext f ComplexRealType
RealPart :: !(f ComplexRealType) -> App ext f RealValType
ImagPart :: !(f ComplexRealType) -> App ext f RealValType
----------------------------------------------------------------------
-- BV
-- | Generate an "undefined" bitvector value. The semantics of this construct
-- are still under discussion, see crucible#366.
BVUndef :: (1 <= w) => NatRepr w -> App ext f (BVType w)
BVLit :: (1 <= w) => NatRepr w -> BV.BV w -> App ext f (BVType w)
-- concatenate two bitvectors
BVConcat :: (1 <= u, 1 <= v, 1 <= u+v)
=> !(NatRepr u)
-> !(NatRepr v)
-> !(f (BVType u)) -- Most significant bits
-> !(f (BVType v)) -- Least significant bits
-> App ext f (BVType (u+v))
-- BVSelect idx n bv chooses bits [idx, .. , idx+n-1] from bitvector bv.
-- The resulting bitvector will have width n.
-- Index 0 denotes the least-significant bit.
BVSelect :: (1 <= w, 1 <= len, idx + len <= w)
=> !(NatRepr idx)
-> !(NatRepr len)
-> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType len)
BVTrunc :: (1 <= r, r+1 <= w)
=> !(NatRepr r)
-> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType r)
BVZext :: (1 <= w, 1 <= r, w+1 <= r)
=> !(NatRepr r)
-> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType r)
BVSext :: (1 <= w, 1 <= r, w+1 <= r)
=> !(NatRepr r)
-> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType r)
-- Complement bits in bitvector.
BVNot :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType w)
BVAnd :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVOr :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVXor :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVNeg :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType w)
BVAdd :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVSub :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVMul :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVUdiv :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
-- | This performs signed division. The result is truncated to zero.
--
-- TODO: Document semantics when divisor is zero and case of
-- minSigned w / -1 = minSigned w.
BVSdiv :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVUrem :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVSrem :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f (BVType w)
BVUle :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
BVUlt :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
BVSle :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
BVSlt :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
-- True if the unsigned addition of the two given bitvectors
-- has a carry-out; that is, if the unsigned addition overflows.
BVCarry :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
-- True if the signed addition of the two given bitvectors
-- has a signed overflow condition.
BVSCarry :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
-- True if the signed subtraction of the two given bitvectors
-- has a signed overflow condition.
BVSBorrow :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> !(f (BVType w))
-> App ext f BoolType
-- Perform a left-shift
BVShl :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w)) -- Value to shift
-> !(f (BVType w)) -- The shift amount as an unsigned integer.
-> App ext f (BVType w)
-- Perform a logical shift right
BVLshr :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w)) -- Value to shift
-> !(f (BVType w)) -- The shift amount as an unsigned integer.
-> App ext f (BVType w)
-- Perform a signed shift right (if the
BVAshr :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w)) -- Value to shift
-> !(f (BVType w)) -- The shift amount as an unsigned integer.
-> App ext f (BVType w)
-- Rotate left
BVRol :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w)) -- Value to rotate
-> !(f (BVType w)) -- The rotate amount as an unsigned integer
-> App ext f (BVType w)
-- Rotate right
BVRor :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w)) -- Value to rotate
-> !(f (BVType w)) -- The rotate amount as an unsigned integer
-> App ext f (BVType w)
-- Return the number of consecutive 0 bits in the input, starting from
-- the most significant bit position. If the input is zero, all bits are counted
-- as leading.
BVCountLeadingZeros :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType w)
-- Return the number of consecutive 0 bits in the input, starting from
-- the least significant bit position. If the input is zero, all bits are counted
-- as trailing.
BVCountTrailingZeros :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType w)
-- popcount
BVPopcount :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f (BVType w)
-- Return the minimum of the two arguments using unsigned comparisons
BVUMin ::
(1 <= w) =>
!(NatRepr w) ->
!(f (BVType w)) ->
!(f (BVType w)) ->
App ext f (BVType w)
-- Return the maximum of the two arguments using unsigned comparisons
BVUMax ::
(1 <= w) =>
!(NatRepr w) ->
!(f (BVType w)) ->
!(f (BVType w)) ->
App ext f (BVType w)
-- Return the minimum of the two arguments using signed comparisons
BVSMin ::
(1 <= w) =>
!(NatRepr w) ->
!(f (BVType w)) ->
!(f (BVType w)) ->
App ext f (BVType w)
-- Return the maximum of the two arguments using signed comparisons
BVSMax ::
(1 <= w) =>
!(NatRepr w) ->
!(f (BVType w)) ->
!(f (BVType w)) ->
App ext f (BVType w)
-- Given a Boolean, returns one if Boolean is True and zero otherwise.
BoolToBV :: (1 <= w)
=> !(NatRepr w)
-> !(f BoolType)
-> App ext f (BVType w)
-- Return the unsigned value of the given bitvector as an integer
BvToInteger :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f IntegerType
-- Return the signed value of the given bitvector as an integer
SbvToInteger :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f IntegerType
-- Return the unsigned value of the given bitvector as a nat
BvToNat :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f NatType
BVNonzero :: (1 <= w)
=> !(NatRepr w)
-> !(f (BVType w))
-> App ext f BoolType
----------------------------------------------------------------------
-- WordMap
EmptyWordMap :: (1 <= w)
=> !(NatRepr w)
-> !(BaseTypeRepr tp)
-> App ext f (WordMapType w tp)
InsertWordMap :: (1 <= w)
=> !(NatRepr w)
-> !(BaseTypeRepr tp)
-> !(f (BVType w))
-> !(f (BaseToType tp))
-> !(f (WordMapType w tp))
-> App ext f (WordMapType w tp)
LookupWordMap :: (1 <= w)
=> !(BaseTypeRepr tp)
-> !(f (BVType w))
-> !(f (WordMapType w tp))
-> App ext f (BaseToType tp)
LookupWordMapWithDefault
:: (1 <= w)
=> !(BaseTypeRepr tp)
-> !(f (BVType w))
-> !(f (WordMapType w tp))
-> !(f (BaseToType tp))
-> App ext f (BaseToType tp)
----------------------------------------------------------------------
-- Variants
InjectVariant :: !(CtxRepr ctx)
-> !(Ctx.Index ctx tp)
-> !(f tp)
-> App ext f (VariantType ctx)
ProjectVariant :: !(CtxRepr ctx)
-> !(Ctx.Index ctx tp)
-> !(f (VariantType ctx))
-> App ext f (MaybeType tp)
----------------------------------------------------------------------
-- Struct
MkStruct :: !(CtxRepr ctx)
-> !(Ctx.Assignment f ctx)
-> App ext f (StructType ctx)
GetStruct :: !(f (StructType ctx))
-> !(Ctx.Index ctx tp)
-> !(TypeRepr tp)
-> App ext f tp
SetStruct :: !(CtxRepr ctx)
-> !(f (StructType ctx))
-> !(Ctx.Index ctx tp)
-> !(f tp)
-> App ext f (StructType ctx)
----------------------------------------------------------------------
-- StringMapType
-- Initialize the ident value map to the given value.
EmptyStringMap :: !(TypeRepr tp)
-> App ext f (StringMapType tp)
-- Lookup the value of a string in a string map.
LookupStringMapEntry :: !(TypeRepr tp)
-> !(f (StringMapType tp))
-> !(f (StringType Unicode))
-> App ext f (MaybeType tp)
-- Update the name of the ident value map with the given value.
InsertStringMapEntry :: !(TypeRepr tp)
-> !(f (StringMapType tp))
-> !(f (StringType Unicode))
-> !(f (MaybeType tp))
-> App ext f (StringMapType tp)
----------------------------------------------------------------------
-- String
-- Create a concrete string literal
StringLit :: !(StringLiteral si)
-> App ext f (StringType si)
-- Create an empty string literal
StringEmpty :: !(StringInfoRepr si)
-> App ext f (StringType si)
StringConcat :: !(StringInfoRepr si)