Skip to content

Commit

Permalink
Add write for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 13, 2024
1 parent 7ce4072 commit 280d80c
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lvtc/lvtc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ library
WatLike
WatLikeToWat
WatToWasm
WriteWasm
other-modules:
Paths_lvtc
autogen-modules:
Expand All @@ -49,6 +50,7 @@ library
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
base >=4.7 && <5
, bytestring
default-language: Haskell2010

executable lvtc-exe
Expand Down
2 changes: 2 additions & 0 deletions lvtc/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ ghc-options:

library:
source-dirs: src
dependencies:
- bytestring

executables:
lvtc-exe:
Expand Down
35 changes: 35 additions & 0 deletions lvtc/src/WasmUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module WasmUtils
fillBlankExportSection,
fillBlankCodeSectionCode,
fillBlankCodeSection,
--
opCodeByte,
variableTypeByte,
exportSectionExportTypeByte
) where

import Wasm
Expand Down Expand Up @@ -214,3 +218,34 @@ getDefaultWasm = Wasm {
exportSection = getDefaultExportSection,
codeSection = getDefaultCodeSection
}

opCodeByte :: OpCode -> Int
opCodeByte (LocalGet _) = 0x20
opCodeByte (LocalSet _) = 0x21
opCodeByte (I32Const _) = 0x41
opCodeByte I32Store = 0x36
opCodeByte I32Load = 0x28
opCodeByte I32GT_S = 0x4a
opCodeByte I32LT_S = 0x48
opCodeByte I32GE_S = 0x4e
opCodeByte I32LE_S = 0x4c
opCodeByte I32EQ = 0x46
opCodeByte I32NE = 0x47
opCodeByte I32Add = 0x6a
opCodeByte I32Sub = 0x6b
opCodeByte I32Mul = 0x6c
opCodeByte I32Div = 0x6d
opCodeByte Return = 0x0f
opCodeByte (Call _) = 0x10
opCodeByte If = 0x04
opCodeByte Else = 0x05
opCodeByte End = 0x0b

variableTypeByte :: VariableType -> Int
variableTypeByte I32 = 0x7f

exportSectionExportTypeByte :: ExportSectionExportType -> Int
exportSectionExportTypeByte (FuncExport) = 0x00
exportSectionExportTypeByte (TableExport) = 0x01
exportSectionExportTypeByte (MemoryExport) = 0x02
exportSectionExportTypeByte (GlobalExport) = 0x03
123 changes: 123 additions & 0 deletions lvtc/src/WriteWasm.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{-
-- EPITECH PROJECT, 2023
-- Leviator compiler
-- File description:
-- WriteWasm
-}

module WriteWasm
(
writeWasm
, wasmToByteString
) where

import Wasm
import WasmUtils
import qualified Data.ByteString as B
import WatAST (OpCode (..))
import Data.Char (ord)

-- extend

extendBytes :: B.ByteString -> [B.ByteString] -> B.ByteString
extendBytes l [] = l
extendBytes l (x:xs) = extendBytes (l `B.append` x) xs

extendsBytes :: B.ByteString -> [[B.ByteString]] -> B.ByteString
extendsBytes a [] = a
extendsBytes a (x:xs) = extendsBytes (extendBytes a x) xs

--

headerWasmToByteString :: Wasm -> B.ByteString
headerWasmToByteString (Wasm (a, b, c, d) (e, f, g, h) _ _ _ _ _) =
B.pack (map fromIntegral [a, b, c, d, e, f, g, h])

--

typeSectionTypeToByteString :: TypeSectionType -> B.ByteString
typeSectionTypeToByteString (Func a b lc d le) =
extendBytes
(B.pack (map fromIntegral [a, b]))
[
B.pack (map (fromIntegral . variableTypeByte) lc),
B.pack [fromIntegral d],
B.pack (map (fromIntegral . variableTypeByte) le)
]

typeSectionToByteString :: TypeSection -> B.ByteString
typeSectionToByteString (TS a b c ld) =
extendBytes
(B.pack (map fromIntegral [a, b, c]))
(map typeSectionTypeToByteString ld)

--

functionSectionToByteString :: FunctionSection -> B.ByteString
functionSectionToByteString (FS a b c ld) =
B.pack (map fromIntegral ([a, b, c] ++ ld))

--

memorySectionToByteString :: MemorySection -> B.ByteString
memorySectionToByteString (MS a b 0 d _) =
B.pack (map fromIntegral ([a, b, 0, d]))
memorySectionToByteString (MS a b _ d e) =
B.pack (map fromIntegral ([a, b, 1, d, e]))

--

exportSectionExportToByteString :: ExportSectionExport -> B.ByteString
exportSectionExportToByteString (ESE a lb c d) =
B.pack (map fromIntegral ([a] ++ (map ord lb) ++ [exportSectionExportTypeByte c, d]))

exportSectionToByteString :: ExportSection -> B.ByteString
exportSectionToByteString (ES a b c ld) =
extendBytes
(B.pack (map fromIntegral [a, b, c]))
(map exportSectionExportToByteString ld)

--

codeSectionCodeLocalsToByte :: CodeSectionCodeLocals -> B.ByteString
codeSectionCodeLocalsToByte (a, b) =
B.pack (map fromIntegral [fromIntegral a, variableTypeByte b])

opCodeToByte :: OpCode -> B.ByteString
opCodeToByte (LocalGet a) = B.pack (map fromIntegral [opCodeByte (LocalGet a), fromIntegral a])
opCodeToByte (LocalSet a) = B.pack (map fromIntegral [opCodeByte (LocalSet a), fromIntegral a])
opCodeToByte (I32Const a) = B.pack (map fromIntegral [opCodeByte (I32Const a), fromIntegral a])
opCodeToByte op = B.pack [fromIntegral (opCodeByte op)]

codeSectionCodeToByte :: CodeSectionCode -> B.ByteString
codeSectionCodeToByte (CSC a b lc ld e) =
extendsBytes
(B.pack (map fromIntegral [a, b]))
[
(map codeSectionCodeLocalsToByte lc),
(map opCodeToByte ld),
([B.pack [fromIntegral e]])
]

codeSectionToByte :: CodeSection -> B.ByteString
codeSectionToByte (CS a b c ld) =
extendBytes
(B.pack (map fromIntegral [a, b, c]))
(map codeSectionCodeToByte ld)

wasmToByteString :: Wasm -> B.ByteString
wasmToByteString (Wasm hW vW tS fS mS eS cS) =
extendBytes
(headerWasmToByteString (Wasm hW vW tS fS mS eS cS))
[
typeSectionToByteString tS,
functionSectionToByteString fS,
memorySectionToByteString mS,
exportSectionToByteString eS,
codeSectionToByte cS
]

writeWasm :: Wasm -> FilePath -> IO ()
writeWasm w f = B.writeFile f bytes
where
bytes = wasmToByteString w
1 change: 1 addition & 0 deletions lvtc/stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ packages:
extra-deps:
- tasty-1.4.2.2
- tasty-hunit-0.10.1
- bytestring-0.12.0.2

# Override default flag values for local packages and extra-deps
# flags: {}
Expand Down
7 changes: 7 additions & 0 deletions lvtc/stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ packages:
size: 399
original:
hackage: tasty-hunit-0.10.1
- completed:
hackage: bytestring-0.12.0.2@sha256:9fc077ff5b7ed2246773c3ac4370ef8822e4834d4587522b68ae551a5968fb86,7891
pantry-tree:
sha256: 05b5b3ef529f137062c632c38d9c94482ee25bcc0438d51a4be5448dafe690c9
size: 4355
original:
hackage: bytestring-0.12.0.2
snapshots:
- completed:
sha256: e176944bc843f740e05242fa7a66ca1f440c127e425254f7f1257f9b19add23f
Expand Down

0 comments on commit 280d80c

Please sign in to comment.