1 | ;;;-*-Mode: LISP; Package: CCL -*- |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2006 Clozure Associates and contributors |
---|
4 | ;;; This file is part of OpenMCL. |
---|
5 | ;;; |
---|
6 | ;;; OpenMCL is licensed under the terms of the Lisp Lesser GNU Public |
---|
7 | ;;; License , known as the LLGPL and distributed with OpenMCL as the |
---|
8 | ;;; file "LICENSE". The LLGPL consists of a preamble and the LGPL, |
---|
9 | ;;; which is distributed with OpenMCL as the file "LGPL". Where these |
---|
10 | ;;; conflict, the preamble takes precedence. |
---|
11 | ;;; |
---|
12 | ;;; OpenMCL is referenced in the preamble as the "LIBRARY." |
---|
13 | ;;; |
---|
14 | ;;; The LLGPL is also available online at |
---|
15 | ;;; http://opensource.franz.com/preamble.html |
---|
16 | |
---|
17 | |
---|
18 | (in-package "CCL") |
---|
19 | |
---|
20 | |
---|
21 | ;;; Returns two values: |
---|
22 | ;;; [nil, nil] if it can be reliably determined that function uses no registers at PC |
---|
23 | ;;; [mask, saved-location] if it can be reliably determined that the registers specified by "mask" |
---|
24 | ;;; were saved at "saved-location" in the function's stack frame |
---|
25 | ;;; [mask, nil] if registers in "mask" MAY have been saved, but we don't know how to restore them |
---|
26 | ;;; (perhaps because the "at-pc" argument wasn't specified. |
---|
27 | |
---|
28 | |
---|
29 | (defun registers-used-by (function &optional at-pc) |
---|
30 | (multiple-value-bind (mask stack-location rpc) |
---|
31 | (%function-register-usage function) |
---|
32 | (if (null mask) |
---|
33 | (values nil nil) |
---|
34 | (values (canonicalize-register-mask mask) (if (and at-pc rpc (> at-pc rpc)) stack-location))))) |
---|
35 | |
---|
36 | (defun canonicalize-register-mask (mask) |
---|
37 | (dpb (ldb (byte 2 14) mask) (byte 2 2) (ldb (byte 2 11) mask))) |
---|
38 | |
---|
39 | (defun xcf-p (p) |
---|
40 | (eql 0 (%fixnum-ref p x8664::lisp-frame.return-address))) |
---|
41 | |
---|
42 | (defun %current-xcf () |
---|
43 | (do* ((q (%get-frame-ptr) (%%frame-backlink q))) |
---|
44 | ((zerop q)) |
---|
45 | (declare (fixnum q)) |
---|
46 | (when (xcf-p q) (return q)))) |
---|
47 | |
---|
48 | ;;; Try to determine the program counter value, relative to an xcf's nominal function. |
---|
49 | (defun pc-from-xcf (xcf) |
---|
50 | (let* ((nominal-function (%fixnum-ref xcf x8664::xcf.nominal-function)) |
---|
51 | (containing-object (%fixnum-ref xcf x8664::xcf.containing-object))) |
---|
52 | (when (typep nominal-function 'function) |
---|
53 | (if (eq containing-object (function-to-function-vector nominal-function)) |
---|
54 | (- (%fixnum-ref xcf x8664::xcf.relative-pc) |
---|
55 | x8664::tag-function) |
---|
56 | (let* ((tra (%fixnum-ref xcf x8664::xcf.ra0))) |
---|
57 | (if (and (= (lisptag tra) x8664::tag-tra) |
---|
58 | (eq nominal-function (%return-address-function tra))) |
---|
59 | (%return-address-offset tra))))))) |
---|
60 | |
---|
61 | (defun cfp-lfun (p) |
---|
62 | (if (xcf-p p) |
---|
63 | (values |
---|
64 | (%fixnum-ref p x8664::xcf.nominal-function) |
---|
65 | (pc-from-xcf p)) |
---|
66 | (%cfp-lfun p))) |
---|
67 | |
---|
68 | ;;; On PPC, some frames on the control stack are associated with catch |
---|
69 | ;;; frames rather than with function calls. The whole concept doesn't |
---|
70 | ;;; really apply here (e.g., nothing we encounter while walking frame |
---|
71 | ;;; pointer links belongs to a catch frame.) |
---|
72 | (defun catch-csp-p (p context) |
---|
73 | (declare (ignore p context))) |
---|
74 | |
---|
75 | (defun %raw-frame-ref (frame context idx bad) |
---|
76 | (declare (fixnum frame idx)) |
---|
77 | (let* ((base (parent-frame frame context)) |
---|
78 | (raw-size (- base frame))) |
---|
79 | (declare (fixnum base raw-size)) |
---|
80 | (if (and (>= idx 0) |
---|
81 | (< idx raw-size)) |
---|
82 | (let* ((addr (- (the fixnum (1- base)) |
---|
83 | idx))) |
---|
84 | (multiple-value-bind (db-count first-db last-db) |
---|
85 | (count-db-links-in-frame frame base context) |
---|
86 | (let* ((is-db-link |
---|
87 | (unless (zerop db-count) |
---|
88 | (do* ((last last-db (previous-db-link last first-db))) |
---|
89 | ((null last)) |
---|
90 | (when (= addr last) |
---|
91 | (return t)))))) |
---|
92 | (if is-db-link |
---|
93 | (oldest-binding-frame-value context addr) |
---|
94 | (%fixnum-ref addr))))) |
---|
95 | bad))) |
---|
96 | |
---|
97 | (defun %raw-frame-set (frame context idx new) |
---|
98 | (declare (fixnum frame idx)) |
---|
99 | (let* ((base (parent-frame frame context)) |
---|
100 | (raw-size (- base frame))) |
---|
101 | (declare (fixnum base raw-size)) |
---|
102 | (if (and (>= idx 0) |
---|
103 | (< idx raw-size)) |
---|
104 | (let* ((addr (- (the fixnum (1- base)) |
---|
105 | idx))) |
---|
106 | (multiple-value-bind (db-count first-db last-db) |
---|
107 | (count-db-links-in-frame frame base context) |
---|
108 | (let* ((is-db-link |
---|
109 | (unless (zerop db-count) |
---|
110 | (do* ((last last-db (previous-db-link last first-db))) |
---|
111 | ((null last)) |
---|
112 | (when (= addr last) |
---|
113 | (return t)))))) |
---|
114 | (if is-db-link |
---|
115 | (setf (oldest-binding-frame-value context addr) new) |
---|
116 | (setf (%fixnum-ref addr) new)))))))) |
---|
117 | |
---|
118 | (defun %stack< (index1 index2 &optional context) |
---|
119 | (let* ((tcr (if context (bt.tcr context) (%current-tcr))) |
---|
120 | (vs-area (%fixnum-ref tcr target::tcr.vs-area))) |
---|
121 | (and (%ptr-in-area-p index1 vs-area) |
---|
122 | (%ptr-in-area-p index2 vs-area) |
---|
123 | (< (the fixnum index1) (the fixnum index2))))) |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | (defun register-number->saved-register-index (regnum) |
---|
129 | (ecase regnum |
---|
130 | (#.x8664::save3 0) |
---|
131 | (#.x8664::save2 1) |
---|
132 | (#.x8664::save1 2) |
---|
133 | (#.x8664::save0 3))) |
---|
134 | |
---|
135 | |
---|
136 | (defun get-register-value (address last-catch index) |
---|
137 | (if address |
---|
138 | (%fixnum-ref address) |
---|
139 | (uvref last-catch (+ index target::catch-frame.save-save3-cell)))) |
---|
140 | |
---|
141 | ;;; Inverse of get-register-value |
---|
142 | |
---|
143 | (defun set-register-value (value address last-catch index) |
---|
144 | (if address |
---|
145 | (%fixnum-set address value) |
---|
146 | (setf (uvref last-catch (+ index target::catch-frame.save-save3-cell)) |
---|
147 | value))) |
---|
148 | |
---|
149 | (defun %find-register-argument-value (context cfp regval bad) |
---|
150 | (let* ((last-catch (last-catch-since cfp context)) |
---|
151 | (index (register-number->saved-register-index regval))) |
---|
152 | (do* ((frame cfp (child-frame frame context)) |
---|
153 | (first t)) |
---|
154 | ((null frame)) |
---|
155 | (if (xcf-p frame) |
---|
156 | (with-macptrs (xp) |
---|
157 | (%setf-macptr-to-object xp (%fixnum-ref frame x8664::xcf.xp)) |
---|
158 | (return-from %find-register-argument-value |
---|
159 | (encoded-gpr-lisp xp regval))) |
---|
160 | (progn |
---|
161 | (unless first |
---|
162 | (multiple-value-bind (lfun pc) |
---|
163 | (cfp-lfun frame) |
---|
164 | (when lfun |
---|
165 | (multiple-value-bind (mask where) |
---|
166 | (registers-used-by lfun pc) |
---|
167 | (when (if mask (logbitp index mask)) |
---|
168 | (incf where (logcount (logandc2 mask (1- (ash 1 (1+ index)))))) |
---|
169 | |
---|
170 | |
---|
171 | (return-from %find-register-argument-value |
---|
172 | (raw-frame-ref frame context where bad))))))) |
---|
173 | (setq first nil)))) |
---|
174 | (get-register-value nil last-catch index))) |
---|
175 | |
---|
176 | (defun %set-register-argument-value (context cfp regval new) |
---|
177 | (let* ((last-catch (last-catch-since cfp context)) |
---|
178 | (index (register-number->saved-register-index regval))) |
---|
179 | (do* ((frame cfp (child-frame frame context)) |
---|
180 | (first t)) |
---|
181 | ((null frame)) |
---|
182 | (if (xcf-p frame) |
---|
183 | (with-macptrs (xp) |
---|
184 | (%setf-macptr-to-object xp (%fixnum-ref frame x8664::xcf.xp)) |
---|
185 | (return-from %set-register-argument-value |
---|
186 | (setf (encoded-gpr-lisp xp regval) new))) |
---|
187 | (progn |
---|
188 | (unless first |
---|
189 | (multiple-value-bind (lfun pc) |
---|
190 | (cfp-lfun frame) |
---|
191 | (when lfun |
---|
192 | (multiple-value-bind (mask where) |
---|
193 | (registers-used-by lfun pc) |
---|
194 | (when (if mask (logbitp index mask)) |
---|
195 | (incf where (logcount (logandc2 mask (1- (ash 1 (1+ index)))))) |
---|
196 | |
---|
197 | (return-from %set-register-argument-value |
---|
198 | (raw-frame-set frame context where new))))))) |
---|
199 | (setq first nil)))) |
---|
200 | (set-register-value new nil last-catch index))) |
---|
201 | |
---|
202 | ;;; Used for printing only. |
---|
203 | (defun index->address (p) |
---|
204 | (ldb (byte #+32-bit-target 32 #+64-bit-target 64 0) (ash p target::fixnumshift))) |
---|
205 | |
---|
206 | (defun vsp-limits (frame context) |
---|
207 | (let* ((parent (parent-frame frame context))) |
---|
208 | (if (xcf-p frame) |
---|
209 | (values (+ frame (ash x8664::xcf.size (- x8664::word-shift))) |
---|
210 | parent) |
---|
211 | (let* ((tra (%fixnum-ref frame x8664::lisp-frame.return-address))) |
---|
212 | (values (+ frame 2 (if (eq tra (%get-kernel-global ret1valaddr)) 1 0)) |
---|
213 | parent))))) |
---|
214 | |
---|
215 | (defun last-catch-since (fp context) |
---|
216 | (let* ((tcr (if context (bt.tcr context) (%current-tcr))) |
---|
217 | (catch (%catch-top tcr)) |
---|
218 | (last-catch nil)) |
---|
219 | (loop |
---|
220 | (unless catch (return last-catch)) |
---|
221 | (let ((catch-fp (uvref catch target::catch-frame.rbp-cell))) |
---|
222 | (when (%stack< fp catch-fp context) (return last-catch)) |
---|
223 | (setq last-catch catch |
---|
224 | catch (next-catch catch)))))) |
---|
225 | |
---|
226 | (defun match-local-name (cellno info pc) |
---|
227 | (when info |
---|
228 | (let* ((syms (%car info)) |
---|
229 | (ptrs (%cdr info))) |
---|
230 | (dotimes (i (length syms)) |
---|
231 | (let ((j (%i+ i (%i+ i i )))) |
---|
232 | (and (eq (uvref ptrs j) (%ilogior (%ilsl (+ 6 target::word-shift) cellno) #o77)) |
---|
233 | (%i>= pc (uvref ptrs (%i+ j 1))) |
---|
234 | (%i< pc (uvref ptrs (%i+ j 2))) |
---|
235 | (return (aref syms i)))))))) |
---|