types.scm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. ;;; WebAssembly data types
  2. ;;; Copyright (C) 2023 Igalia, S.L.
  3. ;;; Copyright (C) 2023 Christine Lemmer-Webber <christine@spritely.institute>
  4. ;;; Copyright (C) 2023 Robin Templeton <robin@spritely.institute>
  5. ;;; Copyright (C) 2023, 2024 David Thompson <dave@spritely.institute>
  6. ;;;
  7. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  8. ;;; you may not use this file except in compliance with the License.
  9. ;;; You may obtain a copy of the License at
  10. ;;;
  11. ;;; http://www.apache.org/licenses/LICENSE-2.0
  12. ;;;
  13. ;;; Unless required by applicable law or agreed to in writing, software
  14. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  15. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. ;;; See the License for the specific language governing permissions and
  17. ;;; limitations under the License.
  18. ;;; Commentary:
  19. ;;;
  20. ;;; This module defines all of the data types that form a WebAssembly
  21. ;;; module.
  22. ;;;
  23. ;;; Code:
  24. (define-module (wasm types)
  25. #:use-module (srfi srfi-9)
  26. #:use-module (srfi srfi-9 gnu)
  27. #:use-module (ice-9 match)
  28. #:export (<wasm>
  29. make-wasm wasm?
  30. wasm-id wasm-types wasm-imports wasm-funcs wasm-tables wasm-memories
  31. wasm-globals wasm-exports wasm-start wasm-elems wasm-datas
  32. wasm-tags wasm-strings wasm-custom
  33. <param>
  34. make-param param?
  35. param-id param-type
  36. <func-sig>
  37. make-func-sig func-sig?
  38. func-sig-params func-sig-results
  39. <type-use>
  40. make-type-use type-use?
  41. type-use-idx type-use-sig
  42. <ref-type>
  43. make-ref-type ref-type?
  44. ref-type-nullable? ref-type-heap-type
  45. <limits>
  46. make-limits limits?
  47. limits-min limits-max
  48. <table-type>
  49. make-table-type table-type?
  50. table-type-limits table-type-elem-type
  51. <mem-type>
  52. make-mem-type mem-type?
  53. mem-type-limits
  54. <global-type>
  55. make-global-type global-type?
  56. global-type-mutable? global-type-type
  57. <array-type>
  58. make-array-type array-type?
  59. array-type-mutable?
  60. array-type-type
  61. <field>
  62. make-field field?
  63. field-id field-mutable? field-type
  64. <struct-type>
  65. make-struct-type struct-type?
  66. struct-type-fields
  67. <sub-type>
  68. make-sub-type sub-type?
  69. sub-type-final? sub-type-supers sub-type-type
  70. <rec-group>
  71. make-rec-group rec-group?
  72. rec-group-types
  73. <type>
  74. make-type type?
  75. type-id type-val
  76. <import>
  77. make-import import?
  78. import-mod import-name import-kind import-id import-type
  79. <export>
  80. make-export export?
  81. export-name export-kind export-idx
  82. <mem-arg>
  83. make-mem-arg mem-arg?
  84. mem-arg-id mem-arg-offset mem-arg-align
  85. <elem>
  86. make-elem elem?
  87. elem-id elem-mode elem-table elem-type elem-offset elem-inits
  88. <data>
  89. make-data data?
  90. data-id data-mode data-mem data-offset data-init
  91. <tag>
  92. make-tag tag?
  93. tag-id tag-type
  94. <local>
  95. make-local local?
  96. local-id local-type
  97. <func>
  98. make-func func?
  99. func-id func-type func-locals func-body
  100. <table>
  101. make-table table?
  102. table-id table-type table-init
  103. <memory>
  104. make-memory memory?
  105. memory-id memory-type
  106. <global>
  107. make-global global?
  108. global-id global-type global-init
  109. <custom>
  110. make-custom custom?
  111. custom-name custom-bytes
  112. <names>
  113. make-names names?
  114. names-module names-func names-local names-label names-type
  115. names-table names-memory names-global names-elem names-data
  116. names-field names-tag
  117. find-type))
  118. (define-record-type <wasm>
  119. (make-wasm id types imports funcs tables memories
  120. globals exports start elems datas tags
  121. strings custom)
  122. wasm?
  123. (id wasm-id)
  124. (types wasm-types)
  125. (imports wasm-imports)
  126. (funcs wasm-funcs)
  127. (tables wasm-tables)
  128. (memories wasm-memories)
  129. (globals wasm-globals)
  130. (exports wasm-exports)
  131. (start wasm-start)
  132. (elems wasm-elems)
  133. (datas wasm-datas)
  134. (tags wasm-tags)
  135. (strings wasm-strings)
  136. (custom wasm-custom))
  137. (define (print-wasm wasm port)
  138. (format port "#<wasm ~a>" (object-address wasm)))
  139. (set-record-type-printer! <wasm> print-wasm)
  140. (define-record-type <param>
  141. (make-param id type)
  142. param?
  143. (id param-id)
  144. (type param-type))
  145. (define-record-type <func-sig>
  146. (make-func-sig params results)
  147. func-sig?
  148. (params func-sig-params)
  149. (results func-sig-results))
  150. (define-record-type <type-use>
  151. (make-type-use idx sig)
  152. type-use?
  153. (idx type-use-idx)
  154. (sig type-use-sig))
  155. (define-record-type <ref-type>
  156. (make-ref-type nullable? heap-type)
  157. ref-type?
  158. (nullable? ref-type-nullable?)
  159. (heap-type ref-type-heap-type))
  160. (define-record-type <limits>
  161. (make-limits min max)
  162. limits?
  163. (min limits-min)
  164. (max limits-max))
  165. (define-record-type <table-type>
  166. (make-table-type limits elem-type)
  167. table-type?
  168. (limits table-type-limits)
  169. (elem-type table-type-elem-type))
  170. (define-record-type <mem-type>
  171. (make-mem-type limits)
  172. mem-type?
  173. (limits mem-type-limits))
  174. (define-record-type <global-type>
  175. (make-global-type mutable? type)
  176. global-type?
  177. (mutable? global-type-mutable?)
  178. (type global-type-type))
  179. (define-record-type <array-type>
  180. (make-array-type mutable? type)
  181. array-type?
  182. (mutable? array-type-mutable?)
  183. (type array-type-type))
  184. (define-record-type <field>
  185. (make-field id mutable? type)
  186. field?
  187. (id field-id)
  188. (mutable? field-mutable?)
  189. (type field-type))
  190. (define-record-type <struct-type>
  191. (make-struct-type fields)
  192. struct-type?
  193. (fields struct-type-fields))
  194. (define-record-type <sub-type>
  195. (make-sub-type final? supers type)
  196. sub-type?
  197. (final? sub-type-final?)
  198. (supers sub-type-supers)
  199. (type sub-type-type))
  200. (define-record-type <rec-group>
  201. (make-rec-group types)
  202. rec-group?
  203. (types rec-group-types))
  204. (define-record-type <type>
  205. (make-type id val)
  206. type?
  207. (id type-id)
  208. (val type-val))
  209. (define-record-type <import>
  210. (make-import mod name kind id type)
  211. import?
  212. (mod import-mod)
  213. (name import-name)
  214. (kind import-kind)
  215. (id import-id)
  216. (type import-type))
  217. (define-record-type <export>
  218. (make-export name kind idx)
  219. export?
  220. (name export-name)
  221. (kind export-kind)
  222. (idx export-idx))
  223. (define-record-type <mem-arg>
  224. (make-mem-arg id offset align)
  225. mem-arg?
  226. (id mem-arg-id)
  227. (offset mem-arg-offset)
  228. (align mem-arg-align))
  229. (define-record-type <elem>
  230. (make-elem id mode table type offset inits)
  231. elem?
  232. (id elem-id)
  233. (mode elem-mode)
  234. (table elem-table)
  235. (type elem-type)
  236. (offset elem-offset)
  237. (inits elem-inits))
  238. (define-record-type <data>
  239. (make-data id mode mem offset init)
  240. data?
  241. (id data-id)
  242. (mode data-mode)
  243. (mem data-mem)
  244. (offset data-offset)
  245. (init data-init))
  246. (define-record-type <tag>
  247. (make-tag id type)
  248. tag?
  249. (id tag-id)
  250. (type tag-type))
  251. (define-record-type <local>
  252. (make-local id type)
  253. local?
  254. (id local-id)
  255. (type local-type))
  256. (define-record-type <func>
  257. (make-func id type locals body)
  258. func?
  259. (id func-id)
  260. (type func-type)
  261. (locals func-locals)
  262. (body func-body))
  263. (define-record-type <table>
  264. (make-table id type init)
  265. table?
  266. (id table-id)
  267. (type table-type)
  268. (init table-init))
  269. (define-record-type <memory>
  270. (make-memory id type)
  271. memory?
  272. (id memory-id)
  273. (type memory-type))
  274. (define-record-type <global>
  275. (make-global id type init)
  276. global?
  277. (id global-id)
  278. (type global-type)
  279. (init global-init))
  280. (define-record-type <names>
  281. (make-names module func local label type table
  282. memory global elem data field tag)
  283. names?
  284. (module names-module)
  285. (func names-func)
  286. (local names-local)
  287. (label names-label)
  288. (type names-type)
  289. (table names-table)
  290. (memory names-memory)
  291. (global names-global)
  292. (elem names-elem)
  293. (data names-data)
  294. (field names-field)
  295. (tag names-tag))
  296. (define-record-type <custom>
  297. (make-custom name bytes)
  298. custom?
  299. (name custom-name)
  300. (bytes custom-bytes))
  301. (define (find-type pred types)
  302. (let lp ((types types) (idx 0))
  303. (define (visit-base rec supers type-id type-idx type)
  304. (pred rec type-id type-idx supers type))
  305. (define (visit-sub rec type-id type-idx type)
  306. (match type
  307. (($ <sub-type> final? supers type)
  308. (visit-base rec supers type-id type-idx type))
  309. (_ (visit-base rec '() type-id type-idx type))))
  310. (match types
  311. (() #f)
  312. ((($ <rec-group> subtypes) . types)
  313. (let ((rec idx))
  314. (let lp2 ((subtypes subtypes) (idx idx))
  315. (match subtypes
  316. (() (lp types idx))
  317. ((($ <type> id subtype) . subtypes)
  318. (or (visit-sub rec id idx subtype)
  319. (lp2 subtypes (1+ idx))))))))
  320. ((($ <type> id type) . types)
  321. (or (visit-sub idx id idx type)
  322. (lp types (1+ idx)))))))