c2rust-transpile 0.22.1

C2Rust transpiler implementation
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
#![deny(missing_docs)]
//! This module provides basic support for converting inline assembly statements.

use std::fmt::{self as fmt, Display, Formatter};

use crate::diagnostics::TranslationResult;

use super::*;
use log::warn;
use proc_macro2::{TokenStream, TokenTree};
use syn::__private::ToTokens;

/// An argument direction specifier for a Rust asm! expression
enum ArgDirSpec {
    In,
    Out,
    InOut,
    LateOut,
    InLateOut,
}

impl Display for ArgDirSpec {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        use ArgDirSpec::*;
        write!(
            f,
            "{}",
            match self {
                In => "in",
                Out => "out",
                InOut => "inout",
                LateOut => "lateout",
                InLateOut => "inlateout",
            }
        )
    }
}

impl ArgDirSpec {
    fn with_in(&self) -> Self {
        use ArgDirSpec::*;
        match self {
            In => In,
            Out => InOut,
            InOut => InOut,
            LateOut => InLateOut,
            InLateOut => InLateOut,
        }
    }
}

/// A machine architecture that rustc inline assembly knows about
#[derive(Copy, Clone, PartialEq)]
enum Arch {
    X86,
    X86_64,
    Arm,
    Aarch64,
    Riscv,
}

/// Parse a machine architecture from a target tuple. This is a best-effort attempt.
fn parse_arch(target_tuple: &str) -> Option<Arch> {
    if target_tuple.starts_with("x86_64") {
        Some(Arch::X86_64)
    } else if target_tuple.starts_with("i386")
        || target_tuple.starts_with("i486")
        || target_tuple.starts_with("i586")
        || target_tuple.starts_with("i686")
        || target_tuple.starts_with("x86")
    {
        Some(Arch::X86)
    } else if target_tuple.starts_with("aarch64")
        || target_tuple.starts_with("armv8")
        || target_tuple.starts_with("arm64")
    {
        Some(Arch::Aarch64)
    } else if target_tuple.starts_with("arm") || target_tuple.starts_with("thumbv") {
        Some(Arch::Arm)
    } else if target_tuple.starts_with("riscv") {
        Some(Arch::Riscv)
    } else {
        None
    }
}

fn parse_constraints(
    mut constraints: &str,
    arch: Arch,
) -> TranslationResult<(ArgDirSpec, bool, String)> {
    let parse_error = |constraints| {
        Err(TranslationError::new(
            None,
            failure::err_msg(
                "Inline assembly constraints could not be parsed: ".to_owned() + constraints,
            )
            .context(TranslationErrorKind::Generic),
        ))
    };
    use ArgDirSpec::*;
    let mut is_input = match constraints.chars().next() {
        Some('+') => {
            constraints = &constraints[1..];
            true
        }
        Some('=') => {
            constraints = &constraints[1..];
            false
        }
        _ => true,
    };

    let early_clobber = if constraints.starts_with('&') {
        constraints = &constraints[1..];
        true
    } else {
        false
    };

    let mut mem_only = if constraints.starts_with('*') {
        constraints = &constraints[1..];
        true
    } else {
        false
    };

    let mut split = constraints.splitn(2, ',');
    constraints = match split.next() {
        Some(c) => c,
        // Parse error
        _ => return parse_error(constraints),
    };
    // If a comma is present, this is an output of form =[&]foo,N
    if split.next().is_some() {
        if !is_input {
            is_input = true;
        } else {
            // '+' followed by ',' is a parse error
            return parse_error(constraints);
        }
    }

    // Handle register names
    let constraints = constraints.replace(['{', '}'], "\"");
    let mut llvm_constraints = constraints.clone();
    let mut constraints = constraints.as_str();

    // Convert (simple) constraints to ones rustc understands
    while !constraints.is_empty() {
        let (c, rest) = constraints.split_at(1);
        let c = c.chars().next().unwrap();
        match c {
            'm' => {
                mem_only = true;
                llvm_constraints = "reg".into();
            }
            'r' => {
                llvm_constraints = "reg".into();
            }
            'i' => {
                // Rust inline assembly has no constraint for that, but uses the argument as an
                // immediate value anyway
                llvm_constraints = "reg".into();
            }
            _ => {
                let is_explicit_reg = c == '"';
                let is_tied = !constraints.contains(|c: char| !c.is_ascii_digit());

                if !(is_explicit_reg || is_tied) {
                    // Attempt to parse machine-specific constraints
                    if let Some((machine_constraints, is_mem)) =
                        translate_machine_constraint(constraints, arch)
                    {
                        llvm_constraints = machine_constraints.into();
                        mem_only = is_mem;
                    } else {
                        warn!(
                            "Did not recognize inline asm constraint: {}\n\
                            It is likely that this will cause compilation errors or \
                            incorrect semantics in the translated program; please \
                            manually correct.",
                            constraints
                        );
                        llvm_constraints = constraints.into();
                    }
                }
                break;
            }
        }
        constraints = rest;
    }

    let mode = if mem_only {
        In
    } else {
        match (is_input, early_clobber) {
            (false, false) => LateOut,
            (false, true) => Out,
            (true, false) => InLateOut,
            (true, true) => InOut,
        }
    };

    Ok((mode, mem_only, llvm_constraints))
}

fn is_regname_or_int(parsed_constraint: &str) -> bool {
    parsed_constraint.contains('"') || parsed_constraint.starts_with(|c: char| c.is_ascii_digit())
}

/// Translate an architecture-specific assembly constraint from llvm/gcc
/// to those accepted by the Rust asm! macro. "Simple" (arch-independent)
/// constraints are handled in `parse_constraints`, not here.
/// See <https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html>,
/// <https://llvm.org/docs/LangRef.html#constraint-codes>, and
/// <https://doc.rust-lang.org/nightly/reference/inline-assembly.html#register-operands>
fn translate_machine_constraint(constraint: &str, arch: Arch) -> Option<(&str, bool)> {
    let mem = &mut false;
    // Many constraints are not handled here, because rustc does. The best we can
    let constraint = match arch {
        Arch::X86 | Arch::X86_64 => match constraint {
            // "R" => "reg_word", // rust does not support this
            "Q" => "reg_abcd",
            "q" => "reg_byte",
            "a" => "\"a\"",
            "b" => "\"b\"",
            "c" => "\"c\"",
            "d" => "\"d\"",
            "S" => "\"si\"",
            "D" => "\"di\"",
            // "A" => "a_and_d", // rust does not support this
            "U" => {
                warn!(
                    "the x86 'U' inline assembly operand constraint cannot \
                be translated correctly. It corresponds to the `clobber_abi` \
                option for `asm!`, but c2rust does not know the ABI being \
                used, so it cannot be translated automatically. Please correct \
                manually after translation."
                );
                return None;
            }
            "f" => "x87_reg",
            "t" => "\"st(0)\"",
            "u" => "\"st(1)\"",
            "x" => "xmm_reg", // this could also translate as ymm_reg
            "y" => "mmx_reg",
            "v" => "zmm_reg",
            "Yz" => "\"xmm0\"",
            "Yk" => "kreg",

            _ => return None,
        },
        Arch::Aarch64 => match constraint {
            "k" => "\"SP\"",
            "w" => "vreg",
            "x" => "vreg_low16",
            // "y" => "vreg_low8", // rust does not support this
            // "Upl" => "preg_low8", // rust does not support this
            "Upa" => "preg",
            "Q" => {
                *mem = true;
                "reg"
            }
            "Ump" => {
                *mem = true;
                "reg"
            }
            _ => return None,
        },
        Arch::Arm => match constraint {
            // "h" => "reg_8_15", // rust does not support this
            "k" => "\"SP\"",
            "l" => "reg",
            "t" => "sreg",
            "x" => "sreg_low16",
            "w" => "dreg",
            // "y" => "sreg",
            // "z" => "sreg",
            "Q" => {
                *mem = true;
                "reg"
            }
            "Uv" | "Uy" | "Uq" => {
                *mem = true;
                "reg"
            }
            _ => return None,
        },
        Arch::Riscv => match constraint {
            "f" => "freg",
            _ => return None,
        },
    };
    Some((constraint, *mem))
}

/// Translate a template modifier from llvm/gcc asm template argument modifiers
/// to those accepted by the Rust asm! macro. This is arch-dependent, so we need
/// to know which architecture the asm targets.
/// See <https://doc.rust-lang.org/nightly/reference/inline-assembly.html#template-modifiers>
fn translate_modifier(modifier: char, arch: Arch) -> Option<char> {
    Some(match arch {
        Arch::X86 | Arch::X86_64 => match modifier {
            'k' => 'e',
            'q' => 'r',
            'b' => 'l',
            'h' => 'h',
            'w' => 'x',
            _ => return None,
        },
        Arch::Aarch64 => modifier,
        Arch::Arm => match modifier {
            'p' | 'q' => return None,
            _ => modifier,
        },
        Arch::Riscv => modifier,
    })
}

/// Rust-native asm! operands, which may be inputs, outputs, or both.
struct BidirAsmOperand {
    dir_spec: ArgDirSpec,
    mem_only: bool,
    constraints: String,
    name: Option<String>,
    // At least one of these is non-None
    in_expr: Option<(usize, CExprId)>,
    out_expr: Option<(usize, CExprId)>,
}

impl BidirAsmOperand {
    /// Return whether an operand is positional (as opposed to named or using an explicit register)
    fn is_positional(&self) -> bool {
        !self.constraints.contains('"') && self.name.is_none()
    }

    /// Return whether this operand occurs at the given position in the original sequence of
    /// [outputs...inputs]. For tied operands, both input and output index is considered.
    fn has_orig_idx(&self, orig_idx: usize) -> bool {
        match (self.out_expr, self.in_expr) {
            (Some((idx, _)), _) if idx == orig_idx => true,
            (_, Some((idx, _))) if idx == orig_idx => true,
            _ => false,
        }
    }
}

/// Return the register and corresponding template modifiers if the constraint
/// uses a reserved register.
fn reg_is_reserved(constraint: &str, arch: Arch) -> Option<(&str, &str)> {
    Some(match arch {
        Arch::X86 => match constraint {
            // esi is reserved on x86
            "\"esi\"" | "\"si\"" => {
                let reg = constraint.trim_matches('"');
                // "e" if esi, "" if si
                let mods = &reg[..reg.len() - 2];
                (reg, mods)
            }
            _ => return None,
        },
        Arch::X86_64 => match constraint {
            // rbx is reserved on x86_64
            "\"bl\"" | "\"bh\"" | "\"bx\"" | "\"ebx\"" | "\"rbx\"" => {
                let reg = constraint.trim_matches('"');
                let mods = if reg.len() == 2 {
                    &reg[1..] // l/h/x
                } else {
                    &reg[..1] // e/r
                };
                (reg, mods)
            }
            _ => return None,
        },
        _ => return None,
    })
}

/// Emit mov instructions and modify inline assembly operands to copy in and/or
/// out when an operand uses a reserved register. Instead of constraining the
/// operand to the reserved register, constrain it to any register and then copy
/// between the reserved register and the (suitably modified, e.g. `{0:x}`)
/// operand.
/// This also requires reordering the operands because we convert them to
/// named operands, which must precede explicit register operands.
///
/// Modifies `operands` and returns a pair of prefix and suffix strings that
/// should be appended to the assembly template.
fn rewrite_reserved_reg_operands(
    att_syntax: bool,
    arch: Arch,
    operands: &mut [BidirAsmOperand],
) -> (String, String) {
    let (mut prolog, mut epilog) = (String::new(), String::new());

    let mut rewrite_idxs = vec![];
    let mut total_positional = 0;

    // Determine which operands must be rewritten and how many
    // positional operands there are. Positional operands must precede named
    // operands, so this tells us where to reinsert operands we rewrite.
    for (i, operand) in operands.iter().enumerate() {
        if operand.is_positional() {
            total_positional += 1;
        } else if let Some((reg, mods)) = reg_is_reserved(&operand.constraints, arch) {
            rewrite_idxs.push((i, reg.to_owned(), mods.to_owned()));
        }
    }

    for (n_moved, (idx, reg, mods)) in rewrite_idxs.into_iter().enumerate() {
        let operand = &mut operands[idx];
        let name = format!("restmp{}", n_moved);
        if let Some((_idx, _in_expr)) = operand.in_expr {
            let move_input = if att_syntax {
                format!("mov %{}, {{{}:{}}}\n", reg, name, mods)
            } else {
                format!("mov {{{}:{}}}\n, {}", name, mods, reg)
            };
            prolog.push_str(&move_input);
        }
        if let Some((_idx, _out_expr)) = operand.out_expr {
            let move_output = if att_syntax {
                format!("\nmov {{{}:{}}}, %{}", name, mods, reg)
            } else {
                format!("\nmov {}, {{{}:{}}}", reg, name, mods)
            };
            epilog.push_str(&move_output);
        }
        operand.constraints = "reg".into();
        operand.name = Some(name);

        // Move operand to after all positional arguments. This does not
        // interfere with moving subsequent operands that use reserved registers
        // because explicit register operands must all come after positional
        // and named operands.
        //let (positional, named, explicit) = split_operands(operands);
        let nth_non_positional = total_positional + n_moved;
        operands.swap(idx, nth_non_positional);
    }

    (prolog, epilog)
}

/// Remove comments from an x86 assembly template. Used only to provide a less-
/// confounding input to our Intel-vs-AT&T detection in `asm_is_att_syntax`.
fn remove_comments(mut asm: &str) -> String {
    // Remove C-style comments
    let mut without_c_comments = String::with_capacity(asm.len());
    while let Some(comment_begin) = asm.find("/*") {
        let comment_len = asm[comment_begin..]
            .find("*/")
            // Comments with no terminator extend to the end of the string
            .unwrap_or_else(|| asm[comment_begin..].len());
        let before_comment = &asm[..comment_begin];
        without_c_comments.push_str(before_comment);
        asm = &asm[comment_begin + comment_len..];
    }
    // Push whatever is left after the final comment
    without_c_comments.push_str(asm);

    // Remove EOL comments from each line
    let mut without_comments = String::with_capacity(without_c_comments.len());
    for line in without_c_comments.lines() {
        if let Some(line_comment_idx) = line.find('#') {
            without_comments.push_str(&line[..line_comment_idx]);
        } else {
            without_comments.push_str(line);
        }
        without_comments.push('\n');
    }
    without_comments
}

/// Detect whether an x86(_64) extended asm template string uses AT&T syntax (vs. Intel).
/// For gcc, AT&T syntax is default... unless `-masm=intel` is passed. This
/// means we can hope but not guarantee that x86 asm with no syntax directive
/// uses AT&T syntax.
/// To handle other cases, try to heuristically detect the variant we get
/// (assuming it's actually x86 asm in the first place...).
/// As the rust x86 default is intel syntax, we need to emit the "att_syntax"
/// option if we get a hint that this asm uses AT&T syntax.
///
/// Note that this function receives the asm template after Clang's translation
/// from gcc syntax (with `%` for substitution/escapes) to LLVM syntax which
/// uses `$` for substitution/escapes. See:
/// <https://llvm.org/docs/LangRef.html#inline-assembler-expressions>
fn asm_is_att_syntax(asm: &str) -> bool {
    // First, remove comments, so we can look at only the semantically
    // significant parts of the asm template.
    let asm = &*remove_comments(asm);

    // Look for syntax directives.
    let intel_directive = asm.find(".intel_syntax");
    let att_directive = asm.find(".att_syntax");
    match (intel_directive, att_directive) {
        (Some(intel_pos), Some(att_pos)) => {
            // Both directives are present; presumably this asm switches to one at
            // its start and restores the default at the end. Whichever comes first
            // should be what the asm uses.
            att_pos < intel_pos
        }
        (Some(_intel), None) => false,
        (None, Some(_att)) => true,
        (None, None) => {
            #[allow(clippy::needless_bool)]
            if asm.contains("word ptr") {
                false
            } else if asm.contains("$$") || asm.contains('%') || asm.contains('(') {
                // Guess based on sigils used in AT&T assembly:
                // $ (escaped) for constants, % for registers, and ( for address calculations
                true
            } else if asm.contains('[') {
                // default to true, because AT&T is the default for gcc inline asm
                false
            } else {
                true
            }
        }
    }
}

/// If applicable, maps the index of an input operand to the
/// output operand it is tied to.
///
/// Utilizes the fact that GNU inline assembly specifies all
/// operands in a particular order of [output1, ... outputN]
/// followed by [input1, ..., inputN] where the indices for all
/// input operands are indexed relative to the size of the
/// output operand sequence.
fn tied_output_operand_idx(
    idx: usize,
    num_output_operands: usize,
    tied_operands: &HashMap<(usize, bool), usize>,
) -> usize {
    if let Some(adj_idx) = idx.checked_sub(num_output_operands) {
        // will only be Some(idx) if it was an input operand
        match tied_operands.get(&(adj_idx, false)) {
            Some(&out_idx) => {
                return out_idx;
            }
            None => {
                // get number of tied inputs before this index
                // TODO: can calculate this once before the call, not once for each input
                let num_tied_before = tied_operands
                    .keys()
                    .filter(|&&(iidx, is_out)| !is_out && iidx < adj_idx)
                    .count();
                // shift the original index by the number of tied input operands prior to the current one
                return idx - num_tied_before;
            }
        };
    }

    idx
}

/// Rewrite a LLVM inline assembly template string into an asm!-compatible one
/// by translating its references to operands (of the form $0 or $x0) to {0} or
/// {0:y} (and wrapping mem-only references in square brackets).
fn rewrite_asm<F: Fn(&str) -> bool, M: Fn(usize) -> usize>(
    asm: &str,
    att_syntax: bool,
    input_op_mapper: M,
    is_mem_only: F,
    arch: Arch,
) -> TranslationResult<String> {
    let mut out = String::with_capacity(asm.len());

    let mut first = true;
    let mut last_empty = false;

    // Iterate over $-prefixed chunks
    for chunk in asm.split('$') {
        // No modification needed for first chunk
        if first {
            first = false;
            out.push_str(chunk);
            continue;
        }

        // Pass-through $$ as one $
        if last_empty {
            last_empty = false;
            out.push('$');
            out.push_str(chunk);
            continue;
        }

        // Note empty chunks
        if chunk.is_empty() {
            last_empty = true;
            continue;
        }

        // Do not re-wrap ${...}, but do translate modifiers
        if chunk.starts_with('{') {
            // Translate operand modifiers ("template modifiers" per Rust)
            if let Some(end_idx) = chunk.find('}') {
                let ref_str = &chunk[..end_idx];
                if let Some(colon_idx) = ref_str.find(':') {
                    let (before_mods, _modifiers) = ref_str.split_at(colon_idx + 1);
                    out.push('{');
                    let idx: usize = before_mods
                        .trim_matches(|c: char| !c.is_ascii_digit())
                        .parse()
                        .map_err(|_| TranslationError::generic("could not parse operand idx"))?;
                    out.push_str(input_op_mapper(idx).to_string().as_str());
                    out.push(':');
                    let modifiers = ref_str[colon_idx + 1..].chars();
                    for modifier in modifiers {
                        if let Some(new) = translate_modifier(modifier, arch) {
                            out.push(new);
                        }
                    }
                    out.push_str(&chunk[end_idx..]);
                }
            } else {
                out.push_str(chunk);
            }
            continue;
        }

        // Translate references of the form %k0 or %3, which look like $k0
        // or $3 in LLVM asm.
        if chunk.starts_with(|c: char| c.is_ascii_alphanumeric()) {
            // Find the end of the reference itself (after 'k0' or '3').
            let end_idx = chunk
                .find(|c: char| c == ',' || !c.is_ascii_alphanumeric())
                .unwrap_or(chunk.len());
            let ref_str = &chunk[..end_idx];

            let index_str;
            let mut new_modifiers = String::new();
            // If the ref string starts with a letter, it's a modifier to translate.
            if let Some(true) = ref_str.chars().next().map(|c| c.is_ascii_alphabetic()) {
                let (modifiers, index) = ref_str.split_at(1);

                index_str = index;

                for modifier in modifiers.chars() {
                    if let Some(new) = translate_modifier(modifier, arch) {
                        new_modifiers.push(new);
                    }
                }
            } else {
                // Just digits
                index_str = ref_str;
            }
            let mem_only = is_mem_only(index_str);
            // Push the reference wrapped in {}, or in [{}] if mem-only
            if mem_only {
                out.push(if att_syntax { '(' } else { '[' });
            };
            out.push('{');
            let idx: usize = index_str
                .parse()
                .map_err(|_| TranslationError::generic("could not parse operand idx"))?;
            out.push_str(input_op_mapper(idx).to_string().as_str());
            if !new_modifiers.is_empty() {
                out.push(':');
                out.push_str(&new_modifiers);
            }
            out.push('}');
            if mem_only {
                out.push(if att_syntax { ')' } else { ']' });
            };
            // Push the rest of the chunk
            out.push_str(&chunk[end_idx..]);
            continue;
        }

        // We failed to parse this operand reference
        out.push_str(chunk);
    }
    Ok(out)
}

impl<'c> Translation<'c> {
    /// Convert an inline-assembly statement into one or more Rust statements.
    /// If inline assembly translation is not enabled this will result in an
    /// error message instead of a conversion. Because the inline assembly syntax
    /// used in C is different than the one used in Rust (Rust uses the LLVM syntax
    /// directly) the resulting translated assembly statements will be unlikely to work
    /// without further manual translation. The translator will properly translate
    /// the arguments to the assembly statement, however.
    pub fn convert_asm(
        &self,
        ctx: ExprContext,
        span: Span,
        is_volatile: bool,
        asm: &str,
        inputs: &[AsmOperand],
        outputs: &[AsmOperand],
        clobbers: &[String],
    ) -> TranslationResult<Vec<Stmt>> {
        if !self.tcfg.translate_asm {
            return Err(TranslationError::generic(
                "Inline assembly translation not enabled.",
            ));
        }

        let arch = match parse_arch(&self.ast_context.target) {
            Some(arch) => arch,
            None => {
                return Err(TranslationError::generic(
                    "Cannot translate inline assembly for unfamiliar architecture",
                ))
            }
        };

        self.use_feature("asm");

        fn push_expr(tokens: &mut Vec<TokenTree>, expr: Box<Expr>) {
            tokens.extend(expr.to_token_stream());
        }

        let mut stmts: Vec<Stmt> = vec![];
        let mut post_stmts: Vec<Stmt> = vec![];
        let mut tokens: Vec<TokenTree> = vec![];

        // Identify tied operands
        let mut tied_operands = HashMap::new();
        for (input_idx, AsmOperand { constraints, .. }) in inputs.iter().enumerate() {
            let constraints_digits = constraints.trim_matches(|c: char| !c.is_ascii_digit());
            if let Ok(output_idx) = constraints_digits.parse::<usize>() {
                let output_key = (output_idx, true);
                // Positional references in template count across outputs then inputs.
                // Add output count so that our index is into the full sequence and can be used for
                // positional template substitutions.
                let input_key = (input_idx + outputs.len(), false);
                tied_operands.insert(output_key, input_idx);
                tied_operands.insert(input_key, output_idx);
            }
        }

        let operand_is_mem_only = |operand: &AsmOperand| -> bool {
            if let Ok((_dir_spec, mem_only, _parsed)) =
                parse_constraints(&operand.constraints, arch)
            {
                mem_only
            } else {
                println!("could not parse asm constraints: {}", operand.constraints);
                false
            }
        };

        // Detect and pair inputs/outputs that constrain themselves to the same register
        let mut inputs_by_register = HashMap::new();
        let mut other_inputs = Vec::new();
        for (i, input) in inputs.iter().enumerate() {
            let combined_idx = i + outputs.len();
            let (_dir_spec, _mem_only, parsed) = parse_constraints(&input.constraints, arch)?;
            // Only pair operands with an explicit register or index
            if is_regname_or_int(&parsed) {
                inputs_by_register.insert(parsed, (combined_idx, input.clone()));
            } else {
                other_inputs.push((parsed, (combined_idx, input.clone())));
            }
        }

        // Convert gcc asm arguments (input and output lists) into a single list
        // of operands with explicit arg dir specs (asm!-style)

        // The unified arg list
        let mut args = Vec::new();

        // Add outputs as inout if a matching input is found, else as outputs
        for (output_idx, output) in outputs.iter().enumerate() {
            match parse_constraints(&output.constraints, arch) {
                Ok((mut dir_spec, mem_only, parsed)) => {
                    // Add to args list; if a matching in_expr is found, this is
                    // an inout and we remove the output from the outputs list
                    let mut in_expr = inputs_by_register.remove(&parsed);
                    if in_expr.is_none() {
                        // Also check for by-index references to this output
                        in_expr = inputs_by_register.remove(&output_idx.to_string());
                    }
                    // Extract expression
                    let in_expr = in_expr.map(|(i, operand)| (i, operand.expression));

                    // For inouts, change the dirspec to include 'in'
                    if in_expr.is_some() {
                        dir_spec = dir_spec.with_in();
                    }
                    args.push(BidirAsmOperand {
                        dir_spec,
                        mem_only,
                        name: None,
                        constraints: parsed,
                        in_expr,
                        out_expr: Some((output_idx, output.expression)),
                    });
                }
                // Constraint could not be parsed, drop it
                Err(e) => eprintln!("{}", e),
            }
        }
        // Add unmatched inputs
        for (_, (input_idx, input)) in inputs_by_register
            .into_iter()
            .chain(other_inputs.into_iter())
        {
            let (dir_spec, mem_only, parsed) = match parse_constraints(&input.constraints, arch) {
                Ok(x) => x,
                Err(e) => {
                    eprintln!("{}", e);
                    continue;
                }
            };
            args.push(BidirAsmOperand {
                dir_spec,
                mem_only,
                name: None,
                constraints: parsed,
                in_expr: Some((input_idx, input.expression)),
                out_expr: None,
            });
        }

        // Determine whether the assembly is in AT&T syntax
        let att_syntax = match arch {
            Arch::X86 | Arch::X86_64 => asm_is_att_syntax(asm),
            _ => false,
        };

        // Sort positional args before named ones.
        args.sort_by_key(|arg| !arg.is_positional());

        // Add workaround for reserved registers (e.g. rbx on x86_64)
        let (prolog, epilog) = rewrite_reserved_reg_operands(att_syntax, arch, &mut args);

        // Find new idx by searching args for one with this original idx
        let new_idx_for_orig = |orig_idx| {
            args.iter()
                .position(|operand| operand.has_orig_idx(orig_idx))
                .unwrap_or_else(|| panic!("no operand had index {orig_idx} in asm str:\n{asm}"))
        };

        // Rewrite arg references in assembly template
        let rewritten_asm = rewrite_asm(
            asm,
            att_syntax,
            |idx: usize| {
                new_idx_for_orig(tied_output_operand_idx(idx, outputs.len(), &tied_operands))
            },
            |ref_str: &str| {
                if let Ok(idx) = ref_str.parse::<usize>() {
                    outputs
                        .iter()
                        .chain(inputs.iter())
                        .nth(idx)
                        .map(operand_is_mem_only)
                        .unwrap_or(false)
                } else {
                    false
                }
            },
            arch,
        )?;

        let rewritten_asm = prolog + &rewritten_asm + &epilog;

        // Emit assembly template
        for line in rewritten_asm.split('\n') {
            push_expr(&mut tokens, mk().lit_expr(line.to_string() + "\n"));
            tokens.push(TokenTree::Punct(Punct::new(',', Alone)));
        }
        tokens.pop();

        // Outputs and Inputs
        let mut operand_renames = HashMap::new();
        for operand in args {
            tokens.push(TokenTree::Punct(Punct::new(',', Alone)));

            // First, convert output expr if present
            let out_expr = if let Some((output_idx, out_expr)) = operand.out_expr {
                let mut out_expr = self.convert_expr(ctx.used(), out_expr, None)?;
                stmts.append(out_expr.stmts_mut());
                let mut out_expr = out_expr.into_value();

                if operand.mem_only {
                    // If the constraint string contains `*`, then
                    // c2rust-ast-exporter added it (there's no gcc equivalent);
                    // in this case, we need to do what clang does and pass in
                    // the operand by-address instead of by-value
                    out_expr = mk().mutbl().borrow_expr(out_expr);
                }

                if let Some(_tied_operand) = tied_operands.get(&(output_idx, true)) {
                    // If we have an input operand tied to an output operand,
                    // we need to replicate clang's behavior: the inline assembly
                    // uses the larger type internally, and the smaller value gets
                    // extended to the larger one before the call, and truncated
                    // back after (if needed). For portability, we moved the
                    // type conversions into the `c2rust-asm-casts` crate,
                    // so we call into that one from here.

                    // Convert `x` into `let freshN = &mut x; *x`
                    let output_name = self.renamer.borrow_mut().fresh();
                    let output_local = mk().local(
                        mk().ident_pat(&output_name),
                        None,
                        Some(mk().mutbl().borrow_expr(out_expr)),
                    );
                    stmts.push(mk().local_stmt(Box::new(output_local)));

                    // `let mut freshN;`
                    let inner_name = self.renamer.borrow_mut().fresh();
                    let inner_local = mk().local(mk().ident_pat(&inner_name), None, None);
                    stmts.push(mk().local_stmt(Box::new(inner_local)));

                    out_expr = mk().ident_expr(&inner_name);
                    operand_renames.insert(output_idx, (output_name, inner_name));
                }
                Some(out_expr)
            } else {
                None
            };

            // Then, handle input expr if present
            let in_expr = if let Some((input_idx, in_expr)) = operand.in_expr {
                let mut in_expr = self.convert_expr(ctx.used(), in_expr, None)?;
                stmts.append(in_expr.stmts_mut());
                let mut in_expr = in_expr.into_value();

                if operand.mem_only {
                    in_expr = mk().borrow_expr(in_expr);
                }
                if let Some(tied_operand) = tied_operands.get(&(input_idx, false)) {
                    self.use_crate(ExternCrate::C2RustAsmCasts);

                    // Import the trait into scope
                    self.with_cur_file_item_store(|item_store| {
                        item_store.add_use(true, vec!["c2rust_asm_casts".into()], "AsmCastTrait");
                    });

                    let (output_name, inner_name) = operand_renames.get(tied_operand).unwrap();

                    let input_name = self.renamer.borrow_mut().fresh();
                    let input_local = mk().local(mk().ident_pat(&input_name), None, Some(in_expr));
                    stmts.push(mk().local_stmt(Box::new(input_local)));

                    // Replace `in_expr` with
                    // `c2rust_asm_casts::AsmCast::cast_in(output, input)`
                    let path_expr = mk().path_expr(vec!["c2rust_asm_casts", "AsmCast", "cast_in"]);
                    let output = mk().ident_expr(output_name);
                    let input = mk().ident_expr(input_name);
                    in_expr = mk().call_expr(path_expr, vec![output.clone(), input.clone()]);

                    // Append the cast-out call after the assembly macro:
                    // `c2rust_asm_casts::AsmCast::cast_out(output, input, inner);`
                    let path_expr = mk().path_expr(vec!["c2rust_asm_casts", "AsmCast", "cast_out"]);
                    let inner = mk().ident_expr(inner_name);
                    let cast_out = mk().call_expr(path_expr, vec![output, input, inner]);
                    post_stmts.push(mk().semi_stmt(cast_out));
                }
                Some(in_expr)
            } else {
                None
            };

            // Emit "name =" if a name is given
            if let Some(name) = operand.name {
                push_expr(&mut tokens, mk().ident_expr(name));
                tokens.push(TokenTree::Punct(Punct::new('=', Alone)));
            }

            // Emit dir_spec(constraint), quoting constraint if needed
            push_expr(&mut tokens, mk().ident_expr(operand.dir_spec.to_string()));
            let constraints_ident = if is_regname_or_int(&operand.constraints) {
                mk().lit_expr(operand.constraints.trim_matches('"'))
            } else {
                mk().ident_expr(operand.constraints)
            };

            // Emit input and/or output expressions, separated by "=>" if both
            push_expr(&mut tokens, mk().paren_expr(constraints_ident));
            if let Some(in_expr) = in_expr {
                let in_expr_span = in_expr.span();
                push_expr(&mut tokens, in_expr);
                if out_expr.is_some() {
                    tokens.push(TokenTree::Punct(Punct::new('=', Joint)));
                    tokens.push(TokenTree::Punct(Punct::new('>', Alone)));
                } else {
                    // If inout but no out expr was given, mark clobbered ('_')
                    if let ArgDirSpec::InOut | ArgDirSpec::InLateOut = operand.dir_spec {
                        tokens.push(TokenTree::Punct(Punct::new('=', Joint)));
                        tokens.push(TokenTree::Punct(Punct::new('>', Alone)));

                        tokens.push(TokenTree::Ident(Ident::new("_", in_expr_span)));
                    }
                }
            }
            if let Some(out_expr) = out_expr {
                push_expr(&mut tokens, out_expr);
            }
        }

        let mut preserves_flags = true;
        let mut read_only = true;

        // Clobbers
        for clobber in clobbers {
            // Process and drop non-register clobbers
            if clobber == "cc" {
                preserves_flags = false;
                continue;
            };
            if clobber == "memory" {
                read_only = false;
                continue;
            };

            // We must drop clobbers of reserved registers, even though this
            // really means we're misinforming the compiler of what's been
            // overwritten. Warn verbosely.
            let quoted = format!("\"{}\"", clobber);
            if reg_is_reserved(&quoted, arch).is_some() {
                warn!(
                    "Attempting to clobber reserved register ({}), dropping clobber! \
                This likely means the potential for miscompilation has been introduced. \
                Please rewrite this assembly to save/restore the value of this register \
                if at all possible.",
                    clobber
                );
                continue;
            }

            tokens.push(TokenTree::Punct(Punct::new(',', Alone)));
            let result = mk().call_expr(mk().ident_expr("out"), vec![mk().lit_expr(clobber)]);
            push_expr(&mut tokens, result);
            push_expr(&mut tokens, mk().ident_expr("_"));
        }

        // Options
        {
            let mut options = vec![];
            if preserves_flags {
                options.push(mk().ident_expr("preserves_flags"));
            }
            if !is_volatile {
                // Pure cannot be applied if we have no outputs
                if read_only && (outputs.len() + clobbers.len()) > 0 {
                    options.push(mk().ident_expr("pure"));
                    options.push(mk().ident_expr("readonly"));
                }
                // We never emit [pure, nomem] right now, but it would be nice
            }

            if att_syntax {
                options.push(mk().ident_expr("att_syntax"));
            }

            if !options.is_empty() {
                tokens.push(TokenTree::Punct(Punct::new(',', Alone)));
                let result = mk().call_expr(mk().ident_expr("options"), options);
                push_expr(&mut tokens, result);
            }
        }

        self.with_cur_file_item_store(|item_store| {
            item_store.add_use(true, vec!["core".into(), "arch".into()], "asm");
        });

        let mac = mk().mac(
            mk().path(vec!["asm"]),
            tokens.into_iter().collect::<TokenStream>(),
            MacroDelimiter::Paren(Default::default()),
        );
        let mac = mk().mac_expr(mac);
        let mac = mk().span(span).semi_stmt(mac);
        stmts.push(mac);

        // Push the post-macro statements
        stmts.extend(post_stmts);

        Ok(stmts)
    }
}