Debian Codegraph @

kernel/nativevalues.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Util
12 open Names
13 open Values
14 open Constr
15
16 (** This module defines the representation of values internally used by
17 the native compiler *)
18
19 type ('a,'b) eq = ('a,'b) Util.eq = Refl : ('a,'a) eq
20
21 type t
22 let t_eq : (t, t -> t) eq = Obj.magic ()
23 let t_eq2 : (t, t -> t -> t) eq = Obj.magic ()
24 let t_eq3 : (t, t -> t -> t -> t) eq = Obj.magic ()
25 let t_eq4 : (t, t -> t -> t -> t -> t) eq = Obj.magic ()
26
27 let cast_gen (type a) (type b) (e:(a,b) eq) (x:a) : b =
28 let Refl = e in x
29
30 let apply (f:t) : t -> t =
31 cast_gen t_eq f
32
33 (* Writing this as [apply2 f x y = apply (apply f x) y] seems to
34 confuse ocaml and it produces less efficient code.
35
36 When written with a direct [cast_gen t_eq2] ocaml seems smart
37 enough to fully reduce it where used. *)
38 let apply2 (f:t) : t -> t -> t =
39 cast_gen t_eq2 f
40
41 let apply3 (f:t) : t -> t -> t -> t =
42 cast_gen t_eq3 f
43
44 let apply4 (f:t) : t -> t -> t -> t -> t =
45 cast_gen t_eq4 f
46
47 let of_fun (f:t->t) : t =
48 cast_gen (sym t_eq) f
49
50 let eta_expand f = of_fun (fun x -> apply f x)
51
52 type accumulator (* = t (* a block [0:code;atom;arguments] *) *)
53
54 type tag = int
55
56 type arity = int
57
58 type reloc_table = (tag * arity) array
59
60 type annot_sw = {
61 asw_ind : inductive;
62 asw_reloc : reloc_table;
63 asw_prefix : string
64 }
65
66 type sort_annot = string * int
67
68 type rec_pos = int array
69
70 let eq_rec_pos = Array.equal Int.equal
71
72 type vcofix = t Lazy.t
73
74 type atom =
75 | Arel of int
76 | Aconstant of pconstant
77 | Aind of pinductive
78 | Asort of Sorts.t
79 | Avar of Id.t
80 | Acase of annot_sw * accumulator * t * t
81 | Afix of t array * t array * rec_pos * int
82 (* types, bodies, rec_pos, pos *)
83 | Acofix of t array * t array * int * t array * vcofix
84 | Aevar of Evar.t * t array
85 | Aproj of (inductive * int) * accumulator
86
87 type symbol =
88 | SymbValue of t
89 | SymbSort of Sorts.t
90 | SymbName of Name.t
91 | SymbConst of Constant.t
92 | SymbMatch of annot_sw
93 | SymbInd of inductive
94 | SymbEvar of Evar.t
95 | SymbInstance of UVars.Instance.t
96 | SymbProj of (inductive * int)
97
98 type block
99
100 type symbols = symbol array
101
102 let empty_symbols = [| |]
103
104
105 let accumulate_tag = 0
106
107 (** Unique pointer used to drive the accumulator function *)
108 let ret_accu = Obj.repr (ref ())
109
110 type accu_val = { acc_atm : atom; acc_arg : t list }
111
112 (** Return a pointer to [caml_curry2_1] that is also recognized as an unscannable block *)
113 external get_curry2_1 : unit -> Obj.t = "rocq_curry2_1_addr"
114
115 type _ curry2_1_clos = Curry2_1 : Obj.t * int * 'a * ('a -> 'b -> 'c) -> ('b -> 'c) curry2_1_clos
116
117 let mk_accu =
118 let curry2_1 = get_curry2_1 () in
119 let rec accumulate data x =
120 if Obj.repr x == ret_accu then Obj.repr data
121 else
122 let data = { data with acc_arg = x :: data.acc_arg } in
123 let ans = Curry2_1 (curry2_1, 2, data, accumulate) in
124 Obj.repr ans in
125 fun (a : atom) ->
126 let data = { acc_atm = a; acc_arg = [] } in
127 let ans = Curry2_1 (curry2_1, 2, data, accumulate) in
128 (Obj.magic ans : t)
129
130 let get_accu (k : accumulator) =
131 (Obj.magic k : Obj.t -> accu_val) ret_accu
132
133 let mk_rel_accu i =
134 mk_accu (Arel i)
135
136 let rel_tbl_size = 100
137 let rel_tbl_init = ref false
138 (* Initialize the table lazily not to generate naked pointers at startup *)
139 let rel_tbl = Array.make rel_tbl_size (Obj.magic 0 : t)
140
141 let mk_rel_accu i =
142 if i < rel_tbl_size then
143 let () =
144 if not !rel_tbl_init then begin
145 for i = 0 to rel_tbl_size - 1 do
146 rel_tbl.(i) <- mk_rel_accu i
147 done;
148 rel_tbl_init := true
149 end
150 in
151 rel_tbl.(i)
152 else mk_rel_accu i
153
154 let mk_rels_accu lvl len =
155 Array.init len (fun i -> mk_rel_accu (lvl + i))
156
157 let napply (f:t) (args: t array) =
158 Array.fold_left (fun f a -> apply f a) f args
159
160 let mk_constant_accu kn u = mk_accu (Aconstant (kn,u))
161
162 let mk_ind_accu ind u = mk_accu (Aind (ind,u))
163
164 let mk_sort_accu s =
165 mk_accu (Asort s)
166
167 let mk_var_accu id =
168 mk_accu (Avar id)
169
170 let mk_sw_accu annot c p ac =
171 mk_accu (Acase(annot,c,p,ac))
172
173 let prod_tag =
174 (* We rely on the tag of Vprod! *)
175 let () = assert (Obj.tag (Obj.repr (Vprod (Anonymous, Obj.magic 0, Obj.magic 0))) == 2) in
176 2
177
178 let mk_prod s dom codom =
179 (* [Prod (s, dom, codom)] is coded as [tag:0|[tag:2|s; dom; codom]]
180 This looks like a PArray but has a tag distinct from all PArray values on
181 the inner block. This cannot be an accumulator because all accumulators
182 have length >= 2. *)
183 let block = Obj.repr (Vprod (s, dom, codom)) in
184 (Obj.magic (ref block) : t)
185
186 let mk_evar_accu ev args =
187 mk_accu (Aevar (ev, args))
188
189 let mk_proj_accu kn c =
190 mk_accu (Aproj (kn,c))
191
192 let atom_of_accu (k:accumulator) =
193 (get_accu k).acc_atm
194
195 let accu_nargs (k:accumulator) =
196 List.length (get_accu k).acc_arg
197
198 let args_of_accu (k:accumulator) =
199 (get_accu k).acc_arg
200
201 let mk_fix_accu rec_pos pos types bodies =
202 mk_accu (Afix(types,bodies,rec_pos, pos))
203
204 let mk_cofix_accu pos types norm cofix args =
205 mk_accu (Acofix (types, norm, pos, args, Lazy.from_fun cofix))
206
207 let force_cofix (cofix : t) =
208 let accu = (Obj.magic cofix : accumulator) in
209 let atom = atom_of_accu accu in
210 match atom with
211 | Acofix (_, _, _, _, v) ->
212 let () = assert (List.is_empty (args_of_accu accu)) in
213 Lazy.force v
214 | _ -> cofix
215
216 let mk_const tag = Obj.magic tag
217
218 let mk_block tag args =
219 let nargs = Array.length args in
220 let r = Obj.new_block tag nargs in
221 for i = 0 to nargs - 1 do
222 Obj.set_field r i (Obj.magic args.(i))
223 done;
224 (Obj.magic r : t)
225
226 let cast_accu v = (Obj.magic v:accumulator)
227 [@@ocaml.inline always]
228
229 let mk_int (x : int) = (Obj.magic x : t)
230 [@@ocaml.inline always]
231
232 (* Rocq's booleans are reversed... *)
233 let mk_bool (b : bool) = (Obj.magic (not b) : t)
234 [@@ocaml.inline always]
235
236 let mk_uint (x : Uint63.t) = (Obj.magic x : t)
237 [@@ocaml.inline always]
238
239 let mk_float (x : Float64.t) = (Obj.magic x : t)
240 [@@ocaml.inline always]
241
242 let mk_string (x : Pstring.t) = (Obj.magic x : t)
243 [@@ocaml.inline always]
244
245 let block_size (b:block) =
246 Obj.size (Obj.magic b)
247
248 let block_field (b:block) i = (Obj.magic (Obj.field (Obj.magic b) i) : t)
249
250 let block_tag (b:block) =
251 Obj.tag (Obj.magic b)
252
253 type kind = (t, accumulator, t -> t, Name.t * t * t, Empty.t, Empty.t, block) Values.kind
254
255 let kind_of_value (v:t) =
256 let o = Obj.repr v in
257 if Obj.is_int o then Vconst (Obj.magic v)
258 else
259 let tag = Obj.tag o in
260 if Int.equal tag accumulate_tag then
261 if Int.equal (Obj.size o) 1 then
262 let w = Obj.field o 0 in
263 let tag = Obj.tag w in
264 if Int.equal tag prod_tag then Obj.magic w
265 else Varray (Obj.magic v)
266 else Vaccu (Obj.magic v)
267 else if Int.equal tag Obj.custom_tag then Vint64 (Obj.magic v)
268 else if Int.equal tag Obj.double_tag then Vfloat64 (Obj.magic v)
269 else if Int.equal tag Obj.string_tag then Vstring (Obj.magic v)
270 else if (tag < Obj.lazy_tag) then Vblock (Obj.magic v)
271 else
272 (* assert (tag = Obj.closure_tag || tag = Obj.infix_tag);
273 or ??? what is 1002*)
274 Vfun (apply v)
275
276 (** Support for machine integers *)
277
278 let is_int (x:t) =
279 let o = Obj.repr x in
280 Obj.is_int o || Int.equal (Obj.tag o) Obj.custom_tag
281 [@@ocaml.inline always]
282
283 let val_to_int (x:t) = (Obj.magic x : int)
284 [@@ocaml.inline always]
285
286 let to_uint (x:t) = (Obj.magic x : Uint63.t)
287 [@@ocaml.inline always]
288
289 let no_check_head0 x =
290 mk_uint (Uint63.head0 (to_uint x))
291 [@@ocaml.inline always]
292
293 let head0 accu x =
294 if is_int x then no_check_head0 x
295 else apply accu x
296
297 let no_check_tail0 x =
298 mk_uint (Uint63.tail0 (to_uint x))
299 [@@ocaml.inline always]
300
301 let tail0 accu x =
302 if is_int x then no_check_tail0 x
303 else apply accu x
304
305 let no_check_add x y =
306 mk_uint (Uint63.add (to_uint x) (to_uint y))
307 [@@ocaml.inline always]
308
309 let add accu x y =
310 if is_int x && is_int y then no_check_add x y
311 else apply2 accu x y
312
313 let no_check_sub x y =
314 mk_uint (Uint63.sub (to_uint x) (to_uint y))
315 [@@ocaml.inline always]
316
317 let sub accu x y =
318 if is_int x && is_int y then no_check_sub x y
319 else apply2 accu x y
320
321 let no_check_mul x y =
322 mk_uint (Uint63.mul (to_uint x) (to_uint y))
323 [@@ocaml.inline always]
324
325 let mul accu x y =
326 if is_int x && is_int y then no_check_mul x y
327 else apply2 accu x y
328
329 let no_check_div x y =
330 mk_uint (Uint63.div (to_uint x) (to_uint y))
331 [@@ocaml.inline always]
332
333 let div accu x y =
334 if is_int x && is_int y then no_check_div x y
335 else apply2 accu x y
336
337 let no_check_rem x y =
338 mk_uint (Uint63.rem (to_uint x) (to_uint y))
339 [@@ocaml.inline always]
340
341 let rem accu x y =
342 if is_int x && is_int y then no_check_rem x y
343 else apply2 accu x y
344
345 let no_check_divs x y =
346 mk_uint (Uint63.divs (to_uint x) (to_uint y))
347 [@@ocaml.inline always]
348
349 let divs accu x y =
350 if is_int x && is_int y then no_check_divs x y
351 else apply2 accu x y
352
353 let no_check_rems x y =
354 mk_uint (Uint63.rems (to_uint x) (to_uint y))
355 [@@ocaml.inline always]
356
357 let rems accu x y =
358 if is_int x && is_int y then no_check_rems x y
359 else apply2 accu x y
360
361 let no_check_l_sr x y =
362 mk_uint (Uint63.l_sr (to_uint x) (to_uint y))
363 [@@ocaml.inline always]
364
365 let l_sr accu x y =
366 if is_int x && is_int y then no_check_l_sr x y
367 else apply2 accu x y
368
369 let no_check_l_sl x y =
370 mk_uint (Uint63.l_sl (to_uint x) (to_uint y))
371 [@@ocaml.inline always]
372
373 let l_sl accu x y =
374 if is_int x && is_int y then no_check_l_sl x y
375 else apply2 accu x y
376
377 let no_check_a_sr x y =
378 mk_uint (Uint63.a_sr (to_uint x) (to_uint y))
379 [@@ocaml.inline always]
380
381 let a_sr accu x y =
382 if is_int x && is_int y then no_check_a_sr x y
383 else apply2 accu x y
384
385 let no_check_l_and x y =
386 mk_uint (Uint63.l_and (to_uint x) (to_uint y))
387 [@@ocaml.inline always]
388
389 let l_and accu x y =
390 if is_int x && is_int y then no_check_l_and x y
391 else apply2 accu x y
392
393 let no_check_l_xor x y =
394 mk_uint (Uint63.l_xor (to_uint x) (to_uint y))
395 [@@ocaml.inline always]
396
397 let l_xor accu x y =
398 if is_int x && is_int y then no_check_l_xor x y
399 else apply2 accu x y
400
401 let no_check_l_or x y =
402 mk_uint (Uint63.l_or (to_uint x) (to_uint y))
403 [@@ocaml.inline always]
404
405 let l_or accu x y =
406 if is_int x && is_int y then no_check_l_or x y
407 else apply2 accu x y
408
409 [@@@ocaml.warning "-37"]
410 type rocq_carry =
411 | Caccu of t
412 | C0 of t
413 | C1 of t
414
415 type rocq_pair =
416 | Paccu of t
417 | PPair of t * t
418
419 let mkCarry b i =
420 if b then (Obj.magic (C1(mk_uint i)):t)
421 else (Obj.magic (C0(mk_uint i)):t)
422
423 let no_check_addc x y =
424 let s = Uint63.add (to_uint x) (to_uint y) in
425 mkCarry (Uint63.lt s (to_uint x)) s
426 [@@ocaml.inline always]
427
428 let addc accu x y =
429 if is_int x && is_int y then no_check_addc x y
430 else apply2 accu x y
431
432 let no_check_subc x y =
433 let s = Uint63.sub (to_uint x) (to_uint y) in
434 mkCarry (Uint63.lt (to_uint x) (to_uint y)) s
435 [@@ocaml.inline always]
436
437 let subc accu x y =
438 if is_int x && is_int y then no_check_subc x y
439 else apply2 accu x y
440
441 let no_check_addCarryC x y =
442 let s =
443 Uint63.add (Uint63.add (to_uint x) (to_uint y))
444 (Uint63.of_int 1) in
445 mkCarry (Uint63.le s (to_uint x)) s
446 [@@ocaml.inline always]
447
448 let addCarryC accu x y =
449 if is_int x && is_int y then no_check_addCarryC x y
450 else apply2 accu x y
451
452 let no_check_subCarryC x y =
453 let s =
454 Uint63.sub (Uint63.sub (to_uint x) (to_uint y))
455 (Uint63.of_int 1) in
456 mkCarry (Uint63.le (to_uint x) (to_uint y)) s
457 [@@ocaml.inline always]
458
459 let subCarryC accu x y =
460 if is_int x && is_int y then no_check_subCarryC x y
461 else apply2 accu x y
462
463 let of_pair (x, y) =
464 (Obj.magic (PPair(mk_uint x, mk_uint y)):t)
465 [@@ocaml.inline always]
466
467 let no_check_mulc x y =
468 of_pair (Uint63.mulc (to_uint x) (to_uint y))
469 [@@ocaml.inline always]
470
471 let mulc accu x y =
472 if is_int x && is_int y then no_check_mulc x y
473 else apply2 accu x y
474
475 let no_check_diveucl x y =
476 let i1, i2 = to_uint x, to_uint y in
477 of_pair(Uint63.div i1 i2, Uint63.rem i1 i2)
478 [@@ocaml.inline always]
479
480 let diveucl accu x y =
481 if is_int x && is_int y then no_check_diveucl x y
482 else apply2 accu x y
483
484 let no_check_div21 x y z =
485 let i1, i2, i3 = to_uint x, to_uint y, to_uint z in
486 of_pair (Uint63.div21 i1 i2 i3)
487 [@@ocaml.inline always]
488
489 let div21 accu x y z =
490 if is_int x && is_int y && is_int z then no_check_div21 x y z
491 else apply3 accu x y z
492
493 let no_check_addMulDiv x y z =
494 let p, i, j = to_uint x, to_uint y, to_uint z in
495 mk_uint (Uint63.addmuldiv p i j)
496 [@@ocaml.inline always]
497
498 let addMulDiv accu x y z =
499 if is_int x && is_int y && is_int z then no_check_addMulDiv x y z
500 else apply3 accu x y z
501
502 [@@@ocaml.warning "-34"]
503 type rocq_bool =
504 | Baccu of t
505 | Btrue
506 | Bfalse
507
508 type rocq_cmp =
509 | CmpAccu of t
510 | CmpEq
511 | CmpLt
512 | CmpGt
513
514 let no_check_eq x y =
515 mk_bool (Uint63.equal (to_uint x) (to_uint y))
516 [@@ocaml.inline always]
517
518 let eq accu x y =
519 if is_int x && is_int y then no_check_eq x y
520 else apply2 accu x y
521
522 let no_check_lt x y =
523 mk_bool (Uint63.lt (to_uint x) (to_uint y))
524 [@@ocaml.inline always]
525
526 let lt accu x y =
527 if is_int x && is_int y then no_check_lt x y
528 else apply2 accu x y
529
530 let no_check_le x y =
531 mk_bool (Uint63.le (to_uint x) (to_uint y))
532 [@@ocaml.inline always]
533
534 let le accu x y =
535 if is_int x && is_int y then no_check_le x y
536 else apply2 accu x y
537
538 let no_check_lts x y =
539 mk_bool (Uint63.lts (to_uint x) (to_uint y))
540 [@@ocaml.inline always]
541
542 let lts accu x y =
543 if is_int x && is_int y then no_check_lts x y
544 else apply2 accu x y
545
546 let no_check_les x y =
547 mk_bool (Uint63.les (to_uint x) (to_uint y))
548 [@@ocaml.inline always]
549
550 let les accu x y =
551 if is_int x && is_int y then no_check_les x y
552 else apply2 accu x y
553
554 let no_check_compare x y =
555 match Uint63.compare (to_uint x) (to_uint y) with
556 | x when x < 0 -> (Obj.magic CmpLt:t)
557 | 0 -> (Obj.magic CmpEq:t)
558 | _ -> (Obj.magic CmpGt:t)
559
560 let compare accu x y =
561 if is_int x && is_int y then no_check_compare x y
562 else apply2 accu x y
563
564 let no_check_compares x y =
565 match Uint63.compares (to_uint x) (to_uint y) with
566 | x when x < 0 -> (Obj.magic CmpLt:t)
567 | 0 -> (Obj.magic CmpEq:t)
568 | _ -> (Obj.magic CmpGt:t)
569
570 let compares accu x y =
571 if is_int x && is_int y then no_check_compares x y
572 else apply2 accu x y
573
574 let print x =
575 Printf.fprintf stderr "%s" (Uint63.to_string (to_uint x));
576 flush stderr;
577 x
578
579 (** Support for machine floating point values *)
580
581 external is_float : t -> bool = "rocq_is_double"
582 [@@noalloc]
583
584 let to_float (x:t) = (Obj.magic x : Float64.t)
585 [@@ocaml.inline always]
586
587 let no_check_fopp x =
588 mk_float (Float64.opp (to_float x))
589 [@@ocaml.inline always]
590
591 let fopp accu x =
592 if is_float x then no_check_fopp x
593 else apply accu x
594
595 let no_check_fabs x =
596 mk_float (Float64.abs (to_float x))
597 [@@ocaml.inline always]
598
599 let fabs accu x =
600 if is_float x then no_check_fabs x
601 else apply accu x
602
603 let no_check_feq x y =
604 mk_bool (Float64.eq (to_float x) (to_float y))
605 [@@ocaml.inline always]
606
607 let feq accu x y =
608 if is_float x && is_float y then no_check_feq x y
609 else apply2 accu x y
610
611 let no_check_flt x y =
612 mk_bool (Float64.lt (to_float x) (to_float y))
613 [@@ocaml.inline always]
614
615 let flt accu x y =
616 if is_float x && is_float y then no_check_flt x y
617 else apply2 accu x y
618
619 let no_check_fle x y =
620 mk_bool (Float64.le (to_float x) (to_float y))
621 [@@ocaml.inline always]
622
623 let fle accu x y =
624 if is_float x && is_float y then no_check_fle x y
625 else apply2 accu x y
626
627 type rocq_fcmp =
628 | CFcmpAccu of t
629 | CFcmpEq
630 | CFcmpLt
631 | CFcmpGt
632 | CFcmpNotComparable
633
634 let no_check_fcompare x y =
635 let c = Float64.compare (to_float x) (to_float y) in
636 (Obj.magic c:t)
637 [@@ocaml.inline always]
638
639 let fcompare accu x y =
640 if is_float x && is_float y then no_check_fcompare x y
641 else apply2 accu x y
642
643 let no_check_fequal x y =
644 mk_bool (Float64.equal (to_float x) (to_float y))
645 [@@ocaml.inline always]
646
647 let fequal accu x y =
648 if is_float x && is_float y then no_check_fequal x y
649 else apply2 accu x y
650
651 type rocq_fclass =
652 | CFclassAccu of t
653 | CFclassPNormal
654 | CFclassNNormal
655 | CFclassPSubn
656 | CFclassNSubn
657 | CFclassPZero
658 | CFclassNZero
659 | CFclassPInf
660 | CFclassNInf
661 | CFclassNaN
662
663 let no_check_fclassify x =
664 let c = Float64.classify (to_float x) in
665 (Obj.magic c:t)
666 [@@ocaml.inline always]
667
668 let fclassify accu x =
669 if is_float x then no_check_fclassify x
670 else apply accu x
671
672 let no_check_fadd x y =
673 mk_float (Float64.add (to_float x) (to_float y))
674 [@@ocaml.inline always]
675
676 let fadd accu x y =
677 if is_float x && is_float y then no_check_fadd x y
678 else apply2 accu x y
679
680 let no_check_fsub x y =
681 mk_float (Float64.sub (to_float x) (to_float y))
682 [@@ocaml.inline always]
683
684 let fsub accu x y =
685 if is_float x && is_float y then no_check_fsub x y
686 else apply2 accu x y
687
688 let no_check_fmul x y =
689 mk_float (Float64.mul (to_float x) (to_float y))
690 [@@ocaml.inline always]
691
692 let fmul accu x y =
693 if is_float x && is_float y then no_check_fmul x y
694 else apply2 accu x y
695
696 let no_check_fdiv x y =
697 mk_float (Float64.div (to_float x) (to_float y))
698 [@@ocaml.inline always]
699
700 let fdiv accu x y =
701 if is_float x && is_float y then no_check_fdiv x y
702 else apply2 accu x y
703
704 let no_check_fsqrt x =
705 mk_float (Float64.sqrt (to_float x))
706 [@@ocaml.inline always]
707
708 let fsqrt accu x =
709 if is_float x then no_check_fsqrt x
710 else apply accu x
711
712 let no_check_float_of_int x =
713 mk_float (Float64.of_uint63 (to_uint x))
714 [@@ocaml.inline always]
715
716 let float_of_int accu x =
717 if is_int x then no_check_float_of_int x
718 else apply accu x
719
720 let no_check_normfr_mantissa x =
721 mk_uint (Float64.normfr_mantissa (to_float x))
722 [@@ocaml.inline always]
723
724 let normfr_mantissa accu x =
725 if is_float x then no_check_normfr_mantissa x
726 else apply accu x
727
728 let no_check_frshiftexp x =
729 let f, e = Float64.frshiftexp (to_float x) in
730 (Obj.magic (PPair(mk_float f, mk_uint e)):t)
731 [@@ocaml.inline always]
732
733 let frshiftexp accu x =
734 if is_float x then no_check_frshiftexp x
735 else apply accu x
736
737 let no_check_ldshiftexp x e =
738 mk_float (Float64.ldshiftexp (to_float x) (to_uint e))
739 [@@ocaml.inline always]
740
741 let ldshiftexp accu x e =
742 if is_float x && is_int e then no_check_ldshiftexp x e
743 else apply2 accu x e
744
745 let no_check_next_up x =
746 mk_float (Float64.next_up (to_float x))
747 [@@ocaml.inline always]
748
749 let next_up accu x =
750 if is_float x then no_check_next_up x
751 else apply accu x
752
753 let no_check_next_down x =
754 mk_float (Float64.next_down (to_float x))
755 [@@ocaml.inline always]
756
757 let next_down accu x =
758 if is_float x then no_check_next_down x
759 else apply accu x
760
761 (** Support for primitive strings *)
762
763 let is_string (x:t) =
764 let o = Obj.repr x in
765 Int.equal (Obj.tag o) Obj.string_tag
766
767 let to_string (x:t) = (Obj.magic x : Pstring.t)
768 [@@ocaml.inline always]
769
770 let no_check_string_make n c =
771 mk_string (Pstring.make (to_uint n) (to_uint c))
772 [@@ocaml.inline always]
773
774 let no_check_string_length s =
775 mk_uint (Pstring.length (to_string s))
776 [@@ocaml.inline always]
777
778 let no_check_string_get s i =
779 mk_uint (Pstring.get (to_string s) (to_uint i))
780 [@@ocaml.inline always]
781
782 let no_check_string_sub s off len =
783 mk_string (Pstring.sub (to_string s) (to_uint off) (to_uint len))
784 [@@ocaml.inline always]
785
786 let no_check_string_cat s1 s2 =
787 mk_string (Pstring.cat (to_string s1) (to_string s2))
788 [@@ocaml.inline always]
789
790 let no_check_string_compare s1 s2 =
791 match Pstring.compare (to_string s1) (to_string s2) with
792 | x when x < 0 -> (Obj.magic CmpLt:t)
793 | 0 -> (Obj.magic CmpEq:t)
794 | _ -> (Obj.magic CmpGt:t)
795
796 let string_make accu n c =
797 if is_int n && is_int c then
798 no_check_string_make n c
799 else
800 apply2 accu n c
801
802 let string_length accu s =
803 if is_string s then
804 no_check_string_length s
805 else
806 apply accu s
807
808 let string_get accu s i =
809 if is_string s && is_int i then
810 no_check_string_get s i
811 else
812 apply2 accu s i
813
814 let string_sub accu s off len =
815 if is_string s && is_int off && is_int len then
816 no_check_string_sub s off len
817 else
818 apply3 accu s off len
819
820 let string_cat accu s1 s2 =
821 if is_string s1 && is_string s2 then
822 no_check_string_cat s1 s2
823 else
824 apply2 accu s1 s2
825
826 let string_compare accu s1 s2 =
827 if is_string s1 && is_string s2 then
828 no_check_string_compare s1 s2
829 else
830 apply2 accu s1 s2
831
832 (** Support for primitive arrays *)
833
834 let is_parray t =
835 (* This is only used over values known to inhabit an array type, so we just
836 have to discriminate between actual arrays and accumulators. The latter
837 are always closures with the tag set to 0, so they have size >= 2. *)
838 let t = Obj.magic t in
839 Obj.is_block t && Obj.size t = 1
840
841 let to_parray t = Obj.magic t
842 let of_parray t = Obj.magic t
843
844 let no_check_arraymake n def =
845 of_parray (Parray.make (to_uint n) def)
846 [@@ocaml.inline always]
847
848 let arraymake accu vA n def =
849 if is_int n then
850 no_check_arraymake n def
851 else apply3 accu vA n def
852
853 let no_check_arrayget t n =
854 Parray.get (to_parray t) (to_uint n)
855 [@@ocaml.inline always]
856
857 let arrayget accu vA t n =
858 if is_parray t && is_int n then
859 no_check_arrayget t n
860 else apply3 accu vA t n
861
862 let no_check_arraydefault t =
863 Parray.default (to_parray t)
864 [@@ocaml.inline always]
865
866 let arraydefault accu vA t =
867 if is_parray t then
868 no_check_arraydefault t
869 else apply2 accu vA t
870
871 let no_check_arrayset t n v =
872 of_parray (Parray.set (to_parray t) (to_uint n) v)
873 [@@ocaml.inline always]
874
875 let arrayset accu vA t n v =
876 if is_parray t && is_int n then
877 no_check_arrayset t n v
878 else apply4 accu vA t n v
879
880 let no_check_arraycopy t =
881 of_parray (Parray.copy (to_parray t))
882 [@@ocaml.inline always]
883
884 let arraycopy accu vA t =
885 if is_parray t then
886 no_check_arraycopy t
887 else apply2 accu vA t
888
889 let no_check_arraylength t =
890 mk_uint (Parray.length (to_parray t))
891 [@@ocaml.inline always]
892
893 let arraylength accu vA t =
894 if is_parray t then
895 no_check_arraylength t
896 else apply2 accu vA t
897
898 let parray_of_array t def =
899 (Obj.magic (Parray.unsafe_of_obj (Obj.repr t) def) : t)
900
901 let hobcnv = Array.init 256 (fun i -> Printf.sprintf "%02x" i)
902 let bohcnv = Array.init 256 (fun i -> i -
903 (if 0x30 <= i then 0x30 else 0) -
904 (if 0x41 <= i then 0x7 else 0) -
905 (if 0x61 <= i then 0x20 else 0))
906
907 let hex_of_bin ch = hobcnv.(int_of_char ch)
908 let bin_of_hex s = char_of_int (bohcnv.(int_of_char s.[0]) * 16 + bohcnv.(int_of_char s.[1]))
909
910 let str_encode expr =
911 let mshl_expr = Marshal.to_string expr [] in
912 let payload = Buffer.create (String.length mshl_expr * 2) in
913 String.iter (fun c -> Buffer.add_string payload (hex_of_bin c)) mshl_expr;
914 Buffer.contents payload
915
916 let str_decode s =
917 let mshl_expr_len = String.length s / 2 in
918 let mshl_expr = Buffer.create mshl_expr_len in
919 let buf = Bytes.create 2 in
920 for i = 0 to mshl_expr_len - 1 do
921 Bytes.blit_string s (2*i) buf 0 2;
922 Buffer.add_char mshl_expr (bin_of_hex (Bytes.to_string buf))
923 done;
924 Marshal.from_bytes (Buffer.to_bytes mshl_expr) 0