frame.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. ;;; Guile VM frame functions
  2. ;;; Copyright (C) 2001, 2005, 2009-2016, 2018 Free Software Foundation, Inc.
  3. ;;;
  4. ;;; This library is free software; you can redistribute it and/or
  5. ;;; modify it under the terms of the GNU Lesser General Public
  6. ;;; License as published by the Free Software Foundation; either
  7. ;;; version 3 of the License, or (at your option) any later version.
  8. ;;;
  9. ;;; This library is distributed in the hope that it will be useful,
  10. ;;; but 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 library; if not, write to the Free Software
  16. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system vm frame)
  19. #:use-module (system base pmatch)
  20. #:use-module (system foreign)
  21. #:use-module (system vm program)
  22. #:use-module (system vm debug)
  23. #:use-module (system vm disassembler)
  24. #:use-module (srfi srfi-9)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (rnrs bytevectors)
  27. #:use-module (ice-9 match)
  28. #:export (binding-index
  29. binding-name
  30. binding-slot
  31. binding-representation
  32. frame-bindings
  33. frame-lookup-binding
  34. binding-ref binding-set!
  35. frame-call-representation
  36. frame-return-values
  37. frame-environment
  38. frame-object-binding frame-object-name))
  39. (eval-when (expand compile load eval)
  40. (load-extension (string-append "libguile-" (effective-version))
  41. "scm_init_frames_builtins"))
  42. (define-record-type <binding>
  43. (make-binding frame idx name slot representation)
  44. binding?
  45. (frame binding-frame)
  46. (idx binding-index)
  47. (name binding-name)
  48. (slot binding-slot)
  49. (representation binding-representation))
  50. (define (parse-code code)
  51. (let ((len (bytevector-length code)))
  52. (let lp ((pos 0) (out '()))
  53. (cond
  54. ((< pos len)
  55. (let* ((inst-len (instruction-length code pos))
  56. (pos (+ pos inst-len)))
  57. (unless (<= pos len)
  58. (error "Failed to parse codestream"))
  59. (lp pos (cons inst-len out))))
  60. (else
  61. (list->vector (reverse out)))))))
  62. (define (compute-predecessors code parsed)
  63. (let ((preds (make-vector (vector-length parsed) '())))
  64. (define (add-pred! from target)
  65. (let lp ((to from) (target target))
  66. (cond
  67. ((negative? target)
  68. (lp (1- to) (+ target (vector-ref parsed (1- to)))))
  69. ((positive? target)
  70. (lp (1+ to) (- target (vector-ref parsed to))))
  71. ((= to (vector-length preds))
  72. ;; This can happen when an arity fails to match. Just ignore
  73. ;; this case.
  74. #t)
  75. (else
  76. (vector-set! preds to (cons from (vector-ref preds to)))))))
  77. (let lp ((n 0) (pos 0))
  78. (when (< n (vector-length preds))
  79. (when (instruction-has-fallthrough? code pos)
  80. (add-pred! n (vector-ref parsed n)))
  81. (for-each (lambda (target)
  82. (add-pred! n target))
  83. (instruction-relative-jump-targets code pos))
  84. (lp (1+ n) (+ pos (vector-ref parsed n)))))
  85. preds))
  86. (define (compute-frame-sizes code parsed initial-size)
  87. (let ((in-sizes (make-vector (vector-length parsed) #f))
  88. (out-sizes (make-vector (vector-length parsed) #f)))
  89. ;; This only computes all possible valid stack sizes if the bytecode
  90. ;; is sorted topologically. Guiles' compiler does this currently,
  91. ;; but if that changes we should do a proper pre-order visit. Of
  92. ;; course the bytecode has to be valid too.
  93. (define (find-idx n diff)
  94. (let lp ((n n) (diff diff))
  95. (cond
  96. ((= n (vector-length parsed))
  97. ;; Possible for jumps to alternate arities.
  98. #f)
  99. ((negative? diff)
  100. (lp (1- n) (+ diff (vector-ref parsed (1- n)))))
  101. ((positive? diff)
  102. (lp (1+ n) (- diff (vector-ref parsed n))))
  103. (else n))))
  104. (vector-set! in-sizes 0 initial-size)
  105. (let lp ((n 0) (pos 0))
  106. (define (offset->idx target)
  107. (call-with-values (lambda ()
  108. (if (>= target pos)
  109. (values n pos)
  110. (values 0 0)))
  111. (lambda (n pos)
  112. (let lp ((n n) (pos pos))
  113. (cond
  114. ((= pos target) n)
  115. ((< pos target) (lp (1+ n) (+ pos (vector-ref parsed n))))
  116. (else (error "bad target" target)))))))
  117. (when (< n (vector-length parsed))
  118. (let* ((in (vector-ref in-sizes n))
  119. (out (instruction-stack-size-after code pos in)))
  120. (vector-set! out-sizes n out)
  121. (when out
  122. (when (instruction-has-fallthrough? code pos)
  123. (vector-set! in-sizes (1+ n) out))
  124. (for-each (lambda (target)
  125. (let ((idx (find-idx n target)))
  126. (when idx
  127. (vector-set! in-sizes idx out))))
  128. (instruction-relative-jump-targets code pos))))
  129. (lp (1+ n) (+ pos (vector-ref parsed n)))))
  130. (values in-sizes out-sizes)))
  131. (define (compute-genv parsed defs)
  132. (let ((genv (make-vector (vector-length parsed) '())))
  133. (define (add-def! pos var)
  134. (vector-set! genv pos (cons var (vector-ref genv pos))))
  135. (let lp ((var 0) (pos 0) (pc-offset 0))
  136. (when (< var (vector-length defs))
  137. (match (vector-ref defs var)
  138. (#(name offset slot representation)
  139. (when (< offset pc-offset)
  140. (error "mismatch between def offsets and parsed code"))
  141. (cond
  142. ((< pc-offset offset)
  143. (lp var (1+ pos) (+ pc-offset (vector-ref parsed pos))))
  144. (else
  145. (add-def! pos var)
  146. (lp (1+ var) pos pc-offset)))))))
  147. genv))
  148. (define (compute-defs-by-slot defs)
  149. (let* ((nslots (match defs
  150. (#(#(_ _ slot _) ...) (1+ (apply max slot)))))
  151. (by-slot (make-vector nslots #f)))
  152. (let lp ((n 0))
  153. (when (< n nslots)
  154. (vector-set! by-slot n (make-bitvector (vector-length defs) #f))
  155. (lp (1+ n))))
  156. (let lp ((n 0))
  157. (when (< n (vector-length defs))
  158. (match (vector-ref defs n)
  159. (#(_ _ slot _)
  160. (bitvector-set! (vector-ref by-slot slot) n #t)
  161. (lp (1+ n))))))
  162. by-slot))
  163. (define (compute-killv code parsed defs)
  164. (let*-values (((defs-by-slot) (compute-defs-by-slot defs))
  165. ((initial-frame-size) (vector-length defs-by-slot))
  166. ((in-sizes out-sizes)
  167. (compute-frame-sizes code parsed initial-frame-size))
  168. ((killv) (make-vector (vector-length parsed) #f)))
  169. (define (kill-slot! n slot)
  170. (bit-set*! (vector-ref killv n) (vector-ref defs-by-slot slot) #t))
  171. (let lp ((n 0))
  172. (when (< n (vector-length killv))
  173. (vector-set! killv n (make-bitvector (vector-length defs) #f))
  174. (lp (1+ n))))
  175. ;; Some defs get into place without explicit instructions -- this is
  176. ;; the case if no shuffling need occur, for example. In any case,
  177. ;; mark them as killing any previous definitions at that slot.
  178. (let lp ((var 0) (pos 0) (pc-offset 0))
  179. (when (< var (vector-length defs))
  180. (match (vector-ref defs var)
  181. (#(name offset slot representation)
  182. (when (< offset pc-offset)
  183. (error "mismatch between def offsets and parsed code"))
  184. (cond
  185. ((< pc-offset offset)
  186. (lp var (1+ pos) (+ pc-offset (vector-ref parsed pos))))
  187. (else
  188. (kill-slot! pos slot)
  189. (lp (1+ var) pos pc-offset)))))))
  190. (let lp ((n 0) (pos 0))
  191. (when (< n (vector-length parsed))
  192. (for-each (lambda (slot)
  193. (when (< slot (vector-length defs-by-slot))
  194. (kill-slot! n slot)))
  195. (let ((in (vector-ref in-sizes n))
  196. (out (vector-ref out-sizes n)))
  197. (instruction-slot-clobbers code pos in out)))
  198. (lp (1+ n) (+ pos (vector-ref parsed n)))))
  199. killv))
  200. (define (available-bindings frame arity ip top-frame?)
  201. (let* ((defs (list->vector (arity-definitions arity)))
  202. (code (arity-code arity))
  203. (parsed (parse-code code))
  204. (len (vector-length parsed))
  205. (preds (compute-predecessors code parsed))
  206. (genv (compute-genv parsed defs))
  207. (killv (compute-killv code parsed defs))
  208. (inv (make-vector len #f))
  209. (outv (make-vector len #f))
  210. (tmp (make-bitvector (vector-length defs) #f)))
  211. (define (bitvector-copy! dst src)
  212. (bitvector-fill! dst #f)
  213. (bit-set*! dst src #t))
  214. (define (bitvector-meet! accum src)
  215. (bitvector-copy! tmp src)
  216. (bit-invert! tmp)
  217. (bit-set*! accum tmp #f))
  218. (let lp ((n 0))
  219. (when (< n len)
  220. (vector-set! inv n (make-bitvector (vector-length defs) #f))
  221. (vector-set! outv n (make-bitvector (vector-length defs) #f))
  222. (lp (1+ n))))
  223. (let lp ((n 0) (first? #t) (changed? #f))
  224. (cond
  225. ((< n len)
  226. (let ((in (vector-ref inv n))
  227. (out (vector-ref outv n))
  228. (kill (vector-ref killv n))
  229. (gen (vector-ref genv n)))
  230. (let ((out-count (or changed? (bit-count #t out))))
  231. (bitvector-fill! in (not (zero? n)))
  232. (let lp ((preds (vector-ref preds n)))
  233. (match preds
  234. (() #t)
  235. ((pred . preds)
  236. (unless (and first? (<= n pred))
  237. (bitvector-meet! in (vector-ref outv pred)))
  238. (lp preds))))
  239. (bitvector-copy! out in)
  240. (bit-set*! out kill #f)
  241. (for-each (lambda (def)
  242. (bitvector-set! out def #t))
  243. gen)
  244. (lp (1+ n) first?
  245. (or changed? (not (eqv? out-count (bit-count #t out))))))))
  246. ((or changed? first?)
  247. (lp 0 #f #f))))
  248. (let lp ((n 0) (offset (- ip (arity-low-pc arity))))
  249. (when (< offset 0)
  250. (error "ip did not correspond to an instruction boundary?"))
  251. (if (zero? offset)
  252. ;; It shouldn't be the case that both OFFSET and N are zero
  253. ;; but TOP-FRAME? is false. Still, it could happen, as is
  254. ;; currently the case in frame-arguments.
  255. (let ((live (if (or top-frame? (zero? n))
  256. (vector-ref inv n)
  257. ;; If we're not at a top frame, the IP points
  258. ;; to the continuation -- but we haven't
  259. ;; returned and defined its values yet. The
  260. ;; set of live variables is the set that was
  261. ;; live going into the call, minus the set
  262. ;; killed by the call, but not including
  263. ;; values defined by the call.
  264. (begin
  265. (bitvector-copy! tmp (vector-ref inv (1- n)))
  266. (bit-set*! tmp (vector-ref killv (1- n)) #f)
  267. tmp))))
  268. (let lp ((n 0))
  269. (let ((n (bit-position #t live n)))
  270. (if n
  271. (match (vector-ref defs n)
  272. (#(name def-offset slot representation)
  273. (cons (make-binding frame n name slot representation)
  274. (lp (1+ n)))))
  275. '()))))
  276. (lp (1+ n) (- offset (vector-ref parsed n)))))))
  277. (define* (frame-bindings frame #:optional top-frame?)
  278. (let ((ip (frame-instruction-pointer frame)))
  279. (cond
  280. ((find-program-arity ip)
  281. => (lambda (arity)
  282. (available-bindings frame arity ip top-frame?)))
  283. (else '()))))
  284. (define (frame-lookup-binding frame var)
  285. (let lp ((bindings (frame-bindings frame)))
  286. (cond ((null? bindings)
  287. #f)
  288. ((eq? (binding-name (car bindings)) var)
  289. (car bindings))
  290. (else
  291. (lp (cdr bindings))))))
  292. (define (binding-ref binding)
  293. (frame-local-ref (or (binding-frame binding)
  294. (error "binding has no frame" binding))
  295. (binding-slot binding)
  296. (binding-representation binding)))
  297. (define (binding-set! binding val)
  298. (frame-local-set! (or (binding-frame binding)
  299. (error "binding has no frame" binding))
  300. (binding-slot binding)
  301. val
  302. (binding-representation binding)))
  303. (define* (frame-procedure-name frame #:key
  304. (info (find-program-debug-info
  305. (frame-instruction-pointer frame))))
  306. (if info
  307. (program-debug-info-name info)
  308. (primitive-code-name (frame-instruction-pointer frame))))
  309. ;; This function is always called to get some sort of representation of the
  310. ;; frame to present to the user, so let's do the logical thing and dispatch to
  311. ;; frame-call-representation.
  312. (define (frame-arguments frame)
  313. (cdr (frame-call-representation frame)))
  314. ;;;
  315. ;;; Pretty printing
  316. ;;;
  317. ;; Basically there are three cases to deal with here:
  318. ;;
  319. ;; 1. We've already parsed the arguments, and bound them to local
  320. ;; variables. In a standard (lambda (a b c) ...) call, this doesn't
  321. ;; involve any argument shuffling; but with rest, optional, or
  322. ;; keyword arguments, the arguments as given to the procedure may
  323. ;; not correspond to what's on the stack. We reconstruct the
  324. ;; arguments using e.g. for the case above: `(,a ,b ,c). This works
  325. ;; for rest arguments too: (a b . c) => `(,a ,b . ,c)
  326. ;;
  327. ;; 2. We have failed to parse the arguments. Perhaps it's the wrong
  328. ;; number of arguments, or perhaps we're doing a typed dispatch and
  329. ;; the types don't match. In that case the arguments are all on the
  330. ;; stack, and nothing else is on the stack.
  331. ;;
  332. ;; 3. Alternately it's possible that we're between a primitive call
  333. ;; and its associated return. In that case, we won't be able to
  334. ;; say anything at all.
  335. (define* (frame-call-representation frame #:key top-frame?)
  336. (let* ((ip (frame-instruction-pointer frame))
  337. (info (find-program-debug-info ip))
  338. (nlocals (frame-num-locals frame)))
  339. (define (find-slot i bindings)
  340. (match bindings
  341. (() #f)
  342. (((and binding ($ <binding> frame idx name slot)) . bindings)
  343. (if (< idx i)
  344. (find-slot i bindings)
  345. (and (= idx i) binding)))))
  346. (define (local-ref i bindings)
  347. (cond
  348. ((not bindings)
  349. ;; This case is only hit for primitives and application
  350. ;; arguments.
  351. (frame-local-ref frame i 'scm))
  352. ((find-slot i bindings)
  353. => (lambda (binding)
  354. (let ((val (frame-local-ref frame (binding-slot binding)
  355. (binding-representation binding))))
  356. ;; It could be that there's a value that isn't clobbered
  357. ;; by a call but that isn't live after a call either. In
  358. ;; that case, if GC runs during the call, the value will
  359. ;; be collected, and on the stack it will be replaced
  360. ;; with the unspecified value. Assume that clobbering
  361. ;; values is more likely than passing the unspecified
  362. ;; value as an argument, and replace unspecified with _,
  363. ;; as if the binding were not available.
  364. (if (unspecified? val) '_ val))))
  365. (else
  366. '_)))
  367. (define (application-arguments)
  368. ;; Case 1.
  369. (map (lambda (local) (local-ref local #f))
  370. ;; Cdr past the 0th local, which is the procedure.
  371. (cdr (iota nlocals))))
  372. (define (reconstruct-arguments bindings nreq nopt kw has-rest? local)
  373. ;; Case 2.
  374. (cond
  375. ((positive? nreq)
  376. (cons (local-ref local bindings)
  377. (reconstruct-arguments bindings
  378. (1- nreq) nopt kw has-rest? (1+ local))))
  379. ((positive? nopt)
  380. (cons (local-ref local bindings)
  381. (reconstruct-arguments bindings
  382. nreq (1- nopt) kw has-rest? (1+ local))))
  383. ((pair? kw)
  384. (cons* (caar kw) (local-ref (cdar kw) bindings)
  385. (reconstruct-arguments bindings
  386. nreq nopt (cdr kw) has-rest? (1+ local))))
  387. (has-rest?
  388. (local-ref local bindings))
  389. (else
  390. '())))
  391. (cons
  392. (or (frame-procedure-name frame #:info info) '_)
  393. (cond
  394. ((find-program-arity ip)
  395. => (lambda (arity)
  396. (if (and top-frame? (eqv? ip (arity-low-pc arity)))
  397. (application-arguments)
  398. (reconstruct-arguments
  399. (available-bindings frame arity ip top-frame?)
  400. (arity-nreq arity)
  401. (arity-nopt arity)
  402. (arity-keyword-args arity)
  403. (arity-has-rest? arity)
  404. 1))))
  405. ((and (primitive-code? ip)
  406. (program-arguments-alist (frame-local-ref frame 0 'scm) ip))
  407. => (lambda (args)
  408. (match args
  409. ((('required . req)
  410. ('optional . opt)
  411. ('keyword . kw)
  412. ('allow-other-keys? . _)
  413. ('rest . rest))
  414. (reconstruct-arguments #f
  415. (length req) (length opt) kw rest 1)))))
  416. (else
  417. (application-arguments))))))
  418. ;;; Misc
  419. ;;;
  420. (define (frame-environment frame)
  421. (map (lambda (binding)
  422. (cons (binding-name binding) (binding-ref binding)))
  423. (frame-bindings frame)))
  424. (define (frame-object-binding frame obj)
  425. (do ((bs (frame-bindings frame) (cdr bs)))
  426. ((or (null? bs) (eq? obj (binding-ref (car bs))))
  427. (and (pair? bs) (car bs)))))
  428. (define (frame-object-name frame obj)
  429. (cond ((frame-object-binding frame obj) => binding-name)
  430. (else #f)))