Other Parts Discussed in Thread: SYSCONFIG,
In my project i use i2c for accessing an acceleration sensor. A quick look to the generated code (lst-file) shows horrible code: Many redundant load and stores.
I have a marked all redundant load/store in yellow, and other options for better code in orange.
SCS-Source-Code:
macro setMpuReg( reg, val) { i2cStart(); i2cTx( MPU_ADDR | I2C_OP_WRITE); if (state.i2cStatus == 0x0000) { i2cTx( reg ); } if (state.i2cStatus == 0x0000) { i2cTx( val ); } i2cStop(); }
// LowPower-Mode with ACC only setMpuReg( MPU_REG_PM1, MPU_CFG_PM1); // cycle+dis-temp+best-osc setMpuReg( MPU_REG_PM2, MPU_CFG_PM2); // WupFreq=40Hz, Acc:on, Gyro:off
... and more settings of registers
Generated Code :
;? // LowPower-Mode with ACC only ;? setMpuReg( MPU_REG_PM1, MPU_CFG_PM1); // cycle+dis-temp+best-osc ;? > i2cStart(); 014e ---- 6000 ld R6, #0x0000 014f ---- 16a6 jsr I2cStart 0150 ---- 6c9a st R6, [#accsampling/state/i2cStatus] // no further useof state.i2cStatus ;? > i2cTx( MPU_ADDR | I2C_OP_WRITE); 0151 ---- 70d0 ld R7, #208 0152 ---- 689a ld R6, [#accsampling/state/i2cStatus] // value already in R6 0153 ---- 16bd jsr I2cTxByte 0154 ---- 6c9a st R6, [#accsampling/state/i2cStatus] ;? > if (state.i2cStatus == 0x0000) { 0155 ---- 089a ld R0, [#accsampling/state/i2cStatus] // use value in R6 for compare 0156 ---- 8a00 cmp R0, #0 0157 ---- be04 bneq /id0257 ;? > i2cTx( reg ); 0158 ---- 706b ld R7, #107 0159 ---- 689a ld R6, [#accsampling/state/i2cStatus] // value already in R6 015a ---- 16bd jsr I2cTxByte 015b ---- 6c9a st R6, [#accsampling/state/i2cStatus] ;? > } /id0257: ;? > if (state.i2cStatus == 0x0000) { 015c ---- 089a ld R0, [#accsampling/state/i2cStatus] // use value in R6 for compare 015d ---- 8a00 cmp R0, #0 015e ---- be04 bneq /id0263 ;? > i2cTx( val ); 015f ---- 7029 ld R7, #41 0160 ---- 689a ld R6, [#accsampling/state/i2cStatus] // value already in R6 0161 ---- 16bd jsr I2cTxByte 0162 ---- 6c9a st R6, [#accsampling/state/i2cStatus] // no further use of state.i2cStatus ;? > } /id0263: ;? > i2cStop(); 0163 ---- 689a ld R6, [#accsampling/state/i2cStatus] // value already in R6 0164 ---- 16db jsr I2cStop 0165 ---- 6c9a st R6, [#accsampling/state/i2cStatus] ;? setMpuReg( MPU_REG_PM2, MPU_CFG_PM2); // WupFreq=40Hz, Acc:on, Gyro:off
... same as above
With some soft optimization rom 24 instructions 6 could simply removed and 2 instructions could removed with changing the compares -> Code-size reduced by 1/3
All this could be done with simple peep-hole optimisation.
Are there any plans to improve the SCS codegenerator with a simple peep-hole optimizer?
To generate better code i can create special procedures in asm, better i can't find any documentation about the code-generation templates.
My wishes:
- Better codegenerator with optimizer
- Better documentation of CG (.prd-files and .asm-files)
- Integration of SCS into CCS