1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 07:29:33 +00:00

add extra underscore to _bzero, add a test for bzero/memset

This commit is contained in:
mrdudz 2022-08-29 23:20:48 +02:00
parent 3b03a96375
commit 5bda57de87
12 changed files with 86 additions and 35 deletions

View File

@ -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 /* The following is an internal function, the compiler will replace memset
** with it if the fill value is zero. Never use this one directly! ** 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: */ /* Non standard: */
#if __CC65_STD__ == __CC65_STD_CC65__ #if __CC65_STD__ == __CC65_STD_CC65__

View File

@ -4,7 +4,7 @@
.export _clrscr .export _clrscr
.import _screen .import _screen
.import pushax, __bzero .import pushax, ___bzero
.include "extzp.inc" .include "extzp.inc"
.code .code
@ -16,7 +16,7 @@
jsr pushax jsr pushax
ldx #>(charsperline * screenrows) ldx #>(charsperline * screenrows)
lda #<(charsperline * screenrows) lda #<(charsperline * screenrows)
jmp __bzero jmp ___bzero
.endproc .endproc

View File

@ -4,7 +4,7 @@
.export _mono_clrscr .export _mono_clrscr
.import _mono_screen .import _mono_screen
.import pushax, __bzero .import pushax, ___bzero
.include "extzp.inc" .include "extzp.inc"
.code .code
@ -16,7 +16,7 @@
jsr pushax jsr pushax
ldx #>(mono_charsperline * screenrows) ldx #>(mono_charsperline * screenrows)
lda #<(mono_charsperline * screenrows) lda #<(mono_charsperline * screenrows)
jmp __bzero jmp ___bzero
.endproc .endproc

View File

@ -7,7 +7,7 @@
; ;
.export _calloc .export _calloc
.import _malloc, __bzero .import _malloc, ___bzero
.import tosumulax, pushax .import tosumulax, pushax
@ -48,7 +48,7 @@ ClearBlock:
jsr pushax ; ptr jsr pushax ; ptr
lda Size lda Size
ldx Size+1 ; Size ldx Size+1 ; Size
jmp __bzero jmp ___bzero
.endproc .endproc

View File

@ -1,6 +1,6 @@
; ;
; void* __fastcall__ memset (void* ptr, int c, size_t n); ; 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); ; void __fastcall__ bzero (void* ptr, size_t n);
; ;
; Ullrich von Bassewitz, 29.05.1998 ; Ullrich von Bassewitz, 29.05.1998
@ -8,19 +8,19 @@
; Christian Krueger, 12.09.2009, slightly improved 12.01.2011 ; 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 ; 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 ; to declare the return value as void, since it may be ignored. __bzero
; (note the leading underscore) is declared with the proper return type, ; (note the leading underscores) is declared with the proper return type,
; because the compiler will replace memset by _bzero if the fill value ; 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 ; is zero, and the optimizer looks at the return type to see if the value
; in a/x is of any use. ; in a/x is of any use.
; ;
.export _memset, _bzero, __bzero .export _memset, _bzero, ___bzero
.import popax .import popax
.importzp sp, ptr1, ptr2, ptr3 .importzp sp, ptr1, ptr2, ptr3
_bzero: _bzero:
__bzero: ___bzero:
sta ptr3 sta ptr3
stx ptr3+1 ; Save n stx ptr3+1 ; Save n
ldx #0 ; Fill with zeros ldx #0 ; Fill with zeros

View File

@ -1,14 +1,14 @@
; ;
; void* memset (void* ptr, int c, size_t n); ; 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); ; void bzero (void* ptr, size_t n);
; ;
; Maciej 'YTM/Elysium' Witkowiak, 20.08.2003 ; Maciej 'YTM/Elysium' Witkowiak, 20.08.2003
; ;
.export _memset, _bzero, __bzero .export _memset, _bzero, ___bzero
.import _ClearRam, _FillRam .import _ClearRam, _FillRam
_bzero = _ClearRam _bzero = _ClearRam
__bzero = _ClearRam ___bzero = _ClearRam
_memset = _FillRam _memset = _FillRam

View File

@ -6,21 +6,21 @@
; 1998-05-29, Ullrich von Bassewitz ; 1998-05-29, Ullrich von Bassewitz
; 2015-11-06, Greg King ; 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__ bzero (void* ptr, size_t n);
; void* __fastcall__ memset (void* ptr, int c, 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 ; 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. ; problem to declare the return value as void, because it can be ignored.
; _bzero() (note the leading underscore) is declared with the proper ; __bzero() (note the leading underscores) is declared with the proper
; return type because the compiler will replace memset() by _bzero() if ; return type because the compiler will replace memset() by __bzero() if
; the fill value is zero; and, the optimizer looks at the return type ; the fill value is zero; and, the optimizer looks at the return type
; to see if the value in .XA is of any use. ; to see if the value in .XA is of any use.
; ;
; NOTE: This function uses entry points from "pce/memcpy.s"! ; NOTE: This function uses entry points from "pce/memcpy.s"!
; ;
.export __bzero, _bzero, _memset .export ___bzero, _bzero, _memset
.import memcpy_getparams, memcpy_increment .import memcpy_getparams, memcpy_increment
.import pushax, popax .import pushax, popax
@ -30,7 +30,7 @@
; ---------------------------------------------------------------------- ; ----------------------------------------------------------------------
__bzero: ___bzero:
_bzero: pha _bzero: pha
cla ; fill with zeros cla ; fill with zeros
jsr pushax ; (high byte isn't important) jsr pushax ; (high byte isn't important)

View File

@ -1329,7 +1329,7 @@ static unsigned Opt_a_tosult (StackOpData* D)
/* The first column of these two tables must be sorted in lexical order */ /* The first column of these two tables must be sorted in lexical order */
static const OptFuncDesc FuncTable[] = { 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 }, { "staspidx", Opt_staspidx, REG_NONE, OP_NONE },
{ "staxspidx", Opt_staxspidx, REG_AX, OP_NONE }, { "staxspidx", Opt_staxspidx, REG_AX, OP_NONE },
{ "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE }, { "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE },

View File

@ -604,7 +604,7 @@ static void StdFunc_memset (FuncDesc* F attribute ((unused)), ExprDesc* Expr)
DoDeferred (SQP_KEEP_EAX, &Arg3.Expr); DoDeferred (SQP_KEEP_EAX, &Arg3.Expr);
/* Emit the actual function call. This will also cleanup the stack. */ /* 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) { if (ED_IsConstAbsInt (&Arg3.Expr) && Arg3.Expr.IVal == 0) {

View File

@ -44,9 +44,9 @@
const char Func__bzero[] = "_bzero"; /* Asm name of "_bzero" */ const char Func___bzero[] = "__bzero"; /* C name of "__bzero" */
const char Func_memcpy[] = "memcpy"; /* Asm name of "memcpy" */ const char Func_memcpy[] = "memcpy"; /* C name of "memcpy" */
const char Func_memset[] = "memset"; /* Asm name of "memset" */ const char Func_memset[] = "memset"; /* C name of "memset" */
const char Func_strcmp[] = "strcmp"; /* Asm name of "strcmp" */ const char Func_strcmp[] = "strcmp"; /* C name of "strcmp" */
const char Func_strcpy[] = "strcpy"; /* Asm name of "strcpy" */ const char Func_strcpy[] = "strcpy"; /* C name of "strcpy" */
const char Func_strlen[] = "strlen"; /* Asm name of "strlen" */ const char Func_strlen[] = "strlen"; /* C name of "strlen" */

View File

@ -44,12 +44,12 @@
extern const char Func__bzero[]; /* Asm name of "_bzero" */ extern const char Func___bzero[]; /* C name of "__bzero" */
extern const char Func_memcpy[]; /* Asm name of "memcpy" */ extern const char Func_memcpy[]; /* C name of "memcpy" */
extern const char Func_memset[]; /* Asm name of "memset" */ extern const char Func_memset[]; /* C name of "memset" */
extern const char Func_strcmp[]; /* Asm name of "strcmp" */ extern const char Func_strcmp[]; /* C name of "strcmp" */
extern const char Func_strcpy[]; /* Asm name of "strcpy" */ extern const char Func_strcpy[]; /* C name of "strcpy" */
extern const char Func_strlen[]; /* Asm name of "strlen" */ extern const char Func_strlen[]; /* C name of "strlen" */

51
test/val/bzero.c Normal file
View File

@ -0,0 +1,51 @@
// test if memset and bzero work as expected after optimizations
#include <string.h>
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;
}