Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Architecture IR #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ohdlc/src/ir/registries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use super::{
modules::{Module, ModuleId},
stages::{
refine::registries::{Entity, Type},
rough::registries::{RoughEntity, RoughType},
rough::registries::{RoughArch, RoughEntity, RoughType},
},
};

pub type ModuleRegistry = SimpleSurotto<ModuleId, Module>;
pub type RoughTypeRegistry<'ast> = SimpleSurotto<TypeId, RoughType<'ast>>;
pub type RoughEntityRegistry<'ast> = SimpleSurotto<EntityId, RoughEntity<'ast>>;
pub type RoughArchRegistry<'ast> = SimpleSurotto<ArchId, RoughArch<'ast>>;
pub type RefinedTypeRegistry<'ir> = SimpleSurotto<TypeId, Type<'ir>>;
pub type RefinedEntityRegistry<'ir> = SimpleSurotto<EntityId, Entity<'ir>>;

Expand All @@ -21,3 +22,7 @@ simple_key!(
simple_key!(
pub struct EntityId;
);

simple_key!(
pub struct ArchId;
);
87 changes: 87 additions & 0 deletions ohdlc/src/ir/stages/architectures/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use bumpalo::Bump;

use crate::{
ast,
ir::{
import_bucket::LookupStrategy,
name_lookup::{PostFlattenNameLookup, Resolved, ScopeId},
registries::{ArchId, EntityId, ModuleRegistry, RoughArchRegistry},
},
message::Message,
MESSAGES,
};

use super::rough::registries::RoughArch;

pub struct ArchitectureStage<'ir, 'b, 'ast> {
pub arena: &'ir Bump,
pub name_lookup: &'b PostFlattenNameLookup,
pub module_reg: &'b ModuleRegistry,
pub arch_reg: &'b RoughArchRegistry<'ast>,
}

impl<'ir, 'ast> ArchitectureStage<'ir, '_, 'ast> {
pub fn lower(self) {
for (id, arch) in self.arch_reg.iter() {
self.lower_arch(id, arch)
}
}

fn lower_arch(&self, id: ArchId, arch: &RoughArch<'ast>) {
let ty = self.lookup_entity(arch.0, &arch.1.ty);
println!("Implementing arch '{}' for {:?}", arch.1.name.0.get(), ty);
}

// TODO: similar to one in `architectures`, fix duplicate pls
fn lookup_entity(&self, scope: ScopeId, ty: &ast::Type) -> Option<EntityId> {
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::Entity(_))) => {
MESSAGES.report(Message::use_continues_after_type(segment.1));
return None;
}
(false, Some(Resolved::Module(m))) => {
lookup_scope = self.module_reg[*m].scope;
}

(true, Some(Resolved::Entity(t))) => return Some(*t),
(true, Some(Resolved::Module(_))) => {
MESSAGES.report(Message::wrong_path_end(segment, "Entity", "Module"));
return None;
}

(_, Some(Resolved::Type(_))) => {
// TODO: better error, this can also appear at non-end position
MESSAGES.report(Message::wrong_path_end(segment, "Entity", "Type"))
}
(_, None) => {
MESSAGES.report(Message::could_not_resolve(segment));
return None;
}
}

is_start = false;
}

return None;
}
}
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;

pub mod architectures;
1 change: 1 addition & 0 deletions ohdlc/src/ir/stages/refine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl<'ir, 'ast> RefineStage<'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
9 changes: 6 additions & 3 deletions ohdlc/src/ir/stages/rough/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::{
import_bucket::{Import, ImportBucket, LookupStrategy},
modules::Module,
name_lookup::{PreFlattenNameLookup, Resolvable, Resolved, ScopeId},
registries::{ModuleRegistry, RoughEntityRegistry, RoughTypeRegistry},
registries::{ModuleRegistry, RoughArchRegistry, RoughEntityRegistry, RoughTypeRegistry},
},
span::Spanned,
symbol::Ident,
};

use self::registries::{RoughEntity, RoughType};
use self::registries::{RoughArch, RoughEntity, RoughType};

pub mod registries;

Expand All @@ -23,6 +23,7 @@ pub struct RoughStage<'ir, 'b, 'ast> {
pub module_reg: &'b mut ModuleRegistry,
pub type_reg: &'b mut RoughTypeRegistry<'ast>,
pub entity_reg: &'b mut RoughEntityRegistry<'ast>,
pub arch_reg: &'b mut RoughArchRegistry<'ast>,
pub root: &'ast [Spanned<ast::Item<'ast>>],
}

Expand All @@ -40,7 +41,9 @@ impl<'ir, 'ast> RoughStage<'ir, '_, 'ast> {
ast::Item::Entity(e) => self.introduce_entity(scope, e.name, RoughEntity(scope, e)),
ast::Item::Record(r) => self.introduce_type(scope, r.name, RoughType::Record(scope, r)),
ast::Item::Enum(e) => self.introduce_type(scope, e.name, RoughType::Enum(e)),
ast::Item::Arch(_) => {}
ast::Item::Arch(a) => {
self.arch_reg.insert(RoughArch(scope, a));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions ohdlc/src/ir/stages/rough/registries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pub enum RoughType<'ast> {

#[derive(Debug)]
pub struct RoughEntity<'ast>(pub ScopeId, pub &'ast ast::Entity);

#[derive(Debug)]
pub struct RoughArch<'ast>(pub ScopeId, pub &'ast ast::Arch<'ast>);
20 changes: 18 additions & 2 deletions ohdlc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use ohdlc::{
ir::{
import_bucket::ImportBucket,
name_lookup::NameLookup,
registries::{ModuleRegistry, RoughEntityRegistry, RoughTypeRegistry},
stages::{flatten_lookup::FlattenLookupStage, refine::RefineStage, rough::RoughStage},
registries::{ModuleRegistry, RoughArchRegistry, RoughEntityRegistry, RoughTypeRegistry},
stages::{
architectures::ArchitectureStage, flatten_lookup::FlattenLookupStage,
refine::RefineStage, rough::RoughStage,
},
},
lexer::Lexer,
parser::Parser,
Expand Down Expand Up @@ -44,6 +47,7 @@ fn main() -> Result<(), ()> {
let mut module_reg = ModuleRegistry::default();
let mut type_reg = RoughTypeRegistry::default();
let mut entity_reg = RoughEntityRegistry::default();
let mut arch_reg = RoughArchRegistry::default();
let mut name_lookup = NameLookup::new();
let mut import_bucket = ImportBucket::new();

Expand All @@ -55,6 +59,7 @@ fn main() -> Result<(), ()> {
module_reg: &mut module_reg,
type_reg: &mut type_reg,
entity_reg: &mut entity_reg,
arch_reg: &mut arch_reg,
root: &root,
};
rough.lower();
Expand Down Expand Up @@ -93,6 +98,17 @@ fn main() -> Result<(), ()> {
println!("{type_reg:#?}");
println!("{entity_reg:#?}");

{
let architectures = ArchitectureStage {
arena: &ir_arena,
name_lookup: &name_lookup,
module_reg: &module_reg,
arch_reg: &arch_reg,
};
architectures.lower();
report_messages(&source);
};

Ok(())
}

Expand Down
20 changes: 18 additions & 2 deletions ohdlc/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ use ohdlc::{
ir::{
import_bucket::ImportBucket,
name_lookup::NameLookup,
registries::{ModuleRegistry, RoughEntityRegistry, RoughTypeRegistry},
stages::{flatten_lookup::FlattenLookupStage, refine::RefineStage, rough::RoughStage},
registries::{ModuleRegistry, RoughArchRegistry, RoughEntityRegistry, RoughTypeRegistry},
stages::{
architectures::ArchitectureStage, flatten_lookup::FlattenLookupStage,
refine::RefineStage, rough::RoughStage,
},
},
lexer::Lexer,
parser::Parser,
Expand Down Expand Up @@ -45,6 +48,7 @@ fn main() {
let mut module_reg = ModuleRegistry::default();
let mut type_reg = RoughTypeRegistry::default();
let mut entity_reg = RoughEntityRegistry::default();
let mut arch_reg = RoughArchRegistry::default();
let mut name_lookup = NameLookup::new();
let mut import_bucket = ImportBucket::new();

Expand All @@ -56,6 +60,7 @@ fn main() {
module_reg: &mut module_reg,
type_reg: &mut type_reg,
entity_reg: &mut entity_reg,
arch_reg: &mut arch_reg,
root: &root,
};
rough.lower();
Expand Down Expand Up @@ -93,6 +98,17 @@ fn main() {

assert_debug_snapshot!(type_reg);
assert_debug_snapshot!(entity_reg);

{
let architectures = ArchitectureStage {
arena: &ir_arena,
name_lookup: &name_lookup,
module_reg: &module_reg,
arch_reg: &arch_reg,
};
architectures.lower();
report_messages(&source);
};
}

fn report_messages(source: &Source<'_>) {
Expand Down
Loading