3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of Mes.
8 ;;; Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
23 ;;; compiler.mes produces an i386 binary from the C produced by
30 (set-port-encoding! (current-output-port) "ISO-8859-1"))
33 (mes-use-module (nyacc lang c99 parser))
34 (mes-use-module (mes elf-util))
35 (mes-use-module (mes pmatch))
36 (mes-use-module (mes elf))
37 (mes-use-module (mes as-i386))
38 (mes-use-module (mes libc))
39 (mes-use-module (mes optargs))))
41 (define (logf port string . rest)
42 (apply format (cons* port string rest))
46 (define (stderr string . rest)
47 (apply logf (cons* (current-error-port) string rest)))
49 (define (gnuc-xdef? name mode) (if (equal? name "__GNUC__") #f (eq? mode 'code)))
53 #:inc-dirs (string-split (getenv "C_INCLUDE_PATH") #\:)
57 "__NYACC__=1" ;; REMOVEME
74 (write-char (cond ((char? x) x)
75 ((and (number? x) (< (+ x 256) 0)) (format (current-error-port) "***BROKEN*** x=~a ==> ~a\n" x (dec->hex x)) (integer->char #xaa))
76 ((number? x) (integer->char (if (>= x 0) x (+ x 256))))
78 (stderr "write-any: proc: ~a\n" x)
79 (stderr " ==> ~a\n" (map dec->hex (x '() '() 0 0)))
81 (else (stderr "write-any: ~a\n" x) barf))))
83 (define (ast:function? o)
84 (and (pair? o) (eq? (car o) 'fctn-defn)))
88 ((fctn-defn _ (ftn-declr (ident ,name) _) _) name)
89 ((fctn-defn _ (ptr-declr (pointer) (ftn-declr (ident ,name) _)) _) name)
90 ((param-decl _ (param-declr (ident ,name))) name)
91 ((param-decl _ (param-declr (ptr-declr (pointer) (ident ,name)))) name)
92 ((param-decl _ (param-declr (ptr-declr (pointer) (array-of (ident ,name))))) name)
94 (format (current-error-port) "SKIP: .name =~a\n" o))))
98 ((param-decl (decl-spec-list (type-spec ,type)) _) (decl->type type))
99 ((param-decl ,type _) type)
101 (format (current-error-port) "SKIP: .type =~a\n" o))))
103 (define (.statements o)
105 ((fctn-defn _ (ftn-declr (ident ,name) _) (compd-stmt (block-item-list . ,statements))) statements)
106 ((fctn-defn _ (ptr-declr (pointer) (ftn-declr (ident ,name) _)) (compd-stmt (block-item-list . ,statements))) statements)))
108 (define <info> '<info>)
109 (define <types> '<types>)
110 (define <constants> '<constants>)
111 (define <functions> '<functions>)
112 (define <globals> '<globals>)
113 (define <init> '<init>)
114 (define <locals> '<locals>)
115 (define <function> '<function>)
116 (define <text> '<text>)
118 (define* (make o #:key (types '()) (constants '()) (functions '()) (globals '()) (init '()) (locals '()) (function #f) (text '()))
122 (cons <constants> constants)
123 (cons <functions> functions)
124 (cons <globals> globals)
126 (cons <locals> locals)
127 (cons <function> function)
128 (cons <text> text)))))
132 ((<info> . ,alist) (assq-ref alist <types>))))
134 (define (.constants o)
136 ((<info> . ,alist) (assq-ref alist <constants>))))
138 (define (.functions o)
140 ((<info> . ,alist) (assq-ref alist <functions>))))
144 ((<info> . ,alist) (assq-ref alist <globals>))))
148 ((<info> . ,alist) (assq-ref alist <init>))))
152 ((<info> . ,alist) (assq-ref alist <locals>))))
154 (define (.function o)
156 ((<info> . ,alist) (assq-ref alist <function>))))
160 ((<info> . ,alist) (assq-ref alist <text>))))
163 (and (pair? o) (eq? (car o) <info>)))
165 (define (clone o . rest)
167 (let ((types (.types o))
168 (constants (.constants o))
169 (functions (.functions o))
170 (globals (.globals o))
173 (function (.function o))
178 (constants constants)
179 (functions functions)
185 (make <info> #:types types #:constants constants #:functions functions #:globals globals #:init init #:locals locals #:function function #:text text))))))
187 (define (push-global globals)
191 (i386:push-global (+ (data-offset o g) d))))))
193 (define (push-local locals)
197 (i386:push-local (local:id o))))))
199 (define (push-global-address globals)
203 (i386:push-global-address (+ (data-offset o g) d))))))
205 (define (push-local-address locals)
209 (i386:push-local-address (local:id o))))))
211 (define push-global-de-ref push-global)
213 (define (push-local-de-ref locals)
217 (i386:push-local-de-ref (local:id o))))))
219 (define (string->global string)
220 (make-global (add-s:-prefix string) "string" 0 (append (string->list string) (list #\nul))))
222 (define (ident->global name type pointer value)
223 (make-global name type pointer (int->bv32 value)))
225 (define (make-local name type pointer id)
226 (cons name (list type pointer id)))
227 (define local:type car)
228 (define local:pointer cadr)
229 (define local:id caddr)
231 (define (push-ident info)
233 (let ((local (assoc-ref (.locals info) o)))
234 (if local ((push-local (.locals info)) local)
235 (let ((global (assoc-ref (.globals info) o)))
237 ((push-global (.globals info)) o) ;; FIXME: char*/int
238 (let ((constant (assoc-ref (.constants info) o)))
240 (list (lambda (f g ta t d)
242 (i386:value->accu constant)
244 TODO:push-function))))))))
246 (define (push-ident-address info)
248 (let ((local (assoc-ref (.locals info) o)))
249 (if local ((push-local-address (.locals info)) local)
250 ((push-global-address (.globals info)) o)))))
252 (define (push-ident-de-ref info)
254 (let ((local (assoc-ref (.locals info) o)))
255 (if local ((push-local-de-ref (.locals info)) local)
256 ((push-global-de-ref (.globals info)) o)))))
258 (define (expr->arg info) ;; FIXME: get Mes curried-definitions
260 (let ((text (.text info)))
261 ;;(stderr "expr->arg o=~s\n" o)
263 ((p-expr (fixed ,value))
264 (let ((value (cstring->number value)))
265 (clone info #:text (append text
269 (i386:value->accu value)
270 (i386:push-accu))))))))
272 ((neg (p-expr (fixed ,value)))
273 (let ((value (- (cstring->number value))))
274 (clone info #:text (append text
278 (i386:value->accu value)
279 (i386:push-accu))))))))
281 ((p-expr (string ,string))
282 (clone info #:text (append text ((push-global-address info) (add-s:-prefix string)))))
284 ((p-expr (ident ,name))
285 (clone info #:text (append text ((push-ident info) name))))
288 ((array-ref (p-expr (fixed ,index)) (p-expr (ident ,array)))
289 (let* ((index (cstring->number index))
290 (type (ident->type info array))
291 (size (type->size info type)))
294 ((ident->base info) array)
298 (i386:value->accu (* size index))
300 (i386:byte-base-mem->accu)
301 (i386:base-mem->accu))
302 (i386:push-accu))))))))
305 ((array-ref (p-expr (ident ,index)) (p-expr (ident ,array)))
306 (let* ((type (ident->type info array))
307 (size (type->size info type)))
308 (clone info #:text (append text
309 ((ident->base info) index)
310 (list (lambda (f g ta t d)
316 (if (= size 12) (i386:accu+base)
318 ((ident->base info) array)
319 (list (lambda (f g ta t d)
321 (i386:byte-base-mem->accu)
322 (i386:base-mem->accu))))
325 (i386:push-accu)))))))
327 ((de-ref (p-expr (ident ,name)))
328 (clone info #:text (append text ((push-ident-de-ref info) name))))
330 ((ref-to (p-expr (ident ,name)))
331 (clone info #:text (append text ((push-ident-address info) name))))
335 (let* (;;(empty (clone info #:text '()))
336 ;;(info ((ast->info empty) o))
337 (info ((ast->info info) o))
343 (i386:push-accu)))))))
347 (let* (;;(empty (clone info #:text '()))
348 ;;(expr ((expr->accu empty) `(d-sel ,@d-sel)))
349 (expr ((expr->accu info) `(d-sel ,@d-sel)))
353 (list (lambda (f g ta t d)
354 (i386:push-accu)))))))
357 ((p-expr (char ,char))
358 (let ((char (char->integer (car (string->list char)))))
361 (list (lambda (f g ta t d)
363 (i386:value->accu char)
364 (i386:push-accu)))))))
368 ;;;((add (p-expr (fixed ,value)) (d-sel (ident cdr) (array-ref (p-expr (ident x)) (p-expr (ident g_cells))))))
370 ((cast (type-name (decl-spec-list (type-spec (fixed-type _)))
371 (abs-declr (pointer)))
373 ((expr->arg info) cast))
376 ;; (stderr "catch: expr->arg=~s\n" o)
377 (let* ((info ((expr->accu info) o))
381 (list (lambda (f g ta t d)
384 (i386:push-accu))))))))
387 (stderr "SKIP: expr->arg=~s\n" o)
391 ;; FIXME: see ident->base
392 (define (ident->accu info)
394 (let ((local (assoc-ref (.locals info) o))
395 (global (assoc-ref (.globals info) o))
396 (constant (assoc-ref (.constants info) o)))
397 ;; (stderr "ident->accu: local[~a]: ~a\n" o (and local (local:id local)))
398 ;; (stderr "ident->accu: global[~a]: ~a\n" o global)
399 ;; (stderr "globals: ~a\n" (.globals info))
400 ;; (if (and (not global) (not (local:id local)))
401 ;; (stderr "globals: ~a\n" (map car (.globals info))))
403 (let* ((ptr (local:pointer local))
404 (type (ident->type info o))
405 (size (and type (type->size info type))))
406 ;;(stderr "ident->accu PTR[~a]: ~a\n" o ptr)
407 ;;(stderr "type: ~s\n" type)
408 ;;(stderr "ident->accu PTR[~a]: ~a\n" o ptr)
409 ;;(stderr "locals: ~s\n" locals)
411 ((-1) (list (lambda (f g ta t d)
412 (i386:local-ptr->accu (local:id local)))))
413 ((1) (list (lambda (f g ta t d)
414 (i386:local->accu (local:id local)))))
416 (list (lambda (f g ta t d)
418 (i386:byte-local->accu (local:id local))
419 (i386:local->accu (local:id local))))))))
421 (let ((ptr (ident->pointer info o)))
422 ;;(stderr "ident->accu PTR[~a]: ~a\n" o ptr)
424 ((-1) (list (lambda (f g ta t d)
425 (i386:global->accu (+ (data-offset o g) d)))))
426 (else (list (lambda (f g ta t d)
427 (i386:global-address->accu (+ (data-offset o g) d)))))))
429 (list (lambda (f g ta t d)
430 (i386:value->accu constant)))
431 (list (lambda (f g ta t d)
432 (i386:global->accu (+ ta (function-offset o f)))))))))))
434 (define (value->accu v)
435 (list (lambda (f g ta t d)
436 (i386:value->accu v))))
438 (define (accu->ident info)
440 (let ((local (assoc-ref (.locals info) o)))
442 (list (lambda (f g ta t d)
443 (i386:accu->local (local:id local))))
444 (list (lambda (f g ta t d)
445 (i386:accu->global (+ (data-offset o g) d))))))))
447 (define (base->ident info)
449 (let ((local (assoc-ref (.locals info) o)))
451 (list (lambda (f g ta t d)
452 (i386:base->local (local:id local))))
453 (list (lambda (f g ta t d)
454 (i386:base->global (+ (data-offset o g) d))))))))
456 (define (base->ident-address info)
458 (let ((local (assoc-ref (.locals info) o)))
460 (list (lambda (f g ta t d)
462 (i386:local->accu (local:id local))
463 (i386:byte-base->accu-address))))
464 TODO:base->ident-address-global))))
466 (define (value->ident info)
468 (let ((local (assoc-ref (.locals info) o)))
470 (list (lambda (f g ta t d)
471 (i386:value->local (local:id local) value)))
472 (list (lambda (f g ta t d)
473 (i386:value->global (+ (data-offset o g) d) value)))))))
475 (define (ident-add info)
477 (let ((local (assoc-ref (.locals info) o)))
479 (list (lambda (f g ta t d)
480 (i386:local-add (local:id local) n)))
481 (list (lambda (f g ta t d)
482 (i386:global-add (+ (data-offset o g) d) n)))))))
484 ;; FIXME: see ident->accu
485 (define (ident->base info)
487 (let ((local (assoc-ref (.locals info) o)))
488 ;;(stderr "ident->base: local[~a]: ~a\n" o (and local (local:id local)))
490 (let* ((ptr (local:pointer local))
491 (type (ident->type info o))
492 (size (and type (type->size info type))))
494 ((-1) (list (lambda (f g ta t d)
495 (i386:local-ptr->base (local:id local)))))
496 ((1) (list (lambda (f g ta t d)
497 (i386:local->base (local:id local)))))
499 (list (lambda (f g ta t d)
501 (i386:byte-local->base (local:id local))
502 (i386:local->base (local:id local))))))))
503 (let ((global (assoc-ref (.globals info) o) ))
505 (let ((ptr (ident->pointer info o)))
506 ;;(stderr "ident->accu PTR[~a]: ~a\n" o ptr)
508 ((-1) (list (lambda (f g ta t d)
509 (i386:global->base (+ (data-offset o g) d)))))
510 (else (list (lambda (f g ta t d)
511 (i386:global-address->base (+ (data-offset o g) d)))))))
512 (let ((constant (assoc-ref (.constants info) o)))
514 (list (lambda (f g ta t d)
515 (i386:value->base constant)))
516 (list (lambda (f g ta t d)
517 (i386:global->base (+ ta (function-offset o f)))))))))))))
519 (define (expr->accu info)
521 (let ((text (.text info))
522 (locals (.locals info))
523 (globals (.globals info)))
524 ;;(stderr "expr->accu o=~a\n" o)
526 ((p-expr (string ,string))
527 (clone info #:text (append text (list (lambda (f g ta t d)
528 ;;(stderr "OFF[~a]: ~a\n" string (data-offset string globals))
529 ;;(stderr "globals: ~s\n" (map car globals))
530 (i386:global->accu (+ (data-offset (add-s:-prefix string) globals) d)))))))
531 ((p-expr (fixed ,value))
532 (clone info #:text (append text (value->accu (cstring->number value)))))
533 ((p-expr (ident ,name))
534 (clone info #:text (append text ((ident->accu info) name))))
535 ((fctn-call . _) ((ast->info info) `(expr-stmt ,o)))
536 ((not (fctn-call . _)) ((ast->info info) o))
537 ((neg (p-expr (fixed ,value)))
538 (clone info #:text (append text (value->accu (- (cstring->number value))))))
540 ((initzer ,initzer) ((expr->accu info) initzer))
541 ((ref-to (p-expr (ident ,name)))
544 ((ident->accu info) name))))
546 ((sizeof-type (type-name (decl-spec-list (type-spec (struct-ref (ident ,name))))))
547 (let* ((type (list "struct" name))
548 (fields (or (type->description info type) '()))
549 (size (type->size info type)))
552 (list (lambda (f g ta t d)
554 (i386:value->accu size))))))))
557 ((array-ref (p-expr (fixed ,index)) (p-expr (ident ,array)))
558 (let* ((index (cstring->number index))
559 (type (ident->type info array))
560 (size (type->size info type)))
563 ((ident->base info) array)
564 (list (lambda (f g ta t d)
566 (i386:value->accu (* size index))
568 (i386:byte-base-mem->accu)
569 (i386:base-mem->accu)))))))))
572 ((array-ref (p-expr (ident ,index)) (p-expr (ident ,array)))
573 (let* ((type (ident->type info array))
574 (size (type->size info type)))
575 (clone info #:text (append text
576 ((ident->base info) index)
577 (list (lambda (f g ta t d)
583 (if (= size 12) (i386:accu+base)
585 ((ident->base info) array)
586 (list (lambda (f g ta t d)
588 (i386:byte-base-mem->accu)
589 (i386:base-mem->accu))))))))
592 ((d-sel (ident ,field) (p-expr (ident ,array)))
593 (let* ((type (ident->type info array))
594 (fields (type->description info type))
595 (field-size 4) ;; FIXME:4, not fixed
596 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))))))
600 ((ident->accu info) array)
601 (list (lambda (f g ta t d)
602 (i386:mem+n->accu offset)))))))
605 ((d-sel (ident ,field) (array-ref (p-expr (fixed ,index)) (p-expr (ident ,array))))
606 (let* ((type (ident->type info array))
607 (fields (or (type->description info type) '()))
608 (size (type->size info type))
609 (count (length fields))
610 (field-size 4) ;; FIXME:4, not fixed
611 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))))))
612 (index (cstring->number index))
616 (list (lambda (f g ta t d)
618 (i386:value->base index)
620 (if (> count 1) (i386:accu+accu) '())
621 (if (= count 3) (i386:accu+base) '())
623 ((ident->base info) array)
624 (list (lambda (f g ta t d)
625 (i386:base-mem+n->accu offset)))))))
628 ((d-sel (ident ,field) (array-ref (p-expr (ident ,index)) (p-expr (ident ,array))))
629 (let* ((type (ident->type info array))
630 (fields (or (type->description info type) '()))
631 (size (type->size info type))
632 (count (length fields))
633 (field-size 4) ;; FIXME:4, not fixed
634 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))))))
638 ((ident->base info) index)
639 (list (lambda (f g ta t d)
642 (if (> count 1) (i386:accu+accu) '())
643 (if (= count 3) (i386:accu+base) '())
645 ((ident->base info) array)
646 (list (lambda (f g ta t d)
647 (i386:base-mem+n->accu offset)))))))
649 ;; g_functions[g_cells[fn].cdr].arity
650 ;; INDEX0: g_cells[fn].cdr
652 ;;; index: (d-sel (ident ,cdr) (array-ref (p-expr (ident ,fn)) (p-expr (ident ,g_cells))))
653 ;;((d-sel (ident ,arity) (array-ref (d-sel (ident ,cdr) (array-ref (p-expr (ident ,fn)) (p-expr (ident ,g_cells)))) (p-expr (ident ,g_functions)))))
654 ((d-sel (ident ,field) (array-ref ,index (p-expr (ident ,array))))
655 (let* ((empty (clone info #:text '()))
656 (index ((expr->accu empty) index))
657 (type (ident->type info array))
658 (fields (or (type->description info type) '()))
659 (size (type->size info type))
660 (count (length fields))
661 (field-size 4) ;; FIXME:4, not fixed
662 (rest (or (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))
665 (offset (* field-size (1- (length rest))))
670 (list (lambda (f g ta t d)
673 (if (> count 1) (i386:accu+accu) '())
674 (if (= count 3) (i386:accu+base) '())
676 ((ident->base info) array)
677 (list (lambda (f g ta t d)
678 (i386:base-mem+n->accu offset)))))))
680 ;;; FIXME: FROM INFO ...only zero?!
681 ((p-expr (fixed ,value))
682 (let ((value (cstring->number value)))
685 (list (lambda (f g ta t d)
686 (i386:value->accu value)))))))
688 ((p-expr (char ,char))
689 (let ((char (char->integer (car (string->list char)))))
692 (list (lambda (f g ta t d)
693 (i386:value->accu char)))))))
695 ((p-expr (ident ,name))
698 ((ident->accu info) name))))
700 ((de-ref (p-expr (ident ,name)))
701 (let* ((type (ident->type info name))
702 (size (and type (type->size info type))))
705 ((ident->accu info) name)
706 (list (lambda (f g ta t d)
708 (i386:byte-mem->accu)
709 (i386:mem->accu))))))))
711 ;; GRR --> info again??!?
713 ((ast->info info) `(expr-stmt ,o)))
715 ((cond-expr . ,cond-expr)
716 ((ast->info info) `(expr-stmt ,o)))
719 ;;((post-inc ,expr) ((ast->info info) `(expr-stmt ,o)))
720 ((post-inc (p-expr (ident ,name)))
723 ((ident->accu info) name)
724 ((ident-add info) name 1))))
726 ;; GRR --> info again??!?
727 ((post-inc ,expr) ((ast->info info) `(expr-stmt ,o)))
728 ((post-dec ,expr) ((ast->info info) `(expr-stmt ,o)))
729 ((pre-inc ,expr) ((ast->info info) `(expr-stmt ,o)))
730 ((pre-dec ,expr) ((ast->info info) `(expr-stmt ,o)))
732 ((add (p-expr (ident ,name)) ,b)
733 (let* ((empty (clone info #:text '()))
734 (base ((expr->base empty) b)))
738 ((ident->accu info) name)
739 (list (lambda (f g ta t d)
740 (i386:accu+base)))))))
743 (let* ((empty (clone info #:text '()))
744 (accu ((expr->accu empty) a))
745 (base ((expr->base empty) b)))
750 (list (lambda (f g ta t d)
751 (i386:accu+base)))))))
754 (let* ((empty (clone info #:text '()))
755 (accu ((expr->accu empty) a))
756 (base ((expr->base empty) b)))
761 (list (lambda (f g ta t d)
762 (i386:accu-base)))))))
765 (let* ((empty (clone info #:text '()))
766 (accu ((expr->accu empty) a))
767 (base ((expr->base empty) b)))
772 (list (lambda (f g ta t d)
773 (i386:accu-or-base)))))))
776 (let* ((empty (clone info #:text '()))
777 (accu ((expr->accu empty) a))
778 (base ((expr->base empty) b)))
783 (list (lambda (f g ta t d)
784 (i386:accu<<base)))))))
787 (let* ((empty (clone info #:text '()))
788 (accu ((expr->accu empty) a))
789 (base ((expr->base empty) b)))
794 (list (lambda (f g ta t d)
795 (i386:accu>>base)))))))
798 (let* ((empty (clone info #:text '()))
799 (accu ((expr->accu empty) a))
800 (base ((expr->base empty) b)))
805 (list (lambda (f g ta t d)
806 (i386:accu/base)))))))
809 (let* ((empty (clone info #:text '()))
810 (accu ((expr->accu empty) a))
811 (base ((expr->base empty) b)))
813 (append text ;;FIXME:empty
816 (list (lambda (f g ta t d)
817 (i386:accu%base)))))))
820 (let* ((empty (clone info #:text '()))
821 (accu ((expr->accu empty) a))
822 (base ((expr->base empty) b)))
827 (list (lambda (f g ta t d)
828 (i386:accu*base)))))))
830 ;; FIXME: c/p ast->info
832 (let* ((base ((expr->base info) a))
833 (empty (clone base #:text '()))
834 (accu ((expr->accu empty) b)))
837 (list (lambda (f g ta t d)
840 (list (lambda (f g ta t d)
842 (list (lambda (f g ta t d)
843 (i386:sub-base)))))))
845 ;; FIXME: c/p ast->info
847 (let* ((base ((expr->base info) a))
848 (empty (clone base #:text '()))
849 (accu ((expr->accu empty) b)))
853 (list (lambda (f g ta t d)
854 (i386:base-sub)))))))
856 ;; FIXME: ...c/p ast->info
857 ((neg (p-expr (ident ,name)))
858 (clone info #:text (append text
859 ((ident->base info) name)
860 (list (lambda (f g ta t d)
861 (i386:value->accu 0)))
862 (list (lambda (f g ta t d)
865 ;;((cast (type-name (decl-spec-list (type-spec (typename "SCM"))) (abs-declr (declr-fctn (declr-scope (abs-declr (pointer))) (param-list (param-decl (decl-spec-list (type-spec (typename "SCM")))))))) (d-sel (ident "function") (array-ref (d-sel (ident "cdr") (array-ref (p-expr (ident "fn")) (p-expr (ident "g_cells")))) (p-expr (ident "functions"))))))
867 ((expr->accu info) o))
869 ((assn-expr (p-expr (ident ,name)) ,op ,expr)
870 (let ((info ((ast->info info) o)))
871 (clone info #:text (append (.text info)
872 ((ident->accu info) name)))))
875 (format (current-error-port) "SKIP: expr->accu=~s\n" o)
879 (define (expr->base info)
881 (let ((info ((expr->accu info) o)))
884 (list (lambda (f g ta t d)
887 (list (lambda (f g ta t d)
890 (i386:pop-accu)))))))))
892 (define (expr->accu* info)
895 ;;(stderr "expr->accu* o=~s\n" o)
897 ((d-sel (ident ,field) (array-ref (p-expr (fixed ,index)) (p-expr (ident ,array))))
898 (let* ((type (ident->type info array))
899 (fields (or (type->description info type) '()))
900 (size (type->size info type))
901 (count (length fields))
902 (field-size 4) ;; FIXME:4, not fixed
903 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))))))
904 (index (cstring->number index))
908 (list (lambda (f g ta t d)
910 (i386:value->base index)
912 (if (> count 1) (i386:accu+accu) '())
913 (if (= count 3) (i386:accu+base) '())
915 ;; de-ref: g_cells, non: arena
916 ;;((ident->base info) array)
917 ((ident->base info) array)
918 (list (lambda (f g ta t d)
921 (i386:accu+value offset))))))))
924 ((d-sel (ident ,field) (array-ref (p-expr (ident ,index)) (p-expr (ident ,array))))
925 (let* ((type (ident->type info array))
926 (fields (or (type->description info type) '()))
927 (size (type->size info type))
928 (count (length fields))
929 (field-size 4) ;; FIXME:4, not fixed
930 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))))))
934 ((ident->base info) index)
935 (list (lambda (f g ta t d)
938 (if (> count 1) (i386:accu+accu) '())
939 (if (= count 3) (i386:accu+base) '())
941 ;; de-ref: g_cells, non: arena
942 ;;((ident->base info) array)
943 ((ident->base info) array)
944 (list (lambda (f g ta t d)
947 (i386:accu+value offset))))))))
949 ;;((d-sel (ident "cdr") (p-expr (ident "scm_make_cell"))))
950 ((d-sel (ident ,field) (p-expr (ident ,name)))
951 (let* ((type (ident->type info name))
952 (fields (or (type->description info type) '()))
953 (field-size 4) ;; FIXME
954 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b))))))))
958 ((ident->accu info) name)
959 (list (lambda (f g ta t d)
960 (i386:accu+value offset)))))))
963 (format (current-error-port) "SKIP: expr->accu*=~s\n" o)
968 (define (ident->constant name value)
971 (define (make-type name type size description)
972 (cons name (list type size description)))
974 (define (enum->type name fields)
975 (make-type name 'enum 4 fields))
977 (define (struct->type name fields)
978 (make-type name 'struct (* 4 (length fields)) fields)) ;; FIXME
980 (define (decl->type o)
982 ((fixed-type ,type) type)
983 ((struct-ref (ident ,name)) (list "struct" name))
984 ((decl (decl-spec-list (type-spec (struct-ref (ident ,name)))));; "scm"
985 (list "struct" name)) ;; FIXME
986 ((typename ,name) name)
988 (stderr "SKIP: decl type=~s\n" o)
992 (define (expr->global o)
994 ((p-expr (string ,string)) (string->global string))
997 (define (initzer->global o)
999 ((initzer ,initzer) (expr->global initzer))
1002 (define (byte->hex o)
1003 (string->number (string-drop o 2) 16))
1005 (define (asm->hex o)
1006 (let ((prefix ".byte "))
1007 (if (not (string-prefix? prefix o)) (begin (stderr "SKIP:~s\n" o)'())
1008 (let ((s (string-drop o (string-length prefix))))
1009 (map byte->hex (string-split s #\space))))))
1011 (define (case->jump-info info)
1013 (list (lambda (f g ta t d) (i386:Xjump n))))
1015 (list (lambda (f g ta t d) (i386:Xjump-nz n))))
1016 (define (statement->info info body-length)
1019 ((break) (clone info #:text (append (.text info) (jump body-length)
1022 ((ast->info info) o)))))
1025 ((case (p-expr (ident ,constant)) (compd-stmt (block-item-list . ,elements)))
1026 (lambda (body-length)
1028 (define (test->text value clause-length)
1029 (append (list (lambda (f g ta t d) (i386:accu-cmp-value value)))
1030 (jump-nz clause-length)))
1031 (let* ((value (assoc-ref (.constants info) constant))
1033 (clone info #:text (append (.text info) (test->text value 0))))
1034 (text-length (length (.text test-info)))
1035 (clause-info (let loop ((elements elements) (info test-info))
1036 (if (null? elements) info
1037 (loop (cdr elements) ((statement->info info body-length) (car elements))))))
1038 (clause-text (list-tail (.text clause-info) text-length))
1039 (clause-length (length (text->list clause-text))))
1040 (clone info #:text (append
1042 (test->text value clause-length)
1044 #:globals (.globals clause-info)))))
1046 ((case (p-expr (fixed ,value)) (compd-stmt (block-item-list . ,elements)))
1047 (lambda (body-length)
1049 (define (test->text value clause-length)
1050 (append (list (lambda (f g ta t d) (i386:accu-cmp-value value)))
1051 (jump-nz clause-length)))
1052 (let* ((value (cstring->number value))
1054 (clone info #:text (append (.text info) (test->text value 0))))
1055 (text-length (length (.text test-info)))
1056 (clause-info (let loop ((elements elements) (info test-info))
1057 (if (null? elements) info
1058 (loop (cdr elements) ((statement->info info body-length) (car elements))))))
1059 (clause-text (list-tail (.text clause-info) text-length))
1060 (clause-length (length (text->list clause-text))))
1061 (clone info #:text (append
1063 (test->text value clause-length)
1065 #:globals (.globals clause-info)))))
1067 ((case (neg (p-expr (fixed ,value))) ,statement)
1068 ((case->jump-info info) `(case (p-expr (fixed ,(string-append "-" value))) ,statement)))
1070 ((default (compd-stmt (block-item-list . ,elements)))
1071 (lambda (body-length)
1072 (let ((text-length (length (.text info))))
1073 (let loop ((elements elements) (info info))
1074 (if (null? elements) info
1075 (loop (cdr elements) ((statement->info info body-length) (car elements))))))))
1077 ((case (p-expr (ident ,constant)) ,statement)
1078 ((case->jump-info info) `(case (p-expr (ident ,constant)) (compd-stmt (block-item-list ,statement)))))
1080 ((case (p-expr (fixed ,value)) ,statement)
1081 ((case->jump-info info) `(case (p-expr (fixed ,value)) (compd-stmt (block-item-list ,statement)))))
1083 ((default ,statement)
1084 ((case->jump-info info) `(default (compd-stmt (block-item-list ,statement)))))
1086 (_ (stderr "no case match: ~a\n" o) barf)
1089 (define (test->jump->info info)
1090 (define (jump type . test)
1092 (let* ((text (.text info))
1093 (info (clone info #:text '()))
1094 (info ((ast->info info) o))
1095 (jump-text (lambda (body-length)
1096 (list (lambda (f g ta t d) (type body-length))))))
1097 (lambda (body-length)
1101 (if (null? test) '() (car test))
1102 (jump-text body-length)))))))
1106 ;; ((le ,a ,b) ((jump i386:Xjump-ncz) o)) ; ja
1107 ;; ((lt ,a ,b) ((jump i386:Xjump-nc) o)) ; jae
1108 ;; ((ge ,a ,b) ((jump i386:Xjump-ncz) o))
1109 ;; ((gt ,a ,b) ((jump i386:Xjump-nc) o))
1111 ((le ,a ,b) ((jump i386:Xjump-g) o))
1112 ((lt ,a ,b) ((jump i386:Xjump-ge) o))
1113 ((ge ,a ,b) ((jump i386:Xjump-g) o))
1114 ((gt ,a ,b) ((jump i386:Xjump-ge) o))
1116 ((ne ,a ,b) ((jump i386:Xjump-nz) o))
1117 ((eq ,a ,b) ((jump i386:Xjump-nz) o))
1118 ((not _) ((jump i386:Xjump-z) o))
1120 (let* ((text (.text info))
1121 (info (clone info #:text '()))
1123 (a-jump ((test->jump->info info) a))
1124 (a-text (.text (a-jump 0)))
1125 (a-length (length (text->list a-text)))
1127 (b-jump ((test->jump->info info) b))
1128 (b-text (.text (b-jump 0)))
1129 (b-length (length (text->list b-text))))
1131 (lambda (body-length)
1134 (.text (a-jump (+ b-length body-length)))
1135 (.text (b-jump body-length)))))))
1137 (let* ((text (.text info))
1138 (info (clone info #:text '()))
1140 (a-jump ((test->jump->info info) a))
1141 (a-text (.text (a-jump 0)))
1142 (a-length (length (text->list a-text)))
1144 (jump-text (list (lambda (f g ta t d) (i386:Xjump 0))))
1145 (jump-length (length (text->list jump-text)))
1147 (b-jump ((test->jump->info info) b))
1148 (b-text (.text (b-jump 0)))
1149 (b-length (length (text->list b-text)))
1151 (jump-text (list (lambda (f g ta t d) (i386:Xjump b-length)))))
1153 (lambda (body-length)
1156 (.text (a-jump jump-length))
1158 (.text (b-jump body-length)))))))
1160 ((array-ref . _) ((jump i386:jump-byte-z
1161 (list (lambda (f g ta t d) (i386:accu-zero?)))) o))
1163 ((de-ref _) ((jump i386:jump-byte-z
1164 (list (lambda (f g ta t d) (i386:accu-zero?)))) o))
1166 ((assn-expr (p-expr (ident ,name)) ,op ,expr)
1169 ((ident->accu info) name)
1170 (list (lambda (f g ta t d) (i386:accu-zero?))))) o))
1172 (_ ((jump i386:Xjump-z (list (lambda (f g ta t d) (i386:accu-zero?)))) o)))))
1174 (define (cstring->number s)
1175 (cond ((string-prefix? "0x" s) (string->number (string-drop s 2) 16))
1176 ((string-prefix? "0" s) (string->number s 8))
1177 (else (string->number s))))
1179 (define (struct-field o)
1181 ((comp-decl (decl-spec-list (type-spec (enum-ref (ident ,type))))
1182 (comp-declr-list (comp-declr (ident ,name))))
1184 ((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ident ,name))))
1186 ((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ident ,name))))
1188 ((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list (param-decl (decl-spec-list (type-spec (void)))))))))
1189 (cons type name)) ;; FIXME function / int
1190 ((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
1191 (cons type name)) ;; FIXME: ptr/char
1192 (_ (stderr "struct-field: no match: ~s\n" o) barf)))
1194 (define (ast->type o)
1198 ((struct-ref (ident ,type))
1199 (list "struct" type))
1200 (_ (stderr "SKIP: type=~s\n" o)
1203 (define i386:type-alist
1204 '(("char" . (builtin 1 #f))
1205 ("int" . (builtin 4 #f))))
1207 (define (type->size info o)
1208 ;;(stderr "types=~s\n" (.types info))
1209 ;;(stderr "type->size o=~s => ~s\n" o (cadr (assoc-ref (.types info) o)))
1211 ((decl-spec-list (type-spec (fixed-type ,type)))
1212 (type->size info type))
1213 ((decl-spec-list (type-spec (fixed-type ,type)) (type-qual ,qual))
1214 (type->size info type))
1215 (_ (let ((type (assoc-ref (.types info) o)))
1216 (if type (cadr type)
1218 (stderr "***TYPE NOT FOUND**: o=~s\n" o)
1222 (define (ident->decl info o)
1223 ;; (stderr "ident->decl o=~s\n" o)
1224 ;; (stderr " types=~s\n" (.types info))
1225 ;; (stderr " local=~s\n" (assoc-ref (.locals info) o))
1226 ;; (stderr " global=~s\n" (assoc-ref (.globals info) o))
1227 (or (assoc-ref (.locals info) o)
1228 (assoc-ref (.globals info) o)
1230 (stderr "NO IDENT: ~a\n" (assoc-ref (.functions info) o))
1231 (assoc-ref (.functions info) o))))
1233 (define (ident->type info o)
1234 (and=> (ident->decl info o) car))
1236 (define (ident->pointer info o)
1237 (let ((local (assoc-ref (.locals info) o)))
1238 (if local (local:pointer local)
1239 (or (and=> (ident->decl info o) global:pointer) 0))))
1241 (define (type->description info o)
1242 ;; (stderr "type->description =~s\n" o)
1243 ;; (stderr "types=~s\n" (.types info))
1244 ;; (stderr "type->description o=~s ==> ~s\n" o (caddr (assoc-ref (.types info) o)))
1245 ;; (stderr " assoc ~a\n" (assoc-ref (.types info) o))
1247 ((decl-spec-list (type-spec (fixed-type ,type)))
1248 (type->description info type))
1249 ((decl-spec-list (type-spec (fixed-type ,type)) (type-qual ,qual))
1250 (type->description info type))
1251 (_ (caddr (assoc-ref (.types info) o)))))
1253 (define (local? o) ;; formals < 0, locals > 0
1254 (positive? (local:id o)))
1256 (define (ast->info info)
1258 (let ((globals (.globals info))
1259 (locals (.locals info))
1260 (constants (.constants info))
1261 (text (.text info)))
1262 (define (add-local locals name type pointer)
1263 (let* ((id (1+ (length (filter local? (map cdr locals)))))
1264 (locals (cons (make-local name type pointer id) locals)))
1267 ;; (stderr "\n ast->info=~s\n" o)
1268 ;; (stderr " globals[~a=>~a]: ~a\n" (length globals) (length (append-map cdr globals)) (map (lambda (s) (if (string? s) (string-delete #\newline s))) (map car globals)))
1269 ;; (stderr " text=~a\n" text)
1270 ;; (stderr " info=~a\n" info)
1271 ;; (stderr " globals=~a\n" globals)
1273 (((trans-unit . _) . _)
1274 ((ast-list->info info) o))
1275 ((trans-unit . ,elements)
1276 ((ast-list->info info) elements))
1277 ((fctn-defn . _) ((function->info info) o))
1278 ((comment . _) info)
1279 ((cpp-stmt (define (name ,name) (repl ,value)))
1282 ((cast (type-name (decl-spec-list (type-spec (void)))) _)
1285 ;; FIXME: expr-stmt wrapper?
1288 ((assn-expr . ,assn-expr)
1289 ((ast->info info) `(expr-stmt ,o)))
1292 (let ((expr ((expr->accu info) `(d-sel ,@d-sel))))
1295 ((compd-stmt (block-item-list . ,statements)) ((ast-list->info info) statements))
1297 ((expr-stmt (fctn-call (p-expr (ident ,name)) (expr-list . ,expr-list)))
1298 (if (equal? name "asm") (let ((arg0 (cadr (cadar expr-list)))) ;; FIXME
1299 (clone info #:text (append text (list (lambda (f g ta t d) (asm->hex arg0))))))
1300 (let* ((globals (append globals (filter-map expr->global expr-list)))
1301 (info (clone info #:globals globals))
1302 (text-length (length text))
1303 (args-info (let loop ((expressions (reverse expr-list)) (info info))
1304 (if (null? expressions) info
1305 (loop (cdr expressions) ((expr->arg info) (car expressions))))))
1306 (text (.text args-info))
1307 (n (length expr-list)))
1308 (if (and (not (assoc-ref locals name))
1309 (assoc-ref (.functions info) name))
1310 (clone args-info #:text
1312 (list (lambda (f g ta t d)
1313 (i386:call f g ta t d (+ t (function-offset name f)) n))))
1315 (let* ((empty (clone info #:text '()))
1316 (accu ((expr->accu empty) `(p-expr (ident ,name)))))
1317 (clone args-info #:text
1320 (list (lambda (f g ta t d)
1321 (i386:call-accu f g ta t d n))))
1322 #:globals globals))))))
1324 ;;((expr-stmt (fctn-call (d-sel (ident "function") (array-ref (d-sel (ident "cdr") (array-ref (p-expr (ident "fn")) (p-expr (ident "g_cells")))) (p-expr (ident "g_functions")))) (expr-list))))
1325 ((expr-stmt (fctn-call ,function (expr-list . ,expr-list)))
1326 (let* ((globals (append globals (filter-map expr->global expr-list)))
1327 (info (clone info #:globals globals))
1328 (text-length (length text))
1329 (args-info (let loop ((expressions (reverse expr-list)) (info info))
1330 (if (null? expressions) info
1331 (loop (cdr expressions) ((expr->arg info) (car expressions))))))
1332 (text (.text args-info))
1333 (n (length expr-list))
1334 (empty (clone info #:text '()))
1335 (accu ((expr->accu empty) function)))
1339 (list (lambda (f g ta t d)
1340 (i386:call-accu f g ta t d n))))
1341 #:globals globals)))
1344 (let* ((text-length (length text))
1346 (test-jump->info ((test->jump->info info) test))
1347 (test+jump-info (test-jump->info 0))
1348 (test-length (length (.text test+jump-info)))
1350 (body-info ((ast->info test+jump-info) body))
1351 (text-body-info (.text body-info))
1352 (body-text (list-tail text-body-info test-length))
1353 (body-length (length (text->list body-text)))
1355 (text+test-text (.text (test-jump->info body-length)))
1356 (test-text (list-tail text+test-text text-length)))
1362 #:globals (.globals body-info))))
1364 ((if ,test ,then ,else)
1365 (let* ((text-length (length text))
1367 (test-jump->info ((test->jump->info info) test))
1368 (test+jump-info (test-jump->info 0))
1369 (test-length (length (.text test+jump-info)))
1371 (then-info ((ast->info test+jump-info) then))
1372 (text-then-info (.text then-info))
1373 (then-text (list-tail text-then-info test-length))
1374 (then-jump-text (list (lambda (f g ta t d) (i386:Xjump 0))))
1375 (then-jump-length (length (text->list then-jump-text)))
1376 (then-length (+ (length (text->list then-text)) then-jump-length))
1378 (then+jump-info (clone then-info #:text (append text-then-info then-jump-text)))
1379 (else-info ((ast->info then+jump-info) else))
1380 (text-else-info (.text else-info))
1381 (else-text (list-tail text-else-info (length (.text then+jump-info))))
1382 (else-length (length (text->list else-text)))
1384 (text+test-text (.text (test-jump->info then-length)))
1385 (test-text (list-tail text+test-text text-length))
1386 (then-jump-text (list (lambda (f g ta t d) (i386:Xjump else-length)))))
1394 #:globals (append (.globals then-info)
1395 (list-tail (.globals else-info) (length globals))))))
1397 ((expr-stmt (cond-expr ,test ,then ,else))
1398 (let* ((text-length (length text))
1400 (test-jump->info ((test->jump->info info) test))
1401 (test+jump-info (test-jump->info 0))
1402 (test-length (length (.text test+jump-info)))
1404 (then-info ((ast->info test+jump-info) then))
1405 (text-then-info (.text then-info))
1406 (then-text (list-tail text-then-info test-length))
1407 (then-length (length (text->list then-text)))
1409 (jump-text (list (lambda (f g ta t d) (i386:Xjump 0))))
1410 (jump-length (length (text->list jump-text)))
1412 (test+then+jump-info
1414 #:text (append (.text then-info) jump-text)))
1416 (else-info ((ast->info test+then+jump-info) else))
1417 (text-else-info (.text else-info))
1418 (else-text (list-tail text-else-info (length (.text test+then+jump-info))))
1419 (else-length (length (text->list else-text)))
1421 (text+test-text (.text (test-jump->info (+ then-length jump-length))))
1422 (test-text (list-tail text+test-text text-length))
1423 (jump-text (list (lambda (f g ta t d) (i386:Xjump else-length)))))
1431 #:globals (.globals else-info))))
1433 ((switch ,expr (compd-stmt (block-item-list . ,cases)))
1434 (let* ((expr ((expr->accu info) expr))
1435 (empty (clone info #:text '()))
1436 (case-infos (map (case->jump-info empty) cases))
1437 (case-lengths (map (lambda (c-j) (length (text->list (.text (c-j 0))))) case-infos))
1438 (cases-info (let loop ((cases cases) (info expr) (lengths case-lengths))
1439 (if (null? cases) info
1440 (let ((c-j ((case->jump-info info) (car cases))))
1441 (loop (cdr cases) (c-j (apply + (cdr lengths))) (cdr lengths)))))))
1444 ((for ,init ,test ,step ,body)
1445 (let* ((info (clone info #:text '())) ;; FIXME: goto in body...
1447 (info ((ast->info info) init))
1449 (init-text (.text info))
1450 (init-locals (.locals info))
1451 (info (clone info #:text '()))
1453 (body-info ((ast->info info) body))
1454 (body-text (.text body-info))
1455 (body-length (length (text->list body-text)))
1457 (step-info ((ast->info info) `(expr-stmt ,step)))
1458 (step-text (.text step-info))
1459 (step-length (length (text->list step-text)))
1461 (test-jump->info ((test->jump->info info) test))
1462 (test+jump-info (test-jump->info 0))
1463 (test-length (length (text->list (.text test+jump-info))))
1465 (skip-body-text (list (lambda (f g ta t d)
1466 (i386:Xjump (+ body-length step-length)))))
1468 (jump-text (list (lambda (f g ta t d)
1469 (i386:Xjump (- (+ body-length step-length test-length))))))
1470 (jump-length (length (text->list jump-text)))
1472 (test-text (.text (test-jump->info jump-length))))
1482 #:globals (append globals (list-tail (.globals body-info) (length globals)))
1485 ;; FIXME: support break statement (see switch/case)
1486 ((while ,test ,body)
1487 (let* ((skip-info (lambda (body-length)
1488 (clone info #:text (append text
1489 (list (lambda (f g ta t d) (i386:Xjump body-length)))))))
1490 (text (.text (skip-info 0)))
1491 (text-length (length text))
1493 (body-info (lambda (body-length)
1494 ((ast->info (skip-info body-length)) body)))
1495 (body-text (list-tail (.text (body-info 0)) text-length))
1496 (body-length (length (text->list body-text)))
1498 (body-info (body-info body-length))
1500 (empty (clone info #:text '()))
1501 (test-jump->info ((test->jump->info empty) test))
1502 (test+jump-info (test-jump->info 0))
1503 (test-length (length (text->list (.text test+jump-info))))
1505 (jump-text (list (lambda (f g ta t d)
1506 (i386:Xjump (- (+ body-length test-length))))))
1507 (jump-length (length (text->list jump-text)))
1509 (test-text (.text (test-jump->info jump-length))))
1515 #:globals (.globals body-info))))
1517 ((do-while ,body ,test)
1518 (let* ((text-length (length text))
1520 (body-info ((ast->info info) body))
1521 (body-text (list-tail (.text body-info) text-length))
1522 (body-length (length (text->list body-text)))
1524 (empty (clone info #:text '()))
1525 (test-jump->info ((test->jump->info empty) test))
1526 (test+jump-info (test-jump->info 0))
1527 (test-length (length (text->list (.text test+jump-info))))
1529 (jump-text (list (lambda (f g ta t d)
1530 (i386:Xjump (- (+ body-length test-length))))))
1531 (jump-length (length (text->list jump-text)))
1533 (test-text (.text (test-jump->info jump-length))))
1539 #:globals (.globals body-info))))
1541 ((labeled-stmt (ident ,label) ,statement)
1542 (let ((info (clone info #:text (append text (list label)))))
1543 ((ast->info info) statement)))
1545 ((goto (ident ,label))
1546 (let* ((jump (lambda (n) (i386:XXjump n)))
1547 (offset (+ (length (jump 0)) (length (text->list text)))))
1550 (list (lambda (f g ta t d)
1551 (jump (- (label-offset (.function info) label f) offset))))))))
1553 ;;; FIXME: only zero?!
1554 ((p-expr (ident ,name))
1557 ((ident->accu info) name)
1558 (list (lambda (f g ta t d)
1560 (i386:accu-zero?)))))))
1562 ((p-expr (fixed ,value))
1563 (let ((value (cstring->number value)))
1566 (list (lambda (f g ta t d)
1568 (i386:value->accu value)
1569 (i386:accu-zero?))))))))
1571 ((de-ref (p-expr (ident ,name)))
1574 ((ident->accu info) name)
1575 (list (lambda (f g ta t d)
1577 (i386:byte-mem->accu)))))))
1579 ((fctn-call . ,call)
1580 (let ((info ((ast->info info) `(expr-stmt ,o))))
1582 (append (.text info)
1583 (list (lambda (f g ta t d)
1584 (i386:accu-zero?)))))))
1587 ;;((post-inc ,expr) ((ast->info info) `(expr-stmt ,o)))
1588 ((post-inc (p-expr (ident ,name)))
1591 ((ident->accu info) name)
1592 ((ident-add info) name 1)
1593 (list (lambda (f g ta t d)
1595 (i386:accu-zero?)))))))
1596 ((post-inc ,expr) ((ast->info info) `(expr-stmt ,o)))
1597 ((post-dec ,expr) ((ast->info info) `(expr-stmt ,o)))
1598 ((pre-inc ,expr) ((ast->info info) `(expr-stmt ,o)))
1599 ((pre-dec ,expr) ((ast->info info) `(expr-stmt ,o)))
1602 ((expr-stmt (post-inc (p-expr (ident ,name))))
1603 (clone info #:text (append text ((ident-add info) name 1))))
1606 ((expr-stmt (pre-inc (p-expr (ident ,name))))
1607 (or (assoc-ref locals name) barf)
1610 ((ident-add info) name 1)
1611 ((ident->accu info) name)
1612 (list (lambda (f g ta t d)
1614 ;;(i386:local->accu (local:id (assoc-ref locals name)))
1615 (i386:accu-zero?)))))))
1618 ((expr-stmt (post-dec (p-expr (ident ,name))))
1619 (or (assoc-ref locals name) barf)
1622 ((ident->accu info) name)
1623 ((ident-add info) name -1)
1624 (list (lambda (f g ta t d)
1626 ;;(i386:local-add (local:id (assoc-ref locals name)) -1)
1627 (i386:accu-zero?)))))))
1630 ((expr-stmt (pre-dec (p-expr (ident ,name))))
1631 (or (assoc-ref locals name) barf)
1634 ((ident-add info) name -1)
1635 ((ident->accu info) name)
1636 (list (lambda (f g ta t d)
1638 ;;(i386:local-add (local:id (assoc-ref locals name)) -1)
1639 ;;(i386:local->accu (local:id (assoc-ref locals name)))
1640 (i386:accu-zero?)))))))
1643 (let* ((test-info ((ast->info info) expr)))
1645 (append (.text test-info)
1646 (list (lambda (f g ta t d)
1649 (i386:accu-zero?)))))
1650 #:globals (.globals test-info))))
1653 (let* ((base ((expr->base info) a))
1654 (empty (clone base #:text '()))
1655 (accu ((expr->accu empty) b)))
1659 (list (lambda (f g ta t d)
1662 (list (lambda (f g ta t d)
1664 (list (lambda (f g ta t d)
1665 (i386:sub-base)))))))
1668 (let* ((base ((expr->base info) a))
1669 (empty (clone base #:text '()))
1670 (accu ((expr->accu empty) b)))
1674 (list (lambda (f g ta t d)
1677 (list (lambda (f g ta t d)
1679 (list (lambda (f g ta t d)
1680 (i386:sub-base)))))))
1683 (let* ((base ((expr->base info) a))
1684 (empty (clone base #:text '()))
1685 (accu ((expr->accu empty) b)))
1689 (list (lambda (f g ta t d)
1692 (list (lambda (f g ta t d)
1694 (list (lambda (f g ta t d)
1695 (i386:sub-base)))))))
1698 (let* ((base ((expr->base info) a))
1699 (empty (clone base #:text '()))
1700 (accu ((expr->accu empty) b)))
1704 (list (lambda (f g ta t d)
1707 (list (lambda (f g ta t d)
1709 (list (lambda (f g ta t d)
1712 (i386:xor-zf))))))))
1715 (let* ((base ((expr->base info) a))
1716 (empty (clone base #:text '()))
1717 (accu ((expr->accu empty) b)))
1721 (list (lambda (f g ta t d)
1724 (list (lambda (f g ta t d)
1726 (list (lambda (f g ta t d)
1727 (i386:base-sub)))))))
1730 (let* ((base ((expr->base info) a))
1731 (empty (clone base #:text '()))
1732 (accu ((expr->accu empty) b)))
1736 (list (lambda (f g ta t d)
1739 (list (lambda (f g ta t d)
1741 (list (lambda (f g ta t d)
1742 (i386:base-sub)))))))
1745 ((lshift . _) ((expr->accu info) o))
1746 ((rshift . _) ((expr->accu info) o))
1748 ;; TODO: byte dinges
1750 (let* ((base ((expr->base info) a))
1751 (empty (clone base #:text '()))
1752 (accu ((expr->accu empty) b)))
1756 (list (lambda (f g ta t d)
1759 (list (lambda (f g ta t d)
1761 (list (lambda (f g ta t d)
1762 (i386:base-sub)))))))
1764 ((Xsub (de-ref (p-expr (ident ,a))) (de-ref (p-expr (ident ,b))))
1767 (list (lambda (f g ta t d)
1769 (i386:local->accu (local:id (assoc-ref locals a)))
1770 (i386:byte-mem->base)
1771 (i386:local->accu (local:id (assoc-ref locals b)))
1772 (i386:byte-mem->accu)
1773 (i386:byte-sub-base)))))))
1776 ((array-ref (p-expr (fixed ,index)) (p-expr (ident ,array)))
1777 (let* ((value (cstring->number value))
1778 (type (ident->type info array))
1779 (size (type->size info type)))
1782 ((ident->base info) array)
1783 (list (lambda (f g ta t d)
1785 (i386:value->accu (* size index))
1787 (i386:byte-base-mem->accu)
1788 (i386:base-mem->accu)))))))))
1791 ((array-ref (p-expr (ident ,index)) (p-expr (ident ,array)))
1792 (let* ((type (ident->type info array))
1793 (size (type->size info type)))
1796 ((ident->base info) index)
1797 (list (lambda (f g ta t d)
1803 (if (= size 12) (i386:accu+base) '())
1804 (i386:accu-shl 2))))))
1805 ((ident->base info) array)
1806 (list (lambda (f g ta t d)
1808 (i386:byte-base-mem->accu)
1809 (i386:base-mem->accu))))))))
1812 (let ((accu ((expr->accu info) expr)))
1814 (append (.text accu) (list (lambda (f g ta t d) (i386:ret)))))))
1817 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ident ,name))))
1818 (if (.function info)
1819 (clone info #:locals (add-local locals name type 0))
1820 (clone info #:globals (append globals (list (ident->global name type 0 0))))))
1823 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ident ,name) (initzer (p-expr (fixed ,value))))))
1824 (let ((value (cstring->number value)))
1825 (if (.function info)
1826 (let* ((locals (add-local locals name type 0))
1827 (info (clone info #:locals locals)))
1830 ((value->ident info) name value))))
1831 (clone info #:globals (append globals (list (ident->global name type 0 value)))))))
1834 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ident ,name) (initzer (p-expr (char ,value))))))
1835 (if (not (.function info)) decl-barf0)
1836 (let* ((locals (add-local locals name type 0))
1837 (info (clone info #:locals locals))
1838 (value (char->integer (car (string->list value)))))
1841 ((value->ident info) name value)))))
1844 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ident ,name) (initzer (neg (p-expr (fixed ,value)))))))
1845 (let ((value (- (cstring->number value))))
1846 (if (.function info)
1847 (let* ((locals (add-local locals name type 0))
1848 (info (clone info #:locals locals)))
1851 ((value->ident info) name value))))
1852 (clone info #:globals (append globals (list (ident->global name type 0 value)))))))
1855 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ident ,name) (initzer (p-expr (ident ,local))))))
1856 (if (not (.function info)) decl-barf2)
1857 (let* ((locals (add-local locals name type 0))
1858 (info (clone info #:locals locals)))
1861 ((ident->accu info) local)
1862 ((accu->ident info) name)))))
1865 ;;(decl (decl-spec-list (type-spec (fixed-type "char"))) (init-declr-list (init-declr (ptr-declr (pointer) (ident "p")) (initzer (p-expr (string "t.c\n"))))))
1866 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)) (initzer (p-expr (string ,string))))))
1867 (when (not (.function info))
1870 (let* ((locals (add-local locals name type 1))
1871 (globals (append globals (list (string->global string))))
1872 (info (clone info #:locals locals #:globals globals)))
1875 (list (lambda (f g ta t d)
1877 (i386:global->accu (+ (data-offset (add-s:-prefix string) g) d)))))
1878 ((accu->ident info) name)))))
1881 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)) (initzer (p-expr (fixed ,value))))))
1882 (let ((value (cstring->number value)))
1883 (if (.function info)
1884 (let* ((locals (add-local locals name type 1))
1885 (info (clone info #:locals locals)))
1888 (list (lambda (f g ta t d)
1889 (i386:value->accu value)))
1890 ((accu->ident info) name))))
1891 (clone info #:globals (append globals (list (ident->global name type 0 value)))))))
1893 ;; char arena[20000];
1894 ((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
1895 (let ((type (ast->type type)))
1896 (if (.function info)
1898 (let* ((globals (.globals info))
1899 (count (cstring->number count))
1900 (size (type->size info type))
1901 ;;;;(array (make-global name type -1 (string->list (make-string (* count size) #\nul))))
1902 (array (make-global name type -1 (string->list (make-string (* count size) #\nul))))
1903 (globals (append globals (list array))))
1905 #:globals globals)))))
1907 ;;struct scm *g_cells = (struct scm*)arena;
1908 ((decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)) (initzer (cast (type-name (decl-spec-list (type-spec (struct-ref (ident ,=type)))) (abs-declr (pointer))) (p-expr (ident ,value)))))))
1909 ;;(stderr "0TYPE: ~s\n" type)
1910 (if (.function info)
1911 (let* ((locals (add-local locals name type 1))
1912 (info (clone info #:locals locals)))
1915 ((ident->accu info) name)
1916 ((accu->ident info) value)))) ;; FIXME: deref?
1917 (let* ((globals (append globals (list (ident->global name type 1 0))))
1918 (info (clone info #:globals globals)))
1921 ((ident->accu info) name)
1922 ((accu->ident info) value)))))) ;; FIXME: deref?
1925 ((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ident ,name))))
1926 ;;(stderr "1TYPE: ~s\n" type)
1927 (if (.function info)
1928 (clone info #:locals (add-local locals name type 0))
1929 (clone info #:globals (append globals (list (ident->global name type 0 0))))))
1932 ((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ident ,name) (initzer (p-expr (fixed ,value))))))
1933 ;;(stderr "2TYPE: ~s\n" type)
1934 (let ((value (cstring->number value)))
1935 (if (.function info)
1936 (let* ((locals (add-local locals name type 0))
1937 (info (clone info #:locals locals)))
1940 ((value->ident info) name value))))
1941 (let ((globals (append globals (list (ident->global name type 0 value)))))
1942 (clone info #:globals globals)))))
1944 ;; SCM g_stack = 0; // comment
1945 ((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ident _) (initzer (p-expr (fixed _))))) (comment _))
1946 ((ast->info info) (list-head o (- (length o) 1))))
1949 ((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ident ,name) (initzer (p-expr (ident ,local))))))
1950 ;;(stderr "3TYPE: ~s\n" type)
1951 (if (.function info)
1952 (let* ((locals (add-local locals name type 0))
1953 (info (clone info #:locals locals)))
1956 ((ident->accu info) local)
1957 ((accu->ident info) name))))
1958 (let* ((globals (append globals (list (ident->global name type 0 0))))
1959 (info (clone info #:globals globals)))
1962 ((ident->accu info) local)
1963 ((accu->ident info) name))))))
1965 ;; int (*function) (void) = g_functions[g_cells[fn].cdr].function;
1966 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,param-list)) (initzer ,initzer))))
1967 (let* ((locals (add-local locals name type 1))
1968 (info (clone info #:locals locals))
1969 (empty (clone info #:text '()))
1970 (accu ((expr->accu empty) initzer)))
1975 ((accu->ident info) name)
1976 (list (lambda (f g ta t d)
1978 ;;(i386:value->base t)
1980 (i386:value->base ta)
1981 (i386:accu+base)))))
1984 ;; char *p = (char*)g_cells;
1985 ((decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)) (initzer (cast (type-name (decl-spec-list (type-spec (fixed-type ,=type))) (abs-declr (pointer))) (p-expr (ident ,value)))))))
1986 ;;(stderr "6TYPE: ~s\n" type)
1987 (if (.function info)
1988 (let* ((locals (add-local locals name type 1))
1989 (info (clone info #:locals locals)))
1992 ((ident->accu info) value)
1993 ((accu->ident info) name))))
1994 (let* ((globals (append globals (list (ident->global name type 1 0))))
1995 (here (data-offset name globals))
1996 (there (data-offset value globals)))
1999 #:init (append (.init info)
2000 (list (lambda (functions globals ta t d data)
2002 (list-head data here)
2004 ;;; char *x = arena;
2005 (int->bv32 (+ d (data-offset value globals)))
2007 ;;;(list-head (list-tail data there) 4)
2008 (list-tail data (+ here 4))))))))))
2010 ;; char *p = g_cells;
2011 ((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)) (initzer (p-expr (ident ,value))))))
2012 ;;(stderr "7TYPE: ~s\n" type)
2013 (let ((type (decl->type type)))
2014 ;;(stderr "0DECL: ~s\n" type)
2015 (if (.function info)
2016 (let* ((locals (add-local locals name type 1))
2017 (info (clone info #:locals locals)))
2020 ((ident->accu info) value)
2021 ((accu->ident info) name))))
2022 (let* ((globals (append globals (list (ident->global name type 1 0))))
2023 (here (data-offset name globals)))
2026 #:init (append (.init info)
2027 (list (lambda (functions globals ta t d data)
2029 (list-head data here)
2031 ;;; char *x = arena;p
2032 (int->bv32 (+ d (data-offset value globals)))
2033 (list-tail data (+ here 4)))))))))))
2036 ((decl (decl-spec-list (type-spec (enum-def (ident ,name) (enum-def-list . ,fields)))))
2037 (let ((type (enum->type name fields))
2038 (constants (map ident->constant (map cadadr fields) (iota (length fields)))))
2040 #:types (append (.types info) (list type))
2041 #:constants (append constants (.constants info)))))
2044 ((decl (decl-spec-list (type-spec (struct-def (ident ,name) (field-list . ,fields)))))
2045 (let* ((type (struct->type (list "struct" name) (map struct-field fields))))
2046 ;;(stderr "type: ~a\n" type)
2047 (clone info #:types (append (.types info) (list type)))))
2050 ((expr-stmt (assn-expr (de-ref (post-inc (p-expr (ident ,name)))) (op ,op) ,b))
2051 (when (not (equal? op "="))
2052 (stderr "OOOPS0.0: op=~s\n" op)
2054 (let* ((empty (clone info #:text '()))
2055 (base ((expr->base empty) b)))
2059 ((base->ident-address info) name)
2060 ((ident-add info) name 1)))))
2063 ((expr-stmt (assn-expr (de-ref (post-dec (p-expr (ident ,name)))) (op ,op) ,b))
2064 (when (not (equal? op "="))
2065 (stderr "OOOPS0.0: op=~s\n" op)
2067 (let* ((empty (clone info #:text '()))
2068 (base ((expr->base empty) b)))
2072 ((base->ident-address info) name)
2073 ((ident-add info) name -1)))))
2077 ((expr-stmt (assn-expr (d-sel (ident ,field) . ,d-sel) (op ,op) ,b))
2078 (when (not (equal? op "="))
2079 (stderr "OOOPS0: op=~s\n" op)
2081 (let* ((empty (clone info #:text '()))
2082 (expr ((expr->accu* empty) `(d-sel (ident ,field) ,@d-sel))) ;; <-OFFSET
2083 (base ((expr->base empty) b))
2084 (type (list "struct" "scm")) ;; FIXME
2085 (fields (type->description info type))
2086 (size (type->size info type))
2087 (field-size 4) ;; FIXME:4, not fixed
2088 (offset (* field-size (1- (length (member field (reverse fields) (lambda (a b) (equal? a (cdr b)))))))) )
2089 (clone info #:text (append text
2092 (list (lambda (f g ta t d)
2093 ;;(i386:byte-base->accu-ref) ;; FIXME: size
2094 (i386:base->accu-address)
2102 ((expr-stmt (assn-expr (p-expr (ident ,name)) (op ,op) ,b))
2103 (when (and (not (equal? op "="))
2104 (not (equal? op "+="))
2105 (not (equal? op "-=")))
2106 (stderr "OOOPS1: op=~s\n" op)
2108 (let* ((empty (clone info #:text '()))
2109 (base ((expr->base empty) b)))
2110 (clone info #:text (append text
2112 (if (equal? op "=") '()
2113 (append ((ident->accu info) name)
2114 (list (lambda (f g ta t d)
2116 (if (equal? op "+=")
2119 (i386:accu->base))))))
2121 ((base->ident info) name)))))
2124 ((expr-stmt (assn-expr (de-ref (p-expr (ident ,array))) (op ,op) ,b))
2125 (when (not (equal? op "="))
2126 (stderr "OOOPS2: op=~s\n" op)
2128 (let* ((empty (clone info #:text '()))
2129 (base ((expr->base empty) b)))
2130 (clone info #:text (append text
2133 ((base->ident-address info) array)))))
2136 ((expr-stmt (assn-expr (array-ref (p-expr (fixed ,index)) (p-expr (ident ,array))) (op ,op) ,b))
2137 (when (not (equal? op "="))
2138 (stderr "OOOPS3: op=~s\n" op)
2140 (let* ((index (cstring->number index))
2141 (empty (clone info #:text '()))
2142 (base ((expr->base empty) b))
2143 (type (ident->type info array))
2144 (size (type->size info type))
2145 (ptr (ident->pointer info array)))
2149 (list (lambda (f g ta t d)
2151 (list (lambda (f g ta t d)
2153 (i386:value->base index)
2155 (if (eq? size 1) '()
2157 (if (> size 4) (i386:accu+accu) '())
2158 (if (> size 8) (i386:accu+base) '())
2159 (i386:accu-shl 2))))))
2160 ((ident->base info) array)
2161 (list (lambda (f g ta t d)
2163 (list (lambda (f g ta t d)
2165 (if (eq? size 1) (list (lambda (f g ta t d)
2166 (i386:byte-base->accu-address)))
2168 (list (lambda (f g ta t d)
2169 (i386:base-address->accu-address)))
2171 (list (lambda (f g ta t d)
2175 (i386:base-address->accu-address))))
2178 (list (lambda (f g ta t d)
2182 (i386:base-address->accu-address))))
2186 ((expr-stmt (assn-expr (array-ref (p-expr (ident ,index)) (p-expr (ident ,array))) (op ,op) ,b))
2187 ;;(stderr "pointer_cells4[]: ~s\n" array)
2188 (when (not (equal? op "="))
2189 (stderr "OOOPS4: op=~s\n" op)
2191 (let* ((empty (clone info #:text '()))
2192 (base ((expr->base empty) b))
2193 (type (ident->type info array))
2194 (size (type->size info type))
2195 (ptr (ident->pointer info array)))
2199 (list (lambda (f g ta t d)
2201 ((ident->base info) index)
2202 (list (lambda (f g ta t d)
2205 (if (eq? size 1) '()
2207 (if (> size 4) (i386:accu+accu) '())
2208 (if (> size 8) (i386:accu+base) '())
2209 (i386:accu-shl 2))))))
2210 ((ident->base info) array)
2211 (list (lambda (f g ta t d)
2213 (list (lambda (f g ta t d)
2215 (if (eq? size 1) (list (lambda (f g ta t d)
2216 (i386:byte-base->accu-address)))
2218 (list (lambda (f g ta t d)
2219 (i386:base-address->accu-address)))
2221 (list (lambda (f g ta t d)
2225 (i386:base-address->accu-address))))
2228 (list (lambda (f g ta t d)
2232 (i386:base-address->accu-address))))
2235 ;; g_functions[g_function++] = g_foo;
2236 ((expr-stmt (assn-expr (array-ref (post-inc (p-expr (ident ,index))) (p-expr (ident ,array))) (op ,op) ,b))
2237 (when (not (equal? op "="))
2238 (stderr "OOOPS5: op=~s\n" op)
2240 (let* ((empty (clone info #:text '()))
2241 (base ((expr->base empty) b))
2242 (type (ident->type info array))
2243 (size (type->size info type))
2244 (ptr (ident->pointer info array)))
2248 (list (lambda (f g ta t d)
2250 ((ident->base info) index)
2251 (list (lambda (f g ta t d)
2254 (if (eq? size 1) '()
2256 (if (> size 4) (i386:accu+accu) '())
2257 (if (> size 8) (i386:accu+base) '())
2258 (i386:accu-shl 2))))))
2259 ((ident->base info) array)
2260 (list (lambda (f g ta t d)
2262 (list (lambda (f g ta t d)
2264 (if (eq? size 1) (list (lambda (f g ta t d)
2265 (i386:byte-base->accu-address)))
2267 (list (lambda (f g ta t d)
2268 (i386:base-address->accu-address)))
2270 (list (lambda (f g ta t d)
2274 (i386:base-address->accu-address))))
2277 (list (lambda (f g ta t d)
2281 (i386:base-address->accu-address))))
2283 ((ident-add info) index 1)))))
2287 ;; struct f = {...};
2288 ((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (ident ,name) (initzer (initzer-list . ,initzers)))))
2289 (let* ((type (decl->type type))
2290 ;;(foo (stderr "1DECL: ~s\n" type))
2291 (fields (type->description info type))
2292 (size (type->size info type))
2293 (field-size 4)) ;; FIXME:4, not fixed
2294 ;;(stderr "7TYPE: ~s\n" type)
2295 (if (.function info)
2296 (let* ((globals (append globals (filter-map initzer->global initzers)))
2297 (locals (let loop ((fields (cdr fields)) (locals locals))
2298 (if (null? fields) locals
2299 (loop (cdr fields) (add-local locals "foobar" "int" 0)))))
2300 (locals (add-local locals name type -1))
2301 (info (clone info #:locals locals #:globals globals))
2302 (empty (clone info #:text '())))
2303 (let loop ((fields (iota (length fields))) (initzers initzers) (info info))
2304 (if (null? fields) info
2305 (let ((offset (* field-size (car fields)))
2306 (initzer (car initzers)))
2307 (loop (cdr fields) (cdr initzers)
2311 ((ident->accu info) name)
2312 (list (lambda (f g ta t d)
2314 (i386:accu->base))))
2315 (.text ((expr->accu empty) initzer))
2316 (list (lambda (f g ta t d)
2317 (i386:accu->base-address+n offset))))))))))
2318 (let* ((globals (append globals (filter-map initzer->global initzers)))
2319 (global (make-global name type -1 (string->list (make-string size #\nul))))
2320 (globals (append globals (list global)))
2321 (here (data-offset name globals))
2322 (info (clone info #:globals globals))
2324 (let loop ((fields (iota (length fields))) (initzers initzers) (info info))
2325 (if (null? fields) info
2326 (let ((offset (* field-size (car fields)))
2327 (initzer (car initzers)))
2328 (loop (cdr fields) (cdr initzers)
2332 (list (lambda (functions globals ta t d data)
2334 (list-head data (+ here offset))
2335 (initzer->data info functions globals ta t d (car initzers))
2336 (list-tail data (+ here offset field-size)))))))))))))))
2339 ;;char cc = g_cells[c].cdr; ==> generic?
2340 ((decl (decl-spec-list (type-spec ,type)) (init-declr-list (init-declr (ident ,name) (initzer ,initzer))))
2341 (let ((type (decl->type type)))
2342 (if (.function info)
2343 (let* ((locals (add-local locals name type 0))
2344 (info (clone info #:locals locals)))
2346 (append (.text ((expr->accu info) initzer))
2347 ((accu->ident info) name))))
2348 (let* ((globals (append globals (list (ident->global name type 1 0))))
2349 (here (data-offset name globals)))
2352 #:init (append (.init info)
2353 (list (lambda (functions globals ta t d data)
2355 (list-head data here)
2356 (initzer->data info functions globals ta t d initzer)
2357 (list-tail data (+ here 4)))))))))))
2360 ((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ftn-declr (ident ,name) (param-list . ,param-list)))))
2363 ((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ftn-declr (ident ,name) (param-list . ,param-list)))) (comment ,comment))
2366 ((decl (decl-spec-list (stor-spec (typedef)) (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ident ,name))))
2367 (let ((types (.types info)))
2368 (clone info #:types (cons (cons name (assoc-ref types type)) types))))
2370 ((decl (decl-spec-list (stor-spec (typedef)) ,type) ,name)
2371 (format (current-error-port) "SKIP: typedef=~s\n" o)
2375 (format (current-error-port) "SKIP: at=~s\n" o)
2379 (format (current-error-port) "SKIP: decl statement=~s\n" o)
2384 (format (current-error-port) "SKIP: statement=~s\n" o)
2388 (define (initzer->data info functions globals ta t d o)
2390 ((initzer (p-expr (fixed ,value))) (int->bv32 (cstring->number value)))
2391 ((initzer (neg (p-expr (fixed ,value)))) (int->bv32 (- (cstring->number value))))
2392 ((initzer (ref-to (p-expr (ident ,name))))
2393 ;;(stderr "INITZER[~a] => 0x~a\n" o (dec->hex (+ ta (function-offset name functions))))
2394 (int->bv32 (+ ta (function-offset name functions))))
2395 ((initzer (p-expr (ident ,name)))
2396 (let ((value (assoc-ref (.constants info) name)))
2398 ((initzer (p-expr (string ,string)))
2399 (int->bv32 (+ (data-offset (add-s:-prefix string) globals) d)))
2400 (_ (stderr "initzer->data:SKIP: ~s\n" o)
2404 (define (info->exe info)
2405 (display "dumping elf\n" (current-error-port))
2406 (map write-any (make-elf (.functions info) (.globals info) (.init info))))
2408 (define (.formals o)
2410 ((fctn-defn _ (ftn-declr _ ,formals) _) formals)
2411 ((fctn-defn _ (ptr-declr (pointer) (ftn-declr _ ,formals)) _) formals)
2412 (_ (format (current-error-port) ".formals: no match: ~a\n" o)
2415 (define (formal->text n)
2421 (define (formals->text o)
2423 ((param-list . ,formals)
2424 (let ((n (length formals)))
2425 (list (lambda (f g ta t d)
2427 (i386:function-preamble)
2428 (append-map (formal->text n) formals (iota n))
2429 (i386:function-locals))))))
2430 (_ (format (current-error-port) "formals->text: no match: ~a\n" o)
2433 (define (formal:ptr o)
2435 ((param-decl (decl-spec-list . ,decl) (param-declr (ptr-declr (pointer) . _)))
2437 ((param-decl (decl-spec-list . ,decl) (param-declr (ident ,name)))
2440 (stderr "formal:ptr[~a] => 0\n" o)
2443 (define (formals->locals o)
2445 ((param-list . ,formals)
2446 (let ((n (length formals)))
2447 (map make-local (map .name formals) (map .type formals) (map formal:ptr formals) (iota n -2 -1))))
2448 (_ (format (current-error-port) "formals->info: no match: ~a\n" o)
2451 (define (function->info info)
2454 ;;(stderr "formals=~a\n" (.formals o))
2455 (let* ((name (.name o))
2456 (text (formals->text (.formals o)))
2457 (locals (formals->locals (.formals o))))
2458 (format (current-error-port) "compiling ~a\n" name)
2459 ;;(stderr "locals=~a\n" locals)
2460 (let loop ((statements (.statements o))
2461 (info (clone info #:locals locals #:function (.name o) #:text text)))
2462 (if (null? statements) (clone info
2464 #:functions (append (.functions info) (list (cons name (.text info)))))
2465 (let* ((statement (car statements)))
2466 (loop (cdr statements)
2467 ((ast->info info) (car statements)))))))))
2469 (define (ast-list->info info)
2471 (let loop ((elements elements) (info info))
2472 (if (null? elements) info
2473 (loop (cdr elements) ((ast->info info) (car elements)))))))
2476 (let* ((ast (mescc))
2478 #:functions i386:libc
2479 #:types i386:type-alist))
2480 (ast (append libc ast))
2481 (info ((ast->info info) ast))
2482 (info ((ast->info info) _start)))