From 6c2700aa599183974a5a58b55efd3736248812f6 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 20 Oct 2019 19:25:25 +0200 Subject: [PATCH] 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. --- include/linux/x86/syscall.h | 21 +++-- include/mes/m2.h | 9 +- kaem.run | 134 +++++++++++++++++++++++++++++ lib/linux/_getcwd.c | 2 +- lib/linux/access.c | 4 +- lib/linux/chmod.c | 4 +- lib/linux/clock_gettime.c | 4 +- lib/linux/dup.c | 3 +- lib/linux/dup2.c | 4 +- lib/linux/gettimeofday.c | 4 +- lib/linux/read.c | 3 +- lib/linux/unlink.c | 2 +- lib/m2/abtol.c | 55 ++++++++++++ lib/m2/chmod.c | 31 +++++++ lib/m2/clock_gettime.c | 30 +++++++ lib/m2/execv.c | 25 ++++++ lib/m2/execve.c | 31 +++++++ lib/m2/exit.c | 29 +++++++ lib/m2/getcwd.c | 37 ++++++++ lib/m2/ioctl.c | 34 ++++++++ lib/m2/isatty.c | 54 ++++++++++++ lib/m2/ntoab.c | 69 +++++++++++++++ lib/m2/strncmp.c | 35 ++++++++ lib/m2/time.c | 29 +++++++ lib/m2/waitpid.c | 38 ++++++++ lib/mes/fdgetc.c | 1 + lib/mes/fdputc.c | 3 +- lib/posix/getenv.c | 12 ++- lib/posix/setenv.c | 10 ++- lib/stdlib/realloc.c | 2 +- lib/string/memcmp.c | 3 +- lib/string/memcpy.c | 12 ++- simple.make | 167 ++++++++++++++++++++++-------------- src/posix.c | 3 +- 34 files changed, 808 insertions(+), 96 deletions(-) create mode 100644 kaem.run create mode 100644 lib/m2/abtol.c create mode 100644 lib/m2/chmod.c create mode 100644 lib/m2/clock_gettime.c create mode 100644 lib/m2/execv.c create mode 100644 lib/m2/execve.c create mode 100644 lib/m2/exit.c create mode 100644 lib/m2/getcwd.c create mode 100644 lib/m2/ioctl.c create mode 100644 lib/m2/isatty.c create mode 100644 lib/m2/ntoab.c create mode 100644 lib/m2/strncmp.c create mode 100644 lib/m2/time.c create mode 100644 lib/m2/waitpid.c diff --git a/include/linux/x86/syscall.h b/include/linux/x86/syscall.h index 346548cf..01c46a35 100644 --- a/include/linux/x86/syscall.h +++ b/include/linux/x86/syscall.h @@ -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 diff --git a/include/mes/m2.h b/include/mes/m2.h index 6d52e7a5..061161b3 100644 --- a/include/mes/m2.h +++ b/include/mes/m2.h @@ -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 diff --git a/kaem.run b/kaem.run new file mode 100644 index 00000000..861b9496 --- /dev/null +++ b/kaem.run @@ -0,0 +1,134 @@ +#!/bin/bash +# Copyright (C) 2019 Jeremiah Orians +# 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 . + +# Usage: +# kaem --verbose --strict +# bin/mes-m2 --boot scaffold/boot/14-exit.scm +# bin/mes-m2 --boot scaffold/boot/15-display.scm + +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 --boot scaffold/boot/14-exit.scm +#./bin/mes-m2 --boot scaffold/boot/15-display.scm diff --git a/lib/linux/_getcwd.c b/lib/linux/_getcwd.c index 55db9a02..699edb3b 100644 --- a/lib/linux/_getcwd.c +++ b/lib/linux/_getcwd.c @@ -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; diff --git a/lib/linux/access.c b/lib/linux/access.c index 596697c7..6938ba22 100644 --- a/lib/linux/access.c +++ b/lib/linux/access.c @@ -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); } diff --git a/lib/linux/chmod.c b/lib/linux/chmod.c index 235b3456..54509b53 100644 --- a/lib/linux/chmod.c +++ b/lib/linux/chmod.c @@ -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); } diff --git a/lib/linux/clock_gettime.c b/lib/linux/clock_gettime.c index 2b090650..d04000dc 100644 --- a/lib/linux/clock_gettime.c +++ b/lib/linux/clock_gettime.c @@ -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); } diff --git a/lib/linux/dup.c b/lib/linux/dup.c index 02d01283..9237ef97 100644 --- a/lib/linux/dup.c +++ b/lib/linux/dup.c @@ -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); } diff --git a/lib/linux/dup2.c b/lib/linux/dup2.c index f61344f5..ef996273 100644 --- a/lib/linux/dup2.c +++ b/lib/linux/dup2.c @@ -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); } diff --git a/lib/linux/gettimeofday.c b/lib/linux/gettimeofday.c index fbdc35c3..7d888da6 100644 --- a/lib/linux/gettimeofday.c +++ b/lib/linux/gettimeofday.c @@ -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); } diff --git a/lib/linux/read.c b/lib/linux/read.c index 34e65949..efd25744 100644 --- a/lib/linux/read.c +++ b/lib/linux/read.c @@ -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) diff --git a/lib/linux/unlink.c b/lib/linux/unlink.c index 3889a067..4a6881fc 100644 --- a/lib/linux/unlink.c +++ b/lib/linux/unlink.c @@ -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); } diff --git a/lib/m2/abtol.c b/lib/m2/abtol.c new file mode 100644 index 00000000..48be4fc9 --- /dev/null +++ b/lib/m2/abtol.c @@ -0,0 +1,55 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include + +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; +} diff --git a/lib/m2/chmod.c b/lib/m2/chmod.c new file mode 100644 index 00000000..f5a9a662 --- /dev/null +++ b/lib/m2/chmod.c @@ -0,0 +1,31 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include + +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); +} diff --git a/lib/m2/clock_gettime.c b/lib/m2/clock_gettime.c new file mode 100644 index 00000000..3bcfdc37 --- /dev/null +++ b/lib/m2/clock_gettime.c @@ -0,0 +1,30 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include + +int +clock_gettime (long clk_id, struct timespec *tp) +{ + long long_tp = tp; + return _sys_call2 (SYS_clock_gettime, clk_id, tp); +} diff --git a/lib/m2/execv.c b/lib/m2/execv.c new file mode 100644 index 00000000..9f6a5508 --- /dev/null +++ b/lib/m2/execv.c @@ -0,0 +1,25 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +int +execv (char *file_name, char **argv) +{ + return execve (file_name, argv, environ); +} diff --git a/lib/m2/execve.c b/lib/m2/execve.c new file mode 100644 index 00000000..7fe7c9ba --- /dev/null +++ b/lib/m2/execve.c @@ -0,0 +1,31 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include + +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); +} diff --git a/lib/m2/exit.c b/lib/m2/exit.c new file mode 100644 index 00000000..2fa78612 --- /dev/null +++ b/lib/m2/exit.c @@ -0,0 +1,29 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include + +void _exit (int code); + +void +exit (int code) +{ + _exit (code); +} diff --git a/lib/m2/getcwd.c b/lib/m2/getcwd.c new file mode 100644 index 00000000..b078dde4 --- /dev/null +++ b/lib/m2/getcwd.c @@ -0,0 +1,37 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include +#include + +// 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); +} diff --git a/lib/m2/ioctl.c b/lib/m2/ioctl.c new file mode 100644 index 00000000..35654531 --- /dev/null +++ b/lib/m2/ioctl.c @@ -0,0 +1,34 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include +#include + +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; +} diff --git a/lib/m2/isatty.c b/lib/m2/isatty.c new file mode 100644 index 00000000..cf24b0be --- /dev/null +++ b/lib/m2/isatty.c @@ -0,0 +1,54 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include +#include +#include + +// 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; +} diff --git a/lib/m2/ntoab.c b/lib/m2/ntoab.c new file mode 100644 index 00000000..f2d2e3ad --- /dev/null +++ b/lib/m2/ntoab.c @@ -0,0 +1,69 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include + +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; +} diff --git a/lib/m2/strncmp.c b/lib/m2/strncmp.c new file mode 100644 index 00000000..b4270074 --- /dev/null +++ b/lib/m2/strncmp.c @@ -0,0 +1,35 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +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]; +} diff --git a/lib/m2/time.c b/lib/m2/time.c new file mode 100644 index 00000000..e8a911ae --- /dev/null +++ b/lib/m2/time.c @@ -0,0 +1,29 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include + +long +time (long *result) +{ + return _sys_call1 (SYS_time, result); +} diff --git a/lib/m2/waitpid.c b/lib/m2/waitpid.c new file mode 100644 index 00000000..3e2b8238 --- /dev/null +++ b/lib/m2/waitpid.c @@ -0,0 +1,38 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include + +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 +} diff --git a/lib/mes/fdgetc.c b/lib/mes/fdgetc.c index 0370c24e..43687ec4 100644 --- a/lib/mes/fdgetc.c +++ b/lib/mes/fdgetc.c @@ -26,6 +26,7 @@ #include #include +int errno; int *__ungetc_buf; int diff --git a/lib/mes/fdputc.c b/lib/mes/fdputc.c index 0e68195b..a06b6546 100644 --- a/lib/mes/fdputc.c +++ b/lib/mes/fdputc.c @@ -23,6 +23,7 @@ int fdputc (int c, int fd) { - write (fd, (char *) &c, 1); + char *p = &c; + write (fd, p, 1); return 0; } diff --git a/lib/posix/getenv.c b/lib/posix/getenv.c index 6848399c..cf630e25 100644 --- a/lib/posix/getenv.c +++ b/lib/posix/getenv.c @@ -1,4 +1,4 @@ -/* -*-comment-start: "//";comment-end:""-*- +/* * GNU Mes --- Maxwell Equations of Software * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen * @@ -22,21 +22,29 @@ #include #include +// 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; diff --git a/lib/posix/setenv.c b/lib/posix/setenv.c index 5e695278..a5ee8ff8 100644 --- a/lib/posix/setenv.c +++ b/lib/posix/setenv.c @@ -22,12 +22,16 @@ #include #include +// 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; diff --git a/lib/stdlib/realloc.c b/lib/stdlib/realloc.c index f48df4fc..bd229578 100644 --- a/lib/stdlib/realloc.c +++ b/lib/stdlib/realloc.c @@ -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); diff --git a/lib/string/memcmp.c b/lib/string/memcmp.c index e0f6ea56..4f33e1c3 100644 --- a/lib/string/memcmp.c +++ b/lib/string/memcmp.c @@ -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; diff --git a/lib/string/memcpy.c b/lib/string/memcpy.c index 623b730e..3295d3f7 100644 --- a/lib/string/memcpy.c +++ b/lib/string/memcpy.c @@ -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 * diff --git a/simple.make b/simple.make index 05ee0178..a2086339 100644 --- a/simple.make +++ b/simple.make @@ -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 diff --git a/src/posix.c b/src/posix.c index 7309a8ff..d3653820 100644 --- a/src/posix.c +++ b/src/posix.c @@ -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; }