1 ;;;; various useful macros for generating MIPS 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.
13 ;;; Handy macro for defining top-level forms that depend on the compile
16 (defmacro expand (expr)
17 (let ((gensym (gensym)))
24 ;;; Instruction-like macros.
26 (defmacro move (dst src &optional (always-emit-code-p nil))
28 "Move SRC into DST (unless they are location= and ALWAYS-EMIT-CODE-P
30 (once-only ((n-dst dst)
32 `(if (location= ,n-dst ,n-src)
33 (when ,always-emit-code-p
35 (inst move ,n-dst ,n-src))))
37 (defmacro def-mem-op (op inst shift load)
38 `(defmacro ,op (object base &optional (offset 0) (lowtag 0))
40 (inst ,',inst ,object ,base (- (ash ,offset ,,shift) ,lowtag))
41 ,,@(when load '('(inst nop))))))
43 (def-mem-op loadw lw word-shift t)
44 (def-mem-op storew sw word-shift nil)
46 (defmacro load-symbol (reg symbol)
47 (once-only ((reg reg) (symbol symbol))
48 `(inst addu ,reg null-tn (static-symbol-offset ,symbol))))
50 (defmacro load-symbol-value (reg symbol)
53 (+ (static-symbol-offset ',symbol)
54 (ash symbol-value-slot word-shift)
55 (- other-pointer-lowtag)))
58 (defmacro store-symbol-value (reg symbol)
59 `(inst sw ,reg null-tn
60 (+ (static-symbol-offset ',symbol)
61 (ash symbol-value-slot word-shift)
62 (- other-pointer-lowtag))))
64 (defmacro load-type (target source &optional (offset 0))
66 "Loads the type bits of a pointer into target independent of
67 byte-ordering issues."
68 (once-only ((n-target target)
71 (ecase *backend-byte-order*
73 `(inst lbu ,n-target ,n-source ,n-offset))
75 `(inst lbu ,n-target ,n-source (+ ,n-offset (1- n-word-bytes)))))))
78 ;;; Macros to handle the fact that we cannot use the machine native call and
79 ;;; return instructions.
81 (defmacro lisp-jump (function lip)
83 "Jump to the lisp function FUNCTION. LIP is an interior-reg temporary."
85 (inst addu ,lip ,function (- (ash simple-fun-code-offset word-shift)
88 (move code-tn ,function t)))
90 (defmacro lisp-return (return-pc lip &key (offset 0) (frob-code t))
92 "Return to RETURN-PC. LIP is an interior-reg temporary."
94 (inst addu ,lip ,return-pc
95 (- (* (1+ ,offset) n-word-bytes) other-pointer-lowtag))
98 `(move code-tn ,return-pc t)
102 (defmacro emit-return-pc (label)
104 "Emit a return-pc header word. LABEL is the label to use for this return-pc."
106 (emit-alignment n-lowtag-bits)
108 (inst lra-header-word)))
114 ;;; Move a stack TN to a register and vice-versa.
115 (defmacro load-stack-tn (reg stack)
118 (let ((offset (tn-offset stack)))
121 (loadw reg cfp-tn offset))))))
123 (defmacro store-stack-tn (stack reg)
124 `(let ((stack ,stack)
126 (let ((offset (tn-offset stack)))
129 (storew reg cfp-tn offset))))))
131 (defmacro maybe-load-stack-tn (reg reg-or-stack)
133 "Move the TN Reg-Or-Stack into Reg if it isn't already there."
134 (once-only ((n-reg reg)
135 (n-stack reg-or-stack))
137 ((any-reg descriptor-reg)
139 ((any-reg descriptor-reg)
140 (move ,n-reg ,n-stack))
142 (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
145 ;;;; Storage allocation:
146 (defmacro with-fixed-allocation ((result-tn flag-tn temp-tn type-code
147 size dynamic-extent-p
148 &key (lowtag other-pointer-lowtag))
151 "Do stuff to allocate an other-pointer object of fixed Size with a single
152 word header having the specified Type-Code. The result is placed in
153 Result-TN, Flag-Tn must be wired to NL4-OFFSET, and Temp-TN is a non-
154 descriptor temp (which may be randomly used by the body.) The body is
155 placed inside the PSEUDO-ATOMIC, and presumably initializes the object."
157 (bug "empty &body in WITH-FIXED-ALLOCATION"))
158 (once-only ((result-tn result-tn) (flag-tn flag-tn) (temp-tn temp-tn)
159 (type-code type-code) (size size)
160 (dynamic-extent-p dynamic-extent-p)
162 `(if ,dynamic-extent-p
163 (pseudo-atomic (,flag-tn)
165 (inst or ,result-tn csp-tn ,lowtag)
166 (inst li ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
167 (inst addu csp-tn (pad-data-block ,size))
168 (storew ,temp-tn ,result-tn 0 ,lowtag)
170 (pseudo-atomic (,flag-tn :extra (pad-data-block ,size))
171 ;; The pseudo-atomic bit in alloc-tn is set. If the lowtag also
172 ;; has a 1 bit in the same position, we're all set. Otherwise,
173 ;; we need to subtract the pseudo-atomic bit.
174 (inst or ,result-tn alloc-tn ,lowtag)
175 (unless (logbitp 0 ,lowtag) (inst sub ,result-tn 1))
176 (inst li ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
177 (storew ,temp-tn ,result-tn 0 ,lowtag)
180 (defun align-csp (temp)
181 ;; is used for stack allocation of dynamic-extent objects
182 (let ((aligned (gen-label)))
183 (inst and temp csp-tn lowtag-mask)
184 (inst beq temp aligned)
186 (inst addu csp-tn n-word-bytes)
187 (storew zero-tn csp-tn -1)
188 (emit-label aligned)))
191 ;;;; Three Way Comparison
192 (defun three-way-comparison (x y condition flavor not-p target temp)
196 (inst bne x y target)
197 (inst beq x y target)))
201 (inst sltu temp x y))
203 (inst slt temp x y)))
205 (inst beq temp target)
206 (inst bne temp target)))
210 (inst sltu temp y x))
212 (inst slt temp y x)))
214 (inst beq temp target)
215 (inst bne temp target))))
221 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
222 (defun emit-error-break (vop kind code values)
223 (let ((vector (gensym)))
226 (note-this-location vop :internal-error)))
228 (with-adjustable-vector (,vector)
229 (write-var-integer (error-number-or-lose ',code) ,vector)
230 ,@(mapcar #'(lambda (tn)
232 (write-var-integer (make-sc-offset (sc-number
237 (inst byte (length ,vector))
238 (dotimes (i (length ,vector))
239 (inst byte (aref ,vector i))))
240 (emit-alignment word-shift)))))
242 (defmacro error-call (vop error-code &rest values)
244 "Cause an error. ERROR-CODE is the error to cause."
246 (emit-error-break vop error-trap error-code values)))
249 (defmacro cerror-call (vop label error-code &rest values)
251 "Cause a continuable error. If the error is continued, execution resumes at
254 (without-scheduling ()
256 ,@(emit-error-break vop cerror-trap error-code values))))
258 (defmacro generate-error-code (vop error-code &rest values)
260 "Generate-Error-Code Error-code Value*
261 Emit code for an error with the specified Error-Code and context Values."
262 `(assemble (*elsewhere*)
263 (let ((start-lab (gen-label)))
264 (emit-label start-lab)
265 (error-call ,vop ,error-code ,@values)
268 (defmacro generate-cerror-code (vop error-code &rest values)
270 "Generate-CError-Code Error-code Value*
271 Emit code for a continuable error with the specified Error-Code and
272 context Values. If the error is continued, execution resumes after
273 the GENERATE-CERROR-CODE form."
274 (with-unique-names (continue error)
275 `(let ((,continue (gen-label)))
276 (emit-label ,continue)
277 (assemble (*elsewhere*)
278 (let ((,error (gen-label)))
280 (cerror-call ,vop ,continue ,error-code ,@values)
285 ;;; handy macro for making sequences look atomic
286 (defmacro pseudo-atomic ((flag-tn &key (extra 0)) &rest forms)
288 (aver (= (tn-offset ,flag-tn) nl4-offset))
289 (aver (not (minusp ,extra)))
290 (without-scheduling ()
291 (inst li ,flag-tn ,extra)
292 (inst addu alloc-tn 1))
294 (without-scheduling ()
295 (let ((label (gen-label)))
296 (inst bgez ,flag-tn label)
297 (inst addu alloc-tn (1- ,extra))
299 (emit-label label)))))
301 ;;;; memory accessor vop generators
303 (deftype load/store-index (scale lowtag min-offset
304 &optional (max-offset min-offset))
305 `(integer ,(- (truncate (+ (ash 1 16)
306 (* min-offset n-word-bytes)
309 ,(truncate (- (+ (1- (ash 1 16)) lowtag)
310 (* max-offset n-word-bytes))
313 (defmacro define-full-reffer (name type offset lowtag scs el-type
318 `((:translate ,translate)))
320 (:args (object :scs (descriptor-reg))
321 (index :scs (any-reg)))
322 (:arg-types ,type tagged-num)
323 (:temporary (:scs (interior-reg)) lip)
324 (:results (value :scs ,scs))
325 (:result-types ,el-type)
327 (inst addu lip object index)
328 (loadw value lip ,offset ,lowtag)))
329 (define-vop (,(symbolicate name "-C"))
331 `((:translate ,translate)))
333 (:args (object :scs (descriptor-reg)))
336 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
338 (:results (value :scs ,scs))
339 (:result-types ,el-type)
341 (loadw value object (+ ,offset index) ,lowtag)))))
343 (defmacro define-full-setter (name type offset lowtag scs el-type
348 `((:translate ,translate)))
350 (:args (object :scs (descriptor-reg))
351 (index :scs (any-reg))
352 (value :scs ,scs :target result))
353 (:arg-types ,type tagged-num ,el-type)
354 (:temporary (:scs (interior-reg)) lip)
355 (:results (result :scs ,scs))
356 (:result-types ,el-type)
358 (inst addu lip object index)
359 (storew value lip ,offset ,lowtag)
360 (move result value)))
361 (define-vop (,(symbolicate name "-C"))
363 `((:translate ,translate)))
365 (:args (object :scs (descriptor-reg))
369 (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
372 (:results (result :scs ,scs))
373 (:result-types ,el-type)
375 (storew value object (+ ,offset index) ,lowtag)
376 (move result value)))))
379 (defmacro define-partial-reffer (name type size signed offset lowtag scs
380 el-type &optional translate)
381 (let ((scale (ecase size (:byte 1) (:short 2))))
385 `((:translate ,translate)))
387 (:args (object :scs (descriptor-reg))
388 (index :scs (unsigned-reg)))
389 (:arg-types ,type positive-fixnum)
390 (:results (value :scs ,scs))
391 (:result-types ,el-type)
392 (:temporary (:scs (interior-reg)) lip)
394 (inst addu lip object index)
395 ,@(when (eq size :short)
396 '((inst addu lip index)))
398 (:byte (if signed 'lb 'lbu))
399 (:short (if signed 'lh 'lhu)))
400 value lip (- (* ,offset n-word-bytes) ,lowtag))
402 (define-vop (,(symbolicate name "-C"))
404 `((:translate ,translate)))
406 (:args (object :scs (descriptor-reg)))
409 (:constant (load/store-index ,scale
412 (:results (value :scs ,scs))
413 (:result-types ,el-type)
416 (:byte (if signed 'lb 'lbu))
417 (:short (if signed 'lh 'lhu)))
419 (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
422 (defmacro define-partial-setter (name type size offset lowtag scs el-type
424 (let ((scale (ecase size (:byte 1) (:short 2))))
428 `((:translate ,translate)))
430 (:args (object :scs (descriptor-reg))
431 (index :scs (unsigned-reg))
432 (value :scs ,scs :target result))
433 (:arg-types ,type positive-fixnum ,el-type)
434 (:temporary (:scs (interior-reg)) lip)
435 (:results (result :scs ,scs))
436 (:result-types ,el-type)
438 (inst addu lip object index)
439 ,@(when (eq size :short)
440 '((inst addu lip index)))
441 (inst ,(ecase size (:byte 'sb) (:short 'sh))
442 value lip (- (* ,offset n-word-bytes) ,lowtag))
443 (move result value)))
444 (define-vop (,(symbolicate name "-C"))
446 `((:translate ,translate)))
448 (:args (object :scs (descriptor-reg))
449 (value :scs ,scs :target result))
452 (:constant (load/store-index ,scale
456 (:results (result :scs ,scs))
457 (:result-types ,el-type)
459 (inst ,(ecase size (:byte 'sb) (:short 'sh))
461 (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
462 (move result value))))))
465 (def!macro with-pinned-objects ((&rest objects) &body body)
466 "Arrange with the garbage collector that the pages occupied by
467 OBJECTS will not be moved in memory for the duration of BODY.
468 Useful for e.g. foreign calls where another thread may trigger
469 garbage collection. This is currently implemented by disabling GC"
470 (declare (ignore objects)) ;should we eval these for side-effect?