Skip to content

Commit 3a5d06a

Browse files
authored
flambda-backend: Unboxed literal jane syntax (#1487)
* Add rudimentary support for literal expressions * Add support for patterns * minimize diff * Remove lack of comment * fix ocamlprof
1 parent cc61a3a commit 3a5d06a

File tree

16 files changed

+6880
-6493
lines changed

16 files changed

+6880
-6493
lines changed

boot/menhir/parser.ml

Lines changed: 6429 additions & 6394 deletions
Large diffs are not rendered by default.

parsing/ast_invariants.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ let iterator =
7878
| Cexp_array_comprehension (_, {clauses = []; body = _}) )
7979
->
8080
empty_comprehension loc
81-
| Jexp_comprehension _ | Jexp_immutable_array _ -> ()
81+
| Jexp_comprehension _
82+
| Jexp_immutable_array _
83+
| Jexp_unboxed_constant _
84+
-> ()
8285
in
8386
let expr self exp =
8487
begin match exp.pexp_desc with

parsing/ast_iterator.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ module M = struct
399399
| Pstr_attribute x -> sub.attribute sub x
400400
end
401401

402+
(* A no-op, but makes it clearer which jane syntax cases should have the same
403+
handling as core-language cases. *)
404+
let iter_constant = ()
405+
402406
module E = struct
403407
(* Value expressions for the core language *)
404408

@@ -438,6 +442,7 @@ module E = struct
438442
let iter_jst sub : Jane_syntax.Expression.t -> _ = function
439443
| Jexp_comprehension comp_exp -> iter_comp_exp sub comp_exp
440444
| Jexp_immutable_array iarr_exp -> iter_iarr_exp sub iarr_exp
445+
| Jexp_unboxed_constant _ -> iter_constant
441446

442447
let iter sub
443448
({pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} as expr)=
@@ -450,7 +455,7 @@ module E = struct
450455
sub.attributes sub attrs;
451456
match desc with
452457
| Pexp_ident x -> iter_loc sub x
453-
| Pexp_constant _ -> ()
458+
| Pexp_constant _ -> iter_constant
454459
| Pexp_let (_r, vbs, e) ->
455460
List.iter (sub.value_binding sub) vbs;
456461
sub.expr sub e
@@ -540,6 +545,7 @@ module P = struct
540545

541546
let iter_jst sub : Jane_syntax.Pattern.t -> _ = function
542547
| Jpat_immutable_array iapat -> iter_iapat sub iapat
548+
| Jpat_unboxed_constant _ -> iter_constant
543549

544550
let iter sub
545551
({ppat_desc = desc; ppat_loc = loc; ppat_attributes = attrs} as pat) =
@@ -554,7 +560,7 @@ module P = struct
554560
| Ppat_any -> ()
555561
| Ppat_var s -> iter_loc sub s
556562
| Ppat_alias (p, s) -> sub.pat sub p; iter_loc sub s
557-
| Ppat_constant _ -> ()
563+
| Ppat_constant _ -> iter_constant
558564
| Ppat_interval _ -> ()
559565
| Ppat_tuple pl -> List.iter (sub.pat sub) pl
560566
| Ppat_construct (l, p) ->

parsing/ast_mapper.ml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ module E = struct
487487

488488
module C = Jane_syntax.Comprehensions
489489
module IA = Jane_syntax.Immutable_arrays
490+
module UC = Jane_syntax.Unboxed_constants
490491

491492
let map_iterator sub : C.iterator -> C.iterator = function
492493
| Range { start; stop; direction } ->
@@ -519,10 +520,18 @@ module E = struct
519520
| Iaexp_immutable_array elts ->
520521
Iaexp_immutable_array (List.map (sub.expr sub) elts)
521522

523+
let map_unboxed_constant_exp _sub : UC.expression -> UC.expression = function
524+
(* We can't reasonably call [sub.constant] because it might return a kind
525+
of constant we don't know how to unbox.
526+
*)
527+
| Float _ | Integer _ as x -> x
528+
522529
let map_jst sub : Jane_syntax.Expression.t -> Jane_syntax.Expression.t =
523530
function
524-
| Jexp_comprehension cexp -> Jexp_comprehension (map_cexp sub cexp)
525-
| Jexp_immutable_array iaexp -> Jexp_immutable_array (map_iaexp sub iaexp)
531+
| Jexp_comprehension x -> Jexp_comprehension (map_cexp sub x)
532+
| Jexp_immutable_array x -> Jexp_immutable_array (map_iaexp sub x)
533+
| Jexp_unboxed_constant x ->
534+
Jexp_unboxed_constant (map_unboxed_constant_exp sub x)
526535

527536
let map sub
528537
({pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} as exp) =
@@ -531,10 +540,8 @@ module E = struct
531540
match Jane_syntax.Expression.of_ast exp with
532541
| Some (jexp, attrs) -> begin
533542
let attrs = sub.attributes sub attrs in
534-
Jane_syntax_parsing.Expression.wrap_desc ~loc ~info:attrs @@
535-
match sub.expr_jane_syntax sub jexp with
536-
| Jexp_comprehension c -> Jane_syntax.Comprehensions.expr_of ~loc c
537-
| Jexp_immutable_array i -> Jane_syntax.Immutable_arrays.expr_of ~loc i
543+
Jane_syntax_parsing.Expression.wrap_desc ~loc ~info:attrs
544+
(Jane_syntax.Expression.expr_of ~loc (sub.expr_jane_syntax sub jexp))
538545
end
539546
| None ->
540547
let attrs = sub.attributes sub attrs in
@@ -627,13 +634,22 @@ module P = struct
627634
(* Patterns *)
628635

629636
module IA = Jane_syntax.Immutable_arrays
637+
module UC = Jane_syntax.Unboxed_constants
630638

631639
let map_iapat sub : IA.pattern -> IA.pattern = function
632640
| Iapat_immutable_array elts ->
633641
Iapat_immutable_array (List.map (sub.pat sub) elts)
634642

643+
let map_unboxed_constant_pat _sub : UC.pattern -> UC.pattern = function
644+
(* We can't reasonably call [sub.constant] because it might return a kind
645+
of constant we don't know how to unbox.
646+
*)
647+
| Float _ | Integer _ as x -> x
648+
635649
let map_jst sub : Jane_syntax.Pattern.t -> Jane_syntax.Pattern.t = function
636-
| Jpat_immutable_array iapat -> Jpat_immutable_array (map_iapat sub iapat)
650+
| Jpat_immutable_array x -> Jpat_immutable_array (map_iapat sub x)
651+
| Jpat_unboxed_constant x ->
652+
Jpat_unboxed_constant (map_unboxed_constant_pat sub x)
637653

638654
let map sub
639655
({ppat_desc = desc; ppat_loc = loc; ppat_attributes = attrs} as pat) =
@@ -643,8 +659,7 @@ module P = struct
643659
| Some (jpat, attrs) -> begin
644660
let attrs = sub.attributes sub attrs in
645661
Jane_syntax_parsing.Pattern.wrap_desc ~loc ~info:attrs @@
646-
match sub.pat_jane_syntax sub jpat with
647-
| Jpat_immutable_array i -> Jane_syntax.Immutable_arrays.pat_of ~loc i
662+
Jane_syntax.Pattern.pat_of ~loc (sub.pat_jane_syntax sub jpat)
648663
end
649664
| None ->
650665
let attrs = sub.attributes sub attrs in

parsing/depend.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ let add_type_exception bv te =
178178

179179
let pattern_bv = ref String.Map.empty
180180

181+
(* A no-op, but makes it clearer which jane syntax cases should have the same
182+
handling as core-language cases. *)
183+
let add_constant = ()
184+
181185
let rec add_pattern bv pat =
182186
match Jane_syntax.Pattern.of_ast pat with
183187
| Some (jpat, _attrs) -> add_pattern_jane_syntax bv jpat
@@ -211,6 +215,7 @@ let rec add_pattern bv pat =
211215
and add_pattern_jane_syntax bv : Jane_syntax.Pattern.t -> _ = function
212216
| Jpat_immutable_array (Iapat_immutable_array pl) ->
213217
List.iter (add_pattern bv) pl
218+
| Jpat_unboxed_constant _ -> add_constant
214219

215220
let add_pattern bv pat =
216221
pattern_bv := bv;
@@ -223,7 +228,7 @@ let rec add_expr bv exp =
223228
| None ->
224229
match exp.pexp_desc with
225230
Pexp_ident l -> add bv l
226-
| Pexp_constant _ -> ()
231+
| Pexp_constant _ -> add_constant
227232
| Pexp_let(rf, pel, e) ->
228233
let bv = add_bindings rf bv pel in add_expr bv e
229234
| Pexp_fun (_, opte, p, e) ->
@@ -294,8 +299,9 @@ let rec add_expr bv exp =
294299
| Pexp_unreachable -> ()
295300

296301
and add_expr_jane_syntax bv : Jane_syntax.Expression.t -> _ = function
297-
| Jexp_comprehension cexp -> add_comprehension_expr bv cexp
298-
| Jexp_immutable_array iaexp -> add_immutable_array_expr bv iaexp
302+
| Jexp_comprehension x -> add_comprehension_expr bv x
303+
| Jexp_immutable_array x -> add_immutable_array_expr bv x
304+
| Jexp_unboxed_constant _ -> add_constant
299305

300306
and add_comprehension_expr bv : Jane_syntax.Comprehensions.expression -> _ =
301307
function

parsing/jane_syntax.ml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,57 @@ module Strengthen = struct
364364
| _ -> failwith "Malformed strengthened module type"
365365
end
366366

367+
module Unboxed_constants = struct
368+
type t =
369+
| Float of string * char option
370+
| Integer of string * char
371+
372+
type expression = t
373+
type pattern = t
374+
375+
let feature : Feature.t = Language_extension Layouts
376+
377+
let fail_malformed ~loc =
378+
Location.raise_errorf ~loc "Malformed unboxed numeric literal"
379+
380+
let of_constant ~loc = function
381+
| Pconst_float (x, suffix) -> Float (x, suffix)
382+
| Pconst_integer (x, Some suffix) -> Integer (x, suffix)
383+
| Pconst_integer (_, None) ->
384+
Location.raise_errorf ~loc
385+
"Malformed unboxed int literal: suffix required"
386+
| _ -> fail_malformed ~loc
387+
388+
389+
(* Returns remaining unconsumed attributes *)
390+
let of_expr expr =
391+
let loc = expr.pexp_loc in
392+
match expr.pexp_desc with
393+
| Pexp_constant const -> of_constant ~loc const, expr.pexp_attributes
394+
| _ -> fail_malformed ~loc
395+
396+
(* Returns remaining unconsumed attributes *)
397+
let of_pat pat =
398+
let loc = pat.ppat_loc in
399+
match pat.ppat_desc with
400+
| Ppat_constant const -> of_constant ~loc const, pat.ppat_attributes
401+
| _ -> fail_malformed ~loc
402+
403+
let constant_of = function
404+
| Float (x, suffix) -> Pconst_float (x, suffix)
405+
| Integer (x, suffix) -> Pconst_integer (x, Some suffix)
406+
407+
let expr_of ~loc t =
408+
let constant = constant_of t in
409+
Expression.make_entire_jane_syntax ~loc feature (fun () ->
410+
Ast_helper.Exp.constant constant)
411+
412+
let pat_of ~loc t =
413+
let constant = constant_of t in
414+
Pattern.make_entire_jane_syntax ~loc feature (fun () ->
415+
Ast_helper.Pat.constant constant)
416+
end
417+
367418
(******************************************************************************)
368419
(** The interface to our novel syntax, which we export *)
369420

@@ -396,6 +447,7 @@ module Expression = struct
396447
type t =
397448
| Jexp_comprehension of Comprehensions.expression
398449
| Jexp_immutable_array of Immutable_arrays.expression
450+
| Jexp_unboxed_constant of Unboxed_constants.expression
399451

400452
let of_ast_internal (feat : Feature.t) expr = match feat with
401453
| Language_extension Comprehensions ->
@@ -404,22 +456,38 @@ module Expression = struct
404456
| Language_extension Immutable_arrays ->
405457
let expr, attrs = Immutable_arrays.of_expr expr in
406458
Some (Jexp_immutable_array expr, attrs)
459+
| Language_extension Layouts ->
460+
let expr, attrs = Unboxed_constants.of_expr expr in
461+
Some (Jexp_unboxed_constant expr, attrs)
407462
| _ -> None
408463

409464
let of_ast = Expression.make_of_ast ~of_ast_internal
465+
466+
let expr_of ~loc = function
467+
| Jexp_comprehension x -> Comprehensions.expr_of ~loc x
468+
| Jexp_immutable_array x -> Immutable_arrays.expr_of ~loc x
469+
| Jexp_unboxed_constant x -> Unboxed_constants.expr_of ~loc x
410470
end
411471

412472
module Pattern = struct
413473
type t =
414474
| Jpat_immutable_array of Immutable_arrays.pattern
475+
| Jpat_unboxed_constant of Unboxed_constants.pattern
415476

416477
let of_ast_internal (feat : Feature.t) pat = match feat with
417478
| Language_extension Immutable_arrays ->
418479
let expr, attrs = Immutable_arrays.of_pat pat in
419480
Some (Jpat_immutable_array expr, attrs)
481+
| Language_extension Layouts ->
482+
let pat, attrs = Unboxed_constants.of_pat pat in
483+
Some (Jpat_unboxed_constant pat, attrs)
420484
| _ -> None
421485

422486
let of_ast = Pattern.make_of_ast ~of_ast_internal
487+
488+
let pat_of ~loc = function
489+
| Jpat_immutable_array x -> Immutable_arrays.pat_of ~loc x
490+
| Jpat_unboxed_constant x -> Unboxed_constants.pat_of ~loc x
423491
end
424492

425493
module Module_type = struct

parsing/jane_syntax.mli

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ module Strengthen : sig
121121
-> Parsetree.module_type_desc With_attributes.t
122122
end
123123

124+
(** The ASTs for unboxed literals, like #4.0 *)
125+
module Unboxed_constants : sig
126+
type t =
127+
| Float of string * char option
128+
| Integer of string * char
129+
130+
type expression = t
131+
type pattern = t
132+
133+
val expr_of :
134+
loc:Location.t -> expression -> Parsetree.expression_desc With_attributes.t
135+
136+
val pat_of :
137+
loc:Location.t -> pattern -> Parsetree.pattern_desc With_attributes.t
138+
end
139+
124140
(******************************************)
125141
(* General facility, which we export *)
126142

@@ -219,22 +235,30 @@ end
219235
(** Novel syntax in expressions *)
220236
module Expression : sig
221237
type t =
222-
| Jexp_comprehension of Comprehensions.expression
223-
| Jexp_immutable_array of Immutable_arrays.expression
238+
| Jexp_comprehension of Comprehensions.expression
239+
| Jexp_immutable_array of Immutable_arrays.expression
240+
| Jexp_unboxed_constant of Unboxed_constants.expression
224241

225242
include AST
226243
with type t := t * Parsetree.attributes
227244
and type ast := Parsetree.expression
245+
246+
val expr_of :
247+
loc:Location.t -> t -> Parsetree.expression_desc With_attributes.t
228248
end
229249

230250
(** Novel syntax in patterns *)
231251
module Pattern : sig
232252
type t =
233253
| Jpat_immutable_array of Immutable_arrays.pattern
254+
| Jpat_unboxed_constant of Unboxed_constants.pattern
234255

235256
include AST
236257
with type t := t * Parsetree.attributes
237258
and type ast := Parsetree.pattern
259+
260+
val pat_of :
261+
loc:Location.t -> t -> Parsetree.pattern_desc With_attributes.t
238262
end
239263

240264
(** Novel syntax in module types *)

0 commit comments

Comments
 (0)