1 ;;;; the definition of non-local exit for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;; Make an environment-live stack TN for saving the SP for NLX entry.
15 (!def-vm-support-routine make-nlx-sp-tn (env)
17 (make-representation-tn *fixnum-primitive-type* any-reg-sc-number)
20 ;;; Make a TN for the argument count passing location for a non-local entry.
21 (!def-vm-support-routine make-nlx-entry-argument-start-location ()
22 (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ebx-offset))
24 (defun catch-block-ea (tn)
25 (aver (sc-is tn catch-block))
26 (make-ea :dword :base ebp-tn
27 :disp (- (* (+ (tn-offset tn) catch-block-size) word-bytes))))
30 ;;;; Save and restore dynamic environment.
32 ;;;; These VOPs are used in the reentered function to restore the
33 ;;;; appropriate dynamic environment. Currently we only save the
34 ;;;; Current-Catch, the eval stack pointer, and the alien stack
37 ;;;; We don't need to save/restore the current unwind-protect, since
38 ;;;; unwind-protects are implicitly processed during unwinding.
40 ;;;; We don't need to save the BSP, because that is handled automatically.
42 ;;; Return a list of TNs that can be used to snapshot the dynamic state for
43 ;;; use with the Save/Restore-Dynamic-Environment VOPs.
44 (!def-vm-support-routine make-dynamic-state-tns ()
45 (make-n-tns 3 *backend-t-primitive-type*))
47 (define-vop (save-dynamic-state)
48 (:results (catch :scs (descriptor-reg))
49 (eval :scs (descriptor-reg))
50 (alien-stack :scs (descriptor-reg)))
52 (load-symbol-value catch *current-catch-block*)
53 (load-symbol-value eval *eval-stack-top*)
54 (load-symbol-value alien-stack *alien-stack*)))
56 (define-vop (restore-dynamic-state)
57 (:args (catch :scs (descriptor-reg))
58 (eval :scs (descriptor-reg))
59 (alien-stack :scs (descriptor-reg)))
61 (store-symbol-value catch *current-catch-block*)
62 (store-symbol-value eval *eval-stack-top*)
63 (store-symbol-value alien-stack *alien-stack*)))
65 (define-vop (current-stack-pointer)
66 (:results (res :scs (any-reg control-stack)))
70 (define-vop (current-binding-pointer)
71 (:results (res :scs (any-reg descriptor-reg)))
73 (load-symbol-value res *binding-stack-pointer*)))
75 ;;;; unwind block hackery
77 ;;; Compute the address of the catch block from its TN, then store into the
78 ;;; block the current Fp, Env, Unwind-Protect, and the entry PC.
79 (define-vop (make-unwind-block)
82 (:temporary (:sc unsigned-reg) temp)
83 (:results (block :scs (any-reg)))
85 (inst lea block (catch-block-ea tn))
86 (load-symbol-value temp *current-unwind-protect-block*)
87 (storew temp block unwind-block-current-uwp-slot)
88 (storew ebp-tn block unwind-block-current-cont-slot)
89 (storew (make-fixup nil :code-object entry-label)
90 block catch-block-entry-pc-slot)))
92 ;;; Like Make-Unwind-Block, except that we also store in the specified tag, and
93 ;;; link the block into the Current-Catch list.
94 (define-vop (make-catch-block)
96 (tag :scs (descriptor-reg) :to (:result 1)))
98 (:results (block :scs (any-reg)))
99 (:temporary (:sc descriptor-reg) temp)
101 (inst lea block (catch-block-ea tn))
102 (load-symbol-value temp *current-unwind-protect-block*)
103 (storew temp block unwind-block-current-uwp-slot)
104 (storew ebp-tn block unwind-block-current-cont-slot)
105 (storew (make-fixup nil :code-object entry-label)
106 block catch-block-entry-pc-slot)
107 (storew tag block catch-block-tag-slot)
108 (load-symbol-value temp *current-catch-block*)
109 (storew temp block catch-block-previous-catch-slot)
110 (store-symbol-value block *current-catch-block*)))
112 ;;; Just set the current unwind-protect to TN's address. This instantiates an
113 ;;; unwind block as an unwind-protect.
114 (define-vop (set-unwind-protect)
116 (:temporary (:sc unsigned-reg) new-uwp)
118 (inst lea new-uwp (catch-block-ea tn))
119 (store-symbol-value new-uwp *current-unwind-protect-block*)))
121 (define-vop (unlink-catch-block)
122 (:temporary (:sc unsigned-reg) block)
124 (:translate %catch-breakup)
126 (load-symbol-value block *current-catch-block*)
127 (loadw block block catch-block-previous-catch-slot)
128 (store-symbol-value block *current-catch-block*)))
130 (define-vop (unlink-unwind-protect)
131 (:temporary (:sc unsigned-reg) block)
133 (:translate %unwind-protect-breakup)
135 (load-symbol-value block *current-unwind-protect-block*)
136 (loadw block block unwind-block-current-uwp-slot)
137 (store-symbol-value block *current-unwind-protect-block*)))
140 (define-vop (nlx-entry)
141 ;; Note: we can't list an sc-restriction, 'cause any load vops would
142 ;; be inserted before the return-pc label.
146 (:results (values :more t))
147 (:temporary (:sc descriptor-reg) move-temp)
149 (:save-p :force-to-stack)
153 (note-this-location vop :non-local-entry)
154 (cond ((zerop nvals))
156 (let ((no-values (gen-label)))
157 (inst mov (tn-ref-tn values) nil-value)
158 (inst jecxz no-values)
159 (loadw (tn-ref-tn values) start -1)
160 (emit-label no-values)))
162 (collect ((defaults))
164 (tn-ref values (tn-ref-across tn-ref)))
166 (let ((default-lab (gen-label))
167 (tn (tn-ref-tn tn-ref)))
168 (defaults (cons default-lab tn))
170 (inst cmp count (fixnumize i))
171 (inst jmp :le default-lab)
173 ((descriptor-reg any-reg)
174 (loadw tn start (- (1+ i))))
176 (loadw move-temp start (- (1+ i)))
177 (inst mov tn move-temp)))))
178 (let ((defaulting-done (gen-label)))
179 (emit-label defaulting-done)
180 (assemble (*elsewhere*)
181 (dolist (def (defaults))
182 (emit-label (car def))
183 (inst mov (cdr def) nil-value))
184 (inst jmp defaulting-done))))))
185 (inst mov esp-tn sp)))
187 (define-vop (nlx-entry-multiple)
191 ;; Again, no SC restrictions for the args, 'cause the loading would
192 ;; happen before the entry label.
194 (:temporary (:sc unsigned-reg :offset ecx-offset :from (:argument 2)) ecx)
195 (:temporary (:sc unsigned-reg :offset esi-offset) esi)
196 (:temporary (:sc unsigned-reg :offset edi-offset) edi)
197 (:results (result :scs (any-reg) :from (:argument 0))
198 (num :scs (any-reg control-stack)))
199 (:save-p :force-to-stack)
203 (note-this-location vop :non-local-entry)
205 (inst lea esi (make-ea :dword :base source :disp (- word-bytes)))
206 ;; The 'top' arg contains the %esp value saved at the time the
207 ;; catch block was created and points to where the thrown values
212 (inst sub edi word-bytes)
213 (move ecx count) ; fixnum words == bytes
215 (inst shr ecx word-shift) ; word count for <rep movs>
216 ;; If we got zero, we be done.
224 ;; Reset the CSP at last moved arg.
225 (inst lea esp-tn (make-ea :dword :base edi :disp word-bytes))))
228 ;;; This VOP is just to force the TNs used in the cleanup onto the stack.
229 (define-vop (uwp-entry)
231 (:save-p :force-to-stack)
232 (:results (block) (start) (count))
233 (:ignore block start count)
237 (note-this-location vop :non-local-entry)))