Skip to content

Commit

Permalink
Throw unsupported primitive exception. (#55)
Browse files Browse the repository at this point in the history
* Throw unsupported primitive exception.

* Address comments.
  • Loading branch information
andreistefanescu authored May 26, 2020
1 parent f6ed78a commit c00be00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/Verifier/SAW/Prim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ lg2rem n = (k+1, 2*d+r)
data EvalError
= InvalidIndex Integer
| DivideByZero
| UnsupportedPrimitive String String
| UserError String
deriving (Eq, Typeable)

Expand All @@ -280,6 +281,7 @@ instance Show EvalError where
show e = case e of
InvalidIndex i -> "invalid sequence index: " ++ show i
DivideByZero -> "division by 0"
UnsupportedPrimitive b p -> "unsupported primitive " ++ p ++ " in " ++ b ++ " backend"
UserError msg -> "Run-time error: " ++ msg

-- | A sequencing operation has gotten an invalid index.
Expand All @@ -290,6 +292,11 @@ invalidIndex i = X.throw (InvalidIndex i)
divideByZero :: a
divideByZero = X.throw DivideByZero

-- | A backend with a unsupported primitive.
unsupportedPrimitive :: String -> String -> a
unsupportedPrimitive backend primitive =
X.throw $ UnsupportedPrimitive backend primitive

-- | For `error`
userError :: String -> a
userError msg = X.throw (UserError msg)
Expand Down
5 changes: 4 additions & 1 deletion src/Verifier/SAW/Simulator/Concrete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ prims =
, Prims.bpBvPopcount = pure1 (Prim.bvPopcount undefined)
, Prims.bpBvCountLeadingZeros = pure1 (Prim.bvCountLeadingZeros undefined)
, Prims.bpBvCountTrailingZeros = pure1 (Prim.bvCountTrailingZeros undefined)
, Prims.bpBvForall = error "bvForall unimplemented for backend"
, Prims.bpBvForall = unsupportedConcretePrimitive "bvForall"

-- Integer operations
, Prims.bpIntAdd = pure2 (+)
Expand All @@ -226,6 +226,9 @@ prims =
, Prims.bpIntMax = pure2 max
}

unsupportedConcretePrimitive :: String -> a
unsupportedConcretePrimitive = Prim.unsupportedPrimitive "concrete"

constMap :: Map Ident CValue
constMap =
flip Map.union (Prims.constMap prims) $
Expand Down
15 changes: 9 additions & 6 deletions src/Verifier/SAW/Simulator/RME.hs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ prims =
, Prims.bpBvURem = pure2 RMEV.urem
, Prims.bpBvSDiv = pure2 RMEV.sdiv
, Prims.bpBvSRem = pure2 RMEV.srem
, Prims.bpBvLg2 = undefined--pure1 Prim.bvLg2
, Prims.bpBvLg2 = unsupportedRMEPrimitive "bpBvLg2"
-- Bitvector comparisons
, Prims.bpBvEq = pure2 RMEV.eq
, Prims.bpBvsle = pure2 RMEV.sle
Expand All @@ -198,7 +198,7 @@ prims =
, Prims.bpBvPopcount = pure1 RMEV.popcount
, Prims.bpBvCountLeadingZeros = pure1 RMEV.countLeadingZeros
, Prims.bpBvCountTrailingZeros = pure1 RMEV.countTrailingZeros
, Prims.bpBvForall = error "bvForall unimplemented for backend"
, Prims.bpBvForall = unsupportedRMEPrimitive "bvForall"
-- Integer operations
, Prims.bpIntAdd = pure2 (+)
, Prims.bpIntSub = pure2 (-)
Expand All @@ -210,10 +210,13 @@ prims =
, Prims.bpIntEq = pure2 (\x y -> RME.constant (x == y))
, Prims.bpIntLe = pure2 (\x y -> RME.constant (x <= y))
, Prims.bpIntLt = pure2 (\x y -> RME.constant (x < y))
, Prims.bpIntMin = undefined--pure2 min
, Prims.bpIntMax = undefined--pure2 max
, Prims.bpIntMin = unsupportedRMEPrimitive "bpIntMin"
, Prims.bpIntMax = unsupportedRMEPrimitive "bpIntMax"
}

unsupportedRMEPrimitive :: String -> a
unsupportedRMEPrimitive = Prim.unsupportedPrimitive "RME"

constMap :: Map Ident RValue
constMap =
Map.union (Prims.constMap prims) $
Expand Down Expand Up @@ -246,11 +249,11 @@ constMap =

-- primitive bvToInt :: (n::Nat) -> bitvector n -> Integer;
bvToIntOp :: RValue
bvToIntOp = undefined -- constFun $ wordFun $ VInt . unsigned
bvToIntOp = unsupportedRMEPrimitive "bvToIntOp"

-- primitive sbvToInt :: (n::Nat) -> bitvector n -> Integer;
sbvToIntOp :: RValue
sbvToIntOp = undefined -- constFun $ wordFun $ VInt . signed
sbvToIntOp = unsupportedRMEPrimitive "sbvToIntOp"

-- primitive intToBv :: (n::Nat) -> Integer -> bitvector n;
intToBvOp :: RValue
Expand Down

0 comments on commit c00be00

Please sign in to comment.