Source file expander.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
open! Ppxlib
open! Stdppx
open Ast_builder.Default
include Expander_intf
module Monomorphize = Ppx_template_expander.Monomorphize

let value_name s ~type_name =
  match Ppx_helpers.demangle_template type_name with
  | "t", mangle -> s ^ mangle
  | type_name, mangle -> s ^ "_" ^ type_name ^ mangle
;;

let with_suffix loc s ~type_name ~f = f ~loc (value_name s ~type_name)

let ptyp_poly loc ~params =
  if List.is_empty params
  then Fn.id
  else
    Ppxlib_jane.Ast_builder.Default.ptyp_poly
      ~loc
      (List.map ~f:Ppxlib_jane.get_type_param_name_and_jkind_of_core_type params)
;;

module Arrow = struct
  type t =
    { box : core_type
    ; unbox : core_type
    }

  let create loc ~boxed ~unboxed ~params ~portable =
    let ptyp_poly = ptyp_poly loc ~params in
    let boxed = boxed.type_
    and unboxed = unboxed.type_ in
    if portable
    then
      { box = ptyp_poly [%type: [%t unboxed] @ l p -> [%t boxed] @ l p]
      ; unbox = ptyp_poly [%type: [%t boxed] @ l p -> [%t unboxed] @ l p]
      }
    else
      { box = ptyp_poly [%type: [%t unboxed] @ l -> [%t boxed] @ l]
      ; unbox = ptyp_poly [%type: [%t boxed] @ l -> [%t unboxed] @ l]
      }
  ;;
end

module Witness = struct
  let type_ loc ~(boxed : parts) ~(unboxed : parts) ~params =
    ptyp_poly
      loc
      ~params
      [%type: unit -> ([%t boxed.type_], [%t unboxed.type_]) Ppx_box_lib.Boxed.t]
  ;;

  let structure loc ~(boxed : parts) ~(unboxed : parts) ~type_name ~params =
    let pvar s = with_suffix loc s ~type_name ~f:pvar in
    let evar s = with_suffix loc s ~type_name ~f:evar in
    let type_declaration (name, manifest) =
      type_declaration
        ~loc
        ~name:(Loc.make ~loc name)
        ~params:(List.map params ~f:(fun type_ -> type_, (NoVariance, NoInjectivity)))
        ~cstrs:[]
        ~kind:Ptype_abstract
        ~private_:Public
        ~manifest:(Some manifest.type_)
    in
    let type_ = type_ loc ~boxed ~unboxed ~params in
    let mod_ =
      pmod_structure
        ~loc
        [%str
          [%%i
            pstr_type
              ~loc
              Nonrecursive
              (List.map ~f:type_declaration [ "t", boxed; "u", unboxed ])]

          let box u = ([%e evar Common.box] [@alloc a]) u [@exclave_if_stack a]
          [@@alloc a = (heap, stack)]
          ;;

          let unbox t =
            ([%e evar Common.unbox] [@mode m] [@zero_alloc assume_unless_opt])
              t [@exclave_if_local m]
          [@@mode m = (global, local)]
          ;;]
    in
    match List.length params with
    | 0 ->
      [%str
        open struct
          module Arg = [%m mod_]
        end

        let [%p pvar Common.boxed] : [%t type_] =
          fun () -> Ppx_box_lib.Boxed.magic_create (module Arg)
        ;;]
    | arity when arity < 6 ->
      let payload =
        List.map params ~f:(fun param ->
          match Ppxlib_jane.get_type_param_name_and_jkind_of_core_type param with
          | _, None -> [%expr (_ : (_ : value))]
          | _, jkind ->
            [%expr
              (_
               : [%t
                   { param with
                     ptyp_desc =
                       Ppxlib_jane.Shim.Core_type_desc.to_parsetree (Ptyp_any jkind)
                   }])])
        |> function
        | [] -> assert false
        | [ param ] -> param
        | param :: params ->
          pexp_apply ~loc param (List.map params ~f:(fun param -> Nolabel, param))
      in
      let kind_attribute =
        attribute
          ~loc
          ~name:(Loc.make ~loc "kind.explicit")
          ~payload:(PStr [%str [%e payload]])
      in
      [%str
        open
          [%m
          pmod_apply
            ~loc
            { (pmod_ident
                 ~loc
                 (Loc.make
                    ~loc
                    (Longident.parse
                       (Printf.sprintf "Ppx_box_lib.Boxed.Unsafe_create%d" arity))))
              with
              pmod_attributes = [ kind_attribute ]
            }
            mod_]

        let [%p pvar Common.boxed] : [%t type_] = fun () -> boxed ()]
    | _ -> [%str]
  ;;
end

module Make (X : X) : S with type t = X.t = struct
  include X

  let portable_mode_attributes ~portable loc =
    if portable
    then
      [ attribute
          ~loc
          ~name:(Loc.make ~loc "mode")
          ~payload:(PStr [%str p = (portable, shareable, nonportable)])
      ]
    else []
  ;;

  let structure_items ~portable x loc ~type_name ~params =
    let pvar s = with_suffix loc s ~type_name ~f:pvar in
    let boxed = boxed x loc ~type_name ~params in
    let unboxed = unboxed x loc ~type_name ~params in
    let arrow = Arrow.create loc ~boxed ~unboxed ~params ~portable in
    let witness = Witness.structure loc ~boxed ~unboxed ~type_name ~params in
    let box =
      { Ppxlib_jane.Ast_builder.Default.(
          value_binding
            ~loc
            ~pat:(ppat_constraint ~loc (pvar Common.box) (Some arrow.box) [])
            ~expr:
              [%expr
                fun [%p unboxed.pattern] -> [%e boxed.expression] [@exclave_if_stack a]]
            ~modes:[])
        with
        pvb_attributes =
          [ attribute
              ~loc
              ~name:(Loc.make ~loc "alloc")
              ~payload:(PStr [%str a @ l = (heap_global, stack_local)])
          ]
          @ portable_mode_attributes ~portable loc
      }
    in
    let unbox_mode_attribute =
      attribute
        ~loc
        ~name:(Loc.make ~loc "mode")
        ~payload:
          (PStr
             (if portable
              then [%str l = (global, local), p = (portable, shareable, nonportable)]
              else [%str l = (global, local)]))
    in
    let unbox =
      { Ppxlib_jane.Ast_builder.Default.(
          value_binding
            ~loc
            ~pat:(ppat_constraint ~loc (pvar Common.unbox) (Some arrow.unbox) [])
            ~expr:[%expr fun [%p boxed.pattern] -> [%e unboxed.expression]]
            ~modes:[])
        with
        pvb_attributes = [ unbox_mode_attribute ]
      }
    in
    [ pstr_value ~loc Nonrecursive [ box ]; pstr_value ~loc Nonrecursive [ unbox ] ]
    @ [%str include [%m pmod_structure ~loc witness]]
    (* We directly expand the templated code so that the deriving ppx ignores all values,
       instead of just ignoring the value written concretely and forgetting about the
       templated values.
    *)
    |> Monomorphize.t#structure Monomorphize.Context.top
  ;;

  let zero_alloc_attribute loc =
    attribute ~loc ~name:(Loc.make ~loc "zero_alloc") ~payload:(PStr [])
  ;;

  let zero_alloc_if_stack_attribute loc =
    attribute ~loc ~name:(Loc.make ~loc "zero_alloc_if_stack") ~payload:(PStr [%str a])
  ;;

  let signature_item loc name attributes ~type_ ~type_name =
    let name = with_suffix loc name ~type_name ~f:Loc.make in
    let value_desc =
      Ppxlib_jane.Ast_builder.Default.value_description
        ~loc
        ~name
        ~type_
        ~modalities:[ { txt = Modality "stateless"; loc } ]
        ~prim:[]
    in
    { value_desc with pval_attributes = attributes } |> psig_value ~loc
  ;;

  let signature_items ~portable x loc ~type_name ~params =
    let boxed = boxed x loc ~type_name ~params in
    let unboxed = unboxed x loc ~type_name ~params in
    let arrow = Arrow.create loc ~boxed ~unboxed ~params ~portable in
    let witness_type = Witness.type_ loc ~boxed ~unboxed ~params in
    let unbox_mode_attribute =
      attribute
        ~loc
        ~name:(Loc.make ~loc "mode")
        ~payload:
          (PStr
             (if portable
              then [%str l = (global, local), p = (portable, shareable, nonportable)]
              else [%str l = (global, local)]))
    in
    [ [%sigi:
        [%%template:
        [%%i
          signature_item
            loc
            Common.box
            ([ attribute
                 ~loc
                 ~name:(Loc.make ~loc "alloc")
                 ~payload:(PStr [%str a @ l = (heap_global, stack_local)])
             ]
             @ portable_mode_attributes ~portable loc
             @ [ zero_alloc_if_stack_attribute loc ])
            ~type_:arrow.box
            ~type_name]

        [%%i
          signature_item
            loc
            Common.unbox
            [ unbox_mode_attribute; zero_alloc_attribute loc ]
            ~type_:arrow.unbox
            ~type_name]

        [%%i
          if List.length params < 6
          then
            signature_item
              loc
              Common.boxed
              [ zero_alloc_attribute loc ]
              ~type_:witness_type
              ~type_name
          else [%sigi: include sig end]]]]
    ]
  ;;
end