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