@@ -11,6 +11,7 @@ import Core.Evaluate
1111import Data.Bits
1212import Data.Word
1313import Data.Int
14+import qualified Data.Vector.Unboxed as V
1415
1516data Prim = Prim { p_name :: Name,
1617 p_type :: Type,
@@ -31,130 +32,130 @@ partial = Partial NotCovering
3132primitives :: [Prim]
3233primitives =
3334 -- operators
34− [Prim (UN "prim__eqChar") (ty [ChType, ChType] IType) 2 (bcBin (==))
35− (2, LEq ITNative) total,
36− Prim (UN "prim__ltChar") (ty [ChType, ChType] IType) 2 (bcBin (<))
37− (2, LLt ITNative) total,
38− Prim (UN "prim__lteChar") (ty [ChType, ChType] IType) 2 (bcBin (<=))
39− (2, LLe ITNative) total,
40− Prim (UN "prim__gtChar") (ty [ChType, ChType] IType) 2 (bcBin (>))
41− (2, LGt ITNative) total,
42− Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
43− (2, LGe ITNative) total,
44−
45− iCoerce IT8 IT16 "zext" zext LZExt,
46− iCoerce IT8 IT32 "zext" zext LZExt,
47− iCoerce IT8 IT64 "zext" zext LZExt,
48− iCoerce IT8 ITBig "zext" zext LZExt,
49− iCoerce IT8 ITNative "zext" zext LZExt,
50− iCoerce IT16 IT32 "zext" zext LZExt,
51− iCoerce IT16 IT64 "zext" zext LZExt,
52− iCoerce IT16 ITBig "zext" zext LZExt,
53− iCoerce IT16 ITNative "zext" zext LZExt,
54− iCoerce IT32 IT64 "zext" zext LZExt,
55− iCoerce IT32 ITBig "zext" zext LZExt,
56− iCoerce IT32 ITNative "zext" zext LZExt,
57− iCoerce IT64 ITBig "zext" zext LZExt,
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,
47+ iCoerce (ITFixed IT8) (ITFixed IT32) "zext" zext LZExt,
48+ iCoerce (ITFixed IT8) (ITFixed IT64) "zext" zext LZExt,
49+ iCoerce (ITFixed IT8) ITBig "zext" zext LZExt,
50+ iCoerce (ITFixed IT8) ITNative "zext" zext LZExt,
51+ iCoerce (ITFixed IT16) (ITFixed IT32) "zext" zext LZExt,
52+ iCoerce (ITFixed IT16) (ITFixed IT64) "zext" zext LZExt,
53+ iCoerce (ITFixed IT16) ITBig "zext" zext LZExt,
54+ iCoerce (ITFixed IT16) ITNative "zext" zext LZExt,
55+ iCoerce (ITFixed IT32) (ITFixed IT64) "zext" zext LZExt,
56+ iCoerce (ITFixed IT32) ITBig "zext" zext LZExt,
57+ iCoerce (ITFixed IT32) ITNative "zext" zext LZExt,
58+ iCoerce (ITFixed IT64) ITBig "zext" zext LZExt,
5859 iCoerce ITNative ITBig "zext" zext LZExt,
59− iCoerce ITNative IT64 "zext" zext LZExt,
60− iCoerce ITNative IT32 "zext" zext LZExt,
61− iCoerce ITNative IT16 "zext" zext LZExt,
62−
63− iCoerce IT8 IT16 "sext" sext LSExt,
64− iCoerce IT8 IT32 "sext" sext LSExt,
65− iCoerce IT8 IT64 "sext" sext LSExt,
66− iCoerce IT8 ITBig "sext" sext LSExt,
67− iCoerce IT8 ITNative "sext" sext LSExt,
68− iCoerce IT16 IT32 "sext" sext LSExt,
69− iCoerce IT16 IT64 "sext" sext LSExt,
70− iCoerce IT16 ITBig "sext" sext LSExt,
71− iCoerce IT16 ITNative "sext" sext LSExt,
72− iCoerce IT32 IT64 "sext" sext LSExt,
73− iCoerce IT32 ITBig "sext" sext LSExt,
74− iCoerce IT32 ITNative "sext" sext LSExt,
75− iCoerce IT64 ITBig "sext" sext LSExt,
60+ iCoerce ITNative (ITFixed IT64) "zext" zext LZExt,
61+ iCoerce ITNative (ITFixed IT32) "zext" zext LZExt,
62+ iCoerce ITNative (ITFixed IT16) "zext" zext LZExt,
63+
64+ iCoerce (ITFixed IT8) (ITFixed IT16) "sext" sext LSExt,
65+ iCoerce (ITFixed IT8) (ITFixed IT32) "sext" sext LSExt,
66+ iCoerce (ITFixed IT8) (ITFixed IT64) "sext" sext LSExt,
67+ iCoerce (ITFixed IT8) ITBig "sext" sext LSExt,
68+ iCoerce (ITFixed IT8) ITNative "sext" sext LSExt,
69+ iCoerce (ITFixed IT16) (ITFixed IT32) "sext" sext LSExt,
70+ iCoerce (ITFixed IT16) (ITFixed IT64) "sext" sext LSExt,
71+ iCoerce (ITFixed IT16) ITBig "sext" sext LSExt,
72+ iCoerce (ITFixed IT16) ITNative "sext" sext LSExt,
73+ iCoerce (ITFixed IT32) (ITFixed IT64) "sext" sext LSExt,
74+ iCoerce (ITFixed IT32) ITBig "sext" sext LSExt,
75+ iCoerce (ITFixed IT32) ITNative "sext" sext LSExt,
76+ iCoerce (ITFixed IT64) ITBig "sext" sext LSExt,
7677 iCoerce ITNative ITBig "sext" sext LSExt,
7778 iCoerce ITNative ITBig "sext" sext LSExt,
78− iCoerce ITNative IT64 "sext" sext LSExt,
79− iCoerce ITNative IT32 "sext" sext LSExt,
80− iCoerce ITNative IT16 "sext" sext LSExt,
81−
82− iCoerce IT16 IT8 "trunc" trunc LTrunc,
83− iCoerce IT32 IT8 "trunc" trunc LTrunc,
84− iCoerce IT64 IT8 "trunc" trunc LTrunc,
85− iCoerce ITBig IT8 "trunc" trunc LTrunc,
86− iCoerce ITNative IT8 "trunc" trunc LTrunc,
87− iCoerce IT32 IT16 "trunc" trunc LTrunc,
88− iCoerce IT64 IT16 "trunc" trunc LTrunc,
89− iCoerce ITBig IT16 "trunc" trunc LTrunc,
90− iCoerce ITNative IT16 "trunc" trunc LTrunc,
91− iCoerce IT64 IT32 "trunc" trunc LTrunc,
92− iCoerce ITBig IT32 "trunc" trunc LTrunc,
93− iCoerce ITNative IT32 "trunc" trunc LTrunc,
94− iCoerce ITBig IT64 "trunc" trunc LTrunc,
95− iCoerce IT16 ITNative "trunc" trunc LTrunc,
96− iCoerce IT32 ITNative "trunc" trunc LTrunc,
97− iCoerce IT64 ITNative "trunc" trunc LTrunc,
79+ iCoerce ITNative (ITFixed IT64) "sext" sext LSExt,
80+ iCoerce ITNative (ITFixed IT32) "sext" sext LSExt,
81+ iCoerce ITNative (ITFixed IT16) "sext" sext LSExt,
82+
83+ iCoerce (ITFixed IT16) (ITFixed IT8) "trunc" trunc LTrunc,
84+ iCoerce (ITFixed IT32) (ITFixed IT8) "trunc" trunc LTrunc,
85+ iCoerce (ITFixed IT64) (ITFixed IT8) "trunc" trunc LTrunc,
86+ iCoerce ITBig (ITFixed IT8) "trunc" trunc LTrunc,
87+ iCoerce ITNative (ITFixed IT8) "trunc" trunc LTrunc,
88+ iCoerce (ITFixed IT32) (ITFixed IT16) "trunc" trunc LTrunc,
89+ iCoerce (ITFixed IT64) (ITFixed IT16) "trunc" trunc LTrunc,
90+ iCoerce ITBig (ITFixed IT16) "trunc" trunc LTrunc,
91+ iCoerce ITNative (ITFixed IT16) "trunc" trunc LTrunc,
92+ iCoerce (ITFixed IT64) (ITFixed IT32) "trunc" trunc LTrunc,
93+ iCoerce ITBig (ITFixed IT32) "trunc" trunc LTrunc,
94+ iCoerce ITNative (ITFixed IT32) "trunc" trunc LTrunc,
95+ iCoerce ITBig (ITFixed IT64) "trunc" trunc LTrunc,
96+ iCoerce (ITFixed IT16) ITNative "trunc" trunc LTrunc,
97+ iCoerce (ITFixed IT32) ITNative "trunc" trunc LTrunc,
98+ iCoerce (ITFixed IT64) ITNative "trunc" trunc LTrunc,
9899 iCoerce ITBig ITNative "trunc" trunc LTrunc,
99− iCoerce ITNative IT64 "trunc" trunc LTrunc,
100−
101− Prim (UN "prim__addFloat") (ty [FlType, FlType] FlType) 2 (fBin (+))
102− (2, LFPlus) total,
103− Prim (UN "prim__subFloat") (ty [FlType, FlType] FlType) 2 (fBin (-))
104− (2, LFMinus) total,
105− Prim (UN "prim__mulFloat") (ty [FlType, FlType] FlType) 2 (fBin (*))
106− (2, LFTimes) total,
107− Prim (UN "prim__divFloat") (ty [FlType, FlType] FlType) 2 (fBin (/))
108− (2, LFDiv) total,
109− Prim (UN "prim__eqFloat") (ty [FlType, FlType] IType) 2 (bfBin (==))
110− (2, LFEq) total,
111− Prim (UN "prim__ltFloat") (ty [FlType, FlType] IType) 2 (bfBin (<))
112− (2, LFLt) total,
113− Prim (UN "prim__lteFloat") (ty [FlType, FlType] IType) 2 (bfBin (<=))
114− (2, LFLe) total,
115− Prim (UN "prim__gtFloat") (ty [FlType, FlType] IType) 2 (bfBin (>))
116− (2, LFGt) total,
117− Prim (UN "prim__gteFloat") (ty [FlType, FlType] IType) 2 (bfBin (>=))
118− (2, LFGe) total,
100+ iCoerce ITNative (ITFixed IT64) "trunc" trunc LTrunc,
101+
102+ Prim (UN "prim__addFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (+))
103+ (2, LPlus ATFloat) total,
104+ Prim (UN "prim__subFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (-))
105+ (2, LMinus ATFloat) total,
106+ Prim (UN "prim__mulFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (*))
107+ (2, LTimes ATFloat) total,
108+ Prim (UN "prim__divFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType ATFloat)) 2 (fBin (/))
109+ (2, LSDiv ATFloat) total,
110+ Prim (UN "prim__eqFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType (ATInt ITNative))) 2 (bfBin (==))
111+ (2, LEq ATFloat) total,
112+ Prim (UN "prim__ltFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType (ATInt ITNative))) 2 (bfBin (<))
113+ (2, LLt ATFloat) total,
114+ Prim (UN "prim__lteFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType (ATInt ITNative))) 2 (bfBin (<=))
115+ (2, LLe ATFloat) total,
116+ Prim (UN "prim__gtFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType (ATInt ITNative))) 2 (bfBin (>))
117+ (2, LGt ATFloat) total,
118+ Prim (UN "prim__gteFloat") (ty [(AType ATFloat), (AType ATFloat)] (AType (ATInt ITNative))) 2 (bfBin (>=))
119+ (2, LGe ATFloat) total,
119120 Prim (UN "prim__concat") (ty [StrType, StrType] StrType) 2 (sBin (++))
120121 (2, LStrConcat) total,
121− Prim (UN "prim__eqString") (ty [StrType, StrType] IType) 2 (bsBin (==))
122+ Prim (UN "prim__eqString") (ty [StrType, StrType] (AType (ATInt ITNative))) 2 (bsBin (==))
122123 (2, LStrEq) total,
123− Prim (UN "prim__ltString") (ty [StrType, StrType] IType) 2 (bsBin (<))
124+ Prim (UN "prim__ltString") (ty [StrType, StrType] (AType (ATInt ITNative))) 2 (bsBin (<))
124125 (2, LStrLt) total,
125− Prim (UN "prim_lenString") (ty [StrType] IType) 1 (p_strLen)
126+ Prim (UN "prim_lenString") (ty [StrType] (AType (ATInt ITNative))) 1 (p_strLen)
126127 (1, LStrLen) total,
127128 -- Conversions
128− Prim (UN "prim__charToInt") (ty [ChType] IType) 1 (c_charToInt)
129+ Prim (UN "prim__charToInt") (ty [ChType] (AType (ATInt ITNative))) 1 (c_charToInt)
129130 (1, LChInt ITNative) total,
130− Prim (UN "prim__intToChar") (ty [IType] ChType) 1 (c_intToChar)
131+ Prim (UN "prim__intToChar") (ty [(AType (ATInt ITNative))] ChType) 1 (c_intToChar)
131132 (1, LIntCh ITNative) total,
132− Prim (UN "prim__strToFloat") (ty [StrType] FlType) 1 (c_strToFloat)
133+ Prim (UN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)
133134 (1, LStrFloat) total,
134− Prim (UN "prim__floatToStr") (ty [FlType] StrType) 1 (c_floatToStr)
135+ Prim (UN "prim__floatToStr") (ty [(AType ATFloat)] StrType) 1 (c_floatToStr)
135136 (1, LFloatStr) total,
136137
137− Prim (UN "prim__floatExp") (ty [FlType] FlType) 1 (p_floatExp)
138− (1, LFExp) total,
139− Prim (UN "prim__floatLog") (ty [FlType] FlType) 1 (p_floatLog)
138+ Prim (UN "prim__floatExp") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatExp)
139+ (1, LFExp) total,
140+ Prim (UN "prim__floatLog") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatLog)
140141 (1, LFLog) total,
141− Prim (UN "prim__floatSin") (ty [FlType] FlType) 1 (p_floatSin)
142+ Prim (UN "prim__floatSin") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatSin)
142143 (1, LFSin) total,
143− Prim (UN "prim__floatCos") (ty [FlType] FlType) 1 (p_floatCos)
144+ Prim (UN "prim__floatCos") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatCos)
144145 (1, LFCos) total,
145− Prim (UN "prim__floatTan") (ty [FlType] FlType) 1 (p_floatTan)
146+ Prim (UN "prim__floatTan") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatTan)
146147 (1, LFTan) total,
147− Prim (UN "prim__floatASin") (ty [FlType] FlType) 1 (p_floatASin)
148+ Prim (UN "prim__floatASin") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatASin)
148149 (1, LFASin) total,
149− Prim (UN "prim__floatACos") (ty [FlType] FlType) 1 (p_floatACos)
150+ Prim (UN "prim__floatACos") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatACos)
150151 (1, LFACos) total,
151− Prim (UN "prim__floatATan") (ty [FlType] FlType) 1 (p_floatATan)
152+ Prim (UN "prim__floatATan") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatATan)
152153 (1, LFATan) total,
153− Prim (UN "prim__floatSqrt") (ty [FlType] FlType) 1 (p_floatSqrt)
154+ Prim (UN "prim__floatSqrt") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatSqrt)
154155 (1, LFSqrt) total,
155− Prim (UN "prim__floatFloor") (ty [FlType] FlType) 1 (p_floatFloor)
156+ Prim (UN "prim__floatFloor") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatFloor)
156157 (1, LFFloor) total,
157− Prim (UN "prim__floatCeil") (ty [FlType] FlType) 1 (p_floatCeil)
158+ Prim (UN "prim__floatCeil") (ty [(AType ATFloat)] (AType ATFloat)) 1 (p_floatCeil)
158159 (1, LFCeil) total,
159160
160161 Prim (UN "prim__strHead") (ty [StrType] ChType) 1 (p_strHead)
@@ -163,7 +164,7 @@ primitives =
163164 (1, LStrTail) partial,
164165 Prim (UN "prim__strCons") (ty [ChType, StrType] StrType) 2 (p_strCons)
165166 (2, LStrCons) total,
166− Prim (UN "prim__strIndex") (ty [StrType, IType] ChType) 2 (p_strIndex)
167+ Prim (UN "prim__strIndex") (ty [StrType, (AType (ATInt ITNative))] ChType) 2 (p_strIndex)
167168 (2, LStrIndex) partial,
168169 Prim (UN "prim__strRev") (ty [StrType] StrType) 1 (p_strRev)
169170 (1, LStrRev) total,
@@ -174,22 +175,23 @@ primitives =
174175 -- Streams
175176 Prim (UN "prim__stdin") (ty [] PtrType) 0 (p_cantreduce)
176177 (0, LStdIn) partial
177− ] ++ concatMap intOps [IT8, IT16, IT32, IT64, ITBig, ITNative]
178+ ] ++ concatMap intOps [ITFixed IT8, ITFixed IT16, ITFixed IT32, ITFixed IT64, ITBig, ITNative]
179+ ++ concatMap vecOps [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
178180
179181intOps :: IntTy -> [Prim]
180182intOps ity =
181− [ iCmp ity "lt" (bCmp ity (<)) LLt total
182− , iCmp ity "lte" (bCmp ity (<=)) LLe total
183− , iCmp ity "eq" (bCmp ity (==)) LEq total
184− , iCmp ity "gte" (bCmp ity (>=)) LGe total
185− , iCmp ity "gt" (bCmp ity (>)) LGt total
186− , iBinOp ity "add" (bitBin ity (+)) LPlus total
187− , iBinOp ity "sub" (bitBin ity (-)) LMinus total
188− , iBinOp ity "mul" (bitBin ity (*)) LTimes total
183+ [ iCmp ity "lt" (bCmp ity (<)) (LLt . ATInt) total
184+ , iCmp ity "lte" (bCmp ity (<=)) (LLe . ATInt) total
185+ , iCmp ity "eq" (bCmp ity (==)) (LEq . ATInt) total
186+ , iCmp ity "gte" (bCmp ity (>=)) (LGe . ATInt) total
187+ , iCmp ity "gt" (bCmp ity (>)) (LGt . ATInt) total
188+ , iBinOp ity "add" (bitBin ity (+)) (LPlus . ATInt) total
189+ , iBinOp ity "sub" (bitBin ity (-)) (LMinus . ATInt) total
190+ , iBinOp ity "mul" (bitBin ity (*)) (LTimes . ATInt) total
189191 , iBinOp ity "udiv" (bitBin ity div) LUDiv partial
190− , iBinOp ity "sdiv" (bsdiv ity) LSDiv partial
192+ , iBinOp ity "sdiv" (bsdiv ity) (LSDiv . ATInt) partial
191193 , iBinOp ity "urem" (bitBin ity rem) LURem partial
192− , iBinOp ity "srem" (bsrem ity) LSRem partial
194+ , iBinOp ity "srem" (bsrem ity) (LSRem . ATInt) partial
193195 , iBinOp ity "shl" (bitBin ity (\x y -> shiftL x (fromIntegral y))) LSHL total
194196 , iBinOp ity "lshr" (bitBin ity (\x y -> shiftR x (fromIntegral y))) LLSHR total
195197 , iBinOp ity "ashr" (bashr ity) LASHR total
@@ -197,30 +199,57 @@ intOps ity =
197199 , iBinOp ity "or" (bitBin ity (.|.)) LOr total
198200 , iBinOp ity "xor" (bitBin ity (xor)) LXOr total
199201 , iUnOp ity "compl" (bUn ity complement) LCompl total
200− , Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [intTyToConst ity] StrType) 1 intToStr
202+ , Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [AType . ATInt $ ity] StrType) 1 intToStr
201203 (1, LIntStr ity) total
202− , Prim (UN $ "prim__fromStr" ++ intTyName ity) (ty [StrType] (intTyToConst ity)) 1 (strToInt ity)
204+ , Prim (UN $ "prim__fromStr" ++ intTyName ity) (ty [StrType] (AType . ATInt $ ity)) 1 (strToInt ity)
203205 (1, LStrInt ity) total
204− , Prim (UN $ "prim__toFloat" ++ intTyName ity) (ty [intTyToConst ity] FlType) 1 intToFloat
206+ , Prim (UN $ "prim__toFloat" ++ intTyName ity) (ty [AType . ATInt $ ity] (AType ATFloat)) 1 intToFloat
205207 (1, LIntFloat ity) total
206− , Prim (UN $ "prim__fromFloat" ++ intTyName ity) (ty [FlType] (intTyToConst ity)) 1 (floatToInt ity)
208+ , Prim (UN $ "prim__fromFloat" ++ intTyName ity) (ty [AType ATFloat] (AType . ATInt $ ity)) 1 (floatToInt ity)
207209 (1, LFloatInt ity) total
208210 ]
209211
212+vecOps ity@(ITVec elem count) =
213+ [ Prim (UN $ "prim__mk" ++ intTyName ity)
214+ (ty (replicate count . AType . ATInt . ITFixed $ elem) (AType . ATInt $ ity))
215+ count (mkVecCon elem count) (count, LMkVec elem count) total
216+ ]
217+
218+mkVecCon ity count args = if length ints == count
219+ then Just (mkVec ity count ints)
220+ else error $ "Got: " ++ show args ++ " when expecting " ++ show count ++ " x " ++ show ity
221+ where
222+ ints = getInt args
223+ mkVec :: NativeTy -> Int -> [Integer] -> Const
224+ mkVec IT8 len values = B8V $ V.generate len (fromInteger . (values !!))
225+ mkVec IT16 len values = B16V $ V.generate len (fromInteger . (values !!))
226+ mkVec IT32 len values = B32V $ V.generate len (fromInteger . (values !!))
227+ mkVec IT64 len values = B64V $ V.generate len (fromInteger . (values !!))
228+
210229intTyName :: IntTy -> String
211230intTyName ITNative = "Int"
212231intTyName ITBig = "BigInt"
213−intTyName sized = "B" ++ show (intTyWidth sized)
232+intTyName (ITFixed sized) = "B" ++ show (nativeTyWidth sized)
233+intTyName (ITVec ity count) = "B" ++ show (nativeTyWidth ity) ++ "x" ++ show count
214234
215235iCmp, iBinOp, iUnOp :: IntTy -> String -> ([Const] -> Maybe Const) -> (IntTy -> PrimFn) -> Totality -> Prim
216−iCmp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ intTyName ity) (ty (replicate 2 $ intTyToConst ity) IType) 2 impl (2, irop ity) totality
217−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
218−iUnOp ity op impl irop totality = Prim (UN $ "prim__" ++ op ++ intTyName ity) (ty [intTyToConst ity] (intTyToConst ity)) 1 impl (1, irop ity) totality
236+iCmp ity op impl irop totality
237+ = Prim (UN $ "prim__" ++ op ++ intTyName ity)
238+ (ty (replicate 2 . AType . ATInt $ ity) (AType (ATInt ITNative)))
239+ 2 impl (2, irop ity) totality
240+iBinOp ity op impl irop totality
241+ = Prim (UN $ "prim__" ++ op ++ intTyName ity)
242+ (ty (replicate 2 . AType . ATInt $ ity) (AType . ATInt $ ity))
243+ 2 impl (2, irop ity) totality
244+iUnOp ity op impl irop totality
245+ = Prim (UN $ "prim__" ++ op ++ intTyName ity)
246+ (ty [AType . ATInt $ ity] (AType . ATInt $ ity))
247+ 1 impl (1, irop ity) totality
219248
220249iCoerce :: IntTy -> IntTy -> String -> (IntTy -> IntTy -> [Const] -> Maybe Const) -> (IntTy -> IntTy -> PrimFn) -> Prim
221250iCoerce from to op impl irop =
222251 Prim (UN $ "prim__" ++ op ++ intTyName from ++ "_" ++ intTyName to)
223− (ty [intTyToConst from] (intTyToConst to)) 1 (impl from to) (1, irop from to) total
252+ (ty [AType . ATInt $ from] (AType . ATInt $ to)) 1 (impl from to) (1, irop from to) total
224253
225254iUn :: (Int -> Int) -> [Const] -> Maybe Const
226255iUn op [I x] = Just $ I (op x)
@@ -269,81 +298,81 @@ sBin op [Str x, Str y] = Just $ Str (op x y)
269298sBin _ _ = Nothing
270299
271300bsrem :: IntTy -> [Const] -> Maybe Const
272−bsrem IT8 [B8 x, B8 y]
301+bsrem (ITFixed IT8) [B8 x, B8 y]
273302 = Just $ B8 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int8))
274−bsrem IT16 [B16 x, B16 y]
303+bsrem (ITFixed IT16) [B16 x, B16 y]
275304 = Just $ B16 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int16))
276−bsrem IT32 [B32 x, B32 y]
305+bsrem (ITFixed IT32) [B32 x, B32 y]
277306 = Just $ B32 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int32))
278−bsrem IT64 [B64 x, B64 y]
307+bsrem (ITFixed IT64) [B64 x, B64 y]
279308 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
280309bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
281310bsrem _ _ = Nothing
282311
283312bsdiv :: IntTy -> [Const] -> Maybe Const
284−bsdiv IT8 [B8 x, B8 y]
313+bsdiv (ITFixed IT8) [B8 x, B8 y]
285314 = Just $ B8 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int8))
286−bsdiv IT16 [B16 x, B16 y]
315+bsdiv (ITFixed IT16) [B16 x, B16 y]
287316 = Just $ B16 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int16))
288−bsdiv IT32 [B32 x, B32 y]
317+bsdiv (ITFixed IT32) [B32 x, B32 y]
289318 = Just $ B32 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int32))
290−bsdiv IT64 [B64 x, B64 y]
319+bsdiv (ITFixed IT64) [B64 x, B64 y]
291320 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
292321bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
293322bsdiv _ _ = Nothing
294323
295324bashr :: IntTy -> [Const] -> Maybe Const
296−bashr IT8 [B8 x, B8 y]
325+bashr (ITFixed IT8) [B8 x, B8 y]
297326 = Just $ B8 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int8))
298−bashr IT16 [B16 x, B16 y]
327+bashr (ITFixed IT16) [B16 x, B16 y]
299328 = Just $ B16 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int16))
300−bashr IT32 [B32 x, B32 y]
329+bashr (ITFixed IT32) [B32 x, B32 y]
301330 = Just $ B32 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int32))
302−bashr IT64 [B64 x, B64 y]
331+bashr (ITFixed IT64) [B64 x, B64 y]
303332 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
304333bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
305334bashr _ _ = Nothing
306335
307336bUn :: IntTy -> (forall a. Bits a => a -> a) -> [Const] -> Maybe Const
308−bUn IT8 op [B8 x] = Just $ B8 (op x)
309−bUn IT16 op [B16 x] = Just $ B16 (op x)
310−bUn IT32 op [B32 x] = Just $ B32 (op x)
311−bUn IT64 op [B64 x] = Just $ B64 (op x)
337+bUn (ITFixed IT8) op [B8 x] = Just $ B8 (op x)
338+bUn (ITFixed IT16) op [B16 x] = Just $ B16 (op x)
339+bUn (ITFixed IT32) op [B32 x] = Just $ B32 (op x)
340+bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
312341bUn ITBig op [BI x] = Just $ BI (op x)
313342bUn ITNative op [I x] = Just $ I (op x)
314343bUn _ _ _ = Nothing
315344
316345bitBin :: IntTy -> (forall a. (Bits a, Integral a) => a -> a -> a) -> [Const] -> Maybe Const
317−bitBin IT8 op [B8 x, B8 y] = Just $ B8 (op x y)
318−bitBin IT16 op [B16 x, B16 y] = Just $ B16 (op x y)
319−bitBin IT32 op [B32 x, B32 y] = Just $ B32 (op x y)
320−bitBin IT64 op [B64 x, B64 y] = Just $ B64 (op x y)
346+bitBin (ITFixed IT8) op [B8 x, B8 y] = Just $ B8 (op x y)
347+bitBin (ITFixed IT16) op [B16 x, B16 y] = Just $ B16 (op x y)
348+bitBin (ITFixed IT32) op [B32 x, B32 y] = Just $ B32 (op x y)
349+bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
321350bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
322351bitBin ITNative op [I x, I y] = Just $ I (op x y)
323352bitBin _ _ _ = Nothing
324353
325354bCmp :: IntTy -> (forall a. Ord a => a -> a -> Bool) -> [Const] -> Maybe Const
326−bCmp IT8 op [B8 x, B8 y] = Just $ I (if (op x y) then 1 else 0)
327−bCmp IT16 op [B16 x, B16 y] = Just $ I (if (op x y) then 1 else 0)
328−bCmp IT32 op [B32 x, B32 y] = Just $ I (if (op x y) then 1 else 0)
329−bCmp IT64 op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
355+bCmp (ITFixed IT8) op [B8 x, B8 y] = Just $ I (if (op x y) then 1 else 0)
356+bCmp (ITFixed IT16) op [B16 x, B16 y] = Just $ I (if (op x y) then 1 else 0)
357+bCmp (ITFixed IT32) op [B32 x, B32 y] = Just $ I (if (op x y) then 1 else 0)
358+bCmp (ITFixed IT64) op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
330359bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
331360bCmp ITNative op [I x, I y] = Just $ I (if (op x y) then 1 else 0)
332361bCmp _ _ _ = Nothing
333362
334363toInt :: Integral a => IntTy -> a -> Const
335−toInt IT8 x = B8 (fromIntegral x)
336−toInt IT16 x = B16 (fromIntegral x)
337−toInt IT32 x = B32 (fromIntegral x)
338−toInt IT64 x = B64 (fromIntegral x)
364+toInt (ITFixed IT8) x = B8 (fromIntegral x)
365+toInt (ITFixed IT16) x = B16 (fromIntegral x)
366+toInt (ITFixed IT32) x = B32 (fromIntegral x)
367+toInt (ITFixed IT64) x = B64 (fromIntegral x)
339368toInt ITBig x = BI (fromIntegral x)
340369toInt ITNative x = I (fromIntegral x)
341370
342371intToInt :: IntTy -> IntTy -> [Const] -> Maybe Const
343−intToInt IT8 out [B8 x] = Just $ toInt out x
344−intToInt IT16 out [B16 x] = Just $ toInt out x
345−intToInt IT32 out [B32 x] = Just $ toInt out x
346−intToInt IT64 out [B64 x] = Just $ toInt out x
372+intToInt (ITFixed IT8) out [B8 x] = Just $ toInt out x
373+intToInt (ITFixed IT16) out [B16 x] = Just $ toInt out x
374+intToInt (ITFixed IT32) out [B32 x] = Just $ toInt out x
375+intToInt (ITFixed IT64) out [B64 x] = Just $ toInt out x
347376intToInt ITBig out [BI x] = Just $ toInt out x
348377intToInt ITNative out [I x] = Just $ toInt out x
349378intToInt _ _ _ = Nothing
@@ -356,10 +385,10 @@ zext from to val
356385zext _ _ _ = Nothing
357386
358387sext :: IntTy -> IntTy -> [Const] -> Maybe Const
359−sext IT8 out [B8 x] = Just $ toInt out (fromIntegral x :: Int8)
360−sext IT16 out [B16 x] = Just $ toInt out (fromIntegral x :: Int16)
361−sext IT32 out [B32 x] = Just $ toInt out (fromIntegral x :: Int32)
362−sext IT64 out [B64 x] = Just $ toInt out (fromIntegral x :: Int64)
388+sext (ITFixed IT8) out [B8 x] = Just $ toInt out (fromIntegral x :: Int8)
389+sext (ITFixed IT16) out [B16 x] = Just $ toInt out (fromIntegral x :: Int16)
390+sext (ITFixed IT32) out [B32 x] = Just $ toInt out (fromIntegral x :: Int32)
391+sext (ITFixed IT64) out [B64 x] = Just $ toInt out (fromIntegral x :: Int64)
363392sext ITBig _ _ = Nothing
364393sext from to val = intToInt from to val
365394
@@ -369,19 +398,19 @@ trunc _ ITBig _ = Nothing
369398trunc from to val | intTyWidth from > intTyWidth to = intToInt from to val
370399trunc _ _ _ = Nothing
371400
372−getInt :: [Const] -> Maybe Integer
373−getInt [B8 x] = Just . toInteger $ x
374−getInt [B16 x] = Just . toInteger $ x
375−getInt [B32 x] = Just . toInteger $ x
376−getInt [B64 x] = Just . toInteger $ x
377−getInt [I x] = Just . toInteger $ x
378−getInt [BI x] = Just x
379−getInt _ = Nothing
380−
381401intToStr :: [Const] -> Maybe Const
382−intToStr val | Just i <- getInt val = Just $ Str (show i)
402+intToStr val | [i] <- getInt val = Just $ Str (show i)
383403intToStr _ = Nothing
384404
405+getInt :: [Const] -> [Integer]
406+getInt (B8 x : xs) = toInteger x : getInt xs
407+getInt (B16 x : xs) = toInteger x : getInt xs
408+getInt (B32 x : xs) = toInteger x : getInt xs
409+getInt (B64 x : xs) = toInteger x : getInt xs
410+getInt (I x : xs) = toInteger x : getInt xs
411+getInt (BI x : xs) = x : getInt xs
412+getInt _ = []
413+
385414strToInt :: IntTy -> [Const] -> Maybe Const
386415strToInt ity [Str x] = case reads x of
387416 [(n,"")] -> Just $ toInt ity (n :: Integer)
@@ -389,7 +418,7 @@ strToInt ity [Str x] = case reads x of
389418strToInt _ _ = Nothing
390419
391420intToFloat :: [Const] -> Maybe Const
392−intToFloat val | Just i <- getInt val = Just $ Fl (fromIntegral i)
421+intToFloat val | [i] <- getInt val = Just $ Fl (fromIntegral i)
393422intToFloat _ = Nothing
394423
395424floatToInt :: IntTy -> [Const] -> Maybe Const