c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

assembly translation ignores clang asm variants

Open oinoom opened this issue 3 years ago • 0 comments

the following C code:

int mul2_3(int var64)
{
    int out;
    int dummy = 2;
    asm("add {%0, %2 | %2 %0}\n\t"
        "add %1, %1"
        : "=r"(out)
        : "r"(dummy), "0"(var64));
    return out;
}

will have the following LLVM assembly string: "add $($0, $2 $| $2 $0$)\n\tadd $1, $1"

which gets translated into the following Rust

#[no_mangle]
pub unsafe extern "C" fn rust_mul2_3(mut var64: libc::c_int) -> libc::c_int {
    let mut out: libc::c_int = 0;
    let mut dummy: libc::c_int = 2 as libc::c_int;
    let fresh9 = &mut out;
    let fresh10;
    let fresh11 = var64;
    asm!(
        "add ({0}, {0} | {0} {0})\n\tadd {1}, {1}", inlateout(reg)
        c2rust_asm_casts::AsmCast::cast_in(fresh9, fresh11) => fresh10, inlateout(reg)
        dummy => _, options(preserves_flags, pure, readonly, att_syntax)
    );
    c2rust_asm_casts::AsmCast::cast_out(fresh9, fresh11, fresh10);
    return out;
}

In these cases it will be necessary to parse and identify which variant is which at some level of confidence, and choose an option to output.

oinoom avatar May 13 '22 22:05 oinoom