Skip to content

Commit

Permalink
split opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Jan 14, 2024
1 parent cd28dee commit 139245b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 68 deletions.
138 changes: 80 additions & 58 deletions lvtrun/src/OpCodes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,51 @@ import Errors (CustomException(..))
import Leb128 (getLEB128ToI32, getLEB128ToI64)
import Types (Instruction(..), MemArg(..), BlockType(..))

extractOpCode' :: [Word8] -> ([Word8], BSL.ByteString)
extractOpCode' (0x03:rest) = ([0x03], BSL.pack rest)
extractOpCode' (0x11:rest) = ([0x11], BSL.pack rest)
extractOpCode' (0x00:rest) = ([0x00], BSL.pack rest)
extractOpCode' (0x0b:rest) = ([0x0b], BSL.pack rest)
extractOpCode' (0x0d:rest) = ([0x0d], BSL.pack rest)
extractOpCode' (0x0c:rest) = ([0x0c], BSL.pack rest)
extractOpCode' (0x02:rest) = ([0x02], BSL.pack rest)
extractOpCode' (0x01:rest) = ([0x01], BSL.pack rest)
extractOpCode' (0x0f:rest) = ([0x0f], BSL.pack rest)
extractOpCode' (0x10:rest) = ([0x10], BSL.pack rest)
extractOpCode' (0x41:rest) = ([0x41], BSL.pack rest)
extractOpCode' (0x42:rest) = ([0x42], BSL.pack rest)
extractOpCode' (0x6c:rest) = ([0x6c], BSL.pack rest)
extractOpCode' (0x6d:rest) = ([0x6d], BSL.pack rest)
extractOpCode' (0x43:rest) = ([0x43], BSL.pack rest)
extractOpCode' (0x44:rest) = ([0x44], BSL.pack rest)
extractOpCode' (0x28:rest) = ([0x28], BSL.pack rest)
extractOpCode' (0x29:rest) = ([0x29], BSL.pack rest)
extractOpCode' (0x22:rest) = ([0x22], BSL.pack rest)
extractOpCode' (0x36:rest) = ([0x36], BSL.pack rest)
extractOpCode' (0x37:rest) = ([0x37], BSL.pack rest)
extractOpCode' (0x4b:rest) = ([0x4b], BSL.pack rest)
extractOpCode' (0x20:rest) = ([0x20], BSL.pack rest)
extractOpCode' (0x4d:rest) = ([0x4d], BSL.pack rest)
extractOpCode' (0x21:rest) = ([0x21], BSL.pack rest)
extractOpCode' (0x23:rest) = ([0x23], BSL.pack rest)
extractOpCode' (0x24:rest) = ([0x24], BSL.pack rest)
extractOpCode' (0x6a:rest) = ([0x6a], BSL.pack rest)
extractOpCode' (0x6b:rest) = ([0x6b], BSL.pack rest)
extractOpCode' (0x45:rest) = ([0x45], BSL.pack rest)
extractOpCode' (0x46:rest) = ([0x46], BSL.pack rest)
extractOpCode' (0x71:rest) = ([0x71], BSL.pack rest)
extractOpCode' (0x48:rest) = ([0x48], BSL.pack rest)
extractOpCode' (0x4a:rest) = ([0x4a], BSL.pack rest)
extractOpCode' (0x4c:rest) = ([0x4c], BSL.pack rest)
extractOpCode' (0x4e:rest) = ([0x4e], BSL.pack rest)
extractOpCode' (0x47:rest) = ([0x47], BSL.pack rest)
extractOpCode' (0x3f:0x00:rest) = ([0x3f, 0x00], BSL.pack rest)
extractOpCode' (0x40:0x00:rest) = ([0x40, 0x00], BSL.pack rest)
extractOpCode' (0x04:0x40:rest) = ([0x04, 0x40], BSL.pack rest)
extractOpCode' _ = throw $ WasmError "ExtractOpCode: bad opcode"

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)
0x04 : 0x40 : rest -> ([0x04, 0x40], BSL.pack rest)
_ -> throw $ WasmError "ExtractOpCode: bad opcode"
extractOpCode bytes = extractOpCode' (BSL.unpack bytes)

createInstruction :: [Word8] -> BSL.ByteString -> (Instruction, BSL.ByteString)
createInstruction [0x03] bytes = (Nop, bytes)
Expand All @@ -89,20 +91,40 @@ createInstruction [0x71] bytes = (I32And, bytes)
createInstruction [0x04, 0x40] bytes = (If, bytes)
createInstruction [0x3f, 0x00] bytes = (MemorySize, bytes)
createInstruction [0x40, 0x00] bytes = (MemoryGrow, 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 [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 _ _ = throw $ WasmError "createInstruction: bad instruction"
19 changes: 9 additions & 10 deletions lvtrun/src/Parsing/Exports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ module Parsing.Exports
)
where

import qualified Data.ByteString.Lazy as Bs
import Control.Exception (throw)
import Data.Int (Int64, Int32)
import Data.Word (Word8)
import Numeric (showHex)
import Data.Char (chr)
import Data.Word (Word8)
import Control.Monad (when)
import Data.Int (Int64, Int32)
import Control.Exception (throw)
import qualified Data.ByteString.Lazy as Bs

import Leb128
import Errors
import Types
import Leb128 (getLEB128ToI64, getLEB128ToI32)
import Errors (CustomException(WasmError))

isExportValid :: Word8 -> Bool
isExportValid 0x00 = True
Expand Down Expand Up @@ -59,7 +58,7 @@ parseExports idx maxIdx content
export : parseExports (idx + 1) maxIdx rest3

getExports :: Section -> [Export]
getExports (Section ExportID _ content) = do
let (exprtsNb, rest) = getExportNb content
parseExports 0 exprtsNb rest
getExports (Section ExportID _ content) = parseExports 0 exprtsNb rest
where
(exprtsNb, rest) = getExportNb content
getExports _ = throw $ WasmError "getExports: bad section"

0 comments on commit 139245b

Please sign in to comment.