-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4cd358
commit cda51fd
Showing
6 changed files
with
96 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Calc.Wasm.Allocator (moduleWithAllocator) where | ||
|
||
import qualified Language.Wasm as Wasm | ||
import Data.FileEmbed | ||
import qualified Data.ByteString.Lazy as LB | ||
|
||
-- these are saved in a file that is included in compilation | ||
allocatorSource ::LB.ByteString | ||
allocatorSource = | ||
LB.fromStrict $(makeRelativeToProject "static/bump-allocator.wat" >>= embedFile) | ||
|
||
-- we have an allocator, we need to import it | ||
moduleWithAllocator :: Wasm.Module | ||
moduleWithAllocator = case Wasm.parse allocatorSource of | ||
Right mod' -> mod' | ||
Left e -> error (show e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
;; taken entirely from https://gist.github.com/bryanburgers/2b0f08fd583cf0401a958d7e8edc7552#file-figure-06-wat | ||
(module | ||
;; Create memory with at least 1 page of 64k of memory | ||
(memory $mem 1) | ||
|
||
;; the pointer of the next allocation | ||
(global $alloc.offset (mut i32) (i32.const 32)) | ||
(func $alloc (param $size i32) (result (;pointer;) i32) | ||
(local $this_alloc_ptr i32) | ||
(local $next_alloc_ptr i32) | ||
(local $current_capacity i32) | ||
|
||
;; If the requested size is more than a 64k page, fail. | ||
local.get $size | ||
i32.const 65536 | ||
i32.gt_u | ||
(if | ||
(then | ||
i32.const 0x01 | ||
unreachable | ||
) | ||
) | ||
|
||
;; calculate the current ptr and the next ptr | ||
global.get $alloc.offset | ||
local.tee $this_alloc_ptr | ||
local.get $size | ||
i32.add | ||
local.set $next_alloc_ptr | ||
|
||
;; If this allocation extends into a page of memory we haven't reserved | ||
;; we need to reserve more memory | ||
memory.size | ||
i32.const 65536 | ||
i32.mul | ||
local.set $current_capacity | ||
|
||
local.get $next_alloc_ptr | ||
local.get $current_capacity | ||
i32.gt_u | ||
(if | ||
(then | ||
i32.const 1 | ||
memory.grow | ||
|
||
;; if memory couldn't grow, panic | ||
i32.const -1 | ||
i32.eq | ||
(if | ||
(then | ||
i32.const 0x02 | ||
unreachable | ||
) | ||
) | ||
) | ||
) | ||
|
||
;; store the ptr to the next allocation | ||
local.get $next_alloc_ptr | ||
global.set $alloc.offset | ||
|
||
;; and return the current pointer | ||
local.get $this_alloc_ptr | ||
) | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters