Reputation: 3707
I am trying to create a macro that coverts a #define create in the makefile to an autogenerated list of #defines which are starting address to peripheral components. Here is the test code:
#include <stdio.h>
#include <stdint.h>
/* Created by makefile to specify the core/CPU */
#define CPU_ID 5
/* Autogenerated hardware register descriptors */
#define CPU0_PERIPHERAL_I2C_BASE__ADDR (0x40000)
#define CPU1_PERIPHERAL_I2C_BASE__ADDR (0x56000)
#define CPU2_PERIPHERAL_I2C_BASE__ADDR (0x57000)
#define CPU3_PERIPHERAL_I2C_BASE__ADDR (0x61000)
#define SUBCORE0_PERIPHERAL_I2C_BASE__ADDR (0x67000)
#define SUBCORE1_PERIPHERAL_I2C_BASE__ADDR (0x90000)
#define SUBCORE2_PERIPHERAL_I2C_BASE__ADDR (0xA3000)
#define SUBCORE3_PERIPHERAL_I2C_BASE__ADDR (0xE3000)
#define BASE_ADDR_DEF _PERIPHERAL_I2C_BASE__ADDR
/* defines for translating CPU_ID into autogenerated hardware regs */
#define CPU_ID_0 "CPU0"
#define CPU_ID_1 "CPU1"
#define CPU_ID_2 "CPU2"
#define CPU_ID_3 "CPU3"
#define CPU_ID_4 "SUBCORE0"
#define CPU_ID_5 "SUBCORE1"
#define CPU_ID_6 "SUBCORE2"
#define CPU_ID_7 "SUBCORE3"
#define _JOIN(x,y) x ## y
#define _DEF1(cpu_id) _JOIN(CPU_ID_,cpu_id)
#define _DEF2(cpu_id) _JOIN(_DEF1(cpu_id),BASE_ADDR_DEF)
int main(void)
{
printf("%s\n", _DEF1(CPU_ID)); // <- this prints out "SUBCORE0" which is part of the way there
printf("%X\n", _DEF2(CPU_ID)); // <- This will not compile
return 0;
}
The _DEF1 macro sort of works at the output, in this case, will be "SUBCORE1" as I expect. I can't seem to get the macro to translate up one additional level to combine the "SUBCORE1" with "_PERIPHERAL_I2C_BASE__ADDR" to create "SUBCORE1_PERIPHERAL_I2C_BASE__ADDR" then print out the hex value which should be 0x90000. Am I missing another level of macros during the expansion that I am not seeing or can't get correct?
Edit: I know people are commenting about the literal strings and I know this wasn't the end goal, but this was just one of many iterative steps while experimenting. I just needed some sort of example to show what I was trying to do. Thanks.
Upvotes: 1
Views: 73
Reputation: 141235
"SUBCORE3"
to SUBCORE3
. Preprocessor can't remove "
.#define _JOIN2(x, y) x ## y
and then #define _JOIN(x, y) _JOIN2(x, y)
#include <stdio.h>
#include <stdint.h>
/* Created by makefile to specify the core/CPU */
#define CPU_ID 5
/* Autogenerated hardware register descriptors */
#define CPU0_PERIPHERAL_I2C_BASE__ADDR (0x40000)
#define CPU1_PERIPHERAL_I2C_BASE__ADDR (0x56000)
#define CPU2_PERIPHERAL_I2C_BASE__ADDR (0x57000)
#define CPU3_PERIPHERAL_I2C_BASE__ADDR (0x61000)
#define SUBCORE0_PERIPHERAL_I2C_BASE__ADDR (0x67000)
#define SUBCORE1_PERIPHERAL_I2C_BASE__ADDR (0x90000)
#define SUBCORE2_PERIPHERAL_I2C_BASE__ADDR (0xA3000)
#define SUBCORE3_PERIPHERAL_I2C_BASE__ADDR (0xE3000)
#define BASE_ADDR_DEF _PERIPHERAL_I2C_BASE__ADDR
/* defines for translating CPU_ID into autogenerated hardware regs */
#define CPU_ID_0 CPU0
#define CPU_ID_1 CPU1
#define CPU_ID_2 CPU2
#define CPU_ID_3 CPU3
#define CPU_ID_4 SUBCORE0
#define CPU_ID_5 SUBCORE1
#define CPU_ID_6 SUBCORE2
#define CPU_ID_7 SUBCORE3
// usually those are called CONCAT or CONCATX or CONCAT2 or XCONCAT etc.
// so I name them the same here
// XCONCAT is a mnemonic from "eXpand then CONCATenate"
#define CONCAT(x,y) x ## y
#define XCONCAT(x,y) CONCAT(x, y)
#define STRING(x) #x
#define XSTRING(x) STRING(x)
// note that glibc defines __CONCAT __XCONCAT __STRING __XSTRING
// trigger the expansions
#define DEF1(cpu_id) XCONCAT(CPU_ID_, cpu_id)
#define DEF2(cpu_id) XCONCAT(DEF1(cpu_id),BASE_ADDR_DEF)
int main(void)
{
printf("%s\n", XSTRING(DEF1(CPU_ID))); // <- this prints out "SUBCORE0", which is at all not part the way here
printf("%X\n", DEF2(CPU_ID)); // <- This will expand to 0x90000
return 0;
}
Upvotes: 2