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