replacing magic zeroes with proper constants

This commit is contained in:
mlokr 2024-09-16 15:49:27 +02:00
parent a2e864360e
commit faf068885a
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
2 changed files with 53 additions and 52 deletions

View file

@ -18,7 +18,7 @@
extract_if, extract_if,
ptr_internals ptr_internals
)] )]
#![allow(internal_features, clippy::format_collect)] #![allow(stable_features, internal_features, clippy::format_collect)]
use { use {
self::{ self::{

View file

@ -41,6 +41,8 @@ impl Drop for Drom {
const VC_SIZE: usize = 16; const VC_SIZE: usize = 16;
const INLINE_ELEMS: usize = VC_SIZE / 2 - 1; const INLINE_ELEMS: usize = VC_SIZE / 2 - 1;
const VOID: Nid = 0;
const NEVER: Nid = 1;
union Vc { union Vc {
inline: InlineVc, inline: InlineVc,
@ -908,7 +910,7 @@ impl Nodes {
fn basic_blocks(&mut self) { fn basic_blocks(&mut self) {
let mut out = String::new(); let mut out = String::new();
self.visited.clear(self.values.len()); self.visited.clear(self.values.len());
self.basic_blocks_low(&mut out, 0).unwrap(); self.basic_blocks_low(&mut out, VOID).unwrap();
log::inf!("{out}"); log::inf!("{out}");
} }
@ -1013,7 +1015,7 @@ impl Nodes {
if !self[*lvalue].is_lazy_phi() { if !self[*lvalue].is_lazy_phi() {
self.unlock(*value); self.unlock(*value);
let inps = [loob.node, *lvalue, 0]; let inps = [loob.node, *lvalue, VOID];
self.unlock(inps[1]); self.unlock(inps[1]);
let ty = self[inps[1]].ty; let ty = self[inps[1]].ty;
let phi = self.new_node_nop(ty, Kind::Phi, inps); let phi = self.new_node_nop(ty, Kind::Phi, inps);
@ -1210,8 +1212,6 @@ struct ItemCtx {
task_base: usize, task_base: usize,
nodes: Nodes, nodes: Nodes,
start: Nid,
end: Nid,
ctrl: Nid, ctrl: Nid,
loop_depth: LoopDepth, loop_depth: LoopDepth,
@ -1288,7 +1288,7 @@ impl ItemCtx {
v, v,
Ok(Node { kind: Kind::Tuple { index: 1.. }, Ok(Node { kind: Kind::Tuple { index: 1.. },
color: 1.., color: 1..,
inputs, .. }) if inputs.first() == Some(&0) inputs, .. }) if inputs.first() == Some(&VOID)
) )
}) })
.count(); .count();
@ -1393,11 +1393,11 @@ impl Codegen {
let msg = "i know nothing about this name gal which is vired \ let msg = "i know nothing about this name gal which is vired \
because we parsed succesfully"; because we parsed succesfully";
match *expr { match *expr {
Expr::Comment { .. } => Some(0), Expr::Comment { .. } => Some(VOID),
Expr::Ident { pos, id, .. } => { Expr::Ident { pos, id, .. } => {
let Some(index) = self.ci.vars.iter().position(|v| v.id == id) else { let Some(index) = self.ci.vars.iter().position(|v| v.id == id) else {
self.report(pos, msg); self.report(pos, msg);
return Some(self.ci.end); return Some(NEVER);
}; };
self.ci.nodes.load_loop_value( self.ci.nodes.load_loop_value(
@ -1412,7 +1412,7 @@ impl Codegen {
let value = self.expr(right)?; let value = self.expr(right)?;
self.ci.nodes.lock(value); self.ci.nodes.lock(value);
self.ci.vars.push(Variable { id, value }); self.ci.vars.push(Variable { id, value });
Some(0) Some(VOID)
} }
Expr::BinOp { left: &Expr::Ident { id, pos, .. }, op: TokenKind::Assign, right } => { Expr::BinOp { left: &Expr::Ident { id, pos, .. }, op: TokenKind::Assign, right } => {
let value = self.expr(right)?; let value = self.expr(right)?;
@ -1420,12 +1420,12 @@ impl Codegen {
let Some(var) = self.ci.vars.iter_mut().find(|v| v.id == id) else { let Some(var) = self.ci.vars.iter_mut().find(|v| v.id == id) else {
self.report(pos, msg); self.report(pos, msg);
return Some(self.ci.end); return Some(NEVER);
}; };
let prev = std::mem::replace(&mut var.value, value); let prev = std::mem::replace(&mut var.value, value);
self.ci.nodes.unlock_remove(prev); self.ci.nodes.unlock_remove(prev);
Some(0) Some(VOID)
} }
Expr::BinOp { left, op, right } => { Expr::BinOp { left, op, right } => {
let lhs = self.expr_ctx(left, ctx)?; let lhs = self.expr_ctx(left, ctx)?;
@ -1440,7 +1440,7 @@ impl Codegen {
false, false,
"right operand", "right operand",
); );
let inps = [0, lhs, rhs]; let inps = [VOID, lhs, rhs];
Some(self.ci.nodes.new_node(ty::bin_ret(ty, op), Kind::BinOp { op }, inps)) Some(self.ci.nodes.new_node(ty::bin_ret(ty, op), Kind::BinOp { op }, inps))
} }
Expr::UnOp { pos, op, val } => { Expr::UnOp { pos, op, val } => {
@ -1451,8 +1451,7 @@ impl Codegen {
format_args!("cant negate '{}'", self.ty_display(self.tof(val))), format_args!("cant negate '{}'", self.ty_display(self.tof(val))),
); );
} }
// TODO: rename 0 to void Some(self.ci.nodes.new_node(self.tof(val), Kind::UnOp { op }, [VOID, val]))
Some(self.ci.nodes.new_node(self.tof(val), Kind::UnOp { op }, [0, val]))
} }
Expr::If { cond, then, else_, .. } => { Expr::If { cond, then, else_, .. } => {
let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?; let cond = self.expr_ctx(cond, Ctx::default().with_ty(ty::BOOL))?;
@ -1473,7 +1472,7 @@ impl Codegen {
if let Some(branch) = branch { if let Some(branch) = branch {
return self.expr(branch); return self.expr(branch);
} else { } else {
return Some(0); return Some(VOID);
} }
} }
@ -1504,14 +1503,14 @@ impl Codegen {
for then_var in then_scope { for then_var in then_scope {
self.ci.nodes.unlock_remove(then_var.value); self.ci.nodes.unlock_remove(then_var.value);
} }
return Some(0); return Some(VOID);
} else if rcntrl == Nid::MAX { } else if rcntrl == Nid::MAX {
for else_var in &self.ci.vars { for else_var in &self.ci.vars {
self.ci.nodes.unlock_remove(else_var.value); self.ci.nodes.unlock_remove(else_var.value);
} }
self.ci.vars = then_scope; self.ci.vars = then_scope;
self.ci.ctrl = lcntrl; self.ci.ctrl = lcntrl;
return Some(0); return Some(VOID);
} }
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Region, [lcntrl, rcntrl]); self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Region, [lcntrl, rcntrl]);
@ -1529,7 +1528,7 @@ impl Codegen {
self.ci.vars = else_scope; self.ci.vars = else_scope;
Some(0) Some(VOID)
} }
Expr::Loop { body, .. } => { Expr::Loop { body, .. } => {
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Loop, [self.ci.ctrl; 2]); self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Loop, [self.ci.ctrl; 2]);
@ -1541,9 +1540,9 @@ impl Codegen {
}); });
for var in &mut self.ci.vars { for var in &mut self.ci.vars {
var.value = 0; var.value = VOID;
} }
self.ci.nodes[0].lock_rc += self.ci.vars.len() as LockRc; self.ci.nodes[VOID].lock_rc += self.ci.vars.len() as LockRc;
self.expr(body); self.expr(body);
@ -1573,7 +1572,7 @@ impl Codegen {
self.ci.nodes[node].outputs.swap(idx, 0); self.ci.nodes[node].outputs.swap(idx, 0);
if bre == Nid::MAX { if bre == Nid::MAX {
self.ci.ctrl = self.ci.end; self.ci.ctrl = NEVER;
return None; return None;
} }
self.ci.ctrl = bre; self.ci.ctrl = bre;
@ -1587,7 +1586,7 @@ impl Codegen {
{ {
self.ci.nodes.unlock(loop_var.value); self.ci.nodes.unlock(loop_var.value);
if loop_var.value != 0 { if loop_var.value != VOID {
self.ci.nodes.unlock(scope_var.value); self.ci.nodes.unlock(scope_var.value);
if loop_var.value != scope_var.value { if loop_var.value != scope_var.value {
scope_var.value = scope_var.value =
@ -1596,7 +1595,7 @@ impl Codegen {
} else { } else {
let phi = &self.ci.nodes[scope_var.value]; let phi = &self.ci.nodes[scope_var.value];
debug_assert_eq!(phi.kind, Kind::Phi); debug_assert_eq!(phi.kind, Kind::Phi);
debug_assert_eq!(phi.inputs[2], 0); debug_assert_eq!(phi.inputs[2], VOID);
let prev = phi.inputs[1]; let prev = phi.inputs[1];
self.ci.nodes.replace(scope_var.value, prev); self.ci.nodes.replace(scope_var.value, prev);
scope_var.value = prev; scope_var.value = prev;
@ -1604,7 +1603,7 @@ impl Codegen {
} }
} }
if dest_var.value == 0 { if dest_var.value == VOID {
self.ci.nodes.unlock(dest_var.value); self.ci.nodes.unlock(dest_var.value);
dest_var.value = scope_var.value; dest_var.value = scope_var.value;
self.ci.nodes.lock(dest_var.value); self.ci.nodes.lock(dest_var.value);
@ -1615,7 +1614,7 @@ impl Codegen {
self.ci.nodes.unlock(self.ci.ctrl); self.ci.nodes.unlock(self.ci.ctrl);
Some(0) Some(VOID)
} }
Expr::Break { pos } => self.jump_to(pos, 1), Expr::Break { pos } => self.jump_to(pos, 1),
Expr::Continue { pos } => self.jump_to(pos, 0), Expr::Continue { pos } => self.jump_to(pos, 0),
@ -1630,7 +1629,7 @@ impl Codegen {
self.ty_display(func.compress()) self.ty_display(func.compress())
), ),
); );
return Some(self.ci.end); return Some(NEVER);
}; };
self.make_func_reachable(func); self.make_func_reachable(func);
@ -1679,7 +1678,7 @@ impl Codegen {
let value = if let Some(val) = val { let value = if let Some(val) = val {
self.expr_ctx(val, Ctx { ty: self.ci.ret })? self.expr_ctx(val, Ctx { ty: self.ci.ret })?
} else { } else {
0 VOID
}; };
let inps = [self.ci.ctrl, value]; let inps = [self.ci.ctrl, value];
@ -1688,8 +1687,8 @@ impl Codegen {
self.report_log_to(pos, "returning here", out); self.report_log_to(pos, "returning here", out);
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Return, inps); self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Return, inps);
self.ci.nodes[self.ci.end].inputs.push(self.ci.ctrl); self.ci.nodes[NEVER].inputs.push(self.ci.ctrl);
self.ci.nodes[self.ci.ctrl].outputs.push(self.ci.end); self.ci.nodes[self.ci.ctrl].outputs.push(NEVER);
let expected = *self.ci.ret.get_or_insert(self.tof(value)); let expected = *self.ci.ret.get_or_insert(self.tof(value));
_ = self.assert_ty(pos, self.tof(value), expected, true, "return value"); _ = self.assert_ty(pos, self.tof(value), expected, true, "return value");
@ -1699,7 +1698,7 @@ impl Codegen {
Expr::Block { stmts, .. } => { Expr::Block { stmts, .. } => {
let base = self.ci.vars.len(); let base = self.ci.vars.len();
let mut ret = Some(0); let mut ret = Some(VOID);
for stmt in stmts { for stmt in stmts {
ret = ret.and(self.expr(stmt)); ret = ret.and(self.expr(stmt));
if let Some(id) = ret { if let Some(id) = ret {
@ -1726,11 +1725,11 @@ impl Codegen {
Expr::Number { value, .. } => Some(self.ci.nodes.new_node( Expr::Number { value, .. } => Some(self.ci.nodes.new_node(
ctx.ty.filter(|ty| ty.is_integer() || ty.is_pointer()).unwrap_or(ty::INT.into()), ctx.ty.filter(|ty| ty.is_integer() || ty.is_pointer()).unwrap_or(ty::INT.into()),
Kind::CInt { value }, Kind::CInt { value },
[0], [VOID],
)), )),
ref e => { ref e => {
self.report_unhandled_ast(e, "bruh"); self.report_unhandled_ast(e, "bruh");
Some(self.ci.end) Some(NEVER)
} }
} }
} }
@ -1765,7 +1764,7 @@ impl Codegen {
loob.ctrl[id] = reg; loob.ctrl[id] = reg;
} }
self.ci.ctrl = self.ci.end; self.ci.ctrl = NEVER;
None None
} }
@ -1832,9 +1831,11 @@ impl Codegen {
}; };
let prev_ci = std::mem::replace(&mut self.ci, repl); let prev_ci = std::mem::replace(&mut self.ci, repl);
self.ci.start = self.ci.nodes.new_node(ty::VOID, Kind::Start, []); let start = self.ci.nodes.new_node(ty::VOID, Kind::Start, []);
self.ci.end = self.ci.nodes.new_node(ty::NEVER, Kind::End, []); debug_assert_eq!(start, VOID);
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Tuple { index: 0 }, [self.ci.start]); let end = self.ci.nodes.new_node(ty::NEVER, Kind::End, []);
debug_assert_eq!(end, NEVER);
self.ci.ctrl = self.ci.nodes.new_node(ty::VOID, Kind::Tuple { index: 0 }, [VOID]);
let Expr::BinOp { let Expr::BinOp {
left: Expr::Ident { .. }, left: Expr::Ident { .. },
@ -1848,7 +1849,7 @@ impl Codegen {
let mut sig_args = sig.args.range(); let mut sig_args = sig.args.range();
for (arg, index) in args.iter().zip(1u32..) { for (arg, index) in args.iter().zip(1u32..) {
let ty = self.tys.args[sig_args.next().unwrap()]; let ty = self.tys.args[sig_args.next().unwrap()];
let value = self.ci.nodes.new_node(ty, Kind::Tuple { index }, [self.ci.start]); let value = self.ci.nodes.new_node(ty, Kind::Tuple { index }, [VOID]);
self.ci.nodes.lock(value); self.ci.nodes.lock(value);
let sym = parser::find_symbol(&ast.symbols, arg.id); let sym = parser::find_symbol(&ast.symbols, arg.id);
assert!(sym.flags & idfl::COMPTIME == 0, "TODO"); assert!(sym.flags & idfl::COMPTIME == 0, "TODO");
@ -1883,7 +1884,7 @@ impl Codegen {
self.ci.nodes.basic_blocks(); self.ci.nodes.basic_blocks();
self.ci.nodes.visited.clear(self.ci.nodes.values.len()); self.ci.nodes.visited.clear(self.ci.nodes.values.len());
self.color_node(self.ci.end); self.color_node(NEVER);
let call_count = self.ci.call_count; let call_count = self.ci.call_count;
let mut parama = self.tys.parama(sig.ret); let mut parama = self.tys.parama(sig.ret);
@ -1924,7 +1925,7 @@ impl Codegen {
self.ci.vars = orig_vars; self.ci.vars = orig_vars;
self.ci.call_count = call_count; self.ci.call_count = call_count;
self.ci.nodes.visited.clear(self.ci.nodes.values.len()); self.ci.nodes.visited.clear(self.ci.nodes.values.len());
self.emit_node(self.ci.start, self.ci.start); self.emit_node(VOID, VOID);
self.ci.vars.clear(); self.ci.vars.clear();
if let Some(last_ret) = self.ci.ret_relocs.last() if let Some(last_ret) = self.ci.ret_relocs.last()
@ -2003,7 +2004,7 @@ impl Codegen {
None None
} }
Kind::Return => { Kind::Return => {
if node.inputs[1] != 0 { if node.inputs[1] != VOID {
let col = self.ci.set_next_color(node.inputs[1]); let col = self.ci.set_next_color(node.inputs[1]);
if check_no_calls(node.inputs[1], &mut self.ci.nodes) { if check_no_calls(node.inputs[1], &mut self.ci.nodes) {
self.ci.colors[col as usize - 1].loc = Loc { reg: 1 }; self.ci.colors[col as usize - 1].loc = Loc { reg: 1 };
@ -2174,7 +2175,7 @@ impl Codegen {
let node = self.ci.nodes[ctrl].clone(); let node = self.ci.nodes[ctrl].clone();
match node.kind { match node.kind {
Kind::Start => self.emit_node(node.outputs[0], self.ci.start), Kind::Start => self.emit_node(node.outputs[0], VOID),
Kind::End => None, Kind::End => None,
Kind::If => { Kind::If => {
let &[_, cond] = node.inputs.as_slice() else { unreachable!() }; let &[_, cond] = node.inputs.as_slice() else { unreachable!() };
@ -2210,7 +2211,7 @@ impl Codegen {
None None
} }
Kind::Return => { Kind::Return => {
if node.inputs[1] != 0 { if node.inputs[1] != VOID {
let loc = Loc { reg: 1 }; let loc = Loc { reg: 1 };
if node_loc!(self, node.inputs[1]) != loc { if node_loc!(self, node.inputs[1]) != loc {
self.ci.emit(instrs::cp(loc.reg, node_loc!(self, node.inputs[1]).reg)); self.ci.emit(instrs::cp(loc.reg, node_loc!(self, node.inputs[1]).reg));
@ -2537,10 +2538,10 @@ impl Codegen {
fn gcm(&mut self) { fn gcm(&mut self) {
self.ci.nodes.visited.clear(self.ci.nodes.values.len()); self.ci.nodes.visited.clear(self.ci.nodes.values.len());
push_up(&mut self.ci.nodes, self.ci.end); push_up(&mut self.ci.nodes, NEVER);
// TODO: handle infinte loops // TODO: handle infinte loops
self.ci.nodes.visited.clear(self.ci.nodes.values.len()); self.ci.nodes.visited.clear(self.ci.nodes.values.len());
push_down(&mut self.ci.nodes, self.ci.start); push_down(&mut self.ci.nodes, VOID);
} }
} }
@ -2571,7 +2572,7 @@ fn loop_depth(target: Nid, nodes: &mut Nodes) -> LoopDepth {
} else { } else {
idom(nodes, cursor) idom(nodes, cursor)
}; };
debug_assert_ne!(next, 0); debug_assert_ne!(next, VOID);
if let Kind::Tuple { index } = nodes[cursor].kind if let Kind::Tuple { index } = nodes[cursor].kind
&& nodes[next].kind == Kind::If && nodes[next].kind == Kind::If
{ {
@ -2610,7 +2611,7 @@ fn better(nodes: &mut Nodes, is: Nid, then: Nid) -> bool {
} }
fn idepth(nodes: &mut Nodes, target: Nid) -> IDomDepth { fn idepth(nodes: &mut Nodes, target: Nid) -> IDomDepth {
if target == 0 { if target == VOID {
return 0; return 0;
} }
if nodes[target].depth == 0 { if nodes[target].depth == 0 {
@ -2644,7 +2645,7 @@ fn push_up(nodes: &mut Nodes, node: Nid) {
push_up(nodes, i); push_up(nodes, i);
} }
} else { } else {
let mut max = 0; let mut max = VOID;
for i in 0..nodes[node].inputs.len() { for i in 0..nodes[node].inputs.len() {
let i = nodes[node].inputs[i]; let i = nodes[node].inputs[i];
let is_call = matches!(nodes[i].kind, Kind::Call { .. }); let is_call = matches!(nodes[i].kind, Kind::Call { .. });
@ -2662,7 +2663,7 @@ fn push_up(nodes: &mut Nodes, node: Nid) {
nodes.check_dominance(node, max, false); nodes.check_dominance(node, max, false);
} }
if max == 0 { if max == VOID {
return; return;
} }
@ -2745,7 +2746,7 @@ fn use_block(target: Nid, from: Nid, nodes: &mut Nodes) -> Nid {
fn idom(nodes: &mut Nodes, target: Nid) -> Nid { fn idom(nodes: &mut Nodes, target: Nid) -> Nid {
match nodes[target].kind { match nodes[target].kind {
Kind::Start => 0, Kind::Start => VOID,
Kind::End => unreachable!(), Kind::End => unreachable!(),
Kind::Loop Kind::Loop
| Kind::CInt { .. } | Kind::CInt { .. }
@ -2785,7 +2786,7 @@ fn scan_idom(
return None; return None;
} }
Some(match nodes[target].kind { Some(match nodes[target].kind {
Kind::Start => 0, Kind::Start => VOID,
Kind::End => unreachable!(), Kind::End => unreachable!(),
Kind::Loop Kind::Loop
| Kind::CInt { .. } | Kind::CInt { .. }
@ -2823,7 +2824,7 @@ fn walk_use_doms(
out = use_block(target, out, nodes); out = use_block(target, out, nodes);
while out != dom { while out != dom {
debug_assert!(idepth(nodes, out) > idepth(nodes, dom),); debug_assert!(idepth(nodes, out) > idepth(nodes, dom),);
debug_assert_ne!(out, 0); debug_assert_ne!(out, VOID);
out = match scan_idom(nodes, out, &mut all) { out = match scan_idom(nodes, out, &mut all) {
Some(next) => next, Some(next) => next,
None => return false, None => return false,
@ -2867,7 +2868,7 @@ fn is_last_branch_use(target: Nid, us: Nid, nodes: &mut Nodes) -> bool {
if idepth(nodes, odom) < idepth(nodes, us) { if idepth(nodes, odom) < idepth(nodes, us) {
continue 'o; continue 'o;
} }
debug_assert_ne!(odom, 0); debug_assert_ne!(odom, VOID);
} }
return false; return false;