Skip to content

Commit

Permalink
Add leb128
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 14, 2024
1 parent a93fdb0 commit 423517f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lvtc/src/WasmUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ getDefaultCodeSectionCode = CSC {
}

getSizeOpCode :: OpCode -> Int
getSizeOpCode (LocalGet _) = 2
getSizeOpCode (LocalSet _) = 2
getSizeOpCode (I32Const _) = 2
getSizeOpCode (Call _) = 2
getSizeOpCode (LocalGet n) = 1 + length (leb128Encode (fromIntegral n))
getSizeOpCode (LocalSet n) = 1 + length (leb128Encode (fromIntegral n))
getSizeOpCode (I32Const n) = 1 + length (leb128Encode (fromIntegral n))
getSizeOpCode (Call n) = 1 + length (leb128Encode (fromIntegral n))
getSizeOpCode (If _) = 2
getSizeOpCode (Loop _) = 2
getSizeOpCode (Br _) = 2
Expand Down
12 changes: 9 additions & 3 deletions lvtc/src/WriteWasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ codeSectionCodeLocalsToByte (a, b) =

opCodeToByte :: OpCode -> B.ByteString
opCodeToByte (LocalGet a) =
B.pack [fromIntegral (opCodeByte (LocalGet a)), fromIntegral a]
extendBytes
(B.pack [fromIntegral (opCodeByte (LocalGet a))])
[B.pack (map fromIntegral (leb128Encode (fromIntegral a)))]
opCodeToByte (LocalSet a) =
B.pack [fromIntegral (opCodeByte (LocalSet a)), fromIntegral a]
extendBytes
(B.pack [fromIntegral (opCodeByte (LocalSet a))])
[B.pack (map fromIntegral (leb128Encode (fromIntegral a)))]
opCodeToByte (I32Const a) =
B.pack [fromIntegral (opCodeByte (I32Const a)), fromIntegral a]
extendBytes
(B.pack [fromIntegral (opCodeByte (I32Const a))])
[B.pack (map fromIntegral (leb128Encode (fromIntegral a)))]
opCodeToByte (Call a) =
B.pack [fromIntegral (opCodeByte (Call a)), fromIntegral a]
opCodeToByte (If a) =
Expand Down

0 comments on commit 423517f

Please sign in to comment.