-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtranslattribute.ml
591 lines (536 loc) · 18.9 KB
/
translattribute.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Pierre Chambart, OCamlPro *)
(* *)
(* Copyright 2015 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. *)
(* *)
(**************************************************************************)
open Typedtree
open Lambda
open Location
let is_inline_attribute =
[ ["inline"; "ocaml.inline"],true ]
let is_inlined_attribute =
[ ["inlined"; "ocaml.inlined"], true
; ["unrolled"; "ocaml.unrolled"], (Config.flambda || Config.flambda2)
]
let is_specialise_attribute =
[ ["specialise"; "ocaml.specialise"], Config.flambda ]
let is_specialised_attribute =
[ ["specialised"; "ocaml.specialised"], Config.flambda ]
let is_local_attribute =
[ ["local"; "ocaml.local"], true ]
let is_tailcall_attribute =
[ ["tailcall"; "ocaml.tailcall"], true ]
let is_property_attribute = function
| Zero_alloc -> [ ["zero_alloc"; "ocaml.zero_alloc"], true ]
let is_tmc_attribute =
[ ["tail_mod_cons"; "ocaml.tail_mod_cons"], true ]
let is_poll_attribute =
[ ["poll"; "ocaml.poll"], true ]
let is_loop_attribute =
[ ["loop"; "ocaml.loop"], true ]
let find_attribute p attributes =
let inline_attribute =
Builtin_attributes.filter_attributes
(Builtin_attributes.Attributes_filter.create p)
attributes
in
let attr =
match inline_attribute with
| [] -> None
| [attr] -> Some attr
| attr :: {Parsetree.attr_name = {txt;loc}; _} :: _ ->
Location.prerr_warning loc (Warnings.Duplicated_attribute txt);
Some attr
in
attr
let is_unrolled = function
| {txt="unrolled"|"ocaml.unrolled"} -> true
| {txt="inline"|"ocaml.inline"|"inlined"|"ocaml.inlined"} -> false
| _ -> assert false
let get_payload get_from_exp =
let open Parsetree in
function
| PStr [{pstr_desc = Pstr_eval (exp, [])}] -> get_from_exp exp
| _ -> Result.Error ()
let get_optional_payload get_from_exp =
let open Parsetree in
function
| PStr [] -> Result.Ok None
| other -> Result.map Option.some (get_payload get_from_exp other)
let get_id_from_exp =
let open Parsetree in
function
| { pexp_desc = Pexp_ident { txt = Longident.Lident id } } -> Result.Ok id
| _ -> Result.Error ()
let get_int_from_exp =
let open Parsetree in
function
| { pexp_desc = Pexp_constant (Pconst_integer(s, None)) } ->
begin match Misc.Int_literal_converter.int s with
| n -> Result.Ok n
| exception (Failure _) -> Result.Error ()
end
| _ -> Result.Error ()
let get_construct_from_exp =
let open Parsetree in
function
| { pexp_desc =
Pexp_construct ({ txt = Longident.Lident constr }, None) } ->
Result.Ok constr
| _ -> Result.Error ()
let get_bool_from_exp exp =
Result.bind (get_construct_from_exp exp)
(function
| "true" -> Result.Ok true
| "false" -> Result.Ok false
| _ -> Result.Error ())
let get_ids_from_exp exp =
let open Parsetree in
(match exp with
| { pexp_desc = Pexp_apply (exp, args) } ->
get_id_from_exp exp ::
List.map (function
| (Asttypes.Nolabel, arg) -> get_id_from_exp arg
| (_, _) -> Result.Error ())
args
| _ -> [get_id_from_exp exp])
|> List.fold_left (fun acc r ->
match acc, r with
| Result.Ok ids, Ok id -> Result.Ok (id::ids)
| (Result.Error _ | Ok _), _ -> Result.Error ())
(Ok [])
|> Result.map List.rev
let parse_ids_payload txt loc ~default ~empty cases payload =
let[@local] warn () =
let ( %> ) f g x = g (f x) in
let msg =
cases
|> List.map (fst %> String.concat " " %> Printf.sprintf "'%s'")
|> String.concat ", "
|> Printf.sprintf "It must be either %s or empty"
in
Location.prerr_warning loc (Warnings.Attribute_payload (txt, msg));
default
in
match get_optional_payload get_ids_from_exp payload with
| Error () -> warn ()
| Ok None -> empty
| Ok (Some ids) ->
match List.assoc_opt (List.sort String.compare ids) cases with
| Some r -> r
| None -> warn ()
let parse_id_payload txt loc ~default ~empty cases payload =
let[@local] warn () =
let ( %> ) f g x = g (f x) in
let msg =
cases
|> List.map (fst %> Printf.sprintf "'%s'")
|> String.concat ", "
|> Printf.sprintf "It must be either %s or empty"
in
Location.prerr_warning loc (Warnings.Attribute_payload (txt, msg));
default
in
match get_optional_payload get_id_from_exp payload with
| Error () -> warn ()
| Ok None -> empty
| Ok (Some id) ->
match List.assoc_opt id cases with
| Some r -> r
| None -> warn ()
let parse_inline_attribute attr : inline_attribute =
match attr with
| None -> Default_inline
| Some {Parsetree.attr_name = {txt;loc} as id; attr_payload = payload} ->
if is_unrolled id then begin
(* the 'unrolled' attributes must be used as [@unrolled n]. *)
let warning txt = Warnings.Attribute_payload
(txt, "It must be an integer literal")
in
match get_payload get_int_from_exp payload with
| Ok n -> Unroll n
| Error () ->
Location.prerr_warning loc (warning txt);
Default_inline
end else
parse_id_payload txt loc
~default:Default_inline
~empty:Always_inline
[
"never", Never_inline;
"always", Always_inline;
"available", Available_inline;
]
payload
let parse_inlined_attribute attr : inlined_attribute =
match attr with
| None -> Default_inlined
| Some {Parsetree.attr_name = {txt;loc} as id; attr_payload = payload} ->
if is_unrolled id then begin
(* the 'unrolled' attributes must be used as [@unrolled n]. *)
let warning txt = Warnings.Attribute_payload
(txt, "It must be an integer literal")
in
match get_payload get_int_from_exp payload with
| Ok n -> Unroll n
| Error () ->
Location.prerr_warning loc (warning txt);
Default_inlined
end else
parse_id_payload txt loc
~default:Default_inlined
~empty:Always_inlined
[
"never", Never_inlined;
"always", Always_inlined;
"hint", Hint_inlined;
]
payload
let parse_specialise_attribute attr =
match attr with
| None -> Default_specialise
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload} ->
parse_id_payload txt loc
~default:Default_specialise
~empty:Always_specialise
[
"never", Never_specialise;
"always", Always_specialise;
]
payload
let parse_local_attribute attr =
match attr with
| None -> Default_local
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload} ->
parse_id_payload txt loc
~default:Default_local
~empty:Always_local
[
"never", Never_local;
"always", Always_local;
"maybe", Default_local;
]
payload
let parse_property_attribute attr property =
match attr with
| None -> Default_check
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload}->
parse_ids_payload txt loc
~default:Default_check
~empty:(Check { property; strict = false; opt = false; loc; } )
[
["assume"],
Assume { property; strict = false; never_returns_normally = false; loc; };
["strict"], Check { property; strict = true; opt = false; loc; };
["opt"], Check { property; strict = false; opt = true; loc; };
["opt"; "strict"; ], Check { property; strict = true; opt = true; loc; };
["assume"; "strict"],
Assume { property; strict = true; never_returns_normally = false; loc; };
["assume"; "never_returns_normally"],
Assume { property; strict = false; never_returns_normally = true; loc; };
["assume"; "strict"; "never_returns_normally"],
Assume { property; strict = true; never_returns_normally = true; loc; };
["ignore"], Ignore_assert_all property
]
payload
let parse_poll_attribute attr =
match attr with
| None -> Default_poll
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload} ->
parse_id_payload txt loc
~default:Default_poll
~empty:Default_poll
[
"error", Error_poll;
]
payload
let parse_loop_attribute attr =
match attr with
| None -> Default_loop
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload} ->
parse_id_payload txt loc
~default:Default_loop
~empty:Always_loop
[
"never", Never_loop;
"always", Always_loop;
]
payload
let get_inline_attribute l =
let attr = find_attribute is_inline_attribute l in
parse_inline_attribute attr
let get_specialise_attribute l =
let attr = find_attribute is_specialise_attribute l in
parse_specialise_attribute attr
let get_local_attribute l =
let attr = find_attribute is_local_attribute l in
parse_local_attribute attr
let get_property_attribute l p =
let attr = find_attribute (is_property_attribute p) l in
let res = parse_property_attribute attr p in
(match attr, res with
| None, Default_check -> ()
| _, Default_check -> ()
| None, (Check _ | Assume _ | Ignore_assert_all _) -> assert false
| Some _, Ignore_assert_all _ -> ()
| Some _, Assume _ -> ()
| Some attr, Check { opt; _ } ->
if Lambda.is_check_enabled ~opt p && !Clflags.native_code then
(* The warning for unchecked functions will not trigger if the check is requested
through the [@@@zero_alloc all] top-level annotation rather than through the
function annotation [@zero_alloc]. *)
Builtin_attributes.register_property attr.attr_name);
res
let get_poll_attribute l =
let attr = find_attribute is_poll_attribute l in
parse_poll_attribute attr
let get_loop_attribute l =
let attr = find_attribute is_loop_attribute l in
parse_loop_attribute attr
let check_local_inline loc attr =
match attr.local, attr.inline with
| Always_local, (Always_inline | Available_inline | Unroll _) ->
Location.prerr_warning loc
(Warnings.Duplicated_attribute "local/inline")
| _ ->
()
let check_poll_inline loc attr =
match attr.poll, attr.inline with
| Error_poll, (Always_inline | Available_inline | Unroll _) ->
Location.prerr_warning loc
(Warnings.Inlining_impossible
"[@poll error] is incompatible with inlining")
| _ ->
()
let check_poll_local loc attr =
match attr.poll, attr.local with
| Error_poll, Always_local ->
Location.prerr_warning loc
(Warnings.Inlining_impossible
"[@poll error] is incompatible with local function optimization")
| _ ->
()
let lfunction_with_attr ~attr
{ kind; params; return; body; attr=_; loc; mode; region } =
lfunction ~kind ~params ~return ~body ~attr ~loc ~mode ~region
let add_inline_attribute expr loc attributes =
match expr with
| Lfunction({ attr = { stub = false } as attr } as funct) ->
begin match get_inline_attribute attributes with
| Default_inline -> expr
| (Always_inline | Available_inline | Never_inline | Unroll _)
as inline ->
begin match attr.inline with
| Default_inline -> ()
| Always_inline | Available_inline | Never_inline | Unroll _ ->
Location.prerr_warning loc
(Warnings.Duplicated_attribute "inline")
end;
let attr = { attr with inline } in
check_local_inline loc attr;
check_poll_inline loc attr;
lfunction_with_attr ~attr funct
end
| _ -> expr
let add_specialise_attribute expr loc attributes =
match expr with
| Lfunction({ attr = { stub = false } as attr } as funct) ->
begin match get_specialise_attribute attributes with
| Default_specialise -> expr
| (Always_specialise | Never_specialise) as specialise ->
begin match attr.specialise with
| Default_specialise -> ()
| Always_specialise | Never_specialise ->
Location.prerr_warning loc
(Warnings.Duplicated_attribute "specialise")
end;
let attr = { attr with specialise } in
lfunction_with_attr ~attr funct
end
| _ -> expr
let add_local_attribute expr loc attributes =
match expr with
| Lfunction({ attr = { stub = false } as attr } as funct) ->
begin match get_local_attribute attributes with
| Default_local -> expr
| (Always_local | Never_local) as local ->
begin match attr.local with
| Default_local -> ()
| Always_local | Never_local ->
Location.prerr_warning loc
(Warnings.Duplicated_attribute "local")
end;
let attr = { attr with local } in
check_local_inline loc attr;
check_poll_local loc attr;
lfunction_with_attr ~attr funct
end
| _ -> expr
let assume_zero_alloc attributes =
let p = Zero_alloc in
let attr = find_attribute (is_property_attribute p) attributes in
match parse_property_attribute attr p with
| Default_check -> false
| Ignore_assert_all _ -> false
| Assume { property = Zero_alloc; _ } -> true
| Check { property = Zero_alloc; _ } -> false
let assume_zero_alloc attributes =
(* This function is used for "look-ahead" to find attributes
that affect [Scoped_location] settings before translation
of expressions in that scope.
Warnings will be produced by [add_check_attribute]. *)
Warnings.without_warnings (fun () -> assume_zero_alloc attributes)
let add_check_attribute expr loc attributes =
let to_string = function
| Zero_alloc -> "zero_alloc"
in
let to_string = function
| Check { property; strict; loc = _} ->
Printf.sprintf "assert %s%s"
(to_string property)
(if strict then " strict" else "")
| Assume { property; strict; loc = _} ->
Printf.sprintf "assume %s%s"
(to_string property)
(if strict then " strict" else "")
| Ignore_assert_all property ->
Printf.sprintf "ignore %s" (to_string property)
| Default_check -> assert false
in
match expr with
| Lfunction({ attr = { stub = false } as attr; } as funct) ->
begin match get_property_attribute attributes Zero_alloc with
| Default_check -> expr
| (Ignore_assert_all p | Check { property = p; _ } | Assume { property = p; _ })
as check ->
begin match attr.check with
| Default_check -> ()
| Ignore_assert_all p'
| Assume { property = p'; strict = _; loc = _; }
| Check { property = p'; strict = _; loc = _; } ->
if p = p' then
Location.prerr_warning loc
(Warnings.Duplicated_attribute (to_string check));
end;
let attr = { attr with check } in
lfunction_with_attr ~attr funct
end
| expr -> expr
let add_loop_attribute expr loc attributes =
match expr with
| Lfunction({ attr = { stub = false } as attr } as funct) ->
begin match get_loop_attribute attributes with
| Default_loop -> expr
| (Always_loop | Never_loop) as loop ->
begin match attr.loop with
| Default_loop -> ()
| Always_loop | Never_loop ->
Location.prerr_warning loc
(Warnings.Duplicated_attribute "loop")
end;
let attr = { attr with loop } in
lfunction_with_attr ~attr funct
end
| _ -> expr
let add_tmc_attribute expr loc attributes =
match expr with
| Lfunction funct ->
let attr = find_attribute is_tmc_attribute attributes in
begin match attr with
| None -> expr
| Some _ ->
if funct.attr.tmc_candidate then
Location.prerr_warning loc
(Warnings.Duplicated_attribute "tail_mod_cons");
let attr = { funct.attr with tmc_candidate = true } in
lfunction_with_attr ~attr funct
end
| _ -> expr
let add_poll_attribute expr loc attributes =
match expr with
| Lfunction({ attr = { stub = false } as attr } as funct) ->
begin match get_poll_attribute attributes with
| Default_poll -> expr
| Error_poll as poll ->
begin match attr.poll with
| Default_poll -> ()
| Error_poll ->
Location.prerr_warning loc
(Warnings.Duplicated_attribute "poll error")
end;
let attr = { attr with poll } in
check_poll_inline loc attr;
check_poll_local loc attr;
let attr = { attr with inline = Never_inline; local = Never_local } in
lfunction_with_attr ~attr funct
end
| expr -> expr
(* Get the [@inlined] attribute payload (or default if not present). *)
let get_inlined_attribute e =
let attr = find_attribute is_inlined_attribute e.exp_attributes in
parse_inlined_attribute attr
let get_inlined_attribute_on_module e =
let rec get mod_expr =
let attr = find_attribute is_inlined_attribute mod_expr.mod_attributes in
let attr = parse_inlined_attribute attr in
let attr =
match mod_expr.Typedtree.mod_desc with
| Tmod_constraint (me, _, _, _) ->
let inner_attr = get me in
begin match attr with
| Always_inlined | Hint_inlined | Never_inlined | Unroll _ -> attr
| Default_inlined -> inner_attr
end
| _ -> attr
in
attr
in
get e
let get_specialised_attribute e =
let attr = find_attribute is_specialised_attribute e.exp_attributes in
parse_specialise_attribute attr
let get_tailcall_attribute e =
let attr = find_attribute is_tailcall_attribute e.exp_attributes in
match attr with
| None -> Default_tailcall
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload} ->
match get_optional_payload get_bool_from_exp payload with
| Ok (None | Some true) -> Tailcall_expectation true
| Ok (Some false) -> Tailcall_expectation false
| Error () ->
let msg = "Only an optional boolean literal is supported." in
Location.prerr_warning loc (Warnings.Attribute_payload (txt, msg));
Default_tailcall
let add_function_attributes lam loc attr =
let lam =
add_inline_attribute lam loc attr
in
let lam =
add_specialise_attribute lam loc attr
in
let lam =
add_local_attribute lam loc attr
in
let lam =
add_check_attribute lam loc attr
in
let lam =
add_loop_attribute lam loc attr
in
let lam =
add_tmc_attribute lam loc attr
in
let lam =
(* last because poll overrides inline and local *)
add_poll_attribute lam loc attr
in
lam