From 0a4c3110a48318ab0e8e1032e855a8e059377dd4 Mon Sep 17 00:00:00 2001 From: DasLixou Date: Sun, 12 May 2024 19:57:51 +0200 Subject: [PATCH] Retrieve basic arch --- ohdlc/src/ir/architectures.rs | 13 ++++ ohdlc/src/ir/mod.rs | 1 + ohdlc/src/ir/registry.rs | 12 +++- ohdlc/src/ir/stages/architectures/mod.rs | 83 +++++++++++++++++++++++ ohdlc/src/ir/stages/flatten_lookup/mod.rs | 3 +- ohdlc/src/ir/stages/mod.rs | 2 + ohdlc/src/ir/stages/refine_types/mod.rs | 1 + ohdlc/src/ir/stages/rough/mod.rs | 7 +- ohdlc/src/main.rs | 14 +++- ohdlc/tests/basic.rs | 1 + 10 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 ohdlc/src/ir/architectures.rs create mode 100644 ohdlc/src/ir/stages/architectures/mod.rs diff --git a/ohdlc/src/ir/architectures.rs b/ohdlc/src/ir/architectures.rs new file mode 100644 index 0000000..dac1154 --- /dev/null +++ b/ohdlc/src/ir/architectures.rs @@ -0,0 +1,13 @@ +use std::fmt::Debug; +use surotto::simple_key; + +use crate::ast; + +use super::name_lookup::ScopeId; + +simple_key!( + pub struct ArchId; +); + +#[derive(Debug)] +pub struct RoughArch<'ast>(pub ScopeId, pub &'ast ast::Arch<'ast>); diff --git a/ohdlc/src/ir/mod.rs b/ohdlc/src/ir/mod.rs index 1e4b3ba..287c0ea 100644 --- a/ohdlc/src/ir/mod.rs +++ b/ohdlc/src/ir/mod.rs @@ -1,3 +1,4 @@ +pub mod architectures; pub mod import_bucket; pub mod modules; pub mod name_lookup; diff --git a/ohdlc/src/ir/registry.rs b/ohdlc/src/ir/registry.rs index c765a55..0c5c7b6 100644 --- a/ohdlc/src/ir/registry.rs +++ b/ohdlc/src/ir/registry.rs @@ -1,21 +1,27 @@ use surotto::{simple::SimpleSurotto, simple_key}; -use super::modules::{Module, ModuleId}; +use super::{ + architectures::ArchId, + modules::{Module, ModuleId}, +}; #[derive(Debug)] -pub struct Registry { +pub struct Registry { pub modules: ModuleRegistry, pub types: TypeRegistry, + pub architectures: ArchRegistry, } pub type ModuleRegistry = SimpleSurotto; pub type TypeRegistry = SimpleSurotto; +pub type ArchRegistry = SimpleSurotto; -impl Default for Registry { +impl Default for Registry { fn default() -> Self { Self { modules: SimpleSurotto::new(), types: SimpleSurotto::new(), + architectures: SimpleSurotto::new(), } } } diff --git a/ohdlc/src/ir/stages/architectures/mod.rs b/ohdlc/src/ir/stages/architectures/mod.rs new file mode 100644 index 0000000..be6bb31 --- /dev/null +++ b/ohdlc/src/ir/stages/architectures/mod.rs @@ -0,0 +1,83 @@ +use bumpalo::Bump; + +use crate::{ + ast, + ir::{ + architectures::{ArchId, RoughArch}, + import_bucket::LookupStrategy, + name_lookup::{PostFlattenNameLookup, Resolved, ScopeId}, + registry::{Registry, TypeId}, + }, + message::Message, + MESSAGES, +}; + +use super::refine_types::types::RefinedType; + +pub struct ArchitectureStage<'ir, 'b, 'ast> { + pub arena: &'ir Bump, + pub name_lookup: &'b PostFlattenNameLookup, + pub registry: &'ast Registry, RoughArch<'ast>>, +} + +impl<'ir, 'ast> ArchitectureStage<'ir, '_, 'ast> { + pub fn lower(self) { + for (id, arch) in self.registry.architectures.iter() { + self.lower_arch(id, arch) + } + } + + fn lower_arch(&self, id: ArchId, arch: &RoughArch<'ast>) { + let ty = self.lookup_type(arch.0, &arch.1.ty); + println!("Implementing arch '{}' for {:?}", arch.1.name.0.get(), ty); + } + + // TODO: equal to one in `architectures`, fix duplicate pls + fn lookup_type(&self, scope: ScopeId, ty: &ast::Type) -> Option { + let mut lookup_scope = match ty.path.0 .1 { + ast::PathStart::Root => self.name_lookup.root, + ast::PathStart::Local => scope, + }; + + let mut path = ty.path.0 .0.iter().peekable(); + let mut is_start = true; + while let Some(segment) = path.next() { + let is_terminal = path.peek().is_none(); + let segment = segment.0; + + let lookup = self.name_lookup.lookup( + lookup_scope, + &segment, + if is_start { + LookupStrategy::Indirect + } else { + LookupStrategy::Direct + }, + ); + match (is_terminal, lookup) { + (false, Some(Resolved::Type(_))) => { + MESSAGES.report(Message::use_continues_after_type(segment.1)); + return None; + } + (false, Some(Resolved::Module(m))) => { + lookup_scope = self.registry.modules[*m].scope; + } + + (true, Some(Resolved::Type(t))) => return Some(*t), + (true, Some(Resolved::Module(_))) => { + MESSAGES.report(Message::wrong_path_end(segment, "Type", "Module")); + return None; + } + + (_, None) => { + MESSAGES.report(Message::could_not_resolve(segment)); + return None; + } + } + + is_start = false; + } + + return None; + } +} diff --git a/ohdlc/src/ir/stages/flatten_lookup/mod.rs b/ohdlc/src/ir/stages/flatten_lookup/mod.rs index f8b27fd..e9f0dba 100644 --- a/ohdlc/src/ir/stages/flatten_lookup/mod.rs +++ b/ohdlc/src/ir/stages/flatten_lookup/mod.rs @@ -2,6 +2,7 @@ use std::mem::MaybeUninit; use crate::{ ir::{ + architectures::RoughArch, import_bucket::{ImportBucket, ImportId, LookupStrategy}, name_lookup::{ LookupScope, PostFlattenNameLookup, PreFlattenNameLookup, Resolvable, Resolved, @@ -15,7 +16,7 @@ use crate::{ use super::rough::types::RoughType; pub struct FlattenLookupStage<'ir, 'b, 'ast> { - pub registry: &'b Registry>, + pub registry: &'b Registry, RoughArch<'ast>>, pub name_lookup: PreFlattenNameLookup, pub import_bucket: ImportBucket<'ir>, pub resolvables: Vec, diff --git a/ohdlc/src/ir/stages/mod.rs b/ohdlc/src/ir/stages/mod.rs index ebb4e40..1495923 100644 --- a/ohdlc/src/ir/stages/mod.rs +++ b/ohdlc/src/ir/stages/mod.rs @@ -3,3 +3,5 @@ pub mod rough; pub mod flatten_lookup; pub mod refine_types; + +pub mod architectures; diff --git a/ohdlc/src/ir/stages/refine_types/mod.rs b/ohdlc/src/ir/stages/refine_types/mod.rs index 5c46b18..a82238c 100644 --- a/ohdlc/src/ir/stages/refine_types/mod.rs +++ b/ohdlc/src/ir/stages/refine_types/mod.rs @@ -73,6 +73,7 @@ impl<'ir, 'ast> RefineTypesStage<'ir, '_> { }) } + // TODO: equal to one in `architectures`, fix duplicate pls fn lookup_type(&self, scope: ScopeId, ty: &ast::Type) -> Option { let mut lookup_scope = match ty.path.0 .1 { ast::PathStart::Root => self.name_lookup.root, diff --git a/ohdlc/src/ir/stages/rough/mod.rs b/ohdlc/src/ir/stages/rough/mod.rs index 42518c2..ad19287 100644 --- a/ohdlc/src/ir/stages/rough/mod.rs +++ b/ohdlc/src/ir/stages/rough/mod.rs @@ -3,6 +3,7 @@ use bumpalo::Bump; use crate::{ ast, ir::{ + architectures::RoughArch, import_bucket::{Import, ImportBucket, LookupStrategy}, modules::Module, name_lookup::{PreFlattenNameLookup, Resolvable, Resolved, ScopeId}, @@ -18,7 +19,7 @@ pub mod types; pub struct RoughStage<'ir, 'b, 'ast> { pub arena: &'ir Bump, - pub registry: &'b mut Registry>, + pub registry: &'b mut Registry, RoughArch<'ast>>, pub name_lookup: &'b mut PreFlattenNameLookup, pub import_bucket: &'b mut ImportBucket<'ir>, pub root: &'ast [Spanned>], @@ -38,7 +39,9 @@ impl<'ir, 'ast> RoughStage<'ir, '_, 'ast> { ast::Item::Entity(e) => self.introduce_type(scope, e.name, RoughTypeItem::Entity(e)), ast::Item::Record(r) => self.introduce_type(scope, r.name, RoughTypeItem::Record(r)), ast::Item::Enum(e) => self.introduce_type(scope, e.name, RoughTypeItem::Enum(e)), - ast::Item::Arch(_) => {} + ast::Item::Arch(a) => { + self.registry.architectures.insert(RoughArch(scope, a)); + } } } diff --git a/ohdlc/src/main.rs b/ohdlc/src/main.rs index c75f2e2..4ba1827 100644 --- a/ohdlc/src/main.rs +++ b/ohdlc/src/main.rs @@ -9,7 +9,8 @@ use ohdlc::{ name_lookup::NameLookup, registry::Registry, stages::{ - flatten_lookup::FlattenLookupStage, refine_types::RefineTypesStage, rough::RoughStage, + architectures::ArchitectureStage, flatten_lookup::FlattenLookupStage, + refine_types::RefineTypesStage, rough::RoughStage, }, }, lexer::Lexer, @@ -83,11 +84,22 @@ fn main() -> Result<(), ()> { Registry { modules: registry.modules, types: refined_types, + architectures: registry.architectures, } }; println!("{refined_types:#?}"); + { + let architectures = ArchitectureStage { + arena: &ir_arena, + name_lookup: &name_lookup, + registry: &refined_types, + }; + architectures.lower(); + report_messages(&source); + }; + Ok(()) } diff --git a/ohdlc/tests/basic.rs b/ohdlc/tests/basic.rs index 164ad40..993512d 100644 --- a/ohdlc/tests/basic.rs +++ b/ohdlc/tests/basic.rs @@ -84,6 +84,7 @@ fn main() { Registry { modules: registry.modules, types: refined_types, + architectures: registry.architectures, } };