Skip to content

Commit

Permalink
Rename WireID to FlatID
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Nov 22, 2023
1 parent 22f54e7 commit f5b70aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
10 changes: 5 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use num::bigint::BigUint;

use crate::{tokenizer::{TokenTypeIdx, get_token_type_name}, linker::{NamedUUID, FileUUID}, flattening::{FlattenedModule, WireIDMarker, WireID, FlattenedInterface}, arena_alloc::ListAllocator};
use crate::{tokenizer::{TokenTypeIdx, get_token_type_name}, linker::{NamedUUID, FileUUID}, flattening::{FlattenedModule, FlatIDMarker, FlatID, FlattenedInterface}, arena_alloc::ListAllocator};
use core::ops::Range;
use std::fmt::Display;

Expand Down Expand Up @@ -43,7 +43,7 @@ impl From<usize> for Span {

#[derive(Debug, Clone, Copy)]
pub enum LocalOrGlobal {
Local(WireID),
Local(FlatID),
Global(usize)
}

Expand Down Expand Up @@ -103,7 +103,7 @@ pub type SpanStatement = (Statement, Span);

#[derive(Debug)]
pub enum AssignableExpression {
Named{local_idx : WireID},
Named{local_idx : FlatID},
ArrayIndex(Box<(SpanAssignableExpression, SpanExpression)>)
}

Expand All @@ -120,7 +120,7 @@ pub struct CodeBlock {

#[derive(Debug)]
pub enum Statement {
Declaration(WireID),
Declaration(FlatID),
Assign{to : Vec<AssignableExpressionWithModifiers>, eq_sign_position : Option<usize>, expr : SpanExpression}, // num_regs v = expr;
If{condition : SpanExpression, then : CodeBlock, els : Option<CodeBlock>},
Block(CodeBlock),
Expand All @@ -141,7 +141,7 @@ pub struct LinkInfo {
pub struct Module {
pub link_info : LinkInfo,

pub declarations : ListAllocator<SignalDeclaration, WireIDMarker>,
pub declarations : ListAllocator<SignalDeclaration, FlatIDMarker>,
pub code : CodeBlock,

pub interface : FlattenedInterface,
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ops::Deref, io};

use crate::{linker::Linker, flattening::{FlattenedModule, WireID}, ast::{Module, Span}, arena_alloc::ListAllocator};
use crate::{linker::Linker, flattening::{FlattenedModule, FlatID}, ast::{Module, Span}, arena_alloc::ListAllocator};


use moore_circt::{hw, comb, mlir::{self, Owned, builder, OperationExt, SingleBlockOp}, mlir::{Context, OwnedContext, DialectHandle, Builder, Value, Type}};
Expand Down
48 changes: 24 additions & 24 deletions src/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ use crate::{
};

#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash)]
pub struct WireIDMarker;
impl UUIDMarker for WireIDMarker {const DISPLAY_NAME : &'static str = "wire_";}
pub type WireID = UUID<WireIDMarker>;
pub struct FlatIDMarker;
impl UUIDMarker for FlatIDMarker {const DISPLAY_NAME : &'static str = "obj_";}
pub type FlatID = UUID<FlatIDMarker>;

pub type SpanWireID = (WireID, Span);
pub type SpanFlatID = (FlatID, Span);

pub type FieldID = usize;

// These are assignable connections
#[derive(Debug)]
pub enum ConnectionWrite {
Local(WireID),
ArrayIdx(Box<(SpanConnectionWrite, SpanWireID)>),
Local(FlatID),
ArrayIdx(Box<(SpanConnectionWrite, SpanFlatID)>),
StructField(Box<(SpanConnectionWrite, FieldID)>)
}

Expand All @@ -29,10 +29,10 @@ pub type SpanConnectionWrite = (ConnectionWrite, Span);
pub enum Instantiation {
SubModule{typ : Type, typ_span : Span},
PlainWire{typ : Type, typ_span : Span},
ExtractWire{typ : Type, extract_from : WireID, field : FieldID},
UnaryOp{typ : Type, op : Operator, right : SpanWireID},
BinaryOp{typ : Type, op : Operator, left : SpanWireID, right : SpanWireID},
ArrayAccess{typ : Type, arr : SpanWireID, arr_idx : SpanWireID},
ExtractWire{typ : Type, extract_from : FlatID, field : FieldID},
UnaryOp{typ : Type, op : Operator, right : SpanFlatID},
BinaryOp{typ : Type, op : Operator, left : SpanFlatID, right : SpanFlatID},
ArrayAccess{typ : Type, arr : SpanFlatID, arr_idx : SpanFlatID},
Constant{typ : Type, value : Value},
Error
}
Expand All @@ -55,13 +55,13 @@ impl Instantiation {
#[derive(Debug)]
pub struct Connection {
pub num_regs : u32,
pub from : SpanWireID,
pub from : SpanFlatID,
pub to : SpanConnectionWrite,
pub condition : WireID
pub condition : FlatID
}

struct FlatteningContext<'l, 'm> {
instantiations : ListAllocator<Instantiation, WireIDMarker>,
instantiations : ListAllocator<Instantiation, FlatIDMarker>,
connections : Vec<Connection>,
errors : ErrorCollector,

Expand All @@ -70,7 +70,7 @@ struct FlatteningContext<'l, 'm> {
}

impl<'l, 'm> FlatteningContext<'l, 'm> {
fn typecheck(&self, wire : SpanWireID, expected : &Type, context : &str) -> Option<()> {
fn typecheck(&self, wire : SpanFlatID, expected : &Type, context : &str) -> Option<()> {
let found = self.instantiations[wire.0].get_type();
typecheck(found, wire.1, expected, context, self.linker, &self.errors)
}
Expand All @@ -83,7 +83,7 @@ impl<'l, 'm> FlatteningContext<'l, 'm> {
TypeExpression::Array(b) => {
let (array_type_expr, array_size_expr) = b.deref();
let array_element_type = self.map_to_type(array_type_expr, global_references)?;
let array_size_wire = self.flatten_single_expr(array_size_expr, WireID::INVALID)?;
let array_size_wire = self.flatten_single_expr(array_size_expr, FlatID::INVALID)?;
Some(Type::Array(Box::new((array_element_type, array_size_wire.0))))
},
}
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'l, 'm> FlatteningContext<'l, 'm> {

Some(())
}
fn desugar_func_call(&mut self, func_and_args : &[SpanExpression], closing_bracket_pos : usize, condition : WireID) -> Option<(&'l Module, WireID, Range<FieldID>)> {
fn desugar_func_call(&mut self, func_and_args : &[SpanExpression], closing_bracket_pos : usize, condition : FlatID) -> Option<(&'l Module, FlatID, Range<FieldID>)> {
let (name_expr, name_expr_span) = &func_and_args[0]; // Function name is always there
let func_instantiation = match name_expr {
Expression::Named(LocalOrGlobal::Local(l)) => {
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<'l, 'm> FlatteningContext<'l, 'm> {

Some((md, func_instantiation, output_range))
}
fn flatten_single_expr(&mut self, (expr, expr_span) : &SpanExpression, condition : WireID) -> Option<SpanWireID> {
fn flatten_single_expr(&mut self, (expr, expr_span) : &SpanExpression, condition : FlatID) -> Option<SpanFlatID> {
let single_connection_side = match expr {
Expression::Named(LocalOrGlobal::Local(l)) => {
*l
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<'l, 'm> FlatteningContext<'l, 'm> {
};
Some((single_connection_side, *expr_span))
}
fn flatten_assignable_expr(&mut self, (expr, span) : &SpanAssignableExpression, condition : WireID) -> Option<SpanConnectionWrite> {
fn flatten_assignable_expr(&mut self, (expr, span) : &SpanAssignableExpression, condition : FlatID) -> Option<SpanConnectionWrite> {
Some((match expr {
AssignableExpression::Named{local_idx} => {
ConnectionWrite::Local(*local_idx)
Expand All @@ -253,16 +253,16 @@ impl<'l, 'm> FlatteningContext<'l, 'm> {
}
}, *span))
}
fn extend_condition(&mut self, condition : WireID, additional_condition : SpanWireID) -> WireID {
if condition == WireID::INVALID {
fn extend_condition(&mut self, condition : FlatID, additional_condition : SpanFlatID) -> FlatID {
if condition == FlatID::INVALID {
additional_condition.0
} else {
let bool_typ = Type::Named(get_builtin_uuid("bool"));
assert!(*self.instantiations[condition].get_type() == bool_typ);
self.instantiations.alloc(Instantiation::BinaryOp{typ : bool_typ, op: Operator{op_typ : kw("&")}, left : (condition, additional_condition.1), right : additional_condition})
}
}
fn flatten_code(&mut self, code : &CodeBlock, condition : WireID) {
fn flatten_code(&mut self, code : &CodeBlock, condition : FlatID) {
for (stmt, stmt_span) in &code.statements {
match stmt {
Statement::Declaration(local_id) => {
Expand Down Expand Up @@ -388,14 +388,14 @@ pub fn flatten(flattened : FlattenedModule, module : &Module, linker : &Linker)
module,
linker,
};
context.flatten_code(&module.code, WireID::INVALID);
context.flatten_code(&module.code, FlatID::INVALID);

FlattenedModule{instantiations: context.instantiations, connections: context.connections, errors : context.errors}
}

#[derive(Debug)]
pub struct FlattenedInterfacePort {
wire_id : WireID,
wire_id : FlatID,
is_input : bool,
typ : Type,
port_name : Box<str>,
Expand Down Expand Up @@ -434,7 +434,7 @@ impl FlattenedInterface {

#[derive(Debug)]
pub struct FlattenedModule {
pub instantiations : ListAllocator<Instantiation, WireIDMarker>,
pub instantiations : ListAllocator<Instantiation, FlatIDMarker>,
pub connections : Vec<Connection>,
pub errors : ErrorCollector
}
Expand Down
24 changes: 12 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

use num::bigint::BigUint;

use crate::{tokenizer::*, errors::*, ast::*, linker::{FileUUID, NamedUUID}, flattening::{WireID, WireIDMarker, FlattenedModule, FlattenedInterface}, arena_alloc::ListAllocator};
use crate::{tokenizer::*, errors::*, ast::*, linker::{FileUUID, NamedUUID}, flattening::{FlatID, FlatIDMarker, FlattenedModule, FlattenedInterface}, arena_alloc::ListAllocator};

use std::{iter::Peekable, str::FromStr, ops::Range};
use core::slice::Iter;
Expand Down Expand Up @@ -105,12 +105,12 @@ pub fn to_token_hierarchy(tokens : &[Token], errors : &mut ErrorCollector) -> Ve
}

struct LocalVariableContext<'prev, 'file> {
locals : Vec<(&'file str, WireID)>,
locals : Vec<(&'file str, FlatID)>,
prev : Option<&'prev LocalVariableContext<'prev, 'file>>
}

impl<'prev, 'file> LocalVariableContext<'prev, 'file> {
pub fn get_declaration_for(&self, name : &'file str) -> Option<WireID> {
pub fn get_declaration_for(&self, name : &'file str) -> Option<FlatID> {
for (decl_name, unique_id) in &self.locals {
if *decl_name == name {
return Some(*unique_id);
Expand All @@ -122,7 +122,7 @@ impl<'prev, 'file> LocalVariableContext<'prev, 'file> {
None
}
}
pub fn add_declaration(&mut self, new_local_name : &'file str, new_local_unique_id : WireID) -> Result<(), WireID> { // Returns conflicting signal declaration
pub fn add_declaration(&mut self, new_local_name : &'file str, new_local_unique_id : FlatID) -> Result<(), FlatID> { // Returns conflicting signal declaration
for (existing_local_name, existing_local_id) in &self.locals {
if new_local_name == *existing_local_name {
return Err(*existing_local_id)
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
}
}

fn add_declaration(&mut self, type_expr : SpanTypeExpression, name : TokenContent, identifier_type : IdentifierType, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) -> WireID {
fn add_declaration(&mut self, type_expr : SpanTypeExpression, name : TokenContent, identifier_type : IdentifierType, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) -> FlatID {
let span = Span(type_expr.1.0, name.position);
let decl = SignalDeclaration{typ : type_expr, span, name : self.file_text[name.text.clone()].into(), identifier_type};
let decl_id = declarations.alloc(decl);
Expand Down Expand Up @@ -430,7 +430,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
}
}

fn parse_signal_declaration(&mut self, token_stream : &mut TokenStream, identifier_type : IdentifierType, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) -> Option<()> {
fn parse_signal_declaration(&mut self, token_stream : &mut TokenStream, identifier_type : IdentifierType, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) -> Option<()> {
let sig_type = self.try_parse_type(token_stream, scope)?;
let name = self.eat_identifier(token_stream, "signal declaration")?;
self.add_declaration(sig_type, name, identifier_type, declarations, scope);
Expand All @@ -450,7 +450,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
Some(cur_type)
}

fn try_parse_declaration(&mut self, token_stream : &mut TokenStream, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) -> Option<(WireID, Span)> {
fn try_parse_declaration(&mut self, token_stream : &mut TokenStream, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) -> Option<(FlatID, Span)> {
let identifier_type = if token_stream.eat_is_plain(kw("state")).is_some() {
IdentifierType::State
} else {
Expand All @@ -463,7 +463,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
Some((local_idx, Span::from(name_token.position)))
}

fn parse_bundle(&mut self, token_stream : &mut TokenStream, identifier_type : IdentifierType, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) {
fn parse_bundle(&mut self, token_stream : &mut TokenStream, identifier_type : IdentifierType, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) {
while token_stream.peek_is_plain(TOKEN_IDENTIFIER) {
if let Some(_) = self.parse_signal_declaration(token_stream, identifier_type, declarations, scope) {

Expand All @@ -478,15 +478,15 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
}
}

fn parse_interface(&mut self, token_stream : &mut TokenStream, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) {
fn parse_interface(&mut self, token_stream : &mut TokenStream, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &mut LocalVariableContext<'_, 'file>) {
self.parse_bundle(token_stream, IdentifierType::Input, declarations, scope);

if token_stream.eat_is_plain(kw("->")).is_some() {
self.parse_bundle(token_stream, IdentifierType::Output, declarations, scope);
}
}

fn parse_statement(&mut self, token_stream : &mut TokenStream, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &mut LocalVariableContext<'_, 'file>, code_block : &mut CodeBlock) -> Option<()> {
fn parse_statement(&mut self, token_stream : &mut TokenStream, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &mut LocalVariableContext<'_, 'file>, code_block : &mut CodeBlock) -> Option<()> {
let start_at = if let Some(peek) = token_stream.peek() {
peek.get_span().0
} else {
Expand Down Expand Up @@ -613,7 +613,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {
return None;
}
}
fn parse_if_statement(&mut self, token_stream : &mut TokenStream, if_token : &TokenContent, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, scope : &LocalVariableContext<'_, 'file>) -> Option<(Statement, Span)> {
fn parse_if_statement(&mut self, token_stream : &mut TokenStream, if_token : &TokenContent, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, scope : &LocalVariableContext<'_, 'file>) -> Option<(Statement, Span)> {
let condition = self.parse_expression(token_stream, &scope)?;

let (then_block, then_block_span) = self.eat_block(token_stream, kw("{"), "Then block of if statement")?;
Expand All @@ -637,7 +637,7 @@ impl<'g, 'file> ASTParserContext<'g, 'file> {

Some((Statement::If{condition, then: then_content, els: else_content }, Span(if_token.position, span_end)))
}
fn parse_code_block(&mut self, block_tokens : &[TokenTreeNode], span : Span, declarations : &mut ListAllocator<SignalDeclaration, WireIDMarker>, outer_scope : &LocalVariableContext<'_, 'file>) -> CodeBlock {
fn parse_code_block(&mut self, block_tokens : &[TokenTreeNode], span : Span, declarations : &mut ListAllocator<SignalDeclaration, FlatIDMarker>, outer_scope : &LocalVariableContext<'_, 'file>) -> CodeBlock {
let mut token_stream = TokenStream::new(block_tokens, span.0, span.1);

let mut code_block = CodeBlock{statements : Vec::new()};
Expand Down
4 changes: 2 additions & 2 deletions src/typing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Deref;

use crate::{ast::{Value, Operator, Span}, linker::{get_builtin_uuid, NamedUUID, Linker, Linkable}, tokenizer::kw, flattening::WireID, errors::ErrorCollector};
use crate::{ast::{Value, Operator, Span}, linker::{get_builtin_uuid, NamedUUID, Linker, Linkable}, tokenizer::kw, flattening::FlatID, errors::ErrorCollector};

// Types contain everything that cannot be expressed at runtime
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -10,7 +10,7 @@ pub enum Type {
but doesn't actually take size into account for type checking as that would
make type checking too difficult. Instead delay until proper instantiation
to check array sizes, as then we have concrete numbers*/
Array(Box<(Type, WireID)>)
Array(Box<(Type, FlatID)>)
}

impl Type {
Expand Down

0 comments on commit f5b70aa

Please sign in to comment.