Placing data at an address given by a constant arithmetic expression

63 views Asked by At

Context

My team upgraded our project from using Arm Compiler for Embedded 5 to Embedded 6. In our code, we place data at a specific location in RAM like so:

#define RAM_START_ADDR <some constant>
#define OFFSET <some other constant>
...
volatile my_datatype_t __attribute__((at(RAM_START_ADDR + OFFSET))) var = ...;

But for some reason, Embedded 6 no longer supports __attribute__((at(<addr>))), and instead, we need to use __attribute__((section(".ARM.__at_<addr>"))). As you might have noticed, the address we are dealing with is an arithmetic expression, which is not supported by __attribute__(section), which expects a numeric string argument. ARM does provide a workaround, namely

volatile my_datatype_t * const var = (volatile my_datatype_t *) (RAM_START_ADDR + OFFSET);

Except, when this compiles, rather than allocating space for var, it just overwrites whatever data is at RAM_START_ADDR + OFFSET. Haven't asked ARM how to achieve this, but I'm not confident we'll get anything besides the not-working workaround they already provide.

Question

Is there a way to evaluate a constant arithmetic expression at compile-time and format it as a string? If so, then we could still use __attribute__((section)) and just pass it the numeric string.

I've done a lot of research, and there doesn't seem to be a way to define a macro such that the expression is evaluated and the value is accessible as a token or string or anything.

1

There are 1 answers

0
KamilCuk On

You are out of luck. This is a preprocessor, if you want to add anything you have to hard-code all possible combinations. This has to be generated with a script. You can try searching for an existing project like Boost, but I do not know or see anything relevant, BOOST_PP_LIMIT_MAG is max 1024.

// Add two base10 numbers and output them in hex format
#define ADDHEX_1_1()  2
#define ADDHEX_1_2()  3
// generate each possible ADDHEX_*_*
// .... few billion lines later ...:
#define ADDHEX_123_456()  243

#define CONCAT4(a, b, c, d)  a##b##c##d
#define XCONCAT4(a, b, c, d)  CONCAT4(a, b, c, d)

#define STRING(x)  #x
#define XSTRING(x)  STRING(x)

#define RAM_START_ADDR 123
#define OFFSET 456
#define AT(a, b)    XSTRING(XCONCAT4(ADDHEX_, a, _, b)())

#include <stdio.h>
int main() {
    puts(".ARM.__at_" AT(RAM_START_ADDR, OFFSET));
}