Skip to content

Commit 28fa15b

Browse files
committed
Code reordering
1 parent 6e4b457 commit 28fa15b

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"
@@ -77,55 +126,6 @@
77126
(func $ta_blit_to_bytes
78127
(param (ref extern)) (param i32) (param (ref $bytes)) (param i32)
79128
(param i32)))
80-
(import "fail" "caml_bound_error" (func $caml_bound_error))
81-
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
82-
(import "fail" "caml_invalid_argument"
83-
(func $caml_invalid_argument (param (ref eq))))
84-
(import "fail" "caml_failwith" (func $caml_failwith (param (ref eq))))
85-
(import "jslib" "wrap" (func $wrap (param anyref) (result (ref eq))))
86-
(import "jslib" "unwrap" (func $unwrap (param (ref eq)) (result anyref)))
87-
(import "int32" "caml_copy_int32"
88-
(func $caml_copy_int32 (param i32) (result (ref eq))))
89-
(import "int32" "Int32_val"
90-
(func $Int32_val (param (ref eq)) (result i32)))
91-
(import "int32" "caml_copy_nativeint"
92-
(func $caml_copy_nativeint (param i32) (result (ref eq))))
93-
(import "int64" "caml_copy_int64"
94-
(func $caml_copy_int64 (param i64) (result (ref eq))))
95-
(import "int64" "Int64_val"
96-
(func $Int64_val (param (ref eq)) (result i64)))
97-
(import "obj" "double_array_tag" (global $double_array_tag i32))
98-
(import "compare" "unordered" (global $unordered i32))
99-
(import "hash" "caml_hash_mix_int"
100-
(func $caml_hash_mix_int (param i32) (param i32) (result i32)))
101-
(import "hash" "caml_hash_mix_int64"
102-
(func $caml_hash_mix_int64 (param i32) (param i64) (result i32)))
103-
(import "hash" "caml_hash_mix_double"
104-
(func $caml_hash_mix_double (param i32) (param f64) (result i32)))
105-
(import "hash" "caml_hash_mix_float"
106-
(func $caml_hash_mix_float (param i32) (param f32) (result i32)))
107-
(import "hash" "caml_hash_mix_float16"
108-
(func $caml_hash_mix_float16 (param i32) (param i32) (result i32)))
109-
(import "marshal" "caml_serialize_int_1"
110-
(func $caml_serialize_int_1 (param (ref eq)) (param i32)))
111-
(import "marshal" "caml_serialize_int_2"
112-
(func $caml_serialize_int_2 (param (ref eq)) (param i32)))
113-
(import "marshal" "caml_serialize_int_4"
114-
(func $caml_serialize_int_4 (param (ref eq)) (param i32)))
115-
(import "marshal" "caml_serialize_int_8"
116-
(func $caml_serialize_int_8 (param (ref eq)) (param i64)))
117-
(import "marshal" "caml_deserialize_uint_1"
118-
(func $caml_deserialize_uint_1 (param (ref eq)) (result i32)))
119-
(import "marshal" "caml_deserialize_sint_1"
120-
(func $caml_deserialize_sint_1 (param (ref eq)) (result i32)))
121-
(import "marshal" "caml_deserialize_uint_2"
122-
(func $caml_deserialize_uint_2 (param (ref eq)) (result i32)))
123-
(import "marshal" "caml_deserialize_sint_2"
124-
(func $caml_deserialize_sint_2 (param (ref eq)) (result i32)))
125-
(import "marshal" "caml_deserialize_int_4"
126-
(func $caml_deserialize_int_4 (param (ref eq)) (result i32)))
127-
(import "marshal" "caml_deserialize_int_8"
128-
(func $caml_deserialize_int_8 (param (ref eq)) (result i64)))
129129

130130
(type $block (array (mut (ref eq))))
131131
(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
@@ -38,14 +38,14 @@
3838
(func $caml_jsstring_of_string (param (ref eq)) (result (ref eq))))
3939
(import "jslib" "caml_js_to_string_array"
4040
(func $caml_js_to_string_array (param $a (ref extern)) (result (ref eq))))
41-
(import "fail" "caml_raise_sys_error"
42-
(func $caml_raise_sys_error (param (ref eq))))
4341
(import "fail" "javascript_exception"
4442
(tag $javascript_exception (param externref)))
4543
(import "sys" "caml_handle_sys_error"
4644
(func $caml_handle_sys_error (param externref)))
4745
(import "string" "caml_string_concat"
4846
(func $caml_string_concat (param (ref eq) (ref eq)) (result (ref eq))))
47+
(import "fail" "caml_raise_sys_error"
48+
(func $caml_raise_sys_error (param (ref eq))))
4949

5050
(type $bytes (array (mut i8)))
5151

runtime/wasm/io.wat

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
(param (ref extern)) (param i32) (param i32) (result (ref extern))))
7171
(import "bindings" "ta_set"
7272
(func $ta_set (param (ref extern)) (param (ref extern)) (param i32)))
73+
(import "fail" "javascript_exception"
74+
(tag $javascript_exception (param externref)))
75+
(import "sys" "caml_handle_sys_error"
76+
(func $caml_handle_sys_error (param externref)))
7377
(import "custom" "custom_compare_id"
7478
(func $custom_compare_id
7579
(param (ref eq)) (param (ref eq)) (param i32) (result i32)))
@@ -80,10 +84,6 @@
8084
(func $caml_copy_int64 (param i64) (result (ref eq))))
8185
(import "int64" "Int64_val"
8286
(func $Int64_val (param (ref eq)) (result i64)))
83-
(import "fail" "javascript_exception"
84-
(tag $javascript_exception (param externref)))
85-
(import "sys" "caml_handle_sys_error"
86-
(func $caml_handle_sys_error (param externref)))
8787
(import "bigarray" "caml_ba_get_data"
8888
(func $caml_ba_get_data (param (ref eq)) (result (ref extern))))
8989

@@ -97,6 +97,25 @@
9797
(import "bindings" "map_delete"
9898
(func $map_delete (param (ref extern)) (param i32)))
9999

100+
(func $ta_blit_from_buffer
101+
(param $buf (ref extern)) (param $i i32)
102+
(param $ta (ref extern)) (param $j i32)
103+
(param $len i32)
104+
(call $ta_set
105+
(local.get $ta)
106+
(call $ta_subarray (local.get $buf) (local.get $i)
107+
(i32.add (local.get $i) (local.get $len)))
108+
(local.get $j)))
109+
110+
(func $ta_blit_to_buffer
111+
(param $ta (ref extern)) (param $i i32)
112+
(param $buf (ref extern)) (param $j i32)
113+
(param $len i32)
114+
(call $ta_set (local.get $buf)
115+
(call $ta_subarray (local.get $ta) (local.get $i)
116+
(i32.add (local.get $i) (local.get $len)))
117+
(local.get $j)))
118+
100119
(type $block (array (mut (ref eq))))
101120
(type $bytes (array (mut i8)))
102121
(type $offset_array (array (mut i64)))
@@ -450,12 +469,12 @@
450469
(then
451470
(if (i32.gt_u (local.get $len) (local.get $avail))
452471
(then (local.set $len (local.get $avail))))
453-
(call $ta_set (local.get $d)
454-
(call $ta_subarray (struct.get $channel $buffer (local.get $ch))
455-
(struct.get $channel $curr (local.get $ch))
456-
(i32.add (struct.get $channel $curr (local.get $ch))
457-
(local.get $len)))
458-
(local.get $pos))
472+
(call $ta_blit_from_buffer
473+
(struct.get $channel $buffer (local.get $ch))
474+
(struct.get $channel $curr (local.get $ch))
475+
(local.get $d)
476+
(local.get $pos)
477+
(local.get $len))
459478
(struct.set $channel $curr (local.get $ch)
460479
(i32.add (struct.get $channel $curr (local.get $ch))
461480
(local.get $len)))
@@ -466,10 +485,12 @@
466485
(struct.set $channel $max (local.get $ch) (local.get $nread))
467486
(if (i32.gt_u (local.get $len) (local.get $nread))
468487
(then (local.set $len (local.get $nread))))
469-
(call $ta_set (local.get $d)
470-
(call $ta_subarray (struct.get $channel $buffer (local.get $ch))
471-
(i32.const 0) (local.get $len))
472-
(local.get $pos))
488+
(call $ta_blit_from_buffer
489+
(struct.get $channel $buffer (local.get $ch))
490+
(i32.const 0)
491+
(local.get $d)
492+
(local.get $pos)
493+
(local.get $len))
473494
(struct.set $channel $curr (local.get $ch) (local.get $len))
474495
(local.get $len))
475496

@@ -564,10 +585,7 @@
564585
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
565586
(ref.i31
566587
(i32.sub
567-
(i32.wrap_i64
568-
(struct.get $fd_offset $offset
569-
(call $get_fd_offset
570-
(struct.get $channel $fd (local.get $ch)))))
588+
(i32.wrap_i64 (call $caml_ml_get_channel_offset (local.get $ch)))
571589
(i32.sub
572590
(struct.get $channel $max (local.get $ch))
573591
(struct.get $channel $curr (local.get $ch))))))
@@ -577,10 +595,7 @@
577595
(local $ch (ref $channel))
578596
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
579597
(call $caml_copy_int64
580-
(i64.sub
581-
(struct.get $fd_offset $offset
582-
(call $get_fd_offset
583-
(struct.get $channel $fd (local.get $ch))))
598+
(i64.sub (call $caml_ml_get_channel_offset (local.get $ch))
584599
(i64.extend_i32_s
585600
(i32.sub
586601
(struct.get $channel $max (local.get $ch))
@@ -592,21 +607,15 @@
592607
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
593608
(ref.i31
594609
(i32.add
595-
(i32.wrap_i64
596-
(struct.get $fd_offset $offset
597-
(call $get_fd_offset
598-
(struct.get $channel $fd (local.get $ch)))))
610+
(i32.wrap_i64 (call $caml_ml_get_channel_offset (local.get $ch)))
599611
(struct.get $channel $curr (local.get $ch)))))
600612

601613
(func (export "caml_ml_pos_out_64")
602614
(param $vch (ref eq)) (result (ref eq))
603615
(local $ch (ref $channel))
604616
(local.set $ch (ref.cast (ref $channel) (local.get $vch)))
605617
(call $caml_copy_int64
606-
(i64.add
607-
(struct.get $fd_offset $offset
608-
(call $get_fd_offset
609-
(struct.get $channel $fd (local.get $ch))))
618+
(i64.add (call $caml_ml_get_channel_offset (local.get $ch))
610619
(i64.extend_i32_s (struct.get $channel $curr (local.get $ch))))))
611620

612621
(func $caml_seek_in
@@ -837,10 +846,10 @@
837846
(if (i32.ge_u (local.get $len) (local.get $free))
838847
(then (local.set $len (local.get $free))))
839848
(local.set $buf (struct.get $channel $buffer (local.get $ch)))
840-
(call $ta_set (local.get $buf)
841-
(call $ta_subarray (local.get $d)
842-
(local.get $pos) (i32.add (local.get $pos) (local.get $len)))
843-
(local.get $curr))
849+
(call $ta_blit_to_buffer
850+
(local.get $d) (local.get $pos)
851+
(local.get $buf) (local.get $curr)
852+
(local.get $len))
844853
(struct.set $channel $curr (local.get $ch)
845854
(i32.add (local.get $curr) (local.get $len)))
846855
(if (i32.ge_u (local.get $len) (local.get $free))
@@ -967,7 +976,8 @@
967976
(struct.set $channel $fd
968977
(ref.cast (ref $channel) (local.get 0)) (local.get 1)))
969978

970-
(func (export "caml_ml_get_channel_offset") (param $ch (ref eq)) (result i64)
979+
(func $caml_ml_get_channel_offset (export "caml_ml_get_channel_offset")
980+
(param $ch (ref eq)) (result i64)
971981
(struct.get $fd_offset $offset
972982
(call $get_fd_offset
973983
(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 $exn)))))
234236
(call $exit (i32.const 2)))))
235237
)

0 commit comments

Comments
 (0)