mescc: Tinycc support: array[<const-expr>].

* module/language/c99/compiler.mes (p-expr->number): New function.
  (struct-field, decl->info): Use it to support const expressions in array size.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-23 21:58:34 +02:00
parent d1a7527eaf
commit a7a3de05f6
1 changed files with 28 additions and 9 deletions

View File

@ -1385,6 +1385,25 @@
((string-prefix? "0" s) (string->number s 8))
(else (string->number s)))))
(define (p-expr->number info o)
(pmatch o
((p-expr (fixed ,a))
(cstring->number a))
((neg ,a)
(- (p-expr->number info a)))
((add ,a ,b)
(+ (p-expr->number info a) (p-expr->number info b)))
((div ,a ,b)
(quotient (p-expr->number info a) (p-expr->number info b)))
((mul ,a ,b)
(* (p-expr->number info a) (p-expr->number info b)))
((sub ,a ,b)
(- (p-expr->number info a) (p-expr->number info b)))
((sizeof-expr (i-sel (ident ,field) (p-expr (ident ,struct))))
(let ((type (ident->type info struct)))
(field-size info type field)))
(_ (error (format #f "p-expr->number: not supported: ~s\n" o)))))
(define (struct-field info)
(lambda (o)
(pmatch o
@ -1409,17 +1428,17 @@
(list name '(void) 4))
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
(list name type 4))
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (array-of (ident ,name) (p-expr (fixed ,count)))))))
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (array-of (ident ,name) ,count)))))
(let ((size 4)
(count (cstring->number count)))
(count (p-expr->number info count)))
(list name type (* count size) 0)))
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) ,count))))
(let ((size 4)
(count (cstring->number count)))
(count (p-expr->number info count)))
(list name type (* count size) 0)))
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) ,count))))
(let ((size 4)
(count (cstring->number count)))
(count (p-expr->number info count)))
(list name type (* count size) 0)))
((comp-decl (decl-spec-list (type-spec (struct-ref (ident (,type))))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
@ -1737,18 +1756,18 @@
;; struct foo bar[2];
;; char arena[20000];
((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (array-of (ident ,name) ,count))))
(let ((type (ast->type type)))
(if (.function info)
(let* ((local (car (add-local locals name type -1)))
(count (string->number count))
(count (p-expr->number info count))
(size (ast-type->size info type))
(local (make-local-entry name type -1 (+ (local:id (cdr local)) -1 (quotient (+ (* count size) 3) 4))))
(locals (cons local locals))
(info (clone info #:locals locals)))
info)
(let* ((globals (.globals info))
(count (cstring->number count))
(count (p-expr->number info count))
(size (ast-type->size info type))
(array (make-global-entry name type -1 (string->list (make-string (* count size) #\nul))))
(globals (append globals (list array))))