types.scm 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. ;;; Type analysis on CPS
  2. ;;; Copyright (C) 2014, 2015 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. #:export (;; Specific types.
  86. &exact-integer
  87. &flonum
  88. &complex
  89. &fraction
  90. &char
  91. &unspecified
  92. &unbound
  93. &false
  94. &true
  95. &nil
  96. &null
  97. &symbol
  98. &keyword
  99. &procedure
  100. &pointer
  101. &fluid
  102. &pair
  103. &vector
  104. &box
  105. &struct
  106. &string
  107. &bytevector
  108. &bitvector
  109. &array
  110. &hash-table
  111. ;; Union types.
  112. &number &real
  113. ;; Untagged types.
  114. &f64
  115. &u64
  116. &s64
  117. infer-types
  118. lookup-pre-type
  119. lookup-post-type
  120. primcall-types-check?))
  121. (define-syntax define-flags
  122. (lambda (x)
  123. (syntax-case x ()
  124. ((_ all shift name ...)
  125. (let ((count (length #'(name ...))))
  126. (with-syntax (((n ...) (iota count))
  127. (count count))
  128. #'(begin
  129. (define-syntax name (identifier-syntax (ash 1 n)))
  130. ...
  131. (define-syntax all (identifier-syntax (1- (ash 1 count))))
  132. (define-syntax shift (identifier-syntax count)))))))))
  133. ;; More precise types have fewer bits.
  134. (define-flags &all-types &type-bits
  135. &exact-integer
  136. &flonum
  137. &complex
  138. &fraction
  139. &char
  140. &unspecified
  141. &unbound
  142. &false
  143. &true
  144. &nil
  145. &null
  146. &symbol
  147. &keyword
  148. &procedure
  149. &pointer
  150. &fluid
  151. &pair
  152. &vector
  153. &box
  154. &struct
  155. &string
  156. &bytevector
  157. &bitvector
  158. &array
  159. &hash-table
  160. &f64
  161. &u64
  162. &s64)
  163. (define-syntax &no-type (identifier-syntax 0))
  164. (define-syntax &number
  165. (identifier-syntax (logior &exact-integer &flonum &complex &fraction)))
  166. (define-syntax &real
  167. (identifier-syntax (logior &exact-integer &flonum &fraction)))
  168. ;; Versions of min and max that do not coerce exact numbers to become
  169. ;; inexact.
  170. (define min
  171. (case-lambda
  172. ((a b) (if (< a b) a b))
  173. ((a b c) (min (min a b) c))
  174. ((a b c d) (min (min a b) c d))))
  175. (define max
  176. (case-lambda
  177. ((a b) (if (> a b) a b))
  178. ((a b c) (max (max a b) c))
  179. ((a b c d) (max (max a b) c d))))
  180. (define-syntax-rule (define-compile-time-value name val)
  181. (define-syntax name
  182. (make-variable-transformer
  183. (lambda (x)
  184. (syntax-case x (set!)
  185. (var (identifier? #'var)
  186. (datum->syntax #'var val)))))))
  187. (define-compile-time-value &s64-min (- #x8000000000000000))
  188. (define-compile-time-value &s64-max #x7fffFFFFffffFFFF)
  189. (define-compile-time-value &u64-max #xffffFFFFffffFFFF)
  190. (define-syntax &range-min (identifier-syntax &s64-min))
  191. (define-syntax &range-max (identifier-syntax &u64-max))
  192. ;; This is a hack that takes advantage of knowing that
  193. ;; most-positive-fixnum is the size of a word, but with two tag bits and
  194. ;; one sign bit. We also assume that the current common architectural
  195. ;; restriction of a maximum 48-bit address space means that we won't see
  196. ;; a size_t value above 2^48.
  197. (define *max-size-t*
  198. (min (+ (ash most-positive-fixnum 3) #b111)
  199. (1- (ash 1 48))))
  200. (define *max-codepoint* #x10ffff)
  201. (define-inlinable (make-unclamped-type-entry type min max)
  202. (vector type min max))
  203. (define-inlinable (type-entry-type tentry)
  204. (vector-ref tentry 0))
  205. (define-inlinable (type-entry-min tentry)
  206. (vector-ref tentry 1))
  207. (define-inlinable (type-entry-max tentry)
  208. (vector-ref tentry 2))
  209. (define-inlinable (clamp-min val)
  210. (cond
  211. ;; Fast path to avoid comparisons with bignums.
  212. ((<= most-negative-fixnum val most-positive-fixnum) val)
  213. ((< val &range-min) -inf.0)
  214. ((< &range-max val) &range-max)
  215. (else val)))
  216. (define-inlinable (clamp-max val)
  217. (cond
  218. ;; Fast path to avoid comparisons with bignums.
  219. ((<= most-negative-fixnum val most-positive-fixnum) val)
  220. ((< &range-max val) +inf.0)
  221. ((< val &range-min) &range-min)
  222. (else val)))
  223. (define-inlinable (make-type-entry type min max)
  224. (vector type (clamp-min min) (clamp-max max)))
  225. (define all-types-entry (make-type-entry &all-types -inf.0 +inf.0))
  226. (define* (var-type-entry typeset var #:optional (default all-types-entry))
  227. (intmap-ref typeset var (lambda (_) default)))
  228. (define (var-type typeset var)
  229. (type-entry-type (var-type-entry typeset var)))
  230. (define (var-min typeset var)
  231. (type-entry-min (var-type-entry typeset var)))
  232. (define (var-max typeset var)
  233. (type-entry-max (var-type-entry typeset var)))
  234. ;; Is the type entry A contained entirely within B?
  235. (define (type-entry<=? a b)
  236. (match (cons a b)
  237. ((#(a-type a-min a-max) . #(b-type b-min b-max))
  238. (and (eqv? b-type (logior a-type b-type))
  239. (<= b-min a-min)
  240. (>= b-max a-max)))))
  241. (define (type-entry-union a b)
  242. (cond
  243. ((type-entry<=? b a) a)
  244. ((type-entry<=? a b) b)
  245. (else (make-type-entry
  246. (logior (type-entry-type a) (type-entry-type b))
  247. (min (type-entry-min a) (type-entry-min b))
  248. (max (type-entry-max a) (type-entry-max b))))))
  249. (define (type-entry-saturating-union a b)
  250. (cond
  251. ((type-entry<=? b a) a)
  252. (else
  253. (make-type-entry
  254. (logior (type-entry-type a) (type-entry-type b))
  255. (let ((a-min (type-entry-min a))
  256. (b-min (type-entry-min b)))
  257. (cond
  258. ((not (< b-min a-min)) a-min)
  259. ((< 0 b-min) 0)
  260. ((< &range-min b-min) &range-min)
  261. (else -inf.0)))
  262. (let ((a-max (type-entry-max a))
  263. (b-max (type-entry-max b)))
  264. (cond
  265. ((not (> b-max a-max)) a-max)
  266. ((> *max-size-t* b-max) *max-size-t*)
  267. ((> &range-max b-max) &range-max)
  268. (else +inf.0)))))))
  269. (define (type-entry-intersection a b)
  270. (cond
  271. ((type-entry<=? a b) a)
  272. ((type-entry<=? b a) b)
  273. (else (make-type-entry
  274. (logand (type-entry-type a) (type-entry-type b))
  275. (max (type-entry-min a) (type-entry-min b))
  276. (min (type-entry-max a) (type-entry-max b))))))
  277. (define (adjoin-var typeset var entry)
  278. (intmap-add typeset var entry type-entry-union))
  279. (define (restrict-var typeset var entry)
  280. (intmap-add typeset var entry type-entry-intersection))
  281. (define (constant-type val)
  282. "Compute the type and range of VAL. Return three values: the type,
  283. minimum, and maximum."
  284. (define (return type val)
  285. (if val
  286. (make-type-entry type val val)
  287. (make-type-entry type -inf.0 +inf.0)))
  288. (cond
  289. ((number? val)
  290. (cond
  291. ((exact-integer? val) (return &exact-integer val))
  292. ((eqv? (imag-part val) 0)
  293. (if (nan? val)
  294. (make-type-entry &flonum -inf.0 +inf.0)
  295. (make-type-entry
  296. (if (exact? val) &fraction &flonum)
  297. (if (rational? val) (inexact->exact (floor val)) val)
  298. (if (rational? val) (inexact->exact (ceiling val)) val))))
  299. (else (return &complex #f))))
  300. ((eq? val '()) (return &null #f))
  301. ((eq? val #nil) (return &nil #f))
  302. ((eq? val #t) (return &true #f))
  303. ((eq? val #f) (return &false #f))
  304. ((char? val) (return &char (char->integer val)))
  305. ((eqv? val *unspecified*) (return &unspecified #f))
  306. ((symbol? val) (return &symbol #f))
  307. ((keyword? val) (return &keyword #f))
  308. ((pair? val) (return &pair #f))
  309. ((vector? val) (return &vector (vector-length val)))
  310. ((string? val) (return &string (string-length val)))
  311. ((bytevector? val) (return &bytevector (bytevector-length val)))
  312. ((bitvector? val) (return &bitvector (bitvector-length val)))
  313. ((array? val) (return &array (array-rank val)))
  314. ((not (variable-bound? (make-variable val))) (return &unbound #f))
  315. (else (error "unhandled constant" val))))
  316. (define *type-checkers* (make-hash-table))
  317. (define *type-inferrers* (make-hash-table))
  318. (define-syntax-rule (define-type-helper name)
  319. (define-syntax-parameter name
  320. (lambda (stx)
  321. (syntax-violation 'name
  322. "macro used outside of define-type"
  323. stx))))
  324. (define-type-helper define!)
  325. (define-type-helper restrict!)
  326. (define-type-helper &type)
  327. (define-type-helper &min)
  328. (define-type-helper &max)
  329. ;; Accessors to use in type inferrers where you know that the values
  330. ;; must be in some range for the computation to proceed (not throw an
  331. ;; error). Note that these accessors should be used even for &u64 and
  332. ;; &s64 values, whose definitions you would think would be apparent
  333. ;; already. However it could be that the graph isn't sorted, so we see
  334. ;; a use before a definition, in which case we need to clamp the generic
  335. ;; limits to the &u64/&s64 range.
  336. (define-syntax-rule (&min/0 x) (max (&min x) 0))
  337. (define-syntax-rule (&max/u64 x) (min (&max x) &u64-max))
  338. (define-syntax-rule (&min/s64 x) (max (&min x) &s64-min))
  339. (define-syntax-rule (&max/s64 x) (min (&max x) &s64-max))
  340. (define-syntax-rule (&max/size x) (min (&max x) *max-size-t*))
  341. (define-syntax-rule (define-type-checker (name arg ...) body ...)
  342. (hashq-set!
  343. *type-checkers*
  344. 'name
  345. (lambda (typeset arg ...)
  346. (syntax-parameterize
  347. ((&type (syntax-rules () ((_ val) (var-type typeset val))))
  348. (&min (syntax-rules () ((_ val) (var-min typeset val))))
  349. (&max (syntax-rules () ((_ val) (var-max typeset val)))))
  350. body ...))))
  351. (define-syntax-rule (check-type arg type min max)
  352. ;; If the arg is negative, it is a closure variable.
  353. (and (>= arg 0)
  354. (zero? (logand (lognot type) (&type arg)))
  355. (<= min (&min arg))
  356. (<= (&max arg) max)))
  357. (define-syntax-rule (define-type-inferrer* (name succ var ...) body ...)
  358. (hashq-set!
  359. *type-inferrers*
  360. 'name
  361. (lambda (in succ var ...)
  362. (let ((out in))
  363. (syntax-parameterize
  364. ((define!
  365. (syntax-rules ()
  366. ((_ val type min max)
  367. (set! out (adjoin-var out val
  368. (make-type-entry type min max))))))
  369. (restrict!
  370. (syntax-rules ()
  371. ((_ val type min max)
  372. (set! out (restrict-var out val
  373. (make-type-entry type min max))))))
  374. (&type (syntax-rules () ((_ val) (var-type in val))))
  375. (&min (syntax-rules () ((_ val) (var-min in val))))
  376. (&max (syntax-rules () ((_ val) (var-max in val)))))
  377. body ...
  378. out)))))
  379. (define-syntax-rule (define-type-inferrer (name arg ...) body ...)
  380. (define-type-inferrer* (name succ arg ...) body ...))
  381. (define-syntax-rule (define-predicate-inferrer (name arg ... true?) body ...)
  382. (define-type-inferrer* (name succ arg ...)
  383. (let ((true? (not (zero? succ))))
  384. body ...)))
  385. (define-syntax define-simple-type-checker
  386. (lambda (x)
  387. (define (parse-spec l)
  388. (syntax-case l ()
  389. (() '())
  390. (((type min max) . l) (cons #'(type min max) (parse-spec #'l)))
  391. (((type min+max) . l) (cons #'(type min+max min+max) (parse-spec #'l)))
  392. ((type . l) (cons #'(type -inf.0 +inf.0) (parse-spec #'l)))))
  393. (syntax-case x ()
  394. ((_ (name arg-spec ...) result-spec ...)
  395. (with-syntax
  396. (((arg ...) (generate-temporaries #'(arg-spec ...)))
  397. (((arg-type arg-min arg-max) ...) (parse-spec #'(arg-spec ...))))
  398. #'(define-type-checker (name arg ...)
  399. (and (check-type arg arg-type arg-min arg-max)
  400. ...)))))))
  401. (define-syntax define-simple-type-inferrer
  402. (lambda (x)
  403. (define (parse-spec l)
  404. (syntax-case l ()
  405. (() '())
  406. (((type min max) . l) (cons #'(type min max) (parse-spec #'l)))
  407. (((type min+max) . l) (cons #'(type min+max min+max) (parse-spec #'l)))
  408. ((type . l) (cons #'(type -inf.0 +inf.0) (parse-spec #'l)))))
  409. (syntax-case x ()
  410. ((_ (name arg-spec ...) result-spec ...)
  411. (with-syntax
  412. (((arg ...) (generate-temporaries #'(arg-spec ...)))
  413. (((arg-type arg-min arg-max) ...) (parse-spec #'(arg-spec ...)))
  414. ((res ...) (generate-temporaries #'(result-spec ...)))
  415. (((res-type res-min res-max) ...) (parse-spec #'(result-spec ...))))
  416. #'(define-type-inferrer (name arg ... res ...)
  417. (restrict! arg arg-type arg-min arg-max)
  418. ...
  419. (define! res res-type res-min res-max)
  420. ...))))))
  421. (define-syntax-rule (define-simple-type (name arg-spec ...) result-spec ...)
  422. (begin
  423. (define-simple-type-checker (name arg-spec ...))
  424. (define-simple-type-inferrer (name arg-spec ...) result-spec ...)))
  425. (define-syntax-rule (define-simple-types
  426. ((name arg-spec ...) result-spec ...)
  427. ...)
  428. (begin
  429. (define-simple-type (name arg-spec ...) result-spec ...)
  430. ...))
  431. (define-syntax-rule (define-type-checker-aliases orig alias ...)
  432. (let ((check (hashq-ref *type-checkers* 'orig)))
  433. (hashq-set! *type-checkers* 'alias check)
  434. ...))
  435. (define-syntax-rule (define-type-inferrer-aliases orig alias ...)
  436. (let ((check (hashq-ref *type-inferrers* 'orig)))
  437. (hashq-set! *type-inferrers* 'alias check)
  438. ...))
  439. (define-syntax-rule (define-type-aliases orig alias ...)
  440. (begin
  441. (define-type-checker-aliases orig alias ...)
  442. (define-type-inferrer-aliases orig alias ...)))
  443. ;;; This list of primcall type definitions follows the order of
  444. ;;; effects-analysis.scm; please keep it in a similar order.
  445. ;;;
  446. ;;; There is no need to add checker definitions for expressions that do
  447. ;;; not exhibit the &type-check effect, as callers should not ask if
  448. ;;; such an expression does or does not type-check. For those that do
  449. ;;; exhibit &type-check, you should define a type inferrer unless the
  450. ;;; primcall will never typecheck.
  451. ;;;
  452. ;;; Likewise there is no need to define inferrers for primcalls which
  453. ;;; return &all-types values and which never raise exceptions from which
  454. ;;; we can infer the types of incoming values.
  455. ;;;
  456. ;;; Generic effect-free predicates.
  457. ;;;
  458. (define-predicate-inferrer (eq? a b true?)
  459. ;; We can only propagate information down the true leg.
  460. (when true?
  461. (let ((type (logand (&type a) (&type b)))
  462. (min (max (&min a) (&min b)))
  463. (max (min (&max a) (&max b))))
  464. (restrict! a type min max)
  465. (restrict! b type min max))))
  466. (define-type-inferrer-aliases eq? eqv?)
  467. (define-syntax-rule (define-simple-predicate-inferrer predicate type)
  468. (define-predicate-inferrer (predicate val true?)
  469. (let ((type (if true?
  470. type
  471. (logand (&type val) (lognot type)))))
  472. (restrict! val type -inf.0 +inf.0))))
  473. (define-simple-predicate-inferrer pair? &pair)
  474. (define-simple-predicate-inferrer null? &null)
  475. (define-simple-predicate-inferrer nil? &nil)
  476. (define-simple-predicate-inferrer symbol? &symbol)
  477. (define-simple-predicate-inferrer variable? &box)
  478. (define-simple-predicate-inferrer vector? &vector)
  479. (define-simple-predicate-inferrer struct? &struct)
  480. (define-simple-predicate-inferrer string? &string)
  481. (define-simple-predicate-inferrer bytevector? &bytevector)
  482. (define-simple-predicate-inferrer bitvector? &bitvector)
  483. (define-simple-predicate-inferrer keyword? &keyword)
  484. (define-simple-predicate-inferrer number? &number)
  485. (define-simple-predicate-inferrer char? &char)
  486. (define-simple-predicate-inferrer procedure? &procedure)
  487. (define-simple-predicate-inferrer thunk? &procedure)
  488. ;;;
  489. ;;; Fluids. Note that we can't track bound-ness of fluids, as pop-fluid
  490. ;;; can change boundness.
  491. ;;;
  492. (define-simple-types
  493. ((fluid-ref (&fluid 1)) &all-types)
  494. ((fluid-set! (&fluid 0 1) &all-types))
  495. ((push-fluid (&fluid 0 1) &all-types))
  496. ((pop-fluid)))
  497. ;;;
  498. ;;; Threads. We don't currently track threads as an object type.
  499. ;;;
  500. (define-simple-types
  501. ((current-thread) &all-types))
  502. ;;;
  503. ;;; Prompts. (Nothing to do.)
  504. ;;;
  505. ;;;
  506. ;;; Pairs.
  507. ;;;
  508. (define-simple-types
  509. ((cons &all-types &all-types) &pair)
  510. ((car &pair) &all-types)
  511. ((set-car! &pair &all-types))
  512. ((cdr &pair) &all-types)
  513. ((set-cdr! &pair &all-types)))
  514. ;;;
  515. ;;; Variables.
  516. ;;;
  517. (define-simple-types
  518. ((box &all-types) (&box 1))
  519. ((box-ref (&box 1)) &all-types))
  520. (define-simple-type-checker (box-set! (&box 0 1) &all-types))
  521. (define-type-inferrer (box-set! box val)
  522. (restrict! box &box 1 1))
  523. ;;;
  524. ;;; Vectors.
  525. ;;;
  526. ;; This max-vector-len computation is a hack.
  527. (define *max-vector-len* (ash most-positive-fixnum -5))
  528. (define-syntax-rule (&max/vector x) (min (&max x) *max-vector-len*))
  529. (define-simple-type-checker (make-vector (&u64 0 *max-vector-len*)
  530. &all-types))
  531. (define-type-inferrer (make-vector size init result)
  532. (restrict! size &u64 0 *max-vector-len*)
  533. (define! result &vector (&min/0 size) (&max/vector size)))
  534. (define-type-checker (vector-ref v idx)
  535. (and (check-type v &vector 0 *max-vector-len*)
  536. (check-type idx &u64 0 (1- (&min v)))))
  537. (define-type-inferrer (vector-ref v idx result)
  538. (restrict! v &vector (1+ (&min/0 idx)) *max-vector-len*)
  539. (restrict! idx &u64 0 (1- (&max/vector v)))
  540. (define! result &all-types -inf.0 +inf.0))
  541. (define-type-checker (vector-set! v idx val)
  542. (and (check-type v &vector 0 *max-vector-len*)
  543. (check-type idx &u64 0 (1- (&min v)))))
  544. (define-type-inferrer (vector-set! v idx val)
  545. (restrict! v &vector (1+ (&min/0 idx)) *max-vector-len*)
  546. (restrict! idx &u64 0 (1- (&max/vector v))))
  547. (define-type-aliases make-vector make-vector/immediate)
  548. (define-type-aliases vector-ref vector-ref/immediate)
  549. (define-type-aliases vector-set! vector-set!/immediate)
  550. (define-simple-type-checker (vector-length &vector))
  551. (define-type-inferrer (vector-length v result)
  552. (restrict! v &vector 0 *max-vector-len*)
  553. (define! result &u64 (&min/0 v) (&max/vector v)))
  554. ;;;
  555. ;;; Structs.
  556. ;;;
  557. ;; No type-checker for allocate-struct, as we can't currently check that
  558. ;; vt is actually a vtable.
  559. (define-type-inferrer (allocate-struct vt size result)
  560. (restrict! vt &struct vtable-offset-user *max-size-t*)
  561. (restrict! size &u64 0 *max-size-t*)
  562. (define! result &struct (&min/0 size) (&max/size size)))
  563. (define-type-checker (struct-ref s idx)
  564. (and (check-type s &struct 0 *max-size-t*)
  565. (check-type idx &u64 0 *max-size-t*)
  566. ;; FIXME: is the field readable?
  567. (< (&max idx) (&min s))))
  568. (define-type-inferrer (struct-ref s idx result)
  569. (restrict! s &struct (1+ (&min/0 idx)) *max-size-t*)
  570. (restrict! idx &u64 0 (1- (&max/size s)))
  571. (define! result &all-types -inf.0 +inf.0))
  572. (define-type-checker (struct-set! s idx val)
  573. (and (check-type s &struct 0 *max-size-t*)
  574. (check-type idx &u64 0 *max-size-t*)
  575. ;; FIXME: is the field writable?
  576. (< (&max idx) (&min s))))
  577. (define-type-inferrer (struct-set! s idx val)
  578. (restrict! s &struct (1+ (&min/0 idx)) *max-size-t*)
  579. (restrict! idx &u64 0 (1- (&max/size s))))
  580. (define-type-aliases allocate-struct allocate-struct/immediate)
  581. (define-type-aliases struct-ref struct-ref/immediate)
  582. (define-type-aliases struct-set! struct-set!/immediate)
  583. (define-simple-type (struct-vtable (&struct 0 *max-size-t*))
  584. (&struct vtable-offset-user *max-size-t*))
  585. ;;;
  586. ;;; Strings.
  587. ;;;
  588. (define-type-checker (string-ref s idx)
  589. (and (check-type s &string 0 *max-size-t*)
  590. (check-type idx &u64 0 *max-size-t*)
  591. (< (&max idx) (&min s))))
  592. (define-type-inferrer (string-ref s idx result)
  593. (restrict! s &string (1+ (&min/0 idx)) *max-size-t*)
  594. (restrict! idx &u64 0 (1- (&max/size s)))
  595. (define! result &char 0 *max-codepoint*))
  596. (define-type-checker (string-set! s idx val)
  597. (and (check-type s &string 0 *max-size-t*)
  598. (check-type idx &exact-integer 0 *max-size-t*)
  599. (check-type val &char 0 *max-codepoint*)
  600. (< (&max idx) (&min s))))
  601. (define-type-inferrer (string-set! s idx val)
  602. (restrict! s &string (1+ (&min/0 idx)) *max-size-t*)
  603. (restrict! idx &exact-integer 0 (1- (&max/size s)))
  604. (restrict! val &char 0 *max-codepoint*))
  605. (define-simple-type-checker (string-length &string))
  606. (define-type-inferrer (string-length s result)
  607. (restrict! s &string 0 *max-size-t*)
  608. (define! result &u64 (&min/0 s) (&max/size s)))
  609. (define-simple-type (number->string &number) (&string 0 *max-size-t*))
  610. (define-simple-type (string->number (&string 0 *max-size-t*))
  611. ((logior &number &false) -inf.0 +inf.0))
  612. ;;;
  613. ;;; Unboxed numbers.
  614. ;;;
  615. (define-type-checker (scm->f64 scm)
  616. (check-type scm &real -inf.0 +inf.0))
  617. (define-type-inferrer (scm->f64 scm result)
  618. (restrict! scm &real -inf.0 +inf.0)
  619. (define! result &f64 (&min scm) (&max scm)))
  620. (define-type-aliases scm->f64 load-f64)
  621. (define-type-checker (f64->scm f64)
  622. #t)
  623. (define-type-inferrer (f64->scm f64 result)
  624. (define! result &flonum (&min f64) (&max f64)))
  625. (define-type-checker (scm->u64 scm)
  626. (check-type scm &exact-integer 0 &u64-max))
  627. (define-type-inferrer (scm->u64 scm result)
  628. (restrict! scm &exact-integer 0 &u64-max)
  629. (define! result &u64 (&min/0 scm) (&max/u64 scm)))
  630. (define-type-aliases scm->u64 load-u64)
  631. (define-type-checker (scm->u64/truncate scm)
  632. (check-type scm &exact-integer &range-min &range-max))
  633. (define-type-inferrer (scm->u64/truncate scm result)
  634. (restrict! scm &exact-integer &range-min &range-max)
  635. (define! result &u64 0 &u64-max))
  636. (define-type-checker (u64->scm u64)
  637. #t)
  638. (define-type-inferrer (u64->scm u64 result)
  639. (define! result &exact-integer (&min/0 u64) (&max/u64 u64)))
  640. (define-type-checker (scm->s64 scm)
  641. (check-type scm &exact-integer &s64-min &s64-max))
  642. (define-type-inferrer (scm->s64 scm result)
  643. (restrict! scm &exact-integer &s64-min &s64-max)
  644. (define! result &s64 (&min/s64 scm) (&max/s64 scm)))
  645. (define-type-aliases scm->s64 load-s64)
  646. (define-type-checker (s64->scm s64)
  647. #t)
  648. (define-type-inferrer (s64->scm s64 result)
  649. (define! result &exact-integer (&min/s64 s64) (&max/s64 s64)))
  650. ;;;
  651. ;;; Bytevectors.
  652. ;;;
  653. (define-simple-type-checker (bv-length &bytevector))
  654. (define-type-inferrer (bv-length bv result)
  655. (restrict! bv &bytevector 0 *max-size-t*)
  656. (define! result &u64 (&min/0 bv) (&max/size bv)))
  657. (define-syntax-rule (define-bytevector-accessors ref set type size lo hi)
  658. (begin
  659. (define-type-checker (ref bv idx)
  660. (and (check-type bv &bytevector 0 *max-size-t*)
  661. (check-type idx &u64 0 *max-size-t*)
  662. (< (&max idx) (- (&min bv) size))))
  663. (define-type-inferrer (ref bv idx result)
  664. (restrict! bv &bytevector (+ (&min/0 idx) size) *max-size-t*)
  665. (restrict! idx &u64 0 (- (&max/size bv) size))
  666. (define! result type lo hi))
  667. (define-type-checker (set bv idx val)
  668. (and (check-type bv &bytevector 0 *max-size-t*)
  669. (check-type idx &u64 0 *max-size-t*)
  670. (check-type val type lo hi)
  671. (< (&max idx) (- (&min bv) size))))
  672. (define-type-inferrer (set! bv idx val)
  673. (restrict! bv &bytevector (+ (&min/0 idx) size) *max-size-t*)
  674. (restrict! idx &u64 0 (- (&max/size bv) size))
  675. (restrict! val type lo hi))))
  676. (define-bytevector-accessors bv-u8-ref bv-u8-set! &u64 1 0 #xff)
  677. (define-bytevector-accessors bv-s8-ref bv-s8-set! &s64 1 (- #x80) #x7f)
  678. (define-bytevector-accessors bv-u16-ref bv-u16-set! &u64 2 0 #xffff)
  679. (define-bytevector-accessors bv-s16-ref bv-s16-set! &s64 2 (- #x8000) #x7fff)
  680. (define-bytevector-accessors bv-u32-ref bv-u32-set! &u64 4 0 #xffffffff)
  681. (define-bytevector-accessors bv-s32-ref bv-s32-set! &s64 4
  682. (- #x80000000) #x7fffffff)
  683. (define-bytevector-accessors bv-u64-ref bv-u64-set! &u64 8 0 &u64-max)
  684. (define-bytevector-accessors bv-s64-ref bv-s64-set! &s64 8 &s64-min &s64-max)
  685. (define-bytevector-accessors bv-f32-ref bv-f32-set! &f64 4 -inf.0 +inf.0)
  686. (define-bytevector-accessors bv-f64-ref bv-f64-set! &f64 8 -inf.0 +inf.0)
  687. ;;;
  688. ;;; Numbers.
  689. ;;;
  690. ;; First, branching primitives with no results.
  691. (define-simple-type-checker (= &number &number))
  692. (define-predicate-inferrer (= a b true?)
  693. (when (and true?
  694. (zero? (logand (logior (&type a) (&type b)) (lognot &number))))
  695. (let ((min (max (&min a) (&min b)))
  696. (max (min (&max a) (&max b))))
  697. (restrict! a &number min max)
  698. (restrict! b &number min max))))
  699. (define (restricted-comparison-ranges op type0 min0 max0 type1 min1 max1)
  700. (define (infer-integer-ranges)
  701. (match op
  702. ('< (values min0 (min max0 (1- max1)) (max (1+ min0) min1) max1))
  703. ('<= (values min0 (min max0 max1) (max min0 min1) max1))
  704. ('>= (values (max min0 min1) max0 min1 (min max0 max1)))
  705. ('> (values (max min0 (1+ min1)) max0 min1 (min (1- max0) max1)))))
  706. (define (infer-real-ranges)
  707. (match op
  708. ((or '< '<=) (values min0 (min max0 max1) (max min0 min1) max1))
  709. ((or '> '>=) (values (max min0 min1) max0 min1 (min max0 max1)))))
  710. (if (= (logior type0 type1) &exact-integer)
  711. (infer-integer-ranges)
  712. (infer-real-ranges)))
  713. (define-syntax-rule (true-comparison-restrictions op a b a-type b-type)
  714. (call-with-values
  715. (lambda ()
  716. (restricted-comparison-ranges op
  717. (&type a) (&min a) (&max a)
  718. (&type b) (&min b) (&max b)))
  719. (lambda (min0 max0 min1 max1)
  720. (restrict! a a-type min0 max0)
  721. (restrict! b b-type min1 max1))))
  722. (define-syntax-rule (define-comparison-inferrer (op inverse))
  723. (define-predicate-inferrer (op a b true?)
  724. (when (zero? (logand (logior (&type a) (&type b)) (lognot &number)))
  725. (true-comparison-restrictions (if true? 'op 'inverse) a b &real &real))))
  726. (define-simple-type-checker (< &real &real))
  727. (define-comparison-inferrer (< >=))
  728. (define-simple-type-checker (<= &real &real))
  729. (define-comparison-inferrer (<= >))
  730. (define-simple-type-checker (>= &real &real))
  731. (define-comparison-inferrer (>= <))
  732. (define-simple-type-checker (> &real &real))
  733. (define-comparison-inferrer (> <=))
  734. (define-simple-type-checker (u64-= &u64 &u64))
  735. (define-predicate-inferrer (u64-= a b true?)
  736. (when true?
  737. (let ((min (max (&min/0 a) (&min/0 b)))
  738. (max (min (&max/u64 a) (&max/u64 b))))
  739. (restrict! a &u64 min max)
  740. (restrict! b &u64 min max))))
  741. (define-simple-type-checker (u64-=-scm &u64 &real))
  742. (define-predicate-inferrer (u64-=-scm a b true?)
  743. (when (and true? (zero? (logand (&type b) (lognot &real))))
  744. (let ((min (max (&min/0 a) (&min/0 b)))
  745. (max (min (&max/u64 a) (&max/u64 b))))
  746. (restrict! a &u64 min max)
  747. (restrict! b &real min max))))
  748. (define-simple-type-checker (u64-<-scm &u64 &real))
  749. (define-predicate-inferrer (u64-<-scm a b true?)
  750. (when (and true? (zero? (logand (&type b) (lognot &real))))
  751. (true-comparison-restrictions '< a b &u64 &real)))
  752. (define-simple-type-checker (u64-<=-scm &u64 &real))
  753. (define-predicate-inferrer (u64-<=-scm a b true?)
  754. (when (and true? (zero? (logand (&type b) (lognot &real))))
  755. (true-comparison-restrictions '<= a b &u64 &real)))
  756. (define-simple-type-checker (u64->=-scm &u64 &real))
  757. (define-predicate-inferrer (u64->=-scm a b true?)
  758. (when (and true? (zero? (logand (&type b) (lognot &real))))
  759. (true-comparison-restrictions '>= a b &u64 &real)))
  760. (define-simple-type-checker (u64->-scm &u64 &real))
  761. (define-predicate-inferrer (u64->-scm a b true?)
  762. (when (and true? (zero? (logand (&type b) (lognot &real))))
  763. (true-comparison-restrictions '> a b &u64 &real)))
  764. (define (infer-u64-comparison-ranges op min0 max0 min1 max1)
  765. (match op
  766. ('< (values min0 (min max0 (1- max1)) (max (1+ min0) min1) max1))
  767. ('<= (values min0 (min max0 max1) (max min0 min1) max1))
  768. ('>= (values (max min0 min1) max0 min1 (min max0 max1)))
  769. ('> (values (max min0 (1+ min1)) max0 min1 (min (1- max0) max1)))))
  770. (define-syntax-rule (define-u64-comparison-inferrer (u64-op op inverse))
  771. (define-predicate-inferrer (u64-op a b true?)
  772. (call-with-values
  773. (lambda ()
  774. (infer-u64-comparison-ranges (if true? 'op 'inverse)
  775. (&min/0 a) (&max/u64 a)
  776. (&min/0 b) (&max/u64 b)))
  777. (lambda (min0 max0 min1 max1)
  778. (restrict! a &u64 min0 max0)
  779. (restrict! b &u64 min1 max1)))))
  780. (define-simple-type-checker (u64-< &u64 &u64))
  781. (define-u64-comparison-inferrer (u64-< < >=))
  782. (define-simple-type-checker (u64-<= &u64 &u64))
  783. (define-u64-comparison-inferrer (u64-<= <= >))
  784. (define-simple-type-checker (u64->= &u64 &u64))
  785. (define-u64-comparison-inferrer (u64-<= >= <))
  786. (define-simple-type-checker (u64-> &u64 &u64))
  787. (define-u64-comparison-inferrer (u64-> > <=))
  788. ;; Arithmetic.
  789. (define-syntax-rule (define-unary-result! a result min max)
  790. (let ((min* min)
  791. (max* max)
  792. (type (logand (&type a) &number)))
  793. (cond
  794. ((not (= type (&type a)))
  795. ;; Not a number. Punt and do nothing.
  796. (define! result &all-types -inf.0 +inf.0))
  797. ;; Complex numbers don't have a range.
  798. ((eqv? type &complex)
  799. (define! result &complex -inf.0 +inf.0))
  800. (else
  801. (define! result type min* max*)))))
  802. (define-syntax-rule (define-binary-result! a b result closed? min max)
  803. (let ((min* min)
  804. (max* max)
  805. (a-type (logand (&type a) &number))
  806. (b-type (logand (&type b) &number)))
  807. (cond
  808. ((or (not (= a-type (&type a))) (not (= b-type (&type b))))
  809. ;; One input not a number. Perhaps we end up dispatching to
  810. ;; GOOPS.
  811. (define! result &all-types -inf.0 +inf.0))
  812. ;; Complex and floating-point numbers are contagious.
  813. ((or (eqv? a-type &complex) (eqv? b-type &complex))
  814. (define! result &complex -inf.0 +inf.0))
  815. ((or (eqv? a-type &flonum) (eqv? b-type &flonum))
  816. (define! result &flonum min* max*))
  817. ;; Exact integers are closed under some operations.
  818. ((and closed? (eqv? a-type &exact-integer) (eqv? b-type &exact-integer))
  819. (define! result &exact-integer min* max*))
  820. (else
  821. (let* ((type (logior a-type b-type))
  822. ;; Fractions may become integers.
  823. (type (if (zero? (logand type &fraction))
  824. type
  825. (logior type &exact-integer)))
  826. ;; Integers may become fractions under division.
  827. (type (if (or closed?
  828. (zero? (logand type (logior &exact-integer))))
  829. type
  830. (logior type &fraction))))
  831. (define! result type min* max*))))))
  832. (define-simple-type-checker (add &number &number))
  833. (define-type-aliases add add/immediate)
  834. (define-type-checker (fadd a b) #t)
  835. (define-type-checker (uadd a b) #t)
  836. (define-type-inferrer (add a b result)
  837. (define-binary-result! a b result #t
  838. (+ (&min a) (&min b))
  839. (+ (&max a) (&max b))))
  840. (define-type-inferrer (fadd a b result)
  841. (define! result &f64
  842. (+ (&min a) (&min b))
  843. (+ (&max a) (&max b))))
  844. (define-type-inferrer (uadd a b result)
  845. ;; Handle wraparound.
  846. (let ((max (+ (&max/u64 a) (&max/u64 b))))
  847. (if (<= max &u64-max)
  848. (define! result &u64 (+ (&min/0 a) (&min/0 b)) max)
  849. (define! result &u64 0 &u64-max))))
  850. (define-type-aliases uadd uadd/immediate)
  851. (define-simple-type-checker (sub &number &number))
  852. (define-type-aliases sub sub/immediate)
  853. (define-type-checker (fsub a b) #t)
  854. (define-type-checker (usub a b) #t)
  855. (define-type-inferrer (sub a b result)
  856. (define-binary-result! a b result #t
  857. (- (&min a) (&max b))
  858. (- (&max a) (&min b))))
  859. (define-type-inferrer (fsub a b result)
  860. (define! result &f64
  861. (- (&min a) (&max b))
  862. (- (&max a) (&min b))))
  863. (define-type-inferrer (usub a b result)
  864. ;; Handle wraparound.
  865. (let ((min (- (&min/0 a) (&max/u64 b))))
  866. (if (< min 0)
  867. (define! result &u64 0 &u64-max)
  868. (define! result &u64 min (- (&max/u64 a) (&min/0 b))))))
  869. (define-type-aliases usub usub/immediate)
  870. (define-simple-type-checker (mul &number &number))
  871. (define-type-checker (fmul a b) #t)
  872. (define-type-checker (umul a b) #t)
  873. (define (mul-result-range same? nan-impossible? min-a max-a min-b max-b)
  874. (define (nan* a b)
  875. (if (and (or (and (inf? a) (zero? b))
  876. (and (zero? a) (inf? b)))
  877. nan-impossible?)
  878. 0
  879. (* a b)))
  880. (let ((-- (nan* min-a min-b))
  881. (-+ (nan* min-a max-b))
  882. (++ (nan* max-a max-b))
  883. (+- (nan* max-a min-b)))
  884. (let ((has-nan? (or (nan? --) (nan? -+) (nan? ++) (nan? +-))))
  885. (values (cond
  886. (same? 0)
  887. (has-nan? -inf.0)
  888. (else (min -- -+ ++ +-)))
  889. (if has-nan?
  890. +inf.0
  891. (max -- -+ ++ +-))))))
  892. (define-type-inferrer (mul a b result)
  893. (let ((min-a (&min a)) (max-a (&max a))
  894. (min-b (&min b)) (max-b (&max b))
  895. ;; We only really get +inf.0 at runtime for flonums and
  896. ;; compnums. If we have inferred that the arguments are not
  897. ;; flonums and not compnums, then the result of (* +inf.0 0) at
  898. ;; range inference time is 0 and not +nan.0.
  899. (nan-impossible? (not (logtest (logior (&type a) (&type b))
  900. (logior &flonum &complex)))))
  901. (call-with-values (lambda ()
  902. (mul-result-range (eqv? a b) nan-impossible?
  903. min-a max-a min-b max-b))
  904. (lambda (min max)
  905. (define-binary-result! a b result #t min max)))))
  906. (define-type-inferrer (fmul a b result)
  907. (let ((min-a (&min a)) (max-a (&max a))
  908. (min-b (&min b)) (max-b (&max b))
  909. (nan-impossible? #f))
  910. (call-with-values (lambda ()
  911. (mul-result-range (eqv? a b) nan-impossible?
  912. min-a max-a min-b max-b))
  913. (lambda (min max)
  914. (define! result &f64 min max)))))
  915. (define-type-inferrer (umul a b result)
  916. ;; Handle wraparound.
  917. (let ((max (* (&max/u64 a) (&max/u64 b))))
  918. (if (<= max &u64-max)
  919. (define! result &u64 (* (&min/0 a) (&min/0 b)) max)
  920. (define! result &u64 0 &u64-max))))
  921. (define-type-aliases umul umul/immediate)
  922. (define-type-checker (div a b)
  923. (and (check-type a &number -inf.0 +inf.0)
  924. (check-type b &number -inf.0 +inf.0)
  925. ;; We only know that there will not be an exception if b is not
  926. ;; zero.
  927. (not (<= (&min b) 0 (&max b)))))
  928. (define-type-checker (fdiv a b) #t)
  929. (define (div-result-range min-a max-a min-b max-b)
  930. (if (<= min-b 0 max-b)
  931. ;; If the range of the divisor crosses 0, the result spans
  932. ;; the whole range.
  933. (values -inf.0 +inf.0)
  934. ;; Otherwise min-b and max-b have the same sign, and cannot both
  935. ;; be infinity.
  936. (let ((--- (if (inf? min-b) 0 (floor/ min-a min-b)))
  937. (-+- (if (inf? max-b) 0 (floor/ min-a max-b)))
  938. (++- (if (inf? max-b) 0 (floor/ max-a max-b)))
  939. (+-- (if (inf? min-b) 0 (floor/ max-a min-b)))
  940. (--+ (if (inf? min-b) 0 (ceiling/ min-a min-b)))
  941. (-++ (if (inf? max-b) 0 (ceiling/ min-a max-b)))
  942. (+++ (if (inf? max-b) 0 (ceiling/ max-a max-b)))
  943. (+-+ (if (inf? min-b) 0 (ceiling/ max-a min-b))))
  944. (values (min (min --- -+- ++- +--)
  945. (min --+ -++ +++ +-+))
  946. (max (max --- -+- ++- +--)
  947. (max --+ -++ +++ +-+))))))
  948. (define-type-inferrer (div a b result)
  949. (let ((min-a (&min a)) (max-a (&max a))
  950. (min-b (&min b)) (max-b (&max b)))
  951. (call-with-values (lambda ()
  952. (div-result-range min-a max-a min-b max-b))
  953. (lambda (min max)
  954. (define-binary-result! a b result #f min max)))))
  955. (define-type-inferrer (fdiv a b result)
  956. (let ((min-a (&min a)) (max-a (&max a))
  957. (min-b (&min b)) (max-b (&max b)))
  958. (call-with-values (lambda ()
  959. (div-result-range min-a max-a min-b max-b))
  960. (lambda (min max)
  961. (define! result &f64 min max)))))
  962. (define-type-checker (quo a b)
  963. (and (check-type a &exact-integer -inf.0 +inf.0)
  964. (check-type b &exact-integer -inf.0 +inf.0)
  965. ;; We only know that there will not be an exception if b is not
  966. ;; zero.
  967. (not (<= (&min b) 0 (&max b)))))
  968. (define-type-inferrer (quo a b result)
  969. (restrict! a &exact-integer -inf.0 +inf.0)
  970. (restrict! b &exact-integer -inf.0 +inf.0)
  971. (define! result &exact-integer -inf.0 +inf.0))
  972. (define-type-checker-aliases quo rem)
  973. (define-type-inferrer (rem a b result)
  974. (restrict! a &exact-integer -inf.0 +inf.0)
  975. (restrict! b &exact-integer -inf.0 +inf.0)
  976. ;; Same sign as A.
  977. (let ((max-abs-rem (1- (max (abs (&min b)) (abs (&max b))))))
  978. (cond
  979. ((< (&min a) 0)
  980. (if (< 0 (&max a))
  981. (define! result &exact-integer (- max-abs-rem) max-abs-rem)
  982. (define! result &exact-integer (- max-abs-rem) 0)))
  983. (else
  984. (define! result &exact-integer 0 max-abs-rem)))))
  985. (define-type-checker-aliases quo mod)
  986. (define-type-inferrer (mod a b result)
  987. (restrict! a &exact-integer -inf.0 +inf.0)
  988. (restrict! b &exact-integer -inf.0 +inf.0)
  989. ;; Same sign as B.
  990. (let ((max-abs-mod (1- (max (abs (&min b)) (abs (&max b))))))
  991. (cond
  992. ((< (&min b) 0)
  993. (if (< 0 (&max b))
  994. (define! result &exact-integer (- max-abs-mod) max-abs-mod)
  995. (define! result &exact-integer (- max-abs-mod) 0)))
  996. (else
  997. (define! result &exact-integer 0 max-abs-mod)))))
  998. ;; Predicates.
  999. (define-syntax-rule (define-number-kind-predicate-inferrer name type)
  1000. (define-type-inferrer (name val result)
  1001. (cond
  1002. ((zero? (logand (&type val) type))
  1003. (define! result &false 0 0))
  1004. ((zero? (logand (&type val) (lognot type)))
  1005. (define! result &true 0 0))
  1006. (else
  1007. (define! result (logior &true &false) 0 0)))))
  1008. (define-number-kind-predicate-inferrer complex? &number)
  1009. (define-number-kind-predicate-inferrer real? &real)
  1010. (define-number-kind-predicate-inferrer rational?
  1011. (logior &exact-integer &fraction))
  1012. (define-number-kind-predicate-inferrer integer?
  1013. (logior &exact-integer &flonum))
  1014. (define-number-kind-predicate-inferrer exact-integer?
  1015. &exact-integer)
  1016. (define-simple-type-checker (exact? &number))
  1017. (define-type-inferrer (exact? val result)
  1018. (restrict! val &number -inf.0 +inf.0)
  1019. (cond
  1020. ((zero? (logand (&type val) (logior &exact-integer &fraction)))
  1021. (define! result &false 0 0))
  1022. ((zero? (logand (&type val) (lognot (logior &exact-integer &fraction))))
  1023. (define! result &true 0 0))
  1024. (else
  1025. (define! result (logior &true &false) 0 0))))
  1026. (define-simple-type-checker (inexact? &number))
  1027. (define-type-inferrer (inexact? val result)
  1028. (restrict! val &number -inf.0 +inf.0)
  1029. (cond
  1030. ((zero? (logand (&type val) (logior &flonum &complex)))
  1031. (define! result &false 0 0))
  1032. ((zero? (logand (&type val) (logand &number
  1033. (lognot (logior &flonum &complex)))))
  1034. (define! result &true 0 0))
  1035. (else
  1036. (define! result (logior &true &false) 0 0))))
  1037. (define-simple-type-checker (inf? &real))
  1038. (define-type-inferrer (inf? val result)
  1039. (restrict! val &real -inf.0 +inf.0)
  1040. (cond
  1041. ((or (zero? (logand (&type val) (logior &flonum &complex)))
  1042. (and (not (inf? (&min val))) (not (inf? (&max val)))))
  1043. (define! result &false 0 0))
  1044. (else
  1045. (define! result (logior &true &false) 0 0))))
  1046. (define-type-aliases inf? nan?)
  1047. (define-simple-type (even? &exact-integer)
  1048. ((logior &true &false) 0 0))
  1049. (define-type-aliases even? odd?)
  1050. ;; Bit operations.
  1051. (define-simple-type-checker (ash &exact-integer &exact-integer))
  1052. (define-type-inferrer (ash val count result)
  1053. (define (ash* val count)
  1054. ;; As we only precisely represent a 64-bit range, don't bother inferring
  1055. ;; shifts that might exceed that range.
  1056. (cond
  1057. ((inf? val) val) ; Preserves sign.
  1058. ((< -64 count 64) (ash val count))
  1059. ((zero? val) 0)
  1060. ((positive? val) +inf.0)
  1061. (else -inf.0)))
  1062. (restrict! val &exact-integer -inf.0 +inf.0)
  1063. (restrict! count &exact-integer -inf.0 +inf.0)
  1064. (let ((-- (ash* (&min val) (&min count)))
  1065. (-+ (ash* (&min val) (&max count)))
  1066. (++ (ash* (&max val) (&max count)))
  1067. (+- (ash* (&max val) (&min count))))
  1068. (define! result &exact-integer
  1069. (min -- -+ ++ +-)
  1070. (max -- -+ ++ +-))))
  1071. (define-simple-type-checker (ursh &u64 &u64))
  1072. (define-type-inferrer (ursh a b result)
  1073. (restrict! a &u64 0 &u64-max)
  1074. (restrict! b &u64 0 &u64-max)
  1075. (define! result &u64
  1076. (ash (&min/0 a) (- (&max/u64 b)))
  1077. (ash (&max/u64 a) (- (&min/0 b)))))
  1078. (define-type-aliases ursh ursh/immediate)
  1079. (define-simple-type-checker (ulsh &u64 &u64))
  1080. (define-type-inferrer (ulsh a b result)
  1081. (restrict! a &u64 0 &u64-max)
  1082. (restrict! b &u64 0 &u64-max)
  1083. (if (and (< (&max/u64 b) 64)
  1084. (<= (ash (&max/u64 a) (&max/u64 b)) &u64-max))
  1085. ;; No overflow; we can be precise.
  1086. (define! result &u64
  1087. (ash (&min/0 a) (&min/0 b))
  1088. (ash (&max/u64 a) (&max/u64 b)))
  1089. ;; Otherwise assume the whole range.
  1090. (define! result &u64 0 &u64-max)))
  1091. (define-type-aliases ulsh ulsh/immediate)
  1092. (define (next-power-of-two n)
  1093. (let lp ((out 1))
  1094. (if (< n out)
  1095. out
  1096. (lp (ash out 1)))))
  1097. (define-simple-type-checker (logand &exact-integer &exact-integer))
  1098. (define-type-inferrer (logand a b result)
  1099. (define (logand-min a b)
  1100. (if (and (negative? a) (negative? b))
  1101. (min a b)
  1102. 0))
  1103. (define (logand-max a b)
  1104. (if (and (positive? a) (positive? b))
  1105. (min a b)
  1106. 0))
  1107. (restrict! a &exact-integer -inf.0 +inf.0)
  1108. (restrict! b &exact-integer -inf.0 +inf.0)
  1109. (define! result &exact-integer
  1110. (logand-min (&min a) (&min b))
  1111. (logand-max (&max a) (&max b))))
  1112. (define-simple-type-checker (ulogand &u64 &u64))
  1113. (define-type-inferrer (ulogand a b result)
  1114. (restrict! a &u64 0 &u64-max)
  1115. (restrict! b &u64 0 &u64-max)
  1116. (define! result &u64 0 (max (&max/u64 a) (&max/u64 b))))
  1117. (define-simple-type-checker (logsub &exact-integer &exact-integer))
  1118. (define-type-inferrer (logsub a b result)
  1119. (define (logsub-bounds min-a max-a min-b max-b)
  1120. (cond
  1121. ((negative? max-b)
  1122. ;; Sign bit always set on B, so result will never be negative.
  1123. ;; If A might be negative (all leftmost bits 1), we don't know
  1124. ;; how positive the result might be.
  1125. (values 0 (if (negative? min-a) +inf.0 max-a)))
  1126. ((negative? min-b)
  1127. ;; Sign bit might be set on B.
  1128. (values min-a (if (negative? min-a) +inf.0 max-a)))
  1129. ((negative? min-a)
  1130. ;; Sign bit never set on B -- result will have the sign of A.
  1131. (values min-a (if (negative? max-a) -1 max-a)))
  1132. (else
  1133. ;; Sign bit never set on A and never set on B -- the nice case.
  1134. (values 0 max-a))))
  1135. (restrict! a &exact-integer -inf.0 +inf.0)
  1136. (restrict! b &exact-integer -inf.0 +inf.0)
  1137. (call-with-values (lambda ()
  1138. (logsub-bounds (&min a) (&max a) (&min b) (&max b)))
  1139. (lambda (min max)
  1140. (define! result &exact-integer min max))))
  1141. (define-simple-type-checker (ulogsub &u64 &u64))
  1142. (define-type-inferrer (ulogsub a b result)
  1143. (restrict! a &u64 0 &u64-max)
  1144. (restrict! b &u64 0 &u64-max)
  1145. (define! result &u64 0 (&max/u64 a)))
  1146. (define-simple-type-checker (logior &exact-integer &exact-integer))
  1147. (define-type-inferrer (logior a b result)
  1148. ;; Saturate all bits of val.
  1149. (define (saturate val)
  1150. (1- (next-power-of-two val)))
  1151. (define (logior-min a b)
  1152. (cond ((and (< a 0) (<= 0 b)) a)
  1153. ((and (< b 0) (<= 0 a)) b)
  1154. (else (max a b))))
  1155. (define (logior-max a b)
  1156. ;; If either operand is negative, just assume the max is -1.
  1157. (cond
  1158. ((or (< a 0) (< b 0)) -1)
  1159. ((or (inf? a) (inf? b)) +inf.0)
  1160. (else (saturate (logior a b)))))
  1161. (restrict! a &exact-integer -inf.0 +inf.0)
  1162. (restrict! b &exact-integer -inf.0 +inf.0)
  1163. (define! result &exact-integer
  1164. (logior-min (&min a) (&min b))
  1165. (logior-max (&max a) (&max b))))
  1166. (define-simple-type-checker (ulogior &u64 &u64))
  1167. (define-type-inferrer (ulogior a b result)
  1168. (restrict! a &u64 0 &u64-max)
  1169. (restrict! b &u64 0 &u64-max)
  1170. (define! result &u64
  1171. (max (&min/0 a) (&min/0 b))
  1172. (1- (next-power-of-two (logior (&max/u64 a) (&max/u64 b))))))
  1173. ;; For our purposes, treat logxor the same as logior.
  1174. (define-type-aliases logior logxor)
  1175. (define-simple-type-checker (ulogxor &u64 &u64))
  1176. (define-type-inferrer (ulogxor a b result)
  1177. (restrict! a &u64 0 &u64-max)
  1178. (restrict! b &u64 0 &u64-max)
  1179. (define! result &u64 0 &u64-max))
  1180. (define-simple-type-checker (lognot &exact-integer))
  1181. (define-type-inferrer (lognot a result)
  1182. (restrict! a &exact-integer -inf.0 +inf.0)
  1183. (define! result &exact-integer
  1184. (- -1 (&max a))
  1185. (- -1 (&min a))))
  1186. (define-simple-type-checker (logtest &exact-integer &exact-integer))
  1187. (define-predicate-inferrer (logtest a b true?)
  1188. (restrict! a &exact-integer -inf.0 +inf.0)
  1189. (restrict! b &exact-integer -inf.0 +inf.0))
  1190. (define-simple-type-checker (logbit? (&exact-integer 0 +inf.0) &exact-integer))
  1191. (define-type-inferrer (logbit? a b result)
  1192. (let ((a-min (&min a))
  1193. (a-max (&max a))
  1194. (b-min (&min b))
  1195. (b-max (&max b)))
  1196. (if (and (eqv? a-min a-max) (>= a-min 0) (not (inf? a-min))
  1197. (eqv? b-min b-max) (>= b-min 0) (not (inf? b-min)))
  1198. (let ((type (if (logbit? a-min b-min) &true &false)))
  1199. (define! result type 0 0))
  1200. (define! result (logior &true &false) 0 0))))
  1201. ;; Flonums.
  1202. (define-simple-type-checker (sqrt &number))
  1203. (define-type-inferrer (sqrt x result)
  1204. (let ((type (&type x)))
  1205. (cond
  1206. ((and (zero? (logand type &complex)) (<= 0 (&min x)))
  1207. (define! result
  1208. (logior type &flonum)
  1209. (inexact->exact (floor (sqrt (&min x))))
  1210. (if (inf? (&max x))
  1211. +inf.0
  1212. (inexact->exact (ceiling (sqrt (&max x)))))))
  1213. (else
  1214. (define! result (logior type &flonum &complex) -inf.0 +inf.0)))))
  1215. (define-simple-type-checker (abs &real))
  1216. (define-type-inferrer (abs x result)
  1217. (let ((type (&type x)))
  1218. (cond
  1219. ((eqv? type (logand type &number))
  1220. (restrict! x &real -inf.0 +inf.0)
  1221. (define! result (logand type &real)
  1222. (min (abs (&min x)) (abs (&max x)))
  1223. (max (abs (&min x)) (abs (&max x)))))
  1224. (else
  1225. (define! result (logior (logand (&type x) (lognot &number))
  1226. (logand (&type x) &real))
  1227. (&min/0 x)
  1228. (max (abs (&min x)) (abs (&max x))))))))
  1229. ;;;
  1230. ;;; Characters.
  1231. ;;;
  1232. (define-simple-type-checker (integer->char (&u64 0 *max-codepoint*)))
  1233. (define-type-inferrer (integer->char i result)
  1234. (restrict! i &u64 0 *max-codepoint*)
  1235. (define! result &char (&min/0 i) (min (&max i) *max-codepoint*)))
  1236. (define-simple-type-checker (char->integer &char))
  1237. (define-type-inferrer (char->integer c result)
  1238. (restrict! c &char 0 *max-codepoint*)
  1239. (define! result &u64 (&min/0 c) (min (&max c) *max-codepoint*)))
  1240. ;;;
  1241. ;;; Type flow analysis: the meet (ahem) of the algorithm.
  1242. ;;;
  1243. (define (successor-count cont)
  1244. (match cont
  1245. (($ $kargs _ _ ($ $continue k src exp))
  1246. (match exp
  1247. ((or ($ $branch) ($ $prompt)) 2)
  1248. (_ 1)))
  1249. (($ $kfun src meta self tail clause) (if clause 1 0))
  1250. (($ $kclause arity body alt) (if alt 2 1))
  1251. (($ $kreceive) 1)
  1252. (($ $ktail) 0)))
  1253. (define (intset-pop set)
  1254. (match (intset-next set)
  1255. (#f (values set #f))
  1256. (i (values (intset-remove set i) i))))
  1257. (define-syntax-rule (make-worklist-folder* seed ...)
  1258. (lambda (f worklist seed ...)
  1259. (let lp ((worklist worklist) (seed seed) ...)
  1260. (call-with-values (lambda () (intset-pop worklist))
  1261. (lambda (worklist i)
  1262. (if i
  1263. (call-with-values (lambda () (f i seed ...))
  1264. (lambda (i* seed ...)
  1265. (let add ((i* i*) (worklist worklist))
  1266. (match i*
  1267. (() (lp worklist seed ...))
  1268. ((i . i*) (add i* (intset-add worklist i)))))))
  1269. (values seed ...)))))))
  1270. (define worklist-fold*
  1271. (case-lambda
  1272. ((f worklist seed)
  1273. ((make-worklist-folder* seed) f worklist seed))))
  1274. (define intmap-ensure
  1275. (let* ((*absent* (list 'absent))
  1276. (not-found (lambda (i) *absent*)))
  1277. (lambda (map i ensure)
  1278. (let ((val (intmap-ref map i not-found)))
  1279. (if (eq? val *absent*)
  1280. (let ((val (ensure i)))
  1281. (values (intmap-add map i val) val))
  1282. (values map val))))))
  1283. ;; For best results, the labels in the function starting should be
  1284. ;; topologically sorted (renumbered). Otherwise the backward branch
  1285. ;; detection mentioned in the module commentary will trigger for
  1286. ;; ordinary forward branches.
  1287. (define (infer-types conts kfun)
  1288. "Compute types for all variables bound in the function labelled
  1289. @var{kfun}, from @var{conts}. Returns an intmap mapping labels to type
  1290. entries.
  1291. A type entry is a vector that describes the types of the values that
  1292. flow into and out of a labelled expression. The first slot in the type
  1293. entry vector corresponds to the types that flow in, and the rest of the
  1294. slots correspond to the types that flow out. Each element of the type
  1295. entry vector is an intmap mapping variable name to the variable's
  1296. inferred type. An inferred type is a 3-vector of type, minimum, and
  1297. maximum, where type is a bitset as a fixnum."
  1298. (define (get-entry typev label) (intmap-ref typev label))
  1299. (define (entry-not-found label)
  1300. (make-vector (1+ (successor-count (intmap-ref conts label))) #f))
  1301. (define (ensure-entry typev label)
  1302. (intmap-ensure typev label entry-not-found))
  1303. (define (compute-initial-state)
  1304. (let ((entry (entry-not-found kfun)))
  1305. ;; Nothing flows in to the first label.
  1306. (vector-set! entry 0 empty-intmap)
  1307. (intmap-add empty-intmap kfun entry)))
  1308. (define (adjoin-vars types vars entry)
  1309. (match vars
  1310. (() types)
  1311. ((var . vars)
  1312. (adjoin-vars (adjoin-var types var entry) vars entry))))
  1313. (define (infer-primcall types succ name args result)
  1314. (cond
  1315. ((hashq-ref *type-inferrers* name)
  1316. => (lambda (inferrer)
  1317. ;; FIXME: remove the apply?
  1318. ;; (pk 'primcall name args result)
  1319. (apply inferrer types succ
  1320. (if result
  1321. (append args (list result))
  1322. args))))
  1323. (result
  1324. (adjoin-var types result all-types-entry))
  1325. (else
  1326. types)))
  1327. (define (vector-replace vec idx val)
  1328. (let ((vec (vector-copy vec)))
  1329. (vector-set! vec idx val)
  1330. vec))
  1331. (define (update-out-types label typev types succ-idx)
  1332. (let* ((entry (get-entry typev label))
  1333. (old-types (vector-ref entry (1+ succ-idx))))
  1334. (if (eq? types old-types)
  1335. (values typev #f)
  1336. (let ((entry (vector-replace entry (1+ succ-idx) types))
  1337. (first? (not old-types)))
  1338. (values (intmap-replace typev label entry) first?)))))
  1339. (define (update-in-types label typev types saturate?)
  1340. (let*-values (((typev entry) (ensure-entry typev label))
  1341. ((old-types) (vector-ref entry 0))
  1342. ;; TODO: If the label has only one predecessor, we can
  1343. ;; avoid the meet.
  1344. ((types) (if (not old-types)
  1345. types
  1346. (let ((meet (if saturate?
  1347. type-entry-saturating-union
  1348. type-entry-union)))
  1349. (intmap-intersect old-types types meet)))))
  1350. (if (eq? old-types types)
  1351. (values typev #f)
  1352. (let ((entry (vector-replace entry 0 types)))
  1353. (values (intmap-replace typev label entry) #t)))))
  1354. (define (propagate-types label typev succ-idx succ-label types)
  1355. (let*-values
  1356. (((typev first?) (update-out-types label typev types succ-idx))
  1357. ((saturate?) (and (not first?) (<= succ-label label)))
  1358. ((typev changed?) (update-in-types succ-label typev types saturate?)))
  1359. (values (if changed? (list succ-label) '()) typev)))
  1360. (define (visit-exp label typev k types exp)
  1361. (define (propagate1 succ-label types)
  1362. (propagate-types label typev 0 succ-label types))
  1363. (define (propagate2 succ0-label types0 succ1-label types1)
  1364. (let*-values (((changed0 typev)
  1365. (propagate-types label typev 0 succ0-label types0))
  1366. ((changed1 typev)
  1367. (propagate-types label typev 1 succ1-label types1)))
  1368. (values (append changed0 changed1) typev)))
  1369. ;; Each of these branches must propagate to its successors.
  1370. (match exp
  1371. (($ $branch kt ($ $values (arg)))
  1372. ;; The "normal" continuation is the #f branch.
  1373. (let ((kf-types (restrict-var types arg
  1374. (make-type-entry (logior &false &nil)
  1375. 0
  1376. 0)))
  1377. (kt-types (restrict-var types arg
  1378. (make-type-entry
  1379. (logand &all-types
  1380. (lognot (logior &false &nil)))
  1381. -inf.0 +inf.0))))
  1382. (propagate2 k kf-types kt kt-types)))
  1383. (($ $branch kt ($ $primcall name args))
  1384. ;; The "normal" continuation is the #f branch.
  1385. (let ((kf-types (infer-primcall types 0 name args #f))
  1386. (kt-types (infer-primcall types 1 name args #f)))
  1387. (propagate2 k kf-types kt kt-types)))
  1388. (($ $prompt escape? tag handler)
  1389. ;; The "normal" continuation enters the prompt.
  1390. (propagate2 k types handler types))
  1391. (($ $primcall name args)
  1392. (propagate1 k
  1393. (match (intmap-ref conts k)
  1394. (($ $kargs _ defs)
  1395. (infer-primcall types 0 name args
  1396. (match defs ((var) var) (() #f))))
  1397. (_
  1398. ;; (pk 'warning-no-restrictions name)
  1399. types))))
  1400. (($ $values args)
  1401. (match (intmap-ref conts k)
  1402. (($ $kargs _ defs)
  1403. (let ((in types))
  1404. (let lp ((defs defs) (args args) (out types))
  1405. (match (cons defs args)
  1406. ((() . ())
  1407. (propagate1 k out))
  1408. (((def . defs) . (arg . args))
  1409. (lp defs args
  1410. (adjoin-var out def (var-type-entry in arg))))))))
  1411. (_
  1412. (propagate1 k types))))
  1413. ((or ($ $call) ($ $callk))
  1414. (propagate1 k types))
  1415. (($ $rec names vars funs)
  1416. (let ((proc-type (make-type-entry &procedure -inf.0 +inf.0)))
  1417. (propagate1 k (adjoin-vars types vars proc-type))))
  1418. (_
  1419. (match (intmap-ref conts k)
  1420. (($ $kargs (_) (var))
  1421. (let ((entry (match exp
  1422. (($ $const val)
  1423. (constant-type val))
  1424. ((or ($ $prim) ($ $fun) ($ $closure))
  1425. ;; Could be more precise here.
  1426. (make-type-entry &procedure -inf.0 +inf.0)))))
  1427. (propagate1 k (adjoin-var types var entry))))))))
  1428. (define (visit-cont label typev)
  1429. (let ((types (vector-ref (intmap-ref typev label) 0)))
  1430. (define (propagate0)
  1431. (values '() typev))
  1432. (define (propagate1 succ-label types)
  1433. (propagate-types label typev 0 succ-label types))
  1434. (define (propagate2 succ0-label types0 succ1-label types1)
  1435. (let*-values (((changed0 typev)
  1436. (propagate-types label typev 0 succ0-label types0))
  1437. ((changed1 typev)
  1438. (propagate-types label typev 1 succ1-label types1)))
  1439. (values (append changed0 changed1) typev)))
  1440. ;; Add types for new definitions, and restrict types of
  1441. ;; existing variables due to side effects.
  1442. (match (intmap-ref conts label)
  1443. (($ $kargs names vars ($ $continue k src exp))
  1444. (visit-exp label typev k types exp))
  1445. (($ $kreceive arity k)
  1446. (match (intmap-ref conts k)
  1447. (($ $kargs names vars)
  1448. (propagate1 k (adjoin-vars types vars all-types-entry)))))
  1449. (($ $kfun src meta self tail clause)
  1450. (if clause
  1451. (propagate1 clause (adjoin-var types self all-types-entry))
  1452. (propagate0)))
  1453. (($ $kclause arity kbody kalt)
  1454. (match (intmap-ref conts kbody)
  1455. (($ $kargs _ defs)
  1456. (let ((body-types (adjoin-vars types defs all-types-entry)))
  1457. (if kalt
  1458. (propagate2 kbody body-types kalt types)
  1459. (propagate1 kbody body-types))))))
  1460. (($ $ktail) (propagate0)))))
  1461. (worklist-fold* visit-cont
  1462. (intset-add empty-intset kfun)
  1463. (compute-initial-state)))
  1464. (define (lookup-pre-type types label def)
  1465. (let* ((entry (intmap-ref types label))
  1466. (tentry (var-type-entry (vector-ref entry 0) def)))
  1467. (values (type-entry-type tentry)
  1468. (type-entry-min tentry)
  1469. (type-entry-max tentry))))
  1470. (define (lookup-post-type types label def succ-idx)
  1471. (let* ((entry (intmap-ref types label))
  1472. (tentry (var-type-entry (vector-ref entry (1+ succ-idx)) def)))
  1473. (values (type-entry-type tentry)
  1474. (type-entry-min tentry)
  1475. (type-entry-max tentry))))
  1476. (define (primcall-types-check? types label name args)
  1477. (match (hashq-ref *type-checkers* name)
  1478. (#f #f)
  1479. (checker
  1480. (let ((entry (intmap-ref types label)))
  1481. (apply checker (vector-ref entry 0) args)))))