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
32 (mes-use-module (srfi srfi-1))))
34 (define (make-global name type pointer value)
35 (cons name (list type pointer value)))
37 (define global:type car)
38 (define global:pointer cadr)
39 (define global:value caddr)
41 (define (drop-s:-prefix o) (substring o 2))
42 (define (add-s:-prefix o) (string-append "s:" o))
45 (cond ((number? o) (number->string o 16))
46 ((char? o) (number->string (char->integer o) 16))))
48 (define (functions->lambdas functions)
49 (append-map (lambda (f) (or (cdr f) '())) functions))
51 (define (lambda/label->list f g ta t d)
53 (if (not (procedure? l/l)) '() (l/l f g ta t d))))
55 (define (text->list o)
56 (append-map (lambda/label->list '() '() 0 0 0) o))
58 (define functions->text
60 (lambda (functions globals ta t d)
61 (or (assoc-ref cache (cons ta (map car functions)))
62 (let ((text (let loop ((lambdas/labels (functions->lambdas functions)) (text '()))
63 (if (null? lambdas/labels) text
64 (loop (cdr lambdas/labels)
65 (append text ((lambda/label->list functions globals ta (- (length text)) d) (car lambdas/labels))))))))
66 (set! cache (assoc-set! cache (cons ta (map car functions)) text))
69 (define (function-prefix name functions)
71 ;;(member name (reverse functions) (lambda (a b) (equal? (car b) name)))
73 (x (if (and (pair? x) (equal? (caar x) "exit")) (reverse x) x)))
74 (member name x (lambda (a b) (equal? (car b) name)))))
76 (define function-offset
78 (lambda (name functions)
79 (or (assoc-ref cache name)
80 (let* ((functions (if (and (pair? functions) (equal? (caar functions) "exit")) functions (reverse functions)))
81 (prefix (and=> (function-prefix name functions) cdr))
84 (+ (length (functions->text (list (car prefix)) '() 0 0 0))
85 (if (null? (cdr prefix)) 0
86 (function-offset (caar prefix) functions)))))))
87 (if (and offset (or (equal? name "exit") (> offset 0))) (set! cache (assoc-set! cache name offset)))
92 (lambda (function label functions)
93 (or (assoc-ref cache (cons function label))
94 (let ((prefix (function-prefix function functions)))
96 (let* ((function-entry (car prefix))
97 (offset (let loop ((text (cdr function-entry)))
98 (if (or (equal? (car text) label) (null? text)) 0
99 (let* ((l/l (car text))
100 (t ((lambda/label->list '() '() 0 0 0) l/l))
102 (+ (loop (cdr text)) n))))))
104 (set! cache (assoc-set! cache (cons function label) offset)))
107 (define (globals->data globals)
108 (append-map (compose global:value cdr) globals))
112 (lambda (name globals)
113 (or ;;(assoc-ref cache name)
114 (let* ((prefix (member name (reverse globals)
116 (equal? (car b) name)))))
118 (let ((offset (length (globals->data (cdr prefix)))))
119 (set! cache (assoc-set! cache name offset))