use super::*;
use crate::ir::{self, Endianness};
use crate::isa;
use crate::isa::pulley_shared::abi::PulleyMachineDeps;
use crate::isa::pulley_shared::PointerWidth;
use core::marker::PhantomData;
use cranelift_control::ControlPlane;
use pulley_interpreter::encode as enc;
use pulley_interpreter::regs::BinaryOperands;
pub struct EmitInfo {
call_conv: isa::CallConv,
shared_flags: settings::Flags,
isa_flags: crate::isa::pulley_shared::settings::Flags,
}
impl EmitInfo {
pub(crate) fn new(
call_conv: isa::CallConv,
shared_flags: settings::Flags,
isa_flags: crate::isa::pulley_shared::settings::Flags,
) -> Self {
Self {
call_conv,
shared_flags,
isa_flags,
}
}
fn endianness(&self, flags: MemFlags) -> Endianness {
flags.endianness(self.isa_flags.endianness())
}
}
#[derive(Default, Clone, Debug)]
pub struct EmitState<P>
where
P: PulleyTargetKind,
{
_phantom: PhantomData<P>,
ctrl_plane: ControlPlane,
user_stack_map: Option<ir::UserStackMap>,
frame_layout: FrameLayout,
}
impl<P> EmitState<P>
where
P: PulleyTargetKind,
{
fn take_stack_map(&mut self) -> Option<ir::UserStackMap> {
self.user_stack_map.take()
}
}
impl<P> MachInstEmitState<InstAndKind<P>> for EmitState<P>
where
P: PulleyTargetKind,
{
fn new(abi: &Callee<PulleyMachineDeps<P>>, ctrl_plane: ControlPlane) -> Self {
EmitState {
_phantom: PhantomData,
ctrl_plane,
user_stack_map: None,
frame_layout: abi.frame_layout().clone(),
}
}
fn pre_safepoint(&mut self, user_stack_map: Option<ir::UserStackMap>) {
self.user_stack_map = user_stack_map;
}
fn ctrl_plane_mut(&mut self) -> &mut ControlPlane {
&mut self.ctrl_plane
}
fn take_ctrl_plane(self) -> ControlPlane {
self.ctrl_plane
}
fn frame_layout(&self) -> &FrameLayout {
&self.frame_layout
}
}
impl<P> MachInstEmit for InstAndKind<P>
where
P: PulleyTargetKind,
{
type State = EmitState<P>;
type Info = EmitInfo;
fn emit(&self, sink: &mut MachBuffer<Self>, emit_info: &Self::Info, state: &mut Self::State) {
let mut start = sink.cur_offset();
pulley_emit(self, sink, emit_info, state, &mut start);
let end = sink.cur_offset();
assert!(
(end - start) <= InstAndKind::<P>::worst_case_size(),
"encoded inst {self:?} longer than worst-case size: length: {}, Inst::worst_case_size() = {}",
end - start,
InstAndKind::<P>::worst_case_size()
);
}
fn pretty_print_inst(&self, state: &mut Self::State) -> String {
self.print_with_state(state)
}
}
fn pulley_emit<P>(
inst: &Inst,
sink: &mut MachBuffer<InstAndKind<P>>,
emit_info: &EmitInfo,
state: &mut EmitState<P>,
start_offset: &mut u32,
) where
P: PulleyTargetKind,
{
match inst {
Inst::Args { .. } | Inst::Rets { .. } | Inst::DummyUse { .. } => {}
Inst::TrapIf { cond, code } => {
let trap = sink.defer_trap(*code);
let not_trap = sink.get_label();
<InstAndKind<P>>::from(Inst::BrIf {
cond: cond.clone(),
taken: trap,
not_taken: not_trap,
})
.emit(sink, emit_info, state);
sink.bind_label(not_trap, &mut state.ctrl_plane);
}
Inst::Nop => todo!(),
Inst::GetSpecial { dst, reg } => enc::xmov(sink, dst, reg),
Inst::LoadExtName { .. } => todo!(),
Inst::Call { info } => {
let offset = sink.cur_offset();
let mut args = &info.dest.args[..];
while !args.is_empty() && args.last().copied() == XReg::new(x_reg(args.len() - 1)) {
args = &args[..args.len() - 1];
}
match args {
[] => enc::call(sink, 0),
[x0] => enc::call1(sink, x0, 0),
[x0, x1] => enc::call2(sink, x0, x1, 0),
[x0, x1, x2] => enc::call3(sink, x0, x1, x2, 0),
[x0, x1, x2, x3] => enc::call4(sink, x0, x1, x2, x3, 0),
_ => unreachable!(),
}
let end = sink.cur_offset();
sink.add_reloc_at_offset(
end - 4,
Reloc::X86CallPCRel4,
&info.dest.name,
-i64::from(end - offset - 4),
);
if let Some(s) = state.take_stack_map() {
let offset = sink.cur_offset();
sink.push_user_stack_map(state, offset, s);
}
sink.add_call_site();
let adjust = -i32::try_from(info.callee_pop_size).unwrap();
for i in PulleyMachineDeps::<P>::gen_sp_reg_adjust(adjust) {
<InstAndKind<P>>::from(i).emit(sink, emit_info, state);
}
}
Inst::IndirectCall { info } => {
enc::call_indirect(sink, info.dest);
if let Some(s) = state.take_stack_map() {
let offset = sink.cur_offset();
sink.push_user_stack_map(state, offset, s);
}
sink.add_call_site();
let adjust = -i32::try_from(info.callee_pop_size).unwrap();
for i in PulleyMachineDeps::<P>::gen_sp_reg_adjust(adjust) {
<InstAndKind<P>>::from(i).emit(sink, emit_info, state);
}
}
Inst::ReturnCall { info } => {
emit_return_call_common_sequence(sink, emit_info, state, &info);
sink.put1(pulley_interpreter::Opcode::Jump as u8);
sink.add_reloc(Reloc::X86CallPCRel4, &info.dest, -1);
sink.put4(0);
*start_offset = sink.cur_offset();
}
Inst::ReturnIndirectCall { info } => {
emit_return_call_common_sequence(sink, emit_info, state, &info);
enc::xjump(sink, info.dest);
*start_offset = sink.cur_offset();
}
Inst::IndirectCallHost { info } => {
sink.add_reloc(Reloc::PulleyCallIndirectHost, &info.dest, 0);
enc::call_indirect_host(sink, 0_u8);
if let Some(s) = state.take_stack_map() {
let offset = sink.cur_offset();
sink.push_user_stack_map(state, offset, s);
}
sink.add_call_site();
assert!(info.callee_pop_size == 0);
}
Inst::Jump { label } => {
sink.use_label_at_offset(*start_offset + 1, *label, LabelUse::Jump(1));
sink.add_uncond_branch(*start_offset, *start_offset + 5, *label);
enc::jump(sink, 0x00000000);
}
Inst::BrIf {
cond,
taken,
not_taken,
} => {
let mut inverted = SmallVec::<[u8; 16]>::new();
cond.invert().encode(&mut inverted);
let len = inverted.len() as u32;
debug_assert!(len > 4);
let taken_end = *start_offset + len;
sink.use_label_at_offset(taken_end - 4, *taken, LabelUse::Jump(len - 4));
sink.add_cond_branch(*start_offset, taken_end, *taken, &inverted);
cond.encode(sink);
debug_assert_eq!(sink.cur_offset(), taken_end);
let not_taken_start = taken_end + 1;
let not_taken_end = not_taken_start + 4;
sink.use_label_at_offset(not_taken_start, *not_taken, LabelUse::Jump(1));
sink.add_uncond_branch(taken_end, not_taken_end, *not_taken);
enc::jump(sink, 0x00000000);
assert_eq!(sink.cur_offset(), not_taken_end);
}
Inst::LoadAddr { dst, mem } => {
let base = mem.get_base_register();
let offset = mem.get_offset_with_state(state);
if let Some(base) = base {
if offset == 0 {
enc::xmov(sink, dst, base);
} else {
if let Ok(offset) = i8::try_from(offset) {
enc::xconst8(sink, dst, offset);
} else if let Ok(offset) = i16::try_from(offset) {
enc::xconst16(sink, dst, offset);
} else {
enc::xconst32(sink, dst, offset);
}
match P::pointer_width() {
PointerWidth::PointerWidth32 => {
enc::xadd32(sink, BinaryOperands::new(dst, base, dst))
}
PointerWidth::PointerWidth64 => {
enc::xadd64(sink, BinaryOperands::new(dst, base, dst))
}
}
}
} else {
unreachable!("all pulley amodes have a base register right now")
}
}
Inst::XLoad {
dst,
mem,
ty,
flags,
} => {
use Endianness as E;
assert!(flags.trap_code().is_none());
let addr = AddrO32::Base {
addr: mem.get_base_register().unwrap(),
offset: mem.get_offset_with_state(state),
};
let endian = emit_info.endianness(*flags);
match *ty {
I8 => enc::xload8_u32_o32(sink, dst, addr),
I16 => match endian {
E::Little => enc::xload16le_s32_o32(sink, dst, addr),
E::Big => enc::xload16be_s32_o32(sink, dst, addr),
},
I32 => match endian {
E::Little => enc::xload32le_o32(sink, dst, addr),
E::Big => enc::xload32be_o32(sink, dst, addr),
},
I64 => match endian {
E::Little => enc::xload64le_o32(sink, dst, addr),
E::Big => enc::xload64be_o32(sink, dst, addr),
},
_ => unimplemented!("xload ty={ty:?}"),
}
}
Inst::FLoad {
dst,
mem,
ty,
flags,
} => {
use Endianness as E;
assert!(flags.trap_code().is_none());
let addr = AddrO32::Base {
addr: mem.get_base_register().unwrap(),
offset: mem.get_offset_with_state(state),
};
let endian = emit_info.endianness(*flags);
match *ty {
F32 => match endian {
E::Little => enc::fload32le_o32(sink, dst, addr),
E::Big => enc::fload32be_o32(sink, dst, addr),
},
F64 => match endian {
E::Little => enc::fload64le_o32(sink, dst, addr),
E::Big => enc::fload64be_o32(sink, dst, addr),
},
_ => unimplemented!("fload ty={ty:?}"),
}
}
Inst::VLoad {
dst,
mem,
ty,
flags,
} => {
assert!(flags.trap_code().is_none());
let addr = AddrO32::Base {
addr: mem.get_base_register().unwrap(),
offset: mem.get_offset_with_state(state),
};
let endian = emit_info.endianness(*flags);
assert_eq!(endian, Endianness::Little);
assert_eq!(ty.bytes(), 16);
enc::vload128le_o32(sink, dst, addr);
}
Inst::XStore {
mem,
src,
ty,
flags,
} => {
use Endianness as E;
assert!(flags.trap_code().is_none());
let addr = AddrO32::Base {
addr: mem.get_base_register().unwrap(),
offset: mem.get_offset_with_state(state),
};
let endian = emit_info.endianness(*flags);
match *ty {
I8 => enc::xstore8_o32(sink, addr, src),
I16 => match endian {
E::Little => enc::xstore16le_o32(sink, addr, src),
E::Big => enc::xstore16be_o32(sink, addr, src),
},
I32 => match endian {
E::Little => enc::xstore32le_o32(sink, addr, src),
E::Big => enc::xstore32be_o32(sink, addr, src),
},
I64 => match endian {
E::Little => enc::xstore64le_o32(sink, addr, src),
E::Big => enc::xstore64be_o32(sink, addr, src),
},
_ => unimplemented!("xstore ty={ty:?}"),
}
}
Inst::FStore {
mem,
src,
ty,
flags,
} => {
use Endianness as E;
assert!(flags.trap_code().is_none());
let addr = AddrO32::Base {
addr: mem.get_base_register().unwrap(),
offset: mem.get_offset_with_state(state),
};
let endian = emit_info.endianness(*flags);
match *ty {
F32 => match endian {
E::Little => enc::fstore32le_o32(sink, addr, src),
E::Big => enc::fstore32be_o32(sink, addr, src),
},
F64 => match endian {
E::Little => enc::fstore64le_o32(sink, addr, src),
E::Big => enc::fstore64be_o32(sink, addr, src),
},
_ => unimplemented!("fstore ty={ty:?}"),
}
}
Inst::VStore {
mem,
src,
ty,
flags,
} => {
assert!(flags.trap_code().is_none());
let addr = AddrO32::Base {
addr: mem.get_base_register().unwrap(),
offset: mem.get_offset_with_state(state),
};
let endian = emit_info.endianness(*flags);
assert_eq!(endian, Endianness::Little);
assert_eq!(ty.bytes(), 16);
enc::vstore128le_o32(sink, addr, src);
}
Inst::BrTable {
idx,
default,
targets,
} => {
let amt = u32::try_from(targets.len() + 1).expect("too many branch targets");
let br_table_size = amt * 4 + 6;
if sink.island_needed(br_table_size) {
let label = sink.get_label();
<InstAndKind<P>>::from(Inst::Jump { label }).emit(sink, emit_info, state);
sink.emit_island(br_table_size, &mut state.ctrl_plane);
sink.bind_label(label, &mut state.ctrl_plane);
}
enc::br_table32(sink, *idx, amt);
for target in targets.iter() {
let offset = sink.cur_offset();
sink.use_label_at_offset(offset, *target, LabelUse::Jump(0));
sink.put4(0);
}
let offset = sink.cur_offset();
sink.use_label_at_offset(offset, *default, LabelUse::Jump(0));
sink.put4(0);
*start_offset = sink.cur_offset();
}
Inst::Raw { raw } => {
match raw {
RawInst::PushFrame
| RawInst::StackAlloc32 { .. }
| RawInst::PushFrameSave { .. } => {
sink.add_trap(ir::TrapCode::STACK_OVERFLOW);
}
_ => {}
}
super::generated::emit(raw, sink)
}
}
}
fn emit_return_call_common_sequence<T, P>(
sink: &mut MachBuffer<InstAndKind<P>>,
emit_info: &EmitInfo,
state: &mut EmitState<P>,
info: &ReturnCallInfo<T>,
) where
P: PulleyTargetKind,
{
let mut buffer = MachBuffer::new();
let mut fake_emit_state = state.clone();
return_call_emit_impl(&mut buffer, emit_info, &mut fake_emit_state, info);
let buffer = buffer.finish(&Default::default(), &mut Default::default());
let length = buffer.data().len() as u32;
if sink.island_needed(length) {
let jump_around_label = sink.get_label();
<InstAndKind<P>>::gen_jump(jump_around_label).emit(sink, emit_info, state);
sink.emit_island(length + 4, &mut state.ctrl_plane);
sink.bind_label(jump_around_label, &mut state.ctrl_plane);
}
return_call_emit_impl(sink, emit_info, state, info);
}
fn return_call_emit_impl<T, P>(
sink: &mut MachBuffer<InstAndKind<P>>,
emit_info: &EmitInfo,
state: &mut EmitState<P>,
info: &ReturnCallInfo<T>,
) where
P: PulleyTargetKind,
{
let epilogue = <PulleyMachineDeps<P>>::gen_epilogue_frame_restore(
emit_info.call_conv,
&emit_info.shared_flags,
&emit_info.isa_flags,
&state.frame_layout,
);
for inst in epilogue {
inst.emit(sink, emit_info, state);
}
let incoming_args_diff =
i64::from(state.frame_layout().tail_args_size - info.new_stack_arg_size);
if incoming_args_diff != 0 {
let amt = i32::try_from(incoming_args_diff).unwrap();
for inst in PulleyMachineDeps::<P>::gen_sp_reg_adjust(amt) {
<InstAndKind<P>>::from(inst).emit(sink, emit_info, state);
}
}
}