Продолжение экспериментов. Исходный код:
Код
#define DELAY(TIME) for (volatile uint32_t i = (24000000 / 4 * TIME); i--; )
uint32_t cnt;
int32_t main(void)
{
__nop;
while (1) {
cnt++;
__nop; __nop; __nop;
DELAY(0.001);
}
}
Компилируется в следующий, при -O1 (при -O0 появляется одна команда NOP)
Код
;;;94 int32_t main(void)
000000 4a16 LDR r2,|L1.92|
;;;95 {
;;;96 __nop;
;;;97 while (1) {
;;;98 cnt++;
;;;99 __nop; __nop; __nop;
;;;100 DELAY(0.001);
000002 f2417370 MOV r3,#0x1770
|L1.6|
000006 6810 LDR r0,[r2,#0] ;98 ; cnt
000008 1c40 ADDS r0,r0,#1 ;98
00000a 6010 STR r0,[r2,#0] ;98 ; cnt
00000c 4619 MOV r1,r3
|L1.14|
00000e 0008 MOVS r0,r1
000010 f1a10101 SUB r1,r1,#1
000014 d1fb BNE |L1.14|
000016 e7f6 B |L1.6|
;;;101 }
;;;102 }
Как!? Как это согласуется с цитатой из помощи:
Цитата
__nop
This intrinsic inserts a NOP instruction or an equivalent code sequence into the instruction stream generated by the compiler. One NOP instruction is generated for each __nop intrinsic in the source.
The compiler does not optimize away the NOP instructions, except for normal unreachable code elimination. The __nop intrinsic also acts as a barrier for instruction scheduling in the compiler. That is, instructions are not moved from one side of the NOP to the other as a result of optimization.