preprocess.scm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2017,2018,2020 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. ;;; Commentary:
  19. ;;; Code:
  20. (define-module (mescc preprocess)
  21. #:use-module (ice-9 optargs)
  22. #:use-module (system base pmatch)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (nyacc lang c99 parser)
  26. #:use-module (nyacc lang c99 parser)
  27. #:use-module (nyacc version)
  28. #:use-module (mes guile)
  29. #:export (c99-input->ast))
  30. (define mes-or-reproducible? #t)
  31. (when (getenv "MESC_DEBUG")
  32. (format (current-error-port) "*nyacc-version*=~a\n" *nyacc-version*))
  33. ;; list of which rules you want progress reported
  34. (define need-progress
  35. (or (assoc-ref
  36. '(("0.85.3" (1 2 3))
  37. ("0.86.0" (1 2 3)))
  38. *nyacc-version*)
  39. '((1 2 3))))
  40. (define (progress o)
  41. (when (and o (getenv "NYACC_DEBUG"))
  42. (display " :" (current-error-port))
  43. (display o (current-error-port))
  44. (display "\n" (current-error-port))))
  45. (define (insert-progress-monitors act-v len-v)
  46. (let ((n (vector-length act-v)))
  47. (let loop ((ix 0))
  48. (when (< ix n)
  49. (if (memq ix need-progress)
  50. (vector-set!
  51. act-v ix
  52. (lambda args
  53. (progress (list-ref args (1- (vector-ref len-v ix))))
  54. (apply (vector-ref act-v ix) args))))
  55. (loop (1+ ix))))))
  56. (cond-expand
  57. (guile
  58. (insert-progress-monitors (@@ (nyacc lang c99 parser) c99-act-v)
  59. (@@ (nyacc lang c99 parser) c99-len-v)))
  60. (mes
  61. (insert-progress-monitors c99-act-v c99-len-v)))
  62. (define* (c99-input->full-ast #:key (prefix "") (defines '()) (includes '()) (arch "") verbose?)
  63. (let* ((sys-include (if (equal? prefix "") "include"
  64. (string-append prefix "/include")))
  65. (kernel "linux")
  66. (kernel-include (string-append sys-include "/" kernel "/" arch))
  67. (includes (append
  68. includes
  69. (cons* kernel-include
  70. sys-include
  71. (append (or (and=> (getenv "CPATH")
  72. (cut string-split <> #\:)) '())
  73. (or (and=> (getenv "C_INCLUDE_PATH")
  74. (cut string-split <> #\:)) '())))))
  75. (defines `(
  76. "NULL=0"
  77. "__linux__=1"
  78. "_POSIX_SOURCE=0"
  79. "SYSTEM_LIBC=0"
  80. "__STDC__=1"
  81. "__MESC__=1"
  82. ,(if mes-or-reproducible? "__MESC_MES__=1" "__MESC_MES__=0")
  83. ,@defines)))
  84. (when (and verbose? (> verbose? 1))
  85. (format (current-error-port) "includes: ~s\n" includes)
  86. (format (current-error-port) "defines: ~s\n" defines))
  87. (parse-c99
  88. #:inc-dirs includes
  89. #:cpp-defs defines
  90. #:mode 'code)))
  91. (define* (c99-input->ast #:key (prefix "") (defines '()) (includes '()) (arch "") verbose?)
  92. (when verbose?
  93. (format (current-error-port) "parsing: input\n"))
  94. ((compose ast-strip-attributes
  95. ast-strip-const
  96. ast-strip-comment)
  97. (c99-input->full-ast #:prefix prefix #:defines defines #:includes includes #:arch arch #:verbose? verbose?)))
  98. (define (ast-strip-comment o)
  99. (pmatch o
  100. ((@ (comment . ,comment)) #f) ; Nyacc 0.90.2/0.93.0?
  101. ((comment . ,comment) #f)
  102. (((comment . ,comment) . ,t) (filter-map ast-strip-comment t))
  103. (((comment . ,comment) . ,cdr) cdr)
  104. ((,car . (comment . ,comment)) car)
  105. ((,h . ,t) (if (list? o) (filter-map ast-strip-comment o)
  106. (cons (ast-strip-comment h) (ast-strip-comment t))))
  107. (_ o)))
  108. (define (ast-strip-const o)
  109. (pmatch o
  110. ((type-qual ,qual) (if (equal? qual "const") #f o))
  111. ((pointer (type-qual-list (type-qual ,qual)) . ,rest)
  112. (if (equal? qual "const") `(pointer ,@rest) o))
  113. ((decl-spec-list (type-qual ,qual))
  114. (if (equal? qual "const") #f
  115. `(decl-spec-list (type-qual ,qual))))
  116. ((decl-spec-list (type-qual ,qual) . ,rest)
  117. (if (equal? qual "const") `(decl-spec-list ,@rest)
  118. `(decl-spec-list (type-qual ,qual) ,@(map ast-strip-const rest))))
  119. ((decl-spec-list (type-qual-list (type-qual ,qual)) . ,rest)
  120. (if (equal? qual "const") `(decl-spec-list ,@rest)
  121. `(decl-spec-list (type-qual-list (type-qual ,qual)) ,@(map ast-strip-const rest))))
  122. ((,h . ,t) (if (list? o) (filter-map ast-strip-const o)
  123. (cons (ast-strip-const h) (ast-strip-const t))))
  124. (_ o)))
  125. (define (ast-strip-attributes o)
  126. (pmatch o
  127. ((decl-spec-list (@ (attributes . ,attributes)) . ,rest)
  128. `(decl-spec-list ,@rest))
  129. ((,h . ,t) (if (list? o) (filter-map ast-strip-attributes o)
  130. (cons (ast-strip-attributes h) (ast-strip-attributes t))))
  131. (_ o)))