From 5bda57de87da864e17bce5e5aec79c79420cff9d Mon Sep 17 00:00:00 2001 From: mrdudz Date: Mon, 29 Aug 2022 23:20:48 +0200 Subject: [PATCH] add extra underscore to _bzero, add a test for bzero/memset --- include/string.h | 2 +- libsrc/atari7800/clrscr.s | 4 +-- libsrc/atari7800/mono_clrscr.s | 4 +-- libsrc/common/calloc.s | 4 +-- libsrc/common/memset.s | 12 +++---- libsrc/geos-common/common/memset.s | 6 ++-- libsrc/pce/memset.s | 10 +++--- src/cc65/coptstop.c | 2 +- src/cc65/stdfunc.c | 2 +- src/cc65/stdnames.c | 12 +++---- src/cc65/stdnames.h | 12 +++---- test/val/bzero.c | 51 ++++++++++++++++++++++++++++++ 12 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 test/val/bzero.c diff --git a/include/string.h b/include/string.h index bda960173..854359dad 100644 --- a/include/string.h +++ b/include/string.h @@ -74,7 +74,7 @@ void* __fastcall__ memset (void* s, int c, size_t count); /* The following is an internal function, the compiler will replace memset ** with it if the fill value is zero. Never use this one directly! */ -void* __fastcall__ _bzero (void* ptr, size_t n); +void* __fastcall__ __bzero (void* ptr, size_t n); /* Non standard: */ #if __CC65_STD__ == __CC65_STD_CC65__ diff --git a/libsrc/atari7800/clrscr.s b/libsrc/atari7800/clrscr.s index f9d4938b0..a1f663ba7 100644 --- a/libsrc/atari7800/clrscr.s +++ b/libsrc/atari7800/clrscr.s @@ -4,7 +4,7 @@ .export _clrscr .import _screen - .import pushax, __bzero + .import pushax, ___bzero .include "extzp.inc" .code @@ -16,7 +16,7 @@ jsr pushax ldx #>(charsperline * screenrows) lda #<(charsperline * screenrows) - jmp __bzero + jmp ___bzero .endproc diff --git a/libsrc/atari7800/mono_clrscr.s b/libsrc/atari7800/mono_clrscr.s index 19f1fdfd6..f6f6735cb 100644 --- a/libsrc/atari7800/mono_clrscr.s +++ b/libsrc/atari7800/mono_clrscr.s @@ -4,7 +4,7 @@ .export _mono_clrscr .import _mono_screen - .import pushax, __bzero + .import pushax, ___bzero .include "extzp.inc" .code @@ -16,7 +16,7 @@ jsr pushax ldx #>(mono_charsperline * screenrows) lda #<(mono_charsperline * screenrows) - jmp __bzero + jmp ___bzero .endproc diff --git a/libsrc/common/calloc.s b/libsrc/common/calloc.s index 6e3e1bd3f..4cb701146 100644 --- a/libsrc/common/calloc.s +++ b/libsrc/common/calloc.s @@ -7,7 +7,7 @@ ; .export _calloc - .import _malloc, __bzero + .import _malloc, ___bzero .import tosumulax, pushax @@ -48,7 +48,7 @@ ClearBlock: jsr pushax ; ptr lda Size ldx Size+1 ; Size - jmp __bzero + jmp ___bzero .endproc diff --git a/libsrc/common/memset.s b/libsrc/common/memset.s index a57a90e5c..be78fc30d 100644 --- a/libsrc/common/memset.s +++ b/libsrc/common/memset.s @@ -1,6 +1,6 @@ ; ; void* __fastcall__ memset (void* ptr, int c, size_t n); -; void* __fastcall__ _bzero (void* ptr, size_t n); +; void* __fastcall__ __bzero (void* ptr, size_t n); ; void __fastcall__ bzero (void* ptr, size_t n); ; ; Ullrich von Bassewitz, 29.05.1998 @@ -8,19 +8,19 @@ ; Christian Krueger, 12.09.2009, slightly improved 12.01.2011 ; ; NOTE: bzero will return it's first argument as memset does. It is no problem -; to declare the return value as void, since it may be ignored. _bzero -; (note the leading underscore) is declared with the proper return type, -; because the compiler will replace memset by _bzero if the fill value +; to declare the return value as void, since it may be ignored. __bzero +; (note the leading underscores) is declared with the proper return type, +; because the compiler will replace memset by __bzero if the fill value ; is zero, and the optimizer looks at the return type to see if the value ; in a/x is of any use. ; - .export _memset, _bzero, __bzero + .export _memset, _bzero, ___bzero .import popax .importzp sp, ptr1, ptr2, ptr3 _bzero: -__bzero: +___bzero: sta ptr3 stx ptr3+1 ; Save n ldx #0 ; Fill with zeros diff --git a/libsrc/geos-common/common/memset.s b/libsrc/geos-common/common/memset.s index 2cb6732d1..82b7f4a87 100644 --- a/libsrc/geos-common/common/memset.s +++ b/libsrc/geos-common/common/memset.s @@ -1,14 +1,14 @@ ; ; void* memset (void* ptr, int c, size_t n); -; void* _bzero (void* ptr, size_t n); +; void* __bzero (void* ptr, size_t n); ; void bzero (void* ptr, size_t n); ; ; Maciej 'YTM/Elysium' Witkowiak, 20.08.2003 ; - .export _memset, _bzero, __bzero + .export _memset, _bzero, ___bzero .import _ClearRam, _FillRam _bzero = _ClearRam -__bzero = _ClearRam +___bzero = _ClearRam _memset = _FillRam diff --git a/libsrc/pce/memset.s b/libsrc/pce/memset.s index 45a78d533..1148e00c8 100644 --- a/libsrc/pce/memset.s +++ b/libsrc/pce/memset.s @@ -6,21 +6,21 @@ ; 1998-05-29, Ullrich von Bassewitz ; 2015-11-06, Greg King ; -; void* __fastcall__ _bzero (void* ptr, size_t n); +; void* __fastcall__ __bzero (void* ptr, size_t n); ; void __fastcall__ bzero (void* ptr, size_t n); ; void* __fastcall__ memset (void* ptr, int c, size_t n); ; ; NOTE: bzero() will return its first argument, as memset() does. It is no ; problem to declare the return value as void, because it can be ignored. -; _bzero() (note the leading underscore) is declared with the proper -; return type because the compiler will replace memset() by _bzero() if +; __bzero() (note the leading underscores) is declared with the proper +; return type because the compiler will replace memset() by __bzero() if ; the fill value is zero; and, the optimizer looks at the return type ; to see if the value in .XA is of any use. ; ; NOTE: This function uses entry points from "pce/memcpy.s"! ; - .export __bzero, _bzero, _memset + .export ___bzero, _bzero, _memset .import memcpy_getparams, memcpy_increment .import pushax, popax @@ -30,7 +30,7 @@ ; ---------------------------------------------------------------------- -__bzero: +___bzero: _bzero: pha cla ; fill with zeros jsr pushax ; (high byte isn't important) diff --git a/src/cc65/coptstop.c b/src/cc65/coptstop.c index 08f6c820e..4d0151c55 100644 --- a/src/cc65/coptstop.c +++ b/src/cc65/coptstop.c @@ -1329,7 +1329,7 @@ static unsigned Opt_a_tosult (StackOpData* D) /* The first column of these two tables must be sorted in lexical order */ static const OptFuncDesc FuncTable[] = { - { "__bzero", Opt___bzero, REG_NONE, OP_X_ZERO | OP_A_KNOWN }, + { "___bzero", Opt___bzero, REG_NONE, OP_X_ZERO | OP_A_KNOWN }, { "staspidx", Opt_staspidx, REG_NONE, OP_NONE }, { "staxspidx", Opt_staxspidx, REG_AX, OP_NONE }, { "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE }, diff --git a/src/cc65/stdfunc.c b/src/cc65/stdfunc.c index 5ed5ce671..fc19ce7f0 100644 --- a/src/cc65/stdfunc.c +++ b/src/cc65/stdfunc.c @@ -604,7 +604,7 @@ static void StdFunc_memset (FuncDesc* F attribute ((unused)), ExprDesc* Expr) DoDeferred (SQP_KEEP_EAX, &Arg3.Expr); /* Emit the actual function call. This will also cleanup the stack. */ - g_call (CF_FIXARGC, MemSet? Func_memset : Func__bzero, ParamSize); + g_call (CF_FIXARGC, MemSet? Func_memset : Func___bzero, ParamSize); if (ED_IsConstAbsInt (&Arg3.Expr) && Arg3.Expr.IVal == 0) { diff --git a/src/cc65/stdnames.c b/src/cc65/stdnames.c index 687b53894..cdecd61a1 100644 --- a/src/cc65/stdnames.c +++ b/src/cc65/stdnames.c @@ -44,9 +44,9 @@ -const char Func__bzero[] = "_bzero"; /* Asm name of "_bzero" */ -const char Func_memcpy[] = "memcpy"; /* Asm name of "memcpy" */ -const char Func_memset[] = "memset"; /* Asm name of "memset" */ -const char Func_strcmp[] = "strcmp"; /* Asm name of "strcmp" */ -const char Func_strcpy[] = "strcpy"; /* Asm name of "strcpy" */ -const char Func_strlen[] = "strlen"; /* Asm name of "strlen" */ +const char Func___bzero[] = "__bzero"; /* C name of "__bzero" */ +const char Func_memcpy[] = "memcpy"; /* C name of "memcpy" */ +const char Func_memset[] = "memset"; /* C name of "memset" */ +const char Func_strcmp[] = "strcmp"; /* C name of "strcmp" */ +const char Func_strcpy[] = "strcpy"; /* C name of "strcpy" */ +const char Func_strlen[] = "strlen"; /* C name of "strlen" */ diff --git a/src/cc65/stdnames.h b/src/cc65/stdnames.h index dcb323924..e6497e4c3 100644 --- a/src/cc65/stdnames.h +++ b/src/cc65/stdnames.h @@ -44,12 +44,12 @@ -extern const char Func__bzero[]; /* Asm name of "_bzero" */ -extern const char Func_memcpy[]; /* Asm name of "memcpy" */ -extern const char Func_memset[]; /* Asm name of "memset" */ -extern const char Func_strcmp[]; /* Asm name of "strcmp" */ -extern const char Func_strcpy[]; /* Asm name of "strcpy" */ -extern const char Func_strlen[]; /* Asm name of "strlen" */ +extern const char Func___bzero[]; /* C name of "__bzero" */ +extern const char Func_memcpy[]; /* C name of "memcpy" */ +extern const char Func_memset[]; /* C name of "memset" */ +extern const char Func_strcmp[]; /* C name of "strcmp" */ +extern const char Func_strcpy[]; /* C name of "strcpy" */ +extern const char Func_strlen[]; /* C name of "strlen" */ diff --git a/test/val/bzero.c b/test/val/bzero.c new file mode 100644 index 000000000..4e7ed6ac1 --- /dev/null +++ b/test/val/bzero.c @@ -0,0 +1,51 @@ + +// test if memset and bzero work as expected after optimizations + +#include + +char s1[10] = { 1,2,3,4,5,6,7,8,9,10 }; +char r1[10] = { 0,0,0,0,0,6,7,8,9,10 }; + +char s2[10] = { 1,2,3,4,5,6,7,8,9,10 }; +char r2[10] = { 0,0,0,0,0,0,7,8,9,10 }; + +char s3[10] = { 1,2,3,4,5,6,7,8,9,10 }; +char r3[10] = { 0,0,0,0,0,0,0,8,9,10 }; + +char *p1, *p2, *p3; + +int res = 0; + +int main(void) +{ + /* regular bzero */ + bzero(s1, 5); + p1 = __AX__; /* this works because bzero jumps into memset */ + /* this gets converted to __bzero */ + p2 = memset(s2, 0, 6); + /* call internal __bzero (we should not do this in real code) */ + p3 = __bzero(s3, 7); + + /* check the results */ + if (memcmp(s1, r1, 10) != 0) { + res++; + } + if (memcmp(s2, r2, 10) != 0) { + res++; + } + if (memcmp(s3, r3, 10) != 0) { + res++; + } + + if (p1 != s1) { + res++; + } + if (p2 != s2) { + res++; + } + if (p3 != s3) { + res++; + } + + return res; +}