types.scm 81 KB

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