fix loading of signed ints from memory

This commit is contained in:
Ilya Rezvov
2018-04-10 21:09:58 -07:00
parent ab1782c982
commit c75900a2c8
2 changed files with 5 additions and 5 deletions
+4 -4
View File
@@ -649,7 +649,7 @@ eval store FunctionInstance { funcType, moduleInstance, code = Function { localT
MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0) MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0)
let addr = fromIntegral $ v + fromIntegral offset let addr = fromIntegral $ v + fromIntegral offset
byte <- IOVector.read memory addr byte <- IOVector.read memory addr
let val = asWord32 $ if byte >= 128 then -1 * fromIntegral (byte .&. 0x7F) else fromIntegral byte let val = asWord32 $ if byte >= 128 then -1 * fromIntegral (0xFF - byte + 1) else fromIntegral byte
return $ Done ctx { stack = VI32 val : rest } return $ Done ctx { stack = VI32 val : rest }
step ctx@EvalCtx{ stack = (VI32 v:rest) } (I32Load16U MemArg { offset }) = do step ctx@EvalCtx{ stack = (VI32 v:rest) } (I32Load16U MemArg { offset }) = do
MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0) MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0)
@@ -666,7 +666,7 @@ eval store FunctionInstance { funcType, moduleInstance, code = Function { localT
byte <- IOVector.read memory $ addr + idx byte <- IOVector.read memory $ addr + idx
return $ (fromIntegral byte :: Word32) `shiftL` (idx * 8) return $ (fromIntegral byte :: Word32) `shiftL` (idx * 8)
val <- sum <$> mapM readByte [0..1] val <- sum <$> mapM readByte [0..1]
let signed = asWord32 $ if val >= 2 ^ 15 then -1 * fromIntegral (val .&. 0x7FFF) else fromIntegral val let signed = asWord32 $ if val >= 2 ^ 15 then -1 * fromIntegral (0xFFFF - val + 1) else fromIntegral val
return $ Done ctx { stack = VI32 signed : rest } return $ Done ctx { stack = VI32 signed : rest }
step ctx@EvalCtx{ stack = (VI32 v:rest) } (I64Load8U MemArg { offset }) = do step ctx@EvalCtx{ stack = (VI32 v:rest) } (I64Load8U MemArg { offset }) = do
MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0) MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0)
@@ -677,7 +677,7 @@ eval store FunctionInstance { funcType, moduleInstance, code = Function { localT
MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0) MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0)
let addr = fromIntegral $ v + fromIntegral offset let addr = fromIntegral $ v + fromIntegral offset
byte <- IOVector.read memory addr byte <- IOVector.read memory addr
let val = asWord64 $ if byte >= 128 then -1 * fromIntegral (v .&. 0x7F) else fromIntegral v let val = asWord64 $ if byte >= 128 then -1 * fromIntegral (0xFF - byte + 1) else fromIntegral byte
return $ Done ctx { stack = VI64 val : rest } return $ Done ctx { stack = VI64 val : rest }
step ctx@EvalCtx{ stack = (VI32 v:rest) } (I64Load16U MemArg { offset }) = do step ctx@EvalCtx{ stack = (VI32 v:rest) } (I64Load16U MemArg { offset }) = do
MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0) MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0)
@@ -694,7 +694,7 @@ eval store FunctionInstance { funcType, moduleInstance, code = Function { localT
byte <- IOVector.read memory $ addr + idx byte <- IOVector.read memory $ addr + idx
return $ (fromIntegral byte :: Word32) `shiftL` (idx * 8) return $ (fromIntegral byte :: Word32) `shiftL` (idx * 8)
val <- sum <$> mapM readByte [0..1] val <- sum <$> mapM readByte [0..1]
let signed = asWord64 $ if val >= 2 ^ 15 then -1 * fromIntegral (val .&. 0x7FFF) else fromIntegral val let signed = asWord64 $ if val >= 2 ^ 15 then -1 * fromIntegral (0xFFFF - val + 1) else fromIntegral val
return $ Done ctx { stack = VI64 signed : rest } return $ Done ctx { stack = VI64 signed : rest }
step ctx@EvalCtx{ stack = (VI32 v:rest) } (I64Load32U MemArg { offset }) = do step ctx@EvalCtx{ stack = (VI32 v:rest) } (I64Load32U MemArg { offset }) = do
MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0) MemoryInstance { memory } <- readIORef $ memInstances store ! (memaddrs moduleInstance ! 0)
+1 -1
View File
@@ -34,7 +34,7 @@ compile file = do
main :: IO () main :: IO ()
main = do main = do
files <- Directory.listDirectory "tests/samples" files <- Directory.listDirectory "tests/samples"
-- let files = ["i32.wast"] -- let files = ["memory.wast"]
scriptTestCases <- (`mapM` files) $ \file -> do scriptTestCases <- (`mapM` files) $ \file -> do
content <- LBS.readFile $ "tests/samples/" ++ file content <- LBS.readFile $ "tests/samples/" ++ file
let Right script = Parser.parseScript <$> Lexer.scanner content let Right script = Parser.parseScript <$> Lexer.scanner content