types.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. <tag-type>
  58. make-tag-type tag-type?
  59. tag-type-attribute tag-type-type
  60. <array-type>
  61. make-array-type array-type?
  62. array-type-mutable?
  63. array-type-type
  64. <field>
  65. make-field field?
  66. field-id field-mutable? field-type
  67. <struct-type>
  68. make-struct-type struct-type?
  69. struct-type-fields
  70. <sub-type>
  71. make-sub-type sub-type?
  72. sub-type-final? sub-type-supers sub-type-type
  73. <rec-group>
  74. make-rec-group rec-group?
  75. rec-group-types
  76. <type>
  77. make-type type?
  78. type-id type-val
  79. <import>
  80. make-import import?
  81. import-mod import-name import-kind import-id import-type
  82. <export>
  83. make-export export?
  84. export-name export-kind export-idx
  85. <mem-arg>
  86. make-mem-arg mem-arg?
  87. mem-arg-id mem-arg-offset mem-arg-align
  88. <elem>
  89. make-elem elem?
  90. elem-id elem-mode elem-table elem-type elem-offset elem-inits
  91. <data>
  92. make-data data?
  93. data-id data-mode data-mem data-offset data-init
  94. <tag>
  95. make-tag tag?
  96. tag-id tag-type
  97. <local>
  98. make-local local?
  99. local-id local-type
  100. <func>
  101. make-func func?
  102. func-id func-type func-locals func-body
  103. <table>
  104. make-table table?
  105. table-id table-type table-init
  106. <memory>
  107. make-memory memory?
  108. memory-id memory-type
  109. <global>
  110. make-global global?
  111. global-id global-type global-init
  112. <custom>
  113. make-custom custom?
  114. custom-name custom-bytes
  115. <names>
  116. make-names names?
  117. names-module names-func names-local names-label names-type
  118. names-table names-memory names-global names-elem names-data
  119. names-field names-tag
  120. find-type))
  121. (define-record-type <wasm>
  122. (make-wasm id types imports funcs tables memories
  123. globals exports start elems datas tags
  124. strings custom)
  125. wasm?
  126. (id wasm-id)
  127. (types wasm-types)
  128. (imports wasm-imports)
  129. (funcs wasm-funcs)
  130. (tables wasm-tables)
  131. (memories wasm-memories)
  132. (globals wasm-globals)
  133. (exports wasm-exports)
  134. (start wasm-start)
  135. (elems wasm-elems)
  136. (datas wasm-datas)
  137. (tags wasm-tags)
  138. (strings wasm-strings)
  139. (custom wasm-custom))
  140. (define (print-wasm wasm port)
  141. (format port "#<wasm ~a>" (object-address wasm)))
  142. (set-record-type-printer! <wasm> print-wasm)
  143. (define-record-type <param>
  144. (make-param id type)
  145. param?
  146. (id param-id)
  147. (type param-type))
  148. (define-record-type <func-sig>
  149. (make-func-sig params results)
  150. func-sig?
  151. (params func-sig-params)
  152. (results func-sig-results))
  153. (define-record-type <type-use>
  154. (make-type-use idx sig)
  155. type-use?
  156. (idx type-use-idx)
  157. (sig type-use-sig))
  158. (define-record-type <ref-type>
  159. (make-ref-type nullable? heap-type)
  160. ref-type?
  161. (nullable? ref-type-nullable?)
  162. (heap-type ref-type-heap-type))
  163. (define-record-type <limits>
  164. (make-limits min max)
  165. limits?
  166. (min limits-min)
  167. (max limits-max))
  168. (define-record-type <table-type>
  169. (make-table-type limits elem-type)
  170. table-type?
  171. (limits table-type-limits)
  172. (elem-type table-type-elem-type))
  173. (define-record-type <mem-type>
  174. (make-mem-type limits)
  175. mem-type?
  176. (limits mem-type-limits))
  177. (define-record-type <global-type>
  178. (make-global-type mutable? type)
  179. global-type?
  180. (mutable? global-type-mutable?)
  181. (type global-type-type))
  182. (define-record-type <tag-type>
  183. (make-tag-type attribute type)
  184. tag-type?
  185. (attribute tag-type-attribute)
  186. (type tag-type-type))
  187. (define-record-type <array-type>
  188. (make-array-type mutable? type)
  189. array-type?
  190. (mutable? array-type-mutable?)
  191. (type array-type-type))
  192. (define-record-type <field>
  193. (make-field id mutable? type)
  194. field?
  195. (id field-id)
  196. (mutable? field-mutable?)
  197. (type field-type))
  198. (define-record-type <struct-type>
  199. (make-struct-type fields)
  200. struct-type?
  201. (fields struct-type-fields))
  202. (define-record-type <sub-type>
  203. (make-sub-type final? supers type)
  204. sub-type?
  205. (final? sub-type-final?)
  206. (supers sub-type-supers)
  207. (type sub-type-type))
  208. (define-record-type <rec-group>
  209. (make-rec-group types)
  210. rec-group?
  211. (types rec-group-types))
  212. (define-record-type <type>
  213. (make-type id val)
  214. type?
  215. (id type-id)
  216. (val type-val))
  217. (define-record-type <import>
  218. (make-import mod name kind id type)
  219. import?
  220. (mod import-mod)
  221. (name import-name)
  222. (kind import-kind)
  223. (id import-id)
  224. (type import-type))
  225. (define-record-type <export>
  226. (make-export name kind idx)
  227. export?
  228. (name export-name)
  229. (kind export-kind)
  230. (idx export-idx))
  231. (define-record-type <mem-arg>
  232. (make-mem-arg id offset align)
  233. mem-arg?
  234. (id mem-arg-id)
  235. (offset mem-arg-offset)
  236. (align mem-arg-align))
  237. (define-record-type <elem>
  238. (make-elem id mode table type offset inits)
  239. elem?
  240. (id elem-id)
  241. (mode elem-mode)
  242. (table elem-table)
  243. (type elem-type)
  244. (offset elem-offset)
  245. (inits elem-inits))
  246. (define-record-type <data>
  247. (make-data id mode mem offset init)
  248. data?
  249. (id data-id)
  250. (mode data-mode)
  251. (mem data-mem)
  252. (offset data-offset)
  253. (init data-init))
  254. (define-record-type <tag>
  255. (make-tag id type)
  256. tag?
  257. (id tag-id)
  258. (type tag-type))
  259. (define-record-type <local>
  260. (make-local id type)
  261. local?
  262. (id local-id)
  263. (type local-type))
  264. (define-record-type <func>
  265. (make-func id type locals body)
  266. func?
  267. (id func-id)
  268. (type func-type)
  269. (locals func-locals)
  270. (body func-body))
  271. (define-record-type <table>
  272. (make-table id type init)
  273. table?
  274. (id table-id)
  275. (type table-type)
  276. (init table-init))
  277. (define-record-type <memory>
  278. (make-memory id type)
  279. memory?
  280. (id memory-id)
  281. (type memory-type))
  282. (define-record-type <global>
  283. (make-global id type init)
  284. global?
  285. (id global-id)
  286. (type global-type)
  287. (init global-init))
  288. (define-record-type <names>
  289. (make-names module func local label type table
  290. memory global elem data field tag)
  291. names?
  292. (module names-module)
  293. (func names-func)
  294. (local names-local)
  295. (label names-label)
  296. (type names-type)
  297. (table names-table)
  298. (memory names-memory)
  299. (global names-global)
  300. (elem names-elem)
  301. (data names-data)
  302. (field names-field)
  303. (tag names-tag))
  304. (define-record-type <custom>
  305. (make-custom name bytes)
  306. custom?
  307. (name custom-name)
  308. (bytes custom-bytes))
  309. (define (find-type pred types)
  310. (let lp ((types types) (idx 0))
  311. (define (visit-base rec supers type-id type-idx type)
  312. (pred rec type-id type-idx supers type))
  313. (define (visit-sub rec type-id type-idx type)
  314. (match type
  315. (($ <sub-type> final? supers type)
  316. (visit-base rec supers type-id type-idx type))
  317. (_ (visit-base rec '() type-id type-idx type))))
  318. (match types
  319. (() #f)
  320. ((($ <rec-group> subtypes) . types)
  321. (let ((rec idx))
  322. (let lp2 ((subtypes subtypes) (idx idx))
  323. (match subtypes
  324. (() (lp types idx))
  325. ((($ <type> id subtype) . subtypes)
  326. (or (visit-sub rec id idx subtype)
  327. (lp2 subtypes (1+ idx))))))))
  328. ((($ <type> id type) . types)
  329. (or (visit-sub idx id idx type)
  330. (lp types (1+ idx)))))))