mescc: Tinycc support: assign pointer to struct field.

* module/language/c99/compiler.mes (expr->accu, expr->accu*): Support
  pointer field selection.
* scaffold/tests/72-typedef-struct-def.c (test): Test it.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-16 23:39:59 +02:00
parent 9b66421ce8
commit 3cdfdd7407
2 changed files with 43 additions and 3 deletions

View File

@ -567,6 +567,15 @@
(wrap-as (i386:mem->accu))
(wrap-as (i386:mem+n->accu offset))))))
((i-sel (ident ,field) (de-ref (p-expr (ident ,array))))
(let* ((type (ident->type info array))
(offset (field-offset info type field))
(text (.text info)))
(append-text info (append ((ident-address->accu info) array)
(wrap-as (i386:mem->accu))
(wrap-as (i386:mem->accu))
(wrap-as (i386:mem+n->accu offset))))))
((de-ref (p-expr (ident ,name)))
(let* ((type (ident->type info name))
(ptr (ident->pointer info name))
@ -781,6 +790,13 @@
(wrap-as (append (i386:accu+n 4)
(i386:base+n 4)
(i386:base-address->accu-address)))))))))))
((i-sel (ident ,field) ,array)
(let* ((info (append-text info (wrap-as (i386:push-accu))))
(info ((expr->accu* info) a))
(info (append-text info (wrap-as (i386:pop-base)))))
(append-text info (wrap-as (i386:base->accu-address)))))
(_ (error "expr->accu: unsupported assign: " a)))))
(_ (error "expr->accu: unsupported: " o))))))
@ -881,6 +897,23 @@
(info ((expr->base info) array)))
(append-text info (wrap-as (i386:accu+base)))))
((i-sel (ident ,field) (p-expr (ident ,array)))
(let* ((type (ident->type info array))
(offset (field-offset info type field))
(text (.text info)))
(append-text info (append ((ident-address->accu info) array)
(wrap-as (i386:mem->accu))
(wrap-as (i386:accu+value offset))))))
((i-sel (ident ,field) (de-ref (p-expr (ident ,array))))
(let* ((type (ident->type info array))
(offset (field-offset info type field))
(text (.text info)))
(append-text info (append ((ident-address->accu info) array)
(wrap-as (i386:mem->accu))
(wrap-as (i386:mem->accu))
(wrap-as (i386:accu+value offset))))))
(_ (error "expr->accu*: unsupported: " o)))))
(define (ident->constant name value)

View File

@ -34,11 +34,18 @@ typedef struct
int
test ()
{
foo b = {1};
printf ("b.i=%d\n", b.i);
foo f = {1};
printf ("f.i=%d\n", f.i);
bar b = {2};
bar b = {1};
printf ("b.i=%d\n", b.i);
bar* p = &b;
p->i = 2;
printf ("p->i=%d\n", b.i);
bar** pp = &p;
(*pp)->i = 3;
printf ("(*pp)->i=%d\n", b.i);
return 0;
}