2024-09-02 17:07:20 -05:00
|
|
|
#![allow(dead_code)]
|
|
|
|
use {
|
|
|
|
crate::{
|
2024-09-03 10:51:28 -05:00
|
|
|
ident::{self, Ident},
|
2024-09-04 09:54:34 -05:00
|
|
|
instrs,
|
2024-09-03 10:51:28 -05:00
|
|
|
lexer::{self, TokenKind},
|
2024-09-02 17:07:20 -05:00
|
|
|
log,
|
2024-09-04 09:54:34 -05:00
|
|
|
parser::{
|
|
|
|
self,
|
2024-09-04 10:56:59 -05:00
|
|
|
idfl::{self},
|
2024-09-04 09:54:34 -05:00
|
|
|
Expr, ExprRef, FileId, Pos,
|
|
|
|
},
|
2024-09-02 17:07:20 -05:00
|
|
|
HashMap,
|
|
|
|
},
|
2024-09-03 10:51:28 -05:00
|
|
|
core::fmt,
|
2024-09-02 17:07:20 -05:00
|
|
|
std::{
|
2024-09-06 11:50:28 -05:00
|
|
|
cell::RefCell,
|
2024-09-07 21:20:10 -05:00
|
|
|
collections::{hash_map, BTreeMap},
|
2024-09-06 11:50:28 -05:00
|
|
|
fmt::Display,
|
2024-09-07 21:20:10 -05:00
|
|
|
hash::{Hash as _, Hasher},
|
|
|
|
intrinsics::unreachable,
|
2024-09-02 17:07:20 -05:00
|
|
|
mem,
|
|
|
|
ops::{self, Range},
|
2024-09-05 18:17:54 -05:00
|
|
|
rc::Rc,
|
2024-09-07 20:12:57 -05:00
|
|
|
usize,
|
2024-09-02 17:07:20 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
macro_rules! node_color {
|
|
|
|
($self:expr, $value:expr) => {
|
|
|
|
$self.ci.colors[$self.ci.nodes[$value].color as usize - 1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
macro_rules! node_loc {
|
|
|
|
($self:expr, $value:expr) => {
|
|
|
|
$self.ci.colors[$self.ci.nodes[$value].color as usize - 1].loc
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
struct Drom(&'static str);
|
|
|
|
|
|
|
|
impl Drop for Drom {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
log::inf!("{}", self.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct BitSet {
|
|
|
|
data: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BitSet {
|
|
|
|
const ELEM_SIZE: usize = std::mem::size_of::<usize>() * 8;
|
|
|
|
|
|
|
|
pub fn clear(&mut self, bit_size: usize) {
|
|
|
|
let new_len = (bit_size + Self::ELEM_SIZE - 1) / Self::ELEM_SIZE;
|
|
|
|
self.data.clear();
|
|
|
|
self.data.resize(new_len, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, idx: usize) -> bool {
|
|
|
|
let data_idx = idx / Self::ELEM_SIZE;
|
|
|
|
let sub_idx = idx % Self::ELEM_SIZE;
|
|
|
|
let prev = self.data[data_idx] & (1 << sub_idx);
|
|
|
|
self.data[data_idx] |= 1 << sub_idx;
|
|
|
|
prev == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
type Nid = u32;
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
mod reg {
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub const STACK_PTR: Reg = 254;
|
|
|
|
pub const ZERO: Reg = 0;
|
|
|
|
pub const RET: Reg = 1;
|
|
|
|
pub const RET_ADDR: Reg = 31;
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
pub type Reg = u8;
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct AllocMeta {
|
|
|
|
rc: u16,
|
|
|
|
depth: u16,
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
allocated_at: Option<std::backtrace::Backtrace>,
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
struct Metas([AllocMeta; 256]);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
impl Default for Metas {
|
|
|
|
fn default() -> Self {
|
|
|
|
Metas(std::array::from_fn(|_| AllocMeta::default()))
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
#[derive(Default)]
|
2024-09-04 09:54:34 -05:00
|
|
|
pub struct Alloc {
|
2024-09-05 18:17:54 -05:00
|
|
|
meta: Metas,
|
2024-09-04 09:54:34 -05:00
|
|
|
free: Vec<Reg>,
|
|
|
|
max_used: Reg,
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl Alloc {
|
|
|
|
pub fn init(&mut self) {
|
|
|
|
self.free.clear();
|
|
|
|
self.free.extend((32..=253).rev());
|
|
|
|
self.max_used = RET_ADDR;
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
pub fn allocate(&mut self, depth: u16) -> Reg {
|
2024-09-04 09:54:34 -05:00
|
|
|
let reg = self.free.pop().expect("TODO: we need to spill");
|
|
|
|
self.max_used = self.max_used.max(reg);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.meta.0[reg as usize] = AllocMeta {
|
|
|
|
depth,
|
|
|
|
rc: 1,
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
allocated_at: Some(std::backtrace::Backtrace::capture()),
|
|
|
|
};
|
|
|
|
reg
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
pub fn dup(&mut self, reg: Reg) {
|
|
|
|
self.meta.0[reg as usize].rc += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn free(&mut self, reg: Reg) {
|
|
|
|
if self.meta.0[reg as usize].rc == 1 {
|
|
|
|
self.free.push(reg);
|
|
|
|
self.meta.0[reg as usize] = Default::default();
|
|
|
|
} else {
|
|
|
|
self.meta.0[reg as usize].rc -= 1;
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn pushed_size(&self) -> usize {
|
|
|
|
((self.max_used as usize).saturating_sub(RET_ADDR as usize) + 1) * 8
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-06 09:11:57 -05:00
|
|
|
|
|
|
|
pub fn mark_leaked(&mut self, reg: u8) {
|
|
|
|
self.meta.0[reg as usize].rc = u16::MAX;
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
mod ty {
|
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
lexer::TokenKind,
|
|
|
|
parser::{self, Expr},
|
|
|
|
son::ArrayLen,
|
|
|
|
},
|
|
|
|
std::{num::NonZeroU32, ops::Range},
|
|
|
|
};
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub type Builtin = u32;
|
|
|
|
pub type Struct = u32;
|
|
|
|
pub type Ptr = u32;
|
|
|
|
pub type Func = u32;
|
|
|
|
pub type Global = u32;
|
|
|
|
pub type Module = u32;
|
|
|
|
pub type Param = u32;
|
|
|
|
pub type Slice = u32;
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Tuple(pub u32);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
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;
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
|
|
|
Some(Self((pos << Self::LEN_BITS | len) as u32))
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn view(self, slice: &[Id]) -> &[Id] {
|
|
|
|
&slice[self.0 as usize >> Self::LEN_BITS..][..self.len()]
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn range(self) -> Range<usize> {
|
|
|
|
let start = self.0 as usize >> Self::LEN_BITS;
|
|
|
|
start..start + self.len()
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn len(self) -> usize {
|
|
|
|
self.0 as usize & Self::LEN_MASK
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn is_empty(self) -> bool {
|
|
|
|
self.0 == 0
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn repr(&self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
2024-09-04 09:54:34 -05:00
|
|
|
pub struct Id(NonZeroU32);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl Default for Id {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(UNDECLARED) })
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl Id {
|
|
|
|
pub const fn from_bt(bt: u32) -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(bt) })
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
pub fn is_signed(self) -> bool {
|
|
|
|
(I8..=INT).contains(&self.repr())
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_unsigned(self) -> bool {
|
|
|
|
(U8..=UINT).contains(&self.repr())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_integer(self) -> bool {
|
|
|
|
(U8..=INT).contains(&self.repr())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strip_pointer(self) -> Self {
|
|
|
|
match self.expand() {
|
|
|
|
Kind::Ptr(_) => Kind::Builtin(UINT).compress(),
|
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_pointer(self) -> bool {
|
|
|
|
matches!(Kind::from_ty(self), Kind::Ptr(_))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_upcast(self, ob: Self) -> Option<Self> {
|
|
|
|
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 () {
|
|
|
|
_ if oa == ob => oa,
|
|
|
|
_ 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,
|
|
|
|
_ if oa.is_integer() && ob.is_pointer() => ob,
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expand(self) -> Kind {
|
|
|
|
Kind::from_ty(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn repr(self) -> u32 {
|
|
|
|
self.0.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Id {
|
|
|
|
fn from(id: u64) -> Self {
|
|
|
|
Self(unsafe { NonZeroU32::new_unchecked(id as _) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u32> for Id {
|
|
|
|
fn from(id: u32) -> Self {
|
|
|
|
Kind::Builtin(id).compress()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;)*) => {
|
|
|
|
$(pub const $name: Builtin = ${index(0)} + 1;)*
|
|
|
|
|
|
|
|
mod __lc_names {
|
|
|
|
use super::*;
|
|
|
|
$(pub const $name: &[u8] = &array_to_lower_case(unsafe {
|
|
|
|
*(stringify!($name).as_ptr() as *const [u8; stringify!($name).len()]) });)*
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_str(name: &str) -> Option<Builtin> {
|
|
|
|
match name.as_bytes() {
|
|
|
|
$(__lc_names::$name => Some($name),)*
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_str(ty: Builtin) -> &'static str {
|
|
|
|
match ty {
|
|
|
|
$($name => unsafe { std::str::from_utf8_unchecked(__lc_names::$name) },)*
|
|
|
|
v => unreachable!("invalid type: {}", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
builtin_type! {
|
|
|
|
UNDECLARED;
|
|
|
|
NEVER;
|
|
|
|
VOID;
|
|
|
|
TYPE;
|
|
|
|
BOOL;
|
|
|
|
U8;
|
|
|
|
U16;
|
|
|
|
U32;
|
|
|
|
UINT;
|
|
|
|
I8;
|
|
|
|
I16;
|
|
|
|
I32;
|
|
|
|
INT;
|
2024-09-05 04:16:11 -05:00
|
|
|
LEFT_UNREACHABLE;
|
|
|
|
RIGHT_UNREACHABLE;
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! type_kind {
|
|
|
|
($(#[$meta:meta])* $vis:vis enum $name:ident {$( $variant:ident, )*}) => {
|
|
|
|
$(#[$meta])*
|
|
|
|
$vis enum $name {
|
|
|
|
$($variant($variant),)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
const FLAG_BITS: u32 = (${count($variant)} as u32).next_power_of_two().ilog2();
|
|
|
|
const FLAG_OFFSET: u32 = std::mem::size_of::<Id>() as u32 * 8 - Self::FLAG_BITS;
|
|
|
|
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 {
|
|
|
|
$(${index(0)} => Self::$variant(index),)*
|
|
|
|
i => unreachable!("{i}"),
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
|
|
|
$vis const fn compress(self) -> Id {
|
|
|
|
let (index, flag) = match self {
|
|
|
|
$(Self::$variant(index) => (index, ${index(0)}),)*
|
|
|
|
};
|
|
|
|
Id(unsafe { NonZeroU32::new_unchecked((flag << Self::FLAG_OFFSET) | index) })
|
|
|
|
}
|
|
|
|
|
|
|
|
$vis const fn inner(self) -> u32 {
|
|
|
|
match self {
|
|
|
|
$(Self::$variant(index) => index,)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type_kind! {
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum Kind {
|
|
|
|
Builtin,
|
|
|
|
Struct,
|
|
|
|
Ptr,
|
|
|
|
Func,
|
|
|
|
Global,
|
|
|
|
Module,
|
|
|
|
Slice,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Kind {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Builtin(UNDECLARED)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Display<'a> {
|
|
|
|
tys: &'a super::Types,
|
|
|
|
files: &'a [parser::Ast],
|
|
|
|
ty: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Display<'a> {
|
|
|
|
pub(super) fn new(tys: &'a super::Types, files: &'a [parser::Ast], ty: Id) -> Self {
|
|
|
|
Self { tys, files, ty }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rety(&self, ty: Id) -> Self {
|
|
|
|
Self::new(self.tys, self.files, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::fmt::Display for Display<'a> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Kind as TK;
|
|
|
|
match TK::from_ty(self.ty) {
|
|
|
|
TK::Module(idx) => write!(f, "module{}", idx),
|
|
|
|
TK::Builtin(ty) => write!(f, "{}", to_str(ty)),
|
|
|
|
TK::Ptr(ty) => {
|
|
|
|
write!(f, "^{}", self.rety(self.tys.ptrs[ty as usize].base))
|
|
|
|
}
|
|
|
|
_ if let Some((key, _)) = self
|
|
|
|
.tys
|
|
|
|
.syms
|
|
|
|
.iter()
|
|
|
|
.find(|(sym, &ty)| sym.file < self.files.len() as u32 && ty == self.ty)
|
|
|
|
&& let Some(name) = self.files[key.file as usize].exprs().iter().find_map(
|
|
|
|
|expr| match expr {
|
|
|
|
Expr::BinOp {
|
|
|
|
left: &Expr::Ident { name, id, .. },
|
|
|
|
op: TokenKind::Decl,
|
|
|
|
..
|
|
|
|
} if id == key.ident => Some(name),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
) =>
|
|
|
|
{
|
|
|
|
write!(f, "{name}")
|
|
|
|
}
|
|
|
|
TK::Struct(idx) => {
|
|
|
|
let record = &self.tys.structs[idx as usize];
|
|
|
|
write!(f, "{{")?;
|
|
|
|
for (i, &super::Field { ref name, ty }) in record.fields.iter().enumerate() {
|
|
|
|
if i != 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
write!(f, "{name}: {}", self.rety(ty))?;
|
|
|
|
}
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
|
|
|
TK::Func(idx) => write!(f, "fn{idx}"),
|
|
|
|
TK::Global(idx) => write!(f, "global{idx}"),
|
|
|
|
TK::Slice(idx) => {
|
|
|
|
let array = self.tys.arrays[idx as usize];
|
|
|
|
match array.len {
|
|
|
|
ArrayLen::MAX => write!(f, "[{}]", self.rety(array.ty)),
|
|
|
|
len => write!(f, "[{}; {len}]", self.rety(array.ty)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bin_ret(ty: Id, op: TokenKind) -> Id {
|
|
|
|
use TokenKind as T;
|
|
|
|
match op {
|
|
|
|
T::Lt | T::Gt | T::Le | T::Ge | T::Ne | T::Eq => BOOL.into(),
|
|
|
|
_ => ty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Nodes {
|
2024-09-07 21:20:10 -05:00
|
|
|
values: Vec<Result<Node, u32>>,
|
2024-09-07 20:12:57 -05:00
|
|
|
visited: BitSet,
|
2024-09-04 09:54:34 -05:00
|
|
|
free: u32,
|
2024-09-07 21:20:10 -05:00
|
|
|
lookup: HashMap<Nid, ()>,
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Nodes {
|
|
|
|
fn default() -> Self {
|
2024-09-07 20:12:57 -05:00
|
|
|
Self {
|
|
|
|
values: Default::default(),
|
|
|
|
free: u32::MAX,
|
|
|
|
lookup: Default::default(),
|
|
|
|
visited: Default::default(),
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Nodes {
|
|
|
|
fn remove_low(&mut self, id: u32) -> Node {
|
2024-09-07 21:20:10 -05:00
|
|
|
let value = mem::replace(&mut self.values[id as usize], Err(self.free)).unwrap();
|
2024-09-04 09:54:34 -05:00
|
|
|
self.free = id;
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.values.clear();
|
2024-09-04 16:46:32 -05:00
|
|
|
self.lookup.clear();
|
2024-09-04 09:54:34 -05:00
|
|
|
self.free = u32::MAX;
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
fn new_node_nop(
|
2024-09-04 09:54:34 -05:00
|
|
|
&mut self,
|
|
|
|
ty: impl Into<ty::Id>,
|
|
|
|
kind: Kind,
|
2024-09-07 21:20:10 -05:00
|
|
|
inps: impl Into<Vec<Nid>>,
|
2024-09-04 09:54:34 -05:00
|
|
|
) -> Nid {
|
2024-09-06 11:50:28 -05:00
|
|
|
let ty = ty.into();
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
let node = Node {
|
|
|
|
inputs: inps.into(),
|
2024-09-04 16:46:32 -05:00
|
|
|
kind: kind.clone(),
|
2024-09-05 18:17:54 -05:00
|
|
|
color: 0,
|
2024-09-04 09:54:34 -05:00
|
|
|
depth: u32::MAX,
|
|
|
|
lock_rc: 0,
|
2024-09-06 11:50:28 -05:00
|
|
|
ty,
|
2024-09-04 09:54:34 -05:00
|
|
|
outputs: vec![],
|
2024-09-07 21:20:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut hasher = crate::FnvHasher::default();
|
|
|
|
node.key().hash(&mut hasher);
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
let (raw_entry, _) = Self::find_node(&mut self.lookup, &self.values, &node);
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
let entry = match raw_entry {
|
|
|
|
hash_map::RawEntryMut::Occupied(mut o) => return *o.get_key_value().0,
|
|
|
|
hash_map::RawEntryMut::Vacant(v) => v,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.free == u32::MAX {
|
|
|
|
self.free = self.values.len() as _;
|
|
|
|
self.values.push(Err(u32::MAX));
|
|
|
|
}
|
|
|
|
|
|
|
|
let free = self.free;
|
|
|
|
for &d in &node.inputs {
|
|
|
|
debug_assert_ne!(d, free);
|
|
|
|
self.values[d as usize].as_mut().unwrap().outputs.push(free);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.free = mem::replace(&mut self.values[free as usize], Ok(node)).unwrap_err();
|
|
|
|
*entry.insert(free, ()).0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_node<'a>(
|
|
|
|
lookup: &'a mut HashMap<Nid, ()>,
|
|
|
|
values: &[Result<Node, u32>],
|
|
|
|
node: &Node,
|
|
|
|
) -> (hash_map::RawEntryMut<'a, u32, (), std::hash::BuildHasherDefault<crate::FnvHasher>>, u64)
|
|
|
|
{
|
|
|
|
let mut hasher = crate::FnvHasher::default();
|
|
|
|
node.key().hash(&mut hasher);
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
|
|
|
(
|
|
|
|
lookup
|
|
|
|
.raw_entry_mut()
|
|
|
|
.from_hash(hash, |&n| values[n as usize].as_ref().unwrap().key() == node.key()),
|
|
|
|
hash,
|
|
|
|
)
|
2024-09-06 11:50:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
fn remove_node_lookup(&mut self, target: Nid) {
|
|
|
|
match Self::find_node(
|
|
|
|
&mut self.lookup,
|
|
|
|
&self.values,
|
|
|
|
&self.values[target as usize].as_ref().unwrap(),
|
|
|
|
)
|
|
|
|
.0
|
|
|
|
{
|
|
|
|
hash_map::RawEntryMut::Occupied(o) => o.remove(),
|
|
|
|
hash_map::RawEntryMut::Vacant(_) => unreachable!(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_node(&mut self, ty: impl Into<ty::Id>, kind: Kind, inps: impl Into<Vec<u32>>) -> Nid {
|
2024-09-06 11:50:28 -05:00
|
|
|
let id = self.new_node_nop(ty, kind, inps);
|
2024-09-04 09:54:34 -05:00
|
|
|
if let Some(opt) = self.peephole(id) {
|
|
|
|
debug_assert_ne!(opt, id);
|
|
|
|
self.lock(opt);
|
|
|
|
self.remove(id);
|
|
|
|
self.unlock(opt);
|
|
|
|
opt
|
|
|
|
} else {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lock(&mut self, target: Nid) {
|
|
|
|
self[target].lock_rc += 1;
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
#[track_caller]
|
2024-09-04 09:54:34 -05:00
|
|
|
fn unlock(&mut self, target: Nid) {
|
|
|
|
self[target].lock_rc -= 1;
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
fn remove(&mut self, target: Nid) -> bool {
|
2024-09-04 09:54:34 -05:00
|
|
|
if !self[target].is_dangling() {
|
2024-09-05 18:17:54 -05:00
|
|
|
return false;
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
for i in 0..self[target].inputs.len() {
|
2024-09-04 09:54:34 -05:00
|
|
|
let inp = self[target].inputs[i];
|
|
|
|
let index = self[inp].outputs.iter().position(|&p| p == target).unwrap();
|
|
|
|
self[inp].outputs.swap_remove(index);
|
|
|
|
self.remove(inp);
|
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
self.remove_node_lookup(target);
|
2024-09-04 09:54:34 -05:00
|
|
|
self.remove_low(target);
|
2024-09-07 21:20:10 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
true
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn peephole(&mut self, target: Nid) -> Option<Nid> {
|
|
|
|
match self[target].kind {
|
|
|
|
Kind::Start => {}
|
|
|
|
Kind::End => {}
|
|
|
|
Kind::BinOp { op } => return self.peephole_binop(target, op),
|
|
|
|
Kind::Return => {}
|
|
|
|
Kind::Tuple { .. } => {}
|
|
|
|
Kind::ConstInt { .. } => {}
|
2024-09-04 16:46:32 -05:00
|
|
|
Kind::Call { .. } => {}
|
2024-09-06 11:50:28 -05:00
|
|
|
Kind::If => return self.peephole_if(target),
|
2024-09-05 04:16:11 -05:00
|
|
|
Kind::Region => {}
|
|
|
|
Kind::Phi => {}
|
2024-09-05 18:17:54 -05:00
|
|
|
Kind::Loop => {}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
fn peephole_if(&mut self, target: Nid) -> Option<Nid> {
|
|
|
|
let cond = self[target].inputs[1];
|
|
|
|
if let Kind::ConstInt { value } = self[cond].kind {
|
|
|
|
let ty = if value == 0 { ty::LEFT_UNREACHABLE } else { ty::RIGHT_UNREACHABLE };
|
|
|
|
return Some(self.new_node_nop(ty, Kind::If, [self[target].inputs[0], cond]));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
fn peephole_binop(&mut self, target: Nid, op: TokenKind) -> Option<Nid> {
|
|
|
|
use TokenKind as T;
|
2024-09-07 21:20:10 -05:00
|
|
|
let &[mut lhs, mut rhs] = self[target].inputs.as_slice() else { unreachable!() };
|
2024-09-04 09:54:34 -05:00
|
|
|
|
|
|
|
if lhs == rhs {
|
|
|
|
match op {
|
|
|
|
T::Sub => {
|
|
|
|
return Some(self.new_node(self[target].ty, Kind::ConstInt { value: 0 }, []));
|
|
|
|
}
|
|
|
|
T::Add => {
|
|
|
|
let rhs = self.new_node(self[target].ty, Kind::ConstInt { value: 2 }, []);
|
|
|
|
return Some(
|
|
|
|
self.new_node(self[target].ty, Kind::BinOp { op: T::Mul }, [lhs, rhs]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
if let (&Kind::ConstInt { value: a }, &Kind::ConstInt { value: b }) =
|
|
|
|
(&self[lhs].kind, &self[rhs].kind)
|
2024-09-04 09:54:34 -05:00
|
|
|
{
|
|
|
|
return Some(self.new_node(
|
|
|
|
self[target].ty,
|
|
|
|
Kind::ConstInt { value: op.apply(a, b) },
|
|
|
|
[],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut changed = false;
|
|
|
|
if op.is_comutative() && self[lhs].kind < self[rhs].kind {
|
|
|
|
std::mem::swap(&mut lhs, &mut rhs);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Kind::ConstInt { value } = self[rhs].kind {
|
|
|
|
match (op, value) {
|
|
|
|
(T::Add | T::Sub | T::Shl, 0) | (T::Mul | T::Div, 1) => return Some(lhs),
|
|
|
|
(T::Mul, 0) => return Some(rhs),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if op.is_comutative() && self[lhs].kind == (Kind::BinOp { op }) {
|
|
|
|
if let Kind::ConstInt { value: a } = self[self[lhs].inputs[1]].kind
|
|
|
|
&& let Kind::ConstInt { value: b } = self[rhs].kind
|
|
|
|
{
|
|
|
|
let new_rhs =
|
|
|
|
self.new_node(self[target].ty, Kind::ConstInt { value: op.apply(a, b) }, []);
|
|
|
|
return Some(self.new_node(self[target].ty, Kind::BinOp { op }, [
|
|
|
|
self[lhs].inputs[0],
|
|
|
|
new_rhs,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_const(self[lhs].inputs[1]) {
|
|
|
|
let new_lhs =
|
|
|
|
self.new_node(self[target].ty, Kind::BinOp { op }, [self[lhs].inputs[0], rhs]);
|
|
|
|
return Some(self.new_node(self[target].ty, Kind::BinOp { op }, [
|
|
|
|
new_lhs,
|
|
|
|
self[lhs].inputs[1],
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if op == T::Add
|
|
|
|
&& self[lhs].kind == (Kind::BinOp { op: T::Mul })
|
|
|
|
&& self[lhs].inputs[0] == rhs
|
|
|
|
&& let Kind::ConstInt { value } = self[self[lhs].inputs[1]].kind
|
|
|
|
{
|
|
|
|
let new_rhs = self.new_node(self[target].ty, Kind::ConstInt { value: value + 1 }, []);
|
|
|
|
return Some(
|
|
|
|
self.new_node(self[target].ty, Kind::BinOp { op: T::Mul }, [rhs, new_rhs]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if op == T::Sub && self[lhs].kind == (Kind::BinOp { op }) {
|
|
|
|
// (a - b) - c => a - (b + c)
|
2024-09-07 21:20:10 -05:00
|
|
|
let &[a, b] = self[lhs].inputs.as_slice() else { unreachable!() };
|
2024-09-04 09:54:34 -05:00
|
|
|
let c = rhs;
|
|
|
|
let new_rhs = self.new_node(self[target].ty, Kind::BinOp { op: T::Add }, [b, c]);
|
|
|
|
return Some(self.new_node(self[target].ty, Kind::BinOp { op }, [a, new_rhs]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
2024-09-04 16:46:32 -05:00
|
|
|
return Some(self.new_node(self[target].ty, self[target].kind.clone(), [lhs, rhs]));
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_const(&self, id: Nid) -> bool {
|
|
|
|
matches!(self[id].kind, Kind::ConstInt { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace(&mut self, target: Nid, with: Nid) {
|
|
|
|
for i in 0..self[target].outputs.len() {
|
|
|
|
let out = self[target].outputs[i];
|
2024-09-07 21:20:10 -05:00
|
|
|
let index = self[out].inputs.iter().position(|&p| p == target).unwrap();
|
2024-09-04 09:54:34 -05:00
|
|
|
let rpl = self.modify_input(out, index, with);
|
|
|
|
self[with].outputs.push(rpl);
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
self.remove(target);
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn modify_input(&mut self, target: Nid, inp_index: usize, with: Nid) -> Nid {
|
2024-09-07 21:20:10 -05:00
|
|
|
self.remove_node_lookup(target);
|
2024-09-04 09:54:34 -05:00
|
|
|
debug_assert_ne!(self[target].inputs[inp_index], with);
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
let prev = self[target].inputs[inp_index];
|
2024-09-04 09:54:34 -05:00
|
|
|
self[target].inputs[inp_index] = with;
|
2024-09-07 21:20:10 -05:00
|
|
|
let (entry, hash) = Self::find_node(
|
|
|
|
&mut self.lookup,
|
|
|
|
&self.values,
|
|
|
|
&self.values[target as usize].as_ref().unwrap(),
|
|
|
|
);
|
|
|
|
match entry {
|
|
|
|
hash_map::RawEntryMut::Occupied(mut other) => {
|
|
|
|
let rpl = *other.get_key_value().0;
|
|
|
|
self[target].inputs[inp_index] = prev;
|
|
|
|
self.replace(target, rpl);
|
|
|
|
rpl
|
|
|
|
}
|
|
|
|
hash_map::RawEntryMut::Vacant(slot) => {
|
|
|
|
slot.insert_hashed_nocheck(hash, target, ());
|
|
|
|
let index = self[prev].outputs.iter().position(|&o| o == target).unwrap();
|
|
|
|
self[prev].outputs.swap_remove(index);
|
|
|
|
self[with].outputs.push(target);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
target
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_deps(&mut self, id: Nid, deps: &[Nid]) {
|
|
|
|
for &d in deps {
|
|
|
|
debug_assert_ne!(d, id);
|
|
|
|
self[d].outputs.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
#[track_caller]
|
|
|
|
fn unlock_remove(&mut self, id: Nid) -> bool {
|
|
|
|
self[id].lock_rc -= 1;
|
|
|
|
self.remove(id)
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
fn iter(&self) -> impl DoubleEndedIterator<Item = (Nid, &Node)> {
|
2024-09-07 21:20:10 -05:00
|
|
|
self.values.iter().enumerate().filter_map(|(i, s)| Some((i as _, s.as_ref().ok()?)))
|
2024-09-07 20:12:57 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter, node: Nid, rcs: &mut [usize]) -> fmt::Result {
|
|
|
|
let mut is_ready = || {
|
|
|
|
if rcs[node as usize] == 0 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rcs[node as usize] = rcs[node as usize].saturating_sub(1);
|
|
|
|
rcs[node as usize] == 0
|
|
|
|
};
|
|
|
|
|
|
|
|
match self[node].kind {
|
|
|
|
Kind::BinOp { op } => {
|
|
|
|
write!(f, "(")?;
|
|
|
|
self.fmt(f, self[node].inputs[0], rcs)?;
|
|
|
|
write!(f, " {op} ")?;
|
|
|
|
self.fmt(f, self[node].inputs[1], rcs)?;
|
|
|
|
write!(f, ")")?;
|
|
|
|
}
|
|
|
|
Kind::Return => {
|
|
|
|
write!(f, "{}: return [{:?}] ", node, self[node].inputs[0])?;
|
2024-09-06 11:50:28 -05:00
|
|
|
if self[node].inputs[2] != 0 {
|
2024-09-05 04:16:11 -05:00
|
|
|
self.fmt(f, self[node].inputs[2], rcs)?;
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
writeln!(f)?;
|
2024-09-05 04:16:11 -05:00
|
|
|
self.fmt(f, self[node].inputs[1], rcs)?;
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
Kind::ConstInt { value } => write!(f, "{}", value)?,
|
|
|
|
Kind::End => {
|
|
|
|
if is_ready() {
|
|
|
|
writeln!(f, "{}: {:?}", node, self[node].kind)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Tuple { index } => {
|
|
|
|
if index != 0 && self[self[node].inputs[0]].kind == Kind::Start {
|
|
|
|
write!(f, "{:?}.{}", self[self[node].inputs[0]].kind, index)?;
|
|
|
|
} else if is_ready() {
|
|
|
|
writeln!(f, "{}: {:?}", node, self[node].kind)?;
|
|
|
|
for &o in &self[node].outputs {
|
|
|
|
if self.is_cfg(o) {
|
|
|
|
self.fmt(f, o, rcs)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Start => 'b: {
|
|
|
|
if !is_ready() {
|
|
|
|
break 'b;
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
writeln!(f, "{}: {:?}", node, self[node].kind)?;
|
|
|
|
|
|
|
|
for &o in &self[node].outputs {
|
|
|
|
self.fmt(f, o, rcs)?;
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-07 21:20:10 -05:00
|
|
|
Kind::Call { func } => {
|
2024-09-04 16:46:32 -05:00
|
|
|
if is_ready() {
|
|
|
|
write!(f, "{}: call {}(", node, func)?;
|
2024-09-07 21:20:10 -05:00
|
|
|
for (i, &value) in self[node].inputs.iter().skip(1).enumerate() {
|
2024-09-04 16:46:32 -05:00
|
|
|
if i != 0 {
|
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
|
|
|
self.fmt(f, value, rcs)?;
|
|
|
|
}
|
|
|
|
writeln!(f, ")")?;
|
|
|
|
for &o in &self[node].outputs {
|
|
|
|
if self.is_cfg(o) {
|
|
|
|
self.fmt(f, o, rcs)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
write!(f, "call{node}")?;
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
Kind::If => todo!(),
|
|
|
|
Kind::Region => todo!(),
|
|
|
|
Kind::Phi => todo!(),
|
2024-09-05 18:17:54 -05:00
|
|
|
Kind::Loop => todo!(),
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
fn graphviz_low(&self, out: &mut String) -> std::fmt::Result {
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
for (i, node) in self.iter() {
|
|
|
|
let color = if self.is_cfg(i) { "yellow" } else { "white" };
|
|
|
|
writeln!(out, "node{i}[label=\"{}\" color={color}]", node.kind)?;
|
|
|
|
for &o in &node.outputs {
|
|
|
|
let color = if self.is_cfg(i) && self.is_cfg(o) { "red" } else { "black" };
|
|
|
|
writeln!(out, "node{o} -> node{i}[color={color}]",)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn graphviz(&self) {
|
|
|
|
let out = &mut String::new();
|
|
|
|
_ = self.graphviz_low(out);
|
|
|
|
log::inf!("{out}");
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
fn is_cfg(&self, o: Nid) -> bool {
|
2024-09-04 16:46:32 -05:00
|
|
|
matches!(
|
|
|
|
self[o].kind,
|
2024-09-06 09:11:57 -05:00
|
|
|
Kind::Start
|
|
|
|
| Kind::End
|
|
|
|
| Kind::Return
|
|
|
|
| Kind::Tuple { .. }
|
|
|
|
| Kind::Call { .. }
|
2024-09-06 11:50:28 -05:00
|
|
|
| Kind::If
|
2024-09-06 09:11:57 -05:00
|
|
|
| Kind::Region
|
|
|
|
| Kind::Loop
|
2024-09-04 16:46:32 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_final_integrity(&self) {
|
2024-09-05 18:17:54 -05:00
|
|
|
let mut failed = false;
|
2024-09-07 20:12:57 -05:00
|
|
|
for (i, node) in self.iter() {
|
|
|
|
debug_assert_eq!(node.lock_rc, 0, "{:?}", node.kind);
|
|
|
|
if !matches!(node.kind, Kind::Return | Kind::End) && node.outputs.is_empty() {
|
|
|
|
log::err!("outputs are empry {i} {:?}", node.kind);
|
|
|
|
failed = true;
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
for &o in &node.outputs {
|
|
|
|
let mut occurs = 0;
|
|
|
|
let other = match &self.values[o as usize] {
|
2024-09-07 21:20:10 -05:00
|
|
|
Ok(other) => other,
|
|
|
|
Err(_) => {
|
2024-09-07 20:12:57 -05:00
|
|
|
log::err!("the edge points to dropped node: {i} {:?} {o}", node.kind,);
|
|
|
|
failed = true;
|
|
|
|
continue;
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
};
|
|
|
|
occurs += self[o].inputs.iter().filter(|&&el| el == i).count();
|
|
|
|
if occurs == 0 {
|
|
|
|
log::err!(
|
|
|
|
"the edge is not bidirectional: {i} {:?} {o} {:?}",
|
|
|
|
node.kind,
|
|
|
|
other.kind
|
|
|
|
);
|
|
|
|
failed = true;
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
if failed {
|
|
|
|
panic!()
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
|
|
|
|
fn check_scope_integrity(&self, scope: &[Variable]) {
|
|
|
|
for v in scope {
|
|
|
|
debug_assert!(self[v.value].lock_rc > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
fn climb_expr(&mut self, from: Nid, mut for_each: impl FnMut(Nid, &Node) -> bool) -> bool {
|
2024-09-07 20:12:57 -05:00
|
|
|
fn climb_impl(
|
|
|
|
nodes: &mut Nodes,
|
|
|
|
from: Nid,
|
|
|
|
for_each: &mut impl FnMut(Nid, &Node) -> bool,
|
|
|
|
) -> bool {
|
2024-09-07 21:20:10 -05:00
|
|
|
for i in 0..nodes[from].inputs.len() {
|
|
|
|
let n = nodes[from].inputs[i];
|
|
|
|
if n != Nid::MAX
|
2024-09-07 20:12:57 -05:00
|
|
|
&& nodes.visited.set(n as usize)
|
2024-09-07 20:15:05 -05:00
|
|
|
&& !nodes.is_cfg(n)
|
2024-09-07 20:12:57 -05:00
|
|
|
&& (for_each(n, &nodes[n]) || climb_impl(nodes, n, for_each))
|
2024-09-07 21:20:10 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2024-09-07 20:12:57 -05:00
|
|
|
}
|
|
|
|
self.visited.clear(self.values.len());
|
|
|
|
climb_impl(self, from, &mut for_each)
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Index<u32> for Nodes {
|
|
|
|
type Output = Node;
|
|
|
|
|
|
|
|
fn index(&self, index: u32) -> &Self::Output {
|
2024-09-07 21:20:10 -05:00
|
|
|
self.values[index as usize].as_ref().unwrap()
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl ops::IndexMut<u32> for Nodes {
|
|
|
|
fn index_mut(&mut self, index: u32) -> &mut Self::Output {
|
2024-09-07 21:20:10 -05:00
|
|
|
self.values[index as usize].as_mut().unwrap()
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2024-09-04 09:54:34 -05:00
|
|
|
#[repr(u8)]
|
|
|
|
pub enum Kind {
|
|
|
|
Start,
|
|
|
|
End,
|
2024-09-05 04:16:11 -05:00
|
|
|
If,
|
|
|
|
Region,
|
2024-09-05 18:17:54 -05:00
|
|
|
Loop,
|
2024-09-04 09:54:34 -05:00
|
|
|
Return,
|
|
|
|
ConstInt { value: i64 },
|
2024-09-05 04:16:11 -05:00
|
|
|
Phi,
|
2024-09-04 09:54:34 -05:00
|
|
|
Tuple { index: u32 },
|
|
|
|
BinOp { op: lexer::TokenKind },
|
2024-09-07 21:20:10 -05:00
|
|
|
Call { func: ty::Func },
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Kind {
|
|
|
|
fn disc(&self) -> u8 {
|
|
|
|
unsafe { *(self as *const _ as *const u8) }
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
impl fmt::Display for Kind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Kind::ConstInt { value } => write!(f, "#{value}"),
|
|
|
|
Kind::Tuple { index } => write!(f, "tupl[{index}]"),
|
|
|
|
Kind::BinOp { op } => write!(f, "{op}"),
|
|
|
|
Kind::Call { func, .. } => write!(f, "call {func}"),
|
|
|
|
slf => write!(f, "{slf:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
const MAX_INPUTS: usize = 3;
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Node {
|
2024-09-07 21:20:10 -05:00
|
|
|
inputs: Vec<Nid>,
|
|
|
|
outputs: Vec<Nid>,
|
2024-09-04 09:54:34 -05:00
|
|
|
kind: Kind,
|
2024-09-05 18:17:54 -05:00
|
|
|
color: u32,
|
2024-09-04 09:54:34 -05:00
|
|
|
depth: u32,
|
|
|
|
lock_rc: u32,
|
|
|
|
ty: ty::Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
fn is_dangling(&self) -> bool {
|
|
|
|
self.outputs.len() + self.lock_rc as usize == 0
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
fn key(&self) -> (&[Nid], Kind, ty::Id) {
|
|
|
|
(&self.inputs, self.kind, self.ty)
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl fmt::Display for Nodes {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.fmt(
|
|
|
|
f,
|
|
|
|
0,
|
|
|
|
&mut self
|
|
|
|
.values
|
|
|
|
.iter()
|
|
|
|
.map(|s| match s {
|
2024-09-07 21:20:10 -05:00
|
|
|
Ok(Node { kind: Kind::Start, .. }) => 1,
|
|
|
|
Ok(Node { kind: Kind::End, ref outputs, .. }) => outputs.len(),
|
|
|
|
Ok(val) => val.inputs.len(),
|
|
|
|
Err(_) => 0,
|
2024-09-04 09:54:34 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Offset = u32;
|
|
|
|
type Size = u32;
|
|
|
|
type ArrayLen = u32;
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
struct Loop {
|
2024-09-05 18:17:54 -05:00
|
|
|
node: Nid,
|
|
|
|
continue_: Nid,
|
|
|
|
continue_scope: Vec<Variable>,
|
|
|
|
break_: Nid,
|
|
|
|
break_scope: Vec<Variable>,
|
|
|
|
scope: Vec<Variable>,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 04:16:11 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2024-09-02 17:07:20 -05:00
|
|
|
struct Variable {
|
|
|
|
id: Ident,
|
|
|
|
value: Nid,
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
struct ColorMeta {
|
|
|
|
rc: u32,
|
2024-09-06 09:11:57 -05:00
|
|
|
depth: u32,
|
2024-09-06 15:00:23 -05:00
|
|
|
call_count: u32,
|
2024-09-05 18:17:54 -05:00
|
|
|
loc: Loc,
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ItemCtx {
|
|
|
|
file: FileId,
|
2024-09-04 09:54:34 -05:00
|
|
|
id: ty::Id,
|
2024-09-02 17:07:20 -05:00
|
|
|
ret: Option<ty::Id>,
|
|
|
|
|
|
|
|
task_base: usize,
|
|
|
|
|
2024-09-03 10:51:28 -05:00
|
|
|
nodes: Nodes,
|
2024-09-04 09:54:34 -05:00
|
|
|
start: Nid,
|
|
|
|
end: Nid,
|
2024-09-05 04:16:11 -05:00
|
|
|
ctrl: Nid,
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
loop_depth: u32,
|
|
|
|
colors: Vec<ColorMeta>,
|
2024-09-06 15:00:23 -05:00
|
|
|
call_count: u32,
|
2024-09-05 19:04:19 -05:00
|
|
|
filled: Vec<Nid>,
|
2024-09-06 09:11:57 -05:00
|
|
|
delayed_frees: Vec<u32>,
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
loops: Vec<Loop>,
|
|
|
|
vars: Vec<Variable>,
|
2024-09-04 09:54:34 -05:00
|
|
|
regs: reg::Alloc,
|
|
|
|
ret_relocs: Vec<Reloc>,
|
|
|
|
relocs: Vec<TypedReloc>,
|
|
|
|
code: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemCtx {
|
2024-09-05 18:17:54 -05:00
|
|
|
fn next_color(&mut self) -> u32 {
|
2024-09-06 15:00:23 -05:00
|
|
|
self.colors.push(ColorMeta {
|
|
|
|
rc: 0,
|
|
|
|
call_count: self.call_count,
|
|
|
|
depth: self.loop_depth,
|
|
|
|
loc: Default::default(),
|
|
|
|
});
|
2024-09-05 18:17:54 -05:00
|
|
|
self.colors.len() as _ // leave out 0 (sentinel)
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
fn set_next_color(&mut self, node: Nid) {
|
|
|
|
let color = self.next_color();
|
|
|
|
self.set_color(node, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_color(&mut self, node: Nid, color: u32) {
|
|
|
|
if self.nodes[node].color != 0 {
|
|
|
|
debug_assert_ne!(self.nodes[node].color, color);
|
|
|
|
self.colors[self.nodes[node].color as usize - 1].rc -= 1;
|
|
|
|
}
|
|
|
|
self.nodes[node].color = color;
|
2024-09-05 18:17:54 -05:00
|
|
|
self.colors[color as usize - 1].rc += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recolor(&mut self, node: Nid, from: u32, to: u32) {
|
|
|
|
if from == to {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.nodes[node].color != from {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
self.set_color(node, to);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
for i in 0..self.nodes[node].inputs.len() {
|
2024-09-05 18:17:54 -05:00
|
|
|
self.recolor(self.nodes[node].inputs[i], from, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
fn check_color_integrity(&self) {
|
|
|
|
let node_count = self
|
|
|
|
.nodes
|
|
|
|
.values
|
|
|
|
.iter()
|
|
|
|
.filter(|v| {
|
|
|
|
matches!(
|
|
|
|
v,
|
2024-09-07 21:20:10 -05:00
|
|
|
Ok(Node {
|
2024-09-06 09:11:57 -05:00
|
|
|
kind: Kind::BinOp { .. }
|
|
|
|
| Kind::Call { .. }
|
|
|
|
| Kind::Phi
|
|
|
|
| Kind::ConstInt { .. },
|
|
|
|
..
|
|
|
|
})
|
|
|
|
) || matches!(
|
|
|
|
v,
|
2024-09-07 21:20:10 -05:00
|
|
|
Ok(Node { kind: Kind::Tuple { index: 1.. }, inputs, .. }) if inputs.first() == Some(&0)
|
2024-09-06 09:11:57 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.count();
|
|
|
|
let color_count = self.colors.iter().map(|c| c.rc).sum::<u32>();
|
|
|
|
debug_assert_eq!(node_count, color_count as usize);
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
fn emit(&mut self, instr: (usize, [u8; instrs::MAX_SIZE])) {
|
|
|
|
emit(&mut self.code, instr);
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
|
|
|
|
fn free_loc(&mut self, loc: impl Into<Option<Loc>>) {
|
|
|
|
if let Some(loc) = loc.into() {
|
|
|
|
self.regs.free(loc.reg);
|
|
|
|
}
|
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
fn emit(out: &mut Vec<u8>, (len, instr): (usize, [u8; instrs::MAX_SIZE])) {
|
|
|
|
out.extend_from_slice(&instr[..len]);
|
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
|
|
|
|
fn write_reloc(doce: &mut [u8], offset: usize, value: i64, size: u16) {
|
|
|
|
let value = value.to_ne_bytes();
|
|
|
|
doce[offset..offset + size as usize].copy_from_slice(&value[..size as usize]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash)]
|
|
|
|
struct SymKey {
|
|
|
|
file: u32,
|
|
|
|
ident: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SymKey {
|
|
|
|
pub fn pointer_to(ty: ty::Id) -> Self {
|
|
|
|
Self { file: u32::MAX, ident: ty.repr() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Sig {
|
|
|
|
args: ty::Tuple,
|
|
|
|
ret: ty::Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Func {
|
|
|
|
file: FileId,
|
|
|
|
expr: ExprRef,
|
|
|
|
sig: Option<Sig>,
|
|
|
|
offset: Offset,
|
2024-09-04 09:54:34 -05:00
|
|
|
// TODO: change to indices into common vec
|
|
|
|
relocs: Vec<TypedReloc>,
|
|
|
|
code: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Func {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
file: u32::MAX,
|
|
|
|
expr: Default::default(),
|
|
|
|
sig: None,
|
|
|
|
offset: u32::MAX,
|
|
|
|
relocs: Default::default(),
|
|
|
|
code: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TypedReloc {
|
|
|
|
target: ty::Id,
|
|
|
|
reloc: Reloc,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Global {
|
2024-09-04 16:46:32 -05:00
|
|
|
file: FileId,
|
|
|
|
name: Ident,
|
2024-09-02 17:07:20 -05:00
|
|
|
ty: ty::Id,
|
2024-09-04 09:54:34 -05:00
|
|
|
offset: Offset,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Global {
|
|
|
|
fn default() -> Self {
|
2024-09-04 16:46:32 -05:00
|
|
|
Self {
|
|
|
|
ty: Default::default(),
|
|
|
|
offset: u32::MAX,
|
|
|
|
data: Default::default(),
|
|
|
|
file: 0,
|
|
|
|
name: 0,
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make into bit struct (width: u2, sub_offset: u3, offset: u27)
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
struct Reloc {
|
|
|
|
offset: Offset,
|
|
|
|
sub_offset: u8,
|
|
|
|
width: u8,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
impl Reloc {
|
|
|
|
fn new(offset: usize, sub_offset: u8, width: u8) -> Self {
|
|
|
|
Self { offset: offset as u32, sub_offset, width }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_jump(mut self, code: &mut [u8], to: u32, from: u32) -> i64 {
|
|
|
|
self.offset += from;
|
|
|
|
let offset = to as i64 - self.offset as i64;
|
|
|
|
self.write_offset(code, offset);
|
|
|
|
offset
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_offset(&self, code: &mut [u8], offset: i64) {
|
|
|
|
let bytes = offset.to_ne_bytes();
|
|
|
|
let slice = &mut code[self.offset as usize + self.sub_offset as usize..];
|
|
|
|
slice[..self.width as usize].copy_from_slice(&bytes[..self.width as usize]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
struct Field {
|
|
|
|
name: Rc<str>,
|
|
|
|
ty: ty::Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Struct {
|
|
|
|
fields: Rc<[Field]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Ptr {
|
|
|
|
base: ty::Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ParamAlloc(Range<u8>);
|
|
|
|
|
|
|
|
impl ParamAlloc {
|
|
|
|
pub fn next(&mut self) -> u8 {
|
|
|
|
self.0.next().expect("too many paramteters")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_wide(&mut self) -> u8 {
|
|
|
|
(self.next(), self.next()).0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Array {
|
|
|
|
ty: ty::Id,
|
|
|
|
len: ArrayLen,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Types {
|
|
|
|
syms: HashMap<SymKey, ty::Id>,
|
|
|
|
|
|
|
|
funcs: Vec<Func>,
|
|
|
|
args: Vec<ty::Id>,
|
|
|
|
globals: Vec<Global>,
|
|
|
|
structs: Vec<Struct>,
|
|
|
|
ptrs: Vec<Ptr>,
|
|
|
|
arrays: Vec<Array>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Types {
|
|
|
|
fn parama(&self, ret: impl Into<ty::Id>) -> ParamAlloc {
|
|
|
|
ParamAlloc(2 + (9..=16).contains(&self.size_of(ret.into())) as u8..12)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn offset_of(&self, idx: ty::Struct, field: &str) -> Option<(Offset, ty::Id)> {
|
|
|
|
let record = &self.structs[idx as usize];
|
|
|
|
let until = record.fields.iter().position(|f| f.name.as_ref() == field)?;
|
|
|
|
let mut offset = 0;
|
|
|
|
for &Field { ty, .. } in &record.fields[..until] {
|
|
|
|
offset = Self::align_up(offset, self.align_of(ty));
|
|
|
|
offset += self.size_of(ty);
|
|
|
|
}
|
|
|
|
Some((offset, record.fields[until].ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_ptr(&mut self, base: ty::Id) -> ty::Id {
|
|
|
|
ty::Kind::Ptr(self.make_ptr_low(base)).compress()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_ptr_low(&mut self, base: ty::Id) -> ty::Ptr {
|
|
|
|
let id = SymKey::pointer_to(base);
|
|
|
|
|
|
|
|
self.syms
|
|
|
|
.entry(id)
|
|
|
|
.or_insert_with(|| {
|
|
|
|
self.ptrs.push(Ptr { base });
|
|
|
|
ty::Kind::Ptr(self.ptrs.len() as u32 - 1).compress()
|
|
|
|
})
|
|
|
|
.expand()
|
|
|
|
.inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_array(&mut self, ty: ty::Id, len: ArrayLen) -> ty::Id {
|
|
|
|
ty::Kind::Slice(self.make_array_low(ty, len)).compress()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_array_low(&mut self, ty: ty::Id, len: ArrayLen) -> ty::Slice {
|
|
|
|
let id = SymKey {
|
|
|
|
file: match len {
|
|
|
|
ArrayLen::MAX => ArrayLen::MAX - 1,
|
|
|
|
len => ArrayLen::MAX - len - 2,
|
|
|
|
},
|
|
|
|
ident: ty.repr(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.syms
|
|
|
|
.entry(id)
|
|
|
|
.or_insert_with(|| {
|
|
|
|
self.arrays.push(Array { ty, len });
|
|
|
|
ty::Kind::Slice(self.arrays.len() as u32 - 1).compress()
|
|
|
|
})
|
|
|
|
.expand()
|
|
|
|
.inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn align_up(value: Size, align: Size) -> Size {
|
|
|
|
(value + align - 1) & !(align - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_of(&self, ty: ty::Id) -> Size {
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Ptr(_) => 8,
|
|
|
|
ty::Kind::Builtin(ty::VOID) => 0,
|
|
|
|
ty::Kind::Builtin(ty::NEVER) => unreachable!(),
|
|
|
|
ty::Kind::Builtin(ty::INT | ty::UINT) => 8,
|
|
|
|
ty::Kind::Builtin(ty::I32 | ty::U32 | ty::TYPE) => 4,
|
|
|
|
ty::Kind::Builtin(ty::I16 | ty::U16) => 2,
|
|
|
|
ty::Kind::Builtin(ty::I8 | ty::U8 | ty::BOOL) => 1,
|
|
|
|
ty::Kind::Slice(arr) => {
|
|
|
|
let arr = &self.arrays[arr as usize];
|
|
|
|
match arr.len {
|
|
|
|
0 => 0,
|
|
|
|
ArrayLen::MAX => 16,
|
|
|
|
len => self.size_of(arr.ty) * len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Kind::Struct(stru) => {
|
|
|
|
let mut offset = 0u32;
|
|
|
|
let record = &self.structs[stru as usize];
|
|
|
|
for &Field { ty, .. } in record.fields.iter() {
|
|
|
|
let align = self.align_of(ty);
|
|
|
|
offset = Self::align_up(offset, align);
|
|
|
|
offset += self.size_of(ty);
|
|
|
|
}
|
|
|
|
offset
|
|
|
|
}
|
|
|
|
ty => unimplemented!("size_of: {:?}", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn align_of(&self, ty: ty::Id) -> Size {
|
|
|
|
match ty.expand() {
|
|
|
|
ty::Kind::Struct(stru) => self.structs[stru as usize]
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.map(|&Field { ty, .. }| self.align_of(ty))
|
|
|
|
.max()
|
|
|
|
.unwrap(),
|
|
|
|
ty::Kind::Slice(arr) => {
|
|
|
|
let arr = &self.arrays[arr as usize];
|
|
|
|
match arr.len {
|
|
|
|
ArrayLen::MAX => 8,
|
|
|
|
_ => self.align_of(arr.ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.size_of(ty).max(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod task {
|
|
|
|
use super::Offset;
|
|
|
|
|
|
|
|
pub fn unpack(offset: Offset) -> Result<Offset, usize> {
|
|
|
|
if offset >> 31 != 0 {
|
|
|
|
Err((offset & !(1 << 31)) as usize)
|
|
|
|
} else {
|
|
|
|
Ok(offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(index: usize) -> Offset {
|
|
|
|
1 << 31 | index as u32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FTask {
|
|
|
|
file: FileId,
|
|
|
|
id: ty::Func,
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct Ctx {
|
|
|
|
ty: Option<ty::Id>,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl Ctx {
|
|
|
|
pub fn with_ty(self, ty: impl Into<ty::Id>) -> Self {
|
|
|
|
Self { ty: Some(ty.into()) }
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
2024-09-04 09:54:34 -05:00
|
|
|
struct Loc {
|
2024-09-05 18:17:54 -05:00
|
|
|
reg: reg::Reg,
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
#[derive(Default, Debug, Clone, Copy)]
|
2024-09-04 09:54:34 -05:00
|
|
|
struct GenCtx {
|
|
|
|
loc: Option<Loc>,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
impl GenCtx {
|
|
|
|
pub fn with_loc(self, loc: impl Into<Loc>) -> Self {
|
|
|
|
Self { loc: Some(loc.into()) }
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Pool {
|
|
|
|
cis: Vec<ItemCtx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Codegen {
|
|
|
|
pub files: Vec<parser::Ast>,
|
|
|
|
tasks: Vec<Option<FTask>>,
|
|
|
|
|
|
|
|
tys: Types,
|
|
|
|
ci: ItemCtx,
|
|
|
|
pool: Pool,
|
2024-09-06 11:50:28 -05:00
|
|
|
errors: RefCell<String>,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Codegen {
|
|
|
|
pub fn generate(&mut self) {
|
2024-09-02 17:27:50 -05:00
|
|
|
self.find_or_declare(0, 0, None, "main");
|
2024-09-03 10:51:28 -05:00
|
|
|
self.make_func_reachable(0);
|
2024-09-02 17:07:20 -05:00
|
|
|
self.complete_call_graph_low();
|
|
|
|
}
|
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
fn assemble(&mut self, to: &mut Vec<u8>) {
|
|
|
|
emit(to, instrs::jal(reg::RET_ADDR, reg::ZERO, 0));
|
|
|
|
emit(to, instrs::tx());
|
|
|
|
self.dump_reachable(0, to);
|
|
|
|
Reloc::new(0, 3, 4).apply_jump(to, self.tys.funcs[0].offset, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dump_reachable(&mut self, from: ty::Func, to: &mut Vec<u8>) {
|
|
|
|
let mut frontier = vec![ty::Kind::Func(from).compress()];
|
|
|
|
|
|
|
|
while let Some(itm) = frontier.pop() {
|
|
|
|
match itm.expand() {
|
|
|
|
ty::Kind::Func(func) => {
|
|
|
|
let fuc = &mut self.tys.funcs[func as usize];
|
|
|
|
if task::unpack(fuc.offset).is_ok() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fuc.offset = to.len() as _;
|
|
|
|
to.extend(&fuc.code);
|
|
|
|
frontier.extend(fuc.relocs.iter().map(|r| r.target));
|
|
|
|
}
|
|
|
|
ty::Kind::Global(glob) => {
|
|
|
|
let glb = &mut self.tys.globals[glob as usize];
|
|
|
|
if task::unpack(glb.offset).is_ok() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
glb.offset = to.len() as _;
|
|
|
|
to.extend(&glb.data);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for fuc in &self.tys.funcs {
|
|
|
|
if task::unpack(fuc.offset).is_err() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for rel in &fuc.relocs {
|
|
|
|
let offset = match rel.target.expand() {
|
|
|
|
ty::Kind::Func(fun) => self.tys.funcs[fun as usize].offset,
|
|
|
|
ty::Kind::Global(glo) => self.tys.globals[glo as usize].offset,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
rel.reloc.apply_jump(to, offset, fuc.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disasm(
|
|
|
|
&mut self,
|
|
|
|
mut sluce: &[u8],
|
|
|
|
output: &mut impl std::io::Write,
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
use crate::DisasmItem;
|
|
|
|
let functions = self
|
|
|
|
.tys
|
|
|
|
.funcs
|
|
|
|
.iter()
|
|
|
|
.filter(|f| task::unpack(f.offset).is_ok())
|
|
|
|
.map(|f| {
|
|
|
|
let file = &self.files[f.file as usize];
|
|
|
|
let Expr::BinOp { left: &Expr::Ident { name, .. }, .. } = f.expr.get(file).unwrap()
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
(f.offset, (name, f.code.len() as u32, DisasmItem::Func))
|
|
|
|
})
|
|
|
|
.chain(self.tys.globals.iter().filter(|g| task::unpack(g.offset).is_ok()).map(|g| {
|
|
|
|
let file = &self.files[g.file as usize];
|
|
|
|
|
|
|
|
(g.offset, (file.ident_str(g.name), self.tys.size_of(g.ty), DisasmItem::Global))
|
|
|
|
}))
|
2024-09-05 04:16:11 -05:00
|
|
|
.collect::<BTreeMap<_, _>>();
|
2024-09-04 16:46:32 -05:00
|
|
|
crate::disasm(&mut sluce, &functions, output, |_| {})
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-03 10:51:28 -05:00
|
|
|
fn make_func_reachable(&mut self, func: ty::Func) {
|
|
|
|
let fuc = &mut self.tys.funcs[func as usize];
|
|
|
|
if fuc.offset == u32::MAX {
|
|
|
|
fuc.offset = task::id(self.tasks.len() as _);
|
|
|
|
self.tasks.push(Some(FTask { file: fuc.file, id: func }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
fn expr(&mut self, expr: &Expr) -> Option<Nid> {
|
|
|
|
self.expr_ctx(expr, Ctx::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_struct(&mut self, fields: &[(&str, Expr)]) -> ty::Struct {
|
|
|
|
let fields = fields
|
|
|
|
.iter()
|
|
|
|
.map(|&(name, ty)| Field { name: name.into(), ty: self.ty(&ty) })
|
|
|
|
.collect();
|
|
|
|
self.tys.structs.push(Struct { fields });
|
|
|
|
self.tys.structs.len() as u32 - 1
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:27:50 -05:00
|
|
|
fn expr_ctx(&mut self, expr: &Expr, ctx: Ctx) -> Option<Nid> {
|
2024-09-04 16:46:32 -05:00
|
|
|
let msg = "i know nothing about this name gal which is vired \
|
|
|
|
because we parsed succesfully";
|
2024-09-03 10:51:28 -05:00
|
|
|
match *expr {
|
2024-09-06 11:50:28 -05:00
|
|
|
Expr::Comment { .. } => Some(0),
|
2024-09-05 18:17:54 -05:00
|
|
|
Expr::Ident { pos, id, .. } => {
|
2024-09-06 11:50:28 -05:00
|
|
|
let Some(index) = self.ci.vars.iter().position(|v| v.id == id) else {
|
|
|
|
self.report(pos, msg);
|
|
|
|
return Some(self.ci.end);
|
|
|
|
};
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
if self.ci.vars[index].value == 0 {
|
|
|
|
self.ci.nodes.unlock(self.ci.vars[index].value);
|
2024-09-05 18:17:54 -05:00
|
|
|
let loob = self.ci.loops.last_mut().unwrap();
|
|
|
|
let inps = [loob.node, loob.scope[index].value, loob.scope[index].value];
|
|
|
|
self.ci.nodes.unlock(inps[1]);
|
|
|
|
let ty = self.ci.nodes[inps[1]].ty;
|
|
|
|
// TODO: dont apply peepholes here
|
|
|
|
let phy = self.ci.nodes.new_node(ty, Kind::Phi, inps);
|
|
|
|
self.ci.nodes[phy].lock_rc += 2;
|
|
|
|
self.ci.vars[index].value = phy;
|
|
|
|
loob.scope[index].value = phy;
|
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
Some(self.ci.vars[index].value)
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
Expr::BinOp { left: &Expr::Ident { id, .. }, op: TokenKind::Decl, right } => {
|
|
|
|
let value = self.expr(right)?;
|
|
|
|
self.ci.nodes.lock(value);
|
|
|
|
self.ci.vars.push(Variable { id, value });
|
2024-09-06 11:50:28 -05:00
|
|
|
Some(0)
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
Expr::BinOp { left: &Expr::Ident { id, pos, .. }, op: TokenKind::Assign, right } => {
|
|
|
|
let value = self.expr(right)?;
|
|
|
|
self.ci.nodes.lock(value);
|
|
|
|
|
|
|
|
let Some(var) = self.ci.vars.iter_mut().find(|v| v.id == id) else {
|
|
|
|
self.report(pos, msg);
|
2024-09-06 11:50:28 -05:00
|
|
|
return Some(self.ci.end);
|
2024-09-04 16:46:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let prev = std::mem::replace(&mut var.value, value);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.unlock_remove(prev);
|
2024-09-06 11:50:28 -05:00
|
|
|
Some(0)
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
Expr::BinOp { left, op, right } => {
|
|
|
|
let lhs = self.expr_ctx(left, ctx)?;
|
|
|
|
self.ci.nodes.lock(lhs);
|
|
|
|
let rhs = self.expr_ctx(right, Ctx::default().with_ty(self.tof(lhs)));
|
|
|
|
self.ci.nodes.unlock(lhs);
|
|
|
|
let rhs = rhs?;
|
2024-09-06 11:50:28 -05:00
|
|
|
let ty = self.assert_ty(
|
|
|
|
left.pos(),
|
|
|
|
self.tof(rhs),
|
|
|
|
self.tof(lhs),
|
|
|
|
false,
|
|
|
|
"right operand",
|
|
|
|
);
|
2024-09-03 10:51:28 -05:00
|
|
|
let id =
|
|
|
|
self.ci.nodes.new_node(ty::bin_ret(ty, op), Kind::BinOp { op }, [lhs, rhs]);
|
|
|
|
Some(id)
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
Expr::If { cond, then, else_, .. } => {
|
2024-09-05 04:16:11 -05:00
|
|
|
let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?;
|
|
|
|
|
|
|
|
let if_node = self.ci.nodes.new_node(ty::VOID, Kind::If, [self.ci.ctrl, cond]);
|
|
|
|
|
|
|
|
'b: {
|
|
|
|
let branch = match self.tof(if_node).expand().inner() {
|
|
|
|
ty::LEFT_UNREACHABLE => else_,
|
|
|
|
ty::RIGHT_UNREACHABLE => Some(then),
|
|
|
|
_ => break 'b,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.ci.nodes.lock(self.ci.ctrl);
|
|
|
|
self.ci.nodes.remove(if_node);
|
|
|
|
self.ci.nodes.unlock(self.ci.ctrl);
|
|
|
|
|
|
|
|
if let Some(branch) = branch {
|
|
|
|
return self.expr(branch);
|
|
|
|
} else {
|
2024-09-06 11:50:28 -05:00
|
|
|
return Some(0);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let else_scope = self.ci.vars.clone();
|
2024-09-05 18:17:54 -05:00
|
|
|
for &el in &self.ci.vars {
|
|
|
|
self.ci.nodes.lock(el.value);
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
|
|
|
|
self.ci.ctrl =
|
|
|
|
self.ci.nodes.new_node(ty::VOID, Kind::Tuple { index: 0 }, [if_node]);
|
2024-09-06 11:50:28 -05:00
|
|
|
let lcntrl = self.expr(then).map_or(Nid::MAX, |_| self.ci.ctrl);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
|
|
|
let then_scope = std::mem::replace(&mut self.ci.vars, else_scope);
|
|
|
|
self.ci.ctrl =
|
|
|
|
self.ci.nodes.new_node(ty::VOID, Kind::Tuple { index: 1 }, [if_node]);
|
|
|
|
let rcntrl = if let Some(else_) = else_ {
|
2024-09-06 11:50:28 -05:00
|
|
|
self.expr(else_).map_or(Nid::MAX, |_| self.ci.ctrl)
|
2024-09-05 04:16:11 -05:00
|
|
|
} else {
|
|
|
|
self.ci.ctrl
|
|
|
|
};
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
if lcntrl == Nid::MAX && rcntrl == Nid::MAX {
|
2024-09-05 18:17:54 -05:00
|
|
|
for then_var in then_scope {
|
|
|
|
self.ci.nodes.unlock_remove(then_var.value);
|
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
2024-09-05 04:16:11 -05:00
|
|
|
return None;
|
2024-09-06 11:50:28 -05:00
|
|
|
} else if lcntrl == Nid::MAX {
|
2024-09-05 18:17:54 -05:00
|
|
|
for then_var in then_scope {
|
|
|
|
self.ci.nodes.unlock_remove(then_var.value);
|
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
2024-09-06 11:50:28 -05:00
|
|
|
return Some(0);
|
|
|
|
} else if rcntrl == Nid::MAX {
|
2024-09-05 18:17:54 -05:00
|
|
|
for else_var in &self.ci.vars {
|
|
|
|
self.ci.nodes.unlock_remove(else_var.value);
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
self.ci.vars = then_scope;
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
2024-09-06 15:00:23 -05:00
|
|
|
self.ci.ctrl = lcntrl;
|
2024-09-06 11:50:28 -05:00
|
|
|
return Some(0);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Region, [lcntrl, rcntrl]);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
|
|
|
for (else_var, then_var) in self.ci.vars.iter_mut().zip(then_scope) {
|
|
|
|
if else_var.value == then_var.value {
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.unlock_remove(then_var.value);
|
2024-09-05 04:16:11 -05:00
|
|
|
continue;
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.unlock(then_var.value);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
|
|
|
let ty = self.ci.nodes[else_var.value].ty;
|
|
|
|
debug_assert_eq!(
|
|
|
|
ty, self.ci.nodes[then_var.value].ty,
|
|
|
|
"TODO: typecheck properly"
|
|
|
|
);
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
let inps = [self.ci.ctrl, then_var.value, else_var.value];
|
|
|
|
self.ci.nodes.unlock(else_var.value);
|
2024-09-05 04:16:11 -05:00
|
|
|
else_var.value = self.ci.nodes.new_node(ty, Kind::Phi, inps);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.lock(else_var.value);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
Some(0)
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
Expr::Loop { body, .. } => {
|
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Loop, [self.ci.ctrl; 2]);
|
|
|
|
self.ci.loops.push(Loop {
|
|
|
|
node: self.ci.ctrl,
|
2024-09-06 11:50:28 -05:00
|
|
|
continue_: Nid::MAX,
|
2024-09-05 18:17:54 -05:00
|
|
|
continue_scope: vec![],
|
2024-09-06 11:50:28 -05:00
|
|
|
break_: Nid::MAX,
|
2024-09-05 18:17:54 -05:00
|
|
|
break_scope: vec![],
|
|
|
|
scope: self.ci.vars.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
for var in &mut self.ci.vars {
|
2024-09-06 11:50:28 -05:00
|
|
|
var.value = 0;
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.nodes[0].lock_rc += self.ci.vars.len() as u32;
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
self.expr(body);
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
let Loop { node, continue_, mut continue_scope, break_, mut break_scope, scope } =
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.loops.pop().unwrap();
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
if continue_ != Nid::MAX {
|
|
|
|
self.ci.ctrl =
|
|
|
|
self.ci.nodes.new_node(ty::VOID, Kind::Region, [self.ci.ctrl, continue_]);
|
|
|
|
|
|
|
|
std::mem::swap(&mut self.ci.vars, &mut continue_scope);
|
|
|
|
|
|
|
|
for (else_var, then_var) in self.ci.vars.iter_mut().zip(continue_scope) {
|
|
|
|
if else_var.value == then_var.value {
|
|
|
|
self.ci.nodes.unlock_remove(then_var.value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
self.ci.nodes.unlock(then_var.value);
|
|
|
|
|
|
|
|
let ty = self.ci.nodes[else_var.value].ty;
|
|
|
|
debug_assert_eq!(
|
|
|
|
ty, self.ci.nodes[then_var.value].ty,
|
|
|
|
"TODO: typecheck properly"
|
|
|
|
);
|
|
|
|
|
|
|
|
let inps = [self.ci.ctrl, then_var.value, else_var.value];
|
|
|
|
self.ci.nodes.unlock(else_var.value);
|
|
|
|
else_var.value = self.ci.nodes.new_node(ty, Kind::Phi, inps);
|
|
|
|
self.ci.nodes.lock(else_var.value);
|
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
2024-09-06 15:00:23 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
self.ci.nodes.modify_input(node, 1, self.ci.ctrl);
|
|
|
|
|
|
|
|
self.ci.ctrl = break_;
|
2024-09-06 11:50:28 -05:00
|
|
|
if break_ == Nid::MAX {
|
2024-09-05 18:17:54 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.lock(self.ci.ctrl);
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
std::mem::swap(&mut self.ci.vars, &mut break_scope);
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
|
|
|
self.ci.nodes.check_scope_integrity(&break_scope);
|
|
|
|
self.ci.nodes.check_scope_integrity(&scope);
|
2024-09-06 15:00:23 -05:00
|
|
|
for ((dest_var, scope_var), loop_var) in
|
|
|
|
self.ci.vars.iter_mut().zip(scope).zip(break_scope)
|
|
|
|
{
|
2024-09-06 11:50:28 -05:00
|
|
|
if loop_var.value == 0 {
|
|
|
|
self.ci.nodes.unlock(loop_var.value);
|
2024-09-06 15:00:23 -05:00
|
|
|
debug_assert!(loop_var.value == dest_var.value);
|
|
|
|
self.ci.nodes.unlock(dest_var.value);
|
|
|
|
dest_var.value = scope_var.value;
|
2024-09-05 18:17:54 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert_eq!(self.ci.nodes[scope_var.value].kind, Kind::Phi);
|
|
|
|
|
|
|
|
if scope_var.value == loop_var.value {
|
|
|
|
let orig = self.ci.nodes[scope_var.value].inputs[1];
|
|
|
|
self.ci.nodes.lock(orig);
|
2024-09-06 15:00:23 -05:00
|
|
|
self.ci.nodes.unlock(loop_var.value);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.unlock_remove(scope_var.value);
|
2024-09-06 15:00:23 -05:00
|
|
|
self.ci.nodes.unlock_remove(dest_var.value);
|
|
|
|
dest_var.value = orig;
|
2024-09-05 18:17:54 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.nodes.unlock(loop_var.value);
|
2024-09-06 15:00:23 -05:00
|
|
|
let phy = self.ci.nodes.modify_input(scope_var.value, 2, loop_var.value);
|
|
|
|
self.ci.nodes.unlock_remove(dest_var.value);
|
|
|
|
dest_var.value = phy;
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.unlock(self.ci.ctrl);
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
Some(0)
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-09-06 15:00:23 -05:00
|
|
|
Expr::Break { pos } => {
|
|
|
|
let Some(loob) = self.ci.loops.last_mut() else {
|
|
|
|
self.report(pos, "break outside a loop");
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
if loob.break_ == Nid::MAX {
|
|
|
|
loob.break_ = self.ci.ctrl;
|
|
|
|
loob.break_scope = self.ci.vars[..loob.scope.len()].to_owned();
|
|
|
|
for v in &loob.break_scope {
|
|
|
|
self.ci.nodes.lock(v.value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loob.break_ =
|
|
|
|
self.ci.nodes.new_node(ty::VOID, Kind::Region, [self.ci.ctrl, loob.break_]);
|
|
|
|
|
|
|
|
for (else_var, then_var) in loob.break_scope.iter_mut().zip(&self.ci.vars) {
|
|
|
|
if else_var.value == then_var.value {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = self.ci.nodes[else_var.value].ty;
|
|
|
|
debug_assert_eq!(
|
|
|
|
ty, self.ci.nodes[then_var.value].ty,
|
|
|
|
"TODO: typecheck properly"
|
|
|
|
);
|
|
|
|
|
|
|
|
let inps = [loob.break_, then_var.value, else_var.value];
|
|
|
|
self.ci.nodes.unlock(else_var.value);
|
|
|
|
else_var.value = self.ci.nodes.new_node(ty, Kind::Phi, inps);
|
|
|
|
self.ci.nodes.lock(else_var.value);
|
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
|
|
|
self.ci.nodes.check_scope_integrity(&loob.break_scope);
|
2024-09-06 15:00:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.ctrl = self.ci.end;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Expr::Continue { pos } => {
|
2024-09-07 20:12:57 -05:00
|
|
|
todo!();
|
2024-09-06 15:00:23 -05:00
|
|
|
let Some(loob) = self.ci.loops.last_mut() else {
|
|
|
|
self.report(pos, "break outside a loop");
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
if loob.continue_ == Nid::MAX {
|
|
|
|
loob.continue_ = self.ci.ctrl;
|
|
|
|
loob.continue_scope = self.ci.vars[..loob.scope.len()].to_owned();
|
|
|
|
for v in &loob.continue_scope {
|
|
|
|
self.ci.nodes.lock(v.value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loob.continue_ = self
|
|
|
|
.ci
|
|
|
|
.nodes
|
|
|
|
.new_node(ty::VOID, Kind::Region, [self.ci.ctrl, loob.continue_]);
|
|
|
|
|
|
|
|
for (else_var, then_var) in loob.continue_scope.iter_mut().zip(&self.ci.vars) {
|
|
|
|
if else_var.value == then_var.value {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = self.ci.nodes[else_var.value].ty;
|
|
|
|
debug_assert_eq!(
|
|
|
|
ty, self.ci.nodes[then_var.value].ty,
|
|
|
|
"TODO: typecheck properly"
|
|
|
|
);
|
|
|
|
|
|
|
|
let inps = [loob.continue_, then_var.value, else_var.value];
|
|
|
|
self.ci.nodes.unlock(else_var.value);
|
|
|
|
else_var.value = self.ci.nodes.new_node(ty, Kind::Phi, inps);
|
|
|
|
self.ci.nodes.lock(else_var.value);
|
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.check_scope_integrity(&self.ci.vars);
|
2024-09-06 15:00:23 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.ctrl = self.ci.end;
|
2024-09-05 18:17:54 -05:00
|
|
|
None
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
Expr::Call { func: &Expr::Ident { pos, id, name, .. }, args, .. } => {
|
2024-09-05 04:16:11 -05:00
|
|
|
self.ci.call_count += 1;
|
2024-09-04 16:46:32 -05:00
|
|
|
let func = self.find_or_declare(pos, self.ci.file, Some(id), name);
|
|
|
|
let ty::Kind::Func(func) = func else {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
format_args!(
|
|
|
|
"compiler cant (yet) call '{}'",
|
|
|
|
self.ty_display(func.compress())
|
|
|
|
),
|
|
|
|
);
|
2024-09-06 11:50:28 -05:00
|
|
|
return Some(self.ci.end);
|
2024-09-04 16:46:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let fuc = &self.tys.funcs[func as usize];
|
|
|
|
let sig = fuc.sig.expect("TODO: generic functions");
|
|
|
|
let ast = self.files[fuc.file as usize].clone();
|
|
|
|
let Expr::BinOp { right: &Expr::Closure { args: cargs, .. }, .. } =
|
|
|
|
fuc.expr.get(&ast).unwrap()
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
self.assert_report(
|
|
|
|
args.len() == cargs.len(),
|
|
|
|
pos,
|
|
|
|
format_args!(
|
|
|
|
"expected {} function argumenr{}, got {}",
|
|
|
|
cargs.len(),
|
|
|
|
if cargs.len() == 1 { "" } else { "s" },
|
|
|
|
args.len()
|
|
|
|
),
|
|
|
|
);
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
let mut inps = vec![self.ci.ctrl];
|
2024-09-06 11:50:28 -05:00
|
|
|
for ((arg, carg), tyx) in args.iter().zip(cargs).zip(sig.args.range()) {
|
2024-09-04 16:46:32 -05:00
|
|
|
let ty = self.tys.args[tyx];
|
2024-09-05 04:16:11 -05:00
|
|
|
if self.tys.size_of(ty) == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
let value = self.expr_ctx(arg, Ctx::default().with_ty(ty))?;
|
2024-09-06 11:50:28 -05:00
|
|
|
_ = self.assert_ty(
|
|
|
|
arg.pos(),
|
|
|
|
self.tof(value),
|
|
|
|
ty,
|
|
|
|
true,
|
|
|
|
format_args!("argument {}", carg.name),
|
|
|
|
);
|
2024-09-04 16:46:32 -05:00
|
|
|
inps.push(value);
|
|
|
|
}
|
2024-09-07 21:20:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(sig.ret, Kind::Call { func }, inps);
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-05 04:16:11 -05:00
|
|
|
Some(self.ci.ctrl)
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
Expr::Return { pos, val } => {
|
2024-09-06 15:00:23 -05:00
|
|
|
let value = if let Some(val) = val {
|
|
|
|
self.expr_ctx(val, Ctx { ty: self.ci.ret })?
|
2024-09-03 10:51:28 -05:00
|
|
|
} else {
|
2024-09-06 15:00:23 -05:00
|
|
|
0
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
_ = self.ci.nodes[self.ci.ctrl];
|
|
|
|
_ = self.ci.nodes[self.ci.end];
|
|
|
|
_ = self.ci.nodes[value];
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
let inps = [self.ci.ctrl, self.ci.end, value];
|
2024-09-07 20:12:57 -05:00
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
let out = &mut String::new();
|
|
|
|
self.report_log_to(pos, "returning here", out);
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Return, inps);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
let expected = *self.ci.ret.get_or_insert(self.tof(value));
|
|
|
|
_ = self.assert_ty(pos, self.tof(value), expected, true, "return value");
|
2024-09-03 10:51:28 -05:00
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Expr::Block { stmts, .. } => {
|
|
|
|
let base = self.ci.vars.len();
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
let mut ret = Some(0);
|
2024-09-03 10:51:28 -05:00
|
|
|
for stmt in stmts {
|
|
|
|
ret = ret.and(self.expr(stmt));
|
|
|
|
if let Some(id) = ret {
|
2024-09-06 11:50:28 -05:00
|
|
|
_ = self.assert_ty(
|
|
|
|
stmt.pos(),
|
|
|
|
self.tof(id),
|
|
|
|
ty::VOID.into(),
|
|
|
|
true,
|
|
|
|
"statement",
|
|
|
|
);
|
2024-09-03 10:51:28 -05:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.nodes.lock(self.ci.ctrl);
|
2024-09-03 10:51:28 -05:00
|
|
|
for var in self.ci.vars.drain(base..) {
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.unlock_remove(var.value);
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.nodes.unlock(self.ci.ctrl);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
Expr::Number { value, .. } => Some(self.ci.nodes.new_node(
|
2024-09-06 11:50:28 -05:00
|
|
|
ctx.ty.filter(|ty| ty.is_integer() || ty.is_pointer()).unwrap_or(ty::INT.into()),
|
2024-09-03 10:51:28 -05:00
|
|
|
Kind::ConstInt { value },
|
|
|
|
[],
|
|
|
|
)),
|
2024-09-06 11:50:28 -05:00
|
|
|
ref e => {
|
|
|
|
self.report_unhandled_ast(e, "bruh");
|
|
|
|
Some(self.ci.end)
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn tof(&self, id: Nid) -> ty::Id {
|
|
|
|
self.ci.nodes[id].ty
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn complete_call_graph(&mut self) {
|
|
|
|
self.complete_call_graph_low();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn complete_call_graph_low(&mut self) {
|
|
|
|
while self.ci.task_base < self.tasks.len()
|
|
|
|
&& let Some(task_slot) = self.tasks.pop()
|
|
|
|
{
|
|
|
|
let Some(task) = task_slot else { continue };
|
|
|
|
self.handle_task(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_task(&mut self, FTask { file, id }: FTask) {
|
2024-09-05 04:16:11 -05:00
|
|
|
let func = &mut self.tys.funcs[id as usize];
|
|
|
|
func.offset = u32::MAX - 1;
|
2024-09-03 10:51:28 -05:00
|
|
|
debug_assert!(func.file == file);
|
|
|
|
let sig = func.sig.unwrap();
|
|
|
|
let ast = self.files[file as usize].clone();
|
|
|
|
let expr = func.expr.get(&ast).unwrap();
|
|
|
|
|
|
|
|
let repl = ItemCtx {
|
|
|
|
file,
|
2024-09-04 09:54:34 -05:00
|
|
|
id: ty::Kind::Func(id).compress(),
|
2024-09-03 10:51:28 -05:00
|
|
|
ret: Some(sig.ret),
|
|
|
|
..self.pool.cis.pop().unwrap_or_default()
|
|
|
|
};
|
|
|
|
let prev_ci = std::mem::replace(&mut self.ci, repl);
|
|
|
|
|
|
|
|
self.ci.start = self.ci.nodes.new_node(ty::VOID, Kind::Start, []);
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.end = self.ci.nodes.new_node(ty::NEVER, Kind::End, []);
|
2024-09-05 04:16:11 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Tuple { index: 0 }, [self.ci.start]);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
|
|
|
let Expr::BinOp {
|
|
|
|
left: Expr::Ident { .. },
|
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: &Expr::Closure { body, args, .. },
|
|
|
|
} = expr
|
|
|
|
else {
|
|
|
|
unreachable!("{expr}")
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut sig_args = sig.args.range();
|
|
|
|
for (arg, index) in args.iter().zip(1u32..) {
|
|
|
|
let ty = self.tys.args[sig_args.next().unwrap()];
|
|
|
|
let value = self.ci.nodes.new_node(ty, Kind::Tuple { index }, [self.ci.start]);
|
|
|
|
self.ci.nodes.lock(value);
|
|
|
|
let sym = parser::find_symbol(&ast.symbols, arg.id);
|
|
|
|
assert!(sym.flags & idfl::COMPTIME == 0, "TODO");
|
|
|
|
self.ci.vars.push(Variable { id: arg.id, value });
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
let mut orig_vars = self.ci.vars.clone();
|
|
|
|
|
2024-09-03 10:51:28 -05:00
|
|
|
if self.expr(body).is_some() {
|
|
|
|
self.report(body.pos(), "expected all paths in the fucntion to return");
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
for (i, var) in self.ci.vars.drain(..).enumerate() {
|
|
|
|
let ty = self.ci.nodes[var.value].ty;
|
|
|
|
if self.ci.nodes.unlock_remove(var.value) {
|
|
|
|
// mark as unused
|
|
|
|
orig_vars[i].id = u32::MAX;
|
|
|
|
orig_vars[i].value = ty.repr();
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
if self.errors.borrow().is_empty() {
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.graphviz();
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
self.ci.nodes.check_final_integrity();
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
log::trc!("{}", self.ci.nodes);
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
'_open_function: {
|
|
|
|
self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, 0));
|
|
|
|
self.ci.emit(instrs::st(reg::RET_ADDR, reg::STACK_PTR, 0, 0));
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.regs.init();
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
let call_count = self.ci.call_count;
|
2024-09-06 11:50:28 -05:00
|
|
|
'_color_args: {
|
|
|
|
for var in &orig_vars {
|
|
|
|
if var.id != u32::MAX {
|
|
|
|
self.ci.set_next_color(var.value);
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
self.color_control(self.ci.nodes[self.ci.start].outputs[0]);
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
self.ci.check_color_integrity();
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.vars = orig_vars;
|
2024-09-06 15:00:23 -05:00
|
|
|
self.ci.call_count = call_count;
|
2024-09-06 11:50:28 -05:00
|
|
|
self.emit_control(self.ci.nodes[self.ci.start].outputs[0]);
|
|
|
|
self.ci.vars.clear();
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
if let Some(last_ret) = self.ci.ret_relocs.last()
|
|
|
|
&& last_ret.offset as usize == self.ci.code.len() - 5
|
|
|
|
{
|
|
|
|
self.ci.code.truncate(self.ci.code.len() - 5);
|
|
|
|
self.ci.ret_relocs.pop();
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
let end = self.ci.code.len();
|
|
|
|
for ret_rel in self.ci.ret_relocs.drain(..) {
|
|
|
|
ret_rel.apply_jump(&mut self.ci.code, end as _, 0);
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
'_close_function: {
|
|
|
|
let pushed = self.ci.regs.pushed_size() as i64;
|
|
|
|
let stack = 0;
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
write_reloc(&mut self.ci.code, 3, -(pushed + stack), 8);
|
|
|
|
write_reloc(&mut self.ci.code, 3 + 8 + 3, stack, 8);
|
|
|
|
write_reloc(&mut self.ci.code, 3 + 8 + 3 + 8, pushed, 2);
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
self.ci.emit(instrs::ld(reg::RET_ADDR, reg::STACK_PTR, stack as _, pushed as _));
|
|
|
|
self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, (pushed + stack) as _));
|
|
|
|
self.ci.emit(instrs::jala(reg::ZERO, reg::RET_ADDR, 0));
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self.tys.funcs[id as usize].code.append(&mut self.ci.code);
|
|
|
|
self.tys.funcs[id as usize].relocs.append(&mut self.ci.relocs);
|
2024-09-04 16:46:32 -05:00
|
|
|
self.ci.nodes.clear();
|
2024-09-05 19:04:19 -05:00
|
|
|
self.ci.colors.clear();
|
|
|
|
self.ci.filled.clear();
|
2024-09-04 09:54:34 -05:00
|
|
|
self.pool.cis.push(std::mem::replace(&mut self.ci, prev_ci));
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
fn color_control(&mut self, mut ctrl: Nid) -> Option<Nid> {
|
|
|
|
for _ in 0..30 {
|
2024-09-04 16:46:32 -05:00
|
|
|
match self.ci.nodes[ctrl].kind.clone() {
|
|
|
|
Kind::Start => unreachable!(),
|
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::Return => {
|
2024-09-06 11:50:28 -05:00
|
|
|
let ret = self.ci.nodes[ctrl].inputs[2];
|
|
|
|
if ret != 0 {
|
2024-09-05 18:17:54 -05:00
|
|
|
_ = self.color_expr_consume(ret);
|
2024-09-06 15:00:23 -05:00
|
|
|
if node_color!(self, ret).call_count == self.ci.call_count {
|
|
|
|
node_loc!(self, ret) =
|
|
|
|
match self.tys.size_of(self.ci.ret.expect("TODO")) {
|
|
|
|
0 => Loc::default(),
|
|
|
|
1..=8 => Loc { reg: 1 },
|
|
|
|
s => todo!("{s}"),
|
|
|
|
};
|
|
|
|
}
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.regs.mark_leaked(1);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Kind::ConstInt { .. } => unreachable!(),
|
|
|
|
Kind::Tuple { .. } => {
|
|
|
|
ctrl = self.ci.nodes[ctrl].outputs[0];
|
|
|
|
}
|
|
|
|
Kind::BinOp { .. } => unreachable!(),
|
2024-09-07 21:20:10 -05:00
|
|
|
Kind::Call { .. } => {
|
|
|
|
for i in 1..self.ci.nodes[ctrl].inputs.len() {
|
|
|
|
let arg = self.ci.nodes[ctrl].inputs[i];
|
2024-09-05 18:17:54 -05:00
|
|
|
_ = self.color_expr_consume(arg);
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.set_next_color(arg);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
self.ci.call_count -= 1;
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.set_next_color(ctrl);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
ctrl = *self.ci.nodes[ctrl]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&o| self.ci.nodes.is_cfg(o))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
Kind::If => {
|
|
|
|
_ = self.color_expr_consume(self.ci.nodes[ctrl].inputs[1]);
|
|
|
|
|
|
|
|
let left_unreachable = self.color_control(self.ci.nodes[ctrl].outputs[0]);
|
|
|
|
let right_unreachable = self.color_control(self.ci.nodes[ctrl].outputs[1]);
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
let dest = match (left_unreachable, right_unreachable) {
|
|
|
|
(None, None) => return None,
|
|
|
|
(None, Some(n)) | (Some(n), None) => n,
|
|
|
|
(Some(l), Some(r)) if l == r => l,
|
|
|
|
(Some(left), Some(right)) => {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
if self.ci.nodes[dest].kind == Kind::Loop {
|
|
|
|
return Some(dest);
|
|
|
|
}
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
debug_assert!(left_unreachable.is_none() || right_unreachable.is_none());
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
debug_assert_eq!(self.ci.nodes[dest].kind, Kind::Region);
|
|
|
|
|
2024-09-05 19:04:19 -05:00
|
|
|
for i in 0..self.ci.nodes[dest].outputs.len() {
|
|
|
|
let o = self.ci.nodes[dest].outputs[i];
|
|
|
|
if self.ci.nodes[o].kind == Kind::Phi {
|
|
|
|
self.color_phy(o);
|
|
|
|
self.ci.nodes[o].depth = self.ci.loop_depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
ctrl = *self.ci.nodes[dest]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&o| self.ci.nodes[o].kind != Kind::Phi)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
Kind::Region => return Some(ctrl),
|
|
|
|
Kind::Phi => todo!(),
|
|
|
|
Kind::Loop => {
|
|
|
|
if self.ci.nodes[ctrl].lock_rc != 0 {
|
|
|
|
return Some(ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.ci.nodes[ctrl].outputs.len() {
|
|
|
|
let maybe_phy = self.ci.nodes[ctrl].outputs[i];
|
2024-09-07 21:20:10 -05:00
|
|
|
let Node { kind: Kind::Phi, ref inputs, .. } = self.ci.nodes[maybe_phy]
|
2024-09-05 18:17:54 -05:00
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
_ = self.color_expr_consume(inputs[1]);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes[maybe_phy].depth = self.ci.loop_depth;
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.set_next_color(maybe_phy);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.nodes[ctrl].lock_rc = self.ci.code.len() as _;
|
|
|
|
self.ci.loop_depth += 1;
|
|
|
|
|
|
|
|
self.color_control(
|
|
|
|
*self.ci.nodes[ctrl]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&o| self.ci.nodes[o].kind != Kind::Phi)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
for i in 0..self.ci.nodes[ctrl].outputs.len() {
|
|
|
|
self.color_phy(self.ci.nodes[ctrl].outputs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.loop_depth -= 1;
|
|
|
|
self.ci.nodes[ctrl].lock_rc = 0;
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn color_phy(&mut self, maybe_phy: Nid) {
|
2024-09-07 21:20:10 -05:00
|
|
|
let Node { kind: Kind::Phi, ref inputs, .. } = self.ci.nodes[maybe_phy] else {
|
2024-09-05 18:17:54 -05:00
|
|
|
return;
|
|
|
|
};
|
2024-09-07 21:20:10 -05:00
|
|
|
let &[region, left, right] = inputs.as_slice() else { unreachable!() };
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
let lcolor = self.color_expr_consume(left);
|
|
|
|
let rcolor = self.color_expr_consume(right);
|
|
|
|
|
|
|
|
if self.ci.nodes[maybe_phy].color != 0 {
|
2024-09-07 20:12:57 -05:00
|
|
|
// loop phy
|
|
|
|
if let Some(c) = rcolor
|
2024-09-07 21:20:10 -05:00
|
|
|
&& !self.ci.nodes.climb_expr(right, |i, n| {
|
2024-09-07 20:12:57 -05:00
|
|
|
matches!(n.kind, Kind::Phi) && n.inputs[0] == region && i != maybe_phy
|
|
|
|
})
|
|
|
|
{
|
|
|
|
self.ci.recolor(right, c, self.ci.nodes[maybe_phy].color);
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
} else {
|
2024-09-06 09:11:57 -05:00
|
|
|
let color = match (lcolor, rcolor) {
|
2024-09-05 18:17:54 -05:00
|
|
|
(None, None) => self.ci.next_color(),
|
|
|
|
(None, Some(c)) | (Some(c), None) => c,
|
|
|
|
(Some(lc), Some(rc)) => {
|
|
|
|
self.ci.recolor(right, rc, lc);
|
|
|
|
lc
|
|
|
|
}
|
|
|
|
};
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.set_color(maybe_phy, color);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use = "dont forget to drop the location"]
|
|
|
|
fn color_expr_consume(&mut self, expr: Nid) -> Option<u32> {
|
|
|
|
if self.ci.nodes[expr].lock_rc == 0 && self.ci.nodes[expr].kind != Kind::Phi {
|
|
|
|
self.ci.nodes[expr].depth = self.ci.loop_depth;
|
|
|
|
self.color_expr(expr);
|
|
|
|
}
|
|
|
|
self.use_colored_expr(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn color_expr(&mut self, expr: Nid) {
|
|
|
|
match self.ci.nodes[expr].kind {
|
|
|
|
Kind::Start => unreachable!(),
|
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::Return => unreachable!(),
|
2024-09-06 09:11:57 -05:00
|
|
|
Kind::ConstInt { .. } => self.ci.set_next_color(expr),
|
2024-09-05 18:17:54 -05:00
|
|
|
Kind::Tuple { index } => {
|
|
|
|
debug_assert!(index != 0);
|
|
|
|
}
|
2024-09-06 09:11:57 -05:00
|
|
|
Kind::BinOp { .. } => {
|
2024-09-07 21:20:10 -05:00
|
|
|
let &[left, right] = self.ci.nodes[expr].inputs.as_slice() else { unreachable!() };
|
2024-09-05 18:17:54 -05:00
|
|
|
let lcolor = self.color_expr_consume(left);
|
|
|
|
let rcolor = self.color_expr_consume(right);
|
2024-09-06 09:11:57 -05:00
|
|
|
let color = lcolor.or(rcolor).unwrap_or_else(|| self.ci.next_color());
|
|
|
|
self.ci.set_color(expr, color);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
Kind::Call { .. } => {}
|
|
|
|
Kind::If => todo!(),
|
|
|
|
Kind::Region => todo!(),
|
|
|
|
Kind::Phi => {}
|
|
|
|
Kind::Loop => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
fn use_colored_expr(&mut self, expr: Nid) -> Option<u32> {
|
|
|
|
self.ci.nodes[expr].lock_rc += 1;
|
|
|
|
debug_assert_ne!(self.ci.nodes[expr].color, 0, "{:?}", self.ci.nodes[expr].kind);
|
|
|
|
(self.ci.nodes[expr].lock_rc as usize >= self.ci.nodes[expr].outputs.len()
|
|
|
|
&& self.ci.nodes[expr].depth == self.ci.loop_depth)
|
|
|
|
.then_some(self.ci.nodes[expr].color)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_control(&mut self, mut ctrl: Nid) -> Option<Nid> {
|
|
|
|
for _ in 0..30 {
|
|
|
|
match self.ci.nodes[ctrl].kind.clone() {
|
|
|
|
Kind::Start => unreachable!(),
|
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::Return => {
|
2024-09-06 11:50:28 -05:00
|
|
|
let ret = self.ci.nodes[ctrl].inputs[2];
|
|
|
|
if ret != 0 {
|
2024-09-06 15:00:23 -05:00
|
|
|
// NOTE: this is safer less efficient way, maybe it will be needed
|
2024-09-07 20:12:57 -05:00
|
|
|
// self.emit_expr_consume(ret);
|
|
|
|
// if node_color!(self, ret).call_count != self.ci.call_count {
|
|
|
|
// let src = node_loc!(self, ret);
|
|
|
|
// let loc = match self.tys.size_of(self.ci.ret.expect("TODO")) {
|
|
|
|
// 0 => Loc::default(),
|
|
|
|
// 1..=8 => Loc { reg: 1 },
|
|
|
|
// s => todo!("{s}"),
|
|
|
|
// };
|
|
|
|
// if src != loc {
|
|
|
|
// let inst = instrs::cp(loc.reg, src.reg);
|
|
|
|
// self.ci.emit(inst);
|
|
|
|
// }
|
|
|
|
// }
|
2024-09-06 15:00:23 -05:00
|
|
|
|
|
|
|
node_loc!(self, ret) = match self.tys.size_of(self.ci.ret.expect("TODO")) {
|
|
|
|
0 => Loc::default(),
|
|
|
|
1..=8 => Loc { reg: 1 },
|
|
|
|
s => todo!("{s}"),
|
|
|
|
};
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_expr_consume(ret);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
self.ci.ret_relocs.push(Reloc::new(self.ci.code.len(), 1, 4));
|
|
|
|
self.ci.emit(instrs::jmp(0));
|
2024-09-05 18:17:54 -05:00
|
|
|
return None;
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
Kind::ConstInt { .. } => unreachable!(),
|
2024-09-05 04:16:11 -05:00
|
|
|
Kind::Tuple { .. } => {
|
2024-09-04 16:46:32 -05:00
|
|
|
ctrl = self.ci.nodes[ctrl].outputs[0];
|
|
|
|
}
|
|
|
|
Kind::BinOp { .. } => unreachable!(),
|
2024-09-07 21:20:10 -05:00
|
|
|
Kind::Call { func } => {
|
2024-09-04 16:46:32 -05:00
|
|
|
let ret = self.tof(ctrl);
|
|
|
|
|
|
|
|
let mut parama = self.tys.parama(ret);
|
2024-09-07 21:20:10 -05:00
|
|
|
for i in 1..self.ci.nodes[ctrl].inputs.len() {
|
|
|
|
let arg = self.ci.nodes[ctrl].inputs[i];
|
2024-09-05 18:17:54 -05:00
|
|
|
node_loc!(self, arg) = match self.tys.size_of(self.tof(arg)) {
|
2024-09-04 16:46:32 -05:00
|
|
|
0 => continue,
|
2024-09-05 18:17:54 -05:00
|
|
|
1..=8 => Loc { reg: parama.next() },
|
2024-09-04 16:46:32 -05:00
|
|
|
s => todo!("{s}"),
|
|
|
|
};
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.regs.mark_leaked(node_loc!(self, arg).reg);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_expr_consume(arg);
|
2024-09-06 15:00:23 -05:00
|
|
|
self.ci.nodes[arg].depth = 0;
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let reloc = Reloc::new(self.ci.code.len(), 3, 4);
|
|
|
|
self.ci
|
|
|
|
.relocs
|
|
|
|
.push(TypedReloc { target: ty::Kind::Func(func).compress(), reloc });
|
|
|
|
self.ci.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0));
|
|
|
|
self.make_func_reachable(func);
|
|
|
|
|
2024-09-05 04:16:11 -05:00
|
|
|
self.ci.call_count -= 1;
|
|
|
|
|
|
|
|
'b: {
|
|
|
|
let ret_loc = match self.tys.size_of(ret) {
|
|
|
|
0 => break 'b,
|
2024-09-05 18:17:54 -05:00
|
|
|
1..=8 => Loc { reg: 1 },
|
2024-09-05 04:16:11 -05:00
|
|
|
s => todo!("{s}"),
|
|
|
|
};
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-05 04:16:11 -05:00
|
|
|
if self.ci.nodes[ctrl].outputs.len() == 1 {
|
|
|
|
break 'b;
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
if self.ci.call_count == 0 {
|
|
|
|
node_loc!(self, ctrl) = ret_loc;
|
2024-09-05 04:16:11 -05:00
|
|
|
} else {
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_pass_low(ret_loc, ctrl);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
|
|
|
|
ctrl = *self.ci.nodes[ctrl]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&o| self.ci.nodes.is_cfg(o))
|
|
|
|
.unwrap();
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
Kind::If => {
|
|
|
|
let cond = self.ci.nodes[ctrl].inputs[1];
|
|
|
|
|
|
|
|
let jump_offset: i64;
|
2024-09-06 15:00:23 -05:00
|
|
|
let mut swapped = false;
|
|
|
|
'resolve_cond: {
|
|
|
|
'optimize_cond: {
|
|
|
|
let Kind::BinOp { op } = self.ci.nodes[cond].kind else {
|
|
|
|
break 'optimize_cond;
|
|
|
|
};
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-07 21:20:10 -05:00
|
|
|
let &[left, right] = self.ci.nodes[cond].inputs.as_slice() else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2024-09-06 15:00:23 -05:00
|
|
|
swapped = matches!(op, TokenKind::Lt | TokenKind::Gt);
|
|
|
|
let signed = self.ci.nodes[left].ty.is_signed();
|
2024-09-05 18:17:54 -05:00
|
|
|
let op = match op {
|
2024-09-06 15:00:23 -05:00
|
|
|
TokenKind::Le if signed => instrs::jgts,
|
2024-09-05 18:17:54 -05:00
|
|
|
TokenKind::Le => instrs::jgtu,
|
2024-09-06 15:00:23 -05:00
|
|
|
TokenKind::Lt if signed => instrs::jlts,
|
|
|
|
TokenKind::Lt => instrs::jltu,
|
2024-09-05 18:17:54 -05:00
|
|
|
TokenKind::Eq => instrs::jne,
|
2024-09-06 15:00:23 -05:00
|
|
|
TokenKind::Ne => instrs::jeq,
|
|
|
|
_ => break 'optimize_cond,
|
2024-09-05 04:16:11 -05:00
|
|
|
};
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
self.emit_expr_consume(left);
|
|
|
|
self.emit_expr_consume(right);
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
jump_offset = self.ci.code.len() as _;
|
2024-09-05 04:16:11 -05:00
|
|
|
self.ci.emit(op(
|
2024-09-05 18:17:54 -05:00
|
|
|
node_loc!(self, left).reg,
|
|
|
|
node_loc!(self, right).reg,
|
2024-09-05 04:16:11 -05:00
|
|
|
0,
|
|
|
|
));
|
2024-09-06 15:00:23 -05:00
|
|
|
|
|
|
|
break 'resolve_cond;
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-06 15:00:23 -05:00
|
|
|
|
|
|
|
self.emit_expr_consume(cond);
|
|
|
|
jump_offset = self.ci.code.len() as _;
|
|
|
|
self.ci.emit(instrs::jeq(node_loc!(self, cond).reg, reg::ZERO, 0));
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
let [loff, roff] = [swapped as usize, !swapped as usize];
|
|
|
|
|
2024-09-05 19:04:19 -05:00
|
|
|
let filled_base = self.ci.filled.len();
|
2024-09-06 15:00:23 -05:00
|
|
|
let left_unreachable = self.emit_control(self.ci.nodes[ctrl].outputs[loff]);
|
2024-09-05 19:04:19 -05:00
|
|
|
for fld in self.ci.filled.drain(filled_base..) {
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.nodes[fld].depth = 0;
|
2024-09-05 19:04:19 -05:00
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
let mut skip_then_offset = self.ci.code.len() as i64;
|
|
|
|
if let Some(region) = left_unreachable {
|
|
|
|
for i in 0..self.ci.nodes[region].outputs.len() {
|
|
|
|
let o = self.ci.nodes[region].outputs[i];
|
|
|
|
if self.ci.nodes[o].kind != Kind::Phi {
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-06 15:00:23 -05:00
|
|
|
let out = self.ci.nodes[o].inputs[1 + loff];
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_expr_consume(out);
|
|
|
|
self.emit_pass(out, o);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
skip_then_offset = self.ci.code.len() as i64;
|
2024-09-05 04:16:11 -05:00
|
|
|
self.ci.emit(instrs::jmp(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
let right_base = self.ci.code.len();
|
2024-09-05 19:04:19 -05:00
|
|
|
let filled_base = self.ci.filled.len();
|
2024-09-06 15:00:23 -05:00
|
|
|
let right_unreachable = self.emit_control(self.ci.nodes[ctrl].outputs[roff]);
|
2024-09-05 19:04:19 -05:00
|
|
|
|
|
|
|
for fld in self.ci.filled.drain(filled_base..) {
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.nodes[fld].depth = 0;
|
2024-09-05 19:04:19 -05:00
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
if let Some(region) = left_unreachable {
|
|
|
|
for i in 0..self.ci.nodes[region].outputs.len() {
|
|
|
|
let o = self.ci.nodes[region].outputs[i];
|
|
|
|
if self.ci.nodes[o].kind != Kind::Phi {
|
|
|
|
continue;
|
|
|
|
}
|
2024-09-06 15:00:23 -05:00
|
|
|
let out = self.ci.nodes[o].inputs[1 + roff];
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_expr_consume(out);
|
|
|
|
self.emit_pass(out, o);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let right_end = self.ci.code.len();
|
|
|
|
if right_base == right_end {
|
|
|
|
self.ci.code.truncate(skip_then_offset as _);
|
|
|
|
} else {
|
|
|
|
write_reloc(
|
|
|
|
&mut self.ci.code,
|
|
|
|
skip_then_offset as usize + 1,
|
|
|
|
right_end as i64 - skip_then_offset,
|
|
|
|
4,
|
|
|
|
);
|
|
|
|
skip_then_offset += instrs::jmp(69).0 as i64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write_reloc(
|
|
|
|
&mut self.ci.code,
|
|
|
|
jump_offset as usize + 3,
|
|
|
|
skip_then_offset - jump_offset,
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
let dest = left_unreachable.or(right_unreachable)?;
|
|
|
|
|
|
|
|
if self.ci.nodes[dest].kind == Kind::Loop {
|
|
|
|
return Some(dest);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
debug_assert_eq!(self.ci.nodes[dest].kind, Kind::Region);
|
|
|
|
|
|
|
|
ctrl = *self.ci.nodes[dest]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&o| self.ci.nodes[o].kind != Kind::Phi)
|
|
|
|
.unwrap();
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
Kind::Region => return Some(ctrl),
|
2024-09-05 04:16:11 -05:00
|
|
|
Kind::Phi => todo!(),
|
2024-09-05 18:17:54 -05:00
|
|
|
Kind::Loop => {
|
|
|
|
if self.ci.nodes[ctrl].lock_rc != 0 {
|
|
|
|
return Some(ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.ci.nodes[ctrl].outputs.len() {
|
|
|
|
let o = self.ci.nodes[ctrl].outputs[i];
|
|
|
|
if self.ci.nodes[o].kind != Kind::Phi {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let out = self.ci.nodes[o].inputs[1];
|
|
|
|
self.emit_expr_consume(out);
|
|
|
|
self.emit_pass(out, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.nodes[ctrl].lock_rc = self.ci.code.len() as _;
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.loop_depth += 1;
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
let end = self.emit_control(
|
|
|
|
*self.ci.nodes[ctrl]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&o| self.ci.nodes[o].kind != Kind::Phi)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
debug_assert_eq!(end, Some(ctrl));
|
|
|
|
|
|
|
|
for i in 0..self.ci.nodes[ctrl].outputs.len() {
|
|
|
|
let o = self.ci.nodes[ctrl].outputs[i];
|
|
|
|
if self.ci.nodes[o].kind != Kind::Phi {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let out = self.ci.nodes[o].inputs[2];
|
|
|
|
// TODO: this can be improved if we juggle ownership of the Phi inputs
|
|
|
|
self.emit_expr(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..self.ci.nodes[ctrl].outputs.len() {
|
|
|
|
let o = self.ci.nodes[ctrl].outputs[i];
|
|
|
|
if self.ci.nodes[o].kind != Kind::Phi {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let out = self.ci.nodes[o].inputs[2];
|
|
|
|
self.use_expr(out);
|
|
|
|
self.emit_pass(out, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.emit(instrs::jmp(
|
|
|
|
self.ci.nodes[ctrl].lock_rc as i32 - self.ci.code.len() as i32,
|
|
|
|
));
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.loop_depth -= 1;
|
|
|
|
for free in self.ci.delayed_frees.extract_if(|&mut color| {
|
|
|
|
self.ci.colors[color as usize].depth == self.ci.loop_depth
|
|
|
|
}) {
|
|
|
|
let color = &self.ci.colors[free as usize];
|
|
|
|
debug_assert_ne!(color.loc, Loc::default());
|
|
|
|
self.ci.regs.free(color.loc.reg);
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
return None;
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
unreachable!()
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
fn emit_expr_consume(&mut self, expr: Nid) {
|
|
|
|
self.emit_expr(expr);
|
2024-09-06 09:11:57 -05:00
|
|
|
self.use_expr(expr);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_expr(&mut self, expr: Nid) {
|
2024-09-06 09:11:57 -05:00
|
|
|
if self.ci.nodes[expr].depth == u32::MAX {
|
2024-09-05 18:17:54 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-09-06 09:11:57 -05:00
|
|
|
self.ci.nodes[expr].depth = u32::MAX;
|
2024-09-05 19:04:19 -05:00
|
|
|
self.ci.filled.push(expr);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
match self.ci.nodes[expr].kind {
|
|
|
|
Kind::Start => unreachable!(),
|
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::Return => unreachable!(),
|
|
|
|
Kind::ConstInt { value } => {
|
2024-09-05 18:17:54 -05:00
|
|
|
_ = self.lazy_init(expr);
|
|
|
|
let instr = instrs::li64(node_loc!(self, expr).reg, value as _);
|
|
|
|
self.ci.emit(instr);
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
Kind::Tuple { index } => {
|
|
|
|
debug_assert!(index != 0);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
_ = self.lazy_init(expr);
|
|
|
|
|
|
|
|
let mut params = self.tys.parama(self.ci.ret.unwrap());
|
|
|
|
for (i, var) in self.ci.vars.iter().enumerate() {
|
|
|
|
if var.id == u32::MAX {
|
|
|
|
match self.tys.size_of(ty::Id::from_bt(var.value)) {
|
|
|
|
0 => {}
|
|
|
|
1..=8 => _ = params.next(),
|
|
|
|
s => todo!("{s}"),
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.tys.size_of(self.ci.nodes[var.value].ty) {
|
|
|
|
0 => {}
|
|
|
|
1..=8 => {
|
|
|
|
let reg = params.next();
|
|
|
|
if i == index as usize - 1 {
|
|
|
|
emit(&mut self.ci.code, instrs::cp(node_loc!(self, expr).reg, reg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s => todo!("{s}"),
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
Kind::BinOp { op } => {
|
2024-09-05 18:17:54 -05:00
|
|
|
_ = self.lazy_init(expr);
|
2024-09-04 16:46:32 -05:00
|
|
|
let ty = self.tof(expr);
|
2024-09-07 21:20:10 -05:00
|
|
|
let &[left, right] = self.ci.nodes[expr].inputs.as_slice() else { unreachable!() };
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_expr_consume(left);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
|
|
|
if let Kind::ConstInt { value } = self.ci.nodes[right].kind
|
2024-09-05 19:04:19 -05:00
|
|
|
&& (node_loc!(self, right) == Loc::default()
|
2024-09-06 09:11:57 -05:00
|
|
|
|| self.ci.nodes[right].depth != u32::MAX)
|
2024-09-05 04:16:11 -05:00
|
|
|
&& let Some(op) = Self::imm_math_op(op, ty.is_signed(), self.tys.size_of(ty))
|
|
|
|
{
|
2024-09-05 18:17:54 -05:00
|
|
|
let instr =
|
|
|
|
op(node_loc!(self, expr).reg, node_loc!(self, left).reg, value as _);
|
|
|
|
self.ci.emit(instr);
|
2024-09-05 04:16:11 -05:00
|
|
|
} else {
|
2024-09-05 18:17:54 -05:00
|
|
|
self.emit_expr_consume(right);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
|
|
|
let op = Self::math_op(op, ty.is_signed(), self.tys.size_of(ty))
|
|
|
|
.expect("TODO: what now?");
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
let instr = op(
|
|
|
|
node_loc!(self, expr).reg,
|
|
|
|
node_loc!(self, left).reg,
|
|
|
|
node_loc!(self, right).reg,
|
|
|
|
);
|
|
|
|
self.ci.emit(instr);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
Kind::Call { .. } => {}
|
2024-09-05 04:16:11 -05:00
|
|
|
Kind::If => todo!(),
|
|
|
|
Kind::Region => todo!(),
|
2024-09-05 18:17:54 -05:00
|
|
|
Kind::Phi => {}
|
|
|
|
Kind::Loop => todo!(),
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
fn use_expr(&mut self, expr: Nid) {
|
|
|
|
let node = &mut self.ci.nodes[expr];
|
|
|
|
node.lock_rc = node.lock_rc.saturating_sub(1);
|
|
|
|
if node.lock_rc != 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let color = &mut self.ci.colors[node.color as usize - 1];
|
|
|
|
color.rc -= 1;
|
|
|
|
if color.rc == 0 {
|
|
|
|
if color.depth != self.ci.loop_depth {
|
|
|
|
self.ci.delayed_frees.push(node.color);
|
|
|
|
} else {
|
|
|
|
debug_assert_ne!(color.loc, Loc::default(), "{:?}", node);
|
|
|
|
self.ci.regs.free(color.loc.reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-05 04:16:11 -05:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
fn imm_math_op(
|
|
|
|
op: TokenKind,
|
|
|
|
signed: bool,
|
|
|
|
size: u32,
|
|
|
|
) -> Option<fn(u8, u8, u64) -> (usize, [u8; instrs::MAX_SIZE])> {
|
|
|
|
use {instrs::*, TokenKind as T};
|
|
|
|
|
|
|
|
macro_rules! def_op {
|
|
|
|
($name:ident |$a:ident, $b:ident, $c:ident| $($tt:tt)*) => {
|
|
|
|
macro_rules! $name {
|
|
|
|
($$($$op:ident),*) => {
|
|
|
|
[$$(
|
|
|
|
|$a, $b, $c: u64| $$op($($tt)*),
|
|
|
|
)*]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
def_op!(basic_op | a, b, c | a, b, c as _);
|
|
|
|
def_op!(sub_op | a, b, c | a, b, c.wrapping_neg() as _);
|
|
|
|
|
|
|
|
let ops = match op {
|
|
|
|
T::Add => basic_op!(addi8, addi16, addi32, addi64),
|
|
|
|
T::Sub => sub_op!(addi8, addi16, addi32, addi64),
|
|
|
|
T::Mul => basic_op!(muli8, muli16, muli32, muli64),
|
|
|
|
T::Band => return Some(andi),
|
|
|
|
T::Bor => return Some(ori),
|
|
|
|
T::Xor => return Some(xori),
|
|
|
|
T::Shr if signed => basic_op!(srui8, srui16, srui32, srui64),
|
|
|
|
T::Shr => basic_op!(srui8, srui16, srui32, srui64),
|
|
|
|
T::Shl => basic_op!(slui8, slui16, slui32, slui64),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(ops[size.ilog2() as usize])
|
|
|
|
}
|
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
fn math_op(
|
|
|
|
op: TokenKind,
|
|
|
|
signed: bool,
|
|
|
|
size: u32,
|
|
|
|
) -> Option<fn(u8, u8, u8) -> (usize, [u8; instrs::MAX_SIZE])> {
|
|
|
|
use {instrs::*, TokenKind as T};
|
|
|
|
|
|
|
|
macro_rules! div { ($($op:ident),*) => {[$(|a, b, c| $op(a, reg::ZERO, b, c)),*]}; }
|
|
|
|
macro_rules! rem { ($($op:ident),*) => {[$(|a, b, c| $op(reg::ZERO, a, b, c)),*]}; }
|
|
|
|
|
|
|
|
let ops = match op {
|
|
|
|
T::Add => [add8, add16, add32, add64],
|
|
|
|
T::Sub => [sub8, sub16, sub32, sub64],
|
|
|
|
T::Mul => [mul8, mul16, mul32, mul64],
|
|
|
|
T::Div if signed => div!(dirs8, dirs16, dirs32, dirs64),
|
|
|
|
T::Div => div!(diru8, diru16, diru32, diru64),
|
|
|
|
T::Mod if signed => rem!(dirs8, dirs16, dirs32, dirs64),
|
|
|
|
T::Mod => rem!(diru8, diru16, diru32, diru64),
|
|
|
|
T::Band => return Some(and),
|
|
|
|
T::Bor => return Some(or),
|
|
|
|
T::Xor => return Some(xor),
|
|
|
|
T::Shl => [slu8, slu16, slu32, slu64],
|
|
|
|
T::Shr if signed => [srs8, srs16, srs32, srs64],
|
|
|
|
T::Shr => [sru8, sru16, sru32, sru64],
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(ops[size.ilog2() as usize])
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: sometimes its better to do this in bulk
|
|
|
|
fn ty(&mut self, expr: &Expr) -> ty::Id {
|
2024-09-03 10:51:28 -05:00
|
|
|
match *expr {
|
|
|
|
Expr::Ident { id, .. } if ident::is_null(id) => id.into(),
|
2024-09-06 11:50:28 -05:00
|
|
|
ref e => {
|
|
|
|
self.report_unhandled_ast(e, "type");
|
|
|
|
ty::NEVER.into()
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find_or_declare(
|
|
|
|
&mut self,
|
|
|
|
pos: Pos,
|
|
|
|
file: FileId,
|
2024-09-02 17:27:50 -05:00
|
|
|
name: Option<Ident>,
|
2024-09-02 17:07:20 -05:00
|
|
|
lit_name: &str,
|
|
|
|
) -> ty::Kind {
|
2024-09-04 16:46:32 -05:00
|
|
|
log::trc!("find_or_declare: {lit_name} {file}");
|
2024-09-03 10:51:28 -05:00
|
|
|
|
|
|
|
let f = self.files[file as usize].clone();
|
|
|
|
let Some((expr, ident)) = f.find_decl(name.ok_or(lit_name)) else {
|
|
|
|
match name.ok_or(lit_name) {
|
|
|
|
Ok(name) => {
|
|
|
|
let name = self.cfile().ident_str(name);
|
|
|
|
self.report(pos, format_args!("undefined indentifier: {name}"))
|
|
|
|
}
|
|
|
|
Err("main") => self.report(
|
|
|
|
pos,
|
|
|
|
format_args!(
|
|
|
|
"missing main function in '{}', compiler can't \
|
|
|
|
emmit libraries since such concept is not defined",
|
|
|
|
f.path
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Err(name) => self.report(pos, format_args!("undefined indentifier: {name}")),
|
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
return ty::Kind::Builtin(ty::NEVER);
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(existing) = self.tys.syms.get(&SymKey { file, ident }) {
|
|
|
|
if let ty::Kind::Func(id) = existing.expand()
|
|
|
|
&& let func = &mut self.tys.funcs[id as usize]
|
|
|
|
&& let Err(idx) = task::unpack(func.offset)
|
2024-09-05 04:16:11 -05:00
|
|
|
&& idx < self.tasks.len()
|
2024-09-03 10:51:28 -05:00
|
|
|
{
|
|
|
|
func.offset = task::id(self.tasks.len());
|
|
|
|
let task = self.tasks[idx].take();
|
|
|
|
self.tasks.push(task);
|
|
|
|
}
|
|
|
|
return existing.expand();
|
|
|
|
}
|
|
|
|
|
|
|
|
let prev_file = std::mem::replace(&mut self.ci.file, file);
|
|
|
|
let sym = match expr {
|
|
|
|
Expr::BinOp {
|
|
|
|
left: &Expr::Ident { .. },
|
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: &Expr::Closure { pos, args, ret, .. },
|
|
|
|
} => {
|
|
|
|
let func = Func {
|
|
|
|
file,
|
|
|
|
sig: '_b: {
|
|
|
|
let arg_base = self.tys.args.len();
|
|
|
|
for arg in args {
|
|
|
|
let sym = parser::find_symbol(&f.symbols, arg.id);
|
|
|
|
assert!(sym.flags & idfl::COMPTIME == 0, "TODO");
|
|
|
|
let ty = self.ty(&arg.ty);
|
|
|
|
self.tys.args.push(ty);
|
|
|
|
}
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
let Some(args) = self.pack_args(arg_base) else {
|
|
|
|
self.fatal_report(
|
|
|
|
pos,
|
|
|
|
"you cant be serious, using more the 31 arguments in a function",
|
|
|
|
);
|
|
|
|
};
|
2024-09-03 10:51:28 -05:00
|
|
|
let ret = self.ty(ret);
|
|
|
|
|
|
|
|
Some(Sig { args, ret })
|
|
|
|
},
|
|
|
|
expr: {
|
|
|
|
let refr = ExprRef::new(expr);
|
|
|
|
debug_assert!(refr.get(&f).is_some());
|
|
|
|
refr
|
|
|
|
},
|
2024-09-04 09:54:34 -05:00
|
|
|
..Default::default()
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let id = self.tys.funcs.len() as _;
|
|
|
|
self.tys.funcs.push(func);
|
|
|
|
|
|
|
|
ty::Kind::Func(id)
|
|
|
|
}
|
|
|
|
Expr::BinOp {
|
|
|
|
left: &Expr::Ident { .. },
|
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: Expr::Struct { fields, .. },
|
|
|
|
} => ty::Kind::Struct(self.build_struct(fields)),
|
|
|
|
Expr::BinOp { .. } => {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
e => unimplemented!("{e:#?}"),
|
|
|
|
};
|
|
|
|
self.ci.file = prev_file;
|
|
|
|
self.tys.syms.insert(SymKey { ident, file }, sym.compress());
|
|
|
|
sym
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_display(&self, ty: ty::Id) -> ty::Display {
|
|
|
|
ty::Display::new(&self.tys, &self.files, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
#[track_caller]
|
2024-09-06 11:50:28 -05:00
|
|
|
fn assert_ty(
|
|
|
|
&self,
|
|
|
|
pos: Pos,
|
|
|
|
ty: ty::Id,
|
|
|
|
expected: ty::Id,
|
|
|
|
preserve_expected: bool,
|
|
|
|
hint: impl fmt::Display,
|
|
|
|
) -> ty::Id {
|
2024-09-03 10:51:28 -05:00
|
|
|
if let Some(res) = ty.try_upcast(expected)
|
|
|
|
&& (!preserve_expected || res == expected)
|
|
|
|
{
|
2024-09-02 17:07:20 -05:00
|
|
|
res
|
|
|
|
} else {
|
|
|
|
let ty = self.ty_display(ty);
|
|
|
|
let expected = self.ty_display(expected);
|
2024-09-06 11:50:28 -05:00
|
|
|
self.report(pos, format_args!("expected {hint} to be of type {expected}, got {ty}"));
|
|
|
|
ty::NEVER.into()
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_log(&self, pos: Pos, msg: impl std::fmt::Display) {
|
2024-09-06 11:50:28 -05:00
|
|
|
let mut buf = self.errors.borrow_mut();
|
2024-09-06 15:00:23 -05:00
|
|
|
self.report_log_to(pos, msg, &mut *buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_log_to(&self, pos: Pos, msg: impl std::fmt::Display, out: &mut impl std::fmt::Write) {
|
2024-09-05 19:42:07 -05:00
|
|
|
let str = &self.cfile().file;
|
2024-09-06 09:11:57 -05:00
|
|
|
let (line, mut col) = lexer::line_col(str.as_bytes(), pos);
|
2024-09-06 15:00:23 -05:00
|
|
|
_ = writeln!(out, "{}:{}:{}: {}", self.cfile().path, line, col, msg);
|
2024-09-05 19:42:07 -05:00
|
|
|
|
2024-09-06 09:11:57 -05:00
|
|
|
let line = &str[str[..pos as usize].rfind('\n').map_or(0, |i| i + 1)
|
|
|
|
..str[pos as usize..].find('\n').unwrap_or(str.len()) + pos as usize];
|
|
|
|
col += line.matches('\t').count() * 3;
|
|
|
|
|
2024-09-06 15:00:23 -05:00
|
|
|
_ = writeln!(out, "{}", line.replace("\t", " "));
|
|
|
|
_ = writeln!(out, "{}^", " ".repeat(col - 1));
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
2024-09-06 11:50:28 -05:00
|
|
|
fn assert_report(&self, cond: bool, pos: Pos, msg: impl std::fmt::Display) {
|
|
|
|
if !cond {
|
|
|
|
self.report(pos, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn report(&self, pos: Pos, msg: impl std::fmt::Display) {
|
2024-09-02 17:07:20 -05:00
|
|
|
self.report_log(pos, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
2024-09-06 11:50:28 -05:00
|
|
|
fn report_unhandled_ast(&self, ast: &Expr, hint: &str) {
|
2024-09-02 17:07:20 -05:00
|
|
|
self.report(
|
|
|
|
ast.pos(),
|
|
|
|
format_args!(
|
|
|
|
"compiler does not (yet) know how to handle ({hint}):\n\
|
|
|
|
{ast:}\n\
|
|
|
|
info for weak people:\n\
|
|
|
|
{ast:#?}"
|
|
|
|
),
|
2024-09-06 11:50:28 -05:00
|
|
|
);
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cfile(&self) -> &parser::Ast {
|
|
|
|
&self.files[self.ci.file as usize]
|
|
|
|
}
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
fn pack_args(&mut self, arg_base: usize) -> Option<ty::Tuple> {
|
2024-09-02 17:07:20 -05:00
|
|
|
let needle = &self.tys.args[arg_base..];
|
|
|
|
if needle.is_empty() {
|
2024-09-06 11:50:28 -05:00
|
|
|
return Some(ty::Tuple::empty());
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
let len = needle.len();
|
|
|
|
// FIXME: maybe later when this becomes a bottleneck we use more
|
|
|
|
// efficient search (SIMD?, indexing?)
|
|
|
|
let sp = self.tys.args.windows(needle.len()).position(|val| val == needle).unwrap();
|
|
|
|
self.tys.args.truncate((sp + needle.len()).max(arg_base));
|
|
|
|
ty::Tuple::new(sp, len)
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
fn emit_pass(&mut self, src: Nid, dst: Nid) {
|
|
|
|
if self.ci.nodes[src].color == self.ci.nodes[dst].color {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.emit_pass_low(node_loc!(self, src), dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_pass_low(&mut self, src: Loc, dst: Nid) {
|
|
|
|
let loc = &mut node_loc!(self, dst);
|
|
|
|
if src != *loc {
|
|
|
|
if *loc == Loc::default() {
|
|
|
|
let reg = self.ci.regs.allocate(0);
|
|
|
|
*loc = Loc { reg };
|
|
|
|
}
|
|
|
|
let inst = instrs::cp(loc.reg, src.reg);
|
|
|
|
self.ci.emit(inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
fn lazy_init(&mut self, expr: Nid) -> bool {
|
|
|
|
let loc = &mut node_loc!(self, expr);
|
|
|
|
if *loc == Loc::default() {
|
|
|
|
let reg = self.ci.regs.allocate(0);
|
|
|
|
*loc = Loc { reg };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
|
|
|
|
fn fatal_report(&self, pos: Pos, msg: impl Display) -> ! {
|
|
|
|
self.report(pos, msg);
|
|
|
|
eprintln!("{}", self.errors.borrow());
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct LoggedMem {
|
|
|
|
pub mem: hbvm::mem::HostMemory,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl hbvm::mem::Memory for LoggedMem {
|
|
|
|
unsafe fn load(
|
|
|
|
&mut self,
|
|
|
|
addr: hbvm::mem::Address,
|
|
|
|
target: *mut u8,
|
|
|
|
count: usize,
|
|
|
|
) -> Result<(), hbvm::mem::LoadError> {
|
|
|
|
log::trc!(
|
|
|
|
"load: {:x} {:?}",
|
|
|
|
addr.get(),
|
|
|
|
core::slice::from_raw_parts(addr.get() as *const u8, count)
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.map(|&b| format!("{b:02x}"))
|
|
|
|
.collect::<String>()
|
|
|
|
);
|
|
|
|
self.mem.load(addr, target, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn store(
|
|
|
|
&mut self,
|
|
|
|
addr: hbvm::mem::Address,
|
|
|
|
source: *const u8,
|
|
|
|
count: usize,
|
|
|
|
) -> Result<(), hbvm::mem::StoreError> {
|
|
|
|
log::trc!(
|
|
|
|
"store: {:x} {:?}",
|
|
|
|
addr.get(),
|
|
|
|
core::slice::from_raw_parts(source, count)
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.map(|&b| format!("{b:02x}"))
|
|
|
|
.collect::<String>()
|
|
|
|
);
|
|
|
|
self.mem.store(addr, source, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn prog_read<T: Copy>(&mut self, addr: hbvm::mem::Address) -> T {
|
|
|
|
log::trc!(
|
|
|
|
"read-typed: {:x} {} {:?}",
|
|
|
|
addr.get(),
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
if core::mem::size_of::<T>() == 1
|
|
|
|
&& let Some(nm) =
|
|
|
|
instrs::NAMES.get(std::ptr::read(addr.get() as *const u8) as usize)
|
|
|
|
{
|
|
|
|
nm.to_string()
|
|
|
|
} else {
|
|
|
|
core::slice::from_raw_parts(addr.get() as *const u8, core::mem::size_of::<T>())
|
|
|
|
.iter()
|
|
|
|
.map(|&b| format!("{:02x}", b))
|
|
|
|
.collect::<String>()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
self.mem.prog_read(addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use {
|
2024-09-04 16:46:32 -05:00
|
|
|
crate::{
|
|
|
|
parser::{self, FileId},
|
|
|
|
son::LoggedMem,
|
|
|
|
},
|
2024-09-02 17:07:20 -05:00
|
|
|
std::io,
|
|
|
|
};
|
|
|
|
|
|
|
|
const README: &str = include_str!("../README.md");
|
|
|
|
|
|
|
|
fn generate(ident: &'static str, input: &'static str, output: &mut String) {
|
2024-09-02 17:27:50 -05:00
|
|
|
fn find_block(mut input: &'static str, test_name: &'static str) -> &'static str {
|
|
|
|
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();
|
|
|
|
let mut last_start = 0;
|
|
|
|
let mut last_module_name = "test";
|
|
|
|
for (i, m) in input.match_indices("// in module: ") {
|
|
|
|
parser::test::format(ident, input[last_start..i].trim());
|
|
|
|
module_map.push((last_module_name, &input[last_start..i]));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
parser::test::format(ident, input[last_start..].trim());
|
|
|
|
module_map.push((last_module_name, input[last_start..].trim()));
|
|
|
|
|
|
|
|
let loader = |path: &str, _: &str| {
|
|
|
|
module_map
|
|
|
|
.iter()
|
|
|
|
.position(|&(name, _)| name == path)
|
|
|
|
.map(|i| i as FileId)
|
|
|
|
.ok_or(io::Error::from(io::ErrorKind::NotFound))
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut codegen = super::Codegen {
|
|
|
|
files: module_map
|
|
|
|
.iter()
|
|
|
|
.map(|&(path, content)| parser::Ast::new(path, content.to_owned(), &loader))
|
|
|
|
.collect(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
codegen.generate();
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
{
|
|
|
|
let errors = codegen.errors.borrow();
|
|
|
|
if !errors.is_empty() {
|
|
|
|
output.push_str(&errors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
let mut out = Vec::new();
|
|
|
|
codegen.assemble(&mut out);
|
|
|
|
|
|
|
|
let mut buf = Vec::<u8>::new();
|
|
|
|
let err = codegen.disasm(&out, &mut buf);
|
|
|
|
output.push_str(String::from_utf8(buf).unwrap().as_str());
|
|
|
|
if let Err(e) = err {
|
|
|
|
writeln!(output, "!!! asm is invalid: {e}").unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut stack = [0_u64; 128];
|
|
|
|
|
|
|
|
let mut vm = unsafe {
|
2024-09-05 18:17:54 -05:00
|
|
|
hbvm::Vm::<_, { 1024 * 10 }>::new(
|
2024-09-04 16:46:32 -05:00
|
|
|
LoggedMem::default(),
|
|
|
|
hbvm::mem::Address::new(out.as_ptr() as u64),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
vm.write_reg(super::reg::STACK_PTR, unsafe { stack.as_mut_ptr().add(stack.len()) } as u64);
|
|
|
|
|
2024-09-03 10:51:28 -05:00
|
|
|
use std::fmt::Write;
|
2024-09-04 16:46:32 -05:00
|
|
|
let stat = loop {
|
|
|
|
match vm.run() {
|
|
|
|
Ok(hbvm::VmRunOk::End) => break Ok(()),
|
|
|
|
Ok(hbvm::VmRunOk::Ecall) => match vm.read_reg(2).0 {
|
|
|
|
1 => writeln!(output, "ev: Ecall").unwrap(), // compatibility with a test
|
|
|
|
69 => {
|
|
|
|
let [size, align] = [vm.read_reg(3).0 as usize, vm.read_reg(4).0 as usize];
|
|
|
|
let layout = std::alloc::Layout::from_size_align(size, align).unwrap();
|
|
|
|
let ptr = unsafe { std::alloc::alloc(layout) };
|
|
|
|
vm.write_reg(1, ptr as u64);
|
|
|
|
}
|
|
|
|
96 => {
|
|
|
|
let [ptr, size, align] = [
|
|
|
|
vm.read_reg(3).0 as usize,
|
|
|
|
vm.read_reg(4).0 as usize,
|
|
|
|
vm.read_reg(5).0 as usize,
|
|
|
|
];
|
|
|
|
|
|
|
|
let layout = std::alloc::Layout::from_size_align(size, align).unwrap();
|
|
|
|
unsafe { std::alloc::dealloc(ptr as *mut u8, layout) };
|
|
|
|
}
|
|
|
|
3 => vm.write_reg(1, 42),
|
|
|
|
unknown => unreachable!("unknown ecall: {unknown:?}"),
|
|
|
|
},
|
2024-09-06 09:16:42 -05:00
|
|
|
Ok(hbvm::VmRunOk::Timer) => {
|
|
|
|
writeln!(output, "timed out").unwrap();
|
|
|
|
break Ok(());
|
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
Ok(ev) => writeln!(output, "ev: {:?}", ev).unwrap(),
|
|
|
|
Err(e) => break Err(e),
|
|
|
|
}
|
|
|
|
};
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-04 16:46:32 -05:00
|
|
|
writeln!(output, "code size: {}", out.len()).unwrap();
|
|
|
|
writeln!(output, "ret: {:?}", vm.read_reg(1).0).unwrap();
|
|
|
|
writeln!(output, "status: {:?}", stat).unwrap();
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
crate::run_tests! { generate:
|
|
|
|
arithmetic => README;
|
2024-09-04 16:46:32 -05:00
|
|
|
variables => README;
|
|
|
|
functions => README;
|
2024-09-05 04:16:11 -05:00
|
|
|
comments => README;
|
|
|
|
if_statements => README;
|
2024-09-05 18:17:54 -05:00
|
|
|
loops => README;
|
2024-09-06 09:11:57 -05:00
|
|
|
fb_driver => README;
|
2024-09-02 17:27:50 -05:00
|
|
|
//pointers => README;
|
|
|
|
//structs => README;
|
|
|
|
//different_types => README;
|
|
|
|
//struct_operators => README;
|
|
|
|
//directives => README;
|
|
|
|
//global_variables => README;
|
|
|
|
//generic_types => README;
|
|
|
|
//generic_functions => README;
|
|
|
|
//c_strings => README;
|
|
|
|
//struct_patterns => README;
|
|
|
|
//arrays => README;
|
|
|
|
//struct_return_from_module_function => README;
|
|
|
|
////comptime_pointers => README;
|
|
|
|
//sort_something_viredly => README;
|
2024-09-06 09:16:42 -05:00
|
|
|
hex_octal_binary_literals => README;
|
2024-09-02 17:27:50 -05:00
|
|
|
//comptime_min_reg_leak => README;
|
|
|
|
////structs_in_registers => README;
|
|
|
|
//comptime_function_from_another_file => README;
|
|
|
|
//inline => README;
|
|
|
|
//inline_test => README;
|
2024-09-05 18:17:54 -05:00
|
|
|
const_folding_with_arg => README;
|
|
|
|
// FIXME: contains redundant copies
|
|
|
|
branch_assignments => README;
|
2024-09-06 11:50:28 -05:00
|
|
|
exhaustive_loop_testing => README;
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|