From 423517f84182bbde7ea0e343ee1779f72e2a8e8b Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sun, 14 Jan 2024 23:41:54 +0100 Subject: [PATCH] Add leb128 --- lvtc/src/WasmUtils.hs | 8 ++++---- lvtc/src/WriteWasm.hs | 12 +++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lvtc/src/WasmUtils.hs b/lvtc/src/WasmUtils.hs index f8fd87b..be6cd10 100644 --- a/lvtc/src/WasmUtils.hs +++ b/lvtc/src/WasmUtils.hs @@ -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 diff --git a/lvtc/src/WriteWasm.hs b/lvtc/src/WriteWasm.hs index 7f8dd67..6ad632b 100644 --- a/lvtc/src/WriteWasm.hs +++ b/lvtc/src/WriteWasm.hs @@ -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) =