Skip to content

Commit

Permalink
Move is_compiletime arraysize check to flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jan 4, 2024
1 parent 8635e4e commit 7426462
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 47 deletions.
44 changes: 6 additions & 38 deletions src/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ struct FlatteningContext<'l, 'm, 'fl> {
}

impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
/*fn typecheck(&self, wire_id : FlatID, expected : &Type, context : &str) -> Option<()> {
let wire = self.instantiations[wire_id].extract_wire();
typecheck(&wire.typ, wire.span, expected, context, self.linker, &self.errors)
}*/
pub fn map_to_type(&self, type_expr : &TypeExpression, global_references : &[GlobalReference]) -> Type {
match type_expr {
TypeExpression::Named(n) => {
Expand All @@ -147,46 +143,18 @@ impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
TypeExpression::Array(b) => {
let (array_type_expr, array_size_expr) = b.deref();
let array_element_type = self.map_to_type(&array_type_expr.0, global_references);
if let Some(array_size_wire) = self.flatten_single_expr(array_size_expr, None) {
//self.typecheck(array_size_wire, &Type::Named(get_builtin_uuid("int")), "array size");
Type::Array(Box::new((array_element_type, array_size_wire)))
if let Some(array_size_wire_id) = self.flatten_single_expr(array_size_expr, None) {
let array_size_wire = self.instantiations[array_size_wire_id].extract_wire();
if !array_size_wire.is_compiletime {
self.errors.error_basic(array_size_expr.1, "Array size must be compile time");
}
Type::Array(Box::new((array_element_type, array_size_wire_id)))
} else {
Type::Error
}
}
}
}
// May also error, for example when array accesses happen on non-array types
/*fn get_connectionwrite_type(&self, cw : &ConnectionWrite) -> Option<&Type> {
let mut current_type = &self.instantiations[cw.root].extract_wire_declaration().typ;
for p in &cw.path {
match p {
ConnectionWritePathElement::ArrayIdx{idx, idx_span} => {
let index_was_int = self.typecheck(*idx, &Type::Named(get_builtin_uuid("int")), "array index");
current_type = typecheck_is_array_indexer(current_type, *idx_span, self.linker, &self.errors)?;
index_was_int?;
}
}
}
Some(current_type)
}
fn create_connection(&self, connection : Connection) -> Option<()> {
let expected_type = self.get_connectionwrite_type(&connection.to)?;
let did_typecheck_fail = self.typecheck(connection.from, &expected_type, "connection");
let assign_to_root = self.instantiations[connection.to.root].extract_wire_declaration();
let from_wire = self.instantiations[connection.from].extract_wire();
if assign_to_root.identifier_type == IdentifierType::Generative && !from_wire.is_compiletime {
let decl_info = error_info(assign_to_root.get_full_decl_span(), self.errors.file, "Declared here");
self.errors.error_with_info(from_wire.span, "Assignments to compile-time variables must themselves be known at compile time", vec![decl_info]);
return None;
}
did_typecheck_fail?;// gather all errors, then
self.instantiations.alloc(Instantiation::Connection(connection));
Some(())
}*/
fn alloc_module_interface(&self, name : Box<str>, module : &Module, module_uuid : NamedUUID, typ_span : Span) -> Instantiation {
let flattened_borrow = module.flattened.borrow();
let local_wires : Vec<FlatID> = flattened_borrow.interface.interface_wires.iter().enumerate().map(|(port_idx, port)| {
Expand Down
15 changes: 9 additions & 6 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
let (arr_content_typ, arr_size_wire) = arr_box.deref();
let inner_typ = self.concretize_type(arr_content_typ, span)?;
let size_val = &self.generation_state[*arr_size_wire];
let SubModuleOrWire::CompileTimeValue(cv) = size_val else {self.errors.error_basic(span, format!("Value is not compile time! {size_val:?} instead")); return None;};
let cv = size_val.extract_generation_value();
let arr_usize = self.extract_integer_from_value(cv, span)?;
Some(ConcreteType::Array(Box::new((inner_typ, arr_usize))))
}
Expand Down Expand Up @@ -369,7 +369,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
for (original_wire, inst) in &self.flattened.instantiations {
let instance_to_add : SubModuleOrWire = match inst {
Instantiation::SubModule(SubModuleInstance{module_uuid, name, typ_span, outputs_start, local_wires}) => {
let instance = self.linker.instantiate(*module_uuid);
let Some(instance) = self.linker.instantiate(*module_uuid) else {continue}; // Avoid error from submodule
let interface_real_wires = local_wires.iter().map(|port| {
self.generation_state[*port].extract_wire()
}).collect();
Expand Down Expand Up @@ -452,10 +452,13 @@ impl InstantiationList {
Self{cache : RefCell::new(Vec::new())}
}

pub fn instantiate(&self, module : &Module, linker : &Linker) -> Rc<InstantiatedModule> {
pub fn instantiate(&self, name : &str, flattened : &FlattenedModule, linker : &Linker) -> Option<Rc<InstantiatedModule>> {
if flattened.errors.did_error() {
return None;// Don't instantiate modules that already errored. Otherwise instantiator may crash
}

let mut cache_borrow = self.cache.borrow_mut();

let flattened = module.flattened.borrow();
// Temporary, no template arguments yet
if cache_borrow.is_empty() {
let mut context = InstantiationContext{
Expand All @@ -471,7 +474,7 @@ impl InstantiationList {
let interface = context.make_interface();

cache_borrow.push(Rc::new(InstantiatedModule{
name : module.link_info.name.clone(),
name : name.to_owned().into_boxed_str(),
wires : context.wires,
submodules : context.submodules,
interface,
Expand All @@ -480,7 +483,7 @@ impl InstantiationList {
}

let instance_id = 0; // Temporary, will always be 0 while not template arguments
cache_borrow[instance_id].clone()
Some(cache_borrow[instance_id].clone())
}

pub fn collect_errors(&self, errors : &ErrorCollector) {
Expand Down
5 changes: 3 additions & 2 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,11 @@ impl Linker {
}
}

pub fn instantiate(&self, module_id : NamedUUID) -> Rc<InstantiatedModule> {
pub fn instantiate(&self, module_id : NamedUUID) -> Option<Rc<InstantiatedModule>> {
let Named::Module(md) = &self.links.globals[module_id] else {panic!("{module_id:?} is not a Module!")};
println!("Instantiating {}", md.link_info.name);

md.instantiations.instantiate(&md, self)
let flattened = md.flattened.borrow();
md.instantiations.instantiate(&md.link_info.name, &flattened, self)
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use dev_aid::syntax_highlighting::*;
use linker::{Named, Linker, NamedUUID};

fn codegen_to_file(linker : &Linker, id : NamedUUID, md : &Module) -> Option<()> {
let inst = linker.instantiate(id);
let inst = linker.instantiate(id)?;

let module_name = md.link_info.name.deref();

Expand Down

0 comments on commit 7426462

Please sign in to comment.