1 ;;;; various useful macros for generating Sparc code
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 ;;; Instruction-like macros.
16 (defmacro move (dst src)
17 "Move SRC into DST unless they are location=."
18 (once-only ((n-dst dst)
20 `(unless (location= ,n-dst ,n-src)
21 (inst move ,n-dst ,n-src))))
25 `(defmacro ,op (object base &optional (offset 0) (lowtag 0))
26 `(inst ,',inst ,object ,base (- (ash ,offset ,,shift) ,lowtag)))))
27 (def loadw ld word-shift)
28 (def storew st word-shift))
30 (defmacro load-symbol (reg symbol)
31 `(inst add ,reg null-tn (static-symbol-offset ,symbol)))
35 (let ((loader (intern (concatenate 'simple-string
38 (storer (intern (concatenate 'simple-string
41 (offset (intern (concatenate 'simple-string
45 (find-package "SB!VM"))))
47 (defmacro ,loader (reg symbol)
48 `(inst ld ,reg null-tn
49 (+ (static-symbol-offset ',symbol)
50 (ash ,',offset word-shift)
51 (- other-pointer-lowtag))))
52 (defmacro ,storer (reg symbol)
53 `(inst st ,reg null-tn
54 (+ (static-symbol-offset ',symbol)
55 (ash ,',offset word-shift)
56 (- other-pointer-lowtag))))))))
60 (defmacro load-type (target source &optional (offset 0))
62 "Loads the type bits of a pointer into target independent of
63 byte-ordering issues."
64 (once-only ((n-target target)
67 ;; FIXME: although I don't understand entirely, I'm going to do
68 ;; what whn does in x86/macros.lisp -- Christophe
69 (ecase *backend-byte-order*
71 `(inst ldub ,n-target ,n-source ,n-offset))
73 `(inst ldub ,n-target ,n-source (+ ,n-offset (1- n-word-bytes)))))))
75 ;;; Macros to handle the fact that we cannot use the machine native call and
76 ;;; return instructions.
78 (defmacro lisp-jump (fun)
79 "Jump to the lisp function FUNCTION. LIP is an interior-reg temporary."
82 (- (ash simple-fun-code-offset word-shift) fun-pointer-lowtag))
85 (defmacro lisp-return (return-pc &key (offset 0) (frob-code t))
86 "Return to RETURN-PC."
89 (- (* (1+ ,offset) n-word-bytes) other-pointer-lowtag))
91 `(move code-tn ,return-pc)
94 (defmacro emit-return-pc (label)
95 "Emit a return-pc header word. LABEL is the label to use for this return-pc."
97 (emit-alignment n-lowtag-bits)
99 (inst lra-header-word)))
105 ;;; Move a stack TN to a register and vice-versa.
106 (defmacro load-stack-tn (reg stack)
109 (let ((offset (tn-offset stack)))
112 (loadw reg cfp-tn offset))))))
114 (defmacro store-stack-tn (stack reg)
115 `(let ((stack ,stack)
117 (let ((offset (tn-offset stack)))
120 (storew reg cfp-tn offset))))))
122 (defmacro maybe-load-stack-tn (reg reg-or-stack)
123 "Move the TN Reg-Or-Stack into Reg if it isn't already there."
124 (once-only ((n-reg reg)
125 (n-stack reg-or-stack))
127 ((any-reg descriptor-reg)
129 ((any-reg descriptor-reg)
130 (move ,n-reg ,n-stack))
132 (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
136 ;;;; Storage allocation:
138 ;;;; Allocation macro
140 ;;;; This macro does the appropriate stuff to allocate space.
142 ;;;; The allocated space is stored in RESULT-TN with the lowtag LOWTAG
143 ;;;; applied. The amount of space to be allocated is SIZE bytes (which
144 ;;;; must be a multiple of the lisp object size).
145 (defmacro allocation (result-tn size lowtag &key stack-p temp-tn)
147 ;; A temp register is needed to do inline allocation. TEMP-TN, in
148 ;; this case, can be any register, since it holds a double-word
149 ;; aligned address (essentially a fixnum).
151 ;; We assume we're in a pseudo-atomic so the pseudo-atomic bit is
157 ;; The control stack grows up, so round up CSP to a
158 ;; multiple of 8 (lispobj size). Use that as the
159 ;; allocation pointer. Then add SIZE bytes to the
160 ;; allocation and set CSP to that, so we have the desired
163 ;; Make sure the temp-tn is a non-descriptor register!
164 (assert (and ,temp-tn (sc-is ,temp-tn non-descriptor-reg)))
166 ;; temp-tn is csp-tn rounded up to a multiple of 8 (lispobj size)
168 ;; For the benefit of future historians, this is how CMUCL does the
169 ;; align-csp (I think their version is branch free only because
170 ;; they simply don't worry about zeroing the pad word):
171 #+nil (inst add ,temp-tn csp-tn sb!vm:lowtag-mask)
172 #+nil (inst andn ,temp-tn sb!vm:lowtag-mask)
174 ;; Set the result to temp-tn, with appropriate lowtag
175 (inst or ,result-tn csp-tn ,lowtag)
177 ;; Allocate the desired space on the stack.
179 ;; FIXME: Can't allocate on stack if SIZE is too large.
180 ;; Need to rearrange this code.
181 (inst add csp-tn ,size))
184 ;; Normal allocation to the heap -- cheneygc version.
186 ;; On cheneygc, the alloc-tn currently has the pseudo-atomic bit.
187 ;; If the lowtag also has a 1 bit in the same position, we're all set.
189 ;; See comment in PSEUDO-ATOMIC-FLAG.
190 ((logbitp (1- n-lowtag-bits) ,lowtag)
191 (inst or ,result-tn alloc-tn ,lowtag)
192 (inst add alloc-tn ,size))
194 ;; Otherwise, we need to zap out the lowtag from alloc-tn, and then
198 (inst andn ,result-tn alloc-tn lowtag-mask)
199 (inst or ,result-tn ,lowtag)
200 (inst add alloc-tn ,size))
202 ;; Normal allocation to the heap -- gencgc version.
204 ;; No need to worry about lowtag bits matching up here, since
205 ;; alloc-tn is just a "pseudo-atomic-bit-tn" now and we don't read
209 (inst li ,temp-tn (make-fixup "boxed_region" :foreign))
210 (loadw ,result-tn ,temp-tn 0) ;boxed_region.free_pointer
211 (loadw ,temp-tn ,temp-tn 1) ;boxed_region.end_addr
213 (without-scheduling ()
214 (let ((done (gen-label))
215 (full-alloc (gen-label)))
216 ;; See if we can do an inline allocation. The updated
217 ;; free pointer should not point past the end of the
218 ;; current region. If it does, a full alloc needs to be
220 (inst add ,result-tn ,size)
222 ;; result-tn points to the new end of region. Did we go
223 ;; past the actual end of the region? If so, we need a
225 (inst cmp ,result-tn ,temp-tn)
226 (if (member :sparc-v9 *backend-subfeatures*)
227 (inst b :gtu full-alloc :pn)
228 (inst b :gtu full-alloc))
230 ;; Inline allocation worked, so update the free pointer
231 ;; and go. Should really do a swap instruction here to
232 ;; swap memory with a register.
234 ;; Kludge: We ought to have two distinct FLAG-TN and TEMP-TN
235 ;; here, to avoid the SUB and the TEMP-TN reload which is
236 ;; causing it. PPC gets it right.
237 (inst li ,temp-tn (make-fixup "boxed_region" :foreign))
238 (storew ,result-tn ,temp-tn 0)
241 (inst sub ,result-tn ,size)
243 (emit-label full-alloc)
244 ;; Full alloc via trap to the C allocator. Tell the
245 ;; allocator what the result-tn and size are, using the
246 ;; OR instruction. Then trap to the allocator.
247 (inst or zero-tn ,result-tn ,size)
248 ;; DFL: Not certain why we use two kinds of traps: T for p/a
249 ;; and UNIMP for all other traps. But the C code in the runtime
250 ;; for the UNIMP case is a lot nicer, so I'm hooking into that.
251 ;; (inst t :t allocation-trap)
252 (inst unimp allocation-trap)
255 ;; Set lowtag appropriately
256 (inst or ,result-tn ,lowtag))))))
258 (defmacro with-fixed-allocation ((result-tn temp-tn type-code size)
260 "Do stuff to allocate an other-pointer object of fixed Size with a single
261 word header having the specified Type-Code. The result is placed in
262 Result-TN, and Temp-TN is a non-descriptor temp (which may be randomly used
263 by the body.) The body is placed inside the PSEUDO-ATOMIC, and presumably
264 initializes the object."
266 (bug "empty &body in WITH-FIXED-ALLOCATION"))
267 (once-only ((result-tn result-tn) (temp-tn temp-tn)
268 (type-code type-code) (size size))
270 (allocation ,result-tn (pad-data-block ,size) other-pointer-lowtag
272 (inst li ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
273 (storew ,temp-tn ,result-tn 0 other-pointer-lowtag)
276 (defun align-csp (temp)
277 (let ((aligned (gen-label)))
278 ;; FIXME: why use a TEMP? Why not just ZERO-TN?
279 (inst andcc temp csp-tn lowtag-mask)
280 (if (member :sparc-v9 *backend-subfeatures*)
281 (inst b :eq aligned :pt)
282 (inst b :eq aligned))
283 (storew zero-tn csp-tn 0) ; sneaky use of delay slot
284 (inst add csp-tn csp-tn n-word-bytes)
285 (emit-label aligned)))
288 (eval-when (:compile-toplevel :load-toplevel :execute)
289 (defun emit-error-break (vop kind code values)
290 (let ((vector (gensym)))
293 (note-this-location vop :internal-error)))
295 (with-adjustable-vector (,vector)
296 (write-var-integer (error-number-or-lose ',code) ,vector)
297 ,@(mapcar #'(lambda (tn)
299 (write-var-integer (make-sc-offset (sc-number
304 (inst byte (length ,vector))
305 (dotimes (i (length ,vector))
306 (inst byte (aref ,vector i))))
307 (emit-alignment word-shift)))))
309 (defmacro error-call (vop error-code &rest values)
310 "Cause an error. ERROR-CODE is the error to cause."
312 (emit-error-break vop error-trap error-code values)))
315 (defmacro cerror-call (vop label error-code &rest values)
316 "Cause a continuable error. If the error is continued, execution resumes at
320 ,@(emit-error-break vop cerror-trap error-code values)))
322 (defmacro generate-error-code (vop error-code &rest values)
323 "Generate-Error-Code Error-code Value*
324 Emit code for an error with the specified Error-Code and context Values."
325 `(assemble (*elsewhere*)
326 (let ((start-lab (gen-label)))
327 (emit-label start-lab)
328 (error-call ,vop ,error-code ,@values)
331 (defmacro generate-cerror-code (vop error-code &rest values)
332 "Generate-CError-Code Error-code Value*
333 Emit code for a continuable error with the specified Error-Code and
334 context Values. If the error is continued, execution resumes after
335 the GENERATE-CERROR-CODE form."
336 (with-unique-names (continue error)
337 `(let ((,continue (gen-label)))
338 (emit-label ,continue)
339 (assemble (*elsewhere*)
340 (let ((,error (gen-label)))
342 (cerror-call ,vop ,continue ,error-code ,@values)
345 ;;; a handy macro for making sequences look atomic
346 (defmacro pseudo-atomic ((&optional) &rest forms)
349 ;; Set the pseudo-atomic flag.
350 (without-scheduling ()
351 (inst or alloc-tn 4))
353 ;; Reset the pseudo-atomic flag.
354 (without-scheduling ()
355 ;; Remove the pseudo-atomic flag.
356 (inst andn alloc-tn 4)
357 ;; Check to see if pseudo-atomic interrupted flag is set (bit 0 = 1).
358 (inst andcc zero-tn alloc-tn 3)
359 ;; The C code needs to process this correctly and fixup alloc-tn.
360 (inst t :ne pseudo-atomic-trap)))))
363 (def!macro with-pinned-objects ((&rest objects) &body body)
364 "Arrange with the garbage collector that the pages occupied by
365 OBJECTS will not be moved in memory for the duration of BODY.
366 Useful for e.g. foreign calls where another thread may trigger
367 garbage collection. This is currently implemented by disabling GC"
369 (declare (ignore objects)) ;should we eval these for side-effect?
374 `(let ((*pinned-objects* (list* ,@objects *pinned-objects*)))