types.scm 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. ;;; Type analysis on CPS
  2. ;;; Copyright (C) 2014-2019 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software: you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU Lesser General Public License as
  6. ;;; published by the Free Software Foundation, either version 3 of the
  7. ;;; License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful, but
  10. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;; Lesser General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU Lesser General Public
  15. ;;; License along with this program. If not, see
  16. ;;; <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;;
  19. ;;; Type analysis computes the possible types and ranges that values may
  20. ;;; have at all program positions. This analysis can help to prove that
  21. ;;; a primcall has no side-effects, if its arguments have the
  22. ;;; appropriate type and range. It can also enable constant folding of
  23. ;;; type predicates and, in the future, enable the compiler to choose
  24. ;;; untagged, unboxed representations for numbers.
  25. ;;;
  26. ;;; For the purposes of this analysis, a "type" is an aspect of a value
  27. ;;; that will not change. Guile's CPS intermediate language does not
  28. ;;; carry manifest type information that asserts properties about given
  29. ;;; values; instead, we recover this information via flow analysis,
  30. ;;; garnering properties from type predicates, constant literals,
  31. ;;; primcall results, and primcalls that assert that their arguments are
  32. ;;; of particular types.
  33. ;;;
  34. ;;; A range denotes a subset of the set of values in a type, bounded by
  35. ;;; a minimum and a maximum. The precise meaning of a range depends on
  36. ;;; the type. For real numbers, the range indicates an inclusive lower
  37. ;;; and upper bound on the integer value of a type. For vectors, the
  38. ;;; range indicates the length of the vector. The range is the union of
  39. ;;; the signed and unsigned 64-bit ranges. Additionally, the minimum
  40. ;;; bound of a range may be -inf.0, and the maximum bound may be +inf.0.
  41. ;;; For some types, like pairs, the concept of "range" makes no sense.
  42. ;;; In these cases we consider the range to be -inf.0 to +inf.0.
  43. ;;;
  44. ;;; Types are represented as a bitfield. Fewer bits means a more precise
  45. ;;; type. Although normally only values that have a single type will
  46. ;;; have an associated range, this is not enforced. The range applies
  47. ;;; to all types in the bitfield. When control flow meets, the types and
  48. ;;; ranges meet with the union operator.
  49. ;;;
  50. ;;; It is not practical to precisely compute value ranges in all cases.
  51. ;;; For example, in the following case:
  52. ;;;
  53. ;;; (let lp ((n 0)) (when (foo) (lp (1+ n))))
  54. ;;;
  55. ;;; The first time that range analysis visits the program, N is
  56. ;;; determined to be the exact integer 0. The second time, it is an
  57. ;;; exact integer in the range [0, 1]; the third, [0, 2]; and so on.
  58. ;;; This analysis will terminate, but only after the positive half of
  59. ;;; the 64-bit range has been fully explored and we decide that the
  60. ;;; range of N is [0, +inf.0]. At the same time, we want to do range
  61. ;;; analysis and type analysis at the same time, as there are
  62. ;;; interactions between them, notably in the case of `sqrt' which
  63. ;;; returns a complex number if its argument cannot be proven to be
  64. ;;; non-negative. So what we do instead is to precisely propagate types
  65. ;;; and ranges when propagating forward, but after the first backwards
  66. ;;; branch is seen, we cause backward branches that would expand the
  67. ;;; range of a value to saturate that range towards positive or negative
  68. ;;; infinity (as appropriate).
  69. ;;;
  70. ;;; A naive approach to type analysis would build up a table that has
  71. ;;; entries for all variables at all program points, but this has
  72. ;;; N-squared complexity and quickly grows unmanageable. Instead, we
  73. ;;; use _intmaps_ from (language cps intmap) to share state between
  74. ;;; connected program points.
  75. ;;;
  76. ;;; Code:
  77. (define-module (language cps types)
  78. #:use-module (ice-9 match)
  79. #:use-module (language cps)
  80. #:use-module (language cps utils)
  81. #:use-module (language cps intmap)
  82. #:use-module (language cps intset)
  83. #:use-module (rnrs bytevectors)
  84. #:use-module (srfi srfi-11)
  85. #:use-module ((system syntax internal) #:select (syntax?))
  86. #:use-module (system base target)
  87. #:export (;; Specific types.
  88. &fixnum
  89. &bignum
  90. &flonum
  91. &complex
  92. &fraction
  93. &char
  94. &special-immediate
  95. &symbol
  96. &keyword
  97. &procedure
  98. &pointer
  99. &fluid
  100. &pair
  101. &immutable-vector
  102. &mutable-vector
  103. &box
  104. &struct
  105. &string
  106. &bytevector
  107. &bitvector
  108. &array
  109. &syntax
  110. &other-heap-object
  111. ;; Special immediate values.
  112. &null &nil &false &true &unspecified &undefined &eof
  113. ;; Union types.
  114. &exact-integer &exact-number &real &number &vector
  115. ;; Untagged types.
  116. &f64
  117. &u64
  118. &s64
  119. ;; Helper.
  120. type<=?
  121. ;; Interface for type inference.
  122. infer-types
  123. lookup-pre-type
  124. lookup-post-type
  125. primcall-types-check?))
  126. (define-syntax define-flags
  127. (lambda (x)
  128. (syntax-case x ()
  129. ((_ all shift name ...)
  130. (let ((count (length #'(name ...))))
  131. (with-syntax (((n ...) (iota count))
  132. (count count))
  133. #'(begin
  134. (define-syntax name (identifier-syntax (ash 1 n)))
  135. ...
  136. (define-syntax all (identifier-syntax (1- (ash 1 count))))
  137. (define-syntax shift (identifier-syntax count)))))))))
  138. ;; More precise types have fewer bits.
  139. (define-flags &all-types &type-bits
  140. &fixnum
  141. &bignum
  142. &flonum
  143. &complex
  144. &fraction
  145. &char
  146. &special-immediate
  147. &symbol
  148. &keyword
  149. &procedure
  150. &pointer
  151. &fluid
  152. &pair
  153. &immutable-vector
  154. &mutable-vector
  155. &box
  156. &struct
  157. &string
  158. &bytevector
  159. &bitvector
  160. &array
  161. &syntax
  162. &other-heap-object
  163. &f64
  164. &u64
  165. &s64)
  166. (define-syntax &no-type (identifier-syntax 0))
  167. ;; Special immediate values. Note that the values for the first 4 of
  168. ;; these are important; see uses below.
  169. (define-syntax &null (identifier-syntax 0))
  170. (define-syntax &nil (identifier-syntax 1))
  171. (define-syntax &false (identifier-syntax 2))
  172. (define-syntax &true (identifier-syntax 3))
  173. (define-syntax &unspecified (identifier-syntax 4))
  174. (define-syntax &undefined (identifier-syntax 5))
  175. (define-syntax &eof (identifier-syntax 6))
  176. (define-syntax &exact-integer
  177. (identifier-syntax (logior &fixnum &bignum)))
  178. (define-syntax &exact-number
  179. (identifier-syntax (logior &fixnum &bignum &fraction)))
  180. (define-syntax &real
  181. (identifier-syntax (logior &fixnum &bignum &flonum &fraction)))
  182. (define-syntax &number
  183. (identifier-syntax (logior &fixnum &bignum &flonum &complex &fraction)))
  184. (define-syntax &vector
  185. (identifier-syntax (logior &immutable-vector &mutable-vector)))
  186. (define-syntax-rule (type<=? x type)
  187. (zero? (logand x (lognot type))))
  188. ;; Versions of min and max that do not coerce exact numbers to become
  189. ;; inexact.
  190. (define min
  191. (case-lambda
  192. ((a b) (if (< a b) a b))
  193. ((a b c) (min (min a b) c))
  194. ((a b c d) (min (min a b) c d))))
  195. (define max
  196. (case-lambda
  197. ((a b) (if (> a b) a b))
  198. ((a b c) (max (max a b) c))
  199. ((a b c d) (max (max a b) c d))))
  200. (define-syntax-rule (define-compile-time-value name val)
  201. (define-syntax name
  202. (make-variable-transformer
  203. (lambda (x)
  204. (syntax-case x (set!)
  205. (var (identifier? #'var)
  206. (datum->syntax #'var val)))))))
  207. (define-compile-time-value &fx32-min (- #x20000000))
  208. (define-compile-time-value &fx32-max #x1fffFFFF)
  209. (define-compile-time-value &fx64-min (- #x2000000000000000))
  210. (define-compile-time-value &fx64-max #x1fffFFFFffffFFFF)
  211. (define-compile-time-value &s64-min (- #x8000000000000000))
  212. (define-compile-time-value &s64-max #x7fffFFFFffffFFFF)
  213. (define-compile-time-value &u64-max #xffffFFFFffffFFFF)
  214. (define-syntax &range-min (identifier-syntax &s64-min))
  215. (define-syntax &range-max (identifier-syntax &u64-max))
  216. (define *max-codepoint* #x10ffff)
  217. (define-inlinable (make-unclamped-type-entry type min max)
  218. (vector type min max))
  219. (define-inlinable (type-entry-type tentry)
  220. (vector-ref tentry 0))
  221. (define-inlinable (type-entry-min tentry)
  222. (vector-ref tentry 1))
  223. (define-inlinable (type-entry-max tentry)
  224. (vector-ref tentry 2))
  225. (define-inlinable (clamp-min val)
  226. (cond
  227. ;; Fast path to avoid comparisons with bignums.
  228. ((<= most-negative-fixnum val most-positive-fixnum) val)
  229. ((< val &range-min) -inf.0)
  230. ((< &range-max val) &range-max)
  231. (else val)))
  232. (define-inlinable (clamp-max val)
  233. (cond
  234. ;; Fast path to avoid comparisons with bignums.
  235. ((<= most-negative-fixnum val most-positive-fixnum) val)
  236. ((< &range-max val) +inf.0)
  237. ((< val &range-min) &range-min)
  238. (else val)))
  239. (define-inlinable (make-type-entry type min max)
  240. (vector type (clamp-min min) (clamp-max max)))
  241. (define all-types-entry (make-type-entry &all-types -inf.0 +inf.0))
  242. (define* (var-type-entry typeset var #:optional (default all-types-entry))
  243. (intmap-ref typeset var (lambda (_) default)))
  244. (define (var-type typeset var)
  245. (type-entry-type (var-type-entry typeset var)))
  246. (define (var-min typeset var)
  247. (type-entry-min (var-type-entry typeset var)))
  248. (define (var-max typeset var)
  249. (type-entry-max (var-type-entry typeset var)))
  250. ;; Is the type entry A contained entirely within B?
  251. (define (type-entry<=? a b)
  252. (match (cons a b)
  253. ((#(a-type a-min a-max) . #(b-type b-min b-max))
  254. (and (eqv? b-type (logior a-type b-type))
  255. (<= b-min a-min)
  256. (>= b-max a-max)))))
  257. (define (type-entry-union a b)
  258. (cond
  259. ((type-entry<=? b a) a)
  260. ((type-entry<=? a b) b)
  261. (else (make-type-entry
  262. (logior (type-entry-type a) (type-entry-type b))
  263. (min (type-entry-min a) (type-entry-min b))
  264. (max (type-entry-max a) (type-entry-max b))))))
  265. (define (type-entry-saturating-union a b)
  266. (cond
  267. ((type-entry<=? b a) a)
  268. (else
  269. (make-type-entry
  270. (logior (type-entry-type a) (type-entry-type b))
  271. (let ((a-min (type-entry-min a))
  272. (b-min (type-entry-min b)))
  273. (cond
  274. ((not (< b-min a-min)) a-min)
  275. ((< 0 b-min) 0)
  276. ((< &fx32-min b-min) &fx32-min)
  277. ((< &fx64-min b-min) &fx64-min)
  278. ((< &range-min b-min) &range-min)
  279. (else -inf.0)))
  280. (let ((a-max (type-entry-max a))
  281. (b-max (type-entry-max b)))
  282. (cond
  283. ((not (> b-max a-max)) a-max)
  284. ((> &fx32-max b-max) &fx32-max)
  285. ((> &fx64-max b-max) &fx64-max)
  286. ((> &s64-max b-max) &s64-max)
  287. ((> &range-max b-max) &range-max)
  288. (else +inf.0)))))))
  289. (define (type-entry-intersection a b)
  290. (cond
  291. ((type-entry<=? a b) a)
  292. ((type-entry<=? b a) b)
  293. (else (make-type-entry
  294. (logand (type-entry-type a) (type-entry-type b))
  295. (max (type-entry-min a) (type-entry-min b))
  296. (min (type-entry-max a) (type-entry-max b))))))
  297. (define (adjoin-var typeset var entry)
  298. (intmap-add typeset var entry type-entry-union))
  299. (define (restrict-var typeset var entry)
  300. (intmap-add typeset var entry type-entry-intersection))
  301. (define (constant-type val)
  302. "Compute the type and range of VAL. Return three values: the type,
  303. minimum, and maximum."
  304. (define (return type val)
  305. (if val
  306. (make-type-entry type val val)
  307. (make-type-entry type -inf.0 +inf.0)))
  308. (cond
  309. ((number? val)
  310. (cond
  311. ((exact-integer? val)
  312. (return (if (<= (target-most-negative-fixnum)
  313. val
  314. (target-most-positive-fixnum))
  315. &fixnum
  316. &bignum)
  317. val))
  318. ((eqv? (imag-part val) 0)
  319. (if (nan? val)
  320. (make-type-entry &flonum -inf.0 +inf.0)
  321. (make-type-entry
  322. (if (exact? val) &fraction &flonum)
  323. (if (rational? val) (inexact->exact (floor val)) val)
  324. (if (rational? val) (inexact->exact (ceiling val)) val))))
  325. (else (return &complex #f))))
  326. ((eq? val '()) (return &special-immediate &null))
  327. ((eq? val #nil) (return &special-immediate &nil))
  328. ((eq? val #t) (return &special-immediate &true))
  329. ((eq? val #f) (return &special-immediate &false))
  330. ((eqv? val *unspecified*) (return &special-immediate &unspecified))
  331. ((char? val) (return &char (char->integer val)))
  332. ((symbol? val) (return &symbol #f))
  333. ((keyword? val) (return &keyword #f))
  334. ((pair? val) (return &pair #f))
  335. ((vector? val) (return &immutable-vector (vector-length val)))
  336. ((string? val) (return &string (string-length val)))
  337. ((bytevector? val) (return &bytevector (bytevector-length val)))
  338. ((bitvector? val) (return &bitvector (bitvector-length val)))
  339. ((array? val) (return &array (array-rank val)))
  340. ((syntax? val) (return &syntax 0))
  341. ((not (variable-bound? (make-variable val)))
  342. (return &special-immediate &undefined))
  343. (else (error "unhandled constant" val))))
  344. (define *type-checkers* (make-hash-table))
  345. (define *type-inferrers* (make-hash-table))
  346. (define-syntax-rule (define-type-helper name)
  347. (define-syntax-parameter name
  348. (lambda (stx)
  349. (syntax-violation 'name
  350. "macro used outside of define-type"
  351. stx))))
  352. (define-type-helper define!)
  353. (define-type-helper restrict!)
  354. (define-type-helper &type)
  355. (define-type-helper &min)
  356. (define-type-helper &max)
  357. (define-syntax-rule (define-exact-integer! result min max)
  358. (let ((min* min) (max* max))
  359. (define! result
  360. (if (<= (target-most-negative-fixnum)
  361. min* max*
  362. (target-most-positive-fixnum))
  363. &fixnum
  364. &exact-integer)
  365. min* max*)))
  366. ;; Accessors to use in type inferrers where you know that the values
  367. ;; must be in some range for the computation to proceed (not throw an
  368. ;; error). Note that these accessors should be used even for &u64 and
  369. ;; &s64 values, whose definitions you would think would be apparent
  370. ;; already. However it could be that the graph isn't sorted, so we see
  371. ;; a use before a definition, in which case we need to clamp the generic
  372. ;; limits to the &u64/&s64 range.
  373. (define-syntax-rule (&min/0 x) (max (&min x) 0))
  374. (define-syntax-rule (&max/u64 x) (min (&max x) &u64-max))
  375. (define-syntax-rule (&min/s64 x) (max (&min x) &s64-min))
  376. (define-syntax-rule (&max/s64 x) (min (&max x) &s64-max))
  377. (define-syntax-rule (&min/fixnum x) (max (&min x) (target-most-negative-fixnum)))
  378. (define-syntax-rule (&max/fixnum x) (min (&max x) (target-most-positive-fixnum)))
  379. (define-syntax-rule (&max/size x) (min (&max x) (target-max-size-t)))
  380. (define-syntax-rule (&max/scm-size x) (min (&max x) (target-max-size-t/scm)))
  381. (define-syntax-rule (define-type-checker/param (name param arg ...) body ...)
  382. (hashq-set!
  383. *type-checkers*
  384. 'name
  385. (lambda (typeset param arg ...)
  386. (syntax-parameterize
  387. ((&type (syntax-rules () ((_ val) (var-type typeset val))))
  388. (&min (syntax-rules () ((_ val) (var-min typeset val))))
  389. (&max (syntax-rules () ((_ val) (var-max typeset val)))))
  390. body ...))))
  391. (define-syntax-rule (define-type-checker (name arg ...) body ...)
  392. (define-type-checker/param (name param arg ...) body ...))
  393. (define-syntax-rule (check-type arg type min max)
  394. ;; If the arg is negative, it is a closure variable.
  395. (and (>= arg 0)
  396. (zero? (logand (lognot type) (&type arg)))
  397. (<= min (&min arg))
  398. (<= (&max arg) max)))
  399. (define-syntax-rule (define-type-inferrer* (name param succ var ...) body ...)
  400. (hashq-set!
  401. *type-inferrers*
  402. 'name
  403. (lambda (in succ param var ...)
  404. (let ((out in))
  405. (syntax-parameterize
  406. ((define!
  407. (syntax-rules ()
  408. ((_ val type min max)
  409. (set! out (adjoin-var out val
  410. (make-type-entry type min max))))))
  411. (restrict!
  412. (syntax-rules ()
  413. ((_ val type min max)
  414. (set! out (restrict-var out val
  415. (make-type-entry type min max))))))
  416. (&type (syntax-rules () ((_ val) (var-type in val))))
  417. (&min (syntax-rules () ((_ val) (var-min in val))))
  418. (&max (syntax-rules () ((_ val) (var-max in val)))))
  419. body ...
  420. out)))))
  421. (define-syntax-rule (define-type-inferrer (name arg ...) body ...)
  422. (define-type-inferrer* (name param succ arg ...) body ...))
  423. (define-syntax-rule (define-type-inferrer/param (name param arg ...) body ...)
  424. (define-type-inferrer* (name param succ arg ...) body ...))
  425. (define-syntax-rule (define-predicate-inferrer (name arg ... true?) body ...)
  426. (define-type-inferrer* (name param succ arg ...)
  427. (let ((true? (not (zero? succ))))
  428. body ...)))
  429. (define-syntax-rule (define-predicate-inferrer/param
  430. (name param arg ... true?) body ...)
  431. (define-type-inferrer* (name param succ arg ...)
  432. (let ((true? (not (zero? succ))))
  433. body ...)))
  434. (define-syntax define-simple-type-checker
  435. (lambda (x)
  436. (define (parse-spec l)
  437. (syntax-case l ()
  438. (() '())
  439. (((type min max) . l) (cons #'(type min max) (parse-spec #'l)))
  440. (((type min+max) . l) (cons #'(type min+max min+max) (parse-spec #'l)))
  441. ((type . l) (cons #'(type -inf.0 +inf.0) (parse-spec #'l)))))
  442. (syntax-case x ()
  443. ((_ (name arg-spec ...) result-spec ...)
  444. (with-syntax
  445. (((arg ...) (generate-temporaries #'(arg-spec ...)))
  446. (((arg-type arg-min arg-max) ...) (parse-spec #'(arg-spec ...))))
  447. #'(define-type-checker (name arg ...)
  448. (and (check-type arg arg-type arg-min arg-max)
  449. ...)))))))
  450. (define-syntax define-simple-type-inferrer
  451. (lambda (x)
  452. (define (parse-spec l)
  453. (syntax-case l ()
  454. (() '())
  455. (((type min max) . l) (cons #'(type min max) (parse-spec #'l)))
  456. (((type min+max) . l) (cons #'(type min+max min+max) (parse-spec #'l)))
  457. ((type . l) (cons #'(type -inf.0 +inf.0) (parse-spec #'l)))))
  458. (syntax-case x ()
  459. ((_ (name arg-spec ...) result-spec ...)
  460. (with-syntax
  461. (((arg ...) (generate-temporaries #'(arg-spec ...)))
  462. (((arg-type arg-min arg-max) ...) (parse-spec #'(arg-spec ...)))
  463. ((res ...) (generate-temporaries #'(result-spec ...)))
  464. (((res-type res-min res-max) ...) (parse-spec #'(result-spec ...))))
  465. #'(define-type-inferrer (name arg ... res ...)
  466. (restrict! arg arg-type arg-min arg-max)
  467. ...
  468. (define! res res-type res-min res-max)
  469. ...))))))
  470. (define-syntax-rule (define-simple-type (name arg-spec ...) result-spec ...)
  471. (begin
  472. (define-simple-type-checker (name arg-spec ...))
  473. (define-simple-type-inferrer (name arg-spec ...) result-spec ...)))
  474. (define-syntax-rule (define-simple-types
  475. ((name arg-spec ...) result-spec ...)
  476. ...)
  477. (begin
  478. (define-simple-type (name arg-spec ...) result-spec ...)
  479. ...))
  480. (define-syntax-rule (define-type-checker-aliases orig alias ...)
  481. (let ((check (hashq-ref *type-checkers* 'orig)))
  482. (hashq-set! *type-checkers* 'alias check)
  483. ...))
  484. (define-syntax-rule (define-type-inferrer-aliases orig alias ...)
  485. (let ((check (hashq-ref *type-inferrers* 'orig)))
  486. (hashq-set! *type-inferrers* 'alias check)
  487. ...))
  488. (define-syntax-rule (define-type-aliases orig alias ...)
  489. (begin
  490. (define-type-checker-aliases orig alias ...)
  491. (define-type-inferrer-aliases orig alias ...)))
  492. ;;; This list of primcall type definitions follows the order of
  493. ;;; effects-analysis.scm; please keep it in a similar order.
  494. ;;;
  495. ;;; There is no need to add checker definitions for expressions that do
  496. ;;; not exhibit the &type-check effect, as callers should not ask if
  497. ;;; such an expression does or does not type-check. For those that do
  498. ;;; exhibit &type-check, you should define a type inferrer unless the
  499. ;;; primcall will never typecheck.
  500. ;;;
  501. ;;; Likewise there is no need to define inferrers for primcalls which
  502. ;;; return &all-types values and which never raise exceptions from which
  503. ;;; we can infer the types of incoming values.
  504. ;;;
  505. ;;; Generic effect-free predicates.
  506. ;;;
  507. (define-syntax-rule (define-special-immediate-predicate-inferrer pred imm)
  508. (define-predicate-inferrer (pred val true?)
  509. (define (range-subtract lo hi x)
  510. (values (if (eqv? lo x) (1+ lo) lo)
  511. (if (eqv? hi x) (1- hi) hi)))
  512. (cond
  513. (true? (restrict! val &special-immediate imm imm))
  514. (else
  515. (when (eqv? (&type val) &special-immediate)
  516. (let-values (((lo hi) (range-subtract (&min val) (&max val) imm)))
  517. (restrict! val &special-immediate lo hi)))))))
  518. (define-special-immediate-predicate-inferrer eq-nil? &nil)
  519. (define-special-immediate-predicate-inferrer eq-eol? &null)
  520. (define-special-immediate-predicate-inferrer eq-false? &false)
  521. (define-special-immediate-predicate-inferrer eq-true? &true)
  522. (define-special-immediate-predicate-inferrer unspecified? &unspecified)
  523. (define-special-immediate-predicate-inferrer undefined? &undefined)
  524. (define-special-immediate-predicate-inferrer eof-object? &eof)
  525. ;; Various inferrers rely on these having contiguous values starting from 0.
  526. (eval-when (expand)
  527. (unless (< -1 &null &nil &false &true 4)
  528. (error "unexpected special immediate values")))
  529. (define-predicate-inferrer (null? val true?)
  530. (cond
  531. (true? (restrict! val &special-immediate &null &nil))
  532. (else
  533. (when (eqv? (&type val) &special-immediate)
  534. (restrict! val &special-immediate (1+ &nil) +inf.0)))))
  535. (define-predicate-inferrer (false? val true?)
  536. (cond
  537. (true? (restrict! val &special-immediate &nil &false))
  538. (else
  539. (when (and (eqv? (&type val) &special-immediate) (> (&min val) &null))
  540. (restrict! val &special-immediate (1+ &false) +inf.0)))))
  541. (define-predicate-inferrer (nil? val true?)
  542. (cond
  543. (true? (restrict! val &special-immediate &null &false))
  544. (else
  545. (when (eqv? (&type val) &special-immediate)
  546. (restrict! val &special-immediate (1+ &false) +inf.0)))))
  547. (define-predicate-inferrer (heap-object? val true?)
  548. (define &immediate-types
  549. (logior &fixnum &char &special-immediate))
  550. (define &heap-object-types
  551. (logand &all-types (lognot &immediate-types)))
  552. (restrict! val (if true? &heap-object-types &immediate-types) -inf.0 +inf.0))
  553. (define-predicate-inferrer (heap-number? val true?)
  554. (define &heap-number-types
  555. (logior &bignum &flonum &complex &fraction))
  556. (define &other-types
  557. (logand &all-types (lognot &heap-number-types)))
  558. (restrict! val (if true? &heap-number-types &other-types) -inf.0 +inf.0))
  559. (define-predicate-inferrer (fixnum? val true?)
  560. (cond
  561. (true?
  562. (restrict! val &fixnum
  563. (target-most-negative-fixnum) (target-most-positive-fixnum)))
  564. ((type<=? (&type val) &exact-integer)
  565. (cond
  566. ((<= (&max val) (target-most-positive-fixnum))
  567. (restrict! val &bignum -inf.0 (1- (target-most-negative-fixnum))))
  568. ((>= (&min val) (target-most-negative-fixnum))
  569. (restrict! val &bignum (1+ (target-most-positive-fixnum)) +inf.0))
  570. (else
  571. (restrict! val &bignum -inf.0 +inf.0))))
  572. (else
  573. (restrict! val (logand &all-types (lognot &fixnum)) -inf.0 +inf.0))))
  574. (define-predicate-inferrer (bignum? val true?)
  575. (cond
  576. (true?
  577. (cond
  578. ((<= (&max val) (target-most-positive-fixnum))
  579. (restrict! val &bignum -inf.0 (1- (target-most-negative-fixnum))))
  580. ((>= (&min val) (target-most-negative-fixnum))
  581. (restrict! val &bignum (1+ (target-most-positive-fixnum)) +inf.0))
  582. (else
  583. (restrict! val &bignum -inf.0 +inf.0))))
  584. ((type<=? (&type val) &exact-integer)
  585. (restrict! val &fixnum
  586. (target-most-negative-fixnum) (target-most-positive-fixnum)))
  587. (else
  588. (restrict! val (logand &all-types (lognot &bignum)) -inf.0 +inf.0))))
  589. (define-syntax-rule (define-simple-predicate-inferrer predicate type)
  590. (define-predicate-inferrer (predicate val true?)
  591. (let ((type (if true?
  592. type
  593. (logand (&type val) (lognot type)))))
  594. (restrict! val type -inf.0 +inf.0))))
  595. (define-simple-predicate-inferrer pair? &pair)
  596. (define-simple-predicate-inferrer symbol? &symbol)
  597. (define-simple-predicate-inferrer variable? &box)
  598. (define-simple-predicate-inferrer immutable-vector? &immutable-vector)
  599. (define-simple-predicate-inferrer mutable-vector? &mutable-vector)
  600. (define-simple-predicate-inferrer struct? &struct)
  601. (define-simple-predicate-inferrer string? &string)
  602. (define-simple-predicate-inferrer bytevector? &bytevector)
  603. (define-simple-predicate-inferrer bitvector? &bitvector)
  604. (define-simple-predicate-inferrer keyword? &keyword)
  605. (define-simple-predicate-inferrer number? &number)
  606. (define-simple-predicate-inferrer char? &char)
  607. (define-simple-predicate-inferrer procedure? &procedure)
  608. (define-simple-predicate-inferrer flonum? &flonum)
  609. (define-simple-predicate-inferrer compnum? &complex)
  610. (define-simple-predicate-inferrer fracnum? &fraction)
  611. (define-predicate-inferrer (vector? val true?)
  612. (define &not-vector (logand &all-types (lognot &vector)))
  613. (restrict! val (if true? &vector &not-vector) -inf.0 +inf.0))
  614. (define-predicate-inferrer (eq? a b true?)
  615. ;; We can only propagate information down the true leg.
  616. (when true?
  617. (let ((type (logand (&type a) (&type b)))
  618. (min (max (&min a) (&min b)))
  619. (max (min (&max a) (&max b))))
  620. (restrict! a type min max)
  621. (restrict! b type min max))))
  622. (define-type-inferrer-aliases eq? heap-numbers-equal?)
  623. (define-type-inferrer/param (load-const/unlikely param result)
  624. (let ((ent (constant-type param)))
  625. (define! result (type-entry-type ent)
  626. (type-entry-min ent) (type-entry-max ent))))
  627. (define-type-inferrer (u64->s64 u64 s64)
  628. (if (<= (&max u64) &s64-max)
  629. (define! s64 &s64 (&min u64) (&max u64))
  630. (define! s64 &s64 &s64-min &s64-max)))
  631. (define-type-inferrer (s64->u64 s64 u64)
  632. (if (<= 0 (&min s64))
  633. (define! u64 &u64 (&min s64) (&max s64))
  634. (define! u64 &u64 0 &u64-max)))
  635. ;;;
  636. ;;; Memory.
  637. ;;;
  638. (define (annotation->type ann)
  639. (match ann
  640. ('pair &pair)
  641. ('vector &vector)
  642. ('string &string)
  643. ('stringbuf &string)
  644. ('bytevector &bytevector)
  645. ('box &box)
  646. ('closure &procedure)
  647. ('struct &struct)
  648. ('atomic-box &all-types)))
  649. (define-type-inferrer/param (allocate-words param size result)
  650. (define! result (annotation->type param) (&min/0 size) (&max/scm-size size)))
  651. (define-type-inferrer/param (allocate-words/immediate param result)
  652. (match param
  653. ((annotation . size)
  654. (define! result (annotation->type annotation) size size))))
  655. (define-type-inferrer-aliases allocate-words allocate-pointerless-words)
  656. (define-type-inferrer-aliases allocate-words/immediate
  657. allocate-pointerless-words/immediate)
  658. (define-type-inferrer/param (scm-ref param obj idx result)
  659. (restrict! obj (annotation->type param)
  660. (1+ (&min/0 idx)) (target-max-size-t/scm))
  661. (define! result &all-types -inf.0 +inf.0))
  662. (define-type-inferrer/param (scm-ref/immediate param obj result)
  663. (match param
  664. ((annotation . idx)
  665. (restrict! obj (annotation->type annotation) (1+ idx) +inf.0)
  666. (define! result &all-types -inf.0 +inf.0))))
  667. (define-type-inferrer/param (scm-ref/tag param obj result)
  668. (restrict! obj (annotation->type param) -inf.0 +inf.0)
  669. (define! result &all-types -inf.0 +inf.0))
  670. (define-type-inferrer/param (scm-set!/tag param obj val)
  671. (restrict! obj (annotation->type param) -inf.0 +inf.0))
  672. (define-type-inferrer/param (scm-set! param obj idx val)
  673. (restrict! obj (annotation->type param) (1+ (&min/0 idx)) +inf.0))
  674. (define-type-inferrer/param (scm-set!/immediate param obj val)
  675. (match param
  676. ((annotation . idx)
  677. (restrict! obj (annotation->type annotation) (1+ idx) +inf.0))))
  678. (define-type-inferrer/param (word-ref param obj idx result)
  679. (restrict! obj (annotation->type param)
  680. (1+ (&min/0 idx)) (target-max-size-t/scm))
  681. (define! result &u64 0 &u64-max))
  682. (define-type-inferrer/param (word-ref/immediate param obj result)
  683. (match param
  684. ((annotation . idx)
  685. (restrict! obj (annotation->type annotation) (1+ idx) +inf.0)
  686. (define! result &u64 0 &u64-max))))
  687. (define-type-inferrer/param (word-set! param obj idx word)
  688. (restrict! obj (annotation->type param) (1+ (&min/0 idx)) +inf.0))
  689. (define-type-inferrer/param (word-set!/immediate param obj word)
  690. (match param
  691. ((annotation . idx)
  692. (restrict! obj (annotation->type annotation) (1+ idx) +inf.0))))
  693. (define-type-inferrer/param (pointer-ref/immediate param obj result)
  694. (define! result &other-heap-object -inf.0 +inf.0))
  695. (define-type-inferrer/param (tail-pointer-ref/immediate param obj result)
  696. (define! result &other-heap-object -inf.0 +inf.0))
  697. (define-type-inferrer/param (assume-u64 param val result)
  698. (match param
  699. ((lo . hi)
  700. (define! result &u64 (max lo (&min val)) (min hi (&max val))))))
  701. (define-type-inferrer/param (assume-s64 param val result)
  702. (match param
  703. ((lo . hi)
  704. (define! result &s64 (max lo (&min val)) (min hi (&max val))))))
  705. ;;;
  706. ;;; Fluids. Note that we can't track bound-ness of fluids, as pop-fluid
  707. ;;; can change boundness.
  708. ;;;
  709. (define-simple-types
  710. ((fluid-ref (&fluid 1)) &all-types)
  711. ((fluid-set! (&fluid 0 1) &all-types))
  712. ((push-fluid (&fluid 0 1) &all-types))
  713. ((pop-fluid))
  714. ((push-dynamic-state &all-types))
  715. ((pop-dynamic-state)))
  716. ;;;
  717. ;;; Threads. We don't currently track threads as an object type.
  718. ;;;
  719. (define-simple-types
  720. ((current-thread) &all-types))
  721. ;;;
  722. ;;; Strings.
  723. ;;;
  724. (define-simple-type (number->string &number) (&string 0 (target-max-size-t)))
  725. (define-simple-type (string->number (&string 0 (target-max-size-t)))
  726. ((logior &number &special-immediate) -inf.0 +inf.0))
  727. ;;;
  728. ;;; Unboxed numbers.
  729. ;;;
  730. (define-type-checker (scm->f64 scm)
  731. (check-type scm &real -inf.0 +inf.0))
  732. (define-type-inferrer (scm->f64 scm result)
  733. (restrict! scm &real -inf.0 +inf.0)
  734. (define! result &f64 (&min scm) (&max scm)))
  735. (define-type-inferrer/param (load-f64 param result)
  736. (define! result &f64 param param))
  737. (define-type-checker (inexact scm)
  738. (check-type scm &number -inf.0 +inf.0))
  739. (define-type-inferrer (inexact scm result)
  740. (restrict! scm &number -inf.0 +inf.0)
  741. (let* ((in (logand (&type &number)))
  742. (out (if (type<=? in &real)
  743. &flonum
  744. (logior &flonum &complex))))
  745. (define! result out (&min scm) (&max scm))))
  746. (define-type-checker (s64->f64 s64) #t)
  747. (define-type-inferrer (s64->f64 s64 result)
  748. (define! result &f64 (&min s64) (&max s64)))
  749. (define-type-checker (f64->scm f64)
  750. #t)
  751. (define-type-inferrer (f64->scm f64 result)
  752. (define! result &flonum (&min f64) (&max f64)))
  753. (define-type-checker (scm->u64 scm)
  754. (check-type scm &exact-integer 0 &u64-max))
  755. (define-type-inferrer (scm->u64 scm result)
  756. (restrict! scm &exact-integer 0 &u64-max)
  757. (define! result &u64 (&min/0 scm) (&max/u64 scm)))
  758. (define-type-inferrer/param (load-u64 param result)
  759. (define! result &u64 param param))
  760. (define-type-checker (scm->u64/truncate scm)
  761. (check-type scm &exact-integer &range-min &range-max))
  762. (define-type-inferrer (scm->u64/truncate scm result)
  763. (restrict! scm &exact-integer &range-min &range-max)
  764. (define! result &u64 0 &u64-max))
  765. (define-type-checker (u64->scm u64)
  766. #t)
  767. (define-type-inferrer (u64->scm u64 result)
  768. (define-exact-integer! result (&min/0 u64) (&max/u64 u64)))
  769. (define-type-aliases u64->scm u64->scm/unlikely)
  770. (define-type-checker (scm->s64 scm)
  771. (check-type scm &exact-integer &s64-min &s64-max))
  772. (define-type-inferrer (scm->s64 scm result)
  773. (restrict! scm &exact-integer &s64-min &s64-max)
  774. (define! result &s64 (&min/s64 scm) (&max/s64 scm)))
  775. (define-type-aliases s64->scm s64->scm/unlikely)
  776. (define-type-inferrer/param (load-s64 param result)
  777. (define! result &s64 param param))
  778. (define-type-inferrer (untag-fixnum scm result)
  779. (define! result &s64 (&min/fixnum scm) (&max/fixnum scm)))
  780. (define-type-inferrer (tag-fixnum s64 result)
  781. (define! result &fixnum (&min/fixnum s64) (&max/fixnum s64)))
  782. (define-type-aliases tag-fixnum tag-fixnum/unlikely)
  783. ;;;
  784. ;;; Pointers
  785. ;;;
  786. (define-syntax-rule (define-pointer-ref-inferrer ref type lo hi)
  787. (define-type-inferrer (ref obj bv idx result)
  788. (define! result type lo hi)))
  789. (define-pointer-ref-inferrer u8-ref &u64 0 #xff)
  790. (define-pointer-ref-inferrer u16-ref &u64 0 #xffff)
  791. (define-pointer-ref-inferrer u32-ref &u64 0 #xffffffff)
  792. (define-pointer-ref-inferrer u64-ref &u64 0 &u64-max)
  793. (define-pointer-ref-inferrer s8-ref &s64 (- #x80) #x7f)
  794. (define-pointer-ref-inferrer s16-ref &s64 (- #x8000) #x7fff)
  795. (define-pointer-ref-inferrer s32-ref &s64 (- #x80000000) #x7fffffff)
  796. (define-pointer-ref-inferrer s64-ref &s64 &s64-min &s64-max)
  797. (define-pointer-ref-inferrer f32-ref &f64 -inf.0 +inf.0)
  798. (define-pointer-ref-inferrer f64-ref &f64 -inf.0 +inf.0)
  799. ;;;
  800. ;;; Numbers.
  801. ;;;
  802. (define-syntax-rule (infer-= a b true?)
  803. (when true?
  804. (let ((min (max (&min a) (&min b)))
  805. (max (min (&max a) (&max b))))
  806. (restrict! a &all-types min max)
  807. (restrict! b &all-types min max))))
  808. (define-syntax-rule (infer-integer-< a b true?)
  809. (let ((min0 (&min a)) (max0 (&max a))
  810. (min1 (&min b)) (max1 (&max b)))
  811. (cond
  812. (true?
  813. (restrict! a &all-types min0 (min max0 (1- max1)))
  814. (restrict! b &all-types (max (1+ min0) min1) max1))
  815. (else
  816. (restrict! a &all-types (max min0 min1) max0)
  817. (restrict! b &all-types min1 (min max0 max1))))))
  818. (define-simple-type-checker (= &number &number))
  819. (define-predicate-inferrer (= a b true?)
  820. (let ((types (logior (&type a) (&type b))))
  821. (when (type<=? types &number)
  822. ;; OK if e.g. A is a NaN; in that case the range will be
  823. ;; -inf/+inf.
  824. (infer-= a b true?))))
  825. (define-simple-type-checker (< &real &real))
  826. (define-predicate-inferrer (< a b true?)
  827. (let ((types (logior (&type a) (&type b))))
  828. (cond
  829. ((type<=? types &exact-integer)
  830. (cond
  831. ((and (eqv? (&type a) &bignum) (eqv? (&type b) &fixnum))
  832. (if true?
  833. (restrict! a &bignum -inf.0 (1- (target-most-negative-fixnum)))
  834. (restrict! a &bignum (1+ (target-most-positive-fixnum)) +inf.0)))
  835. ((and (eqv? (&type a) &fixnum) (eqv? (&type b) &bignum))
  836. (if true?
  837. (restrict! b &bignum (1+ (target-most-positive-fixnum)) +inf.0)
  838. (restrict! b &bignum -inf.0 (1- (target-most-negative-fixnum)))))
  839. (else
  840. (infer-integer-< a b true?))))
  841. ;; Can't include &flonum because of NaN. Perhaps we should model
  842. ;; NaN with a separate type bit.
  843. ((type<=? types &exact-number)
  844. (let ((min0 (&min a)) (max0 (&max a))
  845. (min1 (&min b)) (max1 (&max b)))
  846. (cond
  847. (true?
  848. (restrict! a &exact-number min0 (min max0 max1))
  849. (restrict! b &exact-number (max min0 min1) max1))
  850. (else
  851. (restrict! a &exact-number (max min0 min1) max0)
  852. (restrict! b &exact-number min1 (min max0 max1)))))))))
  853. (define (infer-<= types succ param a b)
  854. ;; Infer "(<= a b)" as "(not (< b a))", knowing that we only make
  855. ;; inferences when NaN is impossible.
  856. ((hashq-ref *type-inferrers* '<) types (match succ (0 1) (1 0)) param b a))
  857. (hashq-set! *type-inferrers* '<= infer-<=)
  858. (define-predicate-inferrer (u64-= a b true?)
  859. (infer-= a b true?))
  860. (define-predicate-inferrer (u64-< a b true?)
  861. (infer-integer-< a b true?))
  862. (define-predicate-inferrer (s64-= a b true?)
  863. (infer-= a b true?))
  864. (define-predicate-inferrer (s64-< a b true?)
  865. (infer-integer-< a b true?))
  866. (define-predicate-inferrer/param (u64-imm-= b a true?)
  867. (when true?
  868. (restrict! a &u64 (max (&min a) b) (min (&max a) b))))
  869. (define-predicate-inferrer/param (u64-imm-< b a true?)
  870. (if true?
  871. (restrict! a &u64 (&min a) (min (&max a) (1- b)))
  872. (restrict! a &u64 (max (&min a) b) (&max a))))
  873. (define-predicate-inferrer/param (imm-u64-< b a true?)
  874. (if true?
  875. (restrict! a &u64 (max (&min a) (1+ b)) (&max a))
  876. (restrict! a &u64 (&min a) (min (&max a) b))))
  877. (define-predicate-inferrer/param (s64-imm-= b a true?)
  878. (when true?
  879. (restrict! a &s64 (max (&min a) b) (min (&max a) b))))
  880. (define-predicate-inferrer/param (s64-imm-< b a true?)
  881. (if true?
  882. (restrict! a &s64 (&min a) (min (&max a) (1- b)))
  883. (restrict! a &s64 (max (&min a) b) (&max a))))
  884. (define-predicate-inferrer/param (imm-s64-< b a true?)
  885. (if true?
  886. (restrict! a &s64 (max (&min a) (1+ b)) (&max a))
  887. (restrict! a &s64 (&min a) (min (&max a) b))))
  888. ;; Unfortunately, we can't define f64 comparison inferrers because of
  889. ;; not-a-number values.
  890. ;; Arithmetic.
  891. (define-syntax-rule (define-binary-result! a-type$ b-type$ result closed?
  892. min$ max$)
  893. (let* ((min min$) (max max$) (a-type a-type$) (b-type b-type$)
  894. (type (logior a-type b-type)))
  895. (cond
  896. ((not (type<=? type &number))
  897. ;; One input not a number. Perhaps we end up dispatching to
  898. ;; GOOPS.
  899. (define! result &all-types -inf.0 +inf.0))
  900. ;; Complex numbers are contagious.
  901. ((or (eqv? a-type &complex) (eqv? b-type &complex))
  902. (define! result &complex -inf.0 +inf.0))
  903. ((or (eqv? a-type &flonum) (eqv? b-type &flonum))
  904. ;; If one argument is a flonum, the result will be flonum or
  905. ;; possibly complex.
  906. (let ((result-type (logand type (logior &complex &flonum))))
  907. (define! result result-type min max)))
  908. ;; Exact integers are closed under some operations.
  909. ((and closed? (type<=? type &exact-integer))
  910. (define-exact-integer! result min max))
  911. (else
  912. (let* (;; Fractions may become integers.
  913. (type (if (zero? (logand type &fraction))
  914. type
  915. (logior type &exact-integer)))
  916. ;; Integers may become fractions under division.
  917. (type (if (or closed? (zero? (logand type &exact-integer)))
  918. type
  919. (logior type &fraction)))
  920. ;; Fixnums and bignums may become each other, depending on
  921. ;; the range.
  922. (type (cond
  923. ((zero? (logand type &exact-integer))
  924. type)
  925. ((<= (target-most-negative-fixnum)
  926. min max
  927. (target-most-positive-fixnum))
  928. (logand type (lognot &bignum)))
  929. ((or (< max (target-most-negative-fixnum))
  930. (> min (target-most-positive-fixnum)))
  931. (logand type (lognot &fixnum)))
  932. (else
  933. (logior type &fixnum &bignum)))))
  934. (define! result type min max))))))
  935. (define-simple-type-checker (add &number &number))
  936. (define-simple-type-checker (add/immediate &number))
  937. (define-type-inferrer (add a b result)
  938. (define-binary-result! (&type a) (&type b) result #t
  939. (+ (&min a) (&min b))
  940. (+ (&max a) (&max b))))
  941. (define-type-inferrer/param (add/immediate param a result)
  942. (let ((b-type (type-entry-type (constant-type param))))
  943. (define-binary-result! (&type a) b-type result #t
  944. (+ (&min a) param)
  945. (+ (&max a) param))))
  946. (define-type-inferrer (fadd a b result)
  947. (define! result &f64
  948. (+ (&min a) (&min b))
  949. (+ (&max a) (&max b))))
  950. (define-type-inferrer (uadd a b result)
  951. ;; Handle wraparound.
  952. (let ((max (+ (&max/u64 a) (&max/u64 b))))
  953. (if (<= max &u64-max)
  954. (define! result &u64 (+ (&min/0 a) (&min/0 b)) max)
  955. (define! result &u64 0 &u64-max))))
  956. (define-type-inferrer (sadd a b result)
  957. ;; Handle wraparound.
  958. (let ((min (+ (&min/s64 a) (&min/s64 b)))
  959. (max (+ (&max/s64 a) (&max/s64 b))))
  960. (if (<= &s64-min min max &s64-max)
  961. (define! result &s64 min max)
  962. (define! result &s64 &s64-min &s64-max))))
  963. (define-type-inferrer/param (uadd/immediate param a result)
  964. ;; Handle wraparound.
  965. (let ((max (+ (&max/u64 a) param)))
  966. (if (<= max &u64-max)
  967. (define! result &u64 (+ (&min/0 a) param) max)
  968. (define! result &u64 0 &u64-max))))
  969. (define-type-inferrer/param (sadd/immediate param a result)
  970. ;; Handle wraparound.
  971. (let ((min (+ (&min/s64 a) param))
  972. (max (+ (&max/s64 a) param)))
  973. (if (<= &s64-min min max &s64-max)
  974. (define! result &s64 min max)
  975. (define! result &s64 &s64-min &s64-max))))
  976. (define-simple-type-checker (sub &number &number))
  977. (define-simple-type-checker (sub/immediate &number))
  978. (define-type-checker (fsub a b) #t)
  979. (define-type-checker (usub a b) #t)
  980. (define-type-inferrer (sub a b result)
  981. (define-binary-result! (&type a) (&type b) result #t
  982. (- (&min a) (&max b))
  983. (- (&max a) (&min b))))
  984. (define-type-inferrer/param (sub/immediate param a result)
  985. (let ((b-type (type-entry-type (constant-type param))))
  986. (define-binary-result! (&type a) b-type result #t
  987. (- (&min a) param)
  988. (- (&max a) param))))
  989. (define-type-inferrer (fsub a b result)
  990. (define! result &f64
  991. (- (&min a) (&max b))
  992. (- (&max a) (&min b))))
  993. (define-type-inferrer (usub a b result)
  994. ;; Handle wraparound.
  995. (let ((min (- (&min/0 a) (&max/u64 b))))
  996. (if (< min 0)
  997. (define! result &u64 0 &u64-max)
  998. (define! result &u64 min (- (&max/u64 a) (&min/0 b))))))
  999. (define-type-inferrer/param (usub/immediate param a result)
  1000. ;; Handle wraparound.
  1001. (let ((min (- (&min/0 a) param)))
  1002. (if (< min 0)
  1003. (define! result &u64 0 &u64-max)
  1004. (define! result &u64 min (- (&max/u64 a) param)))))
  1005. (define-simple-type-checker (mul &number &number))
  1006. (define (mul-result-range same? nan-impossible? min-a max-a min-b max-b)
  1007. (define (nan* a b)
  1008. (if (and (or (and (inf? a) (zero? b))
  1009. (and (zero? a) (inf? b)))
  1010. nan-impossible?)
  1011. 0
  1012. (* a b)))
  1013. (let ((-- (nan* min-a min-b))
  1014. (-+ (nan* min-a max-b))
  1015. (++ (nan* max-a max-b))
  1016. (+- (nan* max-a min-b)))
  1017. (let ((has-nan? (or (nan? --) (nan? -+) (nan? ++) (nan? +-))))
  1018. (values (cond
  1019. (same? 0)
  1020. (has-nan? -inf.0)
  1021. (else (min -- -+ ++ +-)))
  1022. (if has-nan?
  1023. +inf.0
  1024. (max -- -+ ++ +-))))))
  1025. (define-type-inferrer (mul a b result)
  1026. (let ((min-a (&min a)) (max-a (&max a))
  1027. (min-b (&min b)) (max-b (&max b))
  1028. ;; We only really get +inf.0 at runtime for flonums and
  1029. ;; compnums. If we have inferred that the arguments are not
  1030. ;; flonums and not compnums, then the result of (* +inf.0 0) at
  1031. ;; range inference time is 0 and not +nan.0.
  1032. (nan-impossible? (not (logtest (logior (&type a) (&type b))
  1033. (logior &flonum &complex)))))
  1034. (call-with-values (lambda ()
  1035. (mul-result-range (eqv? a b) nan-impossible?
  1036. min-a max-a min-b max-b))
  1037. (lambda (min max)
  1038. (define-binary-result! (&type a) (&type b) result #t min max)))))
  1039. (define-type-inferrer (fmul a b result)
  1040. (let ((min-a (&min a)) (max-a (&max a))
  1041. (min-b (&min b)) (max-b (&max b))
  1042. (nan-impossible? #f))
  1043. (call-with-values (lambda ()
  1044. (mul-result-range (eqv? a b) nan-impossible?
  1045. min-a max-a min-b max-b))
  1046. (lambda (min max)
  1047. (define! result &f64 min max)))))
  1048. (define-type-inferrer (umul a b result)
  1049. ;; Handle wraparound.
  1050. (let ((max (* (&max/u64 a) (&max/u64 b))))
  1051. (if (<= max &u64-max)
  1052. (define! result &u64 (* (&min/0 a) (&min/0 b)) max)
  1053. (define! result &u64 0 &u64-max))))
  1054. (define-type-inferrer (smul a b result)
  1055. (call-with-values (lambda ()
  1056. (mul-result-range (eqv? a b) #t
  1057. (&min/s64 a) (&max/s64 a)
  1058. (&min/s64 b) (&max/s64 b)))
  1059. (lambda (min max)
  1060. ;; Handle wraparound.
  1061. (if (<= &s64-min min max &s64-max)
  1062. (define! result &s64 min max)
  1063. (define! result &s64 &s64-min &s64-max)))))
  1064. (define-type-inferrer/param (umul/immediate param a result)
  1065. ;; Handle wraparound.
  1066. (let ((max (* (&max/u64 a) param)))
  1067. (if (<= max &u64-max)
  1068. (define! result &u64 (* (&min/0 a) param) max)
  1069. (define! result &u64 0 &u64-max))))
  1070. (define-type-inferrer/param (smul/immediate param a result)
  1071. (call-with-values (lambda ()
  1072. (mul-result-range #f #t
  1073. (&min/s64 a) (&max/s64 a)
  1074. param param))
  1075. (lambda (min max)
  1076. ;; Handle wraparound.
  1077. (if (<= &s64-min min max &s64-max)
  1078. (define! result &s64 min max)
  1079. (define! result &s64 &s64-min &s64-max)))))
  1080. (define-type-checker (div a b)
  1081. (and (check-type a &number -inf.0 +inf.0)
  1082. (check-type b &number -inf.0 +inf.0)
  1083. ;; We only know that there will not be an exception if b is not
  1084. ;; zero.
  1085. (not (<= (&min b) 0 (&max b)))))
  1086. (define-type-checker (fdiv a b) #t)
  1087. (define (div-result-range min-a max-a min-b max-b)
  1088. (if (<= min-b 0 max-b)
  1089. ;; If the range of the divisor crosses 0, the result spans
  1090. ;; the whole range.
  1091. (values -inf.0 +inf.0)
  1092. ;; Otherwise min-b and max-b have the same sign, and cannot both
  1093. ;; be infinity.
  1094. (let ((--- (if (inf? min-b) 0 (floor/ min-a min-b)))
  1095. (-+- (if (inf? max-b) 0 (floor/ min-a max-b)))
  1096. (++- (if (inf? max-b) 0 (floor/ max-a max-b)))
  1097. (+-- (if (inf? min-b) 0 (floor/ max-a min-b)))
  1098. (--+ (if (inf? min-b) 0 (ceiling/ min-a min-b)))
  1099. (-++ (if (inf? max-b) 0 (ceiling/ min-a max-b)))
  1100. (+++ (if (inf? max-b) 0 (ceiling/ max-a max-b)))
  1101. (+-+ (if (inf? min-b) 0 (ceiling/ max-a min-b))))
  1102. (values (min (min --- -+- ++- +--)
  1103. (min --+ -++ +++ +-+))
  1104. (max (max --- -+- ++- +--)
  1105. (max --+ -++ +++ +-+))))))
  1106. (define-type-inferrer (div a b result)
  1107. (let ((min-a (&min a)) (max-a (&max a))
  1108. (min-b (&min b)) (max-b (&max b)))
  1109. (call-with-values (lambda ()
  1110. (div-result-range min-a max-a min-b max-b))
  1111. (lambda (min max)
  1112. (define-binary-result! (&type a) (&type b) result #f min max)))))
  1113. (define-type-inferrer (fdiv a b result)
  1114. (let ((min-a (&min a)) (max-a (&max a))
  1115. (min-b (&min b)) (max-b (&max b)))
  1116. (call-with-values (lambda ()
  1117. (div-result-range min-a max-a min-b max-b))
  1118. (lambda (min max)
  1119. (define! result &f64 min max)))))
  1120. (define-type-checker (quo a b)
  1121. (and (check-type a &exact-integer -inf.0 +inf.0)
  1122. (check-type b &exact-integer -inf.0 +inf.0)
  1123. ;; We only know that there will not be an exception if b is not
  1124. ;; zero.
  1125. (not (<= (&min b) 0 (&max b)))))
  1126. (define-type-inferrer (quo a b result)
  1127. (restrict! a &exact-integer -inf.0 +inf.0)
  1128. (restrict! b &exact-integer -inf.0 +inf.0)
  1129. (define! result &exact-integer -inf.0 +inf.0))
  1130. (define-type-checker-aliases quo rem)
  1131. (define-type-inferrer (rem a b result)
  1132. (restrict! a &exact-integer -inf.0 +inf.0)
  1133. (restrict! b &exact-integer -inf.0 +inf.0)
  1134. ;; Same sign as A.
  1135. (let ((max-abs-rem (1- (max (abs (&min b)) (abs (&max b))))))
  1136. (cond
  1137. ((< (&min a) 0)
  1138. (define-exact-integer! result
  1139. (- max-abs-rem)
  1140. (if (< 0 (&max a)) max-abs-rem 0)))
  1141. (else
  1142. (define-exact-integer! result 0 max-abs-rem)))))
  1143. (define-type-checker-aliases quo mod)
  1144. (define-type-inferrer (mod a b result)
  1145. (restrict! a &exact-integer -inf.0 +inf.0)
  1146. (restrict! b &exact-integer -inf.0 +inf.0)
  1147. ;; Same sign as B.
  1148. (let ((max-abs-mod (1- (max (abs (&min b)) (abs (&max b))))))
  1149. (cond
  1150. ((< (&min b) 0)
  1151. (define-exact-integer! result
  1152. (- max-abs-mod)
  1153. (if (< 0 (&max b)) max-abs-mod 0)))
  1154. (else
  1155. (define-exact-integer! result 0 max-abs-mod)))))
  1156. ;; Predicates.
  1157. (define-syntax-rule (define-type-predicate-result val result type)
  1158. (cond
  1159. ((zero? (logand (&type val) type))
  1160. (define! result &special-immediate &false &false))
  1161. ((zero? (logand (&type val) (lognot type)))
  1162. (define! result &special-immediate &true &true))
  1163. (else
  1164. (define! result &special-immediate &false &true))))
  1165. (define-simple-type-checker (exact? &number))
  1166. (define-type-inferrer (exact? val result)
  1167. (restrict! val &number -inf.0 +inf.0)
  1168. (define-type-predicate-result val result &exact-number))
  1169. (define-simple-type-checker (inexact? &number))
  1170. (define-type-inferrer (inexact? val result)
  1171. (restrict! val &number -inf.0 +inf.0)
  1172. (define-type-predicate-result val result (logior &flonum &complex)))
  1173. (define-simple-type-checker (inf? &real))
  1174. (define-type-inferrer (inf? val result)
  1175. (restrict! val &real -inf.0 +inf.0)
  1176. (cond
  1177. ((or (zero? (logand (&type val) (logior &flonum &complex)))
  1178. (and (not (inf? (&min val))) (not (inf? (&max val)))))
  1179. (define! result &special-immediate &false &false))
  1180. (else
  1181. (define! result &special-immediate &false &true))))
  1182. (define-type-aliases inf? nan?)
  1183. (define-simple-type (even? &exact-integer)
  1184. (&special-immediate &false &true))
  1185. (define-type-aliases even? odd?)
  1186. ;; Bit operations.
  1187. (define-simple-type-checker (lsh &exact-integer &u64))
  1188. (define-simple-type-checker (rsh &exact-integer &u64))
  1189. (define (compute-ash-range min-val max-val min-shift max-shift)
  1190. (define (ash* val count)
  1191. ;; As we only precisely represent a 64-bit range, don't bother inferring
  1192. ;; shifts that might exceed that range.
  1193. (cond
  1194. ((inf? val) val) ; Preserves sign.
  1195. ((< count 64) (ash val (max count 0)))
  1196. ((zero? val) 0)
  1197. ((positive? val) +inf.0)
  1198. (else -inf.0)))
  1199. (let ((-- (ash* min-val min-shift))
  1200. (-+ (ash* min-val max-shift))
  1201. (++ (ash* max-val max-shift))
  1202. (+- (ash* max-val min-shift)))
  1203. (values (min -- -+ ++ +-) (max -- -+ ++ +-))))
  1204. (define-type-inferrer (lsh val count result)
  1205. (restrict! val &exact-integer -inf.0 +inf.0)
  1206. (let-values (((min max) (compute-ash-range (&min val)
  1207. (&max val)
  1208. (&min/0 count)
  1209. (&max/u64 count))))
  1210. (define-exact-integer! result min max)))
  1211. (define-type-inferrer/param (lsh/immediate count val result)
  1212. (restrict! val &exact-integer -inf.0 +inf.0)
  1213. (let-values (((min max) (compute-ash-range (&min val)
  1214. (&max val)
  1215. count count)))
  1216. (define-exact-integer! result min max)))
  1217. (define-type-inferrer (rsh val count result)
  1218. (restrict! val &exact-integer -inf.0 +inf.0)
  1219. (let-values (((min max) (compute-ash-range (&min val)
  1220. (&max val)
  1221. (- (&min/0 count))
  1222. (- (&max/u64 count)))))
  1223. (define-exact-integer! result min max)))
  1224. (define-type-inferrer/param (rsh/immediate count val result)
  1225. (restrict! val &exact-integer -inf.0 +inf.0)
  1226. (let-values (((min max) (compute-ash-range (&min val)
  1227. (&max val)
  1228. (- count) (- count))))
  1229. (define-exact-integer! result min max)))
  1230. (define-type-inferrer (ursh a b result)
  1231. (define! result &u64
  1232. (ash (&min/0 a) (- (min 63 (&max/u64 b))))
  1233. (ash (&max/u64 a) (- (min 63 (&min/0 b))))))
  1234. (define-type-inferrer/param (ursh/immediate param a result)
  1235. (define! result &u64
  1236. (ash (&min/0 a) (- param))
  1237. (ash (&max/u64 a) (- param))))
  1238. (define-type-inferrer (srsh a b result)
  1239. (let-values (((min max) (compute-ash-range (&min/s64 a)
  1240. (&max/s64 a)
  1241. (- (min 63 (&min/0 b)))
  1242. (- (min 63 (&max/u64 b))))))
  1243. (if (<= &s64-min min max &s64-max)
  1244. (define! result &s64 min max)
  1245. (define! result &s64 &s64-min &s64-max))))
  1246. (define-type-inferrer/param (srsh/immediate count val result)
  1247. (let-values (((min max) (compute-ash-range (&min/s64 val)
  1248. (&max/s64 val)
  1249. (- count) (- count))))
  1250. (if (<= &s64-min min max &s64-max)
  1251. (define! result &s64 min max)
  1252. (define! result &s64 &s64-min &s64-max))))
  1253. (define-type-inferrer (ulsh a b result)
  1254. (if (<= (ash (&max/u64 a) (&max/u64 b)) &u64-max)
  1255. ;; No overflow; we can be precise.
  1256. (define! result &u64
  1257. (ash (&min/0 a) (&min/0 b))
  1258. (ash (&max/u64 a) (&max/u64 b)))
  1259. ;; Otherwise assume the whole range.
  1260. (define! result &u64 0 &u64-max)))
  1261. (define-type-inferrer/param (ulsh/immediate param a result)
  1262. (if (<= (ash (&max/u64 a) param) &u64-max)
  1263. ;; No overflow; we can be precise.
  1264. (define! result &u64
  1265. (ash (&min/0 a) param)
  1266. (ash (&max/u64 a) param))
  1267. ;; Otherwise assume the whole range.
  1268. (define! result &u64 0 &u64-max)))
  1269. (define-type-inferrer (slsh a b result)
  1270. (let-values (((min max) (compute-ash-range (&min a) (&max a)
  1271. (min 63 (&min/0 b))
  1272. (min 63 (&max/u64 b)))))
  1273. (if (<= &s64-min min max &s64-max)
  1274. (define! result &s64 min max)
  1275. (define! result &s64 &s64-min &s64-max))))
  1276. (define-type-inferrer/param (slsh/immediate param a result)
  1277. (let-values (((min max) (compute-ash-range (&min a) (&max a)
  1278. param param)))
  1279. (if (<= &s64-min min max &s64-max)
  1280. (define! result &s64 min max)
  1281. (define! result &s64 &s64-min &s64-max))))
  1282. (define-inlinable (non-negative? n)
  1283. "Return true if N is non-negative, otherwise return false."
  1284. (not (negative? n)))
  1285. ;; Like 'lognot', but handles infinities.
  1286. (define-inlinable (lognot* n)
  1287. "Return the bitwise complement of N. If N is infinite, return -N."
  1288. (- -1 n))
  1289. (define saturate+
  1290. (case-lambda
  1291. "Let N be the least upper bound of the integer lengths of the
  1292. arguments. Return the greatest integer whose integer length is N.
  1293. If any of the arguments are infinite, return positive infinity."
  1294. ((a b)
  1295. (if (or (inf? a) (inf? b))
  1296. +inf.0
  1297. (1- (ash 1 (max (integer-length a)
  1298. (integer-length b))))))
  1299. ((a b c)
  1300. (saturate+ (saturate+ a b) c))
  1301. ((a b c d)
  1302. (saturate+ (saturate+ a b) c d))))
  1303. (define saturate-
  1304. (case-lambda
  1305. "Let N be the least upper bound of the integer lengths of the
  1306. arguments. Return the least integer whose integer length is N.
  1307. If any of the arguments are infinite, return negative infinity."
  1308. ((a b) (lognot* (saturate+ a b)))
  1309. ((a b c) (lognot* (saturate+ a b c)))
  1310. ((a b c d) (lognot* (saturate+ a b c d)))))
  1311. (define (logand-bounds a0 a1 b0 b1)
  1312. "Return two values: lower and upper bounds for (logand A B)
  1313. where (A0 <= A <= A1) and (B0 <= B <= B1)."
  1314. ;; For each argument, we consider three cases: (1) the argument is
  1315. ;; non-negative, (2) its sign is unknown, or (3) it is negative.
  1316. ;; To handle both arguments, we must consider a total of 9 cases:
  1317. ;;
  1318. ;; -----------------------------------------------------------------------
  1319. ;; LOGAND | non-negative B | unknown-sign B | negative B
  1320. ;; -----------------------------------------------------------------------
  1321. ;; non-negative A | 0 .. (min A1 B1) | 0 .. A1 | 0 .. A1
  1322. ;; -----------------------------------------------------------------------
  1323. ;; unknown-sign A | 0 .. B1 | (sat- A0 B0) | (sat- A0 B0)
  1324. ;; | | .. | .. A1
  1325. ;; | | (sat+ A1 B1) |
  1326. ;; -----------------------------------------------------------------------
  1327. ;; negative A | 0 .. B1 | (sat- A0 B0) | (sat- A0 B0)
  1328. ;; | | .. B1 | .. (min A1 B1)
  1329. ;; -----------------------------------------------------------------------
  1330. (values (if (or (non-negative? a0) (non-negative? b0))
  1331. 0
  1332. (saturate- a0 b0))
  1333. (cond ((or (and (non-negative? a0) (non-negative? b0))
  1334. (and (negative? a1) (negative? b1)))
  1335. (min a1 b1))
  1336. ((or (non-negative? a0) (negative? b1))
  1337. a1)
  1338. ((or (non-negative? b0) (negative? a1))
  1339. b1)
  1340. (else
  1341. (saturate+ a1 b1)))))
  1342. (define-simple-type-checker (logand &exact-integer &exact-integer))
  1343. (define-type-inferrer (logand a b result)
  1344. (restrict! a &exact-integer -inf.0 +inf.0)
  1345. (restrict! b &exact-integer -inf.0 +inf.0)
  1346. (call-with-values (lambda ()
  1347. (logand-bounds (&min a) (&max a) (&min b) (&max b)))
  1348. (lambda (min max)
  1349. (define-exact-integer! result min max))))
  1350. (define-type-inferrer (ulogand a b result)
  1351. (restrict! a &u64 0 &u64-max)
  1352. (restrict! b &u64 0 &u64-max)
  1353. (define! result &u64 0 (min (&max/u64 a) (&max/u64 b))))
  1354. (define (logsub-bounds a0 a1 b0 b1)
  1355. "Return two values: lower and upper bounds for (logsub A B),
  1356. i.e. (logand A (lognot B)), where (A0 <= A <= A1) and (B0 <= B <= B1)."
  1357. ;; Here we use 'logand-bounds' to compute the bounds, after
  1358. ;; computing the bounds of (lognot B) from the bounds of B.
  1359. ;; From (B0 <= B <= B1) it follows that (~B1 <= ~B <= ~B0),
  1360. ;; where ~X means (lognot X).
  1361. (logand-bounds a0 a1 (lognot* b1) (lognot* b0)))
  1362. (define-simple-type-checker (logsub &exact-integer &exact-integer))
  1363. (define-type-inferrer (logsub a b result)
  1364. (restrict! a &exact-integer -inf.0 +inf.0)
  1365. (restrict! b &exact-integer -inf.0 +inf.0)
  1366. (call-with-values (lambda ()
  1367. (logsub-bounds (&min a) (&max a) (&min b) (&max b)))
  1368. (lambda (min max)
  1369. (define-exact-integer! result min max))))
  1370. (define-type-inferrer (ulogsub a b result)
  1371. (restrict! a &u64 0 &u64-max)
  1372. (restrict! b &u64 0 &u64-max)
  1373. (define! result &u64 0 (&max/u64 a)))
  1374. (define (logior-bounds a0 a1 b0 b1)
  1375. "Return two values: lower and upper bounds for (logior A B)
  1376. where (A0 <= A <= A1) and (B0 <= B <= B1)."
  1377. ;; For each argument, we consider three cases: (1) the argument is
  1378. ;; non-negative, (2) its sign is unknown, or (3) it is negative.
  1379. ;; To handle both arguments, we must consider a total of 9 cases.
  1380. ;;
  1381. ;; ---------------------------------------------------------------------
  1382. ;; LOGIOR | non-negative B | unknown-sign B | negative B
  1383. ;; ---------------------------------------------------------------------
  1384. ;; non-negative A | (max A0 B0) | B0 | B0 .. -1
  1385. ;; | .. | .. |
  1386. ;; | (sat+ A1 B1) | (sat+ A1 B1) |
  1387. ;; ---------------------------------------------------------------------
  1388. ;; unknown-sign A | A0 | (sat- A0 B0) | B0 .. -1
  1389. ;; | .. | .. |
  1390. ;; | (sat+ A1 B1) | (sat+ A1 B1) |
  1391. ;; ---------------------------------------------------------------------
  1392. ;; negative A | A0 .. -1 | A0 .. -1 | (max A0 B0) .. -1
  1393. ;; ---------------------------------------------------------------------
  1394. (values (cond ((or (and (non-negative? a0) (non-negative? b0))
  1395. (and (negative? a1) (negative? b1)))
  1396. (max a0 b0))
  1397. ((or (non-negative? a0) (negative? b1))
  1398. b0)
  1399. ((or (non-negative? b0) (negative? a1))
  1400. a0)
  1401. (else
  1402. (saturate- a0 b0)))
  1403. (if (or (negative? a1) (negative? b1))
  1404. -1
  1405. (saturate+ a1 b1))))
  1406. (define-simple-type-checker (logior &exact-integer &exact-integer))
  1407. (define-type-inferrer (logior a b result)
  1408. (restrict! a &exact-integer -inf.0 +inf.0)
  1409. (restrict! b &exact-integer -inf.0 +inf.0)
  1410. (call-with-values (lambda ()
  1411. (logior-bounds (&min a) (&max a) (&min b) (&max b)))
  1412. (lambda (min max)
  1413. (define-exact-integer! result min max))))
  1414. (define-type-inferrer (ulogior a b result)
  1415. (restrict! a &u64 0 &u64-max)
  1416. (restrict! b &u64 0 &u64-max)
  1417. (define! result &u64
  1418. (max (&min/0 a) (&min/0 b))
  1419. (saturate+ (&max/u64 a) (&max/u64 b))))
  1420. (define (logxor-bounds a0 a1 b0 b1)
  1421. "Return two values: lower and upper bounds for (logxor A B)
  1422. where (A0 <= A <= A1) and (B0 <= B <= B1)."
  1423. ;; For each argument, we consider three cases: (1) the argument is
  1424. ;; non-negative, (2) its sign is unknown, or (3) it is negative.
  1425. ;; To handle both arguments, we must consider a total of 9 cases.
  1426. ;;
  1427. ;; --------------------------------------------------------------------
  1428. ;; LOGXOR | non-negative B | unknown-sign B | negative B
  1429. ;; --------------------------------------------------------------------
  1430. ;; non-negative A | 0 | (sat- A1 B0) | (sat- A1 B0)
  1431. ;; | .. | .. | ..
  1432. ;; | (sat+ A1 B1) | (sat+ A1 B1) | -1
  1433. ;; --------------------------------------------------------------------
  1434. ;; unknown-sign A | (sat- A0 B1) | (sat- A0 B1 A1 B0) | (sat- A1 B0)
  1435. ;; | .. | .. | ..
  1436. ;; | (sat+ A1 B1) | (sat+ A1 B1 A0 B0) | (sat+ A0 B0)
  1437. ;; --------------------------------------------------------------------
  1438. ;; negative A | (sat- A0 B1) | (sat- A0 B1) | 0
  1439. ;; | .. | .. | ..
  1440. ;; | -1 | (sat+ A0 B0) | (sat+ A0 B0)
  1441. ;; --------------------------------------------------------------------
  1442. (values (cond ((or (and (non-negative? a0) (non-negative? b0))
  1443. (and (negative? a1) (negative? b1)))
  1444. 0)
  1445. ((or (non-negative? a0) (negative? b1))
  1446. (saturate- a1 b0))
  1447. ((or (non-negative? b0) (negative? a1))
  1448. (saturate- a0 b1))
  1449. (else
  1450. (saturate- a0 b1 a1 b0)))
  1451. (cond ((or (and (non-negative? a0) (negative? b1))
  1452. (and (non-negative? b0) (negative? a1)))
  1453. -1)
  1454. ((or (non-negative? a0) (non-negative? b0))
  1455. (saturate+ a1 b1))
  1456. ((or (negative? a1) (negative? b1))
  1457. (saturate+ a0 b0))
  1458. (else
  1459. (saturate+ a1 b1 a0 b0)))))
  1460. (define-simple-type-checker (logxor &exact-integer &exact-integer))
  1461. (define-type-inferrer (logxor a b result)
  1462. (restrict! a &exact-integer -inf.0 +inf.0)
  1463. (restrict! b &exact-integer -inf.0 +inf.0)
  1464. (call-with-values (lambda ()
  1465. (logxor-bounds (&min a) (&max a) (&min b) (&max b)))
  1466. (lambda (min max)
  1467. (define! result &exact-integer min max))))
  1468. (define-type-inferrer (ulogxor a b result)
  1469. (restrict! a &u64 0 &u64-max)
  1470. (restrict! b &u64 0 &u64-max)
  1471. (define! result &u64 0 (saturate+ (&max/u64 a) (&max/u64 b))))
  1472. (define-simple-type-checker (lognot &exact-integer))
  1473. (define-type-inferrer (lognot a result)
  1474. (restrict! a &exact-integer -inf.0 +inf.0)
  1475. (define-exact-integer! result
  1476. (lognot* (&max a))
  1477. (lognot* (&min a))))
  1478. (define-simple-type-checker (logtest &exact-integer &exact-integer))
  1479. (define-type-inferrer (logtest a b result)
  1480. (restrict! a &exact-integer -inf.0 +inf.0)
  1481. (restrict! b &exact-integer -inf.0 +inf.0)
  1482. (define! result &special-immediate &false &true))
  1483. (define-simple-type-checker (logbit? (&exact-integer 0 +inf.0) &exact-integer))
  1484. (define-type-inferrer (logbit? a b result)
  1485. (let ((a-min (&min a))
  1486. (a-max (&max a))
  1487. (b-min (&min b))
  1488. (b-max (&max b)))
  1489. (if (and (eqv? a-min a-max) (>= a-min 0) (not (inf? a-min))
  1490. (eqv? b-min b-max) (>= b-min 0) (not (inf? b-min)))
  1491. (let ((bool (if (logbit? a-min b-min) &true &false)))
  1492. (define! result &special-immediate bool bool))
  1493. (define! result &special-immediate &false &true))))
  1494. ;; Flonums.
  1495. (define-simple-type-checker (sqrt &number))
  1496. (define-type-inferrer (sqrt x result)
  1497. (let ((type (&type x)))
  1498. (cond
  1499. ((and (zero? (logand type &complex))
  1500. (non-negative? (&min x)))
  1501. (define! result
  1502. (logior type &flonum)
  1503. (exact-integer-sqrt (&min x))
  1504. (if (inf? (&max x))
  1505. +inf.0
  1506. (call-with-values (lambda () (exact-integer-sqrt (&max x)))
  1507. (lambda (s r)
  1508. (if (zero? r) s (+ s 1)))))))
  1509. (else
  1510. (define! result (logior type &flonum &complex) -inf.0 +inf.0)))))
  1511. (define-type-checker (fsqrt x) #t)
  1512. (define-type-inferrer (fsqrt x result)
  1513. (define! result
  1514. &f64
  1515. (exact-integer-sqrt (max (&min x) 0))
  1516. (if (inf? (&max x))
  1517. +inf.0
  1518. (call-with-values (lambda () (exact-integer-sqrt (&max x)))
  1519. (lambda (s r)
  1520. (if (zero? r) s (+ s 1)))))))
  1521. (define-simple-type-checker (abs &real))
  1522. (define-type-inferrer (abs x result)
  1523. (let ((type (&type x)))
  1524. (cond
  1525. ((type<=? type &exact-integer)
  1526. (if (< (&min x) 0)
  1527. (define-exact-integer! result 0 (max (abs (&min x)) (abs (&max x))))
  1528. (define! result type (&min x) (&max x))))
  1529. (else
  1530. (when (type<=? type &number)
  1531. (restrict! x &real -inf.0 +inf.0))
  1532. (let* ((min (if (< (&min x) 0) 0 (&min x)))
  1533. (max (max (abs (&min x)) (abs (&max x))))
  1534. (type (cond
  1535. ((not (logtest type &exact-integer)) type)
  1536. ((< (target-most-positive-fixnum) min)
  1537. (logior &bignum (logand type (lognot &fixnum))))
  1538. ((<= max (target-most-positive-fixnum))
  1539. (logior &fixnum (logand type (lognot &bignum))))
  1540. (else (logior type &fixnum &bignum)))))
  1541. (define! result (logior (logand type (lognot &number))
  1542. (logand type &real))
  1543. min max))))))
  1544. (define-type-checker (fabs x) #t)
  1545. (define-type-inferrer (fabs x result)
  1546. (let ((min (if (< (&min x) 0) 0 (&min x)))
  1547. (max (max (abs (&min x)) (abs (&max x)))))
  1548. (define! result &f64 min max)))
  1549. (define-simple-type-checker (floor &real))
  1550. (define-type-inferrer (floor x result)
  1551. (restrict! x &real -inf.0 +inf.0)
  1552. (let* ((in (logand (&type x) &real))
  1553. (out (cond
  1554. ((type<=? in &flonum) &flonum)
  1555. ((type<=? in &exact-integer) in)
  1556. ((logtest in &fraction)
  1557. (logior (logand in (lognot &fraction)) &exact-integer)))))
  1558. (define! result out (&min x) (&max x))))
  1559. (define-type-checker (ffloor x) #t)
  1560. (define-type-inferrer (ffloor x result)
  1561. (define! result &f64 (&min x) (&max x)))
  1562. (define-type-aliases floor ceiling)
  1563. (define-type-aliases ffloor fceiling)
  1564. (define-simple-type-checker (sin &number))
  1565. (define-type-inferrer (sin x result)
  1566. (let* ((in (&type x))
  1567. (out (cond
  1568. ((type<=? in &real) &flonum)
  1569. ((type<=? in &complex) &complex)
  1570. (else (logior &flonum &complex (logand in (lognot &number)))))))
  1571. (define! result out -1 1)))
  1572. (define-type-checker (fsin x) #t)
  1573. (define-type-inferrer (fsin x result)
  1574. (define! result &f64 -1 1))
  1575. (define-type-aliases sin cos)
  1576. (define-type-aliases fsin fcos)
  1577. (define-simple-type-checker (tan &number))
  1578. (define-type-inferrer (tan x result)
  1579. (let* ((in (&type x))
  1580. (out (cond
  1581. ((type<=? in &real) &flonum)
  1582. ((type<=? in &complex) &complex)
  1583. (else (logior &flonum &complex (logand in (lognot &number)))))))
  1584. (define! result out -inf.0 +inf.0)))
  1585. (define-type-checker (ftan x) #t)
  1586. (define-type-inferrer (ftan x result)
  1587. (define! result &f64 -inf.0 +inf.0))
  1588. (define-simple-type-checker (asin &number))
  1589. (define-type-inferrer (asin x result)
  1590. (define! result
  1591. (logior &flonum &complex (logand (&type x) (lognot &number)))
  1592. -inf.0 +inf.0))
  1593. (define-type-checker (fasin x) #t)
  1594. (define-type-inferrer (fasin x result)
  1595. (define! result &f64 -2 2)) ; [-pi/2, pi/2]
  1596. (define-type-aliases asin acos)
  1597. (define-type-checker (facos x) #t)
  1598. (define-type-inferrer (facos x result)
  1599. (define! result &f64 0 4)) ; [0, pi]
  1600. (define-simple-type-checker (atan &number))
  1601. (define-type-inferrer (atan x result)
  1602. (let ((in (&type x)))
  1603. (cond
  1604. ((type<=? in &real)
  1605. (define! result &flonum -2 2)) ; [-pi/2, pi/2]
  1606. (else
  1607. (define! result
  1608. (logior &flonum &complex (logand in (lognot &number)))
  1609. -inf.0 +inf.0)))))
  1610. (define-type-checker (fatan x) #t)
  1611. (define-type-inferrer (fatan x result)
  1612. (define! result &f64 -2 2))
  1613. (define-simple-type-checker (atan2 &number &number))
  1614. (define-type-inferrer (atan2 x y result)
  1615. (let* ((in (logior (&type x) (&type y))))
  1616. (cond
  1617. ((type<=? in &real)
  1618. (define! result &flonum -4 4)) ; [-pi, pi]
  1619. (else
  1620. (define! result (logior &flonum &complex (logand in (lognot &number)))
  1621. -inf.0 +inf.0)))))
  1622. (define-type-checker (fatan2 x y) #t)
  1623. (define-type-inferrer (fatan2 x y result)
  1624. (define! result &f64 -4 4))
  1625. ;;;
  1626. ;;; Characters.
  1627. ;;;
  1628. (define-type-inferrer (untag-char c result)
  1629. (define! result &s64 0 (min (&max c) *max-codepoint*)))
  1630. (define-type-inferrer (tag-char u64 result)
  1631. (define! result &char 0 (min (&max u64) *max-codepoint*)))
  1632. ;;;
  1633. ;;; Type flow analysis: the meet (ahem) of the algorithm.
  1634. ;;;
  1635. (define (successor-count cont)
  1636. (match cont
  1637. (($ $kargs _ _ ($ $throw)) 0)
  1638. (($ $kargs _ _ ($ $continue)) 1)
  1639. (($ $kargs _ _ (or ($ $branch) ($ $prompt))) 2)
  1640. (($ $kfun src meta self tail clause) (if clause 1 0))
  1641. (($ $kclause arity body alt) (if alt 2 1))
  1642. (($ $kreceive) 1)
  1643. (($ $ktail) 0)))
  1644. (define (intset-pop set)
  1645. (match (intset-next set)
  1646. (#f (values set #f))
  1647. (i (values (intset-remove set i) i))))
  1648. (define-syntax-rule (make-worklist-folder* seed ...)
  1649. (lambda (f worklist seed ...)
  1650. (let lp ((worklist worklist) (seed seed) ...)
  1651. (call-with-values (lambda () (intset-pop worklist))
  1652. (lambda (worklist i)
  1653. (if i
  1654. (call-with-values (lambda () (f i seed ...))
  1655. (lambda (i* seed ...)
  1656. (let add ((i* i*) (worklist worklist))
  1657. (match i*
  1658. (() (lp worklist seed ...))
  1659. ((i . i*) (add i* (intset-add worklist i)))))))
  1660. (values seed ...)))))))
  1661. (define worklist-fold*
  1662. (case-lambda
  1663. ((f worklist seed)
  1664. ((make-worklist-folder* seed) f worklist seed))))
  1665. (define intmap-ensure
  1666. (let* ((*absent* (list 'absent))
  1667. (not-found (lambda (i) *absent*)))
  1668. (lambda (map i ensure)
  1669. (let ((val (intmap-ref map i not-found)))
  1670. (if (eq? val *absent*)
  1671. (let ((val (ensure i)))
  1672. (values (intmap-add map i val) val))
  1673. (values map val))))))
  1674. ;; For best results, the labels in the function starting should be
  1675. ;; topologically sorted (renumbered). Otherwise the backward branch
  1676. ;; detection mentioned in the module commentary will trigger for
  1677. ;; ordinary forward branches.
  1678. (define (infer-types conts kfun)
  1679. "Compute types for all variables bound in the function labelled
  1680. @var{kfun}, from @var{conts}. Returns an intmap mapping labels to type
  1681. entries.
  1682. A type entry is a vector that describes the types of the values that
  1683. flow into and out of a labelled expression. The first slot in the type
  1684. entry vector corresponds to the types that flow in, and the rest of the
  1685. slots correspond to the types that flow out. Each element of the type
  1686. entry vector is an intmap mapping variable name to the variable's
  1687. inferred type. An inferred type is a 3-vector of type, minimum, and
  1688. maximum, where type is a bitset as a fixnum."
  1689. (define (get-entry typev label) (intmap-ref typev label))
  1690. (define (entry-not-found label)
  1691. (make-vector (1+ (successor-count (intmap-ref conts label))) #f))
  1692. (define (ensure-entry typev label)
  1693. (intmap-ensure typev label entry-not-found))
  1694. (define (compute-initial-state)
  1695. (let ((entry (entry-not-found kfun)))
  1696. ;; Nothing flows in to the first label.
  1697. (vector-set! entry 0 empty-intmap)
  1698. (intmap-add empty-intmap kfun entry)))
  1699. (define (adjoin-vars types vars entry)
  1700. (match vars
  1701. (() types)
  1702. ((var . vars)
  1703. (adjoin-vars (adjoin-var types var entry) vars entry))))
  1704. (define (infer-primcall types succ name param args result)
  1705. (cond
  1706. ((hashq-ref *type-inferrers* name)
  1707. => (lambda (inferrer)
  1708. ;; FIXME: remove the apply?
  1709. ;; (pk 'primcall name args result)
  1710. (apply inferrer types succ param
  1711. (if result
  1712. (append args (list result))
  1713. args))))
  1714. (result
  1715. (adjoin-var types result all-types-entry))
  1716. (else
  1717. types)))
  1718. (define (vector-replace vec idx val)
  1719. (let ((vec (vector-copy vec)))
  1720. (vector-set! vec idx val)
  1721. vec))
  1722. (define (update-out-types label typev types succ-idx)
  1723. (let* ((entry (get-entry typev label))
  1724. (old-types (vector-ref entry (1+ succ-idx))))
  1725. (if (eq? types old-types)
  1726. (values typev #f)
  1727. (let ((entry (vector-replace entry (1+ succ-idx) types))
  1728. (first? (not old-types)))
  1729. (values (intmap-replace typev label entry) first?)))))
  1730. (define (update-in-types label typev types saturate?)
  1731. (let*-values (((typev entry) (ensure-entry typev label))
  1732. ((old-types) (vector-ref entry 0))
  1733. ;; TODO: If the label has only one predecessor, we can
  1734. ;; avoid the meet.
  1735. ((types) (if (not old-types)
  1736. types
  1737. (let ((meet (if saturate?
  1738. type-entry-saturating-union
  1739. type-entry-union)))
  1740. (intmap-intersect old-types types meet)))))
  1741. (if (eq? old-types types)
  1742. (values typev #f)
  1743. (let ((entry (vector-replace entry 0 types)))
  1744. (values (intmap-replace typev label entry) #t)))))
  1745. (define (propagate-types label typev succ-idx succ-label types)
  1746. (let*-values
  1747. (((typev first?) (update-out-types label typev types succ-idx))
  1748. ((saturate?) (and (not first?) (<= succ-label label)))
  1749. ((typev changed?) (update-in-types succ-label typev types saturate?)))
  1750. (values (if changed? (list succ-label) '()) typev)))
  1751. (define (visit-exp label typev k types exp)
  1752. (define (propagate1 succ-label types)
  1753. (propagate-types label typev 0 succ-label types))
  1754. (define (propagate2 succ0-label types0 succ1-label types1)
  1755. (let*-values (((changed0 typev)
  1756. (propagate-types label typev 0 succ0-label types0))
  1757. ((changed1 typev)
  1758. (propagate-types label typev 1 succ1-label types1)))
  1759. (values (append changed0 changed1) typev)))
  1760. ;; Each of these branches must propagate to its successors.
  1761. (match exp
  1762. (($ $primcall name param args)
  1763. (propagate1 k
  1764. (match (intmap-ref conts k)
  1765. (($ $kargs _ defs)
  1766. (infer-primcall types 0 name param args
  1767. (match defs ((var) var) (() #f))))
  1768. (_
  1769. ;; (pk 'warning-no-restrictions name)
  1770. types))))
  1771. (($ $values args)
  1772. (match (intmap-ref conts k)
  1773. (($ $kargs _ defs)
  1774. (let ((in types))
  1775. (let lp ((defs defs) (args args) (out types))
  1776. (match (cons defs args)
  1777. ((() . ())
  1778. (propagate1 k out))
  1779. (((def . defs) . (arg . args))
  1780. (lp defs args
  1781. (adjoin-var out def (var-type-entry in arg))))))))
  1782. (_
  1783. (propagate1 k types))))
  1784. ((or ($ $call) ($ $callk))
  1785. (propagate1 k types))
  1786. (($ $rec names vars funs)
  1787. (let ((proc-type (make-type-entry &procedure -inf.0 +inf.0)))
  1788. (propagate1 k (adjoin-vars types vars proc-type))))
  1789. (_
  1790. (match (intmap-ref conts k)
  1791. (($ $kargs (_) (var))
  1792. (let ((entry (match exp
  1793. (($ $const val)
  1794. (constant-type val))
  1795. ((or ($ $prim) ($ $fun) ($ $const-fun) ($ $code))
  1796. ;; Could be more precise here.
  1797. (make-type-entry &procedure -inf.0 +inf.0)))))
  1798. (propagate1 k (adjoin-var types var entry))))))))
  1799. (define (visit-cont label typev)
  1800. (let ((types (vector-ref (intmap-ref typev label) 0)))
  1801. (define (propagate0)
  1802. (values '() typev))
  1803. (define (propagate1 succ-label types)
  1804. (propagate-types label typev 0 succ-label types))
  1805. (define (propagate2 succ0-label types0 succ1-label types1)
  1806. (let*-values (((changed0 typev)
  1807. (propagate-types label typev 0 succ0-label types0))
  1808. ((changed1 typev)
  1809. (propagate-types label typev 1 succ1-label types1)))
  1810. (values (append changed0 changed1) typev)))
  1811. ;; Add types for new definitions, and restrict types of
  1812. ;; existing variables due to side effects.
  1813. (match (intmap-ref conts label)
  1814. (($ $kargs names vars ($ $continue k src exp))
  1815. (visit-exp label typev k types exp))
  1816. (($ $kargs names vars ($ $branch kf kt src op param args))
  1817. ;; The "normal" continuation is the #f branch.
  1818. (propagate2 kf (infer-primcall types 0 op param args #f)
  1819. kt (infer-primcall types 1 op param args #f)))
  1820. (($ $kargs names vars ($ $prompt k kh src escape? tag))
  1821. ;; The "normal" continuation enters the prompt.
  1822. (propagate2 k types kh types))
  1823. (($ $kargs names vars ($ $throw))
  1824. (propagate0))
  1825. (($ $kreceive arity k)
  1826. (match (intmap-ref conts k)
  1827. (($ $kargs names vars)
  1828. (propagate1 k (adjoin-vars types vars all-types-entry)))))
  1829. (($ $kfun src meta self tail clause)
  1830. (if clause
  1831. (propagate1 clause (if self
  1832. (adjoin-var types self all-types-entry)
  1833. types))
  1834. (propagate0)))
  1835. (($ $kclause arity kbody kalt)
  1836. (match (intmap-ref conts kbody)
  1837. (($ $kargs _ defs)
  1838. (let ((body-types (adjoin-vars types defs all-types-entry)))
  1839. (if kalt
  1840. (propagate2 kbody body-types kalt types)
  1841. (propagate1 kbody body-types))))))
  1842. (($ $ktail) (propagate0)))))
  1843. (worklist-fold* visit-cont
  1844. (intset-add empty-intset kfun)
  1845. (compute-initial-state)))
  1846. (define (lookup-pre-type types label def)
  1847. (let* ((entry (intmap-ref types label))
  1848. (tentry (var-type-entry (vector-ref entry 0) def)))
  1849. (values (type-entry-type tentry)
  1850. (type-entry-min tentry)
  1851. (type-entry-max tentry))))
  1852. (define (lookup-post-type types label def succ-idx)
  1853. (let* ((entry (intmap-ref types label))
  1854. (tentry (var-type-entry (vector-ref entry (1+ succ-idx)) def)))
  1855. (values (type-entry-type tentry)
  1856. (type-entry-min tentry)
  1857. (type-entry-max tentry))))
  1858. (define (primcall-types-check? types label name param args)
  1859. (match (hashq-ref *type-checkers* name)
  1860. (#f #f)
  1861. (checker
  1862. (let ((entry (intmap-ref types label)))
  1863. (apply checker (vector-ref entry 0) param args)))))