Skip to content

Commit 0373727

Browse files
vouillonhhugo
authored andcommitted
Wasm runtime: code reordering
1 parent 5859d56 commit 0373727

File tree

10 files changed

+137
-130
lines changed

10 files changed

+137
-130
lines changed

runtime/wasm/backtrace.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717

1818
(module
19+
(import "fail" "caml_invalid_argument"
20+
(func $caml_invalid_argument (param (ref eq))))
1921
(import "bindings" "backtrace_status"
2022
(func $backtrace_status (result (ref eq))))
2123
(import "bindings" "record_backtrace"
2224
(func $record_backtrace (param (ref eq))))
23-
(import "fail" "caml_invalid_argument"
24-
(func $caml_invalid_argument (param (ref eq))))
2525

2626
(type $block (array (mut (ref eq))))
2727
(type $bytes (array (mut i8)))

runtime/wasm/bigarray.wat

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,55 @@
1616
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717

1818
(module
19+
(import "fail" "caml_bound_error" (func $caml_bound_error))
20+
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
21+
(import "fail" "caml_invalid_argument"
22+
(func $caml_invalid_argument (param (ref eq))))
23+
(import "fail" "caml_failwith" (func $caml_failwith (param (ref eq))))
24+
(import "jslib" "wrap" (func $wrap (param anyref) (result (ref eq))))
25+
(import "jslib" "unwrap" (func $unwrap (param (ref eq)) (result anyref)))
26+
(import "int32" "caml_copy_int32"
27+
(func $caml_copy_int32 (param i32) (result (ref eq))))
28+
(import "int32" "Int32_val"
29+
(func $Int32_val (param (ref eq)) (result i32)))
30+
(import "int32" "caml_copy_nativeint"
31+
(func $caml_copy_nativeint (param i32) (result (ref eq))))
32+
(import "int64" "caml_copy_int64"
33+
(func $caml_copy_int64 (param i64) (result (ref eq))))
34+
(import "int64" "Int64_val"
35+
(func $Int64_val (param (ref eq)) (result i64)))
36+
(import "obj" "double_array_tag" (global $double_array_tag i32))
37+
(import "compare" "unordered" (global $unordered i32))
38+
(import "hash" "caml_hash_mix_int"
39+
(func $caml_hash_mix_int (param i32) (param i32) (result i32)))
40+
(import "hash" "caml_hash_mix_int64"
41+
(func $caml_hash_mix_int64 (param i32) (param i64) (result i32)))
42+
(import "hash" "caml_hash_mix_double"
43+
(func $caml_hash_mix_double (param i32) (param f64) (result i32)))
44+
(import "hash" "caml_hash_mix_float"
45+
(func $caml_hash_mix_float (param i32) (param f32) (result i32)))
46+
(import "hash" "caml_hash_mix_float16"
47+
(func $caml_hash_mix_float16 (param i32) (param i32) (result i32)))
48+
(import "marshal" "caml_serialize_int_1"
49+
(func $caml_serialize_int_1 (param (ref eq)) (param i32)))
50+
(import "marshal" "caml_serialize_int_2"
51+
(func $caml_serialize_int_2 (param (ref eq)) (param i32)))
52+
(import "marshal" "caml_serialize_int_4"
53+
(func $caml_serialize_int_4 (param (ref eq)) (param i32)))
54+
(import "marshal" "caml_serialize_int_8"
55+
(func $caml_serialize_int_8 (param (ref eq)) (param i64)))
56+
(import "marshal" "caml_deserialize_uint_1"
57+
(func $caml_deserialize_uint_1 (param (ref eq)) (result i32)))
58+
(import "marshal" "caml_deserialize_sint_1"
59+
(func $caml_deserialize_sint_1 (param (ref eq)) (result i32)))
60+
(import "marshal" "caml_deserialize_uint_2"
61+
(func $caml_deserialize_uint_2 (param (ref eq)) (result i32)))
62+
(import "marshal" "caml_deserialize_sint_2"
63+
(func $caml_deserialize_sint_2 (param (ref eq)) (result i32)))
64+
(import "marshal" "caml_deserialize_int_4"
65+
(func $caml_deserialize_int_4 (param (ref eq)) (result i32)))
66+
(import "marshal" "caml_deserialize_int_8"
67+
(func $caml_deserialize_int_8 (param (ref eq)) (result i64)))
1968
(import "bindings" "ta_create"
2069
(func $ta_create (param i32) (param i32) (result (ref extern))))
2170
(import "bindings" "ta_normalize"
@@ -72,55 +121,6 @@
72121
(import "bindings" "dv_set_i8"
73122
(func $dv_set_i8 (param externref i32 i32)))
74123
(import "bindings" "littleEndian" (global $littleEndian i32))
75-
(import "fail" "caml_bound_error" (func $caml_bound_error))
76-
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
77-
(import "fail" "caml_invalid_argument"
78-
(func $caml_invalid_argument (param (ref eq))))
79-
(import "fail" "caml_failwith" (func $caml_failwith (param (ref eq))))
80-
(import "jslib" "wrap" (func $wrap (param anyref) (result (ref eq))))
81-
(import "jslib" "unwrap" (func $unwrap (param (ref eq)) (result anyref)))
82-
(import "int32" "caml_copy_int32"
83-
(func $caml_copy_int32 (param i32) (result (ref eq))))
84-
(import "int32" "Int32_val"
85-
(func $Int32_val (param (ref eq)) (result i32)))
86-
(import "int32" "caml_copy_nativeint"
87-
(func $caml_copy_nativeint (param i32) (result (ref eq))))
88-
(import "int64" "caml_copy_int64"
89-
(func $caml_copy_int64 (param i64) (result (ref eq))))
90-
(import "int64" "Int64_val"
91-
(func $Int64_val (param (ref eq)) (result i64)))
92-
(import "obj" "double_array_tag" (global $double_array_tag i32))
93-
(import "compare" "unordered" (global $unordered i32))
94-
(import "hash" "caml_hash_mix_int"
95-
(func $caml_hash_mix_int (param i32) (param i32) (result i32)))
96-
(import "hash" "caml_hash_mix_int64"
97-
(func $caml_hash_mix_int64 (param i32) (param i64) (result i32)))
98-
(import "hash" "caml_hash_mix_double"
99-
(func $caml_hash_mix_double (param i32) (param f64) (result i32)))
100-
(import "hash" "caml_hash_mix_float"
101-
(func $caml_hash_mix_float (param i32) (param f32) (result i32)))
102-
(import "hash" "caml_hash_mix_float16"
103-
(func $caml_hash_mix_float16 (param i32) (param i32) (result i32)))
104-
(import "marshal" "caml_serialize_int_1"
105-
(func $caml_serialize_int_1 (param (ref eq)) (param i32)))
106-
(import "marshal" "caml_serialize_int_2"
107-
(func $caml_serialize_int_2 (param (ref eq)) (param i32)))
108-
(import "marshal" "caml_serialize_int_4"
109-
(func $caml_serialize_int_4 (param (ref eq)) (param i32)))
110-
(import "marshal" "caml_serialize_int_8"
111-
(func $caml_serialize_int_8 (param (ref eq)) (param i64)))
112-
(import "marshal" "caml_deserialize_uint_1"
113-
(func $caml_deserialize_uint_1 (param (ref eq)) (result i32)))
114-
(import "marshal" "caml_deserialize_sint_1"
115-
(func $caml_deserialize_sint_1 (param (ref eq)) (result i32)))
116-
(import "marshal" "caml_deserialize_uint_2"
117-
(func $caml_deserialize_uint_2 (param (ref eq)) (result i32)))
118-
(import "marshal" "caml_deserialize_sint_2"
119-
(func $caml_deserialize_sint_2 (param (ref eq)) (result i32)))
120-
(import "marshal" "caml_deserialize_int_4"
121-
(func $caml_deserialize_int_4 (param (ref eq)) (result i32)))
122-
(import "marshal" "caml_deserialize_int_8"
123-
(func $caml_deserialize_int_8 (param (ref eq)) (result i64)))
124124

125125
(type $block (array (mut (ref eq))))
126126
(type $bytes (array (mut i8)))

runtime/wasm/effect.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
(func $caml_fresh_oo_id (param (ref eq)) (result (ref eq))))
2626
(import "obj" "cont_tag" (global $cont_tag i32))
2727
(import "obj" "object_tag" (global $object_tag i32))
28+
(import "obj" "caml_callback_1"
29+
(func $caml_callback_1
30+
(param (ref eq)) (param (ref eq)) (result (ref eq))))
2831
(import "stdlib" "caml_named_value"
2932
(func $caml_named_value (param (ref eq)) (result (ref null eq))))
3033
(import "fail" "ocaml_exception" (tag $ocaml_exception (param (ref eq))))
@@ -38,9 +41,6 @@
3841
(param $f funcref) (param $env eqref) (result anyref)))
3942
(import "bindings" "resume_fiber"
4043
(func $resume_fiber (param externref) (param (ref eq))))
41-
(import "obj" "caml_callback_1"
42-
(func $caml_callback_1
43-
(param (ref eq)) (param (ref eq)) (result (ref eq))))
4444

4545
(type $block (array (mut (ref eq))))
4646
(type $bytes (array (mut i8)))

runtime/wasm/fs.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
(func $caml_jsstring_of_string (param (ref eq)) (result (ref eq))))
4141
(import "jslib" "caml_js_to_string_array"
4242
(func $caml_js_to_string_array (param $a (ref extern)) (result (ref eq))))
43-
(import "fail" "caml_raise_sys_error"
44-
(func $caml_raise_sys_error (param (ref eq))))
4543
(import "fail" "javascript_exception"
4644
(tag $javascript_exception (param externref)))
4745
(import "sys" "caml_handle_sys_error"
4846
(func $caml_handle_sys_error (param externref)))
4947
(import "string" "caml_string_concat"
5048
(func $caml_string_concat (param (ref eq) (ref eq)) (result (ref eq))))
49+
(import "fail" "caml_raise_sys_error"
50+
(func $caml_raise_sys_error (param (ref eq))))
5151

5252
(type $bytes (array (mut i8)))
5353

runtime/wasm/io.wat

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
(func $dv_get_ui8 (param externref i32) (result i32)))
7373
(import "bindings" "dv_set_i8"
7474
(func $dv_set_i8 (param externref i32 i32)))
75+
(import "fail" "javascript_exception"
76+
(tag $javascript_exception (param externref)))
77+
(import "sys" "caml_handle_sys_error"
78+
(func $caml_handle_sys_error (param externref)))
7579
(import "custom" "custom_compare_id"
7680
(func $custom_compare_id
7781
(param (ref eq)) (param (ref eq)) (param i32) (result i32)))
@@ -82,10 +86,6 @@
8286
(func $caml_copy_int64 (param i64) (result (ref eq))))
8387
(import "int64" "Int64_val"
8488
(func $Int64_val (param (ref eq)) (result i64)))
85-
(import "fail" "javascript_exception"
86-
(tag $javascript_exception (param externref)))
87-
(import "sys" "caml_handle_sys_error"
88-
(func $caml_handle_sys_error (param externref)))
8989
(import "bigarray" "caml_ba_get_data"
9090
(func $caml_ba_get_data (param (ref eq)) (result (ref extern))))
9191

@@ -99,6 +99,25 @@
9999
(import "bindings" "map_delete"
100100
(func $map_delete (param (ref extern)) (param i32)))
101101

102+
(func $ta_blit_from_buffer
103+
(param $buf (ref extern)) (param $i i32)
104+
(param $ta (ref extern)) (param $j i32)
105+
(param $len i32)
106+
(call $ta_set
107+
(local.get $ta)
108+
(call $ta_subarray (local.get $buf) (local.get $i)
109+
(i32.add (local.get $i) (local.get $len)))
110+
(local.get $j)))
111+
112+
(func $ta_blit_to_buffer
113+
(param $ta (ref extern)) (param $i i32)
114+
(param $buf (ref extern)) (param $j i32)
115+
(param $len i32)
116+
(call $ta_set (local.get $buf)
117+
(call $ta_subarray (local.get $ta) (local.get $i)
118+
(i32.add (local.get $i) (local.get $len)))
119+
(local.get $j)))
120+
102121
(type $block (array (mut (ref eq))))
103122
(type $bytes (array (mut i8)))
104123
(type $offset_array (array (mut i64)))
@@ -462,12 +481,12 @@
462481
(then
463482
(if (i32.gt_u (local.get $len) (local.get $avail))
464483
(then (local.set $len (local.get $avail))))
465-
(call $ta_set (local.get $d)
466-
(call $ta_subarray (struct.get $channel $buffer (local.get $ch))
467-
(struct.get $channel $curr (local.get $ch))
468-
(i32.add (struct.get $channel $curr (local.get $ch))
469-
(local.get $len)))
470-
(local.get $pos))
484+
(call $ta_blit_from_buffer
485+
(struct.get $channel $buffer (local.get $ch))
486+
(struct.get $channel $curr (local.get $ch))
487+
(local.get $d)
488+
(local.get $pos)
489+
(local.get $len))
471490
(struct.set $channel $curr (local.get $ch)
472491
(i32.add (struct.get $channel $curr (local.get $ch))
473492
(local.get $len)))
@@ -478,10 +497,12 @@
478497
(struct.set $channel $max (local.get $ch) (local.get $nread))
479498
(if (i32.gt_u (local.get $len) (local.get $nread))
480499
(then (local.set $len (local.get $nread))))
481-
(call $ta_set (local.get $d)
482-
(call $ta_subarray (struct.get $channel $buffer (local.get $ch))
483-
(i32.const 0) (local.get $len))
484-
(local.get $pos))
500+
(call $ta_blit_from_buffer
501+
(struct.get $channel $buffer (local.get $ch))
502+
(i32.const 0)
503+
(local.get $d)
504+
(local.get $pos)
505+
(local.get $len))
485506
(struct.set $channel $curr (local.get $ch) (local.get $len))
486507
(local.get $len))
487508

@@ -576,10 +597,7 @@
576597
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
577598
(ref.i31
578599
(i32.sub
579-
(i32.wrap_i64
580-
(struct.get $fd_offset $offset
581-
(call $get_fd_offset
582-
(struct.get $channel $fd (local.get $ch)))))
600+
(i32.wrap_i64 (call $caml_ml_get_channel_offset (local.get $ch)))
583601
(i32.sub
584602
(struct.get $channel $max (local.get $ch))
585603
(struct.get $channel $curr (local.get $ch))))))
@@ -589,10 +607,7 @@
589607
(local $ch (ref $channel))
590608
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
591609
(call $caml_copy_int64
592-
(i64.sub
593-
(struct.get $fd_offset $offset
594-
(call $get_fd_offset
595-
(struct.get $channel $fd (local.get $ch))))
610+
(i64.sub (call $caml_ml_get_channel_offset (local.get $ch))
596611
(i64.extend_i32_s
597612
(i32.sub
598613
(struct.get $channel $max (local.get $ch))
@@ -604,21 +619,15 @@
604619
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
605620
(ref.i31
606621
(i32.add
607-
(i32.wrap_i64
608-
(struct.get $fd_offset $offset
609-
(call $get_fd_offset
610-
(struct.get $channel $fd (local.get $ch)))))
622+
(i32.wrap_i64 (call $caml_ml_get_channel_offset (local.get $ch)))
611623
(struct.get $channel $curr (local.get $ch)))))
612624

613625
(func (export "caml_ml_pos_out_64")
614626
(param $vch (ref eq)) (result (ref eq))
615627
(local $ch (ref $channel))
616628
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
617629
(call $caml_copy_int64
618-
(i64.add
619-
(struct.get $fd_offset $offset
620-
(call $get_fd_offset
621-
(struct.get $channel $fd (local.get $ch))))
630+
(i64.add (call $caml_ml_get_channel_offset (local.get $ch))
622631
(i64.extend_i32_s (struct.get $channel $curr (local.get $ch))))))
623632

624633
(func $caml_seek_in
@@ -850,10 +859,10 @@
850859
(if (i32.ge_u (local.get $len) (local.get $free))
851860
(then (local.set $len (local.get $free))))
852861
(local.set $buf (struct.get $channel $buffer (local.get $ch)))
853-
(call $ta_set (local.get $buf)
854-
(call $ta_subarray (local.get $d)
855-
(local.get $pos) (i32.add (local.get $pos) (local.get $len)))
856-
(local.get $curr))
862+
(call $ta_blit_to_buffer
863+
(local.get $d) (local.get $pos)
864+
(local.get $buf) (local.get $curr)
865+
(local.get $len))
857866
(struct.set $channel $curr (local.get $ch)
858867
(i32.add (local.get $curr) (local.get $len)))
859868
(if (i32.ge_u (local.get $len) (local.get $free))
@@ -980,7 +989,8 @@
980989
(struct.set $channel $fd
981990
(ref.cast (ref $channel) (local.get 0)) (local.get 1)))
982991

983-
(func (export "caml_ml_get_channel_offset") (param $ch (ref eq)) (result i64)
992+
(func $caml_ml_get_channel_offset (export "caml_ml_get_channel_offset")
993+
(param $ch (ref eq)) (result i64)
984994
(struct.get $fd_offset $offset
985995
(call $get_fd_offset
986996
(struct.get $channel $fd

runtime/wasm/marshal.wat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
(func $caml_is_closure (param (ref eq)) (result i32)))
3131
(import "effect" "caml_is_continuation"
3232
(func $caml_is_continuation (param (ref eq)) (result i32)))
33-
(import "bindings" "map_new" (func $map_new (result (ref any))))
34-
(import "bindings" "map_get"
35-
(func $map_get (param (ref any)) (param (ref eq)) (result i31ref)))
36-
(import "bindings" "map_set"
37-
(func $map_set (param (ref any)) (param (ref eq)) (param (ref i31))))
3833
(import "io" "caml_really_putblock"
3934
(func $caml_really_putblock
4035
(param (ref eq)) (param (ref $bytes)) (param i32) (param i32)))
@@ -49,6 +44,11 @@
4944
(import "custom" "caml_find_custom_operations"
5045
(func $caml_find_custom_operations
5146
(param (ref $bytes)) (result (ref null $custom_operations))))
47+
(import "bindings" "map_new" (func $map_new (result (ref any))))
48+
(import "bindings" "map_get"
49+
(func $map_get (param (ref any)) (param (ref eq)) (result i31ref)))
50+
(import "bindings" "map_set"
51+
(func $map_set (param (ref any)) (param (ref eq)) (param (ref i31))))
5252

5353
(@string $input_val_from_string "input_value_from_string")
5454

runtime/wasm/stdlib.wat

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
(import "obj" "caml_callback_2"
3232
(func $caml_callback_2
3333
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))))
34-
(import "bindings" "write" (func $write (param i32) (param anyref)))
3534
(import "string" "caml_string_concat"
3635
(func $caml_string_concat
3736
(param (ref eq)) (param (ref eq)) (result (ref eq))))
@@ -41,6 +40,7 @@
4140
(import "fail" "ocaml_exception" (tag $ocaml_exception (param (ref eq))))
4241
(import "fail" "javascript_exception"
4342
(tag $javascript_exception (param externref)))
43+
(import "bindings" "write" (func $write (param i32) (param anyref)))
4444
(import "bindings" "exit" (func $exit (param i32)))
4545

4646
(type $block (array (mut (ref eq))))
@@ -197,6 +197,7 @@
197197

198198
(func $caml_main (export "caml_main") (param $start (ref func))
199199
(local $exn (ref eq))
200+
(local $msg (ref eq))
200201
(try
201202
(do
202203
(drop (call_ref $func (ref.cast (ref $func) (local.get $start)))))
@@ -223,13 +224,14 @@
223224
(br_on_null $null
224225
(call $caml_named_value (global.get $do_at_exit)))
225226
(ref.i31 (i32.const 0)))))
227+
(local.set $msg
228+
(call $caml_string_concat
229+
(global.get $fatal_error)
230+
(call $caml_string_concat
231+
(call $caml_format_exception (local.get $exn))
232+
(@string "\n"))))
226233
(call $write (i32.const 2)
227234
(call $unwrap
228-
(call $caml_jsstring_of_string
229-
(call $caml_string_concat
230-
(global.get $fatal_error)
231-
(call $caml_string_concat
232-
(call $caml_format_exception (local.get $exn))
233-
(@string "\n")))))))
235+
(call $caml_jsstring_of_string (local.get $msg)))))
234236
(call $exit (i32.const 2)))))
235237
)

0 commit comments

Comments
 (0)