cranelift_codegen/machinst/
buffer.rs

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