futilehdl/src/frontend/types.rs

185 lines
4.9 KiB
Rust
Raw Normal View History

2022-02-15 20:32:55 +00:00
use std::fmt::Debug;
/// Alias for &TypeStruct to reduce repetition
/// and make futura migration to interning
/// easier
2022-02-15 20:32:55 +00:00
pub type Type = InternedType;
2022-02-15 22:56:52 +00:00
#[derive(Copy, Clone, PartialEq)]
2022-02-15 20:32:55 +00:00
pub struct InternedType(usize);
2022-02-15 22:56:52 +00:00
impl Debug for InternedType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Type({})", self.0)
}
}
#[derive(Debug)]
2022-02-15 20:32:55 +00:00
pub struct TypeStruct {
kind: TypeKind,
}
2022-02-06 22:19:55 +00:00
#[derive(Debug)]
2022-02-15 20:32:55 +00:00
enum TypeKind {
/// Elaboration-time types
ElabType(ElabKind),
/// Signal/Wire of generic width
2022-02-15 20:32:55 +00:00
Logic(ElabData),
/// UInt of generic width
2022-02-15 20:32:55 +00:00
UInt(ElabData),
/// Callable
2022-02-16 21:17:25 +00:00
Callable(FnSig),
2022-02-15 22:56:52 +00:00
/// A type that was not given and needs to be inferred
Infer,
}
2022-02-16 21:17:25 +00:00
#[derive(Debug, Clone)]
pub struct FnSig {
params: Vec<Type>,
ret: Type,
}
2022-02-16 15:37:12 +00:00
#[derive(Debug, Clone)]
2022-02-15 22:56:52 +00:00
pub struct ElabData {
2022-02-15 20:32:55 +00:00
typ: Type,
value: ElabValue,
}
2022-02-16 15:37:12 +00:00
#[derive(Debug, Clone)]
2022-02-15 20:32:55 +00:00
enum ElabValue {
/// the value is not given and has to be inferred
Infer,
/// the value is given as some byte representation
2022-02-15 20:32:55 +00:00
Concrete(ElabValueData),
}
2022-02-16 15:37:12 +00:00
#[derive(Debug, Clone)]
2022-02-15 20:32:55 +00:00
enum ElabValueData {
U32(u32),
2022-02-15 20:32:55 +00:00
Bytes(Vec<u8>),
}
/// Types that are only valid during Elaboration
2022-02-06 22:19:55 +00:00
#[derive(Debug)]
enum ElabKind {
/// general, unsized number type
2022-01-23 21:52:06 +00:00
Num,
}
2022-02-16 15:37:12 +00:00
#[derive(Debug)]
pub enum GenericArg {
Elab(ElabData),
Type(Type),
}
2022-02-15 20:32:55 +00:00
pub struct PrimitiveTypes {
pub elabnum: Type,
pub logic: Type,
2022-02-15 22:56:52 +00:00
pub infer: Type,
2022-02-15 20:32:55 +00:00
}
2022-02-15 20:32:55 +00:00
pub struct TypingContext {
types: Vec<TypeStruct>,
pub primitives: PrimitiveTypes,
}
2022-02-15 20:32:55 +00:00
impl TypingContext {
pub fn new() -> Self {
let primitives = PrimitiveTypes {
elabnum: InternedType(0),
logic: InternedType(1),
2022-02-15 22:56:52 +00:00
infer: InternedType(2),
2022-02-15 20:32:55 +00:00
};
Self {
2022-02-15 22:56:52 +00:00
types: vec![
TypeStruct {
kind: TypeKind::ElabType(ElabKind::Num),
},
TypeStruct {
kind: TypeKind::Logic(ElabData {
typ: primitives.elabnum,
value: ElabValue::Infer,
}),
},
TypeStruct {
kind: TypeKind::Infer,
},
],
2022-02-15 20:32:55 +00:00
primitives,
2022-02-06 20:02:55 +00:00
}
}
2022-02-15 20:32:55 +00:00
pub fn add(&mut self, typ: TypeStruct) -> Type {
let id = self.types.len();
self.types.push(typ);
InternedType(id)
2022-02-06 20:02:55 +00:00
}
2022-02-15 20:32:55 +00:00
pub fn get(&self, typ: Type) -> &TypeStruct {
&self.types[typ.0]
2022-02-06 22:19:55 +00:00
}
2022-02-16 15:37:12 +00:00
pub fn make_elabnum_u32(&self, num: u32) -> ElabData {
ElabData {
typ: self.primitives.elabnum,
value: ElabValue::Concrete(ElabValueData::U32(num)),
}
}
pub fn parameterize(&mut self, typ: Type, params: &[GenericArg]) -> Option<Type> {
// TODO: return proper error type here
match &self.get(typ).kind {
// Elab types have no params yet
TypeKind::ElabType(_) => None,
TypeKind::Logic(_) => {
if params.len() != 1 {
// invalid number of typeargs
return None;
};
let param = &params[0];
if let GenericArg::Elab(data) = param {
if data.typ == self.primitives.elabnum {
Some(self.add(TypeStruct {
kind: TypeKind::Logic(data.clone()),
}))
} else {
// arg must be elabnum
None
}
} else {
// arg must be an elab value
None
}
}
TypeKind::UInt(_) => todo!(),
2022-02-16 21:17:25 +00:00
TypeKind::Callable(_sig) => todo!("callable generic params"),
2022-02-16 15:37:12 +00:00
// need to know what the type is to parameterize it
TypeKind::Infer => None,
}
}
2022-02-15 22:56:52 +00:00
pub fn pretty_value(&self, w: &mut dyn std::fmt::Write, data: &ElabData) -> std::fmt::Result {
2022-02-16 15:37:12 +00:00
match &data.value {
2022-02-15 22:56:52 +00:00
ElabValue::Infer => write!(w, "?: ")?,
2022-02-16 15:37:12 +00:00
ElabValue::Concrete(val) => match val {
ElabValueData::U32(val) => write!(w, "{:?}: ", val)?,
ElabValueData::Bytes(val) => write!(w, "{:?}: ", val)?,
},
2022-02-15 22:56:52 +00:00
}
self.pretty_type(w, data.typ)
}
2022-02-15 20:32:55 +00:00
pub fn pretty_type(&self, w: &mut dyn std::fmt::Write, typ: Type) -> std::fmt::Result {
match &self.get(typ).kind {
2022-02-15 22:56:52 +00:00
TypeKind::ElabType(val) => write!(w, "{:?}", val),
TypeKind::Logic(val) => {
let mut width = String::new();
self.pretty_value(&mut width, val)?;
write!(w, "Logic<{}>", width)
}
TypeKind::Infer => write!(w, "?"),
2022-02-15 20:44:10 +00:00
TypeKind::UInt(_) => todo!("print uint"),
2022-02-16 21:17:25 +00:00
TypeKind::Callable(_sig) => todo!("print callable"),
2022-02-06 20:02:55 +00:00
}
}
}