@@ -365,13 +365,6 @@ let one = J.ENum (J.Num.of_targetint Targetint.one)
365365
366366let zero = J. ENum (J.Num. of_targetint Targetint. zero)
367367
368- let plus_int x y =
369- match x, y with
370- | J. ENum y , x when J.Num. is_zero y -> x
371- | x , J. ENum y when J.Num. is_zero y -> x
372- | J. ENum x , J. ENum y -> J. ENum (J.Num. add x y)
373- | x , y -> J. EBin (J. Plus , x, y)
374-
375368let bool e = J. ECond (e, one, zero)
376369
377370(* ***)
@@ -1082,16 +1075,6 @@ let register_un_prims names ?(need_loc = false) k f =
10821075
10831076let register_un_prim name k f = register_un_prims [ name ] k f
10841077
1085- let register_un_prim_ctx name k f =
1086- register_prims [ name ] k (fun name l ctx loc ->
1087- match l with
1088- | [ x ] ->
1089- let open Expr_builder in
1090- let * cx = access' ~ctx x in
1091- let * () = info (kind k) in
1092- return (f ctx cx loc)
1093- | _ -> invalid_arity name l ~loc ~expected: 1 )
1094-
10951078let register_bin_prims names k f =
10961079 register_prims names k (fun name l ctx loc ->
10971080 match l with
@@ -1119,28 +1102,7 @@ let register_tern_prims names k f =
11191102
11201103let register_tern_prim name k f = register_tern_prims [ name ] k f
11211104
1122- let register_un_math_prim name prim =
1123- let prim = Utf8_string. of_string_exn prim in
1124- register_un_prim name `Pure (fun cx loc ->
1125- J. call (J. dot (s_var " Math" ) prim) [ cx ] loc)
1126-
1127- let register_bin_math_prim name prim =
1128- let prim = Utf8_string. of_string_exn prim in
1129- register_bin_prims [ name ] `Pure (fun cx cy loc ->
1130- J. call (J. dot (s_var " Math" ) prim) [ cx; cy ] loc)
1131-
11321105let _ =
1133- register_un_prim_ctx " %caml_format_int_special" `Pure (fun ctx cx loc ->
1134- let s = J. EBin (J. Plus , str_js_utf8 " " , cx) in
1135- ocaml_string ~ctx ~loc s);
1136- register_un_prim " %direct_obj_tag" `Pure (fun cx _loc -> Mlvalue.Block. tag cx);
1137- register_bin_prims
1138- [ " caml_array_unsafe_get"
1139- ; " caml_array_unsafe_get_float"
1140- ; " caml_floatarray_unsafe_get"
1141- ]
1142- `Mutable
1143- (fun cx cy _ -> Mlvalue.Array. field cx cy);
11441106 register_un_prims
11451107 [ " caml_int32_of_int"
11461108 ; " caml_int32_to_int"
@@ -1154,83 +1116,6 @@ let _ =
11541116 ]
11551117 `Pure
11561118 (fun cx _ -> cx);
1157- register_bin_prims
1158- [ " %int_add" ; " caml_int32_add" ; " caml_nativeint_add" ]
1159- `Pure
1160- (fun cx cy _ ->
1161- match cx, cy with
1162- | J. EBin (J. Minus, cz , J. ENum n ), J. ENum m ->
1163- to_int (J. EBin (J. Plus , cz, J. ENum (J.Num. add m (J.Num. neg n))))
1164- | _ -> to_int (plus_int cx cy));
1165- register_bin_prims
1166- [ " %int_sub" ; " caml_int32_sub" ; " caml_nativeint_sub" ]
1167- `Pure
1168- (fun cx cy _ ->
1169- match cx, cy with
1170- | J. EBin (J. Minus, cz , J. ENum n ), J. ENum m ->
1171- to_int (J. EBin (J. Minus , cz, J. ENum (J.Num. add n m)))
1172- | _ -> to_int (J. EBin (J. Minus , cx, cy)));
1173- register_bin_prim " %direct_int_mul" `Pure (fun cx cy _ ->
1174- to_int (J. EBin (J. Mul , cx, cy)));
1175- register_bin_prim " %direct_int_div" `Pure (fun cx cy _ ->
1176- to_int (J. EBin (J. Div , cx, cy)));
1177- register_bin_prim " %direct_int_mod" `Pure (fun cx cy _ ->
1178- to_int (J. EBin (J. Mod , cx, cy)));
1179- register_bin_prims
1180- [ " %int_and" ; " caml_int32_and" ; " caml_nativeint_and" ]
1181- `Pure
1182- (fun cx cy _ -> J. EBin (J. Band , cx, cy));
1183- register_bin_prims
1184- [ " %int_or" ; " caml_int32_or" ; " caml_nativeint_or" ]
1185- `Pure
1186- (fun cx cy _ -> J. EBin (J. Bor , cx, cy));
1187- register_bin_prims
1188- [ " %int_xor" ; " caml_int32_xor" ; " caml_nativeint_xor" ]
1189- `Pure
1190- (fun cx cy _ -> J. EBin (J. Bxor , cx, cy));
1191- register_bin_prims
1192- [ " %int_lsl" ; " caml_int32_shift_left" ; " caml_nativeint_shift_left" ]
1193- `Pure
1194- (fun cx cy _ -> J. EBin (J. Lsl , cx, cy));
1195- register_bin_prims
1196- [ " %int_lsr"
1197- ; " caml_int32_shift_right_unsigned"
1198- ; " caml_nativeint_shift_right_unsigned"
1199- ]
1200- `Pure
1201- (fun cx cy _ -> to_int (J. EBin (J. Lsr , cx, cy)));
1202- register_bin_prims
1203- [ " %int_asr" ; " caml_int32_shift_right" ; " caml_nativeint_shift_right" ]
1204- `Pure
1205- (fun cx cy _ -> J. EBin (J. Asr , cx, cy));
1206- register_un_prims
1207- [ " %int_neg" ; " caml_int32_neg" ; " caml_nativeint_neg" ]
1208- `Pure
1209- (fun cx _ -> to_int (J. EUn (J. Neg , cx)));
1210- register_bin_prim " caml_eq_float" `Pure (fun cx cy _ ->
1211- bool (J. EBin (J. EqEqEq , cx, cy)));
1212- register_bin_prim " caml_neq_float" `Pure (fun cx cy _ ->
1213- bool (J. EBin (J. NotEqEq , cx, cy)));
1214- register_bin_prim " caml_ge_float" `Pure (fun cx cy _ -> bool (J. EBin (J. Le , cy, cx)));
1215- register_bin_prim " caml_le_float" `Pure (fun cx cy _ -> bool (J. EBin (J. Le , cx, cy)));
1216- register_bin_prim " caml_gt_float" `Pure (fun cx cy _ -> bool (J. EBin (J. Lt , cy, cx)));
1217- register_bin_prim " caml_lt_float" `Pure (fun cx cy _ -> bool (J. EBin (J. Lt , cx, cy)));
1218- register_bin_prim " caml_add_float" `Pure (fun cx cy _ -> J. EBin (J. Plus , cx, cy));
1219- register_bin_prim " caml_sub_float" `Pure (fun cx cy _ -> J. EBin (J. Minus , cx, cy));
1220- register_bin_prim " caml_mul_float" `Pure (fun cx cy _ -> J. EBin (J. Mul , cx, cy));
1221- register_bin_prim " caml_div_float" `Pure (fun cx cy _ -> J. EBin (J. Div , cx, cy));
1222- register_un_prim " caml_neg_float" `Pure (fun cx _ -> J. EUn (J. Neg , cx));
1223- register_bin_prim " caml_fmod_float" `Pure (fun cx cy _ -> J. EBin (J. Mod , cx, cy));
1224- register_tern_prims
1225- [ " caml_array_unsafe_set"
1226- ; " caml_array_unsafe_set_float"
1227- ; " caml_floatarray_unsafe_set"
1228- ; " caml_array_unsafe_set_addr"
1229- ]
1230- `Mutator
1231- (fun cx cy cz _ -> J. EBin (J. Eq , Mlvalue.Array. field cx cy, cz));
1232- register_un_prims [ " caml_alloc_dummy" ; " caml_alloc_dummy_float" ] `Pure (fun _ _ ->
1233- J. array [] );
12341119 register_un_prims
12351120 [ " caml_int_of_float"
12361121 ; " caml_int32_of_float"
@@ -1240,20 +1125,6 @@ let _ =
12401125 ]
12411126 `Pure
12421127 (fun cx _loc -> to_int cx);
1243- register_un_math_prim " caml_abs_float" " abs" ;
1244- register_un_math_prim " caml_acos_float" " acos" ;
1245- register_un_math_prim " caml_asin_float" " asin" ;
1246- register_un_math_prim " caml_atan_float" " atan" ;
1247- register_bin_math_prim " caml_atan2_float" " atan2" ;
1248- register_un_math_prim " caml_ceil_float" " ceil" ;
1249- register_un_math_prim " caml_cos_float" " cos" ;
1250- register_un_math_prim " caml_exp_float" " exp" ;
1251- register_un_math_prim " caml_floor_float" " floor" ;
1252- register_un_math_prim " caml_log_float" " log" ;
1253- register_bin_math_prim " caml_power_float" " pow" ;
1254- register_un_math_prim " caml_sin_float" " sin" ;
1255- register_un_math_prim " caml_sqrt_float" " sqrt" ;
1256- register_un_math_prim " caml_tan_float" " tan" ;
12571128 register_un_prim " caml_js_from_bool" `Pure (fun cx _ ->
12581129 J. EUn (J. Not , J. EUn (J. Not , cx)));
12591130 register_un_prim " caml_js_to_bool" `Pure (fun cx _ -> to_int cx);
@@ -1318,6 +1189,17 @@ let remove_unused_tail_args ctx exact trampolined args =
13181189 else args
13191190 else args
13201191
1192+ (* var substitution *)
1193+ class subst sub =
1194+ object
1195+ inherit Js_traverse. map as super
1196+
1197+ method expression x =
1198+ match x with
1199+ | EVar v -> ( try sub v with Not_found -> super#expression x)
1200+ | _ -> super#expression x
1201+ end
1202+
13211203let rec translate_expr ctx loc x e level : (_ * J.statement_list) Expr_builder.t =
13221204 let open Expr_builder in
13231205 match e with
@@ -1539,13 +1421,52 @@ let rec translate_expr ctx loc x e level : (_ * J.statement_list) Expr_builder.t
15391421 let name = Primitive. resolve name_orig in
15401422 match internal_prim name with
15411423 | Some f -> f name l ctx loc
1542- | None ->
1424+ | None -> (
15431425 if String. starts_with name ~prefix: " %"
15441426 then failwith (Printf. sprintf " Unresolved internal primitive: %s" name);
1545- let prim = Share. get_prim (runtime_fun ctx) name ctx.Ctx. share in
1546- let * () = info ~need_loc: true (kind (Primitive. kind name)) in
1547- let * args = list_map (fun x -> access' ~ctx x) l in
1548- return (J. call prim args loc))
1427+ match Linker. inline ~name with
1428+ | Some (req, f)
1429+ when Option. is_none ctx.Ctx. exported_runtime || List. is_empty req -> (
1430+ let c = new Js_traverse. rename_variable ~esm: false in
1431+ let f = c#expression f in
1432+ match f with
1433+ | EFun
1434+ ( None
1435+ , ( { async = false ; generator = false }
1436+ , { list = params; rest = None }
1437+ , [ (Return_statement (Some body, _), _) ]
1438+ , _loc ) )
1439+ when List. length params = List. length l ->
1440+ let * l = list_map (fun x -> access' ~ctx x) l in
1441+ let params =
1442+ List. map params ~f: (fun (x , _ ) ->
1443+ match x with
1444+ | BindingIdent x -> x
1445+ | BindingPattern _ -> assert false )
1446+ in
1447+ let sub =
1448+ let t = Hashtbl. create (List. length l) in
1449+ List. iter2 params l ~f: (fun p x ->
1450+ let k =
1451+ match p with
1452+ | J. V v -> v
1453+ | _ -> assert false
1454+ in
1455+ Hashtbl. add t k x);
1456+
1457+ fun x ->
1458+ match x with
1459+ | J. S _ -> J. EVar x
1460+ | J. V x -> Hashtbl. find t x
1461+ in
1462+ let r = new subst sub in
1463+ return (r#expression body)
1464+ | _ -> assert false )
1465+ | None | Some _ ->
1466+ let prim = Share. get_prim (runtime_fun ctx) name ctx.Ctx. share in
1467+ let * () = info ~need_loc: true (kind (Primitive. kind name)) in
1468+ let * args = list_map (fun x -> access' ~ctx x) l in
1469+ return (J. call prim args loc)))
15491470 | Not , [ x ] ->
15501471 let * cx = access' ~ctx x in
15511472 return (J. EBin (J. Minus , one, cx))
@@ -2289,7 +2210,7 @@ let f
22892210 if times () then Format. eprintf " code gen.: %a@." Timer. print t';
22902211 p
22912212
2292- let init () =
2213+ let reset () =
22932214 Hashtbl. iter
22942215 (fun name (k , _ ) -> Primitive. register name k None None )
22952216 internal_primitives
0 commit comments