Compare commits

..

No commits in common. "6cc1822ec6f51ec775744839ec6e53905e0c474c" and "379769bc59d6931f91533d68399413ca6ab86fe7" have entirely different histories.

5 changed files with 213 additions and 1746 deletions

View file

@ -402,73 +402,6 @@ 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>,
@ -641,17 +574,6 @@ 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 {
@ -1375,13 +1297,6 @@ 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,
@ -1389,7 +1304,6 @@ 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,5 +70,3 @@ impl BitSize for &[u8] {
bits as u32 bits as u32
} }
} }
pub fn unit<T>(_: T) {}

View file

@ -35,13 +35,25 @@ 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!(&mut buf, "V{}::", tree.get_ident_str(*name).unwrap()); _ = write!(
&mut buf,
"V{}::",
tree.get_ident_str(*name).unwrap()
);
} }
Tag::GlobalDecl { name, .. } => { Tag::GlobalDecl { name, .. } => {
_ = write!(&mut buf, "G{}::", tree.get_ident_str(*name).unwrap()); _ = write!(
&mut buf,
"G{}::",
tree.get_ident_str(*name).unwrap()
);
} }
Tag::FunctionProto { name, .. } => { Tag::FunctionProto { name, .. } => {
_ = write!(&mut buf, "F{}::", tree.get_ident_str(*name).unwrap()); _ = write!(
&mut buf,
"F{}::",
tree.get_ident_str(*name).unwrap()
);
} }
_ => {} _ => {}
} }
@ -130,7 +142,12 @@ impl Drop for InnerSymbolTable {
} }
impl InnerSymbolTable { impl InnerSymbolTable {
fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord { fn insert_symbol(
&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 {
@ -143,7 +160,11 @@ impl InnerSymbolTable {
} }
} }
fn insert_orderless_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord { fn insert_orderless_symbol(
&mut self,
name: &str,
node: AstNode,
) -> &SymbolRecord {
self.orderless_identifiers.insert( self.orderless_identifiers.insert(
name.to_owned(), name.to_owned(),
SymbolRecord { SymbolRecord {
@ -154,7 +175,11 @@ impl InnerSymbolTable {
self.orderless_identifiers.get(name).unwrap() self.orderless_identifiers.get(name).unwrap()
} }
fn find_symbol_or_insert_with<'a, F>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord fn find_symbol_or_insert_with<'a, F>(
&'a mut self,
name: &str,
cb: F,
) -> &'a SymbolRecord
where where
F: FnOnce() -> (AstNode, SymbolKind), F: FnOnce() -> (AstNode, SymbolKind),
{ {
@ -177,7 +202,9 @@ impl InnerSymbolTable {
.find(|(_, v)| v.decl == decl) .find(|(_, v)| v.decl == decl)
.map(|(_, v)| v) .map(|(_, v)| v)
}) })
.or_else(|| self.parent_ref().and_then(|p| p.find_symbol_by_decl(decl))) .or_else(|| {
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> {
@ -192,7 +219,9 @@ 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(|| self.parent_ref().and_then(|p| p.find_ordered_symbol(name))) .or_else(|| {
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> {
@ -286,7 +315,12 @@ impl SymbolTableWrapper {
} }
impl SymbolTableWrapper { impl SymbolTableWrapper {
pub fn insert_symbol(&mut self, name: &str, node: AstNode, kind: SymbolKind) -> &SymbolRecord { pub fn insert_symbol(
&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)
} }
@ -294,15 +328,27 @@ impl SymbolTableWrapper {
self.root_mut().find_orderless_symbol(name) self.root_mut().find_orderless_symbol(name)
} }
pub fn insert_root_symbol(&mut self, name: &str, node: AstNode) -> &SymbolRecord { pub fn insert_root_symbol(
&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(&mut self, name: &str, node: AstNode) -> &SymbolRecord { pub fn insert_orderless_symbol(
&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>(&'a mut self, name: &str, cb: F) -> &'a SymbolRecord pub fn find_symbol_or_insert_with<'a, F>(
&'a mut self,
name: &str,
cb: F,
) -> &'a SymbolRecord
where where
F: FnOnce() -> (AstNode, SymbolKind), F: FnOnce() -> (AstNode, SymbolKind),
{ {
@ -424,40 +470,6 @@ 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,
@ -467,7 +479,6 @@ pub mod syms2 {
__TypeScope, __TypeScope,
Scope, Scope,
ParentScope, ParentScope,
Parameter(SourceLocation),
Local(SourceLocation), Local(SourceLocation),
__Last, __Last,
} }
@ -515,7 +526,9 @@ 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 { .. } => ExpandedPayload::Intern(val.as_intern()), Key::ScopeByIndex { .. } => {
ExpandedPayload::Intern(val.as_intern())
}
_ => ExpandedPayload::Ast(val.as_ast()), _ => ExpandedPayload::Ast(val.as_ast()),
}; };
@ -554,7 +567,7 @@ pub mod syms2 {
scope: AstIndex, scope: AstIndex,
name: InternIndex, name: InternIndex,
loc: SourceLocation, loc: SourceLocation,
) -> Option<(Key, AstIndex)> { ) -> Option<AstIndex> {
use SymbolKind::*; use SymbolKind::*;
let range = self.inner.range( let range = self.inner.range(
Key::Symbol { Key::Symbol {
@ -568,8 +581,8 @@ pub mod syms2 {
}, },
); );
if let Some((key, payload)) = range.rev().next() { if let Some((_, payload)) = range.rev().next() {
Some((*key, payload.as_ast())) Some(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,
@ -588,7 +601,7 @@ pub mod syms2 {
scope: AstIndex, scope: AstIndex,
name: InternIndex, name: InternIndex,
loc: SourceLocation, loc: SourceLocation,
) -> Option<(Key, AstIndex)> { ) -> Option<AstIndex> {
use SymbolKind::*; use SymbolKind::*;
let range = self.inner.range( let range = self.inner.range(
Key::Symbol { Key::Symbol {
@ -602,15 +615,15 @@ pub mod syms2 {
}, },
); );
if let Some((key, payload)) = range.rev().next() { if let Some((_, payload)) = range.rev().next() {
Some((*key, payload.as_ast())) Some(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_type_symbol(parent.as_ast(), name, loc) self.find_symbol(parent.as_ast(), name, loc)
} else { } else {
None None
} }
@ -624,8 +637,10 @@ pub mod syms2 {
kind: SymbolKind, kind: SymbolKind,
ast: AstIndex, ast: AstIndex,
) { ) {
self.inner self.inner.insert(
.insert(Key::Symbol { scope, name, kind }, Payload::new_ast(ast)); Key::Symbol { scope, name, kind },
Payload::new_ast(ast),
);
} }
} }
} }

View file

@ -159,13 +159,10 @@ 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
@ -762,8 +759,8 @@ impl<'tree, 'ir> IRBuilder<'tree, 'ir> {
} }
pub struct IR { pub struct IR {
pub(crate) nodes: Vec<Inst>, nodes: Vec<Inst>,
pub(crate) data: Vec<Option<Data>>, data: Vec<Option<Data>>,
// intern_pool: &'a mut InternPool, // intern_pool: &'a mut InternPool,
} }