Skip to content

C Compiler generates wrong code for while loop #1439

Closed
@gopherbot

Description

@gopherbot

by [email protected]:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?
1. 6c -S this.c
--SNIP--
typedef unsigned long long uint64;
typedef char int8;
static void
puthex(uint64 n, uint64 l, int8* t) {
        static int8 hexdigits[] = "0123456789ABCDEF";
        while(l--) {
                *--t = hexdigits[n & 0xF];
                n >>= 4;
        }
}
--SNAP--
2. inspect output code
3. notice that an infinite loop (no conditional jump!) is generated

What is the expected output?
A conditional jump.

What do you see instead?
An infinite loop.

Which compiler are you using (5g, 6g, 8g, gccgo)?
6c

Which operating system are you using?
Linux toshiba 2.6.34-12-default #1 SMP 2010-06-29 02:39:08 +0200 i686 i686 i386 GNU/Linux


Which revision are you using?  (hg identify)
09ab865d875c+

Please provide any additional information below.
This code can be used to actually run it.
--SNIP--
void main·main(int argc, char** argv) {
        int8 buf[3] = {0, 0, 0};
        runtime·prints("Hello, World\n");
        puthex(0x42, 2, buf+2);
        runtime·prints(buf);
}

void main·init() {
}
--SNAP--
No surprise, it crashes.
A similar program works fine with GCC (ugh feels bad to say that).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions