From c0f0c8fafeaec847d9e777d669438705dbeba88e Mon Sep 17 00:00:00 2001 From: Olivier Nicole Date: Thu, 12 Sep 2024 18:23:27 +0200 Subject: [PATCH 1/5] Handle primitives that depend on the OCaml version --- runtime/wasm/domain.wat | 22 ++------ runtime/wasm/dune | 96 ++++++++++++++++++++++++++++++++- runtime/wasm/post-5.2.wat | 57 ++++++++++++++++++++ runtime/wasm/pre-5.2.wat | 42 +++++++++++++++ runtime/wasm/runtime_events.wat | 4 -- 5 files changed, 196 insertions(+), 25 deletions(-) create mode 100644 runtime/wasm/post-5.2.wat create mode 100644 runtime/wasm/pre-5.2.wat diff --git a/runtime/wasm/domain.wat b/runtime/wasm/domain.wat index 2ba72bcf9..d07d70053 100644 --- a/runtime/wasm/domain.wat +++ b/runtime/wasm/domain.wat @@ -19,11 +19,6 @@ (type $block (array (mut (ref eq)))) (type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq)))) (type $closure (sub (struct (;(field i32);) (field (ref $function_1))))) - (import "sync" "caml_ml_mutex_unlock" - (func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq)))) - (import "obj" "caml_callback_1" - (func $caml_callback_1 - (param (ref eq)) (param (ref eq)) (result (ref eq)))) (func (export "caml_atomic_cas") (param $ref (ref eq)) (param $o (ref eq)) (param $n (ref eq)) @@ -96,20 +91,9 @@ (param (ref eq)) (result (ref eq)) (ref.i31 (i32.const 1))) - (global $caml_domain_id (mut i32) (i32.const 0)) - (global $caml_domain_latest_id (mut i32) (i32.const 1)) - - (func (export "caml_domain_spawn") - (param $f (ref eq)) (param $mutex (ref eq)) (result (ref eq)) - (local $id i32) (local $old i32) - (local.set $id (global.get $caml_domain_latest_id)) - (global.set $caml_domain_latest_id - (i32.add (local.get $id) (i32.const 1))) - (local.set $old (global.get $caml_domain_id)) - (drop (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0)))) - (global.set $caml_domain_id (local.get $old)) - (drop (call $caml_ml_mutex_unlock (local.get $mutex))) - (ref.i31 (local.get $id))) + (global $caml_domain_id (export "caml_domain_id") (mut i32) (i32.const 0)) + (global $caml_domain_latest_id (export "caml_domain_latest_id") (mut i32) + (i32.const 1)) (func (export "caml_ml_domain_id") (param (ref eq)) (result (ref eq)) (ref.i31 (global.get $caml_domain_id))) diff --git a/runtime/wasm/dune b/runtime/wasm/dune index 2923b1ac9..cf759a62b 100644 --- a/runtime/wasm/dune +++ b/runtime/wasm/dune @@ -3,11 +3,64 @@ (package wasm_of_ocaml-compiler) (files runtime.wasm runtime.js)) +(rule + (target version-dependent.wat) + (deps post-5.2.wat) + (enabled_if (>= %{ocaml_version} 5.2.0)) + (action + (copy %{deps} %{target}))) + +(rule + (target version-dependent.wat) + (deps pre-5.2.wat) + (enabled_if (< %{ocaml_version} 5.2.0)) + (action + (copy %{deps} %{target}))) + (rule (target runtime.wasm) (deps args - (glob_files *.wat)) + array.wat + backtrace.wat + bigarray.wat + bigstring.wat + compare.wat + custom.wat + domain.wat + dynlink.wat + effect.wat + fail.wat + float.wat + fs.wat + gc.wat + hash.wat + int32.wat + int64.wat + ints.wat + io.wat + jslib.wat + jslib_js_of_ocaml.wat + jsstring.wat + lexing.wat + marshal.wat + md5.wat + nat.wat + obj.wat + parsing.wat + printexc.wat + prng.wat + runtime_events.wat + stdlib.wat + str.wat + string.wat + sync.wat + sys.wat + toplevel.wat + unix.wat + version-dependent.wat + weak.wat + zstd.wat) (action (progn (system @@ -47,7 +100,46 @@ (target args) (deps args.ml - (glob_files *.wat)) + array.wat + backtrace.wat + bigarray.wat + bigstring.wat + compare.wat + custom.wat + domain.wat + dynlink.wat + effect.wat + fail.wat + float.wat + fs.wat + gc.wat + hash.wat + int32.wat + int64.wat + ints.wat + io.wat + jslib.wat + jslib_js_of_ocaml.wat + jsstring.wat + lexing.wat + marshal.wat + md5.wat + nat.wat + obj.wat + parsing.wat + printexc.wat + prng.wat + runtime_events.wat + stdlib.wat + str.wat + string.wat + sync.wat + sys.wat + toplevel.wat + unix.wat + version-dependent.wat + weak.wat + zstd.wat) (action (with-stdout-to %{target} diff --git a/runtime/wasm/post-5.2.wat b/runtime/wasm/post-5.2.wat new file mode 100644 index 000000000..cd99832f1 --- /dev/null +++ b/runtime/wasm/post-5.2.wat @@ -0,0 +1,57 @@ +;; Wasm_of_ocaml runtime support +;; http://www.ocsigen.org/js_of_ocaml/ +;; +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU Lesser General Public License as published by +;; the Free Software Foundation, with linking exception; +;; either version 2.1 of the License, or (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU Lesser General Public License for more details. +;; +;; You should have received a copy of the GNU Lesser General Public License +;; along with this program; if not, write to the Free Software +;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +(module + (type $block (array (mut (ref eq)))) + (import "obj" "caml_callback_1" + (func $caml_callback_1 + (param (ref eq)) (param (ref eq)) (result (ref eq)))) + (import "sync" "caml_ml_mutex_unlock" + (func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq)))) + (import "domain" "caml_domain_latest_id" + (global $caml_domain_latest_id (mut i32))) + (import "domain" "caml_domain_id" + (global $caml_domain_id (mut i32))) + + (func (export "caml_runtime_events_user_write") + (param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq)) + (ref.i31 (i32.const 0))) + + (func (export "caml_domain_spawn") + (param $f (ref eq)) (param $term_sync_v (ref eq)) (result (ref eq)) + (local $id i32) (local $old i32) (local $ts (ref $block)) (local $res (ref eq)) + (local.set $id (global.get $caml_domain_latest_id)) + (global.set $caml_domain_latest_id + (i32.add (local.get $id) (i32.const 1))) + (local.set $old (global.get $caml_domain_id)) + (local.set $res + (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0)))) + (global.set $caml_domain_id (local.get $old)) + (local.set $ts (ref.cast (ref $block) (local.get $term_sync_v))) + (drop (call $caml_ml_mutex_unlock (array.get $block (local.get $ts) (i32.const 2)))) + ;; TODO: fix exn case + (array.set + $block + (local.get $ts) + (i32.const 1) + (array.new_fixed + $block + 2 + (ref.i31 (i32.const 0)) + (array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $res)))) + (ref.i31 (local.get $id))) +) diff --git a/runtime/wasm/pre-5.2.wat b/runtime/wasm/pre-5.2.wat new file mode 100644 index 000000000..fafc0413c --- /dev/null +++ b/runtime/wasm/pre-5.2.wat @@ -0,0 +1,42 @@ +;; Wasm_of_ocaml runtime support +;; http://www.ocsigen.org/js_of_ocaml/ +;; +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU Lesser General Public License as published by +;; the Free Software Foundation, with linking exception; +;; either version 2.1 of the License, or (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU Lesser General Public License for more details. +;; +;; You should have received a copy of the GNU Lesser General Public License +;; along with this program; if not, write to the Free Software +;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +(module + (import "obj" "caml_callback_1" + (func $caml_callback_1 + (param (ref eq)) (param (ref eq)) (result (ref eq)))) + (import "sync" "caml_ml_mutex_unlock" + (func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq)))) + (import "domain" "caml_domain_latest_id" + (global $caml_domain_latest_id (mut i32))) + + (func (export "caml_runtime_events_user_write") + (param (ref eq)) (param (ref eq)) (result (ref eq)) + (ref.i31 (i32.const 0))) + + (func (export "caml_domain_spawn") + (param $f (ref eq)) (param $mutex (ref eq)) (result (ref eq)) + (local $id i32) (local $old i32) + (local.set $id (global.get $caml_domain_latest_id)) + (global.set $caml_domain_latest_id + (i32.add (local.get $id) (i32.const 1))) + (local.set $old (global.get $caml_domain_id)) + (drop (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0)))) + (global.set $caml_domain_id (local.get $old)) + (drop (call $caml_ml_mutex_unlock (local.get $mutex))) + (ref.i31 (local.get $id))) +) diff --git a/runtime/wasm/runtime_events.wat b/runtime/wasm/runtime_events.wat index ff77e5570..8a50583cd 100644 --- a/runtime/wasm/runtime_events.wat +++ b/runtime/wasm/runtime_events.wat @@ -33,10 +33,6 @@ (local.get $evtag) (local.get $evtype))) - (func (export "caml_runtime_events_user_write") - (param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq)) - (ref.i31 (i32.const 0))) - (func (export "caml_runtime_events_user_resolve") (param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq)) (ref.i31 (i32.const 0))) From d30fe1f1f98eb001cc6c77c1551a0572438c0b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Fri, 13 Sep 2024 11:45:06 +0200 Subject: [PATCH 2/5] Move version-dependant files to subdirectory --- runtime/wasm/dune | 86 +------------------ .../wasm/{ => version-dependent}/post-5.2.wat | 0 .../wasm/{ => version-dependent}/pre-5.2.wat | 0 3 files changed, 4 insertions(+), 82 deletions(-) rename runtime/wasm/{ => version-dependent}/post-5.2.wat (100%) rename runtime/wasm/{ => version-dependent}/pre-5.2.wat (100%) diff --git a/runtime/wasm/dune b/runtime/wasm/dune index cf759a62b..8f501d40f 100644 --- a/runtime/wasm/dune +++ b/runtime/wasm/dune @@ -5,14 +5,14 @@ (rule (target version-dependent.wat) - (deps post-5.2.wat) + (deps version-dependent/post-5.2.wat) (enabled_if (>= %{ocaml_version} 5.2.0)) (action (copy %{deps} %{target}))) (rule (target version-dependent.wat) - (deps pre-5.2.wat) + (deps version-dependent/pre-5.2.wat) (enabled_if (< %{ocaml_version} 5.2.0)) (action (copy %{deps} %{target}))) @@ -21,46 +21,7 @@ (target runtime.wasm) (deps args - array.wat - backtrace.wat - bigarray.wat - bigstring.wat - compare.wat - custom.wat - domain.wat - dynlink.wat - effect.wat - fail.wat - float.wat - fs.wat - gc.wat - hash.wat - int32.wat - int64.wat - ints.wat - io.wat - jslib.wat - jslib_js_of_ocaml.wat - jsstring.wat - lexing.wat - marshal.wat - md5.wat - nat.wat - obj.wat - parsing.wat - printexc.wat - prng.wat - runtime_events.wat - stdlib.wat - str.wat - string.wat - sync.wat - sys.wat - toplevel.wat - unix.wat - version-dependent.wat - weak.wat - zstd.wat) + (glob_files *.wat)) (action (progn (system @@ -100,46 +61,7 @@ (target args) (deps args.ml - array.wat - backtrace.wat - bigarray.wat - bigstring.wat - compare.wat - custom.wat - domain.wat - dynlink.wat - effect.wat - fail.wat - float.wat - fs.wat - gc.wat - hash.wat - int32.wat - int64.wat - ints.wat - io.wat - jslib.wat - jslib_js_of_ocaml.wat - jsstring.wat - lexing.wat - marshal.wat - md5.wat - nat.wat - obj.wat - parsing.wat - printexc.wat - prng.wat - runtime_events.wat - stdlib.wat - str.wat - string.wat - sync.wat - sys.wat - toplevel.wat - unix.wat - version-dependent.wat - weak.wat - zstd.wat) + (glob_files *.wat)) (action (with-stdout-to %{target} diff --git a/runtime/wasm/post-5.2.wat b/runtime/wasm/version-dependent/post-5.2.wat similarity index 100% rename from runtime/wasm/post-5.2.wat rename to runtime/wasm/version-dependent/post-5.2.wat diff --git a/runtime/wasm/pre-5.2.wat b/runtime/wasm/version-dependent/pre-5.2.wat similarity index 100% rename from runtime/wasm/pre-5.2.wat rename to runtime/wasm/version-dependent/pre-5.2.wat From 5e9c0ea5c52a1c975924a79fb4208dd93075c5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Fri, 13 Sep 2024 11:45:14 +0200 Subject: [PATCH 3/5] Small fix --- runtime/wasm/version-dependent/pre-5.2.wat | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/wasm/version-dependent/pre-5.2.wat b/runtime/wasm/version-dependent/pre-5.2.wat index fafc0413c..27c671a24 100644 --- a/runtime/wasm/version-dependent/pre-5.2.wat +++ b/runtime/wasm/version-dependent/pre-5.2.wat @@ -23,6 +23,8 @@ (func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq)))) (import "domain" "caml_domain_latest_id" (global $caml_domain_latest_id (mut i32))) + (import "domain" "caml_domain_id" + (global $caml_domain_id (mut i32))) (func (export "caml_runtime_events_user_write") (param (ref eq)) (param (ref eq)) (result (ref eq)) From 6d43b92a1c96cf11e6f2bc686ba505e11525dda0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Fri, 13 Sep 2024 11:54:58 +0200 Subject: [PATCH 4/5] Format dune file --- runtime/wasm/dune | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/runtime/wasm/dune b/runtime/wasm/dune index 8f501d40f..e9afd68df 100644 --- a/runtime/wasm/dune +++ b/runtime/wasm/dune @@ -4,18 +4,20 @@ (files runtime.wasm runtime.js)) (rule - (target version-dependent.wat) - (deps version-dependent/post-5.2.wat) - (enabled_if (>= %{ocaml_version} 5.2.0)) - (action - (copy %{deps} %{target}))) + (target version-dependent.wat) + (deps version-dependent/post-5.2.wat) + (enabled_if + (>= %{ocaml_version} 5.2.0)) + (action + (copy %{deps} %{target}))) (rule - (target version-dependent.wat) - (deps version-dependent/pre-5.2.wat) - (enabled_if (< %{ocaml_version} 5.2.0)) - (action - (copy %{deps} %{target}))) + (target version-dependent.wat) + (deps version-dependent/pre-5.2.wat) + (enabled_if + (< %{ocaml_version} 5.2.0)) + (action + (copy %{deps} %{target}))) (rule (target runtime.wasm) From f41575cdf32d793f8ebfceb44003469032b2f7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Fri, 13 Sep 2024 11:54:10 +0200 Subject: [PATCH 5/5] Marshal header size change in OCaml 5.1 --- runtime/wasm/dune | 14 +++++- runtime/wasm/marshal.wat | 4 +- .../{pre-5.2.wat => post-5.1.wat} | 2 + runtime/wasm/version-dependent/post-5.2.wat | 2 + runtime/wasm/version-dependent/pre-5.1.wat | 46 +++++++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) rename runtime/wasm/version-dependent/{pre-5.2.wat => post-5.1.wat} (96%) create mode 100644 runtime/wasm/version-dependent/pre-5.1.wat diff --git a/runtime/wasm/dune b/runtime/wasm/dune index e9afd68df..4ec8e8693 100644 --- a/runtime/wasm/dune +++ b/runtime/wasm/dune @@ -13,9 +13,19 @@ (rule (target version-dependent.wat) - (deps version-dependent/pre-5.2.wat) + (deps version-dependent/post-5.2.wat) + (enabled_if + (and + (>= %{ocaml_version} 5.1.0) + (< %{ocaml_version} 5.2.0))) + (action + (copy %{deps} %{target}))) + +(rule + (target version-dependent.wat) + (deps version-dependent/pre-5.1.wat) (enabled_if - (< %{ocaml_version} 5.2.0)) + (< %{ocaml_version} 5.1.0)) (action (copy %{deps} %{target}))) diff --git a/runtime/wasm/marshal.wat b/runtime/wasm/marshal.wat index 491d84385..46373b2bf 100644 --- a/runtime/wasm/marshal.wat +++ b/runtime/wasm/marshal.wat @@ -48,8 +48,8 @@ (func $caml_find_custom_operations (param (ref $string)) (result (ref null $custom_operations)))) - (global $caml_marshal_header_size (export "caml_marshal_header_size") - (mut i32) (i32.const 20)) + (import "version-dependent" "caml_marshal_header_size" + (global $caml_marshal_header_size i32)) (global $input_val_from_string (ref $string) (array.new_fixed $string 21 diff --git a/runtime/wasm/version-dependent/pre-5.2.wat b/runtime/wasm/version-dependent/post-5.1.wat similarity index 96% rename from runtime/wasm/version-dependent/pre-5.2.wat rename to runtime/wasm/version-dependent/post-5.1.wat index 27c671a24..258505a5e 100644 --- a/runtime/wasm/version-dependent/pre-5.2.wat +++ b/runtime/wasm/version-dependent/post-5.1.wat @@ -41,4 +41,6 @@ (global.set $caml_domain_id (local.get $old)) (drop (call $caml_ml_mutex_unlock (local.get $mutex))) (ref.i31 (local.get $id))) + + (global (export "caml_marshal_header_size") i32 (i32.const 16)) ) diff --git a/runtime/wasm/version-dependent/post-5.2.wat b/runtime/wasm/version-dependent/post-5.2.wat index cd99832f1..b4183d2dc 100644 --- a/runtime/wasm/version-dependent/post-5.2.wat +++ b/runtime/wasm/version-dependent/post-5.2.wat @@ -54,4 +54,6 @@ (ref.i31 (i32.const 0)) (array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $res)))) (ref.i31 (local.get $id))) + + (global (export "caml_marshal_header_size") i32 (i32.const 16)) ) diff --git a/runtime/wasm/version-dependent/pre-5.1.wat b/runtime/wasm/version-dependent/pre-5.1.wat new file mode 100644 index 000000000..cc23b90ad --- /dev/null +++ b/runtime/wasm/version-dependent/pre-5.1.wat @@ -0,0 +1,46 @@ +;; Wasm_of_ocaml runtime support +;; http://www.ocsigen.org/js_of_ocaml/ +;; +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU Lesser General Public License as published by +;; the Free Software Foundation, with linking exception; +;; either version 2.1 of the License, or (at your option) any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU Lesser General Public License for more details. +;; +;; You should have received a copy of the GNU Lesser General Public License +;; along with this program; if not, write to the Free Software +;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +(module + (import "obj" "caml_callback_1" + (func $caml_callback_1 + (param (ref eq)) (param (ref eq)) (result (ref eq)))) + (import "sync" "caml_ml_mutex_unlock" + (func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq)))) + (import "domain" "caml_domain_latest_id" + (global $caml_domain_latest_id (mut i32))) + (import "domain" "caml_domain_id" + (global $caml_domain_id (mut i32))) + + (func (export "caml_runtime_events_user_write") + (param (ref eq)) (param (ref eq)) (result (ref eq)) + (ref.i31 (i32.const 0))) + + (func (export "caml_domain_spawn") + (param $f (ref eq)) (param $mutex (ref eq)) (result (ref eq)) + (local $id i32) (local $old i32) + (local.set $id (global.get $caml_domain_latest_id)) + (global.set $caml_domain_latest_id + (i32.add (local.get $id) (i32.const 1))) + (local.set $old (global.get $caml_domain_id)) + (drop (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0)))) + (global.set $caml_domain_id (local.get $old)) + (drop (call $caml_ml_mutex_unlock (local.get $mutex))) + (ref.i31 (local.get $id))) + + (global (export "caml_marshal_header_size") i32 (i32.const 20)) +)