@@ -17,8 +17,8 @@ open Mo_config
17
17
18
18
open Wasm_exts.Ast
19
19
open Source
20
- (* Re-shadow Source.(@@), to get Stdlib.(@@) *)
21
- let ( @@ ) = Stdlib. ( @@ )
20
+
21
+ open Compile_common
22
22
23
23
module Wasm = struct
24
24
include Wasm
28
28
29
29
open Wasm.Types
30
30
31
- module G = InstrList
32
- let (^^ ) = G. (^^ ) (* is this how we import a single operator from a module that we otherwise use qualified? *)
33
-
34
31
(* WebAssembly pages are 64kb. *)
35
32
let page_size = Int32. of_int (64 * 1024 )
36
33
let page_size64 = Int64. of_int32 page_size
37
34
let page_size_bits = 16
38
35
39
- (* Our code depends on OCaml int having at least 32 bits *)
40
- let _ = assert (Sys. int_size > = 32 )
41
-
42
36
(* Scalar Tagging Scheme *)
43
37
44
38
(* Rationale:
@@ -169,17 +163,6 @@ See documentation of module BitTagged for more detail.
169
163
let ptr_skew = - 1l
170
164
let ptr_unskew = 1l
171
165
172
- (* Generating function names for functions parametrized by prim types *)
173
- let prim_fun_name p stem = Printf. sprintf " %s<%s>" stem (Type. string_of_prim p)
174
-
175
- (* Helper functions to produce annotated terms (Wasm.AST) *)
176
- let nr x = Wasm.Source. { it = x; at = no_region }
177
-
178
- let todo fn se x = Printf. eprintf " %s: %s" fn (Wasm.Sexpr. to_string 80 se); x
179
-
180
- exception CodegenError of string
181
- let fatal fmt = Printf. ksprintf (fun s -> raise (CodegenError s)) fmt
182
-
183
166
module StaticBytes = struct
184
167
(* A very simple DSL to describe static memory *)
185
168
@@ -379,6 +362,7 @@ The fields fall into the following categories:
379
362
(* Before we can define the environment, we need some auxillary types *)
380
363
381
364
module E = struct
365
+ include Compile_common. E
382
366
383
367
(* Utilities, internal to E *)
384
368
let reg (ref : 'a list ref ) (x : 'a ) : int32 =
@@ -394,15 +378,11 @@ module E = struct
394
378
395
379
396
380
(* The environment type *)
397
- module NameEnv = Env. Make (String )
398
381
module StringEnv = Env. Make (String )
399
382
module LabSet = Set. Make (String )
400
383
module FeatureSet = Set. Make (String )
401
384
402
385
module FunEnv = Env. Make (Int32 )
403
- type local_names = (int32 * string ) list (* For the debug section: Names of locals *)
404
- type func_with_names = func * local_names
405
- type lazy_function = (int32 , func_with_names ) Lib.AllocOnUse .t
406
386
type t = {
407
387
(* Global fields *)
408
388
(* Static *)
@@ -415,16 +395,12 @@ module E = struct
415
395
(* Immutable *)
416
396
417
397
(* Mutable *)
418
- func_types : func_type list ref ;
419
- func_imports : import list ref ;
420
- other_imports : import list ref ;
398
+ imports : Imports .t ;
421
399
exports : export list ref ;
422
- funcs : (func * string * local_names ) Lib.Promise .t list ref ;
423
400
func_ptrs : int32 FunEnv .t ref ;
424
401
end_of_table : int32 ref ;
425
402
globals : (global Lib.Promise .t * string ) list ref ;
426
403
global_names : int32 NameEnv .t ref ;
427
- named_imports : int32 NameEnv .t ref ;
428
404
built_in_funcs : lazy_function NameEnv .t ref ;
429
405
static_strings : int32 StringEnv .t ref ;
430
406
(* Pool for shared static objects. Their lookup needs to be specifically
@@ -474,16 +450,12 @@ module E = struct
474
450
mode;
475
451
rts;
476
452
trap_with;
477
- func_types = ref [] ;
478
- func_imports = ref [] ;
479
- other_imports = ref [] ;
453
+ imports = Imports. empty () ;
480
454
exports = ref [] ;
481
- funcs = ref [] ;
482
455
func_ptrs = ref FunEnv. empty;
483
456
end_of_table = ref 0l ;
484
457
globals = ref [] ;
485
458
global_names = ref NameEnv. empty;
486
- named_imports = ref NameEnv. empty;
487
459
built_in_funcs = ref NameEnv. empty;
488
460
static_strings = ref StringEnv. empty;
489
461
object_pool = ref StringEnv. empty;
@@ -539,9 +511,6 @@ module E = struct
539
511
let get_locals (env : t ) = ! (env.locals)
540
512
let get_local_names (env : t ) : (int32 * string) list = ! (env.local_names)
541
513
542
- let _add_other_import (env : t ) m =
543
- ignore (reg env.other_imports m)
544
-
545
514
let add_export (env : t ) e =
546
515
ignore (reg env.exports e)
547
516
@@ -594,17 +563,9 @@ module E = struct
594
563
595
564
let get_globals (env : t ) = List. map (fun (g ,n ) -> Lib.Promise. value g) ! (env.globals)
596
565
597
- let reserve_fun (env : t ) name =
598
- let (j, fill) = reserve_promise env.funcs name in
599
- let n = Int32. of_int (List. length ! (env.func_imports)) in
600
- let fi = Int32. add j n in
601
- let fill_ (f , local_names ) = fill (f, name, local_names) in
602
- (fi, fill_)
566
+ let reserve_fun (env : t ) = Imports. reserve_fun env.imports
603
567
604
- let add_fun (env : t ) name (f , local_names ) =
605
- let (fi, fill) = reserve_fun env name in
606
- fill (f, local_names);
607
- fi
568
+ let add_fun (env : t ) = Imports. add_fun env.imports
608
569
609
570
let make_lazy_function env name : lazy_function =
610
571
Lib.AllocOnUse. make (fun () -> reserve_fun env name)
@@ -625,48 +586,20 @@ module E = struct
625
586
626
587
let get_return_arity (env : t ) = env.return_arity
627
588
628
- let get_func_imports (env : t ) = ! (env.func_imports)
629
- let get_other_imports (env : t ) = ! (env.other_imports)
630
589
let get_exports (env : t ) = ! (env.exports)
631
- let get_funcs (env : t ) = List. map Lib.Promise. value ! (env.funcs)
632
-
633
- let func_type (env : t ) ty =
634
- let rec go i = function
635
- | [] -> env.func_types := ! (env.func_types) @ [ ty ]; Int32. of_int i
636
- | ty' ::tys when ty = ty' -> Int32. of_int i
637
- | _ :: tys -> go (i+ 1 ) tys
638
- in
639
- go 0 ! (env.func_types)
640
-
641
- let get_types (env : t ) = ! (env.func_types)
642
-
643
- let add_func_import (env : t ) modname funcname arg_tys ret_tys =
644
- if ! (env.funcs) <> [] then
645
- raise (CodegenError " Add all imports before all functions!" );
646
-
647
- let i = {
648
- module_name = Lib.Utf8. decode modname;
649
- item_name = Lib.Utf8. decode funcname;
650
- idesc = nr (FuncImport (nr (func_type env (FuncType (arg_tys, ret_tys)))))
651
- } in
652
- let fi = reg env.func_imports (nr i) in
653
- let name = modname ^ " ." ^ funcname in
654
- assert (not (NameEnv. mem name ! (env.named_imports)));
655
- env.named_imports := NameEnv. add name fi ! (env.named_imports)
656
-
657
- let call_import (env : t ) modname funcname =
658
- let name = modname ^ " ." ^ funcname in
659
- match NameEnv. find_opt name ! (env.named_imports) with
660
- | Some fi -> G. i (Call (nr fi))
661
- | _ ->
662
- raise (Invalid_argument (Printf. sprintf " Function import not declared: %s\n " name))
590
+ let get_funcs (env : t ) = Imports. get_funcs env.imports
663
591
664
- let reuse_import (env : t ) modname funcname =
665
- let name = modname ^ " ." ^ funcname in
666
- match NameEnv. find_opt name ! (env.named_imports) with
667
- | Some fi -> fi
668
- | _ ->
669
- raise (Invalid_argument (Printf. sprintf " Function import not declared: %s\n " name))
592
+ let func_type (env : t ) = Imports. func_type env.imports
593
+
594
+ let get_types (env : t ) = Imports. get_types env.imports
595
+
596
+ let add_func_import (env : t ) = Imports. add_func_import env.imports
597
+
598
+ let call_import (env : t ) = Imports. call_import env.imports
599
+
600
+ let reuse_import (env : t ) = Imports. reuse_import env.imports
601
+
602
+ let finalize_func_imports (env : t ) = Imports. finalize_func_imports env.imports
670
603
671
604
let get_rts (env : t ) = env.rts
672
605
@@ -13376,11 +13309,7 @@ and conclude_module env set_serialization_globals start_fi_o =
13376
13309
13377
13310
IC. default_exports env;
13378
13311
13379
- let func_imports = E. get_func_imports env in
13380
- let ni = List. length func_imports in
13381
- let ni' = Int32. of_int ni in
13382
-
13383
- let other_imports = E. get_other_imports env in
13312
+ let func_imports, ni, remapping = E. finalize_func_imports env in
13384
13313
13385
13314
let memories = E. get_memories env in
13386
13315
@@ -13402,15 +13331,15 @@ and conclude_module env set_serialization_globals start_fi_o =
13402
13331
13403
13332
let table_sz = E. get_end_of_table env in
13404
13333
13405
- let module_ = {
13334
+ let module_ = rename_funcs remapping {
13406
13335
types = List. map nr (E. get_types env);
13407
13336
funcs = List. map (fun (f ,_ ,_ ) -> f) funcs;
13408
13337
tables = [ nr { ttype = TableType ({min = table_sz; max = Some table_sz}, FuncRefType ) } ];
13409
13338
elems;
13410
13339
start = Some (nr rts_start_fi);
13411
13340
globals = E. get_globals env;
13412
13341
memories;
13413
- imports = func_imports @ other_imports ;
13342
+ imports = func_imports;
13414
13343
exports = E. get_exports env;
13415
13344
datas
13416
13345
} in
@@ -13419,10 +13348,10 @@ and conclude_module env set_serialization_globals start_fi_o =
13419
13348
let open Wasm_exts.CustomModule in
13420
13349
{ module_;
13421
13350
dylink0 = [] ;
13422
- name = { empty_name_section with function_names =
13423
- List. mapi (fun i (f ,n ,_ ) -> Int32. (add ni' (of_int i), n)) funcs;
13424
- locals_names =
13425
- List. mapi ( fun i ( f , _ , ln ) -> Int32. (add ni' (of_int i), ln)) funcs; };
13351
+ name = { empty_name_section with
13352
+ function_names = List. mapi (fun i (f ,n ,_ ) -> Int32. (add ni (of_int i), n)) funcs;
13353
+ locals_names = List. mapi ( fun i ( f , _ , ln ) -> Int32. (add ni (of_int i), ln)) funcs;
13354
+ };
13426
13355
motoko = {
13427
13356
labels = E. get_labs env;
13428
13357
stable_types = ! (env.E. stable_types);
0 commit comments