sbpf-assembler 0.1.9

Assembler for SBPF (Solana BPF) assembly language
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
use {
    super::{
        SourceLine,
        macro_def::{MacroDef, scan_macro_definitions},
        source_map::SourceOrigin,
    },
    crate::errors::CompileError,
    std::{
        collections::HashMap,
        sync::atomic::{AtomicU64, Ordering},
    },
};

const MAX_EXPANSION_DEPTH: u32 = 100;

/// Global expansion counter for `\@` unique IDs
static EXPANSION_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Reset the expansion counter (for testing)
#[cfg(test)]
pub(crate) fn reset_expansion_counter() {
    EXPANSION_COUNTER.store(0, Ordering::Relaxed);
}

/// An expansion error paired with its source origin
#[derive(Debug)]
pub(crate) struct ExpandError {
    pub error: CompileError,
    pub origin: Option<SourceOrigin>,
}

/// Expand all macros, `.rept`, and `.irp` directives in the given lines.
///
/// Returns the expanded lines and any errors encountered.
pub(crate) fn expand_macros(
    lines: Vec<SourceLine>,
) -> Result<(Vec<SourceLine>, Vec<ExpandError>), Vec<ExpandError>> {
    // Scan for macro definitions and separate them from regular lines
    let scan_result = scan_macro_definitions(lines);

    if !scan_result.errors.is_empty() {
        return Err(scan_result
            .errors
            .into_iter()
            .map(|(error, origin)| ExpandError {
                error,
                origin: Some(origin),
            })
            .collect());
    }

    let macros = scan_result.macros;
    let mut errors = Vec::new();
    let mut output = Vec::new();

    // Expand macro invocations in remaining lines
    for line in scan_result.remaining_lines {
        expand_line(&line, &macros, &mut output, &mut errors, 0);
    }

    // Now, handle .rept and .irp on the post-macro output
    let output = expand_repetitions(output)?;

    Ok((output, errors))
}

/// Expand a single line, checking if it's a macro invocation.
fn expand_line(
    line: &SourceLine,
    macros: &HashMap<String, MacroDef>,
    output: &mut Vec<SourceLine>,
    errors: &mut Vec<ExpandError>,
    depth: u32,
) {
    if depth > MAX_EXPANSION_DEPTH {
        errors.push(ExpandError {
            error: CompileError::MacroRecursionLimit {
                limit: MAX_EXPANSION_DEPTH,
                span: 0..0,
                custom_label: None,
            },
            origin: Some(line.origin.clone()),
        });
        return;
    }

    let trimmed = line.text.trim();

    // Check if the first token matches a known macro name
    let first_token = first_token(trimmed);
    if let Some(macro_def) = first_token.and_then(|name| macros.get(name)) {
        let args_str = trimmed[macro_def.name.len()..].trim();
        let args = split_args(args_str);

        match bind_args(macro_def, &args) {
            Ok(bindings) => {
                let expansion_id = EXPANSION_COUNTER.fetch_add(1, Ordering::Relaxed);

                // Expand each body line with parameter substitution
                for body_line in &macro_def.body_lines {
                    let expanded_text = substitute(body_line, &bindings, expansion_id);
                    let expanded_line = SourceLine {
                        text: expanded_text,
                        origin: SourceOrigin::with_macro_expansion(
                            macro_def.defined_at.file_id,
                            macro_def.defined_at.line,
                            macro_def.name.clone(),
                            line.origin.clone(),
                            depth + 1,
                        ),
                    };

                    // Rescan for further macro invocations
                    expand_line(&expanded_line, macros, output, errors, depth + 1);
                }
            }
            Err(e) => errors.push(ExpandError {
                error: e,
                origin: Some(line.origin.clone()),
            }),
        }
    } else {
        // Not a macro invocation, pass through
        output.push(line.clone());
    }
}

/// Extract the first whitespace-delimited token from a line.
/// Skips labels (tokens ending with ':').
fn first_token(line: &str) -> Option<&str> {
    let trimmed = line.trim();
    if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with(';') {
        return None;
    }

    // If line starts with '.', it's a directive, not a macro
    if trimmed.starts_with('.') {
        return None;
    }

    let end = trimmed
        .find(|c: char| c.is_whitespace() || c == ',')
        .unwrap_or(trimmed.len());
    let token = &trimmed[..end];

    // Skip labels (they end with ':')
    if token.ends_with(':') {
        // Check if there's something after the label on the same line
        let rest = trimmed[end..].trim();
        if rest.is_empty() {
            return None;
        }
        return first_token(rest);
    }

    Some(token)
}

/// Split arguments by top-level commas, respecting quotes and brackets.
fn split_args(args_str: &str) -> Vec<String> {
    if args_str.is_empty() {
        return Vec::new();
    }

    let mut args = Vec::new();
    let mut current = String::new();
    let mut depth = 0i32; // bracket depth
    let mut in_string = false;
    let mut escape_next = false;

    for ch in args_str.chars() {
        if escape_next {
            current.push(ch);
            escape_next = false;
            continue;
        }

        match ch {
            '\\' if in_string => {
                current.push(ch);
                escape_next = true;
            }
            '"' => {
                in_string = !in_string;
                current.push(ch);
            }
            '(' | '[' if !in_string => {
                depth += 1;
                current.push(ch);
            }
            ')' | ']' if !in_string => {
                depth -= 1;
                current.push(ch);
            }
            ',' if !in_string && depth == 0 => {
                args.push(current.trim().to_string());
                current = String::new();
            }
            _ => {
                current.push(ch);
            }
        }
    }

    let last = current.trim().to_string();
    if !last.is_empty() {
        args.push(last);
    }

    args
}

/// Bind arguments to macro parameters, producing a name->value map.
fn bind_args(
    macro_def: &MacroDef,
    args: &[String],
) -> Result<HashMap<String, String>, CompileError> {
    let mut bindings = HashMap::new();

    let required_count = macro_def
        .params
        .iter()
        .filter(|p| p.default.is_none() && !p.is_vararg)
        .count();

    let has_vararg = macro_def.params.iter().any(|p| p.is_vararg);

    // Check argument count
    if !has_vararg && args.len() > macro_def.params.len() {
        return Err(CompileError::MacroArgCount {
            name: macro_def.name.clone(),
            expected: macro_def.params.len(),
            got: args.len(),
            span: 0..0,
            custom_label: None,
        });
    }
    if args.len() < required_count {
        return Err(CompileError::MacroArgCount {
            name: macro_def.name.clone(),
            expected: required_count,
            got: args.len(),
            span: 0..0,
            custom_label: None,
        });
    }

    for (i, param) in macro_def.params.iter().enumerate() {
        if param.is_vararg {
            // Collect all remaining arguments
            let vararg_values: Vec<&str> = args[i..].iter().map(|s| s.as_str()).collect();
            bindings.insert(param.name.clone(), vararg_values.join(", "));
        } else if i < args.len() {
            bindings.insert(param.name.clone(), args[i].clone());
        } else if let Some(ref default) = param.default {
            bindings.insert(param.name.clone(), default.clone());
        }
        // required params without args already caught above
    }

    // Also add positional references (\1, \2, etc.)
    for (i, arg) in args.iter().enumerate() {
        bindings.insert((i + 1).to_string(), arg.clone());
    }

    Ok(bindings)
}

/// Perform parameter substitution on a single body line.
///
/// Handles:
/// - `\name` -> argument value
/// - `\@` -> unique expansion ID
/// - `\()` -> zero-width concatenation (disappears after adjacent substitution)
/// - `\\` -> literal `\`
fn substitute(line: &str, bindings: &HashMap<String, String>, expansion_id: u64) -> String {
    let mut result = String::with_capacity(line.len());
    let chars: Vec<char> = line.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        if chars[i] == '\\' && i + 1 < len {
            let next = chars[i + 1];

            if next == '\\' {
                // Escaped backslash
                result.push('\\');
                i += 2;
            } else if next == '@' {
                // Unique expansion ID
                result.push_str(&expansion_id.to_string());
                i += 2;
            } else if next == '(' && i + 2 < len && chars[i + 2] == ')' {
                // Token concatenation -- just skip it (zero-width separator)
                i += 3;
            } else if next.is_alphanumeric() || next == '_' {
                // Parameter reference: collect the name
                let start = i + 1;
                let mut end = start;
                while end < len && (chars[end].is_alphanumeric() || chars[end] == '_') {
                    end += 1;
                }
                let param_name: String = chars[start..end].iter().collect();

                if let Some(value) = bindings.get(&param_name) {
                    result.push_str(value);
                    // Check if followed by \() for concatenation
                    if end + 2 < len
                        && chars[end] == '\\'
                        && chars[end + 1] == '('
                        && chars[end + 2] == ')'
                    {
                        // Skip the \() separator
                        end += 3;
                    }
                } else {
                    // Unknown parameter -- keep as-is
                    result.push('\\');
                    result.push_str(&param_name);
                }
                i = end;
            } else {
                // Unknown escape, keep as-is
                result.push('\\');
                i += 1;
            }
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }

    result
}

/// Expand `.rept` and `.irp` blocks (these are processed before macro expansion).
fn expand_repetitions(lines: Vec<SourceLine>) -> Result<Vec<SourceLine>, Vec<ExpandError>> {
    let mut output = Vec::new();
    let mut errors = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let trimmed = lines[i].text.trim();

        if let Some(count_str) = trimmed.strip_prefix(".rept").and_then(|r| {
            if r.starts_with(char::is_whitespace) {
                Some(r.trim())
            } else {
                None
            }
        }) {
            // Find matching .endr
            let (body, end_idx) = find_endr_block(&lines, i + 1, &lines[i].origin)?;

            match count_str.parse::<usize>() {
                Ok(count) => {
                    // Recursively expand nested .rept/.irp inside the body
                    let expanded_body = expand_repetitions(body)?;
                    for _ in 0..count {
                        output.extend(expanded_body.iter().cloned());
                    }
                }
                Err(_) => {
                    errors.push(ExpandError {
                        error: CompileError::InvalidReptCount {
                            value: count_str.to_string(),
                            span: 0..0,
                            custom_label: None,
                        },
                        origin: Some(lines[i].origin.clone()),
                    });
                }
            }

            i = end_idx + 1;
        } else if let Some(rest) = trimmed.strip_prefix(".irp").and_then(|r| {
            if r.starts_with(char::is_whitespace) {
                Some(r.trim())
            } else {
                None
            }
        }) {
            // Parse: .irp var, val1, val2, val3
            let (body, end_idx) = find_endr_block(&lines, i + 1, &lines[i].origin)?;

            if let Some((var_name, values_str)) = rest.split_once(',') {
                let var_name = var_name.trim();
                let values = split_args(values_str.trim());

                for value in &values {
                    let mut bindings = HashMap::new();
                    bindings.insert(var_name.to_string(), value.clone());

                    // Substitute for this iteration first, then recursively
                    // expand any nested .rept/.irp in the result.
                    let mut iter_lines = Vec::with_capacity(body.len());
                    for body_line in &body {
                        let expansion_id = EXPANSION_COUNTER.fetch_add(1, Ordering::Relaxed);
                        let expanded_text = substitute(&body_line.text, &bindings, expansion_id);
                        iter_lines.push(SourceLine {
                            text: expanded_text,
                            origin: body_line.origin.clone(),
                        });
                    }
                    output.extend(expand_repetitions(iter_lines)?);
                }
            }

            i = end_idx + 1;
        } else {
            output.push(lines[i].clone());
            i += 1;
        }
    }

    if errors.is_empty() {
        Ok(output)
    } else {
        Err(errors)
    }
}

/// Find the matching `.endr` for a `.rept` or `.irp` block, handling nesting.
fn find_endr_block(
    lines: &[SourceLine],
    start: usize,
    directive_origin: &SourceOrigin,
) -> Result<(Vec<SourceLine>, usize), Vec<ExpandError>> {
    let mut depth = 1u32;
    let mut i = start;

    while i < lines.len() {
        let trimmed = lines[i].text.trim();
        if trimmed == ".endr" {
            depth -= 1;
            if depth == 0 {
                let body = lines[start..i].to_vec();
                return Ok((body, i));
            }
        } else if trimmed.starts_with(".rept") || trimmed.starts_with(".irp") {
            depth += 1;
        }
        i += 1;
    }

    Err(vec![ExpandError {
        error: CompileError::UnclosedRept {
            span: 0..0,
            custom_label: None,
        },
        origin: Some(directive_origin.clone()),
    }])
}

#[cfg(test)]
mod tests {
    use {super::*, crate::preprocessor::source_map::FileId};

    fn make_line(text: &str, line: u32) -> SourceLine {
        SourceLine {
            text: text.to_string(),
            origin: SourceOrigin::new(FileId(0), line),
        }
    }

    fn expand_and_collect(lines: Vec<SourceLine>) -> Vec<String> {
        let (result, errors) = expand_macros(lines).unwrap();
        assert!(errors.is_empty(), "Unexpected errors: {:?}", errors);
        result.iter().map(|l| l.text.clone()).collect()
    }

    #[test]
    fn test_first_token() {
        assert_eq!(first_token("MY_MACRO arg1, arg2"), Some("MY_MACRO"));
        assert_eq!(first_token("  MY_MACRO  "), Some("MY_MACRO"));
        assert_eq!(first_token(".globl entry"), None); // directive
        assert_eq!(first_token("# comment"), None);
        assert_eq!(first_token(""), None);
        assert_eq!(first_token("label:"), None);
        assert_eq!(first_token("label: MY_MACRO"), Some("MY_MACRO"));
    }

    #[test]
    fn test_split_args() {
        assert_eq!(split_args("a, b, c"), vec!["a", "b", "c"]);
        assert_eq!(split_args("r1, 42"), vec!["r1", "42"]);
        assert_eq!(
            split_args("\"hello, world\", 1"),
            vec!["\"hello, world\"", "1"]
        );
        assert_eq!(split_args(""), Vec::<String>::new());
        assert_eq!(split_args("single"), vec!["single"]);
    }

    #[test]
    fn test_substitute_simple() {
        let mut bindings = HashMap::new();
        bindings.insert("reg".to_string(), "r1".to_string());
        bindings.insert("val".to_string(), "42".to_string());

        assert_eq!(
            substitute("    mov64 \\reg, \\val", &bindings, 0),
            "    mov64 r1, 42"
        );
    }

    #[test]
    fn test_substitute_unique_id() {
        let bindings = HashMap::new();
        assert_eq!(substitute("label_\\@:", &bindings, 5), "label_5:");
    }

    #[test]
    fn test_substitute_concatenation() {
        let mut bindings = HashMap::new();
        bindings.insert("msg".to_string(), "e1".to_string());

        assert_eq!(substitute("\\msg\\()_end", &bindings, 0), "e1_end");
    }

    #[test]
    fn test_substitute_escaped_backslash() {
        let bindings = HashMap::new();
        assert_eq!(substitute("\\\\n", &bindings, 0), "\\n");
    }

    #[test]
    fn test_simple_macro_expansion() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro NOP", 1),
            make_line("    mov64 r0, 0", 2),
            make_line(".endm", 3),
            make_line("NOP", 4),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["    mov64 r0, 0"]);
    }

    #[test]
    fn test_macro_with_args() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro MOV dst, src", 1),
            make_line("    mov64 \\dst, \\src", 2),
            make_line(".endm", 3),
            make_line("MOV r1, r2", 4),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["    mov64 r1, r2"]);
    }

    #[test]
    fn test_macro_with_default() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro SET reg, val=0", 1),
            make_line("    mov64 \\reg, \\val", 2),
            make_line(".endm", 3),
            make_line("SET r1", 4),
            make_line("SET r2, 42", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["    mov64 r1, 0", "    mov64 r2, 42"]);
    }

    #[test]
    fn test_macro_unique_id() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro LOOP", 1),
            make_line("loop_\\@:", 2),
            make_line(".endm", 3),
            make_line("LOOP", 4),
            make_line("LOOP", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result.len(), 2);
        assert!(result.iter().all(|s| {
            s.starts_with("loop_")
                && s.ends_with(':')
                && s["loop_".len()..s.len() - 1].parse::<u64>().is_ok()
        }));
        assert_ne!(result[0], result[1]);
    }

    #[test]
    fn test_macro_concatenation() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro DEF_STR name, text", 1),
            make_line("\\name:", 2),
            make_line("    .ascii \\text", 3),
            make_line("\\name\\()_end:", 4),
            make_line(".endm", 5),
            make_line("DEF_STR e1, \"error\"", 6),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["e1:", "    .ascii \"error\"", "e1_end:"]);
    }

    #[test]
    fn test_nested_macro_expansion() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro INNER val", 1),
            make_line("    mov64 r0, \\val", 2),
            make_line(".endm", 3),
            make_line(".macro OUTER val", 4),
            make_line("INNER \\val", 5),
            make_line(".endm", 6),
            make_line("OUTER 42", 7),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["    mov64 r0, 42"]);
    }

    #[test]
    fn test_recursion_limit() {
        let lines = vec![
            make_line(".macro FOREVER", 1),
            make_line("FOREVER", 2),
            make_line(".endm", 3),
            make_line("FOREVER", 4),
        ];

        let (_, errors) = expand_macros(lines).unwrap();
        assert!(!errors.is_empty());
        assert!(matches!(
            &errors[0].error,
            CompileError::MacroRecursionLimit { .. }
        ));
        // Should have origin pointing to the invocation
        assert!(errors[0].origin.is_some());
    }

    #[test]
    fn test_wrong_arg_count() {
        let lines = vec![
            make_line(".macro NEED_TWO a, b", 1),
            make_line("    mov64 \\a, \\b", 2),
            make_line(".endm", 3),
            make_line("NEED_TWO r1", 4),
        ];

        let (_, errors) = expand_macros(lines).unwrap();
        assert!(!errors.is_empty());
        assert!(matches!(
            &errors[0].error,
            CompileError::MacroArgCount {
                expected: 2,
                got: 1,
                ..
            }
        ));
        assert!(errors[0].origin.is_some());
        assert_eq!(errors[0].origin.as_ref().unwrap().line, 4);
    }

    #[test]
    fn test_vararg() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro LOG fmt, args:vararg", 1),
            make_line("    .ascii \\fmt", 2),
            make_line("    .ascii \\args", 3),
            make_line(".endm", 4),
            make_line("LOG \"hello\", a, b, c", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["    .ascii \"hello\"", "    .ascii a, b, c"]);
    }

    #[test]
    fn test_rept() {
        let lines = vec![
            make_line(".rept 3", 1),
            make_line("    nop", 2),
            make_line(".endr", 3),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result, vec!["    nop", "    nop", "    nop"]);
    }

    #[test]
    fn test_rept_zero() {
        let lines = vec![
            make_line(".rept 0", 1),
            make_line("    nop", 2),
            make_line(".endr", 3),
        ];

        let result = expand_and_collect(lines);
        assert!(result.is_empty());
    }

    #[test]
    fn test_irp() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".irp reg, r1, r2, r3", 1),
            make_line("    mov64 \\reg, 0", 2),
            make_line(".endr", 3),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec!["    mov64 r1, 0", "    mov64 r2, 0", "    mov64 r3, 0"]
        );
    }

    #[test]
    fn test_nested_rept() {
        let lines = vec![
            make_line(".rept 2", 1),
            make_line("    .rept 3", 2),
            make_line("        mov64 r1, 0x1", 3),
            make_line("    .endr", 4),
            make_line(".endr", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(result.len(), 6);
        assert!(result.iter().all(|s| s == "        mov64 r1, 0x1"));
    }

    #[test]
    fn test_nested_irp() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".irp reg, r1, r2", 1),
            make_line("    .irp val, 0x1, 0x2", 2),
            make_line("        mov64 \\reg, \\val", 3),
            make_line("    .endr", 4),
            make_line(".endr", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                "        mov64 r1, 0x1",
                "        mov64 r1, 0x2",
                "        mov64 r2, 0x1",
                "        mov64 r2, 0x2",
            ]
        );
    }

    #[test]
    fn test_rept_inside_irp() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".irp r, r1, r2", 1),
            make_line("    .rept 2", 2),
            make_line("        mov64 \\r, 0x1", 3),
            make_line("    .endr", 4),
            make_line(".endr", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                "        mov64 r1, 0x1",
                "        mov64 r1, 0x1",
                "        mov64 r2, 0x1",
                "        mov64 r2, 0x1",
            ]
        );
    }

    #[test]
    fn test_irp_inside_rept() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".rept 2", 1),
            make_line("    .irp r, r1, r2", 2),
            make_line("        mov64 \\r, 0x1", 3),
            make_line("    .endr", 4),
            make_line(".endr", 5),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                "        mov64 r1, 0x1",
                "        mov64 r2, 0x1",
                "        mov64 r1, 0x1",
                "        mov64 r2, 0x1",
            ]
        );
    }

    #[test]
    fn test_rept_count_from_macro_param() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro TEST num", 1),
            make_line("    .rept \\num", 2),
            make_line("        mov64 r1, 0x123", 3),
            make_line("    .endr", 4),
            make_line(".endm", 5),
            make_line("TEST 3", 7),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                "        mov64 r1, 0x123",
                "        mov64 r1, 0x123",
                "        mov64 r1, 0x123",
            ]
        );
    }

    #[test]
    fn test_irp_values_from_macro_vararg() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro LOAD regs:vararg", 1),
            make_line("    .irp r, \\regs", 2),
            make_line("        mov64 \\r, 0x1", 3),
            make_line("    .endr", 4),
            make_line(".endm", 5),
            make_line("LOAD r1, r2, r3", 7),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                "        mov64 r1, 0x1",
                "        mov64 r2, 0x1",
                "        mov64 r3, 0x1",
            ]
        );
    }

    #[test]
    fn test_unclosed_rept() {
        let lines = vec![make_line(".rept 3", 1), make_line("    nop", 2)];

        let result = expand_macros(lines);
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(matches!(
            &errors[0].error,
            CompileError::UnclosedRept { .. }
        ));
        assert!(errors[0].origin.is_some());
    }

    #[test]
    fn test_full_example_from_spec() {
        reset_expansion_counter();
        let lines = vec![
            make_line(".macro DEF_STR name, text", 1),
            make_line("\\name:", 2),
            make_line("    .ascii \\text", 3),
            make_line("\\name\\()_end:", 4),
            make_line(".endm", 5),
            make_line("", 6),
            make_line(".macro RETURN_ERR code, msg", 7),
            make_line("    lddw r0, \\code", 8),
            make_line("    lddw r1, \\msg", 9),
            make_line("    lddw r2, \\msg\\()_end - \\msg", 10),
            make_line("    call sol_log_", 11),
            make_line("    exit", 12),
            make_line(".endm", 13),
            make_line("", 14),
            make_line("DEF_STR e1, \"error\"", 15),
            make_line("", 16),
            make_line("RETURN_ERR 1, e1", 17),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                "", // blank line between definitions
                "", // blank line after definitions
                "e1:",
                "    .ascii \"error\"",
                "e1_end:",
                "", // blank line
                "    lddw r0, 1",
                "    lddw r1, e1",
                "    lddw r2, e1_end - e1",
                "    call sol_log_",
                "    exit",
            ]
        );
    }

    #[test]
    fn test_passthrough_no_macros() {
        let lines = vec![
            make_line(".globl entrypoint", 1),
            make_line("entrypoint:", 2),
            make_line("    mov64 r1, 42", 3),
            make_line("    exit", 4),
        ];

        let result = expand_and_collect(lines);
        assert_eq!(
            result,
            vec![
                ".globl entrypoint",
                "entrypoint:",
                "    mov64 r1, 42",
                "    exit",
            ]
        );
    }
}