surfer_translation_types/variable_index.rs
1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5/// An index that defines the extents of a variable.
6/// For instance, in Verilog, this is declared using `[msb:lsb]`, e.g.:
7/// ```verilog
8/// reg [WIDTH-1:0] foo;
9/// ```
10///
11/// A negative `lsb` usually indicates a fixed-point value where the
12/// `[msb:0]` bits (including 0) belong to the integer part and the `[-1:lsb]` bits
13/// belong to the fractional part of a number.
14#[derive(Clone, Debug, Copy, Eq, PartialEq, Serialize, Deserialize)]
15pub struct VariableIndex {
16 pub msb: i64,
17 pub lsb: i64,
18}
19
20impl Display for VariableIndex {
21 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22 if self.msb == self.lsb {
23 write!(f, "[{}]", self.lsb)
24 } else {
25 write!(f, "[{}:{}]", self.msb, self.lsb)
26 }
27 }
28}