@@ -11,6 +11,7 @@ import Core.Evaluate
1111import Data.Bits
1212import Data.Word
1313import Data.Int
14+import Data.Char
1415import qualified Data.Vector.Unboxed as V
1516
1617data Prim = Prim { p_name :: Name,
@@ -32,18 +33,7 @@ partial = Partial NotCovering
3233primitives :: [Prim]
3334primitives =
3435 -- 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,
45−
46− iCoerce (ITFixed IT8) (ITFixed IT16) "zext" zext LZExt,
36+ [iCoerce (ITFixed IT8) (ITFixed IT16) "zext" zext LZExt,
4737 iCoerce (ITFixed IT8) (ITFixed IT32) "zext" zext LZExt,
4838 iCoerce (ITFixed IT8) (ITFixed IT64) "zext" zext LZExt,
4939 iCoerce (ITFixed IT8) ITBig "zext" zext LZExt,
@@ -60,6 +50,7 @@ primitives =
6050 iCoerce ITNative (ITFixed IT64) "zext" zext LZExt,
6151 iCoerce ITNative (ITFixed IT32) "zext" zext LZExt,
6252 iCoerce ITNative (ITFixed IT16) "zext" zext LZExt,
53+ iCoerce ITChar ITBig "zext" zext LZExt,
6354
6455 iCoerce (ITFixed IT8) (ITFixed IT16) "sext" sext LSExt,
6556 iCoerce (ITFixed IT8) (ITFixed IT32) "sext" sext LSExt,
@@ -79,6 +70,7 @@ primitives =
7970 iCoerce ITNative (ITFixed IT64) "sext" sext LSExt,
8071 iCoerce ITNative (ITFixed IT32) "sext" sext LSExt,
8172 iCoerce ITNative (ITFixed IT16) "sext" sext LSExt,
73+ iCoerce ITChar ITBig "sext" sext LSExt,
8274
8375 iCoerce (ITFixed IT16) (ITFixed IT8) "trunc" trunc LTrunc,
8476 iCoerce (ITFixed IT32) (ITFixed IT8) "trunc" trunc LTrunc,
@@ -98,6 +90,7 @@ primitives =
9890 iCoerce (ITFixed IT64) ITNative "trunc" trunc LTrunc,
9991 iCoerce ITBig ITNative "trunc" trunc LTrunc,
10092 iCoerce ITNative (ITFixed IT64) "trunc" trunc LTrunc,
93+ iCoerce ITBig ITChar "trunc" trunc LTrunc,
10194
10295 Prim (UN "prim__addFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (+))
10396 (2, LPlus ATFloat) total,
@@ -126,9 +119,9 @@ primitives =
126119 Prim (UN "prim_lenString") (ty [StrType] (AType (ATInt ITNative))) 1 (p_strLen)
127120 (1, LStrLen) total,
128121 -- Conversions
129− Prim (UN "prim__charToInt") (ty [ChType] (AType (ATInt ITNative))) 1 (c_charToInt)
130− (1, LChInt ITNative) total,
131− Prim (UN "prim__intToChar") (ty [(AType (ATInt ITNative))] ChType) 1 (c_intToChar)
122+ Prim (UN "prim__charToInt") (ty [(AType (ATInt ITChar))] (AType (ATInt ITNative))) 1 (c_charToInt)
123+ (1, LChInt ITChar) total,
124+ Prim (UN "prim__intToChar") (ty [(AType (ATInt ITNative))] (AType (ATInt ITChar))) 1 (c_intToChar)
132125 (1, LIntCh ITNative) total,
133126 Prim (UN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)
134127 (1, LStrFloat) total,
@@ -158,13 +151,13 @@ primitives =
158151 Prim (UN "prim__floatCeil") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatCeil)
159152 (1, LFCeil) total,
160153
161− Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
154+ Prim (UN "prim__strHead") (ty [StrType] (AType (ATInt ITChar))) 1 (p_strHead)
162155 (1, LStrHead) partial,
163156 Prim (UN "prim__strTail") (ty [StrType] StrType) 1 (p_strTail)
164157 (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)
166159 (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)
168161 (2, LStrIndex) partial,
169162 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
170163 (1, LStrRev) total,
@@ -175,7 +168,7 @@ primitives =
175168 -- Streams
176169 Prim (UN "prim__stdin") (ty [] PtrType) 0 (p_cantreduce)
177170 (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]
179172 ++ concatMap vecOps [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
180173
181174intOps :: IntTy -> [Prim]
@@ -276,6 +269,7 @@ intTyName :: IntTy -> String
276269intTyName ITNative = "Int"
277270intTyName ITBig = "BigInt"
278271intTyName (ITFixed sized) = "B" ++ show (nativeTyWidth sized)
272+intTyName (ITChar) = "Char"
279273intTyName (ITVec ity count) = "B" ++ show (nativeTyWidth ity) ++ "x" ++ show count
280274
281275iCmp :: IntTy -> String -> Bool -> ([Const] -> Maybe Const) -> (IntTy -> PrimFn) -> Totality -> Prim
@@ -333,6 +327,7 @@ bsrem (ITFixed IT32) [B32 x, B32 y]
333327bsrem (ITFixed IT64) [B64 x, B64 y]
334328 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
335329bsrem 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))
336331bsrem (ITVec IT8 _) [B8V x, B8V y]
337332 = Just . B8V $
338333 V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int8))) x y
@@ -357,6 +352,7 @@ bsdiv (ITFixed IT32) [B32 x, B32 y]
357352bsdiv (ITFixed IT64) [B64 x, B64 y]
358353 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
359354bsdiv 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))
360356bsdiv (ITVec IT8 _) [B8V x, B8V y]
361357 = Just . B8V $
362358 V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int8))) x y
@@ -381,6 +377,7 @@ bashr (ITFixed IT32) [B32 x, B32 y]
381377bashr (ITFixed IT64) [B64 x, B64 y]
382378 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
383379bashr 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))
384381bashr (ITVec IT8 _) [B8V x, B8V y]
385382 = Just . B8V $
386383 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)
402399bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
403400bUn ITBig op [BI x] = Just $ BI (op x)
404401bUn ITNative op [I x] = Just $ I (op x)
402+bUn ITChar op [Ch x] = Just $ Ch (chr $ op (ord x))
405403bUn (ITVec IT8 _) op [B8V x] = Just . B8V $ V.map op x
406404bUn (ITVec IT16 _) op [B16V x] = Just . B16V $ V.map op x
407405bUn (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)
415413bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
416414bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
417415bitBin 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))
418417bitBin (ITVec IT8 _) op [B8V x, B8V y] = Just . B8V $ V.zipWith op x y
419418bitBin (ITVec IT16 _) op [B16V x, B16V y] = Just . B16V $ V.zipWith op x y
420419bitBin (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)
428427bCmp (ITFixed IT64) op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
429428bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
430429bCmp 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)
431431bCmp (ITVec IT8 _) op [B8V x, B8V y]
432432 = Just . B8V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
433433bCmp (ITVec IT16 _) op [B16V x, B16V y]
@@ -445,6 +445,7 @@ toInt (ITFixed IT32) x = B32 (fromIntegral x)
445445toInt (ITFixed IT64) x = B64 (fromIntegral x)
446446toInt ITBig x = BI (fromIntegral x)
447447toInt ITNative x = I (fromIntegral x)
448+toInt ITChar x = Ch (chr $ fromIntegral x)
448449
449450intToInt :: IntTy -> IntTy -> [Const] -> Maybe Const
450451intToInt (ITFixed IT8) out [B8 x] = Just $ toInt out x
@@ -453,6 +454,7 @@ intToInt (ITFixed IT32) out [B32 x] = Just $ toInt out x
453454intToInt (ITFixed IT64) out [B64 x] = Just $ toInt out x
454455intToInt ITBig out [BI x] = Just $ toInt out x
455456intToInt ITNative out [I x] = Just $ toInt out x
457+intToInt ITChar out [Ch x] = Just $ toInt out (ord x)
456458intToInt _ _ _ = Nothing
457459
458460zext :: IntTy -> IntTy -> [Const] -> Maybe Const