From 46e0641e9c718fa39e3be4fd83d3fe53598e6c77 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 10 Aug 2018 17:05:01 +0200 Subject: [PATCH] mescc: Mes C Library: Add x86_64 libc-mini support. * lib/linux/x86-mes-gcc/mini.c: Rename from lib/linux/mini-gcc.c. * lib/linux/x86-mes/mini.c: Rename from lib/linux/mini-mes.c. * lib/linux/x86_64-mes-gcc/crt1.c: Rename from lib/linux/x86_64/crt1.c. * lib/linux/x86_64-mes-gcc/mini.c: New file. * build-aux/build-cc.sh: Build libc-mini, scaffold/main, scaffold/hello. * build-aux/cc64.sh: New file. --- .gitignore | 3 + HACKING | 3 + build-aux/build-cc.sh | 36 +++++++++++ build-aux/build-cc32.sh | 16 ++--- build-aux/cc64-mes.sh | 64 ++++++++++++++++++++ build-aux/config.sh | 28 +++++++++ build-aux/export.make | 8 +++ configure | 7 ++- lib/linux/libc-mini.c | 16 +++-- lib/linux/{mini-gcc.c => x86-mes-gcc/mini.c} | 2 +- lib/linux/{mini-mes.c => x86-mes/mini.c} | 0 lib/linux/x86_64-mes-gcc/crt1.c | 57 +++++++++++++++++ lib/linux/x86_64-mes-gcc/mini.c | 54 +++++++++++++++++ scaffold/mini-mes.c | 2 +- src/mes.c | 2 +- 15 files changed, 283 insertions(+), 15 deletions(-) create mode 100755 build-aux/cc64-mes.sh rename lib/linux/{mini-gcc.c => x86-mes-gcc/mini.c} (99%) rename lib/linux/{mini-mes.c => x86-mes/mini.c} (100%) create mode 100644 lib/linux/x86_64-mes-gcc/crt1.c create mode 100644 lib/linux/x86_64-mes-gcc/mini.c diff --git a/.gitignore b/.gitignore index beb36099..3a7e009a 100644 --- a/.gitignore +++ b/.gitignore @@ -35,9 +35,12 @@ *.stderr *.stdout *.x86-out +*.x86_64-mes-gcc-o +*.x86_64-mes-gcc-out *.x86_64-mes-out *.x86_64-out + /lib/x86-mes-gcc /lib/x86-mes-tcc /src/*.h diff --git a/HACKING b/HACKING index a12a2534..f5c61c2e 100644 --- a/HACKING +++ b/HACKING @@ -191,6 +191,9 @@ b *0x804a79d ** Create memory dump with 32 bit Gcc compiled Mes make out/i686-unknown-linux-gnu-mes out/i686-unknown-linux-gnu-mes --dump < module/mes/read-0.mes > module/mes/read-0-32.mo + +x/s *((char **)($rsp+8)) + ** C parser/compiler *** [[https://savannah.gnu.org/projects/nyacc][nyacc]] *** PEG: [[http://piumarta.com/software/peg/][parse C using PEG]] diff --git a/build-aux/build-cc.sh b/build-aux/build-cc.sh index cebd3590..6807b50a 100755 --- a/build-aux/build-cc.sh +++ b/build-aux/build-cc.sh @@ -23,6 +23,10 @@ set -e . ${srcdest}build-aux/config.sh . ${srcdest}build-aux/trace.sh +# cc64 +LIBC=${LIBC-c-mini} + +# native trace "SNARF gc.c" ${srcdest}build-aux/mes-snarf.scm src/gc.c trace "SNARF lib.c" ${srcdest}build-aux/mes-snarf.scm src/lib.c trace "SNARF math.c" ${srcdest}build-aux/mes-snarf.scm src/math.c @@ -31,5 +35,37 @@ trace "SNARF posix.c" ${srcdest}build-aux/mes-snarf.scm src/posix.c trace "SNARF reader.c" ${srcdest}build-aux/mes-snarf.scm src/reader.c trace "SNARF vector.c" ${srcdest}build-aux/mes-snarf.scm src/vector.c +# cc64-mes +trace "MSNARF gc.c" ${srcdest}build-aux/mes-snarf.scm --mes src/gc.c +trace "MSNARF lib.c" ${srcdest}build-aux/mes-snarf.scm --mes src/lib.c +trace "MSNARF math.c" ${srcdest}build-aux/mes-snarf.scm --mes src/math.c +trace "MSNARF mes.c" ${srcdest}build-aux/mes-snarf.scm --mes src/mes.c +trace "MSNARF posix.c" ${srcdest}build-aux/mes-snarf.scm --mes src/posix.c +trace "MSNARF reader.c" ${srcdest}build-aux/mes-snarf.scm --mes src/reader.c +trace "MSNARF vector.c" ${srcdest}build-aux/mes-snarf.scm --mes src/vector.c + ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc.sh lib/libmes sh ${srcdest}build-aux/cc.sh src/mes + +#ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/linux/x86_64/crt0 +ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/linux/x86_64-mes-gcc/crt1 +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/linux/x86_64/crti +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/linux/x86_64/crtn +ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libc-mini +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libc +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libgetopt +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libc+tcc +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libtcc1 +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libc+gnu +# ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc64-mes.sh lib/libg + +sh ${srcdest}build-aux/cc64-mes.sh scaffold/main +sh ${srcdest}build-aux/cc64-mes.sh scaffold/hello +sh ${srcdest}build-aux/cc64-mes.sh scaffold/argv +sh ${srcdest}build-aux/cc64-mes.sh scaffold/malloc +sh ${srcdest}build-aux/cc64-mes.sh scaffold/micro-mes +sh ${srcdest}build-aux/cc64-mes.sh scaffold/tiny-mes +# sh ${srcdest}build-aux/cc64-mes.sh scaffold/cons-mes +sh ${srcdest}build-aux/cc64-mes.sh scaffold/mini-mes + +sh ${srcdest}build-aux/cc64-mes.sh src/mes diff --git a/build-aux/build-cc32.sh b/build-aux/build-cc32.sh index de8f0e1b..abcd0c5e 100755 --- a/build-aux/build-cc32.sh +++ b/build-aux/build-cc32.sh @@ -27,6 +27,7 @@ LIBC=${LIBC-c} ##moduledir=${moduledir-${datadir}${datadir:+/}module} +# native # trace "SNARF gc.c" ${srcdest}build-aux/mes-snarf.scm src/gc.c # trace "SNARF lib.c" ${srcdest}build-aux/mes-snarf.scm src/lib.c # trace "SNARF math.c" ${srcdest}build-aux/mes-snarf.scm src/math.c @@ -35,6 +36,7 @@ LIBC=${LIBC-c} # trace "SNARF reader.c" ${srcdest}build-aux/mes-snarf.scm src/reader.c # trace "SNARF vector.c" ${srcdest}build-aux/mes-snarf.scm src/vector.c +# cc32-mes trace "MSNARF gc.c" ${srcdest}build-aux/mes-snarf.scm --mes src/gc.c trace "MSNARF lib.c" ${srcdest}build-aux/mes-snarf.scm --mes src/lib.c trace "MSNARF math.c" ${srcdest}build-aux/mes-snarf.scm --mes src/math.c @@ -55,12 +57,12 @@ ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc32-mes.sh lib/libtcc1 ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc32-mes.sh lib/libc+gnu ARCHDIR=1 NOLINK=1 sh ${srcdest}build-aux/cc32-mes.sh lib/libg -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/main -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/hello -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/argv -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/malloc -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/micro-mes -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/tiny-mes -# sh ${srcdest}build-aux/cc32-mes.sh scaffold/mini-mes +sh ${srcdest}build-aux/cc32-mes.sh scaffold/main +sh ${srcdest}build-aux/cc32-mes.sh scaffold/hello +sh ${srcdest}build-aux/cc32-mes.sh scaffold/argv +sh ${srcdest}build-aux/cc32-mes.sh scaffold/malloc +sh ${srcdest}build-aux/cc32-mes.sh scaffold/micro-mes +sh ${srcdest}build-aux/cc32-mes.sh scaffold/tiny-mes +sh ${srcdest}build-aux/cc32-mes.sh scaffold/mini-mes sh ${srcdest}build-aux/cc32-mes.sh src/mes diff --git a/build-aux/cc64-mes.sh b/build-aux/cc64-mes.sh new file mode 100755 index 00000000..b9990273 --- /dev/null +++ b/build-aux/cc64-mes.sh @@ -0,0 +1,64 @@ +#! /bin/sh + +# GNU Mes --- Maxwell Equations of Software +# Copyright © 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 . + +set -e + +. ${srcdest}build-aux/config.sh +. ${srcdest}build-aux/trace.sh + +arch=x86_64-mes-gcc +if [ "$CC64" = "$TCC" ]; then + arch=x86_64-mes-tcc + LIBC=c+tcc # tcc bug with undefined symbols +fi + +if [ -n "$LIBC" ]; then + CC64LIBS="lib/$arch/lib$LIBC.o" +fi + +c=$1 + +if [ -z "$ARCHDIR" ]; then + o="$c" + d=${c%%/*} + p="$arch-" +else + b=${c##*/} + d=${c%%/*}/$arch + o="$d/$b" +fi +mkdir -p $d + +trace "CC64 $c.c" $CC64\ + -c\ + $CC64_CPPFLAGS\ + $CC64_CFLAGS\ + -o "$o".${p}o\ + "${srcdest}$c".c + +if [ -z "$NOLINK" ]; then + trace "CCLD64 $c.c" $CC64\ + $CC64_CPPFLAGS\ + $CC64_CFLAGS\ + -o "$o".${p}out\ + lib/$arch/crt1.o\ + "$o".${p}o\ + $CC64LIBS +fi diff --git a/build-aux/config.sh b/build-aux/config.sh index 55b37966..1049409e 100644 --- a/build-aux/config.sh +++ b/build-aux/config.sh @@ -26,6 +26,7 @@ fi export CC export CC CFLAGS export CC32 +export CC64 export CC_CFLAGS export CC_CFLAGS export CC_CPPFLAGS @@ -82,6 +83,33 @@ CC_CFLAGS=${CC_CFLAGS-" -g "} +CC64_CPPFLAGS=${CC64_CPPFLAGS-" +-D 'VERSION=\"$VERSION\"' +-D 'MODULEDIR=\"$moduledir\"' +-D 'PREFIX=\"$prefix\"' +-I src +-I ${srcdest}src +-I ${srcdest}lib +-I ${srcdest}include +"} + +CC64_CFLAGS=${CC64_CFLAGS-" +-std=gnu99 +-O0 +-fno-builtin +-fno-stack-protector +-g +-m64 +-nostdinc +-nostdlib +-Wno-discarded-qualifiers +-Wno-int-to-pointer-cast +-Wno-pointer-to-int-cast +-Wno-pointer-sign +-Wno-int-conversion +-Wno-incompatible-pointer-types +"} + CC32_CPPFLAGS=${CC32_CPPFLAGS-" -D 'VERSION=\"$VERSION\"' -D 'MODULEDIR=\"$moduledir\"' diff --git a/build-aux/export.make b/build-aux/export.make index 693c5f84..3df66d81 100644 --- a/build-aux/export.make +++ b/build-aux/export.make @@ -60,6 +60,10 @@ ifdef CC32 export CC32 endif +ifdef CC64 +export CC64 +endif + ifdef BLOOD_ELF export BLOOD_ELF endif @@ -124,6 +128,10 @@ ifdef CC32_CFLAGS export CC32_CFLAGS endif +ifdef CC64_CFLAGS +export CC64_CFLAGS +endif + ifdef HEX2FLAGS export HEX2FLAGS endif diff --git a/configure b/configure index a04d0d29..9b0c315b 100755 --- a/configure +++ b/configure @@ -281,6 +281,8 @@ Some influential environment variables: CC C compiler command CFLAGS C compiler flags CC32 x86 C compiler command + CC64_CFLAGS x86_64 C compiler flags + CC64 x86_64 C compiler command CC32_CFLAGS x86 C compiler flags GUILE guile command GUILE_TOOLS guile-tools command @@ -364,6 +366,9 @@ Some influential environment variables: (make-dep "cc32" '(2 95) #:optional? #t #:commands '("i686-unknown-linux-gnu-gcc")) + (make-dep "cc64" '(2 95) + #:optional? #t + #:commands '("gcc")) (make-dep "M1" '(0 3)) (make-dep "blood-elf" '(0 1)) (make-dep "hex2" '(0 3)) @@ -458,9 +463,9 @@ Some influential environment variables: '( "CFLAGS" "CC32_CFLAGS" + "CC64_CFLAGS" "HEX2FLAGS" "M1FLAGS" - "CC32_CFLAGS" "MES_CFLAGS" )))) diff --git a/lib/linux/libc-mini.c b/lib/linux/libc-mini.c index 84d7d555..0bcfcb0e 100644 --- a/lib/linux/libc-mini.c +++ b/lib/linux/libc-mini.c @@ -20,13 +20,21 @@ #if __MESC__ -#include +#include -#else // !__MESC__ +#elif __i386__ -#include +#include -#endif // !__MESC__ +#elif __x86_64__ + +#include + +#else + +#error arch not supported + +#endif ssize_t write (int filedes, void const *buffer, size_t size) diff --git a/lib/linux/mini-gcc.c b/lib/linux/x86-mes-gcc/mini.c similarity index 99% rename from lib/linux/mini-gcc.c rename to lib/linux/x86-mes-gcc/mini.c index 59b50847..c2bfb77b 100644 --- a/lib/linux/mini-gcc.c +++ b/lib/linux/x86-mes-gcc/mini.c @@ -38,7 +38,7 @@ _exit (int code) ssize_t _write (int filedes, void const *buffer, size_t size) { - int r; + long r; asm ( "mov $"SYS_write",%%eax\n\t" "mov %1,%%ebx\n\t" diff --git a/lib/linux/mini-mes.c b/lib/linux/x86-mes/mini.c similarity index 100% rename from lib/linux/mini-mes.c rename to lib/linux/x86-mes/mini.c diff --git a/lib/linux/x86_64-mes-gcc/crt1.c b/lib/linux/x86_64-mes-gcc/crt1.c new file mode 100644 index 00000000..701d75fa --- /dev/null +++ b/lib/linux/x86_64-mes-gcc/crt1.c @@ -0,0 +1,57 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 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 . + */ + +char **environ = 0; +int main (int argc, char *argv[]); + +// gcc x86_64 calling convention: +// edi, esi, edx, ecx, r8d, r9d, , + +void +_start () +{ + asm ( + "movq %%rbp,%%rax\n\t" + "add $8,%%rax\n\t" + "movq (%%rax),%%rax\n\t" + "add $3,%%rax\n\t" + "shl $3,%%rax\n\t" + "add %%rbp,%%rax\n\t" + "mov %%rax,%0\n\t" + : "=r" (environ) + : //no inputs "" + ); + asm ( + "mov %rbp,%rax\n\t" + "add $16,%rax\n\t" + "mov %rax,%rsi\n\t" + + "mov %rbp,%rax\n\t" + "add $8,%rax\n\t" + "mov (%rax),%rax\n\t" + "mov %rax,%rdi\n\t" + "call main\n\t" + + "mov %rax,%rdi\n\t" + "mov $0x3c,%rax\n\t" + "syscall \n\t" + "hlt \n\t" + ); +} diff --git a/lib/linux/x86_64-mes-gcc/mini.c b/lib/linux/x86_64-mes-gcc/mini.c new file mode 100644 index 00000000..115957b1 --- /dev/null +++ b/lib/linux/x86_64-mes-gcc/mini.c @@ -0,0 +1,54 @@ +/* -*-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 . + */ + +#define SYS_exit "0x3c" +#define SYS_write "0x01" + +void +_exit (int code) +{ + asm ( + "mov $"SYS_exit",%%rax\n\t" + "mov %0,%%rdi\n\t" + "syscall \n\t" + : // no outputs "=" (r) + : "rm" (code) + ); + // not reached + _exit (0); +} + +ssize_t +_write (int filedes, void const *buffer, size_t size) +{ + long r; + asm ( + "mov $"SYS_write",%%rax\n\t" + "mov %1,%%rdi\n\t" + "mov %2,%%rsi\n\t" + "mov %3,%%rdx\n\t" + "syscall \n\t" + "mov %%rax,%0\n\t" + : "=r" (r) + : "rm" (filedes), "rm" (buffer), "rm" (size) + : "rax", "rdi", "rsi", "rdx" + ); + return r; +} diff --git a/scaffold/mini-mes.c b/scaffold/mini-mes.c index 755d9af8..f65eec42 100644 --- a/scaffold/mini-mes.c +++ b/scaffold/mini-mes.c @@ -38,7 +38,7 @@ int JAM_SIZE = 20000; int GC_SAFETY = 2000; char *g_arena = 0; -typedef int SCM; +typedef long SCM; int g_debug = 0; int g_free = 0; diff --git a/src/mes.c b/src/mes.c index 122843c5..7312d75f 100644 --- a/src/mes.c +++ b/src/mes.c @@ -36,7 +36,7 @@ int JAM_SIZE = 20000; int GC_SAFETY = 2000; char *g_arena = 0; -typedef int SCM; +typedef long SCM; int g_debug = 0; int g_free = 0;