static initialization pre-processing hacks...

C

Chris Thomasson

Here is a way that you can "automate" static initialization of arrays with
customized data. Here is a quick example:



--------------------
#include <stdio.h>
#include <stddef.h>
#include <assert.h>



/* Macro Table
______________________________________________________________ */
#define CONCAT_X(mp_t1, mp_t2)mp_t1##mp_t2
#define CONCAT(mp_t1, mp_t2)CONCAT_X(mp_t1, mp_t2)

#define PLACE_X(mp_fp, mp_idx, mp_t)mp_fp(mp_idx, mp_t)
#define PLACE_1(mp_fp, mp_t)PLACE_X(mp_fp, 0, mp_t)
#define PLACE_2(mp_fp, mp_t)PLACE_1(mp_fp, mp_t), PLACE_X(mp_fp, 1, mp_t)
#define PLACE_3(mp_fp, mp_t)PLACE_2(mp_fp, mp_t), PLACE_X(mp_fp, 2, mp_t)
#define PLACE_4(mp_fp, mp_t)PLACE_3(mp_fp, mp_t), PLACE_X(mp_fp, 3, mp_t)
#define PLACE_5(mp_fp, mp_t)PLACE_4(mp_fp, mp_t), PLACE_X(mp_fp, 4, mp_t)
#define PLACE_6(mp_fp, mp_t)PLACE_5(mp_fp, mp_t), PLACE_X(mp_fp, 5, mp_t)
#define PLACE_7(mp_fp, mp_t)PLACE_6(mp_fp, mp_t), PLACE_X(mp_fp, 6, mp_t)
#define PLACE_8(mp_fp, mp_t)PLACE_7(mp_fp, mp_t), PLACE_X(mp_fp, 7, mp_t)
#define PLACE_9(mp_fp, mp_t)PLACE_8(mp_fp, mp_t), PLACE_X(mp_fp, 8, mp_t)
#define PLACE_10(mp_fp, mp_t)PLACE_9(mp_fp, mp_t), PLACE_X(mp_fp, 9, mp_t)
#define PLACE(mp_fp, mp_t, mp_d)CONCAT(PLACE_, mp_d)(mp_fp, mp_t)

#define RPLACE_X(mp_fp, mp_idx, mp_t)mp_fp(mp_idx, mp_t)
#define RPLACE_1(mp_fp, mp_t)RPLACE_X(mp_fp, 0, mp_t)
#define RPLACE_2(mp_fp, mp_t)RPLACE_1(mp_fp, mp_t), RPLACE_X(mp_fp, 1, mp_t)
#define RPLACE_3(mp_fp, mp_t)RPLACE_2(mp_fp, mp_t), RPLACE_X(mp_fp, 2, mp_t)
#define RPLACE_4(mp_fp, mp_t)RPLACE_3(mp_fp, mp_t), RPLACE_X(mp_fp, 3, mp_t)
#define RPLACE_5(mp_fp, mp_t)RPLACE_4(mp_fp, mp_t), RPLACE_X(mp_fp, 4, mp_t)
#define RPLACE_6(mp_fp, mp_t)RPLACE_5(mp_fp, mp_t), RPLACE_X(mp_fp, 5, mp_t)
#define RPLACE_7(mp_fp, mp_t)RPLACE_6(mp_fp, mp_t), RPLACE_X(mp_fp, 6, mp_t)
#define RPLACE_8(mp_fp, mp_t)RPLACE_7(mp_fp, mp_t), RPLACE_X(mp_fp, 7, mp_t)
#define RPLACE_9(mp_fp, mp_t)RPLACE_8(mp_fp, mp_t), RPLACE_X(mp_fp, 8, mp_t)
#define RPLACE_10(mp_fp, mp_t)RPLACE_9(mp_fp, mp_t), RPLACE_X(mp_fp, 9,
mp_t)
#define RPLACE(mp_fp, mp_t, mp_d)CONCAT(RPLACE_, mp_d)(mp_fp, mp_t)



/* Application
______________________________________________________________ */
static char const g_data_0[] = "Static-Data0";
static char const g_data_1[] = "Static-Data1";
static char const g_data_2[] = "Static-Data2";


static char const g_datax_0[] = "StaticX-Data0";
static char const g_datax_1[] = "StaticX-Data1";
static char const g_datax_2[] = "StaticX-Data2";


typedef struct foo_s {
size_t idx;
char const* data;
} foo_t;


/* setup our custom functions */
#define FOO_OUTER() 2
#define FOO_INNER() 3
#define FOO_IDX_0() g_data_
#define FOO_IDX_1() g_datax_

#define INIT_X(mp_idx, mp_t) \
{ (mp_idx), CONCAT(mp_t(), mp_idx) }

#define INIT(mp_idx, mp_t) \
{ RPLACE(INIT_X, CONCAT(mp_t, mp_idx), FOO_INNER()) }


/* declare the array */
static foo_t g_foo[FOO_OUTER()][FOO_INNER()] = {
PLACE(INIT, FOO_IDX_, FOO_OUTER())
};


int main() {
{
size_t o, i;
for(o = 0; o < FOO_OUTER(); ++o) {
for(i = 0; i < FOO_INNER(); ++i) {
printf("g_foo[%u][%u].idx(%u)\ng_foo[%u][%u].data(%s)\n---\n",
o, i, g_foo[o].idx, o, i, g_foo[o].data);
assert(i == g_foo[o].idx);
}
puts("\n");
}
}
puts("\n\n\n_______________________________\n\
press <ENTER> to exit...\n");
return getchar();
}

--------------------


I suggested something like that to a previous question on the topic:

http://groups.google.com/group/comp.lang.c/msg/71674afb7c772df8


I think it could possibly be useful. What do you think? Is it crap?

:^)
 
R

Richard Bos

Chris Thomasson said:
Here is a way that you can "automate" static initialization of arrays with
customized data. Here is a quick example:
#define CONCAT_X(mp_t1, mp_t2)mp_t1##mp_t2
#define CONCAT(mp_t1, mp_t2)CONCAT_X(mp_t1, mp_t2)

#define PLACE_X(mp_fp, mp_idx, mp_t)mp_fp(mp_idx, mp_t)
#define PLACE_1(mp_fp, mp_t)PLACE_X(mp_fp, 0, mp_t)
#define PLACE_2(mp_fp, mp_t)PLACE_1(mp_fp, mp_t), PLACE_X(mp_fp, 1, mp_t)
#define PLACE_3(mp_fp, mp_t)PLACE_2(mp_fp, mp_t), PLACE_X(mp_fp, 2, mp_t)
#define PLACE_4(mp_fp, mp_t)PLACE_3(mp_fp, mp_t), PLACE_X(mp_fp, 3, mp_t)
#define PLACE_5(mp_fp, mp_t)PLACE_4(mp_fp, mp_t), PLACE_X(mp_fp, 4, mp_t)

*Brrr*...

I'll stick with statically allocated data or dynamically generated C
code, if it's all the same to you.

Richard
 
T

Thad Smith

Chris said:
Here is a way that you can "automate" static initialization of arrays
with customized data. Here is a quick example:
/* Macro Table
______________________________________________________________ */
#define CONCAT_X(mp_t1, mp_t2)mp_t1##mp_t2
#define CONCAT(mp_t1, mp_t2)CONCAT_X(mp_t1, mp_t2)

#define PLACE_X(mp_fp, mp_idx, mp_t)mp_fp(mp_idx, mp_t)
#define PLACE_1(mp_fp, mp_t)PLACE_X(mp_fp, 0, mp_t)
#define PLACE_2(mp_fp, mp_t)PLACE_1(mp_fp, mp_t), PLACE_X(mp_fp, 1, mp_t)
#define PLACE_3(mp_fp, mp_t)PLACE_2(mp_fp, mp_t), PLACE_X(mp_fp, 2, mp_t)
#define PLACE_4(mp_fp, mp_t)PLACE_3(mp_fp, mp_t), PLACE_X(mp_fp, 3, mp_t)
#define PLACE_5(mp_fp, mp_t)PLACE_4(mp_fp, mp_t), PLACE_X(mp_fp, 4, mp_t)
#define PLACE_6(mp_fp, mp_t)PLACE_5(mp_fp, mp_t), PLACE_X(mp_fp, 5, mp_t)
#define PLACE_7(mp_fp, mp_t)PLACE_6(mp_fp, mp_t), PLACE_X(mp_fp, 6, mp_t)
#define PLACE_8(mp_fp, mp_t)PLACE_7(mp_fp, mp_t), PLACE_X(mp_fp, 7, mp_t)
#define PLACE_9(mp_fp, mp_t)PLACE_8(mp_fp, mp_t), PLACE_X(mp_fp, 8, mp_t)
#define PLACE_10(mp_fp, mp_t)PLACE_9(mp_fp, mp_t), PLACE_X(mp_fp, 9, mp_t)
#define PLACE(mp_fp, mp_t, mp_d)CONCAT(PLACE_, mp_d)(mp_fp, mp_t)

Hmmm, it looks like too much confusion and not enough generality for too
little benefit.

When faced with automating generation of data tables in the past, I
wrote a simple program to generate the data I wanted and split out a
section of code for the data tables, either an #include file (my
preference) or a block of code that can be pasted into existing code.
 
C

Chris Thomasson

Thad Smith said:
Chris said:
Here is a way that you can "automate" static initialization of arrays
with customized data. Here is a quick example:
/* Macro Table
______________________________________________________________ */
[...]

Hmmm, it looks like too much confusion and not enough generality for too
little benefit.

When faced with automating generation of data tables in the past, I wrote
a simple program to generate the data I wanted and split out a section of
code for the data tables, either an #include file (my preference) or a
block of code that can be pasted into existing code.

[...]

I basically wanted to see how far one could push the pre-processor wrt
code-generation. IMHO, it can do a "lot" of stuff, but its sure not pretty!
 
T

Thad Smith

Chris said:
Thad Smith said:
/* Macro Table
______________________________________________________________ */
[...]

Hmmm, it looks like too much confusion and not enough generality for
too little benefit.

I basically wanted to see how far one could push the pre-processor wrt
code-generation. IMHO, it can do a "lot" of stuff, but its sure not pretty!

The biggest benefit I get from pushing macro processing is mapping I/O
signal names to physical ports and bits. Each signal is represented by
a macro that includes a port identifier, a starting bit number, number
of bits, one-true or zero-true polarity. In some cases ports had
different means of access and some required a shadow register (cache),
while others do not.

The read and write macros use the definitions to generate customized
code. Since the I/O identifier is constant, rather than a variable, the
compiler generates code optimized for each specific I/O type. Using the
mapping, I can define I/O parameters in one file, so changing I/O pins
is very easy.

For my use, the read and macros are the messiest, but perform a
well-documented function and don't need frequent changing. They are, of
course, tailored to the specific target processor. The procedure for
defining the I/O definition macros is well commented.

The bottom line, for me, is that the macros allow me to provide a
platform to generate very efficient I/O while defining each I/O in one
place. Outside the macro magic header file, a straightforward
function-like macro does what its name implies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top