forked from ocaml-flambda/flambda-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredef.ml
493 lines (457 loc) · 20 KB
/
predef.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Predefined type constructors (with special typing rules in typecore) *)
open Path
open Types
open Btype
let builtin_idents = ref []
let wrap create s =
let id = create s in
builtin_idents := (s, id) :: !builtin_idents;
id
let ident_create = wrap Ident.create_predef
let ident_int = ident_create "int"
and ident_char = ident_create "char"
and ident_bytes = ident_create "bytes"
and ident_float = ident_create "float"
and ident_float32 = ident_create "float32"
and ident_bool = ident_create "bool"
and ident_unit = ident_create "unit"
and ident_exn = ident_create "exn"
and ident_array = ident_create "array"
and ident_iarray = ident_create "iarray"
and ident_list = ident_create "list"
and ident_option = ident_create "option"
and ident_nativeint = ident_create "nativeint"
and ident_int32 = ident_create "int32"
and ident_int64 = ident_create "int64"
and ident_lazy_t = ident_create "lazy_t"
and ident_string = ident_create "string"
and ident_extension_constructor = ident_create "extension_constructor"
and ident_floatarray = ident_create "floatarray"
and ident_lexing_position = ident_create "lexing_position"
and ident_unboxed_float = ident_create "float#"
and ident_unboxed_float32 = ident_create "float32#"
and ident_unboxed_nativeint = ident_create "nativeint#"
and ident_unboxed_int32 = ident_create "int32#"
and ident_unboxed_int64 = ident_create "int64#"
and ident_or_null = ident_create "or_null"
and ident_int8x16 = ident_create "int8x16"
and ident_int16x8 = ident_create "int16x8"
and ident_int32x4 = ident_create "int32x4"
and ident_int64x2 = ident_create "int64x2"
and ident_float32x4 = ident_create "float32x4"
and ident_float64x2 = ident_create "float64x2"
and ident_unboxed_int8x16 = ident_create "int8x16#"
and ident_unboxed_int16x8 = ident_create "int16x8#"
and ident_unboxed_int32x4 = ident_create "int32x4#"
and ident_unboxed_int64x2 = ident_create "int64x2#"
and ident_unboxed_float32x4 = ident_create "float32x4#"
and ident_unboxed_float64x2 = ident_create "float64x2#"
let path_int = Pident ident_int
and path_char = Pident ident_char
and path_bytes = Pident ident_bytes
and path_float = Pident ident_float
and path_float32 = Pident ident_float32
and path_bool = Pident ident_bool
and path_unit = Pident ident_unit
and path_exn = Pident ident_exn
and path_array = Pident ident_array
and path_iarray = Pident ident_iarray
and path_list = Pident ident_list
and path_option = Pident ident_option
and path_nativeint = Pident ident_nativeint
and path_int32 = Pident ident_int32
and path_int64 = Pident ident_int64
and path_lazy_t = Pident ident_lazy_t
and path_string = Pident ident_string
and path_extension_constructor = Pident ident_extension_constructor
and path_floatarray = Pident ident_floatarray
and path_lexing_position = Pident ident_lexing_position
and path_unboxed_float = Pident ident_unboxed_float
and path_unboxed_float32 = Pident ident_unboxed_float32
and path_unboxed_nativeint = Pident ident_unboxed_nativeint
and path_unboxed_int32 = Pident ident_unboxed_int32
and path_unboxed_int64 = Pident ident_unboxed_int64
and path_or_null = Pident ident_or_null
and path_int8x16 = Pident ident_int8x16
and path_int16x8 = Pident ident_int16x8
and path_int32x4 = Pident ident_int32x4
and path_int64x2 = Pident ident_int64x2
and path_float32x4 = Pident ident_float32x4
and path_float64x2 = Pident ident_float64x2
and path_unboxed_int8x16 = Pident ident_unboxed_int8x16
and path_unboxed_int16x8 = Pident ident_unboxed_int16x8
and path_unboxed_int32x4 = Pident ident_unboxed_int32x4
and path_unboxed_int64x2 = Pident ident_unboxed_int64x2
and path_unboxed_float32x4 = Pident ident_unboxed_float32x4
and path_unboxed_float64x2 = Pident ident_unboxed_float64x2
let type_int = newgenty (Tconstr(path_int, [], ref Mnil))
and type_char = newgenty (Tconstr(path_char, [], ref Mnil))
and type_bytes = newgenty (Tconstr(path_bytes, [], ref Mnil))
and type_float = newgenty (Tconstr(path_float, [], ref Mnil))
and type_float32 = newgenty (Tconstr(path_float32, [], ref Mnil))
and type_bool = newgenty (Tconstr(path_bool, [], ref Mnil))
and type_unit = newgenty (Tconstr(path_unit, [], ref Mnil))
and type_exn = newgenty (Tconstr(path_exn, [], ref Mnil))
and type_array t = newgenty (Tconstr(path_array, [t], ref Mnil))
and type_iarray t = newgenty (Tconstr(path_iarray, [t], ref Mnil))
and type_list t = newgenty (Tconstr(path_list, [t], ref Mnil))
and type_option t = newgenty (Tconstr(path_option, [t], ref Mnil))
and type_nativeint = newgenty (Tconstr(path_nativeint, [], ref Mnil))
and type_int32 = newgenty (Tconstr(path_int32, [], ref Mnil))
and type_int64 = newgenty (Tconstr(path_int64, [], ref Mnil))
and type_lazy_t t = newgenty (Tconstr(path_lazy_t, [t], ref Mnil))
and type_string = newgenty (Tconstr(path_string, [], ref Mnil))
and type_extension_constructor =
newgenty (Tconstr(path_extension_constructor, [], ref Mnil))
and type_floatarray = newgenty (Tconstr(path_floatarray, [], ref Mnil))
and type_lexing_position = newgenty (Tconstr(path_lexing_position, [], ref Mnil))
and type_unboxed_float = newgenty (Tconstr(path_unboxed_float, [], ref Mnil))
and type_unboxed_float32 = newgenty (Tconstr(path_unboxed_float32, [], ref Mnil))
and type_unboxed_nativeint =
newgenty (Tconstr(path_unboxed_nativeint, [], ref Mnil))
and type_unboxed_int32 = newgenty (Tconstr(path_unboxed_int32, [], ref Mnil))
and type_unboxed_int64 = newgenty (Tconstr(path_unboxed_int64, [], ref Mnil))
and type_or_null t = newgenty (Tconstr(path_or_null, [t], ref Mnil))
and type_int8x16 = newgenty (Tconstr(path_int8x16, [], ref Mnil))
and type_int16x8 = newgenty (Tconstr(path_int16x8, [], ref Mnil))
and type_int32x4 = newgenty (Tconstr(path_int32x4, [], ref Mnil))
and type_int64x2 = newgenty (Tconstr(path_int64x2, [], ref Mnil))
and type_float32x4 = newgenty (Tconstr(path_float32x4, [], ref Mnil))
and type_float64x2 = newgenty (Tconstr(path_float64x2, [], ref Mnil))
and type_unboxed_int8x16 = newgenty (Tconstr(path_unboxed_int8x16, [], ref Mnil))
and type_unboxed_int16x8 = newgenty (Tconstr(path_unboxed_int16x8, [], ref Mnil))
and type_unboxed_int32x4 = newgenty (Tconstr(path_unboxed_int32x4, [], ref Mnil))
and type_unboxed_int64x2 = newgenty (Tconstr(path_unboxed_int64x2, [], ref Mnil))
and type_unboxed_float32x4 = newgenty (Tconstr(path_unboxed_float32x4, [], ref Mnil))
and type_unboxed_float64x2 = newgenty (Tconstr(path_unboxed_float64x2, [], ref Mnil))
let ident_match_failure = ident_create "Match_failure"
and ident_out_of_memory = ident_create "Out_of_memory"
and ident_invalid_argument = ident_create "Invalid_argument"
and ident_failure = ident_create "Failure"
and ident_not_found = ident_create "Not_found"
and ident_sys_error = ident_create "Sys_error"
and ident_end_of_file = ident_create "End_of_file"
and ident_division_by_zero = ident_create "Division_by_zero"
and ident_stack_overflow = ident_create "Stack_overflow"
and ident_sys_blocked_io = ident_create "Sys_blocked_io"
and ident_assert_failure = ident_create "Assert_failure"
and ident_undefined_recursive_module =
ident_create "Undefined_recursive_module"
let all_predef_exns = [
ident_match_failure;
ident_out_of_memory;
ident_invalid_argument;
ident_failure;
ident_not_found;
ident_sys_error;
ident_end_of_file;
ident_division_by_zero;
ident_stack_overflow;
ident_sys_blocked_io;
ident_assert_failure;
ident_undefined_recursive_module;
]
let path_match_failure = Pident ident_match_failure
and path_invalid_argument = Pident ident_invalid_argument
and path_assert_failure = Pident ident_assert_failure
and path_undefined_recursive_module = Pident ident_undefined_recursive_module
let cstr id args =
{
cd_id = id;
cd_args = Cstr_tuple args;
cd_res = None;
cd_loc = Location.none;
cd_attributes = [];
cd_uid = Uid.of_predef_id id;
}
let ident_false = ident_create "false"
and ident_true = ident_create "true"
and ident_void = ident_create "()"
and ident_nil = ident_create "[]"
and ident_cons = ident_create "::"
and ident_none = ident_create "None"
and ident_some = ident_create "Some"
and ident_null = ident_create "Null"
and ident_this = ident_create "This"
let option_argument_jkind = Jkind.Builtin.value ~why:(
Type_argument {parent_path = path_option; position = 1; arity = 1})
let list_argument_jkind = Jkind.Builtin.value ~why:(
Type_argument {parent_path = path_list; position = 1; arity = 1})
let or_null_argument_jkind = Jkind.Builtin.value ~why:(
Type_argument {parent_path = path_or_null; position = 1; arity = 1})
let mk_add_type add_type
?manifest type_ident
?(kind=Type_abstract Definition)
?jkind
env =
let type_jkind = match jkind with
| None -> Jkind.Builtin.value ~why:(Primitive type_ident)
| Some k -> Jkind.of_builtin ~why:(Primitive type_ident) k
in
let decl =
{type_params = [];
type_arity = 0;
type_kind = kind;
type_jkind;
type_loc = Location.none;
type_private = Asttypes.Public;
type_manifest = manifest;
type_variance = [];
type_separability = [];
type_is_newtype = false;
type_expansion_scope = lowest_level;
type_attributes = [];
type_unboxed_default = false;
type_uid = Uid.of_predef_id type_ident;
type_has_illegal_crossings = false;
}
in
add_type type_ident decl env
let mk_add_type1 add_type type_ident
?(kind=fun _ -> Type_abstract Definition)
?(jkind=Jkind.Builtin.value ~why:(Primitive type_ident))
?(param_jkind=Jkind.Builtin.value ~why:(
Type_argument {
parent_path = Path.Pident type_ident;
position = 1;
arity = 1}
))
~variance ~separability env =
let param = newgenvar param_jkind in
let decl =
{type_params = [param];
type_arity = 1;
type_kind = kind param;
type_jkind = jkind;
type_loc = Location.none;
type_private = Asttypes.Public;
type_manifest = None;
type_variance = [variance];
type_separability = [separability];
type_is_newtype = false;
type_expansion_scope = lowest_level;
type_attributes = [];
type_unboxed_default = false;
type_uid = Uid.of_predef_id type_ident;
type_has_illegal_crossings = false;
}
in
add_type type_ident decl env
let mk_add_extension add_extension id args =
List.iter (fun (_, jkind) ->
let raise_error () = Misc.fatal_error
"sanity check failed: non-value jkind in predef extension \
constructor; should this have Constructor_mixed shape?" in
match Jkind.get_layout jkind with
| Some (Base Value) -> ()
| Some (Any
| Base (Void | Float32 | Float64 | Word | Bits32 | Bits64 | Vec128)
| Product _)
| None -> raise_error ())
args;
add_extension id
{ ext_type_path = path_exn;
ext_type_params = [];
ext_args =
Cstr_tuple
(List.map
(fun (ca_type, ca_jkind) ->
{
ca_type;
ca_jkind;
ca_modalities=Mode.Modality.Value.Const.id;
ca_loc=Location.none
})
args);
ext_shape = Constructor_uniform_value;
ext_constant = args = [];
ext_ret_type = None;
ext_private = Asttypes.Public;
ext_loc = Location.none;
ext_attributes = [Ast_helper.Attr.mk
(Location.mknoloc "ocaml.warn_on_literal_pattern")
(Parsetree.PStr [])];
ext_uid = Uid.of_predef_id id;
}
let variant constrs =
let mk_elt { cd_args } =
let jkinds = match cd_args with
| Cstr_tuple args ->
Misc.Stdlib.Array.of_list_map (fun { ca_jkind } -> ca_jkind) args
| Cstr_record lbls ->
Misc.Stdlib.Array.of_list_map (fun { ld_jkind } -> ld_jkind) lbls
in
Constructor_uniform_value, jkinds
in
Type_variant (constrs,
Variant_boxed (Misc.Stdlib.Array.of_list_map mk_elt constrs))
let unrestricted tvar jkind =
{ca_type=tvar;
ca_jkind=jkind;
ca_modalities=Mode.Modality.Value.Const.id;
ca_loc=Location.none}
(* CR layouts: Changes will be needed here as we add support for the built-ins
to work with non-values, and as we relax the mixed block restriction. *)
let build_initial_env add_type add_extension empty_env =
let add_type = mk_add_type add_type
and add_type1 = mk_add_type1 add_type
and add_extension = mk_add_extension add_extension in
let list_jkind = Jkind.Builtin.value ~why:Boxed_variant in
empty_env
(* Predefined types *)
|> add_type1 ident_array
~variance:Variance.full
~separability:Separability.Ind
~param_jkind:(Jkind.add_nullability_crossing
(Jkind.Builtin.any ~why:Array_type_argument))
|> add_type1 ident_iarray
~variance:Variance.covariant
~separability:Separability.Ind
~param_jkind:(Jkind.add_nullability_crossing
(Jkind.Builtin.any ~why:Array_type_argument))
|> add_type ident_bool
~kind:(variant [ cstr ident_false []; cstr ident_true []])
~jkind:Jkind.Const.Builtin.immediate
|> add_type ident_char ~jkind:Jkind.Const.Builtin.immediate
|> add_type ident_exn ~kind:Type_open ~jkind:Jkind.Const.Builtin.value
|> add_type ident_extension_constructor
|> add_type ident_float ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_floatarray ~jkind:Jkind.Const.Builtin.mutable_data
|> add_type ident_int ~jkind:Jkind.Const.Builtin.immediate
|> add_type ident_int32 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_int64 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type1 ident_lazy_t
~variance:Variance.covariant
~separability:Separability.Ind
|> add_type1 ident_list
~variance:Variance.covariant
~separability:Separability.Ind
~kind:(fun tvar ->
variant [cstr ident_nil [];
cstr ident_cons [unrestricted tvar list_argument_jkind;
unrestricted (type_list tvar) list_jkind]])
~jkind:list_jkind
|> add_type ident_nativeint
~jkind:Jkind.Const.Builtin.immutable_data
|> add_type1 ident_option
~variance:Variance.covariant
~separability:Separability.Ind
~kind:(fun tvar ->
variant [cstr ident_none [];
cstr ident_some [unrestricted tvar option_argument_jkind]])
~jkind:(Jkind.Builtin.value ~why:Boxed_variant)
|> add_type ident_lexing_position
~kind:(
let lbl (field, field_type, jkind) =
let id = Ident.create_predef field in
{
ld_id=id;
ld_mutable=Immutable;
ld_modalities=Mode.Modality.Value.Const.id;
ld_type=field_type;
ld_jkind=jkind;
ld_loc=Location.none;
ld_attributes=[];
ld_uid=Uid.of_predef_id id;
}
in
let immediate = Jkind.Builtin.immediate ~why:(Primitive ident_int) in
let labels = List.map lbl [
("pos_fname", type_string,
Jkind.of_builtin ~why:(Primitive ident_string)
Jkind.Const.Builtin.immutable_data);
("pos_lnum", type_int, immediate);
("pos_bol", type_int, immediate);
("pos_cnum", type_int, immediate) ]
in
Type_record (
labels,
(Record_boxed (List.map (fun label -> label.ld_jkind) labels |> Array.of_list))
)
)
~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_string ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_unboxed_float ~jkind:Jkind.Const.Builtin.float64
|> add_type ident_unboxed_nativeint ~jkind:Jkind.Const.Builtin.word
|> add_type ident_unboxed_int32 ~jkind:Jkind.Const.Builtin.bits32
|> add_type ident_unboxed_int64 ~jkind:Jkind.Const.Builtin.bits64
|> add_type ident_bytes ~jkind:Jkind.Const.Builtin.mutable_data
|> add_type ident_unit
~kind:(variant [cstr ident_void []])
~jkind:Jkind.Const.Builtin.immediate
(* Predefined exceptions - alphabetical order *)
|> add_extension ident_assert_failure
[newgenty (Ttuple[None, type_string; None, type_int; None, type_int]),
Jkind.Builtin.value ~why:Tuple]
|> add_extension ident_division_by_zero []
|> add_extension ident_end_of_file []
|> add_extension ident_failure [type_string,
Jkind.Builtin.value ~why:(Primitive ident_string)]
|> add_extension ident_invalid_argument [type_string,
Jkind.Builtin.value ~why:(Primitive ident_string)]
|> add_extension ident_match_failure
[newgenty (Ttuple[None, type_string; None, type_int; None, type_int]),
Jkind.Builtin.value ~why:Tuple]
|> add_extension ident_not_found []
|> add_extension ident_out_of_memory []
|> add_extension ident_stack_overflow []
|> add_extension ident_sys_blocked_io []
|> add_extension ident_sys_error [type_string,
Jkind.Builtin.value ~why:(Primitive ident_string)]
|> add_extension ident_undefined_recursive_module
[newgenty (Ttuple[None, type_string; None, type_int; None, type_int]),
Jkind.Builtin.value ~why:Tuple]
let add_simd_stable_extension_types add_type env =
let add_type = mk_add_type add_type in
env
|> add_type ident_int8x16 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_int16x8 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_int32x4 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_int64x2 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_float32x4 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_float64x2 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_unboxed_int8x16 ~jkind:Jkind.Const.Builtin.vec128
|> add_type ident_unboxed_int16x8 ~jkind:Jkind.Const.Builtin.vec128
|> add_type ident_unboxed_int32x4 ~jkind:Jkind.Const.Builtin.vec128
|> add_type ident_unboxed_int64x2 ~jkind:Jkind.Const.Builtin.vec128
|> add_type ident_unboxed_float32x4 ~jkind:Jkind.Const.Builtin.vec128
|> add_type ident_unboxed_float64x2 ~jkind:Jkind.Const.Builtin.vec128
let add_small_number_extension_types add_type env =
let add_type = mk_add_type add_type in
env
|> add_type ident_float32 ~jkind:Jkind.Const.Builtin.immutable_data
|> add_type ident_unboxed_float32 ~jkind:Jkind.Const.Builtin.float32
let or_null_kind tvar =
variant [cstr ident_null [];
cstr ident_this [unrestricted tvar or_null_argument_jkind]]
let add_or_null add_type env =
let add_type1 = mk_add_type1 add_type in
env
|> add_type1 ident_or_null
~variance:Variance.covariant
~separability:Separability.Ind
(* CR layouts v3: [or_null] is separable only if the argument type
is non-float. The current separability system can't track that.
We also want to allow [float or_null] despite it being non-separable.
For now, we mark the type argument as [Separability.Ind] to permit
the most argument types, and forbid arrays from accepting [or_null]s.
In the future, we will track separability in the jkind system. *)
~kind:or_null_kind
~jkind:(Jkind.Builtin.value_or_null ~why:(Primitive ident_or_null))
let builtin_values =
List.map (fun id -> (Ident.name id, id)) all_predef_exns
let builtin_idents = List.rev !builtin_idents