2

It seems that by default Clang assumes GNU assembler syntax.

How to compile with Clang an assembly file using ARM syntax as this (directives) and this (instructions)?

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • You probably can't change the directives. If you want armasm directives you'll need to use ARM's armasm. – Ross Ridge Aug 23 '16 at 16:11
  • @RossRidge , here they say how to select syntax for x86 in Clang: http://stackoverflow.com/questions/10990018/how-to-generate-assembly-code-with-clang-in-intel-syntax . Isn't there something analogical for ARM? Or if you mean refactorring the assembly file to change the directives from ARM to GNU syntax - no, I wouldn't like to do this. – Serge Rogatch Aug 23 '16 at 16:26
  • 1
    That only changes the instruction syntax, not the directives used. If you want to use the armasm directives that you linked in your post you'll probably have to a different assembler, one that supports those directives, like `armasm` itself. – Ross Ridge Aug 23 '16 at 16:38
  • @RossRidge , at least, how to change the instruction syntax to ARM? – Serge Rogatch Aug 23 '16 at 16:49
  • 1
    It probably already is. I don't know of any syntax other than ARM's. However there's two variations of it, old ARM syntax and the newer unified syntax (UAL). I believe clang only supports the later. If you want the old form you also probably have to use a different assembler. – Ross Ridge Aug 23 '16 at 17:02
  • 1
    There is the old and unified, but directives exact details of each are and have always been non portable between arms assemblers and gnu (well gnu assembler always manages to mangle something to make it not portable in particular comments). many of the directives and things like how the mrs/msr instruction formatting is, is/was sensitive either by the arm tool or the gnu tool or both. You really have to target one assembly language. – old_timer Aug 23 '16 at 17:36
  • @RossRidge: The actual GNU assembler claims it supports old and new syntax, with a [`.syntax [unified | divided]` directive](https://sourceware.org/binutils/docs/as/ARM-Directives.html). IDK about Clang's built-in assembler, though. But clang has an option to use an external assembler (e.g. the system's `/usr/bin/as`), so you can just do that if necessary. – Peter Cordes Aug 23 '16 at 19:12
  • You can find a real working example of inline ARM ASM added to an Xcode project at this blog post: http://www.modejong.com/blog/post10_arm_timing_framework – MoDJ Aug 23 '16 at 20:11
  • @MoDJ For a question about armasm syntax, I fail to see how a blog post containing some (out-of-line) assembly _not_ in armasm syntax has anything to do with anything other than possibly spammy self-promotion. – Notlikethat Aug 23 '16 at 21:03
  • Spammy self-promotion? Come on, it is Notlikethat :) This blog post and github repo represent many long hours of work. If you take a look at the ASM involved you will find that it is actually using ARM (Unified Assembler Language). While you could try to use inline ASM statements, my experience has shown that this is not a very good idea as compared to just creating a .s file and then linking to the ASM impl at the C level. Sure, it is rather specific to one platform (iOS), but it is still useful to look at actual working source code. – MoDJ Aug 23 '16 at 21:18

1 Answers1

0

Assembly language is defined by the assembler the program that parses the file and makes machine code from it. As we all know turbo assembler, microsoft assembler, nasm and gnu assembler for the x86 dont conform to the same language. No reason to assume any other assembler conforms to anyone elses assembly language. You should never use links like that in a question, see how to ask questions, but if you want to use ARM syntax you have to use one of the ARM assemblers, which can get pricy. If you want gnu syntax you use gnu and some other you use some other. Likewise the author of a C compiler should or often does desire to conform the input to the C standard which is not something defined by the compiler but by a standards body. The output of that compiler doesnt have to conform to any standard other than the one created by that compiler, they can choose machine code in some object format or they can as they often do choose some assembly language in whatever assembler's format they want. No reason to expect them to support multiple assemblers. So then you obviously combine tools into a chain, a compiler chains to an assembler and those outputs chain into the linker. If a particular llvm tool chooses to or only supports one assembly language as defined by one assembler there is no reason to expect them to support some other. if they support others well maybe you can just use the right command line option. The llvm tools as are the gnu are open source, you are welcome at any time to change whatever you want to suit your needs.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • 2
    Short answer if you dont want to use the assembler targeted by llvm then either use the targeted assembler, or modify llvm to target your assembler, or write a tool to port the output, or dont use that compiler. Llvm can now generate objects from llc rather than assembler so you can avoid the assembly step if that helps. – old_timer Aug 23 '16 at 17:39