c1eac07f8b76cce8a5190325e2a469af49d58969
[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 3))))))
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     (move code-tn ,function)
83     (inst bctr)))
84
85 (defmacro lisp-return (return-pc lip &key (offset 0) (frob-code t))
86   "Return to RETURN-PC."
87   `(progn
88      (inst addi ,lip ,return-pc (- (* (1+ ,offset) n-word-bytes) other-pointer-lowtag))
89      (inst mtlr ,lip)
90      ,@(if frob-code
91          `((move code-tn ,return-pc)))
92      (inst blr)))
93
94 (defmacro emit-return-pc (label)
95   "Emit a return-pc header word.  LABEL is the label to use for this return-pc."
96   `(progn
97      (align n-lowtag-bits)
98      (emit-label ,label)
99      (inst lra-header-word)))
100
101
102 \f
103 ;;;; Stack TN's
104
105 ;;; Move a stack TN to a register and vice-versa.
106 (defmacro load-stack-tn (reg stack)
107   `(let ((reg ,reg)
108          (stack ,stack))
109      (let ((offset (tn-offset stack)))
110        (sc-case stack
111          ((control-stack)
112           (loadw reg cfp-tn offset))))))
113 (defmacro store-stack-tn (stack reg)
114   `(let ((stack ,stack)
115          (reg ,reg))
116      (let ((offset (tn-offset stack)))
117        (sc-case stack
118          ((control-stack)
119           (storew reg cfp-tn offset))))))
120
121 (defmacro maybe-load-stack-tn (reg reg-or-stack)
122   "Move the TN Reg-Or-Stack into Reg if it isn't already there."
123   (once-only ((n-reg reg)
124               (n-stack reg-or-stack))
125     `(sc-case ,n-reg
126        ((any-reg descriptor-reg)
127         (sc-case ,n-stack
128           ((any-reg descriptor-reg)
129            (move ,n-reg ,n-stack))
130           ((control-stack)
131            (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
132
133 \f
134 ;;;; Storage allocation:
135
136 ;;; This is the main mechanism for allocating memory in the lisp heap.
137 ;;;
138 ;;; The allocated space is stored in RESULT-TN with the lowtag LOWTAG
139 ;;; applied.  The amount of space to be allocated is SIZE bytes (which
140 ;;; must be a multiple of the lisp object size).
141 ;;;
142 ;;; On other platforms (Non-PPC), if STACK-P is given, then allocation
143 ;;; occurs on the control stack (for dynamic-extent).  In this case,
144 ;;; you MUST also specify NODE, so that the appropriate compiler
145 ;;; policy can be used, and TEMP-TN, which is needed for work-space.
146 ;;; TEMP-TN MUST be a non-descriptor reg. FIXME: This is not yet
147 ;;; implemented on PPC. We should implement this and replace the
148 ;;; inline stack-based allocation that presently occurs in the
149 ;;; VOPs. The stack-p argument is ignored on PPC.
150 ;;;
151 ;;; If generational GC is enabled, you MUST supply a value for TEMP-TN
152 ;;; because a temp register is needed to do inline allocation.
153 ;;; TEMP-TN, in this case, can be any register, since it holds a
154 ;;; double-word aligned address (essentially a fixnum).
155 (defmacro allocation (result-tn size lowtag &key stack-p node temp-tn flag-tn)
156   ;; We assume we're in a pseudo-atomic so the pseudo-atomic bit is
157   ;; set.  If the lowtag also has a 1 bit in the same position, we're all
158   ;; set.  Otherwise, we need to zap out the lowtag from alloc-tn, and
159   ;; then or in the lowtag.
160   ;; Normal allocation to the heap.
161   (declare (ignore stack-p node)
162            #!-gencgc
163            (ignore temp-tn flag-tn))
164     #!-gencgc
165     (let ((alloc-size (gensym)))
166       `(let ((,alloc-size ,size))
167          (if (logbitp (1- n-lowtag-bits) ,lowtag)
168              (progn
169                (inst ori ,result-tn alloc-tn ,lowtag))
170              (progn
171                (inst clrrwi ,result-tn alloc-tn n-lowtag-bits)
172                (inst ori ,result-tn ,result-tn ,lowtag)))
173          (if (numberp ,alloc-size)
174              (inst addi alloc-tn alloc-tn ,alloc-size)
175              (inst add alloc-tn alloc-tn ,alloc-size))))
176     #!+gencgc
177     (let ((fix-addr (gensym))
178           (inline-alloc (gensym)))
179       `(let ((,fix-addr (gen-label))
180              (,inline-alloc (gen-label)))
181          ;; Make temp-tn be the size
182          (cond ((numberp ,size)
183                 (inst lr ,temp-tn ,size))
184                (t
185                 (move ,temp-tn ,size)))
186
187          (inst lr ,flag-tn (make-fixup "boxed_region" :foreign))
188          (inst lwz ,result-tn ,flag-tn 0)
189
190          ;; we can optimize this to only use one fixup here, once we get
191          ;; it working
192          ;; (inst lr ,flag-tn (make-fixup "boxed_region" :foreign 4))
193          ;; (inst lwz ,flag-tn ,flag-tn 0)
194          (inst lwz ,flag-tn ,flag-tn 4)
195
196          (without-scheduling ()
197            ;; CAUTION: The C code depends on the exact order of
198            ;; instructions here.  In particular, three instructions before
199            ;; the TW instruction must be an ADD or ADDI instruction, so it
200            ;; can figure out the size of the desired allocation.
201            ;; Now make result-tn point at the end of the object, to
202            ;; figure out if we overflowed the current region.
203            (inst add ,result-tn ,result-tn ,temp-tn)
204            ;; result-tn points to the new end of the region.  Did we go past
205            ;; the actual end of the region?  If so, we need a full alloc.
206            ;; The C code depends on this exact form of instruction.  If
207            ;; either changes, you have to change the other appropriately!
208            (inst cmpw ,result-tn ,flag-tn)
209
210            (inst bng ,inline-alloc)
211            (inst tw :lge ,result-tn ,flag-tn))
212          (inst b ,fix-addr)
213
214          (emit-label ,inline-alloc)
215          (inst lr ,flag-tn (make-fixup "boxed_region" :foreign))
216          (inst stw ,result-tn ,flag-tn 0)
217
218          (emit-label ,fix-addr)
219          ;; At this point, result-tn points at the end of the object.
220          ;; Adjust to point to the beginning.
221          (inst sub ,result-tn ,result-tn ,temp-tn)
222          ;; Set the lowtag appropriately
223          (inst ori ,result-tn ,result-tn ,lowtag))))
224
225 (defmacro with-fixed-allocation ((result-tn flag-tn temp-tn type-code size
226                                             &key (lowtag other-pointer-lowtag))
227                                  &body body)
228   "Do stuff to allocate an other-pointer object of fixed Size with a single
229   word header having the specified Type-Code.  The result is placed in
230   Result-TN, and Temp-TN is a non-descriptor temp (which may be randomly used
231   by the body.)  The body is placed inside the PSEUDO-ATOMIC, and presumably
232   initializes the object."
233   (once-only ((result-tn result-tn) (temp-tn temp-tn) (flag-tn flag-tn)
234               (type-code type-code) (size size) (lowtag lowtag))
235     `(pseudo-atomic (,flag-tn)
236        (allocation ,result-tn (pad-data-block ,size) ,lowtag
237                    :temp-tn ,temp-tn
238                    :flag-tn ,flag-tn)
239        (when ,type-code
240          (inst lr ,temp-tn (logior (ash (1- ,size) n-widetag-bits) ,type-code))
241          (storew ,temp-tn ,result-tn 0 ,lowtag))
242        ,@body)))
243
244 (defun align-csp (temp)
245   ;; is used for stack allocation of dynamic-extent objects
246   (let ((aligned (gen-label)))
247     (inst andi. temp csp-tn lowtag-mask)
248     (inst beq aligned)
249     (inst addi csp-tn csp-tn n-word-bytes)
250     (storew zero-tn csp-tn -1)
251     (emit-label aligned)))
252
253 \f
254 ;;;; Error Code
255 (eval-when (:compile-toplevel :load-toplevel :execute)
256   (defun emit-error-break (vop kind code values)
257     (let ((vector (gensym)))
258       `((let ((vop ,vop))
259           (when vop
260             (note-this-location vop :internal-error)))
261         (inst unimp ,kind)
262         (with-adjustable-vector (,vector)
263           (write-var-integer (error-number-or-lose ',code) ,vector)
264           ,@(mapcar #'(lambda (tn)
265                         `(let ((tn ,tn))
266                            (write-var-integer (make-sc-offset (sc-number
267                                                                (tn-sc tn))
268                                                               (tn-offset tn))
269                                               ,vector)))
270                     values)
271           (inst byte (length ,vector))
272           (dotimes (i (length ,vector))
273             (inst byte (aref ,vector i))))
274         (align word-shift)))))
275
276 (defmacro error-call (vop error-code &rest values)
277   "Cause an error.  ERROR-CODE is the error to cause."
278   (cons 'progn
279         (emit-error-break vop error-trap error-code values)))
280
281
282 (defmacro cerror-call (vop label error-code &rest values)
283   "Cause a continuable error.  If the error is continued, execution resumes at
284   LABEL."
285   `(progn
286      ,@(emit-error-break vop cerror-trap error-code values)
287      (inst b ,label)))
288
289 (defmacro generate-error-code (vop error-code &rest values)
290   "Generate-Error-Code Error-code Value*
291   Emit code for an error with the specified Error-Code and context Values."
292   `(assemble (*elsewhere*)
293      (let ((start-lab (gen-label)))
294        (emit-label start-lab)
295        (error-call ,vop ,error-code ,@values)
296        start-lab)))
297
298 (defmacro generate-cerror-code (vop error-code &rest values)
299   "Generate-CError-Code Error-code Value*
300   Emit code for a continuable error with the specified Error-Code and
301   context Values.  If the error is continued, execution resumes after
302   the GENERATE-CERROR-CODE form."
303   (with-unique-names (continue error)
304     `(let ((,continue (gen-label)))
305        (emit-label ,continue)
306        (assemble (*elsewhere*)
307          (let ((,error (gen-label)))
308            (emit-label ,error)
309            (cerror-call ,vop ,continue ,error-code ,@values)
310            ,error)))))
311 \f
312 ;;;; PSEUDO-ATOMIC
313
314 ;;; handy macro for making sequences look atomic
315 ;;;
316 ;;; FLAG-TN must be wired to NL3. If a deferred interrupt happens
317 ;;; while we have the low bits of ALLOC-TN set, we add a "large"
318 ;;; constant to FLAG-TN. On exit, we add FLAG-TN to ALLOC-TN which (a)
319 ;;; aligns ALLOC-TN again and (b) makes ALLOC-TN go negative. We then
320 ;;; trap if ALLOC-TN's negative (handling the deferred interrupt) and
321 ;;; using FLAG-TN - minus the large constant - to correct ALLOC-TN.
322 (defmacro pseudo-atomic ((flag-tn) &body forms)
323   `(progn
324      (without-scheduling ()
325        ;; Extra debugging stuff:
326        #+debug
327        (progn
328          (inst andi. ,flag-tn alloc-tn 7)
329          (inst twi :ne ,flag-tn 0))
330        (inst ori alloc-tn alloc-tn 4))
331      ,@forms
332      (without-scheduling ()
333        (inst li ,flag-tn -5)
334        (inst and alloc-tn alloc-tn ,flag-tn)
335        ;; Now test to see if the pseudo-atomic interrupted bit is set.
336        (inst andi. ,flag-tn alloc-tn 1)
337        (inst twi :ne ,flag-tn 0))
338      #+debug
339      (progn
340        (inst andi. ,flag-tn alloc-tn 7)
341        (inst twi :ne ,flag-tn 0))))
342
343 (defmacro sb!sys::with-pinned-objects ((&rest objects) &body body)
344   "Arrange with the garbage collector that the pages occupied by
345 OBJECTS will not be moved in memory for the duration of BODY.
346 Useful for e.g. foreign calls where another thread may trigger
347 garbage collection.  This is currently implemented by disabling GC"
348   (declare (ignore objects))            ; should we eval these for side-effect?
349   `(without-gcing
350     ,@body))