1 ;;;; various useful macros for generating Alpha 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 ;;; a handy macro for defining top level forms that depend on the
15 ;;; compile environment
16 (defmacro expand (expr)
17 (let ((gensym (gensym)))
23 ;;; instruction-like macros
26 ;;(defmacro move (dst src)
28 ;; "Move SRC into DST unless they are location=."
29 ;; (once-only ((n-dst dst)
31 ;; `(unless (location= ,n-dst ,n-src)
32 ;; (inst mov ,n-dst ,n-src))))
34 (defmacro move (src dst)
35 "Move SRC into DST unless they are location=."
36 (once-only ((n-src src) (n-dst dst))
37 `(unless (location= ,n-src ,n-dst)
38 (inst move ,n-src ,n-dst))))
40 (defmacro loadw (result base &optional (offset 0) (lowtag 0))
41 (once-only ((result result) (base base))
42 `(inst ldl ,result (- (ash ,offset word-shift) ,lowtag) ,base)))
44 (defmacro loadq (result base &optional (offset 0) (lowtag 0))
45 (once-only ((result result) (base base))
46 `(inst ldq ,result (- (ash ,offset word-shift) ,lowtag) ,base)))
48 (defmacro storew (value base &optional (offset 0) (lowtag 0))
49 (once-only ((value value) (base base) (offset offset) (lowtag lowtag))
50 `(inst stl ,value (- (ash ,offset word-shift) ,lowtag) ,base)))
52 (defmacro storeq (value base &optional (offset 0) (lowtag 0))
53 (once-only ((value value) (base base) (offset offset) (lowtag lowtag))
54 `(inst stq ,value (- (ash ,offset word-shift) ,lowtag) ,base)))
56 (defmacro load-symbol (reg symbol)
57 (once-only ((reg reg) (symbol symbol))
58 `(inst lda ,reg (static-symbol-offset ,symbol) null-tn)))
60 (defmacro load-symbol-value (reg symbol)
62 (+ (static-symbol-offset ',symbol)
63 (ash symbol-value-slot word-shift)
64 (- other-pointer-lowtag))
67 (defmacro store-symbol-value (reg symbol)
69 (+ (static-symbol-offset ',symbol)
70 (ash symbol-value-slot word-shift)
71 (- other-pointer-lowtag))
74 (defmacro load-type (target source &optional (offset 0))
75 "Loads the type bits of a pointer into target independent of
76 byte-ordering issues."
77 (once-only ((n-target target)
81 (inst ldl ,n-target ,n-offset ,n-source)
82 (inst and ,n-target #xff ,n-target))))
84 ;;; macros to handle the fact that we cannot use the machine native
85 ;;; call and return instructions
87 (defmacro lisp-jump (function lip)
88 "Jump to the lisp function FUNCTION. LIP is an interior-reg temporary."
90 (inst lda ,lip (- (ash simple-fun-code-offset word-shift)
93 (move ,function code-tn)
94 (inst jsr zero-tn ,lip 1)))
96 (defmacro lisp-return (return-pc lip &key (offset 0) (frob-code t))
97 "Return to RETURN-PC. LIP is an interior-reg temporary."
100 (- (* (1+ ,offset) n-word-bytes) other-pointer-lowtag)
103 `((move ,return-pc code-tn)))
104 (inst ret zero-tn ,lip 1)))
107 (defmacro emit-return-pc (label)
108 "Emit a return-pc header word. LABEL is the label to use for this
111 (align n-lowtag-bits)
113 (inst lra-header-word)))
119 ;;; Move a stack TN to a register and vice-versa.
120 (defmacro load-stack-tn (reg stack)
123 (let ((offset (tn-offset stack)))
126 (loadw reg cfp-tn offset))))))
127 (defmacro store-stack-tn (stack reg)
128 `(let ((stack ,stack)
130 (let ((offset (tn-offset stack)))
133 (storew reg cfp-tn offset))))))
135 ;;; Move the TN Reg-Or-Stack into Reg if it isn't already there.
136 (defmacro maybe-load-stack-tn (reg reg-or-stack)
137 (once-only ((n-reg reg)
138 (n-stack reg-or-stack))
140 ((any-reg descriptor-reg)
142 ((any-reg descriptor-reg)
143 (move ,n-stack ,n-reg))
145 (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
147 ;;; Move the TN Reg-Or-Stack into Reg if it isn't already there.
148 (defmacro maybe-load-stack-nfp-tn (reg reg-or-stack temp)
149 (once-only ((n-reg reg)
150 (n-stack reg-or-stack))
153 ((any-reg descriptor-reg)
155 ((any-reg descriptor-reg)
156 (move ,n-stack ,n-reg))
158 (loadw ,n-reg cfp-tn (tn-offset ,n-stack))
159 (inst mskll nsp-tn 0 ,temp)
160 (inst bis ,temp ,n-reg ,n-reg))))))))
162 ;;;; storage allocation
164 ;;; Do stuff to allocate an other-pointer object of fixed SIZE with a
165 ;;; single word header having the specified WIDETAG value. The result is
166 ;;; placed in RESULT-TN, Flag-Tn must be wired to NL3-OFFSET, and
167 ;;; Temp-TN is a non- descriptor temp (which may be randomly used by
168 ;;; the body.) The body is placed inside the PSEUDO-ATOMIC, and
169 ;;; presumably initializes the object.
170 (defmacro with-fixed-allocation ((result-tn temp-tn widetag size)
173 (bug "empty &body in WITH-FIXED-ALLOCATION"))
174 (once-only ((result-tn result-tn) (temp-tn temp-tn) (size size))
175 `(pseudo-atomic (:extra (pad-data-block ,size))
176 (inst bis alloc-tn other-pointer-lowtag ,result-tn)
177 (inst li (logior (ash (1- ,size) n-widetag-bits) ,widetag) ,temp-tn)
178 (storew ,temp-tn ,result-tn 0 other-pointer-lowtag)
182 (eval-when (:compile-toplevel :load-toplevel :execute)
183 (defun emit-error-break (vop kind code values)
184 (let ((vector (gensym)))
187 (note-this-location vop :internal-error)))
189 (with-adjustable-vector (,vector)
190 (write-var-integer (error-number-or-lose ',code) ,vector)
191 ,@(mapcar (lambda (tn)
193 (write-var-integer (make-sc-offset (sc-number
198 (inst byte (length ,vector))
199 (dotimes (i (length ,vector))
200 (inst byte (aref ,vector i))))
201 (align word-shift)))))
203 (defmacro error-call (vop error-code &rest values)
204 "Cause an error. ERROR-CODE is the error to cause."
206 (emit-error-break vop error-trap error-code values)))
209 (defmacro cerror-call (vop label error-code &rest values)
210 "Cause a continuable error. If the error is continued, execution resumes at
213 (inst br zero-tn ,label)
214 ,@(emit-error-break vop cerror-trap error-code values)))
216 (defmacro generate-error-code (vop error-code &rest values)
217 "Generate-Error-Code Error-code Value*
218 Emit code for an error with the specified Error-Code and context Values."
219 `(assemble (*elsewhere*)
220 (let ((start-lab (gen-label)))
221 (emit-label start-lab)
222 (error-call ,vop ,error-code ,@values)
225 (defmacro generate-cerror-code (vop error-code &rest values)
226 "Generate-CError-Code Error-code Value*
227 Emit code for a continuable error with the specified Error-Code and
228 context Values. If the error is continued, execution resumes after
229 the GENERATE-CERROR-CODE form."
230 (with-unique-names (continue error)
231 `(let ((,continue (gen-label)))
232 (emit-label ,continue)
233 (assemble (*elsewhere*)
234 (let ((,error (gen-label)))
236 (cerror-call ,vop ,continue ,error-code ,@values)
240 ;;; a handy macro for making sequences look atomic
241 (defmacro pseudo-atomic ((&key (extra 0)) &rest forms)
243 (inst addq alloc-tn 1 alloc-tn)
245 (inst lda alloc-tn (1- ,extra) alloc-tn)
246 (inst stl zero-tn 0 alloc-tn)))
248 ;;;; memory accessor vop generators
250 (defmacro define-full-reffer (name type offset lowtag scs el-type
255 `((:translate ,translate)))
257 (:args (object :scs (descriptor-reg))
258 (index :scs (any-reg)))
259 (:arg-types ,type tagged-num)
260 (:temporary (:scs (interior-reg)) lip)
261 (:results (value :scs ,scs))
262 (:result-types ,el-type)
264 (inst addq object index lip)
265 (inst ldl value (- (* ,offset n-word-bytes) ,lowtag) lip)
266 ,@(when (equal scs '(unsigned-reg))
267 '((inst mskll value 4 value)))))
268 (define-vop (,(symbolicate name "-C"))
270 `((:translate ,translate)))
272 (:args (object :scs (descriptor-reg)))
275 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
277 (:results (value :scs ,scs))
278 (:result-types ,el-type)
280 (inst ldl value (- (* (+ ,offset index) n-word-bytes) ,lowtag)
282 ,@(when (equal scs '(unsigned-reg))
283 '((inst mskll value 4 value)))))))
285 (defmacro define-full-setter (name type offset lowtag scs el-type
286 &optional translate #!+gengc (remember t))
290 `((:translate ,translate)))
292 (:args (object :scs (descriptor-reg))
293 (index :scs (any-reg))
294 (value :scs ,scs :target result))
295 (:arg-types ,type tagged-num ,el-type)
296 (:temporary (:scs (interior-reg)) lip)
297 (:results (result :scs ,scs))
298 (:result-types ,el-type)
300 (inst addq index object lip)
301 (inst stl value (- (* ,offset n-word-bytes) ,lowtag) lip)
302 (move value result)))
303 (define-vop (,(symbolicate name "-C"))
305 `((:translate ,translate)))
307 (:args (object :scs (descriptor-reg))
311 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
314 (:results (result :scs ,scs))
315 (:result-types ,el-type)
317 (inst stl value (- (* (+ ,offset index) n-word-bytes) ,lowtag)
319 (move value result)))))
322 (defmacro define-partial-reffer (name type size signed offset lowtag scs
323 el-type &optional translate)
324 (let ((scale (ecase size (:byte 1) (:short 2))))
328 `((:translate ,translate)))
330 (:args (object :scs (descriptor-reg))
331 (index :scs (unsigned-reg)))
332 (:arg-types ,type positive-fixnum)
333 (:results (value :scs ,scs))
334 (:result-types ,el-type)
335 (:temporary (:scs (interior-reg)) lip)
336 (:temporary (:sc non-descriptor-reg) temp)
337 (:temporary (:sc non-descriptor-reg) temp1)
339 (inst addq object index lip)
340 ,@(when (eq size :short)
341 '((inst addq index lip lip)))
345 `((inst ldq_u temp (- (* ,offset n-word-bytes) ,lowtag)
347 (inst lda temp1 (1+ (- (* ,offset n-word-bytes) ,lowtag))
349 (inst extqh temp temp1 temp)
350 (inst sra temp 56 value))
353 (- (* ,offset n-word-bytes) ,lowtag)
355 (inst lda temp1 (- (* ,offset n-word-bytes) ,lowtag)
357 (inst extbl temp temp1 value))))
360 `((inst ldq_u temp (- (* ,offset n-word-bytes) ,lowtag)
362 (inst lda temp1 (- (* ,offset n-word-bytes) ,lowtag)
364 (inst extwl temp temp1 temp)
365 (inst sll temp 48 temp)
366 (inst sra temp 48 value))
367 `((inst ldq_u temp (- (* ,offset n-word-bytes) ,lowtag)
369 (inst lda temp1 (- (* ,offset n-word-bytes) ,lowtag) lip)
370 (inst extwl temp temp1 value)))))))
371 (define-vop (,(symbolicate name "-C"))
373 `((:translate ,translate)))
375 (:args (object :scs (descriptor-reg)))
378 (:constant (load/store-index ,scale
381 (:results (value :scs ,scs))
382 (:result-types ,el-type)
383 (:temporary (:sc non-descriptor-reg) temp)
384 (:temporary (:sc non-descriptor-reg) temp1)
389 `((inst ldq_u temp (- (+ (* ,offset n-word-bytes)
390 (* index ,scale)) ,lowtag)
392 (inst lda temp1 (1+ (- (+ (* ,offset n-word-bytes)
393 (* index ,scale)) ,lowtag))
395 (inst extqh temp temp1 temp)
396 (inst sra temp 56 value))
397 `((inst ldq_u temp (- (+ (* ,offset n-word-bytes)
398 (* index ,scale)) ,lowtag)
400 (inst lda temp1 (- (+ (* ,offset n-word-bytes)
401 (* index ,scale)) ,lowtag)
403 (inst extbl temp temp1 value))))
406 `((inst ldq_u temp (- (+ (* ,offset n-word-bytes)
407 (* index ,scale)) ,lowtag)
409 (inst lda temp1 (- (+ (* ,offset n-word-bytes)
410 (* index ,scale)) ,lowtag)
412 (inst extwl temp temp1 temp)
413 (inst sll temp 48 temp)
414 (inst sra temp 48 value))
415 `((inst ldq_u temp (- (+ (* ,offset n-word-bytes)
416 (* index ,scale)) ,lowtag)
418 (inst lda temp1 (- (+ (* ,offset n-word-bytes)
419 (* index ,scale)) ,lowtag)
421 (inst extwl temp temp1 value))))))))))
423 (defmacro define-partial-setter (name type size offset lowtag scs el-type
425 (let ((scale (ecase size (:byte 1) (:short 2))))
429 `((:translate ,translate)))
431 (:args (object :scs (descriptor-reg))
432 (index :scs (unsigned-reg))
433 (value :scs ,scs :target result))
434 (:arg-types ,type positive-fixnum ,el-type)
435 (:temporary (:scs (interior-reg)) lip)
436 (:temporary (:sc non-descriptor-reg) temp)
437 (:temporary (:sc non-descriptor-reg) temp1)
438 (:temporary (:sc non-descriptor-reg) temp2)
439 (:results (result :scs ,scs))
440 (:result-types ,el-type)
442 (inst addq object index lip)
443 ,@(when (eq size :short)
444 '((inst addq lip index lip)))
447 `((inst lda temp (- (* ,offset n-word-bytes) ,lowtag) lip)
448 (inst ldq_u temp1 (- (* ,offset n-word-bytes) ,lowtag) lip)
449 (inst insbl value temp temp2)
450 (inst mskbl temp1 temp temp1)
451 (inst bis temp1 temp2 temp1)
452 (inst stq_u temp1 (- (* ,offset n-word-bytes) ,lowtag) lip)))
454 `((inst lda temp (- (* ,offset n-word-bytes) ,lowtag) lip)
455 (inst ldq_u temp1 (- (* ,offset n-word-bytes) ,lowtag) lip)
456 (inst mskwl temp1 temp temp1)
457 (inst inswl value temp temp2)
458 (inst bis temp1 temp2 temp)
459 (inst stq_u temp (- (* ,offset n-word-bytes) ,lowtag) lip))))
460 (move value result)))
461 (define-vop (,(symbolicate name "-C"))
463 `((:translate ,translate)))
465 (:args (object :scs (descriptor-reg))
466 (value :scs ,scs :target result))
469 (:constant (load/store-index ,scale
473 (:temporary (:sc non-descriptor-reg) temp)
474 (:temporary (:sc non-descriptor-reg) temp1)
475 (:temporary (:sc non-descriptor-reg) temp2)
476 (:results (result :scs ,scs))
477 (:result-types ,el-type)
481 `((inst lda temp (- (+ (* ,offset n-word-bytes)
485 (inst ldq_u temp1 (- (+ (* ,offset n-word-bytes)
489 (inst insbl value temp temp2)
490 (inst mskbl temp1 temp temp1)
491 (inst bis temp1 temp2 temp1)
492 (inst stq_u temp1 (- (+ (* ,offset n-word-bytes)
496 `((inst lda temp (- (+ (* ,offset n-word-bytes)
500 (inst ldq_u temp1 (- (+ (* ,offset n-word-bytes)
504 (inst mskwl temp1 temp temp1)
505 (inst inswl value temp temp2)
506 (inst bis temp1 temp2 temp)
507 (inst stq_u temp (- (+ (* ,offset n-word-bytes)
510 (move value result))))))
512 (defmacro sb!sys::with-pinned-objects ((&rest objects) &body body)
513 "Arrange with the garbage collector that the pages occupied by
514 OBJECTS will not be moved in memory for the duration of BODY.
515 Useful for e.g. foreign calls where another thread may trigger
516 garbage collection. This is currently implemented by disabling GC"
517 (declare (ignore objects)) ;should we eval these for side-effect?