llhd 0.16.0

A Low Level Hardware Description that acts as a foundation for building hardware design tools.
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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright (c) 2017-2021 Fabian Schuiki

//! Value computation
//!
//! This module implements representations for LLHD values as they evolve during
//! the simulation of a design.

use llhd::ir::Opcode;
use num::{bigint::ToBigInt, BigInt, BigUint, One, Signed, ToPrimitive, Zero};
use std::fmt::{Debug, Display};

/// A point in time.
pub type Time = llhd::value::TimeValue;

/// A point in time.
pub type TimeValue = llhd::value::TimeValue;

/// A value.
#[derive(Clone, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum Value {
    Void,
    Time(Time),
    Int(IntValue),
    Array(ArrayValue),
    Struct(StructValue),
}

impl Value {
    /// If this value is a time, access it.
    pub fn get_time(&self) -> Option<&TimeValue> {
        match self {
            Value::Time(v) => Some(v),
            _ => None,
        }
    }

    /// Unwrap this value as a time, or panic.
    pub fn unwrap_time(&self) -> &TimeValue {
        self.get_time().expect("value is not a time")
    }

    /// If this value is an integer, access it.
    pub fn get_int(&self) -> Option<&IntValue> {
        match self {
            Value::Int(v) => Some(v),
            _ => None,
        }
    }

    /// Unwrap this value as an integer, or panic.
    pub fn unwrap_int(&self) -> &IntValue {
        self.get_int().expect("value is not an integer")
    }

    /// If this value is an array, access it.
    pub fn get_array(&self) -> Option<&ArrayValue> {
        match self {
            Value::Array(v) => Some(v),
            _ => None,
        }
    }

    /// Unwrap this value as an array, or panic.
    pub fn unwrap_array(&self) -> &ArrayValue {
        self.get_array().expect("value is not an array")
    }

    /// If this value is a struct, access it.
    pub fn get_struct(&self) -> Option<&StructValue> {
        match self {
            Value::Struct(v) => Some(v),
            _ => None,
        }
    }

    /// Unwrap this value as a struct, or panic.
    pub fn unwrap_struct(&self) -> &StructValue {
        self.get_struct().expect("value is not a struct")
    }

    /// Check if the value is zero.
    pub fn is_zero(&self) -> bool {
        match self {
            Value::Array(..) | Value::Struct(..) | Value::Void => false,
            Value::Time(v) => v.time().is_zero() && v.delta() == 0 && v.epsilon() == 0,
            Value::Int(v) => v.is_zero(),
        }
    }

    /// Check if the value is one.
    pub fn is_one(&self) -> bool {
        match self {
            Value::Array(..) | Value::Struct(..) | Value::Void => false,
            Value::Time(_) => false,
            Value::Int(v) => v.is_one(),
        }
    }
}

impl From<TimeValue> for Value {
    fn from(v: TimeValue) -> Value {
        Value::Time(v)
    }
}

impl From<IntValue> for Value {
    fn from(v: IntValue) -> Value {
        Value::Int(v)
    }
}

impl From<ArrayValue> for Value {
    fn from(v: ArrayValue) -> Value {
        Value::Array(v)
    }
}

impl From<StructValue> for Value {
    fn from(v: StructValue) -> Value {
        Value::Struct(v)
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Value::Void => write!(f, "void"),
            Value::Time(v) => write!(f, "time {}", v),
            Value::Int(v) => Display::fmt(v, f),
            Value::Array(v) => Display::fmt(v, f),
            Value::Struct(v) => Display::fmt(v, f),
        }
    }
}

impl Debug for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

/// An integer value.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct IntValue {
    /// The width of the value in bits.
    pub width: usize,
    /// The value itself.
    pub value: BigUint,
}

impl IntValue {
    /// Create a new integer value from a `usize`.
    pub fn from_usize(width: usize, value: usize) -> Self {
        Self {
            width,
            value: value.into(),
        }
    }

    /// Create a new integer value from a signed `BigInt` value.
    pub fn from_signed(width: usize, value: BigInt) -> Self {
        let modulus = BigInt::one() << width;
        let mut v = value % &modulus;
        if v.is_negative() {
            v += modulus;
        }
        assert!(!v.is_negative());
        Self::from_unsigned(width, v.to_biguint().unwrap())
    }

    /// Create a new integer value from an unsigned `BigUint` value.
    pub fn from_unsigned(width: usize, value: BigUint) -> Self {
        let value = value % (BigUint::one() << width);
        Self { width, value }
    }

    /// Convert the value to a signed `BigInt`.
    pub fn to_signed(&self) -> BigInt {
        let sign_mask = BigUint::one() << (self.width - 1);
        if (&self.value & &sign_mask).is_zero() {
            self.value.to_bigint().unwrap()
        } else {
            (BigInt::one() << self.width) - self.value.to_bigint().unwrap()
        }
    }

    /// Convert the value to a usize.
    pub fn to_usize(&self) -> usize {
        self.value.to_usize().unwrap()
    }

    /// Check if the value is zero.
    pub fn is_zero(&self) -> bool {
        self.value.is_zero()
    }

    /// Check if the value is one.
    pub fn is_one(&self) -> bool {
        self.value.is_one()
    }
}

impl Display for IntValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "i{} {}", self.width, self.value)
    }
}

impl Debug for IntValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

/// Slicing.
impl IntValue {
    /// Extract a slice of bits from the value.
    pub fn extract_slice(&self, off: usize, len: usize) -> IntValue {
        let shifted = self.value.clone() >> off;
        let modulus = BigUint::one() << len;
        IntValue::from_unsigned(len, shifted % modulus)
    }

    /// Insert a slice of bits into the value.
    pub fn insert_slice(&mut self, off: usize, len: usize, value: &IntValue) {
        assert_eq!(len, value.width);
        let mask = ((BigUint::one() << len) - BigUint::one()) << off;
        let mask_inv = ((BigUint::one() << self.width) - BigUint::one()) ^ mask;
        self.value &= mask_inv;
        self.value |= &value.value << off;
    }
}

/// Unary operators.
impl IntValue {
    /// Compute `not`.
    pub fn not(&self) -> IntValue {
        let max = (BigUint::one() << self.width) - BigUint::one();
        let v = &max - &self.value;
        IntValue::from_unsigned(self.width, v)
    }

    /// Compute `neg`.
    pub fn neg(&self) -> IntValue {
        let max = BigUint::one() << self.width;
        let v = &max - &self.value;
        IntValue::from_unsigned(self.width, v)
    }
}

/// Binary operators.
impl IntValue {
    /// Compute `add`.
    pub fn add(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value + &other.value)
    }

    /// Compute `sub`.
    pub fn sub(&self, other: &Self) -> IntValue {
        IntValue::from_signed(self.width, self.to_signed() - other.to_signed())
    }

    /// Compute `and`.
    pub fn and(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value & &other.value)
    }

    /// Compute `or`.
    pub fn or(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value | &other.value)
    }

    /// Compute `xor`.
    pub fn xor(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value ^ &other.value)
    }

    /// Compute `umul`.
    pub fn umul(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value * &other.value)
    }

    /// Compute `udiv`.
    pub fn udiv(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value / &other.value)
    }

    /// Compute `umod`.
    pub fn umod(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value % &other.value)
    }

    /// Compute `urem`.
    pub fn urem(&self, other: &Self) -> IntValue {
        IntValue::from_unsigned(self.width, &self.value % &other.value)
    }

    /// Compute `smul`.
    pub fn smul(&self, other: &Self) -> IntValue {
        IntValue::from_signed(self.width, self.to_signed() * other.to_signed())
    }

    /// Compute `sdiv`.
    pub fn sdiv(&self, other: &Self) -> IntValue {
        IntValue::from_signed(self.width, self.to_signed() / other.to_signed())
    }

    /// Compute `smod`.
    pub fn smod(&self, other: &Self) -> IntValue {
        IntValue::from_signed(self.width, self.to_signed() % other.to_signed())
    }

    /// Compute `srem`.
    pub fn srem(&self, other: &Self) -> IntValue {
        IntValue::from_signed(self.width, self.to_signed() % other.to_signed())
    }
}

/// Comparisons.
impl IntValue {
    /// Compute `==`.
    pub fn eq(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.value == other.value
    }

    /// Compute `!=`.
    pub fn neq(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.value != other.value
    }

    /// Compute unsigned `<`.
    pub fn ult(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.value < other.value
    }

    /// Compute unsigned `>`.
    pub fn ugt(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.value > other.value
    }

    /// Compute unsigned `<=`.
    pub fn ule(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.value <= other.value
    }

    /// Compute unsigned `>=`.
    pub fn uge(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.value >= other.value
    }

    /// Compute signed `<`.
    pub fn slt(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.to_signed() < other.to_signed()
    }

    /// Compute signed `>`.
    pub fn sgt(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.to_signed() > other.to_signed()
    }

    /// Compute signed `<=`.
    pub fn sle(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.to_signed() <= other.to_signed()
    }

    /// Compute signed `>=`.
    pub fn sge(&self, other: &Self) -> bool {
        assert_eq!(self.width, other.width);
        self.to_signed() >= other.to_signed()
    }
}

/// Opcode implementations.
impl IntValue {
    /// Execute a unary opcode.
    pub fn unary_op(op: Opcode, arg: &IntValue) -> IntValue {
        // trace!("{} ({}, {})", op, lhs, rhs);
        match op {
            Opcode::Not => arg.not(),
            Opcode::Neg => arg.neg(),
            _ => panic!("{} is not a unary op", op),
        }
    }

    /// Execute a binary opcode.
    pub fn binary_op(op: Opcode, lhs: &IntValue, rhs: &IntValue) -> IntValue {
        trace!("{} ({}, {})", op, lhs, rhs);
        match op {
            Opcode::Add => lhs.add(rhs),
            Opcode::Sub => lhs.sub(rhs),
            Opcode::And => lhs.and(rhs),
            Opcode::Or => lhs.or(rhs),
            Opcode::Xor => lhs.xor(rhs),
            Opcode::Smul => lhs.smul(rhs),
            Opcode::Sdiv => lhs.sdiv(rhs),
            Opcode::Smod => lhs.smod(rhs),
            Opcode::Srem => lhs.srem(rhs),
            Opcode::Umul => lhs.umul(rhs),
            Opcode::Udiv => lhs.udiv(rhs),
            Opcode::Umod => lhs.umod(rhs),
            Opcode::Urem => lhs.urem(rhs),
            _ => panic!("{} is not a binary op", op),
        }
    }

    /// Execute a comparison opcode.
    pub fn compare_op(op: Opcode, lhs: &IntValue, rhs: &IntValue) -> IntValue {
        // trace!("{} ({}, {})", op, lhs, rhs);
        let v = match op {
            Opcode::Eq => lhs.eq(rhs),
            Opcode::Neq => lhs.neq(rhs),
            Opcode::Ult => lhs.ult(rhs),
            Opcode::Ugt => lhs.ugt(rhs),
            Opcode::Ule => lhs.ule(rhs),
            Opcode::Uge => lhs.uge(rhs),
            Opcode::Slt => lhs.slt(rhs),
            Opcode::Sgt => lhs.sgt(rhs),
            Opcode::Sle => lhs.sle(rhs),
            Opcode::Sge => lhs.sge(rhs),
            _ => panic!("{} is not a compare op", op),
        };
        IntValue::from_usize(1, v as usize)
    }
}

/// An array value.
#[derive(Clone, PartialEq, Eq)]
pub struct ArrayValue(pub Vec<Value>);

impl ArrayValue {
    /// Create a new uniform array.
    pub fn new_uniform(length: usize, value: Value) -> Self {
        ArrayValue(std::iter::repeat(value).take(length).collect())
    }

    /// Create a new array.
    pub fn new(values: Vec<Value>) -> Self {
        ArrayValue(values)
    }
}

impl Display for ArrayValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let mut first = true;
        write!(f, "[")?;
        for v in &self.0 {
            if !first {
                write!(f, ", ")?;
            }
            write!(f, "{}", v)?;
            first = false;
        }
        write!(f, "]")
    }
}

impl Debug for ArrayValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

/// Slicing.
impl ArrayValue {
    /// Extract a single element from the array.
    pub fn extract_field(&self, idx: usize) -> Value {
        self.0[idx].clone()
    }

    /// Extract a slice of elements from the array.
    pub fn extract_slice(&self, off: usize, len: usize) -> ArrayValue {
        ArrayValue::new(self.0[off..off + len].to_vec())
    }

    /// Insert a single element into the array.
    pub fn insert_field(&mut self, idx: usize, value: Value) {
        self.0[idx] = value;
    }

    /// Insert a slice of elements into the array.
    pub fn insert_slice(&mut self, off: usize, len: usize, value: &ArrayValue) {
        assert_eq!(len, value.0.len());
        self.0[off..off + len].clone_from_slice(&value.0);
    }
}

/// A struct value.
#[derive(Clone, PartialEq, Eq)]
pub struct StructValue(pub Vec<Value>);

impl StructValue {
    /// Create a new struct.
    pub fn new(values: Vec<Value>) -> Self {
        StructValue(values)
    }
}

impl Display for StructValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let mut first = true;
        write!(f, "{{")?;
        for v in &self.0 {
            if !first {
                write!(f, ", ")?;
            }
            write!(f, "{}", v)?;
            first = false;
        }
        write!(f, "}}")
    }
}

impl Debug for StructValue {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

/// Slicing.
impl StructValue {
    /// Extract a single field from the struct.
    pub fn extract_field(&self, idx: usize) -> Value {
        self.0[idx].clone()
    }

    /// Insert a field into the struct.
    pub fn insert_field(&mut self, idx: usize, value: Value) {
        self.0[idx] = value;
    }
}