cat bug.c
__sbit __at(0xA0) P2_0;
void main(void) {
// toggle pin
for(;;) {
if(P2_0) {
P2_0=0;
} else {
P2_0=1;
}
}
}
compile:
sdcc -mmcs51 --opt-code-size --model-small --Werror -I. --std-sdcc99 bug.c
cat bug.rst
1 ;--------------------------------------------------------
2 ; File Created by SDCC : free open source ANSI-C Compiler
3 ; Version 3.5.0 #9253 (Jun 20 2015) (MINGW64)
4 ; This file was generated Sat Feb 20 16:35:42 2016
5 ;--------------------------------------------------------
6 .module bug
7 .optsdcc -mmcs51 --model-small
----8<---8<---
000062 123 _main:
000007 124 ar7 = 0x07
000006 125 ar6 = 0x06
000005 126 ar5 = 0x05
000004 127 ar4 = 0x04
000003 128 ar3 = 0x03
000002 129 ar2 = 0x02
000001 130 ar1 = 0x01
000000 131 ar0 = 0x00
000062 132 00105$:
133 ; bug.c:8: if(P2_0) {
134 ; bug.c:9: P2_0=0;
000062 10 A0 02 [24] 135 jbc _P2_0,00115$
000065 80 02 [24] 136 sjmp 00102$
000067 137 00115$:
000067 80 F9 [24] 138 sjmp 00105$
000069 139 00102$:
140 ; bug.c:11: P2_0=1;
000069 D2 A0 [12] 141 setb _P2_0
00006B 80 F5 [24] 142 sjmp 00105$
Dmitriy
kharpost@altlinux.org
The generated assembly is right though not quite efficient.
A better optimized one could be
Last edit: Kharitonov Dmitry 2016-02-24
Last edit: Kharitonov Dmitry 2016-02-25
This should be fixed in the peephole rules. This is where the jbc is inserted. It needs a jump-jump optimization afterwards. Maybe reorder the rules or add a restart.
Last edit: Maarten Brock 2016-02-25
Diff:
The compiler generated code is a non-working
A better optimized one could be
The new peephole rules do not go as far to use CPL, but stop at JBC, SETB.
If you want a real complement, you should write:
... or
Look up what JBC stands for and you'll know why the generated code is ok.
I'm sorry, I'm confused JBC with JNB.
Implemented by peephole rules 257.x and 259.x in [r9519].