@@ -11,6 +11,7 @@ import Core.Evaluate
11import Data.Bits
11import Data.Bits
12import Data.Word
12import Data.Word
13import Data.Int
13import Data.Int
14import qualified Data.Vector.Unboxed as V
15import qualified Data.Vector.Unboxed as V
16data Prim = Prim { p_name :: Name,
17data Prim = Prim { p_name :: Name,
@@ -32,18 +33,7 @@ partial = Partial NotCovering
32primitives :: [Prim]
33primitives :: [Prim]
33primitives =
34primitives =
34 -- operators
35 -- operators
35− [Prim (UN "prim__eqChar") (ty [ChType, ChType] (AType (ATInt ITNative))) 2 (bcBin (==))
36− (2, LEq (ATInt ITNative)) total,
37− Prim (UN "prim__ltChar") (ty [ChType, ChType] (AType (ATInt ITNative))) 2 (bcBin (<))
38− (2, LLt (ATInt ITNative)) total,
39− Prim (UN "prim__lteChar") (ty [ChType, ChType] (AType (ATInt ITNative))) 2 (bcBin (<=))
40− (2, LLe (ATInt ITNative)) total,
41− Prim (UN "prim__gtChar") (ty [ChType, ChType] (AType (ATInt ITNative))) 2 (bcBin (>))
42− (2, LGt (ATInt ITNative)) total,
43− Prim (UN "prim__gteChar") (ty [ChType, ChType] (AType (ATInt ITNative))) 2 (bcBin (>=))
44− (2, LGe (ATInt ITNative)) total,
46− iCoerce (ITFixed IT8) (ITFixed IT16) "zext" zext LZExt,
36+ [iCoerce (ITFixed IT8) (ITFixed IT16) "zext" zext LZExt,
47 iCoerce (ITFixed IT8) (ITFixed IT32) "zext" zext LZExt,
37 iCoerce (ITFixed IT8) (ITFixed IT32) "zext" zext LZExt,
48 iCoerce (ITFixed IT8) (ITFixed IT64) "zext" zext LZExt,
38 iCoerce (ITFixed IT8) (ITFixed IT64) "zext" zext LZExt,
49 iCoerce (ITFixed IT8) ITBig "zext" zext LZExt,
39 iCoerce (ITFixed IT8) ITBig "zext" zext LZExt,
@@ -60,6 +50,7 @@ primitives =
60 iCoerce ITNative (ITFixed IT64) "zext" zext LZExt,
50 iCoerce ITNative (ITFixed IT64) "zext" zext LZExt,
61 iCoerce ITNative (ITFixed IT32) "zext" zext LZExt,
51 iCoerce ITNative (ITFixed IT32) "zext" zext LZExt,
62 iCoerce ITNative (ITFixed IT16) "zext" zext LZExt,
52 iCoerce ITNative (ITFixed IT16) "zext" zext LZExt,
53+ iCoerce ITChar ITBig "zext" zext LZExt,
64 iCoerce (ITFixed IT8) (ITFixed IT16) "sext" sext LSExt,
55 iCoerce (ITFixed IT8) (ITFixed IT16) "sext" sext LSExt,
65 iCoerce (ITFixed IT8) (ITFixed IT32) "sext" sext LSExt,
56 iCoerce (ITFixed IT8) (ITFixed IT32) "sext" sext LSExt,
@@ -79,6 +70,7 @@ primitives =
79 iCoerce ITNative (ITFixed IT64) "sext" sext LSExt,
70 iCoerce ITNative (ITFixed IT64) "sext" sext LSExt,
80 iCoerce ITNative (ITFixed IT32) "sext" sext LSExt,
71 iCoerce ITNative (ITFixed IT32) "sext" sext LSExt,
81 iCoerce ITNative (ITFixed IT16) "sext" sext LSExt,
72 iCoerce ITNative (ITFixed IT16) "sext" sext LSExt,
73+ iCoerce ITChar ITBig "sext" sext LSExt,
83 iCoerce (ITFixed IT16) (ITFixed IT8) "trunc" trunc LTrunc,
75 iCoerce (ITFixed IT16) (ITFixed IT8) "trunc" trunc LTrunc,
84 iCoerce (ITFixed IT32) (ITFixed IT8) "trunc" trunc LTrunc,
76 iCoerce (ITFixed IT32) (ITFixed IT8) "trunc" trunc LTrunc,
@@ -98,6 +90,7 @@ primitives =
98 iCoerce (ITFixed IT64) ITNative "trunc" trunc LTrunc,
90 iCoerce (ITFixed IT64) ITNative "trunc" trunc LTrunc,
99 iCoerce ITBig ITNative "trunc" trunc LTrunc,
91 iCoerce ITBig ITNative "trunc" trunc LTrunc,
100 iCoerce ITNative (ITFixed IT64) "trunc" trunc LTrunc,
92 iCoerce ITNative (ITFixed IT64) "trunc" trunc LTrunc,
93+ iCoerce ITBig ITChar "trunc" trunc LTrunc,
102 Prim (UN "prim__addFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (+))
95 Prim (UN "prim__addFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (+))
103 (2, LPlus ATFloat) total,
96 (2, LPlus ATFloat) total,
@@ -126,9 +119,9 @@ primitives =
126 Prim (UN "prim_lenString") (ty [StrType] (AType (ATInt ITNative))) 1 (p_strLen)
119 Prim (UN "prim_lenString") (ty [StrType] (AType (ATInt ITNative))) 1 (p_strLen)
127 (1, LStrLen) total,
120 (1, LStrLen) total,
128 -- Conversions
121 -- Conversions
129− Prim (UN "prim__charToInt") (ty [ChType] (AType (ATInt ITNative))) 1 (c_charToInt)
122+ Prim (UN "prim__charToInt") (ty [(AType (ATInt ITChar))] (AType (ATInt ITNative))) 1 (c_charToInt)
130− (1, LChInt ITNative) total,
123+ (1, LChInt ITChar) total,
131− Prim (UN "prim__intToChar") (ty [(AType (ATInt ITNative))] ChType) 1 (c_intToChar)
124+ Prim (UN "prim__intToChar") (ty [(AType (ATInt ITNative))] (AType (ATInt ITChar))) 1 (c_intToChar)
132 (1, LIntCh ITNative) total,
125 (1, LIntCh ITNative) total,
133 Prim (UN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)
126 Prim (UN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)
134 (1, LStrFloat) total,
127 (1, LStrFloat) total,
@@ -158,13 +151,13 @@ primitives =
158 Prim (UN "prim__floatCeil") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatCeil)
151 Prim (UN "prim__floatCeil") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatCeil)
159 (1, LFCeil) total,
152 (1, LFCeil) total,
161− Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
154+ Prim (UN "prim__strHead") (ty [StrType] (AType (ATInt ITChar))) 1 (p_strHead)
162 (1, LStrHead) partial,
155 (1, LStrHead) partial,
163 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
156 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
164 (1, LStrTail) partial,
157 (1, LStrTail) partial,
165− Prim (UN "prim__strCons") (ty [ChType, StrType] StrType) 2 (p_strCons)
158+ Prim (UN "prim__strCons") (ty [(AType (ATInt ITChar)), StrType] StrType) 2 (p_strCons)
166 (2, LStrCons) total,
159 (2, LStrCons) total,
167− Prim (UN "prim__strIndex") (ty [StrType, (AType (ATInt ITNative))] ChType) 2 (p_strIndex)
160+ Prim (UN "prim__strIndex") (ty [StrType, (AType (ATInt ITNative))] (AType (ATInt ITChar))) 2 (p_strIndex)
168 (2, LStrIndex) partial,
161 (2, LStrIndex) partial,
169 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
162 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
170 (1, LStrRev) total,
163 (1, LStrRev) total,
@@ -175,7 +168,7 @@ primitives =
175 -- Streams
168 -- Streams
176 Prim (UN "prim__stdin") (ty [] PtrType) 0 (p_cantreduce)
169 Prim (UN "prim__stdin") (ty [] PtrType) 0 (p_cantreduce)
177 (0, LStdIn) partial
170 (0, LStdIn) partial
178− ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative]
171+ ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative, ITChar]
179 ++ concatMap vecOps [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
172 ++ concatMap vecOps [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
181intOps :: IntTy -> [Prim]
174intOps :: IntTy -> [Prim]
@@ -276,6 +269,7 @@ intTyName :: IntTy -> String
276intTyName ITNative = "Int"
269intTyName ITNative = "Int"
277intTyName ITBig = "BigInt"
270intTyName ITBig = "BigInt"
278intTyName (ITFixed sized) = "B" ++ show (nativeTyWidth sized)
271intTyName (ITFixed sized) = "B" ++ show (nativeTyWidth sized)
272+intTyName (ITChar) = "Char"
279intTyName (ITVec ity count) = "B" ++ show (nativeTyWidth ity) ++ "x" ++ show count
273intTyName (ITVec ity count) = "B" ++ show (nativeTyWidth ity) ++ "x" ++ show count
281iCmp :: IntTy -> String -> Bool -> ([Const] -> Maybe Const) -> (IntTy -> PrimFn) -> Totality -> Prim
275iCmp :: IntTy -> String -> Bool -> ([Const] -> Maybe Const) -> (IntTy -> PrimFn) -> Totality -> Prim
@@ -333,6 +327,7 @@ bsrem (ITFixed IT32) [B32 x, B32 y]
333bsrem (ITFixed IT64) [B64 x, B64 y]
327bsrem (ITFixed IT64) [B64 x, B64 y]
334 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
328 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
335bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
329bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
330+bsrem ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `rem` (ord y))
336bsrem (ITVec IT8 _) [B8V x, B8V y]
331bsrem (ITVec IT8 _) [B8V x, B8V y]
337 = Just . B8V $
332 = Just . B8V $
338 V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int8))) x y
333 V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int8))) x y
@@ -357,6 +352,7 @@ bsdiv (ITFixed IT32) [B32 x, B32 y]
357bsdiv (ITFixed IT64) [B64 x, B64 y]
352bsdiv (ITFixed IT64) [B64 x, B64 y]
358 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
353 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
359bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
354bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
355+bsdiv ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `div` (ord y))
360bsdiv (ITVec IT8 _) [B8V x, B8V y]
356bsdiv (ITVec IT8 _) [B8V x, B8V y]
361 = Just . B8V $
357 = Just . B8V $
362 V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int8))) x y
358 V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int8))) x y
@@ -381,6 +377,7 @@ bashr (ITFixed IT32) [B32 x, B32 y]
381bashr (ITFixed IT64) [B64 x, B64 y]
377bashr (ITFixed IT64) [B64 x, B64 y]
382 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
378 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
383bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
379bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
380+bashr ITChar [Ch x, Ch y] = Just $ Ch (chr $ (ord x) `shiftR` (ord y))
384bashr (ITVec IT8 _) [B8V x, B8V y]
381bashr (ITVec IT8 _) [B8V x, B8V y]
385 = Just . B8V $
382 = Just . B8V $
386 V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int8))) x y
383 V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int8))) x y
@@ -402,6 +399,7 @@ bUn (ITFixed IT32) op [B32 x] = Just $ B32 (op x)
402bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
399bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
403bUn ITBig op [BI x] = Just $ BI (op x)
400bUn ITBig op [BI x] = Just $ BI (op x)
404bUn ITNative op [I x] = Just $ I (op x)
401bUn ITNative op [I x] = Just $ I (op x)
402+bUn ITChar op [Ch x] = Just $ Ch (chr $ op (ord x))
405bUn (ITVec IT8 _) op [B8V x] = Just . B8V $ V.map op x
403bUn (ITVec IT8 _) op [B8V x] = Just . B8V $ V.map op x
406bUn (ITVec IT16 _) op [B16V x] = Just . B16V $ V.map op x
404bUn (ITVec IT16 _) op [B16V x] = Just . B16V $ V.map op x
407bUn (ITVec IT32 _) op [B32V x] = Just . B32V $ V.map op x
405bUn (ITVec IT32 _) op [B32V x] = Just . B32V $ V.map op x
@@ -415,6 +413,7 @@ bitBin (ITFixed IT32) op [B32 x, B32 y] = Just $ B32 (op x y)
415bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
413bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
416bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
414bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
417bitBin ITNative op [I x, I y] = Just $ I (op x y)
415bitBin ITNative op [I x, I y] = Just $ I (op x y)
416+bitBin ITChar op [Ch x, Ch y] = Just $ Ch (chr $ op (ord x) (ord y))
418bitBin (ITVec IT8 _) op [B8V x, B8V y] = Just . B8V $ V.zipWith op x y
417bitBin (ITVec IT8 _) op [B8V x, B8V y] = Just . B8V $ V.zipWith op x y
419bitBin (ITVec IT16 _) op [B16V x, B16V y] = Just . B16V $ V.zipWith op x y
418bitBin (ITVec IT16 _) op [B16V x, B16V y] = Just . B16V $ V.zipWith op x y
420bitBin (ITVec IT32 _) op [B32V x, B32V y] = Just . B32V $ V.zipWith op x y
419bitBin (ITVec IT32 _) op [B32V x, B32V y] = Just . B32V $ V.zipWith op x y
@@ -428,6 +427,7 @@ bCmp (ITFixed IT32) op [B32 x, B32 y] = Just $ I (if (op x y) then 1 else 0)
428bCmp (ITFixed IT64) op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
427bCmp (ITFixed IT64) op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
429bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
428bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
430bCmp ITNative op [I x, I y] = Just $ I (if (op x y) then 1 else 0)
429bCmp ITNative op [I x, I y] = Just $ I (if (op x y) then 1 else 0)
430+bCmp ITChar op [Ch x, Ch y] = Just $ I (if (op x y) then 1 else 0)
431bCmp (ITVec IT8 _) op [B8V x, B8V y]
431bCmp (ITVec IT8 _) op [B8V x, B8V y]
432 = Just . B8V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
432 = Just . B8V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
433bCmp (ITVec IT16 _) op [B16V x, B16V y]
433bCmp (ITVec IT16 _) op [B16V x, B16V y]
@@ -445,6 +445,7 @@ toInt (ITFixed IT32) x = B32 (fromIntegral x)
445toInt (ITFixed IT64) x = B64 (fromIntegral x)
445toInt (ITFixed IT64) x = B64 (fromIntegral x)
446toInt ITBig x = BI (fromIntegral x)
446toInt ITBig x = BI (fromIntegral x)
447toInt ITNative x = I (fromIntegral x)
447toInt ITNative x = I (fromIntegral x)
448+toInt ITChar x = Ch (chr $ fromIntegral x)
449intToInt :: IntTy -> IntTy -> [Const] -> Maybe Const
450intToInt :: IntTy -> IntTy -> [Const] -> Maybe Const
450intToInt (ITFixed IT8) out [B8 x] = Just $ toInt out x
451intToInt (ITFixed IT8) out [B8 x] = Just $ toInt out x
@@ -453,6 +454,7 @@ intToInt (ITFixed IT32) out [B32 x] = Just $ toInt out x
453intToInt (ITFixed IT64) out [B64 x] = Just $ toInt out x
454intToInt (ITFixed IT64) out [B64 x] = Just $ toInt out x
454intToInt ITBig out [BI x] = Just $ toInt out x
455intToInt ITBig out [BI x] = Just $ toInt out x
455intToInt ITNative out [I x] = Just $ toInt out x
456intToInt ITNative out [I x] = Just $ toInt out x
457+intToInt ITChar out [Ch x] = Just $ toInt out (ord x)
456intToInt _ _ _ = Nothing
458intToInt _ _ _ = Nothing
458zext :: IntTy -> IntTy -> [Const] -> Maybe Const
460zext :: IntTy -> IntTy -> [Const] -> Maybe Const