druntime: Implement va_arg for WebAssembly#5111
druntime: Implement va_arg for WebAssembly#5111IDONTUSEGH wants to merge 1 commit intoldc-developers:masterfrom
Conversation
| { | ||
| static if (__traits(isScalar, T) || is(T == U*, U)) | ||
| { | ||
| return ldc_va_arg!T(ap); |
There was a problem hiding this comment.
We don't use this LLVM intrinsic anywhere else, it's basically useless and only works for trivial types (if at all), where the implementation is trivial anyway (your else branch most likely).
After glancing at https://github.com/ldc-developers/ldc/blob/master/gen/abi/wasm.cpp and the last paragraph in https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md#function-arguments-and-return-values, I think the else branch might really suffice.
There was a problem hiding this comment.
So if the else branch suffices, I'd say incorporate it in the generic va_arg template below, as for the other archs.
| { | ||
| ap = ap.alignUp!(T.alignof); | ||
| auto p = cast(T*) ap; | ||
| ap += T.sizeof.alignUp; |
There was a problem hiding this comment.
I don't think the size needs to be aligned up here; this will be taken care of by the alignUp!(T.alignof) for the next argument. If I understand the spec paragraph correctly:
Arguments are arranged in order in the buffer, with each argument placed at the lowest appropriately aligned address after the previous argument.
It uses 'pragma(LDC_va_arg)' to emit the LLVM instruction for scalars and pointers (matches how it's implemented in tocall.cpp), else it falls back to manual pointer arithmetic.
I'm not 100% sure if that's correct.