3 ms·
I always found the Microsoft / Turbo C++ inline assembler syntax clunky, very limiting compared with the GCC version. With the Microsoft syntax you were genera
by jlokier 6y ago
I always found the Microsoft / Turbo C++ inline assembler syntax clunky, very limiting compared with the GCC version.
With the Microsoft syntax you were generally stuck with having to fix specific registers, and hope the surrounding code was ok working around the asm code and its unstated and excessive constraints.
Whereas with the GCC extended assembly syntax, you can tell the compiler that it has a choice of which registers to use, which memory locations are read from and written to, and many other options that the compiler is free to optimise with.
The GCC version is much better for optimisation. It can even optimise away assembly instructions whose output is stored in a variable that happens to be unused by another part of the program, and select between code using registers or code using inline constants or direct memory operands, depending on expressions that are passed to functions. It also works with "new" instructions that the compiler doesn't know about but the assembler does, or even assembler macros and linker directives that the compiler doesn't know about. The Linux kernel has used all this flexibility to great effect since the beginning.
- pjmlp 6y agoAnyone with compiler design experience knows that the same is possible with the PC /Amiga syntax, like DMD does, nothing special about GCC's hieroglyphic syntax.
- jlokier 6y agoI have plenty of compiler design experience and I don't know that. What I have seen is in line with my comment. But I'd be happy to discover something new. Care to show me a real-world, working example of compiler-optimisable assembly inlines with something that isn't using the GCC extension? If by DMD you mean the Digital Mars D compiler (which is called DMD), then: No. I checked the online documentation just now for DMD x86 just to be sure. According to that documentation, its inline assembly doesn't provide any of the flexibility that I listed. You can see that easily by the way DMD requires you to name the exact registers in the inline asm, and the way the operands syntax is fixed as either a register, memory, memory with base and offset etc (the usual Intel things). But DMD doesn't let you say "pick any suitable operand type". With GCC the compiler will select among possible operand types according to what fits best with the surrounding code. This way it avoids unnecessary loads, stores, register transfers etc. when interleaving C with asm, intrinsics with asm, or even asm with other asm from different inline functions. You certainly can't do any of the fancy stuff the Linux kernel relies on, with asm directives like '.pushsection/.popsection', memory fault fixup tables, asm goto, inline lock hot paths and cold paths, etc.
- pjmlp 6y agoIf I want the optimizer to play tricks with inline Assembly I will use C instead. As for the rest, I am more than happy with VC++ inline Assembly and compiler intrinsics (which then VC++ can play register games if that is what you're after), as another example. The widespread of GCC hieroglyphics inside strings, is an unfortunate state of affairs in what concerns inline Assembly.
- jlokier 6y agoOf course, use whatever you prefer. But the techniques I listed, which Linux kernel (and other software) relies on for performance, can't be done in C, can't be done with compiler intrinsics, and can't be done with basic asm in the style of DMD and MSVC either. A feature to add new program-specific intrinsics to the compiler would work, but I don't know of any compiler which offers that, and it would probably look like extended asm anyway. The GCC syntax isn't pretty. But I can't agree that it's inferior unless someone points to a better alternative that achieves similar results. I've written programs that were portable to both GCC and MSVC, using the inline asm syntax of both compilers. The MSVC version produced slower code, because there was no way to produce the asm I wanted using MSVC.
- pjmlp 6y agoIf you cannot write the Assembly you want there is certainly a problem. /s
- jlokier 6y ago:-) Well it turns out I can't, and neither can anyone else if they are looking for the fastest performance, because the fastest assembly depends on the context it is called from. I had this problem going all the way back to the 8-bit machines, which is why I took an interest in compilers and dynamic code generation. To get the best :-)