cranelift_codegen/isa/x64/
mod.rs1pub use self::inst::{AtomicRmwSeqOp, EmitInfo, EmitState, Inst, args, external};
4
5use super::{OwnedTargetIsa, TargetIsa};
6use crate::dominator_tree::DominatorTree;
7use crate::ir::{self, Function, Type, types};
8#[cfg(feature = "unwind")]
9use crate::isa::unwind::systemv;
10use crate::isa::x64::settings as x64_settings;
11use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey};
12use crate::machinst::{
13 CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,
14 TextSectionBuilder, VCode, compile,
15};
16use crate::result::CodegenResult;
17use crate::settings::{self as shared_settings, Flags};
18use crate::{Final, MachBufferFinalized};
19use alloc::{boxed::Box, vec::Vec};
20use core::fmt;
21use cranelift_control::ControlPlane;
22use std::string::String;
23use target_lexicon::Triple;
24
25mod abi;
26pub(crate) mod encoding;
27mod inst;
28mod lower;
29mod pcc;
30pub mod settings;
31
32pub use inst::unwind::systemv::create_cie;
33
34pub(crate) struct X64Backend {
36 triple: Triple,
37 flags: Flags,
38 x64_flags: x64_settings::Flags,
39}
40
41impl X64Backend {
42 fn new_with_flags(triple: Triple, flags: Flags, x64_flags: x64_settings::Flags) -> Self {
44 Self {
45 triple,
46 flags,
47 x64_flags,
48 }
49 }
50
51 fn compile_vcode(
52 &self,
53 func: &Function,
54 domtree: &DominatorTree,
55 ctrl_plane: &mut ControlPlane,
56 ) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
57 let emit_info = EmitInfo::new(self.flags.clone(), self.x64_flags.clone());
60 let sigs = SigSet::new::<abi::X64ABIMachineSpec>(func, &self.flags)?;
61 let abi = abi::X64Callee::new(func, self, &self.x64_flags, &sigs)?;
62 compile::compile::<Self>(func, domtree, self, abi, emit_info, sigs, ctrl_plane)
63 }
64}
65
66impl TargetIsa for X64Backend {
67 fn compile_function(
68 &self,
69 func: &Function,
70 domtree: &DominatorTree,
71 want_disasm: bool,
72 ctrl_plane: &mut ControlPlane,
73 ) -> CodegenResult<CompiledCodeStencil> {
74 let (vcode, regalloc_result) = self.compile_vcode(func, domtree, ctrl_plane)?;
75
76 let emit_result = vcode.emit(®alloc_result, want_disasm, &self.flags, ctrl_plane);
77 let frame_size = emit_result.frame_size;
78 let value_labels_ranges = emit_result.value_labels_ranges;
79 let buffer = emit_result.buffer;
80 let sized_stackslot_offsets = emit_result.sized_stackslot_offsets;
81 let dynamic_stackslot_offsets = emit_result.dynamic_stackslot_offsets;
82
83 if let Some(disasm) = emit_result.disasm.as_ref() {
84 crate::trace!("disassembly:\n{}", disasm);
85 }
86
87 Ok(CompiledCodeStencil {
88 buffer,
89 frame_size,
90 vcode: emit_result.disasm,
91 value_labels_ranges,
92 sized_stackslot_offsets,
93 dynamic_stackslot_offsets,
94 bb_starts: emit_result.bb_offsets,
95 bb_edges: emit_result.bb_edges,
96 })
97 }
98
99 fn flags(&self) -> &Flags {
100 &self.flags
101 }
102
103 fn isa_flags(&self) -> Vec<shared_settings::Value> {
104 self.x64_flags.iter().collect()
105 }
106
107 fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
108 IsaFlagsHashKey(self.x64_flags.hash_key())
109 }
110
111 fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {
112 16
113 }
114
115 fn name(&self) -> &'static str {
116 "x64"
117 }
118
119 fn triple(&self) -> &Triple {
120 &self.triple
121 }
122
123 #[cfg(feature = "unwind")]
124 fn emit_unwind_info(
125 &self,
126 result: &CompiledCode,
127 kind: crate::isa::unwind::UnwindInfoKind,
128 ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
129 emit_unwind_info(&result.buffer, kind)
130 }
131
132 #[cfg(feature = "unwind")]
133 fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
134 Some(inst::unwind::systemv::create_cie())
135 }
136
137 #[cfg(feature = "unwind")]
138 fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError> {
139 inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)
140 }
141
142 fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {
143 Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))
144 }
145
146 fn function_alignment(&self) -> FunctionAlignment {
147 Inst::function_alignment()
148 }
149
150 fn page_size_align_log2(&self) -> u8 {
151 debug_assert_eq!(1 << 12, 0x1000);
152 12
153 }
154
155 #[cfg(feature = "disas")]
156 fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
157 use capstone::prelude::*;
158 Capstone::new()
159 .x86()
160 .mode(arch::x86::ArchMode::Mode64)
161 .syntax(arch::x86::ArchSyntax::Att)
162 .detail(true)
163 .build()
164 }
165
166 fn pretty_print_reg(&self, reg: Reg, size: u8) -> String {
167 inst::regs::pretty_print_reg(reg, size)
168 }
169
170 fn has_native_fma(&self) -> bool {
171 self.x64_flags.use_fma()
172 }
173
174 fn has_round(&self) -> bool {
175 self.x64_flags.use_sse41()
176 }
177
178 fn has_x86_blendv_lowering(&self, ty: Type) -> bool {
179 self.x64_flags.use_sse41() && ty != types::I16X8
184 }
185
186 fn has_x86_pshufb_lowering(&self) -> bool {
187 self.x64_flags.use_ssse3()
188 }
189
190 fn has_x86_pmulhrsw_lowering(&self) -> bool {
191 self.x64_flags.use_ssse3()
192 }
193
194 fn has_x86_pmaddubsw_lowering(&self) -> bool {
195 self.x64_flags.use_ssse3()
196 }
197
198 fn default_argument_extension(&self) -> ir::ArgumentExtension {
199 ir::ArgumentExtension::Uext
207 }
208}
209
210pub fn emit_unwind_info(
212 buffer: &MachBufferFinalized<Final>,
213 kind: crate::isa::unwind::UnwindInfoKind,
214) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
215 use crate::isa::unwind::{UnwindInfo, UnwindInfoKind};
216 Ok(match kind {
217 UnwindInfoKind::SystemV => {
218 let mapper = self::inst::unwind::systemv::RegisterMapper;
219 Some(UnwindInfo::SystemV(
220 crate::isa::unwind::systemv::create_unwind_info_from_insts(
221 &buffer.unwind_info[..],
222 buffer.data().len(),
223 &mapper,
224 )?,
225 ))
226 }
227 UnwindInfoKind::Windows => Some(UnwindInfo::WindowsX64(
228 crate::isa::unwind::winx64::create_unwind_info_from_insts::<
229 self::inst::unwind::winx64::RegisterMapper,
230 >(&buffer.unwind_info[..])?,
231 )),
232 _ => None,
233 })
234}
235
236impl fmt::Display for X64Backend {
237 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
238 f.debug_struct("MachBackend")
239 .field("name", &self.name())
240 .field("triple", &self.triple())
241 .field("flags", &format!("{}", self.flags()))
242 .finish()
243 }
244}
245
246pub(crate) fn isa_builder(triple: Triple) -> IsaBuilder {
248 IsaBuilder {
249 triple,
250 setup: x64_settings::builder(),
251 constructor: isa_constructor,
252 }
253}
254
255fn isa_constructor(
256 triple: Triple,
257 shared_flags: Flags,
258 builder: &shared_settings::Builder,
259) -> CodegenResult<OwnedTargetIsa> {
260 let isa_flags = x64_settings::Flags::new(&shared_flags, builder);
261 let backend = X64Backend::new_with_flags(triple, shared_flags, isa_flags);
262 Ok(backend.wrapped())
263}