From 7e795763f3a3848017220e2785f025360586d917 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Tue, 25 Jul 2017 11:11:26 +0200 Subject: [PATCH] mescc: Tinycc support: bugfix for char **pp = *p. * module/language/c99/compiler.mes (ptr-declr->pointer): Grok ***. (decl->info): Bugfix for char **pp = *p, cleanup. * mlibc/libc-mes.c (getenv): Update for bugfix. * scaffold/tests/23-pointer.c (test): Test it. --- mlibc/libc-mes.c | 2 +- module/language/c99/compiler.mes | 21 +-------------------- scaffold/tests/23-pointer.c | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/mlibc/libc-mes.c b/mlibc/libc-mes.c index ddc71ce3..259f1394 100644 --- a/mlibc/libc-mes.c +++ b/mlibc/libc-mes.c @@ -338,7 +338,7 @@ char **g_environment; char * getenv (char const* s) { - char **p = *g_environment; + char **p = g_environment; int length = strlen (s); while (*p) { diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index bc6f507c..3cb94c6e 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -1530,6 +1530,7 @@ (pmatch o ((pointer) 1) ((pointer (pointer)) 2) + ((pointer (pointer (pointer))) 3) (_ (error "ptr-declr->pointer unsupported: " o)))) (define (init-declr->name o) @@ -1747,26 +1748,6 @@ (clone info #:locals (add-local locals name type 0)) (clone info #:globals (append globals (list (ident->global-entry name type 0 0))))))) - ;; char **p; - ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer (pointer)) (ident ,name))))) - (if (.function info) - (let ((locals (add-local locals name type 2))) - (clone info #:locals locals)) - (let ((globals (append globals (list (ident->global-entry name type 2 0))))) - (clone info #:globals globals)))) - - ;; char **p = *x; - ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer (pointer)) (ident ,name)) (initzer (de-ref (p-expr (ident ,value))))))) - (let ((type (decl->ast-type type)) - (info (append-text info (ast->comment o)))) - (if (.function info) - (let* ((locals (add-local locals name type 2)) - (info (clone info #:locals locals))) - (append-text info (append ((ident-address->accu info) value) - (wrap-as (i386:mem->accu)) - ((accu->ident info) name)))) - (error "TODO" o)))) - ;; struct foo bar[2]; ;; char arena[20000]; ((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (array-of (ident ,name) ,count)))) diff --git a/scaffold/tests/23-pointer.c b/scaffold/tests/23-pointer.c index 3cc19a33..c8e2694f 100644 --- a/scaffold/tests/23-pointer.c +++ b/scaffold/tests/23-pointer.c @@ -28,10 +28,23 @@ test () { if (*g_chars != 'X') return 1; g_arena[0] = 'A'; - if (*g_chars != 'A') return 1; + if (*g_chars != 'A') return 2; char *x = g_arena; - if (*x++ != 'A') return 1; + if (*x++ != 'A') return 3; *x++ = 'C'; - if (g_chars[1] != 'C') return 1; + if (g_chars[1] != 'C') return 4; + + char **pp = &x; + if (**pp != 'X') return 5; + + char *p = *pp; + if (*p != 'X') return 6; + + char ***ppp = &pp; + //if (***ppp != 'X') return 7; + + char **pp2 = *ppp; + if (**pp2 != 'X') return 8; + return 0; }