8b704747fb1d45db38b9f45921099cad254dfd1e
[sbcl.git] / src / compiler / ppc / macros.lisp
1 ;;;; a bunch of handy macros for the PPC
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
12 (in-package "SB!VM")
13 \f
14 ;;; Instruction-like macros.
15
16 (defmacro move (dst src)
17   "Move SRC into DST unless they are location=."
18   (once-only ((n-dst dst)
19               (n-src src))
20     `(unless (location= ,n-dst ,n-src)
21        (inst mr ,n-dst ,n-src))))
22
23 (macrolet
24     ((def (op inst shift)
25        `(defmacro ,op (object base &optional (offset 0) (lowtag 0))
26           `(inst ,',inst ,object ,base (- (ash ,offset ,,shift) ,lowtag)))))
27   (def loadw lwz word-shift)
28   (def storew stw word-shift))
29
30 (defmacro load-symbol (reg symbol)
31   `(inst addi ,reg null-tn (static-symbol-offset ,symbol)))
32
33 (macrolet
34     ((frob (slot)
35        (let ((loader (intern (concatenate 'simple-string
36                                           "LOAD-SYMBOL-"
37                                           (string slot))))
38              (storer (intern (concatenate 'simple-string
39                                           "STORE-SYMBOL-"
40                                           (string slot))))
41              (offset (intern (concatenate 'simple-string
42                                           "SYMBOL-"
43                                           (string slot)
44                                           "-SLOT")
45                              (find-package "SB!VM"))))
46          `(progn
47             (defmacro ,loader (reg symbol)
48               `(inst lwz ,reg null-tn
49                      (+ (static-symbol-offset ',symbol)
50                         (ash ,',offset word-shift)
51                         (- other-pointer-lowtag))))
52             (defmacro ,storer (reg symbol)
53               `(inst stw ,reg null-tn
54                      (+ (static-symbol-offset ',symbol)
55                         (ash ,',offset word-shift)
56                         (- other-pointer-lowtag))))))))
57   (frob value)
58   (frob function))
59
60 (defmacro load-type (target source &optional (offset 0))
61   "Loads the type bits of a pointer into target independent of
62   byte-ordering issues."
63   (once-only ((n-target target)
64               (n-source source)
65               (n-offset offset))
66     (ecase *backend-byte-order*
67       (:little-endian
68        `(inst lbz ,n-target ,n-source ,n-offset))
69       (:big-endian
70        `(inst lbz ,n-target ,n-source (+ ,n-offset (1- n-word-bytes)))))))
71
72 ;;; Macros to handle the fact that we cannot use the machine native call and
73 ;;; return instructions.
74
75 (defmacro lisp-jump (function lip)
76   "Jump to the lisp function FUNCTION.  LIP is an interior-reg temporary."
77   `(progn
78     ;; something is deeply bogus.  look at this
79     ;; (loadw ,lip ,function function-code-offset function-pointer-type)
80     (inst addi ,lip ,function (- (* n-word-bytes simple-fun-code-offset) fun-pointer-lowtag))
81     (inst mtctr ,lip)
82     (inst bctr)))
83
84 (defmacro lisp-return (return-pc lip &key (offset 0))
85   "Return to RETURN-PC."
86   `(progn
87      (inst addi ,lip ,return-pc (- (* (1+ ,offset) n-word-bytes) other-pointer-lowtag))
88      (inst mtlr ,lip)
89      (inst blr)))
90
91 (defmacro emit-return-pc (label)
92   "Emit a return-pc header word.  LABEL is the label to use for this return-pc."
93   `(progn
94      (emit-alignment n-lowtag-bits)
95      (emit-label ,label)
96      (inst lra-header-word)))
97
98
99 \f
100 ;;;; Stack TN's
101
102 ;;; Move a stack TN to a register and vice-versa.
103 (defmacro load-stack-tn (reg stack)
104   `(let ((reg ,reg)
105          (stack ,stack))
106      (let ((offset (tn-offset stack)))
107        (sc-case stack
108          ((control-stack)
109           (loadw reg cfp-tn offset))))))
110 (defmacro store-stack-tn (stack reg)
111   `(let ((stack ,stack)
112          (reg ,reg))
113      (let ((offset (tn-offset stack)))
114        (sc-case stack
115          ((control-stack)
116           (storew reg cfp-tn offset))))))
117
118 (defmacro maybe-load-stack-tn (reg reg-or-stack)
119   "Move the TN Reg-Or-Stack into Reg if it isn't already there."
120   (once-only ((n-reg reg)
121               (n-stack reg-or-stack))
122     `(sc-case ,n-reg
123        ((any-reg descriptor-reg)
124         (sc-case ,n-stack
125           ((any-reg descriptor-reg)
126            (move ,n-reg ,n-stack))
127           ((control-stack)
128            (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
129
130 \f
131 ;;;; Storage allocation:
132
133 ;;; This is the main mechanism for allocating memory in the lisp heap.
134 ;;;
135 ;;; The allocated space is stored in RESULT-TN with the lowtag LOWTAG
136 ;;; applied.  The amount of space to be allocated is SIZE bytes (which
137 ;;; must be a multiple of the lisp object size).
138 ;;;
139 ;;; On other platforms (Non-PPC), if STACK-P is given, then allocation
140 ;;; occurs on the control stack (for dynamic-extent).  In this case,
141 ;;; you MUST also specify NODE, so that the appropriate compiler
142 ;;; policy can be used, and TEMP-TN, which is needed for work-space.
143 ;;; TEMP-TN MUST be a non-descriptor reg. FIXME: This is not yet
144 ;;; implemented on PPC. We should implement this and replace the
145 ;;; inline stack-based allocation that presently occurs in the
146 ;;; VOPs. The stack-p argument is ignored on PPC.
147 ;;;
148 ;;; If generational GC is enabled, you MUST supply a value for TEMP-TN
149 ;;; because a temp register is needed to do inline allocation.
150 ;;; TEMP-TN, in this case, can be any register, since it holds a
151 ;;; double-word aligned address (essentially a fixnum).
152 (defmacro allocation (result-tn size lowtag &key stack-p node temp-tn flag-tn)
153   ;; We assume we're in a pseudo-atomic so the pseudo-atomic bit is
154   ;; set.  If the lowtag also has a 1 bit in the same position, we're all
155   ;; set.  Otherwise, we need to zap out the lowtag from alloc-tn, and
156   ;; then or in the lowtag.
157   ;; Normal allocation to the heap.
158   (declare (ignore stack-p node)
159            #!-gencgc
160            (ignore temp-tn flag-tn))
161     #!-gencgc
162     (let ((alloc-size (gensym)))
163       `(let ((,alloc-size ,size))
164          (if (logbitp (1- n-lowtag-bits) ,lowtag)
165              (progn
166                (inst ori ,result-tn alloc-tn ,lowtag))
167              (progn
168                (inst clrrwi ,result-tn alloc-tn n-lowtag-bits)
169                (inst ori ,result-tn ,result-tn ,lowtag)))
170          (if (numberp ,alloc-size)
171              (inst addi alloc-tn alloc-tn ,alloc-size)
172              (inst add alloc-tn alloc-tn ,alloc-size))))
173     #!+gencgc
174     (let ((fix-addr (gensym))
175           (inline-alloc (gensym)))
176       `(let ((,fix-addr (gen-label))
177              (,inline-alloc (gen-label)))
178          ;; Make temp-tn be the size
179          (cond ((numberp ,size)
180                 (inst lr ,temp-tn ,size))
181                (t
182                 (move ,temp-tn ,size)))
183
184          (inst lr ,flag-tn (make-fixup "boxed_region" :foreign))
185          (inst lwz ,result-tn ,flag-tn 0)
186
187          ;; we can optimize this to only use one fixup here, once we get
188          ;; it working
189          ;; (inst lr ,flag-tn (make-fixup "boxed_region" :foreign 4))
190          ;; (inst lwz ,flag-tn ,flag-tn 0)
191          (inst lwz ,flag-tn ,flag-tn 4)
192
193          (without-scheduling ()
194            ;; CAUTION: The C code depends on the exact order of
195            ;; instructions here.  In particular, three instructions before
196            ;; the TW instruction must be an ADD or ADDI instruction, so it
197            ;; can figure out the size of the desired allocation.
198            ;; Now make result-tn point at the end of the object, to
199            ;; figure out if we overflowed the current region.
200            (inst add ,result-tn ,result-tn ,temp-tn)
201            ;; result-tn points to the new end of the region.  Did we go past
202            ;; the actual end of the region?  If so, we need a full alloc.
203            ;; The C code depends on this exact form of instruction.  If
204            ;; either changes, you have to change the other appropriately!
205            (inst cmpw ,result-tn ,flag-tn)
206
207            (inst bng ,inline-alloc)
208            (inst tw :lge ,result-tn ,flag-tn))
209          (inst b ,fix-addr)
210
211          (emit-label ,inline-alloc)
212          (inst lr ,flag-tn (make-fixup "boxed_region" :foreign))
213          (inst stw ,result-tn ,flag-tn 0)
214
215          (emit-label ,fix-addr)
216          ;; At this point, result-tn points at the end of the object.
217          ;; Adjust to point to the beginning.
218          (inst sub ,result-tn ,result-tn ,temp-tn)
219          ;; Set the lowtag appropriately
220          (inst ori ,result-tn ,result-tn ,lowtag))))
221
222 (defmacro with-fixed-allocation ((result-tn flag-tn temp-tn type-code size
223                                             &key (lowtag other-pointer-lowtag))
224                                  &body body)
225   "Do stuff to allocate an other-pointer object of fixed Size with a single
226   word header having the specified Type-Code.  The result is placed in
227   Result-TN, and Temp-TN is a non-descriptor temp (which may be randomly used
228   by the body.)  The body is placed inside the PSEUDO-ATOMIC, and presumably
229   initializes the object."
230   (once-only ((result-tn result-tn) (temp-tn temp-tn) (flag-tn flag-tn)
231               (type-code type-code) (size size) (lowtag lowtag))
232     `(pseudo-atomic (,flag-tn)
233        (allocation ,result-tn (pad-data-block ,size) ,lowtag
234                    :temp-tn ,temp-tn
235                    :flag-tn ,flag-tn)
236        (when ,type-code
237          (inst lr ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
238          (storew ,temp-tn ,result-tn 0 ,lowtag))
239        ,@body)))
240
241 (defun align-csp (temp)
242   ;; is used for stack allocation of dynamic-extent objects
243   (let ((aligned (gen-label)))
244     (inst andi. temp csp-tn lowtag-mask)
245     (inst beq aligned)
246     (inst addi csp-tn csp-tn n-word-bytes)
247     (storew zero-tn csp-tn -1)
248     (emit-label aligned)))
249
250 \f
251 ;;;; Error Code
252 (defun emit-error-break (vop kind code values)
253   (assemble ()
254     (when vop
255       (note-this-location vop :internal-error))
256     (inst unimp kind)
257     (with-adjustable-vector (vector)
258       (write-var-integer code vector)
259       (dolist (tn values)
260         (write-var-integer (make-sc-offset (sc-number (tn-sc tn))
261                                            (or (tn-offset tn) 0))
262                            vector))
263       (inst byte (length vector))
264       (dotimes (i (length vector))
265         (inst byte (aref vector i)))
266       (emit-alignment word-shift))))
267
268 (defun error-call (vop error-code &rest values)
269   #!+sb-doc
270   "Cause an error.  ERROR-CODE is the error to cause."
271   (emit-error-break vop error-trap (error-number-or-lose error-code) values))
272
273 (defun generate-error-code (vop error-code &rest values)
274   #!+sb-doc
275   "Generate-Error-Code Error-code Value*
276   Emit code for an error with the specified Error-Code and context Values."
277   (assemble (*elsewhere*)
278     (let ((start-lab (gen-label)))
279       (emit-label start-lab)
280       (emit-error-break vop error-trap (error-number-or-lose error-code) values)
281       start-lab)))
282 \f
283 ;;;; PSEUDO-ATOMIC
284
285 ;;; handy macro for making sequences look atomic
286 ;;;
287 ;;; FLAG-TN must be wired to NL3. If a deferred interrupt happens
288 ;;; while we have the low bits of ALLOC-TN set, we add a "large"
289 ;;; constant to FLAG-TN. On exit, we add FLAG-TN to ALLOC-TN which (a)
290 ;;; aligns ALLOC-TN again and (b) makes ALLOC-TN go negative. We then
291 ;;; trap if ALLOC-TN's negative (handling the deferred interrupt) and
292 ;;; using FLAG-TN - minus the large constant - to correct ALLOC-TN.
293 (defmacro pseudo-atomic ((flag-tn) &body forms)
294   `(progn
295      (without-scheduling ()
296        ;; Extra debugging stuff:
297        #+debug
298        (progn
299          (inst andi. ,flag-tn alloc-tn 7)
300          (inst twi :ne ,flag-tn 0))
301        (inst ori alloc-tn alloc-tn 4))
302      ,@forms
303      (without-scheduling ()
304        (inst li ,flag-tn -5)
305        (inst and alloc-tn alloc-tn ,flag-tn)
306        ;; Now test to see if the pseudo-atomic interrupted bit is set.
307        (inst andi. ,flag-tn alloc-tn 1)
308        (inst twi :ne ,flag-tn 0))
309      #+debug
310      (progn
311        (inst andi. ,flag-tn alloc-tn 7)
312        (inst twi :ne ,flag-tn 0))))
313
314 (def!macro with-pinned-objects ((&rest objects) &body body)
315   "Arrange with the garbage collector that the pages occupied by
316 OBJECTS will not be moved in memory for the duration of BODY.
317 Useful for e.g. foreign calls where another thread may trigger
318 garbage collection.  This is currently implemented by disabling GC"
319   #!-gencgc
320   (declare (ignore objects))            ; should we eval these for side-effect?
321   #!-gencgc
322   `(without-gcing
323     ,@body)
324   #!+gencgc
325   `(let ((*pinned-objects* (list* ,@objects *pinned-objects*)))
326      ,@body))