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 {
tags: Vec<Tag>,
indices: Vec<u32>,
@ -574,6 +641,17 @@ pub struct TypeInfo {
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 {
pub fn peer_type(&mut self, lhs: Index, rhs: Index) -> Option<Index> {
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 {
let key = Key::PointerType {
pointee,
@ -1304,6 +1389,7 @@ impl InternPool {
};
self.get_or_insert(key)
}
pub fn try_get_pointer_type(
&self,
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
}
}
pub fn unit<T>(_: T) {}

View file

@ -35,25 +35,13 @@ impl SymbolPath {
for node in self.0.iter().skip(1).rev() {
match tree.nodes.get_node(node.unwrap()) {
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, .. } => {
_ = write!(
&mut buf,
"G{}::",
tree.get_ident_str(*name).unwrap()
);
_ = write!(&mut buf, "G{}::", tree.get_ident_str(*name).unwrap());
}
Tag::FunctionProto { name, .. } => {
_ = write!(
&mut buf,
"F{}::",
tree.get_ident_str(*name).unwrap()
);
_ = write!(&mut buf, "F{}::", tree.get_ident_str(*name).unwrap());
}
_ => {}
}
@ -142,12 +130,7 @@ impl Drop for 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 {
SymbolKind::Var => {
self.ordered_identifiers.push(SymbolRecord {
@ -160,11 +143,7 @@ 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(
name.to_owned(),
SymbolRecord {
@ -175,11 +154,7 @@ impl InnerSymbolTable {
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
F: FnOnce() -> (AstNode, SymbolKind),
{
@ -202,9 +177,7 @@ impl InnerSymbolTable {
.find(|(_, v)| v.decl == decl)
.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> {
@ -219,9 +192,7 @@ impl InnerSymbolTable {
self.ordered_identifiers
.iter()
.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> {
@ -315,12 +286,7 @@ 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)
}
@ -328,27 +294,15 @@ impl SymbolTableWrapper {
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)
}
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)
}
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
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)]
pub enum SymbolKind {
__First,
@ -479,6 +467,7 @@ pub mod syms2 {
__TypeScope,
Scope,
ParentScope,
Parameter(SourceLocation),
Local(SourceLocation),
__Last,
}
@ -526,9 +515,7 @@ pub mod syms2 {
}
let entries = self.inner.iter().map(|(key, val)| {
let payload = match key {
Key::ScopeByIndex { .. } => {
ExpandedPayload::Intern(val.as_intern())
}
Key::ScopeByIndex { .. } => ExpandedPayload::Intern(val.as_intern()),
_ => ExpandedPayload::Ast(val.as_ast()),
};
@ -567,7 +554,7 @@ pub mod syms2 {
scope: AstIndex,
name: InternIndex,
loc: SourceLocation,
) -> Option<AstIndex> {
) -> Option<(Key, AstIndex)> {
use SymbolKind::*;
let range = self.inner.range(
Key::Symbol {
@ -581,8 +568,8 @@ pub mod syms2 {
},
);
if let Some((_, payload)) = range.rev().next() {
Some(payload.as_ast())
if let Some((key, payload)) = range.rev().next() {
Some((*key, payload.as_ast()))
} else {
if let Some(parent) = self.inner.get(&Key::Symbol {
scope,
@ -601,7 +588,7 @@ pub mod syms2 {
scope: AstIndex,
name: InternIndex,
loc: SourceLocation,
) -> Option<AstIndex> {
) -> Option<(Key, AstIndex)> {
use SymbolKind::*;
let range = self.inner.range(
Key::Symbol {
@ -615,15 +602,15 @@ pub mod syms2 {
},
);
if let Some((_, payload)) = range.rev().next() {
Some(payload.as_ast())
if let Some((key, payload)) = range.rev().next() {
Some((*key, payload.as_ast()))
} else {
if let Some(parent) = self.inner.get(&Key::Symbol {
scope,
name: InternIndex::invalid(),
kind: ParentScope,
}) {
self.find_symbol(parent.as_ast(), name, loc)
self.find_type_symbol(parent.as_ast(), name, loc)
} else {
None
}
@ -637,10 +624,8 @@ pub mod syms2 {
kind: SymbolKind,
ast: AstIndex,
) {
self.inner.insert(
Key::Symbol { scope, name, kind },
Payload::new_ast(ast),
);
self.inner
.insert(Key::Symbol { scope, name, kind }, Payload::new_ast(ast));
}
}
}

View file

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