I think I can partly reproduce this on gbz80.
It appears to prepare a 16 bit return value, even though it only returns
8 bit?
_bar::
;main.c:54: return (value ? foo(value) + 1 : 0);
ldhl sp, #3
ld a, (hl-)
or a, (hl)
jr Z,00103$
pop bc
pop hl
push hl
push bc
push hl
call _foo
add sp, #2
inc e
ld a, e
rla
sbc a, a
ld d, a
ret
00103$:
ld de, #0x0000
;main.c:55: }
ret
On 19.05.20 23:37, Basil Hussain wrote:
Here's a self-contained example that I can replicate the issue with:
#include <stdint.h>
uint8_t foo(uint16_t value) {
return value;
}
uint8_t bar(uint16_t value) {
return (value ? foo(value) + 1 : 0);
}
void main(void) {
uint8_t x = bar(0xFF);
}
Compiled with:
sdcc.exe -mstm8 --std-c99 -I"C:\Program Files\SDCC\include" test.c
It gives the following assembly output (from .lst file):
89 ;
-----------------------------------------
90 ; function foo
91 ;
-----------------------------------------
000000 92 _foo:
93 ; test.c: 7: return value;
000000 7B 04 [ 1] 94 ld a, (0x04, sp)
95 ; test.c: 8: }
000002 81 [ 4] 96 ret
97 ; test.c: 10: uint8_t
bar(uint16_t value) {
98 ;
-----------------------------------------
99 ; function bar
100 ;
-----------------------------------------
000003 101 _bar:
102 ; test.c: 11: return (value ?
foo(value) + 1 : 0);
000003 1E 03 [ 2] 103 ldw x, (0x03, sp)
000005 27 0E [ 1] 104 jreq 00103$
000007 89 [ 2] 105 pushw x
000008 CDr00r00 [ 4] 106 call _foo
00000B 5B 02 [ 2] 107 addw sp, #2
00000D 4C [ 1] 108 inc a
00000E 88 [ 1] 109 push a ; <--
00000F 49 [ 1] 110 rlc a ; <--
000010 4F [ 1] 111 clr a ; <--
000011 A2 00 [ 1] 112 sbc a, #0x00 ; <--
000013 84 [ 1] 113 pop a ; <--
000014 81 [ 4] 114 ret
000015 115 00103$:
000015 4F [ 1] 116 clr a
117 ; test.c: 12: }
000016 81 [ 4] 118 ret
119 ; test.c: 14: void main(void) {
120 ;
-----------------------------------------
121 ; function main
122 ;
-----------------------------------------
000017 123 _main:
124 ; test.c: 15: uint8_t x =
bar(0xFF);
000017 4B FF [ 1] 125 push #0xff
000019 4B 00 [ 1] 126 push #0x00
00001B CDr00r03 [ 4] 127 call _bar
00001E 5B 02 [ 2] 128 addw sp, #2
129 ; test.c: 16: }
000020 81 [ 4] 130 ret
By the way, interesting that in this example for some reason SDCC didn't
do the jrf optimisation, but instead included another ret, but not
really of any concern as it's beside the point.
Regards,
Basil
On 19/05/2020 22:05, Philipp Klaus Krause wrote:
Could you show a declaration of ctz_16?
Philipp
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user
--
Freundliche Grüße / Yours sincerely
Sebastian Riedel
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user