Skip to content

Commit

Permalink
Fix if
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 14, 2024
1 parent 2a5515f commit a24f195
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 19 deletions.
12 changes: 6 additions & 6 deletions lvtc/src/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ getBuiltinsFuncOperator =
getBuiltinsFuncComp :: [FuncDeclaration]
getBuiltinsFuncComp =
[
((False, "==", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, "<", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, ">", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, "<=", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, ">=", [("x", "Int"), ("y", "Int")], "Int"), []),
((False, "!=", [("x", "Int"), ("y", "Int")], "Int"), [])
((False, "==", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, "<", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, ">", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, "<=", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, ">=", [("x", "Int"), ("y", "Int")], "Bool"), []),
((False, "!=", [("x", "Int"), ("y", "Int")], "Bool"), [])
]

getBuiltinsFunc :: [FuncDeclaration]
Expand Down
11 changes: 8 additions & 3 deletions lvtc/src/WasmUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ module WasmUtils
--
opCodeByte,
variableTypeByte,
exportSectionExportTypeByte
exportSectionExportTypeByte,
ifTypeByte
) where

import Wasm
import WatAST (OpCode (..))
import WatAST (OpCode (..), IfType (..))
import Leb128Encode

getDefaultTypeSectionType :: TypeSectionType
Expand Down Expand Up @@ -179,6 +180,7 @@ getSizeOpCode (LocalGet _) = 2
getSizeOpCode (LocalSet _) = 2
getSizeOpCode (I32Const _) = 2
getSizeOpCode (Call _) = 2
getSizeOpCode (If _) = 2
getSizeOpCode _ = 1

fillBlankCodeSectionCode :: CodeSectionCode -> CodeSectionCode
Expand Down Expand Up @@ -249,10 +251,13 @@ opCodeByte I32Mul = 0x6c
opCodeByte I32Div = 0x6d
opCodeByte Return = 0x0f
opCodeByte (Call _) = 0x10
opCodeByte If = 0x04
opCodeByte (If EmptyType) = 0x04
opCodeByte Else = 0x05
opCodeByte End = 0x0b

ifTypeByte :: IfType -> Int
ifTypeByte EmptyType = 0x40

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

Expand Down
7 changes: 6 additions & 1 deletion lvtc/src/WatAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ module WatAST
OpCode (..)
, Type (..)
, FuncDef (..)
, IfType (..)
) where

import Data.Int (Int32)

data IfType =
EmptyType
deriving (Show, Eq)

-- if opcode added, dont miss to add the right size in ./WasmUtils.hs
data OpCode =
LocalGet Int32
Expand All @@ -33,7 +38,7 @@ data OpCode =
| I32Div
| Return
| Call Int32
| If
| If IfType
| Else
| End
deriving (Show, Eq)
Expand Down
6 changes: 4 additions & 2 deletions lvtc/src/WatLikeToWat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ findTypeFromInstructions name [] = error ("Type not found for: " ++ name)
findTypeFromInstructions name ((Declaration ((name', typ), _)):xs)
| name == name' = typeStringToType typ
| otherwise = findTypeFromInstructions name xs
findTypeFromInstructions name ((Cond (_, insIf, insElse)):xs) =
findTypeFromInstructions name (insIf ++ insElse ++ xs)
findTypeFromInstructions name (_:xs) = findTypeFromInstructions name xs

varsToDecl :: [Index] -> [Instruction] -> [Var] -> [(WatAST.Type, Int32)]
Expand Down Expand Up @@ -98,12 +100,12 @@ instructionToWat (Function (indexName, values)) =
]
instructionToWat (Cond (value, ifTrue, [])) =
valueToWat value
++ [ If ]
++ [ If EmptyType ]
++ instructionsToWat ifTrue
++ [ End ]
instructionToWat (Cond (value, ifTrue, ifFalse)) =
valueToWat value
++ [ If ]
++ [ If EmptyType ]
++ instructionsToWat ifTrue
++ [ Else ]
++ instructionsToWat ifFalse
Expand Down
2 changes: 2 additions & 0 deletions lvtc/src/WriteWasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ opCodeToByte (I32Const a) =
B.pack [fromIntegral (opCodeByte (I32Const a)), fromIntegral a]
opCodeToByte (Call a) =
B.pack [fromIntegral (opCodeByte (Call a)), fromIntegral a]
opCodeToByte (If a) =
B.pack [fromIntegral (opCodeByte (If a)), fromIntegral (ifTypeByte a)]
opCodeToByte op = B.pack [fromIntegral (opCodeByte op)]

codeSectionCodeToByte :: CodeSectionCode -> B.ByteString
Expand Down
6 changes: 6 additions & 0 deletions lvtc/test/UTLexeme.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ utLexeme = testGroup "UTLexeme"
assertEqual "5"
l5_rep
(lexeme1 l5)
, testCase "lexeme6" $
assertEqual "6"
l6_rep
(lexeme1 l6)
]
where
l1 = "@Int a = 0;"
Expand All @@ -50,3 +54,5 @@ utLexeme = testGroup "UTLexeme"
l4_rep = "@Int a=0;@Int c=b(a);if(c){d(a);};"
l5 = "foo(a);\n foo(b);\n"
l5_rep = "foo(a);foo(b);"
l6 = "export fn start() -> Int\n{\n @Int one = 15;\n @Int two = 5;\n three = 0;\n if (one == 5)\n {\n three = 15;\n };\n if (two == 5)\n{\n three = 5;\n };\n <- three;\n};\n"
l6_rep = "export fn start()->Int{@Int one=15;@Int two=5;three=0;if(one==5){three=15;};if(two==5){three=5;};<-three;};"
14 changes: 7 additions & 7 deletions lvtc/test/lvt/Test.lvt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn factorial(n: Int) -> Int
{
@Int a = n - 1;
<- (n * factorial(a));
};

export fn start() -> Int
{
<- factorial(5);
@Int a = 5;
@Int b = 5;
if (a == b)
{
<- 0;
};
<- 0;
};

0 comments on commit a24f195

Please sign in to comment.