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