Skip to content

Commit 1d196f4

Browse files
committed
Support [@unboxed] and [@Untagged] attributes
Adding [@unboxed] (resp [@Untagged]) on a primitive argument means that the argument must passed unboxed (resp untagged) to the external function. Adding [@unboxed] (resp [@Untagged]) on the result means that the external function returns its result unboxed (resp untagged). The unboxing (resp untagging) method is derived from the type. Currently unboxing is suported for: float, int32, int64 and nativeint. Untagging is supported for int. This patch also increases the cm{i,o,a,x,xa} magic numbers as the type Primitive.description is changed. git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@16382 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
1 parent ba6bcdf commit 1d196f4

17 files changed

Lines changed: 324 additions & 131 deletions

File tree

.depend

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ typing/parmatch.cmi : typing/types.cmi typing/typedtree.cmi \
125125
typing/env.cmi parsing/asttypes.cmi
126126
typing/path.cmi : typing/ident.cmi
127127
typing/predef.cmi : typing/types.cmi typing/path.cmi typing/ident.cmi
128-
typing/primitive.cmi :
128+
typing/primitive.cmi : parsing/parsetree.cmi parsing/location.cmi
129129
typing/printtyped.cmi : typing/typedtree.cmi
130130
typing/printtyp.cmi : typing/types.cmi typing/path.cmi \
131131
typing/outcometree.cmi parsing/longident.cmi typing/ident.cmi \
@@ -266,8 +266,10 @@ typing/predef.cmo : typing/types.cmi typing/path.cmi parsing/location.cmi \
266266
typing/ident.cmi typing/btype.cmi parsing/asttypes.cmi typing/predef.cmi
267267
typing/predef.cmx : typing/types.cmx typing/path.cmx parsing/location.cmx \
268268
typing/ident.cmx typing/btype.cmx parsing/asttypes.cmi typing/predef.cmi
269-
typing/primitive.cmo : utils/misc.cmi typing/primitive.cmi
270-
typing/primitive.cmx : utils/misc.cmx typing/primitive.cmi
269+
typing/primitive.cmo : parsing/parsetree.cmi utils/misc.cmi \
270+
parsing/location.cmi typing/primitive.cmi
271+
typing/primitive.cmx : parsing/parsetree.cmi utils/misc.cmx \
272+
parsing/location.cmx typing/primitive.cmi
271273
typing/printtyped.cmo : typing/typedtree.cmi parsing/printast.cmi \
272274
typing/path.cmi utils/misc.cmi parsing/longident.cmi parsing/location.cmi \
273275
typing/ident.cmi parsing/asttypes.cmi typing/printtyped.cmi

asmcomp/cmmgen.ml

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,8 +1061,7 @@ let check_bound unsafe dbg a1 a2 k =
10611061
(* Simplification of some primitives into C calls *)
10621062

10631063
let default_prim name =
1064-
{ prim_name = name; prim_arity = 0 (*ignored*);
1065-
prim_alloc = true; prim_native_name = ""; prim_native_float = false }
1064+
Primitive.simple ~name ~arity:0(*ignored*) ~alloc:true
10661065

10671066
let simplif_primitive_32bits = function
10681067
Pbintofint Pint64 -> Pccall (default_prim "caml_int64_of_int")
@@ -1237,24 +1236,33 @@ type unboxed_number_kind =
12371236
| Boxed_integer of boxed_integer
12381237
| No_result (* expression never returns a result *)
12391238

1239+
let unboxed_number_kind_of_unbox = function
1240+
| Same_as_ocaml_repr -> No_unboxing
1241+
| Unboxed_float -> Boxed_float
1242+
| Unboxed_integer bi -> Boxed_integer bi
1243+
| Untagged_int -> No_unboxing
1244+
12401245
let rec is_unboxed_number e =
12411246
(* Given unboxed_number_kind from two branches of the code, returns the
12421247
resulting unboxed_number_kind *)
12431248
let join k1 e =
12441249
match k1, is_unboxed_number e with
12451250
| Boxed_float, Boxed_float -> Boxed_float
12461251
| Boxed_integer bi1, Boxed_integer bi2 when bi1 = bi2 -> k1
1247-
| No_result, k | k, No_result -> k (* if a branch never returns, it is safe to unbox it *)
1252+
| No_result, k | k, No_result ->
1253+
k (* if a branch never returns, it is safe to unbox it *)
12481254
| _, _ -> No_unboxing
12491255
in
12501256
match e with
12511257
| Uconst(Uconst_ref(_, Uconst_float _)) -> Boxed_float
12521258
| Uconst(Uconst_ref(_, Uconst_int32 _)) -> Boxed_integer Pint32
1253-
| Uconst(Uconst_ref(_, Uconst_int64 _)) when size_int = 8 -> Boxed_integer Pint64
1254-
| Uconst(Uconst_ref(_, Uconst_nativeint _)) -> Boxed_integer Pnativeint
1259+
| Uconst(Uconst_ref(_, Uconst_int64 _)) when size_int = 8 ->
1260+
Boxed_integer Pint64
1261+
| Uconst(Uconst_ref(_, Uconst_nativeint _)) ->
1262+
Boxed_integer Pnativeint
12551263
| Uprim(p, _, _) ->
12561264
begin match simplif_primitive p with
1257-
| Pccall p -> if p.prim_native_float then Boxed_float else No_unboxing
1265+
| Pccall p -> unboxed_number_kind_of_unbox p.prim_native_repr_res
12581266
| Pfloatfield _ -> Boxed_float
12591267
| Pfloatofint -> Boxed_float
12601268
| Pnegfloat -> Boxed_float
@@ -1434,14 +1442,7 @@ let rec transl = function
14341442
| (Pmakeblock(tag, mut), args) ->
14351443
make_alloc tag (List.map transl args)
14361444
| (Pccall prim, args) ->
1437-
if prim.prim_native_float then
1438-
box_float
1439-
(Cop(Cextcall(prim.prim_native_name, typ_float, false, dbg),
1440-
List.map transl_unbox_float args))
1441-
else
1442-
Cop(Cextcall(Primitive.native_name prim, typ_val, prim.prim_alloc,
1443-
dbg),
1444-
List.map transl args)
1445+
transl_ccall prim args dbg
14451446
| (Pmakearray kind, []) ->
14461447
transl_structured_constant (Uconst_block(0, []))
14471448
| (Pmakearray kind, args) ->
@@ -1596,6 +1597,37 @@ let rec transl = function
15961597
| Uassign(id, exp) ->
15971598
return_unit(Cassign(id, transl exp))
15981599

1600+
and transl_ccall prim args dbg =
1601+
let transl_arg native_repr arg =
1602+
match native_repr with
1603+
| Same_as_ocaml_repr -> transl arg
1604+
| Unboxed_float -> transl_unbox_float arg
1605+
| Unboxed_integer bi -> transl_unbox_int bi arg
1606+
| Untagged_int -> untag_int (transl arg)
1607+
in
1608+
let rec transl_args native_repr_args args =
1609+
match native_repr_args, args with
1610+
| [], args ->
1611+
(* We don't require the two lists to be of the same length as
1612+
[default_prim] always sets the arity to [0]. *)
1613+
List.map transl args
1614+
| _, [] -> assert false
1615+
| native_repr :: native_repr_args, arg :: args ->
1616+
transl_arg native_repr arg :: transl_args native_repr_args args
1617+
in
1618+
let typ_res, wrap_result =
1619+
match prim.prim_native_repr_res with
1620+
| Same_as_ocaml_repr -> (typ_val, fun x -> x)
1621+
| Unboxed_float -> (typ_float, box_float)
1622+
| Unboxed_integer Pint64 when size_int = 4 -> ([|Int; Int|], box_int Pint64)
1623+
| Unboxed_integer bi -> (typ_int, box_int bi)
1624+
| Untagged_int -> (typ_int, tag_int)
1625+
in
1626+
let args = transl_args prim.prim_native_repr_args args in
1627+
wrap_result
1628+
(Cop(Cextcall(Primitive.native_name prim,
1629+
typ_res, prim.prim_alloc, dbg), args))
1630+
15991631
and transl_prim_1 p arg dbg =
16001632
match p with
16011633
(* Generic operations *)

asmcomp/i386/proc.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ let extcall_use_push = true
150150
let loc_external_arguments arg =
151151
fatal_error "Proc.loc_external_arguments"
152152
let loc_external_results res =
153-
let (loc, ofs) = calling_conventions 0 0 100 100 not_supported res in loc
153+
match res with
154+
| [|{typ=Int};{typ=Int}|] -> [|eax; edx|]
155+
| _ ->
156+
let (loc, ofs) = calling_conventions 0 0 100 100 not_supported res in loc
154157

155158
let loc_exn_bucket = eax
156159

boot/ocamlc

4.58 KB
Binary file not shown.

boot/ocamldep

-16 Bytes
Binary file not shown.

boot/ocamllex

-26 Bytes
Binary file not shown.

bytecomp/lambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ and comparison =
132132
and array_kind =
133133
Pgenarray | Paddrarray | Pintarray | Pfloatarray
134134

135-
and boxed_integer =
135+
and boxed_integer = Primitive.boxed_integer =
136136
Pnativeint | Pint32 | Pint64
137137

138138
and bigarray_kind =

bytecomp/lambda.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ and comparison =
132132
and array_kind =
133133
Pgenarray | Paddrarray | Pintarray | Pfloatarray
134134

135-
and boxed_integer =
135+
and boxed_integer = Primitive.boxed_integer =
136136
Pnativeint | Pint32 | Pint64
137137

138138
and bigarray_kind =

bytecomp/matching.ml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
open Misc
1616
open Asttypes
17-
open Primitive
1817
open Types
1918
open Typedtree
2019
open Lambda
@@ -1475,10 +1474,7 @@ let matcher_lazy p rem = match p.pat_desc with
14751474
*)
14761475

14771476
let prim_obj_tag =
1478-
{prim_name = "caml_obj_tag";
1479-
prim_arity = 1; prim_alloc = false;
1480-
prim_native_name = "";
1481-
prim_native_float = false}
1477+
Primitive.simple ~name:"caml_obj_tag" ~arity:1 ~alloc:false
14821478

14831479
let get_mod_field modname field =
14841480
lazy (
@@ -1707,14 +1703,16 @@ let divide_array kind ctx pm =
17071703
let strings_test_threshold = 8
17081704

17091705
let prim_string_notequal =
1710-
Pccall{prim_name = "caml_string_notequal";
1711-
prim_arity = 2; prim_alloc = false;
1712-
prim_native_name = ""; prim_native_float = false}
1706+
Pccall(Primitive.simple
1707+
~name:"caml_string_notequal"
1708+
~arity:2
1709+
~alloc:false)
17131710

17141711
let prim_string_compare =
1715-
Pccall{prim_name = "caml_string_compare";
1716-
prim_arity = 2; prim_alloc = false;
1717-
prim_native_name = ""; prim_native_float = false}
1712+
Pccall(Primitive.simple
1713+
~name:"caml_string_compare"
1714+
~arity:2
1715+
~alloc:false)
17181716

17191717
let bind_sw arg k = match arg with
17201718
| Lvar _ -> k arg
@@ -2287,7 +2285,7 @@ let mk_failaction_pos partial seen ctx defs =
22872285
| _ -> scan_def ((List.map fst now,idef)::env) later rem in
22882286

22892287
let fail_pats = complete_pats_constrs seen in
2290-
if List.length fail_pats < 32 then begin
2288+
if List.length fail_pats < 32 then begin
22912289
let fail,jmps =
22922290
scan_def
22932291
[]
@@ -2305,7 +2303,7 @@ let mk_failaction_pos partial seen ctx defs =
23052303
let fail,jumps = mk_failaction_neg partial ctx defs in
23062304
if dbg then
23072305
eprintf "FAIL: %s\n"
2308-
(match fail with
2306+
(match fail with
23092307
| None -> "<none>"
23102308
| Some lam -> string_of_lam lam) ;
23112309
fail,[],jumps

bytecomp/translcore.ml

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,99 +42,78 @@ let transl_object =
4242

4343
let comparisons_table = create_hashtable 11 [
4444
"%equal",
45-
(Pccall{prim_name = "caml_equal"; prim_arity = 2; prim_alloc = true;
46-
prim_native_name = ""; prim_native_float = false},
45+
(Pccall(Primitive.simple ~name:"caml_equal" ~arity:2 ~alloc:true),
4746
Pintcomp Ceq,
4847
Pfloatcomp Ceq,
49-
Pccall{prim_name = "caml_string_equal"; prim_arity = 2;
50-
prim_alloc = false;
51-
prim_native_name = ""; prim_native_float = false},
48+
Pccall(Primitive.simple ~name:"caml_string_equal" ~arity:2
49+
~alloc:false),
5250
Pbintcomp(Pnativeint, Ceq),
5351
Pbintcomp(Pint32, Ceq),
5452
Pbintcomp(Pint64, Ceq),
5553
true);
5654
"%notequal",
57-
(Pccall{prim_name = "caml_notequal"; prim_arity = 2; prim_alloc = true;
58-
prim_native_name = ""; prim_native_float = false},
55+
(Pccall(Primitive.simple ~name:"caml_notequal" ~arity:2 ~alloc:true),
5956
Pintcomp Cneq,
6057
Pfloatcomp Cneq,
61-
Pccall{prim_name = "caml_string_notequal"; prim_arity = 2;
62-
prim_alloc = false; prim_native_name = "";
63-
prim_native_float = false},
58+
Pccall(Primitive.simple ~name:"caml_string_notequal" ~arity:2
59+
~alloc:false),
6460
Pbintcomp(Pnativeint, Cneq),
6561
Pbintcomp(Pint32, Cneq),
6662
Pbintcomp(Pint64, Cneq),
6763
true);
6864
"%lessthan",
69-
(Pccall{prim_name = "caml_lessthan"; prim_arity = 2; prim_alloc = true;
70-
prim_native_name = ""; prim_native_float = false},
65+
(Pccall(Primitive.simple ~name:"caml_lessthan" ~arity:2 ~alloc:true),
7166
Pintcomp Clt,
7267
Pfloatcomp Clt,
73-
Pccall{prim_name = "caml_string_lessthan"; prim_arity = 2;
74-
prim_alloc = false; prim_native_name = "";
75-
prim_native_float = false},
68+
Pccall(Primitive.simple ~name:"caml_string_lessthan" ~arity:2
69+
~alloc:false),
7670
Pbintcomp(Pnativeint, Clt),
7771
Pbintcomp(Pint32, Clt),
7872
Pbintcomp(Pint64, Clt),
7973
false);
8074
"%greaterthan",
81-
(Pccall{prim_name = "caml_greaterthan"; prim_arity = 2; prim_alloc = true;
82-
prim_native_name = ""; prim_native_float = false},
75+
(Pccall(Primitive.simple ~name:"caml_greaterthan" ~arity:2 ~alloc:true),
8376
Pintcomp Cgt,
8477
Pfloatcomp Cgt,
85-
Pccall{prim_name = "caml_string_greaterthan"; prim_arity = 2;
86-
prim_alloc = false; prim_native_name = "";
87-
prim_native_float = false},
78+
Pccall(Primitive.simple ~name:"caml_string_greaterthan" ~arity:2
79+
~alloc: false),
8880
Pbintcomp(Pnativeint, Cgt),
8981
Pbintcomp(Pint32, Cgt),
9082
Pbintcomp(Pint64, Cgt),
9183
false);
9284
"%lessequal",
93-
(Pccall{prim_name = "caml_lessequal"; prim_arity = 2; prim_alloc = true;
94-
prim_native_name = ""; prim_native_float = false},
85+
(Pccall(Primitive.simple ~name:"caml_lessequal" ~arity:2 ~alloc:true),
9586
Pintcomp Cle,
9687
Pfloatcomp Cle,
97-
Pccall{prim_name = "caml_string_lessequal"; prim_arity = 2;
98-
prim_alloc = false; prim_native_name = "";
99-
prim_native_float = false},
88+
Pccall(Primitive.simple ~name:"caml_string_lessequal" ~arity:2
89+
~alloc:false),
10090
Pbintcomp(Pnativeint, Cle),
10191
Pbintcomp(Pint32, Cle),
10292
Pbintcomp(Pint64, Cle),
10393
false);
10494
"%greaterequal",
105-
(Pccall{prim_name = "caml_greaterequal"; prim_arity = 2;
106-
prim_alloc = true;
107-
prim_native_name = ""; prim_native_float = false},
95+
(Pccall(Primitive.simple ~name:"caml_greaterequal" ~arity:2 ~alloc:true),
10896
Pintcomp Cge,
10997
Pfloatcomp Cge,
110-
Pccall{prim_name = "caml_string_greaterequal"; prim_arity = 2;
111-
prim_alloc = false; prim_native_name = "";
112-
prim_native_float = false},
98+
Pccall(Primitive.simple ~name:"caml_string_greaterequal" ~arity:2
99+
~alloc:false),
113100
Pbintcomp(Pnativeint, Cge),
114101
Pbintcomp(Pint32, Cge),
115102
Pbintcomp(Pint64, Cge),
116103
false);
117104
"%compare",
118-
(Pccall{prim_name = "caml_compare"; prim_arity = 2; prim_alloc = true;
119-
prim_native_name = ""; prim_native_float = false},
120-
Pccall{prim_name = "caml_int_compare"; prim_arity = 2;
121-
prim_alloc = false; prim_native_name = "";
122-
prim_native_float = false},
123-
Pccall{prim_name = "caml_float_compare"; prim_arity = 2;
124-
prim_alloc = false; prim_native_name = "";
125-
prim_native_float = false},
126-
Pccall{prim_name = "caml_string_compare"; prim_arity = 2;
127-
prim_alloc = false; prim_native_name = "";
128-
prim_native_float = false},
129-
Pccall{prim_name = "caml_nativeint_compare"; prim_arity = 2;
130-
prim_alloc = false; prim_native_name = "";
131-
prim_native_float = false},
132-
Pccall{prim_name = "caml_int32_compare"; prim_arity = 2;
133-
prim_alloc = false; prim_native_name = "";
134-
prim_native_float = false},
135-
Pccall{prim_name = "caml_int64_compare"; prim_arity = 2;
136-
prim_alloc = false; prim_native_name = "";
137-
prim_native_float = false},
105+
(Pccall(Primitive.simple ~name:"caml_compare" ~arity:2 ~alloc:true),
106+
Pccall(Primitive.simple ~name:"caml_int_compare" ~arity:2 ~alloc:false),
107+
Pccall(Primitive.simple ~name:"caml_float_compare" ~arity:2
108+
~alloc:false),
109+
Pccall(Primitive.simple ~name:"caml_string_compare" ~arity:2
110+
~alloc:false),
111+
Pccall(Primitive.simple ~name:"caml_nativeint_compare" ~arity:2
112+
~alloc:false),
113+
Pccall(Primitive.simple ~name:"caml_int32_compare" ~arity:2
114+
~alloc:false),
115+
Pccall(Primitive.simple ~name:"caml_int64_compare" ~arity:2
116+
~alloc:false),
138117
false)
139118
]
140119

@@ -332,12 +311,10 @@ let index_primitives_table =
332311
]
333312

334313
let prim_makearray =
335-
{ prim_name = "caml_make_vect"; prim_arity = 2; prim_alloc = true;
336-
prim_native_name = ""; prim_native_float = false }
314+
Primitive.simple ~name:"caml_make_vect" ~arity:2 ~alloc:true
337315

338316
let prim_obj_dup =
339-
{ prim_name = "caml_obj_dup"; prim_arity = 1; prim_alloc = true;
340-
prim_native_name = ""; prim_native_float = false }
317+
Primitive.simple ~name:"caml_obj_dup" ~arity:1 ~alloc:true
341318

342319
let find_primitive loc prim_name =
343320
match prim_name with

0 commit comments

Comments
 (0)