[forwarded from http://bugs.debian.org/506713]

seen with 4.1, 4.2, 4.3 branches, not on the trunk

Functions compiled for SPARC with both -O2 and -fPIC options, which
return structures and require global data, may have incorrect code
generated for them.

The following example is based on Qt 3, which is affected by this bug
(see #490999).  The code should of course print "1 + 1 = 2" but when
compiled for SPARC with -O2 -fPIC it prints "1 + 1 = 0" (or may crash).
The first two instructions generated for QTime::addMSecs() are a save
using %sp and a load relative to %sp which depends on the *old* value of
%sp.

This example appears to be compiled correctly if I remove either option
or reduce the value of MSECS_PER_DAY such that it can be an immediate
constant.

Ben.

#include <iostream>

class QTime
{
public:
    explicit QTime(int ms = 0) : ds(ms) {}
    static QTime currentTime() { return QTime(); }
    QTime addMSecs(int ms) const;
    int msecs() const { return ds; }
private:
    unsigned ds;
};

static const unsigned MSECS_PER_DAY = 86400000;

QTime QTime::addMSecs( int ms ) const
{
    QTime t;
    if ( ms < 0 ) {
        // % not well-defined for -ve, but / is.
        int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY;
        t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY)
                % MSECS_PER_DAY;
    } else {
        t.ds = ((int)ds + ms) % MSECS_PER_DAY;
    }
    return t;
}

int main(int argc, char* argv[] )
{
    std::cout << "1 + 1 = " << QTime(1).addMSecs(1).msecs() << std::endl;
}


-- 
           Summary: wrong-code with -O2 -fPIC on sparc-linux-gnu
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: debian-gcc at lists dot debian dot org
GCC target triplet: sparc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38287

Reply via email to