How can I get GCC to place C ++ constexpr in ROM?

I am compiling for LPC1114, a small ARM target (actually Cortex). RAM is much more limited than ROM. I am using the latest MCC (CodeBenchLite) GCC compiler (GCC 4.6.3). I have some permanent objects that I would like to have in ROM. As far as I understand, the ffx object in the code below should end in ROM (code), but instead it is placed in DATA.

class flop { public: int x; constexpr flop( int x ) : x(x){} }; extern constexpr flop ffx( 1 ); 

How can I convince the compiler to pre-compute the object and put it in ROM?

or maybe I should ask:

  • Can I somehow expect the g ++ compiler to generate ROMable data for ffx
  • if so my code is correct for this
  • if so, why is this version of g ++ supported (I use 4.6, maybe I need 4.7?)

=========================================

This bugzilla C ++ / 49673 entry seems to indicate that my issue is known, probably fixed in GCC 4.7. Unfortunately, I prefer to use the generated Mentor / CodeSourcery, which is still at level 4.6.3. So I guess I'm stuck with an error. :(

+6
source share
2 answers

Update # 2: test results with gcc 4.7.0

This is really fixed in gcc 4.7.0. Your code produces the following assembler output:

  .file "constexpr.cpp" .globl _ffx .section .rdata,"dr" .align 4 _ffx: .long 1 

what you probably want. Update?

I think for this you will need a specific solution for the platform. See Searching for code and data in memory (Scatterloading) :

Scatterloading allows you to split the application into several separate areas of code and data distributed throughout the address map. The location of these regions may vary between load time and runtime:

  • Download regions contain application code and / or data used by the application during on / off (usually ROM).

Instead of relying solely on gcc, try using armlink , as suggested in the link above.

Update: I saw the gcc error you noted. LLVM / MinGW is also hopeless. However, I played with GCC 4.6, and here I think it might help you:

 struct Data { int i; }; constexpr Data getData() { return Data{1}; } 

If instead of having ctor you have a constexpr function and try to generate assembler output, the output will usually be empty (no .data ). And if you use this to initialize some variable (in the global scope), as shown below:

 const Data foo = getData(); 

You will get the assembly as follows:

  .file "constexpr.cpp" .section .rdata,"dr" .align 4 __ZL3foo: .long 1 

(this is with the command line g++ -std=c++0x -S ). Does this work for you?

+1
source

In my experience, const data is placed in a read-only section, so all data in this section can go into ROM (or be programmed in FLASH).

The GCC linker instruction file (* .ld) can assign addresses for different sections.

Another alternative is to place large data structures (such as fonts) in assembly language. Assembly language can place data in segments more easily than C or C ++ can be.

One of our next tasks is to separate the data from our executable file. This reduces the executable file and reduces the testing time of the executable file.

0
source

Source: https://habr.com/ru/post/917805/


All Articles