mescc: Mes C Library: Use casting functions.

Silence all casting errors by using casting functions.

* lib/mes/cast.c: New file.
* build-aux/configure-lib.sh (libmes_SOURCES): Add it.
* lib/m2/cast.c: New file.
* kaem.run: Add it.
* simple.make: Add them both.
* include/mes/lib.h: Add cast prototypes.
* include/m2/lib.h: Likewise.
* lib/linux/_getcwd.c (_getcwd): Use them.
* lib/linux/access.c (access): Likewise.
* lib/linux/brk.c (brk): Likewise.
* lib/linux/chmod.c (chmod): Likewise.
* lib/linux/clock_gettime.c (clock_gettime): Likewise.
* lib/linux/gettimeofday.c (gettimeofday): Likewise.
* lib/linux/unlink.c (unlink): Likewise.
* lib/mes/fdputc.c (fdputc): Likewise.
* lib/stdio/putchar.c (putchar): Likewise.
* lib/linux/malloc.c (malloc): Likewise.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2020-10-18 14:55:24 +02:00
parent 3b29abc850
commit ca8e9f0342
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
17 changed files with 163 additions and 22 deletions

View File

@ -93,6 +93,7 @@ libmes_SOURCES="
$libc_mini_shared_SOURCES
lib/ctype/isnumber.c
lib/mes/abtol.c
lib/mes/cast.c
lib/mes/eputc.c
lib/mes/fdgetc.c
lib/mes/fdputc.c

View File

@ -30,6 +30,12 @@ int errno;
// CONSTANT EOF 0xffffffff
// CONSTANT __FILEDES_MAX 512
char* cast_intp_to_charp (int *i);
char* cast_long_to_charp (long i);
long cast_charp_to_long (char const *);
long cast_int_to_long (int i);
long cast_voidp_to_long (void const *);
char *itoa (int number);
char *ltoa (long number);
int __ungetc_p (int filedes);

View File

@ -23,6 +23,18 @@
#include <mes/lib-mini.h>
char* cast_intp_to_charp (int const *i);
char* cast_long_to_charp (long i);
long cast_charp_to_long (char const *);
long cast_int_to_long (int i);
long cast_voidp_to_long (void const *);
// #define cast_intp_to_charp(x) ((char*) x)
// #define cast_long_to_charp(x) ((char*) x)
// #define cast_charp_to_long(x) ((long) x)
// #define cast_int_to_long(x) ((long) x)
// #define cast_voidp_to_long(x) ((long) x)
int __mes_debug ();
void __ungetc_init ();
void __ungetc_clear (int filedes);

View File

@ -30,6 +30,7 @@ M2-Planet \
-f lib/linux/${mes_cpu}-mes-m2/_exit.c \
-f lib/linux/${mes_cpu}-mes-m2/_write.c \
-f lib/mes/globals.c \
-f lib/m2/cast.c \
-f lib/m2/exit.c \
-f lib/mes/mini-write.c \
-f lib/linux/${mes_cpu}-mes-m2/syscall.c \

View File

@ -18,14 +18,15 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib-mini.h>
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
char *
_getcwd (char *buffer, size_t size)
{
int r = _sys_call2 (SYS_getcwd, buffer, size);
long long_buffer = cast_charp_to_long (buffer);
int r = _sys_call2 (SYS_getcwd, long_buffer, size);
if (r >= 0)
return buffer;
return 0;

View File

@ -18,13 +18,14 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
int
access (char const *file_name, int how)
{
long long_file_name = file_name;
long long_how = how;
long long_file_name = cast_charp_to_long (file_name);
long long_how = cast_int_to_long (how);
return _sys_call2 (SYS_access, long_file_name, long_how);
}

View File

@ -18,11 +18,13 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
long
brk (void *addr)
{
return _sys_call1 (SYS_brk, addr);
long long_addr = cast_voidp_to_long (addr);
return _sys_call1 (SYS_brk, long_addr);
}

View File

@ -18,6 +18,7 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
#include <sys/stat.h>
@ -25,7 +26,7 @@
int
chmod (char const *file_name, mode_t mask)
{
long long_file_name = file_name;
long long_mask = mask;
long long_file_name = cast_charp_to_long (file_name);
long long_mask = cast_int_to_long (mask);
return _sys_call2 (SYS_chmod, long_file_name, long_mask);
}

View File

@ -18,6 +18,7 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
#include <time.h>
@ -25,7 +26,7 @@
int
clock_gettime (clockid_t clk_id, struct timespec *tp)
{
long long_clk_id = clk_id;
long long_tp = tp;
return _sys_call2 (SYS_clock_gettime, clk_id, tp);
long long_clk_id = cast_int_to_long (clk_id);
long long_tp = cast_voidp_to_long (tp);
return _sys_call2 (SYS_clock_gettime, long_clk_id, long_tp);
}

View File

@ -18,6 +18,7 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
#include <sys/time.h>
@ -25,7 +26,7 @@
int
gettimeofday (struct timeval *tv, struct timezone *tz)
{
long long_tv = tv;
long long_tz = tz;
long long_tv = cast_voidp_to_long (tv);
long long_tz = cast_voidp_to_long (tz);
return _sys_call2 (SYS_gettimeofday, long_tv, long_tz);
}

View File

@ -30,7 +30,7 @@ void *
malloc (size_t size)
{
if (!__brk)
__brk = (char *) brk (0);
__brk = cast_long_to_charp (brk (0));
/* align what we give back. */
__brk = (char*) (((uintptr_t) __brk
+ sizeof (max_align_t) - 1) & -sizeof (max_align_t));

View File

@ -18,11 +18,13 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <linux/syscall.h>
#include <syscall.h>
int
unlink (char const *file_name)
{
return _sys_call1 (SYS_unlink, file_name);
long long_file_name = cast_charp_to_long (file_name);
return _sys_call1 (SYS_unlink, long_file_name);
}

57
lib/m2/cast.c Normal file
View File

@ -0,0 +1,57 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#undef cast_intp_to_charp
#undef cast_long_to_charp
#undef cast_charp_to_long
#undef cast_int_to_long
#undef cast_voidp_to_long
char*
cast_intp_to_charp (int const *i)
{
return i;
}
char*
cast_long_to_charp (long i)
{
return i;
}
long
cast_charp_to_long (char const *i)
{
return i;
}
long
cast_int_to_long (int i)
{
return i;
}
long
cast_voidp_to_long (void const *i)
{
return i;
}

57
lib/mes/cast.c Normal file
View File

@ -0,0 +1,57 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#undef cast_intp_to_charp
#undef cast_long_to_charp
#undef cast_charp_to_long
#undef cast_int_to_long
#undef cast_voidp_to_long
char*
cast_intp_to_charp (int const *i)
{
return (char*)i;
}
char*
cast_long_to_charp (long i)
{
return (char*)i;
}
long
cast_charp_to_long (char const *i)
{
return (long)i;
}
long
cast_int_to_long (int i)
{
return (long)i;
}
long
cast_voidp_to_long (void const *i)
{
return (long)i;
}

View File

@ -23,7 +23,7 @@
int
fdputc (int c, int fd)
{
char *p = &c;
char *p = cast_intp_to_charp (&c);
write (fd, p, 1);
return 0;
}

View File

@ -18,12 +18,13 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mes/lib.h>
#include <stdio.h>
int
putchar (int c)
{
char *p = &c;
char *p = cast_intp_to_charp (&c);
write (__stdout, p, 1);
return 0;
}

View File

@ -44,12 +44,7 @@ CFLAGS:= \
-D 'MES_VERSION="git"' \
-D 'MES_PKGDATADIR="/usr/local/share/mes"' \
-I include \
-fno-builtin \
-Wno-discarded-qualifiers \
-Wno-discarded-array-qualifiers \
-Wno-ignored-qualifiers \
-Wno-incompatible-pointer-types \
-Wno-int-conversion
-fno-builtin
LIBMES_SOURCES = \
src/builtins.c \
@ -81,6 +76,7 @@ M2_SOURCES = \
lib/linux/x86-mes-m2/crt1.c \
lib/linux/x86-mes-m2/_exit.c \
lib/linux/x86-mes-m2/_write.c \
lib/m2/cast.c \
lib/m2/exit.c \
lib/mes/write.c \
lib/linux/x86-mes-m2/syscall.c \
@ -162,6 +158,7 @@ MES_LIBC = \
GCC_SOURCES = \
lib/mes/__mes_debug.c \
lib/mes/cast.c \
lib/mes/eputc.c \
lib/mes/eputs.c \
lib/mes/fdgetc.c \