types.scm 84 KB

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