2016-10-12 21:40:11 +00:00
|
|
|
#! /bin/sh
|
|
|
|
# -*-scheme-*-
|
2018-04-27 21:47:31 +00:00
|
|
|
mes_p=$(type -p mes)
|
|
|
|
if [ '(' -z "$mes_p" -a -z "$MES" ')' -o "$MES" = "guile" -o "$MES" = "mes.guile" ]; then
|
|
|
|
GODIR=${GODIR-@GODIR@}
|
|
|
|
GUILEDIR=${GUILEDIR-@GUILEDIR@}
|
|
|
|
[ "$GODIR" = @"GODIR"@ ] && GODIR=$(dirname $0)
|
|
|
|
[ "$GUILEDIR" = @"GUILEDIR"@ ] && GUILEDIR=$(dirname $0)/../guile
|
|
|
|
export GUILE_AUTO_COMPILE=${GUILE_AUTO_COMPILE-0}
|
|
|
|
GUILE_LOAD_COMPILED_PATH=$GODIR:$GUILE_LOAD_COMPILED_PATH
|
|
|
|
exec ${GUILE-guile} -L $GUILEDIR -e '(mescc)' -s "$0" "$@"
|
|
|
|
else
|
|
|
|
MES=${MES-$(dirname $0)/mes}
|
|
|
|
PREFIX=${PREFIX-@PREFIX@}
|
|
|
|
if [ "$MES_PREFIX" = @PREFIX""@ ]
|
|
|
|
then
|
|
|
|
MES_PREFIX=$(cd $(dirname $0)/.. && pwd)
|
|
|
|
export MES_PREFIX
|
2018-04-29 16:38:57 +00:00
|
|
|
else
|
|
|
|
MES_PREFIX=${MES_PREFIX-$PREFIX/share/mes}
|
2018-04-27 21:47:31 +00:00
|
|
|
fi
|
|
|
|
MES_MODULEDIR=${MES_MODULEDIR-$MES_PREFIX/"module"}
|
|
|
|
export MES_MODULEDIR
|
|
|
|
$MES -s $0 "$@"
|
|
|
|
exit $?
|
2017-12-09 21:38:51 +00:00
|
|
|
fi
|
2016-10-12 21:40:11 +00:00
|
|
|
!#
|
|
|
|
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
2017-11-21 18:22:26 +00:00
|
|
|
;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2016-10-12 21:40:11 +00:00
|
|
|
;;;
|
2016-12-18 14:44:09 +00:00
|
|
|
;;; This file is part of Mes.
|
2016-10-12 21:40:11 +00:00
|
|
|
;;;
|
|
|
|
;;; Mes is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Mes is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-04-27 21:47:31 +00:00
|
|
|
#!
|
|
|
|
Run with Guile-1.8:
|
|
|
|
GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' scripts/mescc
|
|
|
|
!#
|
2016-10-12 21:40:11 +00:00
|
|
|
|
2018-04-27 21:47:31 +00:00
|
|
|
(define-module (mescc)
|
|
|
|
#:use-module (language c99 info)
|
|
|
|
#:use-module (language c99 compiler)
|
|
|
|
#:use-module (mes elf)
|
|
|
|
#:use-module (mes M1)
|
|
|
|
#:use-module (ice-9 getopt-long)
|
|
|
|
#:use-module (ice-9 pretty-print)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:export (main))
|
2016-10-12 21:40:11 +00:00
|
|
|
|
2018-04-27 21:47:31 +00:00
|
|
|
(define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") (or (getenv "MES_PREFIX") "") "@PREFIX@"))
|
2016-10-12 21:40:11 +00:00
|
|
|
|
2018-04-27 21:47:31 +00:00
|
|
|
(cond-expand
|
|
|
|
(mes
|
2018-04-29 16:38:57 +00:00
|
|
|
(define %scheme "mes")
|
|
|
|
(define (set-port-encoding! port encoding) #t))
|
2018-04-27 21:47:31 +00:00
|
|
|
(guile-2
|
|
|
|
(define %scheme "guile")
|
|
|
|
(define-macro (mes-use-module . rest) #t)
|
|
|
|
(module-define! (resolve-module '(language c99 compiler)) '%prefix %prefix))
|
|
|
|
(guile
|
|
|
|
(use-modules (ice-9 syncase))
|
|
|
|
(define %scheme "guile")
|
|
|
|
(define-macro (mes-use-module . rest) #t)
|
|
|
|
(module-define! (resolve-module '(language c99 compiler)) '%prefix %prefix)))
|
|
|
|
|
|
|
|
(define guile? (equal? %scheme "guile"))
|
2017-04-04 07:53:05 +00:00
|
|
|
|
2016-12-31 08:03:07 +00:00
|
|
|
(mes-use-module (mes guile))
|
2017-05-17 11:56:25 +00:00
|
|
|
(mes-use-module (mes getopt-long))
|
2017-05-22 17:22:18 +00:00
|
|
|
(mes-use-module (mes pretty-print))
|
mescc: Use records for Guile: preparation.
* module/language/c99/info.mes: New file.
* module/mes/M1.mes: Use it.
* scripts/mescc.mes: Use it.
* module/language/c99/compiler.mes: Use it. (<info>, <types>,
<constants>, <functions>, <globals>, <locals>, <function>, <text>,
<break>, <continue>, make, info?, .info, .types, .constants,
.functions, .globals, .locals, .function, .text, .break, .continue):
Remove.
* module/language/c99/info.scm: New file.
* module/language/c99/compiler.scm: Use it.
* guile/mescc.scm: Use it.
* module/mes/M1.scm: Use it.
2017-07-14 18:42:26 +00:00
|
|
|
(mes-use-module (language c99 info))
|
2016-12-31 08:03:07 +00:00
|
|
|
(mes-use-module (language c99 compiler))
|
2018-01-01 20:13:46 +00:00
|
|
|
(mes-use-module (mes display))
|
2017-06-11 11:11:40 +00:00
|
|
|
(mes-use-module (mes elf))
|
2017-07-02 14:25:14 +00:00
|
|
|
(mes-use-module (mes M1))
|
2017-05-17 11:56:25 +00:00
|
|
|
(mes-use-module (srfi srfi-1))
|
|
|
|
(mes-use-module (srfi srfi-26))
|
2016-12-31 08:03:07 +00:00
|
|
|
|
2018-04-27 21:47:31 +00:00
|
|
|
(format (current-error-port) "mescc[~a]...\n" %scheme)
|
2017-04-02 16:50:07 +00:00
|
|
|
|
2017-05-17 11:56:25 +00:00
|
|
|
(define (parse-opts args)
|
|
|
|
(let* ((option-spec
|
|
|
|
'((c (single-char #\c))
|
2018-01-02 06:05:41 +00:00
|
|
|
(define (single-char #\D) (value #t))
|
2017-05-22 17:22:18 +00:00
|
|
|
(E (single-char #\E))
|
2017-06-11 11:11:40 +00:00
|
|
|
(g (single-char #\g))
|
2017-05-17 11:56:25 +00:00
|
|
|
(help (single-char #\h))
|
2018-01-02 06:05:41 +00:00
|
|
|
(include (single-char #\I) (value #t))
|
2017-05-22 17:22:18 +00:00
|
|
|
(o (single-char #\o) (value #t))
|
2018-04-27 21:47:31 +00:00
|
|
|
(version (single-char #\V))
|
|
|
|
(write (single-char #\w) (value #t))))
|
2017-05-22 17:22:18 +00:00
|
|
|
(options (getopt-long args option-spec))
|
|
|
|
(help? (option-ref options 'help #f))
|
|
|
|
(files (option-ref options '() '()))
|
|
|
|
(usage? (and (not help?) (null? files)))
|
2017-05-17 11:56:25 +00:00
|
|
|
(version? (option-ref options 'version #f)))
|
|
|
|
(or
|
|
|
|
(and version?
|
2018-04-27 21:47:31 +00:00
|
|
|
(format (current-output-port) "mescc (mes) ~a\n" %version))
|
2017-05-17 11:56:25 +00:00
|
|
|
(and (or help? usage?)
|
|
|
|
(format (or (and usage? (current-error-port)) (current-output-port)) "\
|
2018-04-27 21:47:31 +00:00
|
|
|
Usage: mescc [OPTION]... FILE...
|
2017-05-17 11:56:25 +00:00
|
|
|
-c compile and assemble, but do not link
|
|
|
|
-D DEFINE define DEFINE
|
2017-05-22 17:22:18 +00:00
|
|
|
-E preprocess only; do not compile, assemble or link
|
2017-06-25 07:33:55 +00:00
|
|
|
-g add debug info [GDB, objdump] TODO: hex2 footer
|
2017-05-17 11:56:25 +00:00
|
|
|
-h, --help display this help and exit
|
|
|
|
-I DIR append DIR to include path
|
|
|
|
-o FILE write output to FILE
|
|
|
|
-v, --version display version and exit
|
2018-04-27 21:47:31 +00:00
|
|
|
-w,--write=TYPE dump Nyacc AST using TYPE {pretty-print,write}
|
|
|
|
|
|
|
|
Environment variables:
|
|
|
|
|
|
|
|
MES=BINARY run on mes-executable BINARY {mes,guile}
|
|
|
|
MES_DEBUG=LEVEL show debug output with verbosity LEVEL {0..5}
|
|
|
|
NYACC_TRACE=1 show Nyacc progress
|
2017-05-17 11:56:25 +00:00
|
|
|
")
|
|
|
|
(exit (or (and usage? 2) 0)))
|
|
|
|
options)))
|
|
|
|
|
2017-06-11 11:11:40 +00:00
|
|
|
(define (read-object file)
|
|
|
|
(let ((char (with-input-from-file file read-char)))
|
|
|
|
(if (eq? char #\#) (error "hex2 format not supported:" file)))
|
|
|
|
(with-input-from-file file read))
|
2017-05-17 11:56:25 +00:00
|
|
|
|
2017-05-22 17:22:18 +00:00
|
|
|
(define (main:ast->info file)
|
|
|
|
(let ((ast (with-input-from-file file read)))
|
2018-01-02 20:35:55 +00:00
|
|
|
(c99-ast->info ast)))
|
2017-05-22 17:22:18 +00:00
|
|
|
|
2018-04-27 21:47:31 +00:00
|
|
|
(define (source->ast write defines includes)
|
2017-05-22 17:22:18 +00:00
|
|
|
(lambda (file)
|
|
|
|
(with-input-from-file file
|
|
|
|
(lambda ()
|
2018-04-22 14:38:55 +00:00
|
|
|
(write (c99-input->ast #:defines defines #:includes includes))))))
|
2017-05-22 17:22:18 +00:00
|
|
|
|
2017-05-17 11:56:25 +00:00
|
|
|
(define (source->info defines includes)
|
|
|
|
(lambda (file)
|
|
|
|
(with-input-from-file file
|
|
|
|
(lambda ()
|
|
|
|
((c99-input->info #:defines defines #:includes includes))))))
|
|
|
|
|
2017-05-22 17:22:18 +00:00
|
|
|
(define (ast? o)
|
|
|
|
(or (string-suffix? ".E" o)
|
2018-04-29 16:38:57 +00:00
|
|
|
(string-suffix? (string-append "." %scheme "-E") o)
|
|
|
|
(string-suffix? "-E" o)))
|
2017-05-22 17:22:18 +00:00
|
|
|
|
|
|
|
(define (object? o)
|
|
|
|
(or (string-suffix? ".o" o)
|
2018-04-29 16:38:57 +00:00
|
|
|
(string-suffix? (string-append "." %scheme "-o") o)
|
|
|
|
(string-suffix? "-o" o)))
|
2017-05-22 17:22:18 +00:00
|
|
|
|
2017-05-17 11:56:25 +00:00
|
|
|
(define (main args)
|
2018-04-22 09:51:28 +00:00
|
|
|
(let* ((options (parse-opts args))
|
2017-05-17 11:56:25 +00:00
|
|
|
(files (option-ref options '() '()))
|
2017-08-06 11:14:09 +00:00
|
|
|
(file (car files))
|
2017-05-22 17:22:18 +00:00
|
|
|
(preprocess? (option-ref options 'E #f))
|
2017-05-17 11:56:25 +00:00
|
|
|
(compile? (option-ref options 'c #f))
|
2017-06-11 11:11:40 +00:00
|
|
|
(debug-info? (option-ref options 'g #f))
|
2017-05-22 17:22:18 +00:00
|
|
|
(asts (filter ast? files))
|
|
|
|
(objects (filter object? files))
|
2017-05-17 11:56:25 +00:00
|
|
|
(sources (filter (cut string-suffix? ".c" <>) files))
|
|
|
|
(base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
|
2017-05-22 17:22:18 +00:00
|
|
|
(out (option-ref options 'o (cond (compile? (string-append base ".o"))
|
|
|
|
(preprocess? (string-append base ".E"))
|
|
|
|
(else "a.out"))))
|
2017-05-17 11:56:25 +00:00
|
|
|
(multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
|
2018-01-02 06:05:41 +00:00
|
|
|
(defines (reverse (filter-map (multi-opt 'define) options)))
|
2018-04-27 21:47:31 +00:00
|
|
|
(includes (reverse (filter-map (multi-opt 'include) options)))
|
|
|
|
(pretty-print/write (string->symbol (option-ref options 'write (if guile? "pretty-print" "write"))))
|
|
|
|
(pretty-print/write (if (eq? pretty-print/write 'pretty-print) pretty-print write)))
|
|
|
|
(when (getenv "MES_DEBUG")
|
|
|
|
(setenv "NYACC_TRACE" "yes")
|
|
|
|
(format (current-error-port) "options=~s\n" options)
|
|
|
|
(format (current-error-port) "output: ~a\n" out))
|
2017-05-17 11:56:25 +00:00
|
|
|
(if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
|
2018-04-27 21:47:31 +00:00
|
|
|
(with-output-to-file out
|
2017-05-17 11:56:25 +00:00
|
|
|
(lambda ()
|
2018-04-27 21:47:31 +00:00
|
|
|
(if (and (not compile?)
|
|
|
|
(not preprocess?)) (set-port-encoding! (current-output-port) "ISO-8859-1"))
|
2017-06-11 11:11:40 +00:00
|
|
|
(cond ((pair? objects) (let ((objects (map read-object objects)))
|
2017-07-02 14:25:14 +00:00
|
|
|
(if compile? (objects->M1 objects)
|
2017-06-11 11:11:40 +00:00
|
|
|
(objects->elf objects))))
|
|
|
|
((pair? asts) (let* ((infos (map main:ast->info asts))
|
|
|
|
(objects (map info->object infos)))
|
2017-07-02 14:25:14 +00:00
|
|
|
(if compile? (objects->M1 objects)
|
2017-06-11 11:11:40 +00:00
|
|
|
(objects->elf objects))))
|
2018-04-27 21:47:31 +00:00
|
|
|
((pair? sources) (if preprocess? (map (source->ast pretty-print/write defines includes) sources)
|
2017-06-11 11:11:40 +00:00
|
|
|
(let* ((infos (map (source->info defines includes) sources))
|
|
|
|
(objects (map info->object infos)))
|
2017-07-02 14:25:14 +00:00
|
|
|
(if compile? (objects->M1 objects)
|
2017-06-11 11:11:40 +00:00
|
|
|
(objects->elf objects))))))))))
|
2016-10-16 06:00:04 +00:00
|
|
|
|
2017-01-03 22:48:12 +00:00
|
|
|
(main (command-line))
|
2018-04-09 05:12:38 +00:00
|
|
|
'done
|