Skip to content

Commit

Permalink
Merge pull request #100 from Wasm-DSL/bug.start
Browse files Browse the repository at this point in the history
Outline start function instructions
  • Loading branch information
rossberg authored Jun 5, 2024
2 parents 83cca63 + 7e5b8d7 commit c0a1763
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 46 deletions.
4 changes: 2 additions & 2 deletions spectec/spec/wasm-3.0/9-module.watsup
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def $rundata_(x, DATA b^n (ACTIVE y instr*)) =
instr* (CONST I32 0) (CONST I32 n) (MEMORY.INIT y x) (DATA.DROP x)

def $instantiate(store, module, externval*) : config
def $instantiate(s, module, externval*) = s'; f; instr_E* instr_D* (CALL x)?
def $instantiate(s, module, externval*) = s'; f; instr_E* instr_D* instr_S?
---- ----
;; TODO: -- Module_ok: |- module : xt_I* -> xt_E*
;; TODO: -- (Externval_type: s |- externval : xt_I)*
Expand All @@ -175,7 +175,7 @@ def $instantiate(s, module, externval*) = s'; f; instr_E* instr_D* (CALL x)?
-- if f = {MODULE mm} ;; TODO: inline
-- if instr_E* = $concat_(instr, $runelem_(i_E, elem*[i_E])^(i_E<|elem*|))
-- if instr_D* = $concat_(instr, $rundata_(i_D, data*[i_D])^(i_D<|data*|))
;;-- if instr_S? = (CALL x)? ;; TODO: use above
-- if instr_S? = (CALL x)?


;;
Expand Down
25 changes: 14 additions & 11 deletions spectec/src/il2al/translate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,21 @@ let rec translate_rhs exp =
| Il.CaseE ([{it = Atom "TRAP"; _}]::_, _) -> [ trapI () ~at:at ]
(* Execute instrs
* TODO: doing this based on variable name is too ad-hoc. Need smarter way. *)
| Il.IterE ({ it = VarE id; _ }, (Il.List, _))
| Il.IterE ({ it = SubE ({ it = VarE id; _ }, _, _); _}, (Il.List, _))
| Il.IterE ({ it = VarE id; _ }, (List, _))
| Il.IterE ({ it = SubE ({ it = VarE id; _ }, _, _); _}, (List, _))
when String.starts_with ~prefix:"instr" id.it ->
[ executeseqI (translate_exp exp) ~at:at ]
| Il.IterE ({ it = CaseE ([{it = Atom "CALL"; _} as atom]::_, _); _ }, (Opt, [ (id, _) ])) ->
let new_name = varE (id.it ^ "_0") in
[ ifI (isDefinedE (varE id.it),
[
letI (optE (Some new_name), varE id.it) ~at:at;
executeI (caseE (translate_atom atom, [ new_name ])) ~at:at
],
[]) ~at:at ]
[executeseqI (translate_exp exp) ~at:at ]
| Il.VarE id | Il.SubE ({ it = VarE id; _ }, _, _)
when String.starts_with ~prefix:"instr" id.it ->
[ executeI (translate_exp exp) ~at:at ]
| Il.IterE (_, (Opt, _)) ->
(* TODO: need AL expression for unwrapping option *)
let tmp_name = varE "instr_0" in
[ ifI (
isDefinedE (translate_exp exp),
[ letI (optE (Some tmp_name), translate_exp exp) ~at:at; executeI tmp_name ],
[]
) ~at:at ]
(* Push *)
| Il.SubE _ | CallE _ | IterE _ -> [ pushI (translate_exp exp) ~at:at ]
| Il.CaseE ([{it = Atom id; _}]::_, _) when List.mem id [
Expand Down
3 changes: 2 additions & 1 deletion spectec/test-frontend/TEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -5702,7 +5702,7 @@ def $rundata_(dataidx : dataidx, data : data) : instr*
;; 9-module.watsup
def $instantiate(store : store, module : module, externval*) : config
;; 9-module.watsup
def $instantiate{s : store, module : module, externval* : externval*, s' : store, f : frame, instr_E* : instr*, instr_D* : instr*, x? : idx?, type* : type*, import* : import*, func* : func*, global* : global*, table* : table*, mem* : mem*, elem* : elem*, data* : data*, start? : start?, export* : export*, globaltype* : globaltype*, expr_G* : expr*, tabletype* : tabletype*, expr_T* : expr*, reftype* : reftype*, expr_E** : expr**, elemmode* : elemmode*, byte** : byte**, datamode* : datamode*, moduleinst_0 : moduleinst, i_F^|func*{func : func}| : nat^|func*{func : func}|, z : state, val_G* : val*, ref_T* : ref*, ref_E** : ref**, mm : moduleinst, i_E^|elem*{elem : elem}| : nat^|elem*{elem : elem}|, i_D^|data*{data : data}| : nat^|data*{data : data}|}(s, module, externval*{externval : externval}) = `%;%`_config(`%;%`_state(s', f), instr_E*{instr_E : instr} :: instr_D*{instr_D : instr} :: CALL_instr(x)?{x : funcidx})
def $instantiate{s : store, module : module, externval* : externval*, s' : store, f : frame, instr_E* : instr*, instr_D* : instr*, instr_S? : instr?, type* : type*, import* : import*, func* : func*, global* : global*, table* : table*, mem* : mem*, elem* : elem*, data* : data*, start? : start?, export* : export*, globaltype* : globaltype*, expr_G* : expr*, tabletype* : tabletype*, expr_T* : expr*, reftype* : reftype*, expr_E** : expr**, elemmode* : elemmode*, byte** : byte**, datamode* : datamode*, x? : idx?, moduleinst_0 : moduleinst, i_F^|func*{func : func}| : nat^|func*{func : func}|, z : state, val_G* : val*, ref_T* : ref*, ref_E** : ref**, mm : moduleinst, i_E^|elem*{elem : elem}| : nat^|elem*{elem : elem}|, i_D^|data*{data : data}| : nat^|data*{data : data}|}(s, module, externval*{externval : externval}) = `%;%`_config(`%;%`_state(s', f), instr_E*{instr_E : instr} :: instr_D*{instr_D : instr} :: instr_S?{instr_S : instr})
-- if (module = MODULE_module(type*{type : type}, import*{import : import}, func*{func : func}, global*{global : global}, table*{table : table}, mem*{mem : mem}, elem*{elem : elem}, data*{data : data}, start?{start : start}, export*{export : export}))
-- if (global*{global : global} = GLOBAL_global(globaltype, expr_G)*{expr_G : expr, globaltype : globaltype})
-- if (table*{table : table} = TABLE_table(tabletype, expr_T)*{expr_T : expr, tabletype : tabletype})
Expand All @@ -5718,6 +5718,7 @@ def $instantiate(store : store, module : module, externval*) : config
-- if (f = {LOCALS [], MODULE mm})
-- if (instr_E*{instr_E : instr} = $concat_(syntax instr, $runelem_(`%`_elemidx(i_E), elem*{elem : elem}[i_E])^(i_E<|elem*{elem : elem}|){i_E : nat}))
-- if (instr_D*{instr_D : instr} = $concat_(syntax instr, $rundata_(`%`_dataidx(i_D), data*{data : data}[i_D])^(i_D<|data*{data : data}|){i_D : nat}))
-- if (instr_S?{instr_S : instr} = CALL_instr(x)?{x : funcidx})

;; 9-module.watsup
def $invoke(store : store, funcaddr : funcaddr, val*) : config
Expand Down
3 changes: 2 additions & 1 deletion spectec/test-latex/TEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -8078,7 +8078,7 @@ $$

$$
\begin{array}{@{}lcl@{}l@{}}
{\mathrm{instantiate}}(s, {\mathit{module}}, {{\mathit{externval}}^\ast}) &=& \multicolumn{2}{l@{}}{ {s'} ; f ; {{\mathit{instr}}_{\mathsf{e}}^\ast}~{{\mathit{instr}}_{\mathsf{d}}^\ast}~{(\mathsf{call}~x)^?} } \\
{\mathrm{instantiate}}(s, {\mathit{module}}, {{\mathit{externval}}^\ast}) &=& \multicolumn{2}{l@{}}{ {s'} ; f ; {{\mathit{instr}}_{\mathsf{e}}^\ast}~{{\mathit{instr}}_{\mathsf{d}}^\ast}~{{\mathit{instr}}_{\mathsf{s}}^?} } \\
\multicolumn{4}{@{}l@{}}{\qquad\quad \mbox{if}~{\mathit{module}} = \mathsf{module}~{{\mathit{type}}^\ast}~{{\mathit{import}}^\ast}~{{\mathit{func}}^\ast}~{{\mathit{global}}^\ast}~{{\mathit{table}}^\ast}~{{\mathit{mem}}^\ast}~{{\mathit{elem}}^\ast}~{{\mathit{data}}^\ast}~{{\mathit{start}}^?}~{{\mathit{export}}^\ast}} \\
\multicolumn{4}{@{}l@{}}{\qquad\quad {\land}~{{\mathit{global}}^\ast} = {(\mathsf{global}~{\mathit{globaltype}}~{\mathit{expr}}_{\mathsf{g}})^\ast}} \\
\multicolumn{4}{@{}l@{}}{\qquad\quad {\land}~{{\mathit{table}}^\ast} = {(\mathsf{table}~{\mathit{tabletype}}~{\mathit{expr}}_{\mathsf{t}})^\ast}} \\
Expand All @@ -8099,6 +8099,7 @@ $$
\mathsf{module}~{\mathit{mm}} \}\end{array}} \\
\multicolumn{4}{@{}l@{}}{\qquad\quad {\land}~{{\mathit{instr}}_{\mathsf{e}}^\ast} = {\mathrm{concat}}({{{\mathrm{runelem}}}_{i_{\mathsf{e}}}({{\mathit{elem}}^\ast}{}[i_{\mathsf{e}}])^{i_{\mathsf{e}}<{|{{\mathit{elem}}^\ast}|}}})} \\
\multicolumn{4}{@{}l@{}}{\qquad\quad {\land}~{{\mathit{instr}}_{\mathsf{d}}^\ast} = {\mathrm{concat}}({{{\mathrm{rundata}}}_{i_{\mathsf{d}}}({{\mathit{data}}^\ast}{}[i_{\mathsf{d}}])^{i_{\mathsf{d}}<{|{{\mathit{data}}^\ast}|}}})} \\
\multicolumn{4}{@{}l@{}}{\qquad\quad {\land}~{{\mathit{instr}}_{\mathsf{s}}^?} = {(\mathsf{call}~x)^?}} \\
\end{array}
$$

Expand Down
Loading

0 comments on commit c0a1763

Please sign in to comment.