@@ -11,6 +11,8 @@ import IRTS.Lang
1111import Core.TT
1212import Core.Evaluate
1313import Data.Bits
14+import Data.Word
15+import Data.Int
1416
1517data Prim = Prim { p_name :: Name,
1618 p_type :: Type,
@@ -75,24 +77,90 @@ primitives =
7577 Prim (UN "prim__gteChar") (ty [ChType, ChType] IType) 2 (bcBin (>=))
7678 (2, LGe) total,
7779
78− Prim (UN "prim__addW8") (ty [W8Type, W8Type] W8Type) 2 (w8Bin (+))
79− (2, LW8Plus) total,
80− Prim (UN "prim__subW8") (ty [W8Type, W8Type] W8Type) 2 (w8Bin (-))
81− (2, LW8Minus) total,
82− Prim (UN "prim__mulW8") (ty [W8Type, W8Type] W8Type) 2 (w8Bin (*))
83− (2, LW8Times) total,
84−
85− Prim (UN "prim__addW16") (ty [W16Type, W16Type] W16Type) 2 (w16Bin (+))
86− (2, LW16Plus) total,
87− Prim (UN "prim__subW16") (ty [W16Type, W16Type] W16Type) 2 (w16Bin (-))
88− (2, LW16Minus) total,
89− Prim (UN "prim__mulW16") (ty [W16Type, W16Type] W16Type) 2 (w16Bin (*))
90− (2, LW16Times) total,
91−
92− Prim (UN "prim__intToWord8") (ty [IType] W8Type) 1 intToWord8
93− (1, LW8) total,
94− Prim (UN "prim__intToWord16") (ty [IType] W16Type) 1 intToWord16
95− (1, LW16) total,
80+ Prim (UN "prim__addB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin (+))
81+ (2, LB8Plus) total,
82+ Prim (UN "prim__subB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin (-))
83+ (2, LB8Minus) total,
84+ Prim (UN "prim__mulB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin (*))
85+ (2, LB8Times) total,
86+ Prim (UN "prim__udivB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin div)
87+ (2, LB8UDiv) total,
88+ Prim (UN "prim__sdivB8") (ty [B8Type, B8Type] B8Type) 2 (b8Bin s8div)
89+ (2, LB8SDiv) total,
90+ Prim (UN "prim__zextB8_16") (ty [B8Type] B16Type) 1 zext8_16
91+ (1, LB8Z16) total,
92+ Prim (UN "prim__zextB8_32") (ty [B8Type] B32Type) 1 zext8_32
93+ (1, LB8Z32) total,
94+ Prim (UN "prim__zextB8_64") (ty [B8Type] B64Type) 1 zext8_64
95+ (1, LB8Z64) total,
96+ Prim (UN "prim__sextB8_16") (ty [B8Type] B16Type) 1 sext8_16
97+ (1, LB8Z16) total,
98+ Prim (UN "prim__sextB8_32") (ty [B8Type] B32Type) 1 sext8_32
99+ (1, LB8Z32) total,
100+ Prim (UN "prim__sextB8_64") (ty [B8Type] B64Type) 1 sext8_64
101+ (1, LB8Z64) total,
102+
103+ Prim (UN "prim__addB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin (+))
104+ (2, LB16Plus) total,
105+ Prim (UN "prim__subB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin (-))
106+ (2, LB16Minus) total,
107+ Prim (UN "prim__mulB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin (*))
108+ (2, LB16Times) total,
109+ Prim (UN "prim__udivB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin div)
110+ (2, LB16UDiv) total,
111+ Prim (UN "prim__sdivB16") (ty [B16Type, B16Type] B16Type) 2 (b16Bin s16div)
112+ (2, LB16SDiv) total,
113+ Prim (UN "prim__zextB16_32") (ty [B16Type] B32Type) 1 zext16_32
114+ (1, LB16Z32) total,
115+ Prim (UN "prim__zextB16_64") (ty [B16Type] B64Type) 1 zext16_64
116+ (1, LB16Z64) total,
117+ Prim (UN "prim__sextB16_32") (ty [B16Type] B32Type) 1 sext16_32
118+ (1, LB16Z32) total,
119+ Prim (UN "prim__sextB16_64") (ty [B16Type] B64Type) 1 sext16_64
120+ (1, LB16Z64) total,
121+ Prim (UN "prim__truncB16_8") (ty [B16Type] B8Type) 1 trunc16_8
122+ (1, LB16T8) total,
123+
124+ Prim (UN "prim__addB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin (+))
125+ (2, LB32Plus) total,
126+ Prim (UN "prim__subB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin (-))
127+ (2, LB32Minus) total,
128+ Prim (UN "prim__mulB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin (*))
129+ (2, LB32Times) total,
130+ Prim (UN "prim__udivB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin div)
131+ (2, LB32UDiv) total,
132+ Prim (UN "prim__sdivB32") (ty [B32Type, B32Type] B32Type) 2 (b32Bin s32div)
133+ (2, LB32SDiv) total,
134+ Prim (UN "prim__zextB32_64") (ty [B32Type] B64Type) 1 zext32_64
135+ (1, LB32Z64) total,
136+ Prim (UN "prim__sextB32_64") (ty [B32Type] B64Type) 1 sext32_64
137+ (1, LB32Z64) total,
138+ Prim (UN "prim__truncB32_8") (ty [B32Type] B8Type) 1 trunc32_8
139+ (1, LB32T8) total,
140+ Prim (UN "prim__truncB32_16") (ty [B32Type] B16Type) 1 trunc32_16
141+ (1, LB32T16) total,
142+
143+ Prim (UN "prim__addB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin (+))
144+ (2, LB64Plus) total,
145+ Prim (UN "prim__subB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin (-))
146+ (2, LB64Minus) total,
147+ Prim (UN "prim__mulB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin (*))
148+ (2, LB64Times) total,
149+ Prim (UN "prim__udivB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin div)
150+ (2, LB64UDiv) total,
151+ Prim (UN "prim__sdivB64") (ty [B64Type, B64Type] B64Type) 2 (b64Bin s64div)
152+ (2, LB64SDiv) total,
153+ Prim (UN "prim__truncB64_8") (ty [B64Type] B8Type) 1 trunc64_8
154+ (1, LB64T8) total,
155+ Prim (UN "prim__truncB64_16") (ty [B64Type] B16Type) 1 trunc64_16
156+ (1, LB64T16) total,
157+ Prim (UN "prim__truncB64_32") (ty [B64Type] B32Type) 1 trunc64_32
158+ (1, LB64T32) total,
159+
160+ Prim (UN "prim__intToBits8") (ty [IType] B8Type) 1 intToBits8
161+ (1, LB8) total,
162+ Prim (UN "prim__intToBits16") (ty [IType] B16Type) 1 intToBits16
163+ (1, LB16) total,
96164
97165 Prim (UN "prim__addBigInt") (ty [BIType, BIType] BIType) 2 (bBin (+))
98166 (2, LBPlus) total,
@@ -245,19 +313,93 @@ bsBin _ _ = Nothing
245313sBin op [VConstant (Str x), VConstant (Str y)] = Just $ VConstant (Str (op x y))
246314sBin _ _ = Nothing
247315
248−w8Bin op [VConstant (W8 x), VConstant (W8 y)] = Just $ VConstant (W8 (op x y))
249−w8Bin _ _ = Nothing
316+s8div :: Word8 -> Word8 -> Word8
317+s8div x y = fromIntegral (fromIntegral x `div` fromIntegral y :: Int8)
250318
251−w16Bin op [VConstant (W16 x), VConstant (W16 y)] = Just $ VConstant (W16 (op x y))
252−w16Bin _ _ = Nothing
319+s16div :: Word16 -> Word16 -> Word16
320+s16div x y = fromIntegral (fromIntegral x `div` fromIntegral y :: Int16)
253321
254−-- w32Bin op [VConstant (W32 x), VConstant (W32 y)] = Just $ VConstant (W32 (op x y))
255−-- w32Bin _ _ = Nothing
322+s32div :: Word32 -> Word32 -> Word32
323+s32div x y = fromIntegral (fromIntegral x `div` fromIntegral y :: Int32)
256324
257−intToWord8 [VConstant (I x)] = Just $ VConstant (W8 $ fromIntegral x)
258−intToWord8 _ = Nothing
259−intToWord16 [VConstant (I x)] = Just $ VConstant (W16 $ fromIntegral x)
260−intToWord16 _ = Nothing
325+s64div :: Word64 -> Word64 -> Word64
326+s64div x y = fromIntegral (fromIntegral x `div` fromIntegral y :: Int64)
327+
328+b8Bin op [VConstant (B8 x), VConstant (B8 y)] = Just $ VConstant (B8 (op x y))
329+b8Bin _ _ = Nothing
330+
331+b16Bin op [VConstant (B16 x), VConstant (B16 y)] = Just $ VConstant (B16 (op x y))
332+b16Bin _ _ = Nothing
333+
334+b32Bin op [VConstant (B32 x), VConstant (B32 y)] = Just $ VConstant (B32 (op x y))
335+b32Bin _ _ = Nothing
336+
337+b64Bin op [VConstant (B64 x), VConstant (B64 y)] = Just $ VConstant (B64 (op x y))
338+b64Bin _ _ = Nothing
339+
340+toB8 x = VConstant (B8 (fromIntegral x))
341+toB16 x = VConstant (B16 (fromIntegral x))
342+toB32 x = VConstant (B32 (fromIntegral x))
343+toB64 x = VConstant (B64 (fromIntegral x))
344+
345+zext8_16 [VConstant (B8 x)] = Just $ toB16 x
346+zext8_16 _ = Nothing
347+
348+zext8_32 [VConstant (B8 x)] = Just $ toB32 x
349+zext8_32 _ = Nothing
350+
351+zext8_64 [VConstant (B8 x)] = Just $ toB64 x
352+zext8_64 _ = Nothing
353+
354+sext8_16 [VConstant (B8 x)] = Just $ toB16 (fromIntegral x :: Int8)
355+sext8_16 _ = Nothing
356+
357+sext8_32 [VConstant (B8 x)] = Just $ toB32 (fromIntegral x :: Int8)
358+sext8_32 _ = Nothing
359+
360+sext8_64 [VConstant (B8 x)] = Just $ toB64 (fromIntegral x :: Int8)
361+sext8_64 _ = Nothing
362+
363+zext16_32 [VConstant (B16 x)] = Just $ toB32 x
364+zext16_32 _ = Nothing
365+
366+zext16_64 [VConstant (B16 x)] = Just $ toB64 x
367+zext16_64 _ = Nothing
368+
369+sext16_32 [VConstant (B16 x)] = Just $ toB32 (fromIntegral x :: Int16)
370+sext16_32 _ = Nothing
371+
372+sext16_64 [VConstant (B16 x)] = Just $ toB64 (fromIntegral x :: Int16)
373+sext16_64 _ = Nothing
374+
375+trunc16_8 [VConstant (B16 x)] = Just $ toB8 x
376+trunc16_8 _ = Nothing
377+
378+zext32_64 [VConstant (B32 x)] = Just $ toB64 x
379+zext32_64 _ = Nothing
380+
381+sext32_64 [VConstant (B32 x)] = Just $ toB64 (fromIntegral x :: Int32)
382+sext32_64 _ = Nothing
383+
384+trunc32_8 [VConstant (B32 x)] = Just $ toB8 x
385+trunc32_8 _ = Nothing
386+
387+trunc32_16 [VConstant (B32 x)] = Just $ toB16 x
388+trunc32_16 _ = Nothing
389+
390+trunc64_8 [VConstant (B64 x)] = Just $ toB8 x
391+trunc64_8 _ = Nothing
392+
393+trunc64_16 [VConstant (B64 x)] = Just $ toB16 x
394+trunc64_16 _ = Nothing
395+
396+trunc64_32 [VConstant (B64 x)] = Just $ toB32 x
397+trunc64_32 _ = Nothing
398+
399+intToBits8 [VConstant (I x)] = Just $ toB8 x
400+intToBits8 _ = Nothing
401+intToBits16 [VConstant (I x)] = Just $ toB16 x
402+intToBits16 _ = Nothing
261403
262404c_intToStr [VConstant (I x)] = Just $ VConstant (Str (show x))
263405c_intToStr _ = Nothing