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 use crate::binemit::{Addend, CodeOffset, CodeSink, Reloc, StackMap};
144 use crate::ir::{ExternalName, Opcode, SourceLoc, TrapCode};
145 use crate::isa::unwind::UnwindInst;
146 use crate::machinst::{BlockIndex, MachInstLabelUse, VCodeConstant, VCodeConstants, VCodeInst};
147 use crate::timing;
148 use cranelift_entity::{entity_impl, SecondaryMap};
149 
150 use log::trace;
151 use smallvec::SmallVec;
152 use std::mem;
153 use std::string::String;
154 
155 /// A buffer of output to be produced, fixed up, and then emitted to a CodeSink
156 /// in bulk.
157 ///
158 /// This struct uses `SmallVec`s to support small-ish function bodies without
159 /// any heap allocation. As such, it will be several kilobytes large. This is
160 /// likely fine as long as it is stack-allocated for function emission then
161 /// thrown away; but beware if many buffer objects are retained persistently.
162 pub struct MachBuffer<I: VCodeInst> {
163     /// The buffer contents, as raw bytes.
164     data: SmallVec<[u8; 1024]>,
165     /// Any relocations referring to this code. Note that only *external*
166     /// relocations are tracked here; references to labels within the buffer are
167     /// resolved before emission.
168     relocs: SmallVec<[MachReloc; 16]>,
169     /// Any trap records referring to this code.
170     traps: SmallVec<[MachTrap; 16]>,
171     /// Any call site records referring to this code.
172     call_sites: SmallVec<[MachCallSite; 16]>,
173     /// Any source location mappings referring to this code.
174     srclocs: SmallVec<[MachSrcLoc; 64]>,
175     /// Any stack maps referring to this code.
176     stack_maps: SmallVec<[MachStackMap; 8]>,
177     /// Any unwind info at a given location.
178     unwind_info: SmallVec<[(CodeOffset, UnwindInst); 8]>,
179     /// The current source location in progress (after `start_srcloc()` and
180     /// before `end_srcloc()`).  This is a (start_offset, src_loc) tuple.
181     cur_srcloc: Option<(CodeOffset, SourceLoc)>,
182     /// Known label offsets; `UNKNOWN_LABEL_OFFSET` if unknown.
183     label_offsets: SmallVec<[CodeOffset; 16]>,
184     /// Label aliases: when one label points to an unconditional jump, and that
185     /// jump points to another label, we can redirect references to the first
186     /// label immediately to the second.
187     ///
188     /// Invariant: we don't have label-alias cycles. We ensure this by,
189     /// before setting label A to alias label B, resolving B's alias
190     /// target (iteratively until a non-aliased label); if B is already
191     /// aliased to A, then we cannot alias A back to B.
192     label_aliases: SmallVec<[MachLabel; 16]>,
193     /// Constants that must be emitted at some point.
194     pending_constants: SmallVec<[MachLabelConstant; 16]>,
195     /// Fixups that must be performed after all code is emitted.
196     fixup_records: SmallVec<[MachLabelFixup<I>; 16]>,
197     /// Current deadline at which all constants are flushed and all code labels
198     /// are extended by emitting long-range jumps in an island. This flush
199     /// should be rare (e.g., on AArch64, the shortest-range PC-rel references
200     /// are +/- 1MB for conditional jumps and load-literal instructions), so
201     /// it's acceptable to track a minimum and flush-all rather than doing more
202     /// detailed "current minimum" / sort-by-deadline trickery.
203     island_deadline: CodeOffset,
204     /// How many bytes are needed in the worst case for an island, given all
205     /// pending constants and fixups.
206     island_worst_case_size: CodeOffset,
207     /// Latest branches, to facilitate in-place editing for better fallthrough
208     /// behavior and empty-block removal.
209     latest_branches: SmallVec<[MachBranch; 4]>,
210     /// All labels at the current offset (emission tail). This is lazily
211     /// cleared: it is actually accurate as long as the current offset is
212     /// `labels_at_tail_off`, but if `cur_offset()` has grown larger, it should
213     /// be considered as empty.
214     ///
215     /// For correctness, this *must* be complete (i.e., the vector must contain
216     /// all labels whose offsets are resolved to the current tail), because we
217     /// rely on it to update labels when we truncate branches.
218     labels_at_tail: SmallVec<[MachLabel; 4]>,
219     /// The last offset at which `labels_at_tail` is valid. It is conceptually
220     /// always describing the tail of the buffer, but we do not clear
221     /// `labels_at_tail` eagerly when the tail grows, rather we lazily clear it
222     /// when the offset has grown past this (`labels_at_tail_off`) point.
223     /// Always <= `cur_offset()`.
224     labels_at_tail_off: CodeOffset,
225     /// Map used constants to their [MachLabel].
226     constant_labels: SecondaryMap<VCodeConstant, MachLabel>,
227 }
228 
229 /// A `MachBuffer` once emission is completed: holds generated code and records,
230 /// without fixups. This allows the type to be independent of the backend.
231 pub struct MachBufferFinalized {
232     /// The buffer contents, as raw bytes.
233     pub data: SmallVec<[u8; 1024]>,
234     /// Any relocations referring to this code. Note that only *external*
235     /// relocations are tracked here; references to labels within the buffer are
236     /// resolved before emission.
237     relocs: SmallVec<[MachReloc; 16]>,
238     /// Any trap records referring to this code.
239     traps: SmallVec<[MachTrap; 16]>,
240     /// Any call site records referring to this code.
241     call_sites: SmallVec<[MachCallSite; 16]>,
242     /// Any source location mappings referring to this code.
243     srclocs: SmallVec<[MachSrcLoc; 64]>,
244     /// Any stack maps referring to this code.
245     stack_maps: SmallVec<[MachStackMap; 8]>,
246     /// Any unwind info at a given location.
247     pub unwind_info: SmallVec<[(CodeOffset, UnwindInst); 8]>,
248 }
249 
250 static UNKNOWN_LABEL_OFFSET: CodeOffset = 0xffff_ffff;
251 static UNKNOWN_LABEL: MachLabel = MachLabel(0xffff_ffff);
252 
253 /// A label refers to some offset in a `MachBuffer`. It may not be resolved at
254 /// the point at which it is used by emitted code; the buffer records "fixups"
255 /// for references to the label, and will come back and patch the code
256 /// appropriately when the label's location is eventually known.
257 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
258 pub struct MachLabel(u32);
259 entity_impl!(MachLabel);
260 
261 impl MachLabel {
262     /// Get a label for a block. (The first N MachLabels are always reseved for
263     /// the N blocks in the vcode.)
264     pub fn from_block(bindex: BlockIndex) -> MachLabel {
265         MachLabel(bindex)
266     }
267 
268     /// Get the numeric label index.
269     pub fn get(self) -> u32 {
270         self.0
271     }
272 
273     /// Creates a string representing this label, for convenience.
274     pub fn to_string(&self) -> String {
275         format!("label{}", self.0)
276     }
277 }
278 
279 impl Default for MachLabel {
280     fn default() -> Self {
281         UNKNOWN_LABEL
282     }
283 }
284 
285 /// A stack map extent, when creating a stack map.
286 pub enum StackMapExtent {
287     /// The stack map starts at this instruction, and ends after the number of upcoming bytes
288     /// (note: this is a code offset diff).
289     UpcomingBytes(CodeOffset),
290 
291     /// The stack map started at the given offset and ends at the current one. This helps
292     /// architectures where the instruction size has not a fixed length.
293     StartedAtOffset(CodeOffset),
294 }
295 
296 impl<I: VCodeInst> MachBuffer<I> {
297     /// Create a new section, known to start at `start_offset` and with a size limited to
298     /// `length_limit`.
299     pub fn new() -> MachBuffer<I> {
300         MachBuffer {
301             data: SmallVec::new(),
302             relocs: SmallVec::new(),
303             traps: SmallVec::new(),
304             call_sites: SmallVec::new(),
305             srclocs: SmallVec::new(),
306             stack_maps: SmallVec::new(),
307             unwind_info: SmallVec::new(),
308             cur_srcloc: None,
309             label_offsets: SmallVec::new(),
310             label_aliases: SmallVec::new(),
311             pending_constants: SmallVec::new(),
312             fixup_records: SmallVec::new(),
313             island_deadline: UNKNOWN_LABEL_OFFSET,
314             island_worst_case_size: 0,
315             latest_branches: SmallVec::new(),
316             labels_at_tail: SmallVec::new(),
317             labels_at_tail_off: 0,
318             constant_labels: SecondaryMap::new(),
319         }
320     }
321 
322     /// Debug-only: check invariants of labels and branch-records described
323     /// under "Branch-optimization Correctness" above.
324     #[cfg(debug)]
325     fn check_label_branch_invariants(&self) {
326         let cur_off = self.cur_offset();
327         // Check that every entry in latest_branches has *correct*
328         // labels_at_this_branch lists. We do not check completeness because
329         // that would require building a reverse index, which is too slow even
330         // for a debug invariant check.
331         let mut last_end = 0;
332         for b in &self.latest_branches {
333             debug_assert!(b.start < b.end);
334             debug_assert!(b.end <= cur_off);
335             debug_assert!(b.start >= last_end);
336             last_end = b.end;
337             for &l in &b.labels_at_this_branch {
338                 debug_assert_eq!(self.resolve_label_offset(l), b.start);
339                 debug_assert_eq!(self.label_aliases[l.0 as usize], UNKNOWN_LABEL);
340             }
341         }
342 
343         // Check that every label is unresolved, or resolved at or before
344         // cur_offset. If at cur_offset, must be in `labels_at_tail`.
345         for (i, &off) in self.label_offsets.iter().enumerate() {
346             let label = MachLabel(i as u32);
347             debug_assert!(off == UNKNOWN_LABEL_OFFSET || off <= cur_off);
348             if off == cur_off {
349                 debug_assert!(
350                     self.labels_at_tail_off == cur_off && self.labels_at_tail.contains(&label)
351                 );
352             }
353         }
354 
355         // Check that every label in `labels_at_tail_off` is precise, i.e.,
356         // resolves to the cur offset.
357         debug_assert!(self.labels_at_tail_off <= cur_off);
358         if self.labels_at_tail_off == cur_off {
359             for &l in &self.labels_at_tail {
360                 debug_assert_eq!(self.resolve_label_offset(l), cur_off);
361                 debug_assert_eq!(self.label_aliases[l.0 as usize], UNKNOWN_LABEL);
362             }
363         }
364     }
365 
366     #[cfg(not(debug))]
367     fn check_label_branch_invariants(&self) {
368         // Nothing.
369     }
370 
371     /// Current offset from start of buffer.
372     pub fn cur_offset(&self) -> CodeOffset {
373         self.data.len() as CodeOffset
374     }
375 
376     /// Add a byte.
377     pub fn put1(&mut self, value: u8) {
378         trace!("MachBuffer: put byte @ {}: {:x}", self.cur_offset(), value);
379         self.data.push(value);
380 
381         // Post-invariant: conceptual-labels_at_tail contains a complete and
382         // precise list of labels bound at `cur_offset()`. We have advanced
383         // `cur_offset()`, hence if it had been equal to `labels_at_tail_off`
384         // before, it is not anymore (and it cannot become equal, because
385         // `labels_at_tail_off` is always <= `cur_offset()`). Thus the list is
386         // conceptually empty (even though it is only lazily cleared). No labels
387         // can be bound at this new offset (by invariant on `label_offsets`).
388         // Hence the invariant holds.
389     }
390 
391     /// Add 2 bytes.
392     pub fn put2(&mut self, value: u16) {
393         trace!(
394             "MachBuffer: put 16-bit word @ {}: {:x}",
395             self.cur_offset(),
396             value
397         );
398         let bytes = value.to_le_bytes();
399         self.data.extend_from_slice(&bytes[..]);
400 
401         // Post-invariant: as for `put1()`.
402     }
403 
404     /// Add 4 bytes.
405     pub fn put4(&mut self, value: u32) {
406         trace!(
407             "MachBuffer: put 32-bit word @ {}: {:x}",
408             self.cur_offset(),
409             value
410         );
411         let bytes = value.to_le_bytes();
412         self.data.extend_from_slice(&bytes[..]);
413 
414         // Post-invariant: as for `put1()`.
415     }
416 
417     /// Add 8 bytes.
418     pub fn put8(&mut self, value: u64) {
419         trace!(
420             "MachBuffer: put 64-bit word @ {}: {:x}",
421             self.cur_offset(),
422             value
423         );
424         let bytes = value.to_le_bytes();
425         self.data.extend_from_slice(&bytes[..]);
426 
427         // Post-invariant: as for `put1()`.
428     }
429 
430     /// Add a slice of bytes.
431     pub fn put_data(&mut self, data: &[u8]) {
432         trace!(
433             "MachBuffer: put data @ {}: len {}",
434             self.cur_offset(),
435             data.len()
436         );
437         self.data.extend_from_slice(data);
438 
439         // Post-invariant: as for `put1()`.
440     }
441 
442     /// Reserve appended space and return a mutable slice referring to it.
443     pub fn get_appended_space(&mut self, len: usize) -> &mut [u8] {
444         trace!("MachBuffer: put data @ {}: len {}", self.cur_offset(), len);
445         let off = self.data.len();
446         let new_len = self.data.len() + len;
447         self.data.resize(new_len, 0);
448         &mut self.data[off..]
449 
450         // Post-invariant: as for `put1()`.
451     }
452 
453     /// Align up to the given alignment.
454     pub fn align_to(&mut self, align_to: CodeOffset) {
455         trace!("MachBuffer: align to {}", align_to);
456         assert!(align_to.is_power_of_two());
457         while self.cur_offset() & (align_to - 1) != 0 {
458             self.put1(0);
459         }
460 
461         // Post-invariant: as for `put1()`.
462     }
463 
464     /// Allocate a `Label` to refer to some offset. May not be bound to a fixed
465     /// offset yet.
466     pub fn get_label(&mut self) -> MachLabel {
467         let l = self.label_offsets.len() as u32;
468         self.label_offsets.push(UNKNOWN_LABEL_OFFSET);
469         self.label_aliases.push(UNKNOWN_LABEL);
470         trace!("MachBuffer: new label -> {:?}", MachLabel(l));
471         MachLabel(l)
472 
473         // Post-invariant: the only mutation is to add a new label; it has no
474         // bound offset yet, so it trivially satisfies all invariants.
475     }
476 
477     /// Reserve the first N MachLabels for blocks.
478     pub fn reserve_labels_for_blocks(&mut self, blocks: BlockIndex) {
479         trace!("MachBuffer: first {} labels are for blocks", blocks);
480         debug_assert!(self.label_offsets.is_empty());
481         self.label_offsets
482             .resize(blocks as usize, UNKNOWN_LABEL_OFFSET);
483         self.label_aliases.resize(blocks as usize, UNKNOWN_LABEL);
484 
485         // Post-invariant: as for `get_label()`.
486     }
487 
488     /// Reserve the next N MachLabels for constants.
489     pub fn reserve_labels_for_constants(&mut self, constants: &VCodeConstants) {
490         trace!(
491             "MachBuffer: next {} labels are for constants",
492             constants.len()
493         );
494         for c in constants.keys() {
495             self.constant_labels[c] = self.get_label();
496         }
497 
498         // Post-invariant: as for `get_label()`.
499     }
500 
501     /// Retrieve the reserved label for a constant.
502     pub fn get_label_for_constant(&self, constant: VCodeConstant) -> MachLabel {
503         self.constant_labels[constant]
504     }
505 
506     /// Bind a label to the current offset. A label can only be bound once.
507     pub fn bind_label(&mut self, label: MachLabel) {
508         trace!(
509             "MachBuffer: bind label {:?} at offset {}",
510             label,
511             self.cur_offset()
512         );
513         debug_assert_eq!(self.label_offsets[label.0 as usize], UNKNOWN_LABEL_OFFSET);
514         debug_assert_eq!(self.label_aliases[label.0 as usize], UNKNOWN_LABEL);
515         let offset = self.cur_offset();
516         self.label_offsets[label.0 as usize] = offset;
517         self.lazily_clear_labels_at_tail();
518         self.labels_at_tail.push(label);
519 
520         // Invariants hold: bound offset of label is <= cur_offset (in fact it
521         // is equal). If the `labels_at_tail` list was complete and precise
522         // before, it is still, because we have bound this label to the current
523         // offset and added it to the list (which contains all labels at the
524         // current offset).
525 
526         self.check_label_branch_invariants();
527         self.optimize_branches();
528 
529         // Post-invariant: by `optimize_branches()` (see argument there).
530         self.check_label_branch_invariants();
531     }
532 
533     /// Lazily clear `labels_at_tail` if the tail offset has moved beyond the
534     /// offset that it applies to.
535     fn lazily_clear_labels_at_tail(&mut self) {
536         let offset = self.cur_offset();
537         if offset > self.labels_at_tail_off {
538             self.labels_at_tail_off = offset;
539             self.labels_at_tail.clear();
540         }
541 
542         // Post-invariant: either labels_at_tail_off was at cur_offset, and
543         // state is untouched, or was less than cur_offset, in which case the
544         // labels_at_tail list was conceptually empty, and is now actually
545         // empty.
546     }
547 
548     /// Resolve a label to an offset, if known. May return `UNKNOWN_LABEL_OFFSET`.
549     fn resolve_label_offset(&self, mut label: MachLabel) -> CodeOffset {
550         let mut iters = 0;
551         while self.label_aliases[label.0 as usize] != UNKNOWN_LABEL {
552             label = self.label_aliases[label.0 as usize];
553             // To protect against an infinite loop (despite our assurances to
554             // ourselves that the invariants make this impossible), assert out
555             // after 1M iterations. The number of basic blocks is limited
556             // in most contexts anyway so this should be impossible to hit with
557             // a legitimate input.
558             iters += 1;
559             assert!(iters < 1_000_000, "Unexpected cycle in label aliases");
560         }
561         self.label_offsets[label.0 as usize]
562 
563         // Post-invariant: no mutations.
564     }
565 
566     /// Emit a reference to the given label with the given reference type (i.e.,
567     /// branch-instruction format) at the current offset.  This is like a
568     /// relocation, but handled internally.
569     ///
570     /// This can be called before the branch is actually emitted; fixups will
571     /// not happen until an island is emitted or the buffer is finished.
572     pub fn use_label_at_offset(&mut self, offset: CodeOffset, label: MachLabel, kind: I::LabelUse) {
573         trace!(
574             "MachBuffer: use_label_at_offset: offset {} label {:?} kind {:?}",
575             offset,
576             label,
577             kind
578         );
579 
580         // Add the fixup, and update the worst-case island size based on a
581         // veneer for this label use.
582         self.fixup_records.push(MachLabelFixup {
583             label,
584             offset,
585             kind,
586         });
587         if kind.supports_veneer() {
588             self.island_worst_case_size += kind.veneer_size();
589             self.island_worst_case_size &= !(I::LabelUse::ALIGN - 1);
590         }
591         let deadline = offset + kind.max_pos_range();
592         if deadline < self.island_deadline {
593             self.island_deadline = deadline;
594         }
595 
596         // Post-invariant: no mutations to branches/labels data structures.
597         self.check_label_branch_invariants();
598     }
599 
600     /// Inform the buffer of an unconditional branch at the given offset,
601     /// targetting the given label. May be used to optimize branches.
602     /// The last added label-use must correspond to this branch.
603     /// This must be called when the current offset is equal to `start`; i.e.,
604     /// before actually emitting the branch. This implies that for a branch that
605     /// uses a label and is eligible for optimizations by the MachBuffer, the
606     /// proper sequence is:
607     ///
608     /// - Call `use_label_at_offset()` to emit the fixup record.
609     /// - Call `add_uncond_branch()` to make note of the branch.
610     /// - Emit the bytes for the branch's machine code.
611     ///
612     /// Additional requirement: no labels may be bound between `start` and `end`
613     /// (exclusive on both ends).
614     pub fn add_uncond_branch(&mut self, start: CodeOffset, end: CodeOffset, target: MachLabel) {
615         assert!(self.cur_offset() == start);
616         debug_assert!(end > start);
617         assert!(!self.fixup_records.is_empty());
618         let fixup = self.fixup_records.len() - 1;
619         self.lazily_clear_labels_at_tail();
620         self.latest_branches.push(MachBranch {
621             start,
622             end,
623             target,
624             fixup,
625             inverted: None,
626             labels_at_this_branch: self.labels_at_tail.clone(),
627         });
628 
629         // Post-invariant: we asserted branch start is current tail; the list of
630         // labels at branch is cloned from list of labels at current tail.
631         self.check_label_branch_invariants();
632     }
633 
634     /// Inform the buffer of a conditional branch at the given offset,
635     /// targetting the given label. May be used to optimize branches.
636     /// The last added label-use must correspond to this branch.
637     ///
638     /// Additional requirement: no labels may be bound between `start` and `end`
639     /// (exclusive on both ends).
640     pub fn add_cond_branch(
641         &mut self,
642         start: CodeOffset,
643         end: CodeOffset,
644         target: MachLabel,
645         inverted: &[u8],
646     ) {
647         assert!(self.cur_offset() == start);
648         debug_assert!(end > start);
649         assert!(!self.fixup_records.is_empty());
650         debug_assert!(inverted.len() == (end - start) as usize);
651         let fixup = self.fixup_records.len() - 1;
652         let inverted = Some(SmallVec::from(inverted));
653         self.lazily_clear_labels_at_tail();
654         self.latest_branches.push(MachBranch {
655             start,
656             end,
657             target,
658             fixup,
659             inverted,
660             labels_at_this_branch: self.labels_at_tail.clone(),
661         });
662 
663         // Post-invariant: we asserted branch start is current tail; labels at
664         // branch list is cloned from list of labels at current tail.
665         self.check_label_branch_invariants();
666     }
667 
668     fn truncate_last_branch(&mut self) {
669         self.lazily_clear_labels_at_tail();
670         // Invariants hold at this point.
671 
672         let b = self.latest_branches.pop().unwrap();
673         assert!(b.end == self.cur_offset());
674 
675         // State:
676         //    [PRE CODE]
677         //  Offset b.start, b.labels_at_this_branch:
678         //    [BRANCH CODE]
679         //  cur_off, self.labels_at_tail -->
680         //    (end of buffer)
681         self.data.truncate(b.start as usize);
682         self.fixup_records.truncate(b.fixup);
683         while let Some(last_srcloc) = self.srclocs.last() {
684             if last_srcloc.end <= b.start {
685                 break;
686             }
687             self.srclocs.pop();
688         }
689         // State:
690         //    [PRE CODE]
691         //  cur_off, Offset b.start, b.labels_at_this_branch:
692         //    (end of buffer)
693         //
694         //  self.labels_at_tail -->  (past end of buffer)
695         let cur_off = self.cur_offset();
696         self.labels_at_tail_off = cur_off;
697         // State:
698         //    [PRE CODE]
699         //  cur_off, Offset b.start, b.labels_at_this_branch,
700         //  self.labels_at_tail:
701         //    (end of buffer)
702         //
703         // resolve_label_offset(l) for l in labels_at_tail:
704         //    (past end of buffer)
705 
706         trace!(
707             "truncate_last_branch: truncated {:?}; off now {}",
708             b,
709             cur_off
710         );
711 
712         // Fix up resolved label offsets for labels at tail.
713         for &l in &self.labels_at_tail {
714             self.label_offsets[l.0 as usize] = cur_off;
715         }
716         // Old labels_at_this_branch are now at cur_off.
717         self.labels_at_tail
718             .extend(b.labels_at_this_branch.into_iter());
719 
720         // Post-invariant: this operation is defined to truncate the buffer,
721         // which moves cur_off backward, and to move labels at the end of the
722         // buffer back to the start-of-branch offset.
723         //
724         // latest_branches satisfies all invariants:
725         // - it has no branches past the end of the buffer (branches are in
726         //   order, we removed the last one, and we truncated the buffer to just
727         //   before the start of that branch)
728         // - no labels were moved to lower offsets than the (new) cur_off, so
729         //   the labels_at_this_branch list for any other branch need not change.
730         //
731         // labels_at_tail satisfies all invariants:
732         // - all labels that were at the tail after the truncated branch are
733         //   moved backward to just before the branch, which becomes the new tail;
734         //   thus every element in the list should remain (ensured by `.extend()`
735         //   above).
736         // - all labels that refer to the new tail, which is the start-offset of
737         //   the truncated branch, must be present. The `labels_at_this_branch`
738         //   list in the truncated branch's record is a complete and precise list
739         //   of exactly these labels; we append these to labels_at_tail.
740         // - labels_at_tail_off is at cur_off after truncation occurs, so the
741         //   list is valid (not to be lazily cleared).
742         //
743         // The stated operation was performed:
744         // - For each label at the end of the buffer prior to this method, it
745         //   now resolves to the new (truncated) end of the buffer: it must have
746         //   been in `labels_at_tail` (this list is precise and complete, and
747         //   the tail was at the end of the truncated branch on entry), and we
748         //   iterate over this list and set `label_offsets` to the new tail.
749         //   None of these labels could have been an alias (by invariant), so
750         //   `label_offsets` is authoritative for each.
751         // - No other labels will be past the end of the buffer, because of the
752         //   requirement that no labels be bound to the middle of branch ranges
753         //   (see comments to `add_{cond,uncond}_branch()`).
754         // - The buffer is truncated to just before the last branch, and the
755         //   fixup record referring to that last branch is removed.
756         self.check_label_branch_invariants();
757     }
758 
759     fn optimize_branches(&mut self) {
760         self.lazily_clear_labels_at_tail();
761         // Invariants valid at this point.
762 
763         trace!(
764             "enter optimize_branches:\n b = {:?}\n l = {:?}\n f = {:?}",
765             self.latest_branches,
766             self.labels_at_tail,
767             self.fixup_records
768         );
769 
770         // We continue to munch on branches at the tail of the buffer until no
771         // more rules apply. Note that the loop only continues if a branch is
772         // actually truncated (or if labels are redirected away from a branch),
773         // so this always makes progress.
774         while let Some(b) = self.latest_branches.last() {
775             let cur_off = self.cur_offset();
776             trace!("optimize_branches: last branch {:?} at off {}", b, cur_off);
777             // If there has been any code emission since the end of the last branch or
778             // label definition, then there's nothing we can edit (because we
779             // don't move code once placed, only back up and overwrite), so
780             // clear the records and finish.
781             if b.end < cur_off {
782                 break;
783             }
784 
785             // Invariant: we are looking at a branch that ends at the tail of
786             // the buffer.
787 
788             // For any branch, conditional or unconditional:
789             // - If the target is a label at the current offset, then remove
790             //   the conditional branch, and reset all labels that targetted
791             //   the current offset (end of branch) to the truncated
792             //   end-of-code.
793             //
794             // Preserves execution semantics: a branch to its own fallthrough
795             // address is equivalent to a no-op; in both cases, nextPC is the
796             // fallthrough.
797             if self.resolve_label_offset(b.target) == cur_off {
798                 trace!("branch with target == cur off; truncating");
799                 self.truncate_last_branch();
800                 continue;
801             }
802 
803             // If latest is an unconditional branch:
804             //
805             // - If the branch's target is not its own start address, then for
806             //   each label at the start of branch, make the label an alias of the
807             //   branch target, and remove the label from the "labels at this
808             //   branch" list.
809             //
810             //   - Preserves execution semantics: an unconditional branch's
811             //     only effect is to set PC to a new PC; this change simply
812             //     collapses one step in the step-semantics.
813             //
814             //   - Post-invariant: the labels that were bound to the start of
815             //     this branch become aliases, so they must not be present in any
816             //     labels-at-this-branch list or the labels-at-tail list. The
817             //     labels are removed form the latest-branch record's
818             //     labels-at-this-branch list, and are never placed in the
819             //     labels-at-tail list. Furthermore, it is correct that they are
820             //     not in either list, because they are now aliases, and labels
821             //     that are aliases remain aliases forever.
822             //
823             // - If there is a prior unconditional branch that ends just before
824             //   this one begins, and this branch has no labels bound to its
825             //   start, then we can truncate this branch, because it is entirely
826             //   unreachable (we have redirected all labels that make it
827             //   reachable otherwise). Do so and continue around the loop.
828             //
829             //   - Preserves execution semantics: the branch is unreachable,
830             //     because execution can only flow into an instruction from the
831             //     prior instruction's fallthrough or from a branch bound to that
832             //     instruction's start offset. Unconditional branches have no
833             //     fallthrough, so if the prior instruction is an unconditional
834             //     branch, no fallthrough entry can happen. The
835             //     labels-at-this-branch list is complete (by invariant), so if it
836             //     is empty, then the instruction is entirely unreachable. Thus,
837             //     it can be removed.
838             //
839             //   - Post-invariant: ensured by truncate_last_branch().
840             //
841             // - If there is a prior conditional branch whose target label
842             //   resolves to the current offset (branches around the
843             //   unconditional branch), then remove the unconditional branch,
844             //   and make the target of the unconditional the target of the
845             //   conditional instead.
846             //
847             //   - Preserves execution semantics: previously we had:
848             //
849             //         L1:
850             //            cond_br L2
851             //            br L3
852             //         L2:
853             //            (end of buffer)
854             //
855             //     by removing the last branch, we have:
856             //
857             //         L1:
858             //            cond_br L2
859             //         L2:
860             //            (end of buffer)
861             //
862             //     we then fix up the records for the conditional branch to
863             //     have:
864             //
865             //         L1:
866             //           cond_br.inverted L3
867             //         L2:
868             //
869             //     In the original code, control flow reaches L2 when the
870             //     conditional branch's predicate is true, and L3 otherwise. In
871             //     the optimized code, the same is true.
872             //
873             //   - Post-invariant: all edits to latest_branches and
874             //     labels_at_tail are performed by `truncate_last_branch()`,
875             //     which maintains the invariants at each step.
876 
877             if b.is_uncond() {
878                 // Set any label equal to current branch's start as an alias of
879                 // the branch's target, if the target is not the branch itself
880                 // (i.e., an infinite loop).
881                 //
882                 // We cannot perform this aliasing if the target of this branch
883                 // ultimately aliases back here; if so, we need to keep this
884                 // branch, so break out of this loop entirely (and clear the
885                 // latest-branches list below).
886                 //
887                 // Note that this check is what prevents cycles from forming in
888                 // `self.label_aliases`. To see why, consider an arbitrary start
889                 // state:
890                 //
891                 // label_aliases[L1] = L2, label_aliases[L2] = L3, ..., up to
892                 // Ln, which is not aliased.
893                 //
894                 // We would create a cycle if we assigned label_aliases[Ln]
895                 // = L1.  Note that the below assignment is the only write
896                 // to label_aliases.
897                 //
898                 // By our other invariants, we have that Ln (`l` below)
899                 // resolves to the offset `b.start`, because it is in the
900                 // set `b.labels_at_this_branch`.
901                 //
902                 // If L1 were already aliased, through some arbitrarily deep
903                 // chain, to Ln, then it must also resolve to this offset
904                 // `b.start`.
905                 //
906                 // By checking the resolution of `L1` against this offset,
907                 // and aborting this branch-simplification if they are
908                 // equal, we prevent the below assignment from ever creating
909                 // a cycle.
910                 if self.resolve_label_offset(b.target) != b.start {
911                     let redirected = b.labels_at_this_branch.len();
912                     for &l in &b.labels_at_this_branch {
913                         trace!(
914                             " -> label at start of branch {:?} redirected to target {:?}",
915                             l,
916                             b.target
917                         );
918                         self.label_aliases[l.0 as usize] = b.target;
919                         // NOTE: we continue to ensure the invariant that labels
920                         // pointing to tail of buffer are in `labels_at_tail`
921                         // because we already ensured above that the last branch
922                         // cannot have a target of `cur_off`; so we never have
923                         // to put the label into `labels_at_tail` when moving it
924                         // here.
925                     }
926                     // Maintain invariant: all branches have been redirected
927                     // and are no longer pointing at the start of this branch.
928                     let mut_b = self.latest_branches.last_mut().unwrap();
929                     mut_b.labels_at_this_branch.clear();
930 
931                     if redirected > 0 {
932                         trace!(" -> after label redirects, restarting loop");
933                         continue;
934                     }
935                 } else {
936                     break;
937                 }
938 
939                 let b = self.latest_branches.last().unwrap();
940 
941                 // Examine any immediately preceding branch.
942                 if self.latest_branches.len() > 1 {
943                     let prev_b = &self.latest_branches[self.latest_branches.len() - 2];
944                     trace!(" -> more than one branch; prev_b = {:?}", prev_b);
945                     // This uncond is immediately after another uncond; we
946                     // should have already redirected labels to this uncond away
947                     // (but check to be sure); so we can truncate this uncond.
948                     if prev_b.is_uncond()
949                         && prev_b.end == b.start
950                         && b.labels_at_this_branch.is_empty()
951                     {
952                         trace!(" -> uncond follows another uncond; truncating");
953                         self.truncate_last_branch();
954                         continue;
955                     }
956 
957                     // This uncond is immediately after a conditional, and the
958                     // conditional's target is the end of this uncond, and we've
959                     // already redirected labels to this uncond away; so we can
960                     // truncate this uncond, flip the sense of the conditional, and
961                     // set the conditional's target (in `latest_branches` and in
962                     // `fixup_records`) to the uncond's target.
963                     if prev_b.is_cond()
964                         && prev_b.end == b.start
965                         && self.resolve_label_offset(prev_b.target) == cur_off
966                     {
967                         trace!(" -> uncond follows a conditional, and conditional's target resolves to current offset");
968                         // Save the target of the uncond (this becomes the
969                         // target of the cond), and truncate the uncond.
970                         let target = b.target;
971                         let data = prev_b.inverted.clone().unwrap();
972                         self.truncate_last_branch();
973 
974                         // Mutate the code and cond branch.
975                         let off_before_edit = self.cur_offset();
976                         let prev_b = self.latest_branches.last_mut().unwrap();
977                         let not_inverted = SmallVec::from(
978                             &self.data[(prev_b.start as usize)..(prev_b.end as usize)],
979                         );
980 
981                         // Low-level edit: replaces bytes of branch with
982                         // inverted form. cur_off remains the same afterward, so
983                         // we do not need to modify label data structures.
984                         self.data.truncate(prev_b.start as usize);
985                         self.data.extend_from_slice(&data[..]);
986 
987                         // Save the original code as the inversion of the
988                         // inverted branch, in case we later edit this branch
989                         // again.
990                         prev_b.inverted = Some(not_inverted);
991                         self.fixup_records[prev_b.fixup].label = target;
992                         trace!(" -> reassigning target of condbr to {:?}", target);
993                         prev_b.target = target;
994                         debug_assert_eq!(off_before_edit, self.cur_offset());
995                         continue;
996                     }
997                 }
998             }
999 
1000             // If we couldn't do anything with the last branch, then break.
1001             break;
1002         }
1003 
1004         self.purge_latest_branches();
1005 
1006         trace!(
1007             "leave optimize_branches:\n b = {:?}\n l = {:?}\n f = {:?}",
1008             self.latest_branches,
1009             self.labels_at_tail,
1010             self.fixup_records
1011         );
1012     }
1013 
1014     fn purge_latest_branches(&mut self) {
1015         // All of our branch simplification rules work only if a branch ends at
1016         // the tail of the buffer, with no following code; and branches are in
1017         // order in latest_branches; so if the last entry ends prior to
1018         // cur_offset, then clear all entries.
1019         let cur_off = self.cur_offset();
1020         if let Some(l) = self.latest_branches.last() {
1021             if l.end < cur_off {
1022                 trace!("purge_latest_branches: removing branch {:?}", l);
1023                 self.latest_branches.clear();
1024             }
1025         }
1026 
1027         // Post-invariant: no invariant requires any branch to appear in
1028         // `latest_branches`; it is always optional. The list-clear above thus
1029         // preserves all semantics.
1030     }
1031 
1032     /// Emit a constant at some point in the future, binding the given label to
1033     /// its offset. The constant will be placed at most `max_distance` from the
1034     /// current offset.
1035     pub fn defer_constant(
1036         &mut self,
1037         label: MachLabel,
1038         align: CodeOffset,
1039         data: &[u8],
1040         max_distance: CodeOffset,
1041     ) {
1042         trace!(
1043             "defer_constant: eventually emit {} bytes aligned to {} at label {:?}",
1044             data.len(),
1045             align,
1046             label
1047         );
1048         let deadline = self.cur_offset().saturating_add(max_distance);
1049         self.island_worst_case_size += data.len() as CodeOffset;
1050         self.island_worst_case_size =
1051             (self.island_worst_case_size + I::LabelUse::ALIGN - 1) & !(I::LabelUse::ALIGN - 1);
1052         self.pending_constants.push(MachLabelConstant {
1053             label,
1054             align,
1055             data: SmallVec::from(data),
1056         });
1057         if deadline < self.island_deadline {
1058             self.island_deadline = deadline;
1059         }
1060     }
1061 
1062     /// Is an island needed within the next N bytes?
1063     pub fn island_needed(&self, distance: CodeOffset) -> bool {
1064         let worst_case_end_of_island = self.cur_offset() + distance + self.island_worst_case_size;
1065         worst_case_end_of_island > self.island_deadline
1066     }
1067 
1068     /// Emit all pending constants and veneers. Should only be called if
1069     /// `island_needed()` returns true, i.e., if we actually reach a deadline:
1070     /// otherwise, unnecessary veneers may be inserted.
1071     pub fn emit_island(&mut self) {
1072         // We're going to purge fixups, so no latest-branch editing can happen
1073         // anymore.
1074         self.latest_branches.clear();
1075 
1076         let pending_constants = mem::replace(&mut self.pending_constants, SmallVec::new());
1077         for MachLabelConstant { label, align, data } in pending_constants.into_iter() {
1078             self.align_to(align);
1079             self.bind_label(label);
1080             self.put_data(&data[..]);
1081         }
1082 
1083         let fixup_records = mem::replace(&mut self.fixup_records, SmallVec::new());
1084         let mut new_fixups = SmallVec::new();
1085         for MachLabelFixup {
1086             label,
1087             offset,
1088             kind,
1089         } in fixup_records.into_iter()
1090         {
1091             trace!(
1092                 "emit_island: fixup for label {:?} at offset {} kind {:?}",
1093                 label,
1094                 offset,
1095                 kind
1096             );
1097             // We eagerly perform fixups whose label targets are known, if not out
1098             // of range, to avoid unnecessary veneers.
1099             let label_offset = self.resolve_label_offset(label);
1100             let known = label_offset != UNKNOWN_LABEL_OFFSET;
1101             let in_range = if known {
1102                 if label_offset >= offset {
1103                     (label_offset - offset) <= kind.max_pos_range()
1104                 } else {
1105                     (offset - label_offset) <= kind.max_neg_range()
1106                 }
1107             } else {
1108                 false
1109             };
1110 
1111             trace!(
1112                 " -> label_offset = {}, known = {}, in_range = {} (pos {} neg {})",
1113                 label_offset,
1114                 known,
1115                 in_range,
1116                 kind.max_pos_range(),
1117                 kind.max_neg_range()
1118             );
1119 
1120             let start = offset as usize;
1121             let end = (offset + kind.patch_size()) as usize;
1122             if in_range {
1123                 debug_assert!(known); // implied by in_range.
1124                 let slice = &mut self.data[start..end];
1125                 trace!("patching in-range!");
1126                 kind.patch(slice, offset, label_offset);
1127             } else if !known && !kind.supports_veneer() {
1128                 // Nothing for now. Keep it for next round.
1129                 new_fixups.push(MachLabelFixup {
1130                     label,
1131                     offset,
1132                     kind,
1133                 });
1134             } else if !in_range && kind.supports_veneer() {
1135                 // Allocate space for a veneer in the island.
1136                 self.align_to(I::LabelUse::ALIGN);
1137                 let veneer_offset = self.cur_offset();
1138                 trace!("making a veneer at {}", veneer_offset);
1139                 let slice = &mut self.data[start..end];
1140                 // Patch the original label use to refer to the veneer.
1141                 trace!(
1142                     "patching original at offset {} to veneer offset {}",
1143                     offset,
1144                     veneer_offset
1145                 );
1146                 kind.patch(slice, offset, veneer_offset);
1147                 // Generate the veneer.
1148                 let veneer_slice = self.get_appended_space(kind.veneer_size() as usize);
1149                 let (veneer_fixup_off, veneer_label_use) =
1150                     kind.generate_veneer(veneer_slice, veneer_offset);
1151                 trace!(
1152                     "generated veneer; fixup offset {}, label_use {:?}",
1153                     veneer_fixup_off,
1154                     veneer_label_use
1155                 );
1156                 // If the label is known (but was just out of range), do the
1157                 // veneer label-use fixup now too; otherwise, save it for later.
1158                 if known {
1159                     let start = veneer_fixup_off as usize;
1160                     let end = (veneer_fixup_off + veneer_label_use.patch_size()) as usize;
1161                     let veneer_slice = &mut self.data[start..end];
1162                     trace!("doing veneer fixup right away too");
1163                     veneer_label_use.patch(veneer_slice, veneer_fixup_off, label_offset);
1164                 } else {
1165                     new_fixups.push(MachLabelFixup {
1166                         label,
1167                         offset: veneer_fixup_off,
1168                         kind: veneer_label_use,
1169                     });
1170                 }
1171             } else {
1172                 panic!(
1173                     "Cannot support label-use {:?} (known = {}, in-range = {})",
1174                     kind, known, in_range
1175                 );
1176             }
1177         }
1178 
1179         self.fixup_records = new_fixups;
1180         self.island_deadline = UNKNOWN_LABEL_OFFSET;
1181     }
1182 
1183     /// Finish any deferred emissions and/or fixups.
1184     pub fn finish(mut self) -> MachBufferFinalized {
1185         let _tt = timing::vcode_emit_finish();
1186 
1187         while !self.pending_constants.is_empty() || !self.fixup_records.is_empty() {
1188             // `emit_island()` will emit any pending veneers and constants, and
1189             // as a side-effect, will also take care of any fixups with resolved
1190             // labels eagerly.
1191             self.emit_island();
1192         }
1193 
1194         // Ensure that all labels have been fixed up after the last island is emitted. This is a
1195         // full (release-mode) assert because an unresolved label means the emitted code is
1196         // incorrect.
1197         assert!(self.fixup_records.is_empty());
1198 
1199         let mut srclocs = self.srclocs;
1200         srclocs.sort_by_key(|entry| entry.start);
1201 
1202         MachBufferFinalized {
1203             data: self.data,
1204             relocs: self.relocs,
1205             traps: self.traps,
1206             call_sites: self.call_sites,
1207             srclocs,
1208             stack_maps: self.stack_maps,
1209             unwind_info: self.unwind_info,
1210         }
1211     }
1212 
1213     /// Add an external relocation at the current offset.
1214     pub fn add_reloc(
1215         &mut self,
1216         srcloc: SourceLoc,
1217         kind: Reloc,
1218         name: &ExternalName,
1219         addend: Addend,
1220     ) {
1221         let name = name.clone();
1222         self.relocs.push(MachReloc {
1223             offset: self.data.len() as CodeOffset,
1224             srcloc,
1225             kind,
1226             name,
1227             addend,
1228         });
1229     }
1230 
1231     /// Add a trap record at the current offset.
1232     pub fn add_trap(&mut self, srcloc: SourceLoc, code: TrapCode) {
1233         self.traps.push(MachTrap {
1234             offset: self.data.len() as CodeOffset,
1235             srcloc,
1236             code,
1237         });
1238     }
1239 
1240     /// Add a call-site record at the current offset.
1241     pub fn add_call_site(&mut self, srcloc: SourceLoc, opcode: Opcode) {
1242         self.call_sites.push(MachCallSite {
1243             ret_addr: self.data.len() as CodeOffset,
1244             srcloc,
1245             opcode,
1246         });
1247     }
1248 
1249     /// Add an unwind record at the current offset.
1250     pub fn add_unwind(&mut self, unwind: UnwindInst) {
1251         self.unwind_info.push((self.cur_offset(), unwind));
1252     }
1253 
1254     /// Set the `SourceLoc` for code from this offset until the offset at the
1255     /// next call to `end_srcloc()`.
1256     pub fn start_srcloc(&mut self, loc: SourceLoc) {
1257         self.cur_srcloc = Some((self.cur_offset(), loc));
1258     }
1259 
1260     /// Mark the end of the `SourceLoc` segment started at the last
1261     /// `start_srcloc()` call.
1262     pub fn end_srcloc(&mut self) {
1263         let (start, loc) = self
1264             .cur_srcloc
1265             .take()
1266             .expect("end_srcloc() called without start_srcloc()");
1267         let end = self.cur_offset();
1268         // Skip zero-length extends.
1269         debug_assert!(end >= start);
1270         if end > start {
1271             self.srclocs.push(MachSrcLoc { start, end, loc });
1272         }
1273     }
1274 
1275     /// Add stack map metadata for this program point: a set of stack offsets
1276     /// (from SP upward) that contain live references.
1277     ///
1278     /// The `offset_to_fp` value is the offset from the nominal SP (at which the `stack_offsets`
1279     /// are based) and the FP value. By subtracting `offset_to_fp` from each `stack_offsets`
1280     /// element, one can obtain live-reference offsets from FP instead.
1281     pub fn add_stack_map(&mut self, extent: StackMapExtent, stack_map: StackMap) {
1282         let (start, end) = match extent {
1283             StackMapExtent::UpcomingBytes(insn_len) => {
1284                 let start_offset = self.cur_offset();
1285                 (start_offset, start_offset + insn_len)
1286             }
1287             StackMapExtent::StartedAtOffset(start_offset) => {
1288                 let end_offset = self.cur_offset();
1289                 debug_assert!(end_offset >= start_offset);
1290                 (start_offset, end_offset)
1291             }
1292         };
1293         self.stack_maps.push(MachStackMap {
1294             offset: start,
1295             offset_end: end,
1296             stack_map,
1297         });
1298     }
1299 }
1300 
1301 impl MachBufferFinalized {
1302     /// Get a list of source location mapping tuples in sorted-by-start-offset order.
1303     pub fn get_srclocs_sorted(&self) -> &[MachSrcLoc] {
1304         &self.srclocs[..]
1305     }
1306 
1307     /// Get the total required size for the code.
1308     pub fn total_size(&self) -> CodeOffset {
1309         self.data.len() as CodeOffset
1310     }
1311 
1312     /// Emit this buffer to the given CodeSink.
1313     pub fn emit<CS: CodeSink>(&self, sink: &mut CS) {
1314         // N.B.: we emit every section into the .text section as far as
1315         // the `CodeSink` is concerned; we do not bother to segregate
1316         // the contents into the actual program text, the jumptable and the
1317         // rodata (constant pool). This allows us to generate code assuming
1318         // that these will not be relocated relative to each other, and avoids
1319         // having to designate each section as belonging in one of the three
1320         // fixed categories defined by `CodeSink`. If this becomes a problem
1321         // later (e.g. because of memory permissions or similar), we can
1322         // add this designation and segregate the output; take care, however,
1323         // to add the appropriate relocations in this case.
1324 
1325         let mut next_reloc = 0;
1326         let mut next_trap = 0;
1327         let mut next_call_site = 0;
1328         for (idx, byte) in self.data.iter().enumerate() {
1329             while next_reloc < self.relocs.len()
1330                 && self.relocs[next_reloc].offset == idx as CodeOffset
1331             {
1332                 let reloc = &self.relocs[next_reloc];
1333                 sink.reloc_external(reloc.srcloc, reloc.kind, &reloc.name, reloc.addend);
1334                 next_reloc += 1;
1335             }
1336             while next_trap < self.traps.len() && self.traps[next_trap].offset == idx as CodeOffset
1337             {
1338                 let trap = &self.traps[next_trap];
1339                 sink.trap(trap.code, trap.srcloc);
1340                 next_trap += 1;
1341             }
1342             while next_call_site < self.call_sites.len()
1343                 && self.call_sites[next_call_site].ret_addr == idx as CodeOffset
1344             {
1345                 let call_site = &self.call_sites[next_call_site];
1346                 sink.add_call_site(call_site.opcode, call_site.srcloc);
1347                 next_call_site += 1;
1348             }
1349             sink.put1(*byte);
1350         }
1351 
1352         sink.begin_jumptables();
1353         sink.begin_rodata();
1354         sink.end_codegen();
1355     }
1356 
1357     /// Get the stack map metadata for this code.
1358     pub fn stack_maps(&self) -> &[MachStackMap] {
1359         &self.stack_maps[..]
1360     }
1361 }
1362 
1363 /// A constant that is deferred to the next constant-pool opportunity.
1364 struct MachLabelConstant {
1365     /// This label will refer to the constant's offset.
1366     label: MachLabel,
1367     /// Required alignment.
1368     align: CodeOffset,
1369     /// This data will be emitted when able.
1370     data: SmallVec<[u8; 16]>,
1371 }
1372 
1373 /// A fixup to perform on the buffer once code is emitted. Fixups always refer
1374 /// to labels and patch the code based on label offsets. Hence, they are like
1375 /// relocations, but internal to one buffer.
1376 #[derive(Debug)]
1377 struct MachLabelFixup<I: VCodeInst> {
1378     /// The label whose offset controls this fixup.
1379     label: MachLabel,
1380     /// The offset to fix up / patch to refer to this label.
1381     offset: CodeOffset,
1382     /// The kind of fixup. This is architecture-specific; each architecture may have,
1383     /// e.g., several types of branch instructions, each with differently-sized
1384     /// offset fields and different places within the instruction to place the
1385     /// bits.
1386     kind: I::LabelUse,
1387 }
1388 
1389 /// A relocation resulting from a compilation.
1390 struct MachReloc {
1391     /// The offset at which the relocation applies, *relative to the
1392     /// containing section*.
1393     offset: CodeOffset,
1394     /// The original source location.
1395     srcloc: SourceLoc,
1396     /// The kind of relocation.
1397     kind: Reloc,
1398     /// The external symbol / name to which this relocation refers.
1399     name: ExternalName,
1400     /// The addend to add to the symbol value.
1401     addend: i64,
1402 }
1403 
1404 /// A trap record resulting from a compilation.
1405 struct MachTrap {
1406     /// The offset at which the trap instruction occurs, *relative to the
1407     /// containing section*.
1408     offset: CodeOffset,
1409     /// The original source location.
1410     srcloc: SourceLoc,
1411     /// The trap code.
1412     code: TrapCode,
1413 }
1414 
1415 /// A call site record resulting from a compilation.
1416 struct MachCallSite {
1417     /// The offset of the call's return address, *relative to the containing section*.
1418     ret_addr: CodeOffset,
1419     /// The original source location.
1420     srcloc: SourceLoc,
1421     /// The call's opcode.
1422     opcode: Opcode,
1423 }
1424 
1425 /// A source-location mapping resulting from a compilation.
1426 #[derive(Clone, Debug)]
1427 pub struct MachSrcLoc {
1428     /// The start of the region of code corresponding to a source location.
1429     /// This is relative to the start of the function, not to the start of the
1430     /// section.
1431     pub start: CodeOffset,
1432     /// The end of the region of code corresponding to a source location.
1433     /// This is relative to the start of the section, not to the start of the
1434     /// section.
1435     pub end: CodeOffset,
1436     /// The source location.
1437     pub loc: SourceLoc,
1438 }
1439 
1440 /// Record of stack map metadata: stack offsets containing references.
1441 #[derive(Clone, Debug)]
1442 pub struct MachStackMap {
1443     /// The code offset at which this stack map applies.
1444     pub offset: CodeOffset,
1445     /// The code offset just past the "end" of the instruction: that is, the
1446     /// offset of the first byte of the following instruction, or equivalently,
1447     /// the start offset plus the instruction length.
1448     pub offset_end: CodeOffset,
1449     /// The stack map itself.
1450     pub stack_map: StackMap,
1451 }
1452 
1453 /// Record of branch instruction in the buffer, to facilitate editing.
1454 #[derive(Clone, Debug)]
1455 struct MachBranch {
1456     start: CodeOffset,
1457     end: CodeOffset,
1458     target: MachLabel,
1459     fixup: usize,
1460     inverted: Option<SmallVec<[u8; 8]>>,
1461     /// All labels pointing to the start of this branch. For correctness, this
1462     /// *must* be complete (i.e., must contain all labels whose resolved offsets
1463     /// are at the start of this branch): we rely on being able to redirect all
1464     /// labels that could jump to this branch before removing it, if it is
1465     /// otherwise unreachable.
1466     labels_at_this_branch: SmallVec<[MachLabel; 4]>,
1467 }
1468 
1469 impl MachBranch {
1470     fn is_cond(&self) -> bool {
1471         self.inverted.is_some()
1472     }
1473     fn is_uncond(&self) -> bool {
1474         self.inverted.is_none()
1475     }
1476 }
1477 
1478 // We use an actual instruction definition to do tests, so we depend on the `arm64` feature here.
1479 #[cfg(all(test, feature = "arm64"))]
1480 mod test {
1481     use super::*;
1482     use crate::ir::{ConstantOffset, Function, JumpTable, Value};
1483     use crate::isa::aarch64::inst::xreg;
1484     use crate::isa::aarch64::inst::{BranchTarget, CondBrKind, EmitInfo, Inst};
1485     use crate::isa::TargetIsa;
1486     use crate::machinst::MachInstEmit;
1487     use crate::settings;
1488     use std::default::Default;
1489     use std::vec::Vec;
1490 
1491     fn label(n: u32) -> MachLabel {
1492         MachLabel::from_block(n)
1493     }
1494     fn target(n: u32) -> BranchTarget {
1495         BranchTarget::Label(label(n))
1496     }
1497 
1498     #[test]
1499     fn test_elide_jump_to_next() {
1500         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1501         let mut buf = MachBuffer::new();
1502         let mut state = Default::default();
1503 
1504         buf.reserve_labels_for_blocks(2);
1505         buf.bind_label(label(0));
1506         let inst = Inst::Jump { dest: target(1) };
1507         inst.emit(&mut buf, &info, &mut state);
1508         buf.bind_label(label(1));
1509         let buf = buf.finish();
1510         assert_eq!(0, buf.total_size());
1511     }
1512 
1513     #[test]
1514     fn test_elide_trivial_jump_blocks() {
1515         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1516         let mut buf = MachBuffer::new();
1517         let mut state = Default::default();
1518 
1519         buf.reserve_labels_for_blocks(4);
1520 
1521         buf.bind_label(label(0));
1522         let inst = Inst::CondBr {
1523             kind: CondBrKind::NotZero(xreg(0)),
1524             taken: target(1),
1525             not_taken: target(2),
1526         };
1527         inst.emit(&mut buf, &info, &mut state);
1528 
1529         buf.bind_label(label(1));
1530         let inst = Inst::Jump { dest: target(3) };
1531         inst.emit(&mut buf, &info, &mut state);
1532 
1533         buf.bind_label(label(2));
1534         let inst = Inst::Jump { dest: target(3) };
1535         inst.emit(&mut buf, &info, &mut state);
1536 
1537         buf.bind_label(label(3));
1538 
1539         let buf = buf.finish();
1540         assert_eq!(0, buf.total_size());
1541     }
1542 
1543     #[test]
1544     fn test_flip_cond() {
1545         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1546         let mut buf = MachBuffer::new();
1547         let mut state = Default::default();
1548 
1549         buf.reserve_labels_for_blocks(4);
1550 
1551         buf.bind_label(label(0));
1552         let inst = Inst::CondBr {
1553             kind: CondBrKind::NotZero(xreg(0)),
1554             taken: target(1),
1555             not_taken: target(2),
1556         };
1557         inst.emit(&mut buf, &info, &mut state);
1558 
1559         buf.bind_label(label(1));
1560         let inst = Inst::Udf {
1561             trap_code: TrapCode::Interrupt,
1562         };
1563         inst.emit(&mut buf, &info, &mut state);
1564 
1565         buf.bind_label(label(2));
1566         let inst = Inst::Nop4;
1567         inst.emit(&mut buf, &info, &mut state);
1568 
1569         buf.bind_label(label(3));
1570 
1571         let buf = buf.finish();
1572 
1573         let mut buf2 = MachBuffer::new();
1574         let mut state = Default::default();
1575         let inst = Inst::TrapIf {
1576             kind: CondBrKind::NotZero(xreg(0)),
1577             trap_code: TrapCode::Interrupt,
1578         };
1579         inst.emit(&mut buf2, &info, &mut state);
1580         let inst = Inst::Nop4;
1581         inst.emit(&mut buf2, &info, &mut state);
1582 
1583         let buf2 = buf2.finish();
1584 
1585         assert_eq!(buf.data, buf2.data);
1586     }
1587 
1588     #[test]
1589     fn test_island() {
1590         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1591         let mut buf = MachBuffer::new();
1592         let mut state = Default::default();
1593 
1594         buf.reserve_labels_for_blocks(4);
1595 
1596         buf.bind_label(label(0));
1597         let inst = Inst::CondBr {
1598             kind: CondBrKind::NotZero(xreg(0)),
1599             taken: target(2),
1600             not_taken: target(3),
1601         };
1602         inst.emit(&mut buf, &info, &mut state);
1603 
1604         buf.bind_label(label(1));
1605         while buf.cur_offset() < 2000000 {
1606             if buf.island_needed(0) {
1607                 buf.emit_island();
1608             }
1609             let inst = Inst::Nop4;
1610             inst.emit(&mut buf, &info, &mut state);
1611         }
1612 
1613         buf.bind_label(label(2));
1614         let inst = Inst::Nop4;
1615         inst.emit(&mut buf, &info, &mut state);
1616 
1617         buf.bind_label(label(3));
1618         let inst = Inst::Nop4;
1619         inst.emit(&mut buf, &info, &mut state);
1620 
1621         let buf = buf.finish();
1622 
1623         assert_eq!(2000000 + 8, buf.total_size());
1624 
1625         let mut buf2 = MachBuffer::new();
1626         let mut state = Default::default();
1627         let inst = Inst::CondBr {
1628             kind: CondBrKind::NotZero(xreg(0)),
1629             taken: BranchTarget::ResolvedOffset(1048576 - 4),
1630             not_taken: BranchTarget::ResolvedOffset(2000000 + 4 - 4),
1631         };
1632         inst.emit(&mut buf2, &info, &mut state);
1633 
1634         let buf2 = buf2.finish();
1635 
1636         assert_eq!(&buf.data[0..8], &buf2.data[..]);
1637     }
1638 
1639     #[test]
1640     fn test_island_backward() {
1641         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1642         let mut buf = MachBuffer::new();
1643         let mut state = Default::default();
1644 
1645         buf.reserve_labels_for_blocks(4);
1646 
1647         buf.bind_label(label(0));
1648         let inst = Inst::Nop4;
1649         inst.emit(&mut buf, &info, &mut state);
1650 
1651         buf.bind_label(label(1));
1652         let inst = Inst::Nop4;
1653         inst.emit(&mut buf, &info, &mut state);
1654 
1655         buf.bind_label(label(2));
1656         while buf.cur_offset() < 2000000 {
1657             let inst = Inst::Nop4;
1658             inst.emit(&mut buf, &info, &mut state);
1659         }
1660 
1661         buf.bind_label(label(3));
1662         let inst = Inst::CondBr {
1663             kind: CondBrKind::NotZero(xreg(0)),
1664             taken: target(0),
1665             not_taken: target(1),
1666         };
1667         inst.emit(&mut buf, &info, &mut state);
1668 
1669         let buf = buf.finish();
1670 
1671         assert_eq!(2000000 + 12, buf.total_size());
1672 
1673         let mut buf2 = MachBuffer::new();
1674         let mut state = Default::default();
1675         let inst = Inst::CondBr {
1676             kind: CondBrKind::NotZero(xreg(0)),
1677             taken: BranchTarget::ResolvedOffset(8),
1678             not_taken: BranchTarget::ResolvedOffset(4 - (2000000 + 4)),
1679         };
1680         inst.emit(&mut buf2, &info, &mut state);
1681         let inst = Inst::Jump {
1682             dest: BranchTarget::ResolvedOffset(-(2000000 + 8)),
1683         };
1684         inst.emit(&mut buf2, &info, &mut state);
1685 
1686         let buf2 = buf2.finish();
1687 
1688         assert_eq!(&buf.data[2000000..], &buf2.data[..]);
1689     }
1690 
1691     #[test]
1692     fn test_multiple_redirect() {
1693         // label0:
1694         //   cbz x0, label1
1695         //   b label2
1696         // label1:
1697         //   b label3
1698         // label2:
1699         //   nop
1700         //   nop
1701         //   b label0
1702         // label3:
1703         //   b label4
1704         // label4:
1705         //   b label5
1706         // label5:
1707         //   b label7
1708         // label6:
1709         //   nop
1710         // label7:
1711         //   ret
1712         //
1713         // -- should become:
1714         //
1715         // label0:
1716         //   cbz x0, label7
1717         // label2:
1718         //   nop
1719         //   nop
1720         //   b label0
1721         // label6:
1722         //   nop
1723         // label7:
1724         //   ret
1725 
1726         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1727         let mut buf = MachBuffer::new();
1728         let mut state = Default::default();
1729 
1730         buf.reserve_labels_for_blocks(8);
1731 
1732         buf.bind_label(label(0));
1733         let inst = Inst::CondBr {
1734             kind: CondBrKind::Zero(xreg(0)),
1735             taken: target(1),
1736             not_taken: target(2),
1737         };
1738         inst.emit(&mut buf, &info, &mut state);
1739 
1740         buf.bind_label(label(1));
1741         let inst = Inst::Jump { dest: target(3) };
1742         inst.emit(&mut buf, &info, &mut state);
1743 
1744         buf.bind_label(label(2));
1745         let inst = Inst::Nop4;
1746         inst.emit(&mut buf, &info, &mut state);
1747         inst.emit(&mut buf, &info, &mut state);
1748         let inst = Inst::Jump { dest: target(0) };
1749         inst.emit(&mut buf, &info, &mut state);
1750 
1751         buf.bind_label(label(3));
1752         let inst = Inst::Jump { dest: target(4) };
1753         inst.emit(&mut buf, &info, &mut state);
1754 
1755         buf.bind_label(label(4));
1756         let inst = Inst::Jump { dest: target(5) };
1757         inst.emit(&mut buf, &info, &mut state);
1758 
1759         buf.bind_label(label(5));
1760         let inst = Inst::Jump { dest: target(7) };
1761         inst.emit(&mut buf, &info, &mut state);
1762 
1763         buf.bind_label(label(6));
1764         let inst = Inst::Nop4;
1765         inst.emit(&mut buf, &info, &mut state);
1766 
1767         buf.bind_label(label(7));
1768         let inst = Inst::Ret;
1769         inst.emit(&mut buf, &info, &mut state);
1770 
1771         let buf = buf.finish();
1772 
1773         let golden_data = vec![
1774             0xa0, 0x00, 0x00, 0xb4, // cbz x0, 0x14
1775             0x1f, 0x20, 0x03, 0xd5, // nop
1776             0x1f, 0x20, 0x03, 0xd5, // nop
1777             0xfd, 0xff, 0xff, 0x17, // b 0
1778             0x1f, 0x20, 0x03, 0xd5, // nop
1779             0xc0, 0x03, 0x5f, 0xd6, // ret
1780         ];
1781 
1782         assert_eq!(&golden_data[..], &buf.data[..]);
1783     }
1784 
1785     #[test]
1786     fn test_handle_branch_cycle() {
1787         // label0:
1788         //   b label1
1789         // label1:
1790         //   b label2
1791         // label2:
1792         //   b label3
1793         // label3:
1794         //   b label4
1795         // label4:
1796         //   b label1  // note: not label0 (to make it interesting).
1797         //
1798         // -- should become:
1799         //
1800         // label0, label1, ..., label4:
1801         //   b label0
1802         let info = EmitInfo::new(settings::Flags::new(settings::builder()));
1803         let mut buf = MachBuffer::new();
1804         let mut state = Default::default();
1805 
1806         buf.reserve_labels_for_blocks(5);
1807 
1808         buf.bind_label(label(0));
1809         let inst = Inst::Jump { dest: target(1) };
1810         inst.emit(&mut buf, &info, &mut state);
1811 
1812         buf.bind_label(label(1));
1813         let inst = Inst::Jump { dest: target(2) };
1814         inst.emit(&mut buf, &info, &mut state);
1815 
1816         buf.bind_label(label(2));
1817         let inst = Inst::Jump { dest: target(3) };
1818         inst.emit(&mut buf, &info, &mut state);
1819 
1820         buf.bind_label(label(3));
1821         let inst = Inst::Jump { dest: target(4) };
1822         inst.emit(&mut buf, &info, &mut state);
1823 
1824         buf.bind_label(label(4));
1825         let inst = Inst::Jump { dest: target(1) };
1826         inst.emit(&mut buf, &info, &mut state);
1827 
1828         let buf = buf.finish();
1829 
1830         let golden_data = vec![
1831             0x00, 0x00, 0x00, 0x14, // b 0
1832         ];
1833 
1834         assert_eq!(&golden_data[..], &buf.data[..]);
1835     }
1836 
1837     #[test]
1838     fn metadata_records() {
1839         let mut buf = MachBuffer::<Inst>::new();
1840 
1841         buf.reserve_labels_for_blocks(1);
1842 
1843         buf.bind_label(label(0));
1844         buf.put1(1);
1845         buf.add_trap(SourceLoc::default(), TrapCode::HeapOutOfBounds);
1846         buf.put1(2);
1847         buf.add_trap(SourceLoc::default(), TrapCode::IntegerOverflow);
1848         buf.add_trap(SourceLoc::default(), TrapCode::IntegerDivisionByZero);
1849         buf.add_call_site(SourceLoc::default(), Opcode::Call);
1850         buf.add_reloc(
1851             SourceLoc::default(),
1852             Reloc::Abs4,
1853             &ExternalName::user(0, 0),
1854             0,
1855         );
1856         buf.put1(3);
1857         buf.add_reloc(
1858             SourceLoc::default(),
1859             Reloc::Abs8,
1860             &ExternalName::user(1, 1),
1861             1,
1862         );
1863         buf.put1(4);
1864 
1865         let buf = buf.finish();
1866 
1867         #[derive(Default)]
1868         struct TestCodeSink {
1869             offset: CodeOffset,
1870             traps: Vec<(CodeOffset, TrapCode)>,
1871             callsites: Vec<(CodeOffset, Opcode)>,
1872             relocs: Vec<(CodeOffset, Reloc)>,
1873         }
1874         impl CodeSink for TestCodeSink {
1875             fn offset(&self) -> CodeOffset {
1876                 self.offset
1877             }
1878             fn put1(&mut self, _: u8) {
1879                 self.offset += 1;
1880             }
1881             fn put2(&mut self, _: u16) {
1882                 self.offset += 2;
1883             }
1884             fn put4(&mut self, _: u32) {
1885                 self.offset += 4;
1886             }
1887             fn put8(&mut self, _: u64) {
1888                 self.offset += 8;
1889             }
1890             fn reloc_external(&mut self, _: SourceLoc, r: Reloc, _: &ExternalName, _: Addend) {
1891                 self.relocs.push((self.offset, r));
1892             }
1893             fn reloc_constant(&mut self, _: Reloc, _: ConstantOffset) {}
1894             fn reloc_jt(&mut self, _: Reloc, _: JumpTable) {}
1895             fn trap(&mut self, t: TrapCode, _: SourceLoc) {
1896                 self.traps.push((self.offset, t));
1897             }
1898             fn begin_jumptables(&mut self) {}
1899             fn begin_rodata(&mut self) {}
1900             fn end_codegen(&mut self) {}
1901             fn add_stack_map(&mut self, _: &[Value], _: &Function, _: &dyn TargetIsa) {}
1902             fn add_call_site(&mut self, op: Opcode, _: SourceLoc) {
1903                 self.callsites.push((self.offset, op));
1904             }
1905         }
1906 
1907         let mut sink = TestCodeSink::default();
1908         buf.emit(&mut sink);
1909 
1910         assert_eq!(sink.offset, 4);
1911         assert_eq!(
1912             sink.traps,
1913             vec![
1914                 (1, TrapCode::HeapOutOfBounds),
1915                 (2, TrapCode::IntegerOverflow),
1916                 (2, TrapCode::IntegerDivisionByZero)
1917             ]
1918         );
1919         assert_eq!(sink.callsites, vec![(2, Opcode::Call),]);
1920         assert_eq!(sink.relocs, vec![(2, Reloc::Abs4), (3, Reloc::Abs8)]);
1921     }
1922 }
1923