> In GCC, inline assembly has insane syntax. Probably on purpose, to discourage writing it.
No, it's primarily for portability and flexibility. Other languages have "inline assembly" that is really a separate 8086 assembler built into the C compiler. And that's good for some things (accessing values in the surrounding C code via natural expressions) and bad for others.
In particular, a gcc asm() block can do anything the underlying assembler can, and the gcc "constraint" syntax is designed to express the idea of "what rules must the compiler follow to generate code around this asm block".
Want to expand your asm block as a big string using preprocessor macrobatics? It's just a string, you can do that.
Want to write a macro that expands into a runtime constant that you want to link into a special section? No problem.
Want to call a function in a special ABI that is going to clobber a specific set of registers and need to tell the compiler about it? The gcc syntax has a constraint for that.
Basically, "inline assembly" in DOS tools was... fine. It handled the case of "1980's Compilers Suck and I Want to Hand-Optimize This Code". And that's fine. But that's not why people write assembly in the modern world, and gcc has a much broader reach.
Being able to use macro expansion, stringification, concatenation, etc. in GCC inline asm is a major advantage over how, e.g. MSVC handled inline asm. Getting the constraints right is the hard part, the syntax isn’t much of an issue beyond the initial learning curve imo.
I never got the point of that weird syntax, I would rather reach out to a raw Assembly instead of dealing with such syntax.
To add to your list, most C and C++ compilers on the PC world as well.
Certain folks will mention it is because the compiler needs register usage info, yet PC compilers could and can, infer the usage as well, when parsing those asm blocks, or how intrisics are used (as alternative).
> > In GCC, inline assembly has insane syntax. Probably on purpose, to discourage writing it.
> Certain folks will mention it is because the compiler needs register usage info, yet PC compilers could and can, infer the usage as well, when parsing those asm blocks,
The compiler can only infer the register usage info for known instructions, but the whole point of inline assembly in GCC is to use instructions the compiler doesn't know about (and many of these have implicit register uses which can't be inferred from just looking at the assembly syntax, you really need to know what the instruction does). And in some cases, the register usage can't be inferred even for known instructions; for instance, the registers used by a "call xyz" instruction depend on how the "xyz" subroutine was implemented (it probably has a nonstandard calling convention, otherwise there would be no reason for using inline assembly to call it).
The "weird syntax" actually matches closely how known instructions are described within GCC itself, which makes sense since GCC inline assembly is all about teaching it about new (or non-standard) machine instructions (or pseudo-instructions), without having to modify and recompile the compiler.
you can even use labels in it etc. for in/ouputs so it's 'easily readable'.
People just write it really lazy, don't use proper formatting / whitespace which makes it really difficult to read _other_peoples_inline_assembly.
I'd wager if you write it nicely with comments etc. it's quite alright. with some macros it might be more beautiful but imho those are harder/more annoying to interpret.
In D language it looks okay too.
In GCC, inline assembly has insane syntax. Probably on purpose, to discourage writing it.