2016-12-31 08:03:07 +00:00
|
|
|
#! /bin/sh
|
|
|
|
# -*-scheme-*-
|
2017-04-12 19:27:59 +00:00
|
|
|
GODIR=${GODIR-@GODIR@}
|
2017-05-05 20:13:01 +00:00
|
|
|
GUILEDIR=${GUILEDIR-@GUILEDIR@}
|
2017-05-17 11:56:25 +00:00
|
|
|
[ "$GODIR" = @"GODIR"@ ] && GODIR=$(dirname $0)
|
|
|
|
[ "$GUILEDIR" = @"GUILEDIR"@ ] && GUILEDIR=$(dirname $0)
|
2017-01-17 18:03:08 +00:00
|
|
|
export GUILE_AUTO_COMPILE=${GUILE_AUTO_COMPILE-0}
|
2017-05-05 20:13:01 +00:00
|
|
|
exec ${GUILE-guile} -L $GUILEDIR -C $GODIR -e '(mescc)' -s "$0" "$@"
|
2016-12-31 08:03:07 +00:00
|
|
|
!#
|
|
|
|
|
|
|
|
;;; Mes --- The Maxwell Equations of Software
|
mescc: Run module/base-0.mes.
* gc.c: New file.
* vector.c: New file.
* mes.c: Remove vector and gc functions, include vector.c, gc.c.
* GNUmakefile (mes.o): Add gc, vector dependencies.
* scaffold/mini-mes.c (eval_apply): Support primitive-load through
read_input_file.
(getenv_, open_input_file, current_input_port,
set_current_input_port force_output, exit_, values, arity_, xassq,
is_p, minus, plus, divide, modulo multiply, logior, ash): New function.
(mes_symbols): Add symbols %gnuc, %mesc.
* scaffold/mini-mes.c (): New functions.
* scaffold/b-0.mes: New file.
* scaffold/t-0.mes: New file.
2017-03-26 19:13:01 +00:00
|
|
|
;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
|
2016-12-31 08:03:07 +00:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; 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/>.
|
|
|
|
|
|
|
|
;; The Maxwell Equations of Software -- John McCarthy page 13
|
|
|
|
;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
|
|
|
|
|
|
|
|
#!
|
|
|
|
Run with Guile-1.8:
|
|
|
|
GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/mescc.scm
|
|
|
|
!#
|
|
|
|
|
|
|
|
(define-module (mescc)
|
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
|
|
|
#:use-module (language c99 info)
|
2016-12-31 08:03:07 +00:00
|
|
|
#:use-module (language c99 compiler)
|
2017-06-11 11:11:40 +00:00
|
|
|
#:use-module (mes elf)
|
2017-07-02 14:25:14 +00:00
|
|
|
#:use-module (mes M1)
|
2017-05-17 11:56:25 +00:00
|
|
|
#:use-module (ice-9 getopt-long)
|
2017-05-22 17:22:18 +00:00
|
|
|
#:use-module (ice-9 pretty-print)
|
2017-05-17 11:56:25 +00:00
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
2016-12-31 08:03:07 +00:00
|
|
|
#:export (main))
|
|
|
|
|
2017-07-26 13:42:44 +00:00
|
|
|
(define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") (or (getenv "PREFIX") "") "@PREFIX@"))
|
2017-04-02 16:50:07 +00:00
|
|
|
(module-define! (resolve-module '(language c99 compiler)) '%prefix %prefix)
|
|
|
|
|
2017-05-17 11:56:25 +00:00
|
|
|
(define (parse-opts args)
|
|
|
|
(let* ((option-spec
|
|
|
|
'((c (single-char #\c))
|
2017-05-22 17:22:18 +00:00
|
|
|
(D (single-char #\D) (value #t))
|
|
|
|
(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))
|
2017-05-22 17:22:18 +00:00
|
|
|
(I (single-char #\I) (value #t))
|
|
|
|
(o (single-char #\o) (value #t))
|
|
|
|
(version (single-char #\V) (value #t))))
|
|
|
|
(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?
|
2017-05-22 17:22:18 +00:00
|
|
|
(format (current-output-port) "mescc.scm (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)) "\
|
2017-06-11 11:11:40 +00:00
|
|
|
Usage: mescc.scm [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
|
|
|
|
")
|
|
|
|
(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)))
|
|
|
|
(with-input-from-file file
|
|
|
|
(lambda ()
|
|
|
|
(c99-ast->info ast)))))
|
|
|
|
|
|
|
|
(define (source->ast defines includes)
|
|
|
|
(lambda (file)
|
|
|
|
(with-input-from-file file
|
|
|
|
(lambda ()
|
|
|
|
(pretty-print (c99-input->ast #:defines defines #:includes includes))))))
|
|
|
|
|
2017-05-17 11:56:25 +00:00
|
|
|
(define (source->info defines includes)
|
|
|
|
(lambda (file)
|
2016-12-31 08:03:07 +00:00
|
|
|
(with-input-from-file file
|
2017-05-17 11:56:25 +00:00
|
|
|
(lambda ()
|
|
|
|
((c99-input->info #:defines defines #:includes includes))))))
|
mescc: Parse mlibc early, show progress.
* module/mes/libc.mes (_start, strlen, getchar, assert_fail, ungetc,
putchar, fputc, eputs, fputs, puts, strcmp, itoa, isdigit, atoi,
malloc, realloc, strncmp, c:getenv): Change to function, add
progress. Update callers.
* module/language/c99/compiler.mes (c99-input->info): Compile libc separately.
* guile/mescc.scm: Update progress.
* scripts/mescc.mes: Update progress.
2017-04-24 17:09:54 +00:00
|
|
|
|
2017-05-22 17:22:18 +00:00
|
|
|
(define (ast? o)
|
|
|
|
(or (string-suffix? ".E" o)
|
|
|
|
(string-suffix? ".guile-E" o)))
|
|
|
|
|
|
|
|
(define (object? o)
|
|
|
|
(or (string-suffix? ".o" o)
|
|
|
|
(string-suffix? ".guile-o" o)))
|
|
|
|
|
2017-05-17 11:56:25 +00:00
|
|
|
(define (main args)
|
|
|
|
(let* ((options (parse-opts args))
|
|
|
|
(files (option-ref options '() '()))
|
|
|
|
(file (if (null? files) (string-append %docdir "examples/main.c")
|
|
|
|
(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)))))
|
|
|
|
(defines (reverse (filter-map (multi-opt 'D) options)))
|
2017-06-25 07:33:55 +00:00
|
|
|
(includes (reverse (filter-map (multi-opt 'I) options))))
|
2017-05-17 11:56:25 +00:00
|
|
|
(when (getenv "MES_DEBUG") (format (current-error-port) "options=~s\n" options)
|
|
|
|
(format (current-error-port) "output: ~a\n" out))
|
|
|
|
(if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
|
|
|
|
(with-output-to-file out
|
|
|
|
(lambda ()
|
2017-05-22 17:22:18 +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))))
|
2017-05-22 17:22:18 +00:00
|
|
|
((pair? sources) (if preprocess? (map (source->ast 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))))))))
|
2017-05-22 17:22:18 +00:00
|
|
|
(if (and (not compile?)
|
|
|
|
(not preprocess?))
|
2017-05-17 11:56:25 +00:00
|
|
|
(chmod out #o755))))
|