It seems that __critical keyword may produce invalid bytecode in some conditions.
command line: sdcc -mmcs51 test.c -o test.ihx
SDCC 4.0.0 #11576 (MINGW64)
volatile int cnt;
inline int cnt_get(void) __critical {
return cnt;
}
int cnt0;
void main() {
cnt0 = cnt;
for(;;) {
if ( (cnt_get()-cnt0) >= 1) {
cnt0++;
}
}
}
produce log:
../test.c:11: warning 84: 'auto' variable '__2621440001' may be used before initialization
../test.c:11: warning 84: 'auto' variable '__2621440001' may be used before initialization
cnt fetching was broken as well:
mov r7,#0x01
jbc ea,00117$
mov r7,#0x00
00117$:
; ../test.c:4: return cnt;
mov a,_cnt
mov a,(_cnt + 1)
; ../test.c:11: if ( (cnt_get()-cnt0) >= 1) {
mov a,r7
rrc a
mov ea,c
Following code
volatile int cnt;
inline int cnt_get(void) {
return cnt;
}
int cnt0;
void main() {
cnt0 = cnt;
for(;;) {
int v;
__critical { v = cnt_get(); }
if ( (v-cnt0) >= 1) {
cnt0++;
}
}
}
correspond to expected correct cnt fetching
mov r7,#0x01
jbc ea,00117$
mov r7,#0x00
00117$:
; ../test.c:5: return cnt;
mov r5,_cnt
mov r6,(_cnt + 1)
; ../test.c:13: __critical { v = cnt_get(); }
mov a,r7
rrc a
mov ea,c