Skip to content

Commit

Permalink
feat(rust-sema): Lower inner functions to MIR as normal
Browse files Browse the repository at this point in the history
  • Loading branch information
chorman0773 committed Nov 9, 2023
1 parent 0b37220 commit 33403c1
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 109 deletions.
6 changes: 6 additions & 0 deletions rust/src/irgen/name_visitor/mangler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ impl Mangle for SeqId {
}
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct LocalItem<'a> {
pub prefix: &'a [Symbol],
pub tail: &'a [Symbol],
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[non_exhaustive]
pub enum Substitution {
Expand Down
1 change: 1 addition & 0 deletions rust/src/irgen/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ def_visitors! {
}

pub trait FunctionBodyVisitor {
fn visit_inner_value(&mut self) -> Option<Box<dyn ValueDefVisitor + '_>>;
fn visit_basic_block(&mut self) -> Option<Box<dyn BasicBlockVisitor + '_>>;
}

Expand Down
63 changes: 45 additions & 18 deletions rust/src/irgen/xir_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub struct XirValueDefVisitor<'a> {
deftys: &'a HashMap<DefId, ir::Type>,
file: &'a mut ir::File,
name: Option<Symbol>,
scope_member: Option<ir::ScopeMember>,
properties: &'a TargetProperties<'a>,
}

Expand All @@ -221,6 +222,7 @@ impl<'a> XirValueDefVisitor<'a> {
deftys,
file,
name: None,
scope_member: None,
properties,
}
}
Expand All @@ -241,36 +243,44 @@ impl<'a> ValueDefVisitor for XirValueDefVisitor<'a> {
}

fn visit_function(&mut self) -> Option<Box<dyn FunctionDefVisitor + '_>> {
let path = ir::Path {
components: vec![ir::PathComponent::Text(
(&*self.name.expect("name should have been set previously")).into(),
)],
};

self.file.root.members.insert(
path.clone(),
ir::ScopeMember {
annotations: ir::AnnotatedElement::default(),
vis: ir::Visibility::Public,
member_decl: ir::MemberDeclaration::Function(ir::FunctionDeclaration::default()),
},
);
self.scope_member = Some(ir::ScopeMember {
annotations: ir::AnnotatedElement::default(),
vis: ir::Visibility::Public,
member_decl: ir::MemberDeclaration::Function(ir::FunctionDeclaration::default()),
});

let def = match &mut self.file.root.members.get_mut(&path).unwrap().member_decl {
ir::MemberDeclaration::Function(fndef) => fndef,
_ => unreachable!(),
};
let def: &mut ir::FunctionDeclaration =
match &mut self.scope_member.as_mut().unwrap().member_decl {
ir::MemberDeclaration::Function(fndef) => fndef,
_ => unreachable!(),
};

Some(Box::new(XirFunctionDefVisitor::new(
self.defs,
self.names,
self.deftys,
self.file,
def,
self.properties,
)))
}
}

impl<'a> Drop for XirValueDefVisitor<'a> {
fn drop(&mut self) {
let name = self.name.expect("defid should already be set");
let path = ir::Path {
components: vec![ir::PathComponent::Text((&name).into())],
};
self.file.root.members.insert(
path,
self.scope_member
.take()
.expect("this was supposed to be visited"),
);
}
}

pub struct XirFunctionTypeGatherer<'a> {
defs: &'a Definitions,
names: &'a NameMap,
Expand Down Expand Up @@ -499,6 +509,7 @@ pub struct XirFunctionDefVisitor<'a> {
defs: &'a Definitions,
names: &'a NameMap,
deftys: &'a HashMap<DefId, ir::Type>,
file: &'a mut ir::File,
fndef: &'a mut ir::FunctionDeclaration,
properties: &'a TargetProperties<'a>,
}
Expand All @@ -508,13 +519,15 @@ impl<'a> XirFunctionDefVisitor<'a> {
defs: &'a Definitions,
names: &'a NameMap,
deftys: &'a HashMap<DefId, ir::Type>,
file: &'a mut ir::File,
fndef: &'a mut ir::FunctionDeclaration,
properties: &'a TargetProperties<'a>,
) -> Self {
Self {
defs,
names,
deftys,
file,
fndef,
properties,
}
Expand All @@ -537,6 +550,7 @@ impl<'a> FunctionDefVisitor for XirFunctionDefVisitor<'a> {
self.names,
self.properties,
self.deftys,
self.file,
&mut self.fndef.ty,
self.fndef.body.insert(ir::FunctionBody::default()),
)))
Expand Down Expand Up @@ -918,6 +932,7 @@ pub struct XirFunctionBodyVisitor<'a> {
names: &'a NameMap,
properties: &'a TargetProperties<'a>,
deftys: &'a HashMap<DefId, ir::Type>,
file: &'a mut ir::File,
cur_fnty: &'a mut ir::FnType,
fndecl: &'a mut ir::FunctionBody,
targs: HashMap<BasicBlockId, Vec<ir::StackItem>>,
Expand All @@ -931,6 +946,7 @@ impl<'a> XirFunctionBodyVisitor<'a> {
names: &'a NameMap,
properties: &'a TargetProperties<'a>,
deftys: &'a HashMap<DefId, ir::Type>,
file: &'a mut ir::File,
cur_fnty: &'a mut ir::FnType,
fndecl: &'a mut ir::FunctionBody,
) -> Self {
Expand All @@ -939,6 +955,7 @@ impl<'a> XirFunctionBodyVisitor<'a> {
names,
properties,
deftys,
file,
fndecl,
cur_fnty,
targs: HashMap::new(),
Expand Down Expand Up @@ -976,6 +993,16 @@ impl<'a> FunctionBodyVisitor for XirFunctionBodyVisitor<'a> {
&mut self.ssa_tys,
)))
}

fn visit_inner_value(&mut self) -> Option<Box<dyn ValueDefVisitor + '_>> {
Some(Box::new(XirValueDefVisitor::new(
self.defs,
self.names,
self.deftys,
self.file,
self.properties,
)))
}
}

pub struct XirBasicBlockVisitor<'a> {
Expand Down
Loading

0 comments on commit 33403c1

Please sign in to comment.