2024-09-02 17:07:20 -05:00
|
|
|
use {
|
|
|
|
crate::{
|
2024-10-01 14:33:30 -05:00
|
|
|
ctx_map::CtxEntry,
|
2024-09-28 14:56:39 -05:00
|
|
|
ident::Ident,
|
2024-09-22 11:17:30 -05:00
|
|
|
instrs,
|
2024-09-03 10:51:28 -05:00
|
|
|
lexer::{self, TokenKind},
|
2024-09-04 09:54:34 -05:00
|
|
|
parser::{
|
|
|
|
self,
|
2024-09-04 10:56:59 -05:00
|
|
|
idfl::{self},
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr, ExprRef, FileId, Pos,
|
2024-09-04 09:54:34 -05:00
|
|
|
},
|
2024-10-10 01:35:17 -05:00
|
|
|
reg, task,
|
2024-09-19 06:40:03 -05:00
|
|
|
ty::{self},
|
2024-09-28 14:56:39 -05:00
|
|
|
vc::{BitSet, Vc},
|
2024-10-17 15:29:09 -05:00
|
|
|
Func, HashMap, Offset, OffsetIter, Reloc, Sig, SymKey, TypedReloc, Types,
|
2024-09-02 17:07:20 -05:00
|
|
|
},
|
2024-10-18 06:11:11 -05:00
|
|
|
alloc::{string::String, vec::Vec},
|
2024-09-30 12:09:17 -05:00
|
|
|
core::{
|
2024-09-20 12:01:44 -05:00
|
|
|
assert_matches::debug_assert_matches,
|
2024-09-06 11:50:28 -05:00
|
|
|
cell::RefCell,
|
2024-09-30 12:09:17 -05:00
|
|
|
fmt::{self, Debug, Display, Write},
|
2024-10-04 14:44:29 -05:00
|
|
|
format_args as fa, mem,
|
2024-10-17 12:32:10 -05:00
|
|
|
ops::{self},
|
2024-09-02 17:07:20 -05:00
|
|
|
},
|
2024-09-30 12:09:17 -05:00
|
|
|
hashbrown::hash_map,
|
|
|
|
regalloc2::VReg,
|
2024-09-02 17:07:20 -05:00
|
|
|
};
|
|
|
|
|
2024-09-16 08:49:27 -05:00
|
|
|
const VOID: Nid = 0;
|
|
|
|
const NEVER: Nid = 1;
|
2024-09-27 09:53:28 -05:00
|
|
|
const ENTRY: Nid = 2;
|
|
|
|
const MEM: Nid = 3;
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-13 07:15:45 -05:00
|
|
|
type Nid = u16;
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
type Lookup = crate::ctx_map::CtxMap<Nid>;
|
|
|
|
|
|
|
|
impl crate::ctx_map::CtxEntry for Nid {
|
|
|
|
type Ctx = [Result<Node, Nid>];
|
|
|
|
type Key<'a> = (Kind, &'a [Nid], ty::Id);
|
2024-09-08 05:00:07 -05:00
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a> {
|
|
|
|
ctx[*self as usize].as_ref().unwrap().key()
|
2024-09-08 05:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
struct Nodes {
|
2024-09-13 07:15:45 -05:00
|
|
|
values: Vec<Result<Node, Nid>>,
|
2024-09-07 20:12:57 -05:00
|
|
|
visited: BitSet,
|
2024-09-13 07:15:45 -05:00
|
|
|
free: Nid,
|
|
|
|
lookup: Lookup,
|
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(),
|
2024-09-13 07:15:45 -05:00
|
|
|
free: Nid::MAX,
|
2024-09-07 20:12:57 -05:00
|
|
|
lookup: Default::default(),
|
|
|
|
visited: Default::default(),
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Nodes {
|
2024-09-13 07:15:45 -05:00
|
|
|
fn remove_low(&mut self, id: Nid) -> 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-13 07:15:45 -05:00
|
|
|
self.free = Nid::MAX;
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn new_node_nop(&mut self, ty: ty::Id, kind: Kind, inps: impl Into<Vc>) -> Nid {
|
2024-09-19 06:40:03 -05:00
|
|
|
let node =
|
|
|
|
Node { ralloc_backref: u16::MAX, inputs: inps.into(), kind, ty, ..Default::default() };
|
2024-09-07 21:20:10 -05:00
|
|
|
|
2024-09-08 05:00:07 -05:00
|
|
|
let mut lookup_meta = None;
|
2024-09-08 10:11:33 -05:00
|
|
|
if !node.is_lazy_phi() {
|
2024-10-01 14:33:30 -05:00
|
|
|
let (raw_entry, hash) = self.lookup.entry(node.key(), &self.values);
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-08 05:00:07 -05:00
|
|
|
let entry = match raw_entry {
|
2024-10-01 14:33:30 -05:00
|
|
|
hash_map::RawEntryMut::Occupied(o) => return o.get_key_value().0.value,
|
2024-09-08 05:00:07 -05:00
|
|
|
hash_map::RawEntryMut::Vacant(v) => v,
|
|
|
|
};
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-08 05:00:07 -05:00
|
|
|
lookup_meta = Some((entry, hash));
|
|
|
|
}
|
2024-09-07 21:20:10 -05:00
|
|
|
|
2024-09-13 07:15:45 -05:00
|
|
|
if self.free == Nid::MAX {
|
2024-09-07 21:20:10 -05:00
|
|
|
self.free = self.values.len() as _;
|
2024-09-13 07:15:45 -05:00
|
|
|
self.values.push(Err(Nid::MAX));
|
2024-09-07 21:20:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let free = self.free;
|
2024-09-13 07:15:45 -05:00
|
|
|
for &d in node.inputs.as_slice() {
|
2024-09-07 21:20:10 -05:00
|
|
|
debug_assert_ne!(d, free);
|
2024-09-15 13:14:56 -05:00
|
|
|
self.values[d as usize].as_mut().unwrap_or_else(|_| panic!("{d}")).outputs.push(free);
|
2024-09-07 21:20:10 -05:00
|
|
|
}
|
|
|
|
self.free = mem::replace(&mut self.values[free as usize], Ok(node)).unwrap_err();
|
2024-09-08 05:00:07 -05:00
|
|
|
|
|
|
|
if let Some((entry, hash)) = lookup_meta {
|
2024-10-01 14:33:30 -05:00
|
|
|
entry.insert(crate::ctx_map::Key { value: free, hash }, ());
|
2024-09-08 05:00:07 -05:00
|
|
|
}
|
|
|
|
free
|
2024-09-07 21:20:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_node_lookup(&mut self, target: Nid) {
|
2024-09-08 10:11:33 -05:00
|
|
|
if !self[target].is_lazy_phi() {
|
2024-10-01 14:33:30 -05:00
|
|
|
self.lookup.remove(&target, &self.values).unwrap();
|
2024-09-08 05:00:07 -05:00
|
|
|
}
|
2024-09-07 21:20:10 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn new_node_low(&mut self, ty: ty::Id, kind: Kind, inps: impl Into<Vc>) -> (Nid, bool) {
|
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);
|
2024-09-28 08:13:32 -05:00
|
|
|
(opt, true)
|
2024-09-04 09:54:34 -05:00
|
|
|
} else {
|
2024-09-28 08:13:32 -05:00
|
|
|
(id, false)
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn new_node(&mut self, ty: ty::Id, kind: Kind, inps: impl Into<Vc>) -> Nid {
|
2024-09-28 08:13:32 -05:00
|
|
|
self.new_node_low(ty, kind, inps).0
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn new_node_lit(&mut self, ty: ty::Id, kind: Kind, inps: impl Into<Vc>) -> Value {
|
|
|
|
Value::new(self.new_node_low(ty, kind, inps).0).ty(ty)
|
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
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> {
|
2024-09-28 08:13:32 -05:00
|
|
|
use {Kind as K, TokenKind as T};
|
2024-09-04 09:54:34 -05:00
|
|
|
match self[target].kind {
|
2024-09-28 08:13:32 -05:00
|
|
|
K::BinOp { op } => {
|
|
|
|
let &[ctrl, mut lhs, mut rhs] = self[target].inputs.as_slice() else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
let ty = self[target].ty;
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if let (&K::CInt { value: a }, &K::CInt { value: b }) =
|
|
|
|
(&self[lhs].kind, &self[rhs].kind)
|
|
|
|
{
|
|
|
|
return Some(
|
|
|
|
self.new_node(ty, K::CInt { value: op.apply_binop(a, b) }, [ctrl]),
|
|
|
|
);
|
|
|
|
}
|
2024-09-08 05:00:07 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if lhs == rhs {
|
|
|
|
match op {
|
|
|
|
T::Sub => return Some(self.new_node(ty, K::CInt { value: 0 }, [ctrl])),
|
|
|
|
T::Add => {
|
|
|
|
let rhs = self.new_node_nop(ty, K::CInt { value: 2 }, [ctrl]);
|
|
|
|
return Some(
|
|
|
|
self.new_node(ty, K::BinOp { op: T::Mul }, [ctrl, lhs, rhs]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-09-08 05:00:07 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
// this is more general the pushing constants to left to help deduplicate expressions more
|
|
|
|
let mut changed = false;
|
|
|
|
if op.is_comutative() && self[lhs].key() < self[rhs].key() {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(&mut lhs, &mut rhs);
|
2024-09-28 08:13:32 -05:00
|
|
|
changed = true;
|
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if let K::CInt { value } = self[lhs].kind
|
|
|
|
&& op == T::Sub
|
|
|
|
{
|
|
|
|
let lhs = self.new_node_nop(ty, K::CInt { value: -value }, [ctrl]);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op: T::Add }, [ctrl, rhs, lhs]));
|
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if let K::CInt { 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),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if op.is_comutative() && self[lhs].kind == (K::BinOp { op }) {
|
|
|
|
let &[_, a, b] = self[lhs].inputs.as_slice() else { unreachable!() };
|
|
|
|
if let K::CInt { value: av } = self[b].kind
|
|
|
|
&& let K::CInt { value: bv } = self[rhs].kind
|
|
|
|
{
|
|
|
|
// (a op #b) op #c => a op (#b op #c)
|
|
|
|
let new_rhs =
|
|
|
|
self.new_node_nop(ty, K::CInt { value: op.apply_binop(av, bv) }, [
|
|
|
|
ctrl,
|
|
|
|
]);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op }, [ctrl, a, new_rhs]));
|
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if self.is_const(b) {
|
|
|
|
// (a op #b) op c => (a op c) op #b
|
|
|
|
let new_lhs = self.new_node(ty, K::BinOp { op }, [ctrl, a, rhs]);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op }, [ctrl, new_lhs, b]));
|
|
|
|
}
|
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if op == T::Add
|
|
|
|
&& self[lhs].kind == (K::BinOp { op: T::Mul })
|
|
|
|
&& self[lhs].inputs[1] == rhs
|
|
|
|
&& let K::CInt { value } = self[self[lhs].inputs[2]].kind
|
|
|
|
{
|
|
|
|
// a * #n + a => a * (#n + 1)
|
|
|
|
let new_rhs = self.new_node_nop(ty, K::CInt { value: value + 1 }, [ctrl]);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op: T::Mul }, [ctrl, rhs, new_rhs]));
|
|
|
|
}
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if op == T::Sub && self[lhs].kind == (K::BinOp { op }) {
|
|
|
|
// (a - b) - c => a - (b + c)
|
|
|
|
let &[_, a, b] = self[lhs].inputs.as_slice() else { unreachable!() };
|
|
|
|
let c = rhs;
|
|
|
|
let new_rhs = self.new_node(ty, K::BinOp { op: T::Add }, [ctrl, b, c]);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op }, [ctrl, a, new_rhs]));
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if changed {
|
|
|
|
return Some(self.new_node(ty, self[target].kind, [ctrl, lhs, rhs]));
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
K::UnOp { op } => {
|
|
|
|
let &[ctrl, oper] = self[target].inputs.as_slice() else { unreachable!() };
|
|
|
|
let ty = self[target].ty;
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if let K::CInt { value } = self[oper].kind {
|
|
|
|
return Some(
|
|
|
|
self.new_node(ty, K::CInt { value: op.apply_unop(value) }, [ctrl]),
|
|
|
|
);
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
K::If => {
|
|
|
|
let cond = self[target].inputs[1];
|
|
|
|
if let K::CInt { value } = self[cond].kind {
|
2024-10-17 12:32:10 -05:00
|
|
|
let ty = if value == 0 {
|
|
|
|
ty::Id::LEFT_UNREACHABLE
|
|
|
|
} else {
|
|
|
|
ty::Id::RIGHT_UNREACHABLE
|
|
|
|
};
|
2024-09-28 08:13:32 -05:00
|
|
|
return Some(self.new_node_nop(ty, K::If, [self[target].inputs[0], cond]));
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
K::Phi => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let &[ctrl, lhs, rhs] = self[target].inputs.as_slice() else { unreachable!() };
|
|
|
|
|
|
|
|
if lhs == rhs {
|
|
|
|
return Some(lhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self[lhs].kind == Kind::Stre
|
|
|
|
&& self[rhs].kind == Kind::Stre
|
|
|
|
&& self[lhs].ty == self[rhs].ty
|
|
|
|
&& self[lhs].inputs[2] == self[rhs].inputs[2]
|
|
|
|
&& self[lhs].inputs.get(3) == self[rhs].inputs.get(3)
|
|
|
|
{
|
|
|
|
let pick_value = self.new_node(self[lhs].ty, Kind::Phi, [
|
|
|
|
ctrl,
|
|
|
|
self[lhs].inputs[1],
|
|
|
|
self[rhs].inputs[1],
|
|
|
|
]);
|
|
|
|
let mut vc = Vc::from([VOID, pick_value, self[lhs].inputs[2]]);
|
|
|
|
for &rest in &self[lhs].inputs[3..] {
|
|
|
|
vc.push(rest);
|
|
|
|
}
|
|
|
|
for &rest in &self[rhs].inputs[4..] {
|
|
|
|
vc.push(rest);
|
|
|
|
}
|
|
|
|
return Some(self.new_node(self[lhs].ty, Kind::Stre, vc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
K::Stre => {
|
|
|
|
if self[target].inputs[2] != VOID
|
|
|
|
&& self[target].inputs.len() == 4
|
|
|
|
&& self[self[target].inputs[3]].kind == Kind::Stre
|
|
|
|
&& self[self[target].inputs[3]].inputs[2] == self[target].inputs[2]
|
|
|
|
{
|
|
|
|
return Some(self.modify_input(
|
|
|
|
self[target].inputs[3],
|
|
|
|
1,
|
|
|
|
self[target].inputs[1],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
K::Load => {
|
|
|
|
if self[target].inputs.len() == 3
|
|
|
|
&& self[self[target].inputs[2]].kind == Kind::Stre
|
|
|
|
&& self[self[target].inputs[2]].inputs[2] == self[target].inputs[1]
|
|
|
|
&& self[self[target].inputs[2]].ty == self[target].ty
|
|
|
|
{
|
|
|
|
return Some(self[self[target].inputs[2]].inputs[1]);
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
_ => {}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
None
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_const(&self, id: Nid) -> bool {
|
2024-09-08 10:11:33 -05:00
|
|
|
matches!(self[id].kind, Kind::CInt { .. })
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn replace(&mut self, target: Nid, with: Nid) {
|
2024-09-08 05:00:07 -05:00
|
|
|
let mut back_press = 0;
|
2024-09-04 09:54:34 -05:00
|
|
|
for i in 0..self[target].outputs.len() {
|
2024-09-08 05:00:07 -05:00
|
|
|
let out = self[target].outputs[i - back_press];
|
2024-09-07 21:20:10 -05:00
|
|
|
let index = self[out].inputs.iter().position(|&p| p == target).unwrap();
|
2024-09-28 08:13:32 -05:00
|
|
|
self.lock(target);
|
2024-09-08 05:00:07 -05:00
|
|
|
let prev_len = self[target].outputs.len();
|
|
|
|
self.modify_input(out, index, with);
|
|
|
|
back_press += (self[target].outputs.len() != prev_len) as usize;
|
2024-09-28 08:13:32 -05:00
|
|
|
self.unlock(target);
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
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-10-01 14:33:30 -05:00
|
|
|
let (entry, hash) = self.lookup.entry(target.key(&self.values), &self.values);
|
2024-09-07 21:20:10 -05:00
|
|
|
match entry {
|
2024-09-30 12:09:17 -05:00
|
|
|
hash_map::RawEntryMut::Occupied(other) => {
|
2024-10-01 14:33:30 -05:00
|
|
|
let rpl = other.get_key_value().0.value;
|
2024-09-07 21:20:10 -05:00
|
|
|
self[target].inputs[inp_index] = prev;
|
|
|
|
self.replace(target, rpl);
|
|
|
|
rpl
|
|
|
|
}
|
|
|
|
hash_map::RawEntryMut::Vacant(slot) => {
|
2024-10-01 14:33:30 -05:00
|
|
|
slot.insert(crate::ctx_map::Key { value: target, hash }, ());
|
2024-09-07 21:20:10 -05:00
|
|
|
let index = self[prev].outputs.iter().position(|&o| o == target).unwrap();
|
|
|
|
self[prev].outputs.swap_remove(index);
|
|
|
|
self[with].outputs.push(target);
|
2024-09-28 08:13:32 -05:00
|
|
|
self.remove(prev);
|
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
|
|
|
}
|
|
|
|
|
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-09 12:36:53 -05:00
|
|
|
#[allow(clippy::format_in_format_args)]
|
2024-09-30 12:09:17 -05:00
|
|
|
fn basic_blocks_instr(&mut self, out: &mut String, node: Nid) -> core::fmt::Result {
|
2024-09-12 11:42:21 -05:00
|
|
|
if self[node].kind != Kind::Loop && self[node].kind != Kind::Region {
|
2024-09-19 06:40:03 -05:00
|
|
|
write!(out, " {node:>2}-c{:>2}: ", self[node].ralloc_backref)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
match self[node].kind {
|
|
|
|
Kind::Start => unreachable!(),
|
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::If => write!(out, " if: "),
|
2024-09-12 11:42:21 -05:00
|
|
|
Kind::Region | Kind::Loop => writeln!(out, " goto: {node}"),
|
|
|
|
Kind::Return => write!(out, " ret: "),
|
2024-09-09 12:36:53 -05:00
|
|
|
Kind::CInt { value } => write!(out, "cint: #{value:<4}"),
|
|
|
|
Kind::Phi => write!(out, " phi: "),
|
2024-09-20 12:01:44 -05:00
|
|
|
Kind::Arg { index } => write!(out, " arg: {index:<5}"),
|
2024-09-15 13:14:56 -05:00
|
|
|
Kind::BinOp { op } | Kind::UnOp { op } => {
|
2024-09-09 12:36:53 -05:00
|
|
|
write!(out, "{:>4}: ", op.name())
|
|
|
|
}
|
|
|
|
Kind::Call { func } => {
|
2024-09-12 11:42:21 -05:00
|
|
|
write!(out, "call: {func} {} ", self[node].depth)
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::Entry => write!(out, "ctrl: {:<5}", "entry"),
|
|
|
|
Kind::Then => write!(out, "ctrl: {:<5}", "then"),
|
|
|
|
Kind::Else => write!(out, "ctrl: {:<5}", "else"),
|
|
|
|
Kind::Stck => write!(out, "stck: "),
|
2024-10-18 02:52:50 -05:00
|
|
|
Kind::Load => write!(out, "load: "),
|
|
|
|
Kind::Stre => write!(out, "stre: "),
|
2024-09-20 12:01:44 -05:00
|
|
|
_ => unreachable!(),
|
2024-09-09 12:36:53 -05:00
|
|
|
}?;
|
|
|
|
|
2024-09-12 11:42:21 -05:00
|
|
|
if self[node].kind != Kind::Loop && self[node].kind != Kind::Region {
|
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
" {:<14} {}",
|
|
|
|
format!("{:?}", self[node].inputs),
|
|
|
|
format!("{:?}", self[node].outputs)
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
fn basic_blocks_low(&mut self, out: &mut String, mut node: Nid) -> core::fmt::Result {
|
2024-09-12 11:42:21 -05:00
|
|
|
let iter = |nodes: &Nodes, node| nodes[node].outputs.clone().into_iter().rev();
|
2024-09-13 07:15:45 -05:00
|
|
|
while self.visited.set(node) {
|
2024-09-12 11:42:21 -05:00
|
|
|
match self[node].kind {
|
2024-09-09 12:36:53 -05:00
|
|
|
Kind::Start => {
|
|
|
|
writeln!(out, "start: {}", self[node].depth)?;
|
|
|
|
let mut cfg_index = Nid::MAX;
|
2024-09-12 11:42:21 -05:00
|
|
|
for o in iter(self, node) {
|
|
|
|
self.basic_blocks_instr(out, o)?;
|
2024-09-20 12:01:44 -05:00
|
|
|
if self[o].kind.is_cfg() {
|
2024-09-09 12:36:53 -05:00
|
|
|
cfg_index = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = cfg_index;
|
|
|
|
}
|
|
|
|
Kind::End => break,
|
|
|
|
Kind::If => {
|
|
|
|
self.basic_blocks_low(out, self[node].outputs[0])?;
|
|
|
|
node = self[node].outputs[1];
|
|
|
|
}
|
|
|
|
Kind::Region => {
|
2024-09-15 13:14:56 -05:00
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"region{node}: {} {} {:?}",
|
|
|
|
self[node].depth, self[node].loop_depth, self[node].inputs
|
|
|
|
)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
let mut cfg_index = Nid::MAX;
|
2024-09-12 11:42:21 -05:00
|
|
|
for o in iter(self, node) {
|
|
|
|
self.basic_blocks_instr(out, o)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
if self.is_cfg(o) {
|
|
|
|
cfg_index = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = cfg_index;
|
|
|
|
}
|
|
|
|
Kind::Loop => {
|
2024-09-15 13:14:56 -05:00
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"loop{node}: {} {} {:?}",
|
|
|
|
self[node].depth, self[node].loop_depth, self[node].outputs
|
|
|
|
)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
let mut cfg_index = Nid::MAX;
|
2024-09-12 11:42:21 -05:00
|
|
|
for o in iter(self, node) {
|
|
|
|
self.basic_blocks_instr(out, o)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
if self.is_cfg(o) {
|
|
|
|
cfg_index = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = cfg_index;
|
|
|
|
}
|
|
|
|
Kind::Return => {
|
|
|
|
node = self[node].outputs[0];
|
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::Then | Kind::Else | Kind::Entry => {
|
2024-09-15 13:14:56 -05:00
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"b{node}: {} {} {:?}",
|
|
|
|
self[node].depth, self[node].loop_depth, self[node].outputs
|
|
|
|
)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
let mut cfg_index = Nid::MAX;
|
2024-09-12 11:42:21 -05:00
|
|
|
for o in iter(self, node) {
|
|
|
|
self.basic_blocks_instr(out, o)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
if self.is_cfg(o) {
|
|
|
|
cfg_index = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = cfg_index;
|
|
|
|
}
|
|
|
|
Kind::Call { .. } => {
|
|
|
|
let mut cfg_index = Nid::MAX;
|
2024-09-13 07:15:45 -05:00
|
|
|
let mut print_ret = true;
|
2024-09-12 11:42:21 -05:00
|
|
|
for o in iter(self, node) {
|
2024-09-13 07:15:45 -05:00
|
|
|
if self[o].inputs[0] == node
|
2024-09-30 12:09:17 -05:00
|
|
|
&& (self[node].outputs[0] != o || core::mem::take(&mut print_ret))
|
2024-09-13 07:15:45 -05:00
|
|
|
{
|
2024-09-09 12:36:53 -05:00
|
|
|
self.basic_blocks_instr(out, o)?;
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
if self.is_cfg(o) {
|
|
|
|
cfg_index = o;
|
|
|
|
}
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
node = cfg_index;
|
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
_ => unreachable!(),
|
2024-09-07 20:12:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
fn basic_blocks(&mut self) {
|
|
|
|
let mut out = String::new();
|
|
|
|
self.visited.clear(self.values.len());
|
2024-09-16 08:49:27 -05:00
|
|
|
self.basic_blocks_low(&mut out, VOID).unwrap();
|
2024-09-30 12:27:00 -05:00
|
|
|
log::info!("{out}");
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
fn is_cfg(&self, o: Nid) -> bool {
|
2024-09-09 12:36:53 -05:00
|
|
|
self[o].kind.is_cfg()
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_final_integrity(&self) {
|
2024-09-15 13:14:56 -05:00
|
|
|
//let mut failed = false;
|
|
|
|
for (_, node) in self.iter() {
|
2024-09-07 20:12:57 -05:00
|
|
|
debug_assert_eq!(node.lock_rc, 0, "{:?}", node.kind);
|
2024-09-15 13:14:56 -05:00
|
|
|
// if !matches!(node.kind, Kind::Return | Kind::End) && node.outputs.is_empty() {
|
|
|
|
// log::err!("outputs are empry {i} {:?}", node.kind);
|
|
|
|
// failed = true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let mut allowed_cfgs = 1 + (node.kind == Kind::If) as usize;
|
|
|
|
// for &o in node.outputs.iter() {
|
|
|
|
// if self.is_cfg(i) {
|
|
|
|
// if allowed_cfgs == 0 && self.is_cfg(o) {
|
|
|
|
// log::err!(
|
|
|
|
// "multiple cfg outputs detected: {:?} -> {:?}",
|
|
|
|
// node.kind,
|
|
|
|
// self[o].kind
|
|
|
|
// );
|
|
|
|
// failed = true;
|
|
|
|
// } else {
|
|
|
|
// allowed_cfgs += self.is_cfg(o) as usize;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let other = match &self.values[o as usize] {
|
|
|
|
// Ok(other) => other,
|
|
|
|
// Err(_) => {
|
|
|
|
// log::err!("the edge points to dropped node: {i} {:?} {o}", node.kind,);
|
|
|
|
// failed = true;
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// let occurs = self[o].inputs.iter().filter(|&&el| el == i).count();
|
|
|
|
// let self_occurs = self[i].outputs.iter().filter(|&&el| el == o).count();
|
|
|
|
// if occurs != self_occurs {
|
|
|
|
// log::err!(
|
|
|
|
// "the edge is not bidirectional: {i} {:?} {self_occurs} {o} {:?} {occurs}",
|
|
|
|
// node.kind,
|
|
|
|
// other.kind
|
|
|
|
// );
|
|
|
|
// failed = true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
//if failed {
|
|
|
|
// panic!()
|
|
|
|
//}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
#[expect(dead_code)]
|
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-13 07:15:45 -05:00
|
|
|
&& nodes.visited.set(n)
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-09-08 05:00:07 -05:00
|
|
|
false
|
2024-09-07 20:12:57 -05:00
|
|
|
}
|
|
|
|
self.visited.clear(self.values.len());
|
|
|
|
climb_impl(self, from, &mut for_each)
|
|
|
|
}
|
2024-09-08 05:00:07 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
#[expect(dead_code)]
|
2024-09-08 05:00:07 -05:00
|
|
|
fn late_peephole(&mut self, target: Nid) -> Nid {
|
|
|
|
if let Some(id) = self.peephole(target) {
|
|
|
|
self.replace(target, id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
target
|
|
|
|
}
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-17 15:29:09 -05:00
|
|
|
fn load_loop_value(&mut self, index: usize, value: &mut Value, loops: &mut [Loop]) {
|
|
|
|
if value.id != 0 {
|
|
|
|
debug_assert!(!value.var);
|
2024-09-08 10:11:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [loob, loops @ ..] = loops else { unreachable!() };
|
2024-10-18 06:11:11 -05:00
|
|
|
let lvalue = &mut loob.scope.vars[index].value;
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-17 15:29:09 -05:00
|
|
|
self.load_loop_value(index, lvalue, loops);
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
if !self[lvalue.id].is_lazy_phi() {
|
2024-10-17 15:29:09 -05:00
|
|
|
self.unlock(value.id);
|
2024-10-17 12:32:10 -05:00
|
|
|
let inps = [loob.node, lvalue.id, VOID];
|
2024-10-17 15:29:09 -05:00
|
|
|
self.unlock(lvalue.id);
|
|
|
|
lvalue.id = self.new_node_nop(lvalue.ty, Kind::Phi, inps);
|
|
|
|
self[lvalue.id].lock_rc += 2;
|
2024-09-08 10:11:33 -05:00
|
|
|
} else {
|
2024-10-17 12:32:10 -05:00
|
|
|
self.lock(lvalue.id);
|
2024-10-17 15:29:09 -05:00
|
|
|
self.unlock(value.id);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
*value = *lvalue;
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_dominance(&mut self, nd: Nid, min: Nid, check_outputs: bool) {
|
|
|
|
let node = self[nd].clone();
|
|
|
|
for &i in node.inputs.iter() {
|
|
|
|
let dom = idom(self, i);
|
|
|
|
debug_assert!(
|
|
|
|
self.dominates(dom, min),
|
|
|
|
"{dom} {min} {node:?} {:?}",
|
|
|
|
self.basic_blocks()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if check_outputs {
|
|
|
|
for &o in node.outputs.iter() {
|
|
|
|
let dom = use_block(nd, o, self);
|
|
|
|
debug_assert!(
|
|
|
|
self.dominates(min, dom),
|
|
|
|
"{min} {dom} {node:?} {:?}",
|
|
|
|
self.basic_blocks()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dominates(&mut self, dominator: Nid, mut dominated: Nid) -> bool {
|
|
|
|
loop {
|
|
|
|
if dominator == dominated {
|
|
|
|
break true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if idepth(self, dominator) > idepth(self, dominated) {
|
|
|
|
break false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dominated = idom(self, dominated);
|
2024-09-08 10:11:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-19 06:40:03 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
#[expect(dead_code)]
|
2024-09-19 06:40:03 -05:00
|
|
|
fn iter_mut(&mut self) -> impl Iterator<Item = &mut Node> {
|
|
|
|
self.values.iter_mut().flat_map(Result::as_mut)
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
|
|
|
|
fn lock_scope(&mut self, scope: &Scope) {
|
|
|
|
if let Some(str) = scope.store {
|
|
|
|
self.lock(str);
|
|
|
|
}
|
|
|
|
for &load in &scope.loads {
|
|
|
|
self.lock(load);
|
|
|
|
}
|
|
|
|
for var in &scope.vars {
|
|
|
|
self.lock(var.value.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unlock_remove_scope(&mut self, scope: &Scope) {
|
|
|
|
if let Some(str) = scope.store {
|
|
|
|
self.unlock_remove(str);
|
|
|
|
}
|
|
|
|
for &load in &scope.loads {
|
|
|
|
self.unlock_remove(load);
|
|
|
|
}
|
|
|
|
for var in &scope.vars {
|
|
|
|
self.unlock_remove(var.value.id);
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 07:15:45 -05:00
|
|
|
impl ops::Index<Nid> for Nodes {
|
2024-09-04 09:54:34 -05:00
|
|
|
type Output = Node;
|
|
|
|
|
2024-09-13 07:15:45 -05:00
|
|
|
fn index(&self, index: Nid) -> &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-13 07:15:45 -05:00
|
|
|
impl ops::IndexMut<Nid> for Nodes {
|
|
|
|
fn index_mut(&mut self, index: Nid) -> &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-12 11:42:21 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
|
2024-09-04 09:54:34 -05:00
|
|
|
#[repr(u8)]
|
|
|
|
pub enum Kind {
|
2024-09-12 11:42:21 -05:00
|
|
|
#[default]
|
2024-09-04 09:54:34 -05:00
|
|
|
Start,
|
2024-09-30 12:09:17 -05:00
|
|
|
// [ctrl]
|
|
|
|
Entry,
|
|
|
|
Mem,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [terms...]
|
2024-09-04 09:54:34 -05:00
|
|
|
End,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, cond]
|
2024-09-05 04:16:11 -05:00
|
|
|
If,
|
2024-09-30 12:09:17 -05:00
|
|
|
Then,
|
|
|
|
Else,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [lhs, rhs]
|
2024-09-05 04:16:11 -05:00
|
|
|
Region,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [entry, back]
|
2024-09-05 18:17:54 -05:00
|
|
|
Loop,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, ?value]
|
2024-09-04 09:54:34 -05:00
|
|
|
Return,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl]
|
2024-09-12 11:42:21 -05:00
|
|
|
CInt {
|
|
|
|
value: i64,
|
|
|
|
},
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, lhs, rhs]
|
2024-09-05 04:16:11 -05:00
|
|
|
Phi,
|
2024-09-20 12:01:44 -05:00
|
|
|
Arg {
|
|
|
|
index: u32,
|
|
|
|
},
|
|
|
|
// [ctrl, oper]
|
2024-09-15 13:14:56 -05:00
|
|
|
UnOp {
|
|
|
|
op: lexer::TokenKind,
|
|
|
|
},
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, lhs, rhs]
|
2024-09-12 11:42:21 -05:00
|
|
|
BinOp {
|
|
|
|
op: lexer::TokenKind,
|
|
|
|
},
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, ...args]
|
2024-09-12 11:42:21 -05:00
|
|
|
Call {
|
|
|
|
func: ty::Func,
|
|
|
|
},
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl]
|
2024-09-27 09:53:28 -05:00
|
|
|
Stck,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, memory]
|
2024-10-18 02:52:50 -05:00
|
|
|
Load,
|
2024-09-30 12:09:17 -05:00
|
|
|
// [ctrl, value, memory]
|
2024-10-18 02:52:50 -05:00
|
|
|
Stre,
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Kind {
|
2024-09-09 12:36:53 -05:00
|
|
|
fn is_pinned(&self) -> bool {
|
2024-09-27 09:53:28 -05:00
|
|
|
self.is_cfg() || matches!(self, Self::Phi | Self::Mem)
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_cfg(&self) -> bool {
|
|
|
|
matches!(
|
|
|
|
self,
|
2024-09-19 06:40:03 -05:00
|
|
|
Self::Start
|
|
|
|
| Self::End
|
|
|
|
| Self::Return
|
2024-09-27 09:53:28 -05:00
|
|
|
| Self::Entry
|
|
|
|
| Self::Then
|
|
|
|
| Self::Else
|
2024-09-20 12:01:44 -05:00
|
|
|
| Self::Arg { .. }
|
2024-09-19 06:40:03 -05:00
|
|
|
| Self::Call { .. }
|
|
|
|
| Self::If
|
|
|
|
| Self::Region
|
|
|
|
| Self::Loop
|
2024-09-09 12:36:53 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ends_basic_block(&self) -> bool {
|
2024-09-19 06:40:03 -05:00
|
|
|
matches!(self, Self::Return | Self::If | Self::End)
|
2024-09-09 12:36:53 -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 {
|
2024-09-30 12:09:17 -05:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2024-09-07 20:12:57 -05:00
|
|
|
match self {
|
2024-09-08 10:11:33 -05:00
|
|
|
Kind::CInt { value } => write!(f, "#{value}"),
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::Entry => write!(f, "ctrl[entry]"),
|
|
|
|
Kind::Then => write!(f, "ctrl[then]"),
|
|
|
|
Kind::Else => write!(f, "ctrl[else]"),
|
2024-09-07 20:12:57 -05:00
|
|
|
Kind::BinOp { op } => write!(f, "{op}"),
|
|
|
|
Kind::Call { func, .. } => write!(f, "call {func}"),
|
|
|
|
slf => write!(f, "{slf:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-14 04:26:54 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2024-09-13 07:15:45 -05:00
|
|
|
//#[repr(align(64))]
|
2024-10-01 14:33:30 -05:00
|
|
|
pub struct Node {
|
2024-09-27 09:53:28 -05:00
|
|
|
kind: Kind,
|
2024-09-13 07:15:45 -05:00
|
|
|
inputs: Vc,
|
|
|
|
outputs: Vc,
|
2024-09-27 09:53:28 -05:00
|
|
|
ty: ty::Id,
|
|
|
|
offset: Offset,
|
2024-09-19 06:40:03 -05:00
|
|
|
ralloc_backref: RallocBRef,
|
2024-09-13 07:15:45 -05:00
|
|
|
depth: IDomDepth,
|
|
|
|
lock_rc: LockRc,
|
|
|
|
loop_depth: LoopDepth,
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08 10:11:33 -05:00
|
|
|
fn key(&self) -> (Kind, &[Nid], ty::Id) {
|
|
|
|
(self.kind, &self.inputs, self.ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_lazy_phi(&self) -> bool {
|
|
|
|
self.kind == Kind::Phi && self.inputs[2] == 0
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-19 06:40:03 -05:00
|
|
|
type RallocBRef = u16;
|
2024-09-13 07:15:45 -05:00
|
|
|
type LoopDepth = u16;
|
|
|
|
type LockRc = u16;
|
|
|
|
type IDomDepth = u16;
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
struct Loop {
|
2024-09-05 18:17:54 -05:00
|
|
|
node: Nid,
|
2024-09-08 10:11:33 -05:00
|
|
|
ctrl: [Nid; 2],
|
2024-10-18 06:11:11 -05:00
|
|
|
ctrl_scope: [Scope; 2],
|
|
|
|
scope: Scope,
|
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,
|
2024-10-17 12:32:10 -05:00
|
|
|
value: Value,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
#[derive(Default, Clone)]
|
|
|
|
struct Scope {
|
|
|
|
vars: Vec<Variable>,
|
|
|
|
loads: Vec<Nid>,
|
|
|
|
store: Option<Nid>,
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct ItemCtx {
|
|
|
|
file: FileId,
|
2024-09-28 14:56:39 -05:00
|
|
|
#[expect(dead_code)]
|
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-05 04:16:11 -05:00
|
|
|
ctrl: Nid,
|
|
|
|
|
2024-09-13 07:15:45 -05:00
|
|
|
call_count: u16,
|
2024-09-05 19:04:19 -05:00
|
|
|
filled: Vec<Nid>,
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
loops: Vec<Loop>,
|
2024-10-18 06:11:11 -05:00
|
|
|
scope: Scope,
|
2024-10-04 14:44:29 -05:00
|
|
|
clobbered: Vec<Nid>,
|
2024-09-04 09:54:34 -05:00
|
|
|
ret_relocs: Vec<Reloc>,
|
|
|
|
relocs: Vec<TypedReloc>,
|
2024-09-15 13:14:56 -05:00
|
|
|
jump_relocs: Vec<(Nid, Reloc)>,
|
2024-09-04 09:54:34 -05:00
|
|
|
code: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemCtx {
|
|
|
|
fn emit(&mut self, instr: (usize, [u8; instrs::MAX_SIZE])) {
|
2024-09-13 11:22:27 -05:00
|
|
|
crate::emit(&mut self.code, instr);
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Pool {
|
|
|
|
cis: Vec<ItemCtx>,
|
|
|
|
}
|
|
|
|
|
2024-09-27 09:53:28 -05:00
|
|
|
struct Regalloc {
|
|
|
|
env: regalloc2::MachineEnv,
|
|
|
|
ctx: regalloc2::Ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Regalloc {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
env: regalloc2::MachineEnv {
|
|
|
|
preferred_regs_by_class: [
|
|
|
|
(1..13).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(),
|
|
|
|
vec![],
|
|
|
|
vec![],
|
|
|
|
],
|
|
|
|
non_preferred_regs_by_class: [
|
|
|
|
(13..64).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(),
|
|
|
|
vec![],
|
|
|
|
vec![],
|
|
|
|
],
|
|
|
|
scratch_by_class: Default::default(),
|
|
|
|
fixed_stack_slots: Default::default(),
|
|
|
|
},
|
|
|
|
ctx: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
#[derive(Default, Clone, Copy)]
|
|
|
|
struct Value {
|
|
|
|
ty: ty::Id,
|
|
|
|
var: bool,
|
|
|
|
ptr: bool,
|
|
|
|
id: Nid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
const NEVER: Option<Value> =
|
2024-10-18 06:11:11 -05:00
|
|
|
Some(Self { ty: ty::Id::NEVER, var: false, ptr: false, id: NEVER });
|
|
|
|
const VOID: Value = Self { ty: ty::Id::VOID, var: false, ptr: false, id: VOID };
|
2024-10-17 12:32:10 -05:00
|
|
|
|
|
|
|
pub fn new(id: Nid) -> Self {
|
|
|
|
Self { id, ..Default::default() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn var(id: usize) -> Self {
|
2024-10-17 15:29:09 -05:00
|
|
|
Self { id: u16::MAX - (id as Nid), var: true, ..Default::default() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptr(id: Nid) -> Self {
|
|
|
|
Self { id, ptr: true, ..Default::default() }
|
2024-10-17 12:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn ty(self, ty: impl Into<ty::Id>) -> Self {
|
|
|
|
Self { ty: ty.into(), ..self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Codegen {
|
|
|
|
pub files: Vec<parser::Ast>,
|
|
|
|
tasks: Vec<Option<FTask>>,
|
|
|
|
|
|
|
|
tys: Types,
|
|
|
|
ci: ItemCtx,
|
|
|
|
pool: Pool,
|
2024-09-27 09:53:28 -05:00
|
|
|
ralloc: Regalloc,
|
2024-09-06 11:50:28 -05:00
|
|
|
errors: RefCell<String>,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Codegen {
|
2024-10-04 14:44:29 -05:00
|
|
|
fn graphviz_low(&self, out: &mut String) -> core::fmt::Result {
|
|
|
|
use core::fmt::Write;
|
2024-09-28 08:13:32 -05:00
|
|
|
|
2024-10-04 14:44:29 -05:00
|
|
|
for (i, node) in self.ci.nodes.iter() {
|
|
|
|
let color = if self.ci.nodes.is_cfg(i) { "yellow" } else { "white" };
|
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"node{i}[label=\"{} {}\" color={color}]",
|
|
|
|
node.kind,
|
|
|
|
self.ty_display(node.ty)
|
|
|
|
)?;
|
|
|
|
for (j, &o) in node.outputs.iter().enumerate() {
|
|
|
|
let color = if self.ci.nodes.is_cfg(i) && self.ci.nodes.is_cfg(o) {
|
|
|
|
"red"
|
|
|
|
} else {
|
|
|
|
"lightgray"
|
|
|
|
};
|
|
|
|
let index = self.ci.nodes[o].inputs.iter().position(|&inp| i == inp).unwrap();
|
|
|
|
let style =
|
|
|
|
if index == 0 && !self.ci.nodes.is_cfg(o) { "style=dotted" } else { "" };
|
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"node{o} -> node{i}[color={color} taillabel={index} headlabel={j} {style}]",
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn graphviz(&self) {
|
|
|
|
let out = &mut String::new();
|
|
|
|
_ = self.graphviz_low(out);
|
|
|
|
log::info!("{out}");
|
|
|
|
}
|
|
|
|
|
2024-10-18 02:52:50 -05:00
|
|
|
fn store_mem(&mut self, region: Nid, value: Nid) -> Nid {
|
2024-10-17 15:29:09 -05:00
|
|
|
let mut vc = Vc::from([VOID, value, region]);
|
2024-10-18 06:11:11 -05:00
|
|
|
if let Some(str) = self.ci.scope.store {
|
2024-10-17 15:29:09 -05:00
|
|
|
self.ci.nodes.unlock(str);
|
|
|
|
vc.push(str);
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
for load in self.ci.scope.loads.drain(..) {
|
2024-10-17 15:29:09 -05:00
|
|
|
if load == value {
|
|
|
|
self.ci.nodes.unlock(load);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if !self.ci.nodes.unlock_remove(load) {
|
|
|
|
vc.push(load);
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-10-18 02:52:50 -05:00
|
|
|
let store = self.ci.nodes.new_node(self.tof(value), Kind::Stre, vc);
|
2024-10-17 15:29:09 -05:00
|
|
|
self.ci.nodes.lock(store);
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.store = Some(store);
|
2024-10-17 15:29:09 -05:00
|
|
|
store
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 02:52:50 -05:00
|
|
|
fn load_mem(&mut self, region: Nid, ty: ty::Id) -> Nid {
|
2024-10-17 15:29:09 -05:00
|
|
|
let mut vc = Vc::from([VOID, region]);
|
2024-10-18 06:11:11 -05:00
|
|
|
if let Some(str) = self.ci.scope.store {
|
2024-10-17 15:29:09 -05:00
|
|
|
vc.push(str);
|
|
|
|
}
|
2024-10-18 02:52:50 -05:00
|
|
|
let load = self.ci.nodes.new_node(ty, Kind::Load, vc);
|
2024-10-17 15:29:09 -05:00
|
|
|
self.ci.nodes.lock(load);
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.loads.push(load);
|
2024-10-17 15:29:09 -05:00
|
|
|
load
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
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-19 06:40:03 -05:00
|
|
|
self.complete_call_graph();
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-09-03 10:51:28 -05:00
|
|
|
fn make_func_reachable(&mut self, func: ty::Func) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &mut self.tys.ins.funcs[func as usize];
|
2024-09-03 10:51:28 -05:00
|
|
|
if fuc.offset == u32::MAX {
|
|
|
|
fuc.offset = task::id(self.tasks.len() as _);
|
|
|
|
self.tasks.push(Some(FTask { file: fuc.file, id: func }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn raw_expr(&mut self, expr: &Expr) -> Option<Value> {
|
2024-10-04 14:44:29 -05:00
|
|
|
self.raw_expr_ctx(expr, Ctx::default())
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn raw_expr_ctx(&mut self, expr: &Expr, ctx: Ctx) -> Option<Value> {
|
2024-09-30 12:09:17 -05:00
|
|
|
let msg = "i know nothing about this name, gal, which is vired \
|
|
|
|
because we parsed succesfully";
|
2024-09-28 14:56:39 -05:00
|
|
|
// ordered by complexity of the expression
|
2024-09-03 10:51:28 -05:00
|
|
|
match *expr {
|
2024-10-17 12:32:10 -05:00
|
|
|
Expr::Comment { .. } => Some(Value::VOID),
|
2024-09-05 18:17:54 -05:00
|
|
|
Expr::Ident { pos, id, .. } => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let Some(index) = self.ci.scope.vars.iter().position(|v| v.id == id) else {
|
2024-09-06 11:50:28 -05:00
|
|
|
self.report(pos, msg);
|
2024-10-17 12:32:10 -05:00
|
|
|
return Value::NEVER;
|
2024-09-06 11:50:28 -05:00
|
|
|
};
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-08 10:11:33 -05:00
|
|
|
self.ci.nodes.load_loop_value(
|
|
|
|
index,
|
2024-10-18 06:11:11 -05:00
|
|
|
&mut self.ci.scope.vars[index].value,
|
2024-09-08 10:11:33 -05:00
|
|
|
&mut self.ci.loops,
|
|
|
|
);
|
2024-10-18 06:11:11 -05:00
|
|
|
debug_assert_ne!(self.ci.scope.vars[index].value.ty, ty::Id::VOID);
|
2024-09-07 20:12:57 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
Some(Value::var(index).ty(self.ci.scope.vars[index].value.ty))
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-10-17 12:32:10 -05:00
|
|
|
Expr::Number { value, .. } => Some(self.ci.nodes.new_node_lit(
|
2024-09-30 12:09:17 -05:00
|
|
|
ctx.ty.filter(|ty| ty.is_integer() || ty.is_pointer()).unwrap_or(ty::Id::INT),
|
2024-09-28 14:56:39 -05:00
|
|
|
Kind::CInt { value },
|
|
|
|
[VOID],
|
|
|
|
)),
|
|
|
|
Expr::Return { pos, val } => {
|
|
|
|
let value = if let Some(val) = val {
|
|
|
|
self.expr_ctx(val, Ctx { ty: self.ci.ret })?
|
|
|
|
} else {
|
2024-10-17 12:32:10 -05:00
|
|
|
Value { ty: ty::Id::VOID, ..Default::default() }
|
2024-09-28 14:56:39 -05:00
|
|
|
};
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let mut inps = Vc::from([self.ci.ctrl, value.id]);
|
2024-10-18 06:11:11 -05:00
|
|
|
for &m in self.ci.scope.store.iter() {
|
2024-10-17 15:29:09 -05:00
|
|
|
inps.push(m);
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::Id::VOID, Kind::Return, inps);
|
2024-09-28 14:56:39 -05:00
|
|
|
|
|
|
|
self.ci.nodes[NEVER].inputs.push(self.ci.ctrl);
|
|
|
|
self.ci.nodes[self.ci.ctrl].outputs.push(NEVER);
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let expected = *self.ci.ret.get_or_insert(value.ty);
|
|
|
|
_ = self.assert_ty(pos, value.ty, expected, true, "return value");
|
2024-09-28 14:56:39 -05:00
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
Expr::Field { target, name, pos } => {
|
|
|
|
let mut vtarget = self.raw_expr(target)?;
|
|
|
|
self.strip_var(&mut vtarget);
|
|
|
|
let tty = vtarget.ty;
|
2024-10-17 12:32:10 -05:00
|
|
|
|
2024-10-17 15:29:09 -05:00
|
|
|
let ty::Kind::Struct(s) = self.tys.base_of(tty).unwrap_or(tty).expand() else {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"the '{}' is not a struct, or pointer to one, \
|
|
|
|
but accessing fields is only possible on structs",
|
|
|
|
self.ty_display(tty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some((offset, ty)) = OffsetIter::offset_of(&self.tys, s, name) else {
|
|
|
|
let field_list = self
|
|
|
|
.tys
|
|
|
|
.struct_fields(s)
|
|
|
|
.iter()
|
|
|
|
.map(|f| self.tys.names.ident_str(f.name))
|
|
|
|
.intersperse("', '")
|
|
|
|
.collect::<String>();
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"the '{}' does not have this field, \
|
|
|
|
but it does have '{field_list}'",
|
|
|
|
self.ty_display(tty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
Some(Value::ptr(self.offset(vtarget.id, ty, offset)).ty(ty))
|
2024-10-17 15:29:09 -05:00
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr::UnOp { op: TokenKind::Band, val, .. } => {
|
|
|
|
let ctx = Ctx { ty: ctx.ty.and_then(|ty| self.tys.base_of(ty)) };
|
|
|
|
|
2024-10-04 14:44:29 -05:00
|
|
|
let mut val = self.raw_expr_ctx(val, ctx)?;
|
2024-10-17 12:32:10 -05:00
|
|
|
self.strip_var(&mut val);
|
|
|
|
|
|
|
|
if val.ptr {
|
|
|
|
val.ptr = false;
|
|
|
|
val.ty = self.tys.make_ptr(val.ty);
|
|
|
|
return Some(val);
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let stack = self.ci.nodes.new_node_nop(val.ty, Kind::Stck, [VOID, MEM]);
|
2024-10-18 02:52:50 -05:00
|
|
|
self.store_mem(stack, val.id);
|
2024-10-17 12:32:10 -05:00
|
|
|
|
|
|
|
Some(Value::new(stack).ty(self.tys.make_ptr(val.ty)))
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
Expr::UnOp { op: TokenKind::Mul, val, pos } => {
|
|
|
|
let ctx = Ctx { ty: ctx.ty.map(|ty| self.tys.make_ptr(ty)) };
|
2024-10-17 12:32:10 -05:00
|
|
|
let mut val = self.expr_ctx(val, ctx)?;
|
|
|
|
|
|
|
|
let Some(base) = self.tys.base_of(val.ty) else {
|
2024-09-28 14:56:39 -05:00
|
|
|
self.report(
|
|
|
|
pos,
|
2024-10-17 12:32:10 -05:00
|
|
|
fa!("the '{}' can not be dereferneced", self.ty_display(val.ty)),
|
2024-09-28 14:56:39 -05:00
|
|
|
);
|
2024-10-17 12:32:10 -05:00
|
|
|
return Value::NEVER;
|
2024-09-28 14:56:39 -05:00
|
|
|
};
|
2024-10-17 12:32:10 -05:00
|
|
|
val.ptr = true;
|
|
|
|
val.ty = base;
|
|
|
|
Some(val)
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
Expr::UnOp { pos, op: op @ TokenKind::Sub, val } => {
|
|
|
|
let val = self.expr_ctx(val, ctx)?;
|
2024-10-17 12:32:10 -05:00
|
|
|
if !val.ty.is_integer() {
|
|
|
|
self.report(pos, fa!("cant negate '{}'", self.ty_display(val.ty)));
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(self.ci.nodes.new_node_lit(val.ty, Kind::UnOp { op }, [VOID, val.id]))
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
Expr::BinOp { left: &Expr::Ident { id, .. }, op: TokenKind::Decl, right } => {
|
|
|
|
let value = self.expr(right)?;
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.lock(value.id);
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.vars.push(Variable { id, value });
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(Value::VOID)
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
2024-10-17 12:32:10 -05:00
|
|
|
Expr::BinOp { left, op: TokenKind::Assign, right } => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let dest = self.raw_expr(left)?;
|
2024-09-04 16:46:32 -05:00
|
|
|
let value = self.expr(right)?;
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
_ = self.assert_ty(left.pos(), value.ty, dest.ty, true, "assignment dest");
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
if dest.var {
|
2024-10-17 15:29:09 -05:00
|
|
|
self.ci.nodes.lock(value.id);
|
2024-10-18 06:11:11 -05:00
|
|
|
let var = &mut self.ci.scope.vars[(u16::MAX - dest.id) as usize];
|
2024-10-17 12:32:10 -05:00
|
|
|
let prev = core::mem::replace(&mut var.value, value);
|
|
|
|
self.ci.nodes.unlock_remove(prev.id);
|
|
|
|
} else if dest.ptr {
|
2024-10-18 02:52:50 -05:00
|
|
|
self.store_mem(dest.id, value.id);
|
2024-10-17 12:32:10 -05:00
|
|
|
} else {
|
|
|
|
self.report(left.pos(), "cannot assign to this expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Value::VOID)
|
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
Expr::BinOp { left, op, right } if op != TokenKind::Assign => {
|
2024-09-03 10:51:28 -05:00
|
|
|
let lhs = self.expr_ctx(left, ctx)?;
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.lock(lhs.id);
|
|
|
|
let rhs = self.expr_ctx(right, Ctx::default().with_ty(lhs.ty));
|
|
|
|
self.ci.nodes.unlock(lhs.id);
|
2024-09-03 10:51:28 -05:00
|
|
|
let rhs = rhs?;
|
2024-10-17 12:32:10 -05:00
|
|
|
let ty = self.assert_ty(left.pos(), rhs.ty, lhs.ty, false, "right operand");
|
|
|
|
let inps = [VOID, lhs.id, rhs.id];
|
|
|
|
Some(self.ci.nodes.new_node_lit(ty::bin_ret(ty, op), Kind::BinOp { op }, inps))
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr::Directive { name: "sizeof", args: [ty], .. } => {
|
|
|
|
let ty = self.ty(ty);
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
ty::Id::INT,
|
2024-09-28 14:56:39 -05:00
|
|
|
Kind::CInt { value: self.tys.size_of(ty) as _ },
|
|
|
|
[VOID],
|
|
|
|
))
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-10-13 05:25:12 -05:00
|
|
|
Expr::Call { func: &Expr::Ident { pos, id, .. }, args, .. } => {
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.call_count += 1;
|
2024-10-13 05:25:12 -05:00
|
|
|
let cfile = self.cfile().clone();
|
|
|
|
let func = self.find_or_declare(pos, self.ci.file, Some(id), cfile.ident_str(id));
|
2024-09-28 14:56:39 -05:00
|
|
|
let ty::Kind::Func(func) = func else {
|
2024-09-28 08:13:32 -05:00
|
|
|
self.report(
|
|
|
|
pos,
|
2024-09-28 14:56:39 -05:00
|
|
|
fa!("compiler cant (yet) call '{}'", self.ty_display(func.compress())),
|
2024-09-28 08:13:32 -05:00
|
|
|
);
|
2024-10-17 12:32:10 -05:00
|
|
|
return Value::NEVER;
|
2024-09-27 09:53:28 -05:00
|
|
|
};
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
self.make_func_reachable(func);
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = &self.tys.ins.funcs[func as usize];
|
2024-09-28 14:56:39 -05:00
|
|
|
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-05 04:16:11 -05:00
|
|
|
};
|
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
self.assert_report(
|
|
|
|
args.len() == cargs.len(),
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"expected {} function argumenr{}, got {}",
|
|
|
|
cargs.len(),
|
|
|
|
if cargs.len() == 1 { "" } else { "s" },
|
|
|
|
args.len()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut inps = Vc::from([self.ci.ctrl]);
|
|
|
|
for ((arg, carg), tyx) in args.iter().zip(cargs).zip(sig.args.range()) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let ty = self.tys.ins.args[tyx];
|
2024-09-28 14:56:39 -05:00
|
|
|
if self.tys.size_of(ty) == 0 {
|
|
|
|
continue;
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-10-04 14:44:29 -05:00
|
|
|
let value = self.expr_ctx(arg, Ctx::default().with_ty(ty))?;
|
2024-09-28 14:56:39 -05:00
|
|
|
_ = self.assert_ty(
|
|
|
|
arg.pos(),
|
2024-10-17 12:32:10 -05:00
|
|
|
value.ty,
|
2024-09-28 14:56:39 -05:00
|
|
|
ty,
|
|
|
|
true,
|
|
|
|
fa!("argument {}", carg.name),
|
|
|
|
);
|
2024-10-17 12:32:10 -05:00
|
|
|
|
|
|
|
inps.push(value.id);
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
if let Some(str) = self.ci.scope.store {
|
2024-10-17 15:29:09 -05:00
|
|
|
inps.push(str);
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
for load in self.ci.scope.loads.drain(..) {
|
2024-10-17 15:29:09 -05:00
|
|
|
if !self.ci.nodes.unlock_remove(load) {
|
|
|
|
inps.push(load);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 02:52:50 -05:00
|
|
|
self.store_mem(VOID, VOID);
|
2024-10-18 06:11:11 -05:00
|
|
|
for load in self.ci.scope.loads.drain(..) {
|
2024-10-17 15:29:09 -05:00
|
|
|
if !self.ci.nodes.unlock_remove(load) {
|
|
|
|
inps.push(load);
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(sig.ret, Kind::Call { func }, inps);
|
2024-10-04 14:44:29 -05:00
|
|
|
for c in self.ci.clobbered.drain(..) {
|
|
|
|
self.ci.nodes[self.ci.ctrl].inputs.push(c);
|
|
|
|
self.ci.nodes[c].outputs.push(self.ci.ctrl);
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(Value::new(self.ci.ctrl).ty(sig.ret))
|
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
Expr::Ctor { pos, ty, fields, .. } => {
|
|
|
|
let Some(sty) = ty.map(|ty| self.ty(ty)).or(ctx.ty) else {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
"the type of struct cannot be inferred from context, \
|
|
|
|
use an explicit type instead: <type>.{ ... }",
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
2024-10-17 12:32:10 -05:00
|
|
|
|
2024-10-17 15:29:09 -05:00
|
|
|
let ty::Kind::Struct(s) = sty.expand() else {
|
|
|
|
let inferred = if ty.is_some() { "" } else { "inferred " };
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"the {inferred}type of the constructor is `{}`, \
|
|
|
|
but thats not a struct",
|
|
|
|
self.ty_display(sty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: dont allocate
|
|
|
|
let mut offs = OffsetIter::new(s, &self.tys)
|
|
|
|
.into_iter(&self.tys)
|
|
|
|
.map(|(f, o)| (f.ty, o))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mem = self.ci.nodes.new_node(sty, Kind::Stck, [VOID, MEM]);
|
|
|
|
for field in fields {
|
|
|
|
let Some(index) = self.tys.find_struct_field(s, field.name) else {
|
|
|
|
self.report(
|
|
|
|
field.pos,
|
|
|
|
fa!("struct '{}' does not have this field", self.ty_display(sty)),
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
let (ty, offset) =
|
|
|
|
core::mem::replace(&mut offs[index], (ty::Id::UNDECLARED, field.pos));
|
|
|
|
|
|
|
|
if ty == ty::Id::UNDECLARED {
|
|
|
|
self.report(field.pos, "the struct field is already initialized");
|
|
|
|
self.report(offset, "previous initialization is here");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = self.expr_ctx(&field.value, Ctx::default().with_ty(ty))?;
|
2024-10-18 06:11:11 -05:00
|
|
|
let mem = self.offset(mem, ty, offset);
|
|
|
|
self.store_mem(mem, value.id);
|
2024-10-17 15:29:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let field_list = self
|
|
|
|
.tys
|
|
|
|
.struct_fields(s)
|
|
|
|
.iter()
|
|
|
|
.zip(offs)
|
|
|
|
.filter(|&(_, (ty, _))| ty != ty::Id::UNDECLARED)
|
|
|
|
.map(|(f, _)| self.tys.names.ident_str(f.name))
|
|
|
|
.intersperse(", ")
|
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
if !field_list.is_empty() {
|
|
|
|
self.report(pos, fa!("the struct initializer is missing {field_list}"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Value::ptr(mem).ty(sty))
|
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr::Block { stmts, .. } => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let base = self.ci.scope.vars.len();
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let mut ret = Some(Value::VOID);
|
2024-09-28 14:56:39 -05:00
|
|
|
for stmt in stmts {
|
|
|
|
ret = ret.and(self.expr(stmt));
|
|
|
|
if let Some(id) = ret {
|
2024-10-17 12:32:10 -05:00
|
|
|
_ = self.assert_ty(stmt.pos(), id.ty, ty::Id::VOID, true, "statement");
|
2024-09-28 14:56:39 -05:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.nodes.lock(self.ci.ctrl);
|
2024-10-18 06:11:11 -05:00
|
|
|
for var in self.ci.scope.vars.drain(base..) {
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.unlock_remove(var.value.id);
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
self.ci.nodes.unlock(self.ci.ctrl);
|
|
|
|
|
|
|
|
ret
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
Expr::Loop { body, .. } => {
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::Id::VOID, Kind::Loop, [self.ci.ctrl; 2]);
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.loops.push(Loop {
|
|
|
|
node: self.ci.ctrl,
|
2024-09-08 10:11:33 -05:00
|
|
|
ctrl: [Nid::MAX; 2],
|
2024-10-18 06:11:11 -05:00
|
|
|
ctrl_scope: core::array::from_fn(|_| Default::default()),
|
|
|
|
scope: self.ci.scope.clone(),
|
2024-09-05 18:17:54 -05:00
|
|
|
});
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
for var in &mut self.ci.scope.vars {
|
2024-10-17 12:32:10 -05:00
|
|
|
var.value = Value::VOID;
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.lock_scope(&self.ci.scope);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
|
|
|
self.expr(body);
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
let Loop {
|
|
|
|
node,
|
|
|
|
ctrl: [mut con, bre],
|
|
|
|
ctrl_scope: [mut cons, mut bres],
|
|
|
|
mut scope,
|
|
|
|
} = self.ci.loops.pop().unwrap();
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
if con != Nid::MAX {
|
2024-10-17 12:32:10 -05:00
|
|
|
con = self.ci.nodes.new_node(ty::Id::VOID, Kind::Region, [con, self.ci.ctrl]);
|
2024-09-15 13:14:56 -05:00
|
|
|
Self::merge_scopes(
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
&mut self.ci.loops,
|
|
|
|
con,
|
2024-10-18 06:11:11 -05:00
|
|
|
&mut self.ci.scope,
|
2024-09-15 13:14:56 -05:00
|
|
|
&mut cons,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
self.ci.ctrl = con;
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.nodes.modify_input(node, 1, self.ci.ctrl);
|
|
|
|
|
2024-09-12 11:42:21 -05:00
|
|
|
let idx = self.ci.nodes[node]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.position(|&n| self.ci.nodes.is_cfg(n))
|
|
|
|
.unwrap();
|
|
|
|
self.ci.nodes[node].outputs.swap(idx, 0);
|
|
|
|
|
2024-09-08 10:11:33 -05:00
|
|
|
if bre == Nid::MAX {
|
2024-09-16 08:49:27 -05:00
|
|
|
self.ci.ctrl = NEVER;
|
2024-09-05 18:17:54 -05:00
|
|
|
return None;
|
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
self.ci.ctrl = bre;
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.lock(self.ci.ctrl);
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
core::mem::swap(&mut self.ci.scope, &mut bres);
|
2024-09-06 15:00:23 -05:00
|
|
|
|
2024-09-08 05:00:07 -05:00
|
|
|
for ((dest_var, mut scope_var), loop_var) in
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.vars.iter_mut().zip(scope.vars.drain(..)).zip(bres.vars.drain(..))
|
2024-09-06 15:00:23 -05:00
|
|
|
{
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.unlock(loop_var.value.id);
|
|
|
|
|
|
|
|
if loop_var.value.id != VOID {
|
|
|
|
self.ci.nodes.unlock(scope_var.value.id);
|
|
|
|
if loop_var.value.id != scope_var.value.id {
|
|
|
|
scope_var.value.id = self.ci.nodes.modify_input(
|
|
|
|
scope_var.value.id,
|
|
|
|
2,
|
|
|
|
loop_var.value.id,
|
|
|
|
);
|
|
|
|
self.ci.nodes.lock(scope_var.value.id);
|
2024-09-08 05:00:07 -05:00
|
|
|
} else {
|
2024-10-17 12:32:10 -05:00
|
|
|
if dest_var.value.id == scope_var.value.id {
|
|
|
|
self.ci.nodes.unlock(dest_var.value.id);
|
|
|
|
dest_var.value.id = VOID;
|
|
|
|
self.ci.nodes.lock(dest_var.value.id);
|
2024-09-22 11:17:30 -05:00
|
|
|
}
|
2024-10-17 12:32:10 -05:00
|
|
|
let phi = &self.ci.nodes[scope_var.value.id];
|
2024-09-08 05:00:07 -05:00
|
|
|
debug_assert_eq!(phi.kind, Kind::Phi);
|
2024-09-16 08:49:27 -05:00
|
|
|
debug_assert_eq!(phi.inputs[2], VOID);
|
2024-10-17 15:29:09 -05:00
|
|
|
debug_assert_eq!(phi.ty, scope_var.value.ty);
|
2024-09-08 05:00:07 -05:00
|
|
|
let prev = phi.inputs[1];
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.replace(scope_var.value.id, prev);
|
|
|
|
scope_var.value.id = prev;
|
2024-09-08 05:00:07 -05:00
|
|
|
self.ci.nodes.lock(prev);
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
if dest_var.value.id == VOID {
|
|
|
|
self.ci.nodes.unlock(dest_var.value.id);
|
2024-10-17 15:29:09 -05:00
|
|
|
dest_var.value = scope_var.value;
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.lock(dest_var.value.id);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
2024-09-22 11:17:30 -05:00
|
|
|
debug_assert!(
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes[dest_var.value.id].kind != Kind::Phi
|
|
|
|
|| self.ci.nodes[dest_var.value.id].inputs[2] != 0
|
2024-09-22 11:17:30 -05:00
|
|
|
);
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.nodes.unlock_remove(scope_var.value.id);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.unlock_remove_scope(&scope);
|
|
|
|
self.ci.nodes.unlock_remove_scope(&bres);
|
|
|
|
|
2024-09-07 20:12:57 -05:00
|
|
|
self.ci.nodes.unlock(self.ci.ctrl);
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(Value::VOID)
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-09-08 10:11:33 -05:00
|
|
|
Expr::Break { pos } => self.jump_to(pos, 1),
|
|
|
|
Expr::Continue { pos } => self.jump_to(pos, 0),
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr::If { cond, then, else_, .. } => {
|
|
|
|
let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?;
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let if_node =
|
|
|
|
self.ci.nodes.new_node(ty::Id::VOID, Kind::If, [self.ci.ctrl, cond.id]);
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
'b: {
|
|
|
|
let branch = match self.tof(if_node).expand().inner() {
|
|
|
|
ty::LEFT_UNREACHABLE => else_,
|
|
|
|
ty::RIGHT_UNREACHABLE => Some(then),
|
|
|
|
_ => break 'b,
|
|
|
|
};
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.nodes.lock(self.ci.ctrl);
|
|
|
|
self.ci.nodes.remove(if_node);
|
|
|
|
self.ci.nodes.unlock(self.ci.ctrl);
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
if let Some(branch) = branch {
|
|
|
|
return self.expr(branch);
|
|
|
|
} else {
|
2024-10-17 12:32:10 -05:00
|
|
|
return Some(Value::VOID);
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
let mut else_scope = self.ci.scope.clone();
|
|
|
|
self.ci.nodes.lock_scope(&else_scope);
|
2024-09-28 14:56:39 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::Id::VOID, Kind::Then, [if_node]);
|
2024-09-28 14:56:39 -05:00
|
|
|
let lcntrl = self.expr(then).map_or(Nid::MAX, |_| self.ci.ctrl);
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
let mut then_scope = core::mem::replace(&mut self.ci.scope, else_scope);
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::Id::VOID, Kind::Else, [if_node]);
|
2024-09-28 14:56:39 -05:00
|
|
|
let rcntrl = if let Some(else_) = else_ {
|
|
|
|
self.expr(else_).map_or(Nid::MAX, |_| self.ci.ctrl)
|
2024-09-03 10:51:28 -05:00
|
|
|
} else {
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.ctrl
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
if lcntrl == Nid::MAX && rcntrl == Nid::MAX {
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.unlock_remove_scope(&then_scope);
|
2024-09-28 14:56:39 -05:00
|
|
|
return None;
|
|
|
|
} else if lcntrl == Nid::MAX {
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.unlock_remove_scope(&then_scope);
|
2024-10-17 12:32:10 -05:00
|
|
|
return Some(Value::VOID);
|
2024-09-28 14:56:39 -05:00
|
|
|
} else if rcntrl == Nid::MAX {
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.unlock_remove_scope(&self.ci.scope);
|
|
|
|
self.ci.scope = then_scope;
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.ctrl = lcntrl;
|
2024-10-17 12:32:10 -05:00
|
|
|
return Some(Value::VOID);
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::Id::VOID, Kind::Region, [lcntrl, rcntrl]);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
else_scope = core::mem::take(&mut self.ci.scope);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
Self::merge_scopes(
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
&mut self.ci.loops,
|
|
|
|
self.ci.ctrl,
|
|
|
|
&mut else_scope,
|
|
|
|
&mut then_scope,
|
|
|
|
true,
|
|
|
|
);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope = else_scope;
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(Value::VOID)
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
ref e => {
|
|
|
|
self.report_unhandled_ast(e, "bruh");
|
2024-10-17 12:32:10 -05:00
|
|
|
Value::NEVER
|
2024-09-06 11:50:28 -05:00
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn expr_ctx(&mut self, expr: &Expr, ctx: Ctx) -> Option<Value> {
|
|
|
|
let mut n = self.raw_expr_ctx(expr, ctx)?;
|
|
|
|
self.strip_var(&mut n);
|
|
|
|
if core::mem::take(&mut n.ptr) {
|
2024-10-18 02:52:50 -05:00
|
|
|
n.id = self.load_mem(n.id, n.ty);
|
2024-10-04 14:44:29 -05:00
|
|
|
}
|
|
|
|
Some(n)
|
|
|
|
}
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
fn offset(&mut self, val: Nid, ty: ty::Id, off: Offset) -> Nid {
|
|
|
|
if off == 0 {
|
|
|
|
return val;
|
2024-10-18 02:52:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
let off = self.ci.nodes.new_node_nop(ty::Id::INT, Kind::CInt { value: off as i64 }, [VOID]);
|
|
|
|
let inps = [VOID, val, off];
|
|
|
|
self.ci.nodes.new_node(ty, Kind::BinOp { op: TokenKind::Add }, inps)
|
2024-10-18 02:52:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn strip_var(&mut self, n: &mut Value) {
|
|
|
|
if core::mem::take(&mut n.var) {
|
2024-10-17 15:29:09 -05:00
|
|
|
let id = (u16::MAX - n.id) as usize;
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.load_loop_value(
|
|
|
|
id,
|
|
|
|
&mut self.ci.scope.vars[id].value,
|
|
|
|
&mut self.ci.loops,
|
|
|
|
);
|
|
|
|
*n = self.ci.scope.vars[id].value;
|
2024-10-17 12:32:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr(&mut self, expr: &Expr) -> Option<Value> {
|
2024-10-04 14:44:29 -05:00
|
|
|
self.expr_ctx(expr, Default::default())
|
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn jump_to(&mut self, pos: Pos, id: usize) -> Option<Value> {
|
2024-09-15 13:14:56 -05:00
|
|
|
let Some(mut loob) = self.ci.loops.last_mut() else {
|
2024-09-08 10:11:33 -05:00
|
|
|
self.report(pos, "break outside a loop");
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
if loob.ctrl[id] == Nid::MAX {
|
|
|
|
loob.ctrl[id] = self.ci.ctrl;
|
2024-10-18 06:11:11 -05:00
|
|
|
loob.ctrl_scope[id] = self.ci.scope.clone();
|
|
|
|
loob.ctrl_scope[id].vars.truncate(loob.scope.vars.len());
|
|
|
|
self.ci.nodes.lock_scope(&loob.ctrl_scope[id]);
|
2024-09-08 10:11:33 -05:00
|
|
|
} else {
|
2024-10-17 12:32:10 -05:00
|
|
|
let reg =
|
|
|
|
self.ci.nodes.new_node(ty::Id::VOID, Kind::Region, [self.ci.ctrl, loob.ctrl[id]]);
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut scope = core::mem::take(&mut loob.ctrl_scope[id]);
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
Self::merge_scopes(
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
&mut self.ci.loops,
|
|
|
|
reg,
|
|
|
|
&mut scope,
|
2024-10-18 06:11:11 -05:00
|
|
|
&mut self.ci.scope,
|
2024-09-15 13:14:56 -05:00
|
|
|
false,
|
|
|
|
);
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
loob = self.ci.loops.last_mut().unwrap();
|
|
|
|
loob.ctrl_scope[id] = scope;
|
|
|
|
loob.ctrl[id] = reg;
|
2024-09-08 10:11:33 -05:00
|
|
|
}
|
|
|
|
|
2024-09-16 08:49:27 -05:00
|
|
|
self.ci.ctrl = NEVER;
|
2024-09-08 10:11:33 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn merge_scopes(
|
|
|
|
nodes: &mut Nodes,
|
|
|
|
loops: &mut [Loop],
|
|
|
|
ctrl: Nid,
|
2024-10-18 06:11:11 -05:00
|
|
|
to: &mut Scope,
|
|
|
|
from: &mut Scope,
|
2024-09-15 13:14:56 -05:00
|
|
|
drop_from: bool,
|
|
|
|
) {
|
2024-10-18 06:11:11 -05:00
|
|
|
for (i, (to_var, from_var)) in to.vars.iter_mut().zip(&mut from.vars).enumerate() {
|
|
|
|
if to_var.value.id != from_var.value.id {
|
|
|
|
nodes.load_loop_value(i, &mut from_var.value, loops);
|
|
|
|
nodes.load_loop_value(i, &mut to_var.value, loops);
|
|
|
|
if to_var.value.id != from_var.value.id {
|
|
|
|
let ty = nodes[to_var.value.id].ty;
|
|
|
|
debug_assert_eq!(ty, nodes[from_var.value.id].ty, "TODO: typecheck properly");
|
|
|
|
|
|
|
|
let inps = [ctrl, from_var.value.id, to_var.value.id];
|
|
|
|
nodes.unlock(to_var.value.id);
|
|
|
|
to_var.value.id = nodes.new_node(ty, Kind::Phi, inps);
|
|
|
|
nodes.lock(to_var.value.id);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
if to.store != from.store {
|
|
|
|
let (to_store, from_store) = (to.store.unwrap(), from.store.unwrap());
|
|
|
|
nodes.unlock(to_store);
|
|
|
|
to.store = Some(nodes.new_node(ty::Id::VOID, Kind::Phi, [ctrl, from_store, to_store]));
|
|
|
|
nodes.lock(to.store.unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
to.loads.extend(&from.loads);
|
|
|
|
for &load in &from.loads {
|
|
|
|
nodes.lock(load);
|
|
|
|
}
|
|
|
|
|
|
|
|
if drop_from {
|
|
|
|
nodes.unlock_remove_scope(from);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
while self.ci.task_base < self.tasks.len()
|
|
|
|
&& let Some(task_slot) = self.tasks.pop()
|
|
|
|
{
|
|
|
|
let Some(task) = task_slot else { continue };
|
2024-09-15 13:14:56 -05:00
|
|
|
self.emit_func(task);
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn emit_func(&mut self, FTask { file, id }: FTask) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let func = &mut self.tys.ins.funcs[id as usize];
|
2024-09-05 04:16:11 -05:00
|
|
|
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()
|
|
|
|
};
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_ci = core::mem::replace(&mut self.ci, repl);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let start = self.ci.nodes.new_node(ty::Id::VOID, Kind::Start, []);
|
2024-09-16 08:49:27 -05:00
|
|
|
debug_assert_eq!(start, VOID);
|
2024-10-17 12:32:10 -05:00
|
|
|
let end = self.ci.nodes.new_node(ty::Id::NEVER, Kind::End, []);
|
2024-09-16 08:49:27 -05:00
|
|
|
debug_assert_eq!(end, NEVER);
|
2024-09-30 12:09:17 -05:00
|
|
|
self.ci.nodes.lock(end);
|
2024-10-17 12:32:10 -05:00
|
|
|
self.ci.ctrl = self.ci.nodes.new_node(ty::Id::VOID, Kind::Entry, [VOID]);
|
2024-09-27 09:53:28 -05:00
|
|
|
debug_assert_eq!(self.ci.ctrl, ENTRY);
|
2024-10-17 12:32:10 -05:00
|
|
|
let mem = self.ci.nodes.new_node(ty::Id::VOID, Kind::Mem, [VOID]);
|
2024-09-27 09:53:28 -05:00
|
|
|
debug_assert_eq!(mem, MEM);
|
2024-09-30 12:09:17 -05:00
|
|
|
self.ci.nodes.lock(mem);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
|
|
|
let Expr::BinOp {
|
2024-09-14 04:26:54 -05:00
|
|
|
left: Expr::Ident { .. },
|
2024-09-03 10:51:28 -05:00
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: &Expr::Closure { body, args, .. },
|
|
|
|
} = expr
|
|
|
|
else {
|
2024-09-30 12:09:17 -05:00
|
|
|
unreachable!("{}", self.ast_display(expr))
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut sig_args = sig.args.range();
|
2024-09-20 12:01:44 -05:00
|
|
|
for (arg, index) in args.iter().zip(0u32..) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let ty = self.tys.ins.args[sig_args.next().unwrap()];
|
2024-09-20 12:01:44 -05:00
|
|
|
let value = self.ci.nodes.new_node(ty, Kind::Arg { index }, [VOID]);
|
2024-09-03 10:51:28 -05:00
|
|
|
self.ci.nodes.lock(value);
|
|
|
|
let sym = parser::find_symbol(&ast.symbols, arg.id);
|
|
|
|
assert!(sym.flags & idfl::COMPTIME == 0, "TODO");
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.vars.push(Variable { id: arg.id, value: Value::new(value).ty(ty) });
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
let orig_vars = self.ci.scope.vars.clone();
|
2024-09-05 18:17:54 -05:00
|
|
|
|
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-30 12:09:17 -05:00
|
|
|
self.ci.nodes.unlock(end);
|
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.nodes.unlock_remove_scope(&core::mem::take(&mut self.ci.scope));
|
2024-09-30 12:09:17 -05:00
|
|
|
self.ci.nodes.unlock(mem);
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
if self.errors.borrow().is_empty() {
|
2024-10-04 14:44:29 -05:00
|
|
|
self.graphviz();
|
2024-09-08 10:11:33 -05:00
|
|
|
self.gcm();
|
|
|
|
|
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
|
|
|
'_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-22 11:17:30 -05:00
|
|
|
//self.ci.nodes.basic_blocks();
|
2024-10-04 14:44:29 -05:00
|
|
|
self.graphviz();
|
2024-09-30 12:09:17 -05:00
|
|
|
|
|
|
|
let mut stack_size = 0;
|
|
|
|
'_compute_stack: {
|
|
|
|
let mems = core::mem::take(&mut self.ci.nodes[MEM].outputs);
|
|
|
|
for &stck in mems.iter() {
|
|
|
|
stack_size += self.tys.size_of(self.ci.nodes[stck].ty);
|
|
|
|
self.ci.nodes[stck].offset = stack_size;
|
|
|
|
}
|
|
|
|
for &stck in mems.iter() {
|
|
|
|
self.ci.nodes[stck].offset = stack_size - self.ci.nodes[stck].offset;
|
|
|
|
}
|
|
|
|
self.ci.nodes[mem].outputs = mems;
|
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.vars = orig_vars;
|
2024-09-14 04:26:54 -05:00
|
|
|
self.ci.nodes.visited.clear(self.ci.nodes.values.len());
|
2024-09-19 06:40:03 -05:00
|
|
|
let saved = self.emit_body(sig);
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope.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-15 13:14:56 -05:00
|
|
|
// FIXME: maybe do this incrementally
|
|
|
|
for (nd, rel) in self.ci.jump_relocs.drain(..) {
|
|
|
|
let offset = self.ci.nodes[nd].offset;
|
|
|
|
rel.apply_jump(&mut self.ci.code, offset, 0);
|
|
|
|
}
|
|
|
|
|
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-28 09:28:05 -05:00
|
|
|
let mut stripped_prelude_size = 0;
|
2024-09-06 11:50:28 -05:00
|
|
|
'_close_function: {
|
2024-09-28 09:28:05 -05:00
|
|
|
let pushed =
|
2024-09-30 12:09:17 -05:00
|
|
|
(saved as i64 + (core::mem::take(&mut self.ci.call_count) != 0) as i64) * 8;
|
|
|
|
let stack = stack_size as i64;
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-28 09:28:05 -05:00
|
|
|
match (pushed, stack) {
|
|
|
|
(0, 0) => {
|
|
|
|
stripped_prelude_size =
|
|
|
|
instrs::addi64(0, 0, 0).0 + instrs::st(0, 0, 0, 0).0;
|
|
|
|
self.ci.code.drain(0..stripped_prelude_size);
|
|
|
|
break '_close_function;
|
|
|
|
}
|
|
|
|
(0, stack) => {
|
|
|
|
write_reloc(&mut self.ci.code, 3, -stack, 8);
|
|
|
|
stripped_prelude_size = instrs::addi64(0, 0, 0).0;
|
|
|
|
let end = stripped_prelude_size + instrs::st(0, 0, 0, 0).0;
|
|
|
|
self.ci.code.drain(stripped_prelude_size..end);
|
|
|
|
self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, stack as _));
|
|
|
|
break '_close_function;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
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 _));
|
|
|
|
}
|
2024-09-28 09:28:05 -05:00
|
|
|
self.ci.relocs.iter_mut().for_each(|r| r.reloc.offset -= stripped_prelude_size as u32);
|
|
|
|
self.ci.emit(instrs::jala(reg::ZERO, reg::RET_ADDR, 0));
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.ins.funcs[id as usize].code.append(&mut self.ci.code);
|
|
|
|
self.tys.ins.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.filled.clear();
|
2024-09-30 12:09:17 -05:00
|
|
|
self.pool.cis.push(core::mem::replace(&mut self.ci, prev_ci));
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-09-19 06:40:03 -05:00
|
|
|
fn emit_body(&mut self, sig: Sig) -> usize {
|
2024-09-30 12:09:17 -05:00
|
|
|
let mut nodes = core::mem::take(&mut self.ci.nodes);
|
2024-09-19 06:40:03 -05:00
|
|
|
|
|
|
|
let func = Function::new(&mut nodes, &self.tys, sig);
|
2024-10-17 15:29:09 -05:00
|
|
|
log::info!("{:?}", func);
|
2024-09-27 09:53:28 -05:00
|
|
|
if self.ci.call_count != 0 {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(
|
2024-09-27 09:53:28 -05:00
|
|
|
&mut self.ralloc.env.preferred_regs_by_class,
|
|
|
|
&mut self.ralloc.env.non_preferred_regs_by_class,
|
|
|
|
);
|
2024-09-19 06:40:03 -05:00
|
|
|
};
|
2024-09-27 09:53:28 -05:00
|
|
|
|
|
|
|
let options = regalloc2::RegallocOptions {
|
|
|
|
verbose_log: false,
|
|
|
|
validate_ssa: false,
|
|
|
|
algorithm: regalloc2::Algorithm::Ion,
|
|
|
|
};
|
|
|
|
regalloc2::run_with_ctx(&func, &self.ralloc.env, &options, &mut self.ralloc.ctx)
|
|
|
|
.unwrap_or_else(|err| panic!("{err}"));
|
|
|
|
|
2024-09-20 04:01:10 -05:00
|
|
|
if self.ci.call_count != 0 {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(
|
2024-09-27 09:53:28 -05:00
|
|
|
&mut self.ralloc.env.preferred_regs_by_class,
|
|
|
|
&mut self.ralloc.env.non_preferred_regs_by_class,
|
|
|
|
);
|
2024-09-20 04:01:10 -05:00
|
|
|
};
|
2024-09-19 06:40:03 -05:00
|
|
|
|
2024-09-27 09:53:28 -05:00
|
|
|
let mut saved_regs = HashMap::<u8, u8>::default();
|
|
|
|
let mut atr = |allc: regalloc2::Allocation| {
|
|
|
|
debug_assert!(allc.is_reg());
|
|
|
|
let hvenc = regalloc2::PReg::from_index(allc.index()).hw_enc() as u8;
|
|
|
|
if hvenc <= 12 {
|
|
|
|
return hvenc;
|
|
|
|
}
|
|
|
|
let would_insert = saved_regs.len() as u8 + reg::RET_ADDR + 1;
|
|
|
|
*saved_regs.entry(hvenc).or_insert(would_insert)
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i, block) in func.blocks.iter().enumerate() {
|
|
|
|
let blk = regalloc2::Block(i as _);
|
|
|
|
func.nodes[block.nid].offset = self.ci.code.len() as _;
|
|
|
|
for instr_or_edit in self.ralloc.ctx.output.block_insts_and_edits(&func, blk) {
|
|
|
|
let inst = match instr_or_edit {
|
|
|
|
regalloc2::InstOrEdit::Inst(inst) => inst,
|
|
|
|
regalloc2::InstOrEdit::Edit(®alloc2::Edit::Move { from, to }) => {
|
|
|
|
self.ci.emit(instrs::cp(atr(to), atr(from)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let nid = func.instrs[inst.index()].nid;
|
|
|
|
if nid == NEVER {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let allocs = self.ralloc.ctx.output.inst_allocs(inst);
|
|
|
|
let node = &func.nodes[nid];
|
|
|
|
match node.kind {
|
|
|
|
Kind::If => {
|
|
|
|
let &[_, cond] = node.inputs.as_slice() else { unreachable!() };
|
|
|
|
if let Kind::BinOp { op } = func.nodes[cond].kind
|
|
|
|
&& let Some((op, swapped)) = op.cond_op(node.ty.is_signed())
|
|
|
|
{
|
|
|
|
let rel = Reloc::new(self.ci.code.len(), 3, 2);
|
|
|
|
self.ci.jump_relocs.push((node.outputs[!swapped as usize], rel));
|
|
|
|
let &[lhs, rhs] = allocs else { unreachable!() };
|
|
|
|
self.ci.emit(op(atr(lhs), atr(rhs), 0));
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Loop | Kind::Region => {
|
|
|
|
if node.ralloc_backref as usize != i + 1 {
|
|
|
|
let rel = Reloc::new(self.ci.code.len(), 1, 4);
|
|
|
|
self.ci.jump_relocs.push((nid, rel));
|
|
|
|
self.ci.emit(instrs::jmp(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Return => {
|
|
|
|
if i != func.blocks.len() - 1 {
|
|
|
|
let rel = Reloc::new(self.ci.code.len(), 1, 4);
|
|
|
|
self.ci.ret_relocs.push(rel);
|
|
|
|
self.ci.emit(instrs::jmp(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::CInt { value } => {
|
|
|
|
self.ci.emit(instrs::li64(atr(allocs[0]), value as _));
|
|
|
|
}
|
|
|
|
Kind::UnOp { op } => {
|
|
|
|
let op = op.unop().expect("TODO: unary operator not supported");
|
|
|
|
let &[dst, oper] = allocs else { unreachable!() };
|
|
|
|
self.ci.emit(op(atr(dst), atr(oper)));
|
|
|
|
}
|
|
|
|
Kind::BinOp { op } => {
|
|
|
|
let &[.., rhs] = node.inputs.as_slice() else { unreachable!() };
|
2024-09-19 06:40:03 -05:00
|
|
|
|
2024-09-27 09:53:28 -05:00
|
|
|
if let Kind::CInt { value } = func.nodes[rhs].kind
|
2024-10-18 06:11:11 -05:00
|
|
|
&& func.nodes[rhs].lock_rc != 0
|
2024-09-27 09:53:28 -05:00
|
|
|
&& let Some(op) =
|
|
|
|
op.imm_binop(node.ty.is_signed(), func.tys.size_of(node.ty))
|
|
|
|
{
|
|
|
|
let &[dst, lhs] = allocs else { unreachable!() };
|
|
|
|
self.ci.emit(op(atr(dst), atr(lhs), value as _));
|
|
|
|
} else if let Some(op) =
|
|
|
|
op.binop(node.ty.is_signed(), func.tys.size_of(node.ty))
|
|
|
|
{
|
|
|
|
let &[dst, lhs, rhs] = allocs else { unreachable!() };
|
|
|
|
self.ci.emit(op(atr(dst), atr(lhs), atr(rhs)));
|
|
|
|
} else if op.cond_op(node.ty.is_signed()).is_some() {
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Call { func } => {
|
|
|
|
self.ci.relocs.push(TypedReloc {
|
|
|
|
target: ty::Kind::Func(func).compress(),
|
|
|
|
reloc: Reloc::new(self.ci.code.len(), 3, 4),
|
|
|
|
});
|
|
|
|
self.ci.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0));
|
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
Kind::Stck => {
|
|
|
|
let base = reg::STACK_PTR;
|
2024-09-30 12:09:17 -05:00
|
|
|
let offset = func.nodes[nid].offset;
|
2024-09-28 08:13:32 -05:00
|
|
|
self.ci.emit(instrs::addi64(atr(allocs[0]), base, offset as _));
|
|
|
|
}
|
2024-10-18 02:52:50 -05:00
|
|
|
Kind::Load => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let mut region = node.inputs[1];
|
|
|
|
let mut offset = 0;
|
|
|
|
if func.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add })
|
|
|
|
&& let Kind::CInt { value } =
|
|
|
|
func.nodes[func.nodes[region].inputs[2]].kind
|
|
|
|
{
|
|
|
|
region = func.nodes[region].inputs[1];
|
|
|
|
offset = value as Offset;
|
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
let size = self.tys.size_of(node.ty);
|
2024-10-17 15:29:09 -05:00
|
|
|
if size <= 8 {
|
|
|
|
let (base, offset) = match func.nodes[region].kind {
|
2024-10-18 06:11:11 -05:00
|
|
|
Kind::Stck => (reg::STACK_PTR, func.nodes[region].offset + offset),
|
|
|
|
_ => (atr(allocs[1]), offset),
|
2024-10-17 15:29:09 -05:00
|
|
|
};
|
|
|
|
self.ci.emit(instrs::ld(atr(allocs[0]), base, offset as _, size as _));
|
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
Kind::Stre if node.inputs[2] == VOID => {}
|
2024-10-18 02:52:50 -05:00
|
|
|
Kind::Stre => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let mut region = node.inputs[2];
|
|
|
|
let mut offset = 0;
|
|
|
|
if func.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add })
|
|
|
|
&& let Kind::CInt { value } =
|
|
|
|
func.nodes[func.nodes[region].inputs[2]].kind
|
|
|
|
{
|
|
|
|
region = func.nodes[region].inputs[1];
|
|
|
|
offset = value as Offset;
|
|
|
|
}
|
|
|
|
let size = u16::try_from(self.tys.size_of(node.ty)).expect("TODO");
|
|
|
|
let nd = &func.nodes[region];
|
|
|
|
let (base, offset, src) = match nd.kind {
|
|
|
|
Kind::Stck => (reg::STACK_PTR, nd.offset + offset, allocs[0]),
|
|
|
|
_ => (atr(allocs[0]), offset, allocs[1]),
|
|
|
|
};
|
|
|
|
if size > 8 {
|
|
|
|
self.ci.emit(instrs::bmc(base, atr(src), size));
|
|
|
|
} else {
|
|
|
|
self.ci.emit(instrs::st(atr(src), base, offset as _, size));
|
2024-10-17 15:29:09 -05:00
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.nodes = nodes;
|
|
|
|
|
|
|
|
saved_regs.len()
|
2024-09-06 09:11:57 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
fn ty(&mut self, expr: &Expr) -> ty::Id {
|
2024-09-28 14:56:39 -05:00
|
|
|
if let Some(ty) = self.tys.ty(self.ci.file, expr, &self.files) {
|
|
|
|
return ty;
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
|
|
|
|
self.report_unhandled_ast(expr, "type");
|
2024-09-30 12:09:17 -05:00
|
|
|
ty::Id::NEVER
|
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-30 12:27:00 -05:00
|
|
|
log::trace!("find_or_declare: {lit_name} {file}");
|
2024-10-01 10:43:15 -05:00
|
|
|
if let Some(ty) = self.tys.find_type(file, name.ok_or(lit_name), &self.files) {
|
|
|
|
return ty.expand();
|
|
|
|
}
|
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);
|
2024-09-28 14:56:39 -05:00
|
|
|
self.report(pos, fa!("idk indentifier: {name}"))
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
Err("main") => self.report(
|
|
|
|
pos,
|
2024-09-28 14:56:39 -05:00
|
|
|
fa!(
|
2024-09-03 10:51:28 -05:00
|
|
|
"missing main function in '{}', compiler can't \
|
|
|
|
emmit libraries since such concept is not defined",
|
|
|
|
f.path
|
|
|
|
),
|
|
|
|
),
|
2024-09-28 14:56:39 -05:00
|
|
|
Err(name) => self.report(pos, fa!("idk indentifier: {name}")),
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-09-06 11:50:28 -05:00
|
|
|
return ty::Kind::Builtin(ty::NEVER);
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
|
|
|
|
2024-10-01 10:43:15 -05:00
|
|
|
let key = SymKey::Decl(file, ident);
|
2024-10-01 15:53:03 -05:00
|
|
|
if let Some(existing) = self.tys.syms.get(key, &self.tys.ins) {
|
2024-09-03 10:51:28 -05:00
|
|
|
if let ty::Kind::Func(id) = existing.expand()
|
2024-10-01 15:53:03 -05:00
|
|
|
&& let func = &mut self.tys.ins.funcs[id as usize]
|
2024-09-03 10:51:28 -05:00
|
|
|
&& 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();
|
|
|
|
}
|
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let prev_file = core::mem::replace(&mut self.ci.file, file);
|
2024-09-03 10:51:28 -05:00
|
|
|
let sym = match expr {
|
|
|
|
Expr::BinOp {
|
2024-10-01 15:53:03 -05:00
|
|
|
left: &Expr::Ident { id, .. },
|
2024-09-03 10:51:28 -05:00
|
|
|
op: TokenKind::Decl,
|
|
|
|
right: &Expr::Closure { pos, args, ret, .. },
|
|
|
|
} => {
|
|
|
|
let func = Func {
|
|
|
|
file,
|
2024-10-01 15:53:03 -05:00
|
|
|
name: id,
|
2024-09-03 10:51:28 -05:00
|
|
|
sig: '_b: {
|
2024-10-12 15:21:20 -05:00
|
|
|
let arg_base = self.tys.tmp.args.len();
|
2024-09-03 10:51:28 -05:00
|
|
|
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);
|
2024-10-12 15:21:20 -05:00
|
|
|
self.tys.tmp.args.push(ty);
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
let Some(args) = self.tys.pack_args(arg_base) else {
|
2024-09-06 11:50:28 -05:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let id = self.tys.ins.funcs.len() as _;
|
|
|
|
self.tys.ins.funcs.push(func);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
|
|
|
ty::Kind::Func(id)
|
|
|
|
}
|
|
|
|
Expr::BinOp {
|
2024-09-28 14:56:39 -05:00
|
|
|
left: Expr::Ident { .. },
|
2024-09-03 10:51:28 -05:00
|
|
|
op: TokenKind::Decl,
|
2024-09-28 14:56:39 -05:00
|
|
|
right: right @ Expr::Struct { .. },
|
|
|
|
} => self.ty(right).expand(),
|
2024-09-03 10:51:28 -05:00
|
|
|
e => unimplemented!("{e:#?}"),
|
|
|
|
};
|
|
|
|
self.ci.file = prev_file;
|
2024-10-01 15:53:03 -05:00
|
|
|
self.tys.syms.insert(key, sym.compress(), &self.tys.ins);
|
2024-09-03 10:51:28 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-01 08:28:18 -05:00
|
|
|
fn ast_display<'a>(&'a self, ast: &'a Expr<'a>) -> parser::Display<'a> {
|
|
|
|
parser::Display::new(&self.cfile().file, ast)
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[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-10-13 07:11:17 -05:00
|
|
|
if let Some(res) = ty.try_upcast(expected, ty::TyCheck::BinOp)
|
2024-09-03 10:51:28 -05:00
|
|
|
&& (!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-28 14:56:39 -05:00
|
|
|
self.report(pos, fa!("expected {hint} to be of type {expected}, got {ty}"));
|
2024-09-30 12:09:17 -05:00
|
|
|
ty::Id::NEVER
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
2024-09-30 12:09:17 -05:00
|
|
|
fn assert_report(&self, cond: bool, pos: Pos, msg: impl core::fmt::Display) {
|
2024-09-06 11:50:28 -05:00
|
|
|
if !cond {
|
|
|
|
self.report(pos, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
2024-09-30 12:09:17 -05:00
|
|
|
fn report(&self, pos: Pos, msg: impl core::fmt::Display) {
|
2024-10-10 12:01:12 -05:00
|
|
|
let mut buf = self.errors.borrow_mut();
|
|
|
|
writeln!(buf, "{}", self.cfile().report(pos, msg)).unwrap();
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
2024-09-06 11:50:28 -05:00
|
|
|
fn report_unhandled_ast(&self, ast: &Expr, hint: &str) {
|
2024-10-10 12:01:12 -05:00
|
|
|
log::debug!("{ast:#?}");
|
2024-10-17 12:32:10 -05:00
|
|
|
self.fatal_report(
|
2024-09-02 17:07:20 -05:00
|
|
|
ast.pos(),
|
2024-10-10 12:01:12 -05:00
|
|
|
fa!("compiler does not (yet) know how to handle ({hint}):\n{}", self.ast_display(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 fatal_report(&self, pos: Pos, msg: impl Display) -> ! {
|
|
|
|
self.report(pos, msg);
|
2024-09-30 12:09:17 -05:00
|
|
|
panic!("{}", self.errors.borrow());
|
2024-09-06 11:50:28 -05:00
|
|
|
}
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
fn gcm(&mut self) {
|
2024-09-15 13:14:56 -05:00
|
|
|
self.ci.nodes.visited.clear(self.ci.nodes.values.len());
|
2024-09-16 08:49:27 -05:00
|
|
|
push_up(&mut self.ci.nodes, NEVER);
|
2024-09-15 13:14:56 -05:00
|
|
|
// TODO: handle infinte loops
|
|
|
|
self.ci.nodes.visited.clear(self.ci.nodes.values.len());
|
2024-09-16 08:49:27 -05:00
|
|
|
push_down(&mut self.ci.nodes, VOID);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-20 01:09:29 -05:00
|
|
|
// FIXME: make this more efficient (allocated with arena)
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Block {
|
|
|
|
nid: Nid,
|
|
|
|
preds: Vec<regalloc2::Block>,
|
|
|
|
succs: Vec<regalloc2::Block>,
|
|
|
|
instrs: regalloc2::InstRange,
|
|
|
|
params: Vec<regalloc2::VReg>,
|
|
|
|
branch_blockparams: Vec<regalloc2::VReg>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Instr {
|
|
|
|
nid: Nid,
|
|
|
|
ops: Vec<regalloc2::Operand>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Function<'a> {
|
|
|
|
sig: Sig,
|
|
|
|
nodes: &'a mut Nodes,
|
|
|
|
tys: &'a Types,
|
|
|
|
blocks: Vec<Block>,
|
|
|
|
instrs: Vec<Instr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Function<'_> {
|
2024-09-30 12:09:17 -05:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2024-09-20 01:09:29 -05:00
|
|
|
for (i, block) in self.blocks.iter().enumerate() {
|
|
|
|
writeln!(f, "sb{i}{:?}-{:?}:", block.params, block.preds)?;
|
|
|
|
|
|
|
|
for inst in block.instrs.iter() {
|
|
|
|
let instr = &self.instrs[inst.index()];
|
|
|
|
writeln!(f, "{}: i{:?}:{:?}", inst.index(), self.nodes[instr.nid].kind, instr.ops)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(f, "eb{i}{:?}-{:?}:", block.branch_blockparams, block.succs)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Function<'a> {
|
|
|
|
fn new(nodes: &'a mut Nodes, tys: &'a Types, sig: Sig) -> Self {
|
|
|
|
let mut s =
|
|
|
|
Self { nodes, tys, sig, blocks: Default::default(), instrs: Default::default() };
|
|
|
|
s.nodes.visited.clear(s.nodes.values.len());
|
|
|
|
s.emit_node(VOID, VOID);
|
|
|
|
s.add_block(0);
|
|
|
|
s.blocks.pop();
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_block(&mut self, nid: Nid) -> RallocBRef {
|
|
|
|
if let Some(prev) = self.blocks.last_mut() {
|
|
|
|
prev.instrs = regalloc2::InstRange::new(
|
|
|
|
prev.instrs.first(),
|
|
|
|
regalloc2::Inst::new(self.instrs.len()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.blocks.push(Block {
|
|
|
|
nid,
|
|
|
|
preds: Default::default(),
|
|
|
|
succs: Default::default(),
|
|
|
|
instrs: regalloc2::InstRange::new(
|
|
|
|
regalloc2::Inst::new(self.instrs.len()),
|
|
|
|
regalloc2::Inst::new(self.instrs.len() + 1),
|
|
|
|
),
|
|
|
|
params: Default::default(),
|
|
|
|
branch_blockparams: Default::default(),
|
|
|
|
});
|
|
|
|
self.blocks.len() as RallocBRef - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_instr(&mut self, nid: Nid, ops: Vec<regalloc2::Operand>) {
|
|
|
|
self.instrs.push(Instr { nid, ops });
|
|
|
|
}
|
|
|
|
|
|
|
|
fn urg(&mut self, nid: Nid) -> regalloc2::Operand {
|
|
|
|
regalloc2::Operand::reg_use(self.rg(nid))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn def_nid(&mut self, _nid: Nid) {}
|
|
|
|
|
|
|
|
fn drg(&mut self, nid: Nid) -> regalloc2::Operand {
|
|
|
|
self.def_nid(nid);
|
|
|
|
regalloc2::Operand::reg_def(self.rg(nid))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rg(&self, nid: Nid) -> VReg {
|
|
|
|
regalloc2::VReg::new(nid as _, regalloc2::RegClass::Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_node(&mut self, nid: Nid, prev: Nid) {
|
|
|
|
if matches!(self.nodes[nid].kind, Kind::Region | Kind::Loop) {
|
|
|
|
let prev_bref = self.nodes[prev].ralloc_backref;
|
|
|
|
let node = self.nodes[nid].clone();
|
|
|
|
|
|
|
|
let idx = 1 + node.inputs.iter().position(|&i| i == prev).unwrap();
|
|
|
|
|
|
|
|
for ph in node.outputs {
|
2024-10-18 06:11:11 -05:00
|
|
|
if self.nodes[ph].kind != Kind::Phi || self.nodes[ph].ty == ty::Id::VOID {
|
2024-09-20 01:09:29 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rg = self.rg(self.nodes[ph].inputs[idx]);
|
|
|
|
self.blocks[prev_bref as usize].branch_blockparams.push(rg);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.add_instr(nid, vec![]);
|
|
|
|
|
|
|
|
match (self.nodes[nid].kind, self.nodes.visited.set(nid)) {
|
|
|
|
(Kind::Loop, false) => {
|
|
|
|
for i in node.inputs {
|
|
|
|
self.bridge(i, nid);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(Kind::Region, true) => return,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else if !self.nodes.visited.set(nid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let node = self.nodes[nid].clone();
|
|
|
|
match node.kind {
|
2024-09-20 12:01:44 -05:00
|
|
|
Kind::Start => {
|
2024-09-27 09:53:28 -05:00
|
|
|
debug_assert_matches!(self.nodes[node.outputs[0]].kind, Kind::Entry);
|
2024-09-20 12:01:44 -05:00
|
|
|
self.emit_node(node.outputs[0], VOID)
|
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
Kind::End => {}
|
|
|
|
Kind::If => {
|
|
|
|
self.nodes[nid].ralloc_backref = self.nodes[prev].ralloc_backref;
|
|
|
|
|
|
|
|
let &[_, cond] = node.inputs.as_slice() else { unreachable!() };
|
|
|
|
let &[mut then, mut else_] = node.outputs.as_slice() else { unreachable!() };
|
|
|
|
|
|
|
|
if let Kind::BinOp { op } = self.nodes[cond].kind
|
|
|
|
&& let Some((_, swapped)) = op.cond_op(node.ty.is_signed())
|
|
|
|
{
|
|
|
|
if swapped {
|
2024-09-30 12:09:17 -05:00
|
|
|
core::mem::swap(&mut then, &mut else_);
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
let &[_, lhs, rhs] = self.nodes[cond].inputs.as_slice() else { unreachable!() };
|
|
|
|
let ops = vec![self.urg(lhs), self.urg(rhs)];
|
|
|
|
self.add_instr(nid, ops);
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit_node(then, nid);
|
|
|
|
self.emit_node(else_, nid);
|
|
|
|
}
|
|
|
|
Kind::Region | Kind::Loop => {
|
|
|
|
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
|
|
|
if node.kind == Kind::Region {
|
|
|
|
for i in node.inputs {
|
|
|
|
self.bridge(i, nid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut block = vec![];
|
|
|
|
for ph in node.outputs.clone() {
|
2024-10-18 06:11:11 -05:00
|
|
|
if self.nodes[ph].kind != Kind::Phi || self.nodes[ph].ty == ty::Id::VOID {
|
2024-09-20 01:09:29 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
self.def_nid(ph);
|
|
|
|
block.push(self.rg(ph));
|
|
|
|
}
|
|
|
|
self.blocks[self.nodes[nid].ralloc_backref as usize].params = block;
|
|
|
|
for o in node.outputs.into_iter().rev() {
|
|
|
|
self.emit_node(o, nid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Return => {
|
|
|
|
let ops = if node.inputs[1] != VOID {
|
|
|
|
vec![regalloc2::Operand::reg_fixed_use(
|
|
|
|
self.rg(node.inputs[1]),
|
|
|
|
regalloc2::PReg::new(1, regalloc2::RegClass::Int),
|
|
|
|
)]
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
self.add_instr(nid, ops);
|
|
|
|
self.emit_node(node.outputs[0], nid);
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
Kind::CInt { .. }
|
|
|
|
if node.outputs.iter().all(|&o| {
|
2024-09-20 04:01:10 -05:00
|
|
|
let ond = &self.nodes[o];
|
|
|
|
matches!(ond.kind, Kind::BinOp { op }
|
|
|
|
if op.imm_binop(ond.ty.is_signed(), 8).is_some()
|
2024-09-28 08:13:32 -05:00
|
|
|
&& self.nodes.is_const(ond.inputs[2])
|
2024-09-20 04:01:10 -05:00
|
|
|
&& op.cond_op(ond.ty.is_signed()).is_none())
|
2024-10-18 06:11:11 -05:00
|
|
|
}) =>
|
|
|
|
{
|
|
|
|
self.nodes.lock(nid)
|
|
|
|
}
|
|
|
|
Kind::CInt { .. } => {
|
|
|
|
let ops = vec![self.drg(nid)];
|
|
|
|
self.add_instr(nid, ops);
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::Entry => {
|
2024-09-20 12:01:44 -05:00
|
|
|
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
|
|
|
|
|
|
|
let mut parama = self.tys.parama(self.sig.ret);
|
|
|
|
for (arg, ti) in
|
2024-09-27 09:53:28 -05:00
|
|
|
self.nodes[VOID].clone().outputs.into_iter().skip(2).zip(self.sig.args.range())
|
2024-09-20 12:01:44 -05:00
|
|
|
{
|
2024-10-01 15:53:03 -05:00
|
|
|
let ty = self.tys.ins.args[ti];
|
2024-09-20 12:01:44 -05:00
|
|
|
match self.tys.size_of(ty) {
|
|
|
|
0 => continue,
|
|
|
|
1..=8 => {
|
|
|
|
self.def_nid(arg);
|
|
|
|
self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def(
|
|
|
|
self.rg(arg),
|
|
|
|
regalloc2::PReg::new(parama.next() as _, regalloc2::RegClass::Int),
|
|
|
|
)]);
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
9..=16 => todo!(),
|
|
|
|
_ => {
|
|
|
|
self.def_nid(arg);
|
|
|
|
self.add_instr(NEVER, vec![regalloc2::Operand::reg_fixed_def(
|
|
|
|
self.rg(arg),
|
|
|
|
regalloc2::PReg::new(parama.next() as _, regalloc2::RegClass::Int),
|
|
|
|
)]);
|
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
2024-09-20 12:01:44 -05:00
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
|
2024-09-20 12:01:44 -05:00
|
|
|
for o in node.outputs.into_iter().rev() {
|
|
|
|
self.emit_node(o, nid);
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::Then | Kind::Else => {
|
2024-09-20 12:01:44 -05:00
|
|
|
self.nodes[nid].ralloc_backref = self.add_block(nid);
|
|
|
|
self.bridge(prev, nid);
|
|
|
|
for o in node.outputs.into_iter().rev() {
|
|
|
|
self.emit_node(o, nid);
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
Kind::BinOp { op: TokenKind::Add }
|
|
|
|
if self.nodes.is_const(node.inputs[2])
|
|
|
|
&& node
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.all(|&n| matches!(self.nodes[n].kind, Kind::Stre | Kind::Load)) =>
|
|
|
|
{
|
|
|
|
self.nodes.lock(nid)
|
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
Kind::BinOp { op } => {
|
|
|
|
let &[_, lhs, rhs] = node.inputs.as_slice() else { unreachable!() };
|
|
|
|
|
|
|
|
let ops = if let Kind::CInt { .. } = self.nodes[rhs].kind
|
2024-10-18 06:11:11 -05:00
|
|
|
&& self.nodes[rhs].lock_rc != 0
|
2024-09-20 01:09:29 -05:00
|
|
|
{
|
|
|
|
vec![self.drg(nid), self.urg(lhs)]
|
|
|
|
} else if op.binop(node.ty.is_signed(), 8).is_some() {
|
|
|
|
vec![self.drg(nid), self.urg(lhs), self.urg(rhs)]
|
|
|
|
} else if op.cond_op(node.ty.is_signed()).is_some() {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
todo!("{op}")
|
|
|
|
};
|
|
|
|
self.add_instr(nid, ops);
|
|
|
|
}
|
|
|
|
Kind::UnOp { .. } => {
|
|
|
|
let ops = vec![self.drg(nid), self.urg(node.inputs[1])];
|
|
|
|
self.add_instr(nid, ops);
|
|
|
|
}
|
|
|
|
Kind::Call { func } => {
|
|
|
|
self.nodes[nid].ralloc_backref = self.nodes[prev].ralloc_backref;
|
|
|
|
let mut ops = vec![];
|
|
|
|
|
2024-10-01 15:53:03 -05:00
|
|
|
let fuc = self.tys.ins.funcs[func as usize].sig.unwrap();
|
2024-09-20 01:09:29 -05:00
|
|
|
if self.tys.size_of(fuc.ret) != 0 {
|
|
|
|
self.def_nid(nid);
|
|
|
|
ops.push(regalloc2::Operand::reg_fixed_def(
|
|
|
|
self.rg(nid),
|
|
|
|
regalloc2::PReg::new(1, regalloc2::RegClass::Int),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut parama = self.tys.parama(fuc.ret);
|
2024-09-28 08:13:32 -05:00
|
|
|
for (&(mut i), ti) in node.inputs[1..].iter().zip(fuc.args.range()) {
|
2024-10-01 15:53:03 -05:00
|
|
|
let ty = self.tys.ins.args[ti];
|
2024-09-28 08:13:32 -05:00
|
|
|
loop {
|
|
|
|
match self.nodes[i].kind {
|
2024-09-30 12:09:17 -05:00
|
|
|
Kind::Stre { .. } => i = self.nodes[i].inputs[2],
|
|
|
|
Kind::Load { .. } => i = self.nodes[i].inputs[1],
|
2024-09-28 08:13:32 -05:00
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
match self.tys.size_of(ty) {
|
|
|
|
0 => continue,
|
|
|
|
1..=8 => {
|
|
|
|
ops.push(regalloc2::Operand::reg_fixed_use(
|
|
|
|
self.rg(i),
|
|
|
|
regalloc2::PReg::new(parama.next() as _, regalloc2::RegClass::Int),
|
|
|
|
));
|
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
9..=16 => todo!("pass in two register"),
|
|
|
|
_ => {
|
|
|
|
ops.push(regalloc2::Operand::reg_fixed_use(
|
|
|
|
self.rg(i),
|
|
|
|
regalloc2::PReg::new(parama.next() as _, regalloc2::RegClass::Int),
|
|
|
|
));
|
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.add_instr(nid, ops);
|
|
|
|
|
|
|
|
for o in node.outputs.into_iter().rev() {
|
|
|
|
if self.nodes[o].inputs[0] == nid {
|
|
|
|
self.emit_node(o, nid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
//Kind::Stck
|
|
|
|
// if node.outputs.iter().all(|&n| {
|
|
|
|
// matches!(self.nodes[n].kind, Kind::Stre | Kind::Load)
|
|
|
|
// || matches!(self.nodes[n].kind, Kind::BinOp { op: TokenKind::Add }
|
|
|
|
// if self.nodes.is_const(self.nodes[n].inputs[2])
|
|
|
|
// && self.nodes[n]
|
|
|
|
// .outputs
|
|
|
|
// .iter()
|
|
|
|
// .all(|&n| matches!(self.nodes[n].kind, Kind::Stre | Kind::Load)))
|
|
|
|
// }) => {}
|
2024-10-17 12:32:10 -05:00
|
|
|
Kind::Stck => {
|
|
|
|
let ops = vec![self.drg(nid)];
|
2024-09-27 09:53:28 -05:00
|
|
|
self.add_instr(nid, ops);
|
|
|
|
}
|
2024-10-17 12:32:10 -05:00
|
|
|
Kind::Phi | Kind::Arg { .. } | Kind::Mem => {}
|
2024-09-30 12:09:17 -05:00
|
|
|
Kind::Load { .. } => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let mut region = node.inputs[1];
|
|
|
|
if self.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add })
|
|
|
|
&& self.nodes.is_const(self.nodes[region].inputs[2])
|
|
|
|
{
|
|
|
|
region = self.nodes[region].inputs[1]
|
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
if self.tys.size_of(node.ty) <= 8 {
|
|
|
|
let ops = match self.nodes[region].kind {
|
|
|
|
Kind::Stck => vec![self.drg(nid)],
|
|
|
|
_ => vec![self.drg(nid), self.urg(region)],
|
|
|
|
};
|
|
|
|
self.add_instr(nid, ops);
|
|
|
|
}
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
Kind::Stre { .. } if node.inputs[2] == VOID => self.nodes.lock(nid),
|
2024-09-30 12:09:17 -05:00
|
|
|
Kind::Stre { .. } => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let mut region = node.inputs[2];
|
|
|
|
if self.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add })
|
|
|
|
&& self.nodes.is_const(self.nodes[region].inputs[2])
|
|
|
|
{
|
|
|
|
region = self.nodes[region].inputs[1]
|
2024-10-17 15:29:09 -05:00
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
let ops = match self.nodes[region].kind {
|
|
|
|
_ if self.tys.size_of(node.ty) > 8 => {
|
|
|
|
vec![self.urg(region), self.urg(self.nodes[node.inputs[1]].inputs[1])]
|
|
|
|
}
|
|
|
|
Kind::Stck => vec![self.urg(node.inputs[1])],
|
|
|
|
_ => vec![self.urg(region), self.urg(node.inputs[1])],
|
|
|
|
};
|
|
|
|
self.add_instr(nid, ops);
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bridge(&mut self, pred: u16, succ: u16) {
|
|
|
|
if self.nodes[pred].ralloc_backref == u16::MAX
|
|
|
|
|| self.nodes[succ].ralloc_backref == u16::MAX
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.blocks[self.nodes[pred].ralloc_backref as usize]
|
|
|
|
.succs
|
|
|
|
.push(regalloc2::Block::new(self.nodes[succ].ralloc_backref as usize));
|
|
|
|
self.blocks[self.nodes[succ].ralloc_backref as usize]
|
|
|
|
.preds
|
|
|
|
.push(regalloc2::Block::new(self.nodes[pred].ralloc_backref as usize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 08:04:58 -05:00
|
|
|
impl regalloc2::Function for Function<'_> {
|
2024-09-20 01:09:29 -05:00
|
|
|
fn num_insts(&self) -> usize {
|
|
|
|
self.instrs.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn num_blocks(&self) -> usize {
|
|
|
|
self.blocks.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn entry_block(&self) -> regalloc2::Block {
|
|
|
|
regalloc2::Block(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_insns(&self, block: regalloc2::Block) -> regalloc2::InstRange {
|
|
|
|
self.blocks[block.index()].instrs
|
|
|
|
}
|
|
|
|
|
2024-09-21 07:46:12 -05:00
|
|
|
fn block_succs(&self, block: regalloc2::Block) -> &[regalloc2::Block] {
|
|
|
|
&self.blocks[block.index()].succs
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
2024-09-21 07:46:12 -05:00
|
|
|
fn block_preds(&self, block: regalloc2::Block) -> &[regalloc2::Block] {
|
|
|
|
&self.blocks[block.index()].preds
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
2024-09-21 07:46:12 -05:00
|
|
|
fn block_params(&self, block: regalloc2::Block) -> &[regalloc2::VReg] {
|
|
|
|
&self.blocks[block.index()].params
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_ret(&self, insn: regalloc2::Inst) -> bool {
|
|
|
|
self.nodes[self.instrs[insn.index()].nid].kind == Kind::Return
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_branch(&self, insn: regalloc2::Inst) -> bool {
|
|
|
|
matches!(
|
|
|
|
self.nodes[self.instrs[insn.index()].nid].kind,
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::If | Kind::Then | Kind::Else | Kind::Entry | Kind::Loop | Kind::Region
|
2024-09-20 01:09:29 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn branch_blockparams(
|
|
|
|
&self,
|
|
|
|
block: regalloc2::Block,
|
|
|
|
_insn: regalloc2::Inst,
|
|
|
|
_succ_idx: usize,
|
2024-09-21 07:46:12 -05:00
|
|
|
) -> &[regalloc2::VReg] {
|
2024-09-20 01:09:29 -05:00
|
|
|
debug_assert!(
|
|
|
|
self.blocks[block.index()].succs.len() == 1
|
|
|
|
|| self.blocks[block.index()].branch_blockparams.is_empty()
|
|
|
|
);
|
|
|
|
|
2024-09-21 07:46:12 -05:00
|
|
|
&self.blocks[block.index()].branch_blockparams
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
2024-09-21 07:46:12 -05:00
|
|
|
fn inst_operands(&self, insn: regalloc2::Inst) -> &[regalloc2::Operand] {
|
|
|
|
&self.instrs[insn.index()].ops
|
2024-09-20 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inst_clobbers(&self, insn: regalloc2::Inst) -> regalloc2::PRegSet {
|
|
|
|
if matches!(self.nodes[self.instrs[insn.index()].nid].kind, Kind::Call { .. }) {
|
|
|
|
let mut set = regalloc2::PRegSet::default();
|
2024-09-27 09:53:28 -05:00
|
|
|
for i in 2..13 {
|
2024-09-20 01:09:29 -05:00
|
|
|
set.add(regalloc2::PReg::new(i, regalloc2::RegClass::Int));
|
|
|
|
}
|
|
|
|
set
|
|
|
|
} else {
|
|
|
|
regalloc2::PRegSet::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn num_vregs(&self) -> usize {
|
|
|
|
self.nodes.values.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spillslot_size(&self, regclass: regalloc2::RegClass) -> usize {
|
|
|
|
match regclass {
|
|
|
|
regalloc2::RegClass::Int => 1,
|
|
|
|
regalloc2::RegClass::Float => unreachable!(),
|
|
|
|
regalloc2::RegClass::Vector => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn loop_depth(target: Nid, nodes: &mut Nodes) -> LoopDepth {
|
|
|
|
if nodes[target].loop_depth != 0 {
|
|
|
|
return nodes[target].loop_depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[target].loop_depth = match nodes[target].kind {
|
2024-09-27 09:53:28 -05:00
|
|
|
Kind::Entry | Kind::Then | Kind::Else | Kind::Call { .. } | Kind::Return | Kind::If => {
|
|
|
|
let dpth = loop_depth(nodes[target].inputs[0], nodes);
|
|
|
|
if nodes[target].loop_depth != 0 {
|
|
|
|
return nodes[target].loop_depth;
|
|
|
|
}
|
|
|
|
dpth
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
Kind::Region => {
|
|
|
|
let l = loop_depth(nodes[target].inputs[0], nodes);
|
|
|
|
let r = loop_depth(nodes[target].inputs[1], nodes);
|
2024-09-27 09:53:28 -05:00
|
|
|
debug_assert_eq!(l, r);
|
2024-09-15 13:14:56 -05:00
|
|
|
l
|
|
|
|
}
|
|
|
|
Kind::Loop => {
|
|
|
|
let depth = loop_depth(nodes[target].inputs[0], nodes) + 1;
|
|
|
|
nodes[target].loop_depth = depth;
|
|
|
|
let mut cursor = nodes[target].inputs[1];
|
|
|
|
while cursor != target {
|
|
|
|
nodes[cursor].loop_depth = depth;
|
|
|
|
let next = if nodes[cursor].kind == Kind::Region {
|
|
|
|
loop_depth(nodes[cursor].inputs[0], nodes);
|
|
|
|
nodes[cursor].inputs[1]
|
|
|
|
} else {
|
|
|
|
idom(nodes, cursor)
|
|
|
|
};
|
2024-09-16 08:49:27 -05:00
|
|
|
debug_assert_ne!(next, VOID);
|
2024-09-27 09:53:28 -05:00
|
|
|
if matches!(nodes[cursor].kind, Kind::Then | Kind::Else) {
|
2024-09-15 13:14:56 -05:00
|
|
|
let other = *nodes[next]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
2024-09-27 09:53:28 -05:00
|
|
|
.find(|&&n| nodes[n].kind != nodes[cursor].kind)
|
2024-09-15 13:14:56 -05:00
|
|
|
.unwrap();
|
|
|
|
if nodes[other].loop_depth == 0 {
|
|
|
|
nodes[other].loop_depth = depth - 1;
|
2024-09-12 11:42:21 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
cursor = next;
|
|
|
|
}
|
|
|
|
depth
|
2024-09-12 11:42:21 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
Kind::Start | Kind::End => 1,
|
2024-09-20 12:01:44 -05:00
|
|
|
_ => unreachable!(),
|
2024-09-15 13:14:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
nodes[target].loop_depth
|
|
|
|
}
|
|
|
|
|
|
|
|
fn better(nodes: &mut Nodes, is: Nid, then: Nid) -> bool {
|
|
|
|
loop_depth(is, nodes) < loop_depth(then, nodes)
|
|
|
|
|| idepth(nodes, is) > idepth(nodes, then)
|
|
|
|
|| nodes[then].kind == Kind::If
|
|
|
|
}
|
|
|
|
|
|
|
|
fn idepth(nodes: &mut Nodes, target: Nid) -> IDomDepth {
|
2024-09-16 08:49:27 -05:00
|
|
|
if target == VOID {
|
2024-09-15 13:14:56 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if nodes[target].depth == 0 {
|
|
|
|
nodes[target].depth = match nodes[target].kind {
|
|
|
|
Kind::End | Kind::Start => unreachable!(),
|
|
|
|
Kind::Region => {
|
|
|
|
idepth(nodes, nodes[target].inputs[0]).max(idepth(nodes, nodes[target].inputs[1]))
|
|
|
|
}
|
2024-09-20 12:01:44 -05:00
|
|
|
_ => idepth(nodes, nodes[target].inputs[0]),
|
2024-09-15 13:14:56 -05:00
|
|
|
} + 1;
|
|
|
|
}
|
|
|
|
nodes[target].depth
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn push_up(nodes: &mut Nodes, node: Nid) {
|
|
|
|
if !nodes.visited.set(node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodes[node].kind.is_pinned() {
|
|
|
|
for i in 0..nodes[node].inputs.len() {
|
|
|
|
let i = nodes[node].inputs[i];
|
|
|
|
push_up(nodes, i);
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-16 08:49:27 -05:00
|
|
|
let mut max = VOID;
|
2024-09-15 13:14:56 -05:00
|
|
|
for i in 0..nodes[node].inputs.len() {
|
|
|
|
let i = nodes[node].inputs[i];
|
|
|
|
let is_call = matches!(nodes[i].kind, Kind::Call { .. });
|
|
|
|
if nodes.is_cfg(i) && !is_call {
|
|
|
|
continue;
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
push_up(nodes, i);
|
|
|
|
if idepth(nodes, i) > idepth(nodes, max) {
|
|
|
|
max = if is_call { i } else { idom(nodes, i) };
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
nodes.check_dominance(node, max, false);
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
|
|
|
|
2024-09-16 08:49:27 -05:00
|
|
|
if max == VOID {
|
2024-09-15 13:14:56 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
let index = nodes[0].outputs.iter().position(|&p| p == node).unwrap();
|
|
|
|
nodes[0].outputs.remove(index);
|
|
|
|
nodes[node].inputs[0] = max;
|
|
|
|
debug_assert!(
|
|
|
|
!nodes[max].outputs.contains(&node) || matches!(nodes[max].kind, Kind::Call { .. }),
|
|
|
|
"{node} {:?} {max} {:?}",
|
|
|
|
nodes[node],
|
|
|
|
nodes[max]
|
|
|
|
);
|
|
|
|
nodes[max].outputs.push(node);
|
|
|
|
}
|
|
|
|
}
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn push_down(nodes: &mut Nodes, node: Nid) {
|
|
|
|
if !nodes.visited.set(node) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
// TODO: handle memory nodes first
|
|
|
|
|
|
|
|
if nodes[node].kind.is_pinned() {
|
|
|
|
// TODO: use buffer to avoid allocation or better yet queue the location changes
|
|
|
|
for i in nodes[node].outputs.clone() {
|
|
|
|
push_down(nodes, i);
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
} else {
|
|
|
|
let mut min = None::<Nid>;
|
|
|
|
for i in 0..nodes[node].outputs.len() {
|
|
|
|
let i = nodes[node].outputs[i];
|
|
|
|
push_down(nodes, i);
|
|
|
|
let i = use_block(node, i, nodes);
|
|
|
|
min = min.map(|m| common_dom(i, m, nodes)).or(Some(i));
|
|
|
|
}
|
|
|
|
let mut min = min.unwrap();
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
debug_assert!(nodes.dominates(nodes[node].inputs[0], min));
|
|
|
|
|
|
|
|
let mut cursor = min;
|
|
|
|
loop {
|
|
|
|
if better(nodes, cursor, min) {
|
|
|
|
min = cursor;
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
if cursor == nodes[node].inputs[0] {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cursor = idom(nodes, cursor);
|
|
|
|
}
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
if nodes[min].kind.ends_basic_block() {
|
|
|
|
min = idom(nodes, min);
|
|
|
|
}
|
2024-09-10 13:50:36 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
nodes.check_dominance(node, min, true);
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
let prev = nodes[node].inputs[0];
|
2024-10-18 02:52:50 -05:00
|
|
|
debug_assert!(idepth(nodes, min) >= idepth(nodes, prev));
|
|
|
|
let index = nodes[prev].outputs.iter().position(|&p| p == node).unwrap();
|
|
|
|
nodes[prev].outputs.remove(index);
|
|
|
|
nodes[node].inputs[0] = min;
|
|
|
|
nodes[min].outputs.push(node);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 13:50:36 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn use_block(target: Nid, from: Nid, nodes: &mut Nodes) -> Nid {
|
|
|
|
if nodes[from].kind != Kind::Phi {
|
|
|
|
return idom(nodes, from);
|
|
|
|
}
|
2024-09-10 13:50:36 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
let index = nodes[from].inputs.iter().position(|&n| n == target).unwrap();
|
|
|
|
nodes[nodes[from].inputs[0]].inputs[index - 1]
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn idom(nodes: &mut Nodes, target: Nid) -> Nid {
|
|
|
|
match nodes[target].kind {
|
2024-09-16 08:49:27 -05:00
|
|
|
Kind::Start => VOID,
|
2024-09-15 13:14:56 -05:00
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::Region => {
|
|
|
|
let &[lcfg, rcfg] = nodes[target].inputs.as_slice() else { unreachable!() };
|
|
|
|
common_dom(lcfg, rcfg, nodes)
|
|
|
|
}
|
2024-09-20 12:01:44 -05:00
|
|
|
_ => nodes[target].inputs[0],
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn common_dom(mut a: Nid, mut b: Nid, nodes: &mut Nodes) -> Nid {
|
|
|
|
while a != b {
|
|
|
|
let [ldepth, rdepth] = [idepth(nodes, a), idepth(nodes, b)];
|
|
|
|
if ldepth >= rdepth {
|
|
|
|
a = idom(nodes, a);
|
|
|
|
}
|
|
|
|
if ldepth <= rdepth {
|
|
|
|
b = idom(nodes, b);
|
2024-09-12 11:42:21 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
a
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-09-30 12:09:17 -05:00
|
|
|
use {
|
|
|
|
alloc::{string::String, vec::Vec},
|
|
|
|
core::fmt::Write,
|
|
|
|
};
|
2024-09-02 17:07:20 -05:00
|
|
|
|
|
|
|
fn generate(ident: &'static str, input: &'static str, output: &mut String) {
|
2024-10-04 14:44:29 -05:00
|
|
|
_ = log::set_logger(&crate::fs::Logger);
|
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2024-09-20 12:01:44 -05:00
|
|
|
|
2024-10-13 13:01:18 -05:00
|
|
|
let (files, _embeds) = crate::test_parse_files(ident, input);
|
|
|
|
let mut codegen = super::Codegen { files, ..Default::default() };
|
2024-09-02 17:27:50 -05:00
|
|
|
|
|
|
|
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();
|
2024-09-13 11:22:27 -05:00
|
|
|
codegen.tys.assemble(&mut out);
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-30 12:09:17 -05:00
|
|
|
let err = codegen.tys.disasm(&out, &codegen.files, output, |_| {});
|
2024-09-04 16:46:32 -05:00
|
|
|
if let Err(e) = err {
|
|
|
|
writeln!(output, "!!! asm is invalid: {e}").unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-13 07:30:23 -05:00
|
|
|
crate::test_run_vm(&out, output);
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
crate::run_tests! { generate:
|
2024-09-28 09:34:08 -05:00
|
|
|
arithmetic;
|
|
|
|
variables;
|
|
|
|
functions;
|
|
|
|
comments;
|
|
|
|
if_statements;
|
|
|
|
loops;
|
|
|
|
fb_driver;
|
|
|
|
pointers;
|
2024-09-28 14:56:39 -05:00
|
|
|
structs;
|
2024-09-28 09:34:08 -05:00
|
|
|
//different_types;
|
|
|
|
//struct_operators;
|
|
|
|
//directives;
|
|
|
|
//global_variables;
|
|
|
|
//generic_types;
|
|
|
|
//generic_functions;
|
|
|
|
//c_strings;
|
|
|
|
//struct_patterns;
|
|
|
|
//arrays;
|
|
|
|
//struct_return_from_module_function;
|
|
|
|
////comptime_pointers;
|
|
|
|
//sort_something_viredly;
|
|
|
|
hex_octal_binary_literals;
|
|
|
|
//comptime_min_reg_leak;
|
|
|
|
////structs_in_registers;
|
|
|
|
//comptime_function_from_another_file;
|
|
|
|
//inline;
|
|
|
|
//inline_test;
|
|
|
|
const_folding_with_arg;
|
|
|
|
branch_assignments;
|
|
|
|
exhaustive_loop_testing;
|
|
|
|
//idk;
|
|
|
|
//comptime_min_reg_leak;
|
|
|
|
//some_generic_code;
|
|
|
|
//integer_inference_issues;
|
|
|
|
//writing_into_string;
|
|
|
|
//request_page;
|
|
|
|
//tests_ptr_to_ptr_copy;
|
|
|
|
//wide_ret;
|
|
|
|
pointer_opts;
|
2024-10-18 06:11:11 -05:00
|
|
|
conditional_stores;
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|