Skip to content

Commit

Permalink
Delay the resolution of casts so that llvm_pointer_cast can be a pu…
Browse files Browse the repository at this point in the history
…re function,

similar to `llvm_field`, `llvm_elem`, etc.
  • Loading branch information
robdockins committed Feb 8, 2022
1 parent 39b247a commit 4dca5af
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions crux-mir-comp/src/Mir/Compositional/MethodSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type instance MS.MethodId MIR = DefId
type instance MS.AllocSpec MIR = Some MirAllocSpec
type instance MS.PointsTo MIR = MirPointsTo
type instance MS.ResolvedState MIR = ()
type instance MS.CastType MIR = ()

type instance MS.Codebase MIR = CollectionState

Expand Down
9 changes: 6 additions & 3 deletions src/SAWScript/Crucible/Common/MethodSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type family TypeName ext :: Type
-- | The type of types of the syntax extension we're dealing with
type family ExtType ext :: Type

-- | The types that can appear in casts
type family CastType ext :: Type

-- | The type of points-to assertions
type family PointsTo ext :: Type

Expand Down Expand Up @@ -123,7 +126,7 @@ data SetupValue ext where
SetupArray :: B (HasSetupArray ext) -> [SetupValue ext] -> SetupValue ext
SetupElem :: B (HasSetupElem ext) -> SetupValue ext -> Int -> SetupValue ext
SetupField :: B (HasSetupField ext) -> SetupValue ext -> String -> SetupValue ext
SetupCast :: B (HasSetupCast ext) -> SetupValue ext -> ExtType ext -> SetupValue ext
SetupCast :: B (HasSetupCast ext) -> SetupValue ext -> CastType ext -> SetupValue ext

-- | A pointer to a global variable
SetupGlobal :: B (HasSetupGlobal ext) -> String -> SetupValue ext
Expand All @@ -143,7 +146,7 @@ type SetupValueHas (c :: Type -> Constraint) ext =
, c (B (HasSetupCast ext))
, c (B (HasSetupGlobal ext))
, c (B (HasSetupGlobalInitializer ext))
, c (ExtType ext)
, c (CastType ext)
)

deriving instance (SetupValueHas Show ext) => Show (SetupValue ext)
Expand All @@ -156,7 +159,7 @@ deriving instance (SetupValueHas Show ext) => Show (SetupValue ext)
-- are implementation details and won't be familiar to users.
-- Consider using 'resolveSetupValue' and printing an 'LLVMVal'
-- with @PP.pretty@ instead.
ppSetupValue :: Show (ExtType ext) => SetupValue ext -> PP.Doc ann
ppSetupValue :: Show (CastType ext) => SetupValue ext -> PP.Doc ann
ppSetupValue setupval = case setupval of
SetupTerm tm -> ppTypedTerm tm
SetupVar i -> ppAllocIndex i
Expand Down
2 changes: 1 addition & 1 deletion src/SAWScript/Crucible/JVM/MethodSpecIR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type JIdent = String -- FIXME(huffman): what to put here?
type instance MS.TypeName CJ.JVM = JIdent

type instance MS.ExtType CJ.JVM = J.Type

type instance MS.CastType CJ.JVM = ()
type instance MS.ResolvedState CJ.JVM = ()

--------------------------------------------------------------------------------
Expand Down
10 changes: 2 additions & 8 deletions src/SAWScript/Crucible/LLVM/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,14 +2103,8 @@ llvm_fresh_pointer lty =
memTy <- memTypeForLLVMType loc lty
constructFreshPointer (llvmTypeAlias lty) loc memTy

llvm_cast_pointer :: AllLLVM SetupValue -> L.Type -> LLVMCrucibleSetupM (AllLLVM SetupValue)
llvm_cast_pointer ptr lty =
LLVMCrucibleSetupM $
do cctx <- getLLVMCrucibleContext
let ?lc = ccTypeCtx cctx
loc <- getW4Position "llvm_cast_pointer"
memTy <- memTypeForLLVMType loc (L.PtrTo lty)
pure (mkAllLLVM (SetupCast () (getAllLLVM ptr) memTy))
llvm_cast_pointer :: AllLLVM SetupValue -> L.Type -> AllLLVM SetupValue
llvm_cast_pointer ptr lty = mkAllLLVM (SetupCast () (getAllLLVM ptr) lty)

constructFreshPointer ::
Crucible.HasPtrWidth (Crucible.ArchWidth arch) =>
Expand Down
4 changes: 2 additions & 2 deletions src/SAWScript/Crucible/LLVM/MethodSpecIR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type instance MS.HasGhostState (LLVM _) = 'True

type instance MS.TypeName (LLVM arch) = CL.Ident
type instance MS.ExtType (LLVM arch) = CL.MemType

type instance MS.CastType (LLVM arch) = L.Type

--------------------------------------------------------------------------------
-- *** LLVMMethodId
Expand Down Expand Up @@ -619,7 +619,7 @@ type instance MS.ResolvedState (LLVM arch) = LLVMResolvedState
data ResolvedPathItem
= ResolvedField String
| ResolvedElem Int
| ResolvedCast CL.MemType
| ResolvedCast L.Type
deriving (Show, Eq, Ord)

type ResolvedPath = [ResolvedPathItem]
Expand Down
13 changes: 10 additions & 3 deletions src/SAWScript/Crucible/LLVM/ResolveSetupValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ resolveSetupValueInfo cc env nameEnv v =
| Just alias <- Map.lookup i nameEnv
-> L.Pointer (L.guessAliasInfo mdMap alias)

SetupCast () _ (Crucible.PtrType (Crucible.Alias alias))
SetupCast () _ (L.Alias alias)
-> L.Pointer (L.guessAliasInfo mdMap alias)

SetupField () a n ->
Expand Down Expand Up @@ -332,10 +332,17 @@ typeOfSetupValue' cc env nameEnv val =
, "instead got:"
, show (ppTypedTermType tp)
]
SetupCast () v tp -> -- INVARIANT: tp is a pointer type
SetupCast () v ltp ->
do memTy <- typeOfSetupValue cc env nameEnv v
case memTy of
Crucible.PtrType _symTy -> return tp
Crucible.PtrType _symTy ->
case let ?lc = lc in Crucible.liftMemType (L.PtrTo ltp) of
Left err -> fail $ unlines [ "typeOfSetupValue: invalid type " ++ show ltp
, "Details:"
, err
]
Right mt -> return mt

_ -> fail $ unwords $
[ "typeOfSetupValue: tried to cast the type of a non-pointer value"
, "actual type of value: " ++ show memTy
Expand Down
2 changes: 1 addition & 1 deletion src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,7 +2603,7 @@ primitives = Map.fromList
Current
[ "Legacy alternative name for `llvm_return`." ]

, prim "llvm_cast_pointer" "SetupValue -> LLVMType -> LLVMSetup SetupValue"
, prim "llvm_cast_pointer" "SetupValue -> LLVMType -> SetupValue"
(pureVal llvm_cast_pointer)
Current
[ "Cast the type of the given setup value (which must be a pointer value)."
Expand Down

0 comments on commit 4dca5af

Please sign in to comment.