Skip to content

Commit 8899df1

Browse files
committed
Update
1 parent 04e3656 commit 8899df1

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/lib/lib.ml

+31
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,37 @@ struct
264264
then (take n xs, drop n xs)
265265
else (xs, [])
266266

267+
let rec init xs =
268+
match xs with
269+
| [] -> failwith "init"
270+
| [_] -> []
271+
| x :: xs -> x :: init xs
272+
273+
let remove idx0 xs0 =
274+
if idx0 < 0 then
275+
failwith "remove -- negative index"
276+
else
277+
let rec go idx xs =
278+
match (idx, xs) with
279+
| (0, _ :: xs) -> xs
280+
| (n, x :: xs) -> x :: go (n - 1) xs
281+
| (_, []) -> failwith "remove -- index out of bounds"
282+
in
283+
go idx0 xs0
284+
285+
let insert idx0 a xs0 =
286+
Printf.printf "insert at %d, list length = %d\n" idx0 (List.length xs0);
287+
if idx0 < 0 then
288+
failwith "insert -- negative index"
289+
else
290+
let rec go idx xs =
291+
match (idx, xs) with
292+
| (0, xs) -> a :: xs
293+
| (n, x :: xs) -> x :: go (n - 1) xs
294+
| (n, []) -> [a] (* failwith "insert -- index out of bounds" TODO *)
295+
in
296+
go idx0 xs0
297+
267298
let hd_opt = function
268299
| x :: _ -> Some x
269300
| _ -> None

src/lib/lib.mli

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ sig
1818
val take : int -> 'a list -> 'a list (* raises Failure *)
1919
val drop : int -> 'a list -> 'a list (* raises Failure *)
2020
val split_at : int -> 'a list -> ('a list * 'a list)
21+
val init : 'a list -> 'a list (* raises Failure *)
22+
val remove : int -> 'a list -> 'a list (* raises Failure *)
23+
val insert : int -> 'a -> 'a list -> 'a list (* raises Failure *)
2124

2225
val hd_opt : 'a list -> 'a option
2326
val last : 'a list -> 'a (* raises Failure *)

src/linking/linkModule.ml

+39-9
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ let rec find_fun_export (name : name) (exports : export list) : var option =
586586
else
587587
find_fun_export name exports
588588

589-
let rec find_idx p lst : int option =
589+
let find_idx p lst : int option =
590590
let rec go idx = function
591591
| [] -> None
592592
| x :: xs -> if p x then Some idx else go (idx + 1) xs
@@ -830,24 +830,54 @@ let link (em1 : extended_module) libname (em2 : extended_module) =
830830
for the imported functions *)
831831
let dm2 =
832832
Hashtbl.fold (fun import_idx (global_idx, func_idx) dm2 ->
833-
(* - Find the function in the table (add it if it's not in the table)
834-
- Remove the import
835-
- Add a global at `global_idx` as i32 of the table index *)
833+
834+
Printf.printf "import_idx=%d, global_idx=%d, func_idx=%d\n"
835+
(Int32.to_int import_idx)
836+
(Int32.to_int global_idx)
837+
(Int32.to_int func_idx);
838+
839+
(* Find the function in the table (add it if it's not in the table) *)
836840
let (tbl_idx, dm2) = match find_table_idx func_idx dm2.elems with
837841
| Some tbl_idx -> (tbl_idx, dm2)
838842
| None ->
839-
(* Function not in elem section, add it *)
843+
(* Function not in elem section, add it to the last elem section in the
844+
RTS module. *)
840845
let last_elem_section = (List.nth dm2.elems (List.length dm2.elems - 1)).it in
846+
(* 'offset' field of the elem section gives us the index of the first
847+
element in the section. *)
841848
let offset = get_i32_const last_elem_section.offset in
849+
(* Elements in the section *)
842850
let var_list: var list = last_elem_section.init in
843-
851+
(* Table index of the function we're adding to the section *)
844852
let tbl_idx = Int32.(add offset (of_int (List.length var_list))) in
845-
(* TODO: Append last elem section with the function index *)
846-
let dm2 = { dm2 with elems = List.append (Lib.List.take (List.length var_list - 1) dm2.elems) [last_elem_section @@ no_region] } in
853+
(* Append the function to the last elem section *)
854+
let last_elem_section =
855+
{ last_elem_section with
856+
init = List.append last_elem_section.init [func_idx @@ no_region] }
857+
in
858+
(* Update the module *)
859+
let final_elem_section =
860+
List.append (Lib.List.init dm2.elems) [last_elem_section @@ no_region]
861+
in
862+
let dm2 = { dm2 with elems = final_elem_section } in
847863

848864
(tbl_idx, dm2)
849865
in
850-
(* TODO *)
866+
867+
(* Turn import into global (remove import, add global) *)
868+
let global =
869+
{ gtype = Wasm.Types.GlobalType (Wasm.Types.I32Type, Wasm.Types.Mutable);
870+
value = [Const (Wasm.Values.I32 tbl_idx @@ no_region) @@ no_region] @@ no_region }
871+
in
872+
let dm2 =
873+
{ dm2 with
874+
(* FIXME: Lots of other imports removed already so the index here is
875+
not right. Maybe remove this with others above? *)
876+
(* imports = Lib.List.remove (Int32.to_int import_idx) dm2.imports; *)
877+
globals = Lib.List.insert (Int32.to_int global_idx) (global @@ no_region) dm2.globals;
878+
}
879+
in
880+
851881
dm2
852882
) got_func_imports dm2
853883
in

0 commit comments

Comments
 (0)