jameysharp / corrode

C to Rust translator
GNU General Public License v2.0
2.16k stars 116 forks source link

implement inline assembly #47

Open jameysharp opened 8 years ago

jameysharp commented 8 years ago

The CAsm constructor of CStatement, and the CAssemblyStatement it contains, are almost easy to translate to Rust's asm! macro.

At a high level, both syntaxes deliberately have the same semantics: Rust borrows LLVM's, which exists to support Clang's attempts to exactly match GCC. However, I'm not sure the details actually agree. I'd like someone who has some experience with inline assembly to take this on.

A correct implementation probably starts like this:

interpretStatement stmt@(CAsm (CAsmStmt qual expr outops inops clobbers node) _) = do
    volatile <- case qual of
        Just (CVolatQual _) -> True
        Nothing -> False
        _ -> badSource stmt "qualifier on inline assembly"

If volatile, then the "volatile" option should be added as the last parameter to the asm! macro.

The template expression can be extracted using something like the code that would run for interpretExpr True (CConst (CStrConst expr node)) (try factoring out the code that handles CStringLiteral expressions) except that the asm! macro probably doesn't expect byte strings, so it should be expanded as a Unicode string instead. Similar string extraction is needed inside the operands and clobbers.

I'm stuck trying to figure out how to map operands and clobbers. Some cases will probably work if they're copied through unchanged, but maybe others are more complicated?

jameysharp commented 8 years ago

@jdub has expressed interest in working on this.

And, here's a nice blog post from someone else who was trying to work out how Rust inline assembly compares to GCC: http://embed.rs/articles/2016/arm-inline-assembly-rust/