Skip to content

Commit 7be82c2

Browse files
committed
Compiler: less aliases
1 parent 5f92726 commit 7be82c2

File tree

4 files changed

+108
-77
lines changed

4 files changed

+108
-77
lines changed

compiler/lib/annot_lexer.mll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ rule main = parse
3939
| ['a'-'z''A'-'Z''$''_']['a'-'z''A'-'Z''$''_''-''0'-'9']* {
4040
let x = Lexing.lexeme lexbuf in
4141
TIdent x}
42+
| '%' ['a'-'z''A'-'Z''$''_']['a'-'z''A'-'Z''$''_''-''0'-'9']* {
43+
let x = Lexing.lexeme lexbuf in
44+
TIdent_percent x}
4245
| ['0'-'9']+ ('.' (['0'-'9']+)) * {
4346
let x = Lexing.lexeme lexbuf in
4447
TVNum x}

compiler/lib/annot_parser.mly

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
%token TProvides TRequires TVersion TWeakdef TIf TAlways TAlias
2121
%token TA_Pure TA_Const TA_Mutable TA_Mutator TA_Shallow TA_Object_literal
22-
%token<string> TIdent TVNum
22+
%token<string> TIdent TIdent_percent TVNum
2323
%token TComma TColon EOF EOL LE LT GE GT EQ LPARENT RPARENT
2424
%token<string> TOTHER
2525
%token<string> TDeprecated
@@ -43,6 +43,7 @@ annot:
4343
| TAlways endline { `Always }
4444
| TDeprecated endline { `Deprecated $1 }
4545
| TAlias TColon name=TIdent endline { `Alias (name) }
46+
| TAlias TColon name=TIdent_percent endline { `Alias (name) }
4647
| TIf TColon name=TIdent endline { `If (name) }
4748
| TIf TColon TBang name=TIdent endline { `Ifnot (name) }
4849
prim_annot:

compiler/lib/generate.ml

Lines changed: 98 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,8 @@ let internal_prim name =
10491049
Some f
10501050
with Not_found -> None
10511051

1052-
let register_prim name k f = Hashtbl.add internal_primitives name (k, f)
1052+
let register_prims names k (f : string -> _ list -> _ -> _ -> _) =
1053+
List.iter names ~f:(fun name -> Hashtbl.add internal_primitives name (k, f))
10531054

10541055
let invalid_arity name l ~loc ~expected =
10551056
failwith
@@ -1063,8 +1064,8 @@ let invalid_arity name l ~loc ~expected =
10631064
expected
10641065
(List.length l))
10651066

1066-
let register_un_prim name ?(need_loc = false) k f =
1067-
register_prim name k (fun l ctx loc ->
1067+
let register_un_prims names ?(need_loc = false) k f =
1068+
register_prims names k (fun name l ctx loc ->
10681069
match l with
10691070
| [ x ] ->
10701071
let open Expr_builder in
@@ -1073,8 +1074,10 @@ let register_un_prim name ?(need_loc = false) k f =
10731074
return (f cx loc)
10741075
| l -> invalid_arity name l ~loc ~expected:1)
10751076

1077+
let register_un_prim name k f = register_un_prims [ name ] k f
1078+
10761079
let register_un_prim_ctx name k f =
1077-
register_prim name k (fun l ctx loc ->
1080+
register_prims [ name ] k (fun name l ctx loc ->
10781081
match l with
10791082
| [ x ] ->
10801083
let open Expr_builder in
@@ -1083,8 +1086,8 @@ let register_un_prim_ctx name k f =
10831086
return (f ctx cx loc)
10841087
| _ -> invalid_arity name l ~loc ~expected:1)
10851088

1086-
let register_bin_prim name k f =
1087-
register_prim name k (fun l ctx loc ->
1089+
let register_bin_prims names k f =
1090+
register_prims names k (fun name l ctx loc ->
10881091
match l with
10891092
| [ x; y ] ->
10901093
let open Expr_builder in
@@ -1094,8 +1097,10 @@ let register_bin_prim name k f =
10941097
return (f cx cy loc)
10951098
| _ -> invalid_arity name l ~loc ~expected:2)
10961099

1097-
let register_tern_prim name f =
1098-
register_prim name `Mutator (fun l ctx loc ->
1100+
let register_bin_prim name k f = register_bin_prims [ name ] k f
1101+
1102+
let register_tern_prims names k f =
1103+
register_prims names k (fun name l ctx loc ->
10991104
match l with
11001105
| [ x; y; z ] ->
11011106
let open Expr_builder in
@@ -1106,29 +1111,55 @@ let register_tern_prim name f =
11061111
return (f cx cy cz loc)
11071112
| _ -> invalid_arity name l ~loc ~expected:3)
11081113

1114+
let register_tern_prim name k f = register_tern_prims [ name ] k f
1115+
11091116
let register_un_math_prim name prim =
11101117
let prim = Utf8_string.of_string_exn prim in
11111118
register_un_prim name `Pure (fun cx loc ->
11121119
J.call (J.dot (s_var "Math") prim) [ cx ] loc)
11131120

11141121
let register_bin_math_prim name prim =
11151122
let prim = Utf8_string.of_string_exn prim in
1116-
register_bin_prim name `Pure (fun cx cy loc ->
1123+
register_bin_prims [ name ] `Pure (fun cx cy loc ->
11171124
J.call (J.dot (s_var "Math") prim) [ cx; cy ] loc)
11181125

11191126
let _ =
11201127
register_un_prim_ctx "%caml_format_int_special" `Pure (fun ctx cx loc ->
11211128
let s = J.EBin (J.Plus, str_js_utf8 "", cx) in
11221129
ocaml_string ~ctx ~loc s);
11231130
register_un_prim "%direct_obj_tag" `Mutator (fun cx _loc -> Mlvalue.Block.tag cx);
1124-
register_bin_prim "caml_array_unsafe_get" `Mutable (fun cx cy _ ->
1125-
Mlvalue.Array.field cx cy);
1126-
register_bin_prim "%int_add" `Pure (fun cx cy _ ->
1131+
register_bin_prims
1132+
[ "caml_array_unsafe_get"
1133+
; "caml_array_unsafe_get_float"
1134+
; "caml_floatarray_unsafe_get"
1135+
]
1136+
`Mutable
1137+
(fun cx cy _ -> Mlvalue.Array.field cx cy);
1138+
register_un_prims
1139+
[ "caml_int32_of_int"
1140+
; "caml_int32_to_int"
1141+
; "caml_int32_to_float"
1142+
; "caml_nativeint_of_int"
1143+
; "caml_nativeint_to_int"
1144+
; "caml_nativeint_to_int32"
1145+
; "caml_nativeint_of_int32"
1146+
; "caml_nativeint_to_float"
1147+
; "caml_float_of_int"
1148+
]
1149+
`Pure
1150+
(fun cx _ -> cx);
1151+
register_bin_prims
1152+
[ "%int_add"; "caml_int32_add"; "caml_nativeint_add" ]
1153+
`Pure
1154+
(fun cx cy _ ->
11271155
match cx, cy with
11281156
| J.EBin (J.Minus, cz, J.ENum n), J.ENum m ->
11291157
to_int (J.EBin (J.Plus, cz, J.ENum (J.Num.add m (J.Num.neg n))))
11301158
| _ -> to_int (plus_int cx cy));
1131-
register_bin_prim "%int_sub" `Pure (fun cx cy _ ->
1159+
register_bin_prims
1160+
[ "%int_sub"; "caml_int32_sub"; "caml_nativeint_sub" ]
1161+
`Pure
1162+
(fun cx cy _ ->
11321163
match cx, cy with
11331164
| J.EBin (J.Minus, cz, J.ENum n), J.ENum m ->
11341165
to_int (J.EBin (J.Minus, cz, J.ENum (J.Num.add n m)))
@@ -1139,13 +1170,37 @@ let _ =
11391170
to_int (J.EBin (J.Div, cx, cy)));
11401171
register_bin_prim "%direct_int_mod" `Pure (fun cx cy _ ->
11411172
to_int (J.EBin (J.Mod, cx, cy)));
1142-
register_bin_prim "%int_and" `Pure (fun cx cy _ -> J.EBin (J.Band, cx, cy));
1143-
register_bin_prim "%int_or" `Pure (fun cx cy _ -> J.EBin (J.Bor, cx, cy));
1144-
register_bin_prim "%int_xor" `Pure (fun cx cy _ -> J.EBin (J.Bxor, cx, cy));
1145-
register_bin_prim "%int_lsl" `Pure (fun cx cy _ -> J.EBin (J.Lsl, cx, cy));
1146-
register_bin_prim "%int_lsr" `Pure (fun cx cy _ -> to_int (J.EBin (J.Lsr, cx, cy)));
1147-
register_bin_prim "%int_asr" `Pure (fun cx cy _ -> J.EBin (J.Asr, cx, cy));
1148-
register_un_prim "%int_neg" `Pure (fun cx _ -> to_int (J.EUn (J.Neg, cx)));
1173+
register_bin_prims
1174+
[ "%int_and"; "caml_int32_and"; "caml_nativeint_and" ]
1175+
`Pure
1176+
(fun cx cy _ -> J.EBin (J.Band, cx, cy));
1177+
register_bin_prims
1178+
[ "%int_or"; "caml_int32_or"; "caml_nativeint_or" ]
1179+
`Pure
1180+
(fun cx cy _ -> J.EBin (J.Bor, cx, cy));
1181+
register_bin_prims
1182+
[ "%int_xor"; "caml_int32_xor"; "caml_nativeint_xor" ]
1183+
`Pure
1184+
(fun cx cy _ -> J.EBin (J.Bxor, cx, cy));
1185+
register_bin_prims
1186+
[ "%int_lsl"; "caml_int32_shift_left"; "caml_nativeint_shift_left" ]
1187+
`Pure
1188+
(fun cx cy _ -> J.EBin (J.Lsl, cx, cy));
1189+
register_bin_prims
1190+
[ "%int_lsr"
1191+
; "caml_int32_shift_right_unsigned"
1192+
; "caml_nativeint_shift_right_unsigned"
1193+
]
1194+
`Pure
1195+
(fun cx cy _ -> to_int (J.EBin (J.Lsr, cx, cy)));
1196+
register_bin_prims
1197+
[ "%int_asr"; "caml_int32_shift_right"; "caml_nativeint_shift_right" ]
1198+
`Pure
1199+
(fun cx cy _ -> J.EBin (J.Asr, cx, cy));
1200+
register_un_prims
1201+
[ "%int_neg"; "caml_int32_neg"; "caml_nativeint_neg" ]
1202+
`Pure
1203+
(fun cx _ -> to_int (J.EUn (J.Neg, cx)));
11491204
register_bin_prim "caml_eq_float" `Pure (fun cx cy _ ->
11501205
bool (J.EBin (J.EqEqEq, cx, cy)));
11511206
register_bin_prim "caml_neq_float" `Pure (fun cx cy _ ->
@@ -1160,10 +1215,25 @@ let _ =
11601215
register_bin_prim "caml_div_float" `Pure (fun cx cy _ -> J.EBin (J.Div, cx, cy));
11611216
register_un_prim "caml_neg_float" `Pure (fun cx _ -> J.EUn (J.Neg, cx));
11621217
register_bin_prim "caml_fmod_float" `Pure (fun cx cy _ -> J.EBin (J.Mod, cx, cy));
1163-
register_tern_prim "caml_array_unsafe_set" (fun cx cy cz _ ->
1164-
J.EBin (J.Eq, Mlvalue.Array.field cx cy, cz));
1165-
register_un_prim "caml_alloc_dummy" `Pure (fun _ _ -> J.array []);
1166-
register_un_prim "caml_int_of_float" `Pure (fun cx _loc -> to_int cx);
1218+
register_tern_prims
1219+
[ "caml_array_unsafe_set"
1220+
; "caml_array_unsafe_set_float"
1221+
; "caml_floatarray_unsafe_set"
1222+
; "caml_array_unsafe_set_addr"
1223+
]
1224+
`Mutator
1225+
(fun cx cy cz _ -> J.EBin (J.Eq, Mlvalue.Array.field cx cy, cz));
1226+
register_un_prims [ "caml_alloc_dummy"; "caml_alloc_dummy_float" ] `Pure (fun _ _ ->
1227+
J.array []);
1228+
register_un_prims
1229+
[ "caml_int_of_float"
1230+
; "caml_int32_of_float"
1231+
; "caml_nativeint_of_float"
1232+
; "caml_js_to_int32"
1233+
; "caml_js_to_nativeint"
1234+
]
1235+
`Pure
1236+
(fun cx _loc -> to_int cx);
11671237
register_un_math_prim "caml_abs_float" "abs";
11681238
register_un_math_prim "caml_acos_float" "acos";
11691239
register_un_math_prim "caml_asin_float" "asin";
@@ -1182,7 +1252,7 @@ let _ =
11821252
J.EUn (J.Not, J.EUn (J.Not, cx)));
11831253
register_un_prim "caml_js_to_bool" `Pure (fun cx _ -> to_int cx);
11841254

1185-
register_tern_prim "caml_js_set" (fun cx cy cz _ ->
1255+
register_tern_prim "caml_js_set" `Mutator (fun cx cy cz _ ->
11861256
J.EBin (J.Eq, J.EAccess (cx, ANormal, cy), cz));
11871257
(* [caml_js_get] can have side effect, we declare it as mutator.
11881258
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get *)
@@ -1459,10 +1529,10 @@ let rec translate_expr ctx loc x e level : (_ * J.statement_list) Expr_builder.t
14591529
| _ -> J.EBin (J.Plus, ca, cb)
14601530
in
14611531
return (add ca cb)
1462-
| Extern name, l -> (
1463-
let name = Primitive.resolve name in
1532+
| Extern name_orig, l -> (
1533+
let name = Primitive.resolve name_orig in
14641534
match internal_prim name with
1465-
| Some f -> f l ctx loc
1535+
| Some f -> f name l ctx loc
14661536
| None ->
14671537
if String.starts_with name ~prefix:"%"
14681538
then failwith (Printf.sprintf "Unresolved internal primitive: %s" name);
@@ -2215,50 +2285,6 @@ let f
22152285
p
22162286

22172287
let init () =
2218-
List.iter
2219-
~f:(fun (nm, nm') -> Primitive.alias nm nm')
2220-
[ "caml_int32_neg", "%int_neg"
2221-
; "caml_int32_add", "%int_add"
2222-
; "caml_int32_sub", "%int_sub"
2223-
; "caml_int32_and", "%int_and"
2224-
; "caml_int32_or", "%int_or"
2225-
; "caml_int32_xor", "%int_xor"
2226-
; "caml_int32_shift_left", "%int_lsl"
2227-
; "caml_int32_shift_right", "%int_asr"
2228-
; "caml_int32_shift_right_unsigned", "%int_lsr"
2229-
; "caml_int32_of_int", "%identity"
2230-
; "caml_int32_to_int", "%identity"
2231-
; "caml_int32_of_float", "caml_int_of_float"
2232-
; "caml_int32_to_float", "%identity"
2233-
; "caml_nativeint_neg", "%int_neg"
2234-
; "caml_nativeint_add", "%int_add"
2235-
; "caml_nativeint_sub", "%int_sub"
2236-
; "caml_nativeint_and", "%int_and"
2237-
; "caml_nativeint_or", "%int_or"
2238-
; "caml_nativeint_xor", "%int_xor"
2239-
; "caml_nativeint_shift_left", "%int_lsl"
2240-
; "caml_nativeint_shift_right", "%int_asr"
2241-
; "caml_nativeint_shift_right_unsigned", "%int_lsr"
2242-
; "caml_nativeint_of_int", "%identity"
2243-
; "caml_nativeint_to_int", "%identity"
2244-
; "caml_nativeint_of_float", "caml_int_of_float"
2245-
; "caml_nativeint_to_float", "%identity"
2246-
; "caml_nativeint_of_int32", "%identity"
2247-
; "caml_nativeint_to_int32", "%identity"
2248-
; "caml_float_of_int", "%identity"
2249-
; "caml_array_unsafe_get_float", "caml_array_unsafe_get"
2250-
; "caml_floatarray_unsafe_get", "caml_array_unsafe_get"
2251-
; "caml_array_unsafe_set_float", "caml_array_unsafe_set"
2252-
; "caml_array_unsafe_set_addr", "caml_array_unsafe_set"
2253-
; "caml_floatarray_unsafe_set", "caml_array_unsafe_set"
2254-
; "caml_alloc_dummy_float", "caml_alloc_dummy"
2255-
; "caml_js_from_float", "%identity"
2256-
; "caml_js_to_float", "%identity"
2257-
; "caml_js_from_int32", "%identity"
2258-
; "caml_js_from_nativeint", "%identity"
2259-
; "caml_js_to_int32", "caml_int_of_float"
2260-
; "caml_js_to_nativeint", "caml_int_of_float"
2261-
];
22622288
Hashtbl.iter
22632289
(fun name (k, _) -> Primitive.register name k None None)
22642290
internal_primitives

compiler/lib/parse_bytecode.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,10 +1773,11 @@ and compile infos pc state (instrs : instr list) =
17731773
| "caml_process_pending_actions_with_root", _ -> true
17741774
| "caml_make_array", `JavaScript -> true
17751775
| "caml_array_of_uniform_array", `JavaScript -> true
1776-
| _, `JavaScript ->
1777-
(* Temporary until we remove aliases to %identity *)
1778-
String.equal (Primitive.resolve prim) "%identity"
1779-
| _, `Wasm -> false
1776+
| "caml_js_from_float", `JavaScript -> true
1777+
| "caml_js_from_int32", `JavaScript -> true
1778+
| "caml_js_from_nativeint", `JavaScript -> true
1779+
| "caml_js_to_float", `JavaScript -> true
1780+
| _ -> false
17801781
in
17811782
if noop
17821783
then (* This is a no-op *)

0 commit comments

Comments
 (0)