From 70cb56025f430dfa9096c8ed7de8b4c3d06b853c Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 11 May 2018 13:34:45 +0200 Subject: [PATCH] mescc: Tinycc support: Valued function assign. --- module/language/c99/compiler.mes | 40 ++++++++++++++++++--------- scaffold/tests/48-function-destruct.c | 1 + scaffold/tests/49-global-static.c | 13 +++++---- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index c810ee9f..fc57bf53 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -307,6 +307,11 @@ ((decl-spec-list (type-spec ,type)) (ast->type type info)) + + ;; `typedef int size; void foo (unsigned size u) + ((decl-spec-list (type-spec ,type) (type-spec ,type2)) + (ast->type type info)) + ((assn-expr ,a ,op ,b) (ast->type a info)) @@ -410,8 +415,7 @@ (assoc-ref (.constants info) o) (assoc-ref (.functions info) o) (begin - (stderr "info=~s\n" info) - (error "ident->variable: undefined variabled:" o)))) + (error "ident->variable: undefined variable:" o)))) (define (static-global? o) ((compose global:function cdr) o)) @@ -702,13 +706,8 @@ (define (ast->comment o) (if mes? '() - (begin - (pmatch o - ;; Nyacc 0.80.42: missing (enum-ref (ident "fred")) - ((decl (decl-spec-list (type-spec (enum-ref . _))) . _) - '()) - (_ (let ((source (with-output-to-string (lambda () (pretty-print-c99 o))))) - (make-comment (string-join (string-split source #\newline) " ")))))))) + (let ((source (with-output-to-string (lambda () (pretty-print-c99 o))))) + (make-comment (string-join (string-split source #\newline) " "))))) (define (accu*n info n) (append-text info (wrap-as (case n @@ -1670,7 +1669,9 @@ ((decl . ,decl) ;;FIXME: ridiculous performance hit with mes - (let ((info (append-text info (ast->comment o)))) + ;; Nyacc 0.80.42: missing (enum-ref (ident "fred")) + (let (;;(info (append-text info (ast->comment o))) + ) (decl->info info decl))) ;; ... ((gt . _) (expr->accu o info)) @@ -1881,6 +1882,11 @@ (if (not size) data (append data (string->list (make-string (max 0 (- size (length data))) #\nul)))))) + (((initzer (p-expr (string . ,strings)))) + (let ((data (string->list (apply string-append strings)))) + (if (not size) data + (append data (string->list (make-string (max 0 (- size (length data))) #\nul)))))) + ((initzer (p-expr (fixed ,fixed))) (int->bv32 (expr->number info fixed))) @@ -2120,10 +2126,16 @@ (rank (ptr-declr->rank pointer))) (if (zero? rank) type (make-pointer type rank)))) - (((decl-spec-list (type-spec ,type)) . ,rest) + (((decl-spec-list (type-spec ,type)) . _) (ast->type type info)) - (((decl-spec-list (stor-spec ,store) (type-spec ,type)) (ftn-declr (ident _) _) _) + (((decl-spec-list (stor-spec ,store) (type-spec ,type)) . _) (ast->type type info)) + + ;; (((decl-spec-list (stor-spec ,store) (type-spec ,type)) (ftn-declr (ident _) _) _) + ;; (ast->type type info)) + ;; (((decl-spec-list (stor-spec ,store) (type-spec ,type)) (ptr-declr ,pointer (ftn-declr (ident _) _)) _) + ;; (ast->type type info)) + (_ (error "fctn-defn:get-type: not supported:" o)))) (define (ftn-declr:get-type info o) @@ -2149,7 +2161,9 @@ (text (param-list->text formals)) (locals (param-list->locals formals info)) (statement (fctn-defn:get-statement o)) - (info (clone info #:locals locals #:function name #:text text #:statics '())) + (function (cons name (make-function name type '()))) + (functions (cons function (.functions info))) + (info (clone info #:locals locals #:function name #:text text #:functions functions #:statics '())) (info (ast->info statement info)) (locals (.locals info)) (local (and (pair? locals) (car locals))) diff --git a/scaffold/tests/48-function-destruct.c b/scaffold/tests/48-function-destruct.c index 6e171690..d07e2e19 100644 --- a/scaffold/tests/48-function-destruct.c +++ b/scaffold/tests/48-function-destruct.c @@ -26,6 +26,7 @@ struct foo struct foo* test (struct foo* f) { + void (*fun) () = test; return f; } diff --git a/scaffold/tests/49-global-static.c b/scaffold/tests/49-global-static.c index e669b339..5b453f1b 100644 --- a/scaffold/tests/49-global-static.c +++ b/scaffold/tests/49-global-static.c @@ -20,16 +20,19 @@ static int sint; static int sint2, sint3; - -static int -test () +typedef unsigned int size; +static void* +test (size u) { - return 0; + void *r; + if (u) + r = test (u); } static int i = 2; int main () { - return test (); + void (*foo)() = &test; + return test (0); }