View previous topic :: View next topic |
Author |
Message |
meyerm Veteran
Joined: 27 Jun 2002 Posts: 1311 Location: Munich / Germany
|
Posted: Thu Jul 01, 2010 1:57 pm Post subject: using the C-preprocessor to fill in data |
|
|
Hi there,
I want to write some algorithms where I'm only interested in some internal behaviour and don't care for the data itself. Of course I could simply allocate some memory within the program and then fill my data field / array with random data. But the problem is, the program should be executed on a quite slow simulator. So getting all that uninteresting meta-stuff should really be left out and as much as possible should be done on (cross-)compiletime.
In D I can let the compiler execute some functions of my program when compiling. That's what I would need. So I can fill my static data on the heap with random data. C - my target language - does not offer sth. like that. But do you know about a possibility to use the C-preprocessor for that? Or are there any GCC-specific additions which could help me with that?
The only solution for now seems to be writing another program which creates the data structure in the target machines format and dumps it into a file which can then be included by the pre processor. But that's not really a nice solution...
Thank you!
M |
|
Back to top |
|
|
mv Watchman
Joined: 20 Apr 2005 Posts: 6747
|
Posted: Thu Jul 01, 2010 4:55 pm Post subject: Re: using the C-preprocessor to fill in data |
|
|
meyerm wrote: | Of course I could simply allocate some memory within the program and then fill my data field / array with random data. But the problem is, the program should be executed on a quite slow simulator. |
Even if it is very slow, it is probably much faster than reading the data from disk.
Quote: | The only solution for now seems to be writing another program which creates the data structure in the target machines format and dumps it into a file which can then be included by the pre processor. |
Or which writes a corresponding .c files whose generated .o you can then link to your code.
Quote: | But that's not really a nice solution... |
Why not? Such things are common practice. The rule is to use the best language for every purpose: You want to generate some particular C code, so you use the best language to generate code. What is the "best language" here depends on your preference and taste, but it is probably something like perl or shell or m4. The latter might be natural if you use autotools for your project anyway. |
|
Back to top |
|
|
meyerm Veteran
Joined: 27 Jun 2002 Posts: 1311 Location: Munich / Germany
|
Posted: Thu Jul 01, 2010 5:06 pm Post subject: Re: using the C-preprocessor to fill in data |
|
|
mv wrote: | meyerm wrote: | Of course I could simply allocate some memory within the program and then fill my data field / array with random data. But the problem is, the program should be executed on a quite slow simulator. |
Even if it is very slow, it is probably much faster than reading the data from disk. |
It's a cycle-accurate simulator where we will likely not even have a full operating system. Definately no disk
mv wrote: | Quote: | The only solution for now seems to be writing another program which creates the data structure in the target machines format and dumps it into a file which can then be included by the pre processor. |
Or which writes a corresponding .c files whose generated .o you can then link to your code. |
Mh, I like that idea much better than the preprocessor one. Thanks for that hint!
mv wrote: | You want to generate some particular C code, so you use the best language to generate code. |
Basically you're right. I would have preferred to not generate additional C-code file but in the end it's not really that different when I simply link in a simple object file with a static array.
Again, nice idea. Thank you |
|
Back to top |
|
|
|