dd32fdcc226f084e2030c54550d0732d57d77df1
[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
118 (defmacro store-stack-tn (stack reg)
119   `(let ((stack ,stack)
120          (reg ,reg))
121      (let ((offset (tn-offset stack)))
122        (sc-case stack
123          ((control-stack)
124           (storew reg cfp-tn offset))))))
125
126 (defmacro maybe-load-stack-tn (reg reg-or-stack)
127   "Move the TN Reg-Or-Stack into Reg if it isn't already there."
128   (once-only ((n-reg reg)
129               (n-stack reg-or-stack))
130     `(sc-case ,n-reg
131        ((any-reg descriptor-reg)
132         (sc-case ,n-stack
133           ((any-reg descriptor-reg)
134            (move ,n-reg ,n-stack))
135           ((control-stack)
136            (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
137
138 \f
139 ;;;; Storage allocation:
140 (defmacro with-fixed-allocation ((result-tn flag-tn temp-tn type-code size)
141                                  &body body)
142   "Do stuff to allocate an other-pointer object of fixed Size with a single
143    word header having the specified Type-Code.  The result is placed in
144    Result-TN, Flag-Tn must be wired to NL4-OFFSET, and Temp-TN is a non-
145    descriptor temp (which may be randomly used by the body.)  The body is
146    placed inside the PSEUDO-ATOMIC, and presumably initializes the object."
147   (unless body
148     (bug "empty &body in WITH-FIXED-ALLOCATION"))
149   (once-only ((result-tn result-tn) (temp-tn temp-tn) (size size))
150     `(pseudo-atomic (,flag-tn :extra (pad-data-block ,size))
151        (inst or ,result-tn alloc-tn other-pointer-lowtag)
152        (inst li ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
153        (storew ,temp-tn ,result-tn 0 other-pointer-lowtag)
154        ,@body)))
155
156 \f
157 ;;;; Three Way Comparison
158 (defun three-way-comparison (x y condition flavor not-p target temp)
159   (ecase condition
160     (:eq
161      (if not-p
162          (inst bne x y target)
163          (inst beq x y target)))
164     (:lt
165      (ecase flavor
166        (:unsigned
167         (inst sltu temp x y))
168        (:signed
169         (inst slt temp x y)))
170      (if not-p
171          (inst beq temp zero-tn target)
172          (inst bne temp zero-tn target)))
173     (:gt
174      (ecase flavor
175        (:unsigned
176         (inst sltu temp y x))
177        (:signed
178         (inst slt temp y x)))
179      (if not-p
180          (inst beq temp zero-tn target)
181          (inst bne temp zero-tn target))))
182   (inst nop))
183
184
185 \f
186 ;;;; Error Code
187 (eval-when (:compile-toplevel :load-toplevel :execute)
188   (defun emit-error-break (vop kind code values)
189     (let ((vector (gensym)))
190       `((let ((vop ,vop))
191           (when vop
192             (note-this-location vop :internal-error)))
193         (inst break ,kind)
194         (with-adjustable-vector (,vector)
195           (write-var-integer (error-number-or-lose ',code) ,vector)
196           ,@(mapcar #'(lambda (tn)
197                         `(let ((tn ,tn))
198                            (write-var-integer (make-sc-offset (sc-number
199                                                                (tn-sc tn))
200                                                               (tn-offset tn))
201                                               ,vector)))
202                     values)
203           (inst byte (length ,vector))
204           (dotimes (i (length ,vector))
205             (inst byte (aref ,vector i))))
206         (align word-shift)))))
207
208 (defmacro error-call (vop error-code &rest values)
209   "Cause an error.  ERROR-CODE is the error to cause."
210   (cons 'progn
211         (emit-error-break vop error-trap error-code values)))
212
213
214 (defmacro cerror-call (vop label error-code &rest values)
215   "Cause a continuable error.  If the error is continued, execution resumes at
216   LABEL."
217   `(progn
218      (inst b ,label)
219      ,@(emit-error-break vop cerror-trap error-code values)))
220
221 (defmacro generate-error-code (vop error-code &rest values)
222   "Generate-Error-Code Error-code Value*
223   Emit code for an error with the specified Error-Code and context Values."
224   `(assemble (*elsewhere*)
225      (let ((start-lab (gen-label)))
226        (emit-label start-lab)
227        (error-call ,vop ,error-code ,@values)
228        start-lab)))
229
230 (defmacro generate-cerror-code (vop error-code &rest values)
231   "Generate-CError-Code Error-code Value*
232   Emit code for a continuable error with the specified Error-Code and
233   context Values.  If the error is continued, execution resumes after
234   the GENERATE-CERROR-CODE form."
235   (with-unique-names (continue error)
236     `(let ((,continue (gen-label)))
237        (emit-label ,continue)
238        (assemble (*elsewhere*)
239          (let ((,error (gen-label)))
240            (emit-label ,error)
241            (cerror-call ,vop ,continue ,error-code ,@values)
242            ,error)))))
243 \f
244 ;;;; PSEUDO-ATOMIC
245
246 ;;; handy macro for making sequences look atomic
247 (defmacro pseudo-atomic ((flag-tn &key (extra 0)) &rest forms)
248   `(progn
249      (aver (= (tn-offset ,flag-tn) nl4-offset))
250      (aver (not (minusp ,extra)))
251      (without-scheduling ()
252        (inst li ,flag-tn ,extra)
253        (inst addu alloc-tn 1))
254      ,@forms
255      (without-scheduling ()
256        (let ((label (gen-label)))
257          (inst bgez ,flag-tn label)
258          (inst addu alloc-tn (1- ,extra))
259          (inst break 16)
260          (emit-label label)))))
261 \f
262 ;;;; memory accessor vop generators
263
264 (deftype load/store-index (scale lowtag min-offset
265                                  &optional (max-offset min-offset))
266   `(integer ,(- (truncate (+ (ash 1 16)
267                              (* min-offset n-word-bytes)
268                              (- lowtag))
269                           scale))
270             ,(truncate (- (+ (1- (ash 1 16)) lowtag)
271                           (* max-offset n-word-bytes))
272                        scale)))
273
274 (defmacro define-full-reffer (name type offset lowtag scs el-type
275                                    &optional translate)
276   `(progn
277      (define-vop (,name)
278        ,@(when translate
279            `((:translate ,translate)))
280        (:policy :fast-safe)
281        (:args (object :scs (descriptor-reg))
282               (index :scs (any-reg)))
283        (:arg-types ,type tagged-num)
284        (:temporary (:scs (interior-reg)) lip)
285        (:results (value :scs ,scs))
286        (:result-types ,el-type)
287        (:generator 5
288          (inst add lip object index)
289          (inst lw value lip (- (* ,offset n-word-bytes) ,lowtag))
290          (inst nop)))
291      (define-vop (,(symbolicate name "-C"))
292        ,@(when translate
293            `((:translate ,translate)))
294        (:policy :fast-safe)
295        (:args (object :scs (descriptor-reg)))
296        (:info index)
297        (:arg-types ,type
298                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
299                                                 ,(eval offset))))
300        (:results (value :scs ,scs))
301        (:result-types ,el-type)
302        (:generator 4
303          (inst lw value object (- (* (+ ,offset index) n-word-bytes) ,lowtag))
304          (inst nop)))))
305
306 (defmacro define-full-setter (name type offset lowtag scs el-type
307                                    &optional translate)
308   `(progn
309      (define-vop (,name)
310        ,@(when translate
311            `((:translate ,translate)))
312        (:policy :fast-safe)
313        (:args (object :scs (descriptor-reg))
314               (index :scs (any-reg))
315               (value :scs ,scs :target result))
316        (:arg-types ,type tagged-num ,el-type)
317        (:temporary (:scs (interior-reg)) lip)
318        (:results (result :scs ,scs))
319        (:result-types ,el-type)
320        (:generator 2
321          (inst add lip object index)
322          (inst sw value lip (- (* ,offset n-word-bytes) ,lowtag))
323          (move result value)))
324      (define-vop (,(symbolicate name "-C"))
325        ,@(when translate
326            `((:translate ,translate)))
327        (:policy :fast-safe)
328        (:args (object :scs (descriptor-reg))
329               (value :scs ,scs))
330        (:info index)
331        (:arg-types ,type
332                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
333                                                 ,(eval offset)))
334                    ,el-type)
335        (:results (result :scs ,scs))
336        (:result-types ,el-type)
337        (:generator 1
338          (inst sw value object (- (* (+ ,offset index) n-word-bytes) ,lowtag))
339          (move result value)))))
340
341
342 (defmacro define-partial-reffer (name type size signed offset lowtag scs
343                                       el-type &optional translate)
344   (let ((scale (ecase size (:byte 1) (:short 2))))
345     `(progn
346        (define-vop (,name)
347          ,@(when translate
348              `((:translate ,translate)))
349          (:policy :fast-safe)
350          (:args (object :scs (descriptor-reg))
351                 (index :scs (unsigned-reg)))
352          (:arg-types ,type positive-fixnum)
353          (:results (value :scs ,scs))
354          (:result-types ,el-type)
355          (:temporary (:scs (interior-reg)) lip)
356          (:generator 5
357            (inst addu lip object index)
358            ,@(when (eq size :short)
359                '((inst addu lip index)))
360            (inst ,(ecase size
361                     (:byte (if signed 'lb 'lbu))
362                     (:short (if signed 'lh 'lhu)))
363                  value lip (- (* ,offset n-word-bytes) ,lowtag))
364            (inst nop)))
365        (define-vop (,(symbolicate name "-C"))
366          ,@(when translate
367              `((:translate ,translate)))
368          (:policy :fast-safe)
369          (:args (object :scs (descriptor-reg)))
370          (:info index)
371          (:arg-types ,type
372                      (:constant (load/store-index ,scale
373                                                   ,(eval lowtag)
374                                                   ,(eval offset))))
375          (:results (value :scs ,scs))
376          (:result-types ,el-type)
377          (:generator 4
378            (inst ,(ecase size
379                     (:byte (if signed 'lb 'lbu))
380                     (:short (if signed 'lh 'lhu)))
381                  value object
382                  (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
383            (inst nop))))))
384
385 (defmacro define-partial-setter (name type size offset lowtag scs el-type
386                                       &optional translate)
387   (let ((scale (ecase size (:byte 1) (:short 2))))
388     `(progn
389        (define-vop (,name)
390          ,@(when translate
391              `((:translate ,translate)))
392          (:policy :fast-safe)
393          (:args (object :scs (descriptor-reg))
394                 (index :scs (unsigned-reg))
395                 (value :scs ,scs :target result))
396          (:arg-types ,type positive-fixnum ,el-type)
397          (:temporary (:scs (interior-reg)) lip)
398          (:results (result :scs ,scs))
399          (:result-types ,el-type)
400          (:generator 5
401            (inst addu lip object index)
402            ,@(when (eq size :short)
403                '((inst addu lip index)))
404            (inst ,(ecase size (:byte 'sb) (:short 'sh))
405                  value lip (- (* ,offset n-word-bytes) ,lowtag))
406            (move result value)))
407        (define-vop (,(symbolicate name "-C"))
408          ,@(when translate
409              `((:translate ,translate)))
410          (:policy :fast-safe)
411          (:args (object :scs (descriptor-reg))
412                 (value :scs ,scs :target result))
413          (:info index)
414          (:arg-types ,type
415                      (:constant (load/store-index ,scale
416                                                   ,(eval lowtag)
417                                                   ,(eval offset)))
418                      ,el-type)
419          (:results (result :scs ,scs))
420          (:result-types ,el-type)
421          (:generator 4
422            (inst ,(ecase size (:byte 'sb) (:short 'sh))
423                  value object
424                  (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
425            (move result value))))))
426
427
428 (defmacro sb!sys::with-pinned-objects ((&rest objects) &body body)
429   "Arrange with the garbage collector that the pages occupied by
430 OBJECTS will not be moved in memory for the duration of BODY.
431 Useful for e.g. foreign calls where another thread may trigger
432 garbage collection.  This is currently implemented by disabling GC"
433   (declare (ignore objects))            ;should we eval these for side-effect?
434   `(without-gcing
435     ,@body))