2024-09-02 17:07:20 -05:00
|
|
|
use {
|
2024-11-05 07:52:30 -06:00
|
|
|
self::{
|
|
|
|
hbvm::{Comptime, HbvmBackend},
|
|
|
|
strong_ref::StrongRef,
|
|
|
|
},
|
2024-09-02 17:07:20 -05:00
|
|
|
crate::{
|
2024-10-01 14:33:30 -05:00
|
|
|
ctx_map::CtxEntry,
|
2024-10-27 08:29:14 -05:00
|
|
|
debug,
|
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-11-08 03:25:34 -06:00
|
|
|
CtorField, Expr, Pos,
|
2024-09-04 09:54:34 -05:00
|
|
|
},
|
2024-11-08 03:25:34 -06:00
|
|
|
ty::{self, Arg, ArrayLen, Loc, Module, Tuple},
|
|
|
|
utils::{BitSet, Ent, Vc},
|
2024-11-05 07:52:30 -06:00
|
|
|
CompState, FTask, Func, Global, Ident, Offset, OffsetIter, OptLayout, Sig, StringRef,
|
|
|
|
SymKey, TypeParser, Types,
|
2024-09-02 17:07:20 -05:00
|
|
|
},
|
2024-11-08 03:25:34 -06: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-11-04 12:18:37 -06:00
|
|
|
cell::{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-11-10 10:32:24 -06:00
|
|
|
ops::{self, Range},
|
2024-09-02 17:07:20 -05:00
|
|
|
},
|
2024-09-30 12:09:17 -05:00
|
|
|
hashbrown::hash_map,
|
2024-10-26 05:09:53 -05:00
|
|
|
hbbytecode::DisasmError,
|
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-10-27 05:32:34 -05:00
|
|
|
const LOOPS: Nid = 4;
|
|
|
|
const ARG_START: usize = 3;
|
2024-10-29 09:04:07 -05:00
|
|
|
const DEFAULT_ACLASS: usize = 0;
|
|
|
|
const GLOBAL_ACLASS: usize = 1;
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-10-27 08:29:14 -05:00
|
|
|
pub mod hbvm;
|
|
|
|
|
2024-09-13 07:15:45 -05:00
|
|
|
type Nid = u16;
|
2024-11-04 05:38:47 -06:00
|
|
|
type AClassId = i16;
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
pub struct AssemblySpec {
|
2024-11-07 01:52:41 -06:00
|
|
|
entry: u32,
|
2024-11-05 07:52:30 -06:00
|
|
|
code_length: u64,
|
|
|
|
data_length: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Backend {
|
|
|
|
fn assemble_reachable(
|
|
|
|
&mut self,
|
|
|
|
from: ty::Func,
|
|
|
|
types: &Types,
|
|
|
|
to: &mut Vec<u8>,
|
|
|
|
) -> AssemblySpec;
|
|
|
|
fn disasm<'a>(
|
|
|
|
&'a self,
|
|
|
|
sluce: &[u8],
|
|
|
|
eca_handler: &mut dyn FnMut(&mut &[u8]),
|
|
|
|
types: &'a Types,
|
|
|
|
files: &'a [parser::Ast],
|
|
|
|
output: &mut String,
|
|
|
|
) -> Result<(), hbbytecode::DisasmError<'a>>;
|
|
|
|
fn emit_body(&mut self, id: ty::Func, ci: &mut Nodes, tys: &Types, files: &[parser::Ast]);
|
|
|
|
|
|
|
|
fn emit_ct_body(&mut self, id: ty::Func, ci: &mut Nodes, tys: &Types, files: &[parser::Ast]) {
|
|
|
|
self.emit_body(id, ci, tys, files);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assemble_bin(&mut self, from: ty::Func, types: &Types, to: &mut Vec<u8>) {
|
|
|
|
self.assemble_reachable(from, types, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 14:33:30 -05:00
|
|
|
type Lookup = crate::ctx_map::CtxMap<Nid>;
|
|
|
|
|
|
|
|
impl crate::ctx_map::CtxEntry for Nid {
|
2024-10-24 06:25:30 -05:00
|
|
|
type Ctx = [Result<Node, (Nid, debug::Trace)>];
|
2024-10-01 14:33:30 -05:00
|
|
|
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> {
|
2024-10-28 17:38:57 -05:00
|
|
|
ctx[*self as usize].as_ref().unwrap_or_else(|(_, t)| panic!("{t:#?}")).key()
|
2024-09-08 05:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
macro_rules! inference {
|
|
|
|
($ty:ident, $ctx:expr, $self:expr, $pos:expr, $subject:literal, $example:literal) => {
|
|
|
|
let Some($ty) = $ctx.ty else {
|
|
|
|
$self.report(
|
|
|
|
$pos,
|
|
|
|
concat!(
|
|
|
|
"resulting ",
|
|
|
|
$subject,
|
|
|
|
" cannot be inferred from context, consider using `",
|
|
|
|
$example,
|
|
|
|
"` to hint the type",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:23 -05:00
|
|
|
#[derive(Clone)]
|
2024-11-05 07:52:30 -06:00
|
|
|
pub struct Nodes {
|
2024-10-24 06:25:30 -05:00
|
|
|
values: Vec<Result<Node, (Nid, debug::Trace)>>,
|
2024-11-12 05:20:08 -06:00
|
|
|
queued_peeps: Vec<Nid>,
|
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-11-12 05:20:08 -06:00
|
|
|
Self {
|
|
|
|
values: Default::default(),
|
|
|
|
queued_peeps: Default::default(),
|
|
|
|
free: Nid::MAX,
|
|
|
|
lookup: Default::default(),
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Nodes {
|
2024-11-07 01:52:41 -06:00
|
|
|
fn loop_depth(&self, target: Nid) -> LoopDepth {
|
|
|
|
if self[target].loop_depth.get() != 0 {
|
|
|
|
return self[target].loop_depth.get();
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
|
2024-11-07 01:52:41 -06:00
|
|
|
self[target].loop_depth.set(match self[target].kind {
|
2024-10-27 15:34:03 -05:00
|
|
|
Kind::Entry | Kind::Then | Kind::Else | Kind::Call { .. } | Kind::Return | Kind::If => {
|
|
|
|
let dpth = self.loop_depth(self[target].inputs[0]);
|
2024-11-07 01:52:41 -06:00
|
|
|
if self[target].loop_depth.get() != 0 {
|
|
|
|
return self[target].loop_depth.get();
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
dpth
|
|
|
|
}
|
|
|
|
Kind::Region => {
|
|
|
|
let l = self.loop_depth(self[target].inputs[0]);
|
|
|
|
let r = self.loop_depth(self[target].inputs[1]);
|
|
|
|
debug_assert_eq!(l, r);
|
|
|
|
l
|
|
|
|
}
|
|
|
|
Kind::Loop => {
|
|
|
|
let depth = Self::loop_depth(self, self[target].inputs[0]) + 1;
|
2024-11-07 01:52:41 -06:00
|
|
|
self[target].loop_depth.set(depth);
|
2024-10-27 15:34:03 -05:00
|
|
|
let mut cursor = self[target].inputs[1];
|
|
|
|
while cursor != target {
|
2024-11-07 01:52:41 -06:00
|
|
|
self[cursor].loop_depth.set(depth);
|
2024-10-27 15:34:03 -05:00
|
|
|
let next = if self[cursor].kind == Kind::Region {
|
|
|
|
self.loop_depth(self[cursor].inputs[0]);
|
|
|
|
self[cursor].inputs[1]
|
|
|
|
} else {
|
|
|
|
self.idom(cursor)
|
|
|
|
};
|
|
|
|
debug_assert_ne!(next, VOID);
|
|
|
|
if matches!(self[cursor].kind, Kind::Then | Kind::Else) {
|
|
|
|
let other = *self[next]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.find(|&&n| self[n].kind != self[cursor].kind)
|
|
|
|
.unwrap();
|
2024-11-07 01:52:41 -06:00
|
|
|
if self[other].loop_depth.get() == 0 {
|
|
|
|
self[other].loop_depth.set(depth - 1);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor = next;
|
|
|
|
}
|
|
|
|
depth
|
|
|
|
}
|
2024-11-03 03:15:03 -06:00
|
|
|
Kind::Start | Kind::End | Kind::Die => 1,
|
2024-10-27 15:34:03 -05:00
|
|
|
u => unreachable!("{u:?}"),
|
2024-11-07 01:52:41 -06:00
|
|
|
});
|
2024-10-27 15:34:03 -05:00
|
|
|
|
2024-11-07 01:52:41 -06:00
|
|
|
self[target].loop_depth.get()
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
fn idepth(&self, target: Nid) -> IDomDepth {
|
2024-10-27 15:34:03 -05:00
|
|
|
if target == VOID {
|
|
|
|
return 0;
|
|
|
|
}
|
2024-11-04 12:18:37 -06:00
|
|
|
if self[target].depth.get() == 0 {
|
|
|
|
let depth = match self[target].kind {
|
2024-10-27 15:34:03 -05:00
|
|
|
Kind::End | Kind::Start => unreachable!("{:?}", self[target].kind),
|
|
|
|
Kind::Region => {
|
|
|
|
self.idepth(self[target].inputs[0]).max(self.idepth(self[target].inputs[1]))
|
|
|
|
}
|
|
|
|
_ => self.idepth(self[target].inputs[0]),
|
|
|
|
} + 1;
|
2024-11-04 12:18:37 -06:00
|
|
|
self[target].depth.set(depth);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
2024-11-04 12:18:37 -06:00
|
|
|
self[target].depth.get()
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fix_loops(&mut self) {
|
|
|
|
'o: for l in self[LOOPS].outputs.clone() {
|
|
|
|
let mut cursor = self[l].inputs[1];
|
2024-11-10 03:28:02 -06:00
|
|
|
let depth = self.loop_depth(cursor);
|
2024-10-27 15:34:03 -05:00
|
|
|
while cursor != l {
|
|
|
|
if self[cursor].kind == Kind::If
|
2024-11-10 03:28:02 -06:00
|
|
|
&& self[cursor].outputs.iter().any(|&b| self.loop_depth(b) < depth)
|
2024-10-27 15:34:03 -05:00
|
|
|
{
|
|
|
|
continue 'o;
|
|
|
|
}
|
|
|
|
cursor = self.idom(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
self[l].outputs.push(NEVER);
|
|
|
|
self[NEVER].inputs.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn push_up_impl(&mut self, node: Nid, visited: &mut BitSet) {
|
|
|
|
if !visited.set(node) {
|
2024-10-27 15:34:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 1..self[node].inputs.len() {
|
|
|
|
let inp = self[node].inputs[i];
|
|
|
|
if !self[inp].kind.is_pinned() {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.push_up_impl(inp, visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self[node].kind.is_pinned() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-08 13:40:18 -06:00
|
|
|
let mut deepest = self[node].inputs[0];
|
|
|
|
for &inp in self[node].inputs[1..].iter() {
|
2024-10-27 15:34:03 -05:00
|
|
|
if self.idepth(inp) > self.idepth(deepest) {
|
2024-11-12 12:02:29 -06:00
|
|
|
if self[inp].kind.is_call() {
|
2024-10-30 07:45:19 -05:00
|
|
|
deepest = inp;
|
|
|
|
} else {
|
2024-11-08 13:40:18 -06:00
|
|
|
debug_assert!(!self.is_cfg(inp));
|
2024-10-30 07:45:19 -05:00
|
|
|
deepest = self.idom(inp);
|
|
|
|
}
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 07:45:19 -05:00
|
|
|
if deepest == self[node].inputs[0] {
|
2024-10-27 15:34:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
let current = self[node].inputs[0];
|
|
|
|
|
|
|
|
let index = self[current].outputs.iter().position(|&p| p == node).unwrap();
|
|
|
|
self[current].outputs.remove(index);
|
2024-10-27 15:34:03 -05:00
|
|
|
self[node].inputs[0] = deepest;
|
|
|
|
debug_assert!(
|
2024-11-12 12:02:29 -06:00
|
|
|
!self[deepest].outputs.contains(&node) || self[deepest].kind.is_call(),
|
2024-10-27 15:34:03 -05:00
|
|
|
"{node} {:?} {deepest} {:?}",
|
|
|
|
self[node],
|
|
|
|
self[deepest]
|
|
|
|
);
|
|
|
|
self[deepest].outputs.push(node);
|
|
|
|
}
|
|
|
|
|
2024-11-08 13:40:18 -06:00
|
|
|
fn collect_rpo(&self, node: Nid, rpo: &mut Vec<Nid>, visited: &mut BitSet) {
|
2024-11-05 07:52:30 -06:00
|
|
|
if !self.is_cfg(node) || !visited.set(node) {
|
2024-10-27 15:34:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-08 13:40:18 -06:00
|
|
|
for &n in self[node].outputs.iter() {
|
|
|
|
self.collect_rpo(n, rpo, visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
2024-11-08 13:40:18 -06:00
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
rpo.push(node);
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn push_up(&mut self, rpo: &mut Vec<Nid>, visited: &mut BitSet) {
|
2024-10-28 17:38:57 -05:00
|
|
|
debug_assert!(rpo.is_empty());
|
2024-11-05 07:52:30 -06:00
|
|
|
self.collect_rpo(VOID, rpo, visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
|
|
|
|
for &node in rpo.iter().rev() {
|
|
|
|
self.loop_depth(node);
|
|
|
|
for i in 0..self[node].inputs.len() {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.push_up_impl(self[node].inputs[i], visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches!(self[node].kind, Kind::Loop | Kind::Region) {
|
|
|
|
for i in 0..self[node].outputs.len() {
|
|
|
|
let usage = self[node].outputs[i];
|
|
|
|
if self[usage].kind == Kind::Phi {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.push_up_impl(usage, visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert_eq!(
|
|
|
|
self.iter()
|
|
|
|
.map(|(n, _)| n)
|
2024-11-05 07:52:30 -06:00
|
|
|
.filter(|&n| !visited.get(n)
|
2024-10-27 15:34:03 -05:00
|
|
|
&& !matches!(self[n].kind, Kind::Arg | Kind::Mem | Kind::Loops))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
vec![],
|
|
|
|
"{:?}",
|
|
|
|
self.iter()
|
2024-11-05 07:52:30 -06:00
|
|
|
.filter(|&(n, nod)| !visited.get(n)
|
2024-10-27 15:34:03 -05:00
|
|
|
&& !matches!(nod.kind, Kind::Arg | Kind::Mem | Kind::Loops))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
);
|
2024-10-28 17:38:57 -05:00
|
|
|
|
|
|
|
rpo.clear();
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn better(&mut self, is: Nid, then: Nid) -> bool {
|
|
|
|
debug_assert_ne!(self.idepth(is), self.idepth(then), "{is} {then}");
|
|
|
|
self.loop_depth(is) < self.loop_depth(then)
|
|
|
|
|| self.idepth(is) > self.idepth(then)
|
|
|
|
|| self[then].kind == Kind::If
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_forward_edge(&mut self, usage: Nid, def: Nid) -> bool {
|
|
|
|
match self[usage].kind {
|
|
|
|
Kind::Phi => {
|
|
|
|
self[usage].inputs[2] != def || self[self[usage].inputs[0]].kind != Kind::Loop
|
|
|
|
}
|
|
|
|
Kind::Loop => self[usage].inputs[1] != def,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn push_down(&mut self, node: Nid, visited: &mut BitSet) {
|
|
|
|
if !visited.set(node) {
|
2024-10-27 15:34:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for usage in self[node].outputs.clone() {
|
|
|
|
if self.is_forward_edge(usage, node) && self[node].kind == Kind::Stre {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.push_down(usage, visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for usage in self[node].outputs.clone() {
|
|
|
|
if self.is_forward_edge(usage, node) {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.push_down(usage, visited);
|
2024-10-27 15:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self[node].kind.is_pinned() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut min = None::<Nid>;
|
|
|
|
for i in 0..self[node].outputs.len() {
|
|
|
|
let usage = self[node].outputs[i];
|
|
|
|
let ub = self.use_block(node, usage);
|
|
|
|
min = min.map(|m| self.common_dom(ub, m)).or(Some(ub));
|
|
|
|
}
|
|
|
|
let mut min = min.unwrap();
|
|
|
|
|
|
|
|
debug_assert!(self.dominates(self[node].inputs[0], min));
|
|
|
|
|
|
|
|
let mut cursor = min;
|
|
|
|
while cursor != self[node].inputs[0] {
|
|
|
|
cursor = self.idom(cursor);
|
|
|
|
if self.better(cursor, min) {
|
|
|
|
min = cursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 07:45:19 -05:00
|
|
|
if self[node].kind == Kind::Load {
|
|
|
|
min = self.find_antideps(node, min);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self[node].kind == Kind::Stre {
|
|
|
|
self[node].antidep = self[node].inputs[0];
|
|
|
|
}
|
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
if self[min].kind.ends_basic_block() {
|
|
|
|
min = self.idom(min);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.check_dominance(node, min, true);
|
|
|
|
|
|
|
|
let prev = self[node].inputs[0];
|
|
|
|
debug_assert!(self.idepth(min) >= self.idepth(prev));
|
|
|
|
let index = self[prev].outputs.iter().position(|&p| p == node).unwrap();
|
|
|
|
self[prev].outputs.remove(index);
|
|
|
|
self[node].inputs[0] = min;
|
|
|
|
self[min].outputs.push(node);
|
|
|
|
}
|
|
|
|
|
2024-10-30 07:45:19 -05:00
|
|
|
fn find_antideps(&mut self, load: Nid, mut min: Nid) -> Nid {
|
|
|
|
debug_assert!(self[load].kind == Kind::Load);
|
|
|
|
|
|
|
|
let (aclass, _) = self.aclass_index(self[load].inputs[1]);
|
|
|
|
|
|
|
|
let mut cursor = min;
|
|
|
|
while cursor != self[load].inputs[0] {
|
|
|
|
self[cursor].antidep = load;
|
2024-10-30 12:42:25 -05:00
|
|
|
if self[cursor].clobbers.get(aclass as _) {
|
2024-10-30 07:45:19 -05:00
|
|
|
min = self[cursor].inputs[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cursor = self.idom(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self[load].inputs[2] == MEM {
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
for out in self[self[load].inputs[2]].outputs.clone() {
|
|
|
|
match self[out].kind {
|
|
|
|
Kind::Stre => {
|
|
|
|
let mut cursor = self[out].inputs[0];
|
|
|
|
while cursor != self[out].antidep {
|
|
|
|
if self[cursor].antidep == load {
|
|
|
|
min = self.common_dom(min, cursor);
|
|
|
|
if min == cursor {
|
|
|
|
self.bind(load, out);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cursor = self.idom(cursor);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Kind::Phi => {
|
|
|
|
let n = self[out].inputs[1..]
|
|
|
|
.iter()
|
|
|
|
.position(|&n| n == self[load].inputs[2])
|
|
|
|
.unwrap();
|
|
|
|
let mut cursor = self[self[out].inputs[0]].inputs[n];
|
|
|
|
while cursor != self[out].antidep {
|
|
|
|
if self[cursor].antidep == load {
|
|
|
|
min = self.common_dom(min, cursor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cursor = self.idom(cursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
min
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bind(&mut self, from: Nid, to: Nid) {
|
2024-11-04 05:38:47 -06:00
|
|
|
debug_assert_ne!(to, 0);
|
2024-10-30 07:45:19 -05:00
|
|
|
self[from].outputs.push(to);
|
|
|
|
self[to].inputs.push(from);
|
|
|
|
}
|
|
|
|
|
2024-11-07 01:52:41 -06:00
|
|
|
fn use_block(&self, target: Nid, from: Nid) -> Nid {
|
2024-10-27 15:34:03 -05:00
|
|
|
if self[from].kind != Kind::Phi {
|
|
|
|
return self.idom(from);
|
|
|
|
}
|
|
|
|
|
2024-11-10 02:17:43 -06:00
|
|
|
let index = self[from].inputs.iter().position(|&n| n == target).unwrap_or_else(|| {
|
|
|
|
panic!("from {from} {:?} target {target} {:?}", self[from], self[target])
|
|
|
|
});
|
2024-10-27 15:34:03 -05:00
|
|
|
self[self[from].inputs[0]].inputs[index - 1]
|
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
fn idom(&self, target: Nid) -> Nid {
|
2024-10-27 15:34:03 -05:00
|
|
|
match self[target].kind {
|
|
|
|
Kind::Start => VOID,
|
|
|
|
Kind::End => unreachable!(),
|
|
|
|
Kind::Region => {
|
|
|
|
let &[lcfg, rcfg] = self[target].inputs.as_slice() else { unreachable!() };
|
|
|
|
self.common_dom(lcfg, rcfg)
|
|
|
|
}
|
|
|
|
_ => self[target].inputs[0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
fn common_dom(&self, mut a: Nid, mut b: Nid) -> Nid {
|
2024-10-27 15:34:03 -05:00
|
|
|
while a != b {
|
|
|
|
let [ldepth, rdepth] = [self.idepth(a), self.idepth(b)];
|
|
|
|
if ldepth >= rdepth {
|
|
|
|
a = self.idom(a);
|
|
|
|
}
|
|
|
|
if ldepth <= rdepth {
|
|
|
|
b = self.idom(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a
|
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
fn merge_scopes(
|
|
|
|
&mut self,
|
|
|
|
loops: &mut [Loop],
|
|
|
|
ctrl: &StrongRef,
|
|
|
|
to: &mut Scope,
|
|
|
|
from: &mut Scope,
|
2024-11-10 10:32:24 -06:00
|
|
|
tys: &Types,
|
2024-10-26 05:09:53 -05:00
|
|
|
) {
|
2024-10-28 10:18:53 -05:00
|
|
|
for (i, (to_value, from_value)) in to.vars.iter_mut().zip(from.vars.iter_mut()).enumerate()
|
|
|
|
{
|
2024-10-26 05:09:53 -05:00
|
|
|
debug_assert_eq!(to_value.ty, from_value.ty);
|
|
|
|
if to_value.value() != from_value.value() {
|
|
|
|
self.load_loop_var(i, from_value, loops);
|
|
|
|
self.load_loop_var(i, to_value, loops);
|
|
|
|
if to_value.value() != from_value.value() {
|
2024-11-10 02:17:43 -06:00
|
|
|
debug_assert!(!to_value.ptr);
|
|
|
|
debug_assert!(!from_value.ptr);
|
2024-10-26 05:09:53 -05:00
|
|
|
let inps = [ctrl.get(), from_value.value(), to_value.value()];
|
2024-11-10 10:32:24 -06:00
|
|
|
to_value
|
|
|
|
.set_value_remove(self.new_node(from_value.ty, Kind::Phi, inps, tys), self);
|
2024-10-26 05:09:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
for (i, (to_class, from_class)) in
|
|
|
|
to.aclasses.iter_mut().zip(from.aclasses.iter_mut()).enumerate()
|
|
|
|
{
|
|
|
|
if to_class.last_store.get() != from_class.last_store.get() {
|
|
|
|
self.load_loop_aclass(i, from_class, loops);
|
|
|
|
self.load_loop_aclass(i, to_class, loops);
|
|
|
|
if to_class.last_store.get() != from_class.last_store.get() {
|
|
|
|
let inps = [ctrl.get(), from_class.last_store.get(), to_class.last_store.get()];
|
|
|
|
to_class
|
|
|
|
.last_store
|
2024-11-10 10:32:24 -06:00
|
|
|
.set_remove(self.new_node(ty::Id::VOID, Kind::Phi, inps, tys), self);
|
2024-10-28 10:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-26 05:09:53 -05:00
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn graphviz_low(&self, disp: ty::Display, out: &mut String) -> core::fmt::Result {
|
2024-10-20 03:37:48 -05:00
|
|
|
use core::fmt::Write;
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
writeln!(out)?;
|
|
|
|
writeln!(out, "digraph G {{")?;
|
|
|
|
writeln!(out, "rankdir=BT;")?;
|
|
|
|
writeln!(out, "concentrate=true;")?;
|
|
|
|
writeln!(out, "compound=true;")?;
|
|
|
|
|
2024-10-20 03:37:48 -05:00
|
|
|
for (i, node) in self.iter() {
|
2024-10-22 15:57:40 -05:00
|
|
|
let color = match () {
|
2024-10-23 05:26:07 -05:00
|
|
|
_ if node.lock_rc == Nid::MAX => "orange",
|
|
|
|
_ if node.lock_rc == Nid::MAX - 1 => "blue",
|
|
|
|
_ if node.lock_rc != 0 => "red",
|
|
|
|
_ if node.outputs.is_empty() => "purple",
|
|
|
|
_ if node.is_mem() => "green",
|
2024-10-22 15:57:40 -05:00
|
|
|
_ if self.is_cfg(i) => "yellow",
|
|
|
|
_ => "white",
|
2024-10-22 09:03:23 -05:00
|
|
|
};
|
2024-10-22 15:57:40 -05:00
|
|
|
|
2024-10-23 05:26:07 -05:00
|
|
|
if node.ty != ty::Id::VOID {
|
|
|
|
writeln!(
|
|
|
|
out,
|
2024-11-04 05:38:47 -06:00
|
|
|
" node{i}[label=\"{i} {} {} {}\" color={color}]",
|
2024-10-23 05:26:07 -05:00
|
|
|
node.kind,
|
2024-11-05 07:52:30 -06:00
|
|
|
disp.rety(node.ty),
|
2024-10-30 07:45:19 -05:00
|
|
|
node.aclass,
|
2024-10-23 05:26:07 -05:00
|
|
|
)?;
|
2024-10-22 15:57:40 -05:00
|
|
|
} else {
|
2024-10-30 07:45:19 -05:00
|
|
|
writeln!(
|
|
|
|
out,
|
2024-11-04 05:38:47 -06:00
|
|
|
" node{i}[label=\"{i} {} {}\" color={color}]",
|
|
|
|
node.kind, node.aclass,
|
2024-10-30 07:45:19 -05:00
|
|
|
)?;
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
|
2024-10-20 03:37:48 -05:00
|
|
|
for (j, &o) in node.outputs.iter().enumerate() {
|
|
|
|
let color = if self.is_cfg(i) && self.is_cfg(o) { "red" } else { "lightgray" };
|
|
|
|
let index = self[o].inputs.iter().position(|&inp| i == inp).unwrap();
|
|
|
|
let style = if index == 0 && !self.is_cfg(o) { "style=dotted" } else { "" };
|
|
|
|
writeln!(
|
|
|
|
out,
|
2024-10-26 03:25:42 -05:00
|
|
|
" node{o} -> node{i}[color={color} taillabel={index} headlabel={j} {style}]",
|
2024-10-20 03:37:48 -05:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
writeln!(out, "}}")?;
|
|
|
|
|
2024-10-20 03:37:48 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn graphviz(&self, disp: ty::Display) {
|
2024-10-20 03:37:48 -05:00
|
|
|
let out = &mut String::new();
|
2024-11-05 07:52:30 -06:00
|
|
|
_ = self.graphviz_low(disp, out);
|
2024-10-20 03:37:48 -05:00
|
|
|
log::info!("{out}");
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn graphviz_in_browser(&self, disp: ty::Display) {
|
2024-10-24 06:25:30 -05:00
|
|
|
#[cfg(all(debug_assertions, feature = "std"))]
|
2024-10-22 15:57:40 -05:00
|
|
|
{
|
2024-10-27 07:57:00 -05:00
|
|
|
let out = &mut String::new();
|
2024-11-05 07:52:30 -06:00
|
|
|
_ = self.graphviz_low(disp, out);
|
2024-10-27 07:57:00 -05:00
|
|
|
if !std::process::Command::new("brave")
|
|
|
|
.arg(format!("https://dreampuf.github.io/GraphvizOnline/#{out}"))
|
|
|
|
.status()
|
|
|
|
.unwrap()
|
|
|
|
.success()
|
|
|
|
{
|
|
|
|
log::error!("{out}");
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn gcm(&mut self, rpo: &mut Vec<Nid>, visited: &mut BitSet) {
|
2024-10-27 15:34:03 -05:00
|
|
|
self.fix_loops();
|
2024-11-05 07:52:30 -06:00
|
|
|
visited.clear(self.values.len());
|
|
|
|
self.push_up(rpo, visited);
|
|
|
|
visited.clear(self.values.len());
|
|
|
|
self.push_down(VOID, visited);
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
|
|
|
|
2024-09-04 09:54:34 -05:00
|
|
|
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-11-05 07:52:30 -06:00
|
|
|
let node = Node { inputs: inps.into(), kind, ty, ..Default::default() };
|
2024-09-07 21:20:10 -05:00
|
|
|
|
2024-10-23 05:26:07 -05:00
|
|
|
if node.kind == Kind::Phi && node.ty != ty::Id::VOID {
|
|
|
|
debug_assert_ne!(
|
|
|
|
self[node.inputs[1]].ty,
|
|
|
|
ty::Id::VOID,
|
|
|
|
"{:?} {:?}",
|
|
|
|
self[node.inputs[1]],
|
|
|
|
node.ty.expand(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if self[node.inputs[0]].kind != Kind::Loop {
|
|
|
|
debug_assert_ne!(
|
|
|
|
self[node.inputs[2]].ty,
|
|
|
|
ty::Id::VOID,
|
|
|
|
"{:?} {:?}",
|
|
|
|
self[node.inputs[2]],
|
|
|
|
node.ty.expand(),
|
|
|
|
);
|
|
|
|
}
|
2024-11-10 02:17:43 -06:00
|
|
|
|
|
|
|
debug_assert!(!matches!(node.ty.expand(), ty::Kind::Struct(_)));
|
2024-10-23 05:26:07 -05:00
|
|
|
}
|
|
|
|
|
2024-09-08 05:00:07 -05:00
|
|
|
let mut lookup_meta = None;
|
2024-10-20 11:49:41 -05:00
|
|
|
if !node.is_not_gvnd() {
|
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-10-24 06:25:30 -05:00
|
|
|
self.values.push(Err((Nid::MAX, debug::trace())));
|
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
|
|
|
}
|
2024-10-22 09:53:48 -05:00
|
|
|
self.free = mem::replace(&mut self.values[free as usize], Ok(node)).unwrap_err().0;
|
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-10-20 11:49:41 -05:00
|
|
|
if !self[target].is_not_gvnd() {
|
2024-10-26 08:18:00 -05:00
|
|
|
self.lookup
|
|
|
|
.remove(&target, &self.values)
|
|
|
|
.unwrap_or_else(|| panic!("{:?}", self[target]));
|
2024-09-08 05:00:07 -05:00
|
|
|
}
|
2024-09-07 21:20:10 -05:00
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
fn new_node(&mut self, ty: ty::Id, kind: Kind, inps: impl Into<Vc>, tys: &Types) -> Nid {
|
2024-09-06 11:50:28 -05:00
|
|
|
let id = self.new_node_nop(ty, kind, inps);
|
2024-11-10 10:32:24 -06:00
|
|
|
if let Some(opt) = self.peephole(id, tys) {
|
2024-09-04 09:54:34 -05:00
|
|
|
debug_assert_ne!(opt, id);
|
2024-11-12 05:20:08 -06:00
|
|
|
self.queued_peeps.clear();
|
2024-09-04 09:54:34 -05:00
|
|
|
self.lock(opt);
|
|
|
|
self.remove(id);
|
|
|
|
self.unlock(opt);
|
2024-10-27 07:57:00 -05:00
|
|
|
opt
|
2024-09-04 09:54:34 -05:00
|
|
|
} else {
|
2024-10-27 07:57:00 -05:00
|
|
|
id
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
fn new_const(&mut self, ty: ty::Id, value: impl Into<i64>) -> Nid {
|
|
|
|
self.new_node_nop(ty, Kind::CInt { value: value.into() }, [VOID])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_const_lit(&mut self, ty: ty::Id, value: impl Into<i64>) -> Value {
|
2024-11-10 10:32:24 -06:00
|
|
|
Value::new(self.new_const(ty, value)).ty(ty)
|
2024-10-30 14:20:03 -05:00
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
fn new_node_lit(&mut self, ty: ty::Id, kind: Kind, inps: impl Into<Vc>, tys: &Types) -> Value {
|
|
|
|
Value::new(self.new_node(ty, kind, inps, tys)).ty(ty)
|
2024-10-17 12:32:10 -05:00
|
|
|
}
|
|
|
|
|
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-10-27 07:57:00 -05:00
|
|
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
mem::replace(&mut self.values[target as usize], Err((Nid::MAX, debug::trace())))
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
mem::replace(&mut self.values[target as usize], Err((self.free, debug::trace())))
|
|
|
|
.unwrap();
|
|
|
|
self.free = 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
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
fn late_peephole(&mut self, target: Nid, tys: &Types) -> Option<Nid> {
|
|
|
|
if let Some(id) = self.peephole(target, tys) {
|
2024-11-12 05:20:08 -06:00
|
|
|
self.queued_peeps.clear();
|
2024-10-26 03:45:50 -05:00
|
|
|
self.replace(target, id);
|
2024-10-28 17:38:57 -05:00
|
|
|
return None;
|
2024-10-26 03:45:50 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
fn iter_peeps(&mut self, mut fuel: usize, stack: &mut Vec<Nid>, tys: &Types) {
|
2024-10-28 17:38:57 -05:00
|
|
|
debug_assert!(stack.is_empty());
|
|
|
|
|
2024-10-27 05:32:34 -05:00
|
|
|
self.iter()
|
2024-10-26 13:29:31 -05:00
|
|
|
.filter_map(|(id, node)| node.kind.is_peeped().then_some(id))
|
2024-10-27 05:32:34 -05:00
|
|
|
.collect_into(stack);
|
2024-10-26 08:18:00 -05:00
|
|
|
stack.iter().for_each(|&s| self.lock(s));
|
2024-10-26 03:45:50 -05:00
|
|
|
|
|
|
|
while fuel != 0
|
|
|
|
&& let Some(node) = stack.pop()
|
|
|
|
{
|
|
|
|
fuel -= 1;
|
2024-10-29 03:04:49 -05:00
|
|
|
|
|
|
|
if self[node].outputs.is_empty() {
|
|
|
|
self.push_adjacent_nodes(node, stack);
|
|
|
|
}
|
|
|
|
|
2024-10-26 08:18:00 -05:00
|
|
|
if self.unlock_remove(node) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
if let Some(new) = self.peephole(node, tys) {
|
2024-11-12 05:20:08 -06:00
|
|
|
let plen = stack.len();
|
|
|
|
stack.append(&mut self.queued_peeps);
|
|
|
|
for &p in &stack[plen..] {
|
|
|
|
self.lock(p);
|
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
self.replace(node, new);
|
|
|
|
self.push_adjacent_nodes(new, stack);
|
|
|
|
}
|
|
|
|
|
2024-11-07 03:43:29 -06:00
|
|
|
//debug_assert_matches!(
|
|
|
|
// self.iter()
|
|
|
|
// .find(|(i, n)| n.lock_rc != 0 && n.kind.is_peeped() && !stack.contains(i)),
|
|
|
|
// None
|
|
|
|
//);
|
2024-10-28 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stack.drain(..).for_each(|s| _ = self.unlock_remove(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_adjacent_nodes(&mut self, of: Nid, stack: &mut Vec<Nid>) {
|
|
|
|
let prev_len = stack.len();
|
2024-10-29 03:04:49 -05:00
|
|
|
for &i in self[of]
|
|
|
|
.outputs
|
|
|
|
.iter()
|
|
|
|
.chain(self[of].inputs.iter())
|
|
|
|
.chain(self[of].peep_triggers.iter())
|
|
|
|
{
|
|
|
|
if self.values[i as usize].is_ok() && self[i].kind.is_peeped() && self[i].lock_rc == 0 {
|
2024-10-28 17:38:57 -05:00
|
|
|
stack.push(i);
|
2024-10-26 03:45:50 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-29 03:04:49 -05:00
|
|
|
|
|
|
|
self[of].peep_triggers = Vc::default();
|
2024-10-28 17:38:57 -05:00
|
|
|
stack.iter().skip(prev_len).for_each(|&n| self.lock(n));
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn aclass_index(&self, region: Nid) -> (usize, Nid) {
|
2024-11-04 05:38:47 -06:00
|
|
|
if self[region].aclass >= 0 {
|
|
|
|
(self[region].aclass as _, region)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
self[self[region].aclass.unsigned_abs() - 1].aclass as _,
|
|
|
|
self[region].aclass.unsigned_abs() - 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pass_aclass(&mut self, from: Nid, to: Nid) {
|
|
|
|
debug_assert!(self[from].aclass >= 0);
|
|
|
|
if from != to {
|
|
|
|
self[to].aclass = -(from as AClassId + 1);
|
|
|
|
}
|
2024-10-26 03:45:50 -05:00
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
fn peephole(&mut self, target: Nid, tys: &Types) -> 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-10-29 07:36:12 -05:00
|
|
|
let is_float = self[lhs].ty.is_float();
|
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if let (&K::CInt { value: a }, &K::CInt { value: b }) =
|
|
|
|
(&self[lhs].kind, &self[rhs].kind)
|
|
|
|
{
|
2024-10-30 14:20:03 -05:00
|
|
|
return Some(self.new_const(ty, op.apply_binop(a, b, is_float)));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-08 05:00:07 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if lhs == rhs {
|
|
|
|
match op {
|
2024-10-30 14:20:03 -05:00
|
|
|
T::Sub => return Some(self.new_const(ty, 0)),
|
2024-09-28 08:13:32 -05:00
|
|
|
T::Add => {
|
2024-10-30 14:20:03 -05:00
|
|
|
let rhs = self.new_const(ty, 2);
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self.new_node(
|
|
|
|
ty,
|
|
|
|
K::BinOp { op: T::Mul },
|
|
|
|
[ctrl, lhs, rhs],
|
|
|
|
tys,
|
|
|
|
));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
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-10-26 05:09:53 -05:00
|
|
|
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[rhs].kind {
|
|
|
|
match (op, value) {
|
2024-11-03 03:15:03 -06:00
|
|
|
(T::Eq, 0) if self[lhs].ty.is_pointer() || self[lhs].kind == Kind::Stck => {
|
|
|
|
return Some(self.new_const(ty::Id::BOOL, 0));
|
|
|
|
}
|
|
|
|
(T::Ne, 0) if self[lhs].ty.is_pointer() || self[lhs].kind == Kind::Stck => {
|
|
|
|
return Some(self.new_const(ty::Id::BOOL, 1));
|
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
(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)
|
2024-10-30 14:20:03 -05:00
|
|
|
let new_rhs = self.new_const(ty, op.apply_binop(av, bv, is_float));
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self.new_node(ty, K::BinOp { op }, [ctrl, a, new_rhs], tys));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
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
|
2024-11-10 10:32:24 -06:00
|
|
|
let new_lhs = self.new_node(ty, K::BinOp { op }, [ctrl, a, rhs], tys);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op }, [ctrl, new_lhs, b], tys));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
|
|
|
}
|
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)
|
2024-10-30 14:20:03 -05:00
|
|
|
let new_rhs = self.new_const(ty, value + 1);
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self.new_node(
|
|
|
|
ty,
|
|
|
|
K::BinOp { op: T::Mul },
|
|
|
|
[ctrl, rhs, new_rhs],
|
|
|
|
tys,
|
|
|
|
));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-21 10:29:11 -05:00
|
|
|
if op == T::Sub
|
|
|
|
&& self[lhs].kind == (K::BinOp { op: T::Add })
|
|
|
|
&& let K::CInt { value: a } = self[rhs].kind
|
|
|
|
&& let K::CInt { value: b } = self[self[lhs].inputs[2]].kind
|
|
|
|
{
|
2024-10-30 14:20:03 -05:00
|
|
|
let new_rhs = self.new_const(ty, b - a);
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self.new_node(
|
|
|
|
ty,
|
|
|
|
K::BinOp { op: T::Add },
|
|
|
|
[ctrl, self[lhs].inputs[1], new_rhs],
|
|
|
|
tys,
|
|
|
|
));
|
2024-10-21 10:29:11 -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;
|
2024-11-10 10:32:24 -06:00
|
|
|
let new_rhs = self.new_node(ty, K::BinOp { op: T::Add }, [ctrl, b, c], tys);
|
|
|
|
return Some(self.new_node(ty, K::BinOp { op }, [ctrl, a, new_rhs], tys));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if changed {
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self.new_node(ty, self[target].kind, [ctrl, lhs, rhs], tys));
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
K::UnOp { op } => {
|
2024-10-30 14:20:03 -05:00
|
|
|
let &[_, oper] = self[target].inputs.as_slice() else { unreachable!() };
|
2024-09-28 08:13:32 -05:00
|
|
|
let ty = self[target].ty;
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-11-12 12:02:29 -06:00
|
|
|
if matches!(op, TokenKind::Number | TokenKind::Float) && ty == self[oper].ty {
|
|
|
|
return Some(oper);
|
|
|
|
}
|
2024-10-29 08:24:31 -05:00
|
|
|
|
2024-09-28 08:13:32 -05:00
|
|
|
if let K::CInt { value } = self[oper].kind {
|
2024-11-12 12:02:29 -06:00
|
|
|
let is_float = self[oper].ty.is_float();
|
2024-10-30 14:20:03 -05:00
|
|
|
return Some(self.new_const(ty, op.apply_unop(value, is_float)));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-28 08:13:32 -05:00
|
|
|
K::If => {
|
2024-10-26 13:29:31 -05:00
|
|
|
if self[target].ty == ty::Id::VOID {
|
2024-11-04 12:18:37 -06:00
|
|
|
match self.try_opt_cond(target) {
|
|
|
|
CondOptRes::Unknown => {}
|
|
|
|
CondOptRes::Known { value, .. } => {
|
|
|
|
let ty = if value {
|
|
|
|
ty::Id::RIGHT_UNREACHABLE
|
|
|
|
} else {
|
|
|
|
ty::Id::LEFT_UNREACHABLE
|
|
|
|
};
|
|
|
|
return Some(self.new_node_nop(ty, K::If, self[target].inputs.clone()));
|
|
|
|
}
|
2024-10-26 13:29:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
K::Then => {
|
|
|
|
if self[self[target].inputs[0]].ty == ty::Id::LEFT_UNREACHABLE {
|
|
|
|
return Some(NEVER);
|
|
|
|
} else if self[self[target].inputs[0]].ty == ty::Id::RIGHT_UNREACHABLE {
|
|
|
|
return Some(self[self[target].inputs[0]].inputs[0]);
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-10-26 13:29:31 -05:00
|
|
|
K::Else => {
|
|
|
|
if self[self[target].inputs[0]].ty == ty::Id::RIGHT_UNREACHABLE {
|
|
|
|
return Some(NEVER);
|
|
|
|
} else if self[self[target].inputs[0]].ty == ty::Id::LEFT_UNREACHABLE {
|
|
|
|
return Some(self[self[target].inputs[0]].inputs[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
K::Region => {
|
|
|
|
let (ctrl, side) = match self[target].inputs.as_slice() {
|
|
|
|
[NEVER, NEVER] => return Some(NEVER),
|
|
|
|
&[NEVER, ctrl] => (ctrl, 2),
|
|
|
|
&[ctrl, NEVER] => (ctrl, 1),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.lock(target);
|
|
|
|
for i in self[target].outputs.clone() {
|
|
|
|
if self[i].kind == Kind::Phi {
|
|
|
|
self.replace(i, self[i].inputs[side]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.unlock(target);
|
|
|
|
|
|
|
|
return Some(ctrl);
|
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
K::Call { .. } => {
|
|
|
|
if self[target].inputs[0] == NEVER {
|
|
|
|
return Some(NEVER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
K::Return => {
|
2024-10-27 05:32:34 -05:00
|
|
|
if self[target].inputs[0] == NEVER {
|
|
|
|
return Some(NEVER);
|
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
|
|
|
|
let mut new_inps = Vc::from(&self[target].inputs[..2]);
|
|
|
|
'a: for &n in self[target].inputs.clone().iter().skip(2) {
|
2024-11-07 03:43:29 -06:00
|
|
|
if self[n].kind != Kind::Stre {
|
2024-10-28 17:38:57 -05:00
|
|
|
new_inps.push(n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-07 03:43:29 -06:00
|
|
|
if let Some(&load) =
|
|
|
|
self[n].outputs.iter().find(|&&n| self[n].kind == Kind::Load)
|
|
|
|
{
|
|
|
|
self[load].peep_triggers.push(target);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
let mut cursor = n;
|
|
|
|
let class = self.aclass_index(self[cursor].inputs[2]);
|
|
|
|
|
2024-10-29 03:04:49 -05:00
|
|
|
if self[class.1].kind != Kind::Stck {
|
|
|
|
new_inps.push(n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-08 04:51:10 -06:00
|
|
|
if self[class.1].outputs.iter().any(|&n| {
|
|
|
|
self[n].kind != Kind::Stre
|
|
|
|
&& self[n].outputs.iter().any(|&n| self[n].kind != Kind::Stre)
|
|
|
|
}) {
|
|
|
|
new_inps.push(n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
cursor = self[cursor].inputs[3];
|
|
|
|
while cursor != MEM {
|
2024-11-07 03:43:29 -06:00
|
|
|
debug_assert_eq!(self[cursor].kind, Kind::Stre);
|
|
|
|
if self.aclass_index(self[cursor].inputs[2]) != class {
|
2024-10-28 17:38:57 -05:00
|
|
|
new_inps.push(n);
|
|
|
|
continue 'a;
|
|
|
|
}
|
2024-11-07 03:43:29 -06:00
|
|
|
|
|
|
|
if let Some(&load) =
|
|
|
|
self[cursor].outputs.iter().find(|&&n| self[n].kind == Kind::Load)
|
|
|
|
{
|
|
|
|
self[load].peep_triggers.push(target);
|
|
|
|
continue 'a;
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
cursor = self[cursor].inputs[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_inps.as_slice() != self[target].inputs.as_slice() {
|
|
|
|
return Some(self.new_node_nop(ty::Id::VOID, Kind::Return, new_inps));
|
|
|
|
}
|
2024-10-27 05:32: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!() };
|
|
|
|
|
2024-11-12 05:20:08 -06:00
|
|
|
if rhs == target || lhs == rhs {
|
2024-10-18 06:11:11 -05:00
|
|
|
return Some(lhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self[lhs].kind == Kind::Stre
|
|
|
|
&& self[rhs].kind == Kind::Stre
|
|
|
|
&& self[lhs].ty == self[rhs].ty
|
2024-11-12 05:20:08 -06:00
|
|
|
&& self[lhs].ty.loc(tys) == Loc::Reg
|
2024-10-18 06:11:11 -05:00
|
|
|
&& self[lhs].inputs[2] == self[rhs].inputs[2]
|
2024-11-10 02:17:43 -06:00
|
|
|
&& self[lhs].inputs[3] == self[rhs].inputs[3]
|
2024-10-18 06:11:11 -05:00
|
|
|
{
|
2024-11-10 10:32:24 -06:00
|
|
|
let pick_value = self.new_node(
|
|
|
|
self[lhs].ty,
|
|
|
|
Kind::Phi,
|
|
|
|
[ctrl, self[lhs].inputs[1], self[rhs].inputs[1]],
|
|
|
|
tys,
|
|
|
|
);
|
2024-10-18 06:11:11 -05:00
|
|
|
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);
|
|
|
|
}
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self.new_node(self[lhs].ty, Kind::Stre, vc, tys));
|
2024-10-18 06:11:11 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-29 14:38:33 -05:00
|
|
|
K::Stck => {
|
|
|
|
if let &[mut a, mut b] = self[target].outputs.as_slice() {
|
|
|
|
if self[a].kind == Kind::Load {
|
|
|
|
mem::swap(&mut a, &mut b);
|
|
|
|
}
|
|
|
|
|
2024-11-12 12:02:29 -06:00
|
|
|
if self[a].kind.is_call()
|
2024-10-29 14:38:33 -05:00
|
|
|
&& self[a].inputs.last() == Some(&target)
|
|
|
|
&& self[b].kind == Kind::Load
|
|
|
|
&& let &[store] = self[b].outputs.as_slice()
|
|
|
|
&& self[store].kind == Kind::Stre
|
|
|
|
{
|
|
|
|
let len = self[a].inputs.len();
|
|
|
|
let stre = self[store].inputs[3];
|
|
|
|
if stre != MEM {
|
|
|
|
self[a].inputs.push(stre);
|
2024-10-30 07:45:19 -05:00
|
|
|
self[a].inputs.swap(len - 1, len);
|
2024-10-29 14:38:33 -05:00
|
|
|
self[stre].outputs.push(a);
|
|
|
|
}
|
|
|
|
return Some(self[store].inputs[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
K::Stre => {
|
2024-10-28 17:38:57 -05:00
|
|
|
let &[_, value, region, store, ..] = self[target].inputs.as_slice() else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
2024-10-29 14:38:33 -05:00
|
|
|
if self[value].kind == Kind::Load && self[value].inputs[1] == region {
|
|
|
|
return Some(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cursor = target;
|
|
|
|
while self[cursor].kind == Kind::Stre
|
|
|
|
&& self[cursor].inputs[1] != VOID
|
|
|
|
&& let &[next_store] = self[cursor].outputs.as_slice()
|
|
|
|
{
|
|
|
|
if self[next_store].inputs[2] == region
|
|
|
|
&& self[next_store].ty == self[target].ty
|
|
|
|
{
|
|
|
|
return Some(store);
|
|
|
|
}
|
|
|
|
cursor = next_store;
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
'eliminate: {
|
2024-10-29 03:04:49 -05:00
|
|
|
if self[target].outputs.is_empty() {
|
|
|
|
break 'eliminate;
|
|
|
|
}
|
|
|
|
|
2024-10-30 08:10:46 -05:00
|
|
|
if self[value].kind != Kind::Load
|
|
|
|
|| self[value].outputs.iter().any(|&n| self[n].kind != Kind::Stre)
|
2024-10-28 17:38:57 -05:00
|
|
|
{
|
2024-10-29 03:04:49 -05:00
|
|
|
for &ele in self[value].outputs.clone().iter().filter(|&&n| n != target) {
|
|
|
|
self[ele].peep_triggers.push(target);
|
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
break 'eliminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
let &[_, stack, last_store] = self[value].inputs.as_slice() else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
if self[stack].ty != self[value].ty || self[stack].kind != Kind::Stck {
|
|
|
|
break 'eliminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut unidentifed = self[stack].outputs.clone();
|
|
|
|
let load_idx = unidentifed.iter().position(|&n| n == value).unwrap();
|
|
|
|
unidentifed.swap_remove(load_idx);
|
|
|
|
|
|
|
|
let mut saved = Vc::default();
|
|
|
|
let mut cursor = last_store;
|
|
|
|
let mut first_store = last_store;
|
|
|
|
while cursor != MEM && self[cursor].kind == Kind::Stre {
|
|
|
|
let mut contact_point = cursor;
|
|
|
|
let mut region = self[cursor].inputs[2];
|
|
|
|
if let Kind::BinOp { op } = self[region].kind {
|
|
|
|
debug_assert_matches!(op, TokenKind::Add | TokenKind::Sub);
|
|
|
|
contact_point = region;
|
|
|
|
region = self[region].inputs[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if region != stack {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let Some(index) = unidentifed.iter().position(|&n| n == contact_point)
|
|
|
|
else {
|
|
|
|
break 'eliminate;
|
|
|
|
};
|
2024-11-12 05:20:08 -06:00
|
|
|
if self[self[cursor].inputs[1]].kind == Kind::Load
|
|
|
|
&& self[value].outputs.iter().any(|&n| {
|
|
|
|
self.aclass_index(self[self[cursor].inputs[1]].inputs[1]).0
|
|
|
|
== self.aclass_index(self[n].inputs[2]).0
|
|
|
|
})
|
|
|
|
{
|
|
|
|
break 'eliminate;
|
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
unidentifed.remove(index);
|
|
|
|
saved.push(contact_point);
|
|
|
|
first_store = cursor;
|
|
|
|
cursor = *self[cursor].inputs.get(3).unwrap_or(&MEM);
|
|
|
|
|
|
|
|
if unidentifed.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 02:17:43 -06:00
|
|
|
if !unidentifed.is_empty() {
|
|
|
|
break 'eliminate;
|
|
|
|
}
|
|
|
|
|
2024-10-29 11:03:00 -05:00
|
|
|
debug_assert_matches!(
|
|
|
|
self[last_store].kind,
|
|
|
|
Kind::Stre | Kind::Mem,
|
|
|
|
"{:?}",
|
|
|
|
self[last_store]
|
|
|
|
);
|
|
|
|
debug_assert_matches!(
|
|
|
|
self[first_store].kind,
|
|
|
|
Kind::Stre | Kind::Mem,
|
|
|
|
"{:?}",
|
|
|
|
self[first_store]
|
|
|
|
);
|
2024-10-28 17:38:57 -05:00
|
|
|
|
2024-10-29 11:03:00 -05:00
|
|
|
// FIXME: when the loads and stores become parallel we will need to get saved
|
|
|
|
// differently
|
|
|
|
let mut prev_store = store;
|
|
|
|
for mut oper in saved.into_iter().rev() {
|
|
|
|
let mut region = region;
|
|
|
|
if let Kind::BinOp { op } = self[oper].kind {
|
|
|
|
debug_assert_eq!(self[oper].outputs.len(), 1);
|
|
|
|
debug_assert_eq!(self[self[oper].outputs[0]].kind, Kind::Stre);
|
2024-11-10 10:32:24 -06:00
|
|
|
let new_region = self.new_node(
|
|
|
|
self[oper].ty,
|
|
|
|
Kind::BinOp { op },
|
|
|
|
[VOID, region, self[oper].inputs[2]],
|
|
|
|
tys,
|
|
|
|
);
|
|
|
|
self.pass_aclass(self.aclass_index(region).1, new_region);
|
|
|
|
region = new_region;
|
2024-10-29 11:03:00 -05:00
|
|
|
oper = self[oper].outputs[0];
|
2024-10-28 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
2024-10-29 11:03:00 -05:00
|
|
|
let mut inps = self[oper].inputs.clone();
|
|
|
|
debug_assert_eq!(inps.len(), 4);
|
|
|
|
inps[2] = region;
|
|
|
|
inps[3] = prev_store;
|
2024-11-12 05:20:08 -06:00
|
|
|
prev_store = self.new_node_nop(self[oper].ty, Kind::Stre, inps);
|
|
|
|
self.queued_peeps.push(prev_store);
|
2024-10-28 17:38:57 -05:00
|
|
|
}
|
2024-10-29 11:03:00 -05:00
|
|
|
|
|
|
|
return Some(prev_store);
|
2024-10-28 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
2024-11-07 03:47:31 -06:00
|
|
|
if let Some(&load) =
|
|
|
|
self[target].outputs.iter().find(|&&n| self[n].kind == Kind::Load)
|
|
|
|
{
|
|
|
|
self[load].peep_triggers.push(target);
|
|
|
|
} else if value != VOID
|
2024-10-28 17:38:57 -05:00
|
|
|
&& self[value].kind != Kind::Load
|
|
|
|
&& self[store].kind == Kind::Stre
|
|
|
|
&& self[store].inputs[2] == region
|
2024-10-18 06:11:11 -05:00
|
|
|
{
|
2024-10-28 17:38:57 -05:00
|
|
|
if self[store].inputs[1] == value {
|
|
|
|
return Some(store);
|
|
|
|
}
|
|
|
|
|
2024-10-29 04:01:37 -05:00
|
|
|
let mut inps = self[target].inputs.clone();
|
|
|
|
inps[3] = self[store].inputs[3];
|
|
|
|
return Some(self.new_node_nop(self[target].ty, Kind::Stre, inps));
|
2024-10-18 06:11:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
K::Load => {
|
2024-11-10 10:32:24 -06:00
|
|
|
fn range_of(s: &Nodes, mut region: Nid, ty: ty::Id, tys: &Types) -> Range<usize> {
|
|
|
|
let loc = s.aclass_index(region).1;
|
|
|
|
let full_size = tys.size_of(
|
|
|
|
if matches!(s[loc].kind, Kind::Stck | Kind::Arg | Kind::Global { .. }) {
|
|
|
|
s[loc].ty
|
|
|
|
} else if let Some(ptr) = tys.base_of(s[loc].ty) {
|
|
|
|
ptr
|
|
|
|
} else {
|
|
|
|
return 0..usize::MAX;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let size = tys.size_of(ty);
|
2024-11-10 13:30:35 -06:00
|
|
|
let mut offset = 0;
|
2024-11-10 10:32:24 -06:00
|
|
|
loop {
|
2024-11-10 13:30:35 -06:00
|
|
|
match s[region].kind {
|
|
|
|
_ if region == loc => {
|
|
|
|
break offset as usize..offset as usize + size as usize
|
|
|
|
}
|
2024-11-10 10:32:24 -06:00
|
|
|
Kind::Assert { kind: AssertKind::NullCheck, .. } => {
|
2024-11-10 13:30:35 -06:00
|
|
|
region = s[region].inputs[2]
|
2024-11-10 10:32:24 -06:00
|
|
|
}
|
|
|
|
Kind::BinOp { op: TokenKind::Add | TokenKind::Sub }
|
2024-11-10 13:30:35 -06:00
|
|
|
if let Kind::CInt { value } = s[s[region].inputs[2]].kind =>
|
2024-11-10 10:32:24 -06:00
|
|
|
{
|
2024-11-10 13:30:35 -06:00
|
|
|
offset += value;
|
|
|
|
region = s[region].inputs[1];
|
2024-11-10 10:32:24 -06:00
|
|
|
}
|
2024-11-10 13:30:35 -06:00
|
|
|
_ => break 0..full_size as usize,
|
2024-11-10 10:32:24 -06:00
|
|
|
};
|
|
|
|
}
|
2024-10-29 04:31:52 -05:00
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
let &[ctrl, region, store] = self[target].inputs.as_slice() else { unreachable!() };
|
|
|
|
let load_range = range_of(self, region, self[target].ty, tys);
|
|
|
|
|
2024-11-10 11:56:33 -06:00
|
|
|
let mut cursor = store;
|
2024-11-10 10:32:24 -06:00
|
|
|
while cursor != MEM && self[cursor].kind != Kind::Phi {
|
|
|
|
if self[cursor].inputs[0] == ctrl
|
|
|
|
&& self[cursor].inputs[2] == region
|
|
|
|
&& self[cursor].ty == self[target].ty
|
2024-11-12 05:20:08 -06:00
|
|
|
&& (self[self[cursor].inputs[1]].kind != Kind::Load
|
|
|
|
|| (!self[target].outputs.is_empty()
|
|
|
|
&& self[target].outputs.iter().all(|&n| {
|
|
|
|
self[n].kind != Kind::Stre
|
|
|
|
|| self
|
|
|
|
.aclass_index(self[self[cursor].inputs[1]].inputs[1])
|
|
|
|
.0
|
|
|
|
!= self.aclass_index(self[n].inputs[2]).0
|
|
|
|
})))
|
2024-10-29 04:31:52 -05:00
|
|
|
{
|
2024-11-10 10:32:24 -06:00
|
|
|
return Some(self[cursor].inputs[1]);
|
|
|
|
}
|
|
|
|
let range = range_of(self, self[cursor].inputs[2], self[cursor].ty, tys);
|
|
|
|
if range.start >= load_range.end || range.end <= load_range.start {
|
2024-10-29 04:31:52 -05:00
|
|
|
cursor = self[cursor].inputs[3];
|
2024-11-10 11:56:33 -06:00
|
|
|
} else {
|
2024-11-10 13:30:35 -06:00
|
|
|
let reg = self.aclass_index(self[cursor].inputs[2]).1;
|
|
|
|
self[reg].peep_triggers.push(target);
|
2024-11-10 11:56:33 -06:00
|
|
|
break;
|
2024-10-29 04:31:52 -05:00
|
|
|
}
|
2024-11-10 10:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if store != cursor {
|
|
|
|
return Some(self.new_node(
|
|
|
|
self[target].ty,
|
|
|
|
Kind::Load,
|
|
|
|
[ctrl, region, cursor],
|
|
|
|
tys,
|
|
|
|
));
|
2024-09-28 08:13:32 -05:00
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-10-21 08:12:37 -05:00
|
|
|
K::Loop => {
|
2024-11-11 14:55:18 -06:00
|
|
|
if self[target].inputs[1] == NEVER || self[target].inputs[0] == NEVER {
|
2024-10-22 15:57:40 -05:00
|
|
|
self.lock(target);
|
2024-10-21 08:12:37 -05:00
|
|
|
for o in self[target].outputs.clone() {
|
|
|
|
if self[o].kind == Kind::Phi {
|
2024-11-11 14:55:18 -06:00
|
|
|
self.remove_node_lookup(target);
|
|
|
|
|
|
|
|
let prev = self[o].inputs[2];
|
|
|
|
self[o].inputs[2] = VOID;
|
|
|
|
self[VOID].outputs.push(o);
|
|
|
|
let index = self[prev].outputs.iter().position(|&n| n == o).unwrap();
|
|
|
|
self[prev].outputs.swap_remove(index);
|
|
|
|
self.lock(o);
|
|
|
|
self.remove(prev);
|
|
|
|
self.unlock(o);
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
self.replace(o, self[o].inputs[1]);
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
self.unlock(target);
|
2024-10-21 08:12:37 -05:00
|
|
|
return Some(self[target].inputs[0]);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
fn try_opt_cond(&self, target: Nid) -> CondOptRes {
|
|
|
|
let &[ctrl, cond, ..] = self[target].inputs.as_slice() else { unreachable!() };
|
|
|
|
if let Kind::CInt { value } = self[cond].kind {
|
|
|
|
return CondOptRes::Known { value: value != 0, pin: None };
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cursor = ctrl;
|
|
|
|
while cursor != ENTRY {
|
|
|
|
let ctrl = &self[cursor];
|
|
|
|
// TODO: do more inteligent checks on the condition
|
|
|
|
if matches!(ctrl.kind, Kind::Then | Kind::Else) {
|
|
|
|
let other_cond = self[ctrl.inputs[0]].inputs[1];
|
|
|
|
if let Some(value) = self.matches_cond(cond, other_cond) {
|
|
|
|
return CondOptRes::Known {
|
|
|
|
value: (ctrl.kind == Kind::Then) ^ !value,
|
|
|
|
pin: Some(cursor),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = self.idom(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
CondOptRes::Unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
fn matches_cond(&self, to_match: Nid, matches: Nid) -> Option<bool> {
|
|
|
|
use TokenKind as K;
|
|
|
|
let [tn, mn] = [&self[to_match], &self[matches]];
|
|
|
|
match (tn.kind, mn.kind) {
|
|
|
|
_ if to_match == matches => Some(true),
|
|
|
|
(Kind::BinOp { op: K::Ne }, Kind::BinOp { op: K::Eq })
|
|
|
|
| (Kind::BinOp { op: K::Eq }, Kind::BinOp { op: K::Ne })
|
|
|
|
if tn.inputs[1..] == mn.inputs[1..] =>
|
|
|
|
{
|
|
|
|
Some(false)
|
|
|
|
}
|
|
|
|
(_, Kind::BinOp { op: K::Band }) => self
|
|
|
|
.matches_cond(to_match, mn.inputs[1])
|
|
|
|
.or(self.matches_cond(to_match, mn.inputs[2])),
|
|
|
|
(_, Kind::BinOp { op: K::Bor }) => match (
|
|
|
|
self.matches_cond(to_match, mn.inputs[1]),
|
|
|
|
self.matches_cond(to_match, mn.inputs[2]),
|
|
|
|
) {
|
|
|
|
(None, Some(a)) | (Some(a), None) => Some(a),
|
|
|
|
(Some(b), Some(a)) if a == b => Some(a),
|
|
|
|
_ => None,
|
|
|
|
},
|
2024-11-07 09:05:16 -06:00
|
|
|
_ => None,
|
2024-11-04 12:18:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-26 13:29:31 -05:00
|
|
|
debug_assert_ne!(target, with, "{:?}", self[target]);
|
2024-10-28 17:38:57 -05:00
|
|
|
for out in self[target].outputs.clone() {
|
2024-09-07 21:20:10 -05:00
|
|
|
let index = self[out].inputs.iter().position(|&p| p == target).unwrap();
|
2024-09-08 05:00:07 -05:00
|
|
|
self.modify_input(out, index, with);
|
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-11-10 03:28:02 -06:00
|
|
|
debug_assert_ne!(
|
|
|
|
self[target].inputs[inp_index], with,
|
|
|
|
"{:?} {:?}",
|
|
|
|
self[target], self[with]
|
|
|
|
);
|
2024-09-04 09:54:34 -05:00
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
if self[target].is_not_gvnd() && (self[target].kind != Kind::Phi || with == 0) {
|
|
|
|
let prev = self[target].inputs[inp_index];
|
|
|
|
self[target].inputs[inp_index] = with;
|
|
|
|
self[with].outputs.push(target);
|
|
|
|
let index = self[prev].outputs.iter().position(|&o| o == target).unwrap();
|
|
|
|
self[prev].outputs.swap_remove(index);
|
|
|
|
self.remove(prev);
|
|
|
|
target
|
|
|
|
} else {
|
|
|
|
let prev = self[target].inputs[inp_index];
|
|
|
|
self[target].inputs[inp_index] = with;
|
|
|
|
let (entry, hash) = self.lookup.entry(target.key(&self.values), &self.values);
|
|
|
|
match entry {
|
|
|
|
hash_map::RawEntryMut::Occupied(other) => {
|
|
|
|
let rpl = other.get_key_value().0.value;
|
|
|
|
self[target].inputs[inp_index] = prev;
|
|
|
|
self.lookup.insert(target.key(&self.values), target, &self.values);
|
|
|
|
self.replace(target, rpl);
|
|
|
|
rpl
|
|
|
|
}
|
|
|
|
hash_map::RawEntryMut::Vacant(slot) => {
|
|
|
|
slot.insert(crate::ctx_map::Key { value: target, hash }, ());
|
|
|
|
let index = self[prev].outputs.iter().position(|&o| o == target).unwrap();
|
|
|
|
self[prev].outputs.swap_remove(index);
|
|
|
|
self[with].outputs.push(target);
|
|
|
|
self.remove(prev);
|
|
|
|
target
|
|
|
|
}
|
2024-09-07 21:20:10 -05:00
|
|
|
}
|
|
|
|
}
|
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-10-27 07:57:00 -05:00
|
|
|
#[expect(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-09 12:36:53 -05:00
|
|
|
match self[node].kind {
|
2024-11-04 05:38:47 -06:00
|
|
|
Kind::Assert { .. } | Kind::Start => unreachable!("{} {out}", self[node].kind),
|
2024-10-27 05:32:34 -05:00
|
|
|
Kind::End => return Ok(()),
|
2024-09-09 12:36:53 -05:00
|
|
|
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-11-03 03:15:03 -06:00
|
|
|
Kind::Die => write!(out, " die: "),
|
2024-09-09 12:36:53 -05:00
|
|
|
Kind::CInt { value } => write!(out, "cint: #{value:<4}"),
|
|
|
|
Kind::Phi => write!(out, " phi: "),
|
2024-10-19 12:37:02 -05:00
|
|
|
Kind::Arg => write!(
|
|
|
|
out,
|
|
|
|
" arg: {:<5}",
|
|
|
|
self[VOID].outputs.iter().position(|&n| n == node).unwrap() - 2
|
|
|
|
),
|
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())
|
|
|
|
}
|
2024-10-24 02:43:07 -05:00
|
|
|
Kind::Call { func, args: _ } => {
|
2024-11-04 12:18:37 -06:00
|
|
|
write!(out, "call: {func} {} ", self[node].depth.get())
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-10-20 03:37:48 -05:00
|
|
|
Kind::Global { global } => write!(out, "glob: {global:<5}"),
|
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-19 03:17:36 -05:00
|
|
|
Kind::Load => write!(out, "load: "),
|
2024-10-18 02:52:50 -05:00
|
|
|
Kind::Stre => write!(out, "stre: "),
|
2024-10-19 03:17:36 -05:00
|
|
|
Kind::Mem => write!(out, " mem: "),
|
2024-10-27 05:32:34 -05:00
|
|
|
Kind::Loops => write!(out, " loops: "),
|
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-11-05 07:52:30 -06:00
|
|
|
fn basic_blocks_low(
|
|
|
|
&mut self,
|
|
|
|
out: &mut String,
|
|
|
|
mut node: Nid,
|
|
|
|
visited: &mut BitSet,
|
|
|
|
) -> core::fmt::Result {
|
2024-09-12 11:42:21 -05:00
|
|
|
let iter = |nodes: &Nodes, node| nodes[node].outputs.clone().into_iter().rev();
|
2024-11-05 07:52:30 -06:00
|
|
|
while 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 => {
|
2024-11-04 12:18:37 -06:00
|
|
|
writeln!(out, "start: {}", self[node].depth.get())?;
|
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-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 => {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.basic_blocks_low(out, self[node].outputs[0], visited)?;
|
2024-09-09 12:36:53 -05:00
|
|
|
node = self[node].outputs[1];
|
|
|
|
}
|
|
|
|
Kind::Region => {
|
2024-09-15 13:14:56 -05:00
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"region{node}: {} {} {:?}",
|
2024-11-04 12:18:37 -06:00
|
|
|
self[node].depth.get(),
|
2024-11-07 01:52:41 -06:00
|
|
|
self[node].loop_depth.get(),
|
2024-11-04 12:18:37 -06:00
|
|
|
self[node].inputs
|
2024-09-15 13:14:56 -05:00
|
|
|
)?;
|
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}: {} {} {:?}",
|
2024-11-04 12:18:37 -06:00
|
|
|
self[node].depth.get(),
|
2024-11-07 01:52:41 -06:00
|
|
|
self[node].loop_depth.get(),
|
2024-11-04 12:18:37 -06:00
|
|
|
self[node].outputs
|
2024-09-15 13:14:56 -05:00
|
|
|
)?;
|
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;
|
|
|
|
}
|
2024-11-03 03:15:03 -06:00
|
|
|
Kind::Return | Kind::Die => {
|
2024-09-09 12:36:53 -05:00
|
|
|
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}: {} {} {:?}",
|
2024-11-04 12:18:37 -06:00
|
|
|
self[node].depth.get(),
|
2024-11-07 01:52:41 -06:00
|
|
|
self[node].loop_depth.get(),
|
2024-11-04 12:18:37 -06:00
|
|
|
self[node].outputs
|
2024-09-15 13:14:56 -05:00
|
|
|
)?;
|
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-10-26 05:09:53 -05:00
|
|
|
&& (self[node].outputs[0] != o || 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();
|
2024-11-05 07:52:30 -06:00
|
|
|
let mut visited = BitSet::default();
|
|
|
|
self.basic_blocks_low(&mut out, VOID, &mut visited).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
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn check_final_integrity(&self, disp: ty::Display) {
|
2024-10-22 09:54:32 -05:00
|
|
|
if !cfg!(debug_assertions) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-19 12:37:02 -05:00
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
let mut failed = false;
|
|
|
|
for (id, node) in self.iter() {
|
|
|
|
if node.lock_rc != 0 {
|
|
|
|
log::error!("{} {} {:?}", node.lock_rc, 0, node.kind);
|
|
|
|
failed = true;
|
|
|
|
}
|
2024-10-27 05:32:34 -05:00
|
|
|
if !matches!(node.kind, Kind::End | Kind::Mem | Kind::Arg | Kind::Loops)
|
|
|
|
&& node.outputs.is_empty()
|
|
|
|
{
|
2024-10-28 17:38:57 -05:00
|
|
|
log::error!("outputs are empry {id} {:?}", node);
|
2024-10-22 15:57:40 -05:00
|
|
|
failed = true;
|
|
|
|
}
|
2024-10-27 05:32:34 -05:00
|
|
|
if node.inputs.first() == Some(&NEVER) && id != NEVER {
|
|
|
|
log::error!("is unreachable but still present {id} {:?}", node.kind);
|
|
|
|
failed = true;
|
|
|
|
}
|
2024-11-04 05:38:47 -06:00
|
|
|
if node.outputs.contains(&id) && !matches!(node.kind, Kind::Loop | Kind::End) {
|
|
|
|
log::error!("node depends on it self and its not a loop {id} {:?}", node);
|
|
|
|
failed = true;
|
|
|
|
}
|
2024-10-22 09:54:32 -05:00
|
|
|
}
|
2024-10-26 03:25:42 -05:00
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
if failed {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.graphviz_in_browser(disp);
|
2024-10-22 15:57:40 -05:00
|
|
|
panic!()
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
2024-09-07 20:12:57 -05:00
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
fn load_loop_var(&mut self, index: usize, var: &mut Variable, loops: &mut [Loop]) {
|
2024-10-22 15:57:40 -05:00
|
|
|
if var.value() != VOID {
|
2024-09-08 10:11:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-10 02:17:43 -06:00
|
|
|
debug_assert!(!var.ptr);
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
let [loops @ .., loob] = loops else { unreachable!() };
|
2024-10-19 03:17:36 -05:00
|
|
|
let node = loob.node;
|
2024-10-28 10:18:53 -05:00
|
|
|
let lvar = &mut loob.scope.vars[index];
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-11-10 02:17:43 -06:00
|
|
|
debug_assert!(!lvar.ptr);
|
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
self.load_loop_var(index, lvar, loops);
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
if !self[lvar.value()].is_lazy_phi(node) {
|
2024-10-30 07:45:19 -05:00
|
|
|
let lvalue = lvar.value();
|
|
|
|
let inps = [node, lvalue, VOID];
|
2024-10-22 15:57:40 -05:00
|
|
|
lvar.set_value(self.new_node_nop(lvar.ty, Kind::Phi, inps), self);
|
2024-11-04 05:38:47 -06:00
|
|
|
|
|
|
|
self.pass_aclass(self.aclass_index(lvalue).1, lvar.value());
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
var.set_value(lvar.value(), self);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
|
2024-11-03 01:59:42 -06:00
|
|
|
fn load_loop_aclass(&mut self, index: usize, aclass: &mut AClass, loops: &mut [Loop]) {
|
|
|
|
if aclass.last_store.get() != VOID {
|
2024-10-28 10:18:53 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [loops @ .., loob] = loops else { unreachable!() };
|
|
|
|
let node = loob.node;
|
|
|
|
let lvar = &mut loob.scope.aclasses[index];
|
|
|
|
|
|
|
|
self.load_loop_aclass(index, lvar, loops);
|
|
|
|
|
|
|
|
if !self[lvar.last_store.get()].is_lazy_phi(node) {
|
|
|
|
let inps = [node, lvar.last_store.get(), VOID];
|
|
|
|
lvar.last_store.set(self.new_node_nop(ty::Id::VOID, Kind::Phi, inps), self);
|
|
|
|
}
|
2024-11-03 01:59:42 -06:00
|
|
|
aclass.last_store.set(lvar.last_store.get(), self);
|
2024-10-28 10:18:53 -05:00
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
fn check_dominance(&mut self, nd: Nid, min: Nid, check_outputs: bool) {
|
2024-10-19 12:37:02 -05:00
|
|
|
if !cfg!(debug_assertions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:14:56 -05:00
|
|
|
let node = self[nd].clone();
|
|
|
|
for &i in node.inputs.iter() {
|
2024-10-27 15:34:03 -05:00
|
|
|
let dom = self.idom(i);
|
2024-09-15 13:14:56 -05:00
|
|
|
debug_assert!(
|
|
|
|
self.dominates(dom, min),
|
|
|
|
"{dom} {min} {node:?} {:?}",
|
|
|
|
self.basic_blocks()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if check_outputs {
|
|
|
|
for &o in node.outputs.iter() {
|
2024-10-27 15:34:03 -05:00
|
|
|
let dom = self.use_block(nd, o);
|
2024-09-15 13:14:56 -05:00
|
|
|
debug_assert!(
|
|
|
|
self.dominates(min, dom),
|
|
|
|
"{min} {dom} {node:?} {:?}",
|
|
|
|
self.basic_blocks()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 02:17:43 -06:00
|
|
|
fn dominates(&self, dominator: Nid, mut dominated: Nid) -> bool {
|
2024-09-15 13:14:56 -05:00
|
|
|
loop {
|
|
|
|
if dominator == dominated {
|
|
|
|
break true;
|
|
|
|
}
|
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
if self.idepth(dominator) > self.idepth(dominated) {
|
2024-09-15 13:14:56 -05:00
|
|
|
break false;
|
|
|
|
}
|
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
dominated = self.idom(dominated);
|
2024-09-08 10:11:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-11-07 01:52:41 -06:00
|
|
|
|
|
|
|
fn is_data_dep(&self, nid: Nid, n: Nid) -> bool {
|
|
|
|
match self[n].kind {
|
2024-11-10 02:17:43 -06:00
|
|
|
Kind::Return => self[n].inputs[1] == nid,
|
|
|
|
_ if self.is_cfg(n) && !matches!(self[n].kind, Kind::Call { .. } | Kind::If) => false,
|
2024-11-07 01:52:41 -06:00
|
|
|
Kind::Stre => self[n].inputs[3] != nid,
|
|
|
|
Kind::Load => self[n].inputs[2] != nid,
|
|
|
|
_ => self[n].inputs[0] != nid || self[n].inputs[1..].contains(&nid),
|
|
|
|
}
|
|
|
|
}
|
2024-11-10 02:17:43 -06:00
|
|
|
|
|
|
|
fn this_or_delegates<'a>(&'a self, source: Nid, target: &'a Nid) -> (Nid, &'a [Nid]) {
|
|
|
|
if self[*target].lock_rc == 0 {
|
|
|
|
(source, core::slice::from_ref(target))
|
|
|
|
} else {
|
|
|
|
(*target, self[*target].outputs.as_slice())
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
enum CondOptRes {
|
|
|
|
Unknown,
|
|
|
|
Known { value: bool, pin: Option<Nid> },
|
|
|
|
}
|
|
|
|
|
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-10-22 09:53:48 -05:00
|
|
|
self.values[index as usize].as_ref().unwrap_or_else(|(_, bt)| panic!("{index} {bt:#?}"))
|
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-10-22 09:53:48 -05:00
|
|
|
self.values[index as usize].as_mut().unwrap_or_else(|(_, bt)| panic!("{index} {bt:#?}"))
|
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-11-03 14:13:24 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
|
|
pub enum AssertKind {
|
|
|
|
NullCheck,
|
2024-11-04 12:18:37 -06:00
|
|
|
UnwrapCheck,
|
2024-11-03 14:13:24 -06: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,
|
2024-10-27 05:32:34 -05:00
|
|
|
// [VOID]
|
2024-09-30 12:09:17 -05:00
|
|
|
Mem,
|
2024-10-27 05:32:34 -05:00
|
|
|
// [VOID]
|
|
|
|
Loops,
|
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-11-03 03:15:03 -06:00
|
|
|
Die,
|
|
|
|
// [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-10-19 12:37:02 -05:00
|
|
|
Arg,
|
2024-09-20 12:01:44 -05:00
|
|
|
// [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-10-20 03:37:48 -05:00
|
|
|
// [ctrl]
|
|
|
|
Global {
|
|
|
|
global: ty::Global,
|
|
|
|
},
|
2024-09-20 12:01:44 -05:00
|
|
|
// [ctrl, ...args]
|
2024-09-12 11:42:21 -05:00
|
|
|
Call {
|
|
|
|
func: ty::Func,
|
2024-10-24 02:43:07 -05:00
|
|
|
args: ty::Tuple,
|
2024-09-12 11:42:21 -05:00
|
|
|
},
|
2024-11-04 12:18:37 -06:00
|
|
|
// [ctrl, cond, value]
|
|
|
|
Assert {
|
|
|
|
kind: AssertKind,
|
|
|
|
pos: Pos,
|
|
|
|
},
|
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-11-12 12:02:29 -06:00
|
|
|
fn is_call(&self) -> bool {
|
|
|
|
matches!(self, Kind::Call { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_eca(&self) -> bool {
|
|
|
|
matches!(self, Kind::Call { func: ty::Func::ECA, .. })
|
|
|
|
}
|
|
|
|
|
2024-09-09 12:36:53 -05:00
|
|
|
fn is_pinned(&self) -> bool {
|
2024-10-27 05:32:34 -05:00
|
|
|
self.is_cfg() || matches!(self, Self::Phi | Self::Arg | Self::Mem | Self::Loops)
|
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-11-03 03:15:03 -06:00
|
|
|
| Self::Die
|
2024-09-27 09:53:28 -05:00
|
|
|
| Self::Entry
|
|
|
|
| Self::Then
|
|
|
|
| Self::Else
|
2024-09-19 06:40:03 -05:00
|
|
|
| Self::Call { .. }
|
2024-11-03 14:13:24 -06:00
|
|
|
| Self::Assert { .. }
|
2024-09-19 06:40:03 -05:00
|
|
|
| Self::If
|
|
|
|
| Self::Region
|
|
|
|
| Self::Loop
|
2024-09-09 12:36:53 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ends_basic_block(&self) -> bool {
|
2024-11-03 03:15:03 -06:00
|
|
|
matches!(self, Self::Return | Self::If | Self::End | Self::Die)
|
2024-09-09 12:36:53 -05:00
|
|
|
}
|
2024-10-26 13:29:31 -05:00
|
|
|
|
2024-11-07 01:52:41 -06:00
|
|
|
fn starts_basic_block(&self) -> bool {
|
|
|
|
matches!(self, Self::Region | Self::Loop | Self::Start | Kind::Then | Kind::Else)
|
|
|
|
}
|
|
|
|
|
2024-10-26 13:29:31 -05:00
|
|
|
fn is_peeped(&self) -> bool {
|
2024-10-28 17:38:57 -05:00
|
|
|
!matches!(self, Self::End | Self::Arg | Self::Mem | Self::Loops)
|
2024-10-26 13:29:31 -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-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-10-29 03:04:49 -05:00
|
|
|
peep_triggers: Vc,
|
2024-10-30 12:42:25 -05:00
|
|
|
clobbers: BitSet,
|
2024-09-27 09:53:28 -05:00
|
|
|
ty: ty::Id,
|
2024-11-04 12:18:37 -06:00
|
|
|
depth: Cell<IDomDepth>,
|
2024-09-13 07:15:45 -05:00
|
|
|
lock_rc: LockRc,
|
2024-11-07 01:52:41 -06:00
|
|
|
loop_depth: Cell<LoopDepth>,
|
2024-10-30 12:42:25 -05:00
|
|
|
aclass: AClassId,
|
2024-10-30 07:45:19 -05:00
|
|
|
antidep: Nid,
|
2024-11-08 04:51:10 -06:00
|
|
|
pos: Pos,
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
|
|
|
fn is_dangling(&self) -> bool {
|
2024-10-29 04:31:52 -05:00
|
|
|
self.outputs.len() + self.lock_rc as usize == 0 && self.kind != Kind::Arg
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
fn is_lazy_phi(&self, loob: Nid) -> bool {
|
|
|
|
self.kind == Kind::Phi && self.inputs[2] == 0 && self.inputs[0] == loob
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
2024-10-19 12:37:02 -05:00
|
|
|
|
2024-10-20 11:49:41 -05:00
|
|
|
fn is_not_gvnd(&self) -> bool {
|
2024-10-21 08:12:37 -05:00
|
|
|
(self.kind == Kind::Phi && self.inputs[2] == 0)
|
2024-10-30 07:45:19 -05:00
|
|
|
|| matches!(self.kind, Kind::Arg | Kind::Stck | Kind::Stre)
|
2024-10-28 17:38:57 -05:00
|
|
|
|| self.kind.is_cfg()
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
|
|
|
|
fn is_mem(&self) -> bool {
|
|
|
|
matches!(self.kind, Kind::Stre | Kind::Load | Kind::Stck)
|
|
|
|
}
|
2024-11-07 01:52:41 -06:00
|
|
|
|
|
|
|
fn is_data_phi(&self) -> bool {
|
|
|
|
self.kind == Kind::Phi && self.ty != ty::Id::VOID
|
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
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-10-21 11:57:23 -05:00
|
|
|
#[derive(Clone)]
|
2024-09-02 17:07:20 -05:00
|
|
|
struct Loop {
|
2024-09-05 18:17:54 -05:00
|
|
|
node: Nid,
|
2024-10-26 05:09:53 -05:00
|
|
|
ctrl: [StrongRef; 2],
|
2024-10-18 06:11:11 -05:00
|
|
|
ctrl_scope: [Scope; 2],
|
|
|
|
scope: Scope,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
mod strong_ref {
|
2024-10-22 15:57:40 -05:00
|
|
|
use {
|
|
|
|
super::{Kind, Nid, Nodes},
|
2024-10-26 05:09:53 -05:00
|
|
|
crate::debug,
|
|
|
|
core::ops::Not,
|
2024-10-22 15:57:40 -05:00
|
|
|
};
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct StrongRef(Nid);
|
2024-10-22 15:57:40 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
impl StrongRef {
|
|
|
|
pub const DEFAULT: Self = Self(Nid::MAX);
|
|
|
|
|
|
|
|
pub fn new(value: Nid, nodes: &mut Nodes) -> Self {
|
2024-10-22 15:57:40 -05:00
|
|
|
nodes.lock(value);
|
2024-10-26 05:09:53 -05:00
|
|
|
Self(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self) -> Nid {
|
|
|
|
debug_assert!(self.0 != Nid::MAX);
|
|
|
|
self.0
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
pub fn unwrap(self, nodes: &mut Nodes) -> Option<Nid> {
|
|
|
|
let nid = self.0;
|
|
|
|
if nid != Nid::MAX {
|
|
|
|
nodes.unlock(nid);
|
|
|
|
core::mem::forget(self);
|
|
|
|
Some(nid)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
pub fn set(&mut self, mut new_value: Nid, nodes: &mut Nodes) -> Nid {
|
|
|
|
nodes.unlock(self.0);
|
|
|
|
core::mem::swap(&mut self.0, &mut new_value);
|
|
|
|
nodes.lock(self.0);
|
2024-10-22 15:57:40 -05:00
|
|
|
new_value
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dup(&self, nodes: &mut Nodes) -> Self {
|
2024-10-26 05:09:53 -05:00
|
|
|
nodes.lock(self.0);
|
|
|
|
Self(self.0)
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
pub fn remove(self, nodes: &mut Nodes) -> Option<Nid> {
|
|
|
|
let ret = nodes.unlock_remove(self.0).not().then_some(self.0);
|
2024-10-24 05:28:18 -05:00
|
|
|
core::mem::forget(self);
|
2024-10-26 05:09:53 -05:00
|
|
|
ret
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
pub fn set_remove(&mut self, new_value: Nid, nodes: &mut Nodes) {
|
|
|
|
let old = self.set(new_value, nodes);
|
2024-10-22 15:57:40 -05:00
|
|
|
nodes.remove(old);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_ignore_arg(self, nodes: &mut Nodes) {
|
2024-10-26 05:09:53 -05:00
|
|
|
if nodes[self.0].kind == Kind::Arg {
|
|
|
|
nodes.unlock(self.0);
|
2024-10-22 15:57:40 -05:00
|
|
|
} else {
|
2024-10-26 05:09:53 -05:00
|
|
|
nodes.unlock_remove(self.0);
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
2024-10-24 05:28:18 -05:00
|
|
|
core::mem::forget(self);
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
2024-10-26 05:09:53 -05:00
|
|
|
|
|
|
|
pub fn soft_remove(self, nodes: &mut Nodes) -> Nid {
|
|
|
|
let nid = self.0;
|
|
|
|
nodes.unlock(self.0);
|
|
|
|
core::mem::forget(self);
|
|
|
|
nid
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_live(&self) -> bool {
|
|
|
|
self.0 != Nid::MAX
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
impl Default for StrongRef {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for StrongRef {
|
2024-10-22 15:57:40 -05:00
|
|
|
fn drop(&mut self) {
|
2024-10-26 05:09:53 -05:00
|
|
|
if self.0 != Nid::MAX && !debug::panicking() {
|
2024-10-22 15:57:40 -05:00
|
|
|
panic!("variable unproperly deinitialized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-26 05:09:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// makes sure value inside is laways locked for this instance of variable
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
struct Variable {
|
|
|
|
id: Ident,
|
|
|
|
ty: ty::Id,
|
|
|
|
ptr: bool,
|
|
|
|
value: StrongRef,
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
impl Variable {
|
|
|
|
fn new(id: Ident, ty: ty::Id, ptr: bool, value: Nid, nodes: &mut Nodes) -> Self {
|
|
|
|
Self { id, ty, ptr, value: StrongRef::new(value, nodes) }
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
fn value(&self) -> Nid {
|
|
|
|
self.value.get()
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
fn set_value(&mut self, new_value: Nid, nodes: &mut Nodes) -> Nid {
|
|
|
|
self.value.set(new_value, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dup(&self, nodes: &mut Nodes) -> Self {
|
|
|
|
Self { id: self.id, ty: self.ty, ptr: self.ptr, value: self.value.dup(nodes) }
|
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
fn remove(self, nodes: &mut Nodes) {
|
|
|
|
self.value.remove(nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_value_remove(&mut self, new_value: Nid, nodes: &mut Nodes) {
|
|
|
|
self.value.set_remove(new_value, nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_ignore_arg(self, nodes: &mut Nodes) {
|
|
|
|
self.value.remove_ignore_arg(nodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone)]
|
2024-10-28 10:18:53 -05:00
|
|
|
pub struct AClass {
|
|
|
|
last_store: StrongRef,
|
2024-10-30 07:45:19 -05:00
|
|
|
clobber: StrongRef,
|
2024-10-26 05:09:53 -05:00
|
|
|
}
|
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
impl AClass {
|
|
|
|
fn dup(&self, nodes: &mut Nodes) -> Self {
|
2024-10-30 07:45:19 -05:00
|
|
|
Self { last_store: self.last_store.dup(nodes), clobber: self.clobber.dup(nodes) }
|
2024-10-28 10:18:53 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 07:45:19 -05:00
|
|
|
fn remove(self, nodes: &mut Nodes) {
|
2024-10-28 10:18:53 -05:00
|
|
|
self.last_store.remove(nodes);
|
2024-10-30 07:45:19 -05:00
|
|
|
self.clobber.remove(nodes);
|
2024-10-26 05:09:53 -05:00
|
|
|
}
|
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
fn new(nodes: &mut Nodes) -> Self {
|
2024-10-30 07:45:19 -05:00
|
|
|
Self { last_store: StrongRef::new(MEM, nodes), clobber: StrongRef::new(VOID, nodes) }
|
2024-10-28 10:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct Scope {
|
|
|
|
vars: Vec<Variable>,
|
|
|
|
aclasses: Vec<AClass>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Scope {
|
2024-10-26 05:09:53 -05:00
|
|
|
fn dup(&self, nodes: &mut Nodes) -> Self {
|
|
|
|
Self {
|
|
|
|
vars: self.vars.iter().map(|v| v.dup(nodes)).collect(),
|
2024-10-28 10:18:53 -05:00
|
|
|
aclasses: self.aclasses.iter().map(|v| v.dup(nodes)).collect(),
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
2024-10-19 03:17:36 -05:00
|
|
|
}
|
2024-10-26 05:09:53 -05:00
|
|
|
|
|
|
|
fn clear(&mut self, nodes: &mut Nodes) {
|
|
|
|
self.vars.drain(..).for_each(|n| n.remove(nodes));
|
2024-10-28 10:18:53 -05:00
|
|
|
self.aclasses.drain(..).for_each(|l| l.remove(nodes));
|
2024-10-26 05:09:53 -05:00
|
|
|
}
|
2024-10-18 06:11:11 -05:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:23 -05:00
|
|
|
#[derive(Default, Clone)]
|
2024-11-05 07:52:30 -06:00
|
|
|
pub struct ItemCtx {
|
2024-11-08 03:25:34 -06:00
|
|
|
file: Module,
|
2024-11-08 01:36:00 -06:00
|
|
|
pos: Vec<Pos>,
|
2024-09-02 17:07:20 -05:00
|
|
|
ret: Option<ty::Id>,
|
|
|
|
task_base: usize,
|
2024-10-23 05:26:07 -05:00
|
|
|
inline_var_base: usize,
|
2024-11-03 01:59:42 -06:00
|
|
|
inline_aclass_base: usize,
|
2024-10-21 08:12:37 -05:00
|
|
|
inline_depth: u16,
|
2024-10-26 05:09:53 -05:00
|
|
|
inline_ret: Option<(Value, StrongRef, Scope)>,
|
2024-09-03 10:51:28 -05:00
|
|
|
nodes: Nodes,
|
2024-10-26 05:09:53 -05:00
|
|
|
ctrl: StrongRef,
|
2024-09-02 17:07:20 -05:00
|
|
|
loops: Vec<Loop>,
|
2024-10-18 06:11:11 -05:00
|
|
|
scope: Scope,
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemCtx {
|
2024-11-08 03:25:34 -06:00
|
|
|
fn init(&mut self, file: Module, ret: Option<ty::Id>, task_base: usize) {
|
2024-10-19 12:37:02 -05:00
|
|
|
debug_assert_eq!(self.loops.len(), 0);
|
|
|
|
debug_assert_eq!(self.scope.vars.len(), 0);
|
2024-11-05 07:52:30 -06:00
|
|
|
debug_assert_eq!(self.scope.aclasses.len(), 0);
|
|
|
|
debug_assert!(self.inline_ret.is_none());
|
|
|
|
debug_assert_eq!(self.inline_depth, 0);
|
|
|
|
debug_assert_eq!(self.inline_var_base, 0);
|
|
|
|
debug_assert_eq!(self.inline_aclass_base, 0);
|
2024-10-24 02:43:07 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
self.file = file;
|
|
|
|
self.ret = ret;
|
|
|
|
self.task_base = task_base;
|
|
|
|
|
|
|
|
self.nodes.clear();
|
|
|
|
self.scope.vars.clear();
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
let start = self.nodes.new_node_nop(ty::Id::VOID, Kind::Start, []);
|
2024-10-19 12:37:02 -05:00
|
|
|
debug_assert_eq!(start, VOID);
|
2024-11-10 10:32:24 -06:00
|
|
|
let end = self.nodes.new_node_nop(ty::Id::NEVER, Kind::End, []);
|
2024-10-19 12:37:02 -05:00
|
|
|
debug_assert_eq!(end, NEVER);
|
|
|
|
self.nodes.lock(end);
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ctrl = StrongRef::new(
|
|
|
|
self.nodes.new_node_nop(ty::Id::VOID, Kind::Entry, [VOID]),
|
|
|
|
&mut self.nodes,
|
|
|
|
);
|
2024-10-26 05:09:53 -05:00
|
|
|
debug_assert_eq!(self.ctrl.get(), ENTRY);
|
2024-11-10 10:32:24 -06:00
|
|
|
let mem = self.nodes.new_node_nop(ty::Id::VOID, Kind::Mem, [VOID]);
|
2024-10-19 12:37:02 -05:00
|
|
|
debug_assert_eq!(mem, MEM);
|
|
|
|
self.nodes.lock(mem);
|
2024-11-10 10:32:24 -06:00
|
|
|
let loops = self.nodes.new_node_nop(ty::Id::VOID, Kind::Loops, [VOID]);
|
2024-10-27 05:32:34 -05:00
|
|
|
debug_assert_eq!(loops, LOOPS);
|
|
|
|
self.nodes.lock(loops);
|
2024-10-29 09:04:07 -05:00
|
|
|
self.scope.aclasses.push(AClass::new(&mut self.nodes)); // DEFAULT
|
|
|
|
self.scope.aclasses.push(AClass::new(&mut self.nodes)); // GLOBAL
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
fn finalize(&mut self, stack: &mut Vec<Nid>, tys: &Types, _files: &[parser::Ast]) {
|
2024-10-22 15:57:40 -05:00
|
|
|
self.scope.clear(&mut self.nodes);
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::take(&mut self.ctrl).soft_remove(&mut self.nodes);
|
2024-10-28 17:38:57 -05:00
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
self.nodes.iter_peeps(1000, stack, tys);
|
2024-11-04 12:18:37 -06:00
|
|
|
}
|
2024-10-28 17:38:57 -05:00
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
fn unlock(&mut self) {
|
2024-10-27 05:32:34 -05:00
|
|
|
self.nodes.unlock(MEM);
|
|
|
|
self.nodes.unlock(NEVER);
|
|
|
|
self.nodes.unlock(LOOPS);
|
2024-10-19 12:37:02 -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]);
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-11-05 07:52:30 -06:00
|
|
|
fn with_ty(self, ty: ty::Id) -> Self {
|
2024-10-25 08:07:39 -05:00
|
|
|
Self { ty: Some(ty) }
|
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)]
|
2024-11-05 07:52:30 -06:00
|
|
|
pub struct Pool {
|
2024-09-02 17:07:20 -05:00
|
|
|
cis: Vec<ItemCtx>,
|
2024-10-19 12:53:43 -05:00
|
|
|
used_cis: usize,
|
2024-10-27 05:32:34 -05:00
|
|
|
nid_stack: Vec<Nid>,
|
2024-11-05 07:52:30 -06:00
|
|
|
nid_set: BitSet,
|
2024-10-19 12:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Pool {
|
2024-11-05 07:52:30 -06:00
|
|
|
fn push_ci(
|
2024-10-19 12:53:43 -05:00
|
|
|
&mut self,
|
2024-11-08 03:25:34 -06:00
|
|
|
file: Module,
|
2024-10-19 12:53:43 -05:00
|
|
|
ret: Option<ty::Id>,
|
|
|
|
task_base: usize,
|
2024-10-20 03:37:48 -05:00
|
|
|
target: &mut ItemCtx,
|
2024-10-19 12:53:43 -05:00
|
|
|
) {
|
|
|
|
if let Some(slot) = self.cis.get_mut(self.used_cis) {
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::swap(slot, target);
|
2024-10-19 12:53:43 -05:00
|
|
|
} else {
|
|
|
|
self.cis.push(ItemCtx::default());
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::swap(self.cis.last_mut().unwrap(), target);
|
2024-10-19 12:53:43 -05:00
|
|
|
}
|
2024-10-20 03:37:48 -05:00
|
|
|
target.init(file, ret, task_base);
|
2024-10-19 12:53:43 -05:00
|
|
|
self.used_cis += 1;
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn pop_ci(&mut self, target: &mut ItemCtx) {
|
2024-10-19 12:53:43 -05:00
|
|
|
self.used_cis -= 1;
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::swap(&mut self.cis[self.used_cis], target);
|
2024-10-19 12:53:43 -05:00
|
|
|
}
|
2024-10-21 11:57:23 -05:00
|
|
|
|
|
|
|
fn save_ci(&mut self, ci: &ItemCtx) {
|
|
|
|
if let Some(slot) = self.cis.get_mut(self.used_cis) {
|
|
|
|
slot.clone_from(ci);
|
|
|
|
} else {
|
|
|
|
self.cis.push(ci.clone());
|
|
|
|
}
|
|
|
|
self.used_cis += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_ci(&mut self, dst: &mut ItemCtx) {
|
|
|
|
self.used_cis -= 1;
|
2024-10-22 15:57:40 -05:00
|
|
|
dst.scope.clear(&mut dst.nodes);
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::take(&mut dst.ctrl).remove(&mut dst.nodes);
|
|
|
|
*dst = mem::take(&mut self.cis[self.used_cis]);
|
2024-10-21 11:57:23 -05:00
|
|
|
}
|
2024-10-25 15:59:01 -05:00
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
debug_assert_eq!(self.used_cis, 0);
|
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
|
2024-10-17 12:32:10 -05:00
|
|
|
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
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn new(id: Nid) -> Self {
|
2024-10-17 12:32:10 -05:00
|
|
|
Self { id, ..Default::default() }
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn var(id: usize) -> Self {
|
2024-10-17 15:29:09 -05:00
|
|
|
Self { id: u16::MAX - (id as Nid), var: true, ..Default::default() }
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn ptr(id: Nid) -> Self {
|
2024-10-17 15:29:09 -05:00
|
|
|
Self { id, ptr: true, ..Default::default() }
|
2024-10-17 12:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-11-08 03:25:34 -06:00
|
|
|
fn ty(self, ty: ty::Id) -> Self {
|
|
|
|
Self { ty, ..self }
|
2024-10-17 12:32:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[derive(Default)]
|
2024-10-25 15:59:01 -05:00
|
|
|
pub struct CodegenCtx {
|
|
|
|
pub parser: parser::Ctx,
|
2024-09-02 17:07:20 -05:00
|
|
|
tys: Types,
|
|
|
|
pool: Pool,
|
2024-10-20 03:37:48 -05:00
|
|
|
ct: Comptime,
|
2024-11-05 07:52:30 -06:00
|
|
|
ct_backend: HbvmBackend,
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
impl CodegenCtx {
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.parser.clear();
|
|
|
|
self.tys.clear();
|
|
|
|
self.pool.clear();
|
|
|
|
self.ct.clear();
|
2024-10-20 08:16:55 -05:00
|
|
|
}
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
2024-10-20 08:16:55 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
pub struct Codegen<'a> {
|
|
|
|
pub files: &'a [parser::Ast],
|
2024-11-08 01:36:00 -06:00
|
|
|
pub errors: &'a RefCell<String>,
|
2024-10-25 15:59:01 -05:00
|
|
|
tys: &'a mut Types,
|
|
|
|
ci: ItemCtx,
|
|
|
|
pool: &'a mut Pool,
|
|
|
|
ct: &'a mut Comptime,
|
2024-11-05 07:52:30 -06:00
|
|
|
ct_backend: &'a mut HbvmBackend,
|
|
|
|
backend: &'a mut dyn Backend,
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
2024-10-20 08:16:55 -05:00
|
|
|
|
2024-11-08 01:36:00 -06:00
|
|
|
impl Drop for Codegen<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if debug::panicking() {
|
|
|
|
if let Some(&pos) = self.ci.pos.last() {
|
|
|
|
self.report(pos, "panic occured here");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.errors.borrow().is_empty() {
|
|
|
|
log::error!("{}", self.errors.borrow());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
impl<'a> Codegen<'a> {
|
2024-11-05 07:52:30 -06:00
|
|
|
pub fn new(
|
|
|
|
backend: &'a mut dyn Backend,
|
|
|
|
files: &'a [parser::Ast],
|
|
|
|
ctx: &'a mut CodegenCtx,
|
|
|
|
) -> Self {
|
2024-10-25 15:59:01 -05:00
|
|
|
Self {
|
|
|
|
files,
|
2024-11-08 01:36:00 -06:00
|
|
|
errors: &ctx.parser.errors,
|
2024-10-25 15:59:01 -05:00
|
|
|
tys: &mut ctx.tys,
|
|
|
|
ci: Default::default(),
|
|
|
|
pool: &mut ctx.pool,
|
|
|
|
ct: &mut ctx.ct,
|
2024-11-05 07:52:30 -06:00
|
|
|
ct_backend: &mut ctx.ct_backend,
|
|
|
|
backend,
|
2024-10-20 09:43:25 -05:00
|
|
|
}
|
2024-10-20 08:16:55 -05:00
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
pub fn generate(&mut self, entry: Module) {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.find_type(0, entry, entry, Err("main"), self.files);
|
|
|
|
if self.tys.ins.funcs.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-08 03:25:34 -06:00
|
|
|
self.make_func_reachable(ty::Func::MAIN);
|
2024-11-05 07:52:30 -06:00
|
|
|
self.complete_call_graph();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assemble_comptime(&mut self) -> Comptime {
|
|
|
|
self.ct.code.clear();
|
2024-11-08 03:25:34 -06:00
|
|
|
self.ct_backend.assemble_bin(ty::Func::MAIN, self.tys, &mut self.ct.code);
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct.reset();
|
|
|
|
core::mem::take(self.ct)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assemble(&mut self, buf: &mut Vec<u8>) {
|
2024-11-08 03:25:34 -06:00
|
|
|
self.backend.assemble_bin(ty::Func::MAIN, self.tys, buf);
|
2024-11-05 07:52:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disasm(&mut self, output: &mut String, bin: &[u8]) -> Result<(), DisasmError> {
|
|
|
|
self.backend.disasm(bin, &mut |_| {}, self.tys, self.files, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_embeds(&mut self, embeds: Vec<Vec<u8>>) {
|
2024-11-08 03:25:34 -06:00
|
|
|
for data in embeds {
|
|
|
|
let g = Global {
|
2024-11-05 07:52:30 -06:00
|
|
|
ty: self.tys.make_array(ty::Id::U8, data.len() as _),
|
|
|
|
data,
|
|
|
|
..Default::default()
|
2024-11-08 03:25:34 -06:00
|
|
|
};
|
|
|
|
self.tys.ins.globals.push(g);
|
|
|
|
}
|
2024-11-05 07:52:30 -06:00
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
fn emit_and_eval(&mut self, file: Module, ret: ty::Id, ret_loc: &mut [u8]) -> u64 {
|
2024-10-27 07:57:00 -05:00
|
|
|
let mut rets =
|
|
|
|
self.ci.nodes[NEVER].inputs.iter().filter(|&&i| self.ci.nodes[i].kind == Kind::Return);
|
|
|
|
if let Some(&ret) = rets.next()
|
|
|
|
&& rets.next().is_none()
|
|
|
|
&& let Kind::CInt { value } = self.ci.nodes[self.ci.nodes[ret].inputs[1]].kind
|
|
|
|
{
|
|
|
|
if let len @ 1..=8 = ret_loc.len() {
|
|
|
|
ret_loc.copy_from_slice(&value.to_ne_bytes()[..len])
|
|
|
|
}
|
|
|
|
return value as _;
|
|
|
|
}
|
|
|
|
|
2024-10-25 04:29:54 -05:00
|
|
|
if !self.complete_call_graph() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
let fuc = self.tys.ins.funcs.push(Func {
|
2024-10-25 04:29:54 -05:00
|
|
|
file,
|
2024-11-05 07:52:30 -06:00
|
|
|
sig: Some(Sig { args: Tuple::empty(), ret }),
|
2024-10-25 04:29:54 -05:00
|
|
|
..Default::default()
|
2024-11-05 07:52:30 -06:00
|
|
|
});
|
2024-10-25 04:29:54 -05:00
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct_backend.emit_ct_body(fuc, &mut self.ci.nodes, self.tys, self.files);
|
2024-10-25 04:29:54 -05:00
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
// TODO: return them back
|
2024-10-25 04:29:54 -05:00
|
|
|
|
2024-11-07 01:52:41 -06:00
|
|
|
let entry = self.ct_backend.assemble_reachable(fuc, self.tys, &mut self.ct.code).entry;
|
2024-10-25 04:29:54 -05:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let mut vc = String::new();
|
2024-11-05 07:52:30 -06:00
|
|
|
if let Err(e) =
|
|
|
|
self.ct_backend.disasm(&self.ct.code, &mut |_| {}, self.tys, self.files, &mut vc)
|
|
|
|
{
|
2024-10-25 04:29:54 -05:00
|
|
|
panic!("{e} {}", vc);
|
|
|
|
} else {
|
2024-11-09 06:54:08 -06:00
|
|
|
log::info!("{}", vc);
|
2024-10-25 04:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct.run(ret_loc, entry)
|
2024-10-21 11:57:23 -05:00
|
|
|
}
|
|
|
|
|
2024-11-08 04:51:10 -06:00
|
|
|
fn new_stack(&mut self, pos: Pos, ty: ty::Id) -> Nid {
|
2024-10-28 11:19:41 -05:00
|
|
|
let stck = self.ci.nodes.new_node_nop(ty, Kind::Stck, [VOID, MEM]);
|
2024-10-30 12:42:25 -05:00
|
|
|
self.ci.nodes[stck].aclass = self.ci.scope.aclasses.len() as _;
|
2024-11-08 04:51:10 -06:00
|
|
|
self.ci.nodes[stck].pos = pos;
|
2024-10-28 11:19:41 -05:00
|
|
|
self.ci.scope.aclasses.push(AClass::new(&mut self.ci.nodes));
|
|
|
|
stck
|
|
|
|
}
|
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
fn store_mem(&mut self, region: Nid, ty: ty::Id, value: Nid) -> Nid {
|
2024-10-19 03:17:36 -05:00
|
|
|
if value == NEVER {
|
|
|
|
return NEVER;
|
|
|
|
}
|
|
|
|
|
2024-10-22 05:40:41 -05:00
|
|
|
debug_assert!(
|
|
|
|
self.ci.nodes[region].kind != Kind::Load || self.ci.nodes[region].ty.is_pointer()
|
|
|
|
);
|
|
|
|
debug_assert!(self.ci.nodes[region].kind != Kind::Stre);
|
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
let (value_index, value_region) = self.ci.nodes.aclass_index(value);
|
2024-10-28 12:53:36 -05:00
|
|
|
if value_index != 0 {
|
2024-11-04 05:38:47 -06:00
|
|
|
self.ci.nodes[value_region].aclass = 0;
|
|
|
|
self.ci.nodes.load_loop_aclass(0, &mut self.ci.scope.aclasses[0], &mut self.ci.loops);
|
|
|
|
self.ci.nodes.load_loop_aclass(
|
|
|
|
value_index,
|
|
|
|
&mut self.ci.scope.aclasses[value_index],
|
|
|
|
&mut self.ci.loops,
|
|
|
|
);
|
|
|
|
let base_class = self.ci.scope.aclasses[0].last_store.get();
|
|
|
|
let last_store = self.ci.scope.aclasses[value_index].last_store.get();
|
|
|
|
if base_class != MEM && last_store != MEM {
|
|
|
|
self.ci.nodes.bind(base_class, last_store);
|
2024-10-28 12:53:36 -05:00
|
|
|
}
|
|
|
|
if last_store != MEM {
|
|
|
|
self.ci.scope.aclasses[0].last_store.set(last_store, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 17:38:57 -05:00
|
|
|
let (index, _) = self.ci.nodes.aclass_index(region);
|
2024-11-08 13:40:18 -06:00
|
|
|
if self.ci.nodes[value].kind == Kind::Load {
|
2024-11-10 11:56:33 -06:00
|
|
|
let (lindex, ..) = self.ci.nodes.aclass_index(self.ci.nodes[value].inputs[1]);
|
2024-11-08 13:40:18 -06:00
|
|
|
let clobber = self.ci.scope.aclasses[lindex].clobber.get();
|
2024-11-10 11:56:33 -06:00
|
|
|
if self.ci.nodes.idepth(clobber)
|
|
|
|
> self.ci.nodes.idepth(self.ci.scope.aclasses[index].clobber.get())
|
|
|
|
{
|
|
|
|
self.ci.scope.aclasses[index].clobber.set(clobber, &mut self.ci.nodes);
|
|
|
|
}
|
2024-11-08 13:40:18 -06:00
|
|
|
}
|
2024-10-28 10:18:53 -05:00
|
|
|
let aclass = &mut self.ci.scope.aclasses[index];
|
|
|
|
self.ci.nodes.load_loop_aclass(index, aclass, &mut self.ci.loops);
|
2024-10-30 07:45:19 -05:00
|
|
|
let vc = Vc::from([aclass.clobber.get(), value, region, aclass.last_store.get()]);
|
2024-10-29 03:04:49 -05:00
|
|
|
mem::take(&mut aclass.last_store).soft_remove(&mut self.ci.nodes);
|
2024-11-10 10:32:24 -06:00
|
|
|
let store = self.ci.nodes.new_node(ty, Kind::Stre, vc, self.tys);
|
2024-10-29 03:04:49 -05:00
|
|
|
aclass.last_store = StrongRef::new(store, &mut self.ci.nodes);
|
|
|
|
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-19 03:17:36 -05:00
|
|
|
debug_assert_ne!(region, VOID);
|
2024-10-23 05:26:07 -05:00
|
|
|
debug_assert_ne!({ self.ci.nodes[region].ty }, ty::Id::VOID, "{:?}", {
|
|
|
|
self.ci.nodes[region].lock_rc = Nid::MAX;
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ci.nodes.graphviz_in_browser(self.ty_display(ty::Id::VOID));
|
2024-10-23 05:26:07 -05:00
|
|
|
});
|
2024-10-22 05:40:41 -05:00
|
|
|
debug_assert!(
|
2024-11-10 05:03:15 -06:00
|
|
|
self.ci.nodes[region].kind != Kind::Load
|
|
|
|
|| self.ci.nodes[region].kind == Kind::Stck
|
|
|
|
|| self.ci.nodes[region].ty.is_pointer(),
|
2024-10-23 05:26:07 -05:00
|
|
|
"{:?} {} {}",
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ci.nodes.graphviz_in_browser(self.ty_display(ty::Id::VOID)),
|
2024-10-26 08:18:00 -05:00
|
|
|
self.file().path,
|
2024-10-23 05:26:07 -05:00
|
|
|
self.ty_display(self.ci.nodes[region].ty)
|
2024-10-22 05:40:41 -05:00
|
|
|
);
|
|
|
|
debug_assert!(self.ci.nodes[region].kind != Kind::Stre);
|
2024-10-28 17:38:57 -05:00
|
|
|
let (index, _) = self.ci.nodes.aclass_index(region);
|
2024-10-28 10:18:53 -05:00
|
|
|
let aclass = &mut self.ci.scope.aclasses[index];
|
|
|
|
self.ci.nodes.load_loop_aclass(index, aclass, &mut self.ci.loops);
|
2024-11-08 16:03:16 -06:00
|
|
|
let vc = [aclass.clobber.get(), region, aclass.last_store.get()];
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(ty, Kind::Load, vc, self.tys)
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-09-03 10:51:28 -05:00
|
|
|
fn make_func_reachable(&mut self, func: ty::Func) {
|
2024-11-05 07:52:30 -06:00
|
|
|
let state_slot = self.ct.active() as usize;
|
2024-11-08 03:25:34 -06:00
|
|
|
let fuc = &mut self.tys.ins.funcs[func];
|
2024-11-05 07:52:30 -06:00
|
|
|
if fuc.comp_state[state_slot] == CompState::Dead {
|
|
|
|
fuc.comp_state[state_slot] = CompState::Queued(self.tys.tasks.len() as _);
|
|
|
|
self.tys.tasks.push(Some(FTask { file: fuc.file, id: func, ct: self.ct.active() }));
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-08 01:36:00 -06:00
|
|
|
fn raw_expr_ctx(&mut self, expr: &Expr, ctx: Ctx) -> Option<Value> {
|
|
|
|
self.ci.pos.push(expr.pos());
|
|
|
|
let res = self.raw_expr_ctx_low(expr, ctx);
|
|
|
|
self.ci.pos.pop().unwrap();
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn raw_expr_ctx_low(&mut self, expr: &Expr, mut ctx: Ctx) -> Option<Value> {
|
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-27 13:55:11 -05:00
|
|
|
Expr::Null { pos } => {
|
2024-10-30 14:20:03 -05:00
|
|
|
inference!(oty, ctx, self, pos, "null pointer", "@as(^<ty>, null)");
|
2024-10-27 13:55:11 -05:00
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
let Some(ty) = self.tys.inner_of(oty) else {
|
2024-10-27 13:55:11 -05:00
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"'null' expression was inferred to be '{}',
|
2024-10-30 14:20:03 -05:00
|
|
|
which is not optional",
|
|
|
|
self.ty_display(oty)
|
2024-10-27 13:55:11 -05:00
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
2024-10-30 14:20:03 -05:00
|
|
|
};
|
2024-10-27 13:55:11 -05:00
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
match oty.loc(self.tys) {
|
|
|
|
Loc::Reg => Some(self.ci.nodes.new_const_lit(oty, 0)),
|
|
|
|
Loc::Stack => {
|
2024-10-31 04:36:18 -05:00
|
|
|
let OptLayout { flag_ty, flag_offset, .. } = self.tys.opt_layout(ty);
|
2024-11-08 04:51:10 -06:00
|
|
|
let stack = self.new_stack(pos, oty);
|
2024-10-31 04:36:18 -05:00
|
|
|
let offset = self.offset(stack, flag_offset);
|
2024-10-30 14:20:03 -05:00
|
|
|
let value = self.ci.nodes.new_const(flag_ty, 0);
|
|
|
|
self.store_mem(offset, flag_ty, value);
|
|
|
|
Some(Value::ptr(stack).ty(oty))
|
|
|
|
}
|
|
|
|
}
|
2024-10-27 13:55:11 -05:00
|
|
|
}
|
2024-10-20 11:49:41 -05:00
|
|
|
Expr::Idk { pos } => {
|
2024-10-27 15:34:03 -05:00
|
|
|
inference!(ty, ctx, self, pos, "value", "@as(<ty>, idk)");
|
2024-10-20 11:49:41 -05:00
|
|
|
|
2024-11-03 15:54:05 -06:00
|
|
|
if ty.loc(self.tys) == Loc::Stack {
|
2024-11-08 04:51:10 -06:00
|
|
|
Some(Value::ptr(self.new_stack(pos, ty)).ty(ty))
|
2024-10-20 11:49:41 -05:00
|
|
|
} else {
|
2024-11-03 15:54:05 -06:00
|
|
|
Some(self.ci.nodes.new_const_lit(ty, 0))
|
2024-10-20 11:49:41 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-30 14:20:03 -05:00
|
|
|
Expr::Bool { value, .. } => Some(self.ci.nodes.new_const_lit(ty::Id::BOOL, value)),
|
2024-11-09 07:02:13 -06:00
|
|
|
Expr::Number { value, .. }
|
|
|
|
if let Some(ty) = ctx.ty
|
|
|
|
&& ty.is_float() =>
|
|
|
|
{
|
|
|
|
Some(self.ci.nodes.new_const_lit(ty, (value as f64).to_bits() as i64))
|
|
|
|
}
|
2024-11-12 12:02:29 -06:00
|
|
|
Expr::Number { value, .. } => {
|
|
|
|
self.gen_inferred_const(ctx, ty::Id::DINT, value, ty::Id::is_integer)
|
|
|
|
}
|
|
|
|
Expr::Float { value, .. } => {
|
|
|
|
self.gen_inferred_const(ctx, ty::Id::F32, value as i64, ty::Id::is_float)
|
|
|
|
}
|
2024-10-19 12:37:02 -05:00
|
|
|
Expr::Ident { id, .. }
|
|
|
|
if let Some(index) = self.ci.scope.vars.iter().rposition(|v| v.id == id) =>
|
|
|
|
{
|
|
|
|
let var = &mut self.ci.scope.vars[index];
|
2024-10-28 11:19:41 -05:00
|
|
|
self.ci.nodes.load_loop_var(index, var, &mut self.ci.loops);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
Some(Value::var(index).ty(var.ty))
|
|
|
|
}
|
|
|
|
Expr::Ident { id, pos, .. } => {
|
2024-10-26 07:06:08 -05:00
|
|
|
let decl = self.find_type(pos, self.ci.file, self.ci.file, Ok(id), self.files);
|
2024-10-20 08:16:55 -05:00
|
|
|
match decl.expand() {
|
2024-11-08 03:25:34 -06:00
|
|
|
ty::Kind::NEVER => Value::NEVER,
|
2024-11-08 03:57:58 -06:00
|
|
|
ty::Kind::Global(global) => self.gen_global(global),
|
2024-11-09 03:58:57 -06:00
|
|
|
ty::Kind::Const(cnst) => self.gen_const(cnst, ctx),
|
2024-10-23 05:26:07 -05:00
|
|
|
_ => Some(Value::new(Nid::MAX).ty(decl)),
|
2024-10-20 03:37:48 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
2024-10-20 05:22:28 -05:00
|
|
|
Expr::Comment { .. } => Some(Value::VOID),
|
|
|
|
Expr::String { pos, literal } => {
|
|
|
|
let literal = &literal[1..literal.len() - 1];
|
|
|
|
|
|
|
|
let report = |bytes: &core::str::Bytes, message: &str| {
|
|
|
|
self.report(pos + (literal.len() - bytes.len()) as u32 - 1, message)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut data = Vec::<u8>::with_capacity(literal.len());
|
|
|
|
crate::endoce_string(literal, &mut data, report).unwrap();
|
|
|
|
|
|
|
|
let ty = self.tys.make_ptr(ty::Id::U8);
|
2024-10-25 15:59:01 -05:00
|
|
|
let global = match self.tys.strings.entry(&data, &self.tys.ins.globals) {
|
|
|
|
(hash_map::RawEntryMut::Occupied(occupied_entry), _) => {
|
|
|
|
occupied_entry.get_key_value().0.value.0
|
|
|
|
}
|
|
|
|
(hash_map::RawEntryMut::Vacant(vacant_entry), hash) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let global =
|
|
|
|
self.tys.ins.globals.push(Global { data, ty, ..Default::default() });
|
2024-10-25 15:59:01 -05:00
|
|
|
vacant_entry
|
|
|
|
.insert(crate::ctx_map::Key { value: StringRef(global), hash }, ())
|
|
|
|
.0
|
|
|
|
.value
|
|
|
|
.0
|
2024-10-24 02:43:07 -05:00
|
|
|
}
|
|
|
|
};
|
2024-11-10 10:32:24 -06:00
|
|
|
let global = self.ci.nodes.new_node_nop(ty, Kind::Global { global }, [VOID]);
|
2024-10-30 12:42:25 -05:00
|
|
|
self.ci.nodes[global].aclass = GLOBAL_ACLASS as _;
|
2024-10-20 05:22:28 -05:00
|
|
|
Some(Value::new(global).ty(ty))
|
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr::Return { pos, val } => {
|
2024-10-20 14:00:56 -05:00
|
|
|
let mut value = if let Some(val) = val {
|
2024-11-07 03:43:29 -06:00
|
|
|
self.raw_expr_ctx(val, Ctx { ty: self.ci.ret })?
|
2024-09-28 14:56:39 -05:00
|
|
|
} 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-11-07 03:43:29 -06:00
|
|
|
self.strip_var(&mut value);
|
2024-09-28 14:56:39 -05:00
|
|
|
|
2024-10-20 14:00:56 -05:00
|
|
|
let expected = *self.ci.ret.get_or_insert(value.ty);
|
|
|
|
self.assert_ty(pos, &mut value, expected, "return value");
|
2024-11-07 03:43:29 -06:00
|
|
|
self.strip_ptr(&mut value);
|
2024-10-20 14:00:56 -05:00
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
if self.ci.inline_depth == 0 {
|
2024-10-26 05:09:53 -05:00
|
|
|
debug_assert_ne!(self.ci.ctrl.get(), VOID);
|
|
|
|
let mut inps = Vc::from([self.ci.ctrl.get(), value.id]);
|
2024-10-28 10:18:53 -05:00
|
|
|
for (i, aclass) in self.ci.scope.aclasses.iter_mut().enumerate() {
|
|
|
|
self.ci.nodes.load_loop_aclass(i, aclass, &mut self.ci.loops);
|
2024-11-04 05:38:47 -06:00
|
|
|
if aclass.last_store.get() != MEM {
|
|
|
|
inps.push(aclass.last_store.get());
|
|
|
|
}
|
2024-10-28 10:18:53 -05:00
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
|
2024-11-08 13:40:18 -06:00
|
|
|
let ret = self.ci.nodes.new_node_nop(ty::Id::VOID, Kind::Return, inps);
|
|
|
|
self.ci.ctrl.set(NEVER, &mut self.ci.nodes);
|
|
|
|
self.ci.nodes[ret].pos = pos;
|
|
|
|
self.ci.nodes.bind(ret, NEVER);
|
2024-10-23 05:26:07 -05:00
|
|
|
} else if let Some((pv, ctrl, scope)) = &mut self.ci.inline_ret {
|
2024-10-26 05:09:53 -05:00
|
|
|
ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(
|
|
|
|
ty::Id::VOID,
|
|
|
|
Kind::Region,
|
|
|
|
[self.ci.ctrl.get(), ctrl.get()],
|
|
|
|
self.tys,
|
|
|
|
),
|
2024-10-23 05:26:07 -05:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.merge_scopes(
|
|
|
|
&mut self.ci.loops,
|
|
|
|
ctrl,
|
|
|
|
scope,
|
|
|
|
&mut self.ci.scope,
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-10-21 08:12:37 -05:00
|
|
|
self.ci.nodes.unlock(pv.id);
|
2024-11-10 10:32:24 -06:00
|
|
|
pv.id = self.ci.nodes.new_node(
|
|
|
|
value.ty,
|
|
|
|
Kind::Phi,
|
|
|
|
[ctrl.get(), value.id, pv.id],
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-10-21 08:12:37 -05:00
|
|
|
self.ci.nodes.lock(pv.id);
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.set(NEVER, &mut self.ci.nodes);
|
2024-10-21 08:12:37 -05:00
|
|
|
} else {
|
2024-11-03 01:59:42 -06:00
|
|
|
for (i, aclass) in self.ci.scope.aclasses[..2].iter_mut().enumerate() {
|
|
|
|
self.ci.nodes.load_loop_aclass(i, aclass, &mut self.ci.loops);
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
self.ci.nodes.lock(value.id);
|
2024-10-23 05:26:07 -05:00
|
|
|
let mut scope = self.ci.scope.dup(&mut self.ci.nodes);
|
|
|
|
scope
|
|
|
|
.vars
|
|
|
|
.drain(self.ci.inline_var_base..)
|
|
|
|
.for_each(|v| v.remove(&mut self.ci.nodes));
|
2024-11-03 01:59:42 -06:00
|
|
|
scope
|
|
|
|
.aclasses
|
|
|
|
.drain(self.ci.inline_aclass_base..)
|
|
|
|
.for_each(|v| v.remove(&mut self.ci.nodes));
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
let repl = StrongRef::new(NEVER, &mut self.ci.nodes);
|
|
|
|
self.ci.inline_ret =
|
|
|
|
Some((value, mem::replace(&mut self.ci.ctrl, repl), scope));
|
2024-10-21 08:12:37 -05:00
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2024-11-03 03:15:03 -06:00
|
|
|
Expr::Die { .. } => {
|
|
|
|
self.ci.ctrl.set(
|
|
|
|
self.ci.nodes.new_node_nop(ty::Id::VOID, Kind::Die, [self.ci.ctrl.get()]),
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
|
|
|
|
|
|
|
self.ci.nodes[NEVER].inputs.push(self.ci.ctrl.get());
|
|
|
|
self.ci.nodes[self.ci.ctrl.get()].outputs.push(NEVER);
|
|
|
|
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);
|
2024-11-04 12:18:37 -06:00
|
|
|
self.implicit_unwrap(pos, &mut vtarget);
|
2024-10-17 15:29:09 -05:00
|
|
|
let tty = vtarget.ty;
|
2024-10-17 12:32:10 -05:00
|
|
|
|
2024-10-23 05:26:07 -05:00
|
|
|
if let ty::Kind::Module(m) = tty.expand() {
|
2024-10-26 07:06:08 -05:00
|
|
|
return match self
|
|
|
|
.find_type(pos, self.ci.file, m, Err(name), self.files)
|
|
|
|
.expand()
|
|
|
|
{
|
2024-11-08 03:25:34 -06:00
|
|
|
ty::Kind::NEVER => Value::NEVER,
|
2024-11-08 03:57:58 -06:00
|
|
|
ty::Kind::Global(global) => self.gen_global(global),
|
2024-11-09 03:58:57 -06:00
|
|
|
ty::Kind::Const(cnst) => self.gen_const(cnst, ctx),
|
2024-10-23 05:26:07 -05:00
|
|
|
v => Some(Value::new(Nid::MAX).ty(v.compress())),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
let Some((offset, ty)) = OffsetIter::offset_of(self.tys, s, name) else {
|
2024-10-17 15:29:09 -05:00
|
|
|
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-20 11:49:41 -05:00
|
|
|
Some(Value::ptr(self.offset(vtarget.id, offset)).ty(ty))
|
2024-10-17 15:29:09 -05:00
|
|
|
}
|
2024-11-08 04:51:10 -06:00
|
|
|
Expr::UnOp { op: TokenKind::Band, val, pos } => {
|
2024-09-28 14:56:39 -05:00
|
|
|
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-11-08 04:51:10 -06:00
|
|
|
let stack = self.new_stack(pos, val.ty);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(stack, val.ty, 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-11-03 03:15:03 -06:00
|
|
|
let mut vl = self.expr_ctx(val, ctx)?;
|
2024-10-17 12:32:10 -05:00
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
self.implicit_unwrap(val.pos(), &mut vl);
|
2024-10-31 04:36:18 -05:00
|
|
|
|
2024-11-03 03:15:03 -06:00
|
|
|
let Some(base) = self.tys.base_of(vl.ty) else {
|
2024-09-28 14:56:39 -05:00
|
|
|
self.report(
|
|
|
|
pos,
|
2024-11-03 03:15:03 -06:00
|
|
|
fa!("the '{}' can not be dereferneced", self.ty_display(vl.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-11-03 03:15:03 -06:00
|
|
|
vl.ptr = true;
|
|
|
|
vl.ty = base;
|
|
|
|
Some(vl)
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
Expr::UnOp { pos, op: op @ TokenKind::Sub, val } => {
|
2024-10-25 08:07:39 -05:00
|
|
|
let val =
|
|
|
|
self.expr_ctx(val, Ctx::default().with_ty(ctx.ty.unwrap_or(ty::Id::INT)))?;
|
2024-10-29 07:36:12 -05:00
|
|
|
if val.ty.is_integer() {
|
2024-11-10 10:32:24 -06:00
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
val.ty,
|
|
|
|
Kind::UnOp { op },
|
|
|
|
[VOID, val.id],
|
|
|
|
self.tys,
|
|
|
|
))
|
2024-10-29 07:36:12 -05:00
|
|
|
} else if val.ty.is_float() {
|
2024-10-30 14:20:03 -05:00
|
|
|
let value = self.ci.nodes.new_const(val.ty, (-1f64).to_bits() as i64);
|
2024-11-10 10:32:24 -06:00
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
val.ty,
|
|
|
|
Kind::BinOp { op: TokenKind::Mul },
|
|
|
|
[VOID, val.id, value],
|
|
|
|
self.tys,
|
|
|
|
))
|
2024-10-29 07:36:12 -05:00
|
|
|
} else {
|
2024-10-17 12:32:10 -05:00
|
|
|
self.report(pos, fa!("cant negate '{}'", self.ty_display(val.ty)));
|
2024-11-11 15:14:54 -06:00
|
|
|
Value::NEVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::UnOp { pos, op: op @ TokenKind::Not, val } => {
|
|
|
|
let val =
|
|
|
|
self.expr_ctx(val, Ctx::default().with_ty(ctx.ty.unwrap_or(ty::Id::INT)))?;
|
|
|
|
if val.ty == ty::Id::BOOL {
|
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
val.ty,
|
|
|
|
Kind::UnOp { op },
|
|
|
|
[VOID, val.id],
|
|
|
|
self.tys,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
self.report(pos, fa!("cant logically negate '{}'", self.ty_display(val.ty)));
|
2024-10-29 07:36:12 -05:00
|
|
|
Value::NEVER
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
}
|
2024-11-08 04:51:10 -06:00
|
|
|
Expr::BinOp { left, op: TokenKind::Decl, right, pos } => {
|
2024-10-22 03:08:50 -05:00
|
|
|
let mut right = self.expr(right)?;
|
2024-10-26 17:02:59 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
if right.ty.loc(self.tys) == Loc::Stack {
|
2024-11-08 04:51:10 -06:00
|
|
|
let stck = self.new_stack(pos, right.ty);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(stck, right.ty, right.id);
|
2024-10-22 03:08:50 -05:00
|
|
|
right.id = stck;
|
|
|
|
right.ptr = true;
|
|
|
|
}
|
2024-10-20 14:00:56 -05:00
|
|
|
self.assign_pattern(left, right);
|
2024-10-17 12:32:10 -05:00
|
|
|
Some(Value::VOID)
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
2024-11-03 15:27:37 -06:00
|
|
|
Expr::BinOp { left: Expr::Wildcard { .. }, op: TokenKind::Assign, right, .. } => {
|
|
|
|
self.expr(right)?;
|
|
|
|
Some(Value::VOID)
|
|
|
|
}
|
2024-10-25 08:40:23 -05:00
|
|
|
Expr::BinOp { left, pos, op: TokenKind::Assign, right } => {
|
2024-10-18 06:11:11 -05:00
|
|
|
let dest = self.raw_expr(left)?;
|
2024-10-20 14:00:56 -05:00
|
|
|
let mut value = self.expr_ctx(right, Ctx::default().with_ty(dest.ty))?;
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-10-25 08:40:23 -05:00
|
|
|
self.assert_ty(pos, &mut value, dest.ty, "assignment source");
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
if dest.var {
|
2024-10-18 06:11:11 -05:00
|
|
|
let var = &mut self.ci.scope.vars[(u16::MAX - dest.id) as usize];
|
2024-10-22 05:40:41 -05:00
|
|
|
|
|
|
|
if var.ptr {
|
2024-10-22 15:57:40 -05:00
|
|
|
let val = var.value();
|
2024-10-24 02:43:07 -05:00
|
|
|
let ty = var.ty;
|
|
|
|
self.store_mem(val, ty, value.id);
|
2024-10-22 05:40:41 -05:00
|
|
|
} else {
|
2024-10-22 15:57:40 -05:00
|
|
|
var.set_value_remove(value.id, &mut self.ci.nodes);
|
2024-10-22 05:40:41 -05:00
|
|
|
}
|
2024-10-17 12:32:10 -05:00
|
|
|
} else if dest.ptr {
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(dest.id, dest.ty, value.id);
|
2024-10-17 12:32:10 -05:00
|
|
|
} else {
|
2024-10-25 08:40:23 -05:00
|
|
|
self.report(pos, "cannot assign to this expression");
|
2024-10-17 12:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(Value::VOID)
|
|
|
|
}
|
2024-10-31 04:36:18 -05:00
|
|
|
Expr::BinOp { left: &Expr::Null { pos }, .. } => {
|
|
|
|
self.report(pos, "'null' must always be no the right side of an expression");
|
|
|
|
Value::NEVER
|
|
|
|
}
|
|
|
|
Expr::BinOp {
|
|
|
|
left,
|
|
|
|
op: op @ (TokenKind::Eq | TokenKind::Ne),
|
|
|
|
right: Expr::Null { .. },
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let mut cmped = self.raw_expr(left)?;
|
|
|
|
self.strip_var(&mut cmped);
|
|
|
|
|
|
|
|
let Some(ty) = self.tys.inner_of(cmped.ty) else {
|
2024-11-03 14:31:46 -06:00
|
|
|
self.report(
|
|
|
|
left.pos(),
|
|
|
|
fa!("'{}' is never null, remove this check", self.ty_display(cmped.ty)),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
2024-10-31 04:36:18 -05:00
|
|
|
};
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
Some(Value::new(self.gen_null_check(cmped, ty, op)).ty(ty::Id::BOOL))
|
2024-10-31 04:36:18 -05:00
|
|
|
}
|
2024-10-25 08:40:23 -05:00
|
|
|
Expr::BinOp { left, pos, op, right }
|
2024-10-20 14:00:56 -05:00
|
|
|
if !matches!(op, TokenKind::Assign | TokenKind::Decl) =>
|
|
|
|
{
|
2024-10-22 05:40:41 -05:00
|
|
|
let mut lhs = self.raw_expr_ctx(left, ctx)?;
|
|
|
|
self.strip_var(&mut lhs);
|
2024-11-04 12:18:37 -06:00
|
|
|
self.implicit_unwrap(left.pos(), &mut lhs);
|
2024-10-22 05:40:41 -05:00
|
|
|
|
|
|
|
match lhs.ty.expand() {
|
2024-10-29 07:36:12 -05:00
|
|
|
_ if lhs.ty.is_pointer()
|
|
|
|
|| lhs.ty.is_integer()
|
|
|
|
|| lhs.ty == ty::Id::BOOL
|
|
|
|
|| (lhs.ty.is_float() && op.is_supported_float_op()) =>
|
|
|
|
{
|
2024-10-26 17:02:59 -05:00
|
|
|
self.strip_ptr(&mut lhs);
|
2024-10-22 05:40:41 -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);
|
|
|
|
let mut rhs = rhs?;
|
|
|
|
self.strip_var(&mut rhs);
|
2024-11-04 12:18:37 -06:00
|
|
|
self.implicit_unwrap(right.pos(), &mut rhs);
|
2024-11-04 05:38:47 -06:00
|
|
|
let (ty, aclass) = self.binop_ty(pos, &mut lhs, &mut rhs, op);
|
2024-11-12 12:02:29 -06:00
|
|
|
let fty = ty.bin_ret(op);
|
2024-11-12 13:42:04 -06:00
|
|
|
if matches!(
|
|
|
|
op,
|
|
|
|
TokenKind::Lt
|
|
|
|
| TokenKind::Gt
|
|
|
|
| TokenKind::Ge
|
|
|
|
| TokenKind::Le
|
|
|
|
| TokenKind::Ne
|
|
|
|
| TokenKind::Eq
|
|
|
|
) {
|
2024-11-12 12:02:29 -06:00
|
|
|
if lhs.ty.is_float() {
|
|
|
|
} else {
|
|
|
|
self.ci.nodes.lock(rhs.id);
|
|
|
|
let lty = lhs.ty.extend();
|
|
|
|
if lty != lhs.ty {
|
|
|
|
self.extend(&mut lhs, lty);
|
|
|
|
}
|
|
|
|
self.ci.nodes.unlock(rhs.id);
|
|
|
|
let rty = rhs.ty.extend();
|
|
|
|
if rty != rhs.ty {
|
|
|
|
self.extend(&mut rhs, rty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-22 05:40:41 -05:00
|
|
|
let inps = [VOID, lhs.id, rhs.id];
|
2024-11-12 12:02:29 -06:00
|
|
|
let bop =
|
|
|
|
self.ci.nodes.new_node_lit(fty, Kind::BinOp { op }, inps, self.tys);
|
2024-11-04 05:38:47 -06:00
|
|
|
self.ci.nodes.pass_aclass(aclass, bop.id);
|
2024-10-30 07:45:19 -05:00
|
|
|
Some(bop)
|
2024-10-22 05:40:41 -05:00
|
|
|
}
|
|
|
|
ty::Kind::Struct(s) if op.is_homogenous() => {
|
|
|
|
self.ci.nodes.lock(lhs.id);
|
|
|
|
let rhs = self.raw_expr_ctx(right, Ctx::default().with_ty(lhs.ty));
|
|
|
|
self.ci.nodes.unlock(lhs.id);
|
|
|
|
let mut rhs = rhs?;
|
|
|
|
self.strip_var(&mut rhs);
|
2024-10-25 08:40:23 -05:00
|
|
|
self.assert_ty(pos, &mut rhs, lhs.ty, "struct operand");
|
2024-11-08 04:51:10 -06:00
|
|
|
let dst = self.new_stack(pos, lhs.ty);
|
2024-10-22 05:40:41 -05:00
|
|
|
self.struct_op(left.pos(), op, s, dst, lhs.id, rhs.id);
|
|
|
|
Some(Value::ptr(dst).ty(lhs.ty))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.report(
|
2024-10-25 08:40:23 -05:00
|
|
|
pos,
|
|
|
|
fa!("'{} {op} _' is not supported", self.ty_display(lhs.ty)),
|
2024-10-22 05:40:41 -05:00
|
|
|
);
|
|
|
|
Value::NEVER
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
2024-10-19 12:37:02 -05:00
|
|
|
Expr::Index { base, index } => {
|
|
|
|
let mut bs = self.raw_expr(base)?;
|
|
|
|
self.strip_var(&mut bs);
|
|
|
|
|
|
|
|
if let Some(base) = self.tys.base_of(bs.ty) {
|
|
|
|
bs.ptr = true;
|
|
|
|
bs.ty = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty::Kind::Slice(s) = bs.ty.expand() else {
|
|
|
|
self.report(
|
|
|
|
base.pos(),
|
|
|
|
fa!(
|
|
|
|
"cant index into '{}' which is not array nor slice",
|
|
|
|
self.ty_display(bs.ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
let elem = self.tys.ins.slices[s].elem;
|
2024-11-12 12:02:29 -06:00
|
|
|
let mut idx = self.expr_ctx(index, Ctx::default().with_ty(ty::Id::DINT))?;
|
|
|
|
self.assert_ty(index.pos(), &mut idx, ty::Id::DINT, "subscript");
|
2024-10-30 14:20:03 -05:00
|
|
|
let size = self.ci.nodes.new_const(ty::Id::INT, self.tys.size_of(elem));
|
2024-10-19 12:37:02 -05:00
|
|
|
let inps = [VOID, idx.id, size];
|
2024-11-10 10:32:24 -06:00
|
|
|
let offset = self.ci.nodes.new_node(
|
|
|
|
ty::Id::INT,
|
|
|
|
Kind::BinOp { op: TokenKind::Mul },
|
|
|
|
inps,
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-11-04 05:38:47 -06:00
|
|
|
let aclass = self.ci.nodes.aclass_index(bs.id).1;
|
2024-10-19 12:37:02 -05:00
|
|
|
let inps = [VOID, bs.id, offset];
|
2024-11-10 10:32:24 -06:00
|
|
|
let ptr = self.ci.nodes.new_node(
|
|
|
|
ty::Id::INT,
|
|
|
|
Kind::BinOp { op: TokenKind::Add },
|
|
|
|
inps,
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-11-04 05:38:47 -06:00
|
|
|
self.ci.nodes.pass_aclass(aclass, ptr);
|
2024-10-30 07:45:19 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
Some(Value::ptr(ptr).ty(elem))
|
|
|
|
}
|
2024-10-21 11:57:23 -05:00
|
|
|
Expr::Embed { id, .. } => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let glob = &self.tys.ins.globals[id];
|
2024-11-10 10:32:24 -06:00
|
|
|
let g =
|
|
|
|
self.ci.nodes.new_node(glob.ty, Kind::Global { global: id }, [VOID], self.tys);
|
2024-10-21 12:57:55 -05:00
|
|
|
Some(Value::ptr(g).ty(glob.ty))
|
2024-10-21 11:57:23 -05:00
|
|
|
}
|
2024-09-28 14:56:39 -05:00
|
|
|
Expr::Directive { name: "sizeof", args: [ty], .. } => {
|
|
|
|
let ty = self.ty(ty);
|
2024-11-12 12:02:29 -06:00
|
|
|
self.gen_inferred_const(ctx, ty::Id::DINT, self.tys.size_of(ty), ty::Id::is_integer)
|
2024-09-27 09:53:28 -05:00
|
|
|
}
|
2024-10-21 11:57:23 -05:00
|
|
|
Expr::Directive { name: "alignof", args: [ty], .. } => {
|
|
|
|
let ty = self.ty(ty);
|
2024-11-12 12:02:29 -06:00
|
|
|
let align = self.tys.align_of(ty);
|
|
|
|
self.gen_inferred_const(ctx, ty::Id::DINT, align, ty::Id::is_integer)
|
2024-10-21 11:57:23 -05:00
|
|
|
}
|
|
|
|
Expr::Directive { name: "bitcast", args: [val], pos } => {
|
|
|
|
let mut val = self.raw_expr(val)?;
|
|
|
|
self.strip_var(&mut val);
|
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
inference!(ty, ctx, self, pos, "type", "@as(<ty>, @bitcast(<expr>))");
|
2024-10-21 11:57:23 -05:00
|
|
|
|
|
|
|
let (got, expected) = (self.tys.size_of(val.ty), self.tys.size_of(ty));
|
|
|
|
if got != expected {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"cast from '{}' to '{}' is not supported, \
|
|
|
|
sizes dont match ({got} != {expected})",
|
|
|
|
self.ty_display(val.ty),
|
|
|
|
self.ty_display(ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
match ty.loc(self.tys) {
|
2024-10-26 05:09:53 -05:00
|
|
|
Loc::Reg if mem::take(&mut val.ptr) => val.id = self.load_mem(val.id, ty),
|
2024-10-24 06:58:58 -05:00
|
|
|
Loc::Stack if !val.ptr => {
|
2024-11-08 04:51:10 -06:00
|
|
|
let stack = self.new_stack(pos, ty);
|
2024-10-24 06:58:58 -05:00
|
|
|
self.store_mem(stack, val.ty, val.id);
|
|
|
|
val.id = stack;
|
|
|
|
val.ptr = true;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:23 -05:00
|
|
|
val.ty = ty;
|
|
|
|
Some(val)
|
|
|
|
}
|
2024-11-03 03:15:03 -06:00
|
|
|
Expr::Directive { name: "unwrap", args: [expr], .. } => {
|
|
|
|
let mut val = self.raw_expr(expr)?;
|
|
|
|
self.strip_var(&mut val);
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
if !val.ty.is_optional() {
|
2024-11-03 03:15:03 -06:00
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
fa!(
|
|
|
|
"only optional types can be unwrapped ('{}' is not optional)",
|
|
|
|
self.ty_display(val.ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
self.explicit_unwrap(expr.pos(), &mut val);
|
2024-11-03 03:15:03 -06:00
|
|
|
Some(val)
|
|
|
|
}
|
2024-10-21 11:57:23 -05:00
|
|
|
Expr::Directive { name: "intcast", args: [expr], pos } => {
|
2024-10-25 07:51:33 -05:00
|
|
|
let mut val = self.expr(expr)?;
|
2024-10-19 12:37:02 -05:00
|
|
|
|
|
|
|
if !val.ty.is_integer() {
|
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
fa!(
|
|
|
|
"only integers can be truncated ('{}' is not an integer)",
|
|
|
|
self.ty_display(val.ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
}
|
|
|
|
|
2024-10-27 15:34:03 -05:00
|
|
|
inference!(ty, ctx, self, pos, "integer", "@as(<ty>, @intcast(<expr>))");
|
2024-10-19 12:37:02 -05:00
|
|
|
|
2024-10-25 07:51:33 -05:00
|
|
|
if !ty.is_integer() {
|
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
fa!(
|
|
|
|
"intcast is inferred to output '{}', which is not an integer",
|
2024-10-25 09:08:20 -05:00
|
|
|
self.ty_display(ty)
|
2024-10-25 07:51:33 -05:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-27 12:21:33 -05:00
|
|
|
if self.tys.size_of(val.ty) < self.tys.size_of(ty) {
|
|
|
|
self.extend(&mut val, ty);
|
|
|
|
Some(val)
|
|
|
|
} else {
|
|
|
|
Some(val.ty(ty))
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-29 08:24:31 -05:00
|
|
|
Expr::Directive { pos, name: "floatcast", args: [expr] } => {
|
|
|
|
let val = self.expr(expr)?;
|
|
|
|
|
|
|
|
if !val.ty.is_float() {
|
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
fa!(
|
|
|
|
"only floats can be truncated ('{}' is not a float)",
|
|
|
|
self.ty_display(val.ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
inference!(ty, ctx, self, pos, "float", "@as(<floaty>, @floatcast(<expr>))");
|
|
|
|
|
|
|
|
if !ty.is_float() {
|
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
fa!(
|
|
|
|
"floatcast is inferred to output '{}', which is not a float",
|
|
|
|
self.ty_display(ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-07 03:43:29 -06:00
|
|
|
if self.tys.size_of(val.ty) != self.tys.size_of(ty) {
|
2024-11-10 10:32:24 -06:00
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
ty,
|
|
|
|
Kind::UnOp { op: TokenKind::Float },
|
|
|
|
[VOID, val.id],
|
|
|
|
self.tys,
|
|
|
|
))
|
2024-10-29 08:24:31 -05:00
|
|
|
} else {
|
|
|
|
Some(val.ty(ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::Directive { name: "fti", args: [expr], .. } => {
|
|
|
|
let val = self.expr(expr)?;
|
|
|
|
|
|
|
|
let ret_ty = match val.ty {
|
2024-11-07 03:43:29 -06:00
|
|
|
ty::Id::F32 | ty::Id::F64 => ty::Id::INT,
|
2024-10-29 08:24:31 -05:00
|
|
|
_ => {
|
|
|
|
self.report(
|
|
|
|
expr.pos(),
|
|
|
|
fa!("expected float ('{}' is not a float)", self.ty_display(val.ty)),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
ret_ty,
|
|
|
|
Kind::UnOp { op: TokenKind::Number },
|
|
|
|
[VOID, val.id],
|
|
|
|
self.tys,
|
|
|
|
))
|
2024-10-29 08:24:31 -05:00
|
|
|
}
|
|
|
|
Expr::Directive { name: "itf", args: [expr], .. } => {
|
|
|
|
let mut val = self.expr(expr)?;
|
|
|
|
|
|
|
|
let (ret_ty, expected) = match val.ty.simple_size().unwrap() {
|
|
|
|
8 => (ty::Id::F64, ty::Id::INT),
|
2024-11-07 03:43:29 -06:00
|
|
|
_ => (ty::Id::F32, ty::Id::INT),
|
2024-10-29 08:24:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.assert_ty(expr.pos(), &mut val, expected, "converted integer");
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
Some(self.ci.nodes.new_node_lit(
|
|
|
|
ret_ty,
|
|
|
|
Kind::UnOp { op: TokenKind::Float },
|
|
|
|
[VOID, val.id],
|
|
|
|
self.tys,
|
|
|
|
))
|
2024-10-29 08:24:31 -05:00
|
|
|
}
|
2024-11-11 15:34:42 -06:00
|
|
|
Expr::Directive { name: "as", args: [ty, expr], pos } => {
|
2024-10-21 12:57:55 -05:00
|
|
|
let ty = self.ty(ty);
|
2024-11-11 15:34:42 -06:00
|
|
|
let mut val = self.raw_expr_ctx(expr, Ctx::default().with_ty(ty))?;
|
|
|
|
|
|
|
|
if let Some(ity) = ctx.ty
|
|
|
|
&& ity.try_upcast(ty) == Some(ty)
|
|
|
|
&& val.ty == ity
|
|
|
|
{
|
|
|
|
self.report(pos, "the type is known at this point, remove the hint");
|
|
|
|
}
|
2024-10-24 07:08:17 -05:00
|
|
|
self.strip_var(&mut val);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.assert_ty(expr.pos(), &mut val, ty, "hinted expr");
|
2024-10-21 12:57:55 -05:00
|
|
|
Some(val)
|
2024-10-19 03:17:36 -05:00
|
|
|
}
|
2024-10-21 11:57:23 -05:00
|
|
|
Expr::Directive { pos, name: "eca", args } => {
|
2024-10-27 15:34:03 -05:00
|
|
|
inference!(ty, ctx, self, pos, "return type", "@as(<return_ty>, @eca(<expr>...))");
|
2024-10-21 11:57:23 -05:00
|
|
|
|
2024-10-23 05:26:07 -05:00
|
|
|
let mut inps = Vc::from([NEVER]);
|
2024-10-24 02:43:07 -05:00
|
|
|
let arg_base = self.tys.tmp.args.len();
|
2024-10-30 12:42:25 -05:00
|
|
|
let mut clobbered_aliases = BitSet::default();
|
2024-10-21 11:57:23 -05:00
|
|
|
for arg in args {
|
|
|
|
let value = self.expr(arg)?;
|
2024-10-29 09:15:30 -05:00
|
|
|
self.add_clobbers(value, &mut clobbered_aliases);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.tys.tmp.args.push(value.ty);
|
2024-10-21 11:57:23 -05:00
|
|
|
debug_assert_ne!(self.ci.nodes[value.id].kind, Kind::Stre);
|
|
|
|
self.ci.nodes.lock(value.id);
|
|
|
|
inps.push(value.id);
|
|
|
|
}
|
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
let args = self.tys.pack_args(arg_base).expect("TODO");
|
|
|
|
|
2024-10-21 11:57:23 -05:00
|
|
|
for &n in inps.iter().skip(1) {
|
|
|
|
self.ci.nodes.unlock(n);
|
|
|
|
}
|
|
|
|
|
2024-10-30 12:42:25 -05:00
|
|
|
self.append_clobbers(&mut inps, &mut clobbered_aliases);
|
2024-10-21 11:57:23 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
let alt_value = match ty.loc(self.tys) {
|
2024-10-21 11:57:23 -05:00
|
|
|
Loc::Reg => None,
|
|
|
|
Loc::Stack => {
|
2024-11-08 04:51:10 -06:00
|
|
|
let stck = self.new_stack(pos, ty);
|
2024-10-21 11:57:23 -05:00
|
|
|
inps.push(stck);
|
|
|
|
Some(Value::ptr(stck).ty(ty))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
inps[0] = self.ci.ctrl.get();
|
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node_nop(ty, Kind::Call { func: ty::Func::ECA, args }, inps),
|
2024-10-26 05:09:53 -05:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-10-21 11:57:23 -05:00
|
|
|
|
2024-10-30 07:45:19 -05:00
|
|
|
self.add_clobber_stores(clobbered_aliases);
|
2024-10-28 11:19:41 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
alt_value.or(Some(Value::new(self.ci.ctrl.get()).ty(ty)))
|
2024-10-21 11:57:23 -05:00
|
|
|
}
|
2024-11-10 12:35:48 -06:00
|
|
|
Expr::Call { func, args, .. } => self.gen_call(func, args, false),
|
2024-10-21 08:12:37 -05:00
|
|
|
Expr::Directive { name: "inline", args: [func, args @ ..], .. } => {
|
2024-11-10 12:35:48 -06:00
|
|
|
self.gen_call(func, args, true)
|
2024-10-21 08:12:37 -05:00
|
|
|
}
|
2024-10-19 03:17:36 -05:00
|
|
|
Expr::Tupl { pos, ty, fields, .. } => {
|
2024-10-31 04:56:59 -05:00
|
|
|
ctx.ty = ty
|
|
|
|
.map(|ty| self.ty(ty))
|
|
|
|
.or(ctx.ty.map(|ty| self.tys.inner_of(ty).unwrap_or(ty)));
|
2024-10-27 15:34:03 -05:00
|
|
|
inference!(sty, ctx, self, pos, "struct or slice", "<struct_ty>.(...)");
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
match sty.expand() {
|
|
|
|
ty::Kind::Struct(s) => {
|
2024-11-08 04:51:10 -06:00
|
|
|
let mem = self.new_stack(pos, sty);
|
2024-10-25 15:59:01 -05:00
|
|
|
let mut offs = OffsetIter::new(s, self.tys);
|
2024-10-19 12:37:02 -05:00
|
|
|
for field in fields {
|
2024-10-25 15:59:01 -05:00
|
|
|
let Some((ty, offset)) = offs.next_ty(self.tys) else {
|
2024-10-19 12:37:02 -05:00
|
|
|
self.report(
|
|
|
|
field.pos(),
|
|
|
|
"this init argumen overflows the field count",
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
};
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
let mut value = self.expr_ctx(field, Ctx::default().with_ty(ty))?;
|
|
|
|
_ = self.assert_ty(field.pos(), &mut value, ty, "tuple field");
|
2024-10-20 11:49:41 -05:00
|
|
|
let mem = self.offset(mem, offset);
|
2024-10-21 08:12:37 -05:00
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(mem, ty, value.id);
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
let field_list = offs
|
2024-10-25 15:59:01 -05:00
|
|
|
.into_iter(self.tys)
|
2024-10-19 12:37:02 -05:00
|
|
|
.map(|(f, ..)| self.tys.names.ident_str(f.name))
|
|
|
|
.intersperse(", ")
|
|
|
|
.collect::<String>();
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
if !field_list.is_empty() {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!("the struct initializer is missing {field_list} \
|
|
|
|
(append them to the end of the constructor)"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Some(Value::ptr(mem).ty(sty))
|
|
|
|
}
|
|
|
|
ty::Kind::Slice(s) => {
|
2024-11-08 03:25:34 -06:00
|
|
|
let slice = &self.tys.ins.slices[s];
|
2024-10-19 12:37:02 -05:00
|
|
|
let len = slice.len().unwrap_or(fields.len());
|
|
|
|
let elem = slice.elem;
|
|
|
|
let elem_size = self.tys.size_of(elem);
|
|
|
|
let aty = slice
|
|
|
|
.len()
|
|
|
|
.map_or_else(|| self.tys.make_array(elem, len as ArrayLen), |_| sty);
|
|
|
|
|
|
|
|
if len != fields.len() {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"expected '{}' but constructor has {} elements",
|
|
|
|
self.ty_display(aty),
|
|
|
|
fields.len()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Value::NEVER;
|
|
|
|
}
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-11-08 04:51:10 -06:00
|
|
|
let mem = self.new_stack(pos, aty);
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
for (field, offset) in
|
|
|
|
fields.iter().zip((0u32..).step_by(elem_size as usize))
|
|
|
|
{
|
2024-10-20 14:00:56 -05:00
|
|
|
let mut value = self.expr_ctx(field, Ctx::default().with_ty(elem))?;
|
|
|
|
_ = self.assert_ty(field.pos(), &mut value, elem, "array value");
|
2024-10-20 11:49:41 -05:00
|
|
|
let mem = self.offset(mem, offset);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(mem, elem, value.id);
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(Value::ptr(mem).ty(aty))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let inferred = if ty.is_some() { "" } else { "inferred " };
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"the {inferred}type of the constructor is `{}`, \
|
|
|
|
but thats not a struct nor slice or array",
|
|
|
|
self.ty_display(sty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
Value::NEVER
|
|
|
|
}
|
|
|
|
}
|
2024-10-19 03:17:36 -05:00
|
|
|
}
|
2024-10-22 03:08:50 -05:00
|
|
|
Expr::Struct { .. } => {
|
2024-10-30 14:20:03 -05:00
|
|
|
let value = self.ty(expr).repr();
|
|
|
|
Some(self.ci.nodes.new_const_lit(ty::Id::TYPE, value))
|
2024-10-22 03:08:50 -05:00
|
|
|
}
|
2024-10-17 15:29:09 -05:00
|
|
|
Expr::Ctor { pos, ty, fields, .. } => {
|
2024-10-31 04:56:59 -05:00
|
|
|
ctx.ty = ty
|
|
|
|
.map(|ty| self.ty(ty))
|
|
|
|
.or(ctx.ty.map(|ty| self.tys.inner_of(ty).unwrap_or(ty)));
|
2024-10-27 15:34:03 -05:00
|
|
|
inference!(sty, ctx, self, pos, "struct", "<struct_ty>.{...}");
|
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
|
2024-10-25 15:59:01 -05:00
|
|
|
let mut offs = OffsetIter::new(s, self.tys)
|
|
|
|
.into_iter(self.tys)
|
2024-10-17 15:29:09 -05:00
|
|
|
.map(|(f, o)| (f.ty, o))
|
|
|
|
.collect::<Vec<_>>();
|
2024-11-08 04:51:10 -06:00
|
|
|
let mem = self.new_stack(pos, sty);
|
2024-10-17 15:29:09 -05:00
|
|
|
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) =
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::replace(&mut offs[index], (ty::Id::UNDECLARED, field.pos));
|
2024-10-17 15:29:09 -05:00
|
|
|
|
|
|
|
if ty == ty::Id::UNDECLARED {
|
|
|
|
self.report(field.pos, "the struct field is already initialized");
|
|
|
|
self.report(offset, "previous initialization is here");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-25 08:07:39 -05:00
|
|
|
let mut value = self.expr_ctx(&field.value, Ctx::default().with_ty(ty))?;
|
|
|
|
self.assert_ty(field.pos, &mut value, ty, fa!("field {}", field.name));
|
2024-10-20 11:49:41 -05:00
|
|
|
let mem = self.offset(mem, offset);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(mem, ty, 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-10-28 11:22:18 -05:00
|
|
|
let aclass_base = self.ci.scope.aclasses.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));
|
2024-10-20 14:00:56 -05:00
|
|
|
if let Some(mut id) = ret {
|
|
|
|
self.assert_ty(stmt.pos(), &mut id, ty::Id::VOID, "statement");
|
2024-09-28 14:56:39 -05:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 04:16:11 -05:00
|
|
|
|
2024-10-18 06:11:11 -05:00
|
|
|
for var in self.ci.scope.vars.drain(base..) {
|
2024-10-22 15:57:40 -05:00
|
|
|
var.remove(&mut self.ci.nodes);
|
2024-09-28 14:56:39 -05:00
|
|
|
}
|
|
|
|
|
2024-10-28 11:22:18 -05:00
|
|
|
for aclass in self.ci.scope.aclasses.drain(aclass_base..) {
|
|
|
|
aclass.remove(&mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
ret
|
2024-09-05 04:16:11 -05:00
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
Expr::Loop { body, .. } => {
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(
|
|
|
|
ty::Id::VOID,
|
|
|
|
Kind::Loop,
|
|
|
|
[self.ci.ctrl.get(), self.ci.ctrl.get(), LOOPS],
|
|
|
|
self.tys,
|
|
|
|
),
|
2024-10-26 05:09:53 -05:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-11-11 14:55:18 -06:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
self.ci.loops.push(Loop {
|
2024-10-26 05:09:53 -05:00
|
|
|
node: self.ci.ctrl.get(),
|
|
|
|
ctrl: [StrongRef::DEFAULT; 2],
|
2024-10-18 06:11:11 -05:00
|
|
|
ctrl_scope: core::array::from_fn(|_| Default::default()),
|
2024-10-22 15:57:40 -05:00
|
|
|
scope: self.ci.scope.dup(&mut self.ci.nodes),
|
2024-09-05 18:17:54 -05:00
|
|
|
});
|
|
|
|
|
2024-11-03 01:59:42 -06:00
|
|
|
for var in self.ci.scope.vars.iter_mut().skip(self.ci.inline_var_base) {
|
2024-11-11 02:06:34 -06:00
|
|
|
if !var.ptr && var.value() != NEVER {
|
2024-11-10 02:17:43 -06:00
|
|
|
var.set_value(VOID, &mut self.ci.nodes);
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
2024-11-03 01:59:42 -06:00
|
|
|
for aclass in self.ci.scope.aclasses[..2].iter_mut() {
|
|
|
|
aclass.last_store.set(VOID, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
for aclass in self.ci.scope.aclasses.iter_mut().skip(self.ci.inline_aclass_base) {
|
2024-10-28 10:18:53 -05:00
|
|
|
aclass.last_store.set(VOID, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
self.expr(body);
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
let Loop { ctrl: [con, ..], ctrl_scope: [cons, ..], .. } =
|
|
|
|
self.ci.loops.last_mut().unwrap();
|
2024-10-26 05:09:53 -05:00
|
|
|
let mut cons = mem::take(cons);
|
2024-09-08 10:11:33 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
if let Some(con) = mem::take(con).unwrap(&mut self.ci.nodes) {
|
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(
|
|
|
|
ty::Id::VOID,
|
|
|
|
Kind::Region,
|
|
|
|
[con, self.ci.ctrl.get()],
|
|
|
|
self.tys,
|
|
|
|
),
|
2024-09-15 13:14:56 -05:00
|
|
|
&mut self.ci.nodes,
|
2024-10-26 05:09:53 -05:00
|
|
|
);
|
|
|
|
self.ci.nodes.merge_scopes(
|
2024-09-15 13:14:56 -05:00
|
|
|
&mut self.ci.loops,
|
2024-10-26 05:09:53 -05:00
|
|
|
&self.ci.ctrl,
|
2024-10-18 06:11:11 -05:00
|
|
|
&mut self.ci.scope,
|
2024-09-15 13:14:56 -05:00
|
|
|
&mut cons,
|
2024-11-10 10:32:24 -06:00
|
|
|
self.tys,
|
2024-09-15 13:14:56 -05:00
|
|
|
);
|
2024-10-22 15:57:40 -05:00
|
|
|
cons.clear(&mut self.ci.nodes);
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
let Loop { node, ctrl: [.., bre], ctrl_scope: [.., mut bres], mut scope } =
|
|
|
|
self.ci.loops.pop().unwrap();
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.nodes.modify_input(node, 1, self.ci.ctrl.get());
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
if let Some(idx) =
|
|
|
|
self.ci.nodes[node].outputs.iter().position(|&n| self.ci.nodes.is_cfg(n))
|
|
|
|
{
|
|
|
|
self.ci.nodes[node].outputs.swap(idx, 0);
|
|
|
|
}
|
2024-09-12 11:42:21 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
let Some(bre) = bre.unwrap(&mut self.ci.nodes) else {
|
2024-10-28 10:18:53 -05:00
|
|
|
for (loop_var, scope_var) in
|
|
|
|
self.ci.scope.vars.iter_mut().zip(scope.vars.iter_mut())
|
|
|
|
{
|
2024-10-23 05:26:07 -05:00
|
|
|
if self.ci.nodes[scope_var.value()].is_lazy_phi(node) {
|
|
|
|
if loop_var.value() != scope_var.value() {
|
|
|
|
scope_var.set_value(
|
|
|
|
self.ci.nodes.modify_input(
|
|
|
|
scope_var.value(),
|
|
|
|
2,
|
|
|
|
loop_var.value(),
|
|
|
|
),
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let phi = &self.ci.nodes[scope_var.value()];
|
|
|
|
let prev = phi.inputs[1];
|
|
|
|
self.ci.nodes.replace(scope_var.value(), prev);
|
|
|
|
scope_var.set_value(prev, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-28 10:18:53 -05:00
|
|
|
|
|
|
|
for (loop_class, scope_class) in
|
|
|
|
self.ci.scope.aclasses.iter_mut().zip(scope.aclasses.iter_mut())
|
|
|
|
{
|
|
|
|
if self.ci.nodes[scope_class.last_store.get()].is_lazy_phi(node) {
|
2024-10-29 04:01:37 -05:00
|
|
|
if loop_class.last_store.get() != scope_class.last_store.get()
|
|
|
|
&& loop_class.last_store.get() != 0
|
|
|
|
{
|
2024-10-28 10:18:53 -05:00
|
|
|
scope_class.last_store.set(
|
|
|
|
self.ci.nodes.modify_input(
|
|
|
|
scope_class.last_store.get(),
|
|
|
|
2,
|
|
|
|
loop_class.last_store.get(),
|
|
|
|
),
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let phi = &self.ci.nodes[scope_class.last_store.get()];
|
|
|
|
let prev = phi.inputs[1];
|
|
|
|
self.ci.nodes.replace(scope_class.last_store.get(), prev);
|
|
|
|
scope_class.last_store.set(prev, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
}
|
2024-11-03 01:59:42 -06:00
|
|
|
|
|
|
|
if loop_class.last_store.get() == 0 {
|
|
|
|
loop_class
|
|
|
|
.last_store
|
|
|
|
.set(scope_class.last_store.get(), &mut self.ci.nodes);
|
|
|
|
}
|
2024-10-28 10:18:53 -05:00
|
|
|
}
|
|
|
|
|
2024-11-03 01:59:42 -06:00
|
|
|
debug_assert!(self.ci.scope.aclasses.iter().all(|a| a.last_store.get() != 0));
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
scope.clear(&mut self.ci.nodes);
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.set(NEVER, &mut self.ci.nodes);
|
2024-10-27 05:32:34 -05:00
|
|
|
|
2024-09-05 18:17:54 -05:00
|
|
|
return None;
|
2024-10-26 05:09:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.ci.ctrl.set(bre, &mut self.ci.nodes);
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
mem::swap(&mut self.ci.scope, &mut bres);
|
2024-09-06 15:00:23 -05:00
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
debug_assert_eq!(self.ci.scope.vars.len(), scope.vars.len());
|
|
|
|
debug_assert_eq!(self.ci.scope.vars.len(), bres.vars.len());
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.nodes.lock(node);
|
2024-10-19 03:17:36 -05:00
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
for ((dest_var, scope_var), loop_var) in self
|
|
|
|
.ci
|
|
|
|
.scope
|
|
|
|
.vars
|
|
|
|
.iter_mut()
|
|
|
|
.zip(scope.vars.iter_mut())
|
|
|
|
.zip(bres.vars.iter_mut())
|
2024-10-22 15:57:40 -05:00
|
|
|
{
|
|
|
|
if self.ci.nodes[scope_var.value()].is_lazy_phi(node) {
|
|
|
|
if loop_var.value() != scope_var.value() {
|
|
|
|
scope_var.set_value(
|
|
|
|
self.ci.nodes.modify_input(scope_var.value(), 2, loop_var.value()),
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-09-08 05:00:07 -05:00
|
|
|
} else {
|
2024-10-22 15:57:40 -05:00
|
|
|
if dest_var.value() == scope_var.value() {
|
|
|
|
dest_var.set_value(VOID, &mut self.ci.nodes);
|
2024-09-22 11:17:30 -05:00
|
|
|
}
|
2024-10-22 15:57:40 -05:00
|
|
|
let phi = &self.ci.nodes[scope_var.value()];
|
2024-09-08 05:00:07 -05:00
|
|
|
let prev = phi.inputs[1];
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.nodes.replace(scope_var.value(), prev);
|
|
|
|
scope_var.set_value(prev, &mut self.ci.nodes);
|
2024-09-08 05:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:17:54 -05:00
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
if dest_var.value() == VOID {
|
|
|
|
dest_var.set_value(scope_var.value(), &mut self.ci.nodes);
|
2024-09-05 18:17:54 -05:00
|
|
|
}
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
debug_assert!(!self.ci.nodes[dest_var.value()].is_lazy_phi(node));
|
2024-10-18 09:51:54 -05:00
|
|
|
}
|
|
|
|
|
2024-10-28 10:18:53 -05:00
|
|
|
for ((dest_class, scope_class), loop_class) in self
|
|
|
|
.ci
|
|
|
|
.scope
|
|
|
|
.aclasses
|
|
|
|
.iter_mut()
|
|
|
|
.zip(scope.aclasses.iter_mut())
|
|
|
|
.zip(bres.aclasses.iter_mut())
|
|
|
|
{
|
|
|
|
if self.ci.nodes[scope_class.last_store.get()].is_lazy_phi(node) {
|
2024-10-29 04:01:37 -05:00
|
|
|
if loop_class.last_store.get() != scope_class.last_store.get()
|
|
|
|
&& loop_class.last_store.get() != 0
|
|
|
|
{
|
2024-10-28 10:18:53 -05:00
|
|
|
scope_class.last_store.set(
|
|
|
|
self.ci.nodes.modify_input(
|
|
|
|
scope_class.last_store.get(),
|
|
|
|
2,
|
|
|
|
loop_class.last_store.get(),
|
|
|
|
),
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if dest_class.last_store.get() == scope_class.last_store.get() {
|
|
|
|
dest_class.last_store.set(VOID, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
let phi = &self.ci.nodes[scope_class.last_store.get()];
|
|
|
|
let prev = phi.inputs[1];
|
|
|
|
self.ci.nodes.replace(scope_class.last_store.get(), prev);
|
|
|
|
scope_class.last_store.set(prev, &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dest_class.last_store.get() == VOID {
|
|
|
|
dest_class.last_store.set(scope_class.last_store.get(), &mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert!(!self.ci.nodes[dest_class.last_store.get()].is_lazy_phi(node));
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
scope.clear(&mut self.ci.nodes);
|
|
|
|
bres.clear(&mut self.ci.nodes);
|
2024-10-20 05:22:28 -05:00
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.nodes.unlock(node);
|
2024-11-10 10:32:24 -06:00
|
|
|
let rpl = self.ci.nodes.late_peephole(node, self.tys).unwrap_or(node);
|
2024-10-26 05:09:53 -05:00
|
|
|
if self.ci.ctrl.get() == node {
|
|
|
|
self.ci.ctrl.set_remove(rpl, &mut self.ci.nodes);
|
2024-10-22 15:57:40 -05:00
|
|
|
}
|
2024-10-21 08:12:37 -05:00
|
|
|
|
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_, .. } => {
|
2024-10-25 08:07:39 -05:00
|
|
|
let mut cnd = self.expr_ctx(cond, Ctx::default().with_ty(ty::Id::BOOL))?;
|
|
|
|
self.assert_ty(cond.pos(), &mut cnd, ty::Id::BOOL, "condition");
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
let if_node = self.ci.nodes.new_node(
|
|
|
|
ty::Id::VOID,
|
|
|
|
Kind::If,
|
|
|
|
[self.ci.ctrl.get(), cnd.id],
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-09-09 12:36:53 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
'b: {
|
2024-10-27 05:32:34 -05:00
|
|
|
let branch = match self.ci.nodes[if_node].ty {
|
|
|
|
ty::Id::LEFT_UNREACHABLE => else_,
|
|
|
|
ty::Id::RIGHT_UNREACHABLE => Some(then),
|
2024-09-28 14:56:39 -05:00
|
|
|
_ => break 'b,
|
|
|
|
};
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-09-28 14:56:39 -05:00
|
|
|
self.ci.nodes.remove(if_node);
|
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-22 15:57:40 -05:00
|
|
|
let else_scope = self.ci.scope.dup(&mut self.ci.nodes);
|
2024-09-28 14:56:39 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(ty::Id::VOID, Kind::Then, [if_node], self.tys),
|
2024-10-26 05:09:53 -05:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
|
|
|
let lcntrl = self.expr(then).map_or(Nid::MAX, |_| self.ci.ctrl.get());
|
2024-09-28 14:56:39 -05:00
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
let mut then_scope = mem::replace(&mut self.ci.scope, else_scope);
|
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(ty::Id::VOID, Kind::Else, [if_node], self.tys),
|
2024-10-26 05:09:53 -05:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-09-28 14:56:39 -05:00
|
|
|
let rcntrl = if let Some(else_) = else_ {
|
2024-10-26 05:09:53 -05:00
|
|
|
self.expr(else_).map_or(Nid::MAX, |_| self.ci.ctrl.get())
|
2024-09-03 10:51:28 -05:00
|
|
|
} else {
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.get()
|
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-22 15:57:40 -05:00
|
|
|
then_scope.clear(&mut self.ci.nodes);
|
2024-09-28 14:56:39 -05:00
|
|
|
return None;
|
|
|
|
} else if lcntrl == Nid::MAX {
|
2024-10-22 15:57:40 -05:00
|
|
|
then_scope.clear(&mut self.ci.nodes);
|
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-22 15:57:40 -05:00
|
|
|
self.ci.scope.clear(&mut self.ci.nodes);
|
2024-10-18 06:11:11 -05:00
|
|
|
self.ci.scope = then_scope;
|
2024-10-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.set(lcntrl, &mut self.ci.nodes);
|
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-26 05:09:53 -05:00
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(ty::Id::VOID, Kind::Region, [lcntrl, rcntrl], self.tys),
|
2024-09-28 14:56:39 -05:00
|
|
|
&mut self.ci.nodes,
|
2024-10-26 05:09:53 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
self.ci.nodes.merge_scopes(
|
2024-09-28 14:56:39 -05:00
|
|
|
&mut self.ci.loops,
|
2024-10-26 05:09:53 -05:00
|
|
|
&self.ci.ctrl,
|
2024-10-18 09:51:54 -05:00
|
|
|
&mut self.ci.scope,
|
2024-09-28 14:56:39 -05:00
|
|
|
&mut then_scope,
|
2024-11-10 10:32:24 -06:00
|
|
|
self.tys,
|
2024-09-28 14:56:39 -05:00
|
|
|
);
|
2024-10-22 15:57:40 -05:00
|
|
|
then_scope.clear(&mut self.ci.nodes);
|
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-10-25 15:59:01 -05:00
|
|
|
ref e => {
|
|
|
|
self.report_unhandled_ast(e, "bruh");
|
2024-10-27 13:55:11 -05:00
|
|
|
Value::NEVER
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-12 12:02:29 -06:00
|
|
|
fn gen_inferred_const(
|
|
|
|
&mut self,
|
|
|
|
ctx: Ctx,
|
|
|
|
fallback: ty::Id,
|
|
|
|
value: impl Into<i64>,
|
|
|
|
filter: impl Fn(ty::Id) -> bool,
|
|
|
|
) -> Option<Value> {
|
|
|
|
Some(
|
|
|
|
self.ci.nodes.new_const_lit(
|
|
|
|
ctx.ty
|
|
|
|
.map(|ty| self.tys.inner_of(ty).unwrap_or(ty))
|
|
|
|
.filter(|&ty| filter(ty))
|
|
|
|
.unwrap_or(fallback),
|
|
|
|
value,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-10 12:35:48 -06:00
|
|
|
fn gen_call(&mut self, func: &Expr, args: &[Expr], inline: bool) -> Option<Value> {
|
|
|
|
let ty = self.ty(func);
|
|
|
|
let ty::Kind::Func(mut fu) = ty.expand() else {
|
|
|
|
self.report(func.pos(), fa!("compiler cant (yet) call '{}'", self.ty_display(ty)));
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(sig) = self.compute_signature(&mut fu, func.pos(), args) else {
|
|
|
|
return Value::NEVER;
|
|
|
|
};
|
|
|
|
self.make_func_reachable(fu);
|
|
|
|
|
|
|
|
let Func { expr, file, is_inline, .. } = self.tys.ins.funcs[fu];
|
|
|
|
let ast = &self.files[file.index()];
|
|
|
|
let &Expr::Closure { args: cargs, body, .. } = expr.get(ast) else { unreachable!() };
|
|
|
|
|
|
|
|
if args.len() != cargs.len() {
|
|
|
|
self.report(
|
|
|
|
func.pos(),
|
|
|
|
fa!(
|
|
|
|
"expected {} function argumenr{}, got {}",
|
|
|
|
cargs.len(),
|
|
|
|
if cargs.len() == 1 { "" } else { "s" },
|
|
|
|
args.len()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if inline && is_inline {
|
|
|
|
self.report(
|
|
|
|
func.pos(),
|
|
|
|
"function is declared as inline so this @inline directive only reduces readability",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_inline || inline {
|
|
|
|
let mut tys = sig.args.args();
|
|
|
|
let mut args = args.iter();
|
|
|
|
let mut cargs = cargs.iter();
|
|
|
|
let var_base = self.ci.scope.vars.len();
|
|
|
|
let aclass_base = self.ci.scope.aclasses.len();
|
|
|
|
while let Some(aty) = tys.next(self.tys) {
|
|
|
|
let carg = cargs.next().unwrap();
|
|
|
|
let Some(arg) = args.next() else { break };
|
|
|
|
match aty {
|
|
|
|
Arg::Type(id) => {
|
|
|
|
self.ci.scope.vars.push(Variable::new(
|
|
|
|
carg.id,
|
|
|
|
id,
|
|
|
|
false,
|
|
|
|
NEVER,
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Arg::Value(ty) => {
|
|
|
|
let mut value = self.raw_expr_ctx(arg, Ctx::default().with_ty(ty))?;
|
|
|
|
self.strip_var(&mut value);
|
|
|
|
debug_assert_ne!(self.ci.nodes[value.id].kind, Kind::Stre);
|
|
|
|
debug_assert_ne!(value.id, 0);
|
|
|
|
self.assert_ty(arg.pos(), &mut value, ty, fa!("argument {}", carg.name));
|
|
|
|
|
|
|
|
self.ci.scope.vars.push(Variable::new(
|
|
|
|
carg.id,
|
|
|
|
ty,
|
|
|
|
value.ptr,
|
|
|
|
value.id,
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let prev_var_base = mem::replace(&mut self.ci.inline_var_base, var_base);
|
|
|
|
let prev_aclass_base = mem::replace(&mut self.ci.inline_aclass_base, aclass_base);
|
|
|
|
let prev_inline_ret = self.ci.inline_ret.take();
|
|
|
|
self.ci.inline_depth += 1;
|
|
|
|
let prev_ret = self.ci.ret.replace(sig.ret);
|
|
|
|
let prev_file = mem::replace(&mut self.ci.file, file);
|
|
|
|
let prev_ctrl = self.ci.ctrl.get();
|
|
|
|
|
|
|
|
if self.expr(body).is_some() {
|
|
|
|
if sig.ret == ty::Id::VOID {
|
|
|
|
self.expr(&Expr::Return { pos: body.pos(), val: None });
|
|
|
|
} else {
|
|
|
|
self.report(
|
|
|
|
body.pos(),
|
|
|
|
"expected all paths in the fucntion to return \
|
|
|
|
or the return type to be 'void'",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.ret = prev_ret;
|
|
|
|
self.ci.file = prev_file;
|
|
|
|
self.ci.inline_depth -= 1;
|
|
|
|
self.ci.inline_var_base = prev_var_base;
|
|
|
|
self.ci.inline_aclass_base = prev_aclass_base;
|
|
|
|
for var in self.ci.scope.vars.drain(var_base..) {
|
|
|
|
var.remove(&mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
for var in self.ci.scope.aclasses.drain(aclass_base..) {
|
|
|
|
var.remove(&mut self.ci.nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (v, ctrl, scope) = mem::replace(&mut self.ci.inline_ret, prev_inline_ret)?;
|
2024-11-12 10:11:39 -06:00
|
|
|
if is_inline
|
2024-11-12 10:32:20 -06:00
|
|
|
&& ctrl.get() != prev_ctrl
|
2024-11-12 12:02:29 -06:00
|
|
|
&& (!self.ci.nodes[ctrl.get()].kind.is_eca()
|
|
|
|
|| self.ci.nodes[ctrl.get()].inputs[0] != prev_ctrl)
|
2024-11-12 10:11:39 -06:00
|
|
|
{
|
2024-11-10 12:35:48 -06:00
|
|
|
self.report(body.pos(), "function is makred inline but it contains controlflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ci.nodes.unlock(v.id);
|
|
|
|
self.ci.scope.clear(&mut self.ci.nodes);
|
|
|
|
self.ci.scope = scope;
|
|
|
|
self.ci.scope.vars.drain(var_base..).for_each(|v| v.remove(&mut self.ci.nodes));
|
|
|
|
self.ci.scope.aclasses.drain(aclass_base..).for_each(|v| v.remove(&mut self.ci.nodes));
|
|
|
|
|
|
|
|
mem::replace(&mut self.ci.ctrl, ctrl).remove(&mut self.ci.nodes);
|
|
|
|
|
|
|
|
Some(v)
|
|
|
|
} else {
|
|
|
|
let mut inps = Vc::from([NEVER]);
|
|
|
|
let mut tys = sig.args.args();
|
|
|
|
let mut cargs = cargs.iter();
|
|
|
|
let mut args = args.iter();
|
|
|
|
let mut clobbered_aliases = BitSet::default();
|
|
|
|
while let Some(ty) = tys.next(self.tys) {
|
|
|
|
let carg = cargs.next().unwrap();
|
|
|
|
let Some(arg) = args.next() else { break };
|
|
|
|
let Arg::Value(ty) = ty else { continue };
|
|
|
|
|
|
|
|
let mut value = self.raw_expr_ctx(arg, Ctx::default().with_ty(ty))?;
|
|
|
|
self.strip_var(&mut value);
|
|
|
|
debug_assert_ne!(self.ci.nodes[value.id].kind, Kind::Stre);
|
|
|
|
self.assert_ty(arg.pos(), &mut value, ty, fa!("argument {}", carg.name));
|
|
|
|
self.strip_ptr(&mut value);
|
|
|
|
self.add_clobbers(value, &mut clobbered_aliases);
|
|
|
|
|
|
|
|
self.ci.nodes.lock(value.id);
|
|
|
|
inps.push(value.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
for &n in inps.iter().skip(1) {
|
|
|
|
self.ci.nodes.unlock(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.append_clobbers(&mut inps, &mut clobbered_aliases);
|
|
|
|
|
|
|
|
let alt_value = match sig.ret.loc(self.tys) {
|
|
|
|
Loc::Reg => None,
|
|
|
|
Loc::Stack => {
|
|
|
|
let stck = self.new_stack(func.pos(), sig.ret);
|
|
|
|
clobbered_aliases.set(self.ci.nodes.aclass_index(stck).0 as _);
|
|
|
|
inps.push(stck);
|
|
|
|
Some(Value::ptr(stck).ty(sig.ret))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inps[0] = self.ci.ctrl.get();
|
|
|
|
self.ci.ctrl.set(
|
|
|
|
self.ci.nodes.new_node_nop(sig.ret, Kind::Call { func: fu, args: sig.args }, inps),
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
|
|
|
|
|
|
|
self.add_clobber_stores(clobbered_aliases);
|
|
|
|
|
|
|
|
alt_value.or(Some(Value::new(self.ci.ctrl.get()).ty(sig.ret)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:57:58 -06:00
|
|
|
fn gen_global(&mut self, global: ty::Global) -> Option<Value> {
|
|
|
|
let gl = &self.tys.ins.globals[global];
|
2024-11-10 10:32:24 -06:00
|
|
|
let value = self.ci.nodes.new_node_nop(gl.ty, Kind::Global { global }, [VOID]);
|
2024-11-08 03:57:58 -06:00
|
|
|
self.ci.nodes[value].aclass = GLOBAL_ACLASS as _;
|
|
|
|
Some(Value::ptr(value).ty(gl.ty))
|
|
|
|
}
|
|
|
|
|
2024-11-09 03:58:57 -06:00
|
|
|
fn gen_const(&mut self, cnst: ty::Const, ctx: Ctx) -> Option<Value> {
|
2024-11-08 03:57:58 -06:00
|
|
|
let c = &self.tys.ins.consts[cnst];
|
2024-11-10 05:26:30 -06:00
|
|
|
let prev = mem::replace(&mut self.ci.file, c.file);
|
2024-11-08 03:57:58 -06:00
|
|
|
let f = &self.files[c.file.index()];
|
|
|
|
let Expr::BinOp { left, right, .. } = c.ast.get(f) else { unreachable!() };
|
|
|
|
|
2024-11-10 05:03:15 -06:00
|
|
|
let mut value = left
|
|
|
|
.find_pattern_path(c.name, right, |expr, is_ct| {
|
|
|
|
debug_assert!(is_ct);
|
|
|
|
self.raw_expr_ctx(expr, ctx)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| unreachable!())?;
|
|
|
|
self.strip_var(&mut value);
|
2024-11-10 05:26:30 -06:00
|
|
|
self.ci.file = prev;
|
2024-11-10 05:03:15 -06:00
|
|
|
Some(value)
|
2024-11-08 03:57:58 -06:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:42:25 -05:00
|
|
|
fn add_clobbers(&mut self, value: Value, clobbered_aliases: &mut BitSet) {
|
2024-10-29 09:15:30 -05:00
|
|
|
if let Some(base) = self.tys.base_of(value.ty) {
|
2024-10-30 12:42:25 -05:00
|
|
|
clobbered_aliases.set(self.ci.nodes.aclass_index(value.id).0 as _);
|
2024-10-29 09:15:30 -05:00
|
|
|
if base.has_pointers(self.tys) {
|
2024-10-30 12:42:25 -05:00
|
|
|
clobbered_aliases.set(DEFAULT_ACLASS as _);
|
2024-10-29 09:15:30 -05:00
|
|
|
}
|
|
|
|
} else if value.ty.has_pointers(self.tys) {
|
2024-10-30 12:42:25 -05:00
|
|
|
clobbered_aliases.set(DEFAULT_ACLASS as _);
|
2024-10-29 09:15:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 12:42:25 -05:00
|
|
|
fn append_clobbers(&mut self, inps: &mut Vc, clobbered_aliases: &mut BitSet) {
|
|
|
|
clobbered_aliases.set(GLOBAL_ACLASS as _);
|
|
|
|
for clobbered in clobbered_aliases.iter() {
|
2024-10-29 09:15:30 -05:00
|
|
|
let aclass = &mut self.ci.scope.aclasses[clobbered];
|
|
|
|
self.ci.nodes.load_loop_aclass(clobbered, aclass, &mut self.ci.loops);
|
|
|
|
inps.push(aclass.last_store.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 12:42:25 -05:00
|
|
|
fn add_clobber_stores(&mut self, clobbered_aliases: BitSet) {
|
|
|
|
for clobbered in clobbered_aliases.iter() {
|
2024-11-10 11:56:33 -06:00
|
|
|
debug_assert_matches!(self.ci.nodes[self.ci.ctrl.get()].kind, Kind::Call { .. });
|
2024-10-30 07:45:19 -05:00
|
|
|
self.ci.scope.aclasses[clobbered].clobber.set(self.ci.ctrl.get(), &mut self.ci.nodes);
|
2024-10-29 09:15:30 -05:00
|
|
|
}
|
2024-10-30 07:45:19 -05:00
|
|
|
self.ci.nodes[self.ci.ctrl.get()].clobbers = clobbered_aliases;
|
2024-10-29 09:15:30 -05:00
|
|
|
}
|
|
|
|
|
2024-10-22 05:40:41 -05:00
|
|
|
fn struct_op(
|
|
|
|
&mut self,
|
|
|
|
pos: Pos,
|
|
|
|
op: TokenKind,
|
|
|
|
s: ty::Struct,
|
|
|
|
dst: Nid,
|
|
|
|
lhs: Nid,
|
|
|
|
rhs: Nid,
|
|
|
|
) -> bool {
|
2024-10-25 15:59:01 -05:00
|
|
|
let mut offs = OffsetIter::new(s, self.tys);
|
|
|
|
while let Some((ty, off)) = offs.next_ty(self.tys) {
|
2024-10-22 05:40:41 -05:00
|
|
|
let lhs = self.offset(lhs, off);
|
|
|
|
let rhs = self.offset(rhs, off);
|
|
|
|
let dst = self.offset(dst, off);
|
|
|
|
match ty.expand() {
|
|
|
|
_ if ty.is_pointer() || ty.is_integer() || ty == ty::Id::BOOL => {
|
|
|
|
let lhs = self.load_mem(lhs, ty);
|
|
|
|
let rhs = self.load_mem(rhs, ty);
|
2024-11-10 10:32:24 -06:00
|
|
|
let res =
|
|
|
|
self.ci.nodes.new_node(ty, Kind::BinOp { op }, [VOID, lhs, rhs], self.tys);
|
2024-10-24 02:43:07 -05:00
|
|
|
self.store_mem(dst, ty, res);
|
2024-10-22 05:40:41 -05:00
|
|
|
}
|
|
|
|
ty::Kind::Struct(is) => {
|
|
|
|
if !self.struct_op(pos, op, is, dst, lhs, rhs) {
|
|
|
|
self.report(
|
|
|
|
pos,
|
|
|
|
fa!(
|
|
|
|
"... when appliing '{0} {op} {0}'",
|
|
|
|
self.ty_display(ty::Kind::Struct(s).compress())
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.report(pos, fa!("'{0} {op} {0}' is not supported", self.ty_display(ty))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-10-22 00:20:08 -05:00
|
|
|
fn compute_signature(&mut self, func: &mut ty::Func, pos: Pos, args: &[Expr]) -> Option<Sig> {
|
2024-11-08 03:25:34 -06:00
|
|
|
let fuc = &self.tys.ins.funcs[*func];
|
|
|
|
let fast = self.files[fuc.file.index()].clone();
|
2024-10-22 00:20:08 -05:00
|
|
|
let &Expr::Closure { args: cargs, ret, .. } = fuc.expr.get(&fast) else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(if let Some(sig) = fuc.sig {
|
|
|
|
sig
|
|
|
|
} else {
|
|
|
|
let arg_base = self.tys.tmp.args.len();
|
|
|
|
|
|
|
|
let base = self.ci.scope.vars.len();
|
|
|
|
for (arg, carg) in args.iter().zip(cargs) {
|
|
|
|
let ty = self.ty(&carg.ty);
|
|
|
|
self.tys.tmp.args.push(ty);
|
|
|
|
let sym = parser::find_symbol(&fast.symbols, carg.id);
|
|
|
|
let ty = if sym.flags & idfl::COMPTIME == 0 {
|
|
|
|
// FIXME: could fuck us
|
|
|
|
ty::Id::UNDECLARED
|
|
|
|
} else {
|
2024-10-24 06:25:30 -05:00
|
|
|
if ty != ty::Id::TYPE {
|
|
|
|
self.report(
|
|
|
|
arg.pos(),
|
|
|
|
fa!(
|
|
|
|
"arbitrary comptime types are not supported yet \
|
|
|
|
(expected '{}' got '{}')",
|
|
|
|
self.ty_display(ty::Id::TYPE),
|
|
|
|
self.ty_display(ty)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
2024-10-22 00:20:08 -05:00
|
|
|
let ty = self.ty(arg);
|
|
|
|
self.tys.tmp.args.push(ty);
|
|
|
|
ty
|
|
|
|
};
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.scope.vars.push(Variable::new(
|
|
|
|
carg.id,
|
|
|
|
ty,
|
|
|
|
false,
|
|
|
|
NEVER,
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
));
|
2024-10-22 00:20:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let Some(args) = self.tys.pack_args(arg_base) else {
|
|
|
|
self.report(pos, "function instance has too many arguments");
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
let ret = self.ty(ret);
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.scope.vars.drain(base..).for_each(|v| v.remove(&mut self.ci.nodes));
|
2024-10-22 00:20:08 -05:00
|
|
|
|
|
|
|
let sym = SymKey::FuncInst(*func, args);
|
|
|
|
let ct = |ins: &mut crate::TypeIns| {
|
2024-11-12 12:02:29 -06:00
|
|
|
let fuc = ins.funcs[*func];
|
|
|
|
debug_assert!(fuc.comp_state.iter().all(|&s| s == CompState::default()));
|
2024-11-08 03:25:34 -06:00
|
|
|
ins.funcs
|
2024-11-12 12:02:29 -06:00
|
|
|
.push(Func { base: Some(*func), sig: Some(Sig { args, ret }), ..fuc })
|
2024-11-08 03:25:34 -06:00
|
|
|
.into()
|
2024-10-22 00:20:08 -05:00
|
|
|
};
|
2024-11-08 03:25:34 -06:00
|
|
|
let ty::Kind::Func(f) =
|
|
|
|
self.tys.syms.get_or_insert(sym, &mut self.tys.ins, ct).expand()
|
|
|
|
else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
*func = f;
|
2024-10-22 00:20:08 -05:00
|
|
|
|
|
|
|
Sig { args, ret }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:12:37 -05:00
|
|
|
fn assign_pattern(&mut self, pat: &Expr, right: Value) {
|
2024-10-20 14:00:56 -05:00
|
|
|
match *pat {
|
|
|
|
Expr::Ident { id, .. } => {
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.scope.vars.push(Variable::new(
|
2024-10-20 14:00:56 -05:00
|
|
|
id,
|
2024-10-22 15:57:40 -05:00
|
|
|
right.ty,
|
|
|
|
right.ptr,
|
|
|
|
right.id,
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
));
|
2024-10-20 14:00:56 -05:00
|
|
|
}
|
|
|
|
Expr::Ctor { pos, fields, .. } => {
|
|
|
|
let ty::Kind::Struct(idx) = right.ty.expand() else {
|
|
|
|
self.report(pos, "can't use struct destruct on non struct value (TODO: shold work with modules)");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
for &CtorField { pos, name, ref value } in fields {
|
2024-10-25 15:59:01 -05:00
|
|
|
let Some((offset, ty)) = OffsetIter::offset_of(self.tys, idx, name) else {
|
2024-10-20 14:00:56 -05:00
|
|
|
self.report(pos, format_args!("field not found: {name:?}"));
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let off = self.offset(right.id, offset);
|
|
|
|
self.assign_pattern(value, Value::ptr(off).ty(ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ref pat => self.report_unhandled_ast(pat, "pattern"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2024-10-26 17:02:59 -05:00
|
|
|
self.strip_ptr(&mut n);
|
2024-10-04 14:44:29 -05:00
|
|
|
Some(n)
|
|
|
|
}
|
|
|
|
|
2024-10-26 17:02:59 -05:00
|
|
|
fn expr(&mut self, expr: &Expr) -> Option<Value> {
|
|
|
|
self.expr_ctx(expr, Default::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strip_ptr(&mut self, target: &mut Value) {
|
|
|
|
if mem::take(&mut target.ptr) {
|
|
|
|
target.id = self.load_mem(target.id, target.ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 11:49:41 -05:00
|
|
|
fn offset(&mut self, val: Nid, off: Offset) -> Nid {
|
2024-10-18 06:11:11 -05:00
|
|
|
if off == 0 {
|
|
|
|
return val;
|
2024-10-18 02:52:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-30 14:20:03 -05:00
|
|
|
let off = self.ci.nodes.new_const(ty::Id::INT, off);
|
2024-11-04 05:38:47 -06:00
|
|
|
let aclass = self.ci.nodes.aclass_index(val).1;
|
2024-10-18 06:11:11 -05:00
|
|
|
let inps = [VOID, val, off];
|
2024-11-10 10:32:24 -06:00
|
|
|
let seted =
|
|
|
|
self.ci.nodes.new_node(ty::Id::INT, Kind::BinOp { op: TokenKind::Add }, inps, self.tys);
|
2024-11-04 05:38:47 -06:00
|
|
|
self.ci.nodes.pass_aclass(aclass, seted);
|
2024-10-30 07:45:19 -05:00
|
|
|
seted
|
2024-10-18 02:52:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-17 12:32:10 -05:00
|
|
|
fn strip_var(&mut self, n: &mut Value) {
|
2024-10-26 05:09:53 -05:00
|
|
|
if mem::take(&mut n.var) {
|
2024-10-17 15:29:09 -05:00
|
|
|
let id = (u16::MAX - n.id) as usize;
|
2024-10-19 12:37:02 -05:00
|
|
|
n.ptr = self.ci.scope.vars[id].ptr;
|
2024-10-22 15:57:40 -05:00
|
|
|
n.id = self.ci.scope.vars[id].value();
|
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;
|
|
|
|
};
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
if loob.ctrl[id].is_live() {
|
|
|
|
loob.ctrl[id].set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(
|
|
|
|
ty::Id::VOID,
|
|
|
|
Kind::Region,
|
|
|
|
[self.ci.ctrl.get(), loob.ctrl[id].get()],
|
|
|
|
self.tys,
|
|
|
|
),
|
2024-09-15 13:14:56 -05:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-10-26 05:09:53 -05:00
|
|
|
let mut scope = mem::take(&mut loob.ctrl_scope[id]);
|
|
|
|
let ctrl = mem::take(&mut loob.ctrl[id]);
|
|
|
|
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.merge_scopes(
|
|
|
|
&mut self.ci.loops,
|
|
|
|
&ctrl,
|
|
|
|
&mut scope,
|
|
|
|
&mut self.ci.scope,
|
|
|
|
self.tys,
|
|
|
|
);
|
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;
|
2024-10-26 05:09:53 -05:00
|
|
|
loob.ctrl[id] = ctrl;
|
|
|
|
self.ci.ctrl.set(NEVER, &mut self.ci.nodes);
|
|
|
|
} else {
|
|
|
|
let term = StrongRef::new(NEVER, &mut self.ci.nodes);
|
|
|
|
loob.ctrl[id] = mem::replace(&mut self.ci.ctrl, term);
|
|
|
|
loob.ctrl_scope[id] = self.ci.scope.dup(&mut self.ci.nodes);
|
|
|
|
loob.ctrl_scope[id]
|
|
|
|
.vars
|
|
|
|
.drain(loob.scope.vars.len()..)
|
|
|
|
.for_each(|v| v.remove(&mut self.ci.nodes));
|
2024-10-28 11:22:18 -05:00
|
|
|
loob.ctrl_scope[id]
|
|
|
|
.aclasses
|
|
|
|
.drain(loob.scope.aclasses.len()..)
|
|
|
|
.for_each(|v| v.remove(&mut self.ci.nodes));
|
2024-09-08 10:11:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2024-10-25 04:29:54 -05:00
|
|
|
fn complete_call_graph(&mut self) -> bool {
|
|
|
|
let prev_err_len = self.errors.borrow().len();
|
2024-10-25 15:59:01 -05:00
|
|
|
while self.ci.task_base < self.tys.tasks.len()
|
|
|
|
&& let Some(task_slot) = self.tys.tasks.pop()
|
2024-09-02 17:07:20 -05:00
|
|
|
{
|
|
|
|
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-10-25 04:29:54 -05:00
|
|
|
self.errors.borrow().len() == prev_err_len
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
fn emit_func(&mut self, FTask { file, id, ct }: FTask) {
|
2024-11-08 03:25:34 -06:00
|
|
|
let func = &mut self.tys.ins.funcs[id];
|
2024-10-19 12:53:43 -05:00
|
|
|
debug_assert_eq!(func.file, file);
|
2024-11-05 07:52:30 -06:00
|
|
|
let cct = self.ct.active();
|
|
|
|
debug_assert_eq!(cct, ct);
|
|
|
|
func.comp_state[cct as usize] = CompState::Compiled;
|
2024-10-19 12:37:02 -05:00
|
|
|
let sig = func.sig.expect("to emmit only concrete functions");
|
2024-11-08 03:25:34 -06:00
|
|
|
let ast = &self.files[file.index()];
|
2024-10-21 08:12:37 -05:00
|
|
|
let expr = func.expr.get(ast);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-10-20 03:37:48 -05:00
|
|
|
self.pool.push_ci(file, Some(sig.ret), 0, &mut self.ci);
|
2024-10-25 04:29:54 -05:00
|
|
|
let prev_err_len = self.errors.borrow().len();
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-11-12 13:42:04 -06:00
|
|
|
log::info!("{}", self.ast_display(expr));
|
|
|
|
|
2024-11-09 08:14:03 -06:00
|
|
|
let &Expr::Closure { body, args, pos, .. } = expr else {
|
2024-09-30 12:09:17 -05:00
|
|
|
unreachable!("{}", self.ast_display(expr))
|
2024-09-03 10:51:28 -05:00
|
|
|
};
|
2024-11-09 08:14:03 -06:00
|
|
|
self.ci.pos.push(pos);
|
2024-09-03 10:51:28 -05:00
|
|
|
|
2024-10-24 02:43:07 -05:00
|
|
|
let mut tys = sig.args.args();
|
|
|
|
let mut args = args.iter();
|
2024-10-25 15:59:01 -05:00
|
|
|
while let Some(aty) = tys.next(self.tys) {
|
2024-10-24 02:43:07 -05:00
|
|
|
let arg = args.next().unwrap();
|
|
|
|
match aty {
|
|
|
|
Arg::Type(ty) => {
|
|
|
|
self.ci.scope.vars.push(Variable::new(
|
|
|
|
arg.id,
|
|
|
|
ty,
|
|
|
|
false,
|
|
|
|
NEVER,
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Arg::Value(ty) => {
|
|
|
|
let mut deps = Vc::from([VOID]);
|
2024-10-25 15:59:01 -05:00
|
|
|
if ty.loc(self.tys) == Loc::Stack && self.tys.size_of(ty) <= 16 {
|
2024-10-24 02:43:07 -05:00
|
|
|
deps.push(MEM);
|
|
|
|
}
|
|
|
|
// TODO: whe we not using the deps?
|
|
|
|
let value = self.ci.nodes.new_node_nop(ty, Kind::Arg, deps);
|
2024-10-25 15:59:01 -05:00
|
|
|
let ptr = ty.loc(self.tys) == Loc::Stack;
|
2024-10-24 02:43:07 -05:00
|
|
|
self.ci.scope.vars.push(Variable::new(
|
|
|
|
arg.id,
|
|
|
|
ty,
|
|
|
|
ptr,
|
|
|
|
value,
|
|
|
|
&mut self.ci.nodes,
|
|
|
|
));
|
2024-10-28 11:19:41 -05:00
|
|
|
if ty.loc(self.tys) == Loc::Stack {
|
2024-10-30 12:42:25 -05:00
|
|
|
self.ci.nodes[value].aclass = self.ci.scope.aclasses.len() as _;
|
2024-10-28 11:19:41 -05:00
|
|
|
self.ci.scope.aclasses.push(AClass::new(&mut self.ci.nodes));
|
|
|
|
}
|
2024-10-24 02:43:07 -05:00
|
|
|
}
|
2024-10-21 08:12:37 -05:00
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 05:32:34 -05:00
|
|
|
if self.expr(body).is_some() {
|
|
|
|
if sig.ret == ty::Id::VOID {
|
|
|
|
self.expr(&Expr::Return { pos: body.pos(), val: None });
|
|
|
|
} else {
|
|
|
|
self.report(
|
|
|
|
body.pos(),
|
|
|
|
fa!(
|
|
|
|
"expected all paths in the fucntion to return \
|
|
|
|
or the return type to be 'void' (return type is '{}')",
|
|
|
|
self.ty_display(sig.ret),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-09-03 10:51:28 -05:00
|
|
|
}
|
|
|
|
|
2024-10-22 15:57:40 -05:00
|
|
|
self.ci.scope.vars.drain(..).for_each(|v| v.remove_ignore_arg(&mut self.ci.nodes));
|
|
|
|
|
2024-11-03 14:13:24 -06:00
|
|
|
if self.finalize(prev_err_len) {
|
2024-11-05 07:52:30 -06:00
|
|
|
let backend = if !cct { &mut *self.backend } else { &mut *self.ct_backend };
|
|
|
|
backend.emit_body(id, &mut self.ci.nodes, self.tys, self.files);
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-11-09 08:14:03 -06:00
|
|
|
self.ci.pos.pop();
|
2024-10-19 12:53:43 -05:00
|
|
|
self.pool.pop_ci(&mut self.ci);
|
2024-09-04 09:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-11-03 14:13:24 -06:00
|
|
|
fn finalize(&mut self, prev_err_len: usize) -> bool {
|
2024-11-04 12:18:37 -06:00
|
|
|
use {AssertKind as AK, CondOptRes as CR};
|
|
|
|
|
2024-11-03 14:13:24 -06:00
|
|
|
self.ci.finalize(&mut self.pool.nid_stack, self.tys, self.files);
|
2024-11-05 07:52:30 -06:00
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
let mut to_remove = vec![];
|
|
|
|
for (id, node) in self.ci.nodes.iter() {
|
2024-11-03 15:54:05 -06:00
|
|
|
let Kind::Assert { kind, pos } = node.kind else { continue };
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
let res = self.ci.nodes.try_opt_cond(id);
|
|
|
|
|
|
|
|
// TODO: highlight the pin position
|
|
|
|
let msg = match (kind, res) {
|
|
|
|
(AK::UnwrapCheck, CR::Known { value: false, .. }) => {
|
|
|
|
"unwrap is not needed since the value is (provably) never null, \
|
|
|
|
remove it, or replace with '@as(<expr_ty>, <opt_expr>)'"
|
|
|
|
}
|
|
|
|
(AK::UnwrapCheck, CR::Known { value: true, .. }) => {
|
|
|
|
"unwrap is incorrect since the value is (provably) always null, \
|
|
|
|
make sure your logic is correct"
|
|
|
|
}
|
|
|
|
(AK::NullCheck, CR::Known { value: true, .. }) => {
|
|
|
|
"the value is always null, some checks might need to be inverted"
|
|
|
|
}
|
|
|
|
(AK::NullCheck, CR::Unknown) => {
|
|
|
|
"can't prove the value is not 'null', \
|
|
|
|
use '@unwrap(<opt>)' if you believe compiler is stupid, \
|
|
|
|
or explicitly check for null and handle it \
|
|
|
|
('if <opt> == null { /* handle */ } else { /* use opt */ }')"
|
|
|
|
}
|
|
|
|
(AK::NullCheck, CR::Known { value: false, pin }) => {
|
|
|
|
to_remove.push((id, pin));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(AK::UnwrapCheck, CR::Unknown) => {
|
|
|
|
to_remove.push((id, None));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.report(pos, msg);
|
|
|
|
}
|
|
|
|
to_remove.into_iter().for_each(|(n, pin)| {
|
|
|
|
let pin = pin.unwrap_or_else(|| {
|
|
|
|
let mut pin = self.ci.nodes[n].inputs[0];
|
|
|
|
while matches!(self.ci.nodes[pin].kind, Kind::Assert { .. }) {
|
|
|
|
pin = self.ci.nodes[n].inputs[0];
|
|
|
|
}
|
|
|
|
pin
|
|
|
|
});
|
|
|
|
for mut out in self.ci.nodes[n].outputs.clone() {
|
|
|
|
if self.ci.nodes.is_cfg(out) {
|
|
|
|
let index = self.ci.nodes[out].inputs.iter().position(|&p| p == n).unwrap();
|
|
|
|
self.ci.nodes.modify_input(out, index, self.ci.nodes[n].inputs[0]);
|
|
|
|
} else {
|
2024-11-10 03:28:02 -06:00
|
|
|
if !self.ci.nodes[out].kind.is_pinned() && self.ci.nodes[out].inputs[0] != pin {
|
2024-11-04 12:18:37 -06:00
|
|
|
out = self.ci.nodes.modify_input(out, 0, pin);
|
2024-11-03 14:13:24 -06:00
|
|
|
}
|
2024-11-04 12:18:37 -06:00
|
|
|
let index =
|
|
|
|
self.ci.nodes[out].inputs[1..].iter().position(|&p| p == n).unwrap() + 1;
|
|
|
|
self.ci.nodes.modify_input(out, index, self.ci.nodes[n].inputs[2]);
|
|
|
|
}
|
2024-11-03 14:13:24 -06:00
|
|
|
}
|
2024-11-04 12:18:37 -06:00
|
|
|
debug_assert!(
|
|
|
|
self.ci.nodes.values[n as usize]
|
|
|
|
.as_ref()
|
|
|
|
.map_or(true, |n| !matches!(n.kind, Kind::Assert { .. })),
|
|
|
|
"{:?} {:?}",
|
|
|
|
self.ci.nodes[n],
|
|
|
|
self.ci.nodes[n].outputs.iter().map(|&o| &self.ci.nodes[o]).collect::<Vec<_>>(),
|
|
|
|
);
|
|
|
|
});
|
2024-11-05 07:52:30 -06:00
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
self.ci.unlock();
|
2024-11-05 07:52:30 -06:00
|
|
|
|
2024-11-08 04:51:10 -06:00
|
|
|
for &node in self.ci.nodes[NEVER].inputs.iter() {
|
|
|
|
if self.ci.nodes[node].kind == Kind::Return
|
|
|
|
&& self.ci.nodes[self.ci.nodes.aclass_index(self.ci.nodes[node].inputs[1]).1].kind
|
|
|
|
== Kind::Stck
|
|
|
|
{
|
|
|
|
self.report(
|
|
|
|
self.ci.nodes[node].pos,
|
|
|
|
"returning value with local provenance \
|
|
|
|
(pointer will be invalid after function returns)",
|
|
|
|
);
|
|
|
|
self.report(
|
|
|
|
self.ci.nodes[self.ci.nodes.aclass_index(self.ci.nodes[node].inputs[1]).1].pos,
|
|
|
|
"...the pointer points to stack allocation created here",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
if self.errors.borrow().len() == prev_err_len {
|
|
|
|
self.ci.nodes.check_final_integrity(self.ty_display(ty::Id::VOID));
|
|
|
|
self.ci.nodes.graphviz(self.ty_display(ty::Id::VOID));
|
|
|
|
self.ci.nodes.gcm(&mut self.pool.nid_stack, &mut self.pool.nid_set);
|
|
|
|
self.ci.nodes.basic_blocks();
|
|
|
|
self.ci.nodes.graphviz(self.ty_display(ty::Id::VOID));
|
2024-11-10 10:32:24 -06:00
|
|
|
} else {
|
2024-11-11 02:06:34 -06:00
|
|
|
//self.ci.nodes.graphviz_in_browser(self.ty_display(ty::Id::VOID));
|
2024-11-05 07:52:30 -06:00
|
|
|
}
|
|
|
|
|
2024-11-03 14:13:24 -06:00
|
|
|
self.errors.borrow().len() == prev_err_len
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
fn ty(&mut self, expr: &Expr) -> ty::Id {
|
2024-10-20 08:16:55 -05:00
|
|
|
self.parse_ty(self.ci.file, expr, None, self.files)
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_display(&self, ty: ty::Id) -> ty::Display {
|
2024-10-25 15:59:01 -05:00
|
|
|
ty::Display::new(self.tys, self.files, ty)
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
fn ast_display(&self, ast: &'a Expr<'a>) -> parser::Display<'a> {
|
2024-10-26 08:18:00 -05:00
|
|
|
parser::Display::new(&self.file().file, ast)
|
2024-09-30 12:09:17 -05:00
|
|
|
}
|
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
#[must_use]
|
|
|
|
#[track_caller]
|
2024-10-30 07:45:19 -05:00
|
|
|
fn binop_ty(
|
|
|
|
&mut self,
|
|
|
|
pos: Pos,
|
|
|
|
lhs: &mut Value,
|
|
|
|
rhs: &mut Value,
|
|
|
|
op: TokenKind,
|
2024-11-04 05:38:47 -06:00
|
|
|
) -> (ty::Id, Nid) {
|
2024-10-27 07:57:00 -05:00
|
|
|
if let Some(upcasted) = lhs.ty.try_upcast(rhs.ty) {
|
2024-10-20 14:50:08 -05:00
|
|
|
let to_correct = if lhs.ty != upcasted {
|
2024-10-30 07:45:19 -05:00
|
|
|
Some((lhs, rhs))
|
2024-10-20 14:00:56 -05:00
|
|
|
} else if rhs.ty != upcasted {
|
2024-10-30 07:45:19 -05:00
|
|
|
Some((rhs, lhs))
|
2024-10-20 14:50:08 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-10-30 07:45:19 -05:00
|
|
|
if let Some((oper, other)) = to_correct {
|
2024-10-27 12:21:33 -05:00
|
|
|
if self.tys.size_of(upcasted) > self.tys.size_of(oper.ty) {
|
|
|
|
self.extend(oper, upcasted);
|
|
|
|
}
|
2024-10-20 14:50:08 -05:00
|
|
|
if matches!(op, TokenKind::Add | TokenKind::Sub)
|
|
|
|
&& let Some(elem) = self.tys.base_of(upcasted)
|
|
|
|
{
|
2024-10-30 14:20:03 -05:00
|
|
|
let cnst = self.ci.nodes.new_const(ty::Id::INT, self.tys.size_of(elem));
|
2024-11-10 10:32:24 -06:00
|
|
|
oper.id = self.ci.nodes.new_node(
|
|
|
|
upcasted,
|
|
|
|
Kind::BinOp { op: TokenKind::Mul },
|
|
|
|
[VOID, oper.id, cnst],
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-11-04 05:38:47 -06:00
|
|
|
return (upcasted, self.ci.nodes.aclass_index(other.id).1);
|
2024-10-20 14:50:08 -05:00
|
|
|
}
|
2024-10-20 14:00:56 -05:00
|
|
|
}
|
2024-10-20 14:50:08 -05:00
|
|
|
|
2024-11-04 05:38:47 -06:00
|
|
|
(upcasted, VOID)
|
2024-09-02 17:07:20 -05:00
|
|
|
} else {
|
2024-10-20 14:00:56 -05:00
|
|
|
let ty = self.ty_display(lhs.ty);
|
|
|
|
let expected = self.ty_display(rhs.ty);
|
2024-10-19 12:37:02 -05:00
|
|
|
self.report(pos, fa!("'{ty} {op} {expected}' is not supported"));
|
2024-11-04 05:38:47 -06:00
|
|
|
(ty::Id::NEVER, VOID)
|
2024-10-19 12:37:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 04:51:10 -06:00
|
|
|
fn wrap_in_opt(&mut self, pos: Pos, val: &mut Value) {
|
2024-10-31 04:36:18 -05:00
|
|
|
debug_assert!(!val.var);
|
|
|
|
|
2024-11-07 03:43:29 -06:00
|
|
|
let was_ptr = val.ptr;
|
2024-10-31 04:36:18 -05:00
|
|
|
let oty = self.tys.make_opt(val.ty);
|
|
|
|
|
|
|
|
if let Some((uninit, ..)) = self.tys.nieche_of(val.ty) {
|
|
|
|
self.strip_ptr(val);
|
|
|
|
val.ty = oty;
|
|
|
|
assert!(!uninit, "TODO");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let OptLayout { flag_ty, flag_offset, payload_offset } = self.tys.opt_layout(val.ty);
|
|
|
|
|
|
|
|
match oty.loc(self.tys) {
|
|
|
|
Loc::Reg => {
|
|
|
|
self.strip_ptr(val);
|
|
|
|
// registers have inverted offsets so that accessing the inner type is a noop
|
2024-11-07 09:39:15 -06:00
|
|
|
let flag_offset = self.tys.size_of(oty) * 8 - flag_offset * 8 - 1;
|
|
|
|
let fill = self.ci.nodes.new_const(oty, 1i64 << flag_offset);
|
2024-11-10 10:32:24 -06:00
|
|
|
val.id = self.ci.nodes.new_node(
|
|
|
|
oty,
|
|
|
|
Kind::BinOp { op: TokenKind::Bor },
|
|
|
|
[VOID, val.id, fill],
|
|
|
|
self.tys,
|
|
|
|
);
|
2024-10-31 04:36:18 -05:00
|
|
|
val.ty = oty;
|
|
|
|
}
|
2024-10-31 05:10:05 -05:00
|
|
|
Loc::Stack => {
|
2024-10-31 04:36:18 -05:00
|
|
|
self.strip_ptr(val);
|
2024-11-08 04:51:10 -06:00
|
|
|
let stack = self.new_stack(pos, oty);
|
2024-10-31 04:36:18 -05:00
|
|
|
let fill = self.ci.nodes.new_const(flag_ty, 1);
|
|
|
|
self.store_mem(stack, flag_ty, fill);
|
|
|
|
let off = self.offset(stack, payload_offset);
|
|
|
|
self.store_mem(off, val.ty, val.id);
|
|
|
|
val.id = stack;
|
|
|
|
val.ptr = true;
|
|
|
|
val.ty = oty;
|
|
|
|
}
|
|
|
|
}
|
2024-11-07 03:43:29 -06:00
|
|
|
|
|
|
|
if !was_ptr {
|
|
|
|
self.strip_ptr(val);
|
|
|
|
}
|
2024-10-31 04:36:18 -05:00
|
|
|
}
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
fn implicit_unwrap(&mut self, pos: Pos, opt: &mut Value) {
|
|
|
|
self.unwrap_low(pos, opt, AssertKind::NullCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn explicit_unwrap(&mut self, pos: Pos, opt: &mut Value) {
|
|
|
|
self.unwrap_low(pos, opt, AssertKind::UnwrapCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap_low(&mut self, pos: Pos, opt: &mut Value, kind: AssertKind) {
|
2024-10-31 04:36:18 -05:00
|
|
|
let Some(ty) = self.tys.inner_of(opt.ty) else { return };
|
|
|
|
let null_check = self.gen_null_check(*opt, ty, TokenKind::Eq);
|
|
|
|
|
2024-11-04 12:18:37 -06:00
|
|
|
let oty = mem::replace(&mut opt.ty, ty);
|
|
|
|
self.unwrap_opt_unchecked(ty, oty, opt);
|
|
|
|
|
2024-10-31 04:36:18 -05:00
|
|
|
// TODO: extract the if check int a fucntion
|
2024-11-03 14:13:24 -06:00
|
|
|
self.ci.ctrl.set(
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node_nop(oty, Kind::Assert { kind, pos }, [
|
2024-11-04 12:18:37 -06:00
|
|
|
self.ci.ctrl.get(),
|
|
|
|
null_check,
|
|
|
|
opt.id,
|
|
|
|
]),
|
2024-11-03 14:13:24 -06:00
|
|
|
&mut self.ci.nodes,
|
|
|
|
);
|
2024-11-04 12:18:37 -06:00
|
|
|
self.ci.nodes.pass_aclass(self.ci.nodes.aclass_index(opt.id).1, self.ci.ctrl.get());
|
|
|
|
opt.id = self.ci.ctrl.get();
|
2024-10-31 04:36:18 -05:00
|
|
|
}
|
|
|
|
|
2024-11-03 03:15:03 -06:00
|
|
|
fn unwrap_opt_unchecked(&mut self, ty: ty::Id, oty: ty::Id, opt: &mut Value) {
|
|
|
|
if self.tys.nieche_of(ty).is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let OptLayout { payload_offset, .. } = self.tys.opt_layout(ty);
|
|
|
|
|
|
|
|
match oty.loc(self.tys) {
|
|
|
|
Loc::Reg => {}
|
|
|
|
Loc::Stack => {
|
|
|
|
opt.id = self.offset(opt.id, payload_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 04:36:18 -05:00
|
|
|
fn gen_null_check(&mut self, mut cmped: Value, ty: ty::Id, op: TokenKind) -> Nid {
|
|
|
|
let OptLayout { flag_ty, flag_offset, .. } = self.tys.opt_layout(ty);
|
2024-11-07 03:43:29 -06:00
|
|
|
debug_assert!(cmped.ty.is_optional());
|
2024-10-31 04:36:18 -05:00
|
|
|
|
|
|
|
match cmped.ty.loc(self.tys) {
|
|
|
|
Loc::Reg => {
|
|
|
|
self.strip_ptr(&mut cmped);
|
|
|
|
let inps = [VOID, cmped.id, self.ci.nodes.new_const(cmped.ty, 0)];
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(ty::Id::BOOL, Kind::BinOp { op }, inps, self.tys)
|
2024-10-31 04:36:18 -05:00
|
|
|
}
|
|
|
|
Loc::Stack => {
|
|
|
|
cmped.id = self.offset(cmped.id, flag_offset);
|
|
|
|
cmped.ty = flag_ty;
|
2024-11-07 03:43:29 -06:00
|
|
|
debug_assert!(cmped.ptr);
|
2024-10-31 04:36:18 -05:00
|
|
|
self.strip_ptr(&mut cmped);
|
2024-10-31 04:56:59 -05:00
|
|
|
let inps = [VOID, cmped.id, self.ci.nodes.new_const(flag_ty, 0)];
|
2024-11-10 10:32:24 -06:00
|
|
|
self.ci.nodes.new_node(ty::Id::BOOL, Kind::BinOp { op }, inps, self.tys)
|
2024-10-31 04:36:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 12:37:02 -05:00
|
|
|
#[track_caller]
|
2024-10-20 14:00:56 -05:00
|
|
|
fn assert_ty(
|
|
|
|
&mut self,
|
|
|
|
pos: Pos,
|
|
|
|
src: &mut Value,
|
|
|
|
expected: ty::Id,
|
|
|
|
hint: impl fmt::Display,
|
|
|
|
) -> bool {
|
2024-10-27 07:57:00 -05:00
|
|
|
if let Some(upcasted) = src.ty.try_upcast(expected)
|
2024-10-20 14:00:56 -05:00
|
|
|
&& upcasted == expected
|
|
|
|
{
|
2024-10-31 04:36:18 -05:00
|
|
|
if src.ty.is_never() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:00:56 -05:00
|
|
|
if src.ty != upcasted {
|
2024-10-31 04:36:18 -05:00
|
|
|
if let Some(inner) = self.tys.inner_of(upcasted) {
|
|
|
|
if inner != src.ty {
|
|
|
|
self.assert_ty(pos, src, inner, hint);
|
|
|
|
}
|
2024-11-08 04:51:10 -06:00
|
|
|
self.wrap_in_opt(pos, src);
|
2024-10-31 04:36:18 -05:00
|
|
|
} else {
|
|
|
|
debug_assert!(
|
|
|
|
src.ty.is_integer() || src.ty == ty::Id::NEVER,
|
|
|
|
"{} {}",
|
|
|
|
self.ty_display(src.ty),
|
|
|
|
self.ty_display(upcasted)
|
|
|
|
);
|
|
|
|
debug_assert!(
|
|
|
|
upcasted.is_integer() || src.ty == ty::Id::NEVER,
|
|
|
|
"{} {}",
|
|
|
|
self.ty_display(src.ty),
|
|
|
|
self.ty_display(upcasted)
|
|
|
|
);
|
|
|
|
self.extend(src, upcasted);
|
|
|
|
}
|
2024-10-20 14:00:56 -05:00
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
2024-10-31 04:36:18 -05:00
|
|
|
if let Some(inner) = self.tys.inner_of(src.ty)
|
|
|
|
&& inner.try_upcast(expected) == Some(expected)
|
|
|
|
{
|
2024-11-04 12:18:37 -06:00
|
|
|
self.implicit_unwrap(pos, src);
|
2024-10-31 04:36:18 -05:00
|
|
|
return self.assert_ty(pos, src, expected, hint);
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:00:56 -05:00
|
|
|
let ty = self.ty_display(src.ty);
|
2024-10-31 04:36:18 -05:00
|
|
|
|
2024-09-02 17:07:20 -05:00
|
|
|
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-10-20 14:00:56 -05:00
|
|
|
false
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 12:04:50 -05:00
|
|
|
fn extend(&mut self, value: &mut Value, to: ty::Id) {
|
|
|
|
self.strip_ptr(value);
|
2024-11-12 12:02:29 -06:00
|
|
|
let inps = [VOID, value.id];
|
2024-11-10 10:32:24 -06:00
|
|
|
*value =
|
2024-11-12 12:02:29 -06:00
|
|
|
self.ci.nodes.new_node_lit(to, Kind::UnOp { op: TokenKind::Number }, inps, self.tys);
|
2024-10-31 04:36:18 -05:00
|
|
|
value.ty = to;
|
2024-10-27 12:04:50 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 11:50:28 -05:00
|
|
|
#[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();
|
2024-10-26 08:18:00 -05:00
|
|
|
write!(buf, "{}", self.file().report(pos, msg)).unwrap();
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
2024-10-25 15:59:01 -05:00
|
|
|
fn report_unhandled_ast(&self, ast: &Expr, hint: impl Display) {
|
2024-10-19 12:37:02 -05:00
|
|
|
log::info!("{ast:#?}");
|
2024-10-25 15:59:01 -05:00
|
|
|
self.report(ast.pos(), fa!("compiler does not (yet) know how to handle ({hint})"));
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 08:18:00 -05:00
|
|
|
fn file(&self) -> &'a parser::Ast {
|
2024-11-08 03:25:34 -06:00
|
|
|
&self.files[self.ci.file.index()]
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeParser for Codegen<'_> {
|
|
|
|
fn tys(&mut self) -> &mut Types {
|
|
|
|
self.tys
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
fn eval_const(&mut self, file: Module, expr: &Expr, ret: ty::Id) -> u64 {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct.activate();
|
2024-10-26 05:09:53 -05:00
|
|
|
let mut scope = mem::take(&mut self.ci.scope.vars);
|
2024-10-25 15:59:01 -05:00
|
|
|
self.pool.push_ci(file, Some(ret), self.tys.tasks.len(), &mut self.ci);
|
|
|
|
self.ci.scope.vars = scope;
|
|
|
|
|
|
|
|
let prev_err_len = self.errors.borrow().len();
|
|
|
|
|
|
|
|
self.expr(&Expr::Return { pos: expr.pos(), val: Some(expr) });
|
|
|
|
|
2024-10-26 05:09:53 -05:00
|
|
|
scope = mem::take(&mut self.ci.scope.vars);
|
2024-10-25 15:59:01 -05:00
|
|
|
|
2024-11-03 14:13:24 -06:00
|
|
|
let res =
|
|
|
|
if self.finalize(prev_err_len) { self.emit_and_eval(file, ret, &mut []) } else { 1 };
|
2024-10-25 15:59:01 -05:00
|
|
|
|
|
|
|
self.pool.pop_ci(&mut self.ci);
|
|
|
|
self.ci.scope.vars = scope;
|
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct.deactivate();
|
2024-10-25 15:59:01 -05:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn infer_type(&mut self, expr: &Expr) -> ty::Id {
|
|
|
|
self.pool.save_ci(&self.ci);
|
|
|
|
let ty = self.expr(expr).map_or(ty::Id::NEVER, |v| v.ty);
|
2024-10-26 05:09:53 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
self.pool.restore_ci(&mut self.ci);
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_reuse(&mut self, existing: ty::Id) {
|
2024-11-05 07:52:30 -06:00
|
|
|
let state_slot = self.ct.active() as usize;
|
2024-10-25 15:59:01 -05:00
|
|
|
if let ty::Kind::Func(id) = existing.expand()
|
2024-11-08 03:25:34 -06:00
|
|
|
&& let func = &mut self.tys.ins.funcs[id]
|
2024-11-05 07:52:30 -06:00
|
|
|
&& let CompState::Queued(idx) = func.comp_state[state_slot]
|
2024-10-25 15:59:01 -05:00
|
|
|
&& idx < self.tys.tasks.len()
|
|
|
|
{
|
2024-11-05 07:52:30 -06:00
|
|
|
func.comp_state[state_slot] = CompState::Queued(self.tys.tasks.len());
|
2024-10-25 15:59:01 -05:00
|
|
|
let task = self.tys.tasks[idx].take();
|
|
|
|
self.tys.tasks.push(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
fn eval_global(&mut self, file: Module, name: Ident, expr: &Expr) -> ty::Id {
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct.activate();
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
let gid = self.tys.ins.globals.push(Global { file, name, ..Default::default() });
|
2024-10-25 15:59:01 -05:00
|
|
|
|
|
|
|
self.pool.push_ci(file, None, self.tys.tasks.len(), &mut self.ci);
|
|
|
|
let prev_err_len = self.errors.borrow().len();
|
|
|
|
|
|
|
|
self.expr(&(Expr::Return { pos: expr.pos(), val: Some(expr) }));
|
2024-09-02 17:07:20 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
let ret = self.ci.ret.expect("for return type to be infered");
|
2024-11-03 14:13:24 -06:00
|
|
|
if self.finalize(prev_err_len) {
|
2024-10-25 15:59:01 -05:00
|
|
|
let mut mem = vec![0u8; self.tys.size_of(ret) as usize];
|
|
|
|
self.emit_and_eval(file, ret, &mut mem);
|
2024-11-08 03:25:34 -06:00
|
|
|
self.tys.ins.globals[gid].data = mem;
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self.pool.pop_ci(&mut self.ci);
|
2024-11-08 03:25:34 -06:00
|
|
|
self.tys.ins.globals[gid].ty = ret;
|
2024-10-25 15:59:01 -05:00
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
self.ct.deactivate();
|
2024-11-08 03:57:58 -06:00
|
|
|
gid.into()
|
2024-10-25 15:59:01 -05:00
|
|
|
}
|
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
fn report(&self, file: Module, pos: Pos, msg: impl Display) -> ty::Id {
|
2024-10-26 07:06:08 -05:00
|
|
|
let mut buf = self.errors.borrow_mut();
|
2024-11-08 03:25:34 -06:00
|
|
|
write!(buf, "{}", self.files[file.index()].report(pos, msg)).unwrap();
|
2024-10-25 15:59:01 -05:00
|
|
|
ty::Id::NEVER
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_local_ty(&mut self, ident: Ident) -> Option<ty::Id> {
|
2024-11-11 02:07:36 -06:00
|
|
|
self.ci.scope.vars.iter().rfind(|v| (v.id == ident && v.value() == NEVER)).map(|v| v.ty)
|
2024-09-06 11:50:28 -05:00
|
|
|
}
|
2024-09-15 13:14:56 -05:00
|
|
|
}
|
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 {
|
2024-11-05 07:52:30 -06:00
|
|
|
super::{hbvm::HbvmBackend, CodegenCtx},
|
2024-11-08 03:25:34 -06:00
|
|
|
crate::ty,
|
2024-09-30 12:09:17 -05:00
|
|
|
alloc::{string::String, vec::Vec},
|
2024-10-27 05:32:34 -05:00
|
|
|
core::fmt::Write,
|
2024-09-30 12:09:17 -05:00
|
|
|
};
|
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);
|
2024-10-25 15:59:01 -05:00
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2024-11-09 06:54:08 -06:00
|
|
|
//log::set_max_level(log::LevelFilter::Trace);
|
2024-09-20 12:01:44 -05:00
|
|
|
|
2024-10-25 15:59:01 -05:00
|
|
|
let mut ctx = CodegenCtx::default();
|
|
|
|
let (ref files, embeds) = crate::test_parse_files(ident, input, &mut ctx.parser);
|
2024-11-05 07:52:30 -06:00
|
|
|
let mut backend = HbvmBackend::default();
|
|
|
|
let mut codegen = super::Codegen::new(&mut backend, files, &mut ctx);
|
2024-10-21 11:57:23 -05:00
|
|
|
codegen.push_embeds(embeds);
|
2024-09-02 17:27:50 -05:00
|
|
|
|
2024-11-08 03:25:34 -06:00
|
|
|
codegen.generate(ty::Module::MAIN);
|
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-11-05 07:52:30 -06:00
|
|
|
codegen.assemble(&mut out);
|
2024-09-04 16:46:32 -05:00
|
|
|
|
2024-11-05 07:52:30 -06:00
|
|
|
let err = codegen.disasm(output, &out);
|
2024-09-04 16:46:32 -05:00
|
|
|
if let Err(e) = err {
|
|
|
|
writeln!(output, "!!! asm is invalid: {e}").unwrap();
|
2024-11-07 01:52:41 -06:00
|
|
|
} else {
|
2024-11-10 02:17:43 -06:00
|
|
|
log::info!("================ running {ident} ==============");
|
|
|
|
log::trace!("{output}");
|
2024-11-07 01:52:41 -06:00
|
|
|
super::hbvm::test_run_vm(&out, output);
|
2024-09-04 16:46:32 -05:00
|
|
|
}
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
crate::run_tests! { generate:
|
2024-10-20 08:33:32 -05:00
|
|
|
// Tour Examples
|
|
|
|
main_fn;
|
2024-09-28 09:34:08 -05:00
|
|
|
arithmetic;
|
2024-10-29 07:36:12 -05:00
|
|
|
floating_point_arithmetic;
|
2024-09-28 09:34:08 -05:00
|
|
|
functions;
|
|
|
|
comments;
|
|
|
|
if_statements;
|
2024-10-20 08:33:32 -05:00
|
|
|
variables;
|
2024-09-28 09:34:08 -05:00
|
|
|
loops;
|
|
|
|
pointers;
|
2024-10-30 14:20:03 -05:00
|
|
|
nullable_types;
|
2024-09-28 14:56:39 -05:00
|
|
|
structs;
|
2024-10-20 08:33:32 -05:00
|
|
|
hex_octal_binary_literals;
|
2024-10-22 05:40:41 -05:00
|
|
|
struct_operators;
|
2024-10-19 12:37:02 -05:00
|
|
|
global_variables;
|
2024-11-08 03:57:58 -06:00
|
|
|
constants;
|
2024-10-21 11:57:23 -05:00
|
|
|
directives;
|
2024-10-20 05:22:28 -05:00
|
|
|
c_strings;
|
2024-10-20 14:00:56 -05:00
|
|
|
struct_patterns;
|
2024-10-19 03:17:36 -05:00
|
|
|
arrays;
|
2024-10-21 08:12:37 -05:00
|
|
|
inline;
|
2024-10-20 11:49:41 -05:00
|
|
|
idk;
|
2024-10-22 00:20:08 -05:00
|
|
|
generic_functions;
|
2024-11-03 03:15:03 -06:00
|
|
|
die;
|
2024-10-20 08:33:32 -05:00
|
|
|
|
|
|
|
// Incomplete Examples;
|
|
|
|
//comptime_pointers;
|
2024-10-22 03:17:16 -05:00
|
|
|
generic_types;
|
2024-10-20 08:33:32 -05:00
|
|
|
fb_driver;
|
|
|
|
|
|
|
|
// Purely Testing Examples;
|
2024-11-12 12:02:29 -06:00
|
|
|
wrong_dead_code_elimination;
|
2024-11-12 05:54:36 -06:00
|
|
|
memory_swap;
|
2024-11-11 14:55:18 -06:00
|
|
|
very_nested_loops;
|
2024-11-11 02:06:34 -06:00
|
|
|
generic_type_mishap;
|
2024-11-10 10:32:24 -06:00
|
|
|
storing_into_nullable_struct;
|
2024-11-09 08:14:03 -06:00
|
|
|
scheduling_block_did_dirty;
|
2024-11-09 03:28:53 -06:00
|
|
|
null_check_returning_small_global;
|
2024-11-08 13:40:18 -06:00
|
|
|
null_check_in_the_loop;
|
2024-11-08 04:51:10 -06:00
|
|
|
stack_provenance;
|
2024-11-08 03:57:58 -06:00
|
|
|
advanced_floating_point_arithmetic;
|
2024-11-07 09:39:15 -06:00
|
|
|
nullable_structure;
|
2024-11-04 12:18:37 -06:00
|
|
|
needless_unwrap;
|
2024-11-04 05:38:47 -06:00
|
|
|
inlining_issues;
|
2024-11-03 14:13:24 -06:00
|
|
|
null_check_test;
|
2024-11-03 01:59:42 -06:00
|
|
|
only_break_loop;
|
2024-10-26 17:02:59 -05:00
|
|
|
reading_idk;
|
2024-10-26 07:06:08 -05:00
|
|
|
nonexistent_ident_import;
|
2024-10-25 17:34:22 -05:00
|
|
|
big_array_crash;
|
2024-10-24 06:25:30 -05:00
|
|
|
returning_global_struct;
|
2024-10-24 06:58:58 -05:00
|
|
|
small_struct_bitcast;
|
2024-10-24 08:39:38 -05:00
|
|
|
small_struct_assignment;
|
2024-10-24 09:26:28 -05:00
|
|
|
intcast_store;
|
2024-10-24 12:57:36 -05:00
|
|
|
string_flip;
|
2024-10-25 08:45:00 -05:00
|
|
|
signed_to_unsigned_upcast;
|
2024-10-20 14:00:56 -05:00
|
|
|
wide_ret;
|
2024-10-20 05:22:28 -05:00
|
|
|
comptime_min_reg_leak;
|
2024-10-20 14:50:08 -05:00
|
|
|
different_types;
|
2024-10-21 12:57:55 -05:00
|
|
|
struct_return_from_module_function;
|
2024-10-20 08:33:32 -05:00
|
|
|
sort_something_viredly;
|
2024-10-27 13:13:25 -05:00
|
|
|
struct_in_register;
|
2024-10-20 08:33:32 -05:00
|
|
|
comptime_function_from_another_file;
|
2024-10-21 08:12:37 -05:00
|
|
|
inline_test;
|
2024-10-22 00:20:08 -05:00
|
|
|
inlined_generic_functions;
|
|
|
|
some_generic_code;
|
2024-10-21 12:57:55 -05:00
|
|
|
integer_inference_issues;
|
2024-10-20 08:33:32 -05:00
|
|
|
writing_into_string;
|
2024-10-21 12:57:55 -05:00
|
|
|
request_page;
|
|
|
|
tests_ptr_to_ptr_copy;
|
2024-10-20 08:33:32 -05:00
|
|
|
|
|
|
|
// Just Testing Optimizations;
|
|
|
|
const_folding_with_arg;
|
|
|
|
branch_assignments;
|
|
|
|
exhaustive_loop_testing;
|
2024-09-28 09:34:08 -05:00
|
|
|
pointer_opts;
|
2024-10-18 06:11:11 -05:00
|
|
|
conditional_stores;
|
2024-10-18 09:51:54 -05:00
|
|
|
loop_stores;
|
2024-10-26 13:29:31 -05:00
|
|
|
dead_code_in_loop;
|
2024-10-27 05:32:34 -05:00
|
|
|
infinite_loop_after_peephole;
|
2024-10-28 12:39:42 -05:00
|
|
|
aliasing_overoptimization;
|
2024-10-29 09:04:07 -05:00
|
|
|
global_aliasing_overptimization;
|
2024-10-29 11:03:00 -05:00
|
|
|
overwrite_aliasing_overoptimization;
|
2024-11-04 12:18:37 -06:00
|
|
|
more_if_opts;
|
2024-11-05 12:07:04 -06:00
|
|
|
optional_from_eca;
|
2024-11-06 09:17:03 -06:00
|
|
|
returning_optional_issues;
|
2024-09-02 17:07:20 -05:00
|
|
|
}
|
|
|
|
}
|