mescc: Tinycc support: pre/post-inc/dec more.

* module/language/c99/compiler.mes (expr-add): New function.
  (expr->pointer): New function.
  (expr->accu): Use it to support broader pre/post-inc/dec..
* scaffold/tests/72-typedef-struct-def.c (test): Test it.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-17 07:33:38 +02:00
parent 59e663021a
commit 0fa425ed5e
2 changed files with 60 additions and 7 deletions

View file

@ -424,6 +424,17 @@
(if local (wrap-as (i386:local-add (local:id local) n)) (if local (wrap-as (i386:local-add (local:id local) n))
(list (i386:label-mem-add `(#:address ,o) n)))))) (list (i386:label-mem-add `(#:address ,o) n))))))
(define (expr-add info)
(lambda (o n)
(let* ((info ((expr->accu* info) o))
(info (append-text info (wrap-as (i386:accu-mem-add n)))))
info)))
(define (expr->pointer info o)
(pmatch o
((p-expr (ident ,name)) (ident->pointer info name)) ;; FIXME
(_ 0)))
(define (ident-address-add info) (define (ident-address-add info)
(lambda (o n) (lambda (o n)
(let ((local (assoc-ref (.locals info) o))) (let ((local (assoc-ref (.locals info) o)))
@ -647,20 +658,49 @@
((ident-add info) name size))))) ((ident-add info) name size)))))
((post-dec (p-expr (ident ,name))) ((post-dec (p-expr (ident ,name)))
(or (assoc-ref locals name) (begin (stderr "i-- ~a\n" name) (error "undefined identifier: " name)))
(append-text info (append ((ident->accu info) name) (append-text info (append ((ident->accu info) name)
((ident-add info) name -1)))) ((ident-add info) name -1))))
((pre-inc (p-expr (ident ,name))) ((pre-inc (p-expr (ident ,name)))
(or (assoc-ref locals name) (begin (stderr "++i ~a\n" name) (error "undefined identifier: " name)))
(append-text info (append ((ident-add info) name 1) (append-text info (append ((ident-add info) name 1)
((ident->accu info) name)))) ((ident->accu info) name))))
((pre-dec (p-expr (ident ,name))) ((pre-dec (p-expr (ident ,name)))
(or (assoc-ref locals name) (begin (stderr "--i ~a\n" name) (error "undefined identifier: " name)))
(append-text info (append ((ident-add info) name -1) (append-text info (append ((ident-add info) name -1)
((ident->accu info) name)))) ((ident->accu info) name))))
((post-inc ,expr)
(let* ((info (append ((expr->accu info) expr)))
(info (append-text info (wrap-as (i386:push-accu))))
(ptr (expr->pointer info expr))
(size (if (> ptr 0) 4 1))
(info ((expr-add info) expr size))
(info (append-text info (wrap-as (i386:pop-accu)))))
info))
((post-dec ,expr)
(let* ((info (append ((expr->accu info) expr)))
(info (append-text info (wrap-as (i386:push-accu))))
(ptr (expr->pointer info expr))
(size (if (> ptr 0) 4 1))
(info ((expr-add info) expr (- size)))
(info (append-text info (wrap-as (i386:pop-accu)))))
info))
((pre-inc ,expr)
(let* ((ptr (expr->pointer info expr))
(size (if (> ptr 0) 4 1))
(info ((expr-add info) expr size))
(info (append ((expr->accu info) expr))))
info))
((pre-dec ,expr)
(let* ((ptr (expr->pointer info expr))
(size (if (> ptr 0) 4 1))
(info ((expr-add info) expr (- size)))
(info (append ((expr->accu info) expr))))
info))
((add ,a ,b) ((binop->accu info) a b (i386:accu+base))) ((add ,a ,b) ((binop->accu info) a b (i386:accu+base)))
((sub ,a ,b) ((binop->accu info) a b (i386:accu-base))) ((sub ,a ,b) ((binop->accu info) a b (i386:accu-base)))
((bitwise-or ,a ,b) ((binop->accu info) a b (i386:accu-or-base))) ((bitwise-or ,a ,b) ((binop->accu info) a b (i386:accu-or-base)))
@ -732,12 +772,18 @@
((expr->accu info) o)) ((expr->accu info) o))
((assn-expr (de-ref (post-inc (p-expr (ident ,name)))) (op ,op) ,b) ((assn-expr (de-ref (post-inc (p-expr (ident ,name)))) (op ,op) ,b)
(let ((info ((expr->accu info) `(assn-expr (de-ref (p-expr (ident ,name))) (op ,op) ,b)))) (let* ((info ((expr->accu info) `(assn-expr (de-ref (p-expr (ident ,name))) (op ,op) ,b)))
(append-text info ((ident-add info) name 1)))) ;; FIXME: size (type (ident->type info name))
(ptr (ident->pointer info name))
(size (if (> ptr 1) 4 1)))
(append-text info ((ident-add info) name size)))) ;; FIXME: size
((assn-expr (de-ref (post-dec (p-expr (ident ,name)))) (op ,op) ,b) ((assn-expr (de-ref (post-dec (p-expr (ident ,name)))) (op ,op) ,b)
(let ((info ((expr->accu info) `(assn-expr (de-ref (p-expr (ident ,name))) (op ,op) ,b)))) (let* ((info ((expr->accu info) `(assn-expr (de-ref (p-expr (ident ,name))) (op ,op) ,b)))
(append-text info ((ident-add info) name -1)))) ;; FIXME: size (type (ident->type info name))
(ptr (ident->pointer info name))
(size (if (> ptr 1) 4 1)))
(append-text info ((ident-add info) name (- size))))) ;; FIXME: size
((assn-expr ,a (op ,op) ,b) ((assn-expr ,a (op ,op) ,b)
(let* ((info (append-text info (ast->comment o))) (let* ((info (append-text info (ast->comment o)))

View file

@ -43,6 +43,13 @@ test ()
p->i = 2; p->i = 2;
printf ("p->i=%d\n", b.i); printf ("p->i=%d\n", b.i);
p->i++;
printf ("p->i=%d\n", b.i);
p->i--;
printf ("p->i=%d\n", b.i);
bar** pp = &p; bar** pp = &p;
(*pp)->i = 3; (*pp)->i = 3;
printf ("(*pp)->i=%d\n", b.i); printf ("(*pp)->i=%d\n", b.i);