Skip to content

Commit

Permalink
add else
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Jan 14, 2024
1 parent 38ea155 commit 01a5070
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
24 changes: 14 additions & 10 deletions lvtrun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@

# ----------------------- 2 -----------------------

02
02 24
# 02 is the id of the import section
24 01 16 77
61 73 69 5f
73 6e 61 70
73 68 6f 74
5f 70 72 65
76 69 65 77
31 09 70 72
6f 63 5f 65
78 69 74 00
# 1 vector
01
# name is 16hex = 22dec bytes long
16
# name = "wasi_snapshot_preview1"
77 61 73 69 5f 73 6e 61 70 73 68 6f 74 5f 70 72 65 76 69 65 77 31
# name is 09
09
# name = "proct_exit"
70 72 6f 63 5f 65 78 69 74
# 00 = func = function
00
# 00 = typeidx = 0
02

# ----------------------- 3 -----------------------
Expand Down
2 changes: 2 additions & 0 deletions lvtrun/src/OpCodes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extractOpCode' (0x4a:rest) = ([0x4a], BSL.pack rest)
extractOpCode' (0x4c:rest) = ([0x4c], BSL.pack rest)
extractOpCode' (0x4e:rest) = ([0x4e], BSL.pack rest)
extractOpCode' (0x47:rest) = ([0x47], BSL.pack rest)
extractOpCode' (0x05:rest) = ([0x05], BSL.pack rest)
extractOpCode' (0x3f:0x00:rest) = ([0x3f, 0x00], BSL.pack rest)
extractOpCode' (0x40:0x00:rest) = ([0x40, 0x00], BSL.pack rest)
extractOpCode' (0x04:0x40:rest) = ([0x04, 0x40], BSL.pack rest)
Expand All @@ -72,6 +73,7 @@ createInstruction [0x00] bytes = (Unreachable, bytes)
createInstruction [0x01] bytes = (Nop, bytes)
createInstruction [0x02] bytes = (Block EmptyType, bytes)
createInstruction [0x0b] bytes = (End, bytes)
createInstruction [0x05] bytes = (Else, bytes)
createInstruction [0x48] bytes = (I32Lts, bytes)
createInstruction [0x0f] bytes = (Return, bytes)
createInstruction [0x4b] bytes = (I32Gtu, bytes)
Expand Down
27 changes: 25 additions & 2 deletions lvtrun/src/Run/Vm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,32 @@ execCall vm cEx funcIdx = cEx { ceStack = newStack }
currentStack = ceStack cEx
res = ceResults (currentExec newVm)

doesElseExist' :: [Instruction] -> Bool
doesElseExist' [] = False
doesElseExist' (Else:_) = True
doesElseExist' (_:rest) = doesElseExist' rest

doesElseExist :: CurrentExec -> Bool
doesElseExist cEx = doesElseExist' (drop (ceInstIdx cEx) (ceInstructions cEx))

getElseIndex' :: [Instruction] -> Int -> Int
getElseIndex' [] _ = throw $ RuntimeError "getElseIndex: missing else"
getElseIndex' (Else:_) idx = idx
getElseIndex' (_:rest) idx = getElseIndex' rest (idx + 1)

getElseIndex :: CurrentExec -> Int
getElseIndex cEx = getElseIndex' (drop (ceInstIdx cEx) (ceInstructions cEx)) 0

executeElse :: CurrentExec -> CurrentExec
executeElse cEx@(CurrentExec {ceStack = stack}) =

Check warning on line 122 in lvtrun/src/Run/Vm.hs

View workflow job for this annotation

GitHub Actions / compil-windows

Defined but not used: `stack'

Check warning on line 122 in lvtrun/src/Run/Vm.hs

View workflow job for this annotation

GitHub Actions / compil-linux

Defined but not used: ‘stack’

Check warning on line 122 in lvtrun/src/Run/Vm.hs

View workflow job for this annotation

GitHub Actions / compil-macos

Defined but not used: ‘stack’

Check warning on line 122 in lvtrun/src/Run/Vm.hs

View workflow job for this annotation

GitHub Actions / tests

Defined but not used: ‘stack’
case doesElseExist cEx of
False -> cEx
True -> cEx { ceInstIdx = getElseIndex cEx }

execIf :: CurrentExec -> CurrentExec
execIf cEx@(CurrentExec {ceStack = stack}) = case stackTop stack of
I_32 0 -> goToEndInstruction cEx
I_32 1 -> addLabel (cEx { crBlockIndents = (crBlockIndents cEx) + 1 })
I_32 1 -> executeElse (addLabel (cEx { crBlockIndents = (crBlockIndents cEx) + 1 }))
I_32 _ -> throw $ RuntimeError "execIf: bad if statement"
_ -> throw $ RuntimeError "execIf: bad type"

Expand Down Expand Up @@ -175,7 +197,8 @@ execOpCode _ cEx (I32Gtu) = execI32GtU cEx
execOpCode _ cEx (Block _) = incrementBlockIndent (addLabel cEx)
execOpCode _ cEx (Br labelIdx) = execBr cEx labelIdx
execOpCode _ cEx (Loop) = incrementBlockIndent (addLabel cEx)
execOpCode _ cEx _ = cEx
execOpCode _ cEx (Else) = throw $ RuntimeError "elseWithoutIf"
execOpCode _ cEx _ = throw $ RuntimeError "execOpCode: not implemented"

execOpCodes :: VM -> [Instruction] -> CurrentExec
execOpCodes vm [] = currentExec vm
Expand Down
1 change: 1 addition & 0 deletions lvtrun/src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ data Instruction =
| I32Leu
| I32Eq
| I32Lts
| Else
| I32Gts
| I32Les
| I32Ges
Expand Down

0 comments on commit 01a5070

Please sign in to comment.