0bcc9c3277712f1cc7e850094a0e0125f3725d34
[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 0 ,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      (without-scheduling ()
219        (inst b ,label)
220        ,@(emit-error-break vop cerror-trap error-code values))))
221
222 (defmacro generate-error-code (vop error-code &rest values)
223   "Generate-Error-Code Error-code Value*
224   Emit code for an error with the specified Error-Code and context Values."
225   `(assemble (*elsewhere*)
226      (let ((start-lab (gen-label)))
227        (emit-label start-lab)
228        (error-call ,vop ,error-code ,@values)
229        start-lab)))
230
231 (defmacro generate-cerror-code (vop error-code &rest values)
232   "Generate-CError-Code Error-code Value*
233   Emit code for a continuable error with the specified Error-Code and
234   context Values.  If the error is continued, execution resumes after
235   the GENERATE-CERROR-CODE form."
236   (with-unique-names (continue error)
237     `(let ((,continue (gen-label)))
238        (emit-label ,continue)
239        (assemble (*elsewhere*)
240          (let ((,error (gen-label)))
241            (emit-label ,error)
242            (cerror-call ,vop ,continue ,error-code ,@values)
243            ,error)))))
244 \f
245 ;;;; PSEUDO-ATOMIC
246
247 ;;; handy macro for making sequences look atomic
248 (defmacro pseudo-atomic ((flag-tn &key (extra 0)) &rest forms)
249   `(progn
250      (aver (= (tn-offset ,flag-tn) nl4-offset))
251      (aver (not (minusp ,extra)))
252      (without-scheduling ()
253        (inst li ,flag-tn ,extra)
254        (inst addu alloc-tn 1))
255      ,@forms
256      (without-scheduling ()
257        (let ((label (gen-label)))
258          (inst bgez ,flag-tn label)
259          (inst addu alloc-tn (1- ,extra))
260          (inst break 0 16)
261          (emit-label label)))))
262 \f
263 ;;;; memory accessor vop generators
264
265 (deftype load/store-index (scale lowtag min-offset
266                                  &optional (max-offset min-offset))
267   `(integer ,(- (truncate (+ (ash 1 16)
268                              (* min-offset n-word-bytes)
269                              (- lowtag))
270                           scale))
271             ,(truncate (- (+ (1- (ash 1 16)) lowtag)
272                           (* max-offset n-word-bytes))
273                        scale)))
274
275 (defmacro define-full-reffer (name type offset lowtag scs el-type
276                                    &optional translate)
277   `(progn
278      (define-vop (,name)
279        ,@(when translate
280            `((:translate ,translate)))
281        (:policy :fast-safe)
282        (:args (object :scs (descriptor-reg))
283               (index :scs (any-reg)))
284        (:arg-types ,type tagged-num)
285        (:temporary (:scs (interior-reg)) lip)
286        (:results (value :scs ,scs))
287        (:result-types ,el-type)
288        (:generator 5
289          (inst add lip object index)
290          (inst lw value lip (- (* ,offset n-word-bytes) ,lowtag))
291          (inst nop)))
292      (define-vop (,(symbolicate name "-C"))
293        ,@(when translate
294            `((:translate ,translate)))
295        (:policy :fast-safe)
296        (:args (object :scs (descriptor-reg)))
297        (:info index)
298        (:arg-types ,type
299                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
300                                                 ,(eval offset))))
301        (:results (value :scs ,scs))
302        (:result-types ,el-type)
303        (:generator 4
304          (inst lw value object (- (* (+ ,offset index) n-word-bytes) ,lowtag))
305          (inst nop)))))
306
307 (defmacro define-full-setter (name type offset lowtag scs el-type
308                                    &optional translate)
309   `(progn
310      (define-vop (,name)
311        ,@(when translate
312            `((:translate ,translate)))
313        (:policy :fast-safe)
314        (:args (object :scs (descriptor-reg))
315               (index :scs (any-reg))
316               (value :scs ,scs :target result))
317        (:arg-types ,type tagged-num ,el-type)
318        (:temporary (:scs (interior-reg)) lip)
319        (:results (result :scs ,scs))
320        (:result-types ,el-type)
321        (:generator 2
322          (inst add lip object index)
323          (inst sw value lip (- (* ,offset n-word-bytes) ,lowtag))
324          (move result value)))
325      (define-vop (,(symbolicate name "-C"))
326        ,@(when translate
327            `((:translate ,translate)))
328        (:policy :fast-safe)
329        (:args (object :scs (descriptor-reg))
330               (value :scs ,scs))
331        (:info index)
332        (:arg-types ,type
333                    (:constant (load/store-index ,n-word-bytes ,(eval lowtag)
334                                                 ,(eval offset)))
335                    ,el-type)
336        (:results (result :scs ,scs))
337        (:result-types ,el-type)
338        (:generator 1
339          (inst sw value object (- (* (+ ,offset index) n-word-bytes) ,lowtag))
340          (move result value)))))
341
342
343 (defmacro define-partial-reffer (name type size signed offset lowtag scs
344                                       el-type &optional translate)
345   (let ((scale (ecase size (:byte 1) (:short 2))))
346     `(progn
347        (define-vop (,name)
348          ,@(when translate
349              `((:translate ,translate)))
350          (:policy :fast-safe)
351          (:args (object :scs (descriptor-reg))
352                 (index :scs (unsigned-reg)))
353          (:arg-types ,type positive-fixnum)
354          (:results (value :scs ,scs))
355          (:result-types ,el-type)
356          (:temporary (:scs (interior-reg)) lip)
357          (:generator 5
358            (inst addu lip object index)
359            ,@(when (eq size :short)
360                '((inst addu lip index)))
361            (inst ,(ecase size
362                     (:byte (if signed 'lb 'lbu))
363                     (:short (if signed 'lh 'lhu)))
364                  value lip (- (* ,offset n-word-bytes) ,lowtag))
365            (inst nop)))
366        (define-vop (,(symbolicate name "-C"))
367          ,@(when translate
368              `((:translate ,translate)))
369          (:policy :fast-safe)
370          (:args (object :scs (descriptor-reg)))
371          (:info index)
372          (:arg-types ,type
373                      (:constant (load/store-index ,scale
374                                                   ,(eval lowtag)
375                                                   ,(eval offset))))
376          (:results (value :scs ,scs))
377          (:result-types ,el-type)
378          (:generator 4
379            (inst ,(ecase size
380                     (:byte (if signed 'lb 'lbu))
381                     (:short (if signed 'lh 'lhu)))
382                  value object
383                  (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
384            (inst nop))))))
385
386 (defmacro define-partial-setter (name type size offset lowtag scs el-type
387                                       &optional translate)
388   (let ((scale (ecase size (:byte 1) (:short 2))))
389     `(progn
390        (define-vop (,name)
391          ,@(when translate
392              `((:translate ,translate)))
393          (:policy :fast-safe)
394          (:args (object :scs (descriptor-reg))
395                 (index :scs (unsigned-reg))
396                 (value :scs ,scs :target result))
397          (:arg-types ,type positive-fixnum ,el-type)
398          (:temporary (:scs (interior-reg)) lip)
399          (:results (result :scs ,scs))
400          (:result-types ,el-type)
401          (:generator 5
402            (inst addu lip object index)
403            ,@(when (eq size :short)
404                '((inst addu lip index)))
405            (inst ,(ecase size (:byte 'sb) (:short 'sh))
406                  value lip (- (* ,offset n-word-bytes) ,lowtag))
407            (move result value)))
408        (define-vop (,(symbolicate name "-C"))
409          ,@(when translate
410              `((:translate ,translate)))
411          (:policy :fast-safe)
412          (:args (object :scs (descriptor-reg))
413                 (value :scs ,scs :target result))
414          (:info index)
415          (:arg-types ,type
416                      (:constant (load/store-index ,scale
417                                                   ,(eval lowtag)
418                                                   ,(eval offset)))
419                      ,el-type)
420          (:results (result :scs ,scs))
421          (:result-types ,el-type)
422          (:generator 4
423            (inst ,(ecase size (:byte 'sb) (:short 'sh))
424                  value object
425                  (- (+ (* ,offset n-word-bytes) (* index ,scale)) ,lowtag))
426            (move result value))))))
427
428
429 (defmacro sb!sys::with-pinned-objects ((&rest objects) &body body)
430   "Arrange with the garbage collector that the pages occupied by
431 OBJECTS will not be moved in memory for the duration of BODY.
432 Useful for e.g. foreign calls where another thread may trigger
433 garbage collection.  This is currently implemented by disabling GC"
434   (declare (ignore objects))            ;should we eval these for side-effect?
435   `(without-gcing
436     ,@body))