好不容易改好了一小段代码,发现用O3级优化后,被CCS已经整的面目全非。。。。。。<br /> <br />例子代码如下:<br /> <br /> for(int i = 0; i < 6; i ++) {//6路报号命令分析及处理<br /> if(NumberBufferLen == 0) {<br /> for(int j = 0; j < 128; j ++) {<br /> SndBuffer[i + 2][j + k] = 0xd5; }<br /> NumberBufferPos = 0;<br /> *((unsigned int *)(0x811d + i * 0x20)) = 0x6655;// }<br /> else {<br /> Flash.PageRead(GetNumberPlayPos(i), 128, i + 2, k);//<br /><br /> asm(" nop");<br /> asm(" nop");<br /> asm(" nop");<br /> }<br /> }<br /> FlashReadFlag = 0;//禁止读出数据 <br /> <br /> <br />就这么简单的代码却始终无法正确运行,原因也很奇怪:<br /> <br />首先,局部变量i,j都必须加 volatile,防止优化<br /> <br />volatile unsigned int i, j;//***********************************************************<br /> //如果不这样,i , j的值会在后面的Flash.PageRead函数里变的不确定,我在<br /> // 实验中就发现i居然变成了10362<br /> for(i = 0; i < 6; i ++) {//6路报号命令分析及处理<br /> if(NumberBufferLen == 0) {<br /> for(j = 0; j < 128; j ++) {<br /> SndBuffer[i + 2][j + k] = 0xd5; }<br /> NumberBufferPos = 0;<br /> *((unsigned int *)(0x811d + i * 0x20)) = 0x6655;// }<br /> else {<br /> Flash.PageRead(GetNumberPlayPos(i), 128, i + 2, k);//进入到此函数后进行单步调试,居然i变成了<br /> //10362!!!!!<br /><br /> asm(" nop");<br /> asm(" nop");<br /> asm(" nop");<br /> }<br /> }<br /> FlashReadFlag = 0;//禁止读出数据 <br /> <br />查阅资料,发现了下面的一段话:::::<br /> <br />Figure 3–1. Compiling a C Program With the Optimizer<br />C source<br />file (.c)<br />Code<br />generator<br />Parser Optimizer<br />.asm file<br />The easiest way to invoke the optimizer is to use the cl500 shell program,<br />specifying the –on option on the cl500 command line. The n denotes the level<br />of optimization (0, 1, 2, and 3), which controls the type and degree of optimization:<br /> –o0<br /> Performs control-flow-graph simplification<br /> Allocates variables to registers<br /> Performs loop rotation<br /> Eliminates unused code<br /> Simplifies expressions and statements<br /> Expands calls to functions declared inline<br /> –o1<br />Performs all –o0 optimizations, plus:<br /> Performs local copy/constant propagation<br /> Removes unused assignments<br /> Eliminates local common expressions<br /> –o2<br />Performs all –o1 optimizations, plus:<br /> Performs loop optimizations<br /> Eliminates global common subexpressions<br /> Eliminates global unused assignments<br /> Performs loop unrolling<br />The optimizer uses –o2 as the default if you use –o without an optimization<br />level.<br /> –o3<br />Performs all –o2 optimizations, plus:<br /> Removes all functions that are never called<br /> Simplifies functions with return values that are never used<br /> Inlines calls to small functions<br /> Reorders function declarations so that the attributes of called<br />functions are known when the caller is optimized<br /> Identifies file-level variable characteristics<br /> <br /> <br />接着看下面的**,似乎也没有提到在循环或者分支子函数中,中间变量会自动变化这一情况,<br />个人认为,因为还在大循环中,所以中间变量应该压栈,此时如果调用子函数,那么变量应该是不会<br />自己随意变化的。<br /> <br />看来只能是再研究研究看了。。。。。。。。。。。。。。<br />
|