0.6.12.5:
[sbcl.git] / src / compiler / alpha / macros.lisp
1 ;;;; various useful macros for generating Alpha 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
12 (in-package "SB!VM")
13
14 ;;; a handy macro for defining top-level forms that depend on the
15 ;;; compile environment
16 (defmacro expand (expr)
17   (let ((gensym (gensym)))
18     `(macrolet
19          ((,gensym ()
20             ,expr))
21        (,gensym))))
22 \f
23 ;;; instruction-like macros
24
25 ;;; c.f. x86 backend:
26 ;;(defmacro move (dst src)
27 ;;  #!+sb-doc
28 ;;  "Move SRC into DST unless they are location=."
29 ;;  (once-only ((n-dst dst)
30 ;;              (n-src src))
31 ;;    `(unless (location= ,n-dst ,n-src)
32 ;;       (inst mov ,n-dst ,n-src))))
33
34 (defmacro move (src dst)
35   "Move SRC into DST unless they are location=."
36   (once-only ((n-src src) (n-dst dst))
37     `(unless (location= ,n-src ,n-dst)
38        (inst move ,n-src ,n-dst))))
39
40 (defmacro loadw (result base &optional (offset 0) (lowtag 0))
41   (once-only ((result result) (base base))
42     `(inst ldl ,result (- (ash ,offset word-shift) ,lowtag) ,base)))
43
44 (defmacro loadq (result base &optional (offset 0) (lowtag 0))
45   (once-only ((result result) (base base))
46     `(inst ldq ,result (- (ash ,offset word-shift) ,lowtag) ,base)))
47
48 (defmacro storew (value base &optional (offset 0) (lowtag 0))
49   (once-only ((value value) (base base) (offset offset) (lowtag lowtag))
50     `(inst stl ,value (- (ash ,offset word-shift) ,lowtag) ,base)))
51
52 (defmacro storeq (value base &optional (offset 0) (lowtag 0))
53   (once-only ((value value) (base base) (offset offset) (lowtag lowtag))
54     `(inst stq ,value (- (ash ,offset word-shift) ,lowtag) ,base)))
55
56 (defmacro load-symbol (reg symbol)
57   (once-only ((reg reg) (symbol symbol))
58     `(inst lda ,reg (static-symbol-offset ,symbol) null-tn)))
59
60 (defmacro load-symbol-value (reg symbol)
61   `(inst ldl ,reg
62          (+ (static-symbol-offset ',symbol)
63             (ash symbol-value-slot word-shift)
64             (- other-pointer-type))
65          null-tn))
66
67 (defmacro store-symbol-value (reg symbol)
68   `(inst stl ,reg
69          (+ (static-symbol-offset ',symbol)
70             (ash symbol-value-slot word-shift)
71             (- other-pointer-type))
72          null-tn))
73
74 (defmacro load-type (target source &optional (offset 0))
75   "Loads the type bits of a pointer into target independent of
76   byte-ordering issues."
77   (once-only ((n-target target)
78               (n-source source)
79               (n-offset offset))
80      `(progn
81         (inst ldl ,n-target ,n-offset ,n-source)
82         (inst and ,n-target #xff ,n-target))))
83
84 ;;; macros to handle the fact that we cannot use the machine native
85 ;;; call and return instructions
86
87 (defmacro lisp-jump (function lip)
88   "Jump to the lisp function FUNCTION.  LIP is an interior-reg temporary."
89   `(progn
90      (inst lda ,lip (- (ash sb!vm:function-code-offset sb!vm:word-shift)
91                              sb!vm:function-pointer-type)
92             ,function)
93      (move ,function code-tn)
94      (inst jsr zero-tn ,lip 1)))
95
96 (defmacro lisp-return (return-pc lip &key (offset 0) (frob-code t))
97   "Return to RETURN-PC.  LIP is an interior-reg temporary."
98   `(progn
99      (inst lda ,lip  
100            (- (* (1+ ,offset) word-bytes) other-pointer-type)
101             ,return-pc)
102      ,@(when frob-code
103          `((move ,return-pc code-tn)))
104      (inst ret zero-tn ,lip 1)))
105
106
107 (defmacro emit-return-pc (label)
108   "Emit a return-pc header word.  LABEL is the label to use for this
109    return-pc."
110   `(progn
111      (align lowtag-bits)
112      (emit-label ,label)
113      (inst lra-header-word)))
114
115
116 \f
117 ;;;; stack TN's
118
119 ;;;    Move a stack TN to a register and vice-versa.
120 (defmacro load-stack-tn (reg stack)
121   `(let ((reg ,reg)
122          (stack ,stack))
123      (let ((offset (tn-offset stack)))
124        (sc-case stack
125          ((control-stack)
126           (loadw reg cfp-tn offset))))))
127 (defmacro store-stack-tn (stack reg)
128   `(let ((stack ,stack)
129          (reg ,reg))
130      (let ((offset (tn-offset stack)))
131        (sc-case stack
132          ((control-stack)
133           (storew reg cfp-tn offset))))))
134
135 ;;; Move the TN Reg-Or-Stack into Reg if it isn't already there.
136 (defmacro maybe-load-stack-tn (reg reg-or-stack)
137   (once-only ((n-reg reg)
138               (n-stack reg-or-stack))
139     `(sc-case ,n-reg
140        ((any-reg descriptor-reg)
141         (sc-case ,n-stack
142           ((any-reg descriptor-reg)
143            (move ,n-stack ,n-reg))
144           ((control-stack)
145            (loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
146
147 ;;; Move the TN Reg-Or-Stack into Reg if it isn't already there.
148 (defmacro maybe-load-stack-nfp-tn (reg reg-or-stack temp)
149   (once-only ((n-reg reg)
150               (n-stack reg-or-stack))
151      `(when ,reg
152         (sc-case ,n-reg
153          ((any-reg descriptor-reg)
154           (sc-case ,n-stack
155            ((any-reg descriptor-reg)
156             (move ,n-stack ,n-reg))
157            ((control-stack)
158             (loadw ,n-reg cfp-tn (tn-offset ,n-stack))
159             (inst mskll nsp-tn 0 ,temp)
160             (inst bis ,temp ,n-reg ,n-reg))))))))
161 \f
162 ;;;; storage allocation
163
164 ;;; Do stuff to allocate an other-pointer object of fixed Size with a
165 ;;; single word header having the specified Type-Code. The result is
166 ;;; placed in Result-TN, Flag-Tn must be wired to NL3-OFFSET, and
167 ;;; Temp-TN is a non- descriptor temp (which may be randomly used by
168 ;;; the body.) The body is placed inside the PSEUDO-ATOMIC, and
169 ;;; presumably initializes the object.
170 (defmacro with-fixed-allocation ((result-tn temp-tn type-code size)
171                                  &body body)
172   `(pseudo-atomic (:extra (pad-data-block ,size))
173      (inst bis alloc-tn other-pointer-type ,result-tn)
174      (inst li (logior (ash (1- ,size) type-bits) ,type-code) ,temp-tn)
175      (storew ,temp-tn ,result-tn 0 other-pointer-type)
176      ,@body))
177
178
179 \f
180 ;;;; Error Code
181
182
183 (defvar *adjustable-vectors* nil)
184
185 (defmacro with-adjustable-vector ((var) &rest body)
186   `(let ((,var (or (pop *adjustable-vectors*)
187                    (make-array 16
188                                :element-type '(unsigned-byte 8)
189                                :fill-pointer 0
190                                :adjustable t))))
191      (setf (fill-pointer ,var) 0)
192      (unwind-protect
193          (progn
194            ,@body)
195        (push ,var *adjustable-vectors*))))
196
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 gentrap ,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      (inst br zero-tn ,label)
229      ,@(emit-error-break vop cerror-trap error-code values)))
230
231 (defmacro generate-error-code (vop error-code &rest values)
232   "Generate-Error-Code Error-code Value*
233   Emit code for an error with the specified Error-Code and context Values."
234   `(assemble (*elsewhere*)
235      (let ((start-lab (gen-label)))
236        (emit-label start-lab)
237        (error-call ,vop ,error-code ,@values)
238        start-lab)))
239
240 (defmacro generate-cerror-code (vop error-code &rest values)
241   "Generate-CError-Code Error-code Value*
242   Emit code for a continuable error with the specified Error-Code and
243   context Values.  If the error is continued, execution resumes after
244   the GENERATE-CERROR-CODE form."
245   (let ((continue (gensym "CONTINUE-LABEL-"))
246         (error (gensym "ERROR-LABEL-")))
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
255 \f
256 ;;; PSEUDO-ATOMIC -- Handy macro for making sequences look atomic.
257 ;;;
258 (defmacro pseudo-atomic ((&key (extra 0)) &rest forms)
259   `(progn
260      (inst addq alloc-tn 1 alloc-tn)
261      ,@forms
262      (inst lda alloc-tn (1- ,extra) alloc-tn)
263      (inst stl zero-tn 0 alloc-tn)))
264
265
266 \f
267 ;;;; Memory accessor vop generators
268
269 (deftype load/store-index (scale lowtag min-offset
270                                  &optional (max-offset min-offset))
271   `(integer ,(- (truncate (+ (ash 1 16)
272                              (* min-offset word-bytes)
273                              (- lowtag))
274                           scale))
275             ,(truncate (- (+ (1- (ash 1 16)) lowtag)
276                           (* max-offset word-bytes))
277                        scale)))
278
279 (defmacro define-full-reffer (name type offset lowtag scs el-type
280                                    &optional translate)
281   `(progn
282      (define-vop (,name)
283        ,@(when translate
284            `((:translate ,translate)))
285        (:policy :fast-safe)
286        (:args (object :scs (descriptor-reg))
287               (index :scs (any-reg)))
288        (:arg-types ,type tagged-num)
289        (:temporary (:scs (interior-reg)) lip)
290        (:results (value :scs ,scs))
291        (:result-types ,el-type)
292        (:generator 5
293          (inst addq object index lip)
294          (inst ldl value (- (* ,offset word-bytes) ,lowtag) lip)
295          ,@(when (equal scs '(unsigned-reg))
296              '((inst mskll value 4 value)))))
297      (define-vop (,(symbolicate name "-C"))
298        ,@(when translate
299            `((:translate ,translate)))
300        (:policy :fast-safe)
301        (:args (object :scs (descriptor-reg)))
302        (:info index)
303        (:arg-types ,type
304                    (:constant (load/store-index ,word-bytes ,(eval lowtag)
305                                                 ,(eval offset))))
306        (:results (value :scs ,scs))
307        (:result-types ,el-type)
308        (:generator 4
309          (inst ldl value (- (* (+ ,offset index) word-bytes) ,lowtag)
310                object)
311          ,@(when (equal scs '(unsigned-reg))
312              '((inst mskll value 4 value)))))))
313
314 (defmacro define-full-setter (name type offset lowtag scs el-type
315                                    &optional translate #+gengc (remember t))
316   `(progn
317      (define-vop (,name)
318        ,@(when translate
319            `((:translate ,translate)))
320        (:policy :fast-safe)
321        (:args (object :scs (descriptor-reg))
322               (index :scs (any-reg))
323               (value :scs ,scs :target result))
324        (:arg-types ,type tagged-num ,el-type)
325        (:temporary (:scs (interior-reg)) lip)
326        (:results (result :scs ,scs))
327        (:result-types ,el-type)
328        (:generator 2
329          (inst addq index object lip)
330          (inst stl value (- (* ,offset word-bytes) ,lowtag) lip)
331          (move value result)))
332      (define-vop (,(symbolicate name "-C"))
333        ,@(when translate
334            `((:translate ,translate)))
335        (:policy :fast-safe)
336        (:args (object :scs (descriptor-reg))
337               (value :scs ,scs))
338        (:info index)
339        (:arg-types ,type
340                    (:constant (load/store-index ,word-bytes ,(eval lowtag)
341                                                 ,(eval offset)))
342                    ,el-type)
343        (:results (result :scs ,scs))
344        (:result-types ,el-type)
345        (:generator 1
346          (inst stl value (- (* (+ ,offset index) word-bytes) ,lowtag)
347                object)
348          (move value result)))))
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          (:temporary (:sc non-descriptor-reg) temp)
366          (:temporary (:sc non-descriptor-reg) temp1)
367          (:generator 5
368            (inst addq object index lip)
369            ,@(when (eq size :short)
370                '((inst addq index lip lip)))
371            ,@(ecase size
372                (:byte
373                 (if signed
374                     `((inst ldq_u temp (- (* ,offset word-bytes) ,lowtag)
375                             lip)
376                       (inst lda temp1 (1+ (- (* ,offset word-bytes) ,lowtag))
377                             lip)
378                       (inst extqh temp temp1 temp)
379                       (inst sra temp 56 value))
380                     `((inst ldq_u temp (- (* ,offset word-bytes) ,lowtag) lip)
381                       (inst lda temp1 (- (* ,offset word-bytes) ,lowtag)
382                                           lip)
383                       (inst extbl temp temp1 value))))
384                (:short
385                 (if signed
386                     `((inst ldq_u temp (- (* ,offset word-bytes) ,lowtag)
387                             lip)
388                       (inst lda temp1 (- (* ,offset word-bytes) ,lowtag)
389                             lip)
390                       (inst extwl temp temp1 temp)
391                       (inst sll temp 48 temp)
392                       (inst sra temp 48 value))
393                     `((inst ldq_u temp (- (* ,offset word-bytes) ,lowtag)
394                             lip)
395                       (inst lda temp1 (- (* ,offset word-bytes) ,lowtag) lip)
396                       (inst extwl temp temp1 value)))))))
397        (define-vop (,(symbolicate name "-C"))
398          ,@(when translate
399              `((:translate ,translate)))
400          (:policy :fast-safe)
401          (:args (object :scs (descriptor-reg)))
402          (:info index)
403          (:arg-types ,type
404                      (:constant (load/store-index ,scale
405                                                   ,(eval lowtag)
406                                                   ,(eval offset))))
407          (:results (value :scs ,scs))
408          (:result-types ,el-type)
409          (:temporary (:sc non-descriptor-reg) temp)
410          (:temporary (:sc non-descriptor-reg) temp1)
411          (:generator 5
412            ,@(ecase size
413                (:byte
414                 (if signed
415                     `((inst ldq_u temp (- (+ (* ,offset word-bytes)
416                                              (* index ,scale)) ,lowtag)
417                             object)
418                       (inst lda temp1 (1+ (- (+ (* ,offset word-bytes)
419                                                 (* index ,scale)) ,lowtag))
420                             object)
421                       (inst extqh temp temp1 temp)
422                       (inst sra temp 56 value))
423                     `((inst ldq_u temp (- (+ (* ,offset word-bytes)
424                                              (* index ,scale)) ,lowtag)
425                             object)
426                       (inst lda temp1 (- (+ (* ,offset word-bytes)
427                                             (* index ,scale)) ,lowtag)
428                             object)
429                       (inst extbl temp temp1 value))))
430                (:short
431                 (if signed
432                     `((inst ldq_u temp (- (+ (* ,offset word-bytes)
433                                              (* index ,scale)) ,lowtag)
434                             object)
435                       (inst lda temp1 (- (+ (* ,offset word-bytes)
436                                             (* index ,scale)) ,lowtag)
437                             object)
438                       (inst extwl temp temp1 temp)
439                       (inst sll temp 48 temp)
440                       (inst sra temp 48 value))
441                     `((inst ldq_u temp (- (+ (* ,offset word-bytes)
442                                              (* index ,scale)) ,lowtag)
443                             object)
444                       (inst lda temp1 (- (+ (* ,offset word-bytes)
445                                             (* index ,scale)) ,lowtag)
446                             object)
447                       (inst extwl temp temp1 value))))))))))
448
449 (defmacro define-partial-setter (name type size offset lowtag scs el-type
450                                       &optional translate)
451   (let ((scale (ecase size (:byte 1) (:short 2))))
452     `(progn
453        (define-vop (,name)
454          ,@(when translate
455              `((:translate ,translate)))
456          (:policy :fast-safe)
457          (:args (object :scs (descriptor-reg))
458                 (index :scs (unsigned-reg))
459                 (value :scs ,scs :target result))
460          (:arg-types ,type positive-fixnum ,el-type)
461          (:temporary (:scs (interior-reg)) lip)
462          (:temporary (:sc non-descriptor-reg) temp)
463          (:temporary (:sc non-descriptor-reg) temp1)
464          (:temporary (:sc non-descriptor-reg) temp2)
465          (:results (result :scs ,scs))
466          (:result-types ,el-type)
467          (:generator 5
468            (inst addq object index lip)
469            ,@(when (eq size :short)
470                '((inst addq lip index lip)))
471            ,@(ecase size
472                (:byte
473                 `((inst lda temp (- (* ,offset word-bytes) ,lowtag) lip)
474                   (inst ldq_u temp1 (- (* ,offset word-bytes) ,lowtag) lip)
475                   (inst insbl value  temp temp2)
476                   (inst mskbl temp1 temp temp1)
477                   (inst bis temp1 temp2 temp1)
478                   (inst stq_u temp1 (- (* ,offset word-bytes) ,lowtag) lip)))
479                (:short
480                 `((inst lda temp (- (* ,offset word-bytes) ,lowtag) lip)
481                   (inst ldq_u temp1 (- (* ,offset word-bytes) ,lowtag) lip)
482                   (inst mskwl temp1 temp temp1)
483                   (inst inswl value temp temp2)
484                   (inst bis temp1 temp2 temp)
485                   (inst stq_u temp (- (* ,offset word-bytes) ,lowtag) lip))))
486            (move value result)))
487        (define-vop (,(symbolicate name "-C"))
488          ,@(when translate
489              `((:translate ,translate)))
490          (:policy :fast-safe)
491          (:args (object :scs (descriptor-reg))
492                 (value :scs ,scs :target result))
493          (:info index)
494          (:arg-types ,type
495                      (:constant (load/store-index ,scale
496                                                   ,(eval lowtag)
497                                                   ,(eval offset)))
498                      ,el-type)
499          (:temporary (:sc non-descriptor-reg) temp)
500          (:temporary (:sc non-descriptor-reg) temp1)
501          (:temporary (:sc non-descriptor-reg) temp2)
502          (:results (result :scs ,scs))
503          (:result-types ,el-type)
504          (:generator 5
505            ,@(ecase size
506                (:byte
507                 `((inst lda temp (- (* ,offset word-bytes)
508                                     (* index ,scale) ,lowtag)
509                         object)
510                   (inst ldq_u temp1 (- (* ,offset word-bytes) 
511                                        (* index ,scale) ,lowtag)
512                         object)
513                   (inst insbl value temp temp2)
514                   (inst mskbl temp1 temp temp1)
515                   (inst bis temp1 temp2 temp1)
516                   (inst stq_u temp1 (- (* ,offset word-bytes)
517                                        (* index ,scale) ,lowtag) object)))
518                (:short
519                 `((inst lda temp (- (* ,offset word-bytes)
520                                     (* index ,scale) ,lowtag)
521                         object)
522                   (inst ldq_u temp1 (- (* ,offset word-bytes)
523                                        (* index ,scale) ,lowtag)
524                         object)
525                   (inst mskwl temp1 temp temp1)
526                   (inst inswl value temp temp2)
527                   (inst bis temp1 temp2 temp)
528                   (inst stq_u temp (- (* ,offset word-bytes)
529                                       (* index ,scale) ,lowtag) object))))
530            (move value result))))))