Skip to content

Commit

Permalink
Have explicit nodes in Flattening for Func Calls
Browse files Browse the repository at this point in the history
This is for two reasons
- Func calls have become relevant for generative code execution. It was really difficult to do with the current Write-based system
- Also future idea for "action" functions, that enable an implicit port when activating.
  • Loading branch information
VonTum committed Jun 2, 2024
1 parent 50806ff commit d6527f2
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 114 deletions.
46 changes: 36 additions & 10 deletions src/arena_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ impl<IndexMarker : UUIDMarker> UUID<IndexMarker> {

pub struct UUIDRange<IndexMarker>(pub UUID<IndexMarker>, pub UUID<IndexMarker>);

impl<IndexMarker> UUIDRange<IndexMarker> {
pub fn contains(&self, id : UUID<IndexMarker>) -> bool {
self.0.0 >= id.0 && self.1.0 < id.0
}
}

impl<IndexMarker : UUIDMarker> Debug for UUIDRange<IndexMarker> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(IndexMarker::DISPLAY_NAME)?;
Expand Down Expand Up @@ -87,16 +81,38 @@ impl<IndexMarker> Hash for UUIDRange<IndexMarker> {
}
}

impl<IndexMarker> IntoIterator for UUIDRange<IndexMarker> {
type Item = UUID<IndexMarker>;

type IntoIter = UUIDRangeIter<IndexMarker>;

fn into_iter(self) -> Self::IntoIter {
UUIDRangeIter(self.0, self.1)
}
}

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct UUIDRangeIter<IndexMarker : UUIDMarker>(UUID<IndexMarker>, UUID<IndexMarker>);
pub struct UUIDRangeIter<IndexMarker>(UUID<IndexMarker>, UUID<IndexMarker>);

impl<IndexMarker : UUIDMarker> UUIDRange<IndexMarker> {
impl<IndexMarker> UUIDRange<IndexMarker> {
pub fn empty() -> Self {
UUIDRange(UUID(0, PhantomData), UUID(0, PhantomData))
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn contains(&self, id : UUID<IndexMarker>) -> bool {
self.0.0 >= id.0 && self.1.0 < id.0
}
pub fn iter(&self) -> UUIDRangeIter<IndexMarker> {
self.into_iter()
}
pub fn len(&self) -> usize {
self.1.0 - self.0.0
}
}

impl<IndexMarker : UUIDMarker> Iterator for UUIDRange<IndexMarker> {
impl<IndexMarker> Iterator for UUIDRangeIter<IndexMarker> {
type Item = UUID<IndexMarker>;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -108,9 +124,19 @@ impl<IndexMarker : UUIDMarker> Iterator for UUIDRange<IndexMarker> {
Some(result)
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let sz = self.len();
(sz, Some(sz))
}
}

impl<IndexMarker> ExactSizeIterator for UUIDRangeIter<IndexMarker> {
fn len(&self) -> usize {
self.1.0 - self.0.0
}
}

impl<IndexMarker : UUIDMarker> UUIDRange<IndexMarker> {
impl<IndexMarker> UUIDRangeIter<IndexMarker> {
pub fn skip_to(&mut self, to : UUID<IndexMarker>) {
assert!(to.0 >= self.0.0);
assert!(to.0 <= self.1.0);
Expand Down
5 changes: 5 additions & 0 deletions src/dev_aid/lsp/tree_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ impl<'linker, Visitor : FnMut(Span, LocationInfo<'linker>), Pruner : Fn(Span) ->
Instruction::Write(write) => {
self.walk_wire_ref(md_id, md, &write.to);
}
Instruction::FuncCall(fc) => {
if let Some(submod_name_span) = fc.name_span {
self.visit(submod_name_span, LocationInfo::InModule(md_id, md, fc.submodule_instruction, InModule::NamedSubmodule(md.instructions[fc.submodule_instruction].unwrap_submodule())));
}
}
Instruction::IfStatement(_) | Instruction::ForStatement(_) => {}
};
}
Expand Down
35 changes: 31 additions & 4 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl Module {
pub fn get_instruction_span(&self, instr_id : FlatID) -> Span {
match &self.instructions[instr_id] {
Instruction::SubModule(sm) => sm.module_name_span,
Instruction::FuncCall(fc) => fc.whole_func_span,
Instruction::Declaration(decl) => decl.get_span(),
Instruction::Wire(w) => w.span,
Instruction::Write(conn) => conn.to_span,
Expand Down Expand Up @@ -393,6 +394,9 @@ impl WireSource {
WireSource::Constant(_) => {}
}
}
pub const fn new_error() -> WireSource {
WireSource::Constant(Value::Error)
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -469,9 +473,9 @@ impl Declaration {
#[derive(Debug)]
pub struct SubModuleInstance {
pub module_uuid : ModuleUUID,
pub module_name_span : Span,
/// Name is not always present in source code. Such as in inline function call syntax: my_mod(a, b, c)
pub name : Option<(String, Span)>,
pub module_name_span : Span,
pub local_interface_domains : FlatAlloc<DomainID, DomainIDMarker>,
pub documentation : Documentation
}
Expand All @@ -486,6 +490,28 @@ impl SubModuleInstance {
}
}

#[derive(Debug)]
pub struct FuncCallInstruction {
pub submodule_instruction : FlatID,
pub module_uuid : ModuleUUID,
/// arguments.len() == func_call_inputs.len() ALWAYS
pub arguments : Vec<FlatID>,
/// arguments.len() == func_call_inputs.len() ALWAYS
pub func_call_inputs : PortIDRange,
pub func_call_outputs : PortIDRange,
/// If this is None, that means the submodule was declared implicitly. Hence it could also be used at compiletime
pub name_span : Option<Span>,
pub arguments_span : BracketSpan,
pub whole_func_span : Span,
}

impl FuncCallInstruction {
pub fn could_be_at_compile_time(&self) -> bool {
todo!("self.name_span.is_none() but also other requirements, like if the module is a function")
}
}


#[derive(Debug)]
pub struct IfStatement {
pub condition : FlatID,
Expand All @@ -506,6 +532,7 @@ pub struct ForStatement {
#[derive(Debug)]
pub enum Instruction {
SubModule(SubModuleInstance),
FuncCall(FuncCallInstruction),
Declaration(Declaration),
Wire(WireInstance),
Write(Write),
Expand All @@ -530,9 +557,9 @@ impl Instruction {
sm
}
#[track_caller]
pub fn unwrap_write(&self) -> &Write {
let Self::Write(sm) = self else {panic!("unwrap_write on not a Write! Found {self:?}")};
sm
pub fn unwrap_func_call(&self) -> &FuncCallInstruction {
let Self::FuncCall(fc) = self else {panic!("unwrap_func_call on not a FuncCallInstruction! Found {self:?}")};
fc
}
}

Expand Down
Loading

0 comments on commit d6527f2

Please sign in to comment.