ldc icon indicating copy to clipboard operation
ldc copied to clipboard

No error when using params in @naked functions

Open MrSmith33 opened this issue 1 year ago • 4 comments
trafficstars

On x86_64: https://godbolt.org/z/dEaaqPh7v

import ldc.attributes;
import ldc.llvmasm;

@naked long spawnLinuxThread1(void* data) {
    return __asm!long("syscall",
        "={eax}, {rax}, {rdi}, {rsi}",
        56, 0x50f00, data);
}

produces:

mov     rsi, qword ptr [rdi]
mov     eax, 56
mov     edi, 331520
syscall
ret

instead of

mov     rsi, rdi
mov     eax, 56
mov     edi, 331520
syscall
ret

Changing void* to ulong causes LLVM ERROR:

Invalid bitcast
  %2 = bitcast i64 %0 to ptr, !dbg !7
LLVM ERROR: Broken module found, compilation aborted!

Same happens on arm64: https://godbolt.org/z/7Eb3Pqabd

MrSmith33 avatar Jun 21 '24 21:06 MrSmith33

This bug is due to using @naked and the resulting IR generation not dealing with that properly. With @naked we load from passed parameter address instead of the usual loading from the temp storage address (where the passed parameter is stored).

Compare the output of this code with/without @naked:

//@naked
size_t foo(void* data) {
    return cast(size_t) data;
}

JohanEngelen avatar Jun 22 '24 08:06 JohanEngelen

I see. Should I expect this to be fixed?

MrSmith33 avatar Jun 22 '24 08:06 MrSmith33

Not sure how much we want/should support with @naked.

JohanEngelen avatar Jun 22 '24 09:06 JohanEngelen

I do consider it a bug. If we don't support this usage / it's buggy, then it's better to emit a compile error for it than to leave it unfixed.

JohanEngelen avatar Jun 22 '24 09:06 JohanEngelen