1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! In-memory representation of compiled machine code, in multiple sections
//! (text, constant pool / rodata, etc). Emission occurs into multiple sections
//! simultaneously, so we buffer the result in memory and hand off to the
//! caller at the end of compilation.

use crate::binemit::{Addend, CodeOffset, CodeSink, Reloc};
use crate::ir::{ExternalName, Opcode, SourceLoc, TrapCode};

use alloc::vec::Vec;

/// A collection of sections with defined start-offsets.
pub struct MachSections {
    /// Sections, in offset order.
    pub sections: Vec<MachSection>,
}

impl MachSections {
    /// New, empty set of sections.
    pub fn new() -> MachSections {
        MachSections { sections: vec![] }
    }

    /// Add a section with a known offset and size. Returns the index.
    pub fn add_section(&mut self, start: CodeOffset, length: CodeOffset) -> usize {
        let idx = self.sections.len();
        self.sections.push(MachSection::new(start, length));
        idx
    }

    /// Mutably borrow the given section by index.
    pub fn get_section<'a>(&'a mut self, idx: usize) -> &'a mut MachSection {
        &mut self.sections[idx]
    }

    /// Get mutable borrows of two sections simultaneously. Used during
    /// instruction emission to provide references to the .text and .rodata
    /// (constant pool) sections.
    pub fn two_sections<'a>(
        &'a mut self,
        idx1: usize,
        idx2: usize,
    ) -> (&'a mut MachSection, &'a mut MachSection) {
        assert!(idx1 < idx2);
        assert!(idx1 < self.sections.len());
        assert!(idx2 < self.sections.len());
        let (first, rest) = self.sections.split_at_mut(idx2);
        (&mut first[idx1], &mut rest[0])
    }

    /// Emit this set of sections to a set of sinks for the code,
    /// relocations, traps, and stackmap.
    pub fn emit<CS: CodeSink>(&self, sink: &mut CS) {
        // N.B.: we emit every section into the .text section as far as
        // the `CodeSink` is concerned; we do not bother to segregate
        // the contents into the actual program text, the jumptable and the
        // rodata (constant pool). This allows us to generate code assuming
        // that these will not be relocated relative to each other, and avoids
        // having to designate each section as belonging in one of the three
        // fixed categories defined by `CodeSink`. If this becomes a problem
        // later (e.g. because of memory permissions or similar), we can
        // add this designation and segregate the output; take care, however,
        // to add the appropriate relocations in this case.

        for section in &self.sections {
            if section.data.len() > 0 {
                while sink.offset() < section.start_offset {
                    sink.put1(0);
                }
                section.emit(sink);
            }
        }
        sink.begin_jumptables();
        sink.begin_rodata();
        sink.end_codegen();
    }

    /// Get a list of source location mapping tuples in sorted-by-start-offset order.
    pub fn get_srclocs_sorted<'a>(&'a self) -> MachSectionsSrcLocs<'a> {
        MachSectionsSrcLocs::new(&self.sections)
    }

    /// Get the total required size for these sections.
    pub fn total_size(&self) -> CodeOffset {
        if self.sections.len() == 0 {
            0
        } else {
            // Find the last non-empty section.
            self.sections
                .iter()
                .rev()
                .find(|s| s.data.len() > 0)
                .map(|s| s.cur_offset_from_start())
                .unwrap_or(0)
        }
    }
}

/// An iterator over the srclocs in each section.
/// Returns MachSrcLocs in an order sorted by start location.
pub struct MachSectionsSrcLocs<'a> {
    sections: &'a [MachSection],
    cur_section: usize,
    cur_srcloc: usize,
    // For validation:
    last_offset: CodeOffset,
}

impl<'a> MachSectionsSrcLocs<'a> {
    fn new(sections: &'a [MachSection]) -> MachSectionsSrcLocs<'a> {
        MachSectionsSrcLocs {
            sections,
            cur_section: 0,
            cur_srcloc: 0,
            last_offset: 0,
        }
    }
}

impl<'a> Iterator for MachSectionsSrcLocs<'a> {
    type Item = &'a MachSrcLoc;

    fn next(&mut self) -> Option<&'a MachSrcLoc> {
        // We simply iterate through sections and srcloc records in order. This produces a
        // sorted order naturally because sections are in starting-offset-order, and srclocs
        // are produced as a section is emitted into, so are in order as well.

        // If we're out of sections, we're done.
        if self.cur_section >= self.sections.len() {
            return None;
        }

        // Otherwise, make sure we have a srcloc in the current section left to return, and
        // advance to the next section if not. Done if we run out of sections.
        while self.cur_srcloc >= self.sections[self.cur_section].srclocs.len() {
            self.cur_srcloc = 0;
            self.cur_section += 1;
            if self.cur_section >= self.sections.len() {
                return None;
            }
        }

        let loc = &self.sections[self.cur_section].srclocs[self.cur_srcloc];
        self.cur_srcloc += 1;
        debug_assert!(loc.start >= self.last_offset);
        self.last_offset = loc.start;
        Some(loc)
    }
}

/// An abstraction over MachSection and MachSectionSize: some
/// receiver of section data.
pub trait MachSectionOutput {
    /// Get the current offset from the start of all sections.
    fn cur_offset_from_start(&self) -> CodeOffset;

    /// Get the start offset of this section.
    fn start_offset(&self) -> CodeOffset;

    /// Add 1 byte to the section.
    fn put1(&mut self, _: u8);

    /// Add 2 bytes to the section.
    fn put2(&mut self, value: u16) {
        let [b0, b1] = value.to_le_bytes();
        self.put1(b0);
        self.put1(b1);
    }

    /// Add 4 bytes to the section.
    fn put4(&mut self, value: u32) {
        let [b0, b1, b2, b3] = value.to_le_bytes();
        self.put1(b0);
        self.put1(b1);
        self.put1(b2);
        self.put1(b3);
    }

    /// Add 8 bytes to the section.
    fn put8(&mut self, value: u64) {
        let [b0, b1, b2, b3, b4, b5, b6, b7] = value.to_le_bytes();
        self.put1(b0);
        self.put1(b1);
        self.put1(b2);
        self.put1(b3);
        self.put1(b4);
        self.put1(b5);
        self.put1(b6);
        self.put1(b7);
    }

    /// Add a slice of bytes to the section.
    fn put_data(&mut self, data: &[u8]);

    /// Add a relocation at the current offset.
    fn add_reloc(&mut self, loc: SourceLoc, kind: Reloc, name: &ExternalName, addend: Addend);

    /// Add a trap record at the current offset.
    fn add_trap(&mut self, loc: SourceLoc, code: TrapCode);

    /// Add a call return address record at the current offset.
    fn add_call_site(&mut self, loc: SourceLoc, opcode: Opcode);

    /// Start the output for the given source-location at the current offset.
    fn start_srcloc(&mut self, loc: SourceLoc);

    /// End the output for the previously-given source-location at the current offset.
    fn end_srcloc(&mut self);

    /// Align up to the given alignment.
    fn align_to(&mut self, align_to: CodeOffset) {
        assert!(align_to.is_power_of_two());
        while self.cur_offset_from_start() & (align_to - 1) != 0 {
            self.put1(0);
        }
    }
}

/// A section of output to be emitted to a CodeSink / RelocSink in bulk.
/// Multiple sections may be created with known start offsets in advance; the
/// usual use-case is to create the .text (code) and .rodata (constant pool) at
/// once, after computing the length of the code, so that constant references
/// can use known offsets as instructions are emitted.
pub struct MachSection {
    /// The starting offset of this section.
    pub start_offset: CodeOffset,
    /// The limit of this section, defined by the start of the next section.
    pub length_limit: CodeOffset,
    /// The section contents, as raw bytes.
    pub data: Vec<u8>,
    /// Any relocations referring to this section.
    pub relocs: Vec<MachReloc>,
    /// Any trap records referring to this section.
    pub traps: Vec<MachTrap>,
    /// Any call site records referring to this section.
    pub call_sites: Vec<MachCallSite>,
    /// Any source location mappings referring to this section.
    pub srclocs: Vec<MachSrcLoc>,
    /// The current source location in progress (after `start_srcloc()` and before `end_srcloc()`).
    /// This is a (start_offset, src_loc) tuple.
    pub cur_srcloc: Option<(CodeOffset, SourceLoc)>,
}

impl MachSection {
    /// Create a new section, known to start at `start_offset` and with a size limited to `length_limit`.
    pub fn new(start_offset: CodeOffset, length_limit: CodeOffset) -> MachSection {
        MachSection {
            start_offset,
            length_limit,
            data: vec![],
            relocs: vec![],
            traps: vec![],
            call_sites: vec![],
            srclocs: vec![],
            cur_srcloc: None,
        }
    }

    /// Emit this section to the CodeSink and other associated sinks.  The
    /// current offset of the CodeSink must match the starting offset of this
    /// section.
    pub fn emit<CS: CodeSink>(&self, sink: &mut CS) {
        assert!(sink.offset() == self.start_offset);

        let mut next_reloc = 0;
        let mut next_trap = 0;
        let mut next_call_site = 0;
        for (idx, byte) in self.data.iter().enumerate() {
            if next_reloc < self.relocs.len() {
                let reloc = &self.relocs[next_reloc];
                if reloc.offset == idx as CodeOffset {
                    sink.reloc_external(reloc.srcloc, reloc.kind, &reloc.name, reloc.addend);
                    next_reloc += 1;
                }
            }
            if next_trap < self.traps.len() {
                let trap = &self.traps[next_trap];
                if trap.offset == idx as CodeOffset {
                    sink.trap(trap.code, trap.srcloc);
                    next_trap += 1;
                }
            }
            if next_call_site < self.call_sites.len() {
                let call_site = &self.call_sites[next_call_site];
                if call_site.ret_addr == idx as CodeOffset {
                    sink.add_call_site(call_site.opcode, call_site.srcloc);
                    next_call_site += 1;
                }
            }
            sink.put1(*byte);
        }
    }
}

impl MachSectionOutput for MachSection {
    fn cur_offset_from_start(&self) -> CodeOffset {
        self.start_offset + self.data.len() as CodeOffset
    }

    fn start_offset(&self) -> CodeOffset {
        self.start_offset
    }

    fn put1(&mut self, value: u8) {
        assert!(((self.data.len() + 1) as CodeOffset) <= self.length_limit);
        self.data.push(value);
    }

    fn put_data(&mut self, data: &[u8]) {
        assert!(((self.data.len() + data.len()) as CodeOffset) <= self.length_limit);
        self.data.extend_from_slice(data);
    }

    fn add_reloc(&mut self, srcloc: SourceLoc, kind: Reloc, name: &ExternalName, addend: Addend) {
        let name = name.clone();
        self.relocs.push(MachReloc {
            offset: self.data.len() as CodeOffset,
            srcloc,
            kind,
            name,
            addend,
        });
    }

    fn add_trap(&mut self, srcloc: SourceLoc, code: TrapCode) {
        self.traps.push(MachTrap {
            offset: self.data.len() as CodeOffset,
            srcloc,
            code,
        });
    }

    fn add_call_site(&mut self, srcloc: SourceLoc, opcode: Opcode) {
        self.call_sites.push(MachCallSite {
            ret_addr: self.data.len() as CodeOffset,
            srcloc,
            opcode,
        });
    }

    fn start_srcloc(&mut self, loc: SourceLoc) {
        self.cur_srcloc = Some((self.cur_offset_from_start(), loc));
    }

    fn end_srcloc(&mut self) {
        let (start, loc) = self
            .cur_srcloc
            .take()
            .expect("end_srcloc() called without start_srcloc()");
        let end = self.cur_offset_from_start();
        // Skip zero-length extends.
        debug_assert!(end >= start);
        if end > start {
            self.srclocs.push(MachSrcLoc { start, end, loc });
        }
    }
}

/// A MachSectionOutput implementation that records only size.
pub struct MachSectionSize {
    /// The starting offset of this section.
    pub start_offset: CodeOffset,
    /// The current offset of this section.
    pub offset: CodeOffset,
}

impl MachSectionSize {
    /// Create a new size-counting dummy section.
    pub fn new(start_offset: CodeOffset) -> MachSectionSize {
        MachSectionSize {
            start_offset,
            offset: start_offset,
        }
    }

    /// Return the size this section would take if emitted with a real sink.
    pub fn size(&self) -> CodeOffset {
        self.offset - self.start_offset
    }
}

impl MachSectionOutput for MachSectionSize {
    fn cur_offset_from_start(&self) -> CodeOffset {
        // All size-counting sections conceptually start at offset 0; this doesn't
        // matter when counting code size.
        self.offset
    }

    fn start_offset(&self) -> CodeOffset {
        self.start_offset
    }

    fn put1(&mut self, _: u8) {
        self.offset += 1;
    }

    fn put_data(&mut self, data: &[u8]) {
        self.offset += data.len() as CodeOffset;
    }

    fn add_reloc(&mut self, _: SourceLoc, _: Reloc, _: &ExternalName, _: Addend) {}

    fn add_trap(&mut self, _: SourceLoc, _: TrapCode) {}

    fn add_call_site(&mut self, _: SourceLoc, _: Opcode) {}

    fn start_srcloc(&mut self, _: SourceLoc) {}

    fn end_srcloc(&mut self) {}
}

/// A relocation resulting from a compilation.
pub struct MachReloc {
    /// The offset at which the relocation applies, *relative to the
    /// containing section*.
    pub offset: CodeOffset,
    /// The original source location.
    pub srcloc: SourceLoc,
    /// The kind of relocation.
    pub kind: Reloc,
    /// The external symbol / name to which this relocation refers.
    pub name: ExternalName,
    /// The addend to add to the symbol value.
    pub addend: i64,
}

/// A trap record resulting from a compilation.
pub struct MachTrap {
    /// The offset at which the trap instruction occurs, *relative to the
    /// containing section*.
    pub offset: CodeOffset,
    /// The original source location.
    pub srcloc: SourceLoc,
    /// The trap code.
    pub code: TrapCode,
}

/// A call site record resulting from a compilation.
pub struct MachCallSite {
    /// The offset of the call's return address, *relative to the containing section*.
    pub ret_addr: CodeOffset,
    /// The original source location.
    pub srcloc: SourceLoc,
    /// The call's opcode.
    pub opcode: Opcode,
}

/// A source-location mapping resulting from a compilation.
#[derive(Clone, Debug)]
pub struct MachSrcLoc {
    /// The start of the region of code corresponding to a source location.
    /// This is relative to the start of the function, not to the start of the
    /// section.
    pub start: CodeOffset,
    /// The end of the region of code corresponding to a source location.
    /// This is relative to the start of the section, not to the start of the
    /// section.
    pub end: CodeOffset,
    /// The source location.
    pub loc: SourceLoc,
}