3 ;;; Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017,2018 Jan (janneke) 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/>.
21 (define-module (mescc mescc)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-26)
24 #:use-module (ice-9 pretty-print)
25 #:use-module (ice-9 getopt-long)
26 #:use-module (mes guile)
27 #:use-module (mes misc)
29 #:use-module (mescc preprocess)
30 #:use-module (mescc compile)
31 #:use-module (mescc M1)
32 #:export (mescc:preprocess
37 (define (mescc:preprocess options)
38 (let* ((defines (reverse (filter-map (multi-opt 'define) options)))
39 (includes (reverse (filter-map (multi-opt 'include) options)))
40 (pretty-print/write (string->symbol (option-ref options 'write (if guile? "pretty-print" "write"))))
41 (pretty-print/write (if (eq? pretty-print/write 'pretty-print) pretty-print write))
42 (files (option-ref options '() '("a.c")))
43 (input-file-name (car files))
44 (ast-file-name (cond ((and (option-ref options 'preprocess #f)
45 (option-ref options 'output #f)))
46 (else (replace-suffix input-file-name ".E"))))
47 (prefix (option-ref options 'prefix "")))
48 (with-output-to-file ast-file-name
49 (lambda _ (for-each (cut c->ast prefix defines includes write <>) files)))))
51 (define (c->ast prefix defines includes write file-name)
52 (with-input-from-file file-name
53 (cut write (c99-input->ast #:prefix prefix #:defines defines #:includes includes))))
55 (define (mescc:compile options)
56 (let* ((files (option-ref options '() '("a.c")))
57 (input-file-name (car files))
58 (M1-file-name (cond ((and (option-ref options 'compile #f)
59 (option-ref options 'output #f)))
60 (else (replace-suffix input-file-name ".S"))))
61 (infos (map (cut file->info options <>) files))
62 (verbose? (option-ref options 'verbose #f)))
64 (stderr "dumping: ~a\n" M1-file-name))
65 (with-output-to-file M1-file-name
66 (cut infos->M1 M1-file-name infos))
69 (define (file->info options file-name)
70 (cond ((.c? file-name) (c->info options file-name))
71 ((.E? file-name) (E->info options file-name))))
73 (define (c->info options file-name)
74 (let ((defines (reverse (filter-map (multi-opt 'define) options)))
75 (includes (reverse (filter-map (multi-opt 'include) options)))
76 (prefix (option-ref options 'prefix "")))
77 (with-input-from-file file-name
78 (cut c99-input->info #:prefix prefix #:defines defines #:includes includes))))
80 (define (E->info options file-name)
81 (let ((ast (with-input-from-file file-name read)))
84 (define (mescc:assemble options)
85 (let* ((files (option-ref options '() '("a.c")))
86 (input-file-name (car files))
87 (hex2-file-name (cond ((and (option-ref options 'assemble #f)
88 (option-ref options 'output #f)))
89 (else (replace-suffix input-file-name ".o"))))
90 (S-files (filter .S? files))
91 (hex2-files M1->hex2 ) ;; FIXME
92 (source-files (filter (disjoin .c? .E?) files))
93 (infos (map (cut file->info options <>) source-files)))
94 (if (and (pair? S-files) (pair? infos))
95 (error "mixing source and object not supported:" source-files S-files))
97 (M1->hex2 options S-files))
99 (infos->hex2 options hex2-file-name infos))
102 (define (mescc:link options)
103 (let* ((files (option-ref options '() '("a.c")))
104 (source-files (filter (disjoin .c? .E?) files))
105 (S-files (filter .S? files))
106 (o-files (filter .o? files))
107 (input-file-name (car files))
108 (hex2-file-name (if (or (string-suffix? ".hex2" input-file-name)
109 (string-suffix? ".o" input-file-name)) input-file-name
110 (replace-suffix input-file-name ".o")))
111 (infos (map (cut file->info options <>) source-files))
112 (S-files (filter .S? files))
113 (hex2-files (filter .o? files))
114 (hex2-files (if (null? S-files) hex2-files
115 (append hex2-files (list (M1->hex2 options S-files)))))
116 (hex2-files (if (null? infos) hex2-files
118 (list (infos->hex2 options hex2-file-name infos)))))
119 (libraries (filter-map (multi-opt 'library) options))
120 (libraries (if (pair? libraries) libraries '("c")))
121 (hex2-libraries (map (cut find-library options ".o" <>) libraries))
122 (hex2-files (append hex2-files hex2-libraries))
123 (S-files (append S-files (map (cut find-library options ".S" <>) libraries)))
124 (debug-info? (option-ref options 'debug-info #f))
125 (S-files (cons (replace-suffix input-file-name ".S") S-files))
126 (elf-footer (and debug-info?
127 (or (M1->blood-elf options S-files)
129 (or (hex2->elf options hex2-files #:elf-footer elf-footer)
132 (define (infos->hex2 options hex2-file-name infos)
133 (let* ((input-file-name (car (option-ref options '() '("a.c"))))
134 (M1-file-name (replace-suffix hex2-file-name ".S"))
135 (options (acons 'compile #t options)) ; ugh
136 (options (acons 'output hex2-file-name options))
137 (verbose? (option-ref options 'verbose #f)))
139 (stderr "dumping: ~a\n" M1-file-name))
140 (with-output-to-file M1-file-name
141 (cut infos->M1 M1-file-name infos))
142 (or (M1->hex2 options (list M1-file-name))
145 (define (M1->hex2 options M1-files)
146 (let* ((input-file-name (car (option-ref options '() '("a.c"))))
147 (M1-file-name (car M1-files))
148 (hex2-file-name (cond ((and (option-ref options 'assemble #f)
149 (option-ref options 'output #f)))
150 ((option-ref options 'assemble #f)
151 (replace-suffix input-file-name ".o"))
152 (else (replace-suffix M1-file-name ".o"))))
153 (verbose? (option-ref options 'verbose #f))
154 (M1 (or (getenv "M1") "M1"))
158 "-f" ,(arch-find options "x86.M1")
159 ,@(append-map (cut list "-f" <>) M1-files)
160 "-o" ,hex2-file-name)))
162 (stderr "~a\n" (string-join command)))
163 (and (zero? (apply assert-system* command))
166 (define* (hex2->elf options hex2-files #:key elf-footer)
167 (let* ((input-file-name (car (option-ref options '() '("a.c"))))
168 (elf-file-name (cond ((option-ref options 'output #f))
169 (else (replace-suffix input-file-name ""))))
170 (verbose? (option-ref options 'verbose #f))
171 (elf-footer (or elf-footer (arch-find options "elf32-footer-single-main.hex2")))
172 (hex2 (or (getenv "HEX2") "hex2"))
176 "--BaseAddress" "0x1000000"
177 "-f" ,(arch-find options "elf32-header.hex2")
178 "-f" ,(arch-find options "crt1.o")
179 ,@(append-map (cut list "-f" <>) hex2-files)
182 "-o" ,elf-file-name)))
184 (stderr "~a\n" (string-join command)))
185 (and (zero? (apply assert-system* command))
188 (define (M1->blood-elf options M1-files)
189 (let* ((M1-file-name (car M1-files))
190 (M1-blood-elf-footer (string-append M1-file-name ".blood-elf"))
191 (hex2-file-name (replace-suffix M1-file-name ".o"))
192 (blood-elf-footer (string-append hex2-file-name ".blood-elf"))
193 (verbose? (option-ref options 'verbose #f))
194 (blood-elf (or (getenv "BLOOD_ELF") "blood-elf"))
195 (command `(,blood-elf
196 "-f" ,(arch-find options "x86.M1")
197 ,@(append-map (cut list "-f" <>) M1-files)
198 "-o" ,M1-blood-elf-footer)))
200 (format (current-error-port) "~a\n" (string-join command)))
201 (and (zero? (apply assert-system* command))
202 (let* ((options (acons 'compile #t options)) ; ugh
203 (options (acons 'output blood-elf-footer options)))
204 (M1->hex2 options (list M1-blood-elf-footer))))))
206 (define (replace-suffix file-name suffix)
207 (let* ((parts (string-split file-name #\.))
208 (base (if (pair? (cdr parts)) (drop-right parts 1))))
209 (string-append (string-join base ".") suffix)))
211 (define (find-library options ext o)
212 (arch-find options (string-append "lib" o ext)))
214 (define* (arch-find options file-name)
215 (let* ((path (cons (prefix-file options "lib")
216 (filter-map (multi-opt 'library-dir) options)))
217 (arch-file-name (string-append "x86-mes/" file-name))
218 (verbose? (option-ref options 'verbose #f)))
220 (stderr "arch-find=~s\n" arch-file-name)
221 (stderr " path=~s\n" path)
222 (stderr " => ~s\n" (search-path path arch-file-name)))
223 (search-path path arch-file-name)))
225 (define (prefix-file options file-name)
226 (let ((prefix (option-ref options 'prefix "")))
227 (define (prefix-file o)
228 (if (string-null? prefix) o (string-append prefix "/" o)))
229 (prefix-file file-name)))
231 (define (assert-system* . commands)
232 (let ((status (apply system* commands)))
233 (when (not (zero? status))
234 (stderr "mescc: failed: ~a\n" (string-join command))
238 (define (multi-opt option-name) (lambda (o) (and (eq? (car o) option-name) (cdr o))))
240 (define (.c? o) (or (string-suffix? ".c" o)
241 (string-suffix? ".M2" o)))
242 (define (.E? o) (string-suffix? ".E" o))
243 (define (.S? o) (or (string-suffix? ".S" o)
244 (string-suffix? ".mes-S" o)
245 (string-suffix? "S" o)
246 (string-suffix? ".M1" o)))
247 (define (.o? o) (or (string-suffix? ".o" o)
248 (string-suffix? ".mes-o" o)
249 (string-suffix? "o" o)
250 (string-suffix? ".hex2" o)))