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 vcode: emit_result.disasm,
111 value_labels_ranges,
112 bb_starts: emit_result.bb_offsets,
113 bb_edges: emit_result.bb_edges,
114 }))
115 }
116
117 fn flags(&self) -> &Flags {
118 &self.flags
119 }
120
121 fn isa_flags(&self) -> Vec<shared_settings::Value> {
122 self.x64_flags.iter().collect()
123 }
124
125 fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
126 IsaFlagsHashKey(self.x64_flags.hash_key())
127 }
128
129 fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {
130 16
131 }
132
133 fn name(&self) -> &'static str {
134 "x64"
135 }
136
137 fn triple(&self) -> &Triple {
138 &self.triple
139 }
140
141 #[cfg(feature = "unwind")]
142 fn emit_unwind_info(
143 &self,
144 result: &crate::machinst::CompiledCode,
145 kind: crate::isa::unwind::UnwindInfoKind,
146 ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
147 emit_unwind_info(&result.buffer, kind)
148 }
149
150 #[cfg(feature = "unwind")]
151 fn create_systemv_cie(&self) -> Option<gimli::write::CommonInformationEntry> {
152 Some(inst::unwind::systemv::create_cie())
153 }
154
155 #[cfg(feature = "unwind")]
156 fn map_regalloc_reg_to_dwarf(&self, reg: Reg) -> Result<u16, systemv::RegisterMappingError> {
157 inst::unwind::systemv::map_reg(reg).map(|reg| reg.0)
158 }
159
160 fn text_section_builder(&self, num_funcs: usize) -> Box<dyn TextSectionBuilder> {
161 Box::new(MachTextSectionBuilder::<inst::Inst>::new(num_funcs))
162 }
163
164 fn function_alignment(&self) -> FunctionAlignment {
165 Inst::function_alignment()
166 }
167
168 fn page_size_align_log2(&self) -> u8 {
169 debug_assert_eq!(1 << 12, 0x1000);
170 12
171 }
172
173 #[cfg(feature = "disas")]
174 fn to_capstone(&self) -> Result<capstone::Capstone, capstone::Error> {
175 use capstone::prelude::*;
176 Capstone::new()
177 .x86()
178 .mode(arch::x86::ArchMode::Mode64)
179 .syntax(arch::x86::ArchSyntax::Att)
180 .detail(true)
181 .build()
182 }
183
184 fn pretty_print_reg(&self, reg: Reg, size: u8) -> String {
185 inst::regs::pretty_print_reg(reg, size)
186 }
187
188 fn has_native_fma(&self) -> bool {
189 self.x64_flags.has_avx() && self.x64_flags.has_fma()
190 }
191
192 fn has_round(&self) -> bool {
193 self.x64_flags.has_sse41()
194 }
195
196 fn has_blendv_lowering(&self, ty: Type) -> bool {
197 self.x64_flags.has_sse41() && ty != types::I16X8
202 }
203
204 fn has_x86_pshufb_lowering(&self) -> bool {
205 self.x64_flags.has_ssse3()
206 }
207
208 fn has_x86_pmulhrsw_lowering(&self) -> bool {
209 self.x64_flags.has_ssse3()
210 }
211
212 fn has_x86_pmaddubsw_lowering(&self) -> bool {
213 self.x64_flags.has_ssse3()
214 }
215
216 fn default_argument_extension(&self) -> ir::ArgumentExtension {
217 ir::ArgumentExtension::Uext
225 }
226}
227
228pub fn emit_unwind_info(
230 buffer: &MachBufferFinalized,
231 kind: crate::isa::unwind::UnwindInfoKind,
232) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
233 #[cfg(feature = "unwind")]
234 use crate::isa::unwind::{UnwindInfo, UnwindInfoKind};
235 #[cfg(not(feature = "unwind"))]
236 let _ = buffer;
237 Ok(match kind {
238 #[cfg(feature = "unwind")]
239 UnwindInfoKind::SystemV => {
240 let mapper = self::inst::unwind::systemv::RegisterMapper;
241 Some(UnwindInfo::SystemV(
242 crate::isa::unwind::systemv::create_unwind_info_from_insts(
243 &buffer.unwind_info[..],
244 buffer.data().len(),
245 &mapper,
246 )?,
247 ))
248 }
249 #[cfg(feature = "unwind")]
250 UnwindInfoKind::Windows => Some(UnwindInfo::WindowsX64(
251 crate::isa::unwind::winx64::create_unwind_info_from_insts::<
252 self::inst::unwind::winx64::RegisterMapper,
253 >(&buffer.unwind_info[..])?,
254 )),
255 _ => None,
256 })
257}
258
259impl fmt::Display for X64Backend {
260 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
261 f.debug_struct("MachBackend")
262 .field("name", &self.name())
263 .field("triple", &self.triple())
264 .field("flags", &format!("{}", self.flags()))
265 .finish()
266 }
267}
268
269pub(crate) fn isa_builder(triple: Triple) -> IsaBuilder {
271 IsaBuilder {
272 triple,
273 setup: x64_settings::builder(),
274 constructor: isa_constructor,
275 }
276}
277
278fn isa_constructor(
279 triple: Triple,
280 shared_flags: Flags,
281 builder: &shared_settings::Builder,
282) -> CodegenResult<OwnedTargetIsa> {
283 let isa_flags = x64_settings::Flags::new(&shared_flags, builder);
284 let backend = X64Backend::new_with_flags(triple, shared_flags, isa_flags)?;
285 Ok(backend.wrapped())
286}