3 ms·
That's beautiful... Why can't GCC and clang have this? Small note: your code seems to be calling a wrapper function rather than running the syscall instruction
by matheusmoreira 3y ago
That's beautiful... Why can't GCC and clang have this?
Small note: your code seems to be calling a wrapper function rather than running the syscall instruction directly so it could probably be even more succinct. Is this valid D?
asm {
mov RAX, __NR_close;
mov RDI, fd;
syscall;
}
- WalterBright 3y agoSYSCALL is an actual instruction, and it's supported by the D inline assembler. https://www.felixcloutier.com/x86/syscall https://www.felixcloutier.com/x86/syscall > Why can't GCC and clang have this? I don't know. My brain bleeds out my ears every time I have to deal with that.
- dundarious 3y agoIt really is painful.
- pjmlp 3y ago> Why can't GCC and clang have this? Because UNIX culture sucks at inline Assembly. D's approach was widely used across PC, Amiga and Atari ST compilers. Besides D, that is what Delphi, C++ Builder and Visual C++ still use nowadays, for the targets that still support inline Assembly. For 64 bit targets, they use intrinsics, which are still preferable to UNIX's approach.
- mananaysiempre 3y ago> That's beautiful... Why can't GCC and clang have this? There are tricky contingent reasons, like the fact that GCC proper doesn’t try and actually isn’t even able to parse assembly—and it would need to here in order to know that it can’t put fd in RAX or in any location whose address computation includes RAX. (GCC’s codegen in general works by stitching together fragments of assembly as text and feeding them into the system assembler. Which is nice and UNIXy and all, but I’m not sure much is won by making the compiler agnostic to the actual bit patterns of the instructions when these days it is still aware of almost every other facet of the instruction set and quite a few microarchitectural details besides.) But the fact of the matter is that GCC’s approach also permits better optimization, by telling the register allocator exactly which registers are clobbered by the inline assembly snippet and where the computations of the input should aim to place their results. You frequently end up with no MOVs at all, although Clang is better at this part than GCC. (Do not be fooled into thinking that an automatic "register asm" variable is pinned to the specified register for all of its scope—it’s only guaranteed to be placed there at the start of any inline assembly blocks, the register allocator can move it out of the way in between them.) I think the x86 code doesn’t actually need the "register asm" syntax at all, FWIW—you should be able to just add "a"(__NR_close), "d"(fd) to the inputs. GCC on RISC archs doesn’t usually define a separate constraint for each individual register, though, so on those it can be necessary.