on master help sign in
Merge pull request #112 from raichoo/master

Data.Bits and primitives

commit0c8d6e7875a0a13b862b77e9b48ba3925bc40cca authorEdwin Brady <edwin.brady@gmail.com> dateFri 30 Nov 2012, 09:20 (14 years ago) parent20627e4e0d0b13
5 files changed+47−6
lib/Data/Bits.idr+25 lib/base.ipkg+1−1 src/IRTS/CodegenC.hs+4 src/IRTS/Lang.hs+2−1 src/Idris/Primitives.hs+15−4
lib/Data/Bits.idrnew file+25
@@ -0,0 +1,25 @@
1+module Data.Bits
2+
3+%access public
4+%default partial
5+
6+infixl 5 .|.
7+infixl 7 .&.
8+
9+class Num a => Bits a where
10+ (.|.) : a -> a -> a
11+ (.&.) : a -> a -> a
12+
13+ xor : a -> a -> a
14+ complement : a -> a
15+ shiftL : a -> Int -> a
16+ shiftR : a -> Int -> a
17+
18+instance Bits Int where
19+ (.|.) = prim__orInt
20+ (.&.) = prim__andInt
21+
22+ xor = prim__xorInt
23+ complement = prim__complInt
24+ shiftL = prim__shLInt
25+ shiftR = prim__shRInt
lib/base.ipkg+1−1
@@ -14,7 +14,7 @@ modules = Builtins, Prelude, IO, System,
1414
1515 Language.Reflection,
1616
17− Data.Morphisms,
17+ Data.Morphisms, Data.Bits,
1818
1919 Control.Monad.Identity, Control.Monad.State, Control.Category,
2020 Control.Arrow
src/IRTS/CodegenC.hs+4
@@ -215,6 +215,10 @@ doOp v LDiv [l, r] = v ++ "INTOP(/," ++ creg l ++ ", " ++ creg r ++ ")"
215215doOp v LMod [l, r] = v ++ "INTOP(%," ++ creg l ++ ", " ++ creg r ++ ")"
216216doOp v LAnd [l, r] = v ++ "INTOP(&," ++ creg l ++ ", " ++ creg r ++ ")"
217217doOp v LOr [l, r] = v ++ "INTOP(|," ++ creg l ++ ", " ++ creg r ++ ")"
218+doOp v LXOr [l, r] = v ++ "INTOP(^," ++ creg l ++ ", " ++ creg r ++ ")"
219+doOp v LSHL [l, r] = v ++ "INTOP(<<," ++ creg l ++ ", " ++ creg r ++ ")"
220+doOp v LSHR [l, r] = v ++ "INTOP(>>," ++ creg l ++ ", " ++ creg r ++ ")"
221+doOp v LCompl [x] = v ++ "INTOP(~," ++ creg x ++ ")"
218222doOp v LEq [l, r] = v ++ "INTOP(==," ++ creg l ++ ", " ++ creg r ++ ")"
219223doOp v LLt [l, r] = v ++ "INTOP(<," ++ creg l ++ ", " ++ creg r ++ ")"
220224doOp v LLe [l, r] = v ++ "INTOP(<=," ++ creg l ++ ", " ++ creg r ++ ")"
src/IRTS/Lang.hs+2−1
@@ -28,7 +28,8 @@ data LExp = LV LVar
2828-- Primitive operators. Backends are not *required* to implement all
2929-- of these, but should report an error if they are unable
3030
31−data PrimFn = LPlus | LMinus | LTimes | LDiv | LMod | LAnd | LOr
31+data PrimFn = LPlus | LMinus | LTimes | LDiv | LMod
32+ | LAnd | LOr | LXOr | LCompl | LSHL| LSHR
3233 | LEq | LLt | LLe | LGt | LGe
3334 | LFPlus | LFMinus | LFTimes | LFDiv
3435 | LFEq | LFLt | LFLe | LFGt | LFGe
src/Idris/Primitives.hs+15−4
@@ -38,14 +38,22 @@ primitives =
3838 (2, LMinus) total,
3939 Prim (UN "prim__mulInt") (ty [IType, IType] IType) 2 (iBin (*))
4040 (2, LTimes) total,
41− Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin (div))
41+ Prim (UN "prim__divInt") (ty [IType, IType] IType) 2 (iBin div)
4242 (2, LDiv) partial,
43− Prim (UN "prim__modInt") (ty [IType, IType] IType) 2 (iBin (mod))
43+ Prim (UN "prim__modInt") (ty [IType, IType] IType) 2 (iBin mod)
4444 (2, LMod) partial,
4545 Prim (UN "prim__andInt") (ty [IType, IType] IType) 2 (iBin (.&.))
4646 (2, LAnd) partial,
4747 Prim (UN "prim__orInt") (ty [IType, IType] IType) 2 (iBin (.|.))
4848 (2, LOr) partial,
49+ Prim (UN "prim__xorInt") (ty [IType, IType] IType) 2 (iBin xor)
50+ (2, LXOr) partial,
51+ Prim (UN "prim__shLInt") (ty [IType, IType] IType) 2 (iBin shiftL)
52+ (2, LSHL) partial,
53+ Prim (UN "prim__shRInt") (ty [IType, IType] IType) 2 (iBin shiftR)
54+ (2, LSHR) partial,
55+ Prim (UN "prim__complInt") (ty [IType] IType) 1 (iUn complement)
56+ (1, LCompl) partial,
4957 Prim (UN "prim__eqInt") (ty [IType, IType] IType) 2 (biBin (==))
5058 (2, LEq) total,
5159 Prim (UN "prim__ltInt") (ty [IType, IType] IType) 2 (biBin (<))
@@ -73,9 +81,9 @@ primitives =
7381 (2, LBMinus) total,
7482 Prim (UN "prim__mulBigInt") (ty [BIType, BIType] BIType) 2 (bBin (*))
7583 (2, LBTimes) total,
76− Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin (div))
84+ Prim (UN "prim__divBigInt") (ty [BIType, BIType] BIType) 2 (bBin div)
7785 (2, LBDiv) partial,
78− Prim (UN "prim__modBigInt") (ty [BIType, BIType] BIType) 2 (bBin (mod))
86+ Prim (UN "prim__modBigInt") (ty [BIType, BIType] BIType) 2 (bBin mod)
7987 (2, LBMod) partial,
8088 Prim (UN "prim__eqBigInt") (ty [BIType, BIType] IType) 2 (bbBin (==))
8189 (2, LBEq) total,
@@ -184,6 +192,9 @@ primitives =
184192p_believeMe [_,_,x] = Just x
185193p_believeMe _ = Nothing
186194
195+iUn op [VConstant (I x)] = Just $ VConstant (I (op x))
196+iUn _ _ = Nothing
197+
187198iBin op [VConstant (I x), VConstant (I y)] = Just $ VConstant (I (op x y))
188199iBin _ _ = Nothing
189200