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 compiler))
44 (mes-use-module (mes elf))
45 (mes-use-module (mes hex2))
46 (mes-use-module (srfi srfi-1))
47 (mes-use-module (srfi srfi-26))
49 (format (current-error-port) "mescc.mes...\n")
51 (define %datadir (if (string-prefix? "@DATADIR" "@DATADIR@") "" "@DATADIR@"))
52 (define %docdir (if (string-prefix? "@DOCDIR" "@DOCDIR@") "doc/" "@DOCDIR@"))
53 (define %moduledir "module/")
54 (define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") "" "@PREFIX@"))
55 (define %version (if (string-prefix? "@VERSION" "@VERSION@") "git" "@VERSION@"))
57 (define (parse-opts args)
59 '((c (single-char #\c))
60 (D (single-char #\D) (value #t))
63 (help (single-char #\h))
64 (I (single-char #\I) (value #t))
65 (o (single-char #\o) (value #t))
66 (version (single-char #\V) (value #t))))
67 (options (getopt-long args option-spec))
68 (help? (option-ref options 'help #f))
69 (files (option-ref options '() '()))
70 (usage? (and (not help?) (null? files)))
71 (version? (option-ref options 'version #f)))
74 (format (current-output-port) "mescc.scm (mes) ~a\n" %version))
75 (and (or help? usage?)
76 (format (or (and usage? (current-error-port)) (current-output-port)) "\
77 Usage: mescc.mes [OPTION]... FILE...
78 -c compile and assemble, but do not link
79 -D DEFINE define DEFINE
80 -E preprocess only; do not compile, assemble or link
81 -g add debug info [GDB, objdump] TODO: hex2 footer
82 -h, --help display this help and exit
83 -I DIR append DIR to include path
84 -o FILE write output to FILE
85 -v, --version display version and exit
87 (exit (or (and usage? 2) 0)))
90 (define (read-object file)
91 (let ((char (with-input-from-file file read-char)))
92 (if (eq? char #\#) (error "hex2 format not supported:" file)))
93 (with-input-from-file file read))
95 (define (main:ast->info file)
96 (let ((ast (with-input-from-file file read)))
97 (with-input-from-file file
99 (c99-ast->info ast)))))
101 (define (source->ast defines includes)
103 (with-input-from-file file
105 (pretty-print (c99-input->ast #:defines defines #:includes includes))))))
107 (define (source->info defines includes)
109 (with-input-from-file file
111 ((c99-input->info #:defines defines #:includes includes))))))
114 (or (string-suffix? ".E" o)
115 (string-suffix? ".mes-E" o)))
118 (or (string-suffix? ".o" o)
119 (string-suffix? ".mes-o" o)))
122 (let* ((args (cons* (car args) (cdr (member "--" args))))
123 (options (parse-opts args))
124 (files (option-ref options '() '()))
125 (file (if (null? files) (string-append %docdir "examples/main.c")
127 (preprocess? (option-ref options 'E #f))
128 (compile? (option-ref options 'c #f))
129 (debug-info? (option-ref options 'g #f))
130 (asts (filter ast? files))
131 (objects (filter object? files))
132 (sources (filter (cut string-suffix? ".c" <>) files))
133 (base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
134 (out (option-ref options 'o (cond (compile? (string-append base ".o"))
135 (preprocess? (string-append base ".E"))
137 (multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
138 (defines (reverse (filter-map (multi-opt 'D) options)))
139 (includes (reverse (filter-map (multi-opt 'I) options))))
140 (when (getenv "MES_DEBUG") (format (current-error-port) "options=~s\n" options)
141 (format (current-error-port) "output: ~a\n" out))
142 (if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
143 (with-output-to-port (open-output-file out (if (and (not compile?)
144 (not preprocess?)) S_IRWXU))
146 (cond ((pair? objects) (let ((objects (map read-object objects)))
147 (if compile? (objects->hex2 objects)
148 (objects->elf objects))))
149 ((pair? asts) (let* ((infos (map main:ast->info asts))
150 (objects (map info->object infos)))
151 (if compile? (objects->hex2 objects)
152 (objects->elf objects))))
153 ((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
154 (let* ((infos (map (source->info defines includes) sources))
155 (objects (map info->object infos)))
156 (if compile? (objects->hex2 objects)
157 (objects->elf objects))))))))))
159 (main (command-line))