Skip to content

Commit

Permalink
Add wat to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 10, 2024
1 parent 6d2a27b commit 841fce5
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 58 deletions.
18 changes: 17 additions & 1 deletion lvtc/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Alias (proceedAlias)
import ParseLvt (parseInstruction, parseInstructions, parseFuncDeclaration)
import WatLike (aSTToWatLike)
import AST
import WatAST
import Builtins (getBuiltinsWat)
import WatToWasm (watToWasm)

test1 :: String
test1 = "if (a)\n{\nb(0);\n};\n"
Expand Down Expand Up @@ -50,10 +53,22 @@ test7 =
[
(
(False, "add", [("a", "Int"), ("b", "Int")], "Int"),
[Return (FuncValue ("+", [Var "a", Var "b"]))]
[AST.Return (FuncValue ("+", [Var "a", Var "b"]))]
)
]

test9 :: [FuncDef]
test9 =
getBuiltinsWat ++
[
FuncDef False "add" 10 [] WatAST.I32 [
I32Const 97,
LocalSet 0,
LocalGet 0,
WatAST.Return
] [(WatAST.I32, 1)]
]

main :: IO ()
main =
print (runParser parseInstruction test1)
Expand All @@ -65,3 +80,4 @@ main =
>> print (runParser (proceedAlias <$> parseAllExpression) text)
>> print (runParser parseFuncDeclaration test8)
>> print (aSTToWatLike test7)
>> print (watToWasm test9)
1 change: 1 addition & 0 deletions lvtc/lvtc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ test-suite lvtc-test
other-modules:
UTParseLvt
UTShuntingYard
UTWasm
UTWat
UTWatLike
Paths_lvtc
Expand Down
128 changes: 74 additions & 54 deletions lvtc/src/Wasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ module Wasm
) where

import WatAST (OpCode (..))
import Data.Int (Int32)

data VariableType =
I32
deriving (Show, Eq)

data TypeSectionType =
Func {
Expand All @@ -34,72 +36,90 @@ data TypeSectionType =
nbResults :: Int,
results :: [VariableType]
}
deriving (Show, Eq)

data TypeSection = TS {
headerTS :: Int,
sizeTS :: Int,
nbTypes :: Int,
types :: [TypeSectionType]
}
data TypeSection =
TS {
headerTS :: Int,
sizeTS :: Int,
nbTypes :: Int,
types :: [TypeSectionType]
}
deriving (Show, Eq)

data FunctionSection = FS {
headerFS :: Int,
sizeFS :: Int,
nbFuncs :: Int,
funcs :: [Int]
}
data FunctionSection =
FS {
headerFS :: Int,
sizeFS :: Int,
nbFuncs :: Int,
funcs :: [Int]
}
deriving (Show, Eq)

data MemorySection = MS {
headerMS :: Int,
sizeMS :: Int,
hasMax :: Int,
minMS :: Int,
maxMS :: Int
}
data MemorySection =
MS {
headerMS :: Int,
sizeMS :: Int,
hasMax :: Int,
minMS :: Int,
maxMS :: Int
}
deriving (Show, Eq)

data ExportSectionExportType =
FuncExport
| TableExport
| MemoryExport
| GlobalExport
deriving (Show, Eq)

data ExportSectionExport = ESE {
nameLength :: Int,
name :: String,
typeESE :: ExportSectionExportType,
indexESE :: Int
}
data ExportSectionExport =
ESE {
nameLength :: Int,
name :: String,
typeESE :: ExportSectionExportType,
indexESE :: Int
}
deriving (Show, Eq)

data ExportSection = ES {
headerES :: Int,
sizeES :: Int,
nbExports :: Int,
exports :: [ExportSectionExport]
}
data ExportSection =
ES {
headerES :: Int,
sizeES :: Int,
nbExports :: Int,
exports :: [ExportSectionExport]
}
deriving (Show, Eq)

type CodeSectionCodeLocals = (Int, VariableType)
type CodeSectionCodeLocals = (Int32, VariableType)

data CodeSectionCode = CSC {
sizeCSC :: Int,
nbLocals :: Int,
locals :: [CodeSectionCodeLocals],
bodyCSC :: [OpCode],
endCSC :: Int
}
data CodeSectionCode =
CSC {
sizeCSC :: Int,
nbLocals :: Int,
locals :: [CodeSectionCodeLocals],
bodyCSC :: [OpCode],
endCSC :: Int
}
deriving (Show, Eq)

data CodeSection = CS {
headerCS :: Int,
sizeCS :: Int,
nbCodes :: Int,
codes :: [CodeSectionCode]
}
data CodeSection =
CS {
headerCS :: Int,
sizeCS :: Int,
nbCodes :: Int,
codes :: [CodeSectionCode]
}
deriving (Show, Eq)

data Wasm = Wasm {
headerWasm :: (Int, Int, Int, Int),
versionWasm :: (Int, Int, Int, Int),
typeSection :: TypeSection,
functionSection :: FunctionSection,
memorySection :: MemorySection,
exportSection :: ExportSection,
codeSection :: CodeSection
}
data Wasm =
Wasm {
headerWasm :: (Int, Int, Int, Int),
versionWasm :: (Int, Int, Int, Int),
typeSection :: TypeSection,
functionSection :: FunctionSection,
memorySection :: MemorySection,
exportSection :: ExportSection,
codeSection :: CodeSection
}
deriving (Show, Eq)
45 changes: 45 additions & 0 deletions lvtc/src/WasmUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ module WasmUtils
getDefaultExportSection,
getDefaultCodeSectionCode,
getDefaultCodeSection,
getDefaultWasm,
--
fillBlankTypeSectionType,
fillBlankTypeSection,
fillBlankFunctionSection,
fillBlankMemorySection,
fillBlankExportSectionExport,
fillBlankExportSection,
fillBlankCodeSectionCode,
fillBlankCodeSection,
Expand Down Expand Up @@ -96,6 +99,37 @@ getDefaultMemorySection = MS {
maxMS = 0x0
}

fillBlankMemorySection :: MemorySection -> MemorySection
fillBlankMemorySection (MS hM _ 0 miMS maMS) =
MS {
headerMS = hM,
sizeMS = size,
hasMax = 0,
minMS = miMS,
maxMS = maMS
}
where
size = 2
fillBlankMemorySection (MS hM _ _ miMS maMS) =
MS {
headerMS = hM,
sizeMS = size,
hasMax = 1,
minMS = miMS,
maxMS = maMS
}
where
size = 3

fillBlankExportSectionExport :: ExportSectionExport -> ExportSectionExport
fillBlankExportSectionExport (ESE _ n t i) =
ESE {
nameLength = length n,
name = n,
typeESE = t,
indexESE = i
}

getSizeExportSectionExport :: ExportSectionExport -> Int
getSizeExportSectionExport (ESE _ n _ _) =
1 + ((length n) * 1) + 1 + 1
Expand Down Expand Up @@ -169,3 +203,14 @@ fillBlankCodeSection (CS hC _ _ c) =
}
where
size = 1 + sum (map getSizeCodeSectionCode c)

getDefaultWasm :: Wasm
getDefaultWasm = Wasm {
headerWasm = (0x00, 0x61, 0x73, 0x6d),
versionWasm = (0x01, 0x00, 0x00, 0x00),
typeSection = getDefaultTypeSection,
functionSection = getDefaultFunctionSection,
memorySection = getDefaultMemorySection,
exportSection = getDefaultExportSection,
codeSection = getDefaultCodeSection
}
4 changes: 4 additions & 0 deletions lvtc/src/WatAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ instance Eq FuncDef where
(==) (FuncDef aa aaa a b c d e) (FuncDef aa' aaa' a' b' c' d' e') =
aa == aa' && aaa == aaa' && a == a'
&& b == b' && c == c' && d == d' && e == e'

instance Ord FuncDef where
compare (FuncDef _ _ ind _ _ _ _) (FuncDef _ _ ind' _ _ _ _) =
compare ind ind'
Loading

0 comments on commit 841fce5

Please sign in to comment.