From: Mary-Ann J. <Mar...@di...> - 2008-01-22 15:40:08
|
If I compile the following code (SDCC 2.7.0): ------------------------------------------------ void func(void) { int timeout; return; timeout =3D 0xFFFF; while (timeout--); } ------------------------------------------------ SDCC produces the following warnings: "src/test.c:8: warning 84: 'auto' variable 'timeout' may be used before initialization src/test.c:8: warning 84: 'auto' variable 'timeout' may be used before initialization src/test.c:8: warning 84: 'auto' variable 'timeout' may be used before initialization" Any idea why? And before someone says "Don't call 'return' there!" - this scenario happens when a macro is set to "return" to eliminate the 2nd half of a function. Thanks, Mary-Ann --=20 Mary-Ann Johnson, Development Engineer, DisplayLink (UK) Limited Tel: +44 (0)1223 443920 | DDI: +44 (0)1223 443971 | Fax: +44 (0)1223 443921 E: Mar...@di... | W: http://www.displaylink.com/ DisplayLink (UK) Limited - Registered in England No. 04811048 Mount Pleasant House, Mount Pleasant, Cambridge, CB3 0RN, United Kingdom. DisplayLink Corp. - a US incorporated company. 480 S. California Avenue Ste 304, Palo Alto, CA 94306, USA. |
From: Ken J. <Ken...@ie...> - 2008-01-22 17:39:26
|
I don't know the answer, though I offer an observation. Most compilers will optimize away the delay that you are trying to implement. Therefore you should declare timeout this way: volatile int timeout; Also, when I do it that way, I don't get the warning. -Ken Jackson Mary-Ann Johnson writes: > If I compile the following code (SDCC 2.7.0): > > ------------------------------------------------ > void func(void) > { > int timeout; > > return; > > timeout = 0xFFFF; > while (timeout--); > } > ------------------------------------------------ > > SDCC produces the following warnings: > "src/test.c:8: warning 84: 'auto' variable 'timeout' may be used before > initialization > src/test.c:8: warning 84: 'auto' variable 'timeout' may be used before > initialization > src/test.c:8: warning 84: 'auto' variable 'timeout' may be used before > initialization" > > Any idea why? > > And before someone says "Don't call 'return' there!" - this scenario > happens when a macro is set to "return" to eliminate the 2nd half of a > function. > > Thanks, > Mary-Ann |
From: Mary-Ann J. <Mar...@di...> - 2008-01-24 11:30:34
|
Yes, I want the compiler to optimise it away, but I'd like it to do so without complaining about the "timeout" variable. The following code has similar problems: void func(void) { return; { int timeout; timeout =3D 0xFFFF; while (timeout--); } } Mary-Ann Ken Jackson wrote: > I don't know the answer, though I offer an observation. >=20 > Most compilers will optimize away the delay that you are > trying to implement. Therefore you should declare timeout this way: >=20 > volatile int timeout; >=20 > Also, when I do it that way, I don't get the warning. >=20 > -Ken Jackson >=20 > Mary-Ann Johnson writes: > > If I compile the following code (SDCC 2.7.0): > > > > ------------------------------------------------ =20 > > void func(void) > > { > > int timeout; > > > > return; > > > > timeout =3D 0xFFFF; > > while (timeout--); > > } > > ------------------------------------------------ > > > > SDCC produces the following warnings: > > "src/test.c:8: warning 84: 'auto' variable 'timeout' may > > be used before initialization =20 > > src/test.c:8: warning 84: 'auto' variable 'timeout' may be > > be used before initialization =20 > > src/test.c:8: warning 84: 'auto' variable 'timeout' may be > > used before initialization" > > > > Any idea why? > > > > And before someone says "Don't call 'return' there!" - > > this scenario happens when a macro is set to "return" to > > eliminate the 2nd half of a function. > > > > Thanks, > > Mary-Ann |
From: Maarten B. <sou...@ds...> - 2008-01-24 17:39:19
|
Mary-Ann, I think you missed the point Ken Jackson was trying to make. Even if you do NOT insert the return-statement the code can still be optimized out by the compiler because it has no so-called side-effects. Only if you make the timeout variable 'volatile' you disallow the compiler to make any assumptions about its use and thus the generated code must actually do all the loops you intended. Greets, Maarten > Yes, I want the compiler to optimise it away, but I'd like it to do so > without complaining about the "timeout" variable. The following code > has similar problems: > > void func(void) > { > return; > > { > int timeout; > timeout = 0xFFFF; > while (timeout--); > } > } > > Mary-Ann > > > Ken Jackson wrote: > > I don't know the answer, though I offer an observation. > > > > Most compilers will optimize away the delay that you are > > trying to implement. Therefore you should declare timeout this way: > > > > volatile int timeout; > > > > Also, when I do it that way, I don't get the warning. > > > > -Ken Jackson > > > > Mary-Ann Johnson writes: > > > If I compile the following code (SDCC 2.7.0): > > > > > > ------------------------------------------------ > > > void func(void) > > > { > > > int timeout; > > > > > > return; > > > > > > timeout = 0xFFFF; > > > while (timeout--); > > > } > > > ------------------------------------------------ > > > > > > SDCC produces the following warnings: > > > "src/test.c:8: warning 84: 'auto' variable 'timeout' may > > > be used before initialization > > > src/test.c:8: warning 84: 'auto' variable 'timeout' may be > > > be used before initialization > > > src/test.c:8: warning 84: 'auto' variable 'timeout' may be > > > used before initialization" > > > > > > Any idea why? > > > > > > And before someone says "Don't call 'return' there!" - > > > this scenario happens when a macro is set to "return" to > > > eliminate the 2nd half of a function. > > > > > > Thanks, > > > Mary-Ann > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Sdcc-user mailing list > Sdc...@li... > https://lists.sourceforge.net/lists/listinfo/sdcc-user > |
From: Mary-Ann J. <Mar...@di...> - 2008-01-29 17:37:34
|
Thanks, I had indeed misunderstood Ken's point. However, if I change the code thus: ----------------------------------- void func(void) { volatile int timeout; return; timeout =3D 0xFFFF; while (timeout--); } ----------------------------------- the only difference is that the warning is printed twice, rather than 3 times. :) Any other ideas? Mary-Ann Maarten Brock wrote: > Mary-Ann, >=20 > I think you missed the point Ken Jackson was trying to make. > Even if you do NOT insert the return-statement the code can > still be optimized out by the compiler because it has no > so-called side-effects. Only if you make the timeout variable > 'volatile' you disallow the compiler to make any assumptions > about its use and thus the generated code must actually do all the > loops you intended.=20 >=20 > Greets, > Maarten >=20 >> Yes, I want the compiler to optimise it away, but I'd like it to do >> so without complaining about the "timeout" variable. The following >> code has similar problems:=20 >>=20 >> void func(void) >> { >> return; >>=20 >> { >> int timeout; >> timeout =3D 0xFFFF; >> while (timeout--); >> } >> } >>=20 >> Mary-Ann >>=20 >>=20 >> Ken Jackson wrote: >>> I don't know the answer, though I offer an observation. >>>=20 >>> Most compilers will optimize away the delay that you are trying to >>> implement. Therefore you should declare timeout this way: >>>=20 >>> volatile int timeout; >>>=20 >>> Also, when I do it that way, I don't get the warning. >>>=20 >>> -Ken Jackson >>>=20 >>> Mary-Ann Johnson writes: >>>> If I compile the following code (SDCC 2.7.0): >>>>=20 >>>> ------------------------------------------------ >>>> void func(void) >>>> { >>>> int timeout; >>>>=20 >>>> return; >>>>=20 >>>> timeout =3D 0xFFFF; >>>> while (timeout--); >>>> } >>>> ------------------------------------------------ >>>>=20 >>>> SDCC produces the following warnings: >>>> "src/test.c:8: warning 84: 'auto' variable 'timeout' may be used >>>> before initialization src/test.c:8: warning 84: 'auto' variable >>>> 'timeout' may be be used before initialization src/test.c:8: >>>> warning 84: 'auto' variable 'timeout' may be used before >>>> initialization" =20 >>>>=20 >>>> Any idea why? >>>>=20 >>>> And before someone says "Don't call 'return' there!" - this >>>> scenario happens when a macro is set to "return" to eliminate the >>>> 2nd half of a function.=20 >>>>=20 >>>> Thanks, >>>> Mary-Ann |