package codex

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file binary_collecting.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
(**************************************************************************)
(*  This file is part of the Codex semantics library.                     *)
(*                                                                        *)
(*  Copyright (C) 2013-2025                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file LICENSE).                      *)
(*                                                                        *)
(**************************************************************************)

let _name = "Collecting";;

include Sva_quadrivalent;;

module In_bits = Units.In_bits

(* Quadrivalent viewed as a set. *)
module Quadrivalent = struct
  include Boolean_Lattice;;

  let empty = Bottom

  let add bool x = match x,bool with
    | Top, _ -> Top
    | True, true -> True
    | True, false -> Top
    | False, true -> Top
    | False, false -> False
    | Bottom, false -> False
    | Bottom, true -> True
  ;;


end

module ZSet = Lattices.BVSet.ZSet;;
module Bitvector_Lattice = struct
  include Lattices.Unimplemented.Bitvector_Lattice(struct
      type t = Lattices.BVSet.t
      let loc = __LOC__
    end)
  include Lattices.BVSet;;
  let widen ~size ~previous x = join ~size previous x
  let is_bottom ~size x = x = ZSet.empty
end

module Concrete = Operator.Concrete.Bitvector_Interp

type bitvector = Bitvector_Lattice.t

[@@@ocaml.warning "-38"]
exception Empty = Operator.Concrete.Empty
[@@@ocaml.warning "+38"]

module Bitvector_Forward = struct

  let lift1 op a =
    ZSet.fold(fun a acc ->
        ZSet.add (op a) acc) a ZSet.empty
  ;;


  let lift2 op a b =
    ZSet.fold (fun a acc ->
        ZSet.fold(fun b acc ->
            match op a b with
            | exception Empty -> acc
            | r -> ZSet.add r acc) b acc) a ZSet.empty
  ;;

  let lift2_pred op a b =
    ZSet.fold (fun a acc ->
        ZSet.fold(fun b acc ->
            Quadrivalent.add (op a b) acc) b acc) a Quadrivalent.empty
  ;;

  let biconst ~size k = ZSet.singleton k

  let band ~size = lift2 (Z.(land));;
  let bor  ~size = lift2 (Z.(lor));;
  let bxor ~size = lift2 (Z.(lxor));;
  let biadd ~size ~flags = lift2 (Concrete.biadd ~size ~flags);;
  let bisub ~size ~flags = lift2 (Concrete.bisub ~size ~flags);;
  let bimul ~size ~flags = lift2 (Concrete.bimul ~flags ~size);;
  let bimul_add ~size ~prod ~offset = lift1 (fun x -> Z.(prod * x + offset))
  let bisdiv ~size = lift2 (Concrete.bisdiv ~size);;
  let biudiv ~size = lift2 (Concrete.biudiv ~size);;
  let bismod ~size = lift2 (Concrete.bismod ~size);;
  let biumod ~size = lift2 (Concrete.biumod ~size);;



  let buext ~size ~oldsize x = x

  let bsext ~size ~oldsize =
    let size = In_bits.to_int size and oldsize = In_bits.to_int oldsize in
    lift1 (fun x -> Z.extract (Z.signed_extract x 0 oldsize) 0 size)
  ;;

  let bshl ~size ~flags  = lift2 (Concrete.bshl ~size ~flags);;

  let blshr ~size = lift2 (Concrete.blshr ~size);;
  let bashr ~size = lift2 (Concrete.bashr ~size);;

  let bconcat ~size1 ~size2 = lift2 (Concrete.bconcat ~size1 ~size2);;

  let bextract ~size ~index ~oldsize =
    lift1 (Concrete.bextract ~index ~size ~oldsize);;
  ;;

  let beq ~size = lift2_pred (Z.equal);;
  let biule ~size = lift2_pred (Z.leq);;
  let bisle ~size = lift2_pred (Concrete.bisle ~size);;

  let _pretty fmt x =
    let l = ZSet.elements x |> List.map (Z.format "%b") in
    Format.fprintf fmt "[";
    (Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt ",") Format.pp_print_string) fmt l;
    Format.fprintf fmt "]"

  let bofbool ~size = function
    | Bottom -> ZSet.empty
    | True -> biconst ~size Z.one
    | False -> biconst ~size Z.zero
    | Top -> Bitvector_Lattice.join ~size (biconst ~size Z.zero) (biconst ~size Z.one)
;;


end

module Bitvector_Backward = struct
  include Sva_Assert_False_Transfer_Functions.Bitvector_Backward
end