Bug 9844 – DMD (-m64) int long initialisation bug

Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2013-03-30T20:01:00Z
Last change time
2015-06-09T05:11:59Z
Assigned to
nobody
Creator
safety0ff.bugz

Attachments

IDFilenameSummaryContent-TypeSize
1206kludge.patchthis kludge makes things work for metext/plain1113

Comments

Comment #0 by safety0ff.bugz — 2013-03-30T20:01:23Z
This program: import std.stdio; void main() { int a = -1; long b = a; long c = -1; writeln("a = ",a); writeln("b = ",b); writeln("c = ",c); assert(a==b&&b==c&&a==c); } Gives the following erroneous output: a = -1 b = -1 c = 4294967295 core.exception.AssertError@buggy(9): Assertion failure Using 64bit output from DMD 2.062 (also DMD git 6202b02f0e from DPaste.) The following compilers/options give the expected output: DMD 2.062 -m32 LDC 2.060 (64 bit on DPaste) GDC 2.060 (64 bit on DPaste)
Comment #1 by safety0ff.bugz — 2013-03-31T01:58:24Z
Also, adding the explicit "L" integer suffix does not help.
Comment #2 by safety0ff.bugz — 2013-03-31T02:30:52Z
When passing the -O (optimize) flag, the issue does not manifest itself.
Comment #3 by john.loughran.colvin — 2013-04-03T12:05:58Z
A simplified test: import std.stdio; void foo() { int a = -1; long b = -1; writeln(a); // -1 writeln(b); // (2^32)-1 } The mistake only happens when a and b are initialised to the same negative value. In light of that and after some perusing of the asm generated, it appears that dmd goes "-1 == -1" and uses the same value for a as for b, except of course that doesn't work because of the different sign bit positions for int and long. e.g. mov eax,0xffffffff //rax is now 0x00000000ffffffff mov DWORD PTR [rbp-0x10],eax mov QWORD PTR [rbp-0x8],rax I don't get any bug with ldc, so it's probably either a recent regression or a backend problem.
Comment #4 by safety0ff.bugz — 2013-04-03T22:10:50Z
(In reply to comment #3) > The mistake only happens when a and b are initialised to the same negative > value. > > In light of that and after some perusing of the asm generated, it appears that > dmd goes "-1 == -1" and uses the same value for a as for b, except of course > that doesn't work because of the different sign bit positions for int and long. Yes, you're right, as demonstrated in this program: import std.stdio; void main() { int a = -2; long b = -1; long c = -2; writeln(a); // -2 writeln(b); // -1 writeln(c); // 2^32-2 } I suppose I should now find a better title for this issue.
Comment #5 by safety0ff.bugz — 2013-04-04T13:54:00Z
Created attachment 1206 this kludge makes things work for me This kludge makes things work for me.
Comment #6 by vlad.z.4096 — 2013-04-04T20:34:29Z
The recent version of LDC (based on DMD v2.062 and LLVM 3.3svn) does not suffer from this issue.
Comment #7 by bugzilla — 2013-04-05T01:15:49Z
Comment #8 by github-bugzilla — 2013-04-07T07:30:02Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/34706176b888442c11d426fa98852a13c3a6b342 fix Issue 9844 - DMD (-m64) int long initialisation bug https://github.com/D-Programming-Language/dmd/commit/2e1547d1bcea7cfae6f9489343bc15e6a193cba6 Merge pull request #1846 from WalterBright/fix9844 fix Issue 9844 - DMD (-m64) int long initialisation bug
Comment #9 by safety0ff.bugz — 2013-10-04T20:48:36Z
*** Issue 9331 has been marked as a duplicate of this issue. ***