Compare commits

...

8 commits

5 changed files with 1747 additions and 214 deletions

View file

@ -402,6 +402,73 @@ impl Index {
} }
} }
pub struct InternPoolWrapper(core::cell::UnsafeCell<InternPool>);
impl InternPoolWrapper {
pub fn as_mut(&self) -> &mut InternPool {
unsafe { &mut *self.0.get() }
}
pub fn as_ref(&self) -> &InternPool {
unsafe { &*self.0.get() }
}
pub fn new() -> Self {
InternPool::new().into()
}
}
impl From<InternPool> for InternPoolWrapper {
fn from(value: InternPool) -> Self {
Self(core::cell::UnsafeCell::new(value))
}
}
impl core::ops::Deref for InternPoolWrapper {
type Target = InternPool;
fn deref(&self) -> &Self::Target {
unsafe { &*self.0.get() }
}
}
impl core::ops::DerefMut for InternPoolWrapper {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}
impl AsRef<InternPool> for InternPoolWrapper {
fn as_ref(&self) -> &InternPool {
Self::as_ref(self)
}
}
impl AsRef<InternPool> for InternPool {
fn as_ref(&self) -> &InternPool {
self
}
}
// impl AsMut<InternPool> for InternPoolWrapper {
// fn as_mut(&mut self) -> &mut InternPool {
// Self::as_mut(self)
// }
// }
// impl AsMut<InternPool> for InternPool {
// fn as_mut(&mut self) -> &mut InternPool {
// self
// }
// }
impl core::fmt::Debug for InternPoolWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("InternPoolWrapper")
.field(self.as_ref())
.finish()
}
}
pub struct InternPool { pub struct InternPool {
tags: Vec<Tag>, tags: Vec<Tag>,
indices: Vec<u32>, indices: Vec<u32>,
@ -574,6 +641,17 @@ pub struct TypeInfo {
pub signed: bool, pub signed: bool,
} }
impl TypeInfo {
/// byte size
pub fn size(&self) -> u32 {
self.bitsize.div_ceil(8)
}
/// byte align
pub fn align(&self) -> u32 {
self.bitalign.div_ceil(8)
}
}
impl InternPool { impl InternPool {
pub fn peer_type(&mut self, lhs: Index, rhs: Index) -> Option<Index> { pub fn peer_type(&mut self, lhs: Index, rhs: Index) -> Option<Index> {
if lhs == rhs { if lhs == rhs {
@ -1297,6 +1375,13 @@ impl InternPool {
}) })
} }
pub fn try_get_pointee_type(&self, pointer: Index) -> Option<Index> {
match self.get_key(pointer) {
Key::PointerType { pointee, .. } | Key::ArrayType { pointee, .. } => Some(pointee),
_ => None,
}
}
pub fn get_pointer_type(&mut self, pointee: Index, flags: Option<PointerFlags>) -> Index { pub fn get_pointer_type(&mut self, pointee: Index, flags: Option<PointerFlags>) -> Index {
let key = Key::PointerType { let key = Key::PointerType {
pointee, pointee,
@ -1304,6 +1389,7 @@ impl InternPool {
}; };
self.get_or_insert(key) self.get_or_insert(key)
} }
pub fn try_get_pointer_type( pub fn try_get_pointer_type(
&self, &self,
pointee: Index, pointee: Index,

File diff suppressed because it is too large Load diff

View file

@ -70,3 +70,5 @@ impl BitSize for &[u8] {
bits as u32 bits as u32
} }
} }
pub fn unit<T>(_: T) {}

View file

@ -35,25 +35,13 @@ impl SymbolPath {
for node in self.0.iter().skip(1).rev() { for node in self.0.iter().skip(1).rev() {
match tree.nodes.get_node(node.unwrap()) { match tree.nodes.get_node(node.unwrap()) {
Tag::VarDecl { name, .. } => { Tag::VarDecl { name, .. } => {
_ = write!( _ = write!(&mut buf, "V{}::", tree.get_ident_str(*name).unwrap());
&mut buf,
"V{}::",
tree.get_ident_str(*name).unwrap()
);
} }
Tag::GlobalDecl { name, .. } => { Tag::GlobalDecl { name, .. } => {
_ = write!( _ = write!(&mut buf, "G{}::", tree.get_ident_str(*name).unwrap());
&mut buf,
"G{}::",
tree.get_ident_str(*name).unwrap()
);
} }
Tag::FunctionProto { name, .. } => { Tag::FunctionProto { name, .. } => {
_ = write!( _ = write!(&mut buf, "F{}::", tree.get_ident_str(*name).unwrap());
&mut buf,
"F{}::",
tree.get_ident_str(*name).unwrap()
);
} }
_ => {} _ => {}
} }
@ -142,12 +130,7 @@ impl Drop for InnerSymbolTable {
} }
impl InnerSymbolTable { impl InnerSymbolTable {
fn insert_symbol( fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
kind: SymbolKind,
) -> &SymbolRecord {
match kind { match kind {
SymbolKind::Var => { SymbolKind::Var => {
self.ordered_identifiers.push(SymbolRecord { self.ordered_identifiers.push(SymbolRecord {
@ -160,11 +143,7 @@ impl InnerSymbolTable {
} }
} }
fn insert_orderless_symbol( fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.orderless_identifiers.insert( self.orderless_identifiers.insert(
name.to_owned(), name.to_owned(),
SymbolRecord { SymbolRecord {
@ -175,11 +154,7 @@ impl InnerSymbolTable {
self.orderless_identifiers.get(name).unwrap() self.orderless_identifiers.get(name).unwrap()
} }
fn find_symbol_or_insert_with<'a, F>( fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord
&'a mut self,
name: &str,
cb: F,
) -> &'a SymbolRecord
where where
F: FnOnce() -> (AstNode, SymbolKind), F: FnOnce() -> (AstNode, SymbolKind),
{ {
@ -202,9 +177,7 @@ impl InnerSymbolTable {
.find(|(_, v)| v.decl == decl) .find(|(_, v)| v.decl == decl)
.map(|(_, v)| v) .map(|(_, v)| v)
}) })
.or_else(|| { .or_else(|| self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl)))
self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl))
})
} }
fn find_any_symbol(&self, name: &str) -> Option<&SymbolRecord> { fn find_any_symbol(&self, name: &str) -> Option<&SymbolRecord> {
@ -219,9 +192,7 @@ impl InnerSymbolTable {
self.ordered_identifiers self.ordered_identifiers
.iter() .iter()
.find(|r| r.name.as_str() == name) .find(|r| r.name.as_str() == name)
.or_else(|| { .or_else(|| self.parent_ref().and_then(|p| p.find_ordered_symbol(name)))
self.parent_ref().and_then(|p| p.find_ordered_symbol(name))
})
} }
fn find_orderless_symbol(&self, name: &str) -> Option<&SymbolRecord> { fn find_orderless_symbol(&self, name: &str) -> Option<&SymbolRecord> {
@ -315,12 +286,7 @@ impl SymbolTableWrapper {
} }
impl SymbolTableWrapper { impl SymbolTableWrapper {
pub fn insert_symbol( pub fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
kind: SymbolKind,
) -> &SymbolRecord {
self.current_mut().insert_symbol(name, node, kind) self.current_mut().insert_symbol(name, node, kind)
} }
@ -328,27 +294,15 @@ impl SymbolTableWrapper {
self.root_mut().find_orderless_symbol(name) self.root_mut().find_orderless_symbol(name)
} }
pub fn insert_root_symbol( pub fn insert_root_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.root_mut().insert_orderless_symbol(name, node) self.root_mut().insert_orderless_symbol(name, node)
} }
pub fn insert_orderless_symbol( pub fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord {
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.current_mut().insert_orderless_symbol(name, node) self.current_mut().insert_orderless_symbol(name, node)
} }
pub fn find_symbol_or_insert_with<'a, F>( pub fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord
&'a mut self,
name: &str,
cb: F,
) -> &'a SymbolRecord
where where
F: FnOnce() -> (AstNode, SymbolKind), F: FnOnce() -> (AstNode, SymbolKind),
{ {
@ -470,6 +424,40 @@ pub mod syms2 {
}, },
} }
impl Key {
pub fn kind(&self) -> Option<SymbolKind> {
match self {
Key::Symbol { kind, .. } => Some(*kind),
_ => None,
}
}
}
#[repr(u32)]
pub enum DeclKind {
Local = 1,
Parameter,
}
impl DeclKind {
pub fn from_u32(v: u32) -> Option<Self> {
match v {
1 => Some(Self::Local),
2 => Some(Self::Parameter),
_ => None,
}
}
}
impl From<SymbolKind> for Option<DeclKind> {
fn from(value: SymbolKind) -> Self {
match value {
SymbolKind::Parameter(_) => Some(DeclKind::Parameter),
SymbolKind::Local(_) => Some(DeclKind::Local),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SymbolKind { pub enum SymbolKind {
__First, __First,
@ -479,6 +467,7 @@ pub mod syms2 {
__TypeScope, __TypeScope,
Scope, Scope,
ParentScope, ParentScope,
Parameter(SourceLocation),
Local(SourceLocation), Local(SourceLocation),
__Last, __Last,
} }
@ -526,9 +515,7 @@ pub mod syms2 {
} }
let entries = self.inner.iter().map(|(key, val)| { let entries = self.inner.iter().map(|(key, val)| {
let payload = match key { let payload = match key {
Key::ScopeByIndex { .. } => { Key::ScopeByIndex { .. } => ExpandedPayload::Intern(val.as_intern()),
ExpandedPayload::Intern(val.as_intern())
}
_ => ExpandedPayload::Ast(val.as_ast()), _ => ExpandedPayload::Ast(val.as_ast()),
}; };
@ -567,7 +554,7 @@ pub mod syms2 {
scope: AstIndex, scope: AstIndex,
name: InternIndex, name: InternIndex,
loc: SourceLocation, loc: SourceLocation,
) -> Option<AstIndex> { ) -> Option<(Key, AstIndex)> {
use SymbolKind::*; use SymbolKind::*;
let range = self.inner.range( let range = self.inner.range(
Key::Symbol { Key::Symbol {
@ -581,8 +568,8 @@ pub mod syms2 {
}, },
); );
if let Some((_, payload)) = range.rev().next() { if let Some((key, payload)) = range.rev().next() {
Some(payload.as_ast()) Some((*key, payload.as_ast()))
} else { } else {
if let Some(parent) = self.inner.get(&Key::Symbol { if let Some(parent) = self.inner.get(&Key::Symbol {
scope, scope,
@ -601,7 +588,7 @@ pub mod syms2 {
scope: AstIndex, scope: AstIndex,
name: InternIndex, name: InternIndex,
loc: SourceLocation, loc: SourceLocation,
) -> Option<AstIndex> { ) -> Option<(Key, AstIndex)> {
use SymbolKind::*; use SymbolKind::*;
let range = self.inner.range( let range = self.inner.range(
Key::Symbol { Key::Symbol {
@ -615,15 +602,15 @@ pub mod syms2 {
}, },
); );
if let Some((_, payload)) = range.rev().next() { if let Some((key, payload)) = range.rev().next() {
Some(payload.as_ast()) Some((*key, payload.as_ast()))
} else { } else {
if let Some(parent) = self.inner.get(&Key::Symbol { if let Some(parent) = self.inner.get(&Key::Symbol {
scope, scope,
name: InternIndex::invalid(), name: InternIndex::invalid(),
kind: ParentScope, kind: ParentScope,
}) { }) {
self.find_symbol(parent.as_ast(), name, loc) self.find_type_symbol(parent.as_ast(), name, loc)
} else { } else {
None None
} }
@ -637,10 +624,8 @@ pub mod syms2 {
kind: SymbolKind, kind: SymbolKind,
ast: AstIndex, ast: AstIndex,
) { ) {
self.inner.insert( self.inner
Key::Symbol { scope, name, kind }, .insert(Key::Symbol { scope, name, kind }, Payload::new_ast(ast));
Payload::new_ast(ast),
);
} }
} }
} }

View file

@ -159,10 +159,13 @@ pub enum Inst {
Constant, Constant,
/// size, align /// size, align
Alloca, Alloca,
/// (pointee type)
/// src /// src
Load(intern::Index), Load(intern::Index),
/// (pointee type)
/// src, dst /// src, dst
Store(intern::Index), Store(intern::Index),
/// (pointer type)
/// ptr, index, /// ptr, index,
GetElementPtr(intern::Index), GetElementPtr(intern::Index),
/// size, align /// size, align
@ -759,8 +762,8 @@ impl<'tree, 'ir> IRBuilder<'tree, 'ir> {
} }
pub struct IR { pub struct IR {
nodes: Vec<Inst>, pub(crate) nodes: Vec<Inst>,
data: Vec<Option<Data>>, pub(crate) data: Vec<Option<Data>>,
// intern_pool: &'a mut InternPool, // intern_pool: &'a mut InternPool,
} }