mlibc: New library layout; remove duplication.

* lib/libc+tcc-gcc.c: New file: Libc bits for GCC from libc-mes+tcc.c.
* lib/libc+tcc-mes.c: New file: Libc bits for MesCC from libc-mes+tcc.c.
* lib/libc+tcc.c: New file: Libc bits from libc-mes+tcc.c.
* lib/libc.c: New file: Merged from libc-mes.c, libc-gcc.c.
  (abtoi): New function: Rename from _atoi.
* lib/linux+tcc-gcc.c: New file:  Linux bits from libc-gcc+tcc.c.
* lib/linux+tcc-mes.c: New file: Linux bits from libc-mes+tcc.c.
* lib/linux-gcc.c: New file: Linux bits from libc-gcc.c.
* lib/linux-mes.c: New file: Linux bits from libc-mes.c.
* lib/mini-libc-gcc.c: New file.
* lib/mini-libc.c: New file: Libc bits from mini-libc-mes.c.
* lib/mini-linux-gcc.c: New file.
* lib/mini-linux-mes.c: New file: Linux bits from mini-libc-mes.c.
* lib/libc-gcc.c: Remove duplication using include now.
* lib/libc-mes.c: Likewise.
* lib/mini-libc-mes.c: Likewise.
* include/stdio.h (puts)[POSIX]: Remomve macro.
  (fputc, fputs)[!POSIX]: New macro.
  (fputc, fputs): Remove declaration.
* scaffold/tests/7j-strtoull.c (strtoull, test): Use abtoi.
* src/lib.c (fputs)[__MESC__]: Remove define.
* guile/guix/make.scm: Update for new library layout.
* make.scm: Likewise.
* build.sh: Likewise.
* install.sh: Likewise.
This commit is contained in:
Jan Nieuwenhuizen 2017-12-03 13:56:21 +01:00
parent d7b35f6b1e
commit 808af5001f
22 changed files with 1099 additions and 1240 deletions

View file

@ -56,8 +56,8 @@ $HEX2 --LittleEndian --Architecture=1 --BaseAddress=0x1000000\
$M1 --LittleEndian --Architecture=1 -f\ $M1 --LittleEndian --Architecture=1 -f\
stage0/x86.M1\ stage0/x86.M1\
-f $MES_SEED/libc-mes+tcc.M1\ -f $MES_SEED/libc+tcc-mes.M1\
-o libc-mes+tcc.hex2 -o libc+tcc-mes.hex2
# TODO: after building from seed, build from src/mes.c # TODO: after building from seed, build from src/mes.c
# build-aux/mes-snarf.scm --mes src/gc.c # build-aux/mes-snarf.scm --mes src/gc.c

View file

@ -61,7 +61,7 @@
crt1.mlibc-o crt1.mlibc-o
libc-gcc.mlibc-o libc-gcc.mlibc-o
libc-gcc+tcc.mlibc-o libc+tcc-gcc.mlibc-o
add-target add-target
get-target get-target
@ -540,4 +540,4 @@
(define crt1.mlibc-o (compile.gcc "lib/crt1.c" #:libc #f)) (define crt1.mlibc-o (compile.gcc "lib/crt1.c" #:libc #f))
(define libc-gcc.mlibc-o (compile.gcc "lib/libc-gcc.c" #:libc #f)) (define libc-gcc.mlibc-o (compile.gcc "lib/libc-gcc.c" #:libc #f))
(define libc-gcc+tcc.mlibc-o (compile.gcc "lib/libc-gcc+tcc.c" #:libc #f)) (define libc+tcc-gcc.mlibc-o (compile.gcc "lib/libc+tcc-gcc.c" #:libc #f))

View file

@ -46,7 +46,6 @@ int g_stdout;
int fdputs (char const* s, int fd); int fdputs (char const* s, int fd);
#undef puts #undef puts
#define puts(x) fdputs(x, STDOUT)
#define fputs fdputs #define fputs fdputs
#ifdef putc #ifdef putc
@ -88,6 +87,9 @@ int fdungetc (int c, int fd);
int fdputs (char const* s, int fd); int fdputs (char const* s, int fd);
#endif // __MES_GNUC__ #endif // __MES_GNUC__
#define fputs fdputs
#define fputc fdputc
typedef int FILE; typedef int FILE;
#ifndef __MES_SIZE_T #ifndef __MES_SIZE_T
@ -104,8 +106,6 @@ FILE *fopen (char const *pathname, char const *mode);
int ferror (FILE *stream); int ferror (FILE *stream);
int fprintf (FILE *stream, char const *format, ...); int fprintf (FILE *stream, char const *format, ...);
int fdputc (int c, int fd); int fdputc (int c, int fd);
int fputc (int c, int fd);
int fputs (char const* s, int fd);
size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream);
int fseek (FILE *stream, long offset, int whence); int fseek (FILE *stream, long offset, int whence);
long ftell (FILE *stream); long ftell (FILE *stream);

View file

@ -14,11 +14,11 @@ mkdir -p $PREFIX/lib
mkdir -p $MES_PREFIX/lib mkdir -p $MES_PREFIX/lib
cp $MES_SEED/crt1.M1 $MES_PREFIX/lib/crt1.M1 cp $MES_SEED/crt1.M1 $MES_PREFIX/lib/crt1.M1
cp $MES_SEED/libc-mes.M1 $MES_PREFIX/lib/libc-mes.M1 cp $MES_SEED/libc-mes.M1 $MES_PREFIX/lib/libc-mes.M1
cp $MES_SEED/libc-mes+tcc.M1 $MES_PREFIX/lib/libc-mes+tcc.M1 cp $MES_SEED/libc+tcc-mes.M1 $MES_PREFIX/lib/libc+tcc-mes.M1
cp crt1.hex2 $MES_PREFIX/lib/crt1.hex2 cp crt1.hex2 $MES_PREFIX/lib/crt1.hex2
cp libc-mes.hex2 $MES_PREFIX/lib/libc-mes.hex2 cp libc-mes.hex2 $MES_PREFIX/lib/libc-mes.hex2
cp libc-mes+tcc.hex2 $MES_PREFIX/lib/libc-mes+tcc.hex2 cp libc+tcc-mes.hex2 $MES_PREFIX/lib/libc+tcc-mes.hex2
cp scripts/mescc.mes $PREFIX/bin/mescc.mes cp scripts/mescc.mes $PREFIX/bin/mescc.mes
cp guile/mescc.scm $PREFIX/bin/mescc.scm cp guile/mescc.scm $PREFIX/bin/mescc.scm

93
lib/libc+tcc-gcc.c Normal file
View file

@ -0,0 +1,93 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
#include <libc-gcc.c>
#include <linux+tcc-gcc.c>
#include <libc+tcc.c>
#include <getopt.c>
int errno;
void
longjmp (jmp_buf env, int val)
{
val = val == 0 ? 1 : val;
asm ("mov 0xc(%ebp),%eax\n\t" // val
"mov 0x8(%ebp),%ebp\n\t" // env*
"mov 0x4(%ebp),%ebx\n\t" // env->__pc
"mov 0x8(%ebp),%esp\n\t" // env->__sp
"mov 0x0(%ebp),%ebp\n\t" // env->__bp
"jmp *%ebx\n\t" // jmp *PC
);
// not reached
exit (42);
}
#if 0
int
setjmp_debug (jmp_buf env, int val)
{
int i;
#if 1
i = env->__bp;
i = env->__pc;
i = env->__sp;
#else
i = env[0].__bp;
i = env[0].__pc;
i = env[0].__sp;
#endif
return val == 0 ? 1 : val;
}
#endif
int
setjmp (jmp_buf env)
{
int *p = (int*)&env;
env[0].__bp = p[-2];
env[0].__pc = p[-1];
env[0].__sp = (int)&env;
return 0;
}
int
__udivdi3 (int a, int b)
{
return a / b;
}
int
__umoddi3 (int a, int b)
{
return a % b;
}

80
lib/libc+tcc-mes.c Normal file
View file

@ -0,0 +1,80 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
#include <libc-mes.c>
#include <linux+tcc-mes.c>
#include <libc+tcc.c>
#include <getopt.c>
int errno;
void
longjmp (jmp_buf env, int val)
{
val = val == 0 ? 1 : val;
asm ("mov____0x8(%ebp),%eax !0x0c"); // val
asm ("mov____0x8(%ebp),%ebp !0x08"); // env*
asm ("mov____0x8(%ebp),%ebx !0x4"); // env.__pc
asm ("mov____0x8(%ebp),%esp !0x8"); // env.__sp
asm ("mov____0x8(%ebp),%ebp !0x0"); // env.__bp
asm ("jmp____*%ebx");
// not reached
exit (42);
}
#if 0
int
setjmp_debug (jmp_buf env, int val)
{
int i;
#if 1
i = env->__bp;
i = env->__pc;
i = env->__sp;
#else
i = env[0].__bp;
i = env[0].__pc;
i = env[0].__sp;
#endif
return val == 0 ? 1 : val;
}
#endif
int
setjmp (__jmp_buf *env)
{
int *p = (int*)&env;
env[0].__bp = p[-2];
env[0].__pc = p[-1];
env[0].__sp = (int)&env;
return 0;
}

View file

@ -29,53 +29,6 @@
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#if !__GNUC__ && !__TINYC__
#include <libc-mes.c>
#include <getopt.c>
int errno;
int
close (int fd)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_close");
asm ("int____$0x80");
}
int
unlink (char const *file_name)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_unlink");
asm ("int____$0x80");
}
off_t
lseek (int fd, off_t offset, int whence)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____0x8(%ebp),%edx !16");
asm ("mov____$i32,%eax SYS_lseek");
asm ("int____$0x80");
}
char *
getcwd (char *buf, size_t size)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____$i32,%eax SYS_getcwd");
asm ("int____$0x80");
}
#endif // !__GNUC__
int int
dlclose (void *handle) dlclose (void *handle)
{ {
@ -181,32 +134,6 @@ localtime (time_t const *timep)
return 0; return 0;
} }
void
longjmp (jmp_buf env, int val)
{
val = val == 0 ? 1 : val;
#if __MESC__
asm ("mov____0x8(%ebp),%eax !0x0c"); // val
asm ("mov____0x8(%ebp),%ebp !0x08"); // env*
asm ("mov____0x8(%ebp),%ebx !0x4"); // env.__pc
asm ("mov____0x8(%ebp),%esp !0x8"); // env.__sp
asm ("mov____0x8(%ebp),%ebp !0x0"); // env.__bp
asm ("jmp____*%ebx");
#else
asm ("mov 0xc(%ebp),%eax\n\t" // val
"mov 0x8(%ebp),%ebp\n\t" // env*
"mov 0x4(%ebp),%ebx\n\t" // env->__pc
"mov 0x8(%ebp),%esp\n\t" // env->__sp
"mov 0x0(%ebp),%ebp\n\t" // env->__bp
"jmp *%ebx\n\t" // jmp *PC
);
#endif
// not reached
exit (42);
}
void * void *
memmove (void *dest, void const *src, size_t n) memmove (void *dest, void const *src, size_t n)
{ {
@ -255,39 +182,6 @@ remove (char const *file_name)
return 0; return 0;
} }
#if 0
int
setjmp_debug (jmp_buf env, int val)
{
int i;
#if 1
i = env->__bp;
i = env->__pc;
i = env->__sp;
#else
i = env[0].__bp;
i = env[0].__pc;
i = env[0].__sp;
#endif
return val == 0 ? 1 : val;
}
#endif
#if __MESC__
int
setjmp (__jmp_buf *env)
#else
int
setjmp (jmp_buf env)
#endif
{
int *p = (int*)&env;
env[0].__bp = p[-2];
env[0].__pc = p[-1];
env[0].__sp = (int)&env;
return 0;
}
int int
sigaction (int signum, struct sigaction const *act, struct sigaction *oldact) sigaction (int signum, struct sigaction const *act, struct sigaction *oldact)
{ {
@ -362,13 +256,12 @@ strstr (char const *haystack, char const *needle)
long long
strtol (char const *nptr, char **endptr, int base) strtol (char const *nptr, char **endptr, int base)
{ {
eputs ("strtol stub\n");
if (!strncmp (nptr, "0x", 2)) if (!strncmp (nptr, "0x", 2))
{ {
char const *p = nptr + 2; char const *p = nptr + 2;
return _atoi (&p, 16); return abtoi (&p, 16);
} }
return _atoi (&nptr, base); return abtoi (&nptr, base);
} }
long long int long long int
@ -386,10 +279,10 @@ strtoul (char const *nptr, char **endptr, int base)
} }
unsigned long long unsigned long long
strtoull (char const *p, char **end, int base) strtoull (char const *p, char **endptr, int base)
{ {
*end = p; *endptr = p;
return _atoi (end, base); return abtoi (endptr, base);
} }
time_t time (time_t *tloc) time_t time (time_t *tloc)

View file

@ -18,586 +18,21 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdio.h>
#include <mlibc.h> #include <mlibc.h>
#include <stdio.h>
#if __GNUC__ #include <mlibc.h>
#include <stdlib.h>
#endif
#if (__GNUC__ || __TINYC__) && !POSIX
void
exit (int code)
{
#if !__TINYC__
asm (
"mov %0,%%ebx\n\t"
"mov $1,%%eax\n\t"
"int $0x80\n\t"
: // no outputs "=" (r)
: "" (code)
);
#else // __TINYC__
asm (
"mov %0,%%ebx\n\t"
"mov $1,%%eax\n\t"
"int $128\n\t"
: // no outputs "=" (r)
: "Ir" (code)
);
#endif // __TINYC__
// not reached
exit (0);
}
int
read (int fd, void* buf, size_t n)
{
#if !__TINYC__
int r;
//syscall (SYS_write, fd, s, n));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"movl $0x3,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd), "" (buf), "" (n)
: "eax", "ebx", "ecx", "edx"
);
return r;
#endif
}
int
write (int fd, char const* s, int n)
{
int r;
#if __GNUC__
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"mov $0x04,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd), "" (s), "" (n)
: "eax", "ebx", "ecx", "edx"
);
//syscall (SYS_write, fd, s, n));
#elif __TINYC__
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"mov $4, %%eax\n\t"
"int $128\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "Ir" (fd), "Ir" (s), "Ir" (n)
: "eax", "ebx", "ecx"//, "edx"
);
#endif
return r;
}
int
open (char const *s, int flags, ...)
{
#if !__TINYC__
int mode;
asm (
"mov %%ebp,%%eax\n\t"
"add $0x10,%%eax\n\t"
"mov (%%eax),%%eax\n\t"
"mov %%eax,%0\n\t"
: "=mode" (mode)
: //no inputs ""
);
int r;
//syscall (SYS_open, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"mov $0x5,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (flags), "" (mode)
: "eax", "ebx", "ecx", "edx"
);
return r;
#endif
}
int
chmod (char const *s, int mode)
{
#if !__TINYC__
int r;
//syscall (SYS_chmod, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $0x0f,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (mode)
: "eax", "ebx", "ecx"
);
return r;
#endif
}
int
access (char const *s, int mode)
{
#if !__TINYC__
int r;
//syscall (SYS_access, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $0x21,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (mode)
: "eax", "ebx", "ecx"
);
return r;
#endif
}
void *
brk (void *p)
{
#if !__TINYC__
void *r;
asm (
"mov %1,%%ebx\n\t"
"mov $0x2d,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (p)
: "eax", "ebx"
);
return r;
#endif
}
int
fsync (int fd)
{
#if !__TINYC__
int r;
//syscall (SYS_fsync, fd));
asm (
"mov %1,%%ebx\n\t"
"mov $0x76, %%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd)
: "eax", "ebx"
);
return r;
#endif
}
int
fputc (int c, int fd)
{
write (fd, (char*)&c, 1);
return 0;
}
void
free (void *ptr)
{
}
char *g_brk = 0;
void *
malloc (size_t size)
{
if (!g_brk)
g_brk = brk (0);
if ((int)brk (g_brk + size) == -1)
return 0;
char *p = g_brk;
g_brk += size;
return p;
}
void *
memcpy (void *dest, void const *src, size_t n)
{
char* p = dest;
char const* q = src;
while (n--) *p++ = *q++;
return dest;
}
int
putchar (int c)
{
write (STDOUT, (char*)&c, 1);
return 0;
}
void *
realloc (void *ptr, size_t size)
{
void *new = malloc (size);
if (ptr && new)
{
memcpy (new, ptr, size);
free (ptr);
}
return new;
}
size_t
strlen (char const* s)
{
int i = 0;
while (s[i]) i++;
return i;
}
int
strcmp (char const* a, char const* b)
{
while (*a && *b && *a == *b) {a++;b++;}
return *a - *b;
}
char *
strcpy (char *dest, char const *src)
{
char *p = dest;
while (*src) *p++ = *src++;
*p = 0;
return dest;
}
int
fputs (char const* s, int fd)
{
int i = strlen (s);
write (fd, s, i);
return 0;
}
int
puts (char const* s)
{
int i = strlen (s);
write (STDOUT, s, i);
return 0;
}
void
assert_fail (char* s)
{
eputs ("assert fail: ");
eputs (s);
eputs ("\n");
*((int*)0) = 0;
}
#define assert(x) ((x) ? (void)0 : assert_fail (#x))
int ungetc_char = -1;
char ungetc_buf[2];
int
getchar ()
{
char c;
int i;
if (ungetc_char == -1)
{
int r = read (g_stdin, &c, 1);
if (r < 1) return -1;
i = c;
}
else
i = ungetc_buf[ungetc_char--];
if (i < 0) i += 256;
return i;
}
int
ungetc (int c, int fd)
{
assert (ungetc_char < 2);
ungetc_buf[++ungetc_char] = c;
return c;
}
char const* itoa (int);
int
strncmp (char const* a, char const* b, size_t length)
{
while (*a && *b && *a == *b && --length) {a++;b++;}
return *a - *b;
}
char *
getenv (char const* s)
{
char **p = g_environment;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
p++;
}
return 0;
}
int
isdigit (int c)
{
return (c>='0') && (c<='9');
}
int
isxdigit (int c)
{
return isdigit (c) || (c>='a') && (c<='f');
}
int
isnumber (int c, int base)
{
if (base == 2)
return (c>='0') && (c<='1');
if (base == 8)
return (c>='0') && (c<='7');
if (base == 10)
return isdigit (c);
if (base == 16)
return isxdigit (c);
}
int
_atoi (char const **p, int base)
{
char const *s = *p;
int i = 0;
int sign = 1;
if (!base) base = 10;
if (*s && *s == '-')
{
sign = -1;
s++;
}
while (isnumber (*s, base))
{
i *= base;
int m = *s > '9' ? 'a' - 10 : '0';
i += *s - m;
s++;
}
*p = s;
return i * sign;
}
int
atoi (char const *s)
{
char const *p = s;
return _atoi (&p, 0);
}
// FIXME: copied from libc-mes.c now
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h>
int
vprintf (char const* format, va_list ap)
{
char const *p = format;
while (*p)
if (*p != '%')
putchar (*p++);
else
{
p++;
char c = *p;
switch (c)
{
case '%': {putchar (*p); break;}
case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
default: putchar (*p);
}
p++;
}
va_end (ap);
return 0;
}
int
printf (char const* format, ...)
{
va_list ap;
va_start (ap, format);
int r = vprintf (format, ap);
va_end (ap);
return r;
}
int
vsprintf (char *str, char const* format, va_list ap)
{
char const *p = format;
while (*p)
if (*p != '%')
*str++ = *p++;
else
{
p++;
char c = *p;
switch (c)
{
case '%': {*str++ = *p; break;}
case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
case 'd': {int d; d = va_arg (ap, int); char const *s = itoa (d); while (*s) *str++ = *s++; break;}
case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
default: *str++ = *p;
}
p++;
}
*str = 0;
return strlen (str);
}
int
sprintf (char *str, char const* format, ...)
{
va_list ap;
va_start (ap, format);
int r = vsprintf (str, format, ap);
va_end (ap);
return r;
}
#endif
char itoa_buf[10];
char const*
itoa (int x)
{
//static char itoa_buf[10];
//char *p = buf+9;
char *p = itoa_buf;
p += 9;
*p-- = 0;
//int sign = x < 0;
int sign;
sign = x < 0;
if (sign)
x = -x;
do
{
*p-- = '0' + (x % 10);
x = x / 10;
} while (x);
if (sign)
*p-- = '-';
return p+1;
}
char const*
itoab (int x, int base)
{
//static char itoa_buf[10];
//char *p = buf+9;
char *p = itoa_buf;
p += 9;
*p-- = 0;
//int sign = x < 0; // FIXME
int sign = 0;
if (x < 0) sign = 1;
if (sign)
x = -x;
do
{
int i = x % base;
*p-- = i > 9 ? 'a' + i - 10 : '0' + i;
x = x / base;
} while (x);
if (sign && *(p + 1) != '0')
*p-- = '-';
return p+1;
}
#if POSIX
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#endif // POSIX
int #include <fcntl.h>
fdputs (char const* s, int fd) #include <assert.h>
{
int i = strlen (s);
write (fd, s, i);
return 0;
}
int #include <mini-linux-gcc.c>
eputc (int c) #include <mini-libc.c>
{ #include <linux-gcc.c>
return fdputc (c, STDERR); #include <libc.c>
}
int
eputs (char const* s)
{
return fdputs (s, STDERR);
}
int
fdputc (int c, int fd)
{
write (fd, (char*)&c, 1);
return 0;
}
#if POSIX #if POSIX

View file

@ -18,469 +18,14 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <mlibc.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
char **g_environment = 0; // FIXME: todo extern
int g_stdin = 0;
void _env (); void _env ();
void #include <mini-linux-mes.c>
exit () #include <mini-libc.c>
{ #include <linux-mes.c>
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx #include <libc.c>
asm ("mov____$i32,%eax SYS_exit"); // mov $0x1,%eax
asm ("int____$0x80"); // int $0x80
}
void
read ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
asm ("mov____0x8(%ebp),%edx !16"); // mov 0x8(%ebp),%edx
asm ("mov____$i32,%eax SYS_read"); // mov $0x3,%eax
asm ("int____$0x80"); // int $0x80
}
void
write ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
asm ("mov____0x8(%ebp),%edx !16"); // mov 0x8(%ebp),%edx
asm ("mov____$i32,%eax SYS_write"); // mov $0x4,%eax
asm ("int____$0x80"); // int $0x80
}
void
open ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
asm ("mov____0x8(%ebp),%edx !16"); // mov 0x8(%ebp),%edx
asm ("mov____$i32,%eax SYS_open"); // mov $0x5,%eax
asm ("int____$0x80"); // int $0x80
}
void
chmod ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
asm ("mov____$i32,%eax SYS_chmod"); // mov $0x0f,%eax
asm ("int____$0x80"); // int $0x80
}
void
access ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
asm ("mov____$i32,%eax SYS_access"); // mov $0x21,%eax
asm ("int____$0x80"); // int $0x80
}
void
brk ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____$i32,%eax SYS_brk"); // mov $0x2d,%eax
asm ("int____$0x80"); // int $0x80
}
void
fsync ()
{
asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
asm ("mov____$i32,%eax SYS_fsync"); // mov $0x7c,%eax
asm ("int____$0x80"); // int $0x80
}
int
strlen (char const* s)
{
int i = 0;
while (s[i]) i++;
return i;
}
int
eputc (int c)
{
return fputc (c, 2);
}
int
eputs (char const* s)
{
int i = strlen (s);
write (2, s, i);
return 0;
}
int
fputs (char const* s, int fd)
{
int i = strlen (s);
write (fd, s, i);
return 0;
}
int
puts (char const* s)
{
int i = strlen (s);
write (1, s, i);
return 0;
}
int
putchar (int c)
{
write (1, (char*)&c, 1);
return 0;
}
int
fputc (int c, int fd)
{
write (fd, (char*)&c, 1);
return 0;
}
void
assert_fail (char* s)
{
eputs ("assert fail: ");
eputs (s);
eputs ("\n");
//*((int*)0) = 0;
char *fail = s;
fail = 0;
*fail = 0;
}
int ungetc_char = -1;
char ungetc_buf[2];
int
getchar ()
{
char c;
int i;
if (ungetc_char == -1)
{
int r = read (g_stdin, &c, 1);
if (r < 1) return -1;
i = c;
}
else
{
//FIXME
//i = ungetc_buf[ungetc_char--];
i = ungetc_buf[ungetc_char];
//ungetc_char--;
ungetc_char = ungetc_char - 1;
}
if (i < 0) i += 256;
return i;
}
int
fgetc (int fd)
{
char c;
int i;
int r = read (fd, &c, 1);
if (r < 1) return -1;
i = c;
return i;
}
void
free (void *ptr)
{
}
//#define assert(x) ((x) ? (void)0 : assert_fail (#x))
int
ungetc (int c, int fd)
{
//FIXME
//assert (ungetc_char < 2);
//assert (ungetc_char == -1 || ungetc_char < 2);
//FIXME
//ungetc_buf[++ungetc_char] = c;
ungetc_char++;
ungetc_buf[ungetc_char] = c;
return c;
}
int
strcmp (char const* a, char const* b)
{
while (*a && *b && *a == *b)
{
a++;b++;
}
return *a - *b;
}
char *
strcpy (char *dest, char const *src)
{
char *p = dest;
while (*src) *p++ = *src++;
*p = 0;
return dest;
}
char itoa_buf[10];
char const*
itoa (int x)
{
//static char itoa_buf[10];
//char *p = buf+9;
char *p = itoa_buf;
p += 9;
*p-- = 0;
//int sign = x < 0; // FIXME
int sign = 0;
if (x < 0) sign = 1;
if (sign)
x = -x;
do
{
*p-- = '0' + (x % 10);
x = x / 10;
} while (x);
if (sign && *(p + 1) != '0')
*p-- = '-';
return p+1;
}
char const*
itoab (int x, int base)
{
//static char itoa_buf[10];
//char *p = buf+9;
char *p = itoa_buf;
p += 9;
*p-- = 0;
//int sign = x < 0; // FIXME
int sign = 0;
if (x < 0) sign = 1;
if (sign)
x = -x;
do
{
int i = x % base;
*p-- = i > 9 ? 'a' + i - 10 : '0' + i;
x = x / base;
} while (x);
if (sign && *(p + 1) != '0')
*p-- = '-';
return p+1;
}
int
isdigit (int c)
{
return (c>='0') && (c<='9');
}
int
isxdigit (int c)
{
return isdigit (c) || (c>='a') && (c<='f');
}
int
isnumber (int c, int base)
{
if (base == 2)
return (c>='0') && (c<='1');
if (base == 8)
return (c>='0') && (c<='7');
if (base == 10)
return isdigit (c);
if (base == 16)
return isxdigit (c);
}
int
_atoi (char const **p, int base)
{
char const *s = *p;
int i = 0;
int sign = 1;
if (!base) base = 10;
if (*s && *s == '-')
{
sign = -1;
s++;
}
while (isnumber (*s, base))
{
i *= base;
int m = *s > '9' ? 'a' - 10 : '0';
i += *s - m;
s++;
}
*p = s;
return i * sign;
}
int
atoi (char const *s)
{
char const *p = s;
return _atoi (&p, 0);
}
char *g_brk = 0;
void *
malloc (size_t size)
{
if (!g_brk)
g_brk = brk (0);
if (brk (g_brk + size) == -1)
return 0;
char *p = g_brk;
g_brk += size;
return p;
}
void *
memcpy (void *dest, void const *src, size_t n)
{
char* p = dest;
char* q = src;
while (n--) *p++ = *q++;
return dest;
}
void *
realloc (void *ptr, size_t size)
{
void *new = malloc (size);
if (ptr && new)
{
memcpy (new, ptr, size);
free (ptr);
}
return new;
}
int
strncmp (char const* a, char const* b, int length)
{
while (*a && *b && *a == *b && --length) {a++;b++;}
return *a - *b;
}
char *
getenv (char const* s)
{
char **p = g_environment;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
p++;
}
return 0;
}
int
vprintf (char const* format, va_list ap)
{
char const *p = format;
while (*p)
if (*p != '%')
putchar (*p++);
else
{
p++;
char c = *p;
switch (c)
{
case '%': {putchar (*p); break;}
case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
default: {putchar (*p); break;}
}
p++;
}
va_end (ap);
return 0;
}
int
printf (char const* format, ...)
{
va_list ap;
va_start (ap, format);
int r = vprintf (format, ap);
va_end (ap);
return r;
}
int
vsprintf (char *str, char const* format, va_list ap)
{
char const *p = format;
while (*p)
if (*p != '%')
*str++ = *p++;
else
{
p++;
char c = *p;
switch (c)
{
case '%': {*str++ = *p; break;}
case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
case 'd': {int d; d = va_arg (ap, int); char const *s; s = itoa (d); while (*s) *str++ = *s++; break;}
case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
default: {*str++ = *p; break;}
}
p++;
}
va_end (ap);
*str = 0;
return strlen (str);
}
int
sprintf (char *str, char const* format, ...)
{
va_list ap;
va_start (ap, format);
int r = vsprintf (str, format, ap);
va_end (ap);
return r;
}

393
lib/libc.c Normal file
View file

@ -0,0 +1,393 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdlib.h>
#if POSIX
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#endif // POSIX
int
isdigit (int c)
{
return (c>='0') && (c<='9');
}
int
isxdigit (int c)
{
return isdigit (c) || (c>='a') && (c<='f');
}
int
isnumber (int c, int base)
{
if (base == 2)
return (c>='0') && (c<='1');
if (base == 8)
return (c>='0') && (c<='7');
if (base == 10)
return isdigit (c);
if (base == 16)
return isxdigit (c);
}
int
abtoi (char const **p, int base)
{
char const *s = *p;
int i = 0;
int sign = 1;
if (!base) base = 10;
if (*s && *s == '-')
{
sign = -1;
s++;
}
while (isnumber (*s, base))
{
i *= base;
int m = *s > '9' ? 'a' - 10 : '0';
i += *s - m;
s++;
}
*p = s;
return i * sign;
}
int
atoi (char const *s)
{
char const *p = s;
return abtoi (&p, 0);
}
char itoa_buf[10];
char const*
itoa (int x)
{
//static char itoa_buf[10];
//char *p = buf+9;
char *p = itoa_buf;
p += 9;
*p-- = 0;
//int sign = x < 0; // FIXME
int sign = 0;
if (x < 0) sign = 1;
if (sign)
x = -x;
do
{
*p-- = '0' + (x % 10);
x = x / 10;
} while (x);
if (sign && *(p + 1) != '0')
*p-- = '-';
return p+1;
}
char const*
itoab (int x, int base)
{
//static char itoa_buf[10];
//char *p = buf+9;
char *p = itoa_buf;
p += 9;
*p-- = 0;
//int sign = x < 0; // FIXME
int sign = 0;
if (x < 0) sign = 1;
if (sign)
x = -x;
do
{
int i = x % base;
*p-- = i > 9 ? 'a' + i - 10 : '0' + i;
x = x / base;
} while (x);
if (sign && *(p + 1) != '0')
*p-- = '-';
return p+1;
}
int
fdputc (int c, int fd)
{
write (fd, (char*)&c, 1);
return 0;
}
int
fdputs (char const* s, int fd)
{
int i = strlen (s);
write (fd, s, i);
return 0;
}
#if !POSIX
///char **g_environment = 0; // FIXME: todo extern
int g_stdin = 0;
void _env ();
int
eputc (int c)
{
return fdputc (c, STDERR);
}
int
putchar (int c)
{
write (STDOUT, (char*)&c, 1);
return 0;
}
void
assert_fail (char* s)
{
eputs ("assert fail: ");
eputs (s);
eputs ("\n");
//*((int*)0) = 0;
char *fail = s;
fail = 0;
*fail = 0;
}
int ungetc_char = -1;
char ungetc_buf[2];
int
getchar ()
{
char c;
int i;
if (ungetc_char == -1)
{
int r = read (g_stdin, &c, 1);
if (r < 1) return -1;
i = c;
}
else
{
//FIXME
//i = ungetc_buf[ungetc_char--];
i = ungetc_buf[ungetc_char];
//ungetc_char--;
ungetc_char = ungetc_char - 1;
}
if (i < 0) i += 256;
return i;
}
int
fgetc (int fd)
{
char c;
int i;
int r = read (fd, &c, 1);
if (r < 1) return -1;
i = c;
return i;
}
void
free (void *ptr)
{
}
//#define assert(x) ((x) ? (void)0 : assert_fail (#x))
int
ungetc (int c, int fd)
{
//FIXME
//assert (ungetc_char < 2);
//assert (ungetc_char == -1 || ungetc_char < 2);
//FIXME
//ungetc_buf[++ungetc_char] = c;
ungetc_char++;
ungetc_buf[ungetc_char] = c;
return c;
}
int
strcmp (char const* a, char const* b)
{
while (*a && *b && *a == *b)
{
a++;b++;
}
return *a - *b;
}
char *
strcpy (char *dest, char const *src)
{
char *p = dest;
while (*src) *p++ = *src++;
*p = 0;
return dest;
}
char *g_brk = 0;
void *
malloc (size_t size)
{
if (!g_brk)
g_brk = brk (0);
if (brk (g_brk + size) == -1)
return 0;
char *p = g_brk;
g_brk += size;
return p;
}
void *
memcpy (void *dest, void const *src, size_t n)
{
char* p = dest;
char* q = src;
while (n--) *p++ = *q++;
return dest;
}
void *
realloc (void *ptr, size_t size)
{
void *new = malloc (size);
if (ptr && new)
{
memcpy (new, ptr, size);
free (ptr);
}
return new;
}
int
strncmp (char const* a, char const* b, size_t length)
{
while (*a && *b && *a == *b && --length) {a++;b++;}
return *a - *b;
}
char *
getenv (char const* s)
{
char **p = g_environment;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
p++;
}
return 0;
}
int
vprintf (char const* format, va_list ap)
{
char const *p = format;
while (*p)
if (*p != '%')
putchar (*p++);
else
{
p++;
char c = *p;
switch (c)
{
case '%': {putchar (*p); break;}
case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
default: {putchar (*p); break;}
}
p++;
}
va_end (ap);
return 0;
}
int
printf (char const* format, ...)
{
va_list ap;
va_start (ap, format);
int r = vprintf (format, ap);
va_end (ap);
return r;
}
int
vsprintf (char *str, char const* format, va_list ap)
{
char const *p = format;
while (*p)
if (*p != '%')
*str++ = *p++;
else
{
p++;
char c = *p;
switch (c)
{
case '%': {*str++ = *p; break;}
case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
case 'd': {int d; d = va_arg (ap, int); char const *s; s = itoa (d); while (*s) *str++ = *s++; break;}
case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
default: {*str++ = *p; break;}
}
p++;
}
va_end (ap);
*str = 0;
return strlen (str);
}
int
sprintf (char *str, char const* format, ...)
{
va_list ap;
va_start (ap, format);
int r = vsprintf (str, format, ap);
va_end (ap);
return r;
}
#endif //!POSIX

View file

@ -18,18 +18,6 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <libc-gcc.c>
#include <libc-mes+tcc.c>
#include <getopt.c>
int errno;
#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/time.h>
#define SYS_exit "0x01" #define SYS_exit "0x01"
#define SYS_read "0x03" #define SYS_read "0x03"
#define SYS_write "0x04" #define SYS_write "0x04"
@ -116,15 +104,3 @@ getcwd (char *buf, size_t size)
return r; return r;
#endif #endif
} }
int
__udivdi3 (int a, int b)
{
return a / b;
}
int
__umoddi3 (int a, int b)
{
return a % b;
}

60
lib/linux+tcc-mes.c Normal file
View file

@ -0,0 +1,60 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
int errno;
int
close (int fd)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_close");
asm ("int____$0x80");
}
int
unlink (char const *file_name)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_unlink");
asm ("int____$0x80");
}
off_t
lseek (int fd, off_t offset, int whence)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____0x8(%ebp),%edx !16");
asm ("mov____$i32,%eax SYS_lseek");
asm ("int____$0x80");
}
char *
getcwd (char *buf, size_t size)
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____$i32,%eax SYS_getcwd");
asm ("int____$0x80");
}

160
lib/linux-gcc.c Normal file
View file

@ -0,0 +1,160 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <mlibc.h>
#include <stdlib.h>
#if !POSIX
int
read (int fd, void* buf, size_t n)
{
#if !__TINYC__
int r;
//syscall (SYS_write, fd, s, n));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"movl $0x3,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd), "" (buf), "" (n)
: "eax", "ebx", "ecx", "edx"
);
return r;
#endif
}
int
open (char const *s, int flags, ...)
{
#if !__TINYC__
int mode;
asm (
"mov %%ebp,%%eax\n\t"
"add $0x10,%%eax\n\t"
"mov (%%eax),%%eax\n\t"
"mov %%eax,%0\n\t"
: "=mode" (mode)
: //no inputs ""
);
int r;
//syscall (SYS_open, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"mov $0x5,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (flags), "" (mode)
: "eax", "ebx", "ecx", "edx"
);
return r;
#endif
}
int
chmod (char const *s, int mode)
{
#if !__TINYC__
int r;
//syscall (SYS_chmod, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $0x0f,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (mode)
: "eax", "ebx", "ecx"
);
return r;
#endif
}
int
access (char const *s, int mode)
{
#if !__TINYC__
int r;
//syscall (SYS_access, mode));
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $0x21,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (s), "" (mode)
: "eax", "ebx", "ecx"
);
return r;
#endif
}
void *
brk (void *p)
{
#if !__TINYC__
void *r;
asm (
"mov %1,%%ebx\n\t"
"mov $0x2d,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (p)
: "eax", "ebx"
);
return r;
#endif
}
int
fsync (int fd)
{
#if !__TINYC__
int r;
//syscall (SYS_fsync, fd));
asm (
"mov %1,%%ebx\n\t"
"mov $0x76, %%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd)
: "eax", "ebx"
);
return r;
#endif
}
#endif //!POSIX

79
lib/linux-mes.c Normal file
View file

@ -0,0 +1,79 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
void
read ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____0x8(%ebp),%edx !16");
asm ("mov____$i32,%eax SYS_read");
asm ("int____$0x80");
}
void
open ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____0x8(%ebp),%edx !16");
asm ("mov____$i32,%eax SYS_open");
asm ("int____$0x80");
}
void
chmod ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____$i32,%eax SYS_chmod");
asm ("int____$0x80");
}
void
access ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____$i32,%eax SYS_access");
asm ("int____$0x80");
}
void
brk ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_brk");
asm ("int____$0x80");
}
void
fsync ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_fsync");
asm ("int____$0x80");
}

22
lib/mini-libc-gcc.c Normal file
View file

@ -0,0 +1,22 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mini-linux-gcc.c>
#include <mini-libc.c>

View file

@ -18,46 +18,7 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
void #include <mini-linux-mes.c>
exit ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_exit"); typedef unsigned long size_t;
asm ("int____$0x80"); #include <mini-libc.c>
}
void
write ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____0x8(%ebp),%edx !16");
asm ("mov____$i32,%eax SYS_write");
asm ("int____$0x80");
}
int
strlen (char const* s)
{
int i = 0;
while (s[i]) i++;
return i;
}
int
eputs (char const* s)
{
int i = strlen (s);
write (2, s, i);
return 0;
}
int
puts (char const* s)
{
int i = strlen (s);
write (1, s, i);
return 0;
}

43
lib/mini-libc.c Normal file
View file

@ -0,0 +1,43 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
size_t
strlen (char const* s)
{
int i = 0;
while (s[i]) i++;
return i;
}
int
eputs (char const* s)
{
int i = strlen (s);
write (2, s, i);
return 0;
}
int
puts (char const* s)
{
int i = strlen (s);
write (1, s, i);
return 0;
}

83
lib/mini-linux-gcc.c Normal file
View file

@ -0,0 +1,83 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#if !POSIX
void
exit (int code)
{
#if !__TINYC__
asm (
"mov %0,%%ebx\n\t"
"mov $1,%%eax\n\t"
"int $0x80\n\t"
: // no outputs "=" (r)
: "" (code)
);
#else // __TINYC__
asm (
"mov %0,%%ebx\n\t"
"mov $1,%%eax\n\t"
"int $128\n\t"
: // no outputs "=" (r)
: "Ir" (code)
);
#endif // __TINYC__
// not reached
exit (0);
}
int
write (int fd, char const* s, int n)
{
int r;
#if __GNUC__
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"mov $0x04,%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd), "" (s), "" (n)
: "eax", "ebx", "ecx", "edx"
);
//syscall (SYS_write, fd, s, n));
#elif __TINYC__
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov %3,%%edx\n\t"
"mov $4, %%eax\n\t"
"int $128\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "Ir" (fd), "Ir" (s), "Ir" (n)
: "eax", "ebx", "ecx"//, "edx"
);
#endif
return r;
}
#endif //!POSIX

39
lib/mini-linux-mes.c Normal file
View file

@ -0,0 +1,39 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* 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.
*
* 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
void
exit ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____$i32,%eax SYS_exit");
asm ("int____$0x80");
}
void
write ()
{
asm ("mov____0x8(%ebp),%ebx !8");
asm ("mov____0x8(%ebp),%ecx !12");
asm ("mov____0x8(%ebp),%edx !16");
asm ("mov____$i32,%eax SYS_write");
asm ("int____$0x80");
}

View file

@ -85,7 +85,7 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
;;(foo (format (current-error-port) "bin[~s .c] base=~s\n" input-file-name base-name)) ;;(foo (format (current-error-port) "bin[~s .c] base=~s\n" input-file-name base-name))
(suffix (cond ((not libc) ".0-guile") (suffix (cond ((not libc) ".0-guile")
((eq? libc libc-mes.hex2) ".guile") ((eq? libc libc-mes.hex2) ".guile")
((eq? libc libc-mes+tcc.hex2) ".tcc-guile") ((eq? libc libc+tcc-mes.hex2) ".tcc-guile")
(else ".mini-guile"))) (else ".mini-guile")))
(target-file-name (string-append base-name suffix)) (target-file-name (string-append base-name suffix))
(hex2-target (m1.as input-file-name #:m1 m1 #:cc cc #:defines defines #:includes includes #:dependencies dependencies))) (hex2-target (m1.as input-file-name #:m1 m1 #:cc cc #:defines defines #:includes includes #:dependencies dependencies)))
@ -103,8 +103,8 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
(define mini-libc-mes.hex2 (m1.as "lib/mini-libc-mes.c")) (define mini-libc-mes.hex2 (m1.as "lib/mini-libc-mes.c"))
(add-target mini-libc-mes.hex2) (add-target mini-libc-mes.hex2)
(define libc-mes+tcc.hex2 (m1.as "lib/libc-mes+tcc.c")) (define libc+tcc-mes.hex2 (m1.as "lib/libc+tcc-mes.c"))
(add-target libc-mes+tcc.hex2) (add-target libc+tcc-mes.hex2)
(add-target (bin.mescc "stage0/exit-42.c" #:libc #f)) (add-target (bin.mescc "stage0/exit-42.c" #:libc #f))
(add-target (check "stage0/exit-42.0-guile" #:exit 42)) (add-target (check "stage0/exit-42.0-guile" #:exit 42))
@ -128,16 +128,17 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
(add-target (bin.mescc (string-append "scaffold/tests/" name ".c") #:libc libc #:includes includes)) (add-target (bin.mescc (string-append "scaffold/tests/" name ".c") #:libc libc #:includes includes))
(add-target (check (string-append "scaffold/tests/" name "." (cond ((not libc) "0-") (add-target (check (string-append "scaffold/tests/" name "." (cond ((not libc) "0-")
((eq? libc mini-libc-mes.hex2) "mini-") ((eq? libc mini-libc-mes.hex2) "mini-")
((eq? libc libc-mes+tcc.hex2) "tcc-") ((eq? libc libc+tcc-mes.hex2) "tcc-")
(else "")) "guile") #:exit exit))) (else "")) "guile") #:exit exit)))
(add-target (compile.gcc "lib/crt1.c" #:libc #f)) (add-target (compile.gcc "lib/crt1.c" #:libc #f))
(add-target (compile.gcc "lib/libc-gcc.c" #:libc #f)) (add-target (compile.gcc "lib/libc-gcc.c" #:libc #f))
(add-target (compile.gcc "lib/libc-gcc+tcc.c" #:libc #f)) (add-target (compile.gcc "lib/libc+tcc-gcc.c" #:libc #f))
;;(add-target (compile.gcc "lib/libc+tcc-mes.c" #:libc #f))
;;(add-scaffold-test "t" #:libc mini-libc-mes.hex2) ;;(add-scaffold-test "t" #:libc mini-libc-mes.hex2)
(add-scaffold-test "t") (add-scaffold-test "t")
;;(add-scaffold-test "t" #:libc libc-mes+tcc.hex2) ;;(add-scaffold-test "t" #:libc libc+tcc-mes.hex2)
;; tests/00: exit, functions without libc ;; tests/00: exit, functions without libc
(add-scaffold-test "00-exit-0" #:libc #f) (add-scaffold-test "00-exit-0" #:libc #f)
@ -257,15 +258,15 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
;; tests/80: and beyond tinycc; building GNU GCC and dependencies ;; tests/80: and beyond tinycc; building GNU GCC and dependencies
(for-each (for-each
(cut add-scaffold-test <> #:libc libc-mes+tcc.hex2 #:libc-gcc libc-gcc+tcc.mlibc-o) (cut add-scaffold-test <> #:libc libc+tcc-mes.hex2 #:libc-gcc libc+tcc-gcc.mlibc-o)
'("80-setjmp")) '("80-setjmp"))
(add-target (group "check-scaffold-tests/8" #:dependencies (filter (target-prefix? "check-scaffold/tests/8") %targets))) (add-target (group "check-scaffold-tests/8" #:dependencies (filter (target-prefix? "check-scaffold/tests/8") %targets)))
(add-target (group "check-scaffold-tests" #:dependencies (filter (target-prefix? "check-scaffold/tests") %targets))) (add-target (group "check-scaffold-tests" #:dependencies (filter (target-prefix? "check-scaffold/tests") %targets)))
(add-target (cpp.mescc "lib/libc-mes+tcc.c")) (add-target (cpp.mescc "lib/libc+tcc-mes.c"))
(add-target (compile.mescc "lib/libc-mes+tcc.c")) (add-target (compile.mescc "lib/libc+tcc-mes.c"))
(define* (add-tcc-test name) (define* (add-tcc-test name)
(add-target (bin.gcc (string-append "scaffold/tinycc/" name ".c") #:libc libc-gcc.mlibc-o #:includes '("scaffold/tinycc"))) (add-target (bin.gcc (string-append "scaffold/tinycc/" name ".c") #:libc libc-gcc.mlibc-o #:includes '("scaffold/tinycc")))
@ -610,14 +611,14 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
(add-target (install "lib/crt1.hex2" #:dir "lib")) (add-target (install "lib/crt1.hex2" #:dir "lib"))
(add-target (install "lib/libc-mes.M1" #:dir "lib")) (add-target (install "lib/libc-mes.M1" #:dir "lib"))
(add-target (install "lib/libc-mes.hex2" #:dir "lib")) (add-target (install "lib/libc-mes.hex2" #:dir "lib"))
(add-target (install "lib/libc-mes+tcc.M1" #:dir "lib")) (add-target (install "lib/libc+tcc-mes.M1" #:dir "lib"))
(add-target (install "lib/libc-mes+tcc.hex2" #:dir "lib")) (add-target (install "lib/libc+tcc-mes.hex2" #:dir "lib"))
(add-target (install "lib/mini-libc-mes.M1" #:dir "lib")) (add-target (install "lib/mini-libc-mes.M1" #:dir "lib"))
(add-target (install "lib/mini-libc-mes.hex2" #:dir "lib")) (add-target (install "lib/mini-libc-mes.hex2" #:dir "lib"))
(add-target (install "lib/crt1.mlibc-o" #:dir "lib")) (add-target (install "lib/crt1.mlibc-o" #:dir "lib"))
(add-target (install "lib/libc-gcc.mlibc-o" #:dir "lib")) (add-target (install "lib/libc-gcc.mlibc-o" #:dir "lib"))
(add-target (install "lib/libc-gcc+tcc.mlibc-o" #:dir "lib")) (add-target (install "lib/libc+tcc-gcc.mlibc-o" #:dir "lib"))
(for-each (for-each
(lambda (f) (lambda (f)

View file

@ -28,14 +28,14 @@ unsigned long long
strtoull (char const *p, char **end, int base) strtoull (char const *p, char **end, int base)
{ {
*end = p; *end = p;
return _atoi (end, base); return abtoi (end, base);
} }
int int
test () test ()
{ {
char *p = "42foo\n"; char *p = "42foo\n";
int n = _atoi (&p, 0); int n = abtoi (&p, 0);
if (n != 42) return 1; if (n != 42) return 1;
eputs (p); eputs (p);
if (strcmp (p, "foo\n")) return 2; if (strcmp (p, "foo\n")) return 2;

View file

@ -18,10 +18,6 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#if !__MESC__
#define fputs fdputs
#endif
int g_depth; int g_depth;
SCM fdisplay_ (SCM, int); SCM fdisplay_ (SCM, int);