frame.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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-bit! (vector-ref by-slot slot) n)
  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. (bitvector-set-bits! (vector-ref killv n)
  171. (vector-ref defs-by-slot slot)))
  172. (let lp ((n 0))
  173. (when (< n (vector-length killv))
  174. (vector-set! killv n (make-bitvector (vector-length defs) #f))
  175. (lp (1+ n))))
  176. ;; Some defs get into place without explicit instructions -- this is
  177. ;; the case if no shuffling need occur, for example. In any case,
  178. ;; mark them as killing any previous definitions at that slot.
  179. (let lp ((var 0) (pos 0) (pc-offset 0))
  180. (when (< var (vector-length defs))
  181. (match (vector-ref defs var)
  182. (#(name offset slot representation)
  183. (when (< offset pc-offset)
  184. (error "mismatch between def offsets and parsed code"))
  185. (cond
  186. ((< pc-offset offset)
  187. (lp var (1+ pos) (+ pc-offset (vector-ref parsed pos))))
  188. (else
  189. (kill-slot! pos slot)
  190. (lp (1+ var) pos pc-offset)))))))
  191. (let lp ((n 0) (pos 0))
  192. (when (< n (vector-length parsed))
  193. (for-each (lambda (slot)
  194. (when (< slot (vector-length defs-by-slot))
  195. (kill-slot! n slot)))
  196. (let ((in (vector-ref in-sizes n))
  197. (out (vector-ref out-sizes n)))
  198. (instruction-slot-clobbers code pos in out)))
  199. (lp (1+ n) (+ pos (vector-ref parsed n)))))
  200. killv))
  201. (define (available-bindings frame arity ip top-frame?)
  202. (let* ((defs (list->vector (arity-definitions arity)))
  203. (code (arity-code arity))
  204. (parsed (parse-code code))
  205. (len (vector-length parsed))
  206. (preds (compute-predecessors code parsed))
  207. (genv (compute-genv parsed defs))
  208. (killv (compute-killv code parsed defs))
  209. (inv (make-vector len #f))
  210. (outv (make-vector len #f))
  211. (tmp (make-bitvector (vector-length defs) #f)))
  212. (define (bitvector-copy! dst src)
  213. (bitvector-clear-all-bits! dst)
  214. (bitvector-set-bits! dst src))
  215. (define (bitvector-meet! accum src)
  216. (bitvector-copy! tmp src)
  217. (bitvector-flip-all-bits! tmp)
  218. (bitvector-clear-bits! accum tmp))
  219. (let lp ((n 0))
  220. (when (< n len)
  221. (vector-set! inv n (make-bitvector (vector-length defs) #f))
  222. (vector-set! outv n (make-bitvector (vector-length defs) #f))
  223. (lp (1+ n))))
  224. (let lp ((n 0) (first? #t) (changed? #f))
  225. (cond
  226. ((< n len)
  227. (let ((in (vector-ref inv n))
  228. (out (vector-ref outv n))
  229. (kill (vector-ref killv n))
  230. (gen (vector-ref genv n)))
  231. (let ((out-count (or changed? (bitvector-count out))))
  232. (if (zero? n)
  233. (bitvector-clear-all-bits! in)
  234. (bitvector-set-all-bits! in))
  235. (let lp ((preds (vector-ref preds n)))
  236. (match preds
  237. (() #t)
  238. ((pred . preds)
  239. (unless (and first? (<= n pred))
  240. (bitvector-meet! in (vector-ref outv pred)))
  241. (lp preds))))
  242. (bitvector-copy! out in)
  243. (bitvector-clear-bits! out kill)
  244. (for-each (lambda (def)
  245. (bitvector-set-bit! out def))
  246. gen)
  247. (lp (1+ n) first?
  248. (or changed? (not (eqv? out-count (bitvector-count out))))))))
  249. ((or changed? first?)
  250. (lp 0 #f #f))))
  251. (let lp ((n 0) (offset (- ip (arity-low-pc arity))))
  252. (when (< offset 0)
  253. (error "ip did not correspond to an instruction boundary?"))
  254. (if (zero? offset)
  255. ;; It shouldn't be the case that both OFFSET and N are zero
  256. ;; but TOP-FRAME? is false. Still, it could happen, as is
  257. ;; currently the case in frame-arguments.
  258. (let ((live (if (or top-frame? (zero? n))
  259. (vector-ref inv n)
  260. ;; If we're not at a top frame, the IP points
  261. ;; to the continuation -- but we haven't
  262. ;; returned and defined its values yet. The
  263. ;; set of live variables is the set that was
  264. ;; live going into the call, minus the set
  265. ;; killed by the call, but not including
  266. ;; values defined by the call.
  267. (begin
  268. (bitvector-copy! tmp (vector-ref inv (1- n)))
  269. (bitvector-clear-bits! tmp (vector-ref killv (1- n)))
  270. tmp))))
  271. (let lp ((n 0))
  272. (let ((n (bitvector-position live #t n)))
  273. (if n
  274. (match (vector-ref defs n)
  275. (#(name def-offset slot representation)
  276. (cons (make-binding frame n name slot representation)
  277. (lp (1+ n)))))
  278. '()))))
  279. (lp (1+ n) (- offset (vector-ref parsed n)))))))
  280. (define* (frame-bindings frame #:optional top-frame?)
  281. (let ((ip (frame-instruction-pointer frame)))
  282. (cond
  283. ((find-program-arity ip)
  284. => (lambda (arity)
  285. (available-bindings frame arity ip top-frame?)))
  286. (else '()))))
  287. (define (frame-lookup-binding frame var)
  288. (let lp ((bindings (frame-bindings frame)))
  289. (cond ((null? bindings)
  290. #f)
  291. ((eq? (binding-name (car bindings)) var)
  292. (car bindings))
  293. (else
  294. (lp (cdr bindings))))))
  295. (define (binding-ref binding)
  296. (frame-local-ref (or (binding-frame binding)
  297. (error "binding has no frame" binding))
  298. (binding-slot binding)
  299. (binding-representation binding)))
  300. (define (binding-set! binding val)
  301. (frame-local-set! (or (binding-frame binding)
  302. (error "binding has no frame" binding))
  303. (binding-slot binding)
  304. val
  305. (binding-representation binding)))
  306. (define* (frame-procedure-name frame #:key
  307. (info (find-program-debug-info
  308. (frame-instruction-pointer frame))))
  309. (if info
  310. (program-debug-info-name info)
  311. (primitive-code-name (frame-instruction-pointer frame))))
  312. ;; This function is always called to get some sort of representation of the
  313. ;; frame to present to the user, so let's do the logical thing and dispatch to
  314. ;; frame-call-representation.
  315. (define (frame-arguments frame)
  316. (cdr (frame-call-representation frame)))
  317. ;;;
  318. ;;; Pretty printing
  319. ;;;
  320. ;; Basically there are three cases to deal with here:
  321. ;;
  322. ;; 1. We've already parsed the arguments, and bound them to local
  323. ;; variables. In a standard (lambda (a b c) ...) call, this doesn't
  324. ;; involve any argument shuffling; but with rest, optional, or
  325. ;; keyword arguments, the arguments as given to the procedure may
  326. ;; not correspond to what's on the stack. We reconstruct the
  327. ;; arguments using e.g. for the case above: `(,a ,b ,c). This works
  328. ;; for rest arguments too: (a b . c) => `(,a ,b . ,c)
  329. ;;
  330. ;; 2. We have failed to parse the arguments. Perhaps it's the wrong
  331. ;; number of arguments, or perhaps we're doing a typed dispatch and
  332. ;; the types don't match. In that case the arguments are all on the
  333. ;; stack, and nothing else is on the stack.
  334. ;;
  335. ;; 3. Alternately it's possible that we're between a primitive call
  336. ;; and its associated return. In that case, we won't be able to
  337. ;; say anything at all.
  338. (define* (frame-call-representation frame #:key top-frame?)
  339. (let* ((ip (frame-instruction-pointer frame))
  340. (info (find-program-debug-info ip))
  341. (nlocals (frame-num-locals frame)))
  342. (define (find-slot i bindings)
  343. (match bindings
  344. (() #f)
  345. (((and binding ($ <binding> frame idx name slot)) . bindings)
  346. (if (< idx i)
  347. (find-slot i bindings)
  348. (and (= idx i) binding)))))
  349. (define (local-ref i bindings)
  350. (cond
  351. ((not bindings)
  352. ;; This case is only hit for primitives and application
  353. ;; arguments.
  354. (frame-local-ref frame i 'scm))
  355. ((find-slot i bindings)
  356. => (lambda (binding)
  357. (let ((val (frame-local-ref frame (binding-slot binding)
  358. (binding-representation binding))))
  359. ;; It could be that there's a value that isn't clobbered
  360. ;; by a call but that isn't live after a call either. In
  361. ;; that case, if GC runs during the call, the value will
  362. ;; be collected, and on the stack it will be replaced
  363. ;; with the unspecified value. Assume that clobbering
  364. ;; values is more likely than passing the unspecified
  365. ;; value as an argument, and replace unspecified with _,
  366. ;; as if the binding were not available.
  367. (if (unspecified? val) '_ val))))
  368. (else
  369. '_)))
  370. (define (application-arguments)
  371. ;; Case 1.
  372. (map (lambda (local) (local-ref local #f))
  373. ;; Cdr past the 0th local, which is the procedure.
  374. (cdr (iota nlocals))))
  375. (define (reconstruct-arguments bindings nreq nopt kw has-rest? local)
  376. ;; Case 2.
  377. (cond
  378. ((positive? nreq)
  379. (cons (local-ref local bindings)
  380. (reconstruct-arguments bindings
  381. (1- nreq) nopt kw has-rest? (1+ local))))
  382. ((positive? nopt)
  383. (cons (local-ref local bindings)
  384. (reconstruct-arguments bindings
  385. nreq (1- nopt) kw has-rest? (1+ local))))
  386. ((pair? kw)
  387. (cons* (caar kw) (local-ref (cdar kw) bindings)
  388. (reconstruct-arguments bindings
  389. nreq nopt (cdr kw) has-rest? (1+ local))))
  390. (has-rest?
  391. (local-ref local bindings))
  392. (else
  393. '())))
  394. (cons
  395. (or (frame-procedure-name frame #:info info) '_)
  396. (cond
  397. ((find-program-arity ip)
  398. => (lambda (arity)
  399. (if (and top-frame? (eqv? ip (arity-low-pc arity)))
  400. (application-arguments)
  401. (reconstruct-arguments
  402. (available-bindings frame arity ip top-frame?)
  403. (arity-nreq arity)
  404. (arity-nopt arity)
  405. (arity-keyword-args arity)
  406. (arity-has-rest? arity)
  407. (if (arity-has-closure? arity) 1 0)))))
  408. ((and (primitive-code? ip)
  409. (program-arguments-alist (frame-local-ref frame 0 'scm) ip))
  410. => (lambda (args)
  411. (match args
  412. ((('required . req)
  413. ('optional . opt)
  414. ('keyword . kw)
  415. ('allow-other-keys? . _)
  416. ('rest . rest))
  417. (reconstruct-arguments #f
  418. (length req) (length opt) kw rest 1)))))
  419. (else
  420. (application-arguments))))))
  421. ;;; Misc
  422. ;;;
  423. (define (frame-environment frame)
  424. (map (lambda (binding)
  425. (cons (binding-name binding) (binding-ref binding)))
  426. (frame-bindings frame)))
  427. (define (frame-object-binding frame obj)
  428. (do ((bs (frame-bindings frame) (cdr bs)))
  429. ((or (null? bs) (eq? obj (binding-ref (car bs))))
  430. (and (pair? bs) (car bs)))))
  431. (define (frame-object-name frame obj)
  432. (cond ((frame-object-binding frame obj) => binding-name)
  433. (else #f)))