mescc.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (mescc)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (ice-9 getopt-long)
  21. #:use-module (mes misc)
  22. #:use-module (mescc mescc)
  23. #:export (mescc:main))
  24. (cond-expand
  25. (mes
  26. (define (set-port-encoding! port encoding) #t)
  27. (mes-use-module (mes guile))
  28. (mes-use-module (mes misc))
  29. (mes-use-module (mes getopt-long))
  30. (mes-use-module (mes display))
  31. (mes-use-module (mescc mescc))
  32. )
  33. (guile
  34. (define-macro (mes-use-module . rest) #t)))
  35. (define %prefix (getenv "%prefix"))
  36. (define %version (getenv "%version"))
  37. (when (and=> (getenv "V") (lambda (v) (> (string->number v) 1)))
  38. (format (current-error-port) "mescc[~a]...\n" %scheme))
  39. (define (unclump-single o)
  40. (cond ((string-prefix? "--" o) (list o))
  41. ((and (string-prefix? "-" o)
  42. (> (string-length o) 2)
  43. (not (eq? (string-ref o 2) #\space)))
  44. (list (substring o 0 2)
  45. (substring o 2)))
  46. (else (list o))))
  47. (define (parse-opts args)
  48. (let* ((option-spec
  49. '((align)
  50. (assemble (single-char #\c))
  51. (base-address (value #t))
  52. (compile (single-char #\S))
  53. (define (single-char #\D) (value #t))
  54. (debug-info (single-char #\g))
  55. (dumpmachine)
  56. (help (single-char #\h))
  57. (include (single-char #\I) (value #t))
  58. (library-dir (single-char #\L) (value #t))
  59. (library (single-char #\l) (value #t))
  60. (machine (single-char #\m) (value #t))
  61. (nodefaultlibs)
  62. (nostartfiles)
  63. (nostdlib)
  64. (preprocess (single-char #\E))
  65. (std (value #t))
  66. (output (single-char #\o) (value #t))
  67. (optimize (single-char #\O) (value #t))
  68. (version (single-char #\V))
  69. (verbose (single-char #\v))
  70. (write (single-char #\w) (value #t))
  71. (language (single-char #\x) (value #t))))
  72. (options (getopt-long args option-spec))
  73. (help? (option-ref options 'help #f))
  74. (files (option-ref options '() '()))
  75. (usage? (and (not help?) (null? files)))
  76. (version? (option-ref options 'version #f)))
  77. (cond ((option-ref options 'dumpmachine #f)
  78. (display "x86-mes")
  79. (exit 0))
  80. (version? (format #t "mescc (GNU Mes) ~a\n" %version) (exit 0))
  81. (else
  82. (and (or help? usage?)
  83. (format (or (and usage? (current-error-port)) (current-output-port)) "\
  84. Usage: mescc [OPTION]... FILE...
  85. --align align globals
  86. -dumpmachine display the compiler's target processor
  87. --base-address=ADRRESS
  88. use BaseAddress ADDRESS [0x1000000]
  89. -D DEFINE[=VALUE] define DEFINE [VALUE=1]
  90. -E preprocess only; do not compile, assemble or link
  91. -g add debug info [GDB, objdump] TODO: hex2 footer
  92. -h, --help display this help and exit
  93. -I DIR append DIR to include path
  94. -L DIR append DIR to library path
  95. -l LIBNAME link with LIBNAME
  96. -m BITS compile for BITS bits [32]
  97. -nodefaultlibs do not use libc.o when linking
  98. -nostartfiles do not use crt1.o when linking
  99. -nostdlib do not use crt1.o or libc.o when linking
  100. -o FILE write output to FILE
  101. -O LEVEL use optimizing LEVEL
  102. -S preprocess and compile only; do not assemble or link
  103. --std=STANDARD assume that the input sources are for STANDARD
  104. -v, --version display version and exit
  105. -w,--write=TYPE dump Nyacc AST using TYPE {pretty-print,write}
  106. -x LANGUAGE specify LANGUAGE of the following input files
  107. Environment variables:
  108. MES=BINARY run on mes-executable BINARY {mes,guile}
  109. MES_DEBUG=LEVEL show debug output with verbosity LEVEL {0..5}
  110. NYACC_TRACE=1 show Nyacc progress
  111. Report bugs to: bug-mes@gnu.org
  112. GNU Mes home page: <http://gnu.org/software/mes/>
  113. General help using GNU software: <http://gnu.org/gethelp/>
  114. ")
  115. (exit (or (and usage? 2) 0)))
  116. options))))
  117. (define (mescc:main args)
  118. (let* ((single-dash-options '("-dumpmachine"
  119. "-nodefaultlibs"
  120. "-nostartfiles"
  121. "-nostdlib"
  122. "-std"))
  123. (args (map (lambda (o)
  124. (if (member o single-dash-options) (string-append "-" o)
  125. o))
  126. args))
  127. (args (append-map unclump-single args))
  128. (options (parse-opts args))
  129. (options (acons 'prefix %prefix options))
  130. (preprocess? (option-ref options 'preprocess #f))
  131. (compile? (option-ref options 'compile #f))
  132. (assemble? (option-ref options 'assemble #f))
  133. (verbose? (option-ref options 'verbose (getenv "MES_DEBUG"))))
  134. (when verbose?
  135. (setenv "NYACC_TRACE" "yes")
  136. (format (current-error-port) "options=~s\n" options))
  137. (cond (preprocess? (mescc:preprocess options))
  138. (compile? (mescc:compile options))
  139. (assemble? (mescc:assemble options))
  140. (else (mescc:link options)))))