pliron 0.14.0

Programming Languages Intermediate RepresentatiON
Documentation
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
//! Aribitrary precision integer implementation.
//! This is similar in functionality to LLVM's APInt class.

use crate::{arg_error_noloc, result::Result};
use awint::{Awi, SerdeError};
use std::num::NonZero;

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct APInt {
    value: Awi,
}

impl From<SerdeError> for crate::result::Error {
    fn from(value: SerdeError) -> Self {
        arg_error_noloc!("APInt error: {}", value)
    }
}

pub use awint::bw;

impl APInt {
    /// Get the bitwidth of the APInt.
    pub fn bw(&self) -> usize {
        self.value.bw()
    }

    /// Get zero valued APInt.
    pub fn zero(width: NonZero<usize>) -> APInt {
        APInt {
            value: Awi::zero(width),
        }
    }

    /// Is this value zero?
    pub fn is_zero(&self) -> bool {
        self.value.is_zero()
    }

    /// Get unsigned max value
    pub fn umax(width: NonZero<usize>) -> APInt {
        APInt {
            value: Awi::umax(width),
        }
    }

    /// Get signed max value
    pub fn imax(width: NonZero<usize>) -> APInt {
        APInt {
            value: Awi::imax(width),
        }
    }

    /// Get signed min value
    pub fn imin(width: NonZero<usize>) -> APInt {
        APInt {
            value: Awi::imin(width),
        }
    }

    /// Get unsigned one value
    pub fn uone(width: NonZero<usize>) -> APInt {
        APInt {
            value: Awi::uone(width),
        }
    }

    /// Parse a string into an APInt.
    pub fn from_str(value: &str, width: usize, radix: u8) -> Result<APInt> {
        let sign_opt = value.chars().next().ok_or(SerdeError::Empty)?;
        let neg = sign_opt == '-';
        let value = if neg || sign_opt == '+' {
            &value[1..]
        } else {
            value
        };

        let sign = if neg { Some(true) } else { None };
        let value = Awi::from_str_radix(
            sign,
            value,
            radix,
            NonZero::new(width).ok_or(SerdeError::ZeroBitwidth)?,
        )?;

        Ok(APInt { value })
    }

    /// Convert APInt to string, interpreting it as a signed or unsigned integer.
    pub fn to_string(&self, radix: u8, signed: bool) -> String {
        match Awi::bits_to_string_radix(&self.value, signed, radix, false, 1) {
            Ok(mut s) => {
                if signed && self.value.msb() {
                    s.insert(0, '-');
                }
                s
            }
            Err(e) => {
                panic!("APInt error: {e}");
            }
        }
    }

    /// Convert APInt to a decimal string
    pub fn to_string_decimal(&self, signed: bool) -> String {
        self.to_string(10, signed)
    }

    /// Convert APInt to string, interpreting it as a signed integer.
    pub fn to_string_signed(&self, radix: u8) -> String {
        self.to_string(radix, true)
    }

    /// Convert APInt to string, interpreting it as an unsigned integer.
    pub fn to_string_unsigned(&self, radix: u8) -> String {
        self.to_string(radix, false)
    }

    /// Convert APInt to decimal string, interpreting it as a signed integer.
    pub fn to_string_signed_decimal(&self) -> String {
        self.to_string_signed(10)
    }

    /// Convert APInt to decimal string, interpreting it as an unsigned integer.
    pub fn to_string_unsigned_decimal(&self) -> String {
        self.to_string_unsigned(10)
    }

    /// Build APInt from u8.
    /// Zero extends value if width > 8.
    /// Truncates value if width < 8.
    pub fn from_u8(value: u8, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.u8_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to u8.
    /// Truncates value if width > 8.
    /// Zero extends value if width < 8.
    pub fn to_u8(&self) -> u8 {
        self.value.to_u8()
    }

    /// Build APInt from u16.
    /// Zero extends value if width > 16.
    /// Truncates value if width < 16.
    pub fn from_u16(value: u16, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.u16_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to u16.
    /// Truncates value if width > 16.
    /// Zero extends value if width < 16.
    pub fn to_u16(&self) -> u16 {
        self.value.to_u16()
    }

    /// Build APInt from u32.
    /// Zero extends value if width > 32.
    /// Truncates value if width < 32.
    pub fn from_u32(value: u32, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.u32_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to u32.
    /// Truncates value if width > 32.
    /// Zero extends value if width < 32.
    pub fn to_u32(&self) -> u32 {
        self.value.to_u32()
    }

    /// Build APInt from u64.
    /// Zero extends value if width > 64.
    /// Truncates value if width < 64.
    pub fn from_u64(value: u64, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.u64_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to u64.
    /// Truncates value if width > 64.
    /// Zero extends value if width < 64.
    pub fn to_u64(&self) -> u64 {
        self.value.to_u64()
    }

    /// Build APInt from u128.
    /// Zero extends value if width > 128.
    /// Truncates value if width < 128.
    pub fn from_u128(value: u128, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.u128_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to u128.
    /// Truncates value if width > 128.
    /// Zero extends value if width < 128.
    pub fn to_u128(&self) -> u128 {
        self.value.to_u128()
    }

    /// Build APInt from i8.
    /// Sign extends value if width > 8.
    /// Truncates value if width < 8.
    pub fn from_i8(value: i8, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.i8_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to i8.
    /// Truncates value if width > 8.
    /// Sign extends value if width < 8.
    pub fn to_i8(&self) -> i8 {
        self.value.to_i8()
    }

    /// Build APInt from i16.
    /// Sign extends value if width > 16.
    /// Truncates value if width < 16.
    pub fn from_i16(value: i16, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.i16_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to i16.
    /// Truncates value if width > 16.
    /// Sign extends value if width < 16.
    pub fn to_i16(&self) -> i16 {
        self.value.to_i16()
    }

    /// Build APInt from i32.
    /// Sign extends value if width > 32.
    /// Truncates value if width < 32.
    pub fn from_i32(value: i32, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.i32_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to i32.
    /// Truncates value if width > 32.
    /// Sign extends value if width < 32.
    pub fn to_i32(&self) -> i32 {
        self.value.to_i32()
    }

    /// Build APInt from i64.
    /// Sign extends value if width > 64.
    /// Truncates value if width < 64.
    pub fn from_i64(value: i64, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.i64_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to i64.
    /// Truncates value if width > 64.
    /// Sign extends value if width < 64.
    pub fn to_i64(&self) -> i64 {
        self.value.to_i64()
    }

    /// Build APInt from i128.
    /// Sign extends value if width > 128.
    /// Truncates value if width < 128.
    pub fn from_i128(value: i128, width: NonZero<usize>) -> APInt {
        let mut awi_value = Awi::zero_with_capacity(width, width);
        awi_value.i128_(value);
        APInt { value: awi_value }
    }

    /// Convert APInt to i128.
    /// Truncates value if width > 128.
    /// Sign extends value if width < 128.
    pub fn to_i128(&self) -> i128 {
        self.value.to_i128()
    }
}
#[cfg(test)]
mod tests {
    use expect_test::expect;

    use super::*;

    #[test]
    fn test_zero() {
        let width = bw(4);
        let apint = APInt::zero(width);
        assert!(apint.is_zero());
    }

    #[test]
    fn test_limits() {
        let width = bw(4);

        let umax = APInt::umax(width);
        assert_eq!(umax.to_u8(), 15);
        assert_eq!(umax.to_i8(), -1);

        let imax = APInt::imax(width);
        assert_eq!(imax.to_i8(), 7);
        assert_eq!(imax.to_u8(), 7);

        let imin = APInt::imin(width);
        assert_eq!(imin.to_i8(), -8);
        assert_eq!(imin.to_u8(), 8);
    }

    #[test]
    fn test_from_str() {
        let width = 4;
        let apint = APInt::from_str("7", width, 10).unwrap();
        assert_eq!(apint.to_u8(), 7);

        let apint = APInt::from_str("-8", width, 10).unwrap();
        assert_eq!(apint.to_i8(), -8);
        assert_eq!(apint.to_string(10, true), "-8");

        let apint = APInt::from_str("+15", width, 10).unwrap();
        assert_eq!(apint.to_i8(), -1);
        assert_eq!(apint.to_u8(), 15);
        assert_eq!(apint.to_string(10, true), "-1");
        assert_eq!(apint.to_string(10, false), "15");

        let apint = APInt::from_str("-2", width, 10).unwrap();
        assert_eq!(apint.to_i8(), -2);
        assert_eq!(apint.to_u8(), 14);
        assert_eq!(apint.to_string(10, true), "-2");
        assert_eq!(apint.to_string(10, false), "14");
    }

    #[test]
    fn test_from_str_failure() {
        let width = 4;
        let result = APInt::from_str("invalid", width, 10);
        expect![[r#"
            Compilation error: invalid argument.
            APInt error: InvalidChar"#]]
        .assert_eq(&result.unwrap_err().to_string());
        let result = APInt::from_str("", width, 10);
        expect![[r#"
            Compilation error: invalid argument.
            APInt error: Empty"#]]
        .assert_eq(&result.unwrap_err().to_string());
        let result = APInt::from_str("16", width, 10);
        expect![[r#"
            Compilation error: invalid argument.
            APInt error: Overflow"#]]
        .assert_eq(&result.unwrap_err().to_string());
    }

    #[test]
    fn test_from_u8() {
        let width = bw(4);
        for i in 0..16 {
            let apint = APInt::from_u8(i, width);
            assert_eq!(apint.to_u8(), i);
        }
    }

    #[test]
    fn test_from_u16() {
        let width = bw(4);
        for i in 0..16 {
            let apint = APInt::from_u16(i, width);
            assert_eq!(apint.to_u16(), i);
        }
    }

    #[test]
    fn test_from_u32() {
        let width = bw(4);
        for i in 0..16 {
            let apint = APInt::from_u32(i, width);
            assert_eq!(apint.to_u32(), i);
        }
    }

    #[test]
    fn test_from_u64() {
        let width = bw(4);
        for i in 0..16 {
            let apint = APInt::from_u64(i, width);
            assert_eq!(apint.to_u64(), i);
        }
    }

    #[test]
    fn test_from_u128() {
        let width = bw(4);
        for i in 0..16 {
            let apint = APInt::from_u128(i, width);
            assert_eq!(apint.to_u128(), i);
        }
    }

    #[test]
    fn test_from_i8() {
        let width = bw(4);
        for i in -8..8 {
            let apint = APInt::from_i8(i, width);
            assert_eq!(apint.to_i8(), i);
        }
    }

    #[test]
    fn test_from_i16() {
        let width = bw(4);
        for i in -8..8 {
            let apint = APInt::from_i16(i, width);
            assert_eq!(apint.to_i16(), i);
        }
    }

    #[test]
    fn test_from_i32() {
        let width = bw(4);
        for i in -8..8 {
            let apint = APInt::from_i32(i, width);
            assert_eq!(apint.to_i32(), i);
        }
    }

    #[test]
    fn test_from_i64() {
        let width = bw(4);
        for i in -8..8 {
            let apint = APInt::from_i64(i, width);
            assert_eq!(apint.to_i64(), i);
        }
    }

    #[test]
    fn test_from_i128() {
        let width = bw(4);
        for i in -8..8 {
            let apint = APInt::from_i128(i, width);
            assert_eq!(apint.to_i128(), i);
        }
    }
}