Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Dec 17, 2024
1 parent e981bd3 commit ed04be2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
67 changes: 41 additions & 26 deletions wasm-calc12/src/Calc/Wasm/FromExpr/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,31 @@ fromLambda args returnTy body = do
)
(S.toList capturedIdentifiers)

let wasmItems = snd <$> capturedArgs

let offsetList = getOffsetListForWasmType wasmItems

-- we need to get types for these
-- add captured arg types to generated function

wasmArgs <-
traverse (\(k, a) -> (,) k <$> liftEither (scalarFromType a)) args

let allArgs = wasmArgs <> capturedArgs
let allArgs = wasmArgs <> [("_env", Pointer)]

-- TODO: change body to unpack environment vars from `env` struct

wasmBody <- withArgs allArgs (fromExpr body)

-- TODO: make it smash the right var numbers into the body
let wasmBodyWithGetters =
foldr
(\(i, (identifier, wasmTy)) wasmExpr' -> WLet (Just identifier) (fromIntegral $ length wasmArgs + i) (WTupleAccess wasmTy (WVar 0) 0) wasmExpr')
wasmBody
(zip [0 ..] capturedArgs)

traceShowM wasmBodyWithGetters

wasmReturnType <- liftEither $ scalarFromType returnTy

index <- gets (length . fesGenerated)
Expand All @@ -237,7 +251,7 @@ fromLambda args returnTy body = do
let fn =
WasmFunction
{ wfName = FunctionName ("fresh_lambda_" <> T.pack (show index)),
wfExpr = wasmBody,
wfExpr = wasmBodyWithGetters,
wfPublic = False,
wfArgs = snd <$> allArgs,
wfReturnType = wasmReturnType,
Expand All @@ -248,39 +262,38 @@ fromLambda args returnTy body = do
-- store it in heaven
wasmFnRef <- addGeneratedFunction fn

let wasmItems = Pointer : (snd <$> capturedArgs)

let offsetList = getOffsetListForWasmType wasmItems

-- first, create a tuple of [capturedArgA, capturedArgB, .. ]

-- TODO that

-- then we create a tuple of [WFunctionPointer, pointerToEnv]
-- and return it
let allItems =
zip
[0 ..]
( (Pointer, WFunctionPointer wasmFnRef)
: capturedValues
)

let wasmType = Pointer
allocIndex <- addLocal Nothing wasmType
envIndex <- addLocal Nothing Pointer

-- total size of the tuple in memory
let tupleLength = getMemorySizeForWasmTuple wasmItems
let allocate = WAllocate tupleLength
let envTupleLength = getMemorySizeForWasmTuple wasmItems

wSet <-
WSet allocIndex allocate
wasmEnv <-
WSet envIndex (WAllocate envTupleLength)
<$> traverse
( \(i, (wasmTy, wasmItem)) ->
(,,) (offsetList !! i)

Check warning on line 275 in wasm-calc12/src/Calc/Wasm/FromExpr/Expr.hs

View workflow job for this annotation

GitHub Actions / hlint

Warning in fromLambda in module Calc.Wasm.FromExpr.Expr: Redundant <$> ▫︎ Found: "(,,) (offsetList !! i) <$> pure wasmTy" ▫︎ Perhaps: "pure ((,,) (offsetList !! i) wasmTy)"
<$> pure wasmTy
<*> pure wasmItem
)
allItems
(zip [0 ..] capturedValues)

traceShowM wasmEnv

-- then we create a tuple of [WFunctionPointer, pointerToEnv]
-- and return it
allocIndex <- addLocal Nothing Pointer

-- total size of the tuple in memory
let tupleLength = getMemorySizeForWasmTuple [Pointer, Pointer]

let wSet =
WSet
allocIndex
(WAllocate tupleLength)
[ (0, Pointer, WFunctionPointer wasmFnRef),
(memorySize Pointer, Pointer, wasmEnv)
]

traceShowM wSet

Expand Down Expand Up @@ -388,7 +401,9 @@ fromApply fnExpr args = do
wasmArgs <- traverse fromExpr args
-- TODO: fetch the other args from `fn` and add them to `args` to send to
-- function
let wasm = WApplyIndirect wasmFunctionPointer wasmArgs
let wasmEnv = WTupleAccess Pointer fn (memorySize Pointer)
let allArgs = wasmArgs <> [wasmEnv]
let wasm = WApplyIndirect wasmFunctionPointer allArgs
-- peek
traceShowM wasm
pure wasm
Expand Down
1 change: 1 addition & 0 deletions wasm-calc12/src/Calc/Wasm/FromExpr/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Calc.Wasm.FromExpr.Helpers
withArgs,
lookupGlobal,
lookupIdent,
memorySize,
addGeneratedFunction,
getGlobalMap,
getFunctionMap,
Expand Down

0 comments on commit ed04be2

Please sign in to comment.