[Issue 22960] importC: K&R-style functions assume variadic calling convention

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 30 15:05:46 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=22960

--- Comment #4 from duser at neet.fi ---
i thought this might be hard to explain

the issue is that all K&R functions have this in the body:

% dmd -c -vasm file2.c
other:
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   48 81 EC D0 00 00 00     sub       RSP,0D0h
000b:   48 89 B5 38 FF FF FF     mov       -0C8h[RBP],RSI
0012:   48 89 95 40 FF FF FF     mov       -0C0h[RBP],RDX
0019:   48 89 8D 48 FF FF FF     mov       -0B8h[RBP],RCX
0020:   4C 89 85 50 FF FF FF     mov       -0B0h[RBP],R8
0027:   4C 89 8D 58 FF FF FF     mov       -0A8h[RBP],R9
002e:   0F B6 C0                 movzx     EAX,AL
0031:   C1 E0 02                 shl       EAX,2
0034:   4C 8D 1D 2A 00 00 00     lea       R11,[02Ah][RIP]
003b:   49 29 C3                 sub       R11,RAX
003e:   48 8D 45 DF              lea       RAX,-021h[RBP]
0042:   41 FF E3                 jmp       R11D
0045:   0F 29 78 F1              movaps    -0Fh[RAX],XMM7
0049:   0F 29 70 E1              movaps    -01Fh[RAX],XMM6
004d:   0F 29 68 D1              movaps    -02Fh[RAX],XMM5
0051:   0F 29 60 C1              movaps    -03Fh[RAX],XMM4
0055:   0F 29 58 B1              movaps    -04Fh[RAX],XMM3
0059:   0F 29 50 A1              movaps    -05Fh[RAX],XMM2
005d:   0F 29 48 91              movaps    -06Fh[RAX],XMM1
0061:   0F 29 40 81              movaps    -07Fh[RAX],XMM0
0065:   C7 40 01 08 00 00 00     mov       dword ptr 1[RAX],8
006c:   C7 40 05 30 00 00 00     mov       dword ptr 5[RAX],030h
0073:   4C 8D 5D 10              lea       R11,010h[RBP]
0077:   4C 89 58 09              mov       9[RAX],R11
007b:   48 2D AF 00 00 00        sub       EAX,0AFh
0081:   48 89 80 C0 00 00 00     mov       0C0h[RAX],RAX
0088:   C9                       leave
0089:   C3                       ret

the jump there depends on the value of AL and will malfunction if it has a
value other than 0-8

K&R functions are interchangeable with normal ones in gcc and clang, but not in
dmd because of the dependence on AL/RAX being set to a valid value before
calling (as is done when calling a variadic function)

some libraries like zlib (and its bindings in phobos) depend on K&R functions
being callable the same way as normal ones

gcc/clang don't emit the code to save registers in K&R functions, and if
va_start/etc can't be used inside one then there's no reason to

--


More information about the Digitalmars-d-bugs mailing list