Compare commits

...

2 commits

Author SHA1 Message Date
Jacob Lifshay 2c1afd1cd6
const generics on hdl_module work!
All checks were successful
/ test (push) Successful in 4m54s
2024-09-17 15:39:23 -07:00
Jacob Lifshay 76ea7f82c3
WIP adding const generics
Some checks failed
/ test (push) Failing after 1m3s
2024-09-16 16:47:10 -07:00
3 changed files with 782 additions and 192 deletions

File diff suppressed because it is too large Load diff

View file

@ -66,8 +66,8 @@ pub(crate) struct ModuleFn {
vis: Visibility,
sig: Signature,
block: Box<Block>,
io: Vec<ModuleIO>,
struct_generics: ParsedGenerics,
the_struct: TokenStream,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
@ -224,6 +224,41 @@ impl Parse for ModuleFn {
errors.finish()?;
let struct_generics = struct_generics.unwrap();
let (block, io) = body_results.unwrap();
let (_struct_impl_generics, _struct_type_generics, struct_where_clause) =
struct_generics.split_for_impl();
let struct_where_clause: Option<WhereClause> = parse_quote! { #struct_where_clause };
if let Some(struct_where_clause) = &struct_where_clause {
sig.generics
.where_clause
.get_or_insert_with(|| WhereClause {
where_token: struct_where_clause.where_token,
predicates: Default::default(),
})
.predicates
.extend(struct_where_clause.predicates.clone());
}
let fn_name = &sig.ident;
let io_flips = io
.iter()
.map(|io| match io.kind.kind {
ModuleIOKind::Input((input,)) => quote_spanned! {input.span=>
#[hdl(flip)]
},
ModuleIOKind::Output(_) => quote! {},
})
.collect::<Vec<_>>();
let io_types = io.iter().map(|io| &io.kind.ty).collect::<Vec<_>>();
let io_names = io.iter().map(|io| &io.name).collect::<Vec<_>>();
let the_struct: ItemStruct = parse_quote! {
#[allow(non_camel_case_types)]
#[hdl(no_runtime_generics, no_static)]
#vis struct #fn_name #struct_generics #struct_where_clause {
#(
#io_flips
#vis #io_names: #io_types,)*
}
};
let the_struct = crate::hdl_bundle::hdl_bundle(the_struct)?;
Ok(Self {
attrs,
config_options,
@ -231,8 +266,8 @@ impl Parse for ModuleFn {
vis,
sig,
block,
io,
struct_generics,
the_struct,
})
}
}
@ -246,8 +281,8 @@ impl ModuleFn {
vis,
sig,
block,
io,
struct_generics,
the_struct,
} = self;
let ConfigOptions {
outline_generated: _,
@ -279,7 +314,7 @@ impl ModuleFn {
ModuleKind::Normal => quote! { ::fayalite::module::ModuleKind::Normal },
};
let fn_name = &outer_sig.ident;
let (_struct_impl_generics, struct_type_generics, struct_where_clause) =
let (_struct_impl_generics, struct_type_generics, _struct_where_clause) =
struct_generics.split_for_impl();
let struct_ty = quote! {#fn_name #struct_type_generics};
body_sig.ident = parse_quote! {__body};
@ -294,17 +329,6 @@ impl ModuleFn {
};
outer_sig.output =
parse_quote! {-> ::fayalite::intern::Interned<::fayalite::module::Module<#struct_ty>>};
let io_flips = io
.iter()
.map(|io| match io.kind.kind {
ModuleIOKind::Input((input,)) => quote_spanned! {input.span=>
#[hdl(flip)]
},
ModuleIOKind::Output(_) => quote! {},
})
.collect::<Vec<_>>();
let io_types = io.iter().map(|io| &io.kind.ty).collect::<Vec<_>>();
let io_names = io.iter().map(|io| &io.name).collect::<Vec<_>>();
let fn_name_str = fn_name.to_string();
let (_, body_type_generics, _) = body_fn.sig.generics.split_for_impl();
let body_turbofish_type_generics = body_type_generics.as_turbofish();
@ -316,15 +340,6 @@ impl ModuleFn {
|m| __body #body_turbofish_type_generics(m, #(#param_names,)*),
)
}};
let the_struct: ItemStruct = parse_quote! {
#[allow(non_camel_case_types)]
#[hdl(no_runtime_generics, no_static)]
#vis struct #fn_name #struct_generics #struct_where_clause {
#(
#io_flips
#vis #io_names: #io_types,)*
}
};
let outer_fn = ItemFn {
attrs,
vis,
@ -332,7 +347,7 @@ impl ModuleFn {
block,
};
let mut retval = outer_fn.into_token_stream();
retval.extend(crate::hdl_bundle::hdl_bundle(the_struct).unwrap());
retval.extend(the_struct);
retval
}
}

View file

@ -6,6 +6,7 @@ use fayalite::{
intern::Intern,
module::transform::simplify_enums::{simplify_enums, SimplifyEnumsKind},
prelude::*,
ty::StaticType,
};
use serde_json::json;
@ -133,9 +134,11 @@ circuit my_module:
};
}
#[cfg(todo)]
#[hdl_module(outline_generated)]
pub fn check_array_repeat<const N: usize>() {
pub fn check_array_repeat<const N: usize>()
where
ConstUsize<N>: KnownSize,
{
#[hdl]
let i: UInt<8> = m.input();
#[hdl]
@ -147,7 +150,6 @@ pub fn check_array_repeat<const N: usize>() {
);
}
#[cfg(todo)]
#[test]
fn test_array_repeat() {
let _n = SourceLocation::normalize_files_for_tests();
@ -188,21 +190,21 @@ circuit check_array_repeat_1:
};
}
#[cfg(todo)]
#[hdl_module(outline_generated)]
pub fn check_skipped_generics<T, #[hdl(skip)] U, const N: usize, #[hdl(skip)] const M: usize>(v: U)
where
T: StaticValue,
T: StaticType,
ConstUsize<N>: KnownSize,
U: std::fmt::Display,
{
dbg!(M);
#[hdl]
let i: T = m.input();
#[hdl]
let o: Array<[T; N]> = m.output();
let o: Array<T, N> = m.output();
let bytes = v.to_string().as_bytes().to_expr();
#[hdl]
let o2: Array<[UInt<8>]> = m.output(bytes.ty());
let o2: Array<UInt<8>> = m.output(Expr::ty(bytes));
connect(
o,
#[hdl]
@ -211,7 +213,6 @@ where
connect(o2, bytes);
}
#[cfg(todo)]
#[test]
fn test_skipped_generics() {
let _n = SourceLocation::normalize_files_for_tests();