M1.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. ;;; M1.scm produces stage0' M1 assembly format
  20. ;;; Code:
  21. (define-module (mescc M1)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-26)
  24. #:use-module (system base pmatch)
  25. #:use-module (mes misc)
  26. #:use-module (mes guile)
  27. #:use-module (mescc as)
  28. #:use-module (mescc info)
  29. #:export (info->M1
  30. infos->M1
  31. M1:merge-infos))
  32. (define* (infos->M1 file-name infos #:key align verbose?)
  33. (let ((info (fold M1:merge-infos (make <info>) infos)))
  34. (info->M1 file-name info #:align align #:verbose? verbose?)))
  35. (define (M1:merge-infos o info)
  36. (clone info
  37. #:functions (alist-add (.functions info) (.functions o))
  38. #:globals (alist-add (.globals info) (.globals o))
  39. #:types (.types o)))
  40. (define (alist-add a b)
  41. (let* ((b-keys (map car b))
  42. (a (filter (lambda (f) (or (cdr f) (not (member (car f) b-keys)))) a))
  43. (a-keys (map car a)))
  44. (append a (filter (lambda (e) (not (member (car e) a-keys))) b))))
  45. (define (hex2:address o)
  46. (string-append "&" o))
  47. (define (hex2:address8 o)
  48. (string-append "&" o " %0")) ;; FIXME: 64bit
  49. (define (hex2:offset o)
  50. (string-append "%" o))
  51. (define (hex2:offset1 o)
  52. (string-append "!" o))
  53. (define (hex2:offset2 o)
  54. (string-append "@" o))
  55. (define (hex2:offset3 o)
  56. "Note: Uses architecture-specific printer (for branch instructions)"
  57. (string-append "^~" o))
  58. (define hex? #t)
  59. (define (hex2:immediate o)
  60. (if hex? (string-append "%0x" (dec->hex o))
  61. (string-append "%" (number->string o))))
  62. (define (hex2:immediate1 o)
  63. (if hex? (string-append "!0x" (dec->hex o))
  64. (string-append "!" (number->string o))))
  65. (define (hex2:immediate2 o)
  66. (if hex? (string-append "@0x" (dec->hex o))
  67. (string-append "@" (number->string o))))
  68. (define (hex2:immediate4 o)
  69. (if hex? (string-append "%0x" (dec->hex o))
  70. (string-append "%" (number->string o))))
  71. (define mesc? (string=? %compiler "mesc"))
  72. (define (hex2:immediate8 o)
  73. ;; FIXME: #x100000000 => 0 divide-by-zero when compiled with 64 bit mesc
  74. (if hex? (string-append "%0x" (dec->hex (if mesc? 0 (modulo o #x100000000)))
  75. " %0x" (if (< o 0) "-1"
  76. (dec->hex (if mesc? o (quotient o #x100000000)))))
  77. (string-append "%" (number->string (dec->hex (if mesc? 0 (modulo o #x100000000))))
  78. " %" (if (< o 0) "-1"
  79. (number->string (dec->hex (if mesc? o (quotient o #x100000000))))))))
  80. (define* (display-join o #:optional (sep ""))
  81. (let loop ((o o))
  82. (when (pair? o)
  83. (display (car o))
  84. (if (pair? (cdr o))
  85. (display sep))
  86. (loop (cdr o)))))
  87. (define (global-string? o)
  88. (and (pair? o) (pair? (car o)) (eq? (caar o) #:string)))
  89. (define (global-extern? o)
  90. (and=> (global:storage o) (cut eq? <> 'extern)))
  91. (define* (info->M1 file-name o #:key align verbose?)
  92. (let* ((functions (.functions o))
  93. (function-names (map car functions))
  94. (globals (.globals o))
  95. (globals (filter (negate (compose global-extern? cdr)) globals))
  96. (strings (filter global-string? globals))
  97. (strings (map car strings))
  98. (reg-size (type:size (assoc-ref (.types o) "*")))
  99. (align-functions? (memq 'functions align))
  100. (align-globals? (memq 'globals align)))
  101. (define (string->label o)
  102. (let ((index (list-index (lambda (s) (equal? s o)) strings)))
  103. (if index
  104. (string-append "_string_" file-name "_" (number->string index))
  105. (if (equal? o "%0") o ; FIXME: 64b
  106. (error "no such string:" o)))))
  107. (define (text->M1 o)
  108. ;;
  109. (cond
  110. ((char? o) (text->M1 (char->integer o)))
  111. ((string? o) o)
  112. ((symbol? o) (symbol->string o))
  113. ((number? o) (let ((o (if (< o #x80) o (- o #x100))))
  114. (if hex? (string-append "!0x"
  115. (if (and (>= o 0) (< o 16)) "0" "")
  116. (number->string o 16))
  117. (string-append "!" (number->string o)))))
  118. ((and (pair? o) (keyword? (car o)))
  119. (pmatch o
  120. ;; FIXME
  121. ((#:address (#:string ,string))
  122. (hex2:address (string->label `(#:string ,string))))
  123. ((#:address (#:address ,address)) (guard (string? address))
  124. (hex2:address address))
  125. ((#:address (#:address ,global)) (guard (global? global))
  126. (hex2:address (global->string global)))
  127. ((#:address ,function) (guard (function? function))
  128. (hex2:address (function->string function)))
  129. ((#:address ,number) (guard (number? number))
  130. (string-join (map text->M1 (int->bv32 number))))
  131. ((#:address8 (#:string ,string))
  132. (hex2:address8 (string->label `(#:string ,string))))
  133. ((#:address8 (#:address ,address)) (guard (string? address))
  134. (hex2:address8 address))
  135. ((#:address8 (#:address ,global)) (guard (global? global))
  136. (hex2:address8 (global->string global)))
  137. ((#:address8 ,function) (guard (function? function))
  138. (hex2:address8 (function->string function)))
  139. ((#:address8 ,number) (guard (number? number))
  140. (string-join (map text->M1 (int->bv64 number))))
  141. ((#:string ,string)
  142. (hex2:address (string->label o)))
  143. ((#:address ,address) (guard (string? address))
  144. (hex2:address address))
  145. ((#:address ,global) (guard (global? global))
  146. (hex2:address (global->string global)))
  147. ((#:address8 ,address) (guard (string? address))
  148. (hex2:address8 address))
  149. ((#:address8 ,global) (guard (global? global))
  150. (hex2:address8 (global->string global)))
  151. ((#:offset ,offset) (hex2:offset offset))
  152. ((#:offset1 ,offset1) (hex2:offset1 offset1))
  153. ((#:offset2 ,offset2) (hex2:offset2 offset2))
  154. ((#:offset3 ,offset3) (hex2:offset3 offset3))
  155. ((#:immediate ,immediate) (hex2:immediate immediate))
  156. ((#:immediate1 ,immediate1) (hex2:immediate1 immediate1))
  157. ((#:immediate2 ,immediate2) (hex2:immediate2 immediate2))
  158. ((#:immediate4 ,immediate4) (hex2:immediate4 immediate4))
  159. ((#:immediate8 ,immediate8) (hex2:immediate8 immediate8))
  160. (_ (error "text->M1 no match o" o))))
  161. ((pair? o) (string-join (map text->M1 o)))
  162. (#t (error "no such text:" o))))
  163. (define (write-function o)
  164. (let ((name (car o))
  165. (text (function:text (cdr o))))
  166. (define (line->M1 o)
  167. (cond ((eq? (car o) #:label)
  168. (display (string-append ":" (cadr o))))
  169. ((eq? (car o) #:comment)
  170. (display "\t\t\t\t\t# ")
  171. (display (text->M1 (cadr o))))
  172. ((or (string? (car o)) (symbol? (car o)))
  173. (display "\t" )
  174. (display-join (map text->M1 o) " "))
  175. ((or (string? (car (reverse o))) (symbol? (car (reverse o))))
  176. (display "\t" )
  177. (display-join (map text->M1 o) " "))
  178. (else (error "line->M1 invalid line:" o)))
  179. (newline))
  180. (when verbose?
  181. (display (string-append " :" name "\n") (current-error-port)))
  182. (display "\n\n")
  183. (when align-functions?
  184. ;; "<" aligns to multiple of 4 Bytes.
  185. (display "<\n"))
  186. (display (string-append ":" name "\n"))
  187. (for-each line->M1 (apply append text))))
  188. (define (write-global o)
  189. (define (labelize o)
  190. (if (not (string? o)) o
  191. (let* ((label o)
  192. (function? (member label function-names))
  193. (string-label (string->label label))
  194. (string? (not (equal? string-label "_string_#f"))))
  195. (cond ((and (pair? o) (global? (cdr o))) (string-append "&" (global->string o)))
  196. ((and (not string?) (not function?)) (format (current-error-port) "warning: unresolved label: ~s\n" label))
  197. ((equal? string-label "%0") o) ;; FIXME: 64b
  198. (else (string-append "&" label))))))
  199. (define (display-align size)
  200. (let ((alignment (- reg-size (modulo size reg-size))))
  201. (when (and align-globals? (> reg-size alignment 0))
  202. (display " ")
  203. (display-join (map text->M1 (map (const 0) (iota alignment))) " "))
  204. #t))
  205. (let* ((label (cond
  206. ((and (pair? (car o)) (eq? (caar o) #:string))
  207. (string->label (car o)))
  208. ((global? (cdr o)) (global->string (cdr o)))
  209. (else (car o))))
  210. (string? (string-prefix? "_string" label))
  211. (foo (when (and verbose? (not (eq? (car (string->list label)) #\_)))
  212. (display (string-append " :" label "\n") (current-error-port))))
  213. (data ((compose global:value cdr) o))
  214. (data (filter-map labelize data))
  215. (len (length data))
  216. (string-max (or (and=> (getenv "M1_STRING_MAX") string->number) 256))
  217. (string-data (and string? (list-head data (1- (length data))))))
  218. (display (string-append "\n:" label "\n"))
  219. (if (and string-data
  220. (< len string-max)
  221. (char? (car data))
  222. (eq? (last data) #\nul)
  223. (not (find (cut memq <> '(#\")) string-data))
  224. (not (any (lambda (ch)
  225. (or (and (not (memq ch '(#\tab #\newline)))
  226. (< (char->integer ch) #x20))
  227. (>= (char->integer ch) #x80))) string-data)))
  228. (let ((text string-data))
  229. (display (string-append "\"" (list->string string-data) "\""))
  230. (display-align (1+ (length string-data))))
  231. (let ((text (map text->M1 data)))
  232. (display-join text " ")
  233. (display-align (length text))))
  234. (newline)))
  235. (when verbose?
  236. (display "M1: functions\n" (current-error-port)))
  237. (for-each write-function (filter cdr functions))
  238. (when (assoc-ref functions "main")
  239. (display "\n\n:ELF_data\n") ;; FIXME
  240. (display "\n\n:HEX2_data\n"))
  241. (when verbose?
  242. (display "M1: globals\n" (current-error-port)))
  243. (for-each write-global (filter global-string? globals))
  244. (for-each write-global (filter (negate global-string?) globals))))