2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes --- Maxwell Equations of Software
|
2018-05-25 06:05:02 +00:00
|
|
|
;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; This file is part of GNU Mes.
|
2018-05-25 06:05:02 +00:00
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes is free software; you can redistribute it and/or modify it
|
2018-05-25 06:05:02 +00:00
|
|
|
;;; 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.
|
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes is distributed in the hope that it will be useful, but
|
2018-05-25 06:05:02 +00:00
|
|
|
;;; 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
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define-module (mescc mescc)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (ice-9 pretty-print)
|
|
|
|
#:use-module (ice-9 getopt-long)
|
|
|
|
#:use-module (mes guile)
|
|
|
|
#:use-module (mes misc)
|
|
|
|
|
2018-08-14 10:35:24 +00:00
|
|
|
#:use-module (mescc i386 info)
|
2018-05-25 06:05:02 +00:00
|
|
|
#:use-module (mescc preprocess)
|
|
|
|
#:use-module (mescc compile)
|
|
|
|
#:use-module (mescc M1)
|
|
|
|
#:export (mescc:preprocess
|
|
|
|
mescc:compile
|
|
|
|
mescc:assemble
|
|
|
|
mescc:link))
|
|
|
|
|
2018-08-14 10:35:24 +00:00
|
|
|
(define %info (x86-info))
|
|
|
|
|
2018-08-14 08:58:02 +00:00
|
|
|
(define GUILE-with-output-to-file with-output-to-file)
|
|
|
|
(define (with-output-to-file file-name thunk)
|
|
|
|
(if (equal? file-name "-") (thunk)
|
|
|
|
(GUILE-with-output-to-file file-name thunk)))
|
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
(define (mescc:preprocess options)
|
2018-08-15 17:11:54 +00:00
|
|
|
(let* ((pretty-print/write (string->symbol (option-ref options 'write (if guile? "pretty-print" "write"))))
|
2018-05-25 06:05:02 +00:00
|
|
|
(pretty-print/write (if (eq? pretty-print/write 'pretty-print) pretty-print write))
|
|
|
|
(files (option-ref options '() '("a.c")))
|
|
|
|
(input-file-name (car files))
|
|
|
|
(ast-file-name (cond ((and (option-ref options 'preprocess #f)
|
|
|
|
(option-ref options 'output #f)))
|
|
|
|
(else (replace-suffix input-file-name ".E"))))
|
2018-08-15 17:11:54 +00:00
|
|
|
(dir (dirname input-file-name))
|
|
|
|
(defines (reverse (filter-map (multi-opt 'define) options)))
|
|
|
|
(includes (reverse (filter-map (multi-opt 'include) options)))
|
|
|
|
(includes (cons dir includes))
|
2018-05-25 06:05:02 +00:00
|
|
|
(prefix (option-ref options 'prefix "")))
|
|
|
|
(with-output-to-file ast-file-name
|
|
|
|
(lambda _ (for-each (cut c->ast prefix defines includes write <>) files)))))
|
|
|
|
|
|
|
|
(define (c->ast prefix defines includes write file-name)
|
|
|
|
(with-input-from-file file-name
|
|
|
|
(cut write (c99-input->ast #:prefix prefix #:defines defines #:includes includes))))
|
|
|
|
|
|
|
|
(define (mescc:compile options)
|
|
|
|
(let* ((files (option-ref options '() '("a.c")))
|
|
|
|
(input-file-name (car files))
|
|
|
|
(M1-file-name (cond ((and (option-ref options 'compile #f)
|
|
|
|
(option-ref options 'output #f)))
|
|
|
|
(else (replace-suffix input-file-name ".S"))))
|
|
|
|
(infos (map (cut file->info options <>) files))
|
|
|
|
(verbose? (option-ref options 'verbose #f)))
|
|
|
|
(when verbose?
|
|
|
|
(stderr "dumping: ~a\n" M1-file-name))
|
|
|
|
(with-output-to-file M1-file-name
|
|
|
|
(cut infos->M1 M1-file-name infos))
|
|
|
|
M1-file-name))
|
|
|
|
|
|
|
|
(define (file->info options file-name)
|
|
|
|
(cond ((.c? file-name) (c->info options file-name))
|
|
|
|
((.E? file-name) (E->info options file-name))))
|
|
|
|
|
|
|
|
(define (c->info options file-name)
|
2018-08-15 17:11:54 +00:00
|
|
|
(let* ((defines (reverse (filter-map (multi-opt 'define) options)))
|
|
|
|
(includes (reverse (filter-map (multi-opt 'include) options)))
|
|
|
|
(dir (dirname file-name))
|
|
|
|
(includes (cons dir includes))
|
|
|
|
(prefix (option-ref options 'prefix "")))
|
2018-05-25 06:05:02 +00:00
|
|
|
(with-input-from-file file-name
|
2018-08-14 10:35:24 +00:00
|
|
|
(cut c99-input->info %info #:prefix prefix #:defines defines #:includes includes))))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (E->info options file-name)
|
|
|
|
(let ((ast (with-input-from-file file-name read)))
|
2018-08-14 10:35:24 +00:00
|
|
|
(c99-ast->info %info ast)))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (mescc:assemble options)
|
|
|
|
(let* ((files (option-ref options '() '("a.c")))
|
|
|
|
(input-file-name (car files))
|
|
|
|
(hex2-file-name (cond ((and (option-ref options 'assemble #f)
|
|
|
|
(option-ref options 'output #f)))
|
|
|
|
(else (replace-suffix input-file-name ".o"))))
|
|
|
|
(S-files (filter .S? files))
|
|
|
|
(hex2-files M1->hex2 ) ;; FIXME
|
|
|
|
(source-files (filter (disjoin .c? .E?) files))
|
|
|
|
(infos (map (cut file->info options <>) source-files)))
|
|
|
|
(if (and (pair? S-files) (pair? infos))
|
|
|
|
(error "mixing source and object not supported:" source-files S-files))
|
|
|
|
(when (pair? S-files)
|
|
|
|
(M1->hex2 options S-files))
|
|
|
|
(when (pair? infos)
|
|
|
|
(infos->hex2 options hex2-file-name infos))
|
|
|
|
hex2-file-name))
|
|
|
|
|
|
|
|
(define (mescc:link options)
|
|
|
|
(let* ((files (option-ref options '() '("a.c")))
|
|
|
|
(source-files (filter (disjoin .c? .E?) files))
|
|
|
|
(S-files (filter .S? files))
|
|
|
|
(o-files (filter .o? files))
|
|
|
|
(input-file-name (car files))
|
|
|
|
(hex2-file-name (if (or (string-suffix? ".hex2" input-file-name)
|
|
|
|
(string-suffix? ".o" input-file-name)) input-file-name
|
|
|
|
(replace-suffix input-file-name ".o")))
|
|
|
|
(infos (map (cut file->info options <>) source-files))
|
|
|
|
(S-files (filter .S? files))
|
|
|
|
(hex2-files (filter .o? files))
|
|
|
|
(hex2-files (if (null? S-files) hex2-files
|
|
|
|
(append hex2-files (list (M1->hex2 options S-files)))))
|
|
|
|
(hex2-files (if (null? infos) hex2-files
|
|
|
|
(append hex2-files
|
|
|
|
(list (infos->hex2 options hex2-file-name infos)))))
|
|
|
|
(libraries (filter-map (multi-opt 'library) options))
|
|
|
|
(libraries (if (pair? libraries) libraries '("c")))
|
2018-08-15 15:46:03 +00:00
|
|
|
(libraries (if (equal? libraries '("none")) '() libraries))
|
2018-05-29 18:07:46 +00:00
|
|
|
(hex2-libraries (map (cut find-library options ".o" <>) libraries))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2-files (append hex2-files hex2-libraries))
|
2018-05-29 18:07:46 +00:00
|
|
|
(S-files (append S-files (map (cut find-library options ".S" <>) libraries)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(debug-info? (option-ref options 'debug-info #f))
|
|
|
|
(S-files (cons (replace-suffix input-file-name ".S") S-files))
|
|
|
|
(elf-footer (and debug-info?
|
|
|
|
(or (M1->blood-elf options S-files)
|
|
|
|
(exit 1)))))
|
|
|
|
(or (hex2->elf options hex2-files #:elf-footer elf-footer)
|
|
|
|
(exit 1))))
|
|
|
|
|
|
|
|
(define (infos->hex2 options hex2-file-name infos)
|
|
|
|
(let* ((input-file-name (car (option-ref options '() '("a.c"))))
|
|
|
|
(M1-file-name (replace-suffix hex2-file-name ".S"))
|
|
|
|
(options (acons 'compile #t options)) ; ugh
|
|
|
|
(options (acons 'output hex2-file-name options))
|
|
|
|
(verbose? (option-ref options 'verbose #f)))
|
|
|
|
(when verbose?
|
|
|
|
(stderr "dumping: ~a\n" M1-file-name))
|
|
|
|
(with-output-to-file M1-file-name
|
|
|
|
(cut infos->M1 M1-file-name infos))
|
|
|
|
(or (M1->hex2 options (list M1-file-name))
|
|
|
|
(exit 1))))
|
|
|
|
|
|
|
|
(define (M1->hex2 options M1-files)
|
|
|
|
(let* ((input-file-name (car (option-ref options '() '("a.c"))))
|
|
|
|
(M1-file-name (car M1-files))
|
|
|
|
(hex2-file-name (cond ((and (option-ref options 'assemble #f)
|
|
|
|
(option-ref options 'output #f)))
|
|
|
|
((option-ref options 'assemble #f)
|
|
|
|
(replace-suffix input-file-name ".o"))
|
|
|
|
(else (replace-suffix M1-file-name ".o"))))
|
|
|
|
(verbose? (option-ref options 'verbose #f))
|
|
|
|
(M1 (or (getenv "M1") "M1"))
|
|
|
|
(command `(,M1
|
|
|
|
"--LittleEndian"
|
2018-06-16 04:38:05 +00:00
|
|
|
"--Architecture" "1"
|
2018-05-29 18:07:46 +00:00
|
|
|
"-f" ,(arch-find options "x86.M1")
|
2018-05-25 06:05:02 +00:00
|
|
|
,@(append-map (cut list "-f" <>) M1-files)
|
|
|
|
"-o" ,hex2-file-name)))
|
|
|
|
(when verbose?
|
|
|
|
(stderr "~a\n" (string-join command)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(and (zero? (apply assert-system* command))
|
2018-05-25 06:05:02 +00:00
|
|
|
hex2-file-name)))
|
|
|
|
|
|
|
|
(define* (hex2->elf options hex2-files #:key elf-footer)
|
|
|
|
(let* ((input-file-name (car (option-ref options '() '("a.c"))))
|
|
|
|
(elf-file-name (cond ((option-ref options 'output #f))
|
|
|
|
(else (replace-suffix input-file-name ""))))
|
|
|
|
(verbose? (option-ref options 'verbose #f))
|
2018-05-29 18:07:46 +00:00
|
|
|
(elf-footer (or elf-footer (arch-find options "elf32-footer-single-main.hex2")))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2 (or (getenv "HEX2") "hex2"))
|
|
|
|
(command `(,hex2
|
|
|
|
"--LittleEndian"
|
2018-06-16 04:38:05 +00:00
|
|
|
"--Architecture" "1"
|
|
|
|
"--BaseAddress" "0x1000000"
|
2018-05-29 18:07:46 +00:00
|
|
|
"-f" ,(arch-find options "elf32-header.hex2")
|
|
|
|
"-f" ,(arch-find options "crt1.o")
|
2018-05-25 06:05:02 +00:00
|
|
|
,@(append-map (cut list "-f" <>) hex2-files)
|
|
|
|
"-f" ,elf-footer
|
|
|
|
"--exec_enable"
|
|
|
|
"-o" ,elf-file-name)))
|
|
|
|
(when verbose?
|
2018-05-29 18:07:46 +00:00
|
|
|
(stderr "~a\n" (string-join command)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(and (zero? (apply assert-system* command))
|
2018-05-25 06:05:02 +00:00
|
|
|
elf-file-name)))
|
|
|
|
|
|
|
|
(define (M1->blood-elf options M1-files)
|
|
|
|
(let* ((M1-file-name (car M1-files))
|
|
|
|
(M1-blood-elf-footer (string-append M1-file-name ".blood-elf"))
|
|
|
|
(hex2-file-name (replace-suffix M1-file-name ".o"))
|
|
|
|
(blood-elf-footer (string-append hex2-file-name ".blood-elf"))
|
|
|
|
(verbose? (option-ref options 'verbose #f))
|
|
|
|
(blood-elf (or (getenv "BLOOD_ELF") "blood-elf"))
|
|
|
|
(command `(,blood-elf
|
2018-05-29 18:07:46 +00:00
|
|
|
"-f" ,(arch-find options "x86.M1")
|
2018-05-25 06:05:02 +00:00
|
|
|
,@(append-map (cut list "-f" <>) M1-files)
|
|
|
|
"-o" ,M1-blood-elf-footer)))
|
|
|
|
(when verbose?
|
|
|
|
(format (current-error-port) "~a\n" (string-join command)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(and (zero? (apply assert-system* command))
|
2018-05-25 06:05:02 +00:00
|
|
|
(let* ((options (acons 'compile #t options)) ; ugh
|
|
|
|
(options (acons 'output blood-elf-footer options)))
|
|
|
|
(M1->hex2 options (list M1-blood-elf-footer))))))
|
|
|
|
|
|
|
|
(define (replace-suffix file-name suffix)
|
|
|
|
(let* ((parts (string-split file-name #\.))
|
|
|
|
(base (if (pair? (cdr parts)) (drop-right parts 1))))
|
|
|
|
(string-append (string-join base ".") suffix)))
|
|
|
|
|
2018-05-29 18:07:46 +00:00
|
|
|
(define (find-library options ext o)
|
|
|
|
(arch-find options (string-append "lib" o ext)))
|
|
|
|
|
|
|
|
(define* (arch-find options file-name)
|
2018-07-25 06:14:13 +00:00
|
|
|
(let* ((srcdest (or (getenv "srcdest") ""))
|
|
|
|
(srcdir-lib (string-append srcdest "lib"))
|
|
|
|
(path (cons* srcdir-lib
|
2018-07-21 22:43:39 +00:00
|
|
|
(prefix-file options "lib")
|
|
|
|
(filter-map (multi-opt 'library-dir) options)))
|
2018-05-29 18:07:46 +00:00
|
|
|
(arch-file-name (string-append "x86-mes/" file-name))
|
|
|
|
(verbose? (option-ref options 'verbose #f)))
|
|
|
|
(when verbose?
|
|
|
|
(stderr "arch-find=~s\n" arch-file-name)
|
|
|
|
(stderr " path=~s\n" path)
|
|
|
|
(stderr " => ~s\n" (search-path path arch-file-name)))
|
|
|
|
(search-path path arch-file-name)))
|
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
(define (prefix-file options file-name)
|
|
|
|
(let ((prefix (option-ref options 'prefix "")))
|
|
|
|
(define (prefix-file o)
|
|
|
|
(if (string-null? prefix) o (string-append prefix "/" o)))
|
|
|
|
(prefix-file file-name)))
|
|
|
|
|
2018-07-07 15:31:06 +00:00
|
|
|
(define (assert-system* . args)
|
|
|
|
(let ((status (apply system* args)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(when (not (zero? status))
|
2018-07-07 15:31:06 +00:00
|
|
|
(stderr "mescc: failed: ~a\n" (string-join args))
|
2018-07-09 07:25:38 +00:00
|
|
|
(exit status))
|
|
|
|
status))
|
2018-05-29 18:07:46 +00:00
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
(define (multi-opt option-name) (lambda (o) (and (eq? (car o) option-name) (cdr o))))
|
|
|
|
|
|
|
|
(define (.c? o) (or (string-suffix? ".c" o)
|
|
|
|
(string-suffix? ".M2" o)))
|
|
|
|
(define (.E? o) (string-suffix? ".E" o))
|
|
|
|
(define (.S? o) (or (string-suffix? ".S" o)
|
|
|
|
(string-suffix? ".mes-S" o)
|
|
|
|
(string-suffix? "S" o)
|
|
|
|
(string-suffix? ".M1" o)))
|
|
|
|
(define (.o? o) (or (string-suffix? ".o" o)
|
|
|
|
(string-suffix? ".mes-o" o)
|
|
|
|
(string-suffix? "o" o)
|
|
|
|
(string-suffix? ".hex2" o)))
|