1 ;;; Guile VM frame functions
3 ;;; Copyright (C) 2001, 2005, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (system vm frame)
22 #:use-module (system base pmatch)
23 #:use-module (system vm program)
24 #:use-module (system vm debug)
25 #:use-module (ice-9 match)
26 #:export (frame-bindings
28 frame-binding-ref frame-binding-set!
29 frame-call-representation
31 frame-object-binding frame-object-name))
33 (define (frame-bindings frame)
34 (let ((p (frame-procedure frame)))
35 (program-bindings-for-ip p (frame-instruction-pointer frame))))
37 (define (frame-lookup-binding frame var)
38 (let lp ((bindings (frame-bindings frame)))
39 (cond ((null? bindings)
41 ((eq? (binding:name (car bindings)) var)
44 (lp (cdr bindings))))))
46 (define (frame-binding-set! frame var val)
47 (frame-local-set! frame
49 (or (frame-lookup-binding frame var)
50 (error "variable not bound in frame" var frame)))
53 (define (frame-binding-ref frame var)
54 (frame-local-ref frame
56 (or (frame-lookup-binding frame var)
57 (error "variable not bound in frame" var frame)))))
60 ;; This function is always called to get some sort of representation of the
61 ;; frame to present to the user, so let's do the logical thing and dispatch to
62 ;; frame-call-representation.
63 (define (frame-arguments frame)
64 (cdr (frame-call-representation frame)))
72 ;; Basically there are two cases to deal with here:
74 ;; 1. We've already parsed the arguments, and bound them to local
75 ;; variables. In a standard (lambda (a b c) ...) call, this doesn't
76 ;; involve any argument shuffling; but with rest, optional, or
77 ;; keyword arguments, the arguments as given to the procedure may
78 ;; not correspond to what's on the stack. We reconstruct the
79 ;; arguments using e.g. for the case above: `(,a ,b ,c). This works
80 ;; for rest arguments too: (a b . c) => `(,a ,b . ,c)
82 ;; 2. We have failed to parse the arguments. Perhaps it's the wrong
83 ;; number of arguments, or perhaps we're doing a typed dispatch and
84 ;; the types don't match. In that case the arguments are all on the
85 ;; stack, and nothing else is on the stack.
87 (define (frame-call-representation frame)
88 (let* ((ip (frame-instruction-pointer frame))
89 (info (find-program-debug-info ip))
90 (nlocals (frame-num-locals frame))
91 (closure (frame-procedure frame)))
94 (frame-local-ref frame i)
95 ;; Let's not error here, as we are called during backtraces.
97 (define (reconstruct-arguments nreq nopt kw has-rest? local)
100 (cons (local-ref local)
101 (reconstruct-arguments (1- nreq) nopt kw has-rest? (1+ local))))
103 (cons (local-ref local)
104 (reconstruct-arguments nreq (1- nopt) kw has-rest? (1+ local))))
106 (cons* (caar kw) (local-ref (cdar kw))
107 (reconstruct-arguments nreq nopt (cdr kw) has-rest? (1+ local))))
113 (or (and=> info program-debug-info-name)
114 (procedure-name closure)
116 ;; No need to give source info, as backtraces will already
117 ;; take care of that.
118 (format #f "#<procedure ~a>"
119 (number->string (program-debug-info-addr info) 16)))
120 (procedure-name closure)
123 ((find-program-arity ip)
126 (reconstruct-arguments (arity-nreq arity)
128 (arity-keyword-args arity)
129 (arity-has-rest? arity)
131 ((and (primitive? closure)
132 (program-arguments-alist closure ip))
138 ('allow-other-keys? . _)
141 (reconstruct-arguments (length req) (length opt) kw rest 1)))))
145 ;; Cdr past the 0th local, which is the procedure.
146 (cdr (iota nlocals))))))))
153 (define (frame-environment frame)
154 (map (lambda (binding)
155 (cons (binding:name binding) (frame-binding-ref frame binding)))
156 (frame-bindings frame)))
158 (define (frame-object-binding frame obj)
159 (do ((bs (frame-bindings frame) (cdr bs)))
160 ((or (null? bs) (eq? obj (frame-binding-ref frame (car bs))))
161 (and (pair? bs) (car bs)))))
163 (define (frame-object-name frame obj)
164 (cond ((frame-object-binding frame obj) => binding:name)