From f77bc5246538ddfb24c0d9495dc6905734381acb Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Wed, 13 Nov 2024 15:25:27 +0100 Subject: [PATCH] fixing unchanged parsed file --- lang/src/lib.rs | 1 + lang/src/son.rs | 15 +- lang/src/son/hbvm.rs | 307 +++++++++++++++++++++++++- lang/src/son/hbvm/my_regalloc.rs | 331 +++++++--------------------- lang/src/son/hbvm/their_regalloc.rs | 320 ++------------------------- 5 files changed, 404 insertions(+), 570 deletions(-) diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 42fae22..f0d61c1 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -892,6 +892,7 @@ impl Array { } } +#[derive(Clone, Copy)] enum PLoc { Reg(u8, u16), WideReg(u8, u16), diff --git a/lang/src/son.rs b/lang/src/son.rs index 33cbf7c..1cc2346 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -1752,6 +1752,7 @@ impl Nodes { match self[n].kind { Kind::Return => self[n].inputs[1] == nid, _ if self.is_cfg(n) && !matches!(self[n].kind, Kind::Call { .. } | Kind::If) => false, + Kind::Join => false, 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), @@ -3794,7 +3795,6 @@ impl<'a> Codegen<'a> { 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()]; @@ -3907,6 +3907,8 @@ impl<'a> Codegen<'a> { Some(v) } else { + self.make_func_reachable(fu); + let mut inps = Vc::from([NEVER]); let mut tys = sig.args.args(); let mut cargs = cargs.iter(); @@ -4049,20 +4051,23 @@ impl<'a> Codegen<'a> { } fn compute_signature(&mut self, func: &mut ty::Func, pos: Pos, args: &[Expr]) -> Option { - let fuc = &self.tys.ins.funcs[*func]; - let fast = self.files[fuc.file.index()].clone(); - let &Expr::Closure { args: cargs, ret, .. } = fuc.expr.get(&fast) else { + let Func { file, expr, sig, .. } = self.tys.ins.funcs[*func]; + let fast = self.files[file.index()].clone(); + let &Expr::Closure { args: cargs, ret, .. } = expr.get(&fast) else { unreachable!(); }; - Some(if let Some(sig) = fuc.sig { + Some(if let Some(sig) = 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 prev_file = mem::replace(&mut self.ci.file, file); let ty = self.ty(&carg.ty); + self.ci.file = prev_file; + self.tys.tmp.args.push(ty); let sym = parser::find_symbol(&fast.symbols, carg.id); let ty = if sym.flags & idfl::COMPTIME == 0 { diff --git a/lang/src/son/hbvm.rs b/lang/src/son/hbvm.rs index 8f1a243..6b30995 100644 --- a/lang/src/son/hbvm.rs +++ b/lang/src/son/hbvm.rs @@ -1,12 +1,13 @@ use { - super::{AssemblySpec, Backend, Nid, Node, Nodes}, + super::{AssemblySpec, Backend, Nid, Node, Nodes, VOID}, crate::{ lexer::TokenKind, - parser, reg, + parser, + reg::{self, Reg}, son::{debug_assert_matches, write_reloc, Kind, MEM}, - ty::{self, Loc, Module}, + ty::{self, Arg, Loc, Module}, utils::{BitSet, Ent, EntVec, Vc}, - Offset, Reloc, Size, TypedReloc, Types, + Offset, PLoc, Reloc, Sig, Size, TypedReloc, Types, }, alloc::{boxed::Box, collections::BTreeMap, string::String, vec::Vec}, core::mem, @@ -271,10 +272,8 @@ impl Backend for HbvmBackend { self.ret_relocs.pop(); } - // FIXME: maybe do this incrementally for (nd, rel) in self.jump_relocs.drain(..) { let offset = self.offsets[nd as usize]; - //debug_assert!(offset < self.code.len() as u32 - 1); rel.apply_jump(&mut self.code, offset, 0); } @@ -426,6 +425,302 @@ impl Nodes { } } +struct InstrCtx<'a> { + nid: Nid, + sig: Sig, + is_last_block: bool, + is_next_block: bool, + retl: Option, + allocs: &'a [u8], + nodes: &'a Nodes, + tys: &'a Types, + files: &'a [parser::Ast], +} + +impl HbvmBackend { + fn extend(&mut self, base: ty::Id, dest: ty::Id, reg: Reg, tys: &Types, files: &[parser::Ast]) { + let (bsize, dsize) = (tys.size_of(base), tys.size_of(dest)); + debug_assert!(bsize <= 8, "{}", ty::Display::new(tys, files, base)); + debug_assert!(dsize <= 8, "{}", ty::Display::new(tys, files, dest)); + if bsize == dsize { + return Default::default(); + } + self.emit(match (base.is_signed(), dest.is_signed()) { + (true, true) => { + let op = [instrs::sxt8, instrs::sxt16, instrs::sxt32][bsize.ilog2() as usize]; + op(reg, reg) + } + _ => { + let mask = (1u64 << (bsize * 8)) - 1; + instrs::andi(reg, reg, mask) + } + }); + } + + fn emit_instr( + &mut self, + InstrCtx { + nid, + sig, + is_last_block, + is_next_block, + allocs, + nodes, + tys, + files, + retl, + }: InstrCtx, + ) { + let node = &nodes[nid]; + + match node.kind { + Kind::If => { + let &[_, cnd] = node.inputs.as_slice() else { unreachable!() }; + if let Kind::BinOp { op } = nodes[cnd].kind + && let Some((op, swapped)) = op.cond_op(nodes[nodes[cnd].inputs[1]].ty) + { + let &[lhs, rhs] = allocs else { unreachable!() }; + let &[_, lh, rh] = nodes[cnd].inputs.as_slice() else { unreachable!() }; + + self.extend(nodes[lh].ty, nodes[lh].ty.extend(), lhs, tys, files); + self.extend(nodes[rh].ty, nodes[rh].ty.extend(), rhs, tys, files); + + let rel = Reloc::new(self.code.len(), 3, 2); + self.jump_relocs.push((node.outputs[!swapped as usize], rel)); + self.emit(op(lhs, rhs, 0)); + } else { + debug_assert_eq!(nodes[node.outputs[0]].kind, Kind::Then); + self.extend(nodes[cnd].ty, nodes[cnd].ty.extend(), allocs[0], tys, files); + let rel = Reloc::new(self.code.len(), 3, 2); + self.jump_relocs.push((node.outputs[0], rel)); + self.emit(instrs::jne(allocs[0], reg::ZERO, 0)); + } + } + Kind::Loop | Kind::Region => { + if !is_next_block { + let rel = Reloc::new(self.code.len(), 1, 4); + self.jump_relocs.push((nid, rel)); + self.emit(instrs::jmp(0)); + } + } + Kind::Return => { + match retl { + Some(PLoc::Reg(r, size)) if sig.ret.loc(tys) == Loc::Stack => { + self.emit(instrs::ld(r, allocs[0], 0, size)) + } + None | Some(PLoc::Reg(..)) => {} + Some(PLoc::WideReg(r, size)) => self.emit(instrs::ld(r, allocs[0], 0, size)), + Some(PLoc::Ref(_, size)) => { + let [src, dst] = [allocs[0], allocs[1]]; + if let Ok(size) = u16::try_from(size) { + self.emit(instrs::bmc(src, dst, size)); + } else { + for _ in 0..size / u16::MAX as u32 { + self.emit(instrs::bmc(src, dst, u16::MAX)); + self.emit(instrs::addi64(src, src, u16::MAX as _)); + self.emit(instrs::addi64(dst, dst, u16::MAX as _)); + } + self.emit(instrs::bmc(src, dst, size as u16)); + self.emit(instrs::addi64(src, src, size.wrapping_neg() as _)); + self.emit(instrs::addi64(dst, dst, size.wrapping_neg() as _)); + } + } + } + + if !is_last_block { + let rel = Reloc::new(self.code.len(), 1, 4); + self.ret_relocs.push(rel); + self.emit(instrs::jmp(0)); + } + } + Kind::Die => { + self.emit(instrs::un()); + } + Kind::CInt { value: 0 } => self.emit(instrs::cp(allocs[0], reg::ZERO)), + Kind::CInt { value } if node.ty == ty::Id::F32 => { + self.emit(instrs::li32(allocs[0], (f64::from_bits(value as _) as f32).to_bits())); + } + Kind::CInt { value } => self.emit(match tys.size_of(node.ty) { + 1 => instrs::li8(allocs[0], value as _), + 2 => instrs::li16(allocs[0], value as _), + 4 => instrs::li32(allocs[0], value as _), + _ => instrs::li64(allocs[0], value as _), + }), + Kind::UnOp { op } => { + let op = op + .unop( + node.ty, + tys.inner_of(nodes[node.inputs[1]].ty).unwrap_or(nodes[node.inputs[1]].ty), + ) + .unwrap_or_else(|| { + panic!( + "TODO: unary operator not supported: {op} {} {}", + ty::Display::new(tys, files, node.ty), + ty::Display::new( + tys, + files, + tys.inner_of(nodes[node.inputs[1]].ty) + .unwrap_or(nodes[node.inputs[1]].ty) + ) + ) + }); + let &[dst, oper] = allocs else { unreachable!() }; + self.emit(op(dst, oper)); + } + Kind::BinOp { .. } if node.lock_rc != 0 => {} + Kind::BinOp { op } => { + let &[.., lh, rh] = node.inputs.as_slice() else { unreachable!() }; + + if let Kind::CInt { value } = nodes[rh].kind + && nodes[rh].lock_rc != 0 + && let Some(op) = op.imm_binop(node.ty) + { + let &[dst, lhs] = allocs else { unreachable!() }; + self.emit(op(dst, lhs, value as _)); + } else if let Some(op) = op.binop(node.ty).or(op.float_cmp(nodes[lh].ty)) { + let &[dst, lhs, rhs] = allocs else { unreachable!() }; + self.emit(op(dst, lhs, rhs)); + } else if let Some(against) = op.cmp_against() { + let op_ty = nodes[rh].ty; + let &[dst, lhs, rhs] = allocs else { unreachable!() }; + + if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { + let opop = match op { + TokenKind::Le => TokenKind::Gt, + TokenKind::Ge => TokenKind::Lt, + _ => unreachable!(), + }; + let op_fn = opop.float_cmp(op_ty).unwrap(); + self.emit(op_fn(dst, lhs, rhs)); + self.emit(instrs::not(dst, dst)); + } else { + let op_fn = if op_ty.is_signed() { instrs::cmps } else { instrs::cmpu }; + self.emit(op_fn(dst, lhs, rhs)); + self.emit(instrs::cmpui(dst, dst, against)); + if matches!(op, TokenKind::Eq | TokenKind::Lt | TokenKind::Gt) { + self.emit(instrs::not(dst, dst)); + } + } + } else { + todo!("unhandled operator: {op}"); + } + } + Kind::Call { args, func } => { + let (ret, mut parama) = tys.parama(node.ty); + let has_ret = ret.is_some() as usize; + let mut args = args.args(); + let mut allocs = allocs[has_ret..].iter(); + while let Some(arg) = args.next(tys) { + let Arg::Value(ty) = arg else { continue }; + let Some(loc) = parama.next(ty, tys) else { continue }; + + let &arg = allocs.next().unwrap(); + let (rg, size) = match loc { + PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), + PLoc::WideReg(rg, size) => (rg, size), + PLoc::Ref(..) | PLoc::Reg(..) => continue, + }; + if size > 8 { + allocs.next().unwrap(); + } + self.emit(instrs::ld(rg, arg, 0, size)); + } + + debug_assert!(!matches!(ret, Some(PLoc::Ref(..))) || allocs.next().is_some()); + + if func == ty::Func::ECA { + self.emit(instrs::eca()); + } else { + self.relocs.push(TypedReloc { + target: ty::Kind::Func(func).compress(), + reloc: Reloc::new(self.code.len(), 3, 4), + }); + self.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0)); + } + + if let Some(PLoc::WideReg(r, size)) = ret { + debug_assert_eq!(nodes[*node.inputs.last().unwrap()].kind, Kind::Stck); + let stck = self.offsets[*node.inputs.last().unwrap() as usize]; + self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); + } + if let Some(PLoc::Reg(r, size)) = ret + && node.ty.loc(tys) == Loc::Stack + { + debug_assert_eq!(nodes[*node.inputs.last().unwrap()].kind, Kind::Stck); + let stck = self.offsets[*node.inputs.last().unwrap() as usize]; + self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); + } + } + Kind::Global { global } => { + let reloc = Reloc::new(self.code.len(), 3, 4); + self.relocs.push(TypedReloc { target: ty::Kind::Global(global).compress(), reloc }); + self.emit(instrs::lra(allocs[0], 0, 0)); + } + Kind::Stck => { + let base = reg::STACK_PTR; + let offset = self.offsets[nid as usize]; + self.emit(instrs::addi64(allocs[0], base, offset as _)); + } + Kind::Load => { + let mut region = node.inputs[1]; + let mut offset = 0; + if nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && let Kind::CInt { value } = nodes[nodes[region].inputs[2]].kind + { + region = nodes[region].inputs[1]; + offset = value as Offset; + } + let size = tys.size_of(node.ty); + if node.ty.loc(tys) != Loc::Stack { + let (base, offset) = match nodes[region].kind { + Kind::Stck => (reg::STACK_PTR, self.offsets[region as usize] + offset), + _ => (allocs[1], offset), + }; + self.emit(instrs::ld(allocs[0], base, offset as _, size as _)); + } + } + Kind::Stre if node.inputs[1] == VOID => {} + Kind::Stre => { + let mut region = node.inputs[2]; + let mut offset = 0; + let size = u16::try_from(tys.size_of(node.ty)).expect("TODO"); + if nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) + && let Kind::CInt { value } = nodes[nodes[region].inputs[2]].kind + && node.ty.loc(tys) == Loc::Reg + { + region = nodes[region].inputs[1]; + offset = value as Offset; + } + let (base, offset, src) = match nodes[region].kind { + Kind::Stck if node.ty.loc(tys) == Loc::Reg => { + (reg::STACK_PTR, self.offsets[region as usize] + offset, allocs[0]) + } + _ => ((allocs[0]), offset, allocs[1]), + }; + + match node.ty.loc(tys) { + Loc::Reg => self.emit(instrs::st(src, base, offset as _, size)), + Loc::Stack => { + debug_assert_eq!(offset, 0); + self.emit(instrs::bmc(src, base, size)) + } + } + } + e @ (Kind::Start + | Kind::Assert { .. } + | Kind::Entry + | Kind::Mem + | Kind::End + | Kind::Loops + | Kind::Then + | Kind::Else + | Kind::Phi + | Kind::Arg + | Kind::Join) => unreachable!("{e:?}"), + } + } +} + impl Node { fn uses_direct_offset_of(&self, nid: Nid, tys: &Types) -> bool { ((self.kind == Kind::Stre && self.inputs[2] == nid) diff --git a/lang/src/son/hbvm/my_regalloc.rs b/lang/src/son/hbvm/my_regalloc.rs index fe8d87c..6c84121 100644 --- a/lang/src/son/hbvm/my_regalloc.rs +++ b/lang/src/son/hbvm/my_regalloc.rs @@ -7,7 +7,7 @@ use { son::{debug_assert_matches, Kind, ARG_START, MEM, VOID}, ty::{self, Arg, Loc}, utils::BitSet, - Offset, PLoc, Reloc, Sig, TypedReloc, Types, + PLoc, Sig, Types, }, alloc::{borrow::ToOwned, vec::Vec}, core::{mem, ops::Range}, @@ -68,13 +68,6 @@ impl HbvmBackend { res.node_to_reg[allc as usize] }; - //for (id, node) in fuc.nodes.iter() { - // if node.kind == Kind::Phi { - // debug_assert_eq!(atr(node.inputs[1]), atr(node.inputs[2])); - // debug_assert_eq!(atr(id), atr(node.inputs[2])); - // } - //} - let (retl, mut parama) = tys.parama(sig.ret); let mut typs = sig.args.args(); let mut args = fuc.nodes[VOID].outputs[ARG_START..].iter(); @@ -97,6 +90,7 @@ impl HbvmBackend { self.emit(instrs::cp(atr(arg), rg)); } + let mut alloc_buf = vec![]; for (i, block) in fuc.func.blocks.iter().enumerate() { self.offsets[block.entry as usize] = self.code.len() as _; for &nid in &fuc.func.instrs[block.range.clone()] { @@ -105,64 +99,21 @@ impl HbvmBackend { } let node = &fuc.nodes[nid]; - let extend = |base: ty::Id, dest: ty::Id, from: Nid, to: Nid| { - let (bsize, dsize) = (tys.size_of(base), tys.size_of(dest)); - debug_assert!(bsize <= 8, "{}", ty::Display::new(tys, files, base)); - debug_assert!(dsize <= 8, "{}", ty::Display::new(tys, files, dest)); - if bsize == dsize { - return Default::default(); - } - match (base.is_signed(), dest.is_signed()) { - (true, true) => { - let op = [instrs::sxt8, instrs::sxt16, instrs::sxt32] - [bsize.ilog2() as usize]; - op(atr(to), atr(from)) - } - _ => { - let mask = (1u64 << (bsize * 8)) - 1; - instrs::andi(atr(to), atr(from), mask) - } - } - }; + alloc_buf.clear(); + let mut is_next_block = false; match node.kind { Kind::If => { let &[_, cnd] = node.inputs.as_slice() else { unreachable!() }; if let Kind::BinOp { op } = fuc.nodes[cnd].kind - && let Some((op, swapped)) = - op.cond_op(fuc.nodes[fuc.nodes[cnd].inputs[1]].ty) + && op.cond_op(fuc.nodes[fuc.nodes[cnd].inputs[1]].ty).is_some() { - let &[_, lhs, rhs] = fuc.nodes[cnd].inputs.as_slice() else { + let &[_, lh, rh] = fuc.nodes[cnd].inputs.as_slice() else { unreachable!() }; - - self.emit(extend( - fuc.nodes[lhs].ty, - fuc.nodes[lhs].ty.extend(), - lhs, - lhs, - )); - self.emit(extend( - fuc.nodes[rhs].ty, - fuc.nodes[rhs].ty.extend(), - rhs, - rhs, - )); - - let rel = Reloc::new(self.code.len(), 3, 2); - self.jump_relocs.push((node.outputs[!swapped as usize], rel)); - self.emit(op(atr(lhs), atr(rhs), 0)); + alloc_buf.extend([atr(lh), atr(rh)]); } else { - self.emit(extend( - fuc.nodes[cnd].ty, - fuc.nodes[cnd].ty.extend(), - cnd, - cnd, - )); - let rel = Reloc::new(self.code.len(), 3, 2); - debug_assert_eq!(fuc.nodes[node.outputs[0]].kind, Kind::Then); - self.jump_relocs.push((node.outputs[0], rel)); - self.emit(instrs::jne(atr(cnd), reg::ZERO, 0)); + alloc_buf.push(atr(cnd)); } } Kind::Loop | Kind::Region => { @@ -215,122 +166,40 @@ impl HbvmBackend { self.emit(instrs::swa(dst, src)); } } - - if fuc.block_of(nid) as usize != i + 1 { - let rel = Reloc::new(self.code.len(), 1, 4); - self.jump_relocs.push((nid, rel)); - self.emit(instrs::jmp(0)); - } + is_next_block = fuc.block_of(nid) as usize == i + 1; } Kind::Return => { let &[_, ret, ..] = node.inputs.as_slice() else { unreachable!() }; + alloc_buf.push(atr(ret)); match retl { - None => {} Some(PLoc::Reg(r, _)) if sig.ret.loc(tys) == Loc::Reg => { self.emit(instrs::cp(r, atr(ret))); } - Some(PLoc::Reg(r, size)) | Some(PLoc::WideReg(r, size)) => { - self.emit(instrs::ld(r, atr(ret), 0, size)) - } - Some(PLoc::Ref(_, size)) => { - let [src, dst] = [atr(ret), atr(MEM)]; - if let Ok(size) = u16::try_from(size) { - self.emit(instrs::bmc(src, dst, size)); - } else { - for _ in 0..size / u16::MAX as u32 { - self.emit(instrs::bmc(src, dst, u16::MAX)); - self.emit(instrs::addi64(src, src, u16::MAX as _)); - self.emit(instrs::addi64(dst, dst, u16::MAX as _)); - } - self.emit(instrs::bmc(src, dst, size as u16)); - self.emit(instrs::addi64(src, src, size.wrapping_neg() as _)); - self.emit(instrs::addi64(dst, dst, size.wrapping_neg() as _)); - } - } - } - - if i != fuc.func.blocks.len() - 1 { - let rel = Reloc::new(self.code.len(), 1, 4); - self.ret_relocs.push(rel); - self.emit(instrs::jmp(0)); + Some(PLoc::Ref(..)) => alloc_buf.push(atr(MEM)), + _ => {} } } Kind::Die => self.emit(instrs::un()), - Kind::CInt { value } if node.ty.is_float() => { - self.emit(match node.ty { - ty::Id::F32 => instrs::li32( - atr(nid), - (f64::from_bits(value as _) as f32).to_bits(), - ), - ty::Id::F64 => instrs::li64(atr(nid), value as _), - _ => unreachable!(), - }); - } - Kind::CInt { value } => self.emit(match tys.size_of(node.ty) { - 1 => instrs::li8(atr(nid), value as _), - 2 => instrs::li16(atr(nid), value as _), - 4 => instrs::li32(atr(nid), value as _), - _ => instrs::li64(atr(nid), value as _), - }), - Kind::UnOp { op } => { - let op = op - .unop(node.ty, fuc.nodes[node.inputs[1]].ty) - .expect("TODO: unary operator not supported"); - self.emit(op(atr(nid), atr(node.inputs[1]))); - } + Kind::CInt { .. } => alloc_buf.push(atr(nid)), + Kind::UnOp { .. } => alloc_buf.extend([atr(nid), atr(node.inputs[1])]), Kind::BinOp { .. } if node.lock_rc != 0 => {} Kind::BinOp { op } => { let &[.., lhs, rhs] = node.inputs.as_slice() else { unreachable!() }; - if let Kind::CInt { value } = fuc.nodes[rhs].kind + if let Kind::CInt { .. } = fuc.nodes[rhs].kind && fuc.nodes[rhs].lock_rc != 0 - && let Some(op) = op.imm_binop(node.ty) + && op.imm_binop(node.ty).is_some() { - self.emit(op(atr(nid), atr(lhs), value as _)); - } else if let Some(op) = - op.binop(node.ty).or(op.float_cmp(fuc.nodes[lhs].ty)) - { - self.emit(op(atr(nid), atr(lhs), atr(rhs))); - } else if let Some(against) = op.cmp_against() { - let op_ty = fuc.nodes[lhs].ty; - - self.emit(extend( - fuc.nodes[lhs].ty, - fuc.nodes[lhs].ty.extend(), - lhs, - lhs, - )); - self.emit(extend( - fuc.nodes[rhs].ty, - fuc.nodes[rhs].ty.extend(), - rhs, - rhs, - )); - - if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { - let opop = match op { - TokenKind::Le => TokenKind::Gt, - TokenKind::Ge => TokenKind::Lt, - _ => unreachable!(), - }; - let op_fn = opop.float_cmp(op_ty).unwrap(); - self.emit(op_fn(atr(nid), atr(lhs), atr(rhs))); - self.emit(instrs::not(atr(nid), atr(nid))); - } else { - let op_fn = - if op_ty.is_signed() { instrs::cmps } else { instrs::cmpu }; - self.emit(op_fn(atr(nid), atr(lhs), atr(rhs))); - self.emit(instrs::cmpui(atr(nid), atr(nid), against)); - if matches!(op, TokenKind::Eq | TokenKind::Lt | TokenKind::Gt) { - self.emit(instrs::not(atr(nid), atr(nid))); - } - } + alloc_buf.extend([atr(nid), atr(lhs)]); } else { - todo!("unhandled operator: {op}"); + alloc_buf.extend([atr(nid), atr(lhs), atr(rhs)]); } } - Kind::Call { args, func } => { + Kind::Call { args, .. } => { let (ret, mut parama) = tys.parama(node.ty); + if ret.is_some() { + alloc_buf.push(atr(nid)); + } let mut args = args.args(); let mut allocs = node.inputs[1..].iter(); while let Some(arg) = args.next(tys) { @@ -338,138 +207,85 @@ impl HbvmBackend { let Some(loc) = parama.next(ty, tys) else { continue }; let arg = *allocs.next().unwrap(); - let (rg, size) = match loc { - PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), - PLoc::WideReg(rg, size) => (rg, size), - PLoc::Ref(r, ..) => { - self.emit(instrs::cp(r, atr(arg))); - continue; - } - PLoc::Reg(r, ..) => { - self.emit(instrs::cp(r, atr(arg))); - continue; + alloc_buf.push(atr(arg)); + match loc { + PLoc::Reg(..) if ty.loc(tys) == Loc::Stack => {} + PLoc::WideReg(..) => alloc_buf.push(0), + PLoc::Reg(r, ..) | PLoc::Ref(r, ..) => { + self.emit(instrs::cp(r, atr(arg))) } }; - - self.emit(instrs::ld(rg, atr(arg), 0, size)); } - debug_assert!( - !matches!(ret, Some(PLoc::Ref(..))) || allocs.next().is_some() - ); - if let Some(PLoc::Ref(r, ..)) = ret { - self.emit(instrs::cp(r, atr(*node.inputs.last().unwrap()))) - } - - if func == ty::Func::ECA { - self.emit(instrs::eca()); - } else { - self.relocs.push(TypedReloc { - target: ty::Kind::Func(func).compress(), - reloc: Reloc::new(self.code.len(), 3, 4), - }); - self.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0)); - } - - match ret { - Some(PLoc::WideReg(r, size)) => { - debug_assert_eq!( - fuc.nodes[*node.inputs.last().unwrap()].kind, - Kind::Stck - ); - let stck = self.offsets[*node.inputs.last().unwrap() as usize]; - self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); - } - Some(PLoc::Reg(r, size)) if node.ty.loc(tys) == Loc::Stack => { - debug_assert_eq!( - fuc.nodes[*node.inputs.last().unwrap()].kind, - Kind::Stck - ); - let stck = self.offsets[*node.inputs.last().unwrap() as usize]; - self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); - } - Some(PLoc::Reg(r, ..)) => self.emit(instrs::cp(atr(nid), r)), - None | Some(PLoc::Ref(..)) => {} + alloc_buf.push(atr(*node.inputs.last().unwrap())); + self.emit(instrs::cp(r, *alloc_buf.last().unwrap())) } } - Kind::Global { global } => { - let reloc = Reloc::new(self.code.len(), 3, 4); - self.relocs.push(TypedReloc { - target: ty::Kind::Global(global).compress(), - reloc, - }); - self.emit(instrs::lra(atr(nid), 0, 0)); - } - Kind::Stck => { - let base = reg::STACK_PTR; - let offset = self.offsets[nid as usize]; - self.emit(instrs::addi64(atr(nid), base, offset as _)); - } + Kind::Stck | Kind::Global { .. } => alloc_buf.push(atr(nid)), Kind::Load => { let mut region = node.inputs[1]; - let mut offset = 0; if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && let Kind::CInt { value } = - fuc.nodes[fuc.nodes[region].inputs[2]].kind + && let Kind::CInt { .. } = fuc.nodes[fuc.nodes[region].inputs[2]].kind { region = fuc.nodes[region].inputs[1]; - offset = value as Offset; } - let size = tys.size_of(node.ty); if node.ty.loc(tys) != Loc::Stack { - let (base, offset) = match fuc.nodes[region].kind { - Kind::Stck => { - (reg::STACK_PTR, self.offsets[region as usize] + offset) - } - _ => (atr(region), offset), - }; - self.emit(instrs::ld(atr(nid), base, offset as _, size as _)); + alloc_buf.push(atr(nid)); + match fuc.nodes[region].kind { + Kind::Stck => {} + _ => alloc_buf.push(atr(region)), + } } } Kind::Stre if node.inputs[1] == VOID => {} Kind::Stre => { let mut region = node.inputs[2]; - let mut offset = 0; - let size = u16::try_from(tys.size_of(node.ty)).expect("TODO"); if matches!(fuc.nodes[region].kind, Kind::BinOp { op: TokenKind::Add | TokenKind::Sub - }) && let Kind::CInt { value } = - fuc.nodes[fuc.nodes[region].inputs[2]].kind + }) && let Kind::CInt { .. } = fuc.nodes[fuc.nodes[region].inputs[2]].kind && node.ty.loc(tys) == Loc::Reg { region = fuc.nodes[region].inputs[1]; - offset = value as Offset; } - let nd = &fuc.nodes[region]; - let value = node.inputs[1]; - let (base, offset, src) = match nd.kind { + match fuc.nodes[region].kind { Kind::Stck if node.ty.loc(tys) == Loc::Reg => { - (reg::STACK_PTR, self.offsets[region as usize] + offset, value) - } - _ => (atr(region), offset, value), - }; - - match node.ty.loc(tys) { - Loc::Reg => self.emit(instrs::st(atr(src), base, offset as _, size)), - Loc::Stack => { - debug_assert_eq!(offset, 0); - self.emit(instrs::bmc(atr(src), base, size)) + alloc_buf.push(atr(node.inputs[1])) } + _ => alloc_buf.extend([atr(region), atr(node.inputs[1])]), } } + Kind::Mem => { + self.emit(instrs::cp(atr(MEM), reg::RET)); + continue; + } + Kind::Arg => { + continue; + } + _ => {} + } - Kind::Mem => self.emit(instrs::cp(atr(MEM), reg::RET)), - Kind::Arg => {} - e @ (Kind::Start - | Kind::Entry - | Kind::End - | Kind::Loops - | Kind::Then - | Kind::Else - | Kind::Phi - | Kind::Join - | Kind::Assert { .. }) => unreachable!("{e:?}"), + self.emit_instr(super::InstrCtx { + nid, + sig, + is_next_block, + is_last_block: i == fuc.func.blocks.len() - 1, + retl, + allocs: &alloc_buf, + nodes: fuc.nodes, + tys, + files, + }); + + if let Kind::Call { .. } = node.kind { + let (ret, ..) = tys.parama(node.ty); + + match ret { + Some(PLoc::WideReg(..)) => {} + Some(PLoc::Reg(..)) if node.ty.loc(tys) == Loc::Stack => {} + Some(PLoc::Reg(r, ..)) => self.emit(instrs::cp(atr(nid), r)), + None | Some(PLoc::Ref(..)) => {} + } } } } @@ -479,10 +295,10 @@ impl HbvmBackend { let bundle_count = self.ralloc_my.bundles.len() + (reg_offset as usize); ( if fuc.tail { - bundle_count.saturating_sub(reg::RET_ADDR as _) - } else { assert!(bundle_count < reg::STACK_PTR as usize, "TODO: spill memory"); self.ralloc_my.bundles.len() + } else { + bundle_count.saturating_sub(reg::RET_ADDR as _) }, fuc.tail, ) @@ -725,6 +541,7 @@ impl<'a> Function<'a> { } } } + Kind::CInt { value: 0 } if self.nodes.is_hard_zero(nid) => {} Kind::CInt { .. } | Kind::BinOp { .. } | Kind::UnOp { .. } diff --git a/lang/src/son/hbvm/their_regalloc.rs b/lang/src/son/hbvm/their_regalloc.rs index a1e6b4a..2d8d2eb 100644 --- a/lang/src/son/hbvm/their_regalloc.rs +++ b/lang/src/son/hbvm/their_regalloc.rs @@ -6,7 +6,7 @@ use { son::{debug_assert_matches, Kind, ARG_START, MEM, NEVER, VOID}, ty::{self, Arg, Loc}, utils::BitSet, - HashMap, Offset, PLoc, Reloc, Sig, TypedReloc, Types, + HashMap, PLoc, Sig, Types, }, alloc::{borrow::ToOwned, vec::Vec}, core::mem, @@ -116,6 +116,7 @@ impl HbvmBackend { } } + let mut alloc_buf = vec![]; for (i, block) in fuc.blocks.iter().enumerate() { let blk = regalloc2::Block(i as _); self.offsets[block.nid as usize] = self.code.len() as _; @@ -132,309 +133,24 @@ impl HbvmBackend { if nid == NEVER { continue; }; - let allocs = ralloc.ctx.output.inst_allocs(inst); - let node = &fuc.nodes[nid]; - let backref = fuc.backrefs[nid as usize]; - let mut extend = |base: ty::Id, dest: ty::Id, from: usize, to: usize| { - let (bsize, dsize) = (tys.size_of(base), tys.size_of(dest)); - debug_assert!(bsize <= 8, "{}", ty::Display::new(tys, files, base)); - debug_assert!(dsize <= 8, "{}", ty::Display::new(tys, files, dest)); - if bsize == dsize { - return Default::default(); - } - match (base.is_signed(), dest.is_signed()) { - (true, true) => { - let op = [instrs::sxt8, instrs::sxt16, instrs::sxt32] - [bsize.ilog2() as usize]; - op(atr(allocs[to]), atr(allocs[from])) - } - _ => { - let mask = (1u64 << (bsize * 8)) - 1; - instrs::andi(atr(allocs[to]), atr(allocs[from]), mask) - } - } - }; - - match node.kind { - Kind::If => { - let &[_, cnd] = node.inputs.as_slice() else { unreachable!() }; - if let Kind::BinOp { op } = fuc.nodes[cnd].kind - && let Some((op, swapped)) = - op.cond_op(fuc.nodes[fuc.nodes[cnd].inputs[1]].ty) - { - let &[lhs, rhs] = allocs else { unreachable!() }; - let &[_, lh, rh] = fuc.nodes[cnd].inputs.as_slice() else { - unreachable!() - }; - - self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 0, 0)); - self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 1, 1)); - - let rel = Reloc::new(self.code.len(), 3, 2); - self.jump_relocs.push((node.outputs[!swapped as usize], rel)); - self.emit(op(atr(lhs), atr(rhs), 0)); - } else { - self.emit(extend(fuc.nodes[cnd].ty, fuc.nodes[cnd].ty.extend(), 0, 0)); - let rel = Reloc::new(self.code.len(), 3, 2); - self.jump_relocs.push((node.outputs[0], rel)); - self.emit(instrs::jne(atr(allocs[0]), reg::ZERO, 0)); - } - } - Kind::Loop | Kind::Region => { - if backref as usize != i + 1 { - let rel = Reloc::new(self.code.len(), 1, 4); - self.jump_relocs.push((nid, rel)); - self.emit(instrs::jmp(0)); - } - } - Kind::Return => { - match retl { - Some(PLoc::Reg(r, size)) if sig.ret.loc(tys) == Loc::Stack => { - self.emit(instrs::ld(r, atr(allocs[0]), 0, size)) - } - None | Some(PLoc::Reg(..)) => {} - Some(PLoc::WideReg(r, size)) => { - self.emit(instrs::ld(r, atr(allocs[0]), 0, size)) - } - Some(PLoc::Ref(_, size)) => { - let [src, dst] = [atr(allocs[0]), atr(allocs[1])]; - if let Ok(size) = u16::try_from(size) { - self.emit(instrs::bmc(src, dst, size)); - } else { - for _ in 0..size / u16::MAX as u32 { - self.emit(instrs::bmc(src, dst, u16::MAX)); - self.emit(instrs::addi64(src, src, u16::MAX as _)); - self.emit(instrs::addi64(dst, dst, u16::MAX as _)); - } - self.emit(instrs::bmc(src, dst, size as u16)); - self.emit(instrs::addi64(src, src, size.wrapping_neg() as _)); - self.emit(instrs::addi64(dst, dst, size.wrapping_neg() as _)); - } - } - } - - if i != fuc.blocks.len() - 1 { - let rel = Reloc::new(self.code.len(), 1, 4); - self.ret_relocs.push(rel); - self.emit(instrs::jmp(0)); - } - } - Kind::Die => { - self.emit(instrs::un()); - } - Kind::CInt { value: 0 } => self.emit(instrs::cp(atr(allocs[0]), reg::ZERO)), - Kind::CInt { value } if node.ty.is_float() => { - self.emit(match node.ty { - ty::Id::F32 => instrs::li32( - atr(allocs[0]), - (f64::from_bits(value as _) as f32).to_bits(), - ), - ty::Id::F64 => instrs::li64(atr(allocs[0]), value as _), - _ => unreachable!(), - }); - } - Kind::CInt { value } => self.emit(match tys.size_of(node.ty) { - 1 => instrs::li8(atr(allocs[0]), value as _), - 2 => instrs::li16(atr(allocs[0]), value as _), - 4 => instrs::li32(atr(allocs[0]), value as _), - _ => instrs::li64(atr(allocs[0]), value as _), - }), - Kind::UnOp { op } => { - let op = op - .unop( - node.ty, - tys.inner_of(fuc.nodes[node.inputs[1]].ty) - .unwrap_or(fuc.nodes[node.inputs[1]].ty), - ) - .unwrap_or_else(|| { - panic!( - "TODO: unary operator not supported: {op} {} {}", - ty::Display::new(tys, files, node.ty), - ty::Display::new( - tys, - files, - tys.inner_of(fuc.nodes[node.inputs[1]].ty) - .unwrap_or(fuc.nodes[node.inputs[1]].ty) - ) - ) - }); - let &[dst, oper] = allocs else { unreachable!() }; - self.emit(op(atr(dst), atr(oper))); - } - Kind::BinOp { .. } if node.lock_rc != 0 => {} - Kind::BinOp { op } => { - let &[.., lh, rh] = node.inputs.as_slice() else { unreachable!() }; - - if let Kind::CInt { value } = fuc.nodes[rh].kind - && fuc.nodes[rh].lock_rc != 0 - && let Some(op) = op.imm_binop(node.ty) - { - let &[dst, lhs] = allocs else { unreachable!() }; - self.emit(op(atr(dst), atr(lhs), value as _)); - } else if let Some(op) = - op.binop(node.ty).or(op.float_cmp(fuc.nodes[lh].ty)) - { - let &[dst, lhs, rhs] = allocs else { unreachable!() }; - self.emit(op(atr(dst), atr(lhs), atr(rhs))); - } else if let Some(against) = op.cmp_against() { - let op_ty = fuc.nodes[rh].ty; - - //self.emit(extend(fuc.nodes[lh].ty, fuc.nodes[lh].ty.extend(), 1, 1)); - //self.emit(extend(fuc.nodes[rh].ty, fuc.nodes[rh].ty.extend(), 2, 2)); - let &[dst, lhs, rhs] = allocs else { unreachable!() }; - - if op_ty.is_float() && matches!(op, TokenKind::Le | TokenKind::Ge) { - let opop = match op { - TokenKind::Le => TokenKind::Gt, - TokenKind::Ge => TokenKind::Lt, - _ => unreachable!(), - }; - let op_fn = opop.float_cmp(op_ty).unwrap(); - self.emit(op_fn(atr(dst), atr(lhs), atr(rhs))); - self.emit(instrs::not(atr(dst), atr(dst))); - } else { - let op_fn = - if op_ty.is_signed() { instrs::cmps } else { instrs::cmpu }; - self.emit(op_fn(atr(dst), atr(lhs), atr(rhs))); - self.emit(instrs::cmpui(atr(dst), atr(dst), against)); - if matches!(op, TokenKind::Eq | TokenKind::Lt | TokenKind::Gt) { - self.emit(instrs::not(atr(dst), atr(dst))); - } - } - } else { - todo!("unhandled operator: {op}"); - } - } - Kind::Call { args, func } => { - let (ret, mut parama) = tys.parama(node.ty); - let has_ret = ret.is_some() as usize; - let mut args = args.args(); - let mut allocs = allocs[has_ret..].iter(); - while let Some(arg) = args.next(tys) { - let Arg::Value(ty) = arg else { continue }; - let Some(loc) = parama.next(ty, tys) else { continue }; - - let &arg = allocs.next().unwrap(); - let (rg, size) = match loc { - PLoc::Reg(rg, size) if ty.loc(tys) == Loc::Stack => (rg, size), - PLoc::WideReg(rg, size) => (rg, size), - PLoc::Ref(..) | PLoc::Reg(..) => continue, - }; - if size > 8 { - allocs.next().unwrap(); - } - self.emit(instrs::ld(rg, atr(arg), 0, size)); - } - - debug_assert!( - !matches!(ret, Some(PLoc::Ref(..))) || allocs.next().is_some() + self.emit_instr(super::InstrCtx { + nid, + sig, + is_next_block: fuc.backrefs[nid as usize] as usize == i + 1, + is_last_block: i == fuc.blocks.len() - 1, + retl, + allocs: { + alloc_buf.clear(); + alloc_buf.extend( + ralloc.ctx.output.inst_allocs(inst).iter().copied().map(&mut atr), ); - - if func == ty::Func::ECA { - self.emit(instrs::eca()); - } else { - self.relocs.push(TypedReloc { - target: ty::Kind::Func(func).compress(), - reloc: Reloc::new(self.code.len(), 3, 4), - }); - self.emit(instrs::jal(reg::RET_ADDR, reg::ZERO, 0)); - } - - if let Some(PLoc::WideReg(r, size)) = ret { - debug_assert_eq!( - fuc.nodes[*node.inputs.last().unwrap()].kind, - Kind::Stck - ); - let stck = self.offsets[*node.inputs.last().unwrap() as usize]; - self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); - } - if let Some(PLoc::Reg(r, size)) = ret - && node.ty.loc(tys) == Loc::Stack - { - debug_assert_eq!( - fuc.nodes[*node.inputs.last().unwrap()].kind, - Kind::Stck - ); - let stck = self.offsets[*node.inputs.last().unwrap() as usize]; - self.emit(instrs::st(r, reg::STACK_PTR, stck as _, size)); - } - } - Kind::Global { global } => { - let reloc = Reloc::new(self.code.len(), 3, 4); - self.relocs.push(TypedReloc { - target: ty::Kind::Global(global).compress(), - reloc, - }); - self.emit(instrs::lra(atr(allocs[0]), 0, 0)); - } - Kind::Stck => { - let base = reg::STACK_PTR; - let offset = self.offsets[nid as usize]; - self.emit(instrs::addi64(atr(allocs[0]), base, offset as _)); - } - Kind::Load => { - let mut region = node.inputs[1]; - let mut offset = 0; - if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && let Kind::CInt { value } = - fuc.nodes[fuc.nodes[region].inputs[2]].kind - { - region = fuc.nodes[region].inputs[1]; - offset = value as Offset; - } - let size = tys.size_of(node.ty); - if node.ty.loc(tys) != Loc::Stack { - let (base, offset) = match fuc.nodes[region].kind { - Kind::Stck => { - (reg::STACK_PTR, self.offsets[region as usize] + offset) - } - _ => (atr(allocs[1]), offset), - }; - self.emit(instrs::ld(atr(allocs[0]), base, offset as _, size as _)); - } - } - Kind::Stre if node.inputs[1] == VOID => {} - Kind::Stre => { - let mut region = node.inputs[2]; - let mut offset = 0; - let size = u16::try_from(tys.size_of(node.ty)).expect("TODO"); - if fuc.nodes[region].kind == (Kind::BinOp { op: TokenKind::Add }) - && let Kind::CInt { value } = - fuc.nodes[fuc.nodes[region].inputs[2]].kind - && node.ty.loc(tys) == Loc::Reg - { - region = fuc.nodes[region].inputs[1]; - offset = value as Offset; - } - let nd = &fuc.nodes[region]; - let (base, offset, src) = match nd.kind { - Kind::Stck if node.ty.loc(tys) == Loc::Reg => { - (reg::STACK_PTR, self.offsets[region as usize] + offset, allocs[0]) - } - _ => (atr(allocs[0]), offset, allocs[1]), - }; - - match node.ty.loc(tys) { - Loc::Reg => self.emit(instrs::st(atr(src), base, offset as _, size)), - Loc::Stack => { - debug_assert_eq!(offset, 0); - self.emit(instrs::bmc(atr(src), base, size)) - } - } - } - Kind::Start - | Kind::Assert { .. } - | Kind::Entry - | Kind::Mem - | Kind::End - | Kind::Loops - | Kind::Then - | Kind::Else - | Kind::Phi - | Kind::Arg - | Kind::Join => unreachable!(), - } + alloc_buf.as_slice() + }, + nodes: fuc.nodes, + tys, + files, + }); } }