rsasm 0.1.0

A multi-syntax, multi-architecture assembler
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
//! Tokenizer.
//!
//! The lexer is pull-based and its [`LexConfig`] is mutable between tokens.
//! That matters because directives such as `.intel_syntax` or `.arch` change
//! how the *rest* of the file is spelled: `;` is a statement separator in GAS
//! but a comment in NASM, `1b` is a local-label reference in GAS but a binary
//! literal in NASM. A batch tokenizer would have to guess; a pull lexer simply
//! asks the config again for every token.

use crate::diag::{DiagBag, Diagnostic};
use crate::intern::{Interner, Name};
use crate::source::{FileId, SourceMap, Span};

/// Overall source-language flavour. Controls lexing and the directive set.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum Dialect {
    /// GNU as: `#` and `//` comments, `;` separates statements, `.directives`.
    #[default]
    Gas,
    /// NASM: `;` comments, no statement separator, bare `directives`.
    Nasm,
}

/// Lexical rules in force for the next token.
#[derive(Clone, Debug)]
pub struct LexConfig {
    pub dialect: Dialect,
    /// Strings that begin a comment running to end of line.
    pub line_comment: Vec<&'static str>,
    /// Whether `/* ... */` is a comment.
    pub block_comment: bool,
    /// Characters that terminate a statement like a newline does.
    pub stmt_sep: Vec<char>,
    /// Accept trailing radix letters: `0ffh`, `1010b`, `17o`, `99d`.
    pub radix_suffix: bool,
    /// Accept `<digits>f` / `<digits>b` as forward/backward local-label refs.
    /// Mutually exclusive with `radix_suffix` for the `b` case.
    pub local_label_refs: bool,
    /// A character literal may hold several characters, packed big-endian, and
    /// requires a closing quote (NASM). When false, `'a` is one character with
    /// an optional closing quote (GAS).
    pub char_multi: bool,
    /// A bare leading `0` introduces an octal literal (GAS).
    pub octal_leading_zero: bool,
}

impl LexConfig {
    pub fn for_dialect(d: Dialect) -> LexConfig {
        match d {
            Dialect::Gas => LexConfig {
                dialect: d,
                line_comment: vec!["#", "//"],
                block_comment: true,
                stmt_sep: vec![';'],
                radix_suffix: false,
                local_label_refs: true,
                char_multi: false,
                octal_leading_zero: true,
            },
            Dialect::Nasm => LexConfig {
                dialect: d,
                line_comment: vec![";"],
                block_comment: false,
                stmt_sep: vec![],
                radix_suffix: true,
                local_label_refs: false,
                char_multi: true,
                octal_leading_zero: false,
            },
        }
    }
}

impl Default for LexConfig {
    fn default() -> LexConfig {
        LexConfig::for_dialect(Dialect::default())
    }
}

/// Direction of a numeric local-label reference (`1f` / `1b`).
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum LocalDir {
    Forward,
    Backward,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Punct {
    Comma,
    Colon,
    LParen,
    RParen,
    LBracket,
    RBracket,
    LBrace,
    RBrace,
    Plus,
    Minus,
    Star,
    Slash,
    Percent,
    Amp,
    Pipe,
    Caret,
    Tilde,
    Bang,
    Lt,
    Gt,
    Eq,
    At,
    Dollar,
    Hash,
    /// A lone `.`: the location counter in GAS.
    Dot,
    Question,
    Backslash,
    Shl,
    Shr,
    EqEq,
    Ne,
    Le,
    Ge,
    AndAnd,
    OrOr,
}

impl Punct {
    #[rustfmt::skip]
    pub fn as_str(self) -> &'static str {
        use Punct::*;
        match self {
            Comma => ",", Colon => ":", LParen => "(", RParen => ")",
            LBracket => "[", RBracket => "]", LBrace => "{", RBrace => "}",
            Plus => "+", Minus => "-", Star => "*", Slash => "/",
            Percent => "%", Amp => "&", Pipe => "|", Caret => "^",
            Tilde => "~", Bang => "!", Lt => "<", Gt => ">", Eq => "=",
            At => "@", Dollar => "$", Hash => "#", Dot => ".",
            Question => "?", Backslash => "\\",
            Shl => "<<", Shr => ">>", EqEq => "==", Ne => "!=",
            Le => "<=", Ge => ">=", AndAnd => "&&", OrOr => "||",
        }
    }
}

/// Byte strings from string literals, shared across every file in a run so a
/// `TokKind::Str` index stays valid after the lexer that produced it is gone.
#[derive(Default)]
pub struct LitPool {
    strings: Vec<Vec<u8>>,
}

impl LitPool {
    pub fn new() -> LitPool {
        LitPool::default()
    }

    pub fn add(&mut self, bytes: Vec<u8>) -> u32 {
        let i = self.strings.len() as u32;
        self.strings.push(bytes);
        i
    }

    pub fn get(&self, idx: u32) -> &[u8] {
        &self.strings[idx as usize]
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum TokKind {
    Eof,
    /// End of statement: a newline or a dialect statement separator.
    Eol,
    Ident(Name),
    Int(u64),
    /// Index into the lexer's string pool.
    Str(u32),
    Punct(Punct),
    /// `1f` / `2b` style reference to a numeric local label.
    LocalRef(u32, LocalDir),
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Token {
    pub kind: TokKind,
    pub span: Span,
    /// True when whitespace immediately precedes this token. Some syntaxes
    /// need it (NASM distinguishes `foo:` at column 0, AT&T needs to split a
    /// mnemonic from its operands).
    pub preceded_by_space: bool,
}

impl Token {
    pub fn is_eol(&self) -> bool {
        matches!(self.kind, TokKind::Eol | TokKind::Eof)
    }

    pub fn is_punct(&self, p: Punct) -> bool {
        self.kind == TokKind::Punct(p)
    }

    pub fn ident(&self) -> Option<Name> {
        match self.kind {
            TokKind::Ident(n) => Some(n),
            _ => None,
        }
    }
}

pub struct Lexer<'a> {
    src: &'a str,
    bytes: &'a [u8],
    /// Global position of `src[0]`.
    base: u32,
    /// Byte offset within `src`.
    pos: usize,
    pub config: LexConfig,
}

impl<'a> Lexer<'a> {
    pub fn new(sm: &'a SourceMap, file: FileId, config: LexConfig) -> Lexer<'a> {
        let f = sm.file(file);
        Lexer {
            src: &f.src,
            bytes: f.src.as_bytes(),
            base: f.start,
            pos: 0,
            config,
        }
    }

    /// Current global position.
    fn gpos(&self) -> u32 {
        self.base + self.pos as u32
    }

    fn span_from(&self, start: usize) -> Span {
        Span::new(self.base + start as u32, self.gpos())
    }

    fn peek(&self) -> u8 {
        self.bytes.get(self.pos).copied().unwrap_or(0)
    }

    fn peek_at(&self, n: usize) -> u8 {
        self.bytes.get(self.pos + n).copied().unwrap_or(0)
    }

    fn at_end(&self) -> bool {
        self.pos >= self.bytes.len()
    }

    /// Length in bytes of the character at the cursor. `pos` is always on a
    /// character boundary, so this is well defined.
    fn char_len(&self) -> usize {
        self.src[self.pos..]
            .chars()
            .next()
            .map_or(1, char::len_utf8)
    }

    fn starts_with(&self, s: &str) -> bool {
        self.bytes[self.pos.min(self.bytes.len())..].starts_with(s.as_bytes())
    }

    /// Skips spaces, comments and escaped newlines. Returns true if anything
    /// was skipped (so the next token knows it is space-preceded).
    fn skip_trivia(&mut self, diags: &mut DiagBag) -> bool {
        let start = self.pos;
        loop {
            let c = self.peek();
            match c {
                b' ' | b'\t' | b'\r' | 0x0b | 0x0c => {
                    self.pos += 1;
                }
                b'\\' if matches!(self.peek_at(1), b'\n') => {
                    self.pos += 2;
                }
                b'\\' if self.peek_at(1) == b'\r' && self.peek_at(2) == b'\n' => {
                    self.pos += 3;
                }
                _ => {
                    if self.config.block_comment && self.starts_with("/*") {
                        let open = self.pos;
                        self.pos += 2;
                        loop {
                            if self.at_end() {
                                diags.emit(Diagnostic::error(
                                    self.span_from(open),
                                    "unterminated block comment",
                                ));
                                break;
                            }
                            if self.starts_with("*/") {
                                self.pos += 2;
                                break;
                            }
                            self.pos += 1;
                        }
                        continue;
                    }
                    let mut matched = false;
                    for lc in &self.config.line_comment {
                        if self.bytes[self.pos.min(self.bytes.len())..].starts_with(lc.as_bytes()) {
                            while !self.at_end() && self.peek() != b'\n' {
                                self.pos += 1;
                            }
                            matched = true;
                            break;
                        }
                    }
                    if !matched {
                        break;
                    }
                }
            }
        }
        self.pos != start
    }

    pub fn next_token(
        &mut self,
        interner: &mut Interner,
        pool: &mut LitPool,
        diags: &mut DiagBag,
    ) -> Token {
        let spaced = self.skip_trivia(diags);
        let start = self.pos;
        let mk = |k: TokKind, this: &Self| Token {
            kind: k,
            span: this.span_from(start),
            preceded_by_space: spaced,
        };

        if self.at_end() {
            return mk(TokKind::Eof, self);
        }

        let c = self.peek();

        if c == b'\n' {
            self.pos += 1;
            return mk(TokKind::Eol, self);
        }
        if self.config.stmt_sep.contains(&(c as char)) {
            self.pos += 1;
            return mk(TokKind::Eol, self);
        }

        if c.is_ascii_digit() {
            return self.lex_number(start, spaced, interner, diags);
        }

        if is_ident_start(c) || (c == b'.' && is_ident_cont(self.peek_at(1))) {
            // Identifiers may contain non-ASCII characters, so advance by
            // whole characters and never leave `pos` inside one.
            while !self.at_end() && is_ident_cont(self.peek()) {
                self.pos += self.char_len();
            }
            let text = &self.src[start..self.pos];
            let name = interner.intern(text);
            return mk(TokKind::Ident(name), self);
        }

        if c == b'"' {
            return self.lex_string(start, spaced, pool, diags);
        }
        if c == b'\'' {
            return self.lex_char(start, spaced, diags);
        }

        self.lex_punct(start, spaced, diags)
    }

    fn lex_punct(&mut self, start: usize, spaced: bool, diags: &mut DiagBag) -> Token {
        use Punct::*;
        let two = |a: u8, b: u8| -> Option<Punct> {
            Some(match (a, b) {
                (b'<', b'<') => Shl,
                (b'>', b'>') => Shr,
                (b'=', b'=') => EqEq,
                (b'!', b'=') => Ne,
                (b'<', b'>') => Ne,
                (b'<', b'=') => Le,
                (b'>', b'=') => Ge,
                (b'&', b'&') => AndAnd,
                (b'|', b'|') => OrOr,
                _ => return None,
            })
        };
        let c = self.peek();
        if let Some(p) = two(c, self.peek_at(1)) {
            self.pos += 2;
            return Token {
                kind: TokKind::Punct(p),
                span: self.span_from(start),
                preceded_by_space: spaced,
            };
        }
        #[rustfmt::skip]
        let p = match c {
            b',' => Comma, b':' => Colon, b'(' => LParen, b')' => RParen,
            b'[' => LBracket, b']' => RBracket, b'{' => LBrace, b'}' => RBrace,
            b'+' => Plus, b'-' => Minus, b'*' => Star, b'/' => Slash,
            b'%' => Percent, b'&' => Amp, b'|' => Pipe, b'^' => Caret,
            b'~' => Tilde, b'!' => Bang, b'<' => Lt, b'>' => Gt, b'=' => Eq,
            b'@' => At, b'$' => Dollar, b'#' => Hash, b'.' => Dot,
            b'?' => Question, b'\\' => Backslash,
            _ => {
                // Consume one whole UTF-8 char so we do not split a codepoint.
                let ch = self.src[start..].chars().next().unwrap_or('\u{fffd}');
                self.pos += ch.len_utf8();
                let span = self.span_from(start);
                diags.emit(Diagnostic::error(span, format!("unexpected character `{ch}`")));
                return Token { kind: TokKind::Punct(Question), span, preceded_by_space: spaced };
            }
        };
        self.pos += 1;
        Token {
            kind: TokKind::Punct(p),
            span: self.span_from(start),
            preceded_by_space: spaced,
        }
    }

    fn lex_number(
        &mut self,
        start: usize,
        spaced: bool,
        _interner: &mut Interner,
        diags: &mut DiagBag,
    ) -> Token {
        let mk = |k: TokKind, this: &Self| Token {
            kind: k,
            span: this.span_from(start),
            preceded_by_space: spaced,
        };

        // GAS numeric local labels: `1f` / `1b` referring to a nearby `1:`.
        if self.config.local_label_refs && self.peek().is_ascii_digit() {
            let mut p = self.pos;
            while p < self.bytes.len() && self.bytes[p].is_ascii_digit() {
                p += 1;
            }
            let after = self.bytes.get(p).copied().unwrap_or(0);
            if (after == b'f' || after == b'b')
                && !is_ident_cont(self.bytes.get(p + 1).copied().unwrap_or(0))
            {
                let digits = &self.src[self.pos..p];
                // Only plain decimal runs are local labels; `0x1f` is a number.
                if (!digits.starts_with('0') || digits.len() == 1)
                    && let Ok(n) = digits.parse::<u32>()
                {
                    let dir = if after == b'f' {
                        LocalDir::Forward
                    } else {
                        LocalDir::Backward
                    };
                    self.pos = p + 1;
                    return mk(TokKind::LocalRef(n, dir), self);
                }
            }
        }

        // A `0x` / `0b` / `0o` prefix fixes the radix up front.
        let mut radix: u32 = 10;
        let mut digits_start = self.pos;
        if self.peek() == b'0' {
            let next = self.peek_at(1) | 0x20;
            let prefix_radix = match next {
                b'x' => Some(16),
                b'b' => Some(2),
                b'o' => Some(8),
                _ => None,
            };
            // Only treat it as a prefix if a valid digit actually follows,
            // so NASM's `0b` (binary zero) still lexes as a suffixed literal.
            if let Some(r) = prefix_radix
                && ((self.peek_at(2) as char).is_digit(r) || self.peek_at(2) == b'_')
            {
                radix = r;
                digits_start = self.pos + 2;
            }
        }
        let prefixed = digits_start != self.pos;
        self.pos = digits_start;

        // Scan the maximal alphanumeric run, then decide what it means. A
        // trailing radix letter can only be recognised once the run is known:
        // in `0ffh` the `f`s are digits and the `h` is the radix.
        let run_start = self.pos;
        while !self.at_end() && (self.peek().is_ascii_alphanumeric() || self.peek() == b'_') {
            self.pos += 1;
        }
        let mut run = &self.src[run_start..self.pos];

        if !prefixed {
            if self.config.radix_suffix
                && let Some(last) = run.as_bytes().last().copied()
            {
                let suffix_radix = match last | 0x20 {
                    b'h' => Some(16),
                    b'b' | b'y' => Some(2),
                    b'o' | b'q' => Some(8),
                    b'd' | b't' => Some(10),
                    _ => None,
                };
                if let Some(sr) = suffix_radix {
                    let body = &run[..run.len() - 1];
                    let ok = !body.is_empty() && body.chars().all(|c| c == '_' || c.is_digit(sr));
                    if ok {
                        radix = sr;
                        run = body;
                    }
                }
            }
            if radix == 10
                && self.config.octal_leading_zero
                && run.len() > 1
                && run.starts_with('0')
            {
                radix = 8;
                run = &run[1..];
            }
        }

        if run.is_empty() || run.chars().all(|c| c == '_') {
            let span = self.span_from(start);
            diags.emit(Diagnostic::error(span, "integer literal has no digits"));
            return mk(TokKind::Int(0), self);
        }

        let mut value: u64 = 0;
        let mut overflow = false;
        for c in run.chars() {
            if c == '_' {
                continue;
            }
            let Some(d) = c.to_digit(radix) else {
                let span = self.span_from(start);
                let text = self.src[start..self.pos].to_string();
                diags.emit(Diagnostic::error(
                    span,
                    format!("invalid digit `{c}` for base-{radix} literal `{text}`"),
                ));
                return mk(TokKind::Int(0), self);
            };
            match value
                .checked_mul(radix as u64)
                .and_then(|v| v.checked_add(d as u64))
            {
                Some(v) => value = v,
                None => overflow = true,
            }
        }

        if overflow {
            let span = self.span_from(start);
            diags.emit(Diagnostic::error(
                span,
                "integer literal out of range for 64 bits",
            ));
            return mk(TokKind::Int(0), self);
        }
        mk(TokKind::Int(value), self)
    }

    fn lex_string(
        &mut self,
        start: usize,
        spaced: bool,
        pool: &mut LitPool,
        diags: &mut DiagBag,
    ) -> Token {
        self.pos += 1; // opening quote
        let mut buf = Vec::new();
        loop {
            if self.at_end() || self.peek() == b'\n' {
                diags.emit(Diagnostic::error(
                    self.span_from(start),
                    "unterminated string literal",
                ));
                break;
            }
            let c = self.peek();
            if c == b'"' {
                self.pos += 1;
                break;
            }
            if c == b'\\' {
                self.pos += 1;
                self.read_escape(&mut buf, diags);
            } else {
                buf.push(c);
                self.pos += 1;
            }
        }
        let idx = pool.add(buf);
        Token {
            kind: TokKind::Str(idx),
            span: self.span_from(start),
            preceded_by_space: spaced,
        }
    }

    fn lex_char(&mut self, start: usize, spaced: bool, diags: &mut DiagBag) -> Token {
        self.pos += 1; // opening quote
        let mut buf = Vec::new();
        if self.config.char_multi {
            // NASM: everything up to the closing quote, packed big-endian.
            loop {
                if self.at_end() || self.peek() == b'\n' {
                    diags.emit(Diagnostic::error(
                        self.span_from(start),
                        "unterminated character literal",
                    ));
                    break;
                }
                if self.peek() == b'\'' {
                    self.pos += 1;
                    break;
                }
                if self.peek() == b'\\' {
                    self.pos += 1;
                    self.read_escape(&mut buf, diags);
                } else {
                    let ch = self.src[self.pos..].chars().next().unwrap_or('\0');
                    self.pos += ch.len_utf8();
                    let mut tmp = [0u8; 4];
                    buf.extend_from_slice(ch.encode_utf8(&mut tmp).as_bytes());
                }
            }
        } else {
            // GAS: exactly one character, closing quote optional (`.byte 'a`).
            if self.at_end() || self.peek() == b'\n' {
                let span = self.span_from(start);
                diags.emit(Diagnostic::error(span, "unterminated character literal"));
                return Token {
                    kind: TokKind::Int(0),
                    span,
                    preceded_by_space: spaced,
                };
            }
            if self.peek() == b'\\' {
                self.pos += 1;
                self.read_escape(&mut buf, diags);
            } else {
                let ch = self.src[self.pos..].chars().next().unwrap_or('\0');
                self.pos += ch.len_utf8();
                let mut tmp = [0u8; 4];
                buf.extend_from_slice(ch.encode_utf8(&mut tmp).as_bytes());
            }
            if self.peek() == b'\'' {
                self.pos += 1;
            }
        }
        // Multi-byte character constants pack big-endian, as GAS does.
        let mut v: u64 = 0;
        for &b in buf.iter().take(8) {
            v = (v << 8) | b as u64;
        }
        Token {
            kind: TokKind::Int(v),
            span: self.span_from(start),
            preceded_by_space: spaced,
        }
    }

    /// Reads one escape sequence, the leading backslash already consumed.
    fn read_escape(&mut self, buf: &mut Vec<u8>, diags: &mut DiagBag) {
        let esc_start = self.pos - 1;
        if self.at_end() {
            diags.emit(Diagnostic::error(
                self.span_from(esc_start),
                "trailing backslash",
            ));
            return;
        }
        let c = self.peek();
        self.pos += 1;
        let simple = match c {
            b'n' => Some(b'\n'),
            b't' => Some(b'\t'),
            b'r' => Some(b'\r'),
            b'0'..=b'7' => None,
            b'a' => Some(0x07),
            b'b' => Some(0x08),
            b'e' => Some(0x1b),
            b'f' => Some(0x0c),
            b'v' => Some(0x0b),
            b'\\' => Some(b'\\'),
            b'\'' => Some(b'\''),
            b'"' => Some(b'"'),
            b'\n' => return, // escaped newline: nothing emitted
            b'x' => {
                let mut v: u32 = 0;
                let mut n = 0;
                while let Some(d) = (self.peek() as char).to_digit(16) {
                    v = v.wrapping_mul(16).wrapping_add(d);
                    self.pos += 1;
                    n += 1;
                }
                if n == 0 {
                    diags.emit(Diagnostic::error(
                        self.span_from(esc_start),
                        "`\\x` escape needs at least one hex digit",
                    ));
                }
                buf.push(v as u8);
                return;
            }
            other => {
                diags.emit(Diagnostic::warning(
                    self.span_from(esc_start),
                    format!("unknown escape `\\{}`", other as char),
                ));
                Some(other)
            }
        };
        match simple {
            Some(b) => buf.push(b),
            None => {
                // Octal: up to three digits, the first already consumed.
                let mut v: u32 = (c - b'0') as u32;
                for _ in 0..2 {
                    let d = self.peek();
                    if !(b'0'..=b'7').contains(&d) {
                        break;
                    }
                    v = v * 8 + (d - b'0') as u32;
                    self.pos += 1;
                }
                if v > 0xff {
                    diags.emit(Diagnostic::warning(
                        self.span_from(esc_start),
                        "octal escape out of range, truncated to 8 bits",
                    ));
                }
                buf.push(v as u8);
            }
        }
    }
}

fn is_ident_start(c: u8) -> bool {
    c.is_ascii_alphabetic() || c == b'_' || c >= 0x80
}

fn is_ident_cont(c: u8) -> bool {
    c.is_ascii_alphanumeric() || c == b'_' || c == b'.' || c == b'$' || c >= 0x80
}

#[cfg(test)]
mod tests {
    use super::*;

    struct Harness {
        sm: SourceMap,
        interner: Interner,
        diags: DiagBag,
        pool: LitPool,
    }

    fn lex_all(src: &str, dialect: Dialect) -> (Vec<TokKind>, Harness) {
        let mut h = Harness {
            sm: SourceMap::new(),
            interner: Interner::new(),
            diags: DiagBag::new(),
            pool: LitPool::new(),
        };
        let f = h.sm.add("t.s", src);
        let mut kinds = Vec::new();
        {
            let mut lx = Lexer::new(&h.sm, f, LexConfig::for_dialect(dialect));
            loop {
                let t = lx.next_token(&mut h.interner, &mut h.pool, &mut h.diags);
                kinds.push(t.kind);
                if t.kind == TokKind::Eof {
                    break;
                }
            }
        }
        (kinds, h)
    }

    #[test]
    fn lexes_att_instruction() {
        let (k, h) = lex_all("movq %rax, %rbx\n", Dialect::Gas);
        assert_eq!(k[0], TokKind::Ident(h.interner.lookup("movq").unwrap()));
        assert_eq!(k[1], TokKind::Punct(Punct::Percent));
        assert_eq!(k[2], TokKind::Ident(h.interner.lookup("rax").unwrap()));
        assert_eq!(k[3], TokKind::Punct(Punct::Comma));
        assert_eq!(k[6], TokKind::Eol);
        assert_eq!(k[7], TokKind::Eof);
        assert!(!h.diags.has_errors());
    }

    #[test]
    fn number_bases() {
        let (k, _) = lex_all("0x1f 0b1010 0o17 42 0 1_000 010", Dialect::Gas);
        assert_eq!(
            &k[..7],
            &[
                TokKind::Int(31),
                TokKind::Int(10),
                TokKind::Int(15),
                TokKind::Int(42),
                TokKind::Int(0),
                TokKind::Int(1000),
                // A bare leading zero is octal in GAS.
                TokKind::Int(8),
            ]
        );
    }

    #[test]
    fn nasm_radix_suffixes() {
        let (k, _) = lex_all("0ffh 1010b 17q 99d 0b1010 0xff 010", Dialect::Nasm);
        assert_eq!(
            &k[..7],
            &[
                TokKind::Int(255),
                TokKind::Int(10),
                TokKind::Int(15),
                TokKind::Int(99),
                TokKind::Int(10),
                TokKind::Int(255),
                // NASM has no leading-zero octal rule.
                TokKind::Int(10),
            ]
        );
    }

    #[test]
    fn gas_local_label_refs_not_binary() {
        let (k, _) = lex_all("jmp 1b\njmp 2f\n", Dialect::Gas);
        assert_eq!(k[1], TokKind::LocalRef(1, LocalDir::Backward));
        assert_eq!(k[4], TokKind::LocalRef(2, LocalDir::Forward));
    }

    #[test]
    fn comment_styles_follow_dialect() {
        let (k, _) = lex_all("nop # gas comment\nnop", Dialect::Gas);
        assert_eq!(k[1], TokKind::Eol);
        // In GAS `;` separates statements rather than starting a comment.
        let (k, _) = lex_all("nop ; nop", Dialect::Gas);
        assert_eq!(k[1], TokKind::Eol);
        assert!(matches!(k[2], TokKind::Ident(_)));
        // In NASM it is a comment.
        let (k, _) = lex_all("nop ; nop", Dialect::Nasm);
        assert!(matches!(k[0], TokKind::Ident(_)));
        assert_eq!(k[1], TokKind::Eof);
    }

    #[test]
    fn block_comments_and_continuations() {
        let (k, _) = lex_all("nop /* here\nand here */ nop \\\n nop\n", Dialect::Gas);
        assert!(matches!(k[0], TokKind::Ident(_)));
        assert!(matches!(k[1], TokKind::Ident(_)));
        assert!(matches!(k[2], TokKind::Ident(_)));
        assert_eq!(k[3], TokKind::Eol);
    }

    #[test]
    fn string_escapes() {
        let (k, h) = lex_all(r#" "a\nb\x41\101\\" "#, Dialect::Gas);
        let TokKind::Str(i) = k[0] else {
            panic!("not a string: {:?}", k[0])
        };
        assert_eq!(h.pool.get(i), b"a\nbAA\\");
        assert!(!h.diags.has_errors());
    }

    #[test]
    fn char_literals() {
        let (k, _) = lex_all("'a' '\\n'", Dialect::Gas);
        assert_eq!(k[0], TokKind::Int(b'a' as u64));
        assert_eq!(k[1], TokKind::Int(10));
        // GAS allows the closing quote to be omitted.
        let (k, _) = lex_all(".byte 'a, 'b", Dialect::Gas);
        assert_eq!(k[1], TokKind::Int(b'a' as u64));
        assert_eq!(k[2], TokKind::Punct(Punct::Comma));
        assert_eq!(k[3], TokKind::Int(b'b' as u64));
        // NASM packs multi-character literals big-endian.
        let (k, _) = lex_all("'ab'", Dialect::Nasm);
        assert_eq!(k[0], TokKind::Int(0x6162));
    }

    #[test]
    fn dot_is_location_counter_but_dot_ident_is_a_name() {
        let (k, h) = lex_all(".byte .", Dialect::Gas);
        assert_eq!(k[0], TokKind::Ident(h.interner.lookup(".byte").unwrap()));
        assert_eq!(k[1], TokKind::Punct(Punct::Dot));
    }

    #[test]
    fn reports_unterminated_string() {
        let (_, h) = lex_all("\"abc\n", Dialect::Gas);
        assert!(h.diags.has_errors());
    }

    #[test]
    fn multi_char_operators() {
        let (k, _) = lex_all("<< >> == != <= >= && ||", Dialect::Gas);
        use Punct::*;
        let want = [Shl, Shr, EqEq, Ne, Le, Ge, AndAnd, OrOr];
        for (i, p) in want.iter().enumerate() {
            assert_eq!(k[i], TokKind::Punct(*p), "at {i}");
        }
    }
}