{-# LANGUAGE QuasiQuotes #-}

-- | Code generation shared by the WASM backends.  The JavaScript API is
-- in @rts/javascript/wrappers.js@, driven by the manifest.
module Futhark.CodeGen.Backends.GenericWASM
  ( emccExportNames,
    javascriptWrapper,
    runServer,
    libraryExports,
  )
where

import Data.Map qualified as M
import Data.Text qualified as T
import Futhark.CodeGen.RTS.JavaScript
import Futhark.Manifest
import Futhark.Util (nubOrd)
import NeatInterpolation (text)

-- | The names of the C functions that must be exported from the WebAssembly
-- module. This is every function named in the manifest, as the server calls
-- these directly, as well as the context management functions.
emccExportNames :: Manifest -> [String]
emccExportNames :: Manifest -> [[Char]]
emccExportNames Manifest
manifest =
  (Text -> [Char]) -> [Text] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (\Text
f -> [Char]
"'_" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
f [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'") ([Text] -> [Text]
forall a. Ord a => [a] -> [a]
nubOrd ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ Manifest -> [Text]
manifestFunctions Manifest
manifest)
    [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [ [Char]
"_futhark_context_config_new",
         [Char]
"_futhark_context_config_free",
         [Char]
"_futhark_context_config_set_debugging",
         [Char]
"_futhark_context_config_set_logging",
         [Char]
"_futhark_context_config_set_profiling",
         [Char]
"_futhark_context_config_set_tuning_param",
         [Char]
"_futhark_get_tuning_param_count",
         [Char]
"_futhark_get_tuning_param_name",
         [Char]
"_futhark_get_tuning_param_class",
         [Char]
"_futhark_context_new",
         [Char]
"_futhark_context_free",
         [Char]
"_futhark_context_get_error",
         [Char]
"_futhark_context_sync",
         [Char]
"_futhark_context_clear_caches",
         [Char]
"_futhark_context_report",
         [Char]
"_futhark_context_pause_profiling",
         [Char]
"_futhark_context_unpause_profiling"
       ]

-- | Every C function named in the manifest.
manifestFunctions :: Manifest -> [CFuncName]
manifestFunctions :: Manifest -> [Text]
manifestFunctions Manifest
manifest =
  (EntryPoint -> Text) -> [EntryPoint] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map EntryPoint -> Text
entryPointCFun (Map Text EntryPoint -> [EntryPoint]
forall k a. Map k a -> [a]
M.elems (Map Text EntryPoint -> [EntryPoint])
-> Map Text EntryPoint -> [EntryPoint]
forall a b. (a -> b) -> a -> b
$ Manifest -> Map Text EntryPoint
manifestEntryPoints Manifest
manifest)
    [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (Type -> [Text]) -> [Type] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Type -> [Text]
typeFunctions (Map Text Type -> [Type]
forall k a. Map k a -> [a]
M.elems (Map Text Type -> [Type]) -> Map Text Type -> [Type]
forall a b. (a -> b) -> a -> b
$ Manifest -> Map Text Type
manifestTypes Manifest
manifest)
  where
    typeFunctions :: Type -> [Text]
typeFunctions (TypeArray Text
_ Text
_ Int
_ ArrayOps
ops) =
      [ ArrayOps -> Text
arrayFree ArrayOps
ops,
        ArrayOps -> Text
arrayShape ArrayOps
ops,
        ArrayOps -> Text
arrayValues ArrayOps
ops,
        ArrayOps -> Text
arrayNew ArrayOps
ops,
        ArrayOps -> Text
arrayNewRaw ArrayOps
ops,
        ArrayOps -> Text
arrayValuesRaw ArrayOps
ops,
        ArrayOps -> Text
arrayIndex ArrayOps
ops,
        ArrayOps -> Text
arraySet ArrayOps
ops
      ]
    typeFunctions (TypeOpaque Text
_ OpaqueOps
ops Maybe OpaqueExtraOps
extra_ops Doc
_) =
      [OpaqueOps -> Text
opaqueFree OpaqueOps
ops, OpaqueOps -> Text
opaqueStore OpaqueOps
ops, OpaqueOps -> Text
opaqueRestore OpaqueOps
ops]
        [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
-> (OpaqueExtraOps -> [Text]) -> Maybe OpaqueExtraOps -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] OpaqueExtraOps -> [Text]
extraFunctions Maybe OpaqueExtraOps
extra_ops

    extraFunctions :: OpaqueExtraOps -> [Text]
extraFunctions (OpaqueRecord RecordOps
ops) =
      RecordOps -> Text
recordNew RecordOps
ops Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: (RecordField -> Text) -> [RecordField] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map RecordField -> Text
recordFieldProject (RecordOps -> [RecordField]
recordFields RecordOps
ops)
    extraFunctions (OpaqueSum SumOps
ops) =
      SumOps -> Text
sumVariant SumOps
ops Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: (SumVariant -> [Text]) -> [SumVariant] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SumVariant -> [Text]
variantFunctions (SumOps -> [SumVariant]
sumVariants SumOps
ops)
    extraFunctions (OpaqueArray OpaqueArrayOps
ops) =
      [ OpaqueArrayOps -> Text
opaqueArrayIndex OpaqueArrayOps
ops,
        OpaqueArrayOps -> Text
opaqueArrayShape OpaqueArrayOps
ops,
        OpaqueArrayOps -> Text
opaqueArrayNew OpaqueArrayOps
ops,
        OpaqueArrayOps -> Text
opaqueArraySet OpaqueArrayOps
ops
      ]
    extraFunctions (OpaqueRecordArray RecordArrayOps
ops) =
      [ RecordArrayOps -> Text
recordArrayZip RecordArrayOps
ops,
        RecordArrayOps -> Text
recordArrayIndex RecordArrayOps
ops,
        RecordArrayOps -> Text
recordArrayShape RecordArrayOps
ops,
        RecordArrayOps -> Text
recordArrayNew RecordArrayOps
ops,
        RecordArrayOps -> Text
recordArraySet RecordArrayOps
ops
      ]
        [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (RecordField -> Text) -> [RecordField] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map RecordField -> Text
recordFieldProject (RecordArrayOps -> [RecordField]
recordArrayFields RecordArrayOps
ops)

    variantFunctions :: SumVariant -> [Text]
variantFunctions SumVariant
v = [SumVariant -> Text
sumVariantConstruct SumVariant
v, SumVariant -> Text
sumVariantDestruct SumVariant
v]

-- | The JavaScript code passed to @emcc@ with @--extern-post-js@.
javascriptWrapper :: Manifest -> T.Text
javascriptWrapper :: Manifest -> Text
javascriptWrapper Manifest
manifest =
  [Text] -> Text
T.unlines
    [ Text
serverJs,
      Text
valuesJs,
      Text
wrappersJs,
      Text
"const futhark_manifest = " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Manifest -> Text
manifestToJSON Manifest
manifest Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
";"
    ]

-- | Javascript code that can be appended to the generated module to
-- run a Futhark server instance when it is loaded.
runServer :: T.Text
runServer :: Text
runServer =
  [text|
   // Worker threads (wasm-multicore) also load this module.
   const is_main_thread = globalThis.process?.versions?.node
     ? (await import("node:worker_threads")).isMainThread
     : globalThis.WorkerGlobalScope === undefined;
   if (is_main_thread) {
     await futhark_server_main();
   }|]

-- | The names exported by the generated module.
libraryExports :: T.Text
libraryExports :: Text
libraryExports = Text
"export {FutharkContext, FutharkArray, FutharkOpaque};"