Skip to content

Commit

Permalink
Retrieve basic arch
Browse files Browse the repository at this point in the history
  • Loading branch information
DasLixou committed May 12, 2024
1 parent 97c68e3 commit 0a4c311
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 7 deletions.
13 changes: 13 additions & 0 deletions ohdlc/src/ir/architectures.rs
Original file line number Diff line number Diff line change
@@ -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>);
1 change: 1 addition & 0 deletions ohdlc/src/ir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod architectures;
pub mod import_bucket;
pub mod modules;
pub mod name_lookup;
Expand Down
12 changes: 9 additions & 3 deletions ohdlc/src/ir/registry.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
pub struct Registry<T, A> {
pub modules: ModuleRegistry,
pub types: TypeRegistry<T>,
pub architectures: ArchRegistry<A>,
}

pub type ModuleRegistry = SimpleSurotto<ModuleId, Module>;
pub type TypeRegistry<T> = SimpleSurotto<TypeId, T>;
pub type ArchRegistry<A> = SimpleSurotto<ArchId, A>;

impl<T> Default for Registry<T> {
impl<T, A> Default for Registry<T, A> {
fn default() -> Self {
Self {
modules: SimpleSurotto::new(),
types: SimpleSurotto::new(),
architectures: SimpleSurotto::new(),
}
}
}
Expand Down
83 changes: 83 additions & 0 deletions ohdlc/src/ir/stages/architectures/mod.rs
Original file line number Diff line number Diff line change
@@ -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<RefinedType<'ir>, 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<TypeId> {
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;
}
}
3 changes: 2 additions & 1 deletion ohdlc/src/ir/stages/flatten_lookup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,7 +16,7 @@ use crate::{
use super::rough::types::RoughType;

pub struct FlattenLookupStage<'ir, 'b, 'ast> {
pub registry: &'b Registry<RoughType<'ast>>,
pub registry: &'b Registry<RoughType<'ast>, RoughArch<'ast>>,
pub name_lookup: PreFlattenNameLookup,
pub import_bucket: ImportBucket<'ir>,
pub resolvables: Vec<ImportId>,
Expand Down
2 changes: 2 additions & 0 deletions ohdlc/src/ir/stages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub mod rough;
pub mod flatten_lookup;

pub mod refine_types;

pub mod architectures;
1 change: 1 addition & 0 deletions ohdlc/src/ir/stages/refine_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeId> {
let mut lookup_scope = match ty.path.0 .1 {
ast::PathStart::Root => self.name_lookup.root,
Expand Down
7 changes: 5 additions & 2 deletions ohdlc/src/ir/stages/rough/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -18,7 +19,7 @@ pub mod types;

pub struct RoughStage<'ir, 'b, 'ast> {
pub arena: &'ir Bump,
pub registry: &'b mut Registry<RoughType<'ast>>,
pub registry: &'b mut Registry<RoughType<'ast>, RoughArch<'ast>>,
pub name_lookup: &'b mut PreFlattenNameLookup,
pub import_bucket: &'b mut ImportBucket<'ir>,
pub root: &'ast [Spanned<ast::Item<'ast>>],
Expand All @@ -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));
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion ohdlc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}

Expand Down
1 change: 1 addition & 0 deletions ohdlc/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fn main() {
Registry {
modules: registry.modules,
types: refined_types,
architectures: registry.architectures,
}
};

Expand Down

0 comments on commit 0a4c311

Please sign in to comment.