mescc: support -E.

* guile/mescc.scm (parse-opts): Add -E.
  (source->ast): New function.
  (main): Use it.
* scripts/mescc.mes (parse-opts): Add -E.
  (source->ast): New function.
  (main): Use it.
This commit is contained in:
Jan Nieuwenhuizen 2017-05-22 19:22:18 +02:00
parent 3c880bbb56
commit cf51f3f5bd
4 changed files with 105 additions and 48 deletions

View file

@ -37,6 +37,7 @@ GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/mescc.scm
(define-module (mescc)
#:use-module (language c99 compiler)
#:use-module (ice-9 getopt-long)
#:use-module (ice-9 pretty-print)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:export (main))
@ -55,24 +56,26 @@ GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/mescc.scm
(define (parse-opts args)
(let* ((option-spec
'((c (single-char #\c))
(D (single-char #\D) (value #t))
(D (single-char #\D) (value #t))
(E (single-char #\E))
(help (single-char #\h))
(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)))
(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)))
(version? (option-ref options 'version #f)))
(or
(and version?
(format (current-output-port) "mescc.scm (mes) ~a\n" %version))
(format (current-output-port) "mescc.scm (mes) ~a\n" %version))
(and (or help? usage?)
(format (or (and usage? (current-error-port)) (current-output-port)) "\
Usage: mescc [OPTION]... FILE...
-c compile and assemble, but do not link
-D DEFINE define DEFINE
-E preprocess only; do not compile, assemble or link
-h, --help display this help and exit
-I DIR append DIR to include path
-o FILE write output to FILE
@ -81,47 +84,71 @@ Usage: mescc [OPTION]... FILE...
(exit (or (and usage? 2) 0)))
options)))
(define (object->info file)
(let* ((string (with-input-from-file file read-string))
(module (resolve-module '(language c99 compiler))))
(eval-string string module)))
(define (object->info file)
(let* ((lst (with-input-from-file file read))
(module (resolve-module '(language c99 compiler))))
(eval lst module)))
(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))))))
(define (source->info defines includes)
(lambda (file)
(with-input-from-file file
(lambda ()
((c99-input->info #:defines defines #:includes includes))))))
(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)))
(define (main args)
(let* ((options (parse-opts args))
(files (option-ref options '() '()))
(file (if (null? files) (string-append %docdir "examples/main.c")
(car files)))
(preprocess? (option-ref options 'E #f))
(compile? (option-ref options 'c #f))
(asts (filter ast? files))
(objects (filter object? files))
(sources (filter (cut string-suffix? ".c" <>) files))
(objects (filter (negate (cut string-suffix? ".c" <>)) files))
(base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
(out (option-ref options 'o (if compile? (string-append base ".o") "a.out")))
(out (option-ref options 'o (cond (compile? (string-append base ".o"))
(preprocess? (string-append base ".E"))
(else "a.out"))))
(multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
(defines (reverse (filter-map (multi-opt 'D) options)))
(includes (reverse (filter-map (multi-opt 'I) options))))
(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))
(format (current-error-port) "inputs: ~a\n" files)
(with-output-to-file out
(lambda ()
(set-port-encoding! (current-output-port) "ISO-8859-1")
(if (pair? objects) (let ((infos (map object->info objects)))
(if (and (not compile?)
(not preprocess?)) (set-port-encoding! (current-output-port) "ISO-8859-1"))
(cond ((pair? objects) (let ((infos (map object->info objects)))
(if compile? (infos->object infos)
(infos->elf infos))))
((pair? asts) (let ((infos (map main:ast->info asts)))
(if compile? (infos->object infos)
(infos->elf infos)))
(let ((infos (map (source->info defines includes) sources)))
(if compile? (infos->object infos)
(infos->elf infos))))))
(if (not compile?)
(infos->elf infos))))
((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
(let ((infos (map (source->info defines includes) sources)))
(if compile? (infos->object infos)
(infos->elf infos))))))))
(if (and (not compile?)
(not preprocess?))
(chmod out #o755))))

View file

@ -2200,6 +2200,9 @@
#:functions (map function:object->text (.functions o))
#:init (map init:object->text (.init o))))
(define* (c99-ast->info ast)
((ast->info (make <info> #:types i386:type-alist)) ast))
(define* (c99-input->elf #:key (defines '()) (includes '()))
((compose info->elf info:object->text (c99-input->info #:defines defines #:includes includes))))

View file

@ -31,7 +31,8 @@
#:use-module (mes elf-util)
#:use-module (mes as-i386)
#:use-module (nyacc lang c99 parser)
#:export (c99-input->ast
#:export (c99-ast->info
c99-input->ast
c99-input->elf
c99-input->info
c99-input->object

View file

@ -39,6 +39,7 @@ exit $r
(mes-use-module (mes guile))
(mes-use-module (mes getopt-long))
(mes-use-module (mes pretty-print))
(mes-use-module (language c99 compiler))
(mes-use-module (srfi srfi-1))
(mes-use-module (srfi srfi-26))
@ -54,24 +55,26 @@ exit $r
(define (parse-opts args)
(let* ((option-spec
'((c (single-char #\c))
(D (single-char #\D) (value #t))
(D (single-char #\D) (value #t))
(E (single-char #\E))
(help (single-char #\h))
(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)))
(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)))
(version? (option-ref options 'version #f)))
(or
(and version?
(format (current-output-port) "mescc.scm (mes) ~a\n" %version))
(format (current-output-port) "mescc.scm (mes) ~a\n" %version))
(and (or help? usage?)
(format (or (and usage? (current-error-port)) (current-output-port)) "\
Usage: mescc [OPTION]... FILE...
-c compile and assemble, but do not link
-D DEFINE define DEFINE
-E preprocess only; do not compile, assemble or link
-h, --help display this help and exit
-I DIR append DIR to include path
-o FILE write output to FILE
@ -80,47 +83,70 @@ Usage: mescc [OPTION]... FILE...
(exit (or (and usage? 2) 0)))
options)))
(define (object->info file)
(let* ((string (with-input-from-file file read-string))
(module (resolve-module '(language c99 compiler))))
(eval-string string module)))
(define (object->info file)
(let* ((lst (with-input-from-file file read)))
(primitive-eval lst)))
(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))))))
(define (source->info defines includes)
(lambda (file)
(with-input-from-file file
(lambda ()
((c99-input->info #:defines defines #:includes includes))))))
(define (ast? o)
(or (string-suffix? ".E" o)
(string-suffix? ".mes-E" o)))
(define (object? o)
(or (string-suffix? ".o" o)
(string-suffix? ".mes-o" o)))
(define (main args)
(let* ((args (cons* (car args) (cdr (member "--" args))))
(options (parse-opts args))
(files (option-ref options '() '()))
(file (if (null? files) (string-append %docdir "examples/main.c")
(car files)))
(preprocess? (option-ref options 'E #f))
(compile? (option-ref options 'c #f))
(asts (filter ast? files))
(objects (filter object? files))
(sources (filter (cut string-suffix? ".c" <>) files))
(objects (filter (negate (cut string-suffix? ".c" <>)) files))
(base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
(out (option-ref options 'o (if compile? (string-append base ".o") "a.out")))
(out (option-ref options 'o (cond (compile? (string-append base ".o"))
(preprocess? (string-append base ".E"))
(else "a.out"))))
(multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
(defines (reverse (filter-map (multi-opt 'D) options)))
(includes (reverse (filter-map (multi-opt 'I) options))))
(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))
(format (current-error-port) "inputs: ~a\n" files)
(with-output-to-port (open-output-file out (if compile? S_IRWXU))
(with-output-to-port (open-output-file out (if (and (not compile?)
(not preprocess?)) S_IRWXU))
(lambda ()
(if (pair? objects) (let ((infos (map object->info objects)))
(cond ((pair? objects) (let ((infos (map object->info objects)))
(if compile? (infos->object infos)
(infos->elf infos))))
((pair? asts) (let ((infos (map main:ast->info asts)))
(if compile? (infos->object infos)
(infos->elf infos)))
(let ((infos (map (source->info defines includes) sources)))
(if compile? (infos->object infos)
(infos->elf infos))))))))
(infos->elf infos))))
((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
(let ((infos (map (source->info defines includes) sources)))
(if compile? (infos->object infos)
(infos->elf infos))))))))))
(main (command-line))
()