Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a Heapster command to introduce new primitive functions #1384

Merged
merged 2 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/SAWScript/HeapsterBuiltins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module SAWScript.HeapsterBuiltins
, heapster_find_trait_method_symbol
, heapster_assume_fun
, heapster_assume_fun_rename
, heapster_assume_fun_rename_prim
, heapster_assume_fun_multi
, heapster_print_fun_trans
, heapster_export_coq
Expand Down Expand Up @@ -869,6 +870,53 @@ heapster_assume_fun_rename _bic _opts henv nm nm_to perms_string term_string =
(globalOpenTerm term_ident)
liftIO $ writeIORef (heapsterEnvPermEnvRef henv) env''

-- | Create a new SAW core primitive named @nm@ with type @tp@ in the module
-- associated with the supplied Heapster environment, and return its identifier
insPrimitive :: HeapsterEnv -> String -> Term -> TopLevel Ident
insPrimitive henv nm tp =
do sc <- getSharedContext
let mnm = heapsterEnvSAWModule henv
let ident = mkSafeIdent mnm nm
i <- liftIO $ scFreshGlobalVar sc
liftIO $ scRegisterName sc i (ModuleIdentifier ident)
let pn = PrimName i ident tp
t <- liftIO $ scFlatTermF sc (Primitive pn)
liftIO $ scRegisterGlobal sc ident t
liftIO $ scModifyModule sc mnm $ \m ->
insDef m $ Def { defIdent = ident,
defQualifier = PrimQualifier,
defType = tp,
defBody = Nothing }
return ident

-- | Assume that the given named function has the supplied type and translates
-- to a SAW core definition given by the second name
heapster_assume_fun_rename_prim :: BuiltinContext -> Options -> HeapsterEnv ->
String -> String -> String -> TopLevel ()
heapster_assume_fun_rename_prim _bic _opts henv nm nm_to perms_string =
do Some lm <- failOnNothing ("Could not find symbol: " ++ nm)
(lookupModContainingSym henv nm)
sc <- getSharedContext
let w = llvmModuleArchReprWidth lm
leq_proof <- case decideLeq (knownNat @1) w of
Left pf -> return pf
Right _ -> fail "LLVM arch width is 0!"
env <- liftIO $ readIORef $ heapsterEnvPermEnvRef henv
(Some cargs, Some ret) <- lookupFunctionType lm nm
let args = mkCruCtx cargs
withKnownNat w $ withLeqProof leq_proof $ do
SomeFunPerm fun_perm <-
parseFunPermStringMaybeRust "permissions" w env args ret perms_string
env' <- liftIO $ readIORef (heapsterEnvPermEnvRef henv)
fun_typ <- liftIO $ translateCompleteFunPerm sc env fun_perm
term_ident <- insPrimitive henv nm_to fun_typ
let env'' = permEnvAddGlobalSymFun env'
(GlobalSymbol $ fromString nm)
w
fun_perm
(globalOpenTerm term_ident)
liftIO $ writeIORef (heapsterEnvPermEnvRef henv) env''

-- | Assume that the given named function has the supplied type and translates
-- to a SAW core definition given by name
heapster_assume_fun :: BuiltinContext -> Options -> HeapsterEnv ->
Expand Down
9 changes: 9 additions & 0 deletions src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3187,6 +3187,15 @@ primitives = Map.fromList
, " trans is not an identifier then it is bound to the defined name nm_to."
]

, prim "heapster_assume_fun_rename_prim"
"HeapsterEnv -> String -> String -> String -> TopLevel HeapsterEnv"
(bicVal heapster_assume_fun_rename_prim)
Experimental
[
"heapster_assume_fun_rename_prim nm nm_to perms assumes that function nm"
, " has permissions perms as a primitive."
]

, prim "heapster_assume_fun_multi"
"HeapsterEnv -> String -> [(String, String)] -> TopLevel HeapsterEnv"
(bicVal heapster_assume_fun_multi)
Expand Down