mescc: Tinycc support: bugfix foo[bar] = baz, with foo*[].

* module/language/c99/compiler.mes (expr->accu): Support size for more expressions.
  (expr->pointer):
  (expr->size): Support more expressions.
* scaffold/tests/77-pointer-assign.c (add0): Test it.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-28 18:00:46 +02:00
parent 17e0e0cab7
commit 73148c6f78
2 changed files with 37 additions and 11 deletions

View file

@ -657,8 +657,12 @@
(append-text info ((ident-add info) name size)))) (append-text info ((ident-add info) name size))))
((de-ref ,expr) ((de-ref ,expr)
(let ((info ((expr->accu info) expr))) (let* ((info ((expr->accu info) expr))
(append-text info (wrap-as (i386:byte-mem->accu))))) ;; FIXME: byte (ptr (expr->pointer info expr))
(size (if (= ptr 1) (expr->size info expr)
4)))
(append-text info (wrap-as (if (= size 1) (i386:byte-mem->accu)
(i386:mem->accu))))))
((fctn-call (p-expr (ident ,name)) (expr-list . ,expr-list)) ((fctn-call (p-expr (ident ,name)) (expr-list . ,expr-list))
(if (equal? name "asm") (let ((arg0 (cadr (cadar expr-list)))) ;; FIXME (if (equal? name "asm") (let ((arg0 (cadr (cadar expr-list)))) ;; FIXME
@ -942,7 +946,9 @@
(append-text info (wrap-as (i386:base->accu-address))))) (append-text info (wrap-as (i386:base->accu-address)))))
((array-ref ,index (p-expr (ident ,array))) ((array-ref ,index (p-expr (ident ,array)))
(let* ((type (ident->type info array)) (let* ((type (ident->type info array))
(size (ast-type->size info type)) (ptr (ident->pointer info array))
(size (if (or (= ptr 1) (= ptr -1)) (ast-type->size info type)
4))
(info (append-text info (wrap-as (i386:push-accu)))) (info (append-text info (wrap-as (i386:push-accu))))
(info ((expr->accu* info) a)) (info ((expr->accu* info) a))
(info (append-text info (wrap-as (i386:pop-base))))) (info (append-text info (wrap-as (i386:pop-base)))))
@ -1586,11 +1592,17 @@
(define (expr->pointer info o) (define (expr->pointer info o)
(pmatch o (pmatch o
((p-expr (ident ,name)) (ident->pointer info name)) ((p-expr (ident ,name)) (ident->pointer info name))
((de-ref ,expr) (1- (expr->pointer info expr)))
((add ,a ,b) (expr->pointer info a))
((sub ,a ,b) (expr->pointer info a))
(_ (stderr "expr->pointer: unsupported: ~s\n" o) 0))) (_ (stderr "expr->pointer: unsupported: ~s\n" o) 0)))
(define (expr->size info o) (define (expr->size info o)
(pmatch o (pmatch o
((p-expr (ident ,name)) (ident->size info name)) ((p-expr (ident ,name)) (ident->size info name))
((de-ref ,expr) (expr->size info expr))
((add ,a ,b) (expr->size info a))
((sub ,a ,b) (expr->size info a))
(_ (stderr "expr->size: unsupported: ~s\n" o) 4))) (_ (stderr "expr->size: unsupported: ~s\n" o) 4)))
(define (p-expr->type info o) (define (p-expr->type info o)

View file

@ -30,11 +30,19 @@ struct foo {
}; };
void void
add (void *ptab) add0 (void *ptab)
{
void **pp = *(void***)ptab;
bla:
pp[0] = 0x11223344;
}
void
add1 (void *ptab)
{ {
void ***x = (void***)ptab; void ***x = (void***)ptab;
bla: bla:
*(void***)ptab = 0x11223344; *(void***)ptab = 0x22334455;
} }
void void
@ -42,23 +50,29 @@ add2 (void *ptab)
{ {
void ***x = (void***)ptab; void ***x = (void***)ptab;
bla: bla:
*x = 0x22334455; *x = 0x33445566;
} }
int int
test () test ()
{ {
int i; int i = 1;
int *p = &i; int *p = &i;
struct foo f; struct foo f;
f.bar = &p; f.bar = &p;
eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n");
add (&f.bar);
eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); add0 (&f.bar);
if (f.bar != 0x11223344) return 1; eputs ("f.bar:"); eputs (itoa (*f.bar)); eputs ("\n");
add2 (&f.bar); if (*f.bar != 0x11223344) return 1;
add1 (&f.bar);
eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n");
if (f.bar != 0x22334455) return 2; if (f.bar != 0x22334455) return 2;
add2 (&f.bar);
eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n");
if (f.bar != 0x33445566) return 3;
return 0; return 0;
} }