3 MES=${MES-$(dirname $0)/mes}
5 echo '()' | cat $moduledir/mes/base-0.mes $0 /dev/stdin | $MES $MES_FLAGS -- "$@"
8 ([ -f a.out ] && chmod +x a.out)
12 ;;; Mes --- Maxwell Equations of Software
13 ;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
15 ;;; This file is part of Mes.
17 ;;; Mes is free software; you can redistribute it and/or modify it
18 ;;; under the terms of the GNU General Public License as published by
19 ;;; the Free Software Foundation; either version 3 of the License, or (at
20 ;;; your option) any later version.
22 ;;; Mes is distributed in the hope that it will be useful, but
23 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;;; GNU General Public License for more details.
27 ;;; You should have received a copy of the GNU General Public License
28 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
32 ;;; mescc.mes is a proof-of-concept simplistic C compiler and linker
37 ;;(mes-use-module (language c compiler))
40 (mes-use-module (mes guile))
41 (mes-use-module (mes getopt-long))
42 (mes-use-module (mes pretty-print))
43 (mes-use-module (language c99 info))
44 (mes-use-module (language c99 compiler))
45 (mes-use-module (mes elf))
46 (mes-use-module (mes M1))
47 (mes-use-module (srfi srfi-1))
48 (mes-use-module (srfi srfi-26))
50 (format (current-error-port) "mescc.mes...\n")
52 (define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") (or (getenv "PREFIX") "") "@PREFIX@"))
54 (define (parse-opts args)
56 '((c (single-char #\c))
57 (D (single-char #\D) (value #t))
60 (help (single-char #\h))
61 (I (single-char #\I) (value #t))
62 (o (single-char #\o) (value #t))
63 (version (single-char #\V) (value #t))))
64 (options (getopt-long args option-spec))
65 (help? (option-ref options 'help #f))
66 (files (option-ref options '() '()))
67 (usage? (and (not help?) (null? files)))
68 (version? (option-ref options 'version #f)))
71 (format (current-output-port) "mescc.scm (mes) ~a\n" %version))
72 (and (or help? usage?)
73 (format (or (and usage? (current-error-port)) (current-output-port)) "\
74 Usage: mescc.mes [OPTION]... FILE...
75 -c compile and assemble, but do not link
76 -D DEFINE define DEFINE
77 -E preprocess only; do not compile, assemble or link
78 -g add debug info [GDB, objdump] TODO: hex2 footer
79 -h, --help display this help and exit
80 -I DIR append DIR to include path
81 -o FILE write output to FILE
82 -v, --version display version and exit
84 (exit (or (and usage? 2) 0)))
87 (define (read-object file)
88 (let ((char (with-input-from-file file read-char)))
89 (if (eq? char #\#) (error "hex2 format not supported:" file)))
90 (with-input-from-file file read))
92 (define (main:ast->info file)
93 (let ((ast (with-input-from-file file read)))
94 (with-input-from-file file
96 (c99-ast->info ast)))))
98 (define (source->ast defines includes)
100 (with-input-from-file file
102 (pretty-print (c99-input->ast #:defines defines #:includes includes))))))
104 (define (source->info defines includes)
106 (with-input-from-file file
108 ((c99-input->info #:defines defines #:includes includes))))))
111 (or (string-suffix? ".E" o)
112 (string-suffix? ".mes-E" o)))
115 (or (string-suffix? ".o" o)
116 (string-suffix? ".mes-o" o)))
119 (let* ((args (cons* (car args) (cdr (member "--" args))))
120 (options (parse-opts args))
121 (files (option-ref options '() '()))
122 (file (if (null? files) (string-append %docdir "examples/main.c")
124 (preprocess? (option-ref options 'E #f))
125 (compile? (option-ref options 'c #f))
126 (debug-info? (option-ref options 'g #f))
127 (asts (filter ast? files))
128 (objects (filter object? files))
129 (sources (filter (cut string-suffix? ".c" <>) files))
130 (base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
131 (out (option-ref options 'o (cond (compile? (string-append base ".o"))
132 (preprocess? (string-append base ".E"))
134 (multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
135 (defines (reverse (filter-map (multi-opt 'D) options)))
136 (includes (reverse (filter-map (multi-opt 'I) options))))
137 (when (getenv "MES_DEBUG") (format (current-error-port) "options=~s\n" options)
138 (format (current-error-port) "output: ~a\n" out))
139 (if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
140 (with-output-to-port (open-output-file out (if (and (not compile?)
141 (not preprocess?)) S_IRWXU))
143 (cond ((pair? objects) (let ((objects (map read-object objects)))
144 (if compile? (objects->M1 objects)
145 (objects->elf objects))))
146 ((pair? asts) (let* ((infos (map main:ast->info asts))
147 (objects (map info->object infos)))
148 (if compile? (objects->M1 objects)
149 (objects->elf objects))))
150 ((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
151 (let* ((infos (map (source->info defines includes) sources))
152 (objects (map info->object infos)))
153 (if compile? (objects->M1 objects)
154 (objects->elf objects))))))))))
156 (main (command-line))