@@ -1,4 +1,4 @@
1−{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
1+{-# LANGUAGE RankNTypes, ScopedTypeVariables, PatternGuards #-}
3module Idris.Primitives(elabPrims) where
3module Idris.Primitives(elabPrims) where
@@ -34,39 +34,7 @@ partial = Partial NotCovering
35primitives =
35primitives =
36 -- operators
36 -- operators
37− [Prim (UN "prim__addInt") (ty [IType, IType] IType) 2 (iBin (+))
38− (2, LPlus ITNative) total,
39− Prim (UN "prim__subInt") (ty [IType, IType] IType) 2 (iBin (-))
40− (2, LMinus ITNative) total,
41− Prim (UN "prim__mulInt") (ty [IType, IType] IType) 2 (iBin (*))
42− (2, LTimes ITNative) total,
43− Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin div)
44− (2, LSDiv ITNative) partial,
45− Prim (UN "prim__modInt") (ty [IType, IType] IType) 2 (iBin mod)
46− (2, LSRem ITNative) partial,
47− Prim (UN "prim__andInt") (ty [IType, IType] IType) 2 (iBin (.&.))
48− (2, LAnd ITNative) partial,
49− Prim (UN "prim__orInt") (ty [IType, IType] IType) 2 (iBin (.|.))
50− (2, LOr ITNative) partial,
51− Prim (UN "prim__xorInt") (ty [IType, IType] IType) 2 (iBin xor)
52− (2, LXOr ITNative) partial,
53− Prim (UN "prim__shLInt") (ty [IType, IType] IType) 2 (iBin shiftL)
54− (2, LSHL ITNative) partial,
55− Prim (UN "prim__shRInt") (ty [IType, IType] IType) 2 (iBin shiftR)
56− (2, LASHR ITNative) partial,
57− Prim (UN "prim__complInt") (ty [IType] IType) 1 (iUn complement)
58− (1, LCompl ITNative) partial,
59− Prim (UN "prim__eqInt") (ty [IType, IType] IType) 2 (biBin (==))
60− (2, LEq ITNative) total,
61− Prim (UN "prim__ltInt") (ty [IType, IType] IType) 2 (biBin (<))
62− (2, LLt ITNative) total,
63− Prim (UN "prim__lteInt") (ty [IType, IType] IType) 2 (biBin (<=))
64− (2, LLe ITNative) total,
65− Prim (UN "prim__gtInt") (ty [IType, IType] IType) 2 (biBin (>))
66− (2, LGt ITNative) total,
67− Prim (UN "prim__gteInt") (ty [IType, IType] IType) 2 (biBin (>=))
68− (2, LGe ITNative) total,
69− Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
37+ [Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
70 (2, LEq ITNative) total,
38 (2, LEq ITNative) total,
71 Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
39 Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
72 (2, LLt ITNative) total,
40 (2, LLt ITNative) total,
@@ -80,55 +48,59 @@ primitives =
80 iCoerce IT8 IT16 "zext" zext LZExt,
48 iCoerce IT8 IT16 "zext" zext LZExt,
81 iCoerce IT8 IT32 "zext" zext LZExt,
49 iCoerce IT8 IT32 "zext" zext LZExt,
82 iCoerce IT8 IT64 "zext" zext LZExt,
50 iCoerce IT8 IT64 "zext" zext LZExt,
51+ iCoerce IT8 ITBig "zext" zext LZExt,
52+ iCoerce IT8 ITNative "zext" zext LZExt,
83 iCoerce IT16 IT32 "zext" zext LZExt,
53 iCoerce IT16 IT32 "zext" zext LZExt,
84 iCoerce IT16 IT64 "zext" zext LZExt,
54 iCoerce IT16 IT64 "zext" zext LZExt,
55+ iCoerce IT16 ITBig "zext" zext LZExt,
56+ iCoerce IT16 ITNative "zext" zext LZExt,
85 iCoerce IT32 IT64 "zext" zext LZExt,
57 iCoerce IT32 IT64 "zext" zext LZExt,
58+ iCoerce IT32 ITBig "zext" zext LZExt,
59+ iCoerce IT32 ITNative "zext" zext LZExt,
60+ iCoerce IT64 ITBig "zext" zext LZExt,
61+ iCoerce ITNative ITBig "zext" zext LZExt,
62+ iCoerce ITNative IT64 "zext" zext LZExt,
63+ iCoerce ITNative IT32 "zext" zext LZExt,
64+ iCoerce ITNative IT16 "zext" zext LZExt,
87 iCoerce IT8 IT16 "sext" sext LSExt,
66 iCoerce IT8 IT16 "sext" sext LSExt,
88 iCoerce IT8 IT32 "sext" sext LSExt,
67 iCoerce IT8 IT32 "sext" sext LSExt,
89 iCoerce IT8 IT64 "sext" sext LSExt,
68 iCoerce IT8 IT64 "sext" sext LSExt,
69+ iCoerce IT8 ITBig "sext" sext LSExt,
70+ iCoerce IT8 ITNative "sext" sext LSExt,
90 iCoerce IT16 IT32 "sext" sext LSExt,
71 iCoerce IT16 IT32 "sext" sext LSExt,
91 iCoerce IT16 IT64 "sext" sext LSExt,
72 iCoerce IT16 IT64 "sext" sext LSExt,
73+ iCoerce IT16 ITBig "sext" sext LSExt,
74+ iCoerce IT16 ITNative "sext" sext LSExt,
92 iCoerce IT32 IT64 "sext" sext LSExt,
75 iCoerce IT32 IT64 "sext" sext LSExt,
76+ iCoerce IT32 ITBig "sext" sext LSExt,
77+ iCoerce IT32 ITNative "sext" sext LSExt,
78+ iCoerce IT64 ITBig "sext" sext LSExt,
79+ iCoerce ITNative ITBig "sext" sext LSExt,
80+ iCoerce ITNative ITBig "sext" sext LSExt,
81+ iCoerce ITNative IT64 "sext" sext LSExt,
82+ iCoerce ITNative IT32 "sext" sext LSExt,
83+ iCoerce ITNative IT16 "sext" sext LSExt,
94 iCoerce IT16 IT8 "trunc" trunc LTrunc,
85 iCoerce IT16 IT8 "trunc" trunc LTrunc,
95 iCoerce IT32 IT8 "trunc" trunc LTrunc,
86 iCoerce IT32 IT8 "trunc" trunc LTrunc,
96 iCoerce IT64 IT8 "trunc" trunc LTrunc,
87 iCoerce IT64 IT8 "trunc" trunc LTrunc,
88+ iCoerce ITBig IT8 "trunc" trunc LTrunc,
89+ iCoerce ITNative IT8 "trunc" trunc LTrunc,
97 iCoerce IT32 IT16 "trunc" trunc LTrunc,
90 iCoerce IT32 IT16 "trunc" trunc LTrunc,
98 iCoerce IT64 IT16 "trunc" trunc LTrunc,
91 iCoerce IT64 IT16 "trunc" trunc LTrunc,
92+ iCoerce ITBig IT16 "trunc" trunc LTrunc,
93+ iCoerce ITNative IT16 "trunc" trunc LTrunc,
99 iCoerce IT64 IT32 "trunc" trunc LTrunc,
94 iCoerce IT64 IT32 "trunc" trunc LTrunc,
95+ iCoerce ITBig IT32 "trunc" trunc LTrunc,
96+ iCoerce ITNative IT32 "trunc" trunc LTrunc,
97+ iCoerce ITBig IT64 "trunc" trunc LTrunc,
98+ iCoerce IT16 ITNative "trunc" trunc LTrunc,
99+ iCoerce IT32 ITNative "trunc" trunc LTrunc,
100+ iCoerce IT64 ITNative "trunc" trunc LTrunc,
101+ iCoerce ITBig ITNative "trunc" trunc LTrunc,
102+ iCoerce ITNative IT64 "trunc" trunc LTrunc,
101− Prim (UN "prim__intToB8") (ty [IType] B8Type) 1 intToBits8
103− Prim (UN "prim__intToB16") (ty [IType] B16Type) 1 intToBits16
105− Prim (UN "prim__intToB32") (ty [IType] B32Type) 1 intToBits32
107− Prim (UN "prim__intToB64") (ty [IType] B64Type) 1 intToBits64
109− Prim (UN "prim__B32ToInt") (ty [B32Type] IType) 1 bits32ToInt
112− Prim (UN "prim__addBigInt") (ty [BIType, BIType] BIType) 2 (bBin (+))
114− Prim (UN "prim__subBigInt") (ty [BIType, BIType] BIType) 2 (bBin (-))
116− Prim (UN "prim__mulBigInt") (ty [BIType, BIType] BIType) 2 (bBin (*))
118− Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin div)
120− Prim (UN "prim__modBigInt") (ty [BIType, BIType] BIType) 2 (bBin mod)
122− Prim (UN "prim__eqBigInt") (ty [BIType, BIType] IType) 2 (bbBin (==))
124− Prim (UN "prim__ltBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<))
126− Prim (UN "prim__lteBigInt") (ty [BIType, BIType] IType) 2 (bbBin (<=))
128− Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>))
130− Prim (UN "prim__gtBigInt") (ty [BIType, BIType] IType) 2 (bbBin (>=))
132 Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
104 Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
133 (2, LFPlus) total,
105 (2, LFPlus) total,
134 Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
106 Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
@@ -156,30 +128,14 @@ primitives =
156 Prim (UN "prim_lenString") (ty [StrType] IType) 1 (p_strLen)
128 Prim (UN "prim_lenString") (ty [StrType] IType) 1 (p_strLen)
157 (1, LStrLen) total,
129 (1, LStrLen) total,
158 -- Conversions
130 -- Conversions
159− Prim (UN "prim__strToInt") (ty [StrType] IType) 1 (c_strToInt)
161− Prim (UN "prim__intToStr") (ty [IType] StrType) 1 (c_intToStr)
163 Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
131 Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
164− (1, LChInt) total,
132+ (1, LChInt ITNative) total,
165 Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
133 Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
166− (1, LIntCh) total,
134+ (1, LIntCh ITNative) total,
167− Prim (UN "prim__intToBigInt") (ty [IType] BIType) 1 (c_intToBigInt)
169− Prim (UN "prim__bigIntToInt") (ty [BIType] IType) 1 (c_bigIntToInt)
171− Prim (UN "prim__strToBigInt") (ty [StrType] BIType) 1 (c_strToBigInt)
173− Prim (UN "prim__bigIntToStr") (ty [BIType] StrType) 1 (c_bigIntToStr)
175 Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
135 Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
176 (1, LStrFloat) total,
136 (1, LStrFloat) total,
177 Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
137 Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
178 (1, LFloatStr) total,
138 (1, LFloatStr) total,
179− Prim (UN "prim__intToFloat") (ty [IType] FlType) 1 (c_intToFloat)
180− (1, LIntFloat) total,
181− Prim (UN "prim__floatToInt") (ty [FlType] IType) 1 (c_floatToInt)
182− (1, LFloatInt) total,
184 Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
140 Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
185 (1, LFExp) total,
141 (1, LFExp) total,
@@ -223,7 +179,7 @@ primitives =
223 (0, LStdIn) partial,
179 (0, LStdIn) partial,
224 Prim (UN "prim__believe_me") believeTy 3 (p_believeMe)
180 Prim (UN "prim__believe_me") believeTy 3 (p_believeMe)
225 (3, LNoOp) partial -- ahem
181 (3, LNoOp) partial -- ahem
226− ] ++ concatMap intOps [IT8, IT16, IT32, IT64]
182+ ] ++ concatMap intOps [IT8, IT16, IT32, IT64, ITBig, ITNative]
228intOps ity =
184intOps ity =
229 [ iCmp ity "lt" (bCmp ity (<)) LLt total
185 [ iCmp ity "lt" (bCmp ity (<)) LLt total
@@ -245,13 +201,26 @@ intOps ity =
245 , iBinOp ity "or" (bitBin ity (.|.)) LOr total
201 , iBinOp ity "or" (bitBin ity (.|.)) LOr total
246 , iBinOp ity "xor" (bitBin ity (xor)) LXOr total
202 , iBinOp ity "xor" (bitBin ity (xor)) LXOr total
247 , iUnOp ity "compl" (bUn ity complement) LCompl total
203 , iUnOp ity "compl" (bUn ity complement) LCompl total
204+ , Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [intTyToConst ity] StrType) 1 intToStr
205+ (1, LIntStr ity) total
206+ , Prim (UN $ "prim__fromStr" ++ intTyName ity) (ty [StrType] (intTyToConst ity)) 1 (strToInt ity)
207+ (1, LStrInt ity) total
208+ , Prim (UN $ "prim__toFloat" ++ intTyName ity) (ty [intTyToConst ity] FlType) 1 intToFloat
209+ (1, LIntFloat ity) total
210+ , Prim (UN $ "prim__fromFloat" ++ intTyName ity) (ty [FlType] (intTyToConst ity)) 1 (floatToInt ity)
211+ (1, LFloatInt ity) total
214+intTyName :: IntTy -> String
215+intTyName ITNative = "Int"
216+intTyName ITBig = "BigInt"
217+intTyName sized = "B" ++ show (intTyWidth sized)
250−iCmp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ "B" ++ show (intTyWidth ity)) (ty (replicate 2 $ intTyToConst ity) IType) 2 impl (2, irop ity) totality
219+iCmp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ intTyName ity) (ty (replicate 2 $ intTyToConst ity) IType) 2 impl (2, irop ity) totality
251−iBinOp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ "B" ++ show (intTyWidth ity)) (ty (replicate 2 $ intTyToConst ity) (intTyToConst ity)) 2 impl (2, irop ity) totality
220+iBinOp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ intTyName ity) (ty (replicate 2 $ intTyToConst ity) (intTyToConst ity)) 2 impl (2, irop ity) totality
252−iUnOp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ "B" ++ show (intTyWidth ity)) (ty [intTyToConst ity] (intTyToConst ity)) 1 impl (1, irop ity) totality
221+iUnOp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ intTyName ity) (ty [intTyToConst ity] (intTyToConst ity)) 1 impl (1, irop ity) totality
253iCoerce from to op impl irop =
222iCoerce from to op impl irop =
254− Prim (UN $ "prim__" ++ op ++ "B" ++ show (intTyWidth from) ++ "_" ++ show (intTyWidth to))
223+ Prim (UN $ "prim__" ++ op ++ intTyName from ++ "_" ++ intTyName to)
255 (ty [intTyToConst from] (intTyToConst to)) 1 (impl from to) (1, irop from to) total
224 (ty [intTyToConst from] (intTyToConst to)) 1 (impl from to) (1, irop from to) total
257p_believeMe [_,_,x] = Just x
226p_believeMe [_,_,x] = Just x
@@ -329,6 +298,8 @@ bUn IT8 op [VConstant (B8 x)] = Just $ VConstant (B8 (op x))
329bUn IT16 op [VConstant (B16 x)] = Just $ VConstant (B16 (op x))
298bUn IT16 op [VConstant (B16 x)] = Just $ VConstant (B16 (op x))
330bUn IT32 op [VConstant (B32 x)] = Just $ VConstant (B32 (op x))
299bUn IT32 op [VConstant (B32 x)] = Just $ VConstant (B32 (op x))
331bUn IT64 op [VConstant (B64 x)] = Just $ VConstant (B64 (op x))
300bUn IT64 op [VConstant (B64 x)] = Just $ VConstant (B64 (op x))
301+bUn ITBig op [VConstant (BI x)] = Just $ VConstant (BI (op x))
302+bUn ITNative op [VConstant (I x)] = Just $ VConstant (I (op x))
332bUn _ _ _ = Nothing
303bUn _ _ _ = Nothing
334bitBin :: IntTy -> (forall a. (Bits a, Integral a) => a -> a -> a) -> [Value] -> Maybe Value
305bitBin :: IntTy -> (forall a. (Bits a, Integral a) => a -> a -> a) -> [Value] -> Maybe Value
@@ -336,6 +307,8 @@ bitBin IT8 op [VConstant (B8 x), VConstant (B8 y)] = Just $ VConstant (B8 (o
336bitBin IT16 op [VConstant (B16 x), VConstant (B16 y)] = Just $ VConstant (B16 (op x y))
307bitBin IT16 op [VConstant (B16 x), VConstant (B16 y)] = Just $ VConstant (B16 (op x y))
337bitBin IT32 op [VConstant (B32 x), VConstant (B32 y)] = Just $ VConstant (B32 (op x y))
308bitBin IT32 op [VConstant (B32 x), VConstant (B32 y)] = Just $ VConstant (B32 (op x y))
338bitBin IT64 op [VConstant (B64 x), VConstant (B64 y)] = Just $ VConstant (B64 (op x y))
309bitBin IT64 op [VConstant (B64 x), VConstant (B64 y)] = Just $ VConstant (B64 (op x y))
310+bitBin ITBig op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (BI (op x y))
311+bitBin ITNative op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
339bitBin _ _ _ = Nothing
312bitBin _ _ _ = Nothing
341bCmp :: IntTy -> (forall a. Ord a => a -> a -> Bool) -> [Value] -> Maybe Value
314bCmp :: IntTy -> (forall a. Ord a => a -> a -> Bool) -> [Value] -> Maybe Value
@@ -343,73 +316,71 @@ bCmp IT8 op [VConstant (B8 x), VConstant (B8 y)] = Just $ VConstant (I (if (o
343bCmp IT16 op [VConstant (B16 x), VConstant (B16 y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
316bCmp IT16 op [VConstant (B16 x), VConstant (B16 y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
344bCmp IT32 op [VConstant (B32 x), VConstant (B32 y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
317bCmp IT32 op [VConstant (B32 x), VConstant (B32 y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
345bCmp IT64 op [VConstant (B64 x), VConstant (B64 y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
318bCmp IT64 op [VConstant (B64 x), VConstant (B64 y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
319+bCmp ITBig op [VConstant (BI x), VConstant (BI y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
320+bCmp ITNative op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (if (op x y) then 1 else 0))
346bCmp _ _ _ = Nothing
321bCmp _ _ _ = Nothing
348−toB IT8 x = VConstant (B8 (fromIntegral x))
323+toInt IT8 x = VConstant (B8 (fromIntegral x))
349−toB IT16 x = VConstant (B16 (fromIntegral x))
324+toInt IT16 x = VConstant (B16 (fromIntegral x))
350−toB IT32 x = VConstant (B32 (fromIntegral x))
325+toInt IT32 x = VConstant (B32 (fromIntegral x))
351−toB IT64 x = VConstant (B64 (fromIntegral x))
326+toInt IT64 x = VConstant (B64 (fromIntegral x))
352−
327+toInt ITBig x = VConstant (BI (fromIntegral x))
353−zext IT8 IT16 [VConstant (B8 x)] = Just $ VConstant (B16 (fromIntegral x))
328+toInt ITNative x = VConstant (I (fromIntegral x))
354−zext IT8 IT32 [VConstant (B8 x)] = Just $ VConstant (B32 (fromIntegral x))
330+intToInt IT8 out [VConstant (B8 x)] = Just $ toInt out x
355−zext IT8 IT64 [VConstant (B8 x)] = Just $ VConstant (B64 (fromIntegral x))
331+intToInt IT16 out [VConstant (B16 x)] = Just $ toInt out x
356−zext IT16 IT32 [VConstant (B16 x)] = Just $ VConstant (B32 (fromIntegral x))
332+intToInt IT32 out [VConstant (B32 x)] = Just $ toInt out x
357−zext IT16 IT64 [VConstant (B16 x)] = Just $ VConstant (B64 (fromIntegral x))
333+intToInt IT64 out [VConstant (B64 x)] = Just $ toInt out x
358−zext IT32 IT64 [VConstant (B32 x)] = Just $ VConstant (B64 (fromIntegral x))
334+intToInt ITBig out [VConstant (BI x)] = Just $ toInt out x
335+intToInt ITNative out [VConstant (I x)] = Just $ toInt out x
336+intToInt _ _ _ = Nothing
338+zext from ITBig val = intToInt from ITBig val
339+zext ITBig _ _ = Nothing
341+ | intTyWidth from < intTyWidth to = intToInt from to val
359zext _ _ _ = Nothing
342zext _ _ _ = Nothing
361−sext IT8 IT16 [VConstant (B8 x)] = Just $ VConstant (B16 (fromIntegral (fromIntegral x :: Int8)))
344+sext IT8 out [VConstant (B8 x)] = Just $ toInt out (fromIntegral x :: Int8)
362−sext IT8 IT32 [VConstant (B8 x)] = Just $ VConstant (B32 (fromIntegral (fromIntegral x :: Int8)))
363−sext IT8 IT64 [VConstant (B8 x)] = Just $ VConstant (B64 (fromIntegral (fromIntegral x :: Int8)))
364−sext IT16 IT32 [VConstant (B16 x)] = Just $ VConstant (B32 (fromIntegral (fromIntegral x :: Int16)))
345+sext IT16 out [VConstant (B16 x)] = Just $ toInt out (fromIntegral x :: Int16)
365−sext IT16 IT64 [VConstant (B16 x)] = Just $ VConstant (B64 (fromIntegral (fromIntegral x :: Int16)))
366−sext IT32 IT64 [VConstant (B32 x)] = Just $ VConstant (B64 (fromIntegral (fromIntegral x :: Int32)))
346+sext IT32 out [VConstant (B32 x)] = Just $ toInt out (fromIntegral x :: Int32)
369−trunc IT16 IT8 [VConstant (B16 x)] = Just $ VConstant (B8 (fromIntegral x))
347+sext IT64 out [VConstant (B64 x)] = Just $ toInt out (fromIntegral x :: Int64)
370−trunc IT32 IT8 [VConstant (B32 x)] = Just $ VConstant (B8 (fromIntegral x))
348+sext ITBig _ _ = Nothing
371−trunc IT64 IT8 [VConstant (B64 x)] = Just $ VConstant (B8 (fromIntegral x))
349+sext from to val = intToInt from to val
372−trunc IT32 IT16 [VConstant (B32 x)] = Just $ VConstant (B16 (fromIntegral x))
350+
373−trunc IT64 IT16 [VConstant (B64 x)] = Just $ VConstant (B16 (fromIntegral x))
351+trunc ITBig to val = intToInt ITBig to val
374−trunc IT64 IT32 [VConstant (B64 x)] = Just $ VConstant (B32 (fromIntegral x))
352+trunc _ ITBig _ = Nothing
353+trunc from to val | intTyWidth from > intTyWidth to = intToInt from to val
375trunc _ _ _ = Nothing
354trunc _ _ _ = Nothing
377−intToBits8 [VConstant (I x)] = Just $ toB IT8 x
356+getInt :: [Value] -> Maybe Integer
378−intToBits8 _ = Nothing
379−intToBits16 [VConstant (I x)] = Just $ toB IT16 x
380−intToBits16 _ = Nothing
381−intToBits32 [VConstant (I x)] = Just $ toB IT32 x
357+getInt [VConstant (B8 x)] = Just . toInteger $ x
382−intToBits32 _ = Nothing
383−intToBits64 [VConstant (I x)] = Just $ toB IT64 x
358+getInt [VConstant (B16 x)] = Just . toInteger $ x
384−intToBits64 _ = Nothing
386−bits32ToInt [VConstant (B32 x)] = Just $ VConstant (I (fromIntegral x))
359+getInt [VConstant (B32 x)] = Just . toInteger $ x
360+getInt [VConstant (B64 x)] = Just . toInteger $ x
361+getInt [VConstant (I x)] = Just . toInteger $ x
362+getInt [VConstant (BI x)] = Just x
387−bits32ToInt _ = Nothing
363+getInt _ = Nothing
389−c_intToStr [VConstant (I x)] = Just $ VConstant (Str (show x))
365+intToStr val | Just i <- getInt val = Just $ VConstant (Str (show i))
390−c_intToStr _ = Nothing
366+intToStr _ = Nothing
391−c_strToInt [VConstant (Str x)] = case reads x of
368+strToInt ity [VConstant (Str x)] = case reads x of
392− [(n,"")] -> Just $ VConstant (I n)
369+ [(n,"")] -> Just $ toInt ity (n :: Integer)
393− _ -> Just $ VConstant (I 0)
370+ _ -> Just $ VConstant (I 0)
371+strToInt _ _ = Nothing
373+intToFloat val | Just i <- getInt val = Just $ VConstant (Fl (fromIntegral i))
374+intToFloat _ = Nothing
376+floatToInt ity [VConstant (Fl x)] = Just $ toInt ity (truncate x :: Integer)
394−c_strToInt _ = Nothing
377+floatToInt _ _ = Nothing
396c_intToChar [VConstant (I x)] = Just $ VConstant (Ch (toEnum x))
379c_intToChar [VConstant (I x)] = Just $ VConstant (Ch (toEnum x))
397c_intToChar _ = Nothing
380c_intToChar _ = Nothing
398c_charToInt [VConstant (Ch x)] = Just $ VConstant (I (fromEnum x))
381c_charToInt [VConstant (Ch x)] = Just $ VConstant (I (fromEnum x))
399c_charToInt _ = Nothing
382c_charToInt _ = Nothing
401−c_intToBigInt [VConstant (I x)] = Just $ VConstant (BI (fromIntegral x))
402−c_intToBigInt _ = Nothing
403−c_bigIntToInt [VConstant (BI x)] = Just $ VConstant (I (fromInteger x))
404−c_bigIntToInt _ = Nothing
406−c_bigIntToStr [VConstant (BI x)] = Just $ VConstant (Str (show x))
407−c_bigIntToStr _ = Nothing
408−c_strToBigInt [VConstant (Str x)] = case reads x of
409− [(n,"")] -> Just $ VConstant (BI n)
410− _ -> Just $ VConstant (BI 0)
411−c_strToBigInt _ = Nothing
413c_floatToStr [VConstant (Fl x)] = Just $ VConstant (Str (show x))
384c_floatToStr [VConstant (Fl x)] = Just $ VConstant (Str (show x))
414c_floatToStr _ = Nothing
385c_floatToStr _ = Nothing
415c_strToFloat [VConstant (Str x)] = case reads x of
386c_strToFloat [VConstant (Str x)] = case reads x of
@@ -417,12 +388,6 @@ c_strToFloat [VConstant (Str x)] = case reads x of
417 _ -> Just $ VConstant (Fl 0)
388 _ -> Just $ VConstant (Fl 0)
418c_strToFloat _ = Nothing
389c_strToFloat _ = Nothing
420−c_floatToInt [VConstant (Fl x)] = Just $ VConstant (I (truncate x))
421−c_floatToInt _ = Nothing
423−c_intToFloat [VConstant (I x)] = Just $ VConstant (Fl (fromIntegral x))
424−c_intToFloat _ = Nothing
426p_fPrim f [VConstant (Fl x)] = Just $ VConstant (Fl (f x))
391p_fPrim f [VConstant (Fl x)] = Just $ VConstant (Fl (f x))
427p_fPrim f _ = Nothing
392p_fPrim f _ = Nothing