info.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. ;;; Commentary:
  19. ;;; info.scm defines [Guile] record data types for MesCC
  20. ;;; Code:
  21. (define-module (mescc info)
  22. #:use-module (ice-9 optargs)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-9 gnu)
  25. #:use-module (srfi srfi-26)
  26. #:export (<info>
  27. make
  28. clone
  29. make-<info>
  30. info?
  31. .types
  32. .constants
  33. .functions
  34. .globals
  35. .locals
  36. .function
  37. .statics
  38. .text
  39. .post
  40. .break
  41. .continue
  42. .allocated
  43. .pushed
  44. .registers
  45. .instructions
  46. <type>
  47. make-type
  48. type?
  49. type:type
  50. type:size
  51. type:pointer
  52. type:description
  53. <c-array>
  54. make-c-array
  55. c-array?
  56. c-array:type
  57. c-array:count
  58. <pointer>
  59. make-pointer
  60. pointer?
  61. pointer:type
  62. pointer:rank
  63. <bit-field>
  64. make-bit-field
  65. bit-field?
  66. bit-field:type
  67. bit-field:bit
  68. bit-field:bits
  69. <var>
  70. var:name
  71. var:type
  72. var:pointer
  73. var:c-array
  74. <global>
  75. make-global
  76. global?
  77. global:name
  78. global:type
  79. global:pointer
  80. global:c-array
  81. global:var
  82. global:value
  83. global:storage
  84. global:function
  85. global->string
  86. <local>
  87. make-local
  88. local?
  89. local:type
  90. local:pointer
  91. local:c-array
  92. local:var
  93. local:id
  94. <function>
  95. make-function
  96. function?
  97. function:name
  98. function:type
  99. function:text
  100. function->string
  101. ->type
  102. ->rank
  103. rank--
  104. rank++
  105. rank+=
  106. structured-type?))
  107. (define-immutable-record-type <info>
  108. (make-<info> types constants functions globals locals statics function text post break continue allocated pushed registers instructions)
  109. info?
  110. (types .types)
  111. (constants .constants)
  112. (functions .functions)
  113. (globals .globals)
  114. (locals .locals)
  115. (statics .statics)
  116. (function .function)
  117. (text .text)
  118. (post .post)
  119. (break .break)
  120. (continue .continue)
  121. (allocated .allocated)
  122. (pushed .pushed)
  123. (registers .registers)
  124. (instructions .instructions))
  125. (define* (make o #:key (types '()) (constants '()) (functions '()) (globals '()) (locals '()) (statics '()) (function #f) (text '()) (post '()) (break '()) (continue '()) (allocated '()) (pushed 0) (registers '()) (instructions '()))
  126. (cond ((eq? o <info>)
  127. (make-<info> types constants functions globals locals statics function text post break continue allocated pushed registers instructions))))
  128. (define (clone o . rest)
  129. (cond ((info? o)
  130. (let ((types (.types o))
  131. (constants (.constants o))
  132. (functions (.functions o))
  133. (globals (.globals o))
  134. (locals (.locals o))
  135. (statics (.statics o))
  136. (function (.function o))
  137. (text (.text o))
  138. (post (.post o))
  139. (break (.break o))
  140. (continue (.continue o))
  141. (allocated (.allocated o))
  142. (pushed (.pushed o))
  143. (registers (.registers o))
  144. (instructions (.instructions o)))
  145. (let-keywords rest
  146. #f
  147. ((types types)
  148. (constants constants)
  149. (functions functions)
  150. (globals globals)
  151. (locals locals)
  152. (statics statics)
  153. (function function)
  154. (text text)
  155. (post post)
  156. (break break)
  157. (continue continue)
  158. (allocated allocated)
  159. (pushed pushed)
  160. (registers registers)
  161. (instructions instructions))
  162. (make <info> #:types types #:constants constants #:functions functions #:globals globals #:locals locals #:statics statics #:function function #:text text #:post post #:break break #:continue continue #:allocated allocated #:pushed pushed #:registers registers #:instructions instructions))))))
  163. ;; ("int" . ,(make-type 'builtin 4 #f 0 #f))
  164. ;; (make-type 'enum 4 0 fields)
  165. ;; (make-type 'struct (apply + (map field:size fields)) 0 fields)
  166. (define-immutable-record-type <type>
  167. (make-type type size description)
  168. type?
  169. (type type:type)
  170. (size type:size)
  171. (description type:description))
  172. (define-immutable-record-type <c-array>
  173. (make-c-array type count)
  174. c-array?
  175. (type c-array:type)
  176. (count c-array:count))
  177. (define-immutable-record-type <pointer>
  178. (make-pointer type rank)
  179. pointer?
  180. (type pointer:type)
  181. (rank pointer:rank))
  182. (define-immutable-record-type <bit-field>
  183. (make-bit-field type bit bits)
  184. bit-field?
  185. (type bit-field:type)
  186. (bit bit-field:bit)
  187. (bits bit-field:bits))
  188. (define-immutable-record-type <var>
  189. (make-var name type function id value)
  190. var?
  191. (name var:name)
  192. (type var:type) ; <type>
  193. (function var:function)
  194. (id var:id)
  195. (value var:value))
  196. (define-immutable-record-type <global>
  197. (make-global- name type var value storage function)
  198. global?
  199. (name global:name)
  200. (type global:type)
  201. (var global:var) ; <var>
  202. (value global:value)
  203. (storage global:storage)
  204. (function global:function))
  205. (define (make-global name type value storage function)
  206. (make-global- name type (make-var name type function #f value) value storage function))
  207. (define (global->string o)
  208. (or (and=> (global:function o) (cut string-append <> "-" (global:name o)))
  209. (global:name o)))
  210. (define-immutable-record-type <local>
  211. (make-local- type var id)
  212. local?
  213. (type local:type)
  214. (var local:var) ; <var>
  215. (id local:id))
  216. (define (make-local name type id)
  217. (make-local- type (make-var name type #f id #f) id))
  218. (define-immutable-record-type <function>
  219. (make-function name type text)
  220. function?
  221. (name function:name)
  222. (type function:type)
  223. (text function:text))
  224. (define (function->string o)
  225. (function:name o))
  226. (define (structured-type? o)
  227. (cond ((type? o) (memq (type:type o) '(struct union)))
  228. ((global? o) ((compose structured-type? global:type) o))
  229. ((local? o) ((compose structured-type? local:type) o))
  230. ((and (pair? o) (eq? (car o) 'tag))) ;; FIXME: enum?
  231. (else #f)))
  232. (define (->type o)
  233. (cond ((type? o) o)
  234. ((bit-field? o) o)
  235. ((pointer? o) ((compose ->type pointer:type) o))
  236. ((c-array? o) ((compose ->type c-array:type) o))
  237. ((and (pair? o) (eq? (car o) 'tag)) o)
  238. ;; FIXME
  239. (#t
  240. (format (current-error-port) "->type--: not a <type>: ~s\n" o)
  241. (make-type 'builtin 4 #f))
  242. (else (error "->type: not a <type>:" o))))
  243. (define (->rank o)
  244. (cond ((type? o) 0)
  245. ((pointer? o) (pointer:rank o))
  246. ((c-array? o) (1+ ((compose ->rank c-array:type) o)))
  247. ((local? o) ((compose ->rank local:type) o))
  248. ((global? o) ((compose ->rank global:type) o))
  249. ((bit-field? o) 0)
  250. ;; FIXME
  251. (#t
  252. (format (current-error-port) "->rank: not a type: ~s\n" o)
  253. 0)
  254. (else (error "->rank: not a <type>:" o))))
  255. (define (rank-- o)
  256. (cond ((and (pointer? o) (= (pointer:rank o) 1)) (pointer:type o))
  257. ((pointer? o) (set-field o (pointer:rank) (1- (pointer:rank o))))
  258. ((c-array? o) (c-array:type o))
  259. ;; FIXME
  260. (#t (format (current-error-port) "rank--: not a pointer: ~s\n" o)
  261. o)
  262. (else (error "rank--: not a pointer" o))))
  263. (define (rank+= o i)
  264. (cond ((pointer? o) (set-field o (pointer:rank) (+ i (pointer:rank o))))
  265. (else (make-pointer o i))))
  266. (define (rank++ o)
  267. (rank+= o 1))