exarmo-core 0.2.2

Runtime support shared by the exarmo-aarch64 and exarmo-aarch32 disassemblers
Documentation
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
461
462
//! Writing an instruction out as tokens.
//!
//! Each instruction set names its own registers and symbols and reads them
//! out of its operands. This writes everything else the same way for both,
//! including the mnemonic and the tab after it, the `, ` between operands,
//! the brackets, and the form of a number. A runtime wraps [`Writer`] in a
//! writer of its own, declared with `rendering_writer!`.

use core::fmt;
use core::fmt::Write as _;

use crate::address::PcRead;
use crate::tokens::{Hex, OperandIndex, TokenKind, TokenSink};

/// How a number is written. For a symbol, how a value its table does not name
/// is written.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Form {
    /// `#0x10`, an immediate.
    Hex,
    /// `#-0x10`, an immediate with its sign.
    Signed,
    /// `3`, a bare integer.
    Decimal,
}

/// A number written in hexadecimal with its sign before the `0x`.
///
/// `negative` carries the sign apart from the value because a subtracted zero
/// is written `-0x0`. That is a distinct word, with the U bit clear, which
/// `#0` would not assemble to.
pub struct SignedHex {
    /// The value, signed.
    pub value: i64,
    /// Whether the assembly writes it subtracted.
    pub negative: bool,
}

impl fmt::Display for SignedHex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.negative || self.value < 0 {
            true => write!(f, "-0x{:x}", self.value.unsigned_abs()),
            false => write!(f, "0x{:x}", self.value),
        }
    }
}

/// The floating-point value of the low `width` bits of `bits`, for a width of
/// 16, 32 or 64.
///
/// Rust has no stable half-precision float, so a 16-bit constant is decoded
/// as IEEE 754 binary16 by hand.
pub fn float_from_bits(bits: u64, width: u32) -> f64 {
    match width {
        16 => {
            let half = bits as u16;
            let sign = if half >> 15 == 1 { -1.0 } else { 1.0 };
            let exponent = (half >> 10) & 0x1f;
            let fraction = f64::from(half & 0x3ff);
            match exponent {
                0 => sign * fraction * 2f64.powi(-24),
                0x1f if fraction == 0.0 => sign * f64::INFINITY,
                0x1f => f64::NAN,
                _ => sign * (1.0 + fraction / 1024.0) * 2f64.powi(i32::from(exponent) - 15),
            }
        }
        32 => f64::from(f32::from_bits(bits as u32)),
        _ => f64::from_bits(bits),
    }
}

/// A floating-point number, written so that a whole one keeps its point.
pub struct Float<T>(pub T);

impl<T: fmt::Display> fmt::Display for Float<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // The text must be inspected to know whether it already has a point.
        // A stack buffer avoids allocating on the formatting path, and text
        // longer than the buffer falls back to the heap.
        let mut held = Held::default();
        if write!(held, "{}", self.0).is_ok() {
            return put(f, held.written());
        }
        put(f, &format!("{}", self.0))
    }
}

/// Writes a number, adding `.0` where its text has no point or exponent.
fn put(f: &mut fmt::Formatter<'_>, written: &str) -> fmt::Result {
    match written.contains(['.', 'e', 'E']) {
        true => f.write_str(written),
        false => write!(f, "{written}.0"),
    }
}

/// A stack buffer large enough for any immediate the architecture encodes.
struct Held {
    bytes: [u8; 64],
    len: usize,
}

impl Default for Held {
    fn default() -> Self {
        Held {
            bytes: [0; 64],
            len: 0,
        }
    }
}

impl Held {
    /// The text written so far, which is UTF-8 because only `write!` fills
    /// the buffer.
    fn written(&self) -> &str {
        core::str::from_utf8(&self.bytes[..self.len]).unwrap_or("")
    }
}

impl fmt::Write for Held {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let end = self.len.checked_add(s.len()).ok_or(fmt::Error)?;
        let room = self.bytes.get_mut(self.len..end).ok_or(fmt::Error)?;
        room.copy_from_slice(s.as_bytes());
        self.len = end;
        Ok(())
    }
}

/// Writes an instruction's tokens to a sink.
///
/// Its methods are kept out of line so that each piece of a rendering is one
/// call. There are a few thousand renderings, and inlining the writing into
/// each would cost code size without gaining speed.
pub struct Writer<'a> {
    sink: &'a mut dyn TokenSink,
    name: &'static str,
    address: u64,
    /// How wide an address is in this instruction set, so a target running
    /// off either end wraps as the architecture's PC does.
    bits: u32,
    /// The operand the tokens being written are part of, which a rendering
    /// sets as it reaches each one.
    operand: OperandIndex,
}

impl<'a> Writer<'a> {
    /// A writer for an instruction with the mnemonic, at the address, in an
    /// instruction set whose addresses are `bits` wide.
    pub fn new(sink: &'a mut dyn TokenSink, name: &'static str, address: u64, bits: u32) -> Self {
        Writer {
            sink,
            name,
            address,
            bits,
            operand: None,
        }
    }

    /// Which operand the tokens after this are part of, or none for the
    /// text between operands. A [`Writer::separator`] clears it.
    #[inline]
    pub fn operand(&mut self, operand: OperandIndex) {
        self.operand = operand;
    }

    /// The instruction's mnemonic.
    #[inline(never)]
    pub fn name(&mut self) -> fmt::Result {
        self.sink.text(TokenKind::Mnemonic, self.operand, self.name)
    }

    /// A piece of a mnemonic written literally: the `b.` of `b.eq`.
    #[inline(never)]
    pub fn mnemonic(&mut self, text: &str) -> fmt::Result {
        self.sink.text(TokenKind::Mnemonic, self.operand, text)
    }

    /// Plain text: the tab after the mnemonic, a `.`, a `!`.
    #[inline(never)]
    pub fn text(&mut self, text: &str) -> fmt::Result {
        self.sink.text(TokenKind::Text, self.operand, text)
    }

    /// The `, ` between operands, which belongs to no operand.
    #[inline(never)]
    pub fn separator(&mut self) -> fmt::Result {
        self.operand = None;
        self.sink.text(TokenKind::Separator, None, ", ")
    }

    /// The `, ` between the parts of one operand: the members of a
    /// register list, the base and the offset of a memory operand, a
    /// register and the shift written on it. It is text of the operand it is
    /// written inside, so that a consumer counting operands can count
    /// separators.
    #[inline(never)]
    pub fn inner_separator(&mut self) -> fmt::Result {
        self.sink.text(TokenKind::Text, self.operand, ", ")
    }

    /// A bracket or brace.
    #[inline(never)]
    pub fn bracket(&mut self, text: &str) -> fmt::Result {
        self.sink.text(TokenKind::Bracket, self.operand, text)
    }

    /// A number in the given form. An immediate's `#` is a text token of its
    /// own.
    #[inline(never)]
    pub fn number(&mut self, value: i64, form: Form) -> fmt::Result {
        match form {
            Form::Hex => {
                self.sink.text(TokenKind::Text, self.operand, "#")?;
                self.sink.value(
                    TokenKind::Immediate(value),
                    self.operand,
                    &Hex(value as u64),
                )
            }
            Form::Signed => self.signed(value, false),
            Form::Decimal => {
                self.sink
                    .value(TokenKind::Integer(value as u64), self.operand, &value)
            }
        }
    }

    /// A floating-point number, after its `#`.
    #[inline(never)]
    pub fn float(&mut self, value: impl fmt::Display) -> fmt::Result {
        self.sink.text(TokenKind::Text, self.operand, "#")?;
        self.sink
            .value(TokenKind::Float, self.operand, &Float(value))
    }

    /// A register, however the instruction set writes one.
    #[inline(never)]
    pub fn register(&mut self, reg: &dyn fmt::Display) -> fmt::Result {
        self.sink.value(TokenKind::Register, self.operand, reg)
    }

    /// A register the view may not hold. A missing one is a formatting error
    /// rather than half a line of text.
    #[inline(never)]
    pub fn some_register<R: fmt::Display>(&mut self, reg: Option<R>) -> fmt::Result {
        self.register(&reg.ok_or(fmt::Error)?)
    }

    /// A number a reading may not have found, as [`Writer::some_register`]
    /// is for a register.
    #[inline(never)]
    pub fn some_number(&mut self, value: Option<i64>, form: Form) -> fmt::Result {
        self.number(value.ok_or(fmt::Error)?, form)
    }

    /// A name from a value table: a shift, a condition, a data type.
    #[inline(never)]
    pub fn symbol(&mut self, name: &dyn fmt::Display) -> fmt::Result {
        self.sink.value(TokenKind::Symbol, self.operand, name)
    }

    /// A token of a kind the instruction set chooses, for a register file or a
    /// symbol the other methods do not cover.
    #[inline(never)]
    pub fn token(&mut self, kind: TokenKind, text: &str) -> fmt::Result {
        self.sink.text(kind, self.operand, text)
    }

    /// A token whose text is a value's own rendering, of a kind the instruction
    /// set names itself.
    #[inline(never)]
    pub fn valued(&mut self, kind: TokenKind, value: &dyn fmt::Display) -> fmt::Result {
        self.sink.value(kind, self.operand, value)
    }

    /// A number after its `#`, written as [`SignedHex`].
    #[inline(never)]
    pub fn signed(&mut self, value: i64, negative: bool) -> fmt::Result {
        self.sink.text(TokenKind::Text, self.operand, "#")?;
        self.sink.value(
            TokenKind::Immediate(value),
            self.operand,
            &SignedHex { value, negative },
        )
    }

    /// An address, written in hex and carrying the address it names.
    #[inline(never)]
    pub fn address(&mut self, target: u64) -> fmt::Result {
        self.sink
            .value(TokenKind::Address(target), self.operand, &Hex(target))
    }

    /// A branch target or a literal's address, written as the PC the
    /// instruction reads plus `offset`, wrapped to the address width.
    #[inline(never)]
    pub fn label(&mut self, offset: i64, pc: PcRead) -> fmt::Result {
        self.address(pc.target(self.address, offset, self.bits))
    }
}

/// Declares a runtime's rendering writer. It wraps the shared [`Writer`] with
/// the instruction set's address width fixed and derefs to it, and the
/// runtime adds the methods that read its own operands.
///
/// ```
/// # mod runtime {
/// exarmo_core::rendering_writer!(
///     /// Where a rendering of this instruction set writes.
///     Writer, 32
/// );
/// # }
/// ```
#[macro_export]
macro_rules! rendering_writer {
    ($(#[$doc:meta])* $name:ident, $bits:literal) => {
        $(#[$doc])*
        pub struct $name<'a> {
            out: $crate::write::Writer<'a>,
        }

        impl<'a> ::core::ops::Deref for $name<'a> {
            type Target = $crate::write::Writer<'a>;
            fn deref(&self) -> &Self::Target {
                &self.out
            }
        }

        impl<'a> ::core::ops::DerefMut for $name<'a> {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.out
            }
        }

        impl<'a> $name<'a> {
            /// A writer for an instruction with the mnemonic, at the address.
            pub fn new(
                sink: &'a mut dyn $crate::tokens::TokenSink,
                name: &'static str,
                address: u64,
            ) -> Self {
                $name {
                    out: $crate::write::Writer::new(sink, name, address, $bits),
                }
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::{Form, Writer, float_from_bits};
    use crate::address::PcRead;
    use crate::tokens::{Token, TokenKind};
    use core::fmt;

    /// What a writer wrote, as its tokens and as the text they come to.
    fn written(
        at: u64,
        bits: u32,
        write: impl FnOnce(&mut Writer) -> fmt::Result,
    ) -> (Vec<Token>, String) {
        let mut sink: Vec<Token> = Vec::new();
        let mut writer = Writer::new(&mut sink, "b", at, bits);
        write(&mut writer).expect("a Vec sink cannot fail");
        let text = sink.iter().map(|t| t.text.as_str()).collect();
        (sink, text)
    }

    #[test]
    fn a_decimal_is_written_bare() {
        let (tokens, text) = written(0, 64, |w| w.number(3, Form::Decimal));
        assert_eq!(text, "3");
        assert_eq!(tokens.len(), 1);
        assert_eq!(tokens[0].kind, TokenKind::Integer(3));
    }

    #[test]
    fn a_subtracted_zero_keeps_its_sign() {
        assert_eq!(written(0, 64, |w| w.signed(0, true)).1, "#-0x0");
        assert_eq!(written(0, 64, |w| w.signed(0, false)).1, "#0x0");
        assert_eq!(written(0, 64, |w| w.signed(16, true)).1, "#-0x10");
    }

    #[test]
    fn a_signed_form_writes_the_value_it_has() {
        assert_eq!(written(0, 64, |w| w.number(-16, Form::Signed)).1, "#-0x10");
    }

    #[test]
    fn a_target_wraps_within_the_instruction_set_s_reach() {
        let pc = PcRead { ahead: 0, align: 1 };
        assert_eq!(
            written(0xffff_fffc, 32, |w| w.label(0x10, pc)).1,
            "0xc",
            "a 32-bit target past the end wraps to the bottom"
        );
        assert_eq!(written(0x10, 32, |w| w.label(-0x20, pc)).1, "0xfffffff0");
    }

    #[test]
    fn each_piece_carries_its_kind() {
        let (tokens, text) = written(0, 64, |w| {
            w.name()?;
            w.text("\t")?;
            w.register(&"x0")?;
            w.separator()?;
            w.operand(Some(1));
            w.bracket("[")?;
            w.register(&"x1")?;
            w.inner_separator()?;
            w.symbol(&"lsl")?;
            w.bracket("]")
        });
        assert_eq!(text, "b\tx0, [x1, lsl]");
        let kinds: Vec<(TokenKind, Option<u8>)> =
            tokens.iter().map(|t| (t.kind, t.operand)).collect();
        assert_eq!(
            kinds,
            vec![
                (TokenKind::Mnemonic, None),
                (TokenKind::Text, None),
                (TokenKind::Register, None),
                (TokenKind::Separator, None),
                (TokenKind::Bracket, Some(1)),
                (TokenKind::Register, Some(1)),
                // Within an operand, a comma is its text
                (TokenKind::Text, Some(1)),
                (TokenKind::Symbol, Some(1)),
                (TokenKind::Bracket, Some(1)),
            ]
        );
    }

    /// The smallest denormal at each width.
    #[test]
    fn denormal() {
        assert_eq!(float_from_bits(0x0001, 16), 2f64.powi(-24));
        assert_eq!(float_from_bits(0x0000_0001, 32), 2f64.powi(-149));
        // 2^-1074 is itself subnormal, so it is named by its bits rather
        // than computed, which would round to zero.
        assert_eq!(
            float_from_bits(0x0000_0000_0000_0001, 64),
            f64::from_bits(1)
        );
    }

    #[test]
    fn infinity() {
        assert_eq!(float_from_bits(0x7c00, 16), f64::INFINITY);
        assert_eq!(float_from_bits(0xfc00, 16), f64::NEG_INFINITY);
        assert_eq!(float_from_bits(0x7f80_0000, 32), f64::INFINITY);
        assert_eq!(float_from_bits(0x7ff0_0000_0000_0000, 64), f64::INFINITY);
    }

    #[test]
    fn nan() {
        assert!(float_from_bits(0x7e00, 16).is_nan());
        assert!(float_from_bits(0x7fc0_0000, 32).is_nan());
        assert!(float_from_bits(0x7ff8_0000_0000_0000, 64).is_nan());
    }
}