From 26e9dd14e3d6264ba4e7aca701184a1ad9a8226d Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Thu, 16 Nov 2017 22:11:07 +0100 Subject: [PATCH] mlibc: Tinycc support: support, most syscalls stubbed. * mlibc/libc-gcc.c (exit, write)[__TINYC__]: tcc-compatible gcc-style asm implementation. (read,open,access,brk,fsync)[__TINYC__]: Stub body. * mlibc/libc-gcc+tcc.c (close,unlink, lseek, getcwd)[__TINYC__]: Stub body. * mlibc/libc-mes+tcc.c: Support tinycc. * mlibc/include/00-test.i (main) [__TINYC__]: Support tcc. --- mlibc/include/00-test.i | 10 +++++++-- mlibc/libc-gcc+tcc.c | 8 +++++++ mlibc/libc-gcc.c | 46 +++++++++++++++++++++++++++++++++++------ mlibc/libc-mes+tcc.c | 2 +- 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/mlibc/include/00-test.i b/mlibc/include/00-test.i index 3e5f2ac2..674bb0fd 100644 --- a/mlibc/include/00-test.i +++ b/mlibc/include/00-test.i @@ -28,11 +28,17 @@ main () asm ("mov____%eax,%ebx"); asm ("mov____$i32,%eax SYS_exit"); asm ("int____$0x80"); -#else // !__MESC__ +#elif __GNUC__ asm ("mov %0,%%ebx" : // no outputs : "" (r)); asm ("mov $1,%eax"); asm ("int $0x80"); -#endif +#elif __TINYC__ + asm ("mov %0,%%ebx" + : // no outputs + : "Ir" (r)); + asm ("mov $1,%eax"); + asm ("int $0x80"); +#endif // __GNUC__ } diff --git a/mlibc/libc-gcc+tcc.c b/mlibc/libc-gcc+tcc.c index ebdca80b..d6ed603b 100644 --- a/mlibc/libc-gcc+tcc.c +++ b/mlibc/libc-gcc+tcc.c @@ -44,6 +44,7 @@ int errno; int close (int fd) { +#if !__TINYC__ int r; asm ( "mov %0,%%ebx\n\t" @@ -53,11 +54,13 @@ close (int fd) : "" (fd) ); return r; +#endif } int unlink (char const *file_name) { +#if !__TINYC__ int r; asm ( "mov %0,%%ebx\n\t" @@ -67,11 +70,13 @@ unlink (char const *file_name) : "" (file_name) ); return r; +#endif } off_t lseek (int fd, off_t offset, int whence) { +#if !__TINYC__ int r; asm ( "mov %1,%%ebx\n\t" @@ -87,11 +92,13 @@ lseek (int fd, off_t offset, int whence) : "eax", "ebx", "ecx", "edx" ); return r; +#endif } char * getcwd (char *buf, size_t size) { +#if !__TINYC__ int r; asm ( "mov %1,%%ebx\n\t" @@ -106,6 +113,7 @@ getcwd (char *buf, size_t size) : "eax", "ebx", "ecx" ); return r; +#endif } int diff --git a/mlibc/libc-gcc.c b/mlibc/libc-gcc.c index fdfaa44b..303d4e3e 100644 --- a/mlibc/libc-gcc.c +++ b/mlibc/libc-gcc.c @@ -18,8 +18,6 @@ * along with Mes. If not, see . */ -int g_stdin = 0; - #include #include @@ -27,18 +25,28 @@ int g_stdin = 0; #include #endif -#if __GNUC__ && !POSIX +#if (__GNUC__ || __TINYC__) && !POSIX void exit (int code) { +#if !__TINYC__ asm ( "mov %0,%%ebx\n\t" "mov $1,%%eax\n\t" - "int $0x80" + "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); } @@ -46,6 +54,7 @@ exit (int code) int read (int fd, void* buf, size_t n) { +#if !__TINYC__ int r; //syscall (SYS_write, fd, s, n)); asm ( @@ -62,31 +71,49 @@ read (int fd, void* buf, size_t n) : "eax", "ebx", "ecx", "edx" ); return r; +#endif } int write (int fd, char const* s, int n) { int r; - //syscall (SYS_write, fd, s, n)); +#if __GNUC__ asm ( "mov %1,%%ebx\n\t" "mov %2,%%ecx\n\t" "mov %3,%%edx\n\t" - "mov $0x4, %%eax\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" @@ -110,11 +137,13 @@ open (char const *s, int flags, ...) : "eax", "ebx", "ecx", "edx" ); return r; +#endif } int access (char const *s, int mode) { +#if !__TINYC__ int r; //syscall (SYS_access, mode)); asm ( @@ -128,11 +157,13 @@ access (char const *s, int mode) : "eax", "ebx", "ecx" ); return r; +#endif } void * brk (void *p) { +#if !__TINYC__ void *r; asm ( "mov %1,%%ebx\n\t" @@ -146,11 +177,13 @@ brk (void *p) : "eax", "ebx" ); return r; +#endif } int fsync (int fd) { +#if !__TINYC__ int r; //syscall (SYS_fsync, fd)); asm ( @@ -164,6 +197,7 @@ fsync (int fd) : "eax", "ebx" ); return r; +#endif } int diff --git a/mlibc/libc-mes+tcc.c b/mlibc/libc-mes+tcc.c index 1ef52069..5aff1360 100644 --- a/mlibc/libc-mes+tcc.c +++ b/mlibc/libc-mes+tcc.c @@ -29,7 +29,7 @@ #include #include -#if !__GNUC__ +#if !__GNUC__ && !__TINYC__ #include int errno;