[Issue 1150] New: Compiler creates wrong code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 15 13:43:19 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1150

           Summary: Compiler creates wrong code
           Product: D
           Version: 1.012
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: niqbmozgvx at mailinator.com


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


-- 



More information about the Digitalmars-d-bugs mailing list