2022-01-23 20:04:19 +00:00
|
|
|
/// Alias for &TypeStruct to reduce repetition
|
|
|
|
/// and make futura migration to interning
|
|
|
|
/// easier
|
|
|
|
pub type Type<'ty> = &'ty TypeStruct<'ty>;
|
|
|
|
|
|
|
|
pub struct TypeStruct<'ty> {
|
|
|
|
kind: TypeKind<'ty>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TypeKind<'ty> {
|
|
|
|
/// Elaboration-time types
|
|
|
|
ElabType(ElabKind),
|
|
|
|
/// Signal/Wire of generic width
|
|
|
|
Logic(ElabData<'ty>),
|
|
|
|
/// UInt of generic width
|
|
|
|
UInt(ElabData<'ty>),
|
|
|
|
/// Callable
|
|
|
|
Callable,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ElabData<'ty> {
|
|
|
|
typ: Type<'ty>,
|
|
|
|
value: ElabValue<'ty>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ElabValue<'ty> {
|
|
|
|
/// the value is not given and has to be inferred
|
|
|
|
Infer,
|
|
|
|
/// the value is given as some byte representation
|
|
|
|
Concrete(ElabValueData<'ty>),
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ElabValueData<'ty> {
|
|
|
|
U32(u32),
|
|
|
|
Bytes(&'ty [u8]),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Types that are only valid during Elaboration
|
|
|
|
enum ElabKind {
|
|
|
|
/// general, unsized number type
|
2022-01-23 21:52:06 +00:00
|
|
|
Num,
|
2022-01-23 20:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper functions to create primitive types
|
|
|
|
impl<'ty> TypeStruct<'ty> {
|
|
|
|
/// a logic signal with inferred width
|
|
|
|
pub fn logic_infer() -> Self {
|
|
|
|
Self {
|
|
|
|
kind: TypeKind::Logic(ElabData {
|
|
|
|
typ: &TypeStruct {
|
2022-01-23 21:52:06 +00:00
|
|
|
kind: TypeKind::ElabType(ElabKind::Num),
|
2022-01-23 20:04:19 +00:00
|
|
|
},
|
|
|
|
value: ElabValue::Infer,
|
2022-01-23 21:52:06 +00:00
|
|
|
}),
|
2022-01-23 20:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// a logic signal with known width
|
|
|
|
pub fn logic_width(width: u32) -> Self {
|
|
|
|
Self {
|
2022-01-23 21:52:06 +00:00
|
|
|
kind: TypeKind::Logic(ElabData::u32(width)),
|
2022-01-23 20:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return an elaboration number type
|
|
|
|
pub fn elab_num() -> Self {
|
|
|
|
Self {
|
2022-01-23 21:52:06 +00:00
|
|
|
kind: TypeKind::ElabType(ElabKind::Num),
|
2022-01-23 20:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper functions to create primitive elaboration values
|
|
|
|
impl<'ty> ElabData<'ty> {
|
|
|
|
/// an integer
|
|
|
|
pub fn u32(val: u32) -> Self {
|
|
|
|
Self {
|
|
|
|
typ: &TypeStruct {
|
2022-01-23 21:52:06 +00:00
|
|
|
kind: TypeKind::ElabType(ElabKind::Num),
|
2022-01-23 20:04:19 +00:00
|
|
|
},
|
2022-01-23 21:52:06 +00:00
|
|
|
value: ElabValue::Concrete(ElabValueData::U32(val)),
|
2022-01-23 20:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|