3

I was looking enviously at the ability to put inline assembler in code compiled by GCC, and I'm wondering if you could do something similar with Clang? For example is there some way I could complete the definition of a function with LLVM assembler:

int add_two_ints(int a, int b) {
  /* 
   * some bitcode stuff goes here to add
   * the ints and return the result
   */
}

Any references, or code to complete the example above would be great.

brooks94
  • 3,836
  • 4
  • 30
  • 57
  • Do you want inline assembler for a physical CPU, or "inline LLVM IR"? Inline assembler means the former, but your code snippet indicates the latter. –  Mar 13 '13 at 14:48
  • It appears that I have my terminology wrong. Fixing the question. – brooks94 Mar 13 '13 at 15:31
  • 1
    clang supports inline *assembly* as well, BTW. As for inlining actual LLVM IR, I agree with Oak that it's a duplicate question – Eli Bendersky Mar 13 '13 at 19:14

1 Answers1

3

clang supports inline assembly, including GCC's extension where you declare input, output, and clobbered registers:

int add_two_ints(int a, int b) {
   int result;
   asm( "addl %1, %2;"
        "movl %2, %0;"
        : "=r"(result)
        : "r"(a), "r"(b)
        :);
   return result;
}

Clang also has experimental support for Microsoft's __asm { } syntax and intel style assembly.

It does not have any support for including LLVM-IR in C or C++ source. Such a feature would largely be just a novelty as inline assembly is typically for accessing special instructions and LLVM-IR doesn't enable that.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • 6
    At least, LLVM-IR gives an access to the intrinsics, explicit alignment annotations and metadata (and none of these is available from C), so such a functionality would be useful. – SK-logic Mar 20 '13 at 17:26