mescc: Tinycc support: arithmetic in initializes.

* module/language/c99/compiler.mes (p-expr->number): Handle
  bitwise-or, constants.
  (initzer-data): Refactor, use p-expr->number.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-24 15:22:51 +02:00
parent accf4159a4
commit ee4471058f

View file

@ -1399,6 +1399,8 @@
(- (p-expr->number info a))) (- (p-expr->number info a)))
((add ,a ,b) ((add ,a ,b)
(+ (p-expr->number info a) (p-expr->number info b))) (+ (p-expr->number info a) (p-expr->number info b)))
((bitwise-or ,a ,b)
(logior (p-expr->number info a) (p-expr->number info b)))
((div ,a ,b) ((div ,a ,b)
(quotient (p-expr->number info a) (p-expr->number info b))) (quotient (p-expr->number info a) (p-expr->number info b)))
((mul ,a ,b) ((mul ,a ,b)
@ -1408,6 +1410,10 @@
((sizeof-expr (i-sel (ident ,field) (p-expr (ident ,struct)))) ((sizeof-expr (i-sel (ident ,field) (p-expr (ident ,struct))))
(let ((type (ident->type info struct))) (let ((type (ident->type info struct)))
(field-size info type field))) (field-size info type field)))
((p-expr (ident ,name))
(let ((value (assoc-ref (.constants info) name)))
(or value
(error (format #f "p-expr->number: undeclared identifier: ~s\n" o)))))
(_ (error (format #f "p-expr->number: not supported: ~s\n" o))))) (_ (error (format #f "p-expr->number: not supported: ~s\n" o)))))
(define (struct-field info) (define (struct-field info)
@ -2215,26 +2221,19 @@
(define (initzer->data info) (define (initzer->data info)
(lambda (o) (lambda (o)
(pmatch o (pmatch o
((initzer (p-expr (ident ,name)))
(let ((value (assoc-ref (.constants info) name)))
(int->bv32 (or value 0))))
((initzer (p-expr (fixed ,value))) (int->bv32 (cstring->number value)))
((initzer (p-expr (char ,char))) (int->bv32 (char->integer (string-ref char 0)))) ((initzer (p-expr (char ,char))) (int->bv32 (char->integer (string-ref char 0))))
((initzer (neg (p-expr (fixed ,value)))) (int->bv32 (- (cstring->number value))))
((initzer (ref-to (p-expr (ident ,name)))) `(,name #f #f #f))
((initzer (p-expr (string ,string))) `((#:string ,string) #f #f #f)) ((initzer (p-expr (string ,string))) `((#:string ,string) #f #f #f))
((initzer (p-expr (string . ,strings))) `((#:string ,(string-join strings "")) #f #f #f)) ((initzer (p-expr (string . ,strings))) `((#:string ,(string-join strings "")) #f #f #f))
((initzer (initzer-list . ,initzers)) (append-map (initzer->data info) initzers)) ((initzer (initzer-list . ,initzers)) (append-map (initzer->data info) initzers))
((initzer (bitwise-or . ,values)) ((initzer (ref-to (p-expr (ident ,name)))) `(,name #f #f #f))
(let loop ((values (map (initzer->value info) values)) (value 0))
(if (null? values) (int->bv32 value)
(loop (cdr values) (logior value (car values))))))
((initzer (ref-to (i-sel (ident ,field) (cast (type-name (decl-spec-list ,struct) (abs-declr (pointer))) (p-expr (fixed ,base)))))) ((initzer (ref-to (i-sel (ident ,field) (cast (type-name (decl-spec-list ,struct) (abs-declr (pointer))) (p-expr (fixed ,base))))))
(let* ((type (decl->ast-type struct)) (let* ((type (decl->ast-type struct))
(offset (field-offset info type field)) (offset (field-offset info type field))
(base (cstring->number base))) (base (cstring->number base)))
(int->bv32 (+ base offset)))) (int->bv32 (+ base offset))))
(() (int->bv32 0)) (() (int->bv32 0))
((initzer ,p-expr)
(int->bv32 (p-expr->number info p-expr)))
(_ (error "initzer->data: unsupported: " o))))) (_ (error "initzer->data: unsupported: " o)))))
(define (initzer->accu info) (define (initzer->accu info)