Skip to content

Commit 7a2165e

Browse files
committed
Allow primitives to be poly-moded (#43)
1 parent 2af3f55 commit 7a2165e

26 files changed

+556
-397
lines changed

asmcomp/cmm_helpers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ let default_prim name =
13731373

13741374

13751375
let int64_native_prim name arity ~alloc =
1376-
let u64 = Primitive.Unboxed_integer Primitive.Pint64 in
1376+
let u64 = Primitive.(Prim_global, Unboxed_integer Pint64) in
13771377
let rec make_args = function 0 -> [] | n -> u64 :: make_args (n - 1) in
13781378
Primitive.make ~name ~native_name:(name ^ "_native")
13791379
~alloc

asmcomp/cmmgen.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,19 +781,19 @@ and transl_ccall env prim args dbg =
781781
(List.map (fun _ -> XInt) args, List.map (transl env) args)
782782
| _, [] ->
783783
assert false
784-
| native_repr :: native_repr_args, arg :: args ->
784+
| (_, native_repr) :: native_repr_args, arg :: args ->
785785
let (ty1, arg') = transl_arg native_repr arg in
786786
let (tys, args') = transl_args native_repr_args args in
787787
(ty1 :: tys, arg' :: args')
788788
in
789789
let typ_res, wrap_result =
790790
match prim.prim_native_repr_res with
791-
| Same_as_ocaml_repr -> (typ_val, fun x -> x)
792-
| Unboxed_float -> (typ_float, box_float dbg)
793-
| Unboxed_integer Pint64 when size_int = 4 ->
791+
| _, Same_as_ocaml_repr -> (typ_val, fun x -> x)
792+
| _, Unboxed_float -> (typ_float, box_float dbg)
793+
| _, Unboxed_integer Pint64 when size_int = 4 ->
794794
([|Int; Int|], box_int dbg Pint64)
795-
| Unboxed_integer bi -> (typ_int, box_int dbg bi)
796-
| Untagged_int -> (typ_int, (fun i -> tag_int i dbg))
795+
| _, Unboxed_integer bi -> (typ_int, box_int dbg bi)
796+
| _, Untagged_int -> (typ_int, (fun i -> tag_int i dbg))
797797
in
798798
let typ_args, args = transl_args prim.prim_native_repr_args args in
799799
wrap_result

lambda/translcore.ml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ let rec push_defaults loc bindings cases partial =
165165
Texp_match
166166
({exp with exp_type = pat.pat_type; exp_env = env; exp_desc =
167167
Texp_ident
168-
(Path.Pident param, mknoloc (Longident.Lident name), desc)},
168+
(Path.Pident param, mknoloc (Longident.Lident name),
169+
desc, Id_value)},
169170
cases, partial) }
170171
in
171172
push_defaults loc bindings
@@ -231,13 +232,14 @@ let rec iter_exn_names f pat =
231232
iter_exn_names f p
232233
| _ -> ()
233234

234-
let transl_ident loc env ty path desc =
235-
match desc.val_kind with
236-
| Val_prim p ->
237-
Translprim.transl_primitive loc p env ty (Some path)
238-
| Val_anc _ ->
235+
let transl_ident loc env ty path desc kind =
236+
match desc.val_kind, kind with
237+
| Val_prim p, Id_prim pmode ->
238+
let poly_mode = transl_alloc_mode pmode in
239+
Translprim.transl_primitive loc p env ty ~poly_mode (Some path)
240+
| Val_anc _, Id_value ->
239241
raise(Error(to_location loc, Free_super_var))
240-
| Val_reg | Val_self _ ->
242+
| (Val_reg | Val_self _), Id_value ->
241243
transl_value_path loc env path
242244
| _ -> fatal_error "Translcore.transl_exp: bad Texp_ident"
243245

@@ -273,9 +275,9 @@ and transl_exp1 ~scopes ~in_new_scope e =
273275

274276
and transl_exp0 ~in_new_scope ~scopes e =
275277
match e.exp_desc with
276-
| Texp_ident(path, _, desc) ->
278+
| Texp_ident(path, _, desc, kind) ->
277279
transl_ident (of_location ~scopes e.exp_loc)
278-
e.exp_env e.exp_type path desc
280+
e.exp_env e.exp_type path desc kind
279281
| Texp_constant cst ->
280282
Lconst(Const_base cst)
281283
| Texp_let(rec_flag, pat_expr_list, body) ->
@@ -288,7 +290,8 @@ and transl_exp0 ~in_new_scope ~scopes e =
288290
else enter_anonymous_function ~scopes
289291
in
290292
transl_function ~scopes e param cases partial
291-
| Texp_apply({ exp_desc = Texp_ident(path, _, {val_kind = Val_prim p});
293+
| Texp_apply({ exp_desc = Texp_ident(path, _, {val_kind = Val_prim p},
294+
Id_prim pmode);
292295
exp_type = prim_type } as funct, oargs)
293296
when List.length oargs >= p.prim_arity
294297
&& List.for_all (fun (_, arg) -> not (is_omitted arg)) oargs ->
@@ -298,9 +301,10 @@ and transl_exp0 ~in_new_scope ~scopes e =
298301
in
299302
let args = transl_list ~scopes arg_exps in
300303
let prim_exp = if extra_args = [] then Some e else None in
304+
let pmode = transl_alloc_mode pmode in
301305
let lam =
302306
Translprim.transl_primitive_application
303-
(of_location ~scopes e.exp_loc) p e.exp_env prim_type path
307+
(of_location ~scopes e.exp_loc) p e.exp_env prim_type pmode path
304308
prim_exp args arg_exps
305309
in
306310
if extra_args = [] then lam
@@ -1248,7 +1252,7 @@ and transl_letop ~scopes loc env let_ ands param case partial =
12481252
let right_id = Ident.create_local "right" in
12491253
let op =
12501254
transl_ident (of_location ~scopes and_.bop_op_name.loc) env
1251-
and_.bop_op_type and_.bop_op_path and_.bop_op_val
1255+
and_.bop_op_type and_.bop_op_path and_.bop_op_val Id_value
12521256
in
12531257
let exp = transl_exp ~scopes and_.bop_exp in
12541258
let lam =
@@ -1266,7 +1270,7 @@ and transl_letop ~scopes loc env let_ ands param case partial =
12661270
in
12671271
let op =
12681272
transl_ident (of_location ~scopes let_.bop_op_name.loc) env
1269-
let_.bop_op_type let_.bop_op_path let_.bop_op_val
1273+
let_.bop_op_type let_.bop_op_path let_.bop_op_val Id_value
12701274
in
12711275
let exp = loop (transl_exp ~scopes let_.bop_exp) ands in
12721276
let func =

lambda/translcore.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ val transl_extension_constructor: scopes:scopes ->
4141

4242
val transl_scoped_exp : scopes:scopes -> expression -> lambda
4343

44+
val transl_alloc_mode : Types.alloc_mode -> Lambda.alloc_mode
45+
4446
type error =
4547
Free_super_var
4648
| Unreachable_reached

lambda/translmod.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ let rec apply_coercion loc strict restr arg =
9595
let param = Ident.create_local "funarg" in
9696
let carg = apply_coercion loc Alias cc_arg (Lvar param) in
9797
apply_coercion_result loc strict arg [param, Pgenval] [carg] cc_res
98-
| Tcoerce_primitive { pc_loc = _; pc_desc; pc_env; pc_type; } ->
99-
Translprim.transl_primitive loc pc_desc pc_env pc_type None
98+
| Tcoerce_primitive { pc_loc = _; pc_desc; pc_env; pc_type; pc_poly_mode } ->
99+
let poly_mode = Translcore.transl_alloc_mode pc_poly_mode in
100+
Translprim.transl_primitive loc pc_desc pc_env pc_type ~poly_mode None
100101
| Tcoerce_alias (env, path, cc) ->
101102
let lam = transl_module_path loc env path in
102103
name_lambda strict arg
@@ -209,7 +210,10 @@ let compose_coercions c1 c2 =
209210
let primitive_declarations = ref ([] : Primitive.description list)
210211
let record_primitive = function
211212
| {val_kind=Val_prim p;val_loc} ->
212-
Translprim.check_primitive_arity val_loc p;
213+
let mode = match p.prim_native_repr_res with
214+
| Prim_global, _ | Prim_poly, _ -> Alloc_heap
215+
| Prim_local, _ -> Alloc_local in
216+
Translprim.check_primitive_arity val_loc p mode;
213217
primitive_declarations := p :: !primitive_declarations
214218
| _ -> ()
215219

@@ -569,7 +573,9 @@ and transl_structure ~scopes loc fields cc rootpath final_env = function
569573
Tcoerce_primitive p ->
570574
Translprim.transl_primitive
571575
(of_location ~scopes p.pc_loc)
572-
p.pc_desc p.pc_env p.pc_type None
576+
p.pc_desc p.pc_env p.pc_type
577+
~poly_mode:(Translcore.transl_alloc_mode p.pc_poly_mode)
578+
None
573579
| _ -> apply_coercion loc Strict cc (get_field pos))
574580
pos_cc_list, loc)
575581
and id_pos_list =
@@ -979,8 +985,9 @@ let field_of_str loc str =
979985
let ids = Array.of_list (defined_idents str.str_items) in
980986
fun (pos, cc) ->
981987
match cc with
982-
| Tcoerce_primitive { pc_loc = _; pc_desc; pc_env; pc_type; } ->
983-
Translprim.transl_primitive loc pc_desc pc_env pc_type None
988+
| Tcoerce_primitive { pc_loc = _; pc_desc; pc_env; pc_type; pc_poly_mode } ->
989+
let poly_mode = Translcore.transl_alloc_mode pc_poly_mode in
990+
Translprim.transl_primitive loc pc_desc pc_env pc_type ~poly_mode None
984991
| Tcoerce_alias (env, path, cc) ->
985992
let lam = transl_module_path loc env path in
986993
apply_coercion loc Alias cc lam
@@ -1300,10 +1307,11 @@ let transl_store_structure ~scopes glob map prims aliases str =
13001307
List.fold_right (add_ident may_coerce) idlist subst
13011308

13021309
and store_primitive (pos, prim) cont =
1310+
let poly_mode = Translcore.transl_alloc_mode prim.pc_poly_mode in
13031311
Lsequence(Lprim(Psetfield(pos, Pointer, Root_initialization),
13041312
[Lprim(Pgetglobal glob, [], Loc_unknown);
13051313
Translprim.transl_primitive Loc_unknown
1306-
prim.pc_desc prim.pc_env prim.pc_type None],
1314+
prim.pc_desc prim.pc_env prim.pc_type ~poly_mode None],
13071315
Loc_unknown),
13081316
cont)
13091317

0 commit comments

Comments
 (0)