Skip to content

Commit 22aa8ab

Browse files
committed
Preprocessor: use the export name to name functions without id
This provides a better debugging experience since reference to these functions will now use an id rather than a number in disassembled code.
1 parent b99f325 commit 22aa8ab

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

compiler/lib-wasm/wat_preprocess.ml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ let rec parse_string_contents lexbuf =
225225
None
226226
| _ -> assert false
227227

228+
let opt_parse_string s =
229+
parse_string_contents
230+
(Sedlexing.Utf8.from_string (String.sub s ~pos:1 ~len:(String.length s - 2)))
231+
228232
let parse_string loc s =
229-
match
230-
parse_string_contents
231-
(Sedlexing.Utf8.from_string (String.sub s ~pos:1 ~len:(String.length s - 2)))
232-
with
233+
match opt_parse_string s with
233234
| None ->
234235
raise
235236
(Error
@@ -284,6 +285,11 @@ let type_name (t : typ) =
284285
| String -> "string"
285286
| Version -> "version"
286287

288+
let variable_is_set st nm =
289+
match StringMap.find_opt nm st.variables with
290+
| Some (Bool true) -> true
291+
| _ -> false
292+
287293
let check_type ?typ expr actual_typ =
288294
match typ with
289295
| None -> ()
@@ -516,6 +522,28 @@ and rewrite st elt =
516522
| { desc = List ({ desc = Atom "@string"; _ } :: _ :: _ :: { loc; _ } :: _); _ } ->
517523
raise
518524
(Error (position_of_loc loc, Printf.sprintf "Expecting a closing parenthesis.\n"))
525+
| { desc =
526+
List
527+
({ desc = Atom "func"; loc = _, pos }
528+
:: { desc =
529+
List
530+
[ { desc = Atom "export"; _ }
531+
; { desc = Atom export_name; loc = export_loc }
532+
]
533+
; loc = pos', _
534+
}
535+
:: l)
536+
; _
537+
}
538+
when variable_is_set st "name-wasm-functions"
539+
&&
540+
match opt_parse_string export_name with
541+
| None -> false
542+
| Some s -> is_id ("$" ^ s) ->
543+
write st pos;
544+
insert st (Printf.sprintf " $%s " (parse_string export_loc export_name));
545+
skip st pos';
546+
rewrite_list st l
519547
| { desc = List l; _ } -> rewrite_list st l
520548
| _ -> ()
521549

@@ -525,12 +553,14 @@ let ocaml_version =
525553
Scanf.sscanf Sys.ocaml_version "%d.%d.%d" (fun major minor patchlevel ->
526554
Version (major, minor, patchlevel))
527555

556+
let default_settings = [ "name-wasm-functions", Bool true ]
557+
528558
let f ~variables ~filename ~contents:text =
529559
let variables =
530560
List.fold_left
531561
~f:(fun m (k, v) -> StringMap.add k v m)
532562
~init:StringMap.empty
533-
variables
563+
(default_settings @ variables)
534564
in
535565
let variables = StringMap.add "ocaml_version" ocaml_version variables in
536566
let lexbuf = Sedlexing.Utf8.from_string text in

manual/wasm_runtime.wiki

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ To form conditional expressions, the following operators are available:
6060

6161
This also provides some syntactic sugar to write strings. The expression{{{(@string "ab")}}} is expanded into {{{(array.new_fixed $string 2 (i32.const 97) (i32.const 98))}}}. The statement {{{(@string $s "ab")}}} is an abbreviatiation for {{{(global $s (ref eq) (@string "ab"))}}}.
6262

63+
To provide a better debugging experience, the function export name is used to name functions with no explicit id: {{{(func (export "foo") ...)}}}} is expanded into {{{(func $foo (export "foo") ...)}}}}.
64+
6365
== Implementing primitives ==
6466

6567
You define a primitive by exporting a Wasm function with parameters and return value of type {{{(ref eq)}}}.

runtime/wasm/jslib_js_of_ocaml.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
(import "jslib" "caml_js_from_array"
2828
(func $caml_js_from_array (param (ref eq)) (result (ref eq))))
2929
(import "js" "caml_js_html_escape"
30-
(func $caml_js_html_escape (param anyref) (result anyref)))
30+
(func $caml_js_html_escape_js (param anyref) (result anyref)))
3131
(import "js" "caml_js_html_entities"
32-
(func $caml_js_html_entities (param anyref) (result anyref)))
32+
(func $caml_js_html_entities_js (param anyref) (result anyref)))
3333

3434
(type $block (array (mut (ref eq))))
3535
(type $string (array (mut i8)))
3636

3737
(func (export "caml_js_html_escape") (param (ref eq)) (result (ref eq))
3838
(return_call $wrap
39-
(call $caml_js_html_escape (call $unwrap (local.get 0)))))
39+
(call $caml_js_html_escape_js (call $unwrap (local.get 0)))))
4040

4141
(func (export "caml_js_html_entities") (param (ref eq)) (result (ref eq))
4242
(return_call $wrap
43-
(call $caml_js_html_entities (call $unwrap (local.get 0)))))
43+
(call $caml_js_html_entities_js (call $unwrap (local.get 0)))))
4444

4545
(@string $console "console")
4646

0 commit comments

Comments
 (0)