@@ -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,30 @@ 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]
180−intOps 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
182+intOps ity = intCmps ity ++ intArith ity ++ intConv ity
183+
184+intCmps :: IntTy -> [Prim]
185+intCmps ity =
186+ [ iCmp ity "lt" False (bCmp ity (<)) (LLt . ATInt) total
187+ , iCmp ity "lte" False (bCmp ity (<=)) (LLe . ATInt) total
188+ , iCmp ity "eq" False (bCmp ity (==)) (LEq . ATInt) total
189+ , iCmp ity "gte" False (bCmp ity (>=)) (LGe . ATInt) total
190+ , iCmp ity "gt" False (bCmp ity (>)) (LGt . ATInt) total
191+ ]
192+
193+intArith :: IntTy -> [Prim]
194+intArith ity =
195+ [ iBinOp ity "add" (bitBin ity (+)) (LPlus . ATInt) total
196+ , iBinOp ity "sub" (bitBin ity (-)) (LMinus . ATInt) total
197+ , iBinOp ity "mul" (bitBin ity (*)) (LTimes . ATInt) total
189198 , iBinOp ity "udiv" (bitBin ity div) LUDiv partial
190− , iBinOp ity "sdiv" (bsdiv ity) LSDiv partial
199+ , iBinOp ity "sdiv" (bsdiv ity) (LSDiv . ATInt) partial
191200 , iBinOp ity "urem" (bitBin ity rem) LURem partial
192− , iBinOp ity "srem" (bsrem ity) LSRem partial
201+ , iBinOp ity "srem" (bsrem ity) (LSRem . ATInt) partial
193202 , iBinOp ity "shl" (bitBin ity (\x y -> shiftL x (fromIntegral y))) LSHL total
194203 , iBinOp ity "lshr" (bitBin ity (\x y -> shiftR x (fromIntegral y))) LLSHR total
195204 , iBinOp ity "ashr" (bashr ity) LASHR total
@@ -197,52 +206,98 @@ intOps ity =
197206 , iBinOp ity "or" (bitBin ity (.|.)) LOr total
198207 , iBinOp ity "xor" (bitBin ity (xor)) LXOr total
199208 , iUnOp ity "compl" (bUn ity complement) LCompl total
200− , Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [intTyToConst ity] StrType) 1 intToStr
209+ ]
210+
211+intConv :: IntTy -> [Prim]
212+intConv ity =
213+ [ Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [AType . ATInt $ ity] StrType) 1 intToStr
201214 (1, LIntStr ity) total
202− , Prim (UN $ "prim__fromStr" ++ intTyName ity) (ty [StrType] (intTyToConst ity)) 1 (strToInt ity)
215+ , Prim (UN $ "prim__fromStr" ++ intTyName ity) (ty [StrType] (AType . ATInt $ ity)) 1 (strToInt ity)
203216 (1, LStrInt ity) total
204− , Prim (UN $ "prim__toFloat" ++ intTyName ity) (ty [intTyToConst ity] FlType) 1 intToFloat
217+ , Prim (UN $ "prim__toFloat" ++ intTyName ity) (ty [AType . ATInt $ ity] (AType ATFloat)) 1 intToFloat
205218 (1, LIntFloat ity) total
206− , Prim (UN $ "prim__fromFloat" ++ intTyName ity) (ty [FlType] (intTyToConst ity)) 1 (floatToInt ity)
219+ , Prim (UN $ "prim__fromFloat" ++ intTyName ity) (ty [AType ATFloat] (AType . ATInt $ ity)) 1 (floatToInt ity)
207220 (1, LFloatInt ity) total
208221 ]
209222
223+vecCmps :: IntTy -> [Prim]
224+vecCmps ity =
225+ [ iCmp ity "lt" True (bCmp ity (<)) (LLt . ATInt) total
226+ , iCmp ity "lte" True (bCmp ity (<=)) (LLe . ATInt) total
227+ , iCmp ity "eq" True (bCmp ity (==)) (LEq . ATInt) total
228+ , iCmp ity "gte" True (bCmp ity (>=)) (LGe . ATInt) total
229+ , iCmp ity "gt" True (bCmp ity (>)) (LGt . ATInt) total
230+ ]
231+
232+vecOps :: IntTy -> [Prim]
233+vecOps ity@(ITVec elem count) =
234+ [ Prim (UN $ "prim__mk" ++ intTyName ity)
235+ (ty (replicate count . AType . ATInt . ITFixed $ elem) (AType . ATInt $ ity))
236+ count (mkVecCon elem count) (count, LMkVec elem count) total
237+ , Prim (UN $ "prim__index" ++ intTyName ity)
238+ (ty [AType . ATInt $ ity, AType (ATInt (ITFixed IT32))] (AType . ATInt . ITFixed $ elem))
239+ 2 (mkVecIndex count) (2, LIdxVec elem count) partial -- TODO: Ensure this reduces
240+ , Prim (UN $ "prim__update" ++ intTyName ity)
241+ (ty [AType . ATInt $ ity, AType (ATInt (ITFixed IT32)), AType . ATInt . ITFixed $ elem]
242+ (AType . ATInt $ ity))
243+ 3 (mkVecUpdate elem count) (3, LUpdateVec elem count) partial -- TODO: Ensure this reduces
244+ ] ++ intArith ity ++ vecCmps ity
245+
246+mkVecCon :: NativeTy -> Int -> [Const] -> Maybe Const
247+mkVecCon ity count args
248+ | length ints == count = Just (mkVec ity count ints)
249+ | otherwise = Nothing
250+ where
251+ ints = getInt args
252+ mkVec :: NativeTy -> Int -> [Integer] -> Const
253+ mkVec IT8 len values = B8V $ V.generate len (fromInteger . (values !!))
254+ mkVec IT16 len values = B16V $ V.generate len (fromInteger . (values !!))
255+ mkVec IT32 len values = B32V $ V.generate len (fromInteger . (values !!))
256+ mkVec IT64 len values = B64V $ V.generate len (fromInteger . (values !!))
257+
258+mkVecIndex count [vec, B32 i] = Just (idxVec vec)
259+ where
260+ idxVec :: Const -> Const
261+ idxVec (B8V v) = B8 $ V.unsafeIndex v (fromIntegral i)
262+ idxVec (B16V v) = B16 $ V.unsafeIndex v (fromIntegral i)
263+ idxVec (B32V v) = B32 $ V.unsafeIndex v (fromIntegral i)
264+ idxVec (B64V v) = B64 $ V.unsafeIndex v (fromIntegral i)
265+mkVecIndex _ _ = Nothing
266+
267+mkVecUpdate ity count [vec, B32 i, newElem] = updateVec vec newElem
268+ where
269+ updateVec :: Const -> Const -> Maybe Const
270+ updateVec (B8V v) (B8 e) = Just . B8V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
271+ updateVec (B16V v) (B16 e) = Just . B16V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
272+ updateVec (B32V v) (B32 e) = Just . B32V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
273+ updateVec (B64V v) (B64 e) = Just . B64V $ V.unsafeUpdate v (V.singleton (fromIntegral i, e))
274+
210275intTyName :: IntTy -> String
211276intTyName ITNative = "Int"
212277intTyName ITBig = "BigInt"
213−intTyName sized = "B" ++ show (intTyWidth sized)
214−
215−iCmp, 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
278+intTyName (ITFixed sized) = "B" ++ show (nativeTyWidth sized)
279+intTyName (ITVec ity count) = "B" ++ show (nativeTyWidth ity) ++ "x" ++ show count
280+
281+iCmp :: IntTy -> String -> Bool -> ([Const] -> Maybe Const) -> (IntTy -> PrimFn) -> Totality -> Prim
282+iCmp ity op self impl irop totality
283+ = Prim (UN $ "prim__" ++ op ++ intTyName ity)
284+ (ty (replicate 2 . AType . ATInt $ ity) (AType (ATInt (if self then ity else ITNative))))
285+ 2 impl (2, irop ity) totality
286+
287+iBinOp, iUnOp :: IntTy -> String -> ([Const] -> Maybe Const) -> (IntTy -> PrimFn) -> Totality -> Prim
288+iBinOp ity op impl irop totality
289+ = Prim (UN $ "prim__" ++ op ++ intTyName ity)
290+ (ty (replicate 2 . AType . ATInt $ ity) (AType . ATInt $ ity))
291+ 2 impl (2, irop ity) totality
292+iUnOp ity op impl irop totality
293+ = Prim (UN $ "prim__" ++ op ++ intTyName ity)
294+ (ty [AType . ATInt $ ity] (AType . ATInt $ ity))
295+ 1 impl (1, irop ity) totality
219296
220297iCoerce :: IntTy -> IntTy -> String -> (IntTy -> IntTy -> [Const] -> Maybe Const) -> (IntTy -> IntTy -> PrimFn) -> Prim
221298iCoerce from to op impl irop =
222299 Prim (UN $ "prim__" ++ op ++ intTyName from ++ "_" ++ intTyName to)
223− (ty [intTyToConst from] (intTyToConst to)) 1 (impl from to) (1, irop from to) total
224−
225−iUn :: (Int -> Int) -> [Const] -> Maybe Const
226−iUn op [I x] = Just $ I (op x)
227−iUn _ _ = Nothing
228−
229−iBin :: (Int -> Int -> Int) -> [Const] -> Maybe Const
230−iBin op [I x, I y] = Just $ I (op x y)
231−iBin _ _ = Nothing
232−
233−bBin :: (Integer -> Integer -> Integer) -> [Const] -> Maybe Const
234−bBin op [BI x, BI y] = Just $ BI (op x y)
235−bBin _ _ = Nothing
236−
237−bBini :: (Integer -> Integer -> Int) -> [Const] -> Maybe Const
238−bBini op [BI x, BI y] = Just $ I (op x y)
239−bBini _ _ = Nothing
240−
241−biBin :: (Int -> Int -> Bool) -> [Const] -> Maybe Const
242−biBin op = iBin (\x y -> if (op x y) then 1 else 0)
243−
244−bbBin :: (Integer -> Integer -> Bool) -> [Const] -> Maybe Const
245−bbBin op = bBini (\x y -> if (op x y) then 1 else 0)
300+ (ty [AType . ATInt $ from] (AType . ATInt $ to)) 1 (impl from to) (1, irop from to) total
246301
247302fBin :: (Double -> Double -> Double) -> [Const] -> Maybe Const
248303fBin op [Fl x, Fl y] = Just $ Fl (op x y)
@@ -269,81 +324,133 @@ sBin op [Str x, Str y] = Just $ Str (op x y)
269324sBin _ _ = Nothing
270325
271326bsrem :: IntTy -> [Const] -> Maybe Const
272−bsrem IT8 [B8 x, B8 y]
327+bsrem (ITFixed IT8) [B8 x, B8 y]
273328 = Just $ B8 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int8))
274−bsrem IT16 [B16 x, B16 y]
329+bsrem (ITFixed IT16) [B16 x, B16 y]
275330 = Just $ B16 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int16))
276−bsrem IT32 [B32 x, B32 y]
331+bsrem (ITFixed IT32) [B32 x, B32 y]
277332 = Just $ B32 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int32))
278−bsrem IT64 [B64 x, B64 y]
333+bsrem (ITFixed IT64) [B64 x, B64 y]
279334 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
280335bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
336+bsrem (ITVec IT8 _) [B8V x, B8V y]
337+ = Just . B8V $
338+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int8))) x y
339+bsrem (ITVec IT16 _) [B16V x, B16V y]
340+ = Just . B16V $
341+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int16))) x y
342+bsrem (ITVec IT32 _) [B32V x, B32V y]
343+ = Just . B32V $
344+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int64))) x y
345+bsrem (ITVec IT64 _) [B64V x, B64V y]
346+ = Just . B64V $
347+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int64))) x y
281348bsrem _ _ = Nothing
282349
283350bsdiv :: IntTy -> [Const] -> Maybe Const
284−bsdiv IT8 [B8 x, B8 y]
351+bsdiv (ITFixed IT8) [B8 x, B8 y]
285352 = Just $ B8 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int8))
286−bsdiv IT16 [B16 x, B16 y]
353+bsdiv (ITFixed IT16) [B16 x, B16 y]
287354 = Just $ B16 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int16))
288−bsdiv IT32 [B32 x, B32 y]
355+bsdiv (ITFixed IT32) [B32 x, B32 y]
289356 = Just $ B32 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int32))
290−bsdiv IT64 [B64 x, B64 y]
357+bsdiv (ITFixed IT64) [B64 x, B64 y]
291358 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
292359bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
360+bsdiv (ITVec IT8 _) [B8V x, B8V y]
361+ = Just . B8V $
362+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int8))) x y
363+bsdiv (ITVec IT16 _) [B16V x, B16V y]
364+ = Just . B16V $
365+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int16))) x y
366+bsdiv (ITVec IT32 _) [B32V x, B32V y]
367+ = Just . B32V $
368+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int64))) x y
369+bsdiv (ITVec IT64 _) [B64V x, B64V y]
370+ = Just . B64V $
371+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int64))) x y
293372bsdiv _ _ = Nothing
294373
295374bashr :: IntTy -> [Const] -> Maybe Const
296−bashr IT8 [B8 x, B8 y]
375+bashr (ITFixed IT8) [B8 x, B8 y]
297376 = Just $ B8 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int8))
298−bashr IT16 [B16 x, B16 y]
377+bashr (ITFixed IT16) [B16 x, B16 y]
299378 = Just $ B16 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int16))
300−bashr IT32 [B32 x, B32 y]
379+bashr (ITFixed IT32) [B32 x, B32 y]
301380 = Just $ B32 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int32))
302−bashr IT64 [B64 x, B64 y]
381+bashr (ITFixed IT64) [B64 x, B64 y]
303382 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
304383bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
384+bashr (ITVec IT8 _) [B8V x, B8V y]
385+ = Just . B8V $
386+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int8))) x y
387+bashr (ITVec IT16 _) [B16V x, B16V y]
388+ = Just . B16V $
389+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int16))) x y
390+bashr (ITVec IT32 _) [B32V x, B32V y]
391+ = Just . B32V $
392+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int64))) x y
393+bashr (ITVec IT64 _) [B64V x, B64V y]
394+ = Just . B64V $
395+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int64))) x y
305396bashr _ _ = Nothing
306397
307398bUn :: 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)
399+bUn (ITFixed IT8) op [B8 x] = Just $ B8 (op x)
400+bUn (ITFixed IT16) op [B16 x] = Just $ B16 (op x)
401+bUn (ITFixed IT32) op [B32 x] = Just $ B32 (op x)
402+bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
312403bUn ITBig op [BI x] = Just $ BI (op x)
313404bUn ITNative op [I x] = Just $ I (op x)
405+bUn (ITVec IT8 _) op [B8V x] = Just . B8V $ V.map op x
406+bUn (ITVec IT16 _) op [B16V x] = Just . B16V $ V.map op x
407+bUn (ITVec IT32 _) op [B32V x] = Just . B32V $ V.map op x
408+bUn (ITVec IT64 _) op [B64V x] = Just . B64V $ V.map op x
314409bUn _ _ _ = Nothing
315410
316411bitBin :: 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)
412+bitBin (ITFixed IT8) op [B8 x, B8 y] = Just $ B8 (op x y)
413+bitBin (ITFixed IT16) op [B16 x, B16 y] = Just $ B16 (op x y)
414+bitBin (ITFixed IT32) op [B32 x, B32 y] = Just $ B32 (op x y)
415+bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
321416bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
322417bitBin ITNative op [I x, I y] = Just $ I (op x y)
418+bitBin (ITVec IT8 _) op [B8V x, B8V y] = Just . B8V $ V.zipWith op x y
419+bitBin (ITVec IT16 _) op [B16V x, B16V y] = Just . B16V $ V.zipWith op x y
420+bitBin (ITVec IT32 _) op [B32V x, B32V y] = Just . B32V $ V.zipWith op x y
421+bitBin (ITVec IT64 _) op [B64V x, B64V y] = Just . B64V $ V.zipWith op x y
323422bitBin _ _ _ = Nothing
324423
325424bCmp :: 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)
425+bCmp (ITFixed IT8) op [B8 x, B8 y] = Just $ I (if (op x y) then 1 else 0)
426+bCmp (ITFixed IT16) op [B16 x, B16 y] = Just $ I (if (op x y) then 1 else 0)
427+bCmp (ITFixed IT32) op [B32 x, B32 y] = Just $ I (if (op x y) then 1 else 0)
428+bCmp (ITFixed IT64) op [B64 x, B64 y] = Just $ I (if (op x y) then 1 else 0)
330429bCmp ITBig op [BI x, BI y] = Just $ I (if (op x y) then 1 else 0)
331430bCmp ITNative op [I x, I y] = Just $ I (if (op x y) then 1 else 0)
431+bCmp (ITVec IT8 _) op [B8V x, B8V y]
432+ = Just . B8V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
433+bCmp (ITVec IT16 _) op [B16V x, B16V y]
434+ = Just . B16V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
435+bCmp (ITVec IT32 _) op [B32V x, B32V y]
436+ = Just . B32V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
437+bCmp (ITVec IT64 _) op [B64V x, B64V y]
438+ = Just . B64V . V.map (\b -> if b then -1 else 0) $ V.zipWith op x y
332439bCmp _ _ _ = Nothing
333440
334441toInt :: 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)
442+toInt (ITFixed IT8) x = B8 (fromIntegral x)
443+toInt (ITFixed IT16) x = B16 (fromIntegral x)
444+toInt (ITFixed IT32) x = B32 (fromIntegral x)
445+toInt (ITFixed IT64) x = B64 (fromIntegral x)
339446toInt ITBig x = BI (fromIntegral x)
340447toInt ITNative x = I (fromIntegral x)
341448
342449intToInt :: 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
450+intToInt (ITFixed IT8) out [B8 x] = Just $ toInt out x
451+intToInt (ITFixed IT16) out [B16 x] = Just $ toInt out x
452+intToInt (ITFixed IT32) out [B32 x] = Just $ toInt out x
453+intToInt (ITFixed IT64) out [B64 x] = Just $ toInt out x
347454intToInt ITBig out [BI x] = Just $ toInt out x
348455intToInt ITNative out [I x] = Just $ toInt out x
349456intToInt _ _ _ = Nothing
@@ -356,10 +463,10 @@ zext from to val
356463zext _ _ _ = Nothing
357464
358465sext :: 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)
466+sext (ITFixed IT8) out [B8 x] = Just $ toInt out (fromIntegral x :: Int8)
467+sext (ITFixed IT16) out [B16 x] = Just $ toInt out (fromIntegral x :: Int16)
468+sext (ITFixed IT32) out [B32 x] = Just $ toInt out (fromIntegral x :: Int32)
469+sext (ITFixed IT64) out [B64 x] = Just $ toInt out (fromIntegral x :: Int64)
363470sext ITBig _ _ = Nothing
364471sext from to val = intToInt from to val
365472
@@ -369,19 +476,19 @@ trunc _ ITBig _ = Nothing
369476trunc from to val | intTyWidth from > intTyWidth to = intToInt from to val
370477trunc _ _ _ = Nothing
371478
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−
381479intToStr :: [Const] -> Maybe Const
382−intToStr val | Just i <- getInt val = Just $ Str (show i)
480+intToStr val | [i] <- getInt val = Just $ Str (show i)
383481intToStr _ = Nothing
384482
483+getInt :: [Const] -> [Integer]
484+getInt (B8 x : xs) = toInteger x : getInt xs
485+getInt (B16 x : xs) = toInteger x : getInt xs
486+getInt (B32 x : xs) = toInteger x : getInt xs
487+getInt (B64 x : xs) = toInteger x : getInt xs
488+getInt (I x : xs) = toInteger x : getInt xs
489+getInt (BI x : xs) = x : getInt xs
490+getInt _ = []
491+
385492strToInt :: IntTy -> [Const] -> Maybe Const
386493strToInt ity [Str x] = case reads x of
387494 [(n,"")] -> Just $ toInt ity (n :: Integer)
@@ -389,7 +496,7 @@ strToInt ity [Str x] = case reads x of
389496strToInt _ _ = Nothing
390497
391498intToFloat :: [Const] -> Maybe Const
392−intToFloat val | Just i <- getInt val = Just $ Fl (fromIntegral i)
499+intToFloat val | [i] <- getInt val = Just $ Fl (fromIntegral i)
393500intToFloat _ = Nothing
394501
395502floatToInt :: IntTy -> [Const] -> Maybe Const