DSPRelated.com
Forums

C6000 does not initialize static data

Started by Andreas Huennebeck November 25, 2004
Hi,

Im having a problem with my first DSP project using a TI 
TMS320C6415 DSP. Any static variables that are not initialized
explicitly in the C or C++ code 

  e.g. static int i1;     // works not
       static int i2 = 0; // works

are either
* not initialized at all (using compiler option -ml3), or
* are initialized while loading the program (using compiler options
  -ml0 or -ml2) although the linker option -c is used.

I checked the _c_int00 function which initializes the data from the .cinit
segment and verified that these static data are not in the .cinit segment.

All data are in external memory, only the .text segment is in internal 
memory.

Any ideas?

Thanks a lot
Andreas
-- 
Andreas H�nnebeck | email: ah@despammed.com
----- privat ---- | www  : http://www.huennebeck-online.de
Fax/Anrufbeantworter: 0721/151-284301
GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
On Thu, 25 Nov 2004 11:21:30 +0100, Andreas Huennebeck
<ah@despammed.com> wrote in comp.dsp:

> Hi, > > Im having a problem with my first DSP project using a TI > TMS320C6415 DSP. Any static variables that are not initialized > explicitly in the C or C++ code > > e.g. static int i1; // works not > static int i2 = 0; // works > > are either > * not initialized at all (using compiler option -ml3), or > * are initialized while loading the program (using compiler options > -ml0 or -ml2) although the linker option -c is used. > > I checked the _c_int00 function which initializes the data from the .cinit > segment and verified that these static data are not in the .cinit segment. > > All data are in external memory, only the .text segment is in internal > memory. > > Any ideas? > > Thanks a lot > Andreas
C initialization in embedded systems often needs a little help from the user. The simplest thing is to add a function that runs before _c_int+00 that sets all the external memory to 0, perhaps after doing a memory test if you want robustness. The second best alternative, if you don't want to modify the default start-up code is to write a C or assembly language function that sets this to 0 and call it in the first executable line in main(). -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Pavel Semyonov wrote:

> Why do you expect that your static vars will be initialized to a specific > value once you do not explicitly do this in your code? ANSI C standard > does not state that static vars shall be initialized.
Yes, it does, in chapter 6.5.7: "A global variable or a local variable that is declared as *static* will automatically be set to zero if no explicit initialization is present." Tons of code all over the world rely on this behaviour.
> In order to get a > portable and robust code, you must explicitly initialize those vars, which > are required to be initialized upon your application.
It seems so. I know that the TI compiler creats good code but I did not expect it to be not compliant to ANSI-C. Tschau Andreas -- Andreas H&#4294967295;nnebeck | email: ah@despammed.com ----- privat ---- | www : http://www.huennebeck-online.de Fax/Anrufbeantworter: 0721/151-284301 GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
Hello Andreas,

Why do you expect that your static vars will be initialized to a specific 
value once you do not explicitly do this in your code? ANSI C standard does 
not state that static vars shall be initialized. In order to get a portable 
and robust code, you must explicitly initialize those vars, which are 
required to be initialized upon your application.

-- 

Regards,
Pavel

"Andreas Huennebeck" <ah@despammed.com> wrote in message 
news:30lq1bF31n864U1@uni-berlin.de...
> Hi, > > Im having a problem with my first DSP project using a TI > TMS320C6415 DSP. Any static variables that are not initialized > explicitly in the C or C++ code > > e.g. static int i1; // works not > static int i2 = 0; // works > > are either > * not initialized at all (using compiler option -ml3), or > * are initialized while loading the program (using compiler options > -ml0 or -ml2) although the linker option -c is used. > > I checked the _c_int00 function which initializes the data from the .cinit > segment and verified that these static data are not in the .cinit segment. > > All data are in external memory, only the .text segment is in internal > memory. > > Any ideas? > > Thanks a lot > Andreas > -- > Andreas H&#4294967295;nnebeck | email: ah@despammed.com > ----- privat ---- | www : http://www.huennebeck-online.de > Fax/Anrufbeantworter: 0721/151-284301 > GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
"Pavel Semyonov" <pavel@NOSPAM-mlabsys.com> wrote in message news:<co7efv$1gmq$1@news.aha.ru>...
> Hello, > > > Yes, it does, in chapter 6.5.7: "A global variable or a local variable > > that is declared as *static* will automatically be set to zero if no > > explicit initialization is present." Tons of code all over the world > > rely on this behaviour. > @This is an opening for me, at least I never heard about that. I will look > thoroughly into the ANSI. That's wonderful: at least I remeber one > confusion: In the past DOS times there were two C compilers available: > Microsoft C and Borland C. Microsoft did clear the allocated storage for > both static vars and dynamically alllocated memory arrays, whereas Borland > compiler did neither of those. Note, that both were claimed to meet the ANSI > C. I remember taht it took me a lot of time to clarify why the code did not > run after Borland, and this made me clear setting: always initialize data > that you wonna to be initialized to the '0' state. probably, the ANSI C has > changed since that time. What revision of ANSI C do you have? > > However anyway, I never rely on any default assumptions, and do explicit > setting myself. I would advise you to do the same should you need portable > and reliable code. > > I would highly recommend you to submit a request to the TI DSP hot line > (dsph@ti.com) and ask this question. Let me know what they will reply you. > > -- > > Regards, > Pavel > > "Andreas Huennebeck" <ah@despammed.com> wrote in message > news:30od25F33ehukU1@uni-berlin.de... > > Pavel Semyonov wrote: > > > >> Why do you expect that your static vars will be initialized to a specific > >> value once you do not explicitly do this in your code? ANSI C standard > >> does not state that static vars shall be initialized. > > > > Yes, it does, in chapter 6.5.7: "A global variable or a local variable > > that is declared as *static* will automatically be set to zero if no > > explicit initialization is present." Tons of code all over the world > > rely on this behaviour. > > > >> In order to get a > >> portable and robust code, you must explicitly initialize those vars, > >> which > >> are required to be initialized upon your application. > > > > It seems so. I know that the TI compiler creats good code but I did > > not expect it to be not compliant to ANSI-C. > > > > Tschau > > Andreas > > -- > > Andreas H&#4294967295;nnebeck | email: ah@despammed.com > > ----- privat ---- | www : http://www.huennebeck-online.de > > Fax/Anrufbeantworter: 0721/151-284301 > > GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
The TI '54X C compiler does not zero global data. John
John Sampson wrote:

> "Pavel Semyonov" <pavel@NOSPAM-mlabsys.com> wrote in message news:<co7efv$1gmq$1@news.aha.ru>... > >>Hello, >> >> >>>Yes, it does, in chapter 6.5.7: "A global variable or a local variable >>>that is declared as *static* will automatically be set to zero if no >>>explicit initialization is present." Tons of code all over the world >>>rely on this behaviour. >> >>@This is an opening for me, at least I never heard about that. I will look >>thoroughly into the ANSI. That's wonderful: at least I remeber one >>confusion: In the past DOS times there were two C compilers available: >>Microsoft C and Borland C. Microsoft did clear the allocated storage for >>both static vars and dynamically alllocated memory arrays, whereas Borland >>compiler did neither of those. Note, that both were claimed to meet the ANSI >>C. I remember taht it took me a lot of time to clarify why the code did not >>run after Borland, and this made me clear setting: always initialize data >>that you wonna to be initialized to the '0' state. probably, the ANSI C has >>changed since that time. What revision of ANSI C do you have? >> >>However anyway, I never rely on any default assumptions, and do explicit >>setting myself. I would advise you to do the same should you need portable >>and reliable code. >> >>I would highly recommend you to submit a request to the TI DSP hot line >>(dsph@ti.com) and ask this question. Let me know what they will reply you. >> >>-- >> >>Regards, >>Pavel >> >>"Andreas Huennebeck" <ah@despammed.com> wrote in message >>news:30od25F33ehukU1@uni-berlin.de... >> >>>Pavel Semyonov wrote: >>> >>> >>>>Why do you expect that your static vars will be initialized to a specific >>>>value once you do not explicitly do this in your code? ANSI C standard >>>>does not state that static vars shall be initialized. >>> >>>Yes, it does, in chapter 6.5.7: "A global variable or a local variable >>>that is declared as *static* will automatically be set to zero if no >>>explicit initialization is present." Tons of code all over the world >>>rely on this behaviour. >>> >>> >>>>In order to get a >>>>portable and robust code, you must explicitly initialize those vars, >>>>which >>>>are required to be initialized upon your application. >>> >>>It seems so. I know that the TI compiler creats good code but I did >>>not expect it to be not compliant to ANSI-C. >>> >>>Tschau >>>Andreas >>>-- >>>Andreas H&#4294967295;nnebeck | email: ah@despammed.com >>>----- privat ---- | www : http://www.huennebeck-online.de >>>Fax/Anrufbeantworter: 0721/151-284301 >>>GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc > > > The TI '54X C compiler does not zero global data. > > John
Technically it's not the compiler that does it -- if the compiler does anything it just puts uninitialized data into a "bss" section. Your start-up code has the responsibility for zeroing the bss section. Since the start-up code is often just the code that came with the compiler it's mostly a nit, except that you can break (or fix) this by appropriate changes to the init code. BTW: The '28xx toolchain _does_ zero out the bss section. Go figure. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Yes, you are right, the compiler does not init static's contrary to
the ANSI standard, so what else is new about TI...  They do things
there own way (actually it's the compiler writer's fault but TI puts
their name on it).

We wrote our own code that executes before _c_int00.

On Thu, 25 Nov 2004 11:21:30 +0100, Andreas Huennebeck
<ah@despammed.com> wrote:

>Hi, > >Im having a problem with my first DSP project using a TI >TMS320C6415 DSP. Any static variables that are not initialized >explicitly in the C or C++ code > > e.g. static int i1; // works not > static int i2 = 0; // works > >are either >* not initialized at all (using compiler option -ml3), or >* are initialized while loading the program (using compiler options > -ml0 or -ml2) although the linker option -c is used. > >I checked the _c_int00 function which initializes the data from the .cinit >segment and verified that these static data are not in the .cinit segment. > >All data are in external memory, only the .text segment is in internal >memory. > >Any ideas? > >Thanks a lot >Andreas
Hello,

> Yes, it does, in chapter 6.5.7: "A global variable or a local variable > that is declared as *static* will automatically be set to zero if no > explicit initialization is present." Tons of code all over the world > rely on this behaviour.
@This is an opening for me, at least I never heard about that. I will look thoroughly into the ANSI. That's wonderful: at least I remeber one confusion: In the past DOS times there were two C compilers available: Microsoft C and Borland C. Microsoft did clear the allocated storage for both static vars and dynamically alllocated memory arrays, whereas Borland compiler did neither of those. Note, that both were claimed to meet the ANSI C. I remember taht it took me a lot of time to clarify why the code did not run after Borland, and this made me clear setting: always initialize data that you wonna to be initialized to the '0' state. probably, the ANSI C has changed since that time. What revision of ANSI C do you have? However anyway, I never rely on any default assumptions, and do explicit setting myself. I would advise you to do the same should you need portable and reliable code. I would highly recommend you to submit a request to the TI DSP hot line (dsph@ti.com) and ask this question. Let me know what they will reply you. -- Regards, Pavel "Andreas Huennebeck" <ah@despammed.com> wrote in message news:30od25F33ehukU1@uni-berlin.de...
> Pavel Semyonov wrote: > >> Why do you expect that your static vars will be initialized to a specific >> value once you do not explicitly do this in your code? ANSI C standard >> does not state that static vars shall be initialized. > > Yes, it does, in chapter 6.5.7: "A global variable or a local variable > that is declared as *static* will automatically be set to zero if no > explicit initialization is present." Tons of code all over the world > rely on this behaviour. > >> In order to get a >> portable and robust code, you must explicitly initialize those vars, >> which >> are required to be initialized upon your application. > > It seems so. I know that the TI compiler creats good code but I did > not expect it to be not compliant to ANSI-C. > > Tschau > Andreas > -- > Andreas H&#4294967295;nnebeck | email: ah@despammed.com > ----- privat ---- | www : http://www.huennebeck-online.de > Fax/Anrufbeantworter: 0721/151-284301 > GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
Pavel Semyonov wrote:

>> Yes, it does, in chapter 6.5.7: "A global variable or a local variable >> that is declared as *static* will automatically be set to zero if no >> explicit initialization is present." Tons of code all over the world >> rely on this behaviour. > > @This is an opening for me, at least I never heard about that. I will look > thoroughly into the ANSI. That's wonderful: at least I remeber one > confusion: In the past DOS times there were two C compilers available: > Microsoft C and Borland C. Microsoft did clear the allocated storage for > both static vars and dynamically alllocated memory arrays, whereas Borland > compiler did neither of those. Note, that both were claimed to meet the > ANSI C. I remember taht it took me a lot of time to clarify why the code > did not run after Borland, and this made me clear setting: always > initialize data that you wonna to be initialized to the '0' state. > probably, the ANSI C has changed since that time. What revision of ANSI C > do you have? > > However anyway, I never rely on any default assumptions, and do explicit > setting myself. I would advise you to do the same should you need portable > and reliable code. > > I would highly recommend you to submit a request to the TI DSP hot line > (dsph@ti.com) and ask this question. Let me know what they will reply you.
This is what TI respondend:
> 1) You are correct that the TI C6000 compiler does not initialized > the un-initialized static variables to 0x0. This is documented at > chap. 7.9 and 8.8.1 of the "Optimizing C Compilers Guide" SPRU187L. > > A workaround would be to use the linker to do the initialization > of the .bss to 0x0. See page 230 and 286 of SPRU187L. > Note: The -b options mentionned is NOT the compiler/linker -b option > (see page 190), but an options to use with the stand alone simulator. > > In case that you use DSP BIOS you can then create an additionnal > .cmd file to fill the .bss section.
This is correct, the chapters cited above make this clear. On the other hand this manual has no references to these chapters from those places dealing with initialization, and it is not documented in that chapter dealing with the compiler incompatibilities, so it's not easy to find. bye Andreas -- Andreas H&#4294967295;nnebeck | email: ah@despammed.com ----- privat ---- | www : http://www.huennebeck-online.de Fax/Anrufbeantworter: 0721/151-284301 GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
On 2004-12-08 12:27:20 +0100, Andreas Huennebeck <ah@despammed.com> said:

> Pavel Semyonov wrote: > >>> Yes, it does, in chapter 6.5.7: "A global variable or a local variable >>> that is declared as *static* will automatically be set to zero if no >>> explicit initialization is present." Tons of code all over the world >>> rely on this behaviour.
And almost every second (or even EVERY) book on C/C++ tells you (and should do so) that you should NOT rely on static variables being initialized to any sensible value. It's bad practice to "rely on this behaviour" of some compilers doing this for you. -- Stephan M. Bernsee http://www.dspdimension.com