Skip to content

Commit

Permalink
Make names more unique
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jul 13, 2024
1 parent 9a327ea commit a9da7cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/instantiation/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,22 +412,18 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
WireSource::Constant(value) => TypedValue::from_value(value.clone()),
})
}
fn get_unique_name(&self) -> String {
format!("_{}", self.wires.get_next_alloc_id().get_hidden_value())
}
fn alloc_wire_for_const(
&mut self,
value: TypedValue,
original_instruction: FlatID,
domain: DomainID,
) -> WireID {
let name = self.get_unique_name();
self.wires.alloc(RealWire {
source: RealWireDataSource::Constant { value: value.value },
original_instruction,
domain,
typ: value.typ,
name,
name: self.unique_name_producer.get_unique_name(""),
absolute_latency: CALCULATE_LATENCY_LATER,
needed_until: CALCULATE_LATENCY_LATER,
})
Expand Down Expand Up @@ -483,7 +479,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
original_instruction: submod_instance.original_instruction,
domain,
typ: ConcreteType::Unknown,
name: format!("{}_{}", submod_instance.name, port_data.name),
name: self.unique_name_producer.get_unique_name(format!("{}_{}", submod_instance.name, port_data.name)),
absolute_latency: CALCULATE_LATENCY_LATER,
needed_until: CALCULATE_LATENCY_LATER,
});
Expand Down Expand Up @@ -561,9 +557,8 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
unreachable!("Constant cannot be non-compile-time");
}
};
let name = self.get_unique_name();
Ok(self.wires.alloc(RealWire {
name,
name : self.unique_name_producer.get_unique_name(""),
typ: ConcreteType::Unknown,
original_instruction,
domain,
Expand All @@ -582,7 +577,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
if let Some(condition) = condition {
self.wires.alloc(RealWire {
typ: BOOL_CONCRETE_TYPE,
name: self.get_unique_name(),
name: self.unique_name_producer.get_unique_name(""),
original_instruction,
domain,
source: RealWireDataSource::BinaryOp {
Expand Down Expand Up @@ -664,7 +659,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
CALCULATE_LATENCY_LATER
};
let wire_id = self.wires.alloc(RealWire {
name: wire_decl.name.clone(),
name: self.unique_name_producer.get_unique_name(&wire_decl.name),
typ,
original_instruction,
domain: wire_decl.typ.domain.unwrap_physical(),
Expand All @@ -689,11 +684,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
Instruction::SubModule(submodule) => {
let sub_module = &self.linker.modules[submodule.module_ref.id];

let name = if let Some((name, _span)) = &submodule.name {
name.clone()
} else {
self.get_unique_name()
};
let name_origin = if let Some((name, _span)) = &submodule.name {name} else {""};
let port_map = sub_module.ports.map(|_| None);
let interface_call_sites = sub_module.interfaces.map(|_| Vec::new());
let mut template_args =
Expand All @@ -717,7 +708,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
instance: None,
port_map,
interface_call_sites,
name,
name : self.unique_name_producer.get_unique_name(name_origin),
module_uuid: submodule.module_ref.id,
template_args,
}))
Expand Down Expand Up @@ -807,7 +798,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
if !else_range.is_empty() {
let else_condition_bool = self.wires.alloc(RealWire {
typ: BOOL_CONCRETE_TYPE,
name: self.get_unique_name(),
name: self.unique_name_producer.get_unique_name(""),
original_instruction,
domain,
source: RealWireDataSource::UnaryOp {
Expand Down
5 changes: 5 additions & 0 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod latency_algorithm;
mod latency_count;
mod list_of_lists;
mod typecheck;
mod unique_names;

use unique_names::UniqueNames;

use crate::prelude::*;

Expand Down Expand Up @@ -271,6 +274,7 @@ struct InstantiationContext<'fl, 'l> {
generation_state: GenerationState<'fl>,
wires: FlatAlloc<RealWire, WireIDMarker>,
submodules: FlatAlloc<SubModule, SubModuleIDMarker>,
unique_name_producer: UniqueNames,

interface_ports: FlatAlloc<Option<InstantiatedPort>, PortIDMarker>,
errors: ErrorCollector<'l>,
Expand Down Expand Up @@ -412,6 +416,7 @@ fn perform_instantiation(
submodules: FlatAlloc::new(),
interface_ports: md.ports.map(|_| None),
errors: ErrorCollector::new_empty(md.link_info.file, &linker.files),
unique_name_producer: UniqueNames::new(),
template_args,
md,
linker,
Expand Down
28 changes: 28 additions & 0 deletions src/instantiation/unique_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::collections::HashMap;


pub struct UniqueNames {
name_map : HashMap<String, i64>
}

impl UniqueNames {
pub fn new() -> Self {
let mut name_map : HashMap<String, i64> = HashMap::new();
name_map.insert(String::new(), 1);
Self {
name_map
}
}
pub fn get_unique_name<S : Into<String> + AsRef<str>>(&mut self, name : S) -> String {
let name_ref = name.as_ref();
if let Some(found_id) = self.name_map.get_mut(name_ref) {
let result = format!("{name_ref}_{found_id}");
*found_id += 1;
result
} else {
let result : String = name.into();
self.name_map.insert(result.clone(), 2);
result
}
}
}

0 comments on commit a9da7cc

Please sign in to comment.