4 ms·
This is amazing to me that you can do this in Zig code directly as opposed to messing with the compiler.
by hawk_ 1y ago
This is amazing to me that you can do this in Zig code directly as opposed to messing with the compiler.
- messe 1y agoTo be fair, this can be done in GNU C as well. Like the Zig implementation, you'd still have to use inline assembly.
- hawk_ 1y ago> If this was done the classical C way, you would always have to stack-save a number of registers I see, so you're saying that GCC can be coaxed into gathering only the relevant registers to stack and unstack not blindly do all of them?
- messe 11mo agoYes, you write inline assembly that saves the frame pointer, stack pointer, and instruction pointer to the stack, and list every other register as a clobber. GCC will know which ones its using at the call-site (assuming the function gets inlined; this is more likely in Zig due to its single unit of compilation model), and save those to the stack. If it doesn't get inlined, it'll be treated as any other C function and only save the ones needed to be preserved by the target ABI.
- lukaslalinsky 11mo agoSee https://github.com/alibaba/PhotonLibOS/blob/2fb4e979a4913e684404e6996970b679c3e4c73f/thread/thread.cpp#L931 https://github.com/alibaba/PhotonLibOS/blob/2fb4e979a4913e68... for GNU C++ example. It's a tiny bit more limited, because of how the compilation works, but the concept is the same.