Bug 1150 – Compiler creates wrong code

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2007-04-15T15:43:00Z
Last change time
2014-02-16T15:23:43Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
niqbmozgvx

Comments

Comment #0 by niqbmozgvx — 2007-04-15T15:43:18Z
The compiler creates wrong assembly code for the following class if _not_ compiled with optimize argument "-O" (example "dmd bug.d -g"). --------- bug.d ------------- class RangeCoder { uint[258] cumCount; // 256 + end + total uint lower; uint upper; ulong range; this() { for (int i=0; i<cumCount.length; i++) cumCount[i] = i; lower = 0; upper = 0xffffffff; range = 0x100000000; } void encode(uint symbol) { uint total = cumCount[length - 1]; // "Error: Access Violation" in following line upper = lower + cast(uint)((cumCount[symbol+1] * range) / total) - 1; lower = lower + cast(uint)((cumCount[symbol] * range) / total); } } int main(char[][] args) { RangeCoder rc = new RangeCoder(); rc.encode(77); return 0; } ------- bug.d ------- Assembler code created by the compiler: ------- asm --------- image00400000!D3bug10RangeCoder6encodeMFkZv: 00402084 c8280000 enter 28h,0 [...] 004020e8 8b4de0 mov ecx,dword ptr [ebp-20h] 004020eb 8b34b1 mov esi,dword ptr [ecx+esi*4] 004020ee 89f0 mov eax,esi // eax = cumCount[symbol+1] 004020f0 8b55f4 mov edx,dword ptr [ebp-0Ch] 004020f3 8db218040000 lea esi,[edx+418h] // esi -> range 004020f9 f76604 mul eax,dword ptr [esi+4] // edx:eax = eax * Hi(range) 004020fc 8975e8 mov dword ptr [ebp-18h],esi 004020ff 96 xchg eax,esi // <-- esi is wrong here! 00402100 f726 mul eax,dword ptr [esi] ds:0023:0000004e=???????? // should be: edx:eax = eax * Lo(range) 00402102 03d6 add edx,esi // add carry from 1. mul ------- asm -------- The consequence is a Access Violation @402100. LLAP, Sascha
Comment #1 by bugzilla — 2007-04-20T13:21:38Z
Fixed DMD 1.013
Comment #2 by thomas-dloop — 2007-04-23T12:55:34Z