diff --git a/GNUmakefile b/GNUmakefile index 6d6ad425..96178863 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,4 +1,5 @@ SHELL:=bash +export SHELL QUIET:=@ default: all diff --git a/libc/mlibc.c b/libc/mlibc.c index c1ec677c..8688d7fd 100644 --- a/libc/mlibc.c +++ b/libc/mlibc.c @@ -18,6 +18,7 @@ * along with Mes. If not, see . */ +char **g_environment = 0; int g_stdin = 0; #define EOF -1 @@ -51,12 +52,6 @@ exit (int code) exit (0); } -char const* -getenv (char const* p) -{ - return 0; -} - int read (int fd, void* buf, size_t n) { @@ -290,6 +285,28 @@ ungetc (int c, int fd) return c; } +char const* itoa (int); + +int +strncmp (char const* a, char const* b, int length) +{ + while (*a && *b && *a == *b && --length) {a++;b++;} + return *a - *b; +} + +char const* +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) { diff --git a/libc/mstart.c b/libc/mstart.c index f50187a5..6e6aaeb4 100644 --- a/libc/mstart.c +++ b/libc/mstart.c @@ -22,21 +22,33 @@ void _start () { + // char **; + asm ( + "mov %%ebp,%%eax\n\t" + "addl $4,%%eax\n\t" + "movzbl (%%eax),%%eax\n\t" + "addl $3,%%eax\n\t" + "shl $2,%%eax\n\t" + "add %%ebp,%%eax\n\t" + "movl %%eax,%0\n\t" + : "=g_environment" (g_environment) + : //no inputs "" + ); int r; asm ( - "mov %%ebp,%%eax\n\t" - "addl $8,%%eax\n\t" - "push %%eax\n\t" + "mov %%ebp,%%eax\n\t" + "addl $8,%%eax\n\t" + "push %%eax\n\t" - "mov %%ebp,%%eax\n\t" - "addl $4,%%eax\n\t" - "movzbl (%%eax),%%eax\n\t" - "push %%eax\n\t" + "mov %%ebp,%%eax\n\t" + "addl $4,%%eax\n\t" + "movzbl (%%eax),%%eax\n\t" + "push %%eax\n\t" - "call main\n\t" - "movl %%eax,%0\n\t" + "call main\n\t" + "movl %%eax,%0\n\t" : "=r" (r) - : //no inputs "" (&main) + : //no inputs "" ); exit (r); } diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index 3231f21c..331c7edc 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -377,14 +377,13 @@ (if global (let ((ptr (ident->pointer info o))) (case ptr - ((10) - (list (lambda (f g ta t d) - (i386:global->accu (+ (data-offset o g) d))))) + ;; ((1) + ;; (list (lambda (f g ta t d) + ;; (i386:global->accu (+ (data-offset o g) d))))) (else (list (lambda (f g ta t d) (append (i386:value->accu (+ (data-offset o g) d)))))))) (error "TODO ident-address->accu" o)))))) - (define (ident-address->base info) (lambda (o) (let ((local (assoc-ref (.locals info) o)) diff --git a/module/mes/libc-i386.mes b/module/mes/libc-i386.mes index 06baffcc..eb70a900 100644 --- a/module/mes/libc-i386.mes +++ b/module/mes/libc-i386.mes @@ -125,10 +125,19 @@ " 0x89 0xe8" ; mov %ebp,%eax " 0x83 0xc0 0x08" ; add $0x8,%eax " 0x50" ; push %eax + " 0x89 0xe8" ; mov %ebp,%eax " 0x83 0xc0 0x04" ; add $0x4,%eax " 0x0f 0xb6 0x00" ; movzbl (%eax),%eax " 0x50" ; push %eax + + " 0x89 0xe8" ; mov %ebp,%eax + " 0x83 0xc0 0x04" ; add $0x4,%eax + " 0x0f 0xb6 0x00" ; movzbl (%eax),%eax + " 0x83 0xc0 0x03" ; add $0x3,%eax + " 0xc1 0xe0 0x02" ; shl $0x2,%eax + " 0x01 0xe8" ; add %ebp,%eax + " 0x50" ; push %eax )) (define i386:libc diff --git a/module/mes/libc.mes b/module/mes/libc.mes index 62ca4d02..993699fa 100644 --- a/module/mes/libc.mes +++ b/module/mes/libc.mes @@ -34,7 +34,24 @@ (define _start (let* ((argc-argv (i386:_start)) (ast (with-input-from-string - (string-append "int _start () {int i;asm(\"" argc-argv "\");i=main ();exit (i);}") + (string-append " +char **g_environment; +char ** +_env (char **e) +{ + return e; +} + +int +_start () +{ + asm(\"" argc-argv "\"); + g_environment = _env (); + asm (\".byte 0x58\"); + int r = main (); + exit (r); +} +") parse-c99))) ast)) @@ -306,6 +323,43 @@ realloc (int *p, int size) parse-c99))) ast)) +(define strncmp + (let* ((ast (with-input-from-string + " +int +strncmp (char const* a, char const* b, int length) +{ + while (*a && *b && *a == *b && --length) {a++;b++;} + return *a - *b; +} +" +;;paredit:" + parse-c99))) + ast)) + +(define c:getenv + (let* ((ast (with-input-from-string + " +char **g_environment; +char const* +getenv (char const* s) +{ + char **p = g_environment; + p = *g_environment; + int length = strlen (s); + while (*p) + { + if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1); + p++; + } + return 0; +} +" +;;paredit:" + parse-c99))) + ast)) + + (define libc (list strlen @@ -322,4 +376,6 @@ realloc (int *p, int size) isdigit malloc realloc + strncmp + c:getenv )) diff --git a/scaffold/cons-mes.c b/scaffold/cons-mes.c index 165944d2..7e4784c6 100644 --- a/scaffold/cons-mes.c +++ b/scaffold/cons-mes.c @@ -23,6 +23,7 @@ #endif #if __MESC__ +char **g_environment; int g_stdin = 0; #define assert(x) ((x) ? (void)0 : assert_fail (#x)) #endif diff --git a/scaffold/hello.c b/scaffold/hello.c index 7cbb26c2..882f9c7b 100644 --- a/scaffold/hello.c +++ b/scaffold/hello.c @@ -18,6 +18,12 @@ * along with Mes. If not, see . */ +#if __MESC__ +char **g_environment; +int g_stdin = 0; +#define assert(x) ((x) ? (void)0 : assert_fail (#x)) +#endif + #if !__MESC__ #include "mlibc.c" #endif diff --git a/scaffold/m.c b/scaffold/m.c index 2d12be37..27020261 100644 --- a/scaffold/m.c +++ b/scaffold/m.c @@ -18,6 +18,12 @@ * along with Mes. If not, see . */ +#if __MESC__ +char **g_environment; +int g_stdin = 0; +#define assert(x) ((x) ? (void)0 : assert_fail (#x)) +#endif + #if !__MESC__ #include "mlibc.c" #endif diff --git a/scaffold/micro-mes.c b/scaffold/micro-mes.c index 01d9b589..8cafd8dc 100644 --- a/scaffold/micro-mes.c +++ b/scaffold/micro-mes.c @@ -22,6 +22,12 @@ #error "POSIX not supported" #endif +#if __MESC__ +char **g_environment; +int g_stdin = 0; +#define assert(x) ((x) ? (void)0 : assert_fail (#x)) +#endif + #if !__MESC__ #include "mlibc.c" #endif diff --git a/scaffold/mini-mes.c b/scaffold/mini-mes.c index 1399619f..6ba40f23 100644 --- a/scaffold/mini-mes.c +++ b/scaffold/mini-mes.c @@ -23,6 +23,7 @@ #endif #if __MESC__ +char **g_environment; int g_stdin = 0; #define assert(x) ((x) ? (void)0 : assert_fail (#x)) #endif diff --git a/scaffold/scaffold.make b/scaffold/scaffold.make index 7dda12f0..91e81b5b 100644 --- a/scaffold/scaffold.make +++ b/scaffold/scaffold.make @@ -42,8 +42,8 @@ include make/check.make TARGET:=hello.mlibc C_FILES:=$(DIR)/hello.c INCLUDES:=libc -C_FLAGS:=-nostdinc -LD_FLAGS:=-nostdlib +C_FLAGS:=-nostdinc -g +LD_FLAGS:=-nostdlib -g CROSS:=$(CC32:%gcc=%) include make/bin.make diff --git a/scaffold/t.c b/scaffold/t.c index 129605ab..00550383 100644 --- a/scaffold/t.c +++ b/scaffold/t.c @@ -245,6 +245,9 @@ array_test (char **e) puts ("t: *(p + 1)\n"); if (*(*p + 1) != 'e') return 1; + puts ("t: getenv ()"); + if (!getenv ("SHELL")) return 1; + return read_test (); } diff --git a/scaffold/tiny-mes.c b/scaffold/tiny-mes.c index 831aa0a9..8a8e4fac 100644 --- a/scaffold/tiny-mes.c +++ b/scaffold/tiny-mes.c @@ -22,6 +22,12 @@ #error "POSIX not supported" #endif +#if __MESC__ +char **g_environment; +int g_stdin = 0; +#define assert(x) ((x) ? (void)0 : assert_fail (#x)) +#endif + #if !__MESC__ #include "mlibc.c" #endif diff --git a/src/mes.c b/src/mes.c index f1e2a387..da8c201a 100644 --- a/src/mes.c +++ b/src/mes.c @@ -19,6 +19,7 @@ */ #if __MESC__ +char **g_environment; int g_stdin = 0; #define assert(x) ((x) ? (void)0 : assert_fail (#x)) #endif diff --git a/src/posix.c b/src/posix.c index 3219c06f..3c1cce95 100644 --- a/src/posix.c +++ b/src/posix.c @@ -87,12 +87,9 @@ string_to_cstring (SCM s) SCM getenv_ (SCM s) ///((name . "getenv")) { -#if _POSIX_SOURCE - char *p = getenv (string_to_cstring (s)); + char *p; + p = getenv (string_to_cstring (s)); return p ? MAKE_STRING (cstring_to_list (p)) : cell_f; -#else - return cell_t; -#endif } SCM