avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Re: C++ and aggregates in flash memory


From: Ned Konz
Subject: [avr-gcc-list] Re: C++ and aggregates in flash memory
Date: Mon, 15 May 2006 11:13:10 -0700
User-agent: Thunderbird 1.5.0.2 (Macintosh/20060308)


I found that the problem was that I had overly complex initializers. I was counting on the compiler to optimize them out (as it did in the rest of the code), but it didn't. For instance:



// this compiles OK under avr-gcc 4.1.0 with -Os or -O2, but...
// I get a __static_initialization_and_destruction_0(int,int) routine
// generated that clobbers RAM memory.
//
// An excerpt from that function (note that someYY is, or should be, an
// address in ROM):
//
//    ldi r24,lo8(3)
//    ldi r25,hi8(3)
//    sts (someYY)+1,r25
//    sts someYY,r24
//
//    ; and then later someYY isn't properly initialized in ROM:
//
//    .global   someYY
//      .section        .progmem.data,"aw",@progbits
//    someYY:
//              .skip 2,0

#include <avr/pgmspace.h>

struct XX
{
    static const unsigned const0 = 2U;
    static unsigned const1() { return 3U; }
    unsigned constm1() const { return 4U; }
};

struct YY
{
    unsigned u;
};

const XX PROGMEM someXX = {};

// this causes __static_initialization_and_destruction_0()
// to be generated:
extern const YY PROGMEM someYY;
const YY PROGMEM someYY = { XX::const1() };

// if I add this declaration I get a "section type conflict"
// for someOtherYY:
//
// extern const YY PROGMEM someOtherYY;

// This doesn't cause __static_initialization_and_destruction_0()
// to be generated:
const YY PROGMEM someOtherYY = { XX::const0 };

volatile unsigned u1;
volatile unsigned u2;
volatile unsigned u3;

int main()
{
    u1 = someXX.constm1();
    u2 = XX::const0;
    u3 = XX::const1();
}




Thanks,
--
Ned Konz
address@hidden
http://bike-nomad.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]