diff --git a/lvtrun/app/Loader.hs b/lvtrun/app/Loader.hs index fe25ddd..f3ae24d 100644 --- a/lvtrun/app/Loader.hs +++ b/lvtrun/app/Loader.hs @@ -20,14 +20,12 @@ import IO import Errors getFilePath :: IO String -getFilePath = do - args <- getArgs +getFilePath = getArgs >>= \args -> case args of [path] -> return path _ -> throw $ UsageError "Usage: ./run " loadModule :: IO WasmModule -loadModule = do - filePath <- getFilePath +loadModule = getFilePath >>= \filePath -> getFileContent filePath >>= \bytes -> return $ parseModule bytes diff --git a/lvtrun/app/OpCodes.hs b/lvtrun/app/OpCodes.hs deleted file mode 100644 index 8f35c18..0000000 --- a/lvtrun/app/OpCodes.hs +++ /dev/null @@ -1,143 +0,0 @@ -{- --- EPITECH PROJECT, 2023 --- Leviator Run --- File description: --- OpCodes --} - -module OpCodes -( - extractOpCode, - createInstruction -) -where - -import qualified Data.ByteString.Lazy as BSL -import Control.Exception (throw) -import Data.Word (Word8) - -import Leb128 -import Types -import Errors - -extractOpCode :: BSL.ByteString -> ([Word8], BSL.ByteString) -extractOpCode bytes - | (head $ BSL.unpack bytes) == 0x03 = ([0x00], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x11 = ([0x00], BSL.drop 3 bytes) - | (head $ BSL.unpack bytes) == 0x00 = ([0x00], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x0b = ([0x0b], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x0d = ([0x0d], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x0c = ([0x0c], BSL.drop 2 bytes) - | (head $ BSL.unpack bytes) == 0x02 = ([0x02], BSL.drop 2 bytes) - | (head $ BSL.unpack bytes) == 0x01 = ([0x01], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x0f = ([0x0f], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x10 = ([0x10], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x41 = ([0x41], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x42 = ([0x42], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x6c = ([0x6c], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x6d = ([0x6d], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x43 = ([0x43], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x44 = ([0x44], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x28 = ([0x28], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x29 = ([0x29], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x22 = ([0x22], BSL.drop 2 bytes) - | (head $ BSL.unpack bytes) == 0x36 = ([0x36], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x37 = ([0x37], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x4b = ([0x4b], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x37 = ([0x37], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x20 = ([0x20], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x4d = ([0x4d], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x21 = ([0x21], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x23 = ([0x23], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x24 = ([0x24], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x6a = ([0x6a], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x6b = ([0x6b], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x45 = ([0x45], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x46 = ([0x46], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x71 = ([0x00], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x48 = ([0x48], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x4a = ([0x4a], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x4c = ([0x4c], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x4e = ([0x4e], BSL.drop 1 bytes) - | (head $ BSL.unpack bytes) == 0x47 = ([0x47], BSL.drop 1 bytes) - | (BSL.unpack $ BSL.take 2 bytes) == [0x3f, 0x00] = ([0x3f, 0x00], BSL.drop 2 bytes) - | (BSL.unpack $ BSL.take 2 bytes) == [0x40, 0x00] = ([0x40, 0x00], BSL.drop 2 bytes) - | otherwise = throw $ WasmError "ExtractOpCode2: bad opcode" - -createInstruction :: OpCode -> BSL.ByteString -> (Instruction, BSL.ByteString) -createInstruction [0x03] bytes = (Nop, bytes) -createInstruction [0x11] bytes = (Nop, bytes) -createInstruction [0x00] bytes = (Unreachable, bytes) -createInstruction [0x01] bytes = (Nop, bytes) -createInstruction [0x02] bytes = (Block EmptyType, bytes) -createInstruction [0x0b] bytes = (End, bytes) -createInstruction [0x48] bytes = (I32Lts, bytes) -createInstruction [0x0f] bytes = (Return, bytes) -createInstruction [0x4b] bytes = (I32Gtu, bytes) -createInstruction [0x6a] bytes = (I32Add, bytes) -createInstruction [0x6c] bytes = (I32Mul, bytes) -createInstruction [0x6d] bytes = (I32Divs, bytes) -createInstruction [0x47] bytes = (I32Ne, bytes) -createInstruction [0x6b] bytes = (I32Sub, bytes) -createInstruction [0x4a] bytes = (I32Gts, bytes) -createInstruction [0x46] bytes = (I32Eqz, bytes) -createInstruction [0x45] bytes = (I32Eqz, bytes) -createInstruction [0x4d] bytes = (I32Leu, bytes) -createInstruction [0x4e] bytes = (I32Ges, bytes) -createInstruction [0x4c] bytes = (I32Les, bytes) -createInstruction [0x71] bytes = (I32And, bytes) -createInstruction [0x0d] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (BrIf value, rest) -createInstruction [0x0c] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (Br value, rest) -createInstruction [0x22] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (LocalTee value, rest) -createInstruction [0x10] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (Call value, rest) -createInstruction [0x41] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (I32Const value, rest) -createInstruction [0x42] bytes = do - let (value, rest) = getLEB128ToI64 bytes - (I64Const value, rest) -createInstruction [0x43] bytes = do - let (value, rest) = getLEB128ToI64 bytes - (F32Const (fromIntegral value), rest) -createInstruction [0x44] bytes = do - let (value, rest) = getLEB128ToI64 bytes - (F64Const (fromIntegral value), rest) -createInstruction [0x28] bytes = do - let (align, rest) = getLEB128ToI32 bytes - let (offset, rest2) = getLEB128ToI32 rest - (I32Load (MemArg offset align), rest2) -createInstruction [0x29] bytes = do - let (align, rest) = getLEB128ToI32 bytes - let (offset, rest2) = getLEB128ToI32 rest - (I64Load (MemArg offset align), rest2) -createInstruction [0x36] bytes = do - let (align, rest) = getLEB128ToI32 bytes - let (offset, rest2) = getLEB128ToI32 rest - (I32Store (MemArg offset align), rest2) -createInstruction [0x37] bytes = do - let (align, rest) = getLEB128ToI32 bytes - let (offset, rest2) = getLEB128ToI32 rest - (I64Store (MemArg offset align), rest2) -createInstruction [0x20] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (GetLocal value, rest) -createInstruction [0x24] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (SetGlobal value, rest) -createInstruction [0x23] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (GetGlobal value, rest) -createInstruction [0x21] bytes = do - let (value, rest) = getLEB128ToI32 bytes - (SetLocal value, rest) -createInstruction [0x3f, 0x00] bytes = (MemorySize, bytes) -createInstruction [0x40, 0x00] bytes = (MemoryGrow, bytes) -createInstruction _ _ = throw $ WasmError "createInstruction: bad instruction" diff --git a/lvtrun/lvtrun.cabal b/lvtrun/lvtrun.cabal index d6f248e..f1377ba 100644 --- a/lvtrun/lvtrun.cabal +++ b/lvtrun/lvtrun.cabal @@ -25,27 +25,9 @@ source-repository head library exposed-modules: - Lib - other-modules: - Paths_lvtrun - autogen-modules: - Paths_lvtrun - hs-source-dirs: - src - ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints - build-depends: - base >=4.7 && <5 - , binary - , bytestring - default-language: Haskell2010 - -executable lvtrun-exe - main-is: Main.hs - other-modules: Errors IO Leb128 - Loader OpCodes Parsing.Code Parsing.Exports @@ -62,6 +44,23 @@ executable lvtrun-exe Run.Start Run.Vm Types + other-modules: + Paths_lvtrun + autogen-modules: + Paths_lvtrun + hs-source-dirs: + src + ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints + build-depends: + base >=4.7 && <5 + , binary + , bytestring + default-language: Haskell2010 + +executable lvtrun-exe + main-is: Main.hs + other-modules: + Loader Paths_lvtrun autogen-modules: Paths_lvtrun diff --git a/lvtrun/app/Errors.hs b/lvtrun/src/Errors.hs similarity index 100% rename from lvtrun/app/Errors.hs rename to lvtrun/src/Errors.hs diff --git a/lvtrun/app/IO.hs b/lvtrun/src/IO.hs similarity index 100% rename from lvtrun/app/IO.hs rename to lvtrun/src/IO.hs diff --git a/lvtrun/app/Leb128.hs b/lvtrun/src/Leb128.hs similarity index 100% rename from lvtrun/app/Leb128.hs rename to lvtrun/src/Leb128.hs diff --git a/lvtrun/src/Lib.hs b/lvtrun/src/Lib.hs deleted file mode 100644 index 3f12ee2..0000000 --- a/lvtrun/src/Lib.hs +++ /dev/null @@ -1,13 +0,0 @@ -{- --- EPITECH PROJECT, 2023 --- Leviator Run --- File description: --- Lib --} - -module Lib - ( someFunc - ) where - -someFunc :: IO () -someFunc = putStrLn "someFunc" diff --git a/lvtrun/src/OpCodes.hs b/lvtrun/src/OpCodes.hs new file mode 100644 index 0000000..ca85328 --- /dev/null +++ b/lvtrun/src/OpCodes.hs @@ -0,0 +1,106 @@ +{- +-- EPITECH PROJECT, 2023 +-- Leviator Run +-- File description: +-- OpCodes +-} + +module OpCodes +( + extractOpCode, + createInstruction +) +where + +import qualified Data.ByteString.Lazy as BSL +import Control.Exception (throw) +import Data.Word (Word8) + +import Leb128 +import Types +import Errors + +extractOpCode :: BSL.ByteString -> ([Word8], BSL.ByteString) +extractOpCode bytes = case BSL.unpack bytes of + 0x03 : rest -> ([0x03], BSL.pack rest) + 0x11 : rest -> ([0x11], BSL.pack rest) + 0x00 : rest -> ([0x00], BSL.pack rest) + 0x0b : rest -> ([0x0b], BSL.pack rest) + 0x0d : rest -> ([0x0d], BSL.pack rest) + 0x0c : rest -> ([0x0c], BSL.pack rest) + 0x02 : rest -> ([0x02], BSL.pack rest) + 0x01 : rest -> ([0x01], BSL.pack rest) + 0x0f : rest -> ([0x0f], BSL.pack rest) + 0x10 : rest -> ([0x10], BSL.pack rest) + 0x41 : rest -> ([0x41], BSL.pack rest) + 0x42 : rest -> ([0x42], BSL.pack rest) + 0x6c : rest -> ([0x6c], BSL.pack rest) + 0x6d : rest -> ([0x6d], BSL.pack rest) + 0x43 : rest -> ([0x43], BSL.pack rest) + 0x44 : rest -> ([0x44], BSL.pack rest) + 0x28 : rest -> ([0x28], BSL.pack rest) + 0x29 : rest -> ([0x29], BSL.pack rest) + 0x22 : rest -> ([0x22], BSL.pack rest) + 0x36 : rest -> ([0x36], BSL.pack rest) + 0x37 : rest -> ([0x37], BSL.pack rest) + 0x4b : rest -> ([0x4b], BSL.pack rest) + 0x20 : rest -> ([0x20], BSL.pack rest) + 0x4d : rest -> ([0x4d], BSL.pack rest) + 0x21 : rest -> ([0x21], BSL.pack rest) + 0x23 : rest -> ([0x23], BSL.pack rest) + 0x24 : rest -> ([0x24], BSL.pack rest) + 0x6a : rest -> ([0x6a], BSL.pack rest) + 0x6b : rest -> ([0x6b], BSL.pack rest) + 0x45 : rest -> ([0x45], BSL.pack rest) + 0x46 : rest -> ([0x46], BSL.pack rest) + 0x71 : rest -> ([0x00], BSL.pack rest) + 0x48 : rest -> ([0x48], BSL.pack rest) + 0x4a : rest -> ([0x4a], BSL.pack rest) + 0x4c : rest -> ([0x4c], BSL.pack rest) + 0x4e : rest -> ([0x4e], BSL.pack rest) + 0x47 : rest -> ([0x47], BSL.pack rest) + 0x3f : 0x00 : rest -> ([0x3f, 0x00], BSL.pack rest) + 0x40 : 0x00 : rest -> ([0x40, 0x00], BSL.pack rest) + _ -> throw $ WasmError "ExtractOpCode: bad opcode" + +createInstruction :: [Word8] -> BSL.ByteString -> (Instruction, BSL.ByteString) +createInstruction [0x03] bytes = (Nop, bytes) +createInstruction [0x11] bytes = (Nop, bytes) +createInstruction [0x00] bytes = (Unreachable, bytes) +createInstruction [0x01] bytes = (Nop, bytes) +createInstruction [0x02] bytes = (Block EmptyType, bytes) +createInstruction [0x0b] bytes = (End, bytes) +createInstruction [0x48] bytes = (I32Lts, bytes) +createInstruction [0x0f] bytes = (Return, bytes) +createInstruction [0x4b] bytes = (I32Gtu, bytes) +createInstruction [0x6a] bytes = (I32Add, bytes) +createInstruction [0x6c] bytes = (I32Mul, bytes) +createInstruction [0x6d] bytes = (I32Divs, bytes) +createInstruction [0x47] bytes = (I32Ne, bytes) +createInstruction [0x6b] bytes = (I32Sub, bytes) +createInstruction [0x4a] bytes = (I32Gts, bytes) +createInstruction [0x46] bytes = (I32Eqz, bytes) +createInstruction [0x45] bytes = (I32Eqz, bytes) +createInstruction [0x4d] bytes = (I32Leu, bytes) +createInstruction [0x4e] bytes = (I32Ges, bytes) +createInstruction [0x4c] bytes = (I32Les, bytes) +createInstruction [0x71] bytes = (I32And, bytes) +createInstruction [0x0d] bytes = (\(value, rest) -> (BrIf value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x0c] bytes = (\(value, rest) -> (Br value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x22] bytes = (\(value, rest) -> (LocalTee value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x10] bytes = (\(value, rest) -> (Call value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x41] bytes = (\(value, rest) -> (I32Const value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x42] bytes = (\(value, rest) -> (I64Const value, rest)) (getLEB128ToI64 bytes) +createInstruction [0x43] bytes = (\(value, rest) -> (F32Const (fromIntegral value), rest)) (getLEB128ToI32 bytes) +createInstruction [0x20] bytes = (\(value, rest) -> (GetLocal value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x24] bytes = (\(value, rest) -> (SetGlobal value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x23] bytes = (\(value, rest) -> (GetGlobal value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x21] bytes = (\(value, rest) -> (SetLocal value, rest)) (getLEB128ToI32 bytes) +createInstruction [0x44] bytes = (\(value, rest) -> (F64Const (fromIntegral value), rest)) (getLEB128ToI64 bytes) +createInstruction [0x28] bytes = (\(align, rest) -> (\(offset, rest2) -> (I32Load (MemArg offset align), rest2)) (getLEB128ToI32 rest)) (getLEB128ToI32 bytes) +createInstruction [0x29] bytes = (\(align, rest) -> (\(offset, rest2) -> (I64Load (MemArg offset align), rest2)) (getLEB128ToI32 rest)) (getLEB128ToI32 bytes) +createInstruction [0x36] bytes = (\(align, rest) -> (\(offset, rest2) -> (I32Store (MemArg offset align), rest2)) (getLEB128ToI32 rest)) (getLEB128ToI32 bytes) +createInstruction [0x37] bytes = (\(align, rest) -> (\(offset, rest2) -> (I64Store (MemArg offset align), rest2)) (getLEB128ToI32 rest)) (getLEB128ToI32 bytes) +createInstruction [0x3f, 0x00] bytes = (MemorySize, bytes) +createInstruction [0x40, 0x00] bytes = (MemoryGrow, bytes) +createInstruction _ _ = throw $ WasmError "createInstruction: bad instruction" diff --git a/lvtrun/app/Parsing/Code.hs b/lvtrun/src/Parsing/Code.hs similarity index 100% rename from lvtrun/app/Parsing/Code.hs rename to lvtrun/src/Parsing/Code.hs diff --git a/lvtrun/app/Parsing/Exports.hs b/lvtrun/src/Parsing/Exports.hs similarity index 100% rename from lvtrun/app/Parsing/Exports.hs rename to lvtrun/src/Parsing/Exports.hs diff --git a/lvtrun/app/Parsing/FuncTypes.hs b/lvtrun/src/Parsing/FuncTypes.hs similarity index 100% rename from lvtrun/app/Parsing/FuncTypes.hs rename to lvtrun/src/Parsing/FuncTypes.hs diff --git a/lvtrun/app/Parsing/Functions.hs b/lvtrun/src/Parsing/Functions.hs similarity index 92% rename from lvtrun/app/Parsing/Functions.hs rename to lvtrun/src/Parsing/Functions.hs index beb429f..15fa49f 100644 --- a/lvtrun/app/Parsing/Functions.hs +++ b/lvtrun/src/Parsing/Functions.hs @@ -23,13 +23,13 @@ parseFunctionsIndex :: Int32 -> Int64 -> BSL.ByteString -> [Function] parseFunctionsIndex idx maxIdx content | idx > (fromIntegral maxIdx) = [] | BSL.length content == 0 = [] - | otherwise = do - let (typeIdx, rest) = getLEB128ToI32 content + | otherwise = Function { funcType = fromIntegral typeIdx, funcIdx = idx, body = [] } : parseFunctionsIndex (idx + 1) maxIdx rest + where (typeIdx, rest) = getLEB128ToI32 content getFunctions :: Section -> [Function] getFunctions (Section FunctionID _ content) = do diff --git a/lvtrun/app/Parsing/Global.hs b/lvtrun/src/Parsing/Global.hs similarity index 100% rename from lvtrun/app/Parsing/Global.hs rename to lvtrun/src/Parsing/Global.hs diff --git a/lvtrun/app/Parsing/Header.hs b/lvtrun/src/Parsing/Header.hs similarity index 100% rename from lvtrun/app/Parsing/Header.hs rename to lvtrun/src/Parsing/Header.hs diff --git a/lvtrun/app/Parsing/Memory.hs b/lvtrun/src/Parsing/Memory.hs similarity index 100% rename from lvtrun/app/Parsing/Memory.hs rename to lvtrun/src/Parsing/Memory.hs diff --git a/lvtrun/app/Parsing/Parser.hs b/lvtrun/src/Parsing/Parser.hs similarity index 100% rename from lvtrun/app/Parsing/Parser.hs rename to lvtrun/src/Parsing/Parser.hs diff --git a/lvtrun/app/Parsing/Sections.hs b/lvtrun/src/Parsing/Sections.hs similarity index 100% rename from lvtrun/app/Parsing/Sections.hs rename to lvtrun/src/Parsing/Sections.hs diff --git a/lvtrun/app/Run/Functions.hs b/lvtrun/src/Run/Functions.hs similarity index 100% rename from lvtrun/app/Run/Functions.hs rename to lvtrun/src/Run/Functions.hs diff --git a/lvtrun/app/Run/Locals.hs b/lvtrun/src/Run/Locals.hs similarity index 100% rename from lvtrun/app/Run/Locals.hs rename to lvtrun/src/Run/Locals.hs diff --git a/lvtrun/app/Run/Stack.hs b/lvtrun/src/Run/Stack.hs similarity index 100% rename from lvtrun/app/Run/Stack.hs rename to lvtrun/src/Run/Stack.hs diff --git a/lvtrun/app/Run/Start.hs b/lvtrun/src/Run/Start.hs similarity index 100% rename from lvtrun/app/Run/Start.hs rename to lvtrun/src/Run/Start.hs diff --git a/lvtrun/app/Run/Vm.hs b/lvtrun/src/Run/Vm.hs similarity index 100% rename from lvtrun/app/Run/Vm.hs rename to lvtrun/src/Run/Vm.hs diff --git a/lvtrun/app/Types.hs b/lvtrun/src/Types.hs similarity index 93% rename from lvtrun/app/Types.hs rename to lvtrun/src/Types.hs index f1daba1..501dc21 100644 --- a/lvtrun/app/Types.hs +++ b/lvtrun/src/Types.hs @@ -36,7 +36,6 @@ module Types SectionID(..), Section(..), Memory(..), - OpCode, Local(..), BlockType(..), Value(..) @@ -60,46 +59,30 @@ type DataIdx = Int32 type LocalIdx = Int32 type LabelIdx = Int32 --- Common - type FileContent = BSL.ByteString +type Memory = Limit + data TypeName = - I32 | - I64 | - F32 | - F64 + I32 + | I64 + | F32 + | F64 deriving (Show, Eq, Enum) -getTypeFromByte :: Word8 -> TypeName -getTypeFromByte 0x7f = I32 -getTypeFromByte 0x7e = I64 -getTypeFromByte 0x7d = F32 -getTypeFromByte 0x7c = F64 -getTypeFromByte _ = throw $ WasmError "GetTypeFromByte: bad type" - data Limit = Limit { lMin :: Int32, lMax :: Maybe Int32 -} - -instance Show Limit where - show limit = "[\n\tmin: " ++ (show $ lMin limit) ++ "\n\tmax: " ++ (show $ lMax limit) ++ "\n]" - ------------------------ +} deriving (Show, Eq) data MemArg = MemArg { offset :: Int32, align :: Int32 -} +} deriving (Show) instance Eq MemArg where - (==) memArg1 memArg2 = (offset memArg1) == (offset memArg2) && (align memArg1) == (align memArg2) - -instance Show MemArg where - show memArg = "[\n\toffset: " ++ (show $ offset memArg) ++ "\n\talign: " ++ (show $ align memArg) ++ "\n]" - -type OpCode = [Word8] + (==) memArg1 memArg2 = (offset memArg1) == (offset memArg2) + && (align memArg1) == (align memArg2) data BlockType = EmptyType @@ -196,6 +179,23 @@ data Value = | F_64 Double deriving (Eq) +data SectionID = + CustomID + | TypeID + | ImportID + | FunctionID + | TableID + | MemoryID + | GlobalID + | ExportID + | StartID + | ElementID + | CodeID + | DataID + | DataCountID + | InvalidID + deriving (Show, Eq) + instance Show Value where show (I_32 val) = show val show (I_64 val) = show val @@ -231,8 +231,6 @@ data Function = Function { body :: [Instruction] } deriving (Show) -type Memory = Limit - data Mutability = Const | Var deriving (Show) data Global = Global { @@ -262,23 +260,6 @@ data ModHeader = ModHeader { version :: BSL.ByteString } deriving (Show) -data SectionID = - CustomID - | TypeID - | ImportID - | FunctionID - | TableID - | MemoryID - | GlobalID - | ExportID - | StartID - | ElementID - | CodeID - | DataID - | DataCountID - | InvalidID - deriving (Show, Eq) - data Section = Section { identifier :: SectionID, size :: Int, @@ -306,3 +287,10 @@ instance Show WasmModule where "- Memory: " ++ (show $ memory wasmMod) ++ "\n" ++ "- Globals: " ++ (show $ globals wasmMod) ++ "\n" ++ "- Exports: " ++ (show $ exports wasmMod) ++ "\n" + +getTypeFromByte :: Word8 -> TypeName +getTypeFromByte 0x7f = I32 +getTypeFromByte 0x7e = I64 +getTypeFromByte 0x7d = F32 +getTypeFromByte 0x7c = F64 +getTypeFromByte _ = throw $ WasmError "GetTypeFromByte: bad type" diff --git a/lvtrun/test/facotial.wasm b/lvtrun/test/facotial.wasm new file mode 100644 index 0000000..86c3dc6 Binary files /dev/null and b/lvtrun/test/facotial.wasm differ