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
// Copyright 2023-2024 The Regents of the University of California
// released under BSD 3-Clause License
// author: Kevin Laeufer <laeufer@berkeley.edu>
//
// Borrowed bit-vector and array values.

use crate::{
    ArrayMutOps, ArrayOps, ArrayValue, BitVecMutOps, BitVecOps, BitVecValue, WidthInt, Word,
};

/// Bit-vector value that does not own its storage.
#[derive(Clone, Copy)]
pub struct BitVecValueRef<'a> {
    pub(crate) width: WidthInt,
    pub(crate) words: &'a [Word],
}

impl<'a> BitVecValueRef<'a> {
    pub(crate) fn new(width: WidthInt, words: &'a [Word]) -> Self {
        Self { width, words }
    }
}

impl<'a> From<&'a BitVecValue> for BitVecValueRef<'a> {
    fn from(value: &'a BitVecValue) -> Self {
        Self::new(value.width, value.words.as_ref())
    }
}

impl<'a> std::fmt::Debug for BitVecValueRef<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BitVecValueRef({})", self.to_bit_str())
    }
}

pub struct BitVecValueMutRef<'a> {
    pub(crate) width: WidthInt,
    pub(crate) words: &'a mut [Word],
}

impl<'a> BitVecValueMutRef<'a> {
    pub(crate) fn new(width: WidthInt, words: &'a mut [Word]) -> Self {
        Self { width, words }
    }
}

impl<'a> From<&'a mut BitVecValue> for BitVecValueMutRef<'a> {
    fn from(value: &'a mut BitVecValue) -> Self {
        Self::new(value.width, value.words.as_mut())
    }
}
impl<'a> std::fmt::Debug for BitVecValueMutRef<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BitVecValueMutRef({})", self.to_bit_str())
    }
}

impl<'a> BitVecOps for BitVecValueRef<'a> {
    fn width(&self) -> WidthInt {
        self.width
    }

    fn words(&self) -> &[Word] {
        self.words
    }
}

impl<'a> BitVecOps for BitVecValueMutRef<'a> {
    fn width(&self) -> WidthInt {
        self.width
    }

    fn words(&self) -> &[Word] {
        self.words
    }
}

impl<'a> BitVecMutOps for BitVecValueMutRef<'a> {
    fn words_mut(&mut self) -> &mut [Word] {
        self.words
    }
}

/// Bit-vector array value that does not own its storage.
#[derive(Clone)]
pub struct ArrayValueRef<'a> {
    pub(crate) index_width: WidthInt,
    pub(crate) data_width: WidthInt,
    pub(crate) words: &'a [Word],
}

impl<'a> ArrayValueRef<'a> {
    pub(crate) fn new(index_width: WidthInt, data_width: WidthInt, words: &'a [Word]) -> Self {
        Self {
            index_width,
            data_width,
            words,
        }
    }
}

impl<'a> From<&'a ArrayValue> for ArrayValueRef<'a> {
    fn from(value: &'a ArrayValue) -> Self {
        Self::new(value.index_width, value.index_width, value.words.as_ref())
    }
}

impl<'a> std::fmt::Debug for ArrayValueRef<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ArrayValueRef(bv<{}> -> bv<{}>)",
            self.index_width, self.data_width
        )
    }
}

impl<'a> ArrayOps for ArrayValueRef<'a> {
    fn index_width(&self) -> WidthInt {
        self.index_width
    }

    fn data_width(&self) -> WidthInt {
        self.data_width
    }

    fn words(&self) -> &[Word] {
        self.words
    }
}

/// Mutable bit-vector array value that does not own its storage.
pub struct ArrayValueMutRef<'a> {
    pub(crate) index_width: WidthInt,
    pub(crate) data_width: WidthInt,
    pub(crate) words: &'a mut [Word],
}

impl<'a> ArrayValueMutRef<'a> {
    pub(crate) fn new(index_width: WidthInt, data_width: WidthInt, words: &'a mut [Word]) -> Self {
        Self {
            index_width,
            data_width,
            words,
        }
    }
}

impl<'a> From<&'a mut ArrayValue> for ArrayValueMutRef<'a> {
    fn from(value: &'a mut ArrayValue) -> Self {
        Self::new(
            value.index_width,
            value.index_width,
            value.words.as_mut_slice(),
        )
    }
}

impl<'a> std::fmt::Debug for ArrayValueMutRef<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ArrayValueMutRef(bv<{}> -> bv<{}>)",
            self.index_width, self.data_width
        )
    }
}

impl<'a> ArrayOps for ArrayValueMutRef<'a> {
    fn index_width(&self) -> WidthInt {
        self.index_width
    }

    fn data_width(&self) -> WidthInt {
        self.data_width
    }

    fn words(&self) -> &[Word] {
        self.words
    }
}

impl<'a> ArrayMutOps for ArrayValueMutRef<'a> {
    fn words_mut(&mut self) -> &mut [Word] {
        self.words
    }
}