sdcc -v
SDCC : z80/sm83/mos6502/mos65c02 TD- 4.4.1 #14877 (Linux)
published under GNU General Public License (GPL)
Getting a codegen error on sm83/gbz80 where a variable declared as stack local should get a return value from a function but instead is overwritten before it can be tested. I've reduced it to a fairly minimal test case.
sdcc -msm83 --no-std-crt0 --fsigned-char --use-stdout -Wa-pogn -c main.c -o main.o
Test case:
unsigned char some_param;
unsigned char some_var;
unsigned char some_func(unsigned char value);
#define J_A 0x10
#define J_START 0x80
#define true 1
#define false 0
void main(void) {
unsigned char some_return_value = some_func(some_param);
some_var = false;
if (some_return_value == J_A) {
some_var = true;
}
}
Output:
_main::
;main.c:14: unsigned char some_return_value = some_func(some_param);
ld a, (_some_param)
call _some_func
;main.c:16: some_var = false;
xor a, a ; <--------- return value stored in A overwritten
ld hl, #_some_var
ld (hl), a
;main.c:18: if (some_return_value == J_A) {
sub a, #0x10 ; <--------- Test is now inaccurate
ret NZ
;main.c:19: some_var = true;
ld (hl), #0x01
;main.c:21: }
ret
Looks like the issue begins to show up starting with:
commit 14814 : Improve storing of 0 into global variables.
https://sourceforge.net/p/sdcc/code/14814/
By the way, the previous generated code without the issue looked like this:
I also get this with
-mz80
, [r14877]produces here:
Where
xor a, a
also destroys the output of the function before the A is to be used in the if statement.Related
Commit: [r14877]
Last edit: Maarten Brock 2024-06-06
Fixed in [r14878].
Related
Commit: [r14878]
Thanks for the quick fix on this!
It appears to fix the issue in the original larger code too.
Last edit: bbbbbr 2024-06-03