Skip to content

Commit

Permalink
move to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Jan 14, 2024
1 parent 34cb9bd commit c08082d
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 226 deletions.
6 changes: 2 additions & 4 deletions lvtrun/app/Loader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file.wasm>"

loadModule :: IO WasmModule
loadModule = do
filePath <- getFilePath
loadModule = getFilePath >>= \filePath ->
getFileContent filePath >>= \bytes ->
return $ parseModule bytes
143 changes: 0 additions & 143 deletions lvtrun/app/OpCodes.hs

This file was deleted.

35 changes: 17 additions & 18 deletions lvtrun/lvtrun.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 0 additions & 13 deletions lvtrun/src/Lib.hs

This file was deleted.

106 changes: 106 additions & 0 deletions lvtrun/src/OpCodes.hs
Original file line number Diff line number Diff line change
@@ -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"
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c08082d

Please sign in to comment.