Skip to content

Commit

Permalink
Permit axiom without a compile block
Browse files Browse the repository at this point in the history
An axiom without a compile block generates a C function signature
without a corresponding body. This allows implementations to be injected
at link time (JS, Anoma).
  • Loading branch information
paulcadman committed Jul 27, 2022
1 parent 4a7009a commit 208d0c8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/Juvix/Translation/MicroJuvixToMiniC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ entryMiniC i = MiniCResult . serialize <$> cunitResult
let buildTable = Micro.buildTable [m]
let defs =
genStructDefs m
<> run (runReader compileInfo (genAxioms m))
<> run (runReader compileInfo (runReader buildTable (genAxioms m)))
<> run (runReader buildTable (genCTypes m))
<> run (runReader buildTable (runReader typesTable (genFunctionSigs m)))
<> run (runReader buildTable (runReader typesTable (genClosures m)))
Expand All @@ -94,7 +94,7 @@ genStructDefs Micro.Module {..} =
concatMap go (i ^. Micro.includeModule . Micro.moduleBody . Micro.moduleStatements)
_ -> []

genAxioms :: forall r. Members '[Reader CompileInfoTable] r => Micro.Module -> Sem r [CCode]
genAxioms :: forall r. Members '[Reader Micro.InfoTable, Reader CompileInfoTable] r => Micro.Module -> Sem r [CCode]
genAxioms Micro.Module {..} =
concatMapM go (_moduleBody ^. Micro.moduleStatements)
where
Expand Down Expand Up @@ -441,15 +441,17 @@ goLiteral l = case l ^. C.withLocParam of
C.LitInteger i -> LiteralInt i

goAxiom ::
Member (Reader CompileInfoTable) r =>
Members [Reader Micro.InfoTable, Reader CompileInfoTable] r =>
Micro.AxiomDef ->
Sem r [CCode]
goAxiom a
| isJust (a ^. Micro.axiomBuiltin) = return []
| otherwise = do
backends <- lookupBackends (axiomName ^. Micro.nameId)
case firstJust getCode backends of
Nothing -> error ("C backend does not support this axiom:" <> show (axiomName ^. Micro.nameText))
backend <- runFail (lookupBackends (axiomName ^. Micro.nameId) >>= firstBackendMatch)
case backend of
Nothing -> do
sig <- ExternalFuncSig <$> (cFunTypeToFunSig defineName <$> typeToFunType (mkPolyType' (a ^. Micro.axiomType)))
return [sig]
Just defineBody ->
return
[ ExternalMacro
Expand All @@ -471,11 +473,15 @@ goAxiom a
guard (BackendC == b ^. backendItemBackend)
$> b
^. backendItemCode
firstBackendMatch :: Member Fail r =>
[BackendItem] ->
Sem r Text
firstBackendMatch = failMaybe . firstJust getCode
lookupBackends ::
Member (Reader CompileInfoTable) r =>
Members '[Fail, Reader CompileInfoTable] r =>
NameId ->
Sem r [BackendItem]
lookupBackends f = (^. Scoper.compileInfoBackendItems) . HashMap.lookupDefault impossible f <$> ask
lookupBackends f = ask >>= failMaybe . fmap (^. Scoper.compileInfoBackendItems) . HashMap.lookup f

goForeign :: ForeignBlock -> [CCode]
goForeign b = case b ^. foreignBackend of
Expand Down
3 changes: 2 additions & 1 deletion test/BackendC/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ tests =
PosTest "Mutually recursive function" "MutuallyRecursive" StdlibExclude,
PosTest "Nested List type" "NestedList" StdlibExclude,
PosTest "Builtin types and functions" "Builtins" StdlibExclude,
PosTest "Import from embedded standard library" "StdlibImport" StdlibInclude
PosTest "Import from embedded standard library" "StdlibImport" StdlibInclude,
PosTest "Axiom without a compile block" "AxiomNoCompile" StdlibInclude
]
14 changes: 14 additions & 0 deletions tests/positive/MiniC/AxiomNoCompile/Input.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Input;

open import Stdlib.Prelude;

inductive Unit {
unit : Unit;
};

axiom ignore : Unit -> Unit;

main : IO;
main ≔ putStrLn "Hello";

end;
1 change: 1 addition & 0 deletions tests/positive/MiniC/AxiomNoCompile/expected.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Empty file.

0 comments on commit 208d0c8

Please sign in to comment.