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
// Copyright (c) 2017-2020 Fabian Schuiki

//! Types of values.

use itertools::Itertools;
use std::sync::Arc;

pub use self::TypeKind::*;

/// An LLHD type.
pub type Type = Arc<TypeKind>;

/// The different kinds of types.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TypeKind {
    /// The `void` type.
    VoidType,
    /// The `time` type.
    TimeType,
    /// Integer types like `i32`.
    IntType(usize),
    /// Enumerated types like `n42`.
    EnumType(usize),
    /// Pointer types like `i32*`.
    PointerType(Type),
    /// Signal types like `i32$`.
    SignalType(Type),
    /// Array types like `[4 x i32]`.
    ArrayType(usize, Type),
    /// Struct types like `{i8, i32}`.
    StructType(Vec<Type>),
    /// Function types like `(i32) void`.
    FuncType(Vec<Type>, Type),
    /// Entity types like `(i8, i8; i32)`.
    EntityType(Vec<Type>, Vec<Type>),
}

impl std::fmt::Display for TypeKind {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            VoidType => write!(f, "void"),
            TimeType => write!(f, "time"),
            IntType(l) => write!(f, "i{}", l),
            EnumType(l) => write!(f, "n{}", l),
            PointerType(ref ty) => write!(f, "{}*", ty),
            SignalType(ref ty) => write!(f, "{}$", ty),
            ArrayType(l, ref ty) => write!(f, "[{} x {}]", l, ty),
            StructType(ref tys) => write!(f, "{{{}}}", tys.iter().format(", ")),
            FuncType(ref args, ref ret) => write!(f, "({}) {}", args.iter().format(", "), ret),
            EntityType(ref ins, ref outs) => write!(
                f,
                "({}) -> ({})",
                ins.iter().format(", "),
                outs.iter().format(", ")
            ),
        }
    }
}

impl TypeKind {
    /// Unwrap the type to its integer bit width, or panic if the type is not an
    /// integer.
    pub fn unwrap_int(&self) -> usize {
        match *self {
            IntType(size) => size,
            _ => panic!("unwrap_int called on {}", self),
        }
    }

    /// Unwrap the type to its number of enumerated states, or panic if the type
    /// is not an enum.
    pub fn unwrap_enum(&self) -> usize {
        match *self {
            EnumType(size) => size,
            _ => panic!("unwrap_enum called on {}", self),
        }
    }

    /// Unwrap the type to its pointer data type, or panic if the type is not a
    /// pointer. E.g. yields the `i8` type in `i8*`.
    pub fn unwrap_pointer(&self) -> &Type {
        match *self {
            PointerType(ref ty) => ty,
            _ => panic!("unwrap_pointer called on {}", self),
        }
    }

    /// Unwrap the type to its signal data type, or panic if the type is not an
    /// integer. E.g. yields the `i8` type in `i8$`.
    pub fn unwrap_signal(&self) -> &Type {
        match *self {
            SignalType(ref ty) => ty,
            _ => panic!("unwrap_signal called on {}", self),
        }
    }

    /// Unwrap the type to its array length and element type, or panic if the
    /// type is not an array. E.g. yields the `(16, i32)` in `[16 x i32]`.
    pub fn unwrap_array(&self) -> (usize, &Type) {
        match *self {
            ArrayType(len, ref ty) => (len, ty),
            _ => panic!("unwrap_array called on {}", self),
        }
    }

    /// Unwrap the type to its struct fields, or panic if the type is not a
    /// struct. E.g. yields the `[i8, i16]` in `{i8, i16}`.
    pub fn unwrap_struct(&self) -> &[Type] {
        match *self {
            StructType(ref fields) => fields,
            _ => panic!("unwrap_struct called on {}", self),
        }
    }

    /// Unwrap the type into arguments and return type, or panic if the type is
    /// not a function.
    pub fn unwrap_func(&self) -> (&[Type], &Type) {
        match *self {
            FuncType(ref args, ref ret) => (args, ret),
            _ => panic!("unwrap_func called on {}", self),
        }
    }

    /// Unwrap the type into input and output arguments, or panic if the type is
    /// not an entity.
    pub fn unwrap_entity(&self) -> (&[Type], &[Type]) {
        match *self {
            EntityType(ref ins, ref outs) => (ins, outs),
            _ => panic!("unwrap_entity called on {}", self),
        }
    }

    /// Check if this is a void type.
    pub fn is_void(&self) -> bool {
        match *self {
            VoidType => true,
            _ => false,
        }
    }

    /// Check if this is a time type.
    pub fn is_time(&self) -> bool {
        match *self {
            TimeType => true,
            _ => false,
        }
    }

    /// Check if this is an integer type.
    pub fn is_int(&self) -> bool {
        match *self {
            IntType(..) => true,
            _ => false,
        }
    }

    /// Check if this is an enum type.
    pub fn is_enum(&self) -> bool {
        match *self {
            EnumType(..) => true,
            _ => false,
        }
    }

    /// Check if this is a pointer type.
    pub fn is_pointer(&self) -> bool {
        match *self {
            PointerType(..) => true,
            _ => false,
        }
    }

    /// Check if this is a signal type.
    pub fn is_signal(&self) -> bool {
        match *self {
            SignalType(..) => true,
            _ => false,
        }
    }

    /// Check if this is an array type.
    pub fn is_array(&self) -> bool {
        match *self {
            ArrayType(..) => true,
            _ => false,
        }
    }

    /// Check if this is a struct type.
    pub fn is_struct(&self) -> bool {
        match *self {
            StructType(..) => true,
            _ => false,
        }
    }

    /// Check if this is a func type.
    pub fn is_func(&self) -> bool {
        match *self {
            FuncType(..) => true,
            _ => false,
        }
    }

    /// Check if this is an entity type.
    pub fn is_entity(&self) -> bool {
        match *self {
            EntityType(..) => true,
            _ => false,
        }
    }

    /// Extract the length of the type.
    ///
    /// This is the number of:
    /// - bits in an integer
    /// - states in an enum
    /// - elements in an array
    /// - fields in a struct
    ///
    /// Returns zero for all other types.
    pub fn len(&self) -> usize {
        match *self {
            IntType(l) | EnumType(l) | ArrayType(l, _) => l,
            StructType(ref f) => f.len(),
            _ => 0,
        }
    }
}

/// Create a void type.
pub fn void_ty() -> Type {
    Type::new(VoidType)
}

/// Create a time type.
pub fn time_ty() -> Type {
    Type::new(TimeType)
}

/// Create an integer type of the requested size.
pub fn int_ty(size: usize) -> Type {
    Type::new(IntType(size))
}

/// Create an enum type of the requested size.
pub fn enum_ty(size: usize) -> Type {
    Type::new(EnumType(size))
}

/// Create a pointer type with the requested data type.
pub fn pointer_ty(ty: Type) -> Type {
    Type::new(PointerType(ty))
}

/// Create a signal type with the requested data type.
pub fn signal_ty(ty: Type) -> Type {
    Type::new(SignalType(ty))
}

/// Create a array type. `size` is the number of elements in the array, and `ty`
/// the type of each individual element.
pub fn array_ty(size: usize, ty: Type) -> Type {
    Type::new(ArrayType(size, ty))
}

/// Create a struct type. `fields` is an list of types, one for each field.
pub fn struct_ty(fields: Vec<Type>) -> Type {
    Type::new(StructType(fields))
}

/// Create a function type with the given arguments and return type.
pub fn func_ty(args: Vec<Type>, ret: Type) -> Type {
    Type::new(FuncType(args, ret))
}

/// Create an entity type with the given input and output arguments.
pub fn entity_ty(ins: Vec<Type>, outs: Vec<Type>) -> Type {
    Type::new(EntityType(ins, outs))
}