Bug 8423 – Wrong code for bool parameter in 5th integer register.
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2012-07-24T02:53:00Z
Last change time
2015-06-09T05:14:40Z
Keywords
wrong-code
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2012-07-24T02:53:06Z
With DMD Git master (04a74bdf) on Linux x86_64, the following snippet asserts/hlts:
---
struct Duration
{
int opCmp(Duration rhs)
{
return 1;
}
}
void enforce(bool value, string a, string b)
{
if (!value) assert(false);
}
void main()
{
auto a = Duration();
auto b = Duration();
enforce(a > b, null, null);
}
---
Here is the relevant assembly:
---
40194d: e8 0e ff ff ff call 401860 <_D4test8Duration5opCmpMFS4test8DurationZi>
401952: 49 89 c0 mov r8,rax
401955: f7 d8 neg eax
401957: 41 c1 e8 1f shr r8d,0x1f
40195b: 31 c0 xor eax,eax
40195d: 31 c9 xor ecx,ecx
40195f: 48 89 c2 mov rdx,rax
401962: 48 89 55 f0 mov QWORD PTR [rbp-0x10],rdx
401966: 48 89 ca mov rdx,rcx
401969: 48 89 c7 mov rdi,rax
40196c: 48 89 d6 mov rsi,rdx
40196f: 48 8b 55 f0 mov rdx,QWORD PTR [rbp-0x10]
401973: e8 38 ff ff ff call 4018b0 <_D4test7enforceFbAyaAyaZv>
---
I haven't checked what causes DMD to emit the »shr« instruction, but it sure kills the opCmp return value in the register. Removing one of the additional parameters, which leads to value being passed in one of {rsi, rdi, rdx, rcx} due to the reverse parameter order in the D calling convention, causes the bug to disappear.
Comment #1 by code — 2012-07-24T03:02:59Z
Hm, seems like the same problem already existed in DMD 2.059 with enforce(bool, int, int, int, int) - what triggers it in 2.060 is the fact that arrays are passed as structs (i.e. in registers) now.
Comment #2 by github-bugzilla — 2012-07-28T00:32:22Z