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