The following code seems to set the variable start incorrectly to 127. If I remove the any of the if statements, the start value is set correctly to 2.
/// GPL 2.0 or later
#include <string.h>
#include <stdio.h>
#include <stdint.h>
const char TextPunc[] = ":%! ";
#define TEXT_SPACE ( TEXT_PUNC + 3 )
#define TEXT_UPPER ( TEXT_NUM + 10 )
#define TEXT_PUNC ( TEXT_UPPER + 26 )
#define TEXT_NUM ( TEXT_START + 0 )
#define TEXT_START 0
void ImageCompleteText( uint8_t x, uint8_t y, const char *text ) {
while( *text ) {
uint8_t ch = *text++;
uint8_t index = 0;
uint8_t start = TEXT_SPACE;
if ( ch == ' ' ) ;
else{
char *ptr = strchr( TextPunc, ch );
if ( ptr )
{
index = ptr - TextPunc;
start = TEXT_PUNC;
}
}
printf( "%d %d %c\n", index, start, ch );
x += 2;
}
}
void main() {
printf("Start\n");
ImageCompleteText(0,0,"!");
printf("End\n");
}
#ifdef __SDCC
__sfr __at 0xff sif;
int putchar( int c ) {
sif = 'p';
sif = c;
return c;
}
#endif
sdcc -mz80 ./strchr.c -o strchr.ihx && ucsim_z80 -I if=outputs[0xff] strchr.ihx
./strchr.c:5: warning 85: in function ImageCompleteText unreferenced function argument : 'y'
./strchr.c:40: warning 283: function declarator with no prototype
uCsim 0.7.5, Copyright (C) 1997 Daniel Drotos.
uCsim comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
0> Loading from strchr.ihx
3260 words read from strchr.ihx
r
Simulation started, PC=0x000000
Start
127 36 !
End
$ gcc ./strchr.c && ./a.out
Start
2 36 !
End
$ sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.8 #13684 (Linux)
I've created a test case from your code (attached).
But I can't reproduce the issue in current trunk on my Debian GNU/Linux testing system. Can you post the asm code, compiled with --fverbose-asm?
Attached
I've been able to reproduce the issue by building sdcc without treedec.
Fixed in [r13695].
Related
Commit: [r13695]
The regression test for this bug fails (for me) without treedec with --max-allocs-per-node between 38142 and 170117.
It fails for me (Debian GNU/Testing on amd64) too for --max-allocs-per-node 500000 and 45142, but not for 39142.
P.S.: Looks like codegen for builtin strchr corrupts b , which is the upper byte of text at that point.
P.P.S.: I have attached a first attempt at a patch (it isn't working yet, gives an error a compilet time)
Last edit: Philipp Klaus Krause 2022-11-01
This should be fixed in [r13745]. Please check if it works for you.
Related
Commit: [r13745]
The regression test no longer fails.
(It's interesting that the fastest and smallest result is for MAPN=1024.)