mescc: Mes C Library: Add M2-Planet support. WIP

* kaem.run: New file.
* simple.make: Update.
* lib/m2/abtol.c: New file.
* lib/m2/access.c: New file.
* lib/m2/atoi.c: New file.
* lib/m2/chmod.c: New file.
* lib/m2/clock_gettime.c: New file.
* lib/m2/dup.c: New file.
* lib/m2/dup2.c: New file.
* lib/m2/eputs.c: New file.
* lib/m2/execv.c: New file.
* lib/m2/execve.c: New file.
* lib/m2/exit.c: New file.
* lib/m2/fdputc.c: New file.
* lib/m2/fork.c: New file.
* lib/m2/getcwd.c: New file.
* lib/m2/getenv.c: New file.
* lib/m2/isatty.c: New file.
* lib/m2/memchr.c: New file.
* lib/m2/memcmp.c: New file.
* lib/m2/memcpy.c: New file.
* lib/m2/memset.c: New file.
* lib/m2/mes_open.c: New file.
* lib/m2/ntoab.c: New file.
* lib/m2/open.c: New file.
* lib/m2/read.c: New file.
* lib/m2/setenv.c: New file.
* lib/m2/strcmp.c: New file.
* lib/m2/strcpy.c: New file.
* lib/m2/strlen.c: New file.
* lib/m2/strncmp.c: New file.
* lib/m2/time.c: New file.
* lib/m2/unlink.c: New file.
* lib/m2/waitpid.c: New file.
* lib/m2/write.c: New file.
This commit is contained in:
Jan Nieuwenhuizen 2019-10-20 19:25:25 +02:00
parent 325f9e7377
commit 316e7a2230
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
34 changed files with 812 additions and 96 deletions

View File

@ -53,16 +53,26 @@
#define SYS_ioctl 0x36
// CONSTANT SYS_fsync 0x76
#define SYS_fsync 0x76
// CONSTANT SYS_getcwd 0xb7
#define SYS_getcwd 0xb7
// CONSTANT SYS_dup 0x29
#define SYS_dup 0x29
// CONSTANT SYS_dup2 0x3f
#define SYS_dup2 0x3f
// CONSTANT SYS_unlink 0x0a
#define SYS_unlink 0x0a
// CONSTANT SYS_gettimeofday 0x4e
#define SYS_gettimeofday 0x4e
// CONSTANT SYS_clock_gettime 0x109
#define SYS_clock_gettime 0x109
// CONSTANT SYS_time 0x0d
#define SYS_time 0x0d
/* libc+tcc */
#define SYS_close 0x06
#define SYS_time 0x0d
#define SYS_lseek 0x13
#define SYS_unlink 0x0a
#define SYS_rmdir 0x28
#define SYS_gettimeofday 0x4e
#define SYS_stat 0x6a
#define SYS_getcwd 0xb7
/* libc+gnu */
@ -73,7 +83,6 @@
#define SYS_kill 0x25
#define SYS_rename 0x26
#define SYS_mkdir 0x27
#define SYS_dup 0x29
#define SYS_pipe 0x2a
#define SYS_getgid 0x2f
#define SYS_signal 0x30
@ -81,14 +90,12 @@
#define SYS_rt_sigaction 0xae
#define SYS_signal 0x30
#define SYS_fcntl 0x37
#define SYS_dup2 0x3f
#define SYS_getrusage 0x4d
#define SYS_lstat 0x6b
#define SYS_setitimer 0x68
#define SYS_fstat 0x6c
#define SYS_nanosleep 0xa2
#define SYS_getdents 0x8d
#define SYS_clock_gettime 0x109
/* bash */
#define SYS_setuid 0x17

View File

@ -37,7 +37,12 @@ void __ungetc_clear (int filedes);
void __ungetc_init ();
void __ungetc_set (int filedes, int c);
/* Recieved SCM in program
struct timezone
{
int tz_minuteswest;
int tz_dsttime;
};
struct timespec
{
long tv_sec;
@ -50,8 +55,6 @@ struct timeval
long tv_usec;
};
*/
#define struct_size 12
#if POINTER_CELLS

138
kaem.run Normal file
View File

@ -0,0 +1,138 @@
#!/bin/bash
# Copyright (C) 2019 Jeremiah Orians <jeremiah@pdp10.guru>
# Copyright (C) 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/>.
# Usage:
# kaem --verbose --strict
./build-aux/pointer.sh
./build-aux/pointer.sh
./build-aux/pointer.sh
./build-aux/pointer.sh
./build-aux/pointer.sh
M2-Planet \
--debug \
--architecture x86 \
-f include/m2/lib.h \
-f lib/linux/x86-mes-m2/crt1.c \
-f lib/linux/x86-mes-m2/mini.c \
-f lib/m2/exit.c \
-f lib/mes/write.c \
-f lib/linux/x86-mes-m2/syscall.c \
-f include/linux/x86/syscall.h \
-f lib/linux/brk.c \
-f lib/stdlib/malloc.c \
-f lib/string/memset.c \
-f lib/m2/read.c \
-f lib/mes/fdgetc.c \
-f lib/stdio/getchar.c \
-f lib/stdio/putchar.c \
-f lib/m2/open.c \
-f lib/m2/mes_open.c \
-f lib/string/strlen.c \
-f lib/mes/eputs.c \
-f lib/mes/fdputc.c \
-f lib/mes/eputc.c \
\
-f include/mes/mes.h \
-f include/mes/m2.h \
-f include/mes/builtins.h \
-f include/mes/constants.h \
-f include/mes/symbols.h \
\
-f lib/mes/__assert_fail.c \
-f lib/mes/assert_msg.c \
\
-f lib/mes/fdputc.c \
-f lib/string/strncmp.c \
-f lib/posix/getenv.c \
-f lib/mes/fdputs.c \
-f lib/m2/ntoab.c \
-f lib/ctype/isdigit.c \
-f lib/ctype/isxdigit.c \
-f lib/ctype/isspace.c \
-f lib/ctype/isnumber.c \
-f lib/mes/abtol.c \
-f lib/stdlib/atoi.c \
-f lib/string/memcpy.c \
-f lib/stdlib/free.c \
-f lib/stdlib/realloc.c \
-f lib/string/strcpy.c \
-f lib/mes/itoa.c \
-f lib/mes/fdungetc.c \
-f lib/posix/setenv.c \
-f lib/linux/access.c \
-f lib/m2/chmod.c \
-f lib/m2/ioctl.c \
-f lib/m2/isatty.c \
-f lib/linux/fork.c \
-f lib/m2/execve.c \
-f lib/m2/execv.c \
-f lib/m2/waitpid.c \
-f lib/linux/gettimeofday.c \
-f lib/m2/clock_gettime.c \
-f lib/m2/time.c \
-f lib/linux/_getcwd.c \
-f lib/m2/getcwd.c \
-f lib/linux/dup.c \
-f lib/linux/dup2.c \
-f lib/string/strcmp.c \
-f lib/string/memcmp.c \
-f lib/linux/unlink.c \
-f src/builtins.c \
-f src/core.c \
-f src/display.c \
-f src/eval-apply.c \
-f src/gc.c \
-f src/hash.c \
-f src/lib.c \
-f src/math.c \
-f src/mes.c \
-f src/module.c \
-f src/posix.c \
-f src/reader.c \
-f src/stack.c \
-f src/string.c \
-f src/struct.c \
-f src/symbol.c \
-f src/vector.c \
-o bin/mes.M1
blood-elf -f bin/mes.M1 -o bin/mes.blood-elf-M1
M1 \
--LittleEndian \
--architecture x86 \
-f lib/m2/x86/x86_defs.M1 \
-f lib/x86-mes/x86.M1 \
-f lib/linux/x86-mes-m2/crt1.M1 \
-f bin/mes.M1 \
-f bin/mes.blood-elf-M1 \
-o bin/mes.hex2
hex2 \
--LittleEndian \
--architecture x86 \
--BaseAddress 0x1000000 \
--exec_enable \
-f lib/x86-mes/elf32-header.hex2 \
-f bin/mes.hex2 \
-o bin/mes-m2
echo Running mes-m2
./bin/mes-m2 -c '(display "Hello Mes-M2\n")'

View File

@ -25,7 +25,7 @@
char *
_getcwd (char *buffer, size_t size)
{
int r = _sys_call2 (SYS_getcwd, (long) buffer, (long) size);
int r = _sys_call2 (SYS_getcwd, buffer, size);
if (r >= 0)
return buffer;
return 0;

View File

@ -24,5 +24,7 @@
int
access (char const *file_name, int how)
{
return _sys_call2 (SYS_access, (long) file_name, (int) how);
long long_file_name = file_name;
long long_how = how;
return _sys_call2 (SYS_access, long_file_name, long_how);
}

View File

@ -25,5 +25,7 @@
int
chmod (char const *file_name, mode_t mask)
{
return _sys_call2 (SYS_chmod, (long) file_name, (long) mask);
long long_file_name = file_name;
long long_mask = mask;
return _sys_call2 (SYS_chmod, long_file_name, long_mask);
}

View File

@ -25,5 +25,7 @@
int
clock_gettime (clockid_t clk_id, struct timespec *tp)
{
return _sys_call2 (SYS_clock_gettime, (long) clk_id, (long) tp);
long long_clk_id = clk_id;
long long_tp = tp;
return _sys_call2 (SYS_clock_gettime, clk_id, tp);
}

View File

@ -24,5 +24,6 @@
int
dup (int old)
{
return _sys_call1 (SYS_dup, (int) old);
long long_old = old;
return _sys_call1 (SYS_dup, long_old);
}

View File

@ -24,5 +24,7 @@
int
dup2 (int old, int new)
{
return _sys_call2 (SYS_dup2, (int) old, (int) new);
long long_old = old;
long long_new = new;
return _sys_call2 (SYS_dup2, long_old, long_new);
}

View File

@ -25,5 +25,7 @@
int
gettimeofday (struct timeval *tv, struct timezone *tz)
{
return _sys_call2 (SYS_gettimeofday, (long) tv, (long) tz);
long long_tv = tv;
long long_tz = tz;
return _sys_call2 (SYS_gettimeofday, long_tv, long_tz);
}

View File

@ -26,7 +26,8 @@
ssize_t
read (int filedes, void *buffer, size_t size)
{
ssize_t bytes = _sys_call3 (SYS_read, filedes, buffer, size);
long long_filedes = filedes;
ssize_t bytes = _sys_call3 (SYS_read, long_filedes, buffer, size);
if (__mes_debug () > 4)
{
if (bytes == 1)

View File

@ -24,5 +24,5 @@
int
unlink (char const *file_name)
{
return _sys_call1 (SYS_unlink, (long) file_name);
return _sys_call1 (SYS_unlink, file_name);
}

55
lib/m2/abtol.c Normal file
View File

@ -0,0 +1,55 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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>
#include <ctype.h>
long
abtol (char **p, int base)
{
char *s = p[0];
int i = 0;
int sign_p = 0;
if (base == 0)
base = 10;
while (isspace (s[0]) != 0)
s = s + 1;
if (s[0] != 0 && s[0] == '+')
s = s + 1;
if (s[0] != 0 && s[0] == '-')
{
sign_p = 1;
s = s + 1;
}
while (isnumber (s[0], base) != 0)
{
i = i * base;
int m = '0';
if (s[0] > '9')
m = 'a' - 10;
i = i + s[0] - m;
s = s + 1;
}
p[0] = s;
if (sign_p != 0)
return -i;
return i;
}

31
lib/m2/chmod.c Normal file
View File

@ -0,0 +1,31 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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 <linux/syscall.h>
#include <syscall.h>
#include <sys/stat.h>
int
chmod (char const *file_name, int mask)
{
long long_file_name = file_name;
long long_mask = mask;
return _sys_call2 (SYS_chmod, long_file_name, long_mask);
}

30
lib/m2/clock_gettime.c Normal file
View File

@ -0,0 +1,30 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2018,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 <linux/syscall.h>
#include <syscall.h>
#include <time.h>
int
clock_gettime (long clk_id, struct timespec *tp)
{
long long_tp = tp;
return _sys_call2 (SYS_clock_gettime, clk_id, tp);
}

25
lib/m2/execv.c Normal file
View File

@ -0,0 +1,25 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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/>.
*/
int
execv (char *file_name, char **argv)
{
return execve (file_name, argv, environ);
}

31
lib/m2/execve.c Normal file
View File

@ -0,0 +1,31 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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 <linux/syscall.h>
#include <syscall.h>
int
execve (char const *file_name, char **argv, char **env)
{
long long_file_name = file_name;
long long_argv = argv;
long long_env = env;
return _sys_call3 (SYS_execve, file_name, argv, env);
}

29
lib/m2/exit.c Normal file
View File

@ -0,0 +1,29 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 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>
void _exit (int code);
void
exit (int code)
{
_exit (code);
}

37
lib/m2/getcwd.c Normal file
View File

@ -0,0 +1,37 @@
/* -*-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>
#include <limits.h>
#include <sys/types.h>
#include <mes/lib.h>
// CONSTANT PATH_MAX 1024
char *
getcwd (char *buffer, int size)
{
if (__getcwd_buf == 0)
__getcwd_buf = malloc (PATH_MAX);
char *buf = __itoa_buf;
if (buffer != 0)
return _getcwd (buffer, size);
return _getcwd (buf, PATH_MAX);
}

34
lib/m2/ioctl.c Normal file
View File

@ -0,0 +1,34 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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 <linux/syscall.h>
#include <syscall.h>
#include <stdarg.h>
#include <sys/ioctl.h>
int
ioctl (int filedes, long command, int data)
{
long long_filedes = filedes;
long long_command = command;
long long_data = data;
int r = _sys_call3 (SYS_ioctl, long_filedes, long_command, long_data);
return r;
}

54
lib/m2/isatty.c Normal file
View File

@ -0,0 +1,54 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <termio.h>
// CONSTANT TCGETS 0x5401
#define TCGETS 0x5401
// CONSTANT NCCS 19
#define NCCS 19
struct ktermios
{
unsigned c_iflag;
unsigned c_oflag;
unsigned c_cflag;
unsigned c_lflag;
char c_line;
char c_cc[NCCS];
unsigned c_ispeed;
unsigned c_ospeed;
};
struct ktermios *__isatty_kernel_termios;
int
isatty (int filedes)
{
if (__isatty_kernel_termios == 0)
__isatty_kernel_termios = malloc (sizeof (struct ktermios));
int r = ioctl (filedes, TCGETS, __isatty_kernel_termios);
return r == 0;
}

69
lib/m2/ntoab.c Normal file
View File

@ -0,0 +1,69 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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>
#include <assert.h>
#include <string.h>
char *__itoa_buf;
char *
ntoab (long x, int base, int signed_p)
{
if (__itoa_buf == 0)
__itoa_buf = malloc (20);
char *buf = __itoa_buf;
char *p = buf + 11;
p[0] = 0;
p = p - 1;
assert_msg (base > 0, "base > 0");
int sign_p = 0;
unsigned u;
if (signed_p != 0 && x < 0)
{
sign_p = 1;
u = -x;
}
else
u = x;
do
{
unsigned i;
i = u % base;
u = u / base;
if (i > 9)
p[0] = 'a' + i - 10;
else
p[0] = '0' + i;
p = p - 1;
}
while (u != 0);
if (sign_p && p[1] != '0')
{
p[0] = '-';
p = p - 1;
}
return p + 1;
}

35
lib/m2/strncmp.c Normal file
View File

@ -0,0 +1,35 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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/>.
*/
int
strncmp (char const *a, char const *b, size_t size)
{
if (size == 0)
return 0;
while (a[0] != 0 && b[0] != 0 && a[0] == b[0] && size > 1)
{
size = size - 1;
a = a + 1;
b = b + 1;
}
return a[0] - b[0];
}

29
lib/m2/time.c Normal file
View File

@ -0,0 +1,29 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2018,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 <linux/syscall.h>
#include <syscall.h>
#include <time.h>
long
time (long *result)
{
return _sys_call1 (SYS_time, result);
}

38
lib/m2/waitpid.c Normal file
View File

@ -0,0 +1,38 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,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 <linux/syscall.h>
#include <syscall.h>
#include <sys/types.h>
int
waitpid (int pid, int *status_ptr, int options)
{
long long_pid = pid;
long long_status_ptr = status_ptr;
long long_options = options;
#if __i386__
return _sys_call3 (SYS_waitpid, long_pid, long_status_ptr, long_options);
#elif __x86_64__
return _sys_call4 (SYS_wait4, long_pid, long_status_ptr, long_options, 0);
#else
#error arch not supported
#endif
}

View File

@ -26,6 +26,7 @@
#include <sys/resource.h>
#include <unistd.h>
int errno;
int *__ungetc_buf;
int

View File

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

View File

@ -1,4 +1,4 @@
/* -*-comment-start: "//";comment-end:""-*-
/*
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
@ -22,21 +22,29 @@
#include <string.h>
#include <stdlib.h>
// CONSTANT M2_PTR_SIZE 4
#define M2_PTR_SIZE 1
char *
getenv (char const *s)
{
/* eputs ("\ngetenv s="); eputs (s); eputs ("\n"); */
char **p = environ;
int length = strlen (s);
while (p[0] != 0)
{
/* eputs ("getenv p[0]="); eputs (p[0]); eputs ("\n"); */
if (strncmp (s, p[0], length) == 0)
{
/* eputs ("found!\n"); */
char *q = p[0] + length;
if (q[0] == '=')
return q + 1;
}
p = p + 1;
/* else */
/* eputs ("not found!\n"); */
p = p + M2_PTR_SIZE;
}
return 0;

View File

@ -22,12 +22,16 @@
#include <string.h>
#include <stdlib.h>
// CONSTANT M2_PTR_SIZE 4
#define M2_PTR_SIZE 1
int
setenv (char const *s, char const *v, int overwrite_p)
{
char **p = environ;
int length = strlen (s);
while (p[0])
while (p[0] != 0)
{
if (strncmp (s, p[0], length) == 0)
{
@ -35,7 +39,7 @@ setenv (char const *s, char const *v, int overwrite_p)
if (q[0] == '=')
break;
}
p = p + 1;
p = p + M2_PTR_SIZE;
}
char *entry = malloc (length + strlen (v) + 2);
int end_p = p[0] == 0;
@ -44,7 +48,7 @@ setenv (char const *s, char const *v, int overwrite_p)
strcpy (entry + length, "=");
strcpy (entry + length + 1, v);
entry[length + strlen (v) + 2] = 0;
if (end_p)
if (end_p != 0)
p[1] = 0;
return 0;

View File

@ -25,7 +25,7 @@ void *
realloc (void *ptr, size_t size)
{
void *new = malloc (size);
if (ptr && new)
if (ptr != 0 && new != 0)
{
memcpy (new, ptr, size);
free (ptr);

View File

@ -23,8 +23,9 @@
int
memcmp (void const *s1, void const *s2, size_t size)
{
if (!size)
if (size == 0)
return 0;
char const *a = s1;
char const *b = s2;

View File

@ -25,12 +25,16 @@ char *
_memcpy (char *dest, char const *src, size_t n)
{
char *p = dest;
char const *q = src;
while (n--)
*p++ = *q++;
while (n != 0)
{
n = n - 1;
dest[0] = src[0];
dest = dest + 1;
src = src + 1;
}
return dest;
return p;
}
void *

View File

@ -26,13 +26,19 @@ MES = bin/mes-gcc
#MES_CPU = x86
M2_PLANET = M2-Planet
M2_PLANET_FLAGS = --architecture amd64
M2_PLANET_ARCH = x86
M2_PLANET_FUBAR = i386
#M2_PLANET_ARCH = amd64
#M2_PLANET_FUBAR = amd64
M2_PLANET_FLAGS = --debug --architecture $(M2_PLANET_ARCH)
CFLAGS:= \
$(CFLAGS) \
-D _GNU_SOURCE \
-D const= \
-ggdb \
-D POINTER_CELLS=0 \
-D SYSTEM_LIBC=1 \
-D 'MES_VERSION="git"' \
-D 'MES_PKGDATADIR="/usr/local/share/mes"' \
@ -63,12 +69,66 @@ MES_SOURCES = \
src/symbol.c \
src/vector.c
LIB_SOURCES = \
lib/mes/eputs.c \
lib/mes/assert_msg.c \
lib/mes/itoa.c
M2_SOURCES = \
lib/linux/x86-mes-m2/crt1.c \
lib/linux/x86-mes-m2/mini.c \
lib/m2/exit.c \
lib/mes/write.c \
lib/linux/x86-mes-m2/syscall.c \
lib/linux/brk.c \
lib/stdlib/malloc.c \
lib/string/memset.c \
\
lib/m2/read.c \
lib/mes/fdgetc.c \
lib/stdio/getchar.c \
lib/stdio/putchar.c \
lib/m2/open.c \
lib/m2/mes_open.c \
lib/string/strlen.c \
lib/mes/eputs.c \
lib/mes/fdputc.c \
lib/mes/eputc.c \
\
lib/mes/__assert_fail.c \
lib/mes/assert_msg.c \
\
lib/mes/fdputc.c \
lib/string/strncmp.c \
lib/posix/getenv.c \
lib/mes/fdputs.c \
lib/m2/ntoab.c \
lib/ctype/isdigit.c \
lib/ctype/isxdigit.c \
lib/ctype/isspace.c \
lib/ctype/isnumber.c \
lib/mes/abtol.c \
lib/stdlib/atoi.c \
lib/string/memcpy.c \
lib/stdlib/free.c \
lib/stdlib/realloc.c \
lib/string/strcpy.c \
lib/mes/itoa.c \
lib/mes/fdungetc.c \
lib/posix/setenv.c \
lib/linux/access.c \
lib/m2/chmod.c \
lib/m2/ioctl.c \
lib/m2/isatty.c \
lib/linux/fork.c \
lib/m2/execve.c \
lib/m2/execv.c \
lib/m2/waitpid.c \
lib/linux/gettimeofday.c \
lib/m2/clock_gettime.c \
lib/m2/time.c \
lib/linux/_getcwd.c \
lib/m2/getcwd.c \
lib/linux/dup.c \
lib/linux/dup2.c \
lib/string/strcmp.c \
lib/string/memcmp.c \
lib/linux/unlink.c
M2_TODO = \
lib/m2/file_print.c \
@ -76,7 +136,6 @@ M2_TODO = \
lib/mes/fdgetc.c \
lib/mes/fdungetc.c
SOURCES = $(M2_SOURCES) $(LIB_SOURCES) $(MES_SOURCES)
INCLUDES = \
include/mes/builtins.h \
include/mes/constants.h \
@ -93,83 +152,63 @@ MES_LIBC = \
GCC_SOURCES = \
lib/mes/__mes_debug.c \
lib/mes/eputc.c \
lib/mes/eputs.c \
lib/mes/fdgetc.c \
lib/mes/fdputc.c \
lib/mes/fdputs.c \
lib/mes/fdungetc.c \
lib/mes/mes_open.c \
lib/mes/ntoab.c \
$(SOURCES)
lib/mes/itoa.c \
lib/mes/assert_msg.c
mes-gcc: bin/mes-gcc
mes-m2: bin/mes-m2
bin/mes-gcc: $(MAKEFILES) $(GCC_SOURCES) $(INCLUDES) | bin
$(CC) $(CFLAGS) $(GCC_SOURCES) -o $@
bin/mes-gcc: simple.make $(GCC_SOURCES) $(MES_SOURCES) $(INCLUDES) | bin
$(CC) $(CFLAGS) $(GCC_SOURCES) $(MES_SOURCES) -o $@
M2_PLANET_INCLUDES = \
include/m2/lib.h \
include/linux/x86/syscall.h \
include/mes/mes.h \
include/mes/m2.h \
include/mes/builtins.h \
include/mes/constants.h \
include/mes/symbols.h
include/mes/symbols.h \
include/linux/$(M2_PLANET_ARCH)/syscall.h
M2_PLANET_PREFIX = ../M2-Planet
M2_PLANET_SOURCES = \
$(M2_PLANET_PREFIX)/test/common_amd64/functions/exit.c \
$(M2_PLANET_PREFIX)/test/common_amd64/functions/malloc.c \
$(M2_PLANET_PREFIX)/functions/calloc.c \
$(M2_PLANET_INCLUDES:%.h=%.h.m2) \
$(SOURCES:%.c=%.c.m2)
M2_PLANET_SOURCES = \
$(M2_PLANET_INCLUDES:%.h=%.h) \
$(M2_SOURCES)
%.h.m2: %.h $(MAKEFILES)
@sed -r \
-e 's,^//,@@,' \
-e 's@^(#include.*)@/* \1 */@' \
$< \
| $(CC) -E -I include \
-D __M2_PLANET__=1 \
-D FUNCTION0=FUNCTION \
-D FUNCTION1=FUNCTION \
-D FUNCTION2=FUNCTION \
-D FUNCTION3=FUNCTION \
-D FUNCTIONN=FUNCTION \
-D const= \
-D long=SCM \
-D size_t=SCM \
-D ssize_t=SCM \
-D unsigned=SCM \
-include mes/m2.h \
-x c - \
| sed -r \
-e 's,^@@,//,' \
> $@ \
bin/mes-m2.M1: simple.make $(M2_PLANET_SOURCES) $(MES_SOURCES) $(M2_PLANET_INCLUDES) | bin
$(M2_PLANET) $(M2_PLANET_FLAGS) $(M2_PLANET_SOURCES:%=-f %) $(MES_SOURCES:%.c=-f %.c) -o $@ || rm -f $@
%.c.m2: %.c $(MAKEFILES)
@sed -r \
-e 's,^//,@@,' \
-e 's@^(#include.*)@/* \1 */@' \
$< \
| $(CC) -E -I include \
-D __M2_PLANET__=1 \
-D FUNCTION0=FUNCTION \
-D FUNCTION1=FUNCTION \
-D FUNCTION2=FUNCTION \
-D FUNCTION3=FUNCTION \
-D FUNCTIONN=FUNCTION \
-D const= \
-D long=SCM \
-D size_t=SCM \
-D ssize_t=SCM \
-D unsigned=SCM \
-include mes/m2.h \
-x c - \
| sed -r \
-e 's,^@@,//,' \
> $@
bin/mes-m2.blood-elf.M1: bin/mes-m2.M1
# blood-elf --32 -f $< -o $@
blood-elf -f $< -o $@
bin/mes-m2: $(MAKEFILES) $(M2_PLANET_SOURCES) $(M2_PLANET_INCLUDES) | bin
$(M2_PLANET) $(M2_PLANET_FLAGS) $(M2_PLANET_SOURCES:%=-f %) -o $@ || rm -f $@
bin/mes-m2.hex2: bin/mes-m2.blood-elf.M1
M1 \
--LittleEndian \
--architecture $(M2_PLANET_ARCH) \
-f lib/m2/x86/x86_defs.M1 \
-f lib/x86-mes/x86.M1 \
-f lib/linux/x86-mes-m2/crt1.M1 \
-f bin/mes-m2.M1 \
-f bin/mes-m2.blood-elf.M1 \
-o $@
bin/mes-m2: bin/mes-m2.hex2
hex2 \
--LittleEndian \
--architecture $(M2_PLANET_ARCH) \
--BaseAddress 0x1000000 \
--exec_enable \
-f lib/x86-mes/elf32-header.hex2 \
-f bin/mes-m2.hex2 \
-o $@
# Clean up after ourselves
.PHONY: clean

View File

@ -189,7 +189,8 @@ setenv_ (SCM s, SCM v) /*:((name . "setenv")) */
SCM
access_p (SCM file_name, SCM mode)
{
if (access (cell_bytes (STRING (file_name)), VALUE (mode)) == 0)
int result = access (cell_bytes (STRING (file_name)), VALUE (mode));
if (result == 0)
return cell_t;
return cell_f;
}