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
// Copyright (c) 2016-2021 Fabian Schuiki

//! Lvalue expressions
//!
//! An MIR representation for all expressions that may appear on the left-hand
//! side of an assignment.

use crate::crate_prelude::*;
use crate::{
    mir::{
        print::{Context, Print},
        rvalue::Rvalue,
        visit::{AcceptVisitor, Visitor, WalkVisitor},
    },
    ty::UnpackedType,
    ParamEnv,
};
use std::fmt::Write;

/// An lvalue expression.
#[moore_derive::visit_without_foreach]
#[derive(Clone, Eq, PartialEq)]
pub struct Lvalue<'a> {
    /// A unique id.
    pub id: NodeId,
    /// The expression node which spawned this lvalue.
    pub origin: NodeId,
    /// The environment within which the lvalue lives.
    pub env: ParamEnv,
    /// The span in the source file where the lvalue originates from.
    pub span: Span,
    /// The type of the expression.
    pub ty: &'a UnpackedType<'a>,
    /// The expression data.
    pub kind: LvalueKind<'a>,
}

impl<'a> Lvalue<'a> {
    /// Check whether the lvalue represents a lowering error tombstone.
    pub fn is_error(&self) -> bool {
        self.ty.is_error() || self.kind.is_error()
    }

    /// Get the `Intf` nested within `Index`, if one exists.
    pub fn get_intf(&self) -> Option<NodeId> {
        match self.kind {
            mir::LvalueKind::Index { value, .. } => value.get_intf(),
            mir::LvalueKind::Intf(intf) => Some(intf),
            _ => None,
        }
    }
}

impl<'a> std::fmt::Debug for Lvalue<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.print(f)
    }
}

impl<'a> Print for Lvalue<'a> {
    fn print_context(
        &self,
        outer: &mut impl Write,
        inner: &mut impl Write,
        ctx: &mut Context,
    ) -> std::fmt::Result {
        write!(inner, "Lvalue ")?;
        match self.kind {
            LvalueKind::Transmute(v) => write!(inner, "Transmute({})", ctx.print(outer, v))?,
            LvalueKind::DestructArray(ref args) => write!(
                inner,
                "DestructArray({})",
                ctx.print_comma_separated(outer, args),
            )?,
            LvalueKind::DestructStruct(ref args) => write!(
                inner,
                "DestructStruct({})",
                ctx.print_comma_separated(outer, args),
            )?,
            LvalueKind::Genvar(arg) => write!(inner, "Genvar({:?})", arg)?,
            LvalueKind::Var(arg) => write!(inner, "Var({:?})", arg)?,
            LvalueKind::Port(arg) => write!(inner, "Port({:?})", arg)?,
            LvalueKind::Intf(arg) => write!(inner, "Intf({:?})", arg)?,
            LvalueKind::IntfSignal(arg, sig) => {
                write!(inner, "IntfSignal({}, {:?})", ctx.print(outer, arg), sig)?
            }
            LvalueKind::Index {
                value,
                base,
                length,
            } => {
                if length == 0 {
                    write!(
                        inner,
                        "{}[{}]",
                        ctx.print(outer, value),
                        ctx.print(outer, base),
                    )?
                } else {
                    write!(
                        inner,
                        "{}[{}+:{}]",
                        ctx.print(outer, value),
                        ctx.print(outer, base),
                        length,
                    )?
                }
            }
            LvalueKind::Member { value, field } => {
                write!(inner, "{}.{}", ctx.print(outer, value), field)?
            }
            LvalueKind::Concat(ref args) => {
                write!(inner, "Concat({})", ctx.print_comma_separated(outer, args))?
            }
            LvalueKind::Repeat(num, arg) => {
                write!(inner, "Repeat({} x {})", num, ctx.print(outer, arg))?
            }
            LvalueKind::Error => write!(inner, "<error>")?,
        }
        write!(inner, " : {}", self.ty)?;
        Ok(())
    }
}

/// The different forms an lvalue expression may take.
#[moore_derive::visit_without_foreach]
#[derive(Debug, Clone, Eq, PartialEq)]
#[allow(missing_docs)]
pub enum LvalueKind<'a> {
    /// A type cast which does not incur any operation. For example, going from
    /// `bit [31:0]` to `int`, or vice versa.
    Transmute(&'a Lvalue<'a>),
    /// Destructor for an array.
    DestructArray(Vec<&'a Lvalue<'a>>),
    /// Destructor for a struct.
    DestructStruct(Vec<&'a Lvalue<'a>>),
    /// A reference to a genvar declaration.
    Genvar(NodeId),
    /// A reference to a variable declaration.
    Var(NodeId),
    /// A reference to a port declaration.
    Port(NodeId),
    /// A reference to an interface.
    Intf(NodeId),
    /// A reference to an interface's signal.
    IntfSignal(&'a Lvalue<'a>, NodeId),
    /// A bit- or part-select.
    Index {
        value: &'a Lvalue<'a>,
        base: &'a Rvalue<'a>,
        length: usize,
    },
    /// A struct field access.
    Member { value: &'a Lvalue<'a>, field: usize },
    /// Concatenate multiple values.
    ///
    /// The values are cast to and treated as packed bit vectors, and the result
    /// is yet another packed bit vector. The lowest index corresponds to the
    /// left-most item in the concatenation, which is at the MSB end of the
    /// final packed bit vector.
    Concat(Vec<&'a Lvalue<'a>>),
    /// Repeat a value multiple times.
    ///
    /// The value is cast to and treated as a packed bit vector, and the result
    /// is yet another packed bit vector.
    Repeat(usize, &'a Lvalue<'a>),
    /// An error occurred during lowering.
    Error,
}

impl<'a> LvalueKind<'a> {
    /// Check whether the lvalue represents a lowering error tombstone.
    pub fn is_error(&self) -> bool {
        match self {
            LvalueKind::Error => true,
            _ => false,
        }
    }
}