From 8c6756e7f21f1f7e04f7ee0b9710707b2d2ac618 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sat, 25 Jul 2020 20:00:44 +0200 Subject: [PATCH] mescc: Mes C Library: Add M2-Planet support. Mes can now be built with M2-Planet by running kaem --verbose --strict * include/linux/x86/syscall.h (SYS_getcwd, SYS_dup, SYS_dup2, SYS_unlilnk, SYS_gettimeofday, SYS_clock_gettime, SYS_time): Move to libc section. Add M2-Planet constants. * include/m2/lib.h (struct timezone, struct timespec): struct timeval): Define. Add M2-Planet flavor prototypes for mes libc. * include/mes/m2.h: Remove macros. * include/mes/mes.h (g_start_time, __gettimeofday_time, __get_internal_run_time_ts): Declare. * src/mes.c (init): Initialize them. * src/posix.c: Use them. * simple.make: Use them. Remove macro preprocessing for M2-Planet. * kaem.run: New file. --- include/linux/x86/syscall.h | 21 +++-- include/m2/lib.h | 29 +++++++ include/mes/m2.h | 85 ------------------ include/mes/mes.h | 3 + simple.make | 167 ++++++++++++++++++++++-------------- src/mes.c | 7 +- src/posix.c | 40 +++------ 7 files changed, 165 insertions(+), 187 deletions(-) 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/m2/lib.h b/include/m2/lib.h index f9ecfc78..d704eb8c 100644 --- a/include/m2/lib.h +++ b/include/m2/lib.h @@ -30,4 +30,33 @@ int errno; // CONSTANT EOF 0xffffffff // CONSTANT __FILEDES_MAX 512 +int __ungetc_p (int filedes); +int eputs (char *s); +int oputs (char *s); +int puts (char *s); +size_t strlen (char *s); +ssize_t _write (); +ssize_t write (int filedes, void *buffer, size_t size); +void __ungetc_clear (int filedes); +void __ungetc_init (); +void __ungetc_set (int filedes, int c); + +struct timezone +{ + int tz_minuteswest; + int tz_dsttime; +}; + +struct timespec +{ + long tv_sec; + long tv_nsec; +}; + +struct timeval +{ + long tv_sec; + long tv_usec; +}; + #endif /* __M2_LIB_H */ diff --git a/include/mes/m2.h b/include/mes/m2.h index 6d52e7a5..d3a4231c 100644 --- a/include/mes/m2.h +++ b/include/mes/m2.h @@ -21,89 +21,4 @@ #ifndef __MES_M2_H #define __MES_M2_H -char **environ; -int __stdin; -int __stdout; -int __stderr; - -int __ungetc_p (int filedes); -int eputs (char *s); -int oputs (char *s); -int puts (char *s); -size_t strlen (char *s); -ssize_t _write (); -ssize_t write (int filedes, void *buffer, size_t size); -void __ungetc_clear (int filedes); -void __ungetc_init (); -void __ungetc_set (int filedes, int c); - -/* Recieved SCM in program -struct timespec -{ - long tv_sec; - long tv_nsec; -}; - -struct timeval -{ - long tv_sec; - long tv_usec; -}; - -*/ - -#define struct_size 12 - -#if POINTER_CELLS - -#define CELL(x) (x) - -#else - -#define CELL(x) ((x*struct_size)+g_cells) - -#define TYPE(x) ((x*struct_size)+g_cells)->type -#define CAR(x) ((x*struct_size)+g_cells)->car -#define CDR(x) ((x*struct_size)+g_cells)->cdr - -#define NTYPE(x) ((x*struct_size)+g_news)->type -#define NCAR(x) ((x*struct_size)+g_news)->car -#define NCDR(x) ((x*struct_size)+g_news)->cdr - - -#define BYTES(x) ((x*struct_size)+g_cells)->car -#define LENGTH(x) ((x*struct_size)+g_cells)->car -#define MACRO(x) ((x*struct_size)+g_cells)->car -#define PORT(x) ((x*struct_size)+g_cells)->car -#define REF(x) ((x*struct_size)+g_cells)->car -#define VARIABLE(x) ((x*struct_size)+g_cells)->car - -#define CLOSURE(x) ((x*struct_size)+g_cells)->cdr -#define CONTINUATION(x) ((x*struct_size)+g_cells)->cdr - -#define CBYTES(x) (((x*struct_size)+g_cells) + 8) - -#define NAME(x) ((x*struct_size)+g_cells)->cdr -#define STRING(x) ((x*struct_size)+g_cells)->cdr -#define STRUCT(x) ((x*struct_size)+g_cells)->cdr -#define VALUE(x) ((x*struct_size)+g_cells)->cdr -#define VECTOR(x) ((x*struct_size)+g_cells)->cdr - -#define NLENGTH(x) ((x*struct_size)+g_news)->car - -#define NVALUE(x) ((x*struct_size)+g_news)->cdr -#define NSTRING(x) ((x*struct_size)+g_news)->cdr -#define NVECTOR(x) ((x*struct_size)+g_news)->cdr - -#define CAAR(x) CAR (CAR (x)) -#define CADR(x) CAR (CDR (x)) -#define CDAR(x) CDR (CAR (x)) -#define CDDR(x) CDR (CDR (x)) -#define CADAR(x) CAR (CDR (CAR (x))) -#define CADDR(x) CAR (CDR (CDR (x))) -#define CDADR(x) CDR (CAR (CDR (x))) -#define CDDAR(x) CDR (CDR (CAR (x))) - -#endif - #endif /* __MES_M2_H */ diff --git a/include/mes/mes.h b/include/mes/mes.h index 53ee23f0..de395bfa 100644 --- a/include/mes/mes.h +++ b/include/mes/mes.h @@ -102,6 +102,9 @@ char *__open_boot_buf; char *__open_boot_file_name; char *__setenv_buf; char *__reader_read_char_buf; +struct timespec *g_start_time; +struct timeval *__gettimeofday_time; +struct timespec *__get_internal_run_time_ts; SCM alloc (long n); SCM apply (SCM f, SCM x, SCM a); diff --git a/simple.make b/simple.make index e55aaaf1..2f5778b2 100644 --- a/simple.make +++ b/simple.make @@ -28,13 +28,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"' \ @@ -65,12 +71,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 \ @@ -78,7 +138,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 \ @@ -95,83 +154,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/mes.c b/src/mes.c index 2590f37a..cabcedd8 100644 --- a/src/mes.c +++ b/src/mes.c @@ -25,7 +25,8 @@ #include #include #include -#include +#include +#include // char const *MES_PKGDATADIR = "mes"; @@ -163,11 +164,15 @@ init (char **envp) environ = envp; __execl_c_argv = malloc (1024 * sizeof (char *)); /* POSIX minimum: 4096 */ __getcwd_buf = malloc (PATH_MAX); + __gettimeofday_time = malloc (sizeof (struct timeval)); + __get_internal_run_time_ts = malloc (sizeof (struct timespec)); __open_boot_buf = malloc (PATH_MAX); __open_boot_file_name = malloc (PATH_MAX); __reader_read_char_buf = malloc (10); __setenv_buf = malloc (1024); g_datadir = malloc (1024); + g_start_time = malloc (sizeof (struct timespec)); + memset (g_start_time, 0, sizeof (struct timespec)); char *p; if (p = getenv ("MES_DEBUG")) diff --git a/src/posix.c b/src/posix.c index e98ad335..40bf0eed 100644 --- a/src/posix.c +++ b/src/posix.c @@ -357,30 +357,18 @@ waitpid_ (SCM pid, SCM options) #if __x86_64__ /* Nanoseconds on 64-bit systems with POSIX timers. */ +// CONSTANT TIME_UNITS_PER_SECOND 1000000000 #define TIME_UNITS_PER_SECOND 1000000000 #else /* Milliseconds for everyone else. */ +// CONSTANT TIME_UNITS_PER_SECOND 1000 #define TIME_UNITS_PER_SECOND 1000 #endif -#if __M2_PLANET -struct timespec -{ - long tv_sec; - long tv_nsec; -}; -struct timeval -{ - long tv_sec; - long tv_usec; -}; -#endif - -struct timespec g_start_time; SCM init_time (SCM a) /*:((internal)) */ { - clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &g_start_time); + clock_gettime (CLOCK_PROCESS_CPUTIME_ID, g_start_time); a = acons (cell_symbol_internal_time_units_per_second, make_number (TIME_UNITS_PER_SECOND), a); } @@ -393,13 +381,9 @@ current_time () SCM gettimeofday_ () /*:((name . "gettimeofday")) */ { -#if __M2_PLANET__ - return make_number (0); -#else - struct timeval time; - gettimeofday (&time, 0); - return cons (make_number (time.tv_sec), make_number (time.tv_usec)); -#endif + struct timeval *time = __gettimeofday_time; + gettimeofday (time, 0); + return cons (make_number (time->tv_sec), make_number (time->tv_usec)); } long @@ -411,15 +395,11 @@ seconds_and_nanoseconds_to_long (long s, long ns) SCM get_internal_run_time () { -#if __M2_PLANET__ - return make_number (0); -#else - struct timespec ts; - clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts); - long time = seconds_and_nanoseconds_to_long (ts.tv_sec - g_start_time.tv_sec, - ts.tv_nsec - g_start_time.tv_nsec); + struct timespec *ts = __get_internal_run_time_ts; + clock_gettime (CLOCK_PROCESS_CPUTIME_ID, ts); + long time = seconds_and_nanoseconds_to_long (ts->tv_sec - g_start_time->tv_sec, + ts->tv_nsec - g_start_time->tv_nsec); return make_number (time); -#endif } SCM