From ce980c82399a6f7ad0966ae666fba5c1b5c91a3f Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 28 Jul 2017 08:07:41 +0200 Subject: [PATCH] mescc: Tinycc support: compile tcc with TCC_IS_NATIVE. * mlibc/include/string.h (strcat): Declare. * mlibc/libc-mes+tcc.c (dlclose, dlopen, mprotect, sigaction, sigemptyset, strcat, vfprintf): Move from libc-gcc+tcc.c. * mlibc/libc-gcc+tcc.c: Remove them. * module/language/c99/compiler.mes (c99-input->full-ast): Define __i386__=1. (i386:type-alist): Parse `long long int', `unsigned short int', `unsigned long long int'. (struct-field): Support void**. (init-declr->name): (init-declr->pointer): Support function declaration. --- mlibc/include/string.h | 3 +- mlibc/libc-gcc+tcc.c | 61 ----------------------------- mlibc/libc-mes+tcc.c | 67 ++++++++++++++++++++++++++++++++ module/language/c99/compiler.mes | 14 +++++-- 4 files changed, 80 insertions(+), 65 deletions(-) diff --git a/mlibc/include/string.h b/mlibc/include/string.h index 48f5e466..9a1e1344 100644 --- a/mlibc/include/string.h +++ b/mlibc/include/string.h @@ -44,10 +44,11 @@ void *memmove (void *dest, void const *src, size_t n); void *memset (void *s, int c, size_t n); int memcmp (void const *s1, void const *s2, size_t n); -size_t strlen (char const*); +char *strcat (char *dest, char const *src); char *strchr (char const *s, int c); int strcmp (char const*, char const*); char *strcpy (char *dest, char const *src); +size_t strlen (char const*); int strncmp (char const*, char const*, size_t); char *strrchr (char const *s, int c); char *strstr (char const *haystack, char const *needle); diff --git a/mlibc/libc-gcc+tcc.c b/mlibc/libc-gcc+tcc.c index d0c3bc29..c3743c7e 100644 --- a/mlibc/libc-gcc+tcc.c +++ b/mlibc/libc-gcc+tcc.c @@ -109,67 +109,6 @@ getcwd (char *buf, size_t size) return r; } -int dlclose (void *handle) -{ - return 0; -} - -void * -dlopen (char const *filename, int flags) -{ - return 0; -} - -int -mprotect (void *addr, size_t len, int prot) -{ - return 0; -} - -int -sigaction (int signum, struct sigaction const *act, struct sigaction *oldact) -{ - return 0; -} - -int -sigemptyset (sigset_t *set) -{ - return 0; -} - -char * -strcat (char *dest, char const *src) -{ - return 0; -} - -int -vfprintf (FILE* f, char const* format, va_list ap) -{ - int fd = (int)f; - char const *p = format; - while (*p) - if (*p != '%') - putchar (*p++); - else - { - p++; - char c = *p; - switch (c) - { - case '%': {fputc (*p, fd); break;} - case 'c': {char c; c = va_arg (ap, char); fputc (c, fd); break;} - case 'd': {int d; d = va_arg (ap, int); fputs (itoa (d), fd); break;} - case 's': {char *s; s = va_arg (ap, char *); fputs (s, fd); break;} - default: {fputc (*p, fd); break;} - } - p++; - } - va_end (ap); - return 0; -} - int __udivdi3 (int a, int b) { diff --git a/mlibc/libc-mes+tcc.c b/mlibc/libc-mes+tcc.c index 2b8a0341..3c310bfd 100644 --- a/mlibc/libc-mes+tcc.c +++ b/mlibc/libc-mes+tcc.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include @@ -31,6 +33,8 @@ #define FULL_MALLOC 1 #include +int errno; + int close (int fd) { @@ -72,6 +76,18 @@ getcwd (char *buf, size_t size) #endif // !__GNUC__ +int +dlclose (void *handle) +{ + return 0; +} + +void * +dlopen (char const *filename, int flags) +{ + return 0; +} + int execvp (char const *file, char *const argv[]) { @@ -183,6 +199,12 @@ memcmp (void const *s1, void const *s2, size_t n) return 0; } +int +mprotect (void *addr, size_t len, int prot) +{ + return 0; +} + void qsort (void *base, size_t nmemb, size_t size, int (*compar)(void const *, void const *)) { @@ -200,6 +222,18 @@ setjmp (jmp_buf env) return 0; } +int +sigaction (int signum, struct sigaction const *act, struct sigaction *oldact) +{ + return 0; +} + +int +sigemptyset (sigset_t *set) +{ + return 0; +} + int snprintf(char *str, size_t size, char const *format, ...) { @@ -212,6 +246,13 @@ sscanf (char const *str, const char *format, ...) return 0; } +char * +strcat (char *dest, char const *src) +{ + eputs ("strcat stub\n"); + return 0; +} + char * strchr (char const *s, int c) { @@ -285,3 +326,29 @@ realloc (void *ptr, size_t size) } return new; } + +int +vfprintf (FILE* f, char const* format, va_list ap) +{ + int fd = (int)f; + char const *p = format; + while (*p) + if (*p != '%') + putchar (*p++); + else + { + p++; + char c = *p; + switch (c) + { + case '%': {fputc (*p, fd); break;} + case 'c': {char c; c = va_arg (ap, char); fputc (c, fd); break;} + case 'd': {int d; d = va_arg (ap, int); fputs (itoa (d), fd); break;} + case 's': {char *s; s = va_arg (ap, char *); fputs (s, fd); break;} + default: {fputc (*p, fd); break;} + } + p++; + } + va_end (ap); + return 0; +} diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index ce602786..979f44fd 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -57,6 +57,7 @@ (parse-c99 #:inc-dirs (append includes (cons* include "mlibc/include" "mlibc" (or (and=> (getenv "C_INCLUDE_PATH") (cut string-split <> #\:)) '()))) #:cpp-defs `( + "__i386__=1" "POSIX=0" "_POSIX_SOURCE=0" "__MESC__=1" @@ -1117,14 +1118,17 @@ ("int" . ,(make-type 'builtin 4 0 #f)) ("long" . ,(make-type 'builtin 4 0 #f)) ("long long" . ,(make-type 'builtin 8 0 #f)) + ("long long int" . ,(make-type 'builtin 8 0 #f)) ("void" . ,(make-type 'builtin 4 0 #f)) ;; FIXME sign ("unsigned char" . ,(make-type 'builtin 1 0 #f)) ("unsigned short" . ,(make-type 'builtin 2 0 #f)) + ("unsigned short int" . ,(make-type 'builtin 2 0 #f)) ("unsigned" . ,(make-type 'builtin 4 0 #f)) ("unsigned int" . ,(make-type 'builtin 4 0 #f)) ("unsigned long" . ,(make-type 'builtin 4 0 #f)) - ("unsigned long long" . ,(make-type 'builtin 8 0 #f)))) + ("unsigned long long" . ,(make-type 'builtin 8 0 #f)) + ("unsigned long long int" . ,(make-type 'builtin 8 0 #f)))) (define (field:name o) (pmatch o @@ -1442,10 +1446,12 @@ (list name type 4 1)) ((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name))))) (list name type 4 2)) + ((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name))))) + (list name "void" 4 2)) ((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name))))) - (list name '(void) 4 1)) + (list name "void" 4 1)) ((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,param-list))))) - (list name '(void) 4 1)) + (list name "void" 4 1)) ((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name))))) (list name type 4 1)) ((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (array-of (ident ,name) ,count))))) @@ -1539,6 +1545,7 @@ ((ptr-declr ,pointer (ident ,name)) name) ((array-of (ident ,name)) name) ((array-of (ident ,name) ,index) name) + ((ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,params)) name) ((ptr-declr (pointer) (array-of (ident ,name))) name) ((ptr-declr (pointer) (array-of (ident ,name) (p-expr ,size))) name) (_ (error "init-declr->name unsupported: " o)))) @@ -1549,6 +1556,7 @@ ((ptr-declr ,pointer (ident ,name)) (ptr-declr->pointer pointer)) ((array-of (ident ,name) ,index) -1) ((array-of (ident ,name)) -1) + ((ftn-declr (scope (ptr-declr ,pointer (ident ,name))) (param-list . ,params)) (ptr-declr->pointer pointer)) ((ptr-declr (pointer) (array-of (ident ,name))) -2) ((ptr-declr (pointer) (array-of (ident ,name) (p-expr ,size))) -2) (_ (error "init-declr->pointer unsupported: " o))))