Skip to content

Commit

Permalink
Linker no longer links
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jan 10, 2024
1 parent c248875 commit ada82a6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 162 deletions.
9 changes: 2 additions & 7 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


use crate::{tokenizer::{TokenTypeIdx, get_token_type_name}, linker::{NamedUUID, FileUUID}, flattening::FlattenedModule, arena_alloc::{UUIDMarker, UUID, FlatAlloc}, instantiation::InstantiationList, value::Value, errors::ErrorCollector};
use crate::{tokenizer::{TokenTypeIdx, get_token_type_name}, linker::FileUUID, flattening::FlattenedModule, arena_alloc::{UUIDMarker, UUID, FlatAlloc}, instantiation::InstantiationList, value::Value, errors::ErrorCollector};
use core::ops::Range;
use std::fmt::Display;

Expand Down Expand Up @@ -146,9 +146,7 @@ pub struct LinkInfo {
pub file : FileUUID,
pub name : Box<str>,
pub name_span : Span,
pub span : Span,
pub global_references : Vec<GlobalReference>,
pub is_fully_linked : bool // Caches if self.global_references contains any INVALID references.
pub span : Span
}

#[derive(Debug)]
Expand Down Expand Up @@ -180,9 +178,6 @@ impl Module {
}
}

#[derive(Debug,Clone,Copy)]
pub struct GlobalReference(pub Span, pub Option<NamedUUID>); // token index, and name span

#[derive(Debug)]
pub struct ASTRoot {
pub modules : Vec<Module>,
Expand Down
58 changes: 28 additions & 30 deletions src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,61 +103,59 @@ fn add_ide_bracket_depths_recursive<'a>(result : &mut [IDEToken], current_depth
}
}

impl Named {
fn get_ide_type(&self) -> IDEIdentifierType{
match self {
Named::Module(_) => IDEIdentifierType::Interface,
Named::Constant(_) => IDEIdentifierType::Constant,
Named::Type(_) => IDEIdentifierType::Type,
}
fn set_span_name_color(span : Span, typ : IDEIdentifierType, result : &mut [IDEToken]) {
for tok_idx in span {
result[tok_idx].typ = IDETokenType::Identifier(typ);
}
}

fn walk_name_color(all_objects : &[NamedUUID], links : &Links, result : &mut [IDEToken]) {
for obj_uuid in all_objects {
let object = &links.globals[*obj_uuid];
match object {
let ide_typ = match object {
Named::Module(module) => {
for (_id, item) in &module.flattened.instantiations {
match item {
Instantiation::Wire(w) => {
if let &WireSource::WireRead{from_wire} = &w.source {
let decl = module.flattened.instantiations[from_wire].extract_wire_declaration();
if decl.is_remote_declaration {continue;} // Virtual wires don't appear in this program text
result[w.span.assert_is_single_token()].typ = IDETokenType::Identifier(IDEIdentifierType::Value(decl.identifier_type));
match &w.source {
&WireSource::WireRead(from_wire) => {
let decl = module.flattened.instantiations[from_wire].extract_wire_declaration();
if decl.is_remote_declaration {continue;} // Virtual wires don't appear in this program text
result[w.span.assert_is_single_token()].typ = IDETokenType::Identifier(IDEIdentifierType::Value(decl.identifier_type));
}
WireSource::UnaryOp { op:_, right:_ } => {}
WireSource::BinaryOp { op:_, left:_, right:_ } => {}
WireSource::ArrayAccess { arr:_, arr_idx:_ } => {}
WireSource::Constant(_) => {}
WireSource::NamedConstant(_name) => {
set_span_name_color(w.span, IDEIdentifierType::Constant, result);
}
}
}
Instantiation::WireDeclaration(decl) => {
if decl.is_remote_declaration {continue;} // Virtual wires don't appear in this program text
result[decl.name_token].typ = IDETokenType::Identifier(IDEIdentifierType::Value(decl.identifier_type));
decl.typ.for_each_located_type(&mut |_, span| {
set_span_name_color(span, IDEIdentifierType::Type, result);
});
}
Instantiation::Connection(conn) => {
let decl = module.flattened.instantiations[conn.to.root].extract_wire_declaration();
if decl.is_remote_declaration {continue;} // Virtual wires don't appear in this program text
result[conn.to.span.0].typ = IDETokenType::Identifier(IDEIdentifierType::Value(decl.identifier_type));
}
Instantiation::SubModule(_) | Instantiation::IfStatement(_) | Instantiation::ForStatement(_) => {}
Instantiation::SubModule(sm) => {
set_span_name_color(sm.typ_span, IDEIdentifierType::Interface, result);
}
Instantiation::IfStatement(_) | Instantiation::ForStatement(_) => {}
}
}
IDEIdentifierType::Interface
}
_other => {}
}
_other => {todo!("Name Color for non-modules not implemented")}
};

let link_info = object.get_link_info().unwrap();
let ide_typ = object.get_ide_type();
for name_part in link_info.name_span {
result[name_part].typ = IDETokenType::Identifier(ide_typ);
}
for GlobalReference(reference_span, ref_uuid) in &link_info.global_references {
let typ = if let Some(id) = ref_uuid {
IDETokenType::Identifier(links.globals[*id].get_ide_type())
} else {
IDETokenType::Invalid
};
for part_tok in *reference_span {
result[part_tok].typ = typ;
}
}
set_span_name_color(link_info.name_span, ide_typ, result);
}
}

Expand Down
33 changes: 19 additions & 14 deletions src/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{ops::{Deref, Range}, iter::zip, cell::RefCell};

use crate::{
ast::{Span, Module, Expression, SpanExpression, LocalOrGlobal, Operator, AssignableExpression, SpanAssignableExpression, Statement, CodeBlock, IdentifierType, TypeExpression, DeclIDMarker, DeclID, SpanTypeExpression},
linker::{Linker, NamedUUID, FileUUID, GlobalResolver, ResolvedGlobals},
linker::{Linker, NamedUUID, FileUUID, GlobalResolver, ResolvedGlobals, Named, NamedConstant},
errors::{ErrorCollector, error_info}, arena_alloc::{UUID, UUIDMarker, FlatAlloc, UUIDRange}, typing::{Type, typecheck_unary_operator, get_binary_operator_types, typecheck, typecheck_is_array_indexer, BOOL_TYPE, INT_TYPE}, value::Value
};

Expand Down Expand Up @@ -43,21 +43,23 @@ pub struct Connection {

#[derive(Debug)]
pub enum WireSource {
WireRead{from_wire : FlatID}, // Used to add a span to the reference of a wire.
WireRead(FlatID), // Used to add a span to the reference of a wire.
UnaryOp{op : Operator, right : FlatID},
BinaryOp{op : Operator, left : FlatID, right : FlatID},
ArrayAccess{arr : FlatID, arr_idx : FlatID},
Constant{value : Value},
Constant(Value),
NamedConstant(NamedUUID),
}

impl WireSource {
pub fn for_each_input_wire<F : FnMut(FlatID)>(&self, func : &mut F) {
match self {
&WireSource::WireRead { from_wire } => {func(from_wire)}
&WireSource::WireRead(from_wire) => {func(from_wire)}
&WireSource::UnaryOp { op:_, right } => {func(right)}
&WireSource::BinaryOp { op:_, left, right } => {func(left); func(right)}
&WireSource::ArrayAccess { arr, arr_idx } => {func(arr); func(arr_idx)}
WireSource::Constant { value:_ } => {}
WireSource::Constant(_) => {}
&WireSource::NamedConstant(_) => {}
}
}
}
Expand Down Expand Up @@ -193,7 +195,7 @@ impl<'inst, 'l, 'm, 'resolved> FlatteningContext<'inst, 'l, 'm, 'resolved> {
match &type_expr.0 {
TypeExpression::Named => {
if let Some(typ_id) = &self.linker.try_get_type(type_expr.1, &self.errors) {
Type::Named(*typ_id)
Type::Named{id : *typ_id, span : Some(type_expr.1)}
} else {
Type::Error
}
Expand Down Expand Up @@ -331,14 +333,14 @@ impl<'inst, 'l, 'm, 'resolved> FlatteningContext<'inst, 'l, 'm, 'resolved> {
Expression::Named(LocalOrGlobal::Local(l)) => {
let from_wire = self.decl_to_flat_map[*l].unwrap();
let decl = self.instantiations[from_wire].extract_wire_declaration();
(decl.identifier_type == IdentifierType::Generative, WireSource::WireRead{from_wire})
(decl.identifier_type == IdentifierType::Generative, WireSource::WireRead(from_wire))
}
Expression::Named(LocalOrGlobal::Global(ref_span)) => {
let cst = self.linker.try_get_constant(*ref_span, &self.errors)?;
(true, WireSource::Constant{value : cst})
(true, WireSource::NamedConstant(cst))
}
Expression::Constant(cst) => {
(true, WireSource::Constant{value : cst.clone()})
(true, WireSource::Constant(cst.clone()))
}
Expression::UnaryOp(op_box) => {
let (op, _op_pos, operate_on) = op_box.deref();
Expand Down Expand Up @@ -435,7 +437,7 @@ impl<'inst, 'l, 'm, 'resolved> FlatteningContext<'inst, 'l, 'm, 'resolved> {

// temporary
let module_port_wire_decl = self.instantiations[*field].extract_wire_declaration();
let module_port_proxy = self.instantiations.alloc(Instantiation::Wire(WireInstance{typ : module_port_wire_decl.typ.clone(), is_compiletime : module_port_wire_decl.identifier_type == IdentifierType::Generative, span : *func_span, is_remote_declaration : self.is_remote_declaration, source : WireSource::WireRead { from_wire: *field }}));
let module_port_proxy = self.instantiations.alloc(Instantiation::Wire(WireInstance{typ : module_port_wire_decl.typ.clone(), is_compiletime : module_port_wire_decl.identifier_type == IdentifierType::Generative, span : *func_span, is_remote_declaration : self.is_remote_declaration, source : WireSource::WireRead(*field)}));
self.instantiations.alloc(Instantiation::Connection(Connection{num_regs : to_i.num_regs, from: module_port_proxy, to: write_side}));
}
},
Expand Down Expand Up @@ -545,7 +547,7 @@ impl FlattenedModule {
The Generating Structure of the code is not yet executed.
It is template-preserving
*/
pub fn initialize(linker : &Linker, module : &Module, starts_with_errors : bool) -> FlattenedModule {
pub fn initialize(linker : &Linker, module : &Module) -> FlattenedModule {
let mut instantiations = FlatAlloc::new();
let resolved_globals : RefCell<ResolvedGlobals> = RefCell::new(ResolvedGlobals::new());
let mut context = FlatteningContext{
Expand All @@ -556,7 +558,6 @@ impl FlattenedModule {
linker : GlobalResolver::new(linker, module.link_info.file, &resolved_globals),
module,
};
context.errors.did_error.set(starts_with_errors);

let interface_ports = context.initialize_interface::<false>();

Expand Down Expand Up @@ -596,7 +597,7 @@ impl FlattenedModule {
}
Instantiation::Wire(w) => {
let result_typ = match &w.source {
&WireSource::WireRead{from_wire} => {
&WireSource::WireRead(from_wire) => {
self.instantiations[from_wire].extract_wire_declaration().typ.clone()
}
&WireSource::UnaryOp{op, right} => {
Expand All @@ -622,9 +623,13 @@ impl FlattenedModule {
Type::Error
}
}
WireSource::Constant{value} => {
WireSource::Constant(value) => {
value.get_type_of_constant()
}
&WireSource::NamedConstant(id) => {
let Named::Constant(NamedConstant::Builtin{name:_, typ, val:_}) = &linker.links.globals[id] else {unreachable!()};
typ.clone()
}
};
let Instantiation::Wire(w) = &mut self.instantiations[elem_id] else {unreachable!()};
w.typ = result_typ;
Expand Down
18 changes: 11 additions & 7 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{rc::Rc, ops::Deref, cell::RefCell};

use num::BigInt;

use crate::{arena_alloc::{UUID, UUIDMarker, FlatAlloc, UUIDRange}, ast::{Operator, IdentifierType, Span}, typing::{ConcreteType, Type, BOOL_CONCRETE_TYPE, INT_CONCRETE_TYPE}, flattening::{FlatID, Instantiation, FlatIDMarker, ConnectionWritePathElement, WireSource, WireInstance, Connection, ConnectionWritePathElementComputed, FlattenedModule, FlatIDRange}, errors::ErrorCollector, linker::Linker, value::{Value, compute_unary_op, compute_binary_op}, tokenizer::kw};
use crate::{arena_alloc::{UUID, UUIDMarker, FlatAlloc, UUIDRange}, ast::{Operator, IdentifierType, Span}, typing::{ConcreteType, Type, BOOL_CONCRETE_TYPE, INT_CONCRETE_TYPE}, flattening::{FlatID, Instantiation, FlatIDMarker, ConnectionWritePathElement, WireSource, WireInstance, Connection, ConnectionWritePathElementComputed, FlattenedModule, FlatIDRange}, errors::ErrorCollector, linker::{Linker, NamedConstant, Named}, value::{Value, compute_unary_op, compute_binary_op}, tokenizer::kw};

pub mod latency;

Expand Down Expand Up @@ -183,8 +183,8 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
fn concretize_type(&self, typ : &Type, span : Span) -> Option<ConcreteType> {
match typ {
Type::Error | Type::Unknown => unreachable!("Bad types should be caught in flattening: {}", typ.to_string(self.linker)),
Type::Named(n) => {
Some(ConcreteType::Named(*n))
Type::Named{id, span : _} => {
Some(ConcreteType::Named(*id))
}
Type::Array(arr_box) => {
let (arr_content_typ, arr_size_wire) = arr_box.deref();
Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
}
fn compute_compile_time(&self, wire_inst : &WireSource) -> Option<Value> {
Some(match wire_inst {
&WireSource::WireRead{from_wire} => {
&WireSource::WireRead(from_wire) => {
self.get_generation_value(from_wire)?.clone()
}
&WireSource::UnaryOp{op, right} => {
Expand All @@ -302,7 +302,11 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
return None
}
}
WireSource::Constant{value} => value.clone()
WireSource::Constant(value) => value.clone(),
WireSource::NamedConstant(id) => {
let Named::Constant(NamedConstant::Builtin{name:_, typ:_, val}) = &self.linker.links[*id] else {unreachable!()};
val.clone()
}
})
}
fn get_unique_name(&self) -> Box<str> {
Expand All @@ -324,7 +328,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
}
fn wire_to_real_wire(&mut self, w: &WireInstance, typ : ConcreteType, original_wire : FlatID) -> Option<WireID> {
let source = match &w.source {
&WireSource::WireRead{from_wire} => {
&WireSource::WireRead(from_wire) => {
/*Assert*/ self.flattened.instantiations[from_wire].extract_wire_declaration(); // WireReads must point to a NamedWire!
return Some(self.generation_state[from_wire].extract_wire())
}
Expand Down Expand Up @@ -352,7 +356,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
}
}
}
WireSource::Constant{value: _} => {
WireSource::Constant(_) | WireSource::NamedConstant(_) => {
unreachable!("Constant cannot be non-compile-time");
}
};
Expand Down
Loading

0 comments on commit ada82a6

Please sign in to comment.