store integer const instructions as words

This commit is contained in:
Ilya Rezvov
2018-03-03 10:20:02 -08:00
parent b9e03584dd
commit c09397539f
2 changed files with 19 additions and 4 deletions
+16 -2
View File
@@ -60,6 +60,8 @@ import Data.List (foldl', findIndex, find)
import Control.Monad (guard) import Control.Monad (guard)
import Numeric.Natural (Natural) import Numeric.Natural (Natural)
import Data.Word (Word32, Word64)
import Data.Bits ((.|.))
import Language.Wasm.Lexer ( import Language.Wasm.Lexer (
Token ( Token (
@@ -1043,6 +1045,18 @@ asString = Just . TLEncoding.decodeUtf8
eitherToMaybe :: Either left right -> Maybe right eitherToMaybe :: Either left right -> Maybe right
eitherToMaybe = either (const Nothing) Just eitherToMaybe = either (const Nothing) Just
integerToWord32 :: Integer -> Word32
integerToWord32 i
| i >= 0 && i <= 2 ^ 32 = fromIntegral i
| i < 0 && i >= -(2 ^ 31) = 0x80000000 .|. (fromIntegral (abs i))
| otherwise = error "I32 is out of bounds."
integerToWord64 :: Integer -> Word64
integerToWord64 i
| i >= 0 && i <= 2 ^ 64 = fromIntegral i
| i < 0 && i >= -(2 ^ 63) = 0x8000000000000000 .|. (fromIntegral (abs i))
| otherwise = error "I64 is out of bounds."
data FuncType = FuncType { params :: [ParamType], results :: [ValueType] } deriving (Show, Eq) data FuncType = FuncType { params :: [ParamType], results :: [ValueType] } deriving (Show, Eq)
emptyFuncType :: FuncType emptyFuncType :: FuncType
@@ -1449,8 +1463,8 @@ desugarize fields =
synInstrToStruct _ (PlainInstr (I64Store32 memArg)) = S.I64Store32 memArg synInstrToStruct _ (PlainInstr (I64Store32 memArg)) = S.I64Store32 memArg
synInstrToStruct _ (PlainInstr CurrentMemory) = S.CurrentMemory synInstrToStruct _ (PlainInstr CurrentMemory) = S.CurrentMemory
synInstrToStruct _ (PlainInstr GrowMemory) = S.GrowMemory synInstrToStruct _ (PlainInstr GrowMemory) = S.GrowMemory
synInstrToStruct _ (PlainInstr (I32Const val)) = S.I32Const val synInstrToStruct _ (PlainInstr (I32Const val)) = S.I32Const $ integerToWord32 val
synInstrToStruct _ (PlainInstr (I64Const val)) = S.I64Const val synInstrToStruct _ (PlainInstr (I64Const val)) = S.I64Const $ integerToWord64 val
synInstrToStruct _ (PlainInstr (F32Const val)) = S.F32Const val synInstrToStruct _ (PlainInstr (F32Const val)) = S.F32Const val
synInstrToStruct _ (PlainInstr (F64Const val)) = S.F64Const val synInstrToStruct _ (PlainInstr (F64Const val)) = S.F64Const val
synInstrToStruct _ (PlainInstr (IUnOp sz op)) = S.IUnOp sz op synInstrToStruct _ (PlainInstr (IUnOp sz op)) = S.IUnOp sz op
+3 -2
View File
@@ -37,6 +37,7 @@ module Language.Wasm.Structure (
) where ) where
import Numeric.Natural (Natural) import Numeric.Natural (Natural)
import Data.Word (Word32, Word64)
import qualified Data.ByteString.Lazy as LBS import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
@@ -142,8 +143,8 @@ data Instruction =
| CurrentMemory | CurrentMemory
| GrowMemory | GrowMemory
-- Numeric instructions -- Numeric instructions
| I32Const Integer | I32Const Word32
| I64Const Integer | I64Const Word64
| F32Const Float | F32Const Float
| F64Const Double | F64Const Double
| IUnOp BitSize IUnOp | IUnOp BitSize IUnOp