From 97ae291be5697934081b733600b693efe823707e Mon Sep 17 00:00:00 2001 From: Ilya Rezvov Date: Fri, 23 Feb 2018 16:17:11 -0800 Subject: [PATCH] handle call_indirect properly in raw instruction case --- src/Language/Wasm/Parser.y | 24 +++++++++++++++++++++++- tests/samples/stack.wast | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Language/Wasm/Parser.y b/src/Language/Wasm/Parser.y index d2e520f..912b771 100644 --- a/src/Language/Wasm/Parser.y +++ b/src/Language/Wasm/Parser.y @@ -581,7 +581,7 @@ instruction :: { [Instruction] } -- TODO: handle call_direct call properly, now it tries to consume ')' raw_instr :: { [Instruction] } : plaininstr { [PlainInstr $1] } - | 'call_indirect' folded_call_indirect { $2 } + | 'call_indirect' raw_call_indirect { $2 } | 'block' opt(ident) raw_block { [$3 $2] } | 'loop' opt(ident) raw_loop { [$3 $2] } | 'if' opt(ident) raw_if_result { $3 $2 } @@ -633,6 +633,28 @@ raw_else :: { [Instruction] } : 'end' opt(ident) { [] } | 'else' opt(ident) list(instruction) 'end' opt(ident) { concat $3 } +raw_call_indirect :: { [Instruction] } + : '(' raw_call_indirect_typeuse { (PlainInstr $ CallIndirect $ fst $2) : snd $2 } + | {- empty -} { [PlainInstr $ CallIndirect $ AnonimousTypeUse $ FuncType [] []] } + +raw_call_indirect_typeuse :: { (TypeUse, [Instruction]) } + : 'type' typeidx ')' raw_call_indirect_functype { + (IndexedTypeUse $2 $ fst $4, snd $4) + } + | raw_call_indirect_functype1 { + (AnonimousTypeUse $ fromMaybe (FuncType [] []) $ fst $1, snd $1) + } + +raw_call_indirect_functype :: { (Maybe FuncType, [Instruction]) } + : '(' raw_call_indirect_functype1 { $2 } + | {- empty -} { (Nothing, []) } + +raw_call_indirect_functype1 :: { (Maybe FuncType, [Instruction]) } + : paramsresulttypeuse raw_call_indirect_functype { + (Just $ mergeFuncType $1 $ fromMaybe emptyFuncType $ fst $2, snd $2) + } + | foldedinstr1 list(instruction) { (Nothing, $1 ++ concat $2) } + foldedinstr :: { [Instruction] } : '(' foldedinstr1 { $2 } diff --git a/tests/samples/stack.wast b/tests/samples/stack.wast index 168f9b1..1657ccb 100644 --- a/tests/samples/stack.wast +++ b/tests/samples/stack.wast @@ -1,4 +1,5 @@ (module + (type $t (func (param i32) (result i32))) (func (export "fac-expr") (param $n i64) (result i64) (local $i i64) (local $res i64)