mescc: Tinycc support: union.struct.

* module/language/c99/compiler.mes (decl->info): Support struct inside union.
* scaffold/tests/78-union-struct.c: Test it.
* make.scm (add-scaffold-test): Build it.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-29 22:50:00 +02:00
parent 042022419e
commit bd3ab85e98
3 changed files with 66 additions and 18 deletions

View File

@ -157,7 +157,8 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
"74-multi-line-string"
"75-struct-union"
"76-pointer-arithmetic"
"77-pointer-assign"))
"77-pointer-assign"
"78-union-struct"))
(add-target (group "check-scaffold-tests/7" #:dependencies (filter (target-prefix? "check-scaffold/tests/7") %targets)))

View File

@ -1055,8 +1055,9 @@
(let* ((type0 (ident->type info struct0))
(type1 (field-type info type0 field0))
(offset (+ (field-offset info type0 field0)
(field-offset info type1 field1))))
(append-text info (append ((ident-address->accu info) struct0)
(field-offset info type1 field1)))
(ptr0 (ident->pointer info struct0)))
(append-text info (append ((ident->accu info) struct0)
(wrap-as (i386:accu+value offset))))))
;; bar->foo.i
@ -1628,14 +1629,17 @@
((p-expr (ident ,name)) (ident->type info name))
((array-ref ,index (p-expr (ident ,array))) (ident->type info array))
((i-sel (ident ,field) (p-expr (ident ,struct)))
(let ((type0 (ident->type info struct)))
(field-type info `("tag" ,type0) field)))
(let* ((type0 (ident->type info struct))
(type0 (if (pair? type0) type0 `("tag" ,type0))))
(field-type info type0 field)))
((d-sel (ident ,field) (p-expr (ident ,struct)))
(let ((type0 (ident->type info struct)))
(field-type info `("tag" ,type0) field)))
(let* ((type0 (ident->type info struct))
(type0 (if (pair? type0) type0 `("tag" ,type0))))
(field-type info type0 field)))
((d-sel (ident ,field) (array-ref ,index (p-expr (ident ,array))))
(let ((type0 (ident->type info array)))
(field-type info `("tag" ,type0) field)))
(let* ((type0 (ident->type info array))
(type0 (if (pair? type0) type0 `("tag" ,type0))))
(field-type info type0 field)))
(_ (error "p-expr->type: unsupported: " o))))
(define (local-var? o) ;; formals < 0, locals > 0
@ -1858,6 +1862,11 @@
(let ((type-entry (struct->type-entry name (map (struct-field info) fields))))
(clone info #:types (cons type-entry types))))
;; union
((decl (decl-spec-list (type-spec (union-def (ident ,name) (field-list . ,fields)))))
(let ((type-entry (union->type-entry name (map (struct-field info) fields))))
(clone info #:types (cons type-entry types))))
;; enum e i;
((decl (decl-spec-list (type-spec (enum-ref (ident ,type)))) (init-declr-list (init-declr (ident ,name))))
(let ((type "int")) ;; FIXME
@ -1953,14 +1962,6 @@
(clone info
#:constants (append constants (.constants info)))))
((decl (decl-spec-list (type-spec (struct-def (ident ,name) (field-list . ,fields)))))
(let ((type-entry (struct->type-entry name (map (struct-field info) fields))))
(clone info #:types (cons type-entry types))))
((decl (decl-spec-list (type-spec (union-def (ident ,name) (field-list . ,fields)))))
(let ((type-entry (union->type-entry name (map (struct-field info) fields))))
(clone info #:types (cons type-entry types))))
((decl (decl-spec-list (type-spec (struct-def (ident ,type) (field-list . ,fields))))
(init-declr-list (init-declr (ident ,name))))
(let ((info ((decl->info info) `(decl (decl-spec-list (type-spec (struct-def (ident ,type) (field-list . ,fields))))))))
@ -2062,7 +2063,7 @@
(info (clone info #:globals globals))
(struct? (and (zero? pointer)
(or (and (pair? type) (equal? (car type) "tag"))
(eq? (type:type (ast-type->type info xtype)) 'struct))))
(memq (type:type (ast-type->type info xtype)) '(struct union)))))
(pointer (if struct? -1 pointer))
(size (if (<= pointer 0) (ast-type->size info type)
4)))

View File

@ -0,0 +1,46 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include "30-test.i"
#include <stdint.h>
struct foo {
int i;
void *p;
};
union bar {
struct foo foo;
};
union bar bar;
int
test ()
{
bar.foo.i = 2;
bar.foo.p = "hallo";
union bar *pb = &bar;
if (pb->foo.i != 2) return 1;
return 0;
}