0.9.3.63:
[sbcl.git] / src / compiler / mips / macros.lisp
1 ;;;; various useful macros for generating MIPS code
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11 (in-package "SB!VM")
12
13 ;;; Handy macro for defining top-level forms that depend on the compile
14 ;;; environment.
15
16 (defmacro expand (expr)
17   (let ((gensym (gensym)))
18     `(macrolet
19          ((,gensym ()
20             ,expr))
21        (,gensym))))
22
23 \f
24 ;;; Instruction-like macros.
25
26 (defmacro move (dst src &optional (always-emit-code-p nil))
27   "Move SRC into DST (unless they are location= and ALWAYS-EMIT-CODE-P
28   is nil)."
29   (once-only ((n-dst dst)
30               (n-src src))
31     `(if (location= ,n-dst ,n-src)
32          (when ,always-emit-code-p
33            (inst nop))
34          (inst move ,n-dst ,n-src))))
35
36 (defmacro def-mem-op (op inst shift load)
37   `(defmacro ,op (object base &optional (offset 0) (lowtag 0))
38      `(progn
39         (inst ,',inst ,object ,base (- (ash ,offset ,,shift) ,lowtag))
40         ,,@(when load '('(inst nop))))))
41 ;;;
42 (def-mem-op loadw lw word-shift t)
43 (def-mem-op storew sw word-shift nil)
44
45 (defmacro load-symbol (reg symbol)
46   (once-only ((reg reg) (symbol symbol))
47     `(inst addu ,reg null-tn (static-symbol-offset ,symbol))))
48
49 (defmacro load-symbol-value (reg symbol)
50   `(progn
51      (inst lw ,reg null-tn
52            (+ (static-symbol-offset ',symbol)
53               (ash symbol-value-slot word-shift)
54               (- other-pointer-lowtag)))
55      (inst nop)))
56
57 (defmacro store-symbol-value (reg symbol)
58   `(inst sw ,reg null-tn
59          (+ (static-symbol-offset ',symbol)
60             (ash symbol-value-slot word-shift)
61             (- other-pointer-lowtag))))
62
63 (defmacro load-type (target source &optional (offset 0))
64   "Loads the type bits of a pointer into target independent of
65   byte-ordering issues."
66   (once-only ((n-target target)
67               (n-source source)
68               (n-offset offset))
69     (ecase *backend-byte-order*
70       (:little-endian
71        `(inst lbu ,n-target ,n-source ,n-offset))
72       (:big-endian
73        `(inst lbu ,n-target ,n-source (+ ,n-offset 3))))))
74
75
76 ;;; Macros to handle the fact that we cannot use the machine native call and
77 ;;; return instructions.
78
79 (defmacro lisp-jump (function lip)
80   "Jump to the lisp function FUNCTION.  LIP is an interior-reg temporary."
81   `(progn
82      (inst addu ,lip ,function (- (ash simple-fun-code-offset word-shift)
83                                    fun-pointer-lowtag))
84      (inst j ,lip)
85      (move code-tn ,function t)))
86
87 (defmacro lisp-return (return-pc lip &key (offset 0) (frob-code t))
88   "Return to RETURN-PC.  LIP is an interior-reg temporary."
89   `(progn
90      (inst addu ,lip ,return-pc
91            (- (* (1+ ,offset) n-word-bytes) other-pointer-lowtag))
92      (inst j ,lip)
93      ,(if frob-code
94           `(move code-tn ,return-pc t)
95           '(inst nop))))
96
97
98 (defmacro emit-return-pc (label)
99   "Emit a return-pc header word.  LABEL is the label to use for this return-pc."
100   `(progn
101      (align n-lowtag-bits)
102      (emit-label ,label)
103      (inst lra-header-word)))
104
105
106 \f
107 ;;;; Stack TN's
108
109 ;;; Move a stack TN to a register and vice-versa.
110 (defmacro load-stack-tn (reg stack)
111   `(let ((reg ,reg)
112          (stack ,stack))
113      (let ((offset (tn-offset stack)))
114        (sc-case stack
115          ((control-stack)
116           (loadw reg cfp-tn offset))))))
117 (defmacro store-stack-tn (stack reg)
118   `(let ((stack ,stack)
119          (reg ,reg))
120      (let ((offset (tn-offset stack)))
121        (sc-case stack
122          ((control-stack)
123           (storew reg cfp-tn offset))))))
124
125 (defmacro maybe-load-stack-tn (reg reg-or-stack)
126   "Move the TN Reg-Or-Stack into Reg if it isn't already there."
127   (once-only ((n-reg reg)
128               (n-stack reg-or-stack))
129     `(sc-case ,n-reg
130        ((any-reg descriptor-reg)
131         (sc-case ,n-stack
132           ((any-reg descriptor-reg)
133            (move ,n-reg ,n-stack))
134           ((control-stack)
135            (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
136
137 \f
138 ;;;; Storage allocation:
139 (defmacro with-fixed-allocation ((result-tn flag-tn temp-tn type-code size)
140                                  &body body)
141   "Do stuff to allocate an other-pointer object of fixed Size with a single
142    word header having the specified Type-Code.  The result is placed in
143    Result-TN, Flag-Tn must be wired to NL4-OFFSET, and Temp-TN is a non-
144    descriptor temp (which may be randomly used by the body.)  The body is
145    placed inside the PSEUDO-ATOMIC, and presumably initializes the object."
146   (unless body
147     (bug "empty &body in WITH-FIXED-ALLOCATION"))
148   (once-only ((result-tn result-tn) (temp-tn temp-tn) (size size))
149     `(pseudo-atomic (,flag-tn :extra (pad-data-block ,size))
150        (inst or ,result-tn alloc-tn other-pointer-lowtag)
151        (inst li ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
152        (storew ,temp-tn ,result-tn 0 other-pointer-lowtag)
153        ,@body)))
154
155 \f
156 ;;;; Three Way Comparison
157 (defun three-way-comparison (x y condition flavor not-p target temp)
158   (ecase condition
159     (:eq
160      (if not-p
161          (inst bne x y target)
162          (inst beq x y target)))
163     (:lt
164      (ecase flavor
165        (:unsigned
166         (inst sltu temp x y))
167        (:signed
168         (inst slt temp x y)))
169      (if not-p
170          (inst beq temp zero-tn target)
171          (inst bne temp zero-tn target)))
172     (:gt
173      (ecase flavor
174        (:unsigned
175         (inst sltu temp y x))
176        (:signed
177         (inst slt temp y x)))
178      (if not-p
179          (inst beq temp zero-tn target)
180          (inst bne temp zero-tn target))))
181   (inst nop))
182
183
184 \f
185 ;;;; Error Code
186 (eval-when (:compile-toplevel :load-toplevel :execute)
187   (defun emit-error-break (vop kind code values)
188     (let ((vector (gensym)))
189       `((let ((vop ,vop))
190           (when vop
191             (note-this-location vop :internal-error)))
192         (inst break ,kind)
193         (with-adjustable-vector (,vector)
194           (write-var-integer (error-number-or-lose ',code) ,vector)
195           ,@(mapcar #'(lambda (tn)
196                         `(let ((tn ,tn))
197                            (write-var-integer (make-sc-offset (sc-number
198                                                                (tn-sc tn))
199                                                               (tn-offset tn))
200                                               ,vector)))
201                     values)
202           (inst byte (length ,vector))
203           (dotimes (i (length ,vector))
204             (inst byte (aref ,vector i))))
205         (align word-shift)))))
206
207 (defmacro error-call (vop error-code &rest values)
208   "Cause an error.  ERROR-CODE is the error to cause."
209   (cons 'progn
210         (emit-error-break vop error-trap error-code values)))
211
212
213 (defmacro cerror-call (vop label error-code &rest values)
214   "Cause a continuable error.  If the error is continued, execution resumes at
215   LABEL."
216   `(progn
217      (inst b ,label)
218      ,@(emit-error-break vop cerror-trap error-code values)))
219
220 (defmacro generate-error-code (vop error-code &rest values)
221   "Generate-Error-Code Error-code Value*
222   Emit code for an error with the specified Error-Code and context Values."
223   `(assemble (*elsewhere*)
224      (let ((start-lab (gen-label)))
225        (emit-label start-lab)
226        (error-call ,vop ,error-code ,@values)
227        start-lab)))
228
229 (defmacro generate-cerror-code (vop error-code &rest values)
230   "Generate-CError-Code Error-code Value*
231   Emit code for a continuable error with the specified Error-Code and
232   context Values.  If the error is continued, execution resumes after
233   the GENERATE-CERROR-CODE form."
234   (with-unique-names (continue error)
235     `(let ((,continue (gen-label)))
236        (emit-label ,continue)
237        (assemble (*elsewhere*)
238          (let ((,error (gen-label)))
239            (emit-label ,error)
240            (cerror-call ,vop ,continue ,error-code ,@values)
241            ,error)))))
242 \f
243 ;;;; PSEUDO-ATOMIC
244
245 ;;; handy macro for making sequences look atomic
246 (defmacro pseudo-atomic ((flag-tn &key (extra 0)) &rest forms)
247   `(progn
248      (aver (= (tn-offset ,flag-tn) nl4-offset))
249      (aver (not (minusp ,extra)))
250      (without-scheduling ()
251        (inst li ,flag-tn ,extra)
252        (inst addu alloc-tn 1))
253      ,@forms
254      (without-scheduling ()
255        (let ((label (gen-label)))
256          (inst bgez ,flag-tn label)
257          (inst addu alloc-tn (1- ,extra))
258          (inst break 16)
259          (emit-label label)))))
260 \f
261 ;;;; memory accessor vop generators
262
263 (deftype load/store-index (scale lowtag min-offset
264                                  &optional (max-offset min-offset))
265   `(integer ,(- (truncate (+ (ash 1 16)
266                              (* min-offset n-word-bytes)
267                              (- lowtag))
268                           scale))
269             ,(truncate (- (+ (1- (ash 1 16)) lowtag)
270                           (* max-offset n-word-bytes))
271                        scale)))
272
273 (defmacro define-full-reffer (name type offset lowtag scs el-type
274                                    &optional translate)
275   `(progn
276      (define-vop (,name)
277        ,@(when translate
278            `((:translate ,translate)))
279        (:policy :fast-safe)
280        (:args (object :scs (descriptor-reg))
281               (index :scs (any-reg)))
282        (:arg-types ,type tagged-num)
283        (:temporary (:scs (interior-reg)) lip)
284        (:results (value :scs ,scs))
285        (:result-types ,el-type)
286        (:generator 5
287          (inst add lip object index)
288          (inst lw value lip (- (* ,offset n-word-bytes) ,lowtag))
289          (inst nop)))
290      (define-vop (,(symbolicate name "-C"))
291        ,@(when translate
292            `((:translate ,translate)))
293        (:policy :fast-safe)
294        (:args (object :scs (descriptor-reg)))
295        (:info index)
296        (:arg-types ,type
297                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
298                                                 ,(eval offset))))
299        (:results (value :scs ,scs))
300        (:result-types ,el-type)
301        (:generator 4
302          (inst lw value object (- (* (+ ,offset index) n-word-bytes) ,lowtag))
303          (inst nop)))))
304
305 (defmacro define-full-setter (name type offset lowtag scs el-type
306                                    &optional translate)
307   `(progn
308      (define-vop (,name)
309        ,@(when translate
310            `((:translate ,translate)))
311        (:policy :fast-safe)
312        (:args (object :scs (descriptor-reg))
313               (index :scs (any-reg))
314               (value :scs ,scs :target result))
315        (:arg-types ,type tagged-num ,el-type)
316        (:temporary (:scs (interior-reg)) lip)
317        (:results (result :scs ,scs))
318        (:result-types ,el-type)
319        (:generator 2
320          (inst add lip object index)
321          (inst sw value lip (- (* ,offset n-word-bytes) ,lowtag))
322          (move result value)))
323      (define-vop (,(symbolicate name "-C"))
324        ,@(when translate
325            `((:translate ,translate)))
326        (:policy :fast-safe)
327        (:args (object :scs (descriptor-reg))
328               (value :scs ,scs))
329        (:info index)
330        (:arg-types ,type
331                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
332                                                 ,(eval offset)))
333                    ,el-type)
334        (:results (result :scs ,scs))
335        (:result-types ,el-type)
336        (:generator 1
337          (inst sw value object (- (* (+ ,offset index) n-word-bytes) ,lowtag))
338          (move result value)))))
339
340
341 (defmacro define-partial-reffer (name type size signed offset lowtag scs
342                                       el-type &optional translate)
343   (let ((scale (ecase size (:byte 1) (:short 2))))
344     `(progn
345        (define-vop (,name)
346          ,@(when translate
347              `((:translate ,translate)))
348          (:policy :fast-safe)
349          (:args (object :scs (descriptor-reg))
350                 (index :scs (unsigned-reg)))
351          (:arg-types ,type positive-fixnum)
352          (:results (value :scs ,scs))
353          (:result-types ,el-type)
354          (:temporary (:scs (interior-reg)) lip)
355          (:generator 5
356            (inst addu lip object index)
357            ,@(when (eq size :short)
358                '((inst addu lip index)))
359            (inst ,(ecase size
360                     (:byte (if signed 'lb 'lbu))
361                     (:short (if signed 'lh 'lhu)))
362                  value lip (- (* ,offset n-word-bytes) ,lowtag))
363            (inst nop)))
364        (define-vop (,(symbolicate name "-C"))
365          ,@(when translate
366              `((:translate ,translate)))
367          (:policy :fast-safe)
368          (:args (object :scs (descriptor-reg)))
369          (:info index)
370          (:arg-types ,type
371                      (:constant (load/store-index ,scale
372                                                   ,(eval lowtag)
373                                                   ,(eval offset))))
374          (:results (value :scs ,scs))
375          (:result-types ,el-type)
376          (:generator 4
377            (inst ,(ecase size
378                     (:byte (if signed 'lb 'lbu))
379                     (:short (if signed 'lh 'lhu)))
380                  value object
381                  (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
382            (inst nop))))))
383
384 (defmacro define-partial-setter (name type size offset lowtag scs el-type
385                                       &optional translate)
386   (let ((scale (ecase size (:byte 1) (:short 2))))
387     `(progn
388        (define-vop (,name)
389          ,@(when translate
390              `((:translate ,translate)))
391          (:policy :fast-safe)
392          (:args (object :scs (descriptor-reg))
393                 (index :scs (unsigned-reg))
394                 (value :scs ,scs :target result))
395          (:arg-types ,type positive-fixnum ,el-type)
396          (:temporary (:scs (interior-reg)) lip)
397          (:results (result :scs ,scs))
398          (:result-types ,el-type)
399          (:generator 5
400            (inst addu lip object index)
401            ,@(when (eq size :short)
402                '((inst addu lip index)))
403            (inst ,(ecase size (:byte 'sb) (:short 'sh))
404                  value lip (- (* ,offset n-word-bytes) ,lowtag))
405            (move result value)))
406        (define-vop (,(symbolicate name "-C"))
407          ,@(when translate
408              `((:translate ,translate)))
409          (:policy :fast-safe)
410          (:args (object :scs (descriptor-reg))
411                 (value :scs ,scs :target result))
412          (:info index)
413          (:arg-types ,type
414                      (:constant (load/store-index ,scale
415                                                   ,(eval lowtag)
416                                                   ,(eval offset)))
417                      ,el-type)
418          (:results (result :scs ,scs))
419          (:result-types ,el-type)
420          (:generator 4
421            (inst ,(ecase size (:byte 'sb) (:short 'sh))
422                  value object
423                  (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
424            (move result value))))))
425
426
427 (defmacro sb!sys::with-pinned-objects ((&rest objects) &body body)
428   "Arrange with the garbage collector that the pages occupied by
429 OBJECTS will not be moved in memory for the duration of BODY.
430 Useful for e.g. foreign calls where another thread may trigger
431 garbage collection.  This is currently implemented by disabling GC"
432   (declare (ignore objects))            ;should we eval these for side-effect?
433   `(without-gcing
434     ,@body))