Source file ppx_cstubs_internals.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
(* Copyright 2018 fdopen

   Permission to use, copy, modify, and/or distribute this software for any
   purpose with or without fee is hereby granted.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
   OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *)

open Ctypes_static

let rec seal : type a. a Ctypes_static.typ -> size:int -> align:int -> unit =
 fun t ~size ~align ->
  match t with
  | Struct ({ spec = Incomplete _; _ } as s) ->
    s.fields <- List.rev s.fields;
    s.spec <- Complete { size; align }
  | Union ({ uspec = None; _ } as u) ->
    u.ufields <- List.rev u.ufields;
    u.uspec <- Some { size; align }
  | Struct { tag; _ } -> raise (ModifyingSealedType tag)
  | Union { utag; _ } -> raise (ModifyingSealedType utag)
  | View { ty; _ } -> seal ty ~size ~align
  | _ -> raise (Unsupported "Sealing a non-structured type")

let rec add_field :
    type t a.
    t Ctypes_static.typ ->
    string ->
    int ->
    a Ctypes_static.typ ->
    (a, t) Ctypes_static.field =
 fun t fname foffset ftype ->
  match t with
  | Struct s ->
    let r = { fname; foffset; ftype } in
    s.fields <- BoxedField r :: s.fields;
    r
  | Union u ->
    let r = { fname; foffset; ftype } in
    u.ufields <- BoxedField r :: u.ufields;
    r
  | View { ty; _ } ->
    let ({ fname = _; _ } as r) = add_field ty fname foffset ftype in
    r
  | _ -> failwith ("Unexpected field " ^ fname)

external identity : 'a -> 'a = "%identity"

let build_enum :
    type a b.
    string ->
    a Ctypes.typ ->
    typedef:bool ->
    ?unexpected:(int64 -> b) ->
    (b * a) list ->
    b Ctypes.typ =
 fun name typ ~typedef ?unexpected alist ->
  let fail t =
    Printf.ksprintf failwith "Invalid enum type %s" (Ctypes.string_of_typ t)
  in
  let rlist = List.map (fun (l, r) -> (r, l)) alist in
  let unexpected =
    match unexpected with
    | None ->
      let to_string =
        match typ with
        | Ctypes_static.Primitive p ->
          (match p with
           | Ctypes_primitive_types.Int8_t -> string_of_int
           | Ctypes_primitive_types.Int32_t -> (Int32.to_string : a -> string)
           | Ctypes_primitive_types.Int16_t -> string_of_int
           | Ctypes_primitive_types.Int -> string_of_int
           | Ctypes_primitive_types.Int64_t -> Int64.to_string
           | Ctypes_primitive_types.Uint8_t -> Unsigned.UInt8.to_string
           | Ctypes_primitive_types.Uint16_t -> Unsigned.UInt16.to_string
           | Ctypes_primitive_types.Uint32_t -> Unsigned.UInt32.to_string
           | Ctypes_primitive_types.Uint64_t -> Unsigned.UInt64.to_string
           | _ -> fail typ
            : a -> string)
        | _ -> fail typ
      in
      fun k ->
        Printf.ksprintf failwith "Unexpected enum value for %s: %s" name
          (to_string k)
    | Some f ->
      let to_int64 =
        match typ with
        | Ctypes_static.Primitive p ->
          (match p with
           | Ctypes_primitive_types.Int8_t -> Int64.of_int
           | Ctypes_primitive_types.Int32_t -> Int64.of_int32
           | Ctypes_primitive_types.Int16_t -> Int64.of_int
           | Ctypes_primitive_types.Int -> Int64.of_int
           | Ctypes_primitive_types.Int64_t -> identity
           | Ctypes_primitive_types.Uint8_t -> Unsigned.UInt8.to_int64
           | Ctypes_primitive_types.Uint16_t -> Unsigned.UInt16.to_int64
           | Ctypes_primitive_types.Uint32_t -> Unsigned.UInt32.to_int64
           | Ctypes_primitive_types.Uint64_t -> Unsigned.UInt64.to_int64
           | _ -> fail typ
            : a -> int64)
        | _ -> fail typ
      in
      fun k -> f (to_int64 k)
  in
  let pname = if typedef then name else "enum " ^ name in
  let write k = List.assoc k alist
  and read k = try List.assoc k rlist with Not_found -> unexpected k
  and format_typ k fmt = Format.fprintf fmt "%s%t" pname k in
  Ctypes_static.view ~format_typ ~read ~write typ

let build_enum_bitmask :
    type a b.
    string ->
    a Ctypes.typ ->
    typedef:bool ->
    ?unexpected:(b list -> int64 -> b list) ->
    (b * a) list ->
    b list Ctypes.typ =
 fun name typ ~typedef ?unexpected alist ->
  let fail t =
    Printf.ksprintf failwith "Invalid enum type %s" (Ctypes.string_of_typ t)
  in
  let lor', land', zero, lnot' =
    match typ with
    | Ctypes_static.Primitive p ->
      (match p with
       | Ctypes_primitive_types.Int8_t -> (( lor ), ( land ), 0, lnot)
       | Ctypes_primitive_types.Int16_t -> (( lor ), ( land ), 0, lnot)
       | Ctypes_primitive_types.Int -> (( lor ), ( land ), 0, lnot)
       | Ctypes_primitive_types.Int32_t -> Int32.(logor, logand, zero, lognot)
       | Ctypes_primitive_types.Int64_t -> Int64.(logor, logand, zero, lognot)
       | Ctypes_primitive_types.Uint8_t ->
         Unsigned.UInt8.(logor, logand, zero, lognot)
       | Ctypes_primitive_types.Uint16_t ->
         Unsigned.UInt16.(logor, logand, zero, lognot)
       | Ctypes_primitive_types.Uint32_t ->
         Unsigned.UInt32.(logor, logand, zero, lognot)
       | Ctypes_primitive_types.Uint64_t ->
         Unsigned.UInt64.(logor, logand, zero, lognot)
       | _ -> fail typ
        : (a -> a -> a) * (a -> a -> a) * a * (a -> a))
    | _ -> fail typ
  in
  let unexpected =
    match unexpected with
    | None ->
      let to_string =
        match typ with
        | Ctypes_static.Primitive p ->
          (match p with
           | Ctypes_primitive_types.Int8_t -> string_of_int
           | Ctypes_primitive_types.Int32_t -> (Int32.to_string : a -> string)
           | Ctypes_primitive_types.Int16_t -> string_of_int
           | Ctypes_primitive_types.Int -> string_of_int
           | Ctypes_primitive_types.Int64_t -> Int64.to_string
           | Ctypes_primitive_types.Uint8_t -> Unsigned.UInt8.to_string
           | Ctypes_primitive_types.Uint16_t -> Unsigned.UInt16.to_string
           | Ctypes_primitive_types.Uint32_t -> Unsigned.UInt32.to_string
           | Ctypes_primitive_types.Uint64_t -> Unsigned.UInt64.to_string
           | _ -> fail typ
            : a -> string)
        | _ -> fail typ
      in
      fun _ k ->
        Printf.ksprintf failwith "Unexpected enum value for %s: %s" name
          (to_string k)
    | Some f ->
      let to_int64 =
        match typ with
        | Ctypes_static.Primitive p ->
          (match p with
           | Ctypes_primitive_types.Int8_t -> Int64.of_int
           | Ctypes_primitive_types.Int32_t -> Int64.of_int32
           | Ctypes_primitive_types.Int16_t -> Int64.of_int
           | Ctypes_primitive_types.Int -> Int64.of_int
           | Ctypes_primitive_types.Int64_t -> identity
           | Ctypes_primitive_types.Uint8_t -> Unsigned.UInt8.to_int64
           | Ctypes_primitive_types.Uint16_t -> Unsigned.UInt16.to_int64
           | Ctypes_primitive_types.Uint32_t -> Unsigned.UInt32.to_int64
           | Ctypes_primitive_types.Uint64_t -> Unsigned.UInt64.to_int64
           | _ -> fail typ
            : a -> int64)
        | _ -> fail typ
      in
      fun a k -> f a (to_int64 k)
  in
  let pname = if typedef then name else "enum " ^ name in
  let ralist = List.rev alist in
  let (write : b list -> a) =
   fun l -> List.fold_left (fun ac k -> lor' (List.assoc k alist) ac) zero l
  and (read : a -> b list) =
   fun res ->
    let rec iter res_orig ac res l =
      match l with
      | [] -> if res = zero then ac else unexpected ac res
      | (a, b) :: tl ->
        if land' b res_orig = b then
          iter res_orig (a :: ac) (land' res (lnot' b)) tl
        else iter res_orig ac res tl
    in
    iter res [] res ralist
  and format_typ k fmt = Format.fprintf fmt "%s%t" pname k in
  Ctypes_static.view ~format_typ ~read ~write typ

external to_voidp : nativeint -> Cstubs_internals.voidp = "%identity"

let invalid_code () = failwith "ppx_cstub generated invalid code"

module Signed = struct
  module Nativeint = struct
    include struct
      [@@@ocaml.warning "-32"]

      let equal (x : nativeint) (y : nativeint) = x = y

      let pp fmt x = Format.fprintf fmt "%nd" x

      let pp_hex fmt n = Format.fprintf fmt "%nx" n
    end

    include Nativeint

    module Infix = struct
      let ( + ) = add

      let ( - ) = sub

      let ( * ) = mul

      let ( / ) = div

      let ( mod ) = rem

      let ( land ) = logand

      let ( lor ) = logor

      let ( lxor ) = logxor

      let ( lsl ) = shift_left

      let ( lsr ) = shift_right_logical

      [@@@ocaml.warning "-32"]

      let ( asr ) = shift_right
    end

    external of_nativeint : t -> t = "%identity"

    external to_nativeint : t -> t = "%identity"

    let of_int64 = Int64.to_nativeint

    let to_int64 = Int64.of_nativeint

    [@@@ocaml.warning "-32"]

    let max = max

    let min = min

    let of_string_opt x = try Some (of_string x) with Failure _ -> None

    let to_hexstring n = Format.asprintf "%nx" n
  end

  module type Int_size = sig
    val int_size : int
  end

  external format_int : string -> int -> string = "caml_format_int"

  module Short_int (X : Int_size) = struct
    open X

    type t = int

    let max_int = (1 lsl (int_size - 1)) - 1

    let min_int = (1 lsl (int_size - 1)) * -1

    let sfactor = Sys.word_size - 1 - int_size

    let of_int x = (x lsl sfactor) asr sfactor

    let add x y = of_int (x + y)

    let sub x y = of_int (x - y)

    let mul x y = of_int (x * y)

    let div x y = of_int (x / y)

    let rem x y = x mod y

    let logand x y = x land y

    let logor x y = x lor y

    let logxor x y = x lxor y

    let shift_left x y = of_int (x lsl y)

    let shift_right_logical x y =
      of_int ((x lsr y) land ((1 lsl (int_size - y)) - 1))

    let shift_right x y = of_int (x asr y)

    module Infix = struct
      let ( + ) = add

      let ( - ) = sub

      let ( * ) = mul

      let ( / ) = div

      let ( mod ) = rem

      let ( land ) = logand

      let ( lor ) = logor

      let ( lxor ) = logxor

      let ( lsl ) = shift_left

      let ( lsr ) = shift_right_logical

      [@@@ocaml.warning "-32"]

      let ( asr ) = shift_right
    end

    let lognot x = lnot x

    let compare = compare

    external to_int : t -> t = "%identity"

    let of_string x =
      let r = int_of_string x in
      if r < min_int || r > max_int then failwith "int_of_string";
      r

    let to_string = string_of_int

    let zero = 0

    let one = 1

    let minus_one = -1

    let succ x = of_int (succ x)

    let pred x = of_int (pred x)

    let to_int64 = Int64.of_int

    let of_int64 x = of_int (Int64.to_int x)

    let to_nativeint = Nativeint.of_int

    let of_nativeint x = of_int (Nativeint.to_int x)

    let abs x = of_int (abs x)

    let neg x = of_int (-x)

    [@@@ocaml.warning "-32"]

    let max = max

    let min = min

    let equal (x : t) (y : t) = x = y

    let of_string_opt x =
      match int_of_string x with
      | exception Failure _ -> None
      | r -> if r < min_int || r > max_int then None else Some r

    let pp fmt n = Format.fprintf fmt "%d" n

    let pp_hex fmt n = Format.fprintf fmt "%x" n

    let to_hexstring = format_int "%x"
  end

  module Int8 = Short_int (struct
    let int_size = 8
  end)

  module Int16 = Short_int (struct
    let int_size = 16
  end)

  module Int32 = Short_int (struct
    let int_size = 32
  end)

  module type Short = sig
    include Signed.S with type t = int
  end

  module Short = (val match Ctypes.sizeof Ctypes.short with
                      | 1 -> (module Int8)
                      | 2 -> (module Int16)
                      | 4 when Ctypes.sizeof Ctypes.int = 4 ->
                        if Sys.word_size = 64 then (module Int32)
                        else (module Signed.Int)
                      | _ -> failwith "invalid size of short" : Short)

  module Schar = Int8
end

module Callback = struct
  module type Info = sig
    type real

    val real : real Ctypes.fn
  end

  module Make (H : Info) : sig
    type fn = H.real

    type 'a t

    type raw_pointer

    val t : H.real t Ctypes.static_funptr Ctypes.typ

    val fn : H.real Ctypes.fn

    val make_pointer : raw_pointer -> H.real t Ctypes.static_funptr
  end = struct
    open H

    type fn = real

    type 'a t = real

    type raw_pointer = nativeint

    let t = Ctypes.static_funptr real

    let fn = H.real

    let make_pointer p = Cstubs_internals.make_fun_ptr H.real (to_voidp p)
  end

  let make (type a) (fn : a Ctypes.fn) =
    (module struct
      type real = a

      let real = fn
    end : Info
      with type real = a)
end

module Shadow = struct
  let rec passable : type a. a typ -> bool = function
    | Void -> true
    | Primitive _ -> true
    | Struct { spec = Incomplete _; _ } -> raise IncompleteType
    | Struct { spec = Complete _; _ } -> true
    | Union { uspec = None; _ } -> raise IncompleteType
    | Union { uspec = Some _; _ } -> true
    | Array _ -> false
    | Bigarray _ -> false
    | Pointer _ -> true
    | Funptr _ -> true
    (* Allow to pass and return abstract types. I don't know why it is
       disabled upstream. They are handled like structs and unions *)
    | Abstract _ -> true
    | OCaml _ -> true
    | View { ty; _ } -> passable ty

  let ( @-> ) a b =
    if not (passable a) then raise (Unsupported "Unsupported argument type")
    else Function (a, b)

  let returning a =
    if not (passable a) then raise (Unsupported "Unsupported return type")
    else Returns a
end

external obj_magic : 'a -> 'b = "%identity"