1 //! In-memory representation of compiled machine code, with labels and fixups to 2 //! refer to those labels. Handles constant-pool island insertion and also 3 //! veneer insertion for out-of-range jumps. 4 //! 5 //! This code exists to solve three problems: 6 //! 7 //! - Branch targets for forward branches are not known until later, when we 8 //! emit code in a single pass through the instruction structs. 9 //! 10 //! - On many architectures, address references or offsets have limited range. 11 //! For example, on AArch64, conditional branches can only target code +/- 1MB 12 //! from the branch itself. 13 //! 14 //! - The lowering of control flow from the CFG-with-edges produced by 15 //! [BlockLoweringOrder](super::BlockLoweringOrder), combined with many empty 16 //! edge blocks when the register allocator does not need to insert any 17 //! spills/reloads/moves in edge blocks, results in many suboptimal branch 18 //! patterns. The lowering also pays no attention to block order, and so 19 //! two-target conditional forms (cond-br followed by uncond-br) can often by 20 //! avoided because one of the targets is the fallthrough. There are several 21 //! cases here where we can simplify to use fewer branches. 22 //! 23 //! This "buffer" implements a single-pass code emission strategy (with a later 24 //! "fixup" pass, but only through recorded fixups, not all instructions). The 25 //! basic idea is: 26 //! 27 //! - Emit branches as they are, including two-target (cond/uncond) compound 28 //! forms, but with zero offsets and optimistically assuming the target will be 29 //! in range. Record the "fixup" for later. Targets are denoted instead by 30 //! symbolic "labels" that are then bound to certain offsets in the buffer as 31 //! we emit code. (Nominally, there is a label at the start of every basic 32 //! block.) 33 //! 34 //! - As we do this, track the offset in the buffer at which the first label 35 //! reference "goes out of range". We call this the "deadline". If we reach the 36 //! deadline and we still have not bound the label to which an unresolved branch 37 //! refers, we have a problem! 38 //! 39 //! - To solve this problem, we emit "islands" full of "veneers". An island is 40 //! simply a chunk of code inserted in the middle of the code actually produced 41 //! by the emitter (e.g., vcode iterating over instruction structs). The emitter 42 //! has some awareness of this: it either asks for an island between blocks, so 43 //! it is not accidentally executed, or else it emits a branch around the island 44 //! when all other options fail (see `Inst::EmitIsland` meta-instruction). 45 //! 46 //! - A "veneer" is an instruction (or sequence of instructions) in an "island" 47 //! that implements a longer-range reference to a label. The idea is that, for 48 //! example, a branch with a limited range can branch to a "veneer" instead, 49 //! which is simply a branch in a form that can use a longer-range reference. On 50 //! AArch64, for example, conditionals have a +/- 1 MB range, but a conditional 51 //! can branch to an unconditional branch which has a +/- 128 MB range. Hence, a 52 //! conditional branch's label reference can be fixed up with a "veneer" to 53 //! achieve a longer range. 54 //! 55 //! - To implement all of this, we require the backend to provide a `LabelUse` 56 //! type that implements a trait. This is nominally an enum that records one of 57 //! several kinds of references to an offset in code -- basically, a relocation 58 //! type -- and will usually correspond to different instruction formats. The 59 //! `LabelUse` implementation specifies the maximum range, how to patch in the 60 //! actual label location when known, and how to generate a veneer to extend the 61 //! range. 62 //! 63 //! That satisfies label references, but we still may have suboptimal branch 64 //! patterns. To clean up the branches, we do a simple "peephole"-style 65 //! optimization on the fly. To do so, the emitter (e.g., `Inst::emit()`) 66 //! informs the buffer of branches in the code and, in the case of conditionals, 67 //! the code that would have been emitted to invert this branch's condition. We 68 //! track the "latest branches": these are branches that are contiguous up to 69 //! the current offset. (If any code is emitted after a branch, that branch or 70 //! run of contiguous branches is no longer "latest".) The latest branches are 71 //! those that we can edit by simply truncating the buffer and doing something 72 //! else instead. 73 //! 74 //! To optimize branches, we implement several simple rules, and try to apply 75 //! them to the "latest branches" when possible: 76 //! 77 //! - A branch with a label target, when that label is bound to the ending 78 //! offset of the branch (the fallthrough location), can be removed altogether, 79 //! because the branch would have no effect). 80 //! 81 //! - An unconditional branch that starts at a label location, and branches to 82 //! another label, results in a "label alias": all references to the label bound 83 //! *to* this branch instruction are instead resolved to the *target* of the 84 //! branch instruction. This effectively removes empty blocks that just 85 //! unconditionally branch to the next block. We call this "branch threading". 86 //! 87 //! - A conditional followed by an unconditional, when the conditional branches 88 //! to the unconditional's fallthrough, results in (i) the truncation of the 89 //! unconditional, (ii) the inversion of the condition's condition, and (iii) 90 //! replacement of the conditional's target (using the original target of the 91 //! unconditional). This is a fancy way of saying "we can flip a two-target 92 //! conditional branch's taken/not-taken targets if it works better with our 93 //! fallthrough". To make this work, the emitter actually gives the buffer 94 //! *both* forms of every conditional branch: the true form is emitted into the 95 //! buffer, and the "inverted" machine-code bytes are provided as part of the 96 //! branch-fixup metadata. 97 //! 98 //! - An unconditional B preceded by another unconditional P, when B's label(s) have 99 //! been redirected to target(B), can be removed entirely. This is an extension 100 //! of the branch-threading optimization, and is valid because if we know there 101 //! will be no fallthrough into this branch instruction (the prior instruction 102 //! is an unconditional jump), and if we know we have successfully redirected 103 //! all labels, then this branch instruction is unreachable. Note that this 104 //! works because the redirection happens before the label is ever resolved 105 //! (fixups happen at island emission time, at which point latest-branches are 106 //! cleared, or at the end of emission), so we are sure to catch and redirect 107 //! all possible paths to this instruction. 108 //! 109 //! # Branch-optimization Correctness 110 //! 111 //! The branch-optimization mechanism depends on a few data structures with 112 //! invariants, which are always held outside the scope of top-level public 113 //! methods: 114 //! 115 //! - The latest-branches list. Each entry describes a span of the buffer 116 //! (start/end offsets), the label target, the corresponding fixup-list entry 117 //! index, and the bytes (must be the same length) for the inverted form, if 118 //! conditional. The list of labels that are bound to the start-offset of this 119 //! branch is *complete* (if any label has a resolved offset equal to `start` 120 //! and is not an alias, it must appear in this list) and *precise* (no label 121 //! in this list can be bound to another offset). No label in this list should 122 //! be an alias. No two branch ranges can overlap, and branches are in 123 //! ascending-offset order. 124 //! 125 //! - The labels-at-tail list. This contains all MachLabels that have been bound 126 //! to (whose resolved offsets are equal to) the tail offset of the buffer. 127 //! No label in this list should be an alias. 128 //! 129 //! - The label_offsets array, containing the bound offset of a label or 130 //! UNKNOWN. No label can be bound at an offset greater than the current 131 //! buffer tail. 132 //! 133 //! - The label_aliases array, containing another label to which a label is 134 //! bound or UNKNOWN. A label's resolved offset is the resolved offset 135 //! of the label it is aliased to, if this is set. 136 //! 137 //! We argue below, at each method, how the invariants in these data structures 138 //! are maintained (grep for "Post-invariant"). 139 //! 140 //! Given these invariants, we argue why each optimization preserves execution 141 //! semantics below (grep for "Preserves execution semantics"). 142 //! 143 //! # Avoiding Quadratic Behavior 144 //! 145 //! There are two cases where we've had to take some care to avoid 146 //! quadratic worst-case behavior: 147 //! 148 //! - The "labels at this branch" list can grow unboundedly if the 149 //! code generator binds many labels at one location. If the count 150 //! gets too high (defined by the `LABEL_LIST_THRESHOLD` constant), we 151 //! simply abort an optimization early in a way that is always correct 152 //! but is conservative. 153 //! 154 //! - The fixup list can interact with island emission to create 155 //! "quadratic island behvior". In a little more detail, one can hit 156 //! this behavior by having some pending fixups (forward label 157 //! references) with long-range label-use kinds, and some others 158 //! with shorter-range references that nonetheless still are pending 159 //! long enough to trigger island generation. In such a case, we 160 //! process the fixup list, generate veneers to extend some forward 161 //! references' ranges, but leave the other (longer-range) ones 162 //! alone. The way this was implemented put them back on a list and 163 //! resulted in quadratic behavior. 164 //! 165 //! To avoid this fixups are split into two lists: one "pending" list and one 166 //! final list. The pending list is kept around for handling fixups related to 167 //! branches so it can be edited/truncated. When an island is reached, which 168 //! starts processing fixups, all pending fixups are flushed into the final 169 //! list. The final list is a `BinaryHeap` which enables fixup processing to 170 //! only process those which are required during island emission, deferring 171 //! all longer-range fixups to later. 172 173 use crate::binemit::{Addend, CodeOffset, Reloc, StackMap}; 174 use crate::ir::function::FunctionParameters; 175 use crate::ir::{ExternalName, Opcode, RelSourceLoc, SourceLoc, TrapCode}; 176 use crate::isa::unwind::UnwindInst; 177 use crate::machinst::{ 178 BlockIndex, MachInstLabelUse, TextSectionBuilder, VCodeConstant, VCodeConstants, VCodeInst, 179 }; 180 use crate::trace; 181 use crate::{ir, MachInstEmitState}; 182 use crate::{timing, VCodeConstantData}; 183 use cranelift_control::ControlPlane; 184 use cranelift_entity::{entity_impl, PrimaryMap}; 185 use smallvec::SmallVec; 186 use std::cmp::Ordering; 187 use std::collections::BinaryHeap; 188 use std::mem; 189 use std::string::String; 190 use std::vec::Vec; 191 192 #[cfg(feature = "enable-serde")] 193 use serde::{Deserialize, Serialize}; 194 195 #[cfg(feature = "enable-serde")] 196 pub trait CompilePhase { 197 type MachSrcLocType: for<'a> Deserialize<'a> + Serialize + core::fmt::Debug + PartialEq + Clone; 198 type SourceLocType: for<'a> Deserialize<'a> + Serialize + core::fmt::Debug + PartialEq + Clone; 199 } 200 201 #[cfg(not(feature = "enable-serde"))] 202 pub trait CompilePhase { 203 type MachSrcLocType: core::fmt::Debug + PartialEq + Clone; 204 type SourceLocType: core::fmt::Debug + PartialEq + Clone; 205 } 206 207 /// Status of a compiled artifact that needs patching before being used. 208 #[derive(Clone, Debug, PartialEq)] 209 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 210 pub struct Stencil; 211 212 /// Status of a compiled artifact ready to use. 213 #[derive(Clone, Debug, PartialEq)] 214 pub struct Final; 215 216 impl CompilePhase for Stencil { 217 type MachSrcLocType = MachSrcLoc<Stencil>; 218 type SourceLocType = RelSourceLoc; 219 } 220 221 impl CompilePhase for Final { 222 type MachSrcLocType = MachSrcLoc<Final>; 223 type SourceLocType = SourceLoc; 224 } 225 226 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 227 enum ForceVeneers { 228 Yes, 229 No, 230 } 231 232 /// A buffer of output to be produced, fixed up, and then emitted to a CodeSink 233 /// in bulk. 234 /// 235 /// This struct uses `SmallVec`s to support small-ish function bodies without 236 /// any heap allocation. As such, it will be several kilobytes large. This is 237 /// likely fine as long as it is stack-allocated for function emission then 238 /// thrown away; but beware if many buffer objects are retained persistently. 239 pub struct MachBuffer<I: VCodeInst> { 240 /// The buffer contents, as raw bytes. 241 data: SmallVec<[u8; 1024]>, 242 /// Any relocations referring to this code. Note that only *external* 243 /// relocations are tracked here; references to labels within the buffer are 244 /// resolved before emission. 245 relocs: SmallVec<[MachReloc; 16]>, 246 /// Any trap records referring to this code. 247 traps: SmallVec<[MachTrap; 16]>, 248 /// Any call site records referring to this code. 249 call_sites: SmallVec<[MachCallSite; 16]>, 250 /// Any source location mappings referring to this code. 251 srclocs: SmallVec<[MachSrcLoc<Stencil>; 64]>, 252 /// Any stack maps referring to this code. 253 stack_maps: SmallVec<[MachStackMap; 8]>, 254 /// Any user stack maps for this code. 255 /// 256 /// Each entry is an `(offset, span, stack_map)` triple. Entries are sorted 257 /// by code offset, and each stack map covers `span` bytes on the stack. 258 user_stack_maps: SmallVec<[(CodeOffset, u32, ir::UserStackMap); 8]>, 259 /// Any unwind info at a given location. 260 unwind_info: SmallVec<[(CodeOffset, UnwindInst); 8]>, 261 /// The current source location in progress (after `start_srcloc()` and 262 /// before `end_srcloc()`). This is a (start_offset, src_loc) tuple. 263 cur_srcloc: Option<(CodeOffset, RelSourceLoc)>, 264 /// Known label offsets; `UNKNOWN_LABEL_OFFSET` if unknown. 265 label_offsets: SmallVec<[CodeOffset; 16]>, 266 /// Label aliases: when one label points to an unconditional jump, and that 267 /// jump points to another label, we can redirect references to the first 268 /// label immediately to the second. 269 /// 270 /// Invariant: we don't have label-alias cycles. We ensure this by, 271 /// before setting label A to alias label B, resolving B's alias 272 /// target (iteratively until a non-aliased label); if B is already 273 /// aliased to A, then we cannot alias A back to B. 274 label_aliases: SmallVec<[MachLabel; 16]>, 275 /// Constants that must be emitted at some point. 276 pending_constants: SmallVec<[VCodeConstant; 16]>, 277 /// Byte size of all constants in `pending_constants`. 278 pending_constants_size: CodeOffset, 279 /// Traps that must be emitted at some point. 280 pending_traps: SmallVec<[MachLabelTrap; 16]>, 281 /// Fixups that haven't yet been flushed into `fixup_records` below and may 282 /// be related to branches that are chomped. These all get added to 283 /// `fixup_records` during island emission. 284 pending_fixup_records: SmallVec<[MachLabelFixup<I>; 16]>, 285 /// The nearest upcoming deadline for entries in `pending_fixup_records`. 286 pending_fixup_deadline: CodeOffset, 287 /// Fixups that must be performed after all code is emitted. 288 fixup_records: BinaryHeap<MachLabelFixup<I>>, 289 /// Latest branches, to facilitate in-place editing for better fallthrough 290 /// behavior and empty-block removal. 291 latest_branches: SmallVec<[MachBranch; 4]>, 292 /// All labels at the current offset (emission tail). This is lazily 293 /// cleared: it is actually accurate as long as the current offset is 294 /// `labels_at_tail_off`, but if `cur_offset()` has grown larger, it should 295 /// be considered as empty. 296 /// 297 /// For correctness, this *must* be complete (i.e., the vector must contain 298 /// all labels whose offsets are resolved to the current tail), because we 299 /// rely on it to update labels when we truncate branches. 300 labels_at_tail: SmallVec<[MachLabel; 4]>, 301 /// The last offset at which `labels_at_tail` is valid. It is conceptually 302 /// always describing the tail of the buffer, but we do not clear 303 /// `labels_at_tail` eagerly when the tail grows, rather we lazily clear it 304 /// when the offset has grown past this (`labels_at_tail_off`) point. 305 /// Always <= `cur_offset()`. 306 labels_at_tail_off: CodeOffset, 307 /// Metadata about all constants that this function has access to. 308 /// 309 /// This records the size/alignment of all constants (not the actual data) 310 /// along with the last available label generated for the constant. This map 311 /// is consulted when constants are referred to and the label assigned to a 312 /// constant may change over time as well. 313 constants: PrimaryMap<VCodeConstant, MachBufferConstant>, 314 /// All recorded usages of constants as pairs of the constant and where the 315 /// constant needs to be placed within `self.data`. Note that the same 316 /// constant may appear in this array multiple times if it was emitted 317 /// multiple times. 318 used_constants: SmallVec<[(VCodeConstant, CodeOffset); 4]>, 319 /// Indicates when a patchable region is currently open, to guard that it's 320 /// not possible to nest patchable regions. 321 open_patchable: bool, 322 } 323 324 impl MachBufferFinalized<Stencil> { 325 /// Get a finalized machine buffer by applying the function's base source location. 326 pub fn apply_base_srcloc(self, base_srcloc: SourceLoc) -> MachBufferFinalized<Final> { 327 MachBufferFinalized { 328 data: self.data, 329 relocs: self.relocs, 330 traps: self.traps, 331 call_sites: self.call_sites, 332 srclocs: self 333 .srclocs 334 .into_iter() 335 .map(|srcloc| srcloc.apply_base_srcloc(base_srcloc)) 336 .collect(), 337 stack_maps: self.stack_maps, 338 user_stack_maps: self.user_stack_maps, 339 unwind_info: self.unwind_info, 340 alignment: self.alignment, 341 } 342 } 343 } 344 345 /// A `MachBuffer` once emission is completed: holds generated code and records, 346 /// without fixups. This allows the type to be independent of the backend. 347 #[derive(PartialEq, Debug, Clone)] 348 #[cfg_attr( 349 feature = "enable-serde", 350 derive(serde_derive::Serialize, serde_derive::Deserialize) 351 )] 352 pub struct MachBufferFinalized<T: CompilePhase> { 353 /// The buffer contents, as raw bytes. 354 pub(crate) data: SmallVec<[u8; 1024]>, 355 /// Any relocations referring to this code. Note that only *external* 356 /// relocations are tracked here; references to labels within the buffer are 357 /// resolved before emission. 358 pub(crate) relocs: SmallVec<[FinalizedMachReloc; 16]>, 359 /// Any trap records referring to this code. 360 pub(crate) traps: SmallVec<[MachTrap; 16]>, 361 /// Any call site records referring to this code. 362 pub(crate) call_sites: SmallVec<[MachCallSite; 16]>, 363 /// Any source location mappings referring to this code. 364 pub(crate) srclocs: SmallVec<[T::MachSrcLocType; 64]>, 365 /// Any stack maps referring to this code. 366 pub(crate) stack_maps: SmallVec<[MachStackMap; 8]>, 367 /// Any user stack maps for this code. 368 /// 369 /// Each entry is an `(offset, span, stack_map)` triple. Entries are sorted 370 /// by code offset, and each stack map covers `span` bytes on the stack. 371 pub(crate) user_stack_maps: SmallVec<[(CodeOffset, u32, ir::UserStackMap); 8]>, 372 /// Any unwind info at a given location. 373 pub unwind_info: SmallVec<[(CodeOffset, UnwindInst); 8]>, 374 /// The required alignment of this buffer. 375 pub alignment: u32, 376 } 377 378 const UNKNOWN_LABEL_OFFSET: CodeOffset = 0xffff_ffff; 379 const UNKNOWN_LABEL: MachLabel = MachLabel(0xffff_ffff); 380 381 /// Threshold on max length of `labels_at_this_branch` list to avoid 382 /// unbounded quadratic behavior (see comment below at use-site). 383 const LABEL_LIST_THRESHOLD: usize = 100; 384 385 /// A label refers to some offset in a `MachBuffer`. It may not be resolved at 386 /// the point at which it is used by emitted code; the buffer records "fixups" 387 /// for references to the label, and will come back and patch the code 388 /// appropriately when the label's location is eventually known. 389 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] 390 pub struct MachLabel(u32); 391 entity_impl!(MachLabel); 392 393 impl MachLabel { 394 /// Get a label for a block. (The first N MachLabels are always reserved for 395 /// the N blocks in the vcode.) 396 pub fn from_block(bindex: BlockIndex) -> MachLabel { 397 MachLabel(bindex.index() as u32) 398 } 399 400 /// Get the numeric label index. 401 pub fn get(self) -> u32 { 402 self.0 403 } 404 405 /// Creates a string representing this label, for convenience. 406 pub fn to_string(&self) -> String { 407 format!("label{}", self.0) 408 } 409 } 410 411 impl Default for MachLabel { 412 fn default() -> Self { 413 UNKNOWN_LABEL 414 } 415 } 416 417 /// A stack map extent, when creating a stack map. 418 pub enum StackMapExtent { 419 /// The stack map starts at this instruction, and ends after the number of upcoming bytes 420 /// (note: this is a code offset diff). 421 UpcomingBytes(CodeOffset), 422 423 /// The stack map started at the given offset and ends at the current one. This helps 424 /// architectures where the instruction size has not a fixed length. 425 StartedAtOffset(CodeOffset), 426 } 427 428 /// Represents the beginning of an editable region in the [`MachBuffer`], while code emission is 429 /// still occurring. An [`OpenPatchRegion`] is closed by [`MachBuffer::end_patchable`], consuming 430 /// the [`OpenPatchRegion`] token in the process. 431 pub struct OpenPatchRegion(usize); 432 433 /// A region in the [`MachBuffer`] code buffer that can be edited prior to finalization. An example 434 /// of where you might want to use this is for patching instructions that mention constants that 435 /// won't be known until later: [`MachBuffer::start_patchable`] can be used to begin the patchable 436 /// region, instructions can be emitted with placeholder constants, and the [`PatchRegion`] token 437 /// can be produced by [`MachBuffer::end_patchable`]. Once the values of those constants are known, 438 /// the [`PatchRegion::patch`] function can be used to get a mutable buffer to the instruction 439 /// bytes, and the constants uses can be updated directly. 440 pub struct PatchRegion { 441 range: std::ops::Range<usize>, 442 } 443 444 impl PatchRegion { 445 /// Consume the patch region to yield a mutable slice of the [`MachBuffer`] data buffer. 446 pub fn patch<I: VCodeInst>(self, buffer: &mut MachBuffer<I>) -> &mut [u8] { 447 &mut buffer.data[self.range] 448 } 449 } 450 451 impl<I: VCodeInst> MachBuffer<I> { 452 /// Create a new section, known to start at `start_offset` and with a size limited to 453 /// `length_limit`. 454 pub fn new() -> MachBuffer<I> { 455 MachBuffer { 456 data: SmallVec::new(), 457 relocs: SmallVec::new(), 458 traps: SmallVec::new(), 459 call_sites: SmallVec::new(), 460 srclocs: SmallVec::new(), 461 stack_maps: SmallVec::new(), 462 user_stack_maps: SmallVec::new(), 463 unwind_info: SmallVec::new(), 464 cur_srcloc: None, 465 label_offsets: SmallVec::new(), 466 label_aliases: SmallVec::new(), 467 pending_constants: SmallVec::new(), 468 pending_constants_size: 0, 469 pending_traps: SmallVec::new(), 470 pending_fixup_records: SmallVec::new(), 471 pending_fixup_deadline: u32::MAX, 472 fixup_records: Default::default(), 473 latest_branches: SmallVec::new(), 474 labels_at_tail: SmallVec::new(), 475 labels_at_tail_off: 0, 476 constants: Default::default(), 477 used_constants: Default::default(), 478 open_patchable: false, 479 } 480 } 481 482 /// Current offset from start of buffer. 483 pub fn cur_offset(&self) -> CodeOffset { 484 self.data.len() as CodeOffset 485 } 486 487 /// Add a byte. 488 pub fn put1(&mut self, value: u8) { 489 self.data.push(value); 490 491 // Post-invariant: conceptual-labels_at_tail contains a complete and 492 // precise list of labels bound at `cur_offset()`. We have advanced 493 // `cur_offset()`, hence if it had been equal to `labels_at_tail_off` 494 // before, it is not anymore (and it cannot become equal, because 495 // `labels_at_tail_off` is always <= `cur_offset()`). Thus the list is 496 // conceptually empty (even though it is only lazily cleared). No labels 497 // can be bound at this new offset (by invariant on `label_offsets`). 498 // Hence the invariant holds. 499 } 500 501 /// Add 2 bytes. 502 pub fn put2(&mut self, value: u16) { 503 let bytes = value.to_le_bytes(); 504 self.data.extend_from_slice(&bytes[..]); 505 506 // Post-invariant: as for `put1()`. 507 } 508 509 /// Add 4 bytes. 510 pub fn put4(&mut self, value: u32) { 511 let bytes = value.to_le_bytes(); 512 self.data.extend_from_slice(&bytes[..]); 513 514 // Post-invariant: as for `put1()`. 515 } 516 517 /// Add 8 bytes. 518 pub fn put8(&mut self, value: u64) { 519 let bytes = value.to_le_bytes(); 520 self.data.extend_from_slice(&bytes[..]); 521 522 // Post-invariant: as for `put1()`. 523 } 524 525 /// Add a slice of bytes. 526 pub fn put_data(&mut self, data: &[u8]) { 527 self.data.extend_from_slice(data); 528 529 // Post-invariant: as for `put1()`. 530 } 531 532 /// Reserve appended space and return a mutable slice referring to it. 533 pub fn get_appended_space(&mut self, len: usize) -> &mut [u8] { 534 let off = self.data.len(); 535 let new_len = self.data.len() + len; 536 self.data.resize(new_len, 0); 537 &mut self.data[off..] 538 539 // Post-invariant: as for `put1()`. 540 } 541 542 /// Align up to the given alignment. 543 pub fn align_to(&mut self, align_to: CodeOffset) { 544 trace!("MachBuffer: align to {}", align_to); 545 assert!( 546 align_to.is_power_of_two(), 547 "{} is not a power of two", 548 align_to 549 ); 550 while self.cur_offset() & (align_to - 1) != 0 { 551 self.put1(0); 552 } 553 554 // Post-invariant: as for `put1()`. 555 } 556 557 /// Begin a region of patchable code. There is one requirement for the 558 /// code that is emitted: It must not introduce any instructions that 559 /// could be chomped (branches are an example of this). In other words, 560 /// you must not call [`MachBuffer::add_cond_branch`] or 561 /// [`MachBuffer::add_uncond_branch`] between calls to this method and 562 /// [`MachBuffer::end_patchable`]. 563 pub fn start_patchable(&mut self) -> OpenPatchRegion { 564 assert!(!self.open_patchable, "Patchable regions may not be nested"); 565 self.open_patchable = true; 566 OpenPatchRegion(usize::try_from(self.cur_offset()).unwrap()) 567 } 568 569 /// End a region of patchable code, yielding a [`PatchRegion`] value that 570 /// can be consumed later to produce a one-off mutable slice to the 571 /// associated region of the data buffer. 572 pub fn end_patchable(&mut self, open: OpenPatchRegion) -> PatchRegion { 573 // No need to assert the state of `open_patchable` here, as we take 574 // ownership of the only `OpenPatchable` value. 575 self.open_patchable = false; 576 let end = usize::try_from(self.cur_offset()).unwrap(); 577 PatchRegion { range: open.0..end } 578 } 579 580 /// Allocate a `Label` to refer to some offset. May not be bound to a fixed 581 /// offset yet. 582 pub fn get_label(&mut self) -> MachLabel { 583 let l = self.label_offsets.len() as u32; 584 self.label_offsets.push(UNKNOWN_LABEL_OFFSET); 585 self.label_aliases.push(UNKNOWN_LABEL); 586 trace!("MachBuffer: new label -> {:?}", MachLabel(l)); 587 MachLabel(l) 588 589 // Post-invariant: the only mutation is to add a new label; it has no 590 // bound offset yet, so it trivially satisfies all invariants. 591 } 592 593 /// Reserve the first N MachLabels for blocks. 594 pub fn reserve_labels_for_blocks(&mut self, blocks: usize) { 595 trace!("MachBuffer: first {} labels are for blocks", blocks); 596 debug_assert!(self.label_offsets.is_empty()); 597 self.label_offsets.resize(blocks, UNKNOWN_LABEL_OFFSET); 598 self.label_aliases.resize(blocks, UNKNOWN_LABEL); 599 600 // Post-invariant: as for `get_label()`. 601 } 602 603 /// Registers metadata in this `MachBuffer` about the `constants` provided. 604 /// 605 /// This will record the size/alignment of all constants which will prepare 606 /// them for emission later on. 607 pub fn register_constants(&mut self, constants: &VCodeConstants) { 608 for (c, val) in constants.iter() { 609 self.register_constant(&c, val); 610 } 611 } 612 613 /// Similar to [`MachBuffer::register_constants`] but registers a 614 /// single constant metadata. This function is useful in 615 /// situations where not all constants are known at the time of 616 /// emission. 617 pub fn register_constant(&mut self, constant: &VCodeConstant, data: &VCodeConstantData) { 618 let c2 = self.constants.push(MachBufferConstant { 619 upcoming_label: None, 620 align: data.alignment(), 621 size: data.as_slice().len(), 622 }); 623 assert_eq!(*constant, c2); 624 } 625 626 /// Completes constant emission by iterating over `self.used_constants` and 627 /// filling in the "holes" with the constant values provided by `constants`. 628 /// 629 /// Returns the alignment required for this entire buffer. Alignment starts 630 /// at the ISA's minimum function alignment and can be increased due to 631 /// constant requirements. 632 fn finish_constants(&mut self, constants: &VCodeConstants) -> u32 { 633 let mut alignment = I::function_alignment().minimum; 634 for (constant, offset) in mem::take(&mut self.used_constants) { 635 let constant = constants.get(constant); 636 let data = constant.as_slice(); 637 self.data[offset as usize..][..data.len()].copy_from_slice(data); 638 alignment = constant.alignment().max(alignment); 639 } 640 alignment 641 } 642 643 /// Returns a label that can be used to refer to the `constant` provided. 644 /// 645 /// This will automatically defer a new constant to be emitted for 646 /// `constant` if it has not been previously emitted. Note that this 647 /// function may return a different label for the same constant at 648 /// different points in time. The label is valid to use only from the 649 /// current location; the MachBuffer takes care to emit the same constant 650 /// multiple times if needed so the constant is always in range. 651 pub fn get_label_for_constant(&mut self, constant: VCodeConstant) -> MachLabel { 652 let MachBufferConstant { 653 align, 654 size, 655 upcoming_label, 656 } = self.constants[constant]; 657 if let Some(label) = upcoming_label { 658 return label; 659 } 660 661 let label = self.get_label(); 662 trace!( 663 "defer constant: eventually emit {size} bytes aligned \ 664 to {align} at label {label:?}", 665 ); 666 self.pending_constants.push(constant); 667 self.pending_constants_size += size as u32; 668 self.constants[constant].upcoming_label = Some(label); 669 label 670 } 671 672 /// Bind a label to the current offset. A label can only be bound once. 673 pub fn bind_label(&mut self, label: MachLabel, ctrl_plane: &mut ControlPlane) { 674 trace!( 675 "MachBuffer: bind label {:?} at offset {}", 676 label, 677 self.cur_offset() 678 ); 679 debug_assert_eq!(self.label_offsets[label.0 as usize], UNKNOWN_LABEL_OFFSET); 680 debug_assert_eq!(self.label_aliases[label.0 as usize], UNKNOWN_LABEL); 681 let offset = self.cur_offset(); 682 self.label_offsets[label.0 as usize] = offset; 683 self.lazily_clear_labels_at_tail(); 684 self.labels_at_tail.push(label); 685 686 // Invariants hold: bound offset of label is <= cur_offset (in fact it 687 // is equal). If the `labels_at_tail` list was complete and precise 688 // before, it is still, because we have bound this label to the current 689 // offset and added it to the list (which contains all labels at the 690 // current offset). 691 692 self.optimize_branches(ctrl_plane); 693 694 // Post-invariant: by `optimize_branches()` (see argument there). 695 } 696 697 /// Lazily clear `labels_at_tail` if the tail offset has moved beyond the 698 /// offset that it applies to. 699 fn lazily_clear_labels_at_tail(&mut self) { 700 let offset = self.cur_offset(); 701 if offset > self.labels_at_tail_off { 702 self.labels_at_tail_off = offset; 703 self.labels_at_tail.clear(); 704 } 705 706 // Post-invariant: either labels_at_tail_off was at cur_offset, and 707 // state is untouched, or was less than cur_offset, in which case the 708 // labels_at_tail list was conceptually empty, and is now actually 709 // empty. 710 } 711 712 /// Resolve a label to an offset, if known. May return `UNKNOWN_LABEL_OFFSET`. 713 pub(crate) fn resolve_label_offset(&self, mut label: MachLabel) -> CodeOffset { 714 let mut iters = 0; 715 while self.label_aliases[label.0 as usize] != UNKNOWN_LABEL { 716 label = self.label_aliases[label.0 as usize]; 717 // To protect against an infinite loop (despite our assurances to 718 // ourselves that the invariants make this impossible), assert out 719 // after 1M iterations. The number of basic blocks is limited 720 // in most contexts anyway so this should be impossible to hit with 721 // a legitimate input. 722 iters += 1; 723 assert!(iters < 1_000_000, "Unexpected cycle in label aliases"); 724 } 725 self.label_offsets[label.0 as usize] 726 727 // Post-invariant: no mutations. 728 } 729 730 /// Emit a reference to the given label with the given reference type (i.e., 731 /// branch-instruction format) at the current offset. This is like a 732 /// relocation, but handled internally. 733 /// 734 /// This can be called before the branch is actually emitted; fixups will 735 /// not happen until an island is emitted or the buffer is finished. 736 pub fn use_label_at_offset(&mut self, offset: CodeOffset, label: MachLabel, kind: I::LabelUse) { 737 trace!( 738 "MachBuffer: use_label_at_offset: offset {} label {:?} kind {:?}", 739 offset, 740 label, 741 kind 742 ); 743 744 // Add the fixup, and update the worst-case island size based on a 745 // veneer for this label use. 746 let fixup = MachLabelFixup { 747 label, 748 offset, 749 kind, 750 }; 751 self.pending_fixup_deadline = self.pending_fixup_deadline.min(fixup.deadline()); 752 self.pending_fixup_records.push(fixup); 753 754 // Post-invariant: no mutations to branches/labels data structures. 755 } 756 757 /// Inform the buffer of an unconditional branch at the given offset, 758 /// targeting the given label. May be used to optimize branches. 759 /// The last added label-use must correspond to this branch. 760 /// This must be called when the current offset is equal to `start`; i.e., 761 /// before actually emitting the branch. This implies that for a branch that 762 /// uses a label and is eligible for optimizations by the MachBuffer, the 763 /// proper sequence is: 764 /// 765 /// - Call `use_label_at_offset()` to emit the fixup record. 766 /// - Call `add_uncond_branch()` to make note of the branch. 767 /// - Emit the bytes for the branch's machine code. 768 /// 769 /// Additional requirement: no labels may be bound between `start` and `end` 770 /// (exclusive on both ends). 771 pub fn add_uncond_branch(&mut self, start: CodeOffset, end: CodeOffset, target: MachLabel) { 772 debug_assert!( 773 !self.open_patchable, 774 "Branch instruction inserted within a patchable region" 775 ); 776 assert!(self.cur_offset() == start); 777 debug_assert!(end > start); 778 assert!(!self.pending_fixup_records.is_empty()); 779 let fixup = self.pending_fixup_records.len() - 1; 780 self.lazily_clear_labels_at_tail(); 781 self.latest_branches.push(MachBranch { 782 start, 783 end, 784 target, 785 fixup, 786 inverted: None, 787 labels_at_this_branch: self.labels_at_tail.clone(), 788 }); 789 790 // Post-invariant: we asserted branch start is current tail; the list of 791 // labels at branch is cloned from list of labels at current tail. 792 } 793 794 /// Inform the buffer of a conditional branch at the given offset, 795 /// targeting the given label. May be used to optimize branches. 796 /// The last added label-use must correspond to this branch. 797 /// 798 /// Additional requirement: no labels may be bound between `start` and `end` 799 /// (exclusive on both ends). 800 pub fn add_cond_branch( 801 &mut self, 802 start: CodeOffset, 803 end: CodeOffset, 804 target: MachLabel, 805 inverted: &[u8], 806 ) { 807 debug_assert!( 808 !self.open_patchable, 809 "Branch instruction inserted within a patchable region" 810 ); 811 assert!(self.cur_offset() == start); 812 debug_assert!(end > start); 813 assert!(!self.pending_fixup_records.is_empty()); 814 debug_assert!(inverted.len() == (end - start) as usize); 815 let fixup = self.pending_fixup_records.len() - 1; 816 let inverted = Some(SmallVec::from(inverted)); 817 self.lazily_clear_labels_at_tail(); 818 self.latest_branches.push(MachBranch { 819 start, 820 end, 821 target, 822 fixup, 823 inverted, 824 labels_at_this_branch: self.labels_at_tail.clone(), 825 }); 826 827 // Post-invariant: we asserted branch start is current tail; labels at 828 // branch list is cloned from list of labels at current tail. 829 } 830 831 fn truncate_last_branch(&mut self) { 832 debug_assert!( 833 !self.open_patchable, 834 "Branch instruction truncated within a patchable region" 835 ); 836 837 self.lazily_clear_labels_at_tail(); 838 // Invariants hold at this point. 839 840 let b = self.latest_branches.pop().unwrap(); 841 assert!(b.end == self.cur_offset()); 842 843 // State: 844 // [PRE CODE] 845 // Offset b.start, b.labels_at_this_branch: 846 // [BRANCH CODE] 847 // cur_off, self.labels_at_tail --> 848 // (end of buffer) 849 self.data.truncate(b.start as usize); 850 self.pending_fixup_records.truncate(b.fixup); 851 while let Some(last_srcloc) = self.srclocs.last_mut() { 852 if last_srcloc.end <= b.start { 853 break; 854 } 855 if last_srcloc.start < b.start { 856 last_srcloc.end = b.start; 857 break; 858 } 859 self.srclocs.pop(); 860 } 861 // State: 862 // [PRE CODE] 863 // cur_off, Offset b.start, b.labels_at_this_branch: 864 // (end of buffer) 865 // 866 // self.labels_at_tail --> (past end of buffer) 867 let cur_off = self.cur_offset(); 868 self.labels_at_tail_off = cur_off; 869 // State: 870 // [PRE CODE] 871 // cur_off, Offset b.start, b.labels_at_this_branch, 872 // self.labels_at_tail: 873 // (end of buffer) 874 // 875 // resolve_label_offset(l) for l in labels_at_tail: 876 // (past end of buffer) 877 878 trace!( 879 "truncate_last_branch: truncated {:?}; off now {}", 880 b, 881 cur_off 882 ); 883 884 // Fix up resolved label offsets for labels at tail. 885 for &l in &self.labels_at_tail { 886 self.label_offsets[l.0 as usize] = cur_off; 887 } 888 // Old labels_at_this_branch are now at cur_off. 889 self.labels_at_tail 890 .extend(b.labels_at_this_branch.into_iter()); 891 892 // Post-invariant: this operation is defined to truncate the buffer, 893 // which moves cur_off backward, and to move labels at the end of the 894 // buffer back to the start-of-branch offset. 895 // 896 // latest_branches satisfies all invariants: 897 // - it has no branches past the end of the buffer (branches are in 898 // order, we removed the last one, and we truncated the buffer to just 899 // before the start of that branch) 900 // - no labels were moved to lower offsets than the (new) cur_off, so 901 // the labels_at_this_branch list for any other branch need not change. 902 // 903 // labels_at_tail satisfies all invariants: 904 // - all labels that were at the tail after the truncated branch are 905 // moved backward to just before the branch, which becomes the new tail; 906 // thus every element in the list should remain (ensured by `.extend()` 907 // above). 908 // - all labels that refer to the new tail, which is the start-offset of 909 // the truncated branch, must be present. The `labels_at_this_branch` 910 // list in the truncated branch's record is a complete and precise list 911 // of exactly these labels; we append these to labels_at_tail. 912 // - labels_at_tail_off is at cur_off after truncation occurs, so the 913 // list is valid (not to be lazily cleared). 914 // 915 // The stated operation was performed: 916 // - For each label at the end of the buffer prior to this method, it 917 // now resolves to the new (truncated) end of the buffer: it must have 918 // been in `labels_at_tail` (this list is precise and complete, and 919 // the tail was at the end of the truncated branch on entry), and we 920 // iterate over this list and set `label_offsets` to the new tail. 921 // None of these labels could have been an alias (by invariant), so 922 // `label_offsets` is authoritative for each. 923 // - No other labels will be past the end of the buffer, because of the 924 // requirement that no labels be bound to the middle of branch ranges 925 // (see comments to `add_{cond,uncond}_branch()`). 926 // - The buffer is truncated to just before the last branch, and the 927 // fixup record referring to that last branch is removed. 928 } 929 930 /// Performs various optimizations on branches pointing at the current label. 931 pub fn optimize_branches(&mut self, ctrl_plane: &mut ControlPlane) { 932 if ctrl_plane.get_decision() { 933 return; 934 } 935 936 self.lazily_clear_labels_at_tail(); 937 // Invariants valid at this point. 938 939 trace!( 940 "enter optimize_branches:\n b = {:?}\n l = {:?}\n f = {:?}", 941 self.latest_branches, 942 self.labels_at_tail, 943 self.pending_fixup_records 944 ); 945 946 // We continue to munch on branches at the tail of the buffer until no 947 // more rules apply. Note that the loop only continues if a branch is 948 // actually truncated (or if labels are redirected away from a branch), 949 // so this always makes progress. 950 while let Some(b) = self.latest_branches.last() { 951 let cur_off = self.cur_offset(); 952 trace!("optimize_branches: last branch {:?} at off {}", b, cur_off); 953 // If there has been any code emission since the end of the last branch or 954 // label definition, then there's nothing we can edit (because we 955 // don't move code once placed, only back up and overwrite), so 956 // clear the records and finish. 957 if b.end < cur_off { 958 break; 959 } 960 961 // If the "labels at this branch" list on this branch is 962 // longer than a threshold, don't do any simplification, 963 // and let the branch remain to separate those labels from 964 // the current tail. This avoids quadratic behavior (see 965 // #3468): otherwise, if a long string of "goto next; 966 // next:" patterns are emitted, all of the labels will 967 // coalesce into a long list of aliases for the current 968 // buffer tail. We must track all aliases of the current 969 // tail for correctness, but we are also allowed to skip 970 // optimization (removal) of any branch, so we take the 971 // escape hatch here and let it stand. In effect this 972 // "spreads" the many thousands of labels in the 973 // pathological case among an actual (harmless but 974 // suboptimal) instruction once per N labels. 975 if b.labels_at_this_branch.len() > LABEL_LIST_THRESHOLD { 976 break; 977 } 978 979 // Invariant: we are looking at a branch that ends at the tail of 980 // the buffer. 981 982 // For any branch, conditional or unconditional: 983 // - If the target is a label at the current offset, then remove 984 // the conditional branch, and reset all labels that targeted 985 // the current offset (end of branch) to the truncated 986 // end-of-code. 987 // 988 // Preserves execution semantics: a branch to its own fallthrough 989 // address is equivalent to a no-op; in both cases, nextPC is the 990 // fallthrough. 991 if self.resolve_label_offset(b.target) == cur_off { 992 trace!("branch with target == cur off; truncating"); 993 self.truncate_last_branch(); 994 continue; 995 } 996 997 // If latest is an unconditional branch: 998 // 999 // - If the branch's target is not its own start address, then for 1000 // each label at the start of branch, make the label an alias of the 1001 // branch target, and remove the label from the "labels at this 1002 // branch" list. 1003 // 1004 // - Preserves execution semantics: an unconditional branch's 1005 // only effect is to set PC to a new PC; this change simply 1006 // collapses one step in the step-semantics. 1007 // 1008 // - Post-invariant: the labels that were bound to the start of 1009 // this branch become aliases, so they must not be present in any 1010 // labels-at-this-branch list or the labels-at-tail list. The 1011 // labels are removed form the latest-branch record's 1012 // labels-at-this-branch list, and are never placed in the 1013 // labels-at-tail list. Furthermore, it is correct that they are 1014 // not in either list, because they are now aliases, and labels 1015 // that are aliases remain aliases forever. 1016 // 1017 // - If there is a prior unconditional branch that ends just before 1018 // this one begins, and this branch has no labels bound to its 1019 // start, then we can truncate this branch, because it is entirely 1020 // unreachable (we have redirected all labels that make it 1021 // reachable otherwise). Do so and continue around the loop. 1022 // 1023 // - Preserves execution semantics: the branch is unreachable, 1024 // because execution can only flow into an instruction from the 1025 // prior instruction's fallthrough or from a branch bound to that 1026 // instruction's start offset. Unconditional branches have no 1027 // fallthrough, so if the prior instruction is an unconditional 1028 // branch, no fallthrough entry can happen. The 1029 // labels-at-this-branch list is complete (by invariant), so if it 1030 // is empty, then the instruction is entirely unreachable. Thus, 1031 // it can be removed. 1032 // 1033 // - Post-invariant: ensured by truncate_last_branch(). 1034 // 1035 // - If there is a prior conditional branch whose target label 1036 // resolves to the current offset (branches around the 1037 // unconditional branch), then remove the unconditional branch, 1038 // and make the target of the unconditional the target of the 1039 // conditional instead. 1040 // 1041 // - Preserves execution semantics: previously we had: 1042 // 1043 // L1: 1044 // cond_br L2 1045 // br L3 1046 // L2: 1047 // (end of buffer) 1048 // 1049 // by removing the last branch, we have: 1050 // 1051 // L1: 1052 // cond_br L2 1053 // L2: 1054 // (end of buffer) 1055 // 1056 // we then fix up the records for the conditional branch to 1057 // have: 1058 // 1059 // L1: 1060 // cond_br.inverted L3 1061 // L2: 1062 // 1063 // In the original code, control flow reaches L2 when the 1064 // conditional branch's predicate is true, and L3 otherwise. In 1065 // the optimized code, the same is true. 1066 // 1067 // - Post-invariant: all edits to latest_branches and 1068 // labels_at_tail are performed by `truncate_last_branch()`, 1069 // which maintains the invariants at each step. 1070 1071 if b.is_uncond() { 1072 // Set any label equal to current branch's start as an alias of 1073 // the branch's target, if the target is not the branch itself 1074 // (i.e., an infinite loop). 1075 // 1076 // We cannot perform this aliasing if the target of this branch 1077 // ultimately aliases back here; if so, we need to keep this 1078 // branch, so break out of this loop entirely (and clear the 1079 // latest-branches list below). 1080 // 1081 // Note that this check is what prevents cycles from forming in 1082 // `self.label_aliases`. To see why, consider an arbitrary start 1083 // state: 1084 // 1085 // label_aliases[L1] = L2, label_aliases[L2] = L3, ..., up to 1086 // Ln, which is not aliased. 1087 // 1088 // We would create a cycle if we assigned label_aliases[Ln] 1089 // = L1. Note that the below assignment is the only write 1090 // to label_aliases. 1091 // 1092 // By our other invariants, we have that Ln (`l` below) 1093 // resolves to the offset `b.start`, because it is in the 1094 // set `b.labels_at_this_branch`. 1095 // 1096 // If L1 were already aliased, through some arbitrarily deep 1097 // chain, to Ln, then it must also resolve to this offset 1098 // `b.start`. 1099 // 1100 // By checking the resolution of `L1` against this offset, 1101 // and aborting this branch-simplification if they are 1102 // equal, we prevent the below assignment from ever creating 1103 // a cycle. 1104 if self.resolve_label_offset(b.target) != b.start { 1105 let redirected = b.labels_at_this_branch.len(); 1106 for &l in &b.labels_at_this_branch { 1107 trace!( 1108 " -> label at start of branch {:?} redirected to target {:?}", 1109 l, 1110 b.target 1111 ); 1112 self.label_aliases[l.0 as usize] = b.target; 1113 // NOTE: we continue to ensure the invariant that labels 1114 // pointing to tail of buffer are in `labels_at_tail` 1115 // because we already ensured above that the last branch 1116 // cannot have a target of `cur_off`; so we never have 1117 // to put the label into `labels_at_tail` when moving it 1118 // here. 1119 } 1120 // Maintain invariant: all branches have been redirected 1121 // and are no longer pointing at the start of this branch. 1122 let mut_b = self.latest_branches.last_mut().unwrap(); 1123 mut_b.labels_at_this_branch.clear(); 1124 1125 if redirected > 0 { 1126 trace!(" -> after label redirects, restarting loop"); 1127 continue; 1128 } 1129 } else { 1130 break; 1131 } 1132 1133 let b = self.latest_branches.last().unwrap(); 1134 1135 // Examine any immediately preceding branch. 1136 if self.latest_branches.len() > 1 { 1137 let prev_b = &self.latest_branches[self.latest_branches.len() - 2]; 1138 trace!(" -> more than one branch; prev_b = {:?}", prev_b); 1139 // This uncond is immediately after another uncond; we 1140 // should have already redirected labels to this uncond away 1141 // (but check to be sure); so we can truncate this uncond. 1142 if prev_b.is_uncond() 1143 && prev_b.end == b.start 1144 && b.labels_at_this_branch.is_empty() 1145 { 1146 trace!(" -> uncond follows another uncond; truncating"); 1147 self.truncate_last_branch(); 1148 continue; 1149 } 1150 1151 // This uncond is immediately after a conditional, and the 1152 // conditional's target is the end of this uncond, and we've 1153 // already redirected labels to this uncond away; so we can 1154 // truncate this uncond, flip the sense of the conditional, and 1155 // set the conditional's target (in `latest_branches` and in 1156 // `fixup_records`) to the uncond's target. 1157 if prev_b.is_cond() 1158 && prev_b.end == b.start 1159 && self.resolve_label_offset(prev_b.target) == cur_off 1160 { 1161 trace!(" -> uncond follows a conditional, and conditional's target resolves to current offset"); 1162 // Save the target of the uncond (this becomes the 1163 // target of the cond), and truncate the uncond. 1164 let target = b.target; 1165 let data = prev_b.inverted.clone().unwrap(); 1166 self.truncate_last_branch(); 1167 1168 // Mutate the code and cond branch. 1169 let off_before_edit = self.cur_offset(); 1170 let prev_b = self.latest_branches.last_mut().unwrap(); 1171 let not_inverted = SmallVec::from( 1172 &self.data[(prev_b.start as usize)..(prev_b.end as usize)], 1173 ); 1174 1175 // Low-level edit: replaces bytes of branch with 1176 // inverted form. cur_off remains the same afterward, so 1177 // we do not need to modify label data structures. 1178 self.data.truncate(prev_b.start as usize); 1179 self.data.extend_from_slice(&data[..]); 1180 1181 // Save the original code as the inversion of the 1182 // inverted branch, in case we later edit this branch 1183 // again. 1184 prev_b.inverted = Some(not_inverted); 1185 self.pending_fixup_records[prev_b.fixup].label = target; 1186 trace!(" -> reassigning target of condbr to {:?}", target); 1187 prev_b.target = target; 1188 debug_assert_eq!(off_before_edit, self.cur_offset()); 1189 continue; 1190 } 1191 } 1192 } 1193 1194 // If we couldn't do anything with the last branch, then break. 1195 break; 1196 } 1197 1198 self.purge_latest_branches(); 1199 1200 trace!( 1201 "leave optimize_branches:\n b = {:?}\n l = {:?}\n f = {:?}", 1202 self.latest_branches, 1203 self.labels_at_tail, 1204 self.pending_fixup_records 1205 ); 1206 } 1207 1208 fn purge_latest_branches(&mut self) { 1209 // All of our branch simplification rules work only if a branch ends at 1210 // the tail of the buffer, with no following code; and branches are in 1211 // order in latest_branches; so if the last entry ends prior to 1212 // cur_offset, then clear all entries. 1213 let cur_off = self.cur_offset(); 1214 if let Some(l) = self.latest_branches.last() { 1215 if l.end < cur_off { 1216 trace!("purge_latest_branches: removing branch {:?}", l); 1217 self.latest_branches.clear(); 1218 } 1219 } 1220 1221 // Post-invariant: no invariant requires any branch to appear in 1222 // `latest_branches`; it is always optional. The list-clear above thus 1223 // preserves all semantics. 1224 } 1225 1226 /// Emit a trap at some point in the future with the specified code and 1227 /// stack map. 1228 /// 1229 /// This function returns a [`MachLabel`] which will be the future address 1230 /// of the trap. Jumps should refer to this label, likely by using the 1231 /// [`MachBuffer::use_label_at_offset`] method, to get a relocation 1232 /// patched in once the address of the trap is known. 1233 /// 1234 /// This will batch all traps into the end of the function. 1235 pub fn defer_trap(&mut self, code: TrapCode) -> MachLabel { 1236 let label = self.get_label(); 1237 self.pending_traps.push(MachLabelTrap { 1238 label, 1239 code, 1240 loc: self.cur_srcloc.map(|(_start, loc)| loc), 1241 }); 1242 label 1243 } 1244 1245 /// Is an island needed within the next N bytes? 1246 pub fn island_needed(&self, distance: CodeOffset) -> bool { 1247 let deadline = match self.fixup_records.peek() { 1248 Some(fixup) => fixup.deadline().min(self.pending_fixup_deadline), 1249 None => self.pending_fixup_deadline, 1250 }; 1251 deadline < u32::MAX && self.worst_case_end_of_island(distance) > deadline 1252 } 1253 1254 /// Returns the maximal offset that islands can reach if `distance` more 1255 /// bytes are appended. 1256 /// 1257 /// This is used to determine if veneers need insertions since jumps that 1258 /// can't reach past this point must get a veneer of some form. 1259 fn worst_case_end_of_island(&self, distance: CodeOffset) -> CodeOffset { 1260 // Assume that all fixups will require veneers and that the veneers are 1261 // the worst-case size for each platform. This is an over-generalization 1262 // to avoid iterating over the `fixup_records` list or maintaining 1263 // information about it as we go along. 1264 let island_worst_case_size = ((self.fixup_records.len() + self.pending_fixup_records.len()) 1265 as u32) 1266 * (I::LabelUse::worst_case_veneer_size()) 1267 + self.pending_constants_size 1268 + (self.pending_traps.len() * I::TRAP_OPCODE.len()) as u32; 1269 self.cur_offset() 1270 .saturating_add(distance) 1271 .saturating_add(island_worst_case_size) 1272 } 1273 1274 /// Emit all pending constants and required pending veneers. 1275 /// 1276 /// Should only be called if `island_needed()` returns true, i.e., if we 1277 /// actually reach a deadline. It's not necessarily a problem to do so 1278 /// otherwise but it may result in unnecessary work during emission. 1279 pub fn emit_island(&mut self, distance: CodeOffset, ctrl_plane: &mut ControlPlane) { 1280 self.emit_island_maybe_forced(ForceVeneers::No, distance, ctrl_plane); 1281 } 1282 1283 /// Same as `emit_island`, but an internal API with a `force_veneers` 1284 /// argument to force all veneers to always get emitted for debugging. 1285 fn emit_island_maybe_forced( 1286 &mut self, 1287 force_veneers: ForceVeneers, 1288 distance: CodeOffset, 1289 ctrl_plane: &mut ControlPlane, 1290 ) { 1291 // We're going to purge fixups, so no latest-branch editing can happen 1292 // anymore. 1293 self.latest_branches.clear(); 1294 1295 // End the current location tracking since anything emitted during this 1296 // function shouldn't be attributed to whatever the current source 1297 // location is. 1298 // 1299 // Note that the current source location, if it's set right now, will be 1300 // restored at the end of this island emission. 1301 let cur_loc = self.cur_srcloc.map(|(_, loc)| loc); 1302 if cur_loc.is_some() { 1303 self.end_srcloc(); 1304 } 1305 1306 let forced_threshold = self.worst_case_end_of_island(distance); 1307 1308 // First flush out all traps/constants so we have more labels in case 1309 // fixups are applied against these labels. 1310 // 1311 // Note that traps are placed first since this typically happens at the 1312 // end of the function and for disassemblers we try to keep all the code 1313 // contiguously together. 1314 for MachLabelTrap { label, code, loc } in mem::take(&mut self.pending_traps) { 1315 // If this trap has source information associated with it then 1316 // emit this information for the trap instruction going out now too. 1317 if let Some(loc) = loc { 1318 self.start_srcloc(loc); 1319 } 1320 self.align_to(I::LabelUse::ALIGN); 1321 self.bind_label(label, ctrl_plane); 1322 self.add_trap(code); 1323 self.put_data(I::TRAP_OPCODE); 1324 if loc.is_some() { 1325 self.end_srcloc(); 1326 } 1327 } 1328 1329 for constant in mem::take(&mut self.pending_constants) { 1330 let MachBufferConstant { align, size, .. } = self.constants[constant]; 1331 let label = self.constants[constant].upcoming_label.take().unwrap(); 1332 self.align_to(align); 1333 self.bind_label(label, ctrl_plane); 1334 self.used_constants.push((constant, self.cur_offset())); 1335 self.get_appended_space(size); 1336 } 1337 1338 // Either handle all pending fixups because they're ready or move them 1339 // onto the `BinaryHeap` tracking all pending fixups if they aren't 1340 // ready. 1341 assert!(self.latest_branches.is_empty()); 1342 for fixup in mem::take(&mut self.pending_fixup_records) { 1343 if self.should_apply_fixup(&fixup, forced_threshold) { 1344 self.handle_fixup(fixup, force_veneers, forced_threshold); 1345 } else { 1346 self.fixup_records.push(fixup); 1347 } 1348 } 1349 self.pending_fixup_deadline = u32::MAX; 1350 while let Some(fixup) = self.fixup_records.peek() { 1351 trace!("emit_island: fixup {:?}", fixup); 1352 1353 // If this fixup shouldn't be applied, that means its label isn't 1354 // defined yet and there'll be remaining space to apply a veneer if 1355 // necessary in the future after this island. In that situation 1356 // because `fixup_records` is sorted by deadline this loop can 1357 // exit. 1358 if !self.should_apply_fixup(fixup, forced_threshold) { 1359 break; 1360 } 1361 1362 let fixup = self.fixup_records.pop().unwrap(); 1363 self.handle_fixup(fixup, force_veneers, forced_threshold); 1364 } 1365 1366 if let Some(loc) = cur_loc { 1367 self.start_srcloc(loc); 1368 } 1369 } 1370 1371 fn should_apply_fixup(&self, fixup: &MachLabelFixup<I>, forced_threshold: CodeOffset) -> bool { 1372 let label_offset = self.resolve_label_offset(fixup.label); 1373 label_offset != UNKNOWN_LABEL_OFFSET || fixup.deadline() < forced_threshold 1374 } 1375 1376 fn handle_fixup( 1377 &mut self, 1378 fixup: MachLabelFixup<I>, 1379 force_veneers: ForceVeneers, 1380 forced_threshold: CodeOffset, 1381 ) { 1382 let MachLabelFixup { 1383 label, 1384 offset, 1385 kind, 1386 } = fixup; 1387 let start = offset as usize; 1388 let end = (offset + kind.patch_size()) as usize; 1389 let label_offset = self.resolve_label_offset(label); 1390 1391 if label_offset != UNKNOWN_LABEL_OFFSET { 1392 // If the offset of the label for this fixup is known then 1393 // we're going to do something here-and-now. We're either going 1394 // to patch the original offset because it's an in-bounds jump, 1395 // or we're going to generate a veneer, patch the fixup to jump 1396 // to the veneer, and then keep going. 1397 // 1398 // If the label comes after the original fixup, then we should 1399 // be guaranteed that the jump is in-bounds. Otherwise there's 1400 // a bug somewhere because this method wasn't called soon 1401 // enough. All forward-jumps are tracked and should get veneers 1402 // before their deadline comes and they're unable to jump 1403 // further. 1404 // 1405 // Otherwise if the label is before the fixup, then that's a 1406 // backwards jump. If it's past the maximum negative range 1407 // then we'll emit a veneer that to jump forward to which can 1408 // then jump backwards. 1409 let veneer_required = if label_offset >= offset { 1410 assert!((label_offset - offset) <= kind.max_pos_range()); 1411 false 1412 } else { 1413 (offset - label_offset) > kind.max_neg_range() 1414 }; 1415 trace!( 1416 " -> label_offset = {}, known, required = {} (pos {} neg {})", 1417 label_offset, 1418 veneer_required, 1419 kind.max_pos_range(), 1420 kind.max_neg_range() 1421 ); 1422 1423 if (force_veneers == ForceVeneers::Yes && kind.supports_veneer()) || veneer_required { 1424 self.emit_veneer(label, offset, kind); 1425 } else { 1426 let slice = &mut self.data[start..end]; 1427 trace!("patching in-range!"); 1428 kind.patch(slice, offset, label_offset); 1429 } 1430 } else { 1431 // If the offset of this label is not known at this time then 1432 // that means that a veneer is required because after this 1433 // island the target can't be in range of the original target. 1434 assert!(forced_threshold - offset > kind.max_pos_range()); 1435 self.emit_veneer(label, offset, kind); 1436 } 1437 } 1438 1439 /// Emits a "veneer" the `kind` code at `offset` to jump to `label`. 1440 /// 1441 /// This will generate extra machine code, using `kind`, to get a 1442 /// larger-jump-kind than `kind` allows. The code at `offset` is then 1443 /// patched to jump to our new code, and then the new code is enqueued for 1444 /// a fixup to get processed at some later time. 1445 fn emit_veneer(&mut self, label: MachLabel, offset: CodeOffset, kind: I::LabelUse) { 1446 // If this `kind` doesn't support a veneer then that's a bug in the 1447 // backend because we need to implement support for such a veneer. 1448 assert!( 1449 kind.supports_veneer(), 1450 "jump beyond the range of {:?} but a veneer isn't supported", 1451 kind, 1452 ); 1453 1454 // Allocate space for a veneer in the island. 1455 self.align_to(I::LabelUse::ALIGN); 1456 let veneer_offset = self.cur_offset(); 1457 trace!("making a veneer at {}", veneer_offset); 1458 let start = offset as usize; 1459 let end = (offset + kind.patch_size()) as usize; 1460 let slice = &mut self.data[start..end]; 1461 // Patch the original label use to refer to the veneer. 1462 trace!( 1463 "patching original at offset {} to veneer offset {}", 1464 offset, 1465 veneer_offset 1466 ); 1467 kind.patch(slice, offset, veneer_offset); 1468 // Generate the veneer. 1469 let veneer_slice = self.get_appended_space(kind.veneer_size() as usize); 1470 let (veneer_fixup_off, veneer_label_use) = 1471 kind.generate_veneer(veneer_slice, veneer_offset); 1472 trace!( 1473 "generated veneer; fixup offset {}, label_use {:?}", 1474 veneer_fixup_off, 1475 veneer_label_use 1476 ); 1477 // Register a new use of `label` with our new veneer fixup and 1478 // offset. This'll recalculate deadlines accordingly and 1479 // enqueue this fixup to get processed at some later 1480 // time. 1481 self.use_label_at_offset(veneer_fixup_off, label, veneer_label_use); 1482 } 1483 1484 fn finish_emission_maybe_forcing_veneers( 1485 &mut self, 1486 force_veneers: ForceVeneers, 1487 ctrl_plane: &mut ControlPlane, 1488 ) { 1489 while !self.pending_constants.is_empty() 1490 || !self.pending_traps.is_empty() 1491 || !self.fixup_records.is_empty() 1492 || !self.pending_fixup_records.is_empty() 1493 { 1494 // `emit_island()` will emit any pending veneers and constants, and 1495 // as a side-effect, will also take care of any fixups with resolved 1496 // labels eagerly. 1497 self.emit_island_maybe_forced(force_veneers, u32::MAX, ctrl_plane); 1498 } 1499 1500 // Ensure that all labels have been fixed up after the last island is emitted. This is a 1501 // full (release-mode) assert because an unresolved label means the emitted code is 1502 // incorrect. 1503 assert!(self.fixup_records.is_empty()); 1504 assert!(self.pending_fixup_records.is_empty()); 1505 } 1506 1507 /// Finish any deferred emissions and/or fixups. 1508 pub fn finish( 1509 mut self, 1510 constants: &VCodeConstants, 1511 ctrl_plane: &mut ControlPlane, 1512 ) -> MachBufferFinalized<Stencil> { 1513 let _tt = timing::vcode_emit_finish(); 1514 1515 self.finish_emission_maybe_forcing_veneers(ForceVeneers::No, ctrl_plane); 1516 1517 let alignment = self.finish_constants(constants); 1518 1519 // Resolve all labels to their offsets. 1520 let finalized_relocs = self 1521 .relocs 1522 .iter() 1523 .map(|reloc| FinalizedMachReloc { 1524 offset: reloc.offset, 1525 kind: reloc.kind, 1526 addend: reloc.addend, 1527 target: match &reloc.target { 1528 RelocTarget::ExternalName(name) => { 1529 FinalizedRelocTarget::ExternalName(name.clone()) 1530 } 1531 RelocTarget::Label(label) => { 1532 FinalizedRelocTarget::Func(self.resolve_label_offset(*label)) 1533 } 1534 }, 1535 }) 1536 .collect(); 1537 1538 let mut srclocs = self.srclocs; 1539 srclocs.sort_by_key(|entry| entry.start); 1540 1541 MachBufferFinalized { 1542 data: self.data, 1543 relocs: finalized_relocs, 1544 traps: self.traps, 1545 call_sites: self.call_sites, 1546 srclocs, 1547 stack_maps: self.stack_maps, 1548 user_stack_maps: self.user_stack_maps, 1549 unwind_info: self.unwind_info, 1550 alignment, 1551 } 1552 } 1553 1554 /// Add an external relocation at the given offset from current offset. 1555 pub fn add_reloc_at_offset<T: Into<RelocTarget> + Clone>( 1556 &mut self, 1557 offset: CodeOffset, 1558 kind: Reloc, 1559 target: &T, 1560 addend: Addend, 1561 ) { 1562 let target: RelocTarget = target.clone().into(); 1563 // FIXME(#3277): This should use `I::LabelUse::from_reloc` to optionally 1564 // generate a label-use statement to track whether an island is possibly 1565 // needed to escape this function to actually get to the external name. 1566 // This is most likely to come up on AArch64 where calls between 1567 // functions use a 26-bit signed offset which gives +/- 64MB. This means 1568 // that if a function is 128MB in size and there's a call in the middle 1569 // it's impossible to reach the actual target. Also, while it's 1570 // technically possible to jump to the start of a function and then jump 1571 // further, island insertion below always inserts islands after 1572 // previously appended code so for Cranelift's own implementation this 1573 // is also a problem for 64MB functions on AArch64 which start with a 1574 // call instruction, those won't be able to escape. 1575 // 1576 // Ideally what needs to happen here is that a `LabelUse` is 1577 // transparently generated (or call-sites of this function are audited 1578 // to generate a `LabelUse` instead) and tracked internally. The actual 1579 // relocation would then change over time if and when a veneer is 1580 // inserted, where the relocation here would be patched by this 1581 // `MachBuffer` to jump to the veneer. The problem, though, is that all 1582 // this still needs to end up, in the case of a singular function, 1583 // generating a final relocation pointing either to this particular 1584 // relocation or to the veneer inserted. Additionally 1585 // `MachBuffer` needs the concept of a label which will never be 1586 // resolved, so `emit_island` doesn't trip over not actually ever 1587 // knowning what some labels are. Currently the loop in 1588 // `finish_emission_maybe_forcing_veneers` would otherwise infinitely 1589 // loop. 1590 // 1591 // For now this means that because relocs aren't tracked at all that 1592 // AArch64 functions have a rough size limits of 64MB. For now that's 1593 // somewhat reasonable and the failure mode is a panic in `MachBuffer` 1594 // when a relocation can't otherwise be resolved later, so it shouldn't 1595 // actually result in any memory unsafety or anything like that. 1596 self.relocs.push(MachReloc { 1597 offset: self.data.len() as CodeOffset + offset, 1598 kind, 1599 target, 1600 addend, 1601 }); 1602 } 1603 1604 /// Add an external relocation at the current offset. 1605 pub fn add_reloc<T: Into<RelocTarget> + Clone>( 1606 &mut self, 1607 kind: Reloc, 1608 target: &T, 1609 addend: Addend, 1610 ) { 1611 self.add_reloc_at_offset(0, kind, target, addend); 1612 } 1613 1614 /// Add a trap record at the current offset. 1615 pub fn add_trap(&mut self, code: TrapCode) { 1616 self.traps.push(MachTrap { 1617 offset: self.data.len() as CodeOffset, 1618 code, 1619 }); 1620 } 1621 1622 /// Add a call-site record at the current offset. 1623 pub fn add_call_site(&mut self, opcode: Opcode) { 1624 debug_assert!( 1625 opcode.is_call(), 1626 "adding call site info for a non-call instruction." 1627 ); 1628 self.call_sites.push(MachCallSite { 1629 ret_addr: self.data.len() as CodeOffset, 1630 opcode, 1631 }); 1632 } 1633 1634 /// Add an unwind record at the current offset. 1635 pub fn add_unwind(&mut self, unwind: UnwindInst) { 1636 self.unwind_info.push((self.cur_offset(), unwind)); 1637 } 1638 1639 /// Set the `SourceLoc` for code from this offset until the offset at the 1640 /// next call to `end_srcloc()`. 1641 /// Returns the current [CodeOffset] and [RelSourceLoc]. 1642 pub fn start_srcloc(&mut self, loc: RelSourceLoc) -> (CodeOffset, RelSourceLoc) { 1643 let cur = (self.cur_offset(), loc); 1644 self.cur_srcloc = Some(cur); 1645 cur 1646 } 1647 1648 /// Mark the end of the `SourceLoc` segment started at the last 1649 /// `start_srcloc()` call. 1650 pub fn end_srcloc(&mut self) { 1651 let (start, loc) = self 1652 .cur_srcloc 1653 .take() 1654 .expect("end_srcloc() called without start_srcloc()"); 1655 let end = self.cur_offset(); 1656 // Skip zero-length extends. 1657 debug_assert!(end >= start); 1658 if end > start { 1659 self.srclocs.push(MachSrcLoc { start, end, loc }); 1660 } 1661 } 1662 1663 /// Add stack map metadata for this program point: a set of stack offsets 1664 /// (from SP upward) that contain live references. 1665 pub fn add_stack_map(&mut self, extent: StackMapExtent, stack_map: StackMap) { 1666 let (start, end) = match extent { 1667 StackMapExtent::UpcomingBytes(insn_len) => { 1668 let start_offset = self.cur_offset(); 1669 (start_offset, start_offset + insn_len) 1670 } 1671 StackMapExtent::StartedAtOffset(start_offset) => { 1672 let end_offset = self.cur_offset(); 1673 debug_assert!(end_offset >= start_offset); 1674 (start_offset, end_offset) 1675 } 1676 }; 1677 trace!("Adding stack map for offsets {start:#x}..{end:#x}: {stack_map:?}"); 1678 self.stack_maps.push(MachStackMap { 1679 offset: start, 1680 offset_end: end, 1681 stack_map, 1682 }); 1683 } 1684 1685 /// Push a user stack map onto this buffer. 1686 /// 1687 /// The stack map is associated with the given `return_addr` code 1688 /// offset. This must be the PC for the instruction just *after* this stack 1689 /// map's associated instruction. For example in the sequence `call $foo; 1690 /// add r8, rax`, the `return_addr` must be the offset of the start of the 1691 /// `add` instruction. 1692 /// 1693 /// Stack maps must be pushed in sorted `return_addr` order. 1694 pub fn push_user_stack_map( 1695 &mut self, 1696 emit_state: &I::State, 1697 return_addr: CodeOffset, 1698 stack_map: ir::UserStackMap, 1699 ) { 1700 let span = emit_state.frame_layout().active_size(); 1701 trace!("Adding user stack map @ {return_addr:#x} spanning {span} bytes: {stack_map:?}"); 1702 1703 debug_assert!( 1704 self.user_stack_maps 1705 .last() 1706 .map_or(true, |(prev_addr, _, _)| *prev_addr < return_addr), 1707 "pushed stack maps out of order: {} is not less than {}", 1708 self.user_stack_maps.last().unwrap().0, 1709 return_addr, 1710 ); 1711 1712 self.user_stack_maps.push((return_addr, span, stack_map)); 1713 } 1714 } 1715 1716 impl<T: CompilePhase> MachBufferFinalized<T> { 1717 /// Get a list of source location mapping tuples in sorted-by-start-offset order. 1718 pub fn get_srclocs_sorted(&self) -> &[T::MachSrcLocType] { 1719 &self.srclocs[..] 1720 } 1721 1722 /// Get the total required size for the code. 1723 pub fn total_size(&self) -> CodeOffset { 1724 self.data.len() as CodeOffset 1725 } 1726 1727 /// Return the code in this mach buffer as a hex string for testing purposes. 1728 pub fn stringify_code_bytes(&self) -> String { 1729 // This is pretty lame, but whatever .. 1730 use std::fmt::Write; 1731 let mut s = String::with_capacity(self.data.len() * 2); 1732 for b in &self.data { 1733 write!(&mut s, "{:02X}", b).unwrap(); 1734 } 1735 s 1736 } 1737 1738 /// Get the code bytes. 1739 pub fn data(&self) -> &[u8] { 1740 // N.B.: we emit every section into the .text section as far as 1741 // the `CodeSink` is concerned; we do not bother to segregate 1742 // the contents into the actual program text, the jumptable and the 1743 // rodata (constant pool). This allows us to generate code assuming 1744 // that these will not be relocated relative to each other, and avoids 1745 // having to designate each section as belonging in one of the three 1746 // fixed categories defined by `CodeSink`. If this becomes a problem 1747 // later (e.g. because of memory permissions or similar), we can 1748 // add this designation and segregate the output; take care, however, 1749 // to add the appropriate relocations in this case. 1750 1751 &self.data[..] 1752 } 1753 1754 /// Get the list of external relocations for this code. 1755 pub fn relocs(&self) -> &[FinalizedMachReloc] { 1756 &self.relocs[..] 1757 } 1758 1759 /// Get the list of trap records for this code. 1760 pub fn traps(&self) -> &[MachTrap] { 1761 &self.traps[..] 1762 } 1763 1764 /// Get the stack map metadata for this code. 1765 pub fn stack_maps(&self) -> &[MachStackMap] { 1766 &self.stack_maps[..] 1767 } 1768 1769 /// Take this buffer's stack map metadata. 1770 pub fn take_stack_maps(&mut self) -> SmallVec<[MachStackMap; 8]> { 1771 mem::take(&mut self.stack_maps) 1772 } 1773 1774 /// Get the list of call sites for this code. 1775 pub fn call_sites(&self) -> &[MachCallSite] { 1776 &self.call_sites[..] 1777 } 1778 } 1779 1780 /// Metadata about a constant. 1781 struct MachBufferConstant { 1782 /// A label which has not yet been bound which can be used for this 1783 /// constant. 1784 /// 1785 /// This is lazily created when a label is requested for a constant and is 1786 /// cleared when a constant is emitted. 1787 upcoming_label: Option<MachLabel>, 1788 /// Required alignment. 1789 align: CodeOffset, 1790 /// The byte size of this constant. 1791 size: usize, 1792 } 1793 1794 /// A trap that is deferred to the next time an island is emitted for either 1795 /// traps, constants, or fixups. 1796 struct MachLabelTrap { 1797 /// This label will refer to the trap's offset. 1798 label: MachLabel, 1799 /// The code associated with this trap. 1800 code: TrapCode, 1801 /// An optional source location to assign for this trap. 1802 loc: Option<RelSourceLoc>, 1803 } 1804 1805 /// A fixup to perform on the buffer once code is emitted. Fixups always refer 1806 /// to labels and patch the code based on label offsets. Hence, they are like 1807 /// relocations, but internal to one buffer. 1808 #[derive(Debug)] 1809 struct MachLabelFixup<I: VCodeInst> { 1810 /// The label whose offset controls this fixup. 1811 label: MachLabel, 1812 /// The offset to fix up / patch to refer to this label. 1813 offset: CodeOffset, 1814 /// The kind of fixup. This is architecture-specific; each architecture may have, 1815 /// e.g., several types of branch instructions, each with differently-sized 1816 /// offset fields and different places within the instruction to place the 1817 /// bits. 1818 kind: I::LabelUse, 1819 } 1820 1821 impl<I: VCodeInst> MachLabelFixup<I> { 1822 fn deadline(&self) -> CodeOffset { 1823 self.offset.saturating_add(self.kind.max_pos_range()) 1824 } 1825 } 1826 1827 impl<I: VCodeInst> PartialEq for MachLabelFixup<I> { 1828 fn eq(&self, other: &Self) -> bool { 1829 self.deadline() == other.deadline() 1830 } 1831 } 1832 1833 impl<I: VCodeInst> Eq for MachLabelFixup<I> {} 1834 1835 impl<I: VCodeInst> PartialOrd for MachLabelFixup<I> { 1836 fn partial_cmp(&self, other: &Self) -> Option<Ordering> { 1837 Some(self.cmp(other)) 1838 } 1839 } 1840 1841 impl<I: VCodeInst> Ord for MachLabelFixup<I> { 1842 fn cmp(&self, other: &Self) -> Ordering { 1843 other.deadline().cmp(&self.deadline()) 1844 } 1845 } 1846 1847 /// A relocation resulting from a compilation. 1848 #[derive(Clone, Debug, PartialEq)] 1849 #[cfg_attr( 1850 feature = "enable-serde", 1851 derive(serde_derive::Serialize, serde_derive::Deserialize) 1852 )] 1853 pub struct MachRelocBase<T> { 1854 /// The offset at which the relocation applies, *relative to the 1855 /// containing section*. 1856 pub offset: CodeOffset, 1857 /// The kind of relocation. 1858 pub kind: Reloc, 1859 /// The external symbol / name to which this relocation refers. 1860 pub target: T, 1861 /// The addend to add to the symbol value. 1862 pub addend: i64, 1863 } 1864 1865 type MachReloc = MachRelocBase<RelocTarget>; 1866 1867 /// A relocation resulting from a compilation. 1868 pub type FinalizedMachReloc = MachRelocBase<FinalizedRelocTarget>; 1869 1870 /// A Relocation target 1871 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 1872 pub enum RelocTarget { 1873 /// Points to an [ExternalName] outside the current function. 1874 ExternalName(ExternalName), 1875 /// Points to a [MachLabel] inside this function. 1876 /// This is different from [MachLabelFixup] in that both the relocation and the 1877 /// label will be emitted and are only resolved at link time. 1878 /// 1879 /// There is no reason to prefer this over [MachLabelFixup] unless the ABI requires it. 1880 Label(MachLabel), 1881 } 1882 1883 impl From<ExternalName> for RelocTarget { 1884 fn from(name: ExternalName) -> Self { 1885 Self::ExternalName(name) 1886 } 1887 } 1888 1889 impl From<MachLabel> for RelocTarget { 1890 fn from(label: MachLabel) -> Self { 1891 Self::Label(label) 1892 } 1893 } 1894 1895 /// A Relocation target 1896 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 1897 #[cfg_attr( 1898 feature = "enable-serde", 1899 derive(serde_derive::Serialize, serde_derive::Deserialize) 1900 )] 1901 pub enum FinalizedRelocTarget { 1902 /// Points to an [ExternalName] outside the current function. 1903 ExternalName(ExternalName), 1904 /// Points to a [CodeOffset] from the start of the current function. 1905 Func(CodeOffset), 1906 } 1907 1908 impl FinalizedRelocTarget { 1909 /// Returns a display for the current [FinalizedRelocTarget], with extra context to prettify the 1910 /// output. 1911 pub fn display<'a>(&'a self, params: Option<&'a FunctionParameters>) -> String { 1912 match self { 1913 FinalizedRelocTarget::ExternalName(name) => format!("{}", name.display(params)), 1914 FinalizedRelocTarget::Func(offset) => format!("func+{offset}"), 1915 } 1916 } 1917 } 1918 1919 /// A trap record resulting from a compilation. 1920 #[derive(Clone, Debug, PartialEq)] 1921 #[cfg_attr( 1922 feature = "enable-serde", 1923 derive(serde_derive::Serialize, serde_derive::Deserialize) 1924 )] 1925 pub struct MachTrap { 1926 /// The offset at which the trap instruction occurs, *relative to the 1927 /// containing section*. 1928 pub offset: CodeOffset, 1929 /// The trap code. 1930 pub code: TrapCode, 1931 } 1932 1933 /// A call site record resulting from a compilation. 1934 #[derive(Clone, Debug, PartialEq)] 1935 #[cfg_attr( 1936 feature = "enable-serde", 1937 derive(serde_derive::Serialize, serde_derive::Deserialize) 1938 )] 1939 pub struct MachCallSite { 1940 /// The offset of the call's return address, *relative to the containing section*. 1941 pub ret_addr: CodeOffset, 1942 /// The call's opcode. 1943 pub opcode: Opcode, 1944 } 1945 1946 /// A source-location mapping resulting from a compilation. 1947 #[derive(PartialEq, Debug, Clone)] 1948 #[cfg_attr( 1949 feature = "enable-serde", 1950 derive(serde_derive::Serialize, serde_derive::Deserialize) 1951 )] 1952 pub struct MachSrcLoc<T: CompilePhase> { 1953 /// The start of the region of code corresponding to a source location. 1954 /// This is relative to the start of the function, not to the start of the 1955 /// section. 1956 pub start: CodeOffset, 1957 /// The end of the region of code corresponding to a source location. 1958 /// This is relative to the start of the section, not to the start of the 1959 /// section. 1960 pub end: CodeOffset, 1961 /// The source location. 1962 pub loc: T::SourceLocType, 1963 } 1964 1965 impl MachSrcLoc<Stencil> { 1966 fn apply_base_srcloc(self, base_srcloc: SourceLoc) -> MachSrcLoc<Final> { 1967 MachSrcLoc { 1968 start: self.start, 1969 end: self.end, 1970 loc: self.loc.expand(base_srcloc), 1971 } 1972 } 1973 } 1974 1975 /// Record of stack map metadata: stack offsets containing references. 1976 #[derive(Clone, Debug, PartialEq)] 1977 #[cfg_attr( 1978 feature = "enable-serde", 1979 derive(serde_derive::Serialize, serde_derive::Deserialize) 1980 )] 1981 pub struct MachStackMap { 1982 /// The code offset at which this stack map applies. 1983 pub offset: CodeOffset, 1984 /// The code offset just past the "end" of the instruction: that is, the 1985 /// offset of the first byte of the following instruction, or equivalently, 1986 /// the start offset plus the instruction length. 1987 pub offset_end: CodeOffset, 1988 /// The stack map itself. 1989 pub stack_map: StackMap, 1990 } 1991 1992 /// Record of branch instruction in the buffer, to facilitate editing. 1993 #[derive(Clone, Debug)] 1994 struct MachBranch { 1995 start: CodeOffset, 1996 end: CodeOffset, 1997 target: MachLabel, 1998 fixup: usize, 1999 inverted: Option<SmallVec<[u8; 8]>>, 2000 /// All labels pointing to the start of this branch. For correctness, this 2001 /// *must* be complete (i.e., must contain all labels whose resolved offsets 2002 /// are at the start of this branch): we rely on being able to redirect all 2003 /// labels that could jump to this branch before removing it, if it is 2004 /// otherwise unreachable. 2005 labels_at_this_branch: SmallVec<[MachLabel; 4]>, 2006 } 2007 2008 impl MachBranch { 2009 fn is_cond(&self) -> bool { 2010 self.inverted.is_some() 2011 } 2012 fn is_uncond(&self) -> bool { 2013 self.inverted.is_none() 2014 } 2015 } 2016 2017 /// Implementation of the `TextSectionBuilder` trait backed by `MachBuffer`. 2018 /// 2019 /// Note that `MachBuffer` was primarily written for intra-function references 2020 /// of jumps between basic blocks, but it's also quite usable for entire text 2021 /// sections and resolving references between functions themselves. This 2022 /// builder interprets "blocks" as labeled functions for the purposes of 2023 /// resolving labels internally in the buffer. 2024 pub struct MachTextSectionBuilder<I: VCodeInst> { 2025 buf: MachBuffer<I>, 2026 next_func: usize, 2027 force_veneers: ForceVeneers, 2028 } 2029 2030 impl<I: VCodeInst> MachTextSectionBuilder<I> { 2031 /// Creates a new text section builder which will have `num_funcs` functions 2032 /// pushed into it. 2033 pub fn new(num_funcs: usize) -> MachTextSectionBuilder<I> { 2034 let mut buf = MachBuffer::new(); 2035 buf.reserve_labels_for_blocks(num_funcs); 2036 MachTextSectionBuilder { 2037 buf, 2038 next_func: 0, 2039 force_veneers: ForceVeneers::No, 2040 } 2041 } 2042 } 2043 2044 impl<I: VCodeInst> TextSectionBuilder for MachTextSectionBuilder<I> { 2045 fn append( 2046 &mut self, 2047 labeled: bool, 2048 func: &[u8], 2049 align: u32, 2050 ctrl_plane: &mut ControlPlane, 2051 ) -> u64 { 2052 // Conditionally emit an island if it's necessary to resolve jumps 2053 // between functions which are too far away. 2054 let size = func.len() as u32; 2055 if self.force_veneers == ForceVeneers::Yes || self.buf.island_needed(size) { 2056 self.buf 2057 .emit_island_maybe_forced(self.force_veneers, size, ctrl_plane); 2058 } 2059 2060 self.buf.align_to(align); 2061 let pos = self.buf.cur_offset(); 2062 if labeled { 2063 self.buf.bind_label( 2064 MachLabel::from_block(BlockIndex::new(self.next_func)), 2065 ctrl_plane, 2066 ); 2067 self.next_func += 1; 2068 } 2069 self.buf.put_data(func); 2070 u64::from(pos) 2071 } 2072 2073 fn resolve_reloc(&mut self, offset: u64, reloc: Reloc, addend: Addend, target: usize) -> bool { 2074 crate::trace!( 2075 "Resolving relocation @ {offset:#x} + {addend:#x} to target {target} of kind {reloc:?}" 2076 ); 2077 let label = MachLabel::from_block(BlockIndex::new(target)); 2078 let offset = u32::try_from(offset).unwrap(); 2079 match I::LabelUse::from_reloc(reloc, addend) { 2080 Some(label_use) => { 2081 self.buf.use_label_at_offset(offset, label, label_use); 2082 true 2083 } 2084 None => false, 2085 } 2086 } 2087 2088 fn force_veneers(&mut self) { 2089 self.force_veneers = ForceVeneers::Yes; 2090 } 2091 2092 fn finish(&mut self, ctrl_plane: &mut ControlPlane) -> Vec<u8> { 2093 // Double-check all functions were pushed. 2094 assert_eq!(self.next_func, self.buf.label_offsets.len()); 2095 2096 // Finish up any veneers, if necessary. 2097 self.buf 2098 .finish_emission_maybe_forcing_veneers(self.force_veneers, ctrl_plane); 2099 2100 // We don't need the data any more, so return it to the caller. 2101 mem::take(&mut self.buf.data).into_vec() 2102 } 2103 } 2104 2105 // We use an actual instruction definition to do tests, so we depend on the `arm64` feature here. 2106 #[cfg(all(test, feature = "arm64"))] 2107 mod test { 2108 use cranelift_entity::EntityRef as _; 2109 2110 use super::*; 2111 use crate::ir::UserExternalNameRef; 2112 use crate::isa::aarch64::inst::xreg; 2113 use crate::isa::aarch64::inst::{BranchTarget, CondBrKind, EmitInfo, Inst}; 2114 use crate::machinst::{MachInstEmit, MachInstEmitState}; 2115 use crate::settings; 2116 2117 fn label(n: u32) -> MachLabel { 2118 MachLabel::from_block(BlockIndex::new(n as usize)) 2119 } 2120 fn target(n: u32) -> BranchTarget { 2121 BranchTarget::Label(label(n)) 2122 } 2123 2124 #[test] 2125 fn test_elide_jump_to_next() { 2126 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2127 let mut buf = MachBuffer::new(); 2128 let mut state = <Inst as MachInstEmit>::State::default(); 2129 let constants = Default::default(); 2130 2131 buf.reserve_labels_for_blocks(2); 2132 buf.bind_label(label(0), state.ctrl_plane_mut()); 2133 let inst = Inst::Jump { dest: target(1) }; 2134 inst.emit(&mut buf, &info, &mut state); 2135 buf.bind_label(label(1), state.ctrl_plane_mut()); 2136 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2137 assert_eq!(0, buf.total_size()); 2138 } 2139 2140 #[test] 2141 fn test_elide_trivial_jump_blocks() { 2142 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2143 let mut buf = MachBuffer::new(); 2144 let mut state = <Inst as MachInstEmit>::State::default(); 2145 let constants = Default::default(); 2146 2147 buf.reserve_labels_for_blocks(4); 2148 2149 buf.bind_label(label(0), state.ctrl_plane_mut()); 2150 let inst = Inst::CondBr { 2151 kind: CondBrKind::NotZero(xreg(0)), 2152 taken: target(1), 2153 not_taken: target(2), 2154 }; 2155 inst.emit(&mut buf, &info, &mut state); 2156 2157 buf.bind_label(label(1), state.ctrl_plane_mut()); 2158 let inst = Inst::Jump { dest: target(3) }; 2159 inst.emit(&mut buf, &info, &mut state); 2160 2161 buf.bind_label(label(2), state.ctrl_plane_mut()); 2162 let inst = Inst::Jump { dest: target(3) }; 2163 inst.emit(&mut buf, &info, &mut state); 2164 2165 buf.bind_label(label(3), state.ctrl_plane_mut()); 2166 2167 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2168 assert_eq!(0, buf.total_size()); 2169 } 2170 2171 #[test] 2172 fn test_flip_cond() { 2173 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2174 let mut buf = MachBuffer::new(); 2175 let mut state = <Inst as MachInstEmit>::State::default(); 2176 let constants = Default::default(); 2177 2178 buf.reserve_labels_for_blocks(4); 2179 2180 buf.bind_label(label(0), state.ctrl_plane_mut()); 2181 let inst = Inst::CondBr { 2182 kind: CondBrKind::Zero(xreg(0)), 2183 taken: target(1), 2184 not_taken: target(2), 2185 }; 2186 inst.emit(&mut buf, &info, &mut state); 2187 2188 buf.bind_label(label(1), state.ctrl_plane_mut()); 2189 let inst = Inst::Nop4; 2190 inst.emit(&mut buf, &info, &mut state); 2191 2192 buf.bind_label(label(2), state.ctrl_plane_mut()); 2193 let inst = Inst::Udf { 2194 trap_code: TrapCode::Interrupt, 2195 }; 2196 inst.emit(&mut buf, &info, &mut state); 2197 2198 buf.bind_label(label(3), state.ctrl_plane_mut()); 2199 2200 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2201 2202 let mut buf2 = MachBuffer::new(); 2203 let mut state = Default::default(); 2204 let inst = Inst::TrapIf { 2205 kind: CondBrKind::NotZero(xreg(0)), 2206 trap_code: TrapCode::Interrupt, 2207 }; 2208 inst.emit(&mut buf2, &info, &mut state); 2209 let inst = Inst::Nop4; 2210 inst.emit(&mut buf2, &info, &mut state); 2211 2212 let buf2 = buf2.finish(&constants, state.ctrl_plane_mut()); 2213 2214 assert_eq!(buf.data, buf2.data); 2215 } 2216 2217 #[test] 2218 fn test_island() { 2219 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2220 let mut buf = MachBuffer::new(); 2221 let mut state = <Inst as MachInstEmit>::State::default(); 2222 let constants = Default::default(); 2223 2224 buf.reserve_labels_for_blocks(4); 2225 2226 buf.bind_label(label(0), state.ctrl_plane_mut()); 2227 let inst = Inst::CondBr { 2228 kind: CondBrKind::NotZero(xreg(0)), 2229 taken: target(2), 2230 not_taken: target(3), 2231 }; 2232 inst.emit(&mut buf, &info, &mut state); 2233 2234 buf.bind_label(label(1), state.ctrl_plane_mut()); 2235 while buf.cur_offset() < 2000000 { 2236 if buf.island_needed(0) { 2237 buf.emit_island(0, state.ctrl_plane_mut()); 2238 } 2239 let inst = Inst::Nop4; 2240 inst.emit(&mut buf, &info, &mut state); 2241 } 2242 2243 buf.bind_label(label(2), state.ctrl_plane_mut()); 2244 let inst = Inst::Nop4; 2245 inst.emit(&mut buf, &info, &mut state); 2246 2247 buf.bind_label(label(3), state.ctrl_plane_mut()); 2248 let inst = Inst::Nop4; 2249 inst.emit(&mut buf, &info, &mut state); 2250 2251 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2252 2253 assert_eq!(2000000 + 8, buf.total_size()); 2254 2255 let mut buf2 = MachBuffer::new(); 2256 let mut state = Default::default(); 2257 let inst = Inst::CondBr { 2258 kind: CondBrKind::NotZero(xreg(0)), 2259 2260 // This conditionally taken branch has a 19-bit constant, shifted 2261 // to the left by two, giving us a 21-bit range in total. Half of 2262 // this range positive so the we should be around 1 << 20 bytes 2263 // away for our jump target. 2264 // 2265 // There are two pending fixups by the time we reach this point, 2266 // one for this 19-bit jump and one for the unconditional 26-bit 2267 // jump below. A 19-bit veneer is 4 bytes large and the 26-bit 2268 // veneer is 20 bytes large, which means that pessimistically 2269 // assuming we'll need two veneers. Currently each veneer is 2270 // pessimistically assumed to be the maximal size which means we 2271 // need 40 bytes of extra space, meaning that the actual island 2272 // should come 40-bytes before the deadline. 2273 taken: BranchTarget::ResolvedOffset((1 << 20) - 20 - 20), 2274 2275 // This branch is in-range so no veneers should be needed, it should 2276 // go directly to the target. 2277 not_taken: BranchTarget::ResolvedOffset(2000000 + 4 - 4), 2278 }; 2279 inst.emit(&mut buf2, &info, &mut state); 2280 2281 let buf2 = buf2.finish(&constants, state.ctrl_plane_mut()); 2282 2283 assert_eq!(&buf.data[0..8], &buf2.data[..]); 2284 } 2285 2286 #[test] 2287 fn test_island_backward() { 2288 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2289 let mut buf = MachBuffer::new(); 2290 let mut state = <Inst as MachInstEmit>::State::default(); 2291 let constants = Default::default(); 2292 2293 buf.reserve_labels_for_blocks(4); 2294 2295 buf.bind_label(label(0), state.ctrl_plane_mut()); 2296 let inst = Inst::Nop4; 2297 inst.emit(&mut buf, &info, &mut state); 2298 2299 buf.bind_label(label(1), state.ctrl_plane_mut()); 2300 let inst = Inst::Nop4; 2301 inst.emit(&mut buf, &info, &mut state); 2302 2303 buf.bind_label(label(2), state.ctrl_plane_mut()); 2304 while buf.cur_offset() < 2000000 { 2305 let inst = Inst::Nop4; 2306 inst.emit(&mut buf, &info, &mut state); 2307 } 2308 2309 buf.bind_label(label(3), state.ctrl_plane_mut()); 2310 let inst = Inst::CondBr { 2311 kind: CondBrKind::NotZero(xreg(0)), 2312 taken: target(0), 2313 not_taken: target(1), 2314 }; 2315 inst.emit(&mut buf, &info, &mut state); 2316 2317 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2318 2319 assert_eq!(2000000 + 12, buf.total_size()); 2320 2321 let mut buf2 = MachBuffer::new(); 2322 let mut state = Default::default(); 2323 let inst = Inst::CondBr { 2324 kind: CondBrKind::NotZero(xreg(0)), 2325 taken: BranchTarget::ResolvedOffset(8), 2326 not_taken: BranchTarget::ResolvedOffset(4 - (2000000 + 4)), 2327 }; 2328 inst.emit(&mut buf2, &info, &mut state); 2329 let inst = Inst::Jump { 2330 dest: BranchTarget::ResolvedOffset(-(2000000 + 8)), 2331 }; 2332 inst.emit(&mut buf2, &info, &mut state); 2333 2334 let buf2 = buf2.finish(&constants, state.ctrl_plane_mut()); 2335 2336 assert_eq!(&buf.data[2000000..], &buf2.data[..]); 2337 } 2338 2339 #[test] 2340 fn test_multiple_redirect() { 2341 // label0: 2342 // cbz x0, label1 2343 // b label2 2344 // label1: 2345 // b label3 2346 // label2: 2347 // nop 2348 // nop 2349 // b label0 2350 // label3: 2351 // b label4 2352 // label4: 2353 // b label5 2354 // label5: 2355 // b label7 2356 // label6: 2357 // nop 2358 // label7: 2359 // ret 2360 // 2361 // -- should become: 2362 // 2363 // label0: 2364 // cbz x0, label7 2365 // label2: 2366 // nop 2367 // nop 2368 // b label0 2369 // label6: 2370 // nop 2371 // label7: 2372 // ret 2373 2374 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2375 let mut buf = MachBuffer::new(); 2376 let mut state = <Inst as MachInstEmit>::State::default(); 2377 let constants = Default::default(); 2378 2379 buf.reserve_labels_for_blocks(8); 2380 2381 buf.bind_label(label(0), state.ctrl_plane_mut()); 2382 let inst = Inst::CondBr { 2383 kind: CondBrKind::Zero(xreg(0)), 2384 taken: target(1), 2385 not_taken: target(2), 2386 }; 2387 inst.emit(&mut buf, &info, &mut state); 2388 2389 buf.bind_label(label(1), state.ctrl_plane_mut()); 2390 let inst = Inst::Jump { dest: target(3) }; 2391 inst.emit(&mut buf, &info, &mut state); 2392 2393 buf.bind_label(label(2), state.ctrl_plane_mut()); 2394 let inst = Inst::Nop4; 2395 inst.emit(&mut buf, &info, &mut state); 2396 inst.emit(&mut buf, &info, &mut state); 2397 let inst = Inst::Jump { dest: target(0) }; 2398 inst.emit(&mut buf, &info, &mut state); 2399 2400 buf.bind_label(label(3), state.ctrl_plane_mut()); 2401 let inst = Inst::Jump { dest: target(4) }; 2402 inst.emit(&mut buf, &info, &mut state); 2403 2404 buf.bind_label(label(4), state.ctrl_plane_mut()); 2405 let inst = Inst::Jump { dest: target(5) }; 2406 inst.emit(&mut buf, &info, &mut state); 2407 2408 buf.bind_label(label(5), state.ctrl_plane_mut()); 2409 let inst = Inst::Jump { dest: target(7) }; 2410 inst.emit(&mut buf, &info, &mut state); 2411 2412 buf.bind_label(label(6), state.ctrl_plane_mut()); 2413 let inst = Inst::Nop4; 2414 inst.emit(&mut buf, &info, &mut state); 2415 2416 buf.bind_label(label(7), state.ctrl_plane_mut()); 2417 let inst = Inst::Ret {}; 2418 inst.emit(&mut buf, &info, &mut state); 2419 2420 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2421 2422 let golden_data = vec![ 2423 0xa0, 0x00, 0x00, 0xb4, // cbz x0, 0x14 2424 0x1f, 0x20, 0x03, 0xd5, // nop 2425 0x1f, 0x20, 0x03, 0xd5, // nop 2426 0xfd, 0xff, 0xff, 0x17, // b 0 2427 0x1f, 0x20, 0x03, 0xd5, // nop 2428 0xc0, 0x03, 0x5f, 0xd6, // ret 2429 ]; 2430 2431 assert_eq!(&golden_data[..], &buf.data[..]); 2432 } 2433 2434 #[test] 2435 fn test_handle_branch_cycle() { 2436 // label0: 2437 // b label1 2438 // label1: 2439 // b label2 2440 // label2: 2441 // b label3 2442 // label3: 2443 // b label4 2444 // label4: 2445 // b label1 // note: not label0 (to make it interesting). 2446 // 2447 // -- should become: 2448 // 2449 // label0, label1, ..., label4: 2450 // b label0 2451 let info = EmitInfo::new(settings::Flags::new(settings::builder())); 2452 let mut buf = MachBuffer::new(); 2453 let mut state = <Inst as MachInstEmit>::State::default(); 2454 let constants = Default::default(); 2455 2456 buf.reserve_labels_for_blocks(5); 2457 2458 buf.bind_label(label(0), state.ctrl_plane_mut()); 2459 let inst = Inst::Jump { dest: target(1) }; 2460 inst.emit(&mut buf, &info, &mut state); 2461 2462 buf.bind_label(label(1), state.ctrl_plane_mut()); 2463 let inst = Inst::Jump { dest: target(2) }; 2464 inst.emit(&mut buf, &info, &mut state); 2465 2466 buf.bind_label(label(2), state.ctrl_plane_mut()); 2467 let inst = Inst::Jump { dest: target(3) }; 2468 inst.emit(&mut buf, &info, &mut state); 2469 2470 buf.bind_label(label(3), state.ctrl_plane_mut()); 2471 let inst = Inst::Jump { dest: target(4) }; 2472 inst.emit(&mut buf, &info, &mut state); 2473 2474 buf.bind_label(label(4), state.ctrl_plane_mut()); 2475 let inst = Inst::Jump { dest: target(1) }; 2476 inst.emit(&mut buf, &info, &mut state); 2477 2478 let buf = buf.finish(&constants, state.ctrl_plane_mut()); 2479 2480 let golden_data = vec![ 2481 0x00, 0x00, 0x00, 0x14, // b 0 2482 ]; 2483 2484 assert_eq!(&golden_data[..], &buf.data[..]); 2485 } 2486 2487 #[test] 2488 fn metadata_records() { 2489 let mut buf = MachBuffer::<Inst>::new(); 2490 let ctrl_plane = &mut Default::default(); 2491 let constants = Default::default(); 2492 2493 buf.reserve_labels_for_blocks(1); 2494 2495 buf.bind_label(label(0), ctrl_plane); 2496 buf.put1(1); 2497 buf.add_trap(TrapCode::HeapOutOfBounds); 2498 buf.put1(2); 2499 buf.add_trap(TrapCode::IntegerOverflow); 2500 buf.add_trap(TrapCode::IntegerDivisionByZero); 2501 buf.add_call_site(Opcode::Call); 2502 buf.add_reloc( 2503 Reloc::Abs4, 2504 &ExternalName::User(UserExternalNameRef::new(0)), 2505 0, 2506 ); 2507 buf.put1(3); 2508 buf.add_reloc( 2509 Reloc::Abs8, 2510 &ExternalName::User(UserExternalNameRef::new(1)), 2511 1, 2512 ); 2513 buf.put1(4); 2514 2515 let buf = buf.finish(&constants, ctrl_plane); 2516 2517 assert_eq!(buf.data(), &[1, 2, 3, 4]); 2518 assert_eq!( 2519 buf.traps() 2520 .iter() 2521 .map(|trap| (trap.offset, trap.code)) 2522 .collect::<Vec<_>>(), 2523 vec![ 2524 (1, TrapCode::HeapOutOfBounds), 2525 (2, TrapCode::IntegerOverflow), 2526 (2, TrapCode::IntegerDivisionByZero) 2527 ] 2528 ); 2529 assert_eq!( 2530 buf.call_sites() 2531 .iter() 2532 .map(|call_site| (call_site.ret_addr, call_site.opcode)) 2533 .collect::<Vec<_>>(), 2534 vec![(2, Opcode::Call)] 2535 ); 2536 assert_eq!( 2537 buf.relocs() 2538 .iter() 2539 .map(|reloc| (reloc.offset, reloc.kind)) 2540 .collect::<Vec<_>>(), 2541 vec![(2, Reloc::Abs4), (3, Reloc::Abs8)] 2542 ); 2543 } 2544 } 2545