Skip to content

Commit

Permalink
add some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Jan 13, 2024
1 parent 185c58e commit d3a18ba
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lvtrun/app/Parsing/Code.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ createInstruction [0x20] bytes = do
(GetLocal value, rest)
createInstruction [0x24] bytes = do
let (value, rest) = extractLEB1282 bytes
(SetLocal value, rest)
(SetGlobal value, rest)
createInstruction [0x23] bytes = do
let (value, rest) = extractLEB1282 bytes
(GetGlobal value, rest)
createInstruction [0x21] bytes = do
let (value, rest) = extractLEB1282 bytes
(SetGlobal value, rest)
(SetLocal value, rest)
createInstruction [0x3f, 0x00] bytes = (MemorySize, bytes)
createInstruction [0x40, 0x00] bytes = (MemoryGrow, bytes)
createInstruction opCode _ = trace ("createInstruction: " ++ show opCode) throw $ WasmError "createInstruction: bad instruction"
Expand Down
4 changes: 4 additions & 0 deletions lvtrun/app/Parsing/Exports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Data.Int (Int64, Int32)
import Data.Word (Word8)
import Numeric (showHex)
import Data.Char (chr)
import Control.Monad (when)

import Leb128
import Errors
Expand Down Expand Up @@ -48,7 +49,10 @@ parseExports idx maxIdx content
| Bs.length content == 0 = []
| otherwise = do
let (nameLen, rest) = extractLEB128 content
when (nameLen == 0) (throw $ WasmError "parseExports: bad export")
when (Bs.length rest == 0) (throw $ WasmError "parseExports: bad export")
let (name, rest2) = Bs.splitAt (fromIntegral nameLen) rest
when (Bs.length rest2 == 0) (throw $ WasmError "parseExports: bad export")
let exportType = head (Bs.unpack rest2)
let (exportValue, rest3) = extractLEB128 (Bs.drop 1 rest2)
let export = createExport (Bs.unpack name) exportType (fromIntegral exportValue)
Expand Down
12 changes: 6 additions & 6 deletions lvtrun/app/Parsing/Functions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import Debug.Trace

parseFunctionsIndex :: Int32 -> Int64 -> BSL.ByteString -> [Function]
parseFunctionsIndex idx maxIdx content
| idx > (fromIntegral maxIdx) = []
| BSL.length content == 0 = []
| otherwise = do
let (typeIdx, rest) = extractLEB1282 content
Function {funcType = fromIntegral typeIdx, funcIdx = idx, body = []} : parseFunctionsIndex (idx + 1) maxIdx rest
| idx > (fromIntegral maxIdx) = []
| BSL.length content == 0 = []
| otherwise = do
let (typeIdx, rest) = extractLEB1282 content
Function {funcType = fromIntegral typeIdx, funcIdx = idx, body = []} : parseFunctionsIndex (idx + 1) maxIdx rest

getFunctions :: Section -> [Function]
getFunctions (Section FunctionID _ content) = do
let (vecSize, rest) = extractLEB128 content
parseFunctionsIndex 1 vecSize rest
parseFunctionsIndex 0 vecSize rest
getFunctions _ = throw $ WasmError "getFunctions: bad section"
4 changes: 2 additions & 2 deletions lvtrun/app/Parsing/Global.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ createInstruction [0x20] bytes = do
(GetLocal value, rest)
createInstruction [0x24] bytes = do
let (value, rest) = extractLEB1282 bytes
(SetLocal value, rest)
(SetGlobal value, rest)
createInstruction [0x23] bytes = do
let (value, rest) = extractLEB1282 bytes
(GetGlobal value, rest)
createInstruction [0x21] bytes = do
let (value, rest) = extractLEB1282 bytes
(SetGlobal value, rest)
(SetLocal value, rest)
createInstruction [0x3f, 0x00] bytes = (MemorySize, bytes)
createInstruction [0x40, 0x00] bytes = (MemoryGrow, bytes)
createInstruction _ _ = throw $ WasmError "CreateInstruction: bad instruction"
Expand Down
2 changes: 1 addition & 1 deletion lvtrun/app/Parsing/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ parseModule bytes = do
((getFuncCode (getSectionWithId sections CodeID) (getFunctions (getSectionWithId sections FunctionID))))
[]
(getMemories (getSectionWithId sections MemoryID))
(getGlobals (getSectionWithId sections GlobalID))
[]
(getExports (getSectionWithId sections ExportID))
5 changes: 1 addition & 4 deletions lvtrun/app/Run/Start.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ import Run.Functions
import Debug.Trace

start :: WasmModule -> IO ()
start wasmMod = do
print wasmMod
let res = startExecution (createVm wasmMod) (getStartFunctionId (exports wasmMod))
return ()
start wasmMod = startExecution (createVm wasmMod) (getStartFunctionId (exports wasmMod))
12 changes: 11 additions & 1 deletion lvtrun/app/Run/Vm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ getLocalFromId' idx id (x:xs)
getLocalFromId :: CurrentExec -> LocalIdx -> Value
getLocalFromId cEx id = getLocalFromId' 0 id (ceLocals cEx)

setLocalWithId :: Locals -> Value -> LocalIdx -> Locals
setLocalWithId [] _ _ = throw $ WasmError "setLocalWithId: bad id"
setLocalWithId (x:xs) value id
| id == 0 = value : xs
| otherwise = x : setLocalWithId xs value (id - 1)

-- pushResults StackToPushTo StackToPopFrom ResultTypes
pushResults :: Stack -> Stack -> [TypeName] -> Stack
pushResults stack1 stack2 [] = stack1
Expand Down Expand Up @@ -200,6 +206,10 @@ execOpCode vm cEx (Unreachable) = throw $ WasmError "execOpCode: unreachable"
execOpCode vm cEx (GetLocal localIdx) = do
let value = getLocalFromId cEx localIdx
cEx { ceStack = stackPush (ceStack cEx) value }
execOpCode vm cEx (SetLocal localIdx) = do
let (value, newStack) = stackPop (ceStack cEx)
let newLocals = setLocalWithId (ceLocals cEx) value localIdx
cEx { ceStack = newStack, ceLocals = newLocals }
execOpCode vm cEx _ = cEx

execOpCodes :: VM -> [Instruction] -> CurrentExec
Expand Down Expand Up @@ -243,7 +253,7 @@ startExecution vm funcIdx = do
let function = getFunctionFromId funcIdx (functions (wasmModule vm))
let funcTypee = getFuncTypeFromId (funcType function) (types (wasmModule vm))
let cexec = CurrentExec {
ceLocals = [],
ceLocals = createEmptyLocals [] (locals function),
ceStack = [],
ceInstructions = body function,
ceInstIdx = 0,
Expand Down
Binary file modified lvtrun/test/out.wasm
Binary file not shown.

0 comments on commit d3a18ba

Please sign in to comment.