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
//! The Tydi High-level Intermediate Representation.

pub mod component;
pub mod identifier;
pub mod net;
pub mod r#type;

pub use crate::{
    component::{Component, Instance},
    identifier::Identifier,
    net::{Connection, InstancePort, Mode, Net, Port, Wire},
    r#type::*,
};
use std::sync::Arc;
use tydi_intern::Id;

/// The root node of a hardware design description.
// TODO: When we allow importing external projects, we need to change this.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Project {
    /// The modules within a project.
    pub modules: Vec<Id<Module>>,
}

/// A collection of components, constants and types.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Module {
    /// The identifier of the module.
    pub identifier: Id<Identifier>,
    /// The components that this module describes.
    pub components: Vec<Id<Component>>,
    /// The types that this module describes.
    pub types: Vec<Id<Type>>,
    // TODO: constants
    // TODO: nested modules
}

impl Module {
    /// Create an empty module.
    pub fn new(identifier: Id<Identifier>) -> Self {
        Self {
            identifier,
            components: vec![],
            types: vec![],
        }
    }

    /// Append a component to the module.
    pub fn with_component(mut self, comp: Id<Component>) -> Self {
        self.components.push(comp);
        self
    }

    /// Append a type to the module.
    pub fn with_type(mut self, typ: Id<Type>) -> Self {
        self.types.push(typ);
        self
    }
}

#[salsa::query_group(HirStorage)]
pub trait Hir {
    #[salsa::input]
    fn project(&self) -> Arc<Project>;

    #[salsa::interned]
    fn intern_module(&self, modules: Module) -> Id<Module>;
    #[salsa::interned]
    fn intern_component(&self, component: Component) -> Id<Component>;
    #[salsa::interned]
    fn intern_instance(&self, instance: Instance) -> Id<Instance>;
    #[salsa::interned]
    fn intern_identifier(&self, identifier: Identifier) -> Id<Identifier>;
    #[salsa::interned]
    fn intern_port(&self, port: Port) -> Id<Port>;
    #[salsa::interned]
    fn intern_net(&self, net: Net) -> Id<Net>;
    #[salsa::interned]
    fn intern_instance_port(&self, instance_port: InstancePort) -> Id<InstancePort>;
    #[salsa::interned]
    fn intern_connection(&self, connection: Connection) -> Id<Connection>;
    #[salsa::interned]
    fn intern_type(&self, typ: Type) -> Id<Type>;
    #[salsa::interned]
    fn intern_logical_type(&self, typ: LogicalType) -> Id<LogicalType>;
    #[salsa::interned]
    fn intern_field(&self, field: Field) -> Id<Field>;

    /// Obtain all modules.
    fn modules(&self) -> Arc<Vec<Id<Module>>>;

    /// Obtain all components from a modules.
    fn components(&self, modules: Id<Module>) -> Arc<Vec<Id<Component>>>;

    /// Obtain all instances of a component.
    fn instances(&self, component: Id<Component>) -> Arc<Vec<Id<Instance>>>;

    /// Obtain all nets of a component.
    fn nets(&self, component: Id<Component>) -> Arc<Vec<Net>>;

    /// Obtain all Port nets of a component.
    fn ports(&self, component: Id<Component>) -> Arc<Vec<Port>>;

    /// Obtain all Wire nets of a component.
    fn wires(&self, component: Id<Component>) -> Arc<Vec<Wire>>;

    /// Obtain all InstancePort nets of a component.
    fn instance_ports(&self, component: Id<Component>) -> Arc<Vec<InstancePort>>;

    /// Get the identifier of a modules.
    fn module_identifier(&self, modules: Id<Module>) -> Arc<Identifier>;

    /// Get the identifier of a component.
    fn component_identifier(&self, component: Id<Component>) -> Arc<Identifier>;

    /// Get the identifier of a port.
    fn port_identifier(&self, component: Id<Port>) -> Arc<Identifier>;

    /// Get the identifier of an instance.
    fn instance_identifier(&self, component: Id<Instance>) -> Arc<Identifier>;
}

fn modules(db: &dyn Hir) -> Arc<Vec<Id<Module>>> {
    Arc::new(db.project().modules.clone())
}

fn components(db: &dyn Hir, module: Id<Module>) -> Arc<Vec<Id<Component>>> {
    Arc::new(db.lookup_intern_module(module).components)
}

fn instances(db: &dyn Hir, component: Id<Component>) -> Arc<Vec<Id<Instance>>> {
    Arc::new(db.lookup_intern_component(component).instances)
}

fn nets(db: &dyn Hir, component: Id<Component>) -> Arc<Vec<Net>> {
    Arc::new(
        db.lookup_intern_component(component)
            .nets
            .iter()
            .map(|&nid| db.lookup_intern_net(nid))
            .collect(),
    )
}

fn ports(db: &dyn Hir, component: Id<Component>) -> Arc<Vec<Port>> {
    Arc::new(
        db.nets(component)
            .iter()
            .filter_map(|net| match *net {
                Net::Port(p) => Some(db.lookup_intern_port(p)),
                _ => None,
            })
            .collect(),
    )
}

fn wires(db: &dyn Hir, component: Id<Component>) -> Arc<Vec<Wire>> {
    Arc::new(
        db.nets(component)
            .iter()
            .filter_map(|net| match *net {
                Net::Wire(w) => Some(w),
                _ => None,
            })
            .collect(),
    )
}

fn instance_ports(db: &dyn Hir, component: Id<Component>) -> Arc<Vec<InstancePort>> {
    Arc::new(
        db.nets(component)
            .iter()
            .filter_map(|net| match *net {
                Net::InstancePort(i) => Some(i),
                _ => None,
            })
            .collect(),
    )
}

// TODO: make a macro for everything that has an identifier:
fn module_identifier(db: &dyn Hir, modules: Id<Module>) -> Arc<Identifier> {
    Arc::new(db.lookup_intern_identifier(db.lookup_intern_module(modules).identifier))
}

fn component_identifier(db: &dyn Hir, component: Id<Component>) -> Arc<Identifier> {
    Arc::new(db.lookup_intern_identifier(db.lookup_intern_component(component).identifier))
}

fn port_identifier(db: &dyn Hir, port: Id<Port>) -> Arc<Identifier> {
    Arc::new(db.lookup_intern_identifier(db.lookup_intern_port(port).identifier))
}

fn instance_identifier(db: &dyn Hir, instance: Id<Instance>) -> Arc<Identifier> {
    Arc::new(db.lookup_intern_identifier(db.lookup_intern_instance(instance).identifier))
}