mescc: Tinycc support: foo *bar[baz].

* module/language/c99/compiler.mes (decl->info): Support foo *bar[baz].
* module/language/c99/compiler.mes (push-global): Support push of -2,
  -1.  Change signature to take info.  Update callers.
  (push-global-address): Change signature to take info.  Update callers.
This commit is contained in:
Jan Nieuwenhuizen 2017-07-28 18:04:39 +02:00
parent 73148c6f78
commit abd1272a31
2 changed files with 56 additions and 13 deletions

View file

@ -150,15 +150,19 @@
(define (append-text info text) (define (append-text info text)
(clone info #:text (append (.text info) text))) (clone info #:text (append (.text info) text)))
(define (push-global globals) (define (push-global info)
(lambda (o) (lambda (o)
(list (i386:push-label-mem `(#:address ,o))))) (let ((ptr (ident->pointer info o)))
(case ptr
((-2) (list (i386:push-label `(#:address ,o))))
((-1) (list (i386:push-label `(#:address ,o))))
(else (list (i386:push-label-mem `(#:address ,o))))))))
(define (push-local locals) (define (push-local locals)
(lambda (o) (lambda (o)
(wrap-as (i386:push-local (local:id o))))) (wrap-as (i386:push-local (local:id o)))))
(define (push-global-address globals) (define (push-global-address info)
(lambda (o) (lambda (o)
(list (i386:push-label o)))) (list (i386:push-label o))))
@ -216,7 +220,7 @@
((push-local (.locals info)) local)))) ((push-local (.locals info)) local))))
(let ((global (assoc-ref (.globals info) o))) (let ((global (assoc-ref (.globals info) o)))
(if global (if global
((push-global (.globals info)) o) ;; FIXME: char*/int ((push-global info) o) ;; FIXME: char*/int
(let ((constant (assoc-ref (.constants info) o))) (let ((constant (assoc-ref (.constants info) o)))
(if constant (if constant
(wrap-as (append (i386:value->accu constant) (wrap-as (append (i386:value->accu constant)
@ -229,14 +233,14 @@
(if local ((push-local-address (.locals info)) local) (if local ((push-local-address (.locals info)) local)
(let ((global (assoc-ref (.globals info) o))) (let ((global (assoc-ref (.globals info) o)))
(if global (if global
((push-global-address (.globals info)) o) ((push-global-address info) o)
((push-global-address #f) `(#:address ,o)))))))) ((push-global-address #f) `(#:address ,o))))))))
(define (push-ident-de-ref info) (define (push-ident-de-ref info)
(lambda (o) (lambda (o)
(let ((local (assoc-ref (.locals info) o))) (let ((local (assoc-ref (.locals info) o)))
(if local ((push-local-de-ref info) local) (if local ((push-local-de-ref info) local)
((push-global-de-ref (.globals info)) o))))) ((push-global-de-ref info) o)))))
(define (push-ident-de-de-ref info) (define (push-ident-de-de-ref info)
(lambda (o) (lambda (o)
@ -393,13 +397,21 @@
(type (ident->type info o)) (type (ident->type info o))
(size (if (= ptr 1) (ast-type->size info type) (size (if (= ptr 1) (ast-type->size info type)
4))) 4)))
(wrap-as (append (i386:local->accu (local:id local)) - (wrap-as (append (i386:local->accu (local:id local))
(if (= size 1) (i386:byte-base->accu-address) (if (= size 1) (i386:byte-base->accu-address)
(i386:base->accu-address))))) (i386:base->accu-address)))))
(let ((size 4)) ;; FIXME (let ((ptr (ident->pointer info o))
(wrap-as (append (i386:label-mem->accu `(#:address ,o)) (size 4)) ;; FIXME size
(if (= size 1) (i386:byte-base->accu-address) (case ptr
(i386:base->accu-address))))))))) ((-2) (wrap-as (append (i386:label->accu `(#:address ,o))
(if (= size 1) (i386:byte-base->accu-address)
(i386:base->accu-address)))))
((-1) (wrap-as (append (i386:label->accu `(#:address ,o))
(if (= size 1) (i386:byte-base->accu-address)
(i386:base->accu-address)))))
(else (wrap-as (append (i386:label-mem->accu `(#:address ,o))
(if (= size 1) (i386:byte-base->accu-address)
(i386:base->accu-address)))))))))))
(define (value->ident info) (define (value->ident info)
(lambda (o value) (lambda (o value)
@ -926,7 +938,7 @@
(let* ((type (ident->type info name)) (let* ((type (ident->type info name))
(ptr (ident->pointer info name)) (ptr (ident->pointer info name))
(size (if (= ptr 1) (ast-type->size info type) (size (if (= ptr 1) (ast-type->size info type)
4))) 4)))
(append-text info (append (wrap-as (i386:accu->base)) (append-text info (append (wrap-as (i386:accu->base))
((base->ident-address info) name))))) ; FIXME: size ((base->ident-address info) name))))) ; FIXME: size
((de-ref ,expr) ((de-ref ,expr)
@ -1866,6 +1878,24 @@
(globals (append globals (list array)))) (globals (append globals (list array))))
(clone info #:globals globals))))) (clone info #:globals globals)))))
;; struct foo *bar[2];
((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (ptr-declr (pointer) (array-of (ident ,name) ,count)))))
(let ((type (ast->type type)))
(if (.function info)
(let* ((local (car (add-local locals name type -1)))
(count (p-expr->number info count))
(size 4)
(local (make-local-entry name type -2 (+ (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 (p-expr->number info count))
(size 4)
(global (make-global-entry name type -2 (string->list (make-string (* count size) #\nul))))
(globals (append globals (list global))))
(clone info #:globals globals)))))
((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (array-of (ident ,array) (p-expr (fixed ,size))) (initzer (p-expr (string ,string)))))) ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (array-of (ident ,array) (p-expr (fixed ,size))) (initzer (p-expr (string ,string))))))
(if (.function info) (if (.function info)
(error "TODO: " o) (error "TODO: " o)

View file

@ -20,7 +20,6 @@
#include "30-test.i" #include "30-test.i"
struct baz { struct baz {
int i; int i;
}; };
@ -53,6 +52,16 @@ add2 (void *ptab)
*x = 0x33445566; *x = 0x33445566;
} }
struct foo *hash_ident[10];
void *
memset (void *s, int c, int n)
{
char *p = s;
while (n--) *p++ = c;
return s;
}
int int
test () test ()
{ {
@ -74,5 +83,9 @@ test ()
eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n");
if (f.bar != 0x33445566) return 3; if (f.bar != 0x33445566) return 3;
hash_ident[0] = 10;
*hash_ident = 0;
memset (hash_ident, 0, 10);
return 0; return 0;
} }