Skip to content

Commit

Permalink
add save
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Jan 9, 2024
1 parent 7623136 commit 4ee064c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 10 deletions.
25 changes: 19 additions & 6 deletions lvtrun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,25 @@
# ----------------------- 3 -----------------------

# section 3 is the function section with 17 bytes of length
# index of the signature (type) of each internal function
# functionsec = section(vec(typeidx))
03 11 10
01 00 01
01 01 02 02
00 01 00 00
00 00 02 03 00
01
00
01
01
01
02
02
00
01
00
00
00
00
02
03
00

# ----------------------- 4 ----------------------- the table section

Expand All @@ -75,8 +89,7 @@

# 01 = number of memory (its a vector)
01
01 c7
02 c7 02
01 c7 02 c7 02

# ----------------------- 6 ----------------------- the global section

Expand Down
1 change: 0 additions & 1 deletion lvtrun/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
-- Main
-}


module Main (main) where

import Control.Exception (try)
Expand Down
9 changes: 9 additions & 0 deletions lvtrun/app/WasmMod/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import WasmMod.Header
import WasmMod.Sections
import WasmMod.Sections.Types
import WasmMod.Sections.Global
import WasmMod.Sections.Memory

data WasmModule = WasmModule {
header :: ModHeader,
Expand All @@ -46,6 +47,12 @@ getGlobalSection (x:xs)
| identifier x == GlobalID = x
| otherwise = getGlobalSection xs

getMemorySection :: [Section] -> Section
getMemorySection [] = throw (WasmError "No memory section")
getMemorySection (x:xs)
| identifier x == MemoryID = x
| otherwise = getMemorySection xs

loadModule :: String -> IO WasmModule
loadModule filePath = do
bytes <- getFileContent filePath
Expand All @@ -57,4 +64,6 @@ loadModule filePath = do
print funcType
let globals = parseGlobals $ getGlobalSection modSections
print globals
let memory = parseMemory $ getMemorySection modSections
print memory
return $ WasmModule modHeader modSections
47 changes: 47 additions & 0 deletions lvtrun/app/WasmMod/Sections/Memory.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

module WasmMod.Sections.Memory
(
Memory(..),
parseMemory
) where

import qualified Data.ByteString.Lazy as BS
import Control.Exception (throw)
import Control.Monad (when)

import WasmMod.Leb128
import WasmMod.Sections.Types
import WasmMod.Sections
import Errors

data Memory = Memory { memMin :: Int, memMax :: Maybe Int }
deriving (Show, Eq)

parseMinMax :: BS.ByteString -> Memory
parseMinMax content
| endBs /= BS.empty = throw $ WasmError "parseMinMax: bad memory section"
| otherwise = Memory {memMin = fromIntegral min, memMax = Just (fromIntegral max)}
where
(min, rest) = extractLEB128 content
(max, endBs) = extractLEB128 rest

parseMin :: BS.ByteString -> Memory
parseMin content
| endBs /= BS.empty = throw $ WasmError "parseMin: bad memory section"
| otherwise = Memory {memMin = fromIntegral min, memMax = Nothing}
where
(min, endBs) = extractLEB128 content

parseMemory' :: BS.ByteString -> Memory
parseMemory' content
| head (BS.unpack content) == 0x01 = parseMinMax (BS.drop 1 content)
| head (BS.unpack content) == 0x00 = parseMin (BS.drop 1 content)
| otherwise = throw $ WasmError "parseMemory': bad memory section"

parseMemory :: Section -> Memory
parseMemory (Section MemoryID _ content)
| head (BS.unpack content) == 0x01 = parseMemory' (BS.drop 1 content)
| otherwise = throw $ WasmError "parseMemory: v1 allow 1 memory only"
parseMemory _ = throw $ WasmError "parseMemory: bad memory section"

--https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances
1 change: 1 addition & 0 deletions lvtrun/lvtrun.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ executable lvtrun-exe
WasmMod.Module
WasmMod.Sections
WasmMod.Sections.Global
WasmMod.Sections.Memory
WasmMod.Sections.Types
Paths_lvtrun
autogen-modules:
Expand Down
11 changes: 8 additions & 3 deletions lvtrun/test/test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <iostream>

int globa15 = 15;

int add(int a, int b) {
return a + b;
}

int test()
{
return 15;
return globa15;
}

bool compare(int a, int b) {
Expand All @@ -15,8 +19,9 @@ bool compare(int a, int b) {
}

int main() {
int a = 5;
int b = 10;
int a = add(5, 10);
int b = add(10, 5);
bool res = compare(a, b);
std::cout << "res: " << res << std::endl;
return 0;
}

0 comments on commit 4ee064c

Please sign in to comment.