1fa25bc3b6238e20c6db4525c5194e27ad2cee1d
[bpt/guile.git] / module / system / vm / frame.scm
1 ;;; Guile VM frame functions
2
3 ;;; Copyright (C) 2001, 2005, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
4 ;;;
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.
9 ;;;
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.
14 ;;;
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
18
19 ;;; Code:
20
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
27 frame-lookup-binding
28 frame-binding-ref frame-binding-set!
29 frame-call-representation
30 frame-environment
31 frame-object-binding frame-object-name))
32
33 (define (frame-bindings frame)
34 (let ((p (frame-procedure frame)))
35 (program-bindings-for-ip p (frame-instruction-pointer frame))))
36
37 (define (frame-lookup-binding frame var)
38 (let lp ((bindings (frame-bindings frame)))
39 (cond ((null? bindings)
40 #f)
41 ((eq? (binding:name (car bindings)) var)
42 (car bindings))
43 (else
44 (lp (cdr bindings))))))
45
46 (define (frame-binding-set! frame var val)
47 (frame-local-set! frame
48 (binding:index
49 (or (frame-lookup-binding frame var)
50 (error "variable not bound in frame" var frame)))
51 val))
52
53 (define (frame-binding-ref frame var)
54 (frame-local-ref frame
55 (binding:index
56 (or (frame-lookup-binding frame var)
57 (error "variable not bound in frame" var frame)))))
58
59
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)))
65
66
67 \f
68 ;;;
69 ;;; Pretty printing
70 ;;;
71
72 ;; Basically there are two cases to deal with here:
73 ;;
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)
81 ;;
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.
86
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)))
92 (define (local-ref i)
93 (if (< i nlocals)
94 (frame-local-ref frame i)
95 ;; Let's not error here, as we are called during backtraces.
96 '???))
97 (define (reconstruct-arguments nreq nopt kw has-rest? local)
98 (cond
99 ((positive? nreq)
100 (cons (local-ref local)
101 (reconstruct-arguments (1- nreq) nopt kw has-rest? (1+ local))))
102 ((positive? nopt)
103 (cons (local-ref local)
104 (reconstruct-arguments nreq (1- nopt) kw has-rest? (1+ local))))
105 ((pair? kw)
106 (cons* (caar kw) (local-ref (cdar kw))
107 (reconstruct-arguments nreq nopt (cdr kw) has-rest? (1+ local))))
108 (has-rest?
109 (local-ref local))
110 (else
111 '())))
112 (cons
113 (or (and=> info program-debug-info-name)
114 (procedure-name closure)
115 (and info
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)
121 closure)
122 (cond
123 ((find-program-arity ip)
124 => (lambda (arity)
125 ;; case 1
126 (reconstruct-arguments (arity-nreq arity)
127 (arity-nopt arity)
128 (arity-keyword-args arity)
129 (arity-has-rest? arity)
130 1)))
131 ((and (primitive? closure)
132 (program-arguments-alist closure ip))
133 => (lambda (args)
134 (match args
135 ((('required . req)
136 ('optional . opt)
137 ('keyword . kw)
138 ('allow-other-keys? . _)
139 ('rest . rest))
140 ;; case 1
141 (reconstruct-arguments (length req) (length opt) kw rest 1)))))
142 (else
143 ;; case 2
144 (map local-ref
145 ;; Cdr past the 0th local, which is the procedure.
146 (cdr (iota nlocals))))))))
147
148
149 \f
150 ;;; Misc
151 ;;;
152
153 (define (frame-environment frame)
154 (map (lambda (binding)
155 (cons (binding:name binding) (frame-binding-ref frame binding)))
156 (frame-bindings frame)))
157
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)))))
162
163 (define (frame-object-name frame obj)
164 (cond ((frame-object-binding frame obj) => binding:name)
165 (else #f)))