2024-07-08 03:13:50 -05:00
|
|
|
#![feature(
|
2024-11-14 13:25:52 -06:00
|
|
|
iter_array_chunks,
|
2024-09-20 12:01:44 -05:00
|
|
|
assert_matches,
|
2024-07-08 03:13:50 -05:00
|
|
|
let_chains,
|
|
|
|
if_let_guard,
|
|
|
|
macro_metavar_expr,
|
|
|
|
anonymous_lifetime_in_impl_trait,
|
|
|
|
core_intrinsics,
|
|
|
|
never_type,
|
|
|
|
unwrap_infallible,
|
|
|
|
slice_partition_dedup,
|
|
|
|
portable_simd,
|
|
|
|
iter_collect_into,
|
|
|
|
ptr_metadata,
|
2024-09-03 15:34:17 -05:00
|
|
|
slice_ptr_get,
|
|
|
|
slice_take,
|
2024-09-06 09:11:57 -05:00
|
|
|
map_try_insert,
|
2024-09-12 11:42:21 -05:00
|
|
|
extract_if,
|
2024-09-30 12:09:17 -05:00
|
|
|
ptr_internals,
|
2024-10-01 08:28:18 -05:00
|
|
|
iter_intersperse,
|
2024-10-10 01:35:17 -05:00
|
|
|
str_from_raw_parts,
|
|
|
|
ptr_sub_ptr,
|
2024-10-12 06:07:49 -05:00
|
|
|
slice_from_ptr_range,
|
2024-10-27 07:57:00 -05:00
|
|
|
iter_next_chunk,
|
2024-10-30 12:42:25 -05:00
|
|
|
pointer_is_aligned_to,
|
2024-11-16 10:41:40 -06:00
|
|
|
maybe_uninit_fill
|
2024-07-08 03:13:50 -05:00
|
|
|
)]
|
2024-11-23 12:47:17 -06:00
|
|
|
#![feature(array_chunks)]
|
2024-09-30 12:09:17 -05:00
|
|
|
#![warn(clippy::dbg_macro)]
|
2024-11-16 10:41:40 -06:00
|
|
|
#![expect(internal_features)]
|
2024-09-30 12:09:17 -05:00
|
|
|
#![no_std]
|
2024-05-17 12:53:59 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub use fs::*;
|
2024-11-08 03:25:34 -06:00
|
|
|
pub use utils::Ent;
|
2024-07-08 00:22:53 -05:00
|
|
|
use {
|
2024-09-13 08:12:20 -05:00
|
|
|
self::{
|
2024-11-24 09:43:45 -06:00
|
|
|
parser::{CommentOr, Expr, ExprRef, Pos},
|
2024-11-08 03:25:34 -06:00
|
|
|
ty::{ArrayLen, Builtin, Module},
|
|
|
|
utils::EntVec,
|
2024-09-13 08:12:20 -05:00
|
|
|
},
|
2024-10-27 08:29:14 -05:00
|
|
|
alloc::{string::String, vec::Vec},
|
2024-11-17 14:09:36 -06:00
|
|
|
core::{cell::Cell, ops::Range},
|
2024-09-30 12:09:17 -05:00
|
|
|
hashbrown::hash_map,
|
2024-05-20 07:11:58 -05:00
|
|
|
};
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate alloc;
|
|
|
|
|
|
|
|
#[cfg(any(feature = "std", test))]
|
|
|
|
extern crate std;
|
|
|
|
|
2024-09-28 09:34:08 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
const README: &str = include_str!("../README.md");
|
|
|
|
|
2024-05-09 16:41:59 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! run_tests {
|
2024-09-28 09:34:08 -05:00
|
|
|
($runner:path: $($name:ident;)*) => {$(
|
2024-05-09 16:41:59 -05:00
|
|
|
#[test]
|
|
|
|
fn $name() {
|
2024-09-30 12:09:17 -05:00
|
|
|
$crate::run_test(core::any::type_name_of_val(&$name), stringify!($name), $crate::README, $runner);
|
2024-05-09 16:41:59 -05:00
|
|
|
}
|
|
|
|
)*};
|
|
|
|
}
|
|
|
|
|
2024-10-04 14:44:29 -05:00
|
|
|
pub mod fmt;
|
2024-09-30 12:09:17 -05:00
|
|
|
#[cfg(any(feature = "std", test))]
|
|
|
|
pub mod fs;
|
2024-10-27 07:57:00 -05:00
|
|
|
pub mod fuzz;
|
|
|
|
pub mod lexer;
|
2024-06-23 06:55:48 -05:00
|
|
|
pub mod parser;
|
2024-09-02 17:07:20 -05:00
|
|
|
pub mod son;
|
2024-06-23 06:55:48 -05:00
|
|
|
|
2024-10-30 12:42:25 -05:00
|
|
|
mod utils;
|
2024-06-23 06:55:48 -05:00
|
|
|
|
2024-10-24 06:25:30 -05:00
|
|
|
mod debug {
|
|
|
|
pub fn panicking() -> bool {
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
{
|
|
|
|
std::thread::panicking()
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
{
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
|
|
|
pub type Trace = std::rc::Rc<std::backtrace::Backtrace>;
|
|
|
|
#[cfg(not(all(debug_assertions, feature = "std")))]
|
|
|
|
pub type Trace = ();
|
|
|
|
|
|
|
|
pub fn trace() -> Trace {
|
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
|
|
|
{
|
|
|
|
std::rc::Rc::new(std::backtrace::Backtrace::capture())
|
|
|
|
}
|
|
|
|
#[cfg(not(all(debug_assertions, feature = "std")))]
|
|
|
|
{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
mod ctx_map {
|
|
|
|
use core::hash::BuildHasher;
|
|
|
|
|
|
|
|
pub type Hash = u64;
|
|
|
|
pub type HashBuilder = core::hash::BuildHasherDefault<IdentityHasher>;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct IdentityHasher(u64);
|
|
|
|
|
|
|
|
impl core::hash::Hasher for IdentityHasher {
|
|
|
|
fn finish(&self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, _: &[u8]) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_u64(&mut self, i: u64) {
|
|
|
|
self.0 = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:23 -05:00
|
|
|
#[derive(Clone)]
|
2024-10-01 14:33:30 -05:00
|
|
|
pub struct Key<T> {
|
|
|
|
pub value: T,
|
|
|
|
pub hash: Hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> core::hash::Hash for Key<T> {
|
|
|
|
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
state.write_u64(self.hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait CtxEntry {
|
|
|
|
type Ctx: ?Sized;
|
|
|
|
type Key<'a>: Eq + core::hash::Hash;
|
|
|
|
|
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a>;
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:23 -05:00
|
|
|
#[derive(Clone)]
|
2024-10-01 14:33:30 -05:00
|
|
|
pub struct CtxMap<T> {
|
|
|
|
inner: hashbrown::HashMap<Key<T>, (), HashBuilder>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Default for CtxMap<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { inner: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: CtxEntry> CtxMap<T> {
|
|
|
|
pub fn entry<'a, 'b>(
|
|
|
|
&'a mut self,
|
2024-10-01 15:53:03 -05:00
|
|
|
key: T::Key<'b>,
|
2024-10-01 14:33:30 -05:00
|
|
|
ctx: &'b T::Ctx,
|
|
|
|
) -> (hashbrown::hash_map::RawEntryMut<'a, Key<T>, (), HashBuilder>, Hash) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let hash = crate::FnvBuildHasher::default().hash_one(&key);
|
|
|
|
(self.inner.raw_entry_mut().from_hash(hash, |k| k.value.key(ctx) == key), hash)
|
2024-10-01 14:33:30 -05:00
|
|
|
}
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
pub fn get<'a>(&self, key: T::Key<'a>, ctx: &'a T::Ctx) -> Option<&T> {
|
|
|
|
let hash = crate::FnvBuildHasher::default().hash_one(&key);
|
2024-10-01 14:33:30 -05:00
|
|
|
self.inner
|
|
|
|
.raw_entry()
|
2024-10-01 15:53:03 -05:00
|
|
|
.from_hash(hash, |k| k.value.key(ctx) == key)
|
2024-10-01 14:33:30 -05:00
|
|
|
.map(|(k, _)| &k.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.inner.clear();
|
|
|
|
}
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
pub fn remove(&mut self, value: &T, ctx: &T::Ctx) -> Option<T> {
|
2024-10-01 14:33:30 -05:00
|
|
|
let (entry, _) = self.entry(value.key(ctx), ctx);
|
|
|
|
match entry {
|
|
|
|
hashbrown::hash_map::RawEntryMut::Occupied(o) => Some(o.remove_entry().0.value),
|
|
|
|
hashbrown::hash_map::RawEntryMut::Vacant(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2024-10-01 15:53:03 -05:00
|
|
|
|
|
|
|
pub fn insert<'a>(&mut self, key: T::Key<'a>, value: T, ctx: &'a T::Ctx) {
|
|
|
|
let (entry, hash) = self.entry(key, ctx);
|
|
|
|
match entry {
|
|
|
|
hashbrown::hash_map::RawEntryMut::Occupied(_) => unreachable!(),
|
|
|
|
hashbrown::hash_map::RawEntryMut::Vacant(v) => {
|
|
|
|
_ = v.insert(Key { hash, value }, ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_or_insert<'a>(
|
|
|
|
&mut self,
|
|
|
|
key: T::Key<'a>,
|
|
|
|
ctx: &'a mut T::Ctx,
|
|
|
|
with: impl FnOnce(&'a mut T::Ctx) -> T,
|
|
|
|
) -> &mut T {
|
|
|
|
let (entry, hash) = self.entry(key, unsafe { &mut *(&mut *ctx as *mut _) });
|
|
|
|
match entry {
|
|
|
|
hashbrown::hash_map::RawEntryMut::Occupied(o) => &mut o.into_key_value().0.value,
|
|
|
|
hashbrown::hash_map::RawEntryMut::Vacant(v) => {
|
|
|
|
&mut v.insert(Key { hash, value: with(ctx) }, ()).0.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-01 14:33:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
|
|
|
|
pub struct Ident(u32);
|
2024-09-13 11:22:27 -05:00
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
impl Ident {
|
|
|
|
pub const INVALID: Self = Self(u32::MAX);
|
2024-09-13 11:22:27 -05:00
|
|
|
const LEN_BITS: u32 = 6;
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn len(self) -> u32 {
|
|
|
|
self.0 & ((1 << Self::LEN_BITS) - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(self) -> bool {
|
|
|
|
self.len() == 0
|
2024-09-13 11:22:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn is_null(self) -> bool {
|
|
|
|
(self.0 >> Self::LEN_BITS) == 0
|
2024-09-13 11:22:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn pos(self) -> u32 {
|
|
|
|
(self.0 >> Self::LEN_BITS).saturating_sub(1)
|
2024-09-13 11:22:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn new(pos: u32, len: u32) -> Option<Self> {
|
|
|
|
(len < (1 << Self::LEN_BITS)).then_some(((pos + 1) << Self::LEN_BITS) | len).map(Self)
|
2024-09-13 11:22:27 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn range(self) -> core::ops::Range<usize> {
|
|
|
|
let (len, pos) = (self.len() as usize, self.pos() as usize);
|
2024-09-13 11:22:27 -05:00
|
|
|
pos..pos + len
|
|
|
|
}
|
2024-10-27 07:57:00 -05:00
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
fn builtin(builtin: Builtin) -> Ident {
|
|
|
|
Self(builtin.index() as _)
|
2024-10-27 07:57:00 -05:00
|
|
|
}
|
2024-09-13 11:22:27 -05:00
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
pub mod ty {
|
2024-09-13 08:12:20 -05:00
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
lexer::TokenKind,
|
2024-11-08 03:25:34 -06:00
|
|
|
parser::{self, Pos},
|
|
|
|
utils::Ent,
|
2024-10-27 07:57:00 -05:00
|
|
|
Ident, Size, Types,
|
2024-09-13 08:12:20 -05:00
|
|
|
},
|
2024-10-24 06:25:30 -05:00
|
|
|
core::{num::NonZeroU32, ops::Range},
|
2024-09-13 08:12:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub type ArrayLen = u32;
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
impl Func {
|
|
|
|
pub const ECA: Func = Func(u32::MAX);
|
|
|
|
pub const MAIN: Func = Func(u32::MIN);
|
|
|
|
}
|
2024-10-21 11:57:23 -05:00
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, PartialOrd, Ord)]
|
2024-09-13 08:12:20 -05:00
|
|
|
pub struct Tuple(pub u32);
|
|
|
|
|
|
|
|
impl Tuple {
|
|
|
|
const LEN_BITS: u32 = 5;
|
|
|
|
const LEN_MASK: usize = Self::MAX_LEN - 1;
|
|
|
|
const MAX_LEN: usize = 1 << Self::LEN_BITS;
|
|
|
|
|
|
|
|
pub fn new(pos: usize, len: usize) -> Option<Self> {
|
|
|
|
if len >= Self::MAX_LEN {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Self((pos << Self::LEN_BITS | len) as u32))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range(self) -> Range<usize> {
|
|
|
|
let start = self.0 as usize >> Self::LEN_BITS;
|
|
|
|
start..start + self.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(self) -> usize {
|
|
|
|
self.0 as usize & Self::LEN_MASK
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
pub fn is_empty(self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:12:20 -05:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
2024-10-24 02:43:07 -05:00
|
|
|
|
|
|
|
pub fn args(self) -> ArgIter {
|
|
|
|
ArgIter(self.range())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ArgIter(Range<usize>);
|
|
|
|
|
|
|
|
pub enum Arg {
|
|
|
|
Type(Id),
|
|
|
|
Value(Id),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgIter {
|
|
|
|
pub(crate) fn next(&mut self, tys: &Types) -> Option<Arg> {
|
|
|
|
let ty = tys.ins.args[self.0.next()?];
|
|
|
|
if ty == Id::TYPE {
|
|
|
|
return Some(Arg::Type(tys.ins.args[self.0.next().unwrap()]));
|
|
|
|
}
|
|
|
|
Some(Arg::Value(ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn next_value(&mut self, tys: &Types) -> Option<Id> {
|
|
|
|
loop {
|
|
|
|
match self.next(tys)? {
|
|
|
|
Arg::Type(_) => continue,
|
|
|
|
Arg::Value(id) => break Some(id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
|
|
|
pub struct Id(NonZeroU32);
|
|
|
|
|
2024-11-23 08:28:02 -06:00
|
|
|
impl From<Id> for i64 {
|
|
|
|
fn from(value: Id) -> Self {
|
|
|
|
value.0.get() as _
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
impl crate::ctx_map::CtxEntry for Id {
|
2024-10-01 15:53:03 -05:00
|
|
|
type Ctx = crate::TypeIns;
|
2024-10-01 14:33:30 -05:00
|
|
|
type Key<'a> = crate::SymKey<'a>;
|
|
|
|
|
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
2024-10-01 15:53:03 -05:00
|
|
|
match self.expand() {
|
|
|
|
Kind::Struct(s) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let st = &ctx.structs[s];
|
2024-10-01 15:53:03 -05:00
|
|
|
debug_assert_ne!(st.pos, Pos::MAX);
|
2024-11-30 06:38:44 -06:00
|
|
|
crate::SymKey::Struct(st.file, st.pos, st.captured)
|
2024-10-01 15:53:03 -05:00
|
|
|
}
|
2024-11-17 09:25:39 -06:00
|
|
|
Kind::Enum(e) => {
|
|
|
|
let en = &ctx.enums[e];
|
|
|
|
debug_assert_ne!(en.pos, Pos::MAX);
|
|
|
|
crate::SymKey::Enum(en.file, en.pos)
|
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
Kind::Ptr(p) => crate::SymKey::Pointer(&ctx.ptrs[p]),
|
|
|
|
Kind::Opt(p) => crate::SymKey::Optional(&ctx.opts[p]),
|
2024-10-01 15:53:03 -05:00
|
|
|
Kind::Func(f) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let fc = &ctx.funcs[f];
|
2024-10-01 15:53:03 -05:00
|
|
|
if let Some(base) = fc.base {
|
2024-11-08 03:25:34 -06:00
|
|
|
// TODO: merge base and sig
|
2024-10-01 15:53:03 -05:00
|
|
|
crate::SymKey::FuncInst(base, fc.sig.unwrap().args)
|
|
|
|
} else {
|
2024-11-30 09:30:58 -06:00
|
|
|
crate::SymKey::Decl(fc.parent, fc.name)
|
2024-10-01 15:53:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Global(g) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let gb = &ctx.globals[g];
|
2024-11-30 09:30:58 -06:00
|
|
|
crate::SymKey::Decl(gb.file.into(), gb.name)
|
2024-10-01 15:53:03 -05:00
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
Kind::Slice(s) => crate::SymKey::Array(&ctx.slices[s]),
|
2024-10-27 07:57:00 -05:00
|
|
|
Kind::Module(_) | Kind::Builtin(_) => {
|
2024-11-30 09:30:58 -06:00
|
|
|
crate::SymKey::Decl(Module::default().into(), Ident::INVALID)
|
2024-10-27 07:57:00 -05:00
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
Kind::Const(c) => crate::SymKey::Constant(&ctx.consts[c]),
|
2024-10-01 15:53:03 -05:00
|
|
|
}
|
2024-10-01 14:33:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:12:20 -05:00
|
|
|
impl Default for Id {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(UNDECLARED) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Id {
|
2024-11-12 12:02:29 -06:00
|
|
|
pub const DINT: Self = Self::UINT;
|
2024-10-25 07:51:33 -05:00
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn bin_ret(self, op: TokenKind) -> Id {
|
2024-11-17 03:06:10 -06:00
|
|
|
if op.is_compatison() {
|
|
|
|
Self::BOOL
|
|
|
|
} else {
|
|
|
|
self
|
2024-10-27 07:57:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 07:36:12 -05:00
|
|
|
pub fn is_float(self) -> bool {
|
|
|
|
matches!(self.repr(), F32 | F64) || self.is_never()
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:12:20 -05:00
|
|
|
pub fn is_signed(self) -> bool {
|
2024-10-27 07:57:00 -05:00
|
|
|
matches!(self.repr(), I8..=INT) || self.is_never()
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_unsigned(self) -> bool {
|
2024-10-27 07:57:00 -05:00
|
|
|
matches!(self.repr(), U8..=UINT) || self.is_never()
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_integer(self) -> bool {
|
2024-10-27 07:57:00 -05:00
|
|
|
matches!(self.repr(), U8..=INT) || self.is_never()
|
2024-10-25 08:14:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_never(self) -> bool {
|
|
|
|
self == Self::NEVER
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strip_pointer(self) -> Self {
|
|
|
|
match self.expand() {
|
2024-11-08 03:25:34 -06:00
|
|
|
Kind::Ptr(_) => Id::UINT,
|
2024-09-13 08:12:20 -05:00
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_pointer(self) -> bool {
|
2024-10-25 08:14:32 -05:00
|
|
|
matches!(self.expand(), Kind::Ptr(_)) || self.is_never()
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
pub fn is_optional(self) -> bool {
|
|
|
|
matches!(self.expand(), Kind::Opt(_)) || self.is_never()
|
|
|
|
}
|
|
|
|
|
2024-10-27 07:57:00 -05:00
|
|
|
pub fn try_upcast(self, ob: Self) -> Option<Self> {
|
2024-11-23 07:19:47 -06:00
|
|
|
self.try_upcast_low(ob, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_upcast_low(self, ob: Self, coerce_pointer: bool) -> Option<Self> {
|
2024-09-13 08:12:20 -05:00
|
|
|
let (oa, ob) = (Self(self.0.min(ob.0)), Self(self.0.max(ob.0)));
|
|
|
|
let (a, b) = (oa.strip_pointer(), ob.strip_pointer());
|
|
|
|
Some(match () {
|
2024-11-08 03:25:34 -06:00
|
|
|
_ if oa == Id::NEVER => ob,
|
|
|
|
_ if ob == Id::NEVER => oa,
|
2024-09-13 08:12:20 -05:00
|
|
|
_ if oa == ob => oa,
|
2024-10-31 04:36:18 -05:00
|
|
|
_ if ob.is_optional() => ob,
|
2024-09-13 08:12:20 -05:00
|
|
|
_ if oa.is_pointer() && ob.is_pointer() => return None,
|
|
|
|
_ if a.is_signed() && b.is_signed() || a.is_unsigned() && b.is_unsigned() => ob,
|
|
|
|
_ if a.is_unsigned() && b.is_signed() && a.repr() - U8 < b.repr() - I8 => ob,
|
2024-10-25 08:45:00 -05:00
|
|
|
_ if a.is_unsigned() && b.is_signed() && a.repr() - U8 > b.repr() - I8 => oa,
|
2024-11-23 07:19:47 -06:00
|
|
|
_ if oa.is_integer() && ob.is_pointer() && coerce_pointer => ob,
|
2024-09-13 08:12:20 -05:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand(self) -> Kind {
|
|
|
|
Kind::from_ty(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn repr(self) -> u32 {
|
|
|
|
self.0.get()
|
|
|
|
}
|
2024-10-20 14:50:08 -05:00
|
|
|
|
|
|
|
pub(crate) fn simple_size(&self) -> Option<Size> {
|
|
|
|
Some(match self.expand() {
|
|
|
|
Kind::Ptr(_) => 8,
|
2024-11-08 03:25:34 -06:00
|
|
|
Kind::Builtin(Builtin(VOID)) => 0,
|
|
|
|
Kind::Builtin(Builtin(NEVER)) => 0,
|
|
|
|
Kind::Builtin(Builtin(INT | UINT | F64)) => 8,
|
|
|
|
Kind::Builtin(Builtin(I32 | U32 | TYPE | F32)) => 4,
|
|
|
|
Kind::Builtin(Builtin(I16 | U16)) => 2,
|
|
|
|
Kind::Builtin(Builtin(I8 | U8 | BOOL)) => 1,
|
2024-10-20 14:50:08 -05:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
2024-10-21 08:12:37 -05:00
|
|
|
|
|
|
|
pub(crate) fn extend(self) -> Self {
|
|
|
|
if self.is_signed() {
|
|
|
|
Self::INT
|
|
|
|
} else if self.is_pointer() {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
Self::UINT
|
|
|
|
}
|
|
|
|
}
|
2024-10-21 10:04:29 -05:00
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
pub(crate) fn loc(&self, tys: &Types) -> Loc {
|
2024-10-21 10:04:29 -05:00
|
|
|
match self.expand() {
|
2024-10-30 14:20:03 -05:00
|
|
|
Kind::Opt(o)
|
2024-11-08 03:25:34 -06:00
|
|
|
if let ty = tys.ins.opts[o].base
|
2024-10-30 14:20:03 -05:00
|
|
|
&& ty.loc(tys) == Loc::Reg
|
|
|
|
&& (ty.is_pointer() || tys.size_of(ty) < 8) =>
|
|
|
|
{
|
|
|
|
Loc::Reg
|
|
|
|
}
|
2024-11-17 09:25:39 -06:00
|
|
|
Kind::Ptr(_) | Kind::Enum(_) | Kind::Builtin(_) => Loc::Reg,
|
2024-10-21 10:04:29 -05:00
|
|
|
Kind::Struct(_) if tys.size_of(*self) == 0 => Loc::Reg,
|
2024-10-30 14:20:03 -05:00
|
|
|
Kind::Struct(_) | Kind::Slice(_) | Kind::Opt(_) => Loc::Stack,
|
2024-11-24 07:47:38 -06:00
|
|
|
c @ (Kind::Func(_) | Kind::Global(_) | Kind::Module(_) | Kind::Const(_)) => {
|
|
|
|
unreachable!("{c:?}")
|
2024-11-08 03:25:34 -06:00
|
|
|
}
|
2024-10-21 10:04:29 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-24 05:28:18 -05:00
|
|
|
|
|
|
|
pub(crate) fn has_pointers(&self, tys: &Types) -> bool {
|
|
|
|
match self.expand() {
|
|
|
|
Kind::Struct(s) => tys.struct_fields(s).iter().any(|f| f.ty.has_pointers(tys)),
|
|
|
|
Kind::Ptr(_) => true,
|
2024-11-08 03:25:34 -06:00
|
|
|
Kind::Slice(s) => tys.ins.slices[s].len == ArrayLen::MAX,
|
2024-10-24 05:28:18 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2024-10-21 10:04:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum Loc {
|
|
|
|
Reg,
|
|
|
|
Stack,
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Id {
|
|
|
|
fn from(id: u64) -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(id as _) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn array_to_lower_case<const N: usize>(array: [u8; N]) -> [u8; N] {
|
|
|
|
let mut result = [0; N];
|
|
|
|
let mut i = 0;
|
|
|
|
while i < N {
|
|
|
|
result[i] = array[i].to_ascii_lowercase();
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
// const string to lower case
|
|
|
|
|
|
|
|
macro_rules! builtin_type {
|
|
|
|
($($name:ident;)*) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
$(const $name: u32 = ${index(0)} + 1;)*
|
2024-09-13 08:12:20 -05:00
|
|
|
|
|
|
|
mod __lc_names {
|
|
|
|
use super::*;
|
2024-10-10 01:35:17 -05:00
|
|
|
$(pub const $name: &str = unsafe {
|
|
|
|
const LCL: &[u8] = unsafe {
|
|
|
|
&array_to_lower_case(
|
|
|
|
*(stringify!($name).as_ptr() as *const [u8; stringify!($name).len()])
|
|
|
|
)
|
|
|
|
};
|
|
|
|
core::str::from_utf8_unchecked(LCL)
|
|
|
|
};)*
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-11-17 13:04:53 -06:00
|
|
|
impl Builtin {
|
|
|
|
$(pub const $name: Self = Builtin($name);)*
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
impl Id {
|
2024-11-08 03:25:34 -06:00
|
|
|
$(pub const $name: Self = Kind::Builtin(Builtin($name)).compress();)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Kind {
|
|
|
|
$(pub const $name: Self = Kind::Builtin(Builtin($name));)*
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 08:12:20 -05:00
|
|
|
pub fn from_str(name: &str) -> Option<Builtin> {
|
2024-10-10 01:35:17 -05:00
|
|
|
match name {
|
2024-11-08 03:25:34 -06:00
|
|
|
$(__lc_names::$name => Some(Builtin($name)),)*
|
2024-09-13 08:12:20 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_str(ty: Builtin) -> &'static str {
|
2024-11-08 03:25:34 -06:00
|
|
|
match ty.0 {
|
2024-10-10 01:35:17 -05:00
|
|
|
$($name => __lc_names::$name,)*
|
2024-09-13 08:12:20 -05:00
|
|
|
v => unreachable!("invalid type: {}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin_type! {
|
|
|
|
UNDECLARED;
|
2024-10-19 03:17:36 -05:00
|
|
|
LEFT_UNREACHABLE;
|
|
|
|
RIGHT_UNREACHABLE;
|
2024-09-13 08:12:20 -05:00
|
|
|
NEVER;
|
|
|
|
VOID;
|
|
|
|
TYPE;
|
|
|
|
BOOL;
|
|
|
|
U8;
|
|
|
|
U16;
|
|
|
|
U32;
|
|
|
|
UINT;
|
|
|
|
I8;
|
|
|
|
I16;
|
|
|
|
I32;
|
|
|
|
INT;
|
2024-10-29 07:36:12 -05:00
|
|
|
F32;
|
|
|
|
F64;
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! type_kind {
|
|
|
|
($(#[$meta:meta])* $vis:vis enum $name:ident {$( $variant:ident, )*}) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
crate::utils::decl_ent! {
|
|
|
|
$(pub struct $variant(u32);)*
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:12:20 -05:00
|
|
|
$(#[$meta])*
|
|
|
|
$vis enum $name {
|
|
|
|
$($variant($variant),)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
const FLAG_BITS: u32 = (${count($variant)} as u32).next_power_of_two().ilog2();
|
2024-09-30 12:09:17 -05:00
|
|
|
const FLAG_OFFSET: u32 = core::mem::size_of::<Id>() as u32 * 8 - Self::FLAG_BITS;
|
2024-09-13 08:12:20 -05:00
|
|
|
const INDEX_MASK: u32 = (1 << (32 - Self::FLAG_BITS)) - 1;
|
|
|
|
|
|
|
|
$vis fn from_ty(ty: Id) -> Self {
|
|
|
|
let (flag, index) = (ty.repr() >> Self::FLAG_OFFSET, ty.repr() & Self::INDEX_MASK);
|
|
|
|
match flag {
|
2024-11-08 03:25:34 -06:00
|
|
|
$(${index(0)} => Self::$variant($variant(index)),)*
|
2024-09-13 08:12:20 -05:00
|
|
|
i => unreachable!("{i}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$vis const fn compress(self) -> Id {
|
|
|
|
let (index, flag) = match self {
|
2024-11-08 03:25:34 -06:00
|
|
|
$(Self::$variant(index) => (index.0, ${index(0)}),)*
|
2024-09-13 08:12:20 -05:00
|
|
|
};
|
|
|
|
Id(unsafe { NonZeroU32::new_unchecked((flag << Self::FLAG_OFFSET) | index) })
|
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
$(
|
|
|
|
impl From<$variant> for $name {
|
|
|
|
fn from(value: $variant) -> Self {
|
|
|
|
Self::$variant(value)
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
|
2024-11-23 08:28:02 -06:00
|
|
|
impl From<$variant> for i64 {
|
|
|
|
fn from(value: $variant) -> Self {
|
|
|
|
Id::from(value).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
impl From<$variant> for Id {
|
|
|
|
fn from(value: $variant) -> Self {
|
|
|
|
$name::$variant(value).compress()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
2024-09-13 08:12:20 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type_kind! {
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum Kind {
|
|
|
|
Builtin,
|
|
|
|
Struct,
|
2024-11-17 09:25:39 -06:00
|
|
|
Enum,
|
2024-09-13 08:12:20 -05:00
|
|
|
Ptr,
|
2024-10-31 04:36:18 -05:00
|
|
|
Slice,
|
|
|
|
Opt,
|
2024-09-13 08:12:20 -05:00
|
|
|
Func,
|
|
|
|
Global,
|
|
|
|
Module,
|
2024-11-08 03:25:34 -06:00
|
|
|
Const,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
|
|
|
pub const MAIN: Self = Self(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Module {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(u32::MAX)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Ident> for Builtin {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(value: Ident) -> Result<Self, Self::Error> {
|
|
|
|
if value.is_null() {
|
|
|
|
Ok(Self(value.len()))
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Kind {
|
|
|
|
fn default() -> Self {
|
2024-11-08 03:25:34 -06:00
|
|
|
Id::UNDECLARED.expand()
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Display<'a> {
|
|
|
|
tys: &'a super::Types,
|
|
|
|
files: &'a [parser::Ast],
|
|
|
|
ty: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display<'a> {
|
2024-10-20 03:37:48 -05:00
|
|
|
pub fn new(tys: &'a super::Types, files: &'a [parser::Ast], ty: Id) -> Self {
|
2024-09-13 08:12:20 -05:00
|
|
|
Self { tys, files, ty }
|
|
|
|
}
|
|
|
|
|
2024-10-20 03:37:48 -05:00
|
|
|
pub fn rety(&self, ty: Id) -> Self {
|
2024-09-13 08:12:20 -05:00
|
|
|
Self::new(self.tys, self.files, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 08:04:58 -05:00
|
|
|
impl core::fmt::Display for Display<'_> {
|
2024-09-30 12:09:17 -05:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2024-09-13 08:12:20 -05:00
|
|
|
use Kind as TK;
|
|
|
|
match TK::from_ty(self.ty) {
|
2024-10-12 06:07:49 -05:00
|
|
|
TK::Module(idx) => {
|
|
|
|
f.write_str("@use(\"")?;
|
2024-11-08 03:25:34 -06:00
|
|
|
self.files[idx.index()].path.fmt(f)?;
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str(")[")?;
|
|
|
|
idx.fmt(f)?;
|
|
|
|
f.write_str("]")
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
TK::Builtin(ty) => f.write_str(to_str(ty)),
|
2024-10-30 14:20:03 -05:00
|
|
|
TK::Opt(ty) => {
|
|
|
|
f.write_str("?")?;
|
2024-11-08 03:25:34 -06:00
|
|
|
self.rety(self.tys.ins.opts[ty].base).fmt(f)
|
2024-10-30 14:20:03 -05:00
|
|
|
}
|
2024-10-13 06:30:00 -05:00
|
|
|
TK::Ptr(ty) => {
|
|
|
|
f.write_str("^")?;
|
2024-11-08 03:25:34 -06:00
|
|
|
self.rety(self.tys.ins.ptrs[ty].base).fmt(f)
|
2024-10-13 06:30:00 -05:00
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
TK::Struct(idx) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let record = &self.tys.ins.structs[idx];
|
2024-10-27 07:57:00 -05:00
|
|
|
if record.name.is_null() {
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str("[")?;
|
|
|
|
idx.fmt(f)?;
|
|
|
|
f.write_str("]{")?;
|
2024-11-17 09:25:39 -06:00
|
|
|
for (i, &super::StructField { name, ty }) in
|
2024-09-30 12:09:17 -05:00
|
|
|
self.tys.struct_fields(idx).iter().enumerate()
|
2024-09-28 14:56:39 -05:00
|
|
|
{
|
|
|
|
if i != 0 {
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str(", ")?;
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str(self.tys.names.ident_str(name))?;
|
|
|
|
f.write_str(": ")?;
|
|
|
|
self.rety(ty).fmt(f)?;
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str("}")
|
2024-09-28 14:56:39 -05:00
|
|
|
} else {
|
2024-11-08 03:25:34 -06:00
|
|
|
let file = &self.files[record.file.index()];
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str(file.ident_str(record.name))
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
2024-11-17 09:25:39 -06:00
|
|
|
TK::Enum(idx) => {
|
|
|
|
let enm = &self.tys.ins.enums[idx];
|
|
|
|
debug_assert!(!enm.name.is_null());
|
|
|
|
let file = &self.files[enm.file.index()];
|
|
|
|
f.write_str(file.ident_str(enm.name))
|
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
TK::Func(idx) => {
|
|
|
|
f.write_str("fn")?;
|
|
|
|
idx.fmt(f)
|
|
|
|
}
|
|
|
|
TK::Global(idx) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let global = &self.tys.ins.globals[idx];
|
|
|
|
let file = &self.files[global.file.index()];
|
2024-11-17 13:04:53 -06:00
|
|
|
f.write_str(file.ident_str(global.name))?;
|
|
|
|
f.write_str(" (global)")
|
2024-10-12 06:07:49 -05:00
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
TK::Slice(idx) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let array = self.tys.ins.slices[idx];
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str("[")?;
|
2024-10-19 12:37:02 -05:00
|
|
|
self.rety(array.elem).fmt(f)?;
|
2024-10-12 06:07:49 -05:00
|
|
|
if array.len != ArrayLen::MAX {
|
|
|
|
f.write_str("; ")?;
|
|
|
|
array.len.fmt(f)?;
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
2024-10-12 06:07:49 -05:00
|
|
|
f.write_str("]")
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
TK::Const(idx) => {
|
|
|
|
let cnst = &self.tys.ins.consts[idx];
|
|
|
|
let file = &self.files[cnst.file.index()];
|
2024-11-17 13:04:53 -06:00
|
|
|
f.write_str(file.ident_str(cnst.name))?;
|
|
|
|
f.write_str(" (const)")
|
2024-11-08 03:25:34 -06:00
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Offset = u32;
|
|
|
|
type Size = u32;
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
|
|
|
pub enum SymKey<'a> {
|
2024-10-01 14:33:30 -05:00
|
|
|
Pointer(&'a Ptr),
|
2024-10-30 14:20:03 -05:00
|
|
|
Optional(&'a Opt),
|
2024-11-08 03:25:34 -06:00
|
|
|
Struct(Module, Pos, ty::Tuple),
|
2024-11-17 09:25:39 -06:00
|
|
|
Enum(Module, Pos),
|
2024-10-01 10:43:15 -05:00
|
|
|
FuncInst(ty::Func, ty::Tuple),
|
2024-11-30 09:30:58 -06:00
|
|
|
Decl(ty::Id, Ident),
|
2024-10-01 14:33:30 -05:00
|
|
|
Array(&'a Array),
|
2024-11-08 03:25:34 -06:00
|
|
|
Constant(&'a Const),
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
2024-11-05 07:52:30 -06:00
|
|
|
pub struct Sig {
|
2024-09-13 08:12:20 -05:00
|
|
|
args: ty::Tuple,
|
|
|
|
ret: ty::Id,
|
|
|
|
}
|
|
|
|
|
2024-11-12 12:02:29 -06:00
|
|
|
#[derive(Default, Clone, Copy)]
|
2024-09-13 08:12:20 -05:00
|
|
|
struct Func {
|
2024-11-08 03:25:34 -06:00
|
|
|
file: Module,
|
2024-11-24 07:47:38 -06:00
|
|
|
parent: ty::Id,
|
2024-10-01 15:53:03 -05:00
|
|
|
name: Ident,
|
|
|
|
base: Option<ty::Func>,
|
2024-09-13 08:12:20 -05:00
|
|
|
expr: ExprRef,
|
|
|
|
sig: Option<Sig>,
|
2024-11-10 12:35:48 -06:00
|
|
|
is_inline: bool,
|
2024-11-17 13:04:53 -06:00
|
|
|
returns_type: bool,
|
2024-11-05 07:52:30 -06:00
|
|
|
comp_state: [CompState; 2],
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-11-12 12:02:29 -06:00
|
|
|
#[derive(Default, PartialEq, Eq, Clone, Copy)]
|
2024-11-05 07:52:30 -06:00
|
|
|
enum CompState {
|
|
|
|
#[default]
|
|
|
|
Dead,
|
|
|
|
Queued(usize),
|
|
|
|
Compiled,
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
#[derive(Clone, Default)]
|
2024-09-13 08:12:20 -05:00
|
|
|
struct Global {
|
2024-11-08 03:25:34 -06:00
|
|
|
file: Module,
|
2024-09-13 08:12:20 -05:00
|
|
|
name: Ident,
|
|
|
|
ty: ty::Id,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
#[derive(PartialEq, Eq, Hash)]
|
|
|
|
pub struct Const {
|
|
|
|
ast: ExprRef,
|
|
|
|
name: Ident,
|
|
|
|
file: Module,
|
2024-11-24 07:47:38 -06:00
|
|
|
parent: ty::Id,
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-11-17 09:25:39 -06:00
|
|
|
struct EnumField {
|
|
|
|
name: Ident,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Enum {
|
|
|
|
name: Ident,
|
|
|
|
pos: Pos,
|
|
|
|
file: Module,
|
|
|
|
field_start: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StructField {
|
2024-09-30 12:09:17 -05:00
|
|
|
name: Ident,
|
2024-09-13 08:12:20 -05:00
|
|
|
ty: ty::Id,
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
#[derive(Default)]
|
2024-09-13 08:12:20 -05:00
|
|
|
struct Struct {
|
2024-09-28 14:56:39 -05:00
|
|
|
name: Ident,
|
2024-10-01 15:53:03 -05:00
|
|
|
pos: Pos,
|
2024-11-08 03:25:34 -06:00
|
|
|
file: Module,
|
2024-09-30 12:09:17 -05:00
|
|
|
size: Cell<Size>,
|
|
|
|
align: Cell<u8>,
|
2024-11-30 06:38:44 -06:00
|
|
|
captured: ty::Tuple,
|
2024-09-30 12:09:17 -05:00
|
|
|
explicit_alignment: Option<u8>,
|
|
|
|
field_start: u32,
|
2024-11-24 07:47:38 -06:00
|
|
|
ast: ExprRef,
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
|
|
|
pub struct Opt {
|
|
|
|
base: ty::Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
2024-10-01 15:53:03 -05:00
|
|
|
pub struct Ptr {
|
2024-09-13 08:12:20 -05:00
|
|
|
base: ty::Id,
|
|
|
|
}
|
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
2024-10-01 15:53:03 -05:00
|
|
|
pub struct Array {
|
2024-10-19 12:37:02 -05:00
|
|
|
elem: ty::Id,
|
2024-09-13 08:12:20 -05:00
|
|
|
len: ArrayLen,
|
|
|
|
}
|
2024-10-19 12:37:02 -05:00
|
|
|
impl Array {
|
|
|
|
fn len(&self) -> Option<usize> {
|
|
|
|
(self.len != ArrayLen::MAX).then_some(self.len as usize)
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
impl ctx_map::CtxEntry for Ident {
|
|
|
|
type Ctx = str;
|
|
|
|
type Key<'a> = &'a str;
|
2024-09-30 12:09:17 -05:00
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
2024-10-27 07:57:00 -05:00
|
|
|
unsafe { ctx.get_unchecked(self.range()) }
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct IdentInterner {
|
2024-10-01 14:33:30 -05:00
|
|
|
lookup: ctx_map::CtxMap<Ident>,
|
2024-09-30 12:09:17 -05:00
|
|
|
strings: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IdentInterner {
|
|
|
|
fn intern(&mut self, ident: &str) -> Ident {
|
2024-10-01 14:33:30 -05:00
|
|
|
let (entry, hash) = self.lookup.entry(ident, &self.strings);
|
|
|
|
match entry {
|
|
|
|
hash_map::RawEntryMut::Occupied(o) => o.get_key_value().0.value,
|
2024-09-30 12:09:17 -05:00
|
|
|
hash_map::RawEntryMut::Vacant(v) => {
|
2024-10-27 07:57:00 -05:00
|
|
|
let id = Ident::new(self.strings.len() as _, ident.len() as _).unwrap();
|
2024-09-30 12:09:17 -05:00
|
|
|
self.strings.push_str(ident);
|
2024-10-01 14:33:30 -05:00
|
|
|
v.insert(ctx_map::Key { hash, value: id }, ());
|
2024-09-30 12:09:17 -05:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ident_str(&self, ident: Ident) -> &str {
|
2024-10-27 07:57:00 -05:00
|
|
|
&self.strings[ident.range()]
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn project(&self, ident: &str) -> Option<Ident> {
|
2024-10-01 14:33:30 -05:00
|
|
|
self.lookup.get(ident, &self.strings).copied()
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
2024-10-25 15:59:01 -05:00
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.lookup.clear();
|
|
|
|
self.strings.clear()
|
|
|
|
}
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 08:12:20 -05:00
|
|
|
#[derive(Default)]
|
2024-10-01 14:33:30 -05:00
|
|
|
struct TypesTmp {
|
2024-11-17 09:25:39 -06:00
|
|
|
struct_fields: Vec<StructField>,
|
|
|
|
enum_fields: Vec<EnumField>,
|
2024-10-12 15:21:20 -05:00
|
|
|
args: Vec<ty::Id>,
|
2024-10-01 14:33:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2024-10-01 15:53:03 -05:00
|
|
|
pub struct TypeIns {
|
2024-09-13 08:12:20 -05:00
|
|
|
args: Vec<ty::Id>,
|
2024-11-17 09:25:39 -06:00
|
|
|
struct_fields: Vec<StructField>,
|
|
|
|
enum_fields: Vec<EnumField>,
|
2024-11-08 03:25:34 -06:00
|
|
|
funcs: EntVec<ty::Func, Func>,
|
|
|
|
globals: EntVec<ty::Global, Global>,
|
|
|
|
consts: EntVec<ty::Const, Const>,
|
|
|
|
structs: EntVec<ty::Struct, Struct>,
|
2024-11-17 09:25:39 -06:00
|
|
|
enums: EntVec<ty::Enum, Enum>,
|
2024-11-08 03:25:34 -06:00
|
|
|
ptrs: EntVec<ty::Ptr, Ptr>,
|
|
|
|
opts: EntVec<ty::Opt, Opt>,
|
|
|
|
slices: EntVec<ty::Slice, Array>,
|
2024-10-01 14:33:30 -05:00
|
|
|
}
|
2024-10-01 08:28:18 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
struct FTask {
|
2024-11-08 03:25:34 -06:00
|
|
|
file: Module,
|
2024-10-25 15:59:01 -05:00
|
|
|
id: ty::Func,
|
2024-11-05 07:52:30 -06:00
|
|
|
ct: bool,
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct StringRef(ty::Global);
|
|
|
|
|
|
|
|
impl ctx_map::CtxEntry for StringRef {
|
2024-11-08 03:25:34 -06:00
|
|
|
type Ctx = EntVec<ty::Global, Global>;
|
2024-10-25 15:59:01 -05:00
|
|
|
type Key<'a> = &'a [u8];
|
|
|
|
|
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
2024-11-08 03:25:34 -06:00
|
|
|
&ctx[self.0].data
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
#[derive(Default)]
|
2024-11-05 07:52:30 -06:00
|
|
|
pub struct Types {
|
2024-10-01 14:33:30 -05:00
|
|
|
syms: ctx_map::CtxMap<ty::Id>,
|
|
|
|
names: IdentInterner,
|
2024-10-25 15:59:01 -05:00
|
|
|
strings: ctx_map::CtxMap<StringRef>,
|
2024-10-01 14:33:30 -05:00
|
|
|
ins: TypeIns,
|
|
|
|
tmp: TypesTmp,
|
2024-10-25 15:59:01 -05:00
|
|
|
tasks: Vec<Option<FTask>>,
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-20 08:16:55 -05:00
|
|
|
impl Types {
|
2024-11-17 13:04:53 -06:00
|
|
|
pub fn case(&self, ty: ty::Id) -> fn(&str) -> Result<(), &'static str> {
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::NEVER => |_| Ok(()),
|
|
|
|
ty::Kind::Enum(_)
|
|
|
|
| ty::Kind::Struct(_)
|
|
|
|
| ty::Kind::Builtin(_)
|
|
|
|
| ty::Kind::Ptr(_)
|
|
|
|
| ty::Kind::Slice(_)
|
|
|
|
| ty::Kind::Opt(_) => utils::is_pascal_case,
|
|
|
|
ty::Kind::Func(f) if self.ins.funcs[f].returns_type => utils::is_pascal_case,
|
|
|
|
ty::Kind::Func(_) | ty::Kind::Global(_) | ty::Kind::Module(_) => utils::is_snake_case,
|
|
|
|
ty::Kind::Const(_) => utils::is_screaming_case,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 08:16:55 -05:00
|
|
|
fn pack_args(&mut self, arg_base: usize) -> Option<ty::Tuple> {
|
|
|
|
let base = self.ins.args.len();
|
|
|
|
self.ins.args.extend(self.tmp.args.drain(arg_base..));
|
|
|
|
let needle = &self.ins.args[base..];
|
|
|
|
if needle.is_empty() {
|
|
|
|
return Some(ty::Tuple::empty());
|
|
|
|
}
|
|
|
|
let len = needle.len();
|
|
|
|
// FIXME: maybe later when this becomes a bottleneck we use more
|
|
|
|
// efficient search (SIMD?, indexing?)
|
|
|
|
let sp = self.ins.args.windows(needle.len()).position(|val| val == needle).unwrap();
|
|
|
|
self.ins.args.truncate((sp + needle.len()).max(base));
|
|
|
|
ty::Tuple::new(sp, len)
|
|
|
|
}
|
|
|
|
|
2024-11-17 09:25:39 -06:00
|
|
|
fn struct_fields(&self, strct: ty::Struct) -> &[StructField] {
|
|
|
|
&self.ins.struct_fields[self.struct_field_range(strct)]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn struct_field_range(&self, strct: ty::Struct) -> Range<usize> {
|
|
|
|
let start = self.ins.structs[strct].field_start as usize;
|
|
|
|
let end = self
|
|
|
|
.ins
|
|
|
|
.structs
|
|
|
|
.next(strct)
|
|
|
|
.map_or(self.ins.struct_fields.len(), |s| s.field_start as usize);
|
|
|
|
start..end
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enum_fields(&self, enm: ty::Enum) -> &[EnumField] {
|
|
|
|
&self.ins.enum_fields[self.enum_field_range(enm)]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enum_field_range(&self, enm: ty::Enum) -> Range<usize> {
|
|
|
|
let start = self.ins.enums[enm].field_start as usize;
|
|
|
|
let end =
|
|
|
|
self.ins.enums.next(enm).map_or(self.ins.enum_fields.len(), |s| s.field_start as usize);
|
|
|
|
start..end
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
fn make_opt(&mut self, base: ty::Id) -> ty::Id {
|
2024-11-08 03:25:34 -06:00
|
|
|
self.make_generic_ty(Opt { base }, |ins| &mut ins.opts, |e| SymKey::Optional(e))
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
fn make_ptr(&mut self, base: ty::Id) -> ty::Id {
|
2024-11-08 03:25:34 -06:00
|
|
|
self.make_generic_ty(Ptr { base }, |ins| &mut ins.ptrs, |e| SymKey::Pointer(e))
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
fn make_array(&mut self, elem: ty::Id, len: ArrayLen) -> ty::Id {
|
2024-11-08 03:25:34 -06:00
|
|
|
self.make_generic_ty(Array { elem, len }, |ins| &mut ins.slices, |e| SymKey::Array(e))
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
fn make_generic_ty<K: Ent + Into<ty::Id>, T: Copy>(
|
2024-10-30 14:20:03 -05:00
|
|
|
&mut self,
|
|
|
|
ty: T,
|
2024-11-08 03:25:34 -06:00
|
|
|
get_col: fn(&mut TypeIns) -> &mut EntVec<K, T>,
|
2024-10-30 14:20:03 -05:00
|
|
|
key: fn(&T) -> SymKey,
|
|
|
|
) -> ty::Id {
|
2024-11-08 03:25:34 -06:00
|
|
|
*self.syms.get_or_insert(key(&{ ty }), &mut self.ins, |ins| get_col(ins).push(ty).into())
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn size_of(&self, ty: ty::Id) -> Size {
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Slice(arr) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let arr = &self.ins.slices[arr];
|
2024-09-13 08:12:20 -05:00
|
|
|
match arr.len {
|
|
|
|
0 => 0,
|
|
|
|
ArrayLen::MAX => 16,
|
2024-10-19 12:37:02 -05:00
|
|
|
len => self.size_of(arr.elem) * len,
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Kind::Struct(stru) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
if self.ins.structs[stru].size.get() != 0 {
|
|
|
|
return self.ins.structs[stru].size.get();
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut oiter = OffsetIter::new(stru, self);
|
2024-09-28 14:56:39 -05:00
|
|
|
while oiter.next(self).is_some() {}
|
2024-11-08 03:25:34 -06:00
|
|
|
self.ins.structs[stru].size.set(oiter.offset);
|
2024-09-28 14:56:39 -05:00
|
|
|
oiter.offset
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
2024-11-17 09:25:39 -06:00
|
|
|
ty::Kind::Enum(enm) => (self.enum_field_range(enm).len().ilog2() + 7) / 8,
|
2024-10-31 04:36:18 -05:00
|
|
|
ty::Kind::Opt(opt) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let base = self.ins.opts[opt].base;
|
2024-10-31 04:36:18 -05:00
|
|
|
if self.nieche_of(base).is_some() {
|
|
|
|
self.size_of(base)
|
|
|
|
} else {
|
|
|
|
self.size_of(base) + self.align_of(base)
|
|
|
|
}
|
|
|
|
}
|
2024-10-20 14:50:08 -05:00
|
|
|
_ if let Some(size) = ty.simple_size() => size,
|
2024-09-13 08:12:20 -05:00
|
|
|
ty => unimplemented!("size_of: {:?}", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn align_of(&self, ty: ty::Id) -> Size {
|
|
|
|
match ty.expand() {
|
2024-09-22 11:17:30 -05:00
|
|
|
ty::Kind::Struct(stru) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
if self.ins.structs[stru].align.get() != 0 {
|
|
|
|
return self.ins.structs[stru].align.get() as _;
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
let align = self.ins.structs[stru].explicit_alignment.map_or_else(
|
2024-09-30 12:09:17 -05:00
|
|
|
|| {
|
|
|
|
self.struct_fields(stru)
|
|
|
|
.iter()
|
2024-11-17 09:25:39 -06:00
|
|
|
.map(|&StructField { ty, .. }| self.align_of(ty))
|
2024-09-30 12:09:17 -05:00
|
|
|
.max()
|
|
|
|
.unwrap_or(1)
|
|
|
|
},
|
|
|
|
|a| a as _,
|
|
|
|
);
|
2024-11-08 03:25:34 -06:00
|
|
|
self.ins.structs[stru].align.set(align.try_into().unwrap());
|
2024-09-30 12:09:17 -05:00
|
|
|
align
|
2024-09-22 11:17:30 -05:00
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
ty::Kind::Slice(arr) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let arr = &self.ins.slices[arr];
|
2024-09-13 08:12:20 -05:00
|
|
|
match arr.len {
|
|
|
|
ArrayLen::MAX => 8,
|
2024-10-19 12:37:02 -05:00
|
|
|
_ => self.align_of(arr.elem),
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.size_of(ty).max(1),
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
|
|
|
|
fn base_of(&self, ty: ty::Id) -> Option<ty::Id> {
|
|
|
|
match ty.expand() {
|
2024-11-08 03:25:34 -06:00
|
|
|
ty::Kind::Ptr(p) => Some(self.ins.ptrs[p].base),
|
2024-09-28 08:13:32 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2024-09-30 12:09:17 -05:00
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
fn inner_of(&self, ty: ty::Id) -> Option<ty::Id> {
|
|
|
|
match ty.expand() {
|
2024-11-08 03:25:34 -06:00
|
|
|
ty::Kind::Opt(o) => Some(self.ins.opts[o].base),
|
2024-10-30 14:20:03 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 04:36:18 -05:00
|
|
|
fn opt_layout(&self, inner_ty: ty::Id) -> OptLayout {
|
|
|
|
match self.nieche_of(inner_ty) {
|
|
|
|
Some((_, flag_offset, flag_ty)) => {
|
|
|
|
OptLayout { flag_ty, flag_offset, payload_offset: 0 }
|
|
|
|
}
|
|
|
|
None => OptLayout {
|
|
|
|
flag_ty: ty::Id::BOOL,
|
|
|
|
flag_offset: 0,
|
|
|
|
payload_offset: self.align_of(inner_ty),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nieche_of(&self, ty: ty::Id) -> Option<(bool, Offset, ty::Id)> {
|
2024-10-30 14:20:03 -05:00
|
|
|
match ty.expand() {
|
2024-10-31 04:36:18 -05:00
|
|
|
ty::Kind::Ptr(_) => Some((false, 0, ty::Id::UINT)),
|
|
|
|
// TODO: cache this
|
|
|
|
ty::Kind::Struct(s) => OffsetIter::new(s, self).into_iter(self).find_map(|(f, off)| {
|
|
|
|
self.nieche_of(f.ty).map(|(uninit, o, ty)| (uninit, o + off, ty))
|
|
|
|
}),
|
|
|
|
_ => None,
|
2024-10-30 14:20:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
fn find_struct_field(&self, s: ty::Struct, name: &str) -> Option<usize> {
|
2024-10-01 14:33:30 -05:00
|
|
|
let name = self.names.project(name)?;
|
2024-09-30 12:09:17 -05:00
|
|
|
self.struct_fields(s).iter().position(|f| f.name == name)
|
|
|
|
}
|
2024-10-25 15:59:01 -05:00
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.syms.clear();
|
|
|
|
self.names.clear();
|
|
|
|
self.strings.clear();
|
|
|
|
|
|
|
|
self.ins.funcs.clear();
|
|
|
|
self.ins.args.clear();
|
|
|
|
self.ins.globals.clear();
|
|
|
|
self.ins.structs.clear();
|
2024-11-17 09:25:39 -06:00
|
|
|
self.ins.struct_fields.clear();
|
2024-10-25 15:59:01 -05:00
|
|
|
self.ins.ptrs.clear();
|
|
|
|
self.ins.slices.clear();
|
|
|
|
|
2024-11-17 09:25:39 -06:00
|
|
|
debug_assert_eq!(self.tmp.struct_fields.len(), 0);
|
2024-10-25 15:59:01 -05:00
|
|
|
debug_assert_eq!(self.tmp.args.len(), 0);
|
|
|
|
|
|
|
|
debug_assert_eq!(self.tasks.len(), 0);
|
|
|
|
}
|
2024-11-24 09:43:45 -06:00
|
|
|
|
|
|
|
fn scope_of<'a>(&self, parent: ty::Id, file: &'a parser::Ast) -> Option<&'a [Expr<'a>]> {
|
|
|
|
match parent.expand() {
|
|
|
|
ty::Kind::Struct(s) => {
|
|
|
|
if let Expr::Struct { fields: [.., CommentOr::Or(Err(scope))], .. } =
|
|
|
|
self.ins.structs[s].ast.get(file)
|
|
|
|
{
|
|
|
|
Some(scope)
|
|
|
|
} else {
|
|
|
|
Some(&[])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_of(&self, ty: ty::Id) -> Option<ty::Id> {
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Struct(s) => Some(self.ins.structs[s].file.into()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2024-11-30 06:38:44 -06:00
|
|
|
|
|
|
|
fn captures_of<'a>(
|
|
|
|
&self,
|
|
|
|
ty: ty::Id,
|
|
|
|
file: &'a parser::Ast,
|
|
|
|
) -> Option<(&'a [Ident], ty::Tuple)> {
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Struct(s) => {
|
|
|
|
let Expr::Struct { captured, .. } = self.ins.structs[s].ast.get(file) else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
Some((captured, self.ins.structs[s].captured))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 08:12:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-31 04:36:18 -05:00
|
|
|
struct OptLayout {
|
|
|
|
flag_ty: ty::Id,
|
|
|
|
flag_offset: Offset,
|
|
|
|
payload_offset: Offset,
|
|
|
|
}
|
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
struct OffsetIter {
|
|
|
|
strct: ty::Struct,
|
|
|
|
offset: Offset,
|
2024-09-30 12:09:17 -05:00
|
|
|
fields: Range<usize>,
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OffsetIter {
|
2024-09-30 12:09:17 -05:00
|
|
|
fn new(strct: ty::Struct, tys: &Types) -> Self {
|
|
|
|
Self { strct, offset: 0, fields: tys.struct_field_range(strct) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn offset_of(tys: &Types, idx: ty::Struct, field: &str) -> Option<(Offset, ty::Id)> {
|
2024-10-01 14:33:30 -05:00
|
|
|
let field_id = tys.names.project(field)?;
|
2024-09-30 12:09:17 -05:00
|
|
|
OffsetIter::new(idx, tys)
|
|
|
|
.into_iter(tys)
|
|
|
|
.find(|(f, _)| f.name == field_id)
|
|
|
|
.map(|(f, off)| (off, f.ty))
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
|
2024-11-17 09:25:39 -06:00
|
|
|
fn next<'a>(&mut self, tys: &'a Types) -> Option<(&'a StructField, Offset)> {
|
2024-11-08 03:25:34 -06:00
|
|
|
let stru = &tys.ins.structs[self.strct];
|
2024-11-17 09:25:39 -06:00
|
|
|
let field = &tys.ins.struct_fields[self.fields.next()?];
|
2024-09-30 12:09:17 -05:00
|
|
|
|
|
|
|
let align = stru.explicit_alignment.map_or_else(|| tys.align_of(field.ty), |a| a as u32);
|
|
|
|
self.offset = (self.offset + align - 1) & !(align - 1);
|
2024-09-28 14:56:39 -05:00
|
|
|
|
|
|
|
let off = self.offset;
|
|
|
|
self.offset += tys.size_of(field.ty);
|
|
|
|
Some((field, off))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_ty(&mut self, tys: &Types) -> Option<(ty::Id, Offset)> {
|
|
|
|
let (field, off) = self.next(tys)?;
|
|
|
|
Some((field.ty, off))
|
|
|
|
}
|
|
|
|
|
2024-11-17 09:25:39 -06:00
|
|
|
fn into_iter(mut self, tys: &Types) -> impl Iterator<Item = (&StructField, Offset)> {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::iter::from_fn(move || self.next(tys))
|
2024-05-17 12:53:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 06:55:48 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn run_test(
|
|
|
|
name: &'static str,
|
|
|
|
ident: &'static str,
|
|
|
|
input: &'static str,
|
|
|
|
test: fn(&'static str, &'static str, &mut String),
|
|
|
|
) {
|
2024-09-30 12:09:17 -05:00
|
|
|
use std::{io::Write, path::PathBuf, string::ToString};
|
2024-06-23 06:55:48 -05:00
|
|
|
|
|
|
|
let filter = std::env::var("PT_FILTER").unwrap_or_default();
|
|
|
|
if !filter.is_empty() && !name.contains(&filter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output = String::new();
|
2024-09-04 16:46:32 -05:00
|
|
|
{
|
|
|
|
struct DumpOut<'a>(&'a mut String);
|
|
|
|
impl Drop for DumpOut<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if std::thread::panicking() {
|
2024-09-30 12:09:17 -05:00
|
|
|
std::println!("{}", self.0);
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dump = DumpOut(&mut output);
|
|
|
|
test(ident, input, dump.0);
|
|
|
|
}
|
2024-06-23 06:55:48 -05:00
|
|
|
|
|
|
|
let mut root = PathBuf::from(
|
|
|
|
std::env::var("PT_TEST_ROOT")
|
|
|
|
.unwrap_or(concat!(env!("CARGO_MANIFEST_DIR"), "/tests").to_string()),
|
|
|
|
);
|
2024-07-08 00:22:53 -05:00
|
|
|
root.push(name.replace("::", "_").replace(concat!(env!("CARGO_PKG_NAME"), "_"), ""));
|
2024-06-23 06:55:48 -05:00
|
|
|
root.set_extension("txt");
|
|
|
|
|
|
|
|
let expected = std::fs::read_to_string(&root).unwrap_or_default();
|
|
|
|
|
|
|
|
if output == expected {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if std::env::var("PT_UPDATE").is_ok() {
|
|
|
|
std::fs::write(&root, output).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !root.exists() {
|
|
|
|
std::fs::create_dir_all(root.parent().unwrap()).unwrap();
|
|
|
|
std::fs::write(&root, vec![]).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut proc = std::process::Command::new("diff")
|
|
|
|
.arg("-u")
|
|
|
|
.arg("--color")
|
|
|
|
.arg(&root)
|
|
|
|
.arg("-")
|
|
|
|
.stdin(std::process::Stdio::piped())
|
|
|
|
.stdout(std::process::Stdio::inherit())
|
|
|
|
.spawn()
|
|
|
|
.unwrap();
|
|
|
|
|
2024-07-08 00:22:53 -05:00
|
|
|
proc.stdin.as_mut().unwrap().write_all(output.as_bytes()).unwrap();
|
2024-06-23 06:55:48 -05:00
|
|
|
|
|
|
|
proc.wait().unwrap();
|
|
|
|
|
|
|
|
panic!("test failed");
|
|
|
|
}
|
|
|
|
|
2024-09-13 07:30:23 -05:00
|
|
|
#[cfg(test)]
|
2024-10-25 15:59:01 -05:00
|
|
|
fn test_parse_files(
|
|
|
|
ident: &str,
|
|
|
|
input: &str,
|
|
|
|
ctx: &mut parser::Ctx,
|
|
|
|
) -> (Vec<parser::Ast>, Vec<Vec<u8>>) {
|
2024-10-13 08:22:16 -05:00
|
|
|
use {
|
|
|
|
self::parser::FileKind,
|
|
|
|
std::{borrow::ToOwned, string::ToString},
|
|
|
|
};
|
2024-09-30 12:09:17 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
fn find_block<'a>(mut input: &'a str, test_name: &str) -> &'a str {
|
2024-09-13 07:30:23 -05:00
|
|
|
const CASE_PREFIX: &str = "#### ";
|
|
|
|
const CASE_SUFFIX: &str = "\n```hb";
|
|
|
|
loop {
|
|
|
|
let Some(pos) = input.find(CASE_PREFIX) else {
|
|
|
|
unreachable!("test {test_name} not found");
|
|
|
|
};
|
|
|
|
|
|
|
|
input = unsafe { input.get_unchecked(pos + CASE_PREFIX.len()..) };
|
|
|
|
if !input.starts_with(test_name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
input = unsafe { input.get_unchecked(test_name.len()..) };
|
|
|
|
if !input.starts_with(CASE_SUFFIX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
input = unsafe { input.get_unchecked(CASE_SUFFIX.len()..) };
|
|
|
|
|
|
|
|
let end = input.find("```").unwrap_or(input.len());
|
|
|
|
break unsafe { input.get_unchecked(..end) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let input = find_block(input, ident);
|
|
|
|
|
|
|
|
let mut module_map = Vec::new();
|
2024-10-13 13:01:18 -05:00
|
|
|
let mut embed_map = Vec::new();
|
2024-09-13 07:30:23 -05:00
|
|
|
let mut last_start = 0;
|
2024-10-13 13:01:18 -05:00
|
|
|
let mut last_module_name = "test.hb";
|
2024-09-13 07:30:23 -05:00
|
|
|
for (i, m) in input.match_indices("// in module: ") {
|
2024-10-13 13:01:18 -05:00
|
|
|
if last_module_name.ends_with(".hb") {
|
|
|
|
fmt::test::format(ident, input[last_start..i].trim());
|
|
|
|
module_map.push((last_module_name, &input[last_start..i]));
|
|
|
|
} else {
|
|
|
|
embed_map.push((last_module_name, &input[last_start..i]));
|
|
|
|
}
|
2024-09-13 07:30:23 -05:00
|
|
|
let (module_name, _) = input[i + m.len()..].split_once('\n').unwrap();
|
|
|
|
last_module_name = module_name;
|
|
|
|
last_start = i + m.len() + module_name.len() + 1;
|
|
|
|
}
|
2024-10-13 13:01:18 -05:00
|
|
|
if last_module_name.ends_with(".hb") {
|
|
|
|
fmt::test::format(ident, input[last_start..].trim());
|
|
|
|
module_map.push((last_module_name, &input[last_start..]));
|
|
|
|
} else {
|
|
|
|
embed_map.push((last_module_name, &input[last_start..]));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut loader = |path: &str, _: &str, kind| match kind {
|
|
|
|
FileKind::Module => module_map
|
2024-09-13 07:30:23 -05:00
|
|
|
.iter()
|
|
|
|
.position(|&(name, _)| name == path)
|
2024-10-13 13:01:18 -05:00
|
|
|
.ok_or("Module Not Found".to_string()),
|
|
|
|
FileKind::Embed => embed_map
|
|
|
|
.iter()
|
|
|
|
.position(|&(name, _)| name == path)
|
|
|
|
.ok_or("Embed Not Found".to_string()),
|
2024-09-13 07:30:23 -05:00
|
|
|
};
|
|
|
|
|
2024-10-13 13:01:18 -05:00
|
|
|
(
|
|
|
|
module_map
|
|
|
|
.iter()
|
2024-10-25 15:59:01 -05:00
|
|
|
.map(|&(path, content)| parser::Ast::new(path, content.to_owned(), ctx, &mut loader))
|
2024-10-13 13:01:18 -05:00
|
|
|
.collect(),
|
|
|
|
embed_map.iter().map(|&(_, content)| content.to_owned().into_bytes()).collect(),
|
|
|
|
)
|
2024-09-13 07:30:23 -05:00
|
|
|
}
|
|
|
|
|
2024-10-20 05:22:28 -05:00
|
|
|
fn endoce_string(
|
|
|
|
literal: &str,
|
|
|
|
str: &mut Vec<u8>,
|
|
|
|
report: impl Fn(&core::str::Bytes, &str),
|
|
|
|
) -> Option<()> {
|
|
|
|
let report = |bytes: &core::str::Bytes, msg: &_| {
|
|
|
|
report(bytes, msg);
|
|
|
|
None::<u8>
|
|
|
|
};
|
|
|
|
|
|
|
|
let decode_braces = |str: &mut Vec<u8>, bytes: &mut core::str::Bytes| {
|
|
|
|
while let Some(b) = bytes.next()
|
|
|
|
&& b != b'}'
|
|
|
|
{
|
|
|
|
let c = bytes.next().or_else(|| report(bytes, "incomplete escape sequence"))?;
|
|
|
|
let decode = |b: u8| {
|
|
|
|
Some(match b {
|
|
|
|
b'0'..=b'9' => b - b'0',
|
|
|
|
b'a'..=b'f' => b - b'a' + 10,
|
|
|
|
b'A'..=b'F' => b - b'A' + 10,
|
|
|
|
_ => report(bytes, "expected hex digit or '}'")?,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
str.push(decode(b)? << 4 | decode(c)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = literal.bytes();
|
|
|
|
while let Some(b) = bytes.next() {
|
|
|
|
if b != b'\\' {
|
|
|
|
str.push(b);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let b = match bytes.next().or_else(|| report(&bytes, "incomplete escape sequence"))? {
|
|
|
|
b'n' => b'\n',
|
|
|
|
b'r' => b'\r',
|
|
|
|
b't' => b'\t',
|
|
|
|
b'\\' => b'\\',
|
|
|
|
b'\'' => b'\'',
|
|
|
|
b'"' => b'"',
|
|
|
|
b'0' => b'\0',
|
|
|
|
b'{' => {
|
|
|
|
decode_braces(str, &mut bytes);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => report(&bytes, "unknown escape sequence, expected [nrt\\\"'{0]")?,
|
|
|
|
};
|
|
|
|
str.push(b);
|
|
|
|
}
|
|
|
|
|
2024-10-20 11:49:41 -05:00
|
|
|
if str.last() != Some(&0) {
|
|
|
|
report(&bytes, "string literal must end with null byte (for now)");
|
|
|
|
}
|
|
|
|
|
2024-10-20 05:22:28 -05:00
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2024-10-12 06:07:49 -05:00
|
|
|
pub fn quad_sort<T>(mut slice: &mut [T], mut cmp: impl FnMut(&T, &T) -> core::cmp::Ordering) {
|
|
|
|
while let Some(it) = slice.take_first_mut() {
|
|
|
|
for ot in &mut *slice {
|
|
|
|
if cmp(it, ot) == core::cmp::Ordering::Greater {
|
|
|
|
core::mem::swap(it, ot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_assert!(slice.is_sorted_by(|a, b| cmp(a, b) != core::cmp::Ordering::Greater));
|
|
|
|
}
|
2024-11-16 07:22:34 -06:00
|
|
|
|
|
|
|
type FnvBuildHasher = core::hash::BuildHasherDefault<FnvHasher>;
|
|
|
|
|
|
|
|
struct FnvHasher(u64);
|
|
|
|
|
|
|
|
impl core::hash::Hasher for FnvHasher {
|
|
|
|
fn finish(&self) -> u64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, bytes: &[u8]) {
|
|
|
|
self.0 = bytes.iter().fold(self.0, |hash, &byte| {
|
|
|
|
let mut hash = hash;
|
|
|
|
hash ^= byte as u64;
|
|
|
|
hash = hash.wrapping_mul(0x100000001B3);
|
|
|
|
hash
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FnvHasher {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(0xCBF29CE484222325)
|
|
|
|
}
|
|
|
|
}
|