Hi,
I just found out that if O3 is used as optimization level, then the compiler can optimize a function which has a constant assigned its input, and do static calculation on this and optimize away the useless code.
Example:
int spi_initMaster(volatile avr32_spi_t *spi, const spi_options_t *options) { u_avr32_spi_mr_t u_avr32_spi_mr; if (options->fdiv > 1 || options->modfdis > 1) { return SPI_ERROR_ARGUMENT; } // Reset. spi->cr = AVR32_SPI_CR_SWRST_MASK; // Master Mode. u_avr32_spi_mr.mr = spi->mr; u_avr32_spi_mr.MR.mstr = 1; u_avr32_spi_mr.MR.fdiv = options->fdiv; u_avr32_spi_mr.MR.modfdis = options->modfdis; u_avr32_spi_mr.MR.llb = 0; u_avr32_spi_mr.MR.pcs = (1 << AVR32_SPI_MR_PCS_SIZE) - 1; spi->mr = u_avr32_spi_mr.mr; return SPI_OK; }
The code
if (options->fdiv > 1 ||
options->modfdis > 1) {
return SPI_ERROR_ARGUMENT;
}
will not appear in the compiled program if the values checked as constant.
And I also found out that if the values are outside the limits, then compiler would completely delete this function, because it got return with SPI_ERROR_ARGUMENT and it's useless to other program.
This is a great stuff!!! This is what I dreamed of in AVRGCC, now I can do the static assertion for all the hardware limited parameters!
Cheng