From abd1272a31e434aa619ae6bb533d6e86ea478a5e Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 28 Jul 2017 18:04:39 +0200 Subject: [PATCH] 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. --- module/language/c99/compiler.mes | 54 +++++++++++++++++++++++------- scaffold/tests/77-pointer-assign.c | 15 ++++++++- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index 677b333c..89ed87c6 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -150,15 +150,19 @@ (define (append-text info text) (clone info #:text (append (.text info) text))) -(define (push-global globals) +(define (push-global info) (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) (lambda (o) (wrap-as (i386:push-local (local:id o))))) -(define (push-global-address globals) +(define (push-global-address info) (lambda (o) (list (i386:push-label o)))) @@ -216,7 +220,7 @@ ((push-local (.locals info)) local)))) (let ((global (assoc-ref (.globals info) o))) (if global - ((push-global (.globals info)) o) ;; FIXME: char*/int + ((push-global info) o) ;; FIXME: char*/int (let ((constant (assoc-ref (.constants info) o))) (if constant (wrap-as (append (i386:value->accu constant) @@ -229,14 +233,14 @@ (if local ((push-local-address (.locals info)) local) (let ((global (assoc-ref (.globals info) o))) (if global - ((push-global-address (.globals info)) o) + ((push-global-address info) o) ((push-global-address #f) `(#:address ,o)))))))) (define (push-ident-de-ref info) (lambda (o) (let ((local (assoc-ref (.locals info) o))) (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) (lambda (o) @@ -393,13 +397,21 @@ (type (ident->type info o)) (size (if (= ptr 1) (ast-type->size info type) 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) (i386:base->accu-address))))) - (let ((size 4)) ;; FIXME - (wrap-as (append (i386:label-mem->accu `(#:address ,o)) - (if (= size 1) (i386:byte-base->accu-address) - (i386:base->accu-address))))))))) + (let ((ptr (ident->pointer info o)) + (size 4)) ;; FIXME size + (case ptr + ((-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) (lambda (o value) @@ -926,7 +938,7 @@ (let* ((type (ident->type info name)) (ptr (ident->pointer info name)) (size (if (= ptr 1) (ast-type->size info type) - 4))) + 4))) (append-text info (append (wrap-as (i386:accu->base)) ((base->ident-address info) name))))) ; FIXME: size ((de-ref ,expr) @@ -1866,6 +1878,24 @@ (globals (append globals (list array)))) (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)))))) (if (.function info) (error "TODO: " o) diff --git a/scaffold/tests/77-pointer-assign.c b/scaffold/tests/77-pointer-assign.c index a9a16e86..c54ed886 100644 --- a/scaffold/tests/77-pointer-assign.c +++ b/scaffold/tests/77-pointer-assign.c @@ -20,7 +20,6 @@ #include "30-test.i" - struct baz { int i; }; @@ -53,6 +52,16 @@ add2 (void *ptab) *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 test () { @@ -74,5 +83,9 @@ test () eputs ("f.bar:"); eputs (itoa (f.bar)); eputs ("\n"); if (f.bar != 0x33445566) return 3; + hash_ident[0] = 10; + *hash_ident = 0; + memset (hash_ident, 0, 10); + return 0; }