Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / meta-vmdef.lisp
blob528b4ed51e8526b133a2408a639fede3ef875cb7
1 ;;;; This file contains the implementation-independent facilities used
2 ;;;; for defining the compiler's interface to the VM in a given
3 ;;;; implementation that are needed at meta-compile time. They are
4 ;;;; separated out from vmdef.lisp so that they can be compiled and
5 ;;;; loaded without trashing the running compiler.
6 ;;;;
7 ;;;; FIXME: The "trashing the running [CMU CL] compiler" motivation no
8 ;;;; longer makes sense in SBCL, since we can cross-compile cleanly.
10 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; more information.
12 ;;;;
13 ;;;; This software is derived from the CMU CL system, which was
14 ;;;; written at Carnegie Mellon University and released into the
15 ;;;; public domain. The software is in the public domain and is
16 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
17 ;;;; files for more information.
19 (in-package "SB!C")
21 ;;;; storage class and storage base definition
23 ;;; Define a storage base having the specified NAME. KIND may be :FINITE,
24 ;;; :UNBOUNDED or :NON-PACKED. The following keywords are legal:
25 ;;; :SIZE specifies the number of locations in a :FINITE SB or
26 ;;; the initial size of an :UNBOUNDED SB.
27 ;;;
28 ;;; We enter the basic structure at meta-compile time, and then fill
29 ;;; in the missing slots at load time.
30 (defmacro define-storage-base (name kind &key size (size-increment size)
31 (size-alignment 1))
33 (declare (type symbol name))
34 (declare (type (member :finite :unbounded :non-packed) kind))
36 ;; SIZE is either mandatory or forbidden.
37 (ecase kind
38 (:non-packed
39 (when size
40 (error "A size specification is meaningless in a ~S SB." kind)))
41 ((:finite :unbounded)
42 (unless size (error "Size is not specified in a ~S SB." kind))
43 (aver (typep size 'unsigned-byte))
44 (aver (= 1 (logcount size-alignment)))
45 (aver (not (logtest size (1- size-alignment))))
46 (aver (not (logtest size-increment (1- size-alignment))))))
48 (let ((sb (if (eq kind :non-packed)
49 (make-sb :name name :kind kind)
50 (make-finite-sb :name name :kind kind :size size
51 :size-increment size-increment
52 :size-alignment size-alignment))))
53 `(progn
54 (/show0 "in DEFINE-STORAGE-BASE")
55 ;; DEFINE-STORAGE-CLASS need the storage bases while building
56 ;; the cross-compiler, but to eval this during cross-compilation
57 ;; would kill the cross-compiler.
58 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
59 (let ((sb (,(if (eq kind :non-packed) 'copy-sb 'copy-finite-sb)
60 ',sb)))
61 (setf *backend-sb-list*
62 (cons sb (remove ',name *backend-sb-list* :key #'sb-name)))))
63 ,@(unless (eq kind :non-packed)
64 `((let ((res (sb-or-lose ',name)))
65 (/show0 "not :NON-PACKED, i.e. hairy case")
66 (setf (finite-sb-always-live res)
67 (make-array ',size :initial-element #*))
68 (/show0 "doing second SETF")
69 (setf (finite-sb-conflicts res)
70 (make-array ',size :initial-element '#()))
71 (/show0 "doing third SETF")
72 (setf (finite-sb-live-tns res)
73 (make-array ',size :initial-element nil))
74 (/show0 "doing fourth SETF")
75 (setf (finite-sb-always-live-count res)
76 (make-array ',size :initial-element 0)))))
77 (/show0 "finished with DEFINE-STORAGE-BASE expansion")
78 ',name)))
80 ;;; Define a storage class NAME that uses the named Storage-Base.
81 ;;; NUMBER is a small, non-negative integer that is used as an alias.
82 ;;; The following keywords are defined:
83 ;;;
84 ;;; :ELEMENT-SIZE Size
85 ;;; The size of objects in this SC in whatever units the SB uses.
86 ;;; This defaults to 1.
87 ;;;
88 ;;; :ALIGNMENT Size
89 ;;; The alignment restrictions for this SC. TNs will only be
90 ;;; allocated at offsets that are an even multiple of this number.
91 ;;; This defaults to 1.
92 ;;;
93 ;;; :LOCATIONS (Location*)
94 ;;; If the SB is :FINITE, then this is a list of the offsets within
95 ;;; the SB that are in this SC.
96 ;;;
97 ;;; :RESERVE-LOCATIONS (Location*)
98 ;;; A subset of the Locations that the register allocator should try to
99 ;;; reserve for operand loading (instead of to hold variable values.)
101 ;;; :SAVE-P {T | NIL}
102 ;;; If T, then values stored in this SC must be saved in one of the
103 ;;; non-save-p :ALTERNATE-SCs across calls.
105 ;;; :ALTERNATE-SCS (SC*)
106 ;;; Indicates other SCs that can be used to hold values from this SC across
107 ;;; calls or when storage in this SC is exhausted. The SCs should be
108 ;;; specified in order of decreasing \"goodness\". There must be at least
109 ;;; one SC in an unbounded SB, unless this SC is only used for restricted or
110 ;;; wired TNs.
112 ;;; :CONSTANT-SCS (SC*)
113 ;;; A list of the names of all the constant SCs that can be loaded into this
114 ;;; SC by a move function.
115 (defmacro define-storage-class (name number sb-name &key (element-size '1)
116 (alignment '1) locations reserve-locations
117 save-p alternate-scs constant-scs)
118 (declare (type symbol name))
119 (declare (type sc-number number))
120 (declare (type symbol sb-name))
121 (declare (type list locations reserve-locations alternate-scs constant-scs))
122 (declare (type boolean save-p))
123 (unless (= (logcount alignment) 1)
124 (error "alignment not a power of two: ~W" alignment))
126 (let ((sb (sb-or-lose sb-name)))
127 (if (eq (sb-kind sb) :finite)
128 (let ((size (sb-size sb))
129 (element-size (eval element-size)))
130 (declare (type unsigned-byte element-size))
131 (dolist (el locations)
132 (declare (type unsigned-byte el))
133 (unless (<= 1 (+ el element-size) size)
134 (error "SC element ~W out of bounds for ~S" el sb))))
135 (when locations
136 (error ":LOCATIONS is meaningless in a ~S SB." (sb-kind sb))))
138 (unless (subsetp reserve-locations locations)
139 (error "RESERVE-LOCATIONS not a subset of LOCATIONS."))
141 (when (and (or alternate-scs constant-scs)
142 (eq (sb-kind sb) :non-packed))
143 (error
144 "It's meaningless to specify alternate or constant SCs in a ~S SB."
145 (sb-kind sb))))
147 (let ((nstack-p
148 (if (or (eq sb-name 'non-descriptor-stack)
149 (find 'non-descriptor-stack
150 (mapcar #'sc-or-lose alternate-scs)
151 :key (lambda (x)
152 (sb-name (sc-sb x)))))
153 t nil)))
154 `(progn
155 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
156 (let ((res (make-sc :name ',name :number ',number
157 :sb (sb-or-lose ',sb-name)
158 :element-size ,element-size
159 :alignment ,alignment
160 :locations ',locations
161 :reserve-locations ',reserve-locations
162 :save-p ',save-p
163 :number-stack-p ,nstack-p
164 :alternate-scs (mapcar #'sc-or-lose
165 ',alternate-scs)
166 :constant-scs (mapcar #'sc-or-lose
167 ',constant-scs))))
168 (setf (gethash ',name *backend-sc-names*) res)
169 (setf (svref (sc-load-costs res) ',number) 0)))
171 (let ((old (svref *backend-sc-numbers* ',number)))
172 (when (and old (not (eq (sc-name old) ',name)))
173 (warn "redefining SC number ~W from ~S to ~S" ',number
174 (sc-name old) ',name)))
176 (setf (svref *backend-sc-numbers* ',number) (sc-or-lose ',name))
177 (setf (gethash ',name *backend-sc-names*) (sc-or-lose ',name))
178 (setf (sc-sb (sc-or-lose ',name)) (sb-or-lose ',sb-name))
179 ',name)))
181 ;;;; move/coerce definition
183 ;;; Given a list of pairs of lists of SCs (as given to DEFINE-MOVE-VOP,
184 ;;; etc.), bind TO-SC and FROM-SC to all the combinations.
185 (defmacro do-sc-pairs ((from-sc-var to-sc-var scs) &body body)
186 `(do ((froms ,scs (cddr froms))
187 (tos (cdr ,scs) (cddr tos)))
188 ((null froms))
189 (dolist (from (car froms))
190 (let ((,from-sc-var (sc-or-lose from)))
191 (dolist (to (car tos))
192 (let ((,to-sc-var (sc-or-lose to)))
193 ,@body))))))
195 ;;; Define the function NAME and note it as the function used for
196 ;;; moving operands from the From-SCs to the To-SCs. Cost is the cost
197 ;;; of this move operation. The function is called with three
198 ;;; arguments: the VOP (for context), and the source and destination
199 ;;; TNs. An ASSEMBLE form is wrapped around the body. All uses of
200 ;;; DEFINE-MOVE-FUN should be compiled before any uses of
201 ;;; DEFINE-VOP.
202 (defmacro define-move-fun ((name cost) lambda-list scs &body body)
203 (declare (type index cost))
204 (when (or (oddp (length scs)) (null scs))
205 (error "malformed SCs spec: ~S" scs))
206 `(progn
207 (eval-when (:compile-toplevel :load-toplevel :execute)
208 (do-sc-pairs (from-sc to-sc ',scs)
209 (unless (eq from-sc to-sc)
210 (let ((num (sc-number from-sc)))
211 (setf (svref (sc-move-funs to-sc) num) ',name)
212 (setf (svref (sc-load-costs to-sc) num) ',cost)))))
214 (defun ,name ,lambda-list
215 (sb!assem:assemble (*code-segment* ,(first lambda-list))
216 ,@body))))
218 (eval-when (:compile-toplevel :load-toplevel :execute)
219 (defparameter *sc-vop-slots*
220 '((:move . sc-move-vops)
221 (:move-arg . sc-move-arg-vops))))
223 ;;;; primitive type definition
225 ;;; Define a primitive type NAME. Each SCS entry specifies a storage
226 ;;; class that values of this type may be allocated in. TYPE is the
227 ;;; type descriptor for the Lisp type that is equivalent to this type.
228 (defmacro !def-primitive-type (name scs &key (type name))
229 (declare (type symbol name) (type list scs))
230 (let ((scns (mapcar #'sc-number-or-lose scs)))
231 `(progn
232 (/show0 "doing !DEF-PRIMITIVE-TYPE, NAME=..")
233 (/primitive-print ,(symbol-name name))
234 (assert (not (gethash ',name *backend-primitive-type-names*)))
235 (setf (gethash ',name *backend-primitive-type-names*)
236 (make-primitive-type :name ',name
237 :scs ',scns
238 :specifier ',type))
239 (/show0 "done with !DEF-PRIMITIVE-TYPE")
240 ',name)))
242 ;;; Define NAME to be an alias for RESULT in VOP operand type restrictions.
243 (defmacro !def-primitive-type-alias (name result)
244 ;; Just record the translation.
245 `(progn
246 (assert (not (assoc ',name *backend-primitive-type-aliases*)))
247 (push (cons ',name ,result) *backend-primitive-type-aliases*)
248 ',name))
251 ;;;; VOP definition structures
252 ;;;;
253 ;;;; DEFINE-VOP uses some fairly complex data structures at
254 ;;;; meta-compile time, both to hold the results of parsing the
255 ;;;; elaborate syntax and to retain the information so that it can be
256 ;;;; inherited by other VOPs.
258 ;;; An OPERAND-PARSE object contains stuff we need to know about an
259 ;;; operand or temporary at meta-compile time. Besides the obvious
260 ;;; stuff, we also store the names of per-operand temporaries here.
261 (def!struct (operand-parse
262 #-sb-xc-host (:pure t))
263 ;; name of the operand (which we bind to the TN)
264 (name nil :type symbol)
265 ;; the way this operand is used:
266 (kind (missing-arg)
267 :type (member :argument :result :temporary
268 :more-argument :more-result))
269 ;; If true, the name of an operand that this operand is targeted to.
270 ;; This is only meaningful in :ARGUMENT and :TEMPORARY operands.
271 (target nil :type (or symbol null))
272 ;; TEMP is a temporary that holds the TN-REF for this operand.
273 (temp (make-operand-parse-temp) :type symbol)
274 ;; the time that this operand is first live and the time at which it
275 ;; becomes dead again. These are TIME-SPECs, as returned by
276 ;; PARSE-TIME-SPEC.
277 born
278 dies
279 ;; a list of the names of the SCs that this operand is allowed into.
280 ;; If false, there is no restriction.
281 (scs nil :type list)
282 ;; Variable that is bound to the load TN allocated for this operand, or to
283 ;; NIL if no load-TN was allocated.
284 (load-tn (make-operand-parse-load-tn) :type symbol)
285 ;; an expression that tests whether to do automatic operand loading
286 (load t)
287 ;; In a wired or restricted temporary this is the SC the TN is to be
288 ;; packed in. Null otherwise.
289 (sc nil :type (or symbol null))
290 ;; If non-null, we are a temp wired to this offset in SC.
291 (offset nil :type (or unsigned-byte null)))
292 (!set-load-form-method operand-parse (:host :xc :target))
294 ;;; A VOP-PARSE object holds everything we need to know about a VOP at
295 ;;; meta-compile time.
296 (def!struct (vop-parse #-sb-xc-host (:pure t))
297 ;; the name of this VOP
298 (name nil :type symbol)
299 ;; If true, then the name of the VOP we inherit from.
300 (inherits nil :type (or symbol null))
301 ;; lists of OPERAND-PARSE structures describing the arguments,
302 ;; results and temporaries of the VOP
303 (args nil :type list)
304 (results nil :type list)
305 (temps nil :type list)
306 ;; OPERAND-PARSE structures containing information about more args
307 ;; and results. If null, then there there are no more operands of
308 ;; that kind
309 (more-args nil :type (or operand-parse null))
310 (more-results nil :type (or operand-parse null))
311 ;; a list of all the above together
312 (operands nil :type list)
313 ;; names of variables that should be declared IGNORE
314 (ignores () :type list)
315 ;; true if this is a :CONDITIONAL VOP. T if a branchful VOP,
316 ;; a list of condition descriptor otherwise. See $ARCH/pred.lisp
317 ;; for more information.
318 (conditional-p nil)
319 ;; argument and result primitive types. These are pulled out of the
320 ;; operands, since we often want to change them without respecifying
321 ;; the operands.
322 (arg-types :unspecified :type (or (member :unspecified) list))
323 (result-types :unspecified :type (or (member :unspecified) list))
324 ;; the guard expression specified, or NIL if none
325 (guard nil)
326 ;; the cost of and body code for the generator
327 (cost 0 :type unsigned-byte)
328 (body :unspecified :type (or (member :unspecified) list))
329 ;; info for VOP variants. The list of forms to be evaluated to get
330 ;; the variant args for this VOP, and the list of variables to be
331 ;; bound to the variant args.
332 (variant () :type list)
333 (variant-vars () :type list)
334 ;; variables bound to the VOP and Vop-Node when in the generator body
335 (vop-var '.vop. :type symbol)
336 (node-var nil :type (or symbol null))
337 ;; a list of the names of the codegen-info arguments to this VOP
338 (info-args () :type list)
339 ;; an efficiency note associated with this VOP
340 (note nil :type (or string null))
341 ;; a list of the names of the Effects and Affected attributes for
342 ;; this VOP
343 (effects '#1=(any) :type list)
344 (affected '#1# :type list)
345 ;; a list of the names of functions this VOP is a translation of and
346 ;; the policy that allows this translation to be done. :FAST is a
347 ;; safe default, since it isn't a safe policy.
348 (translate () :type list)
349 (ltn-policy :fast :type ltn-policy)
350 ;; stuff used by life analysis
351 (save-p nil :type (member t nil :compute-only :force-to-stack))
352 ;; info about how to emit MOVE-ARG VOPs for the &MORE operand in
353 ;; call/return VOPs
354 (move-args nil :type (member nil :local-call :full-call :known-return)))
355 (!set-load-form-method vop-parse (:host :xc :target))
356 (defprinter (vop-parse)
357 name
358 (inherits :test inherits)
359 args
360 results
361 temps
362 (more-args :test more-args)
363 (more-results :test more-results)
364 (conditional-p :test conditional-p)
365 ignores
366 arg-types
367 result-types
368 cost
369 body
370 (variant :test variant)
371 (variant-vars :test variant-vars)
372 (info-args :test info-args)
373 (note :test note)
374 effects
375 affected
376 translate
377 ltn-policy
378 (save-p :test save-p)
379 (move-args :test move-args))
381 (defprinter (operand-parse)
382 name
383 kind
384 (target :test target)
385 born
386 dies
387 (scs :test scs)
388 (load :test load)
389 (sc :test sc)
390 (offset :test offset))
392 ;;; Make NAME be the VOP used to move values in the specified FROM-SCs
393 ;;; to the representation of the TO-SCs of each SC pair in SCS.
395 ;;; If KIND is :MOVE-ARG, then the VOP takes an extra argument,
396 ;;; which is the frame pointer of the frame to move into.
398 ;;; We record the VOP and costs for all SCs that we can move between
399 ;;; (including implicit loading).
400 (defmacro define-move-vop (name kind &rest scs)
401 (when (or (oddp (length scs)) (null scs))
402 (error "malformed SCs spec: ~S" scs))
403 (let ((accessor (or (cdr (assoc kind *sc-vop-slots*))
404 (error "unknown kind ~S" kind))))
405 `(progn
406 ,@(when (eq kind :move)
407 `((eval-when (:compile-toplevel :load-toplevel :execute)
408 (do-sc-pairs (from-sc to-sc ',scs)
409 (compute-move-costs from-sc to-sc
410 ,(vop-parse-cost
411 (vop-parse-or-lose name)))))))
413 (let ((vop (template-or-lose ',name)))
414 (do-sc-pairs (from-sc to-sc ',scs)
415 (dolist (dest-sc (cons to-sc (sc-alternate-scs to-sc)))
416 (let ((vec (,accessor dest-sc)))
417 (let ((scn (sc-number from-sc)))
418 (setf (svref vec scn)
419 (adjoin-template vop (svref vec scn))))
420 (dolist (sc (append (sc-alternate-scs from-sc)
421 (sc-constant-scs from-sc)))
422 (let ((scn (sc-number sc)))
423 (setf (svref vec scn)
424 (adjoin-template vop (svref vec scn))))))))))))
426 ;;;; miscellaneous utilities
428 ;;; Find the operand or temporary with the specifed Name in the VOP
429 ;;; Parse. If there is no such operand, signal an error. Also error if
430 ;;; the operand kind isn't one of the specified Kinds. If Error-P is
431 ;;; NIL, just return NIL if there is no such operand.
432 (defun find-operand (name parse &optional
433 (kinds '(:argument :result :temporary))
434 (error-p t))
435 (declare (symbol name) (type vop-parse parse) (list kinds))
436 (let ((found (find name (vop-parse-operands parse)
437 :key #'operand-parse-name)))
438 (if found
439 (unless (member (operand-parse-kind found) kinds)
440 (error "Operand ~S isn't one of these kinds: ~S." name kinds))
441 (when error-p
442 (error "~S is not an operand to ~S." name (vop-parse-name parse))))
443 found))
445 ;;; Get the VOP-PARSE structure for NAME or die trying. For all
446 ;;; meta-compile time uses, the VOP-PARSE should be used instead of
447 ;;; the VOP-INFO.
448 (defun vop-parse-or-lose (name)
449 (the vop-parse
450 (or (gethash name *backend-parsed-vops*)
451 (error "~S is not the name of a defined VOP." name))))
453 ;;; Return a list of LET-forms to parse a TN-REF list into the temps
454 ;;; specified by the operand-parse structures. MORE-OPERAND is the
455 ;;; OPERAND-PARSE describing any more operand, or NIL if none. REFS is
456 ;;; an expression that evaluates into the first TN-REF.
457 (defun access-operands (operands more-operand refs)
458 (declare (list operands))
459 (collect ((res))
460 (let ((prev refs))
461 (dolist (op operands)
462 (let ((n-ref (operand-parse-temp op)))
463 (res `(,n-ref ,prev))
464 (setq prev `(tn-ref-across ,n-ref))))
466 (when more-operand
467 (res `(,(operand-parse-name more-operand) ,prev))))
468 (res)))
470 ;;; This is used with ACCESS-OPERANDS to prevent warnings for TN-REF
471 ;;; temps not used by some particular function. It returns the name of
472 ;;; the last operand, or NIL if OPERANDS is NIL.
473 (defun ignore-unreferenced-temps (operands)
474 (when operands
475 (operand-parse-temp (car (last operands)))))
477 ;;; Grab an arg out of a VOP spec, checking the type and syntax and stuff.
478 (defun vop-spec-arg (spec type &optional (n 1) (last t))
479 (let ((len (length spec)))
480 (when (<= len n)
481 (error "~:R argument missing: ~S" n spec))
482 (when (and last (> len (1+ n)))
483 (error "extra junk at end of ~S" spec))
484 (let ((thing (elt spec n)))
485 (unless (typep thing type)
486 (error "~:R argument is not a ~S: ~S" n type spec))
487 thing)))
489 ;;;; time specs
491 ;;; Return a time spec describing a time during the evaluation of a
492 ;;; VOP, used to delimit operand and temporary lifetimes. The
493 ;;; representation is a cons whose CAR is the number of the evaluation
494 ;;; phase and the CDR is the sub-phase. The sub-phase is 0 in the
495 ;;; :LOAD and :SAVE phases.
496 (defun parse-time-spec (spec)
497 (let ((dspec (if (atom spec) (list spec 0) spec)))
498 (unless (and (= (length dspec) 2)
499 (typep (second dspec) 'unsigned-byte))
500 (error "malformed time specifier: ~S" spec))
502 (cons (case (first dspec)
503 (:load 0)
504 (:argument 1)
505 (:eval 2)
506 (:result 3)
507 (:save 4)
509 (error "unknown phase in time specifier: ~S" spec)))
510 (second dspec))))
512 ;;; Return true if the time spec X is the same or later time than Y.
513 (defun time-spec-order (x y)
514 (or (> (car x) (car y))
515 (and (= (car x) (car y))
516 (>= (cdr x) (cdr y)))))
518 ;;;; generation of emit functions
520 (defun compute-temporaries-description (parse)
521 (let ((temps (vop-parse-temps parse))
522 (element-type '(unsigned-byte 16)))
523 (when temps
524 (let ((results (!make-specialized-array (length temps) element-type))
525 (index 0))
526 (dolist (temp temps)
527 (declare (type operand-parse temp))
528 (let ((sc (operand-parse-sc temp))
529 (offset (operand-parse-offset temp)))
530 (aver sc)
531 (setf (aref results index)
532 (if offset
533 (+ (ash offset (1+ sc-bits))
534 (ash (sc-number-or-lose sc) 1)
536 (ash (sc-number-or-lose sc) 1))))
537 (incf index))
538 results))))
540 (defun compute-ref-ordering (parse)
541 (let* ((num-args (+ (length (vop-parse-args parse))
542 (if (vop-parse-more-args parse) 1 0)))
543 (num-results (+ (length (vop-parse-results parse))
544 (if (vop-parse-more-results parse) 1 0)))
545 (index 0))
546 (collect ((refs) (targets))
547 (dolist (op (vop-parse-operands parse))
548 (when (operand-parse-target op)
549 (unless (member (operand-parse-kind op) '(:argument :temporary))
550 (error "cannot target a ~S operand: ~S" (operand-parse-kind op)
551 (operand-parse-name op)))
552 (let ((target (find-operand (operand-parse-target op) parse
553 '(:temporary :result))))
554 ;; KLUDGE: These formulas must be consistent with those in
555 ;; EMIT-VOP, and this is currently maintained by
556 ;; hand. -- WHN 2002-01-30, paraphrasing APD
557 (targets (+ (* index max-vop-tn-refs)
558 (ecase (operand-parse-kind target)
559 (:result
560 (+ (position-or-lose target
561 (vop-parse-results parse))
562 num-args))
563 (:temporary
564 (+ (* (position-or-lose target
565 (vop-parse-temps parse))
568 num-args
569 num-results)))))))
570 (let ((born (operand-parse-born op))
571 (dies (operand-parse-dies op)))
572 (ecase (operand-parse-kind op)
573 (:argument
574 (refs (cons (cons dies nil) index)))
575 (:more-argument
576 (refs (cons (cons dies nil) index)))
577 (:result
578 (refs (cons (cons born t) index)))
579 (:more-result
580 (refs (cons (cons born t) index)))
581 (:temporary
582 (refs (cons (cons dies nil) index))
583 (incf index)
584 (refs (cons (cons born t) index))))
585 (incf index)))
586 (let* ((sorted (stable-sort (refs)
587 (lambda (x y)
588 (let ((x-time (car x))
589 (y-time (car y)))
590 (if (time-spec-order x-time y-time)
591 (if (time-spec-order y-time x-time)
592 (and (not (cdr x)) (cdr y))
593 nil)
594 t)))
595 :key #'car))
596 ;; :REF-ORDERING element type
598 ;; KLUDGE: was (MOD #.MAX-VOP-TN-REFS), which is still right
599 (oe-type '(unsigned-byte 8))
600 ;; :TARGETS element-type
602 ;; KLUDGE: was (MOD #.(* MAX-VOP-TN-REFS 2)), which does
603 ;; not correspond to the definition in
604 ;; src/compiler/vop.lisp.
605 (te-type '(unsigned-byte 16))
606 (ordering (!make-specialized-array (length sorted) oe-type)))
607 (let ((index 0))
608 (dolist (ref sorted)
609 (setf (aref ordering index) (cdr ref))
610 (incf index)))
611 `(:num-args ,num-args
612 :num-results ,num-results
613 :ref-ordering ,ordering
614 ,@(when (targets)
615 `(:targets ,(!make-specialized-array
616 (length (targets)) te-type (targets)))))))))
618 (defun make-emit-function-and-friends (parse)
619 `(:temps ,(compute-temporaries-description parse)
620 ,@(compute-ref-ordering parse)))
622 ;;;; generator functions
624 ;;; Return an alist that translates from lists of SCs we can load OP
625 ;;; from to the move function used for loading those SCs. We quietly
626 ;;; ignore restrictions to :non-packed (constant) and :unbounded SCs,
627 ;;; since we don't load into those SCs.
628 (defun find-move-funs (op load-p)
629 (collect ((funs))
630 (dolist (sc-name (operand-parse-scs op))
631 (let* ((sc (sc-or-lose sc-name))
632 (scn (sc-number sc))
633 (load-scs (append (when load-p
634 (sc-constant-scs sc))
635 (sc-alternate-scs sc))))
636 (cond
637 (load-scs
638 (dolist (alt load-scs)
639 (unless (member (sc-name alt) (operand-parse-scs op) :test #'eq)
640 (let* ((altn (sc-number alt))
641 (name (if load-p
642 (svref (sc-move-funs sc) altn)
643 (svref (sc-move-funs alt) scn)))
644 (found (or (assoc alt (funs) :test #'member)
645 (rassoc name (funs)))))
646 (unless name
647 (error "no move function defined to ~:[save~;load~] SC ~S ~
648 ~:[to~;from~] from SC ~S"
649 load-p sc-name load-p (sc-name alt)))
650 (cond (found
651 (pushnew alt (car found)))
653 (funs (cons (list alt) name))))))))
654 ((member (sb-kind (sc-sb sc)) '(:non-packed :unbounded)))
656 (error "SC ~S has no alternate~:[~; or constant~] SCs, yet it is~@
657 mentioned in the restriction for operand ~S"
658 sc-name load-p (operand-parse-name op))))))
659 (funs)))
661 ;;; Return a form to load/save the specified operand when it has a
662 ;;; load TN. For any given SC that we can load from, there must be a
663 ;;; unique load function. If all SCs we can load from have the same
664 ;;; move function, then we just call that when there is a load TN. If
665 ;;; there are multiple possible move functions, then we dispatch off
666 ;;; of the operand TN's type to see which move function to use.
667 (defun call-move-fun (parse op load-p)
668 (let ((funs (find-move-funs op load-p))
669 (load-tn (operand-parse-load-tn op)))
670 (if funs
671 (let* ((tn `(tn-ref-tn ,(operand-parse-temp op)))
672 (n-vop (or (vop-parse-vop-var parse)
673 (setf (vop-parse-vop-var parse) '.vop.)))
674 (form (if (rest funs)
675 `(sc-case ,tn
676 ,@(mapcar (lambda (x)
677 `(,(mapcar #'sc-name (car x))
678 ,(if load-p
679 `(,(cdr x) ,n-vop ,tn
680 ,load-tn)
681 `(,(cdr x) ,n-vop ,load-tn
682 ,tn))))
683 funs))
684 (if load-p
685 `(,(cdr (first funs)) ,n-vop ,tn ,load-tn)
686 `(,(cdr (first funs)) ,n-vop ,load-tn ,tn)))))
687 (if (eq (operand-parse-load op) t)
688 `(when ,load-tn ,form)
689 `(when (eq ,load-tn ,(operand-parse-name op))
690 ,form)))
691 `(when ,load-tn
692 (error "load TN allocated, but no move function?~@
693 VM definition is inconsistent, recompile and try again.")))))
695 ;;; Return the TN that we should bind to the operand's var in the
696 ;;; generator body. In general, this involves evaluating the :LOAD-IF
697 ;;; test expression.
698 (defun decide-to-load (parse op)
699 (let ((load (operand-parse-load op))
700 (load-tn (operand-parse-load-tn op))
701 (temp (operand-parse-temp op)))
702 (if (eq load t)
703 `(or ,load-tn (tn-ref-tn ,temp))
704 (collect ((binds)
705 (ignores))
706 (dolist (x (vop-parse-operands parse))
707 (when (member (operand-parse-kind x) '(:argument :result))
708 (let ((name (operand-parse-name x)))
709 (binds `(,name (tn-ref-tn ,(operand-parse-temp x))))
710 (ignores name))))
711 `(if (and ,load-tn
712 (let ,(binds)
713 (declare (ignorable ,@(ignores)))
714 ,load))
715 ,load-tn
716 (tn-ref-tn ,temp))))))
718 ;;; Make a lambda that parses the VOP TN-REFS, does automatic operand
719 ;;; loading, and runs the appropriate code generator.
720 (defun make-generator-function (parse)
721 (declare (type vop-parse parse))
722 (let ((n-vop (vop-parse-vop-var parse))
723 (operands (vop-parse-operands parse))
724 (n-info (gensym)) (n-variant (gensym)))
725 (collect ((binds)
726 (loads)
727 (saves))
728 (dolist (op operands)
729 (ecase (operand-parse-kind op)
730 ((:argument :result)
731 (let ((temp (operand-parse-temp op))
732 (name (operand-parse-name op)))
733 (cond ((and (operand-parse-load op) (operand-parse-scs op))
734 (binds `(,(operand-parse-load-tn op)
735 (tn-ref-load-tn ,temp)))
736 (binds `(,name ,(decide-to-load parse op)))
737 (if (eq (operand-parse-kind op) :argument)
738 (loads (call-move-fun parse op t))
739 (saves (call-move-fun parse op nil))))
741 (binds `(,name (tn-ref-tn ,temp)))))))
742 (:temporary
743 (binds `(,(operand-parse-name op)
744 (tn-ref-tn ,(operand-parse-temp op)))))
745 ((:more-argument :more-result))))
747 `(named-lambda (vop ,(vop-parse-name parse)) (,n-vop)
748 (let* (,@(access-operands (vop-parse-args parse)
749 (vop-parse-more-args parse)
750 `(vop-args ,n-vop))
751 ,@(access-operands (vop-parse-results parse)
752 (vop-parse-more-results parse)
753 `(vop-results ,n-vop))
754 ,@(access-operands (vop-parse-temps parse) nil
755 `(vop-temps ,n-vop))
756 ,@(when (vop-parse-info-args parse)
757 `((,n-info (vop-codegen-info ,n-vop))
758 ,@(mapcar (lambda (x) `(,x (pop ,n-info)))
759 (vop-parse-info-args parse))))
760 ,@(when (vop-parse-variant-vars parse)
761 `((,n-variant (vop-info-variant (vop-info ,n-vop)))
762 ,@(mapcar (lambda (x) `(,x (pop ,n-variant)))
763 (vop-parse-variant-vars parse))))
764 ,@(when (vop-parse-node-var parse)
765 `((,(vop-parse-node-var parse) (vop-node ,n-vop))))
766 ,@(binds))
767 (declare (ignore ,@(vop-parse-ignores parse)))
768 ,@(loads)
769 (sb!assem:assemble (*code-segment* ,n-vop)
770 ,@(vop-parse-body parse))
771 ,@(saves))))))
773 (defvar *parse-vop-operand-count*)
774 (defun make-operand-parse-temp ()
775 (without-package-locks
776 (intern (format nil "OPERAND-PARSE-TEMP-~D" *parse-vop-operand-count*)
777 (symbol-package '*parse-vop-operand-count*))))
778 (defun make-operand-parse-load-tn ()
779 (without-package-locks
780 (intern (format nil "OPERAND-PARSE-LOAD-TN-~D" *parse-vop-operand-count*)
781 (symbol-package '*parse-vop-operand-count*))))
783 ;;; Given a list of operand specifications as given to DEFINE-VOP,
784 ;;; return a list of OPERAND-PARSE structures describing the fixed
785 ;;; operands, and a single OPERAND-PARSE describing any more operand.
786 ;;; If we are inheriting a VOP, we default attributes to the inherited
787 ;;; operand of the same name.
788 (defun parse-vop-operands (parse specs kind)
789 (declare (list specs)
790 (type (member :argument :result) kind))
791 (let ((num -1)
792 (more nil))
793 (collect ((operands))
794 (dolist (spec specs)
795 (unless (and (consp spec) (symbolp (first spec)) (oddp (length spec)))
796 (error "malformed operand specifier: ~S" spec))
797 (when more
798 (error "The MORE operand isn't the last operand: ~S" specs))
799 (incf *parse-vop-operand-count*)
800 (let* ((name (first spec))
801 (old (if (vop-parse-inherits parse)
802 (find-operand name
803 (vop-parse-or-lose
804 (vop-parse-inherits parse))
805 (list kind)
806 nil)
807 nil))
808 (res (if old
809 (make-operand-parse
810 :name name
811 :kind kind
812 :target (operand-parse-target old)
813 :born (operand-parse-born old)
814 :dies (operand-parse-dies old)
815 :scs (operand-parse-scs old)
816 :load-tn (operand-parse-load-tn old)
817 :load (operand-parse-load old))
818 (ecase kind
819 (:argument
820 (make-operand-parse
821 :name (first spec)
822 :kind :argument
823 :born (parse-time-spec :load)
824 :dies (parse-time-spec `(:argument ,(incf num)))))
825 (:result
826 (make-operand-parse
827 :name (first spec)
828 :kind :result
829 :born (parse-time-spec `(:result ,(incf num)))
830 :dies (parse-time-spec :save)))))))
831 (do ((key (rest spec) (cddr key)))
832 ((null key))
833 (let ((value (second key)))
834 (case (first key)
835 (:scs
836 (aver (typep value 'list))
837 (aver (= (length value) (length (remove-duplicates value))))
838 (setf (operand-parse-scs res) (copy-list value)))
839 (:load-tn
840 (aver (typep value 'symbol))
841 (setf (operand-parse-load-tn res) value))
842 (:load-if
843 (setf (operand-parse-load res) value))
844 (:more
845 (aver (typep value 'boolean))
846 (setf (operand-parse-kind res)
847 (if (eq kind :argument) :more-argument :more-result))
848 (setf (operand-parse-load res) nil)
849 (setq more res))
850 (:target
851 (aver (typep value 'symbol))
852 (setf (operand-parse-target res) value))
853 (:from
854 (unless (eq kind :result)
855 (error "can only specify :FROM in a result: ~S" spec))
856 (setf (operand-parse-born res) (parse-time-spec value)))
857 (:to
858 (unless (eq kind :argument)
859 (error "can only specify :TO in an argument: ~S" spec))
860 (setf (operand-parse-dies res) (parse-time-spec value)))
862 (error "unknown keyword in operand specifier: ~S" spec)))))
864 (cond ((not more)
865 (operands res))
866 ((operand-parse-target more)
867 (error "cannot specify :TARGET in a :MORE operand"))
868 ((operand-parse-load more)
869 (error "cannot specify :LOAD-IF in a :MORE operand")))))
870 (values (the list (operands)) more))))
872 ;;; Parse a temporary specification, putting the OPERAND-PARSE
873 ;;; structures in the PARSE structure.
874 (defun parse-temporary (spec parse)
875 (declare (list spec)
876 (type vop-parse parse))
877 (let ((len (length spec)))
878 (unless (>= len 2)
879 (error "malformed temporary spec: ~S" spec))
880 (unless (listp (second spec))
881 (error "malformed options list: ~S" (second spec)))
882 (unless (evenp (length (second spec)))
883 (error "odd number of arguments in keyword options: ~S" spec))
884 (unless (consp (cddr spec))
885 (warn "temporary spec allocates no temps:~% ~S" spec))
886 (dolist (name (cddr spec))
887 (unless (symbolp name)
888 (error "bad temporary name: ~S" name))
889 (incf *parse-vop-operand-count*)
890 (let ((res (make-operand-parse :name name
891 :kind :temporary
892 :born (parse-time-spec :load)
893 :dies (parse-time-spec :save))))
894 (do ((opt (second spec) (cddr opt)))
895 ((null opt))
896 (case (first opt)
897 (:target
898 (setf (operand-parse-target res)
899 (vop-spec-arg opt 'symbol 1 nil)))
900 (:sc
901 (setf (operand-parse-sc res)
902 (vop-spec-arg opt 'symbol 1 nil)))
903 (:offset
904 (let ((offset (eval (second opt))))
905 (aver (typep offset 'unsigned-byte))
906 (setf (operand-parse-offset res) offset)))
907 (:from
908 (setf (operand-parse-born res) (parse-time-spec (second opt))))
909 (:to
910 (setf (operand-parse-dies res) (parse-time-spec (second opt))))
911 ;; backward compatibility...
912 (:scs
913 (let ((scs (vop-spec-arg opt 'list 1 nil)))
914 (unless (= (length scs) 1)
915 (error "must specify exactly one SC for a temporary"))
916 (setf (operand-parse-sc res) (first scs))))
917 (:type)
919 (error "unknown temporary option: ~S" opt))))
921 (unless (and (time-spec-order (operand-parse-dies res)
922 (operand-parse-born res))
923 (not (time-spec-order (operand-parse-born res)
924 (operand-parse-dies res))))
925 (error "Temporary lifetime doesn't begin before it ends: ~S" spec))
927 (unless (operand-parse-sc res)
928 (error "must specify :SC for all temporaries: ~S" spec))
930 (setf (vop-parse-temps parse)
931 (cons res
932 (remove name (vop-parse-temps parse)
933 :key #'operand-parse-name))))))
934 (values))
936 (defun compute-parse-vop-operand-count (parse)
937 (declare (type vop-parse parse))
938 (labels ((compute-count-aux (parse)
939 (declare (type vop-parse parse))
940 (if (null (vop-parse-inherits parse))
941 (length (vop-parse-operands parse))
942 (+ (length (vop-parse-operands parse))
943 (compute-count-aux
944 (vop-parse-or-lose (vop-parse-inherits parse)))))))
945 (if (null (vop-parse-inherits parse))
947 (compute-count-aux (vop-parse-or-lose (vop-parse-inherits parse))))))
949 ;;; the top level parse function: clobber PARSE to represent the
950 ;;; specified options.
951 (defun parse-define-vop (parse specs)
952 (declare (type vop-parse parse) (list specs))
953 (let ((*parse-vop-operand-count* (compute-parse-vop-operand-count parse)))
954 (dolist (spec specs)
955 (unless (consp spec)
956 (error "malformed option specification: ~S" spec))
957 (case (first spec)
958 (:args
959 (multiple-value-bind (fixed more)
960 (parse-vop-operands parse (rest spec) :argument)
961 (setf (vop-parse-args parse) fixed)
962 (setf (vop-parse-more-args parse) more)))
963 (:results
964 (multiple-value-bind (fixed more)
965 (parse-vop-operands parse (rest spec) :result)
966 (setf (vop-parse-results parse) fixed)
967 (setf (vop-parse-more-results parse) more))
968 (setf (vop-parse-conditional-p parse) nil))
969 (:conditional
970 (setf (vop-parse-result-types parse) ())
971 (setf (vop-parse-results parse) ())
972 (setf (vop-parse-more-results parse) nil)
973 (setf (vop-parse-conditional-p parse) (or (rest spec) t)))
974 (:temporary
975 (parse-temporary spec parse))
976 (:generator
977 (setf (vop-parse-cost parse)
978 (vop-spec-arg spec 'unsigned-byte 1 nil))
979 (setf (vop-parse-body parse) (cddr spec)))
980 (:effects
981 (setf (vop-parse-effects parse) (rest spec)))
982 (:affected
983 (setf (vop-parse-affected parse) (rest spec)))
984 (:info
985 (setf (vop-parse-info-args parse) (rest spec)))
986 (:ignore
987 (setf (vop-parse-ignores parse) (rest spec)))
988 (:variant
989 (setf (vop-parse-variant parse) (rest spec)))
990 (:variant-vars
991 (let ((vars (rest spec)))
992 (setf (vop-parse-variant-vars parse) vars)
993 (setf (vop-parse-variant parse)
994 (make-list (length vars) :initial-element nil))))
995 (:variant-cost
996 (setf (vop-parse-cost parse) (vop-spec-arg spec 'unsigned-byte)))
997 (:vop-var
998 (setf (vop-parse-vop-var parse) (vop-spec-arg spec 'symbol)))
999 (:move-args
1000 (setf (vop-parse-move-args parse)
1001 (vop-spec-arg spec '(member nil :local-call :full-call
1002 :known-return))))
1003 (:node-var
1004 (setf (vop-parse-node-var parse) (vop-spec-arg spec 'symbol)))
1005 (:note
1006 (setf (vop-parse-note parse) (vop-spec-arg spec '(or string null))))
1007 (:arg-types
1008 (setf (vop-parse-arg-types parse)
1009 (parse-vop-operand-types (rest spec) t)))
1010 (:result-types
1011 (setf (vop-parse-result-types parse)
1012 (parse-vop-operand-types (rest spec) nil)))
1013 (:translate
1014 (setf (vop-parse-translate parse) (rest spec)))
1015 (:guard
1016 (setf (vop-parse-guard parse) (vop-spec-arg spec t)))
1017 ;; FIXME: :LTN-POLICY would be a better name for this. It
1018 ;; would probably be good to leave it unchanged for a while,
1019 ;; though, at least until the first port to some other
1020 ;; architecture, since the renaming would be a change to the
1021 ;; interface between
1022 (:policy
1023 (setf (vop-parse-ltn-policy parse)
1024 (vop-spec-arg spec 'ltn-policy)))
1025 (:save-p
1026 (setf (vop-parse-save-p parse)
1027 (vop-spec-arg spec
1028 '(member t nil :compute-only :force-to-stack))))
1030 (error "unknown option specifier: ~S" (first spec)))))
1031 (values)))
1033 ;;;; making costs and restrictions
1035 ;;; Given an operand, returns two values:
1036 ;;; 1. A SC-vector of the cost for the operand being in that SC,
1037 ;;; including both the costs for move functions and coercion VOPs.
1038 ;;; 2. A SC-vector holding the SC that we load into, for any SC
1039 ;;; that we can directly load from.
1041 ;;; In both vectors, unused entries are NIL. LOAD-P specifies the
1042 ;;; direction: if true, we are loading, if false we are saving.
1043 (defun compute-loading-costs (op load-p)
1044 (declare (type operand-parse op))
1045 (let ((scs (operand-parse-scs op))
1046 (costs (make-array sc-number-limit :initial-element nil))
1047 (load-scs (make-array sc-number-limit :initial-element nil)))
1048 (dolist (sc-name (reverse scs))
1049 (let* ((load-sc (sc-or-lose sc-name))
1050 (load-scn (sc-number load-sc)))
1051 (setf (svref costs load-scn) 0)
1052 (setf (svref load-scs load-scn) t)
1053 (dolist (op-sc (append (when load-p
1054 (sc-constant-scs load-sc))
1055 (sc-alternate-scs load-sc)))
1056 (let* ((op-scn (sc-number op-sc))
1057 (load (if load-p
1058 (aref (sc-load-costs load-sc) op-scn)
1059 (aref (sc-load-costs op-sc) load-scn))))
1060 (unless load
1061 (error "no move function defined to move ~:[from~;to~] SC ~
1062 ~S~%~:[to~;from~] alternate or constant SC ~S"
1063 load-p sc-name load-p (sc-name op-sc)))
1065 (let ((op-cost (svref costs op-scn)))
1066 (when (or (not op-cost) (< load op-cost))
1067 (setf (svref costs op-scn) load)))
1069 (let ((op-load (svref load-scs op-scn)))
1070 (unless (eq op-load t)
1071 (pushnew load-scn (svref load-scs op-scn))))))
1073 (dotimes (i sc-number-limit)
1074 (unless (svref costs i)
1075 (let ((op-sc (svref *backend-sc-numbers* i)))
1076 (when op-sc
1077 (let ((cost (if load-p
1078 (svref (sc-move-costs load-sc) i)
1079 (svref (sc-move-costs op-sc) load-scn))))
1080 (when cost
1081 (setf (svref costs i) cost)))))))))
1083 (values costs load-scs)))
1085 (defparameter *no-costs*
1086 (make-array sc-number-limit :initial-element 0))
1088 (defparameter *no-loads*
1089 (make-array sc-number-limit :initial-element t))
1091 ;;; Pick off the case of operands with no restrictions.
1092 (defun compute-loading-costs-if-any (op load-p)
1093 (declare (type operand-parse op))
1094 (if (operand-parse-scs op)
1095 (compute-loading-costs op load-p)
1096 (values *no-costs* *no-loads*)))
1098 (defun compute-costs-and-restrictions-list (ops load-p)
1099 (declare (list ops))
1100 (collect ((costs)
1101 (scs))
1102 (dolist (op ops)
1103 (multiple-value-bind (costs scs) (compute-loading-costs-if-any op load-p)
1104 (costs costs)
1105 (scs scs)))
1106 (values (costs) (scs))))
1108 (defun make-costs-and-restrictions (parse)
1109 (multiple-value-bind (arg-costs arg-scs)
1110 (compute-costs-and-restrictions-list (vop-parse-args parse) t)
1111 (multiple-value-bind (result-costs result-scs)
1112 (compute-costs-and-restrictions-list (vop-parse-results parse) nil)
1114 :cost ,(vop-parse-cost parse)
1116 :arg-costs ',arg-costs
1117 :arg-load-scs ',arg-scs
1118 :result-costs ',result-costs
1119 :result-load-scs ',result-scs
1121 :more-arg-costs
1122 ',(if (vop-parse-more-args parse)
1123 (compute-loading-costs-if-any (vop-parse-more-args parse) t)
1124 nil)
1126 :more-result-costs
1127 ',(if (vop-parse-more-results parse)
1128 (compute-loading-costs-if-any (vop-parse-more-results parse) nil)
1129 nil)))))
1131 ;;;; operand checking and stuff
1133 ;;; Given a list of arg/result restrictions, check for valid syntax
1134 ;;; and convert to canonical form.
1135 (defun parse-vop-operand-types (specs args-p)
1136 (declare (list specs))
1137 (labels ((primtype-alias-p (spec)
1138 (cdr (assq spec *backend-primitive-type-aliases*)))
1139 (parse-operand-type (spec)
1140 (cond ((eq spec '*) spec)
1141 ((symbolp spec)
1142 (let ((alias (primtype-alias-p spec)))
1143 (if alias
1144 (parse-operand-type alias)
1145 `(:or ,spec))))
1146 ((atom spec)
1147 (error "bad thing to be a operand type: ~S" spec))
1149 (case (first spec)
1150 (:or
1151 (collect ((results))
1152 (dolist (item (cdr spec))
1153 (unless (symbolp item)
1154 (error "bad PRIMITIVE-TYPE name in ~S: ~S"
1155 spec item))
1156 (let ((alias (primtype-alias-p item)))
1157 (if alias
1158 (let ((alias (parse-operand-type alias)))
1159 (unless (eq (car alias) :or)
1160 (error "can't include primitive-type ~
1161 alias ~S in an :OR restriction: ~S"
1162 item spec))
1163 (dolist (x (cdr alias))
1164 (results x)))
1165 (results item))))
1166 `(:or ,@(remove-duplicates (results) :test #'eq))))
1167 (:constant
1168 (unless args-p
1169 (error "can't :CONSTANT for a result"))
1170 (unless (= (length spec) 2)
1171 (error "bad :CONSTANT argument type spec: ~S" spec))
1172 spec)
1174 (error "bad thing to be a operand type: ~S" spec)))))))
1175 (mapcar #'parse-operand-type specs)))
1177 ;;; Check the consistency of OP's SC restrictions with the specified
1178 ;;; primitive-type restriction. :CONSTANT operands have already been
1179 ;;; filtered out, so only :OR and * restrictions are left.
1181 ;;; We check that every representation allowed by the type can be
1182 ;;; directly loaded into some SC in the restriction, and that the type
1183 ;;; allows every SC in the restriction. With *, we require that T
1184 ;;; satisfy the first test, and omit the second.
1185 (defun check-operand-type-scs (parse op type load-p)
1186 (declare (type vop-parse parse) (type operand-parse op))
1187 (let ((ptypes (if (eq type '*) (list t) (rest type)))
1188 (scs (operand-parse-scs op)))
1189 (when scs
1190 (multiple-value-bind (costs load-scs) (compute-loading-costs op load-p)
1191 (declare (ignore costs))
1192 (dolist (ptype ptypes)
1193 (unless (dolist (rep (primitive-type-scs
1194 (primitive-type-or-lose ptype))
1195 nil)
1196 (when (svref load-scs rep) (return t)))
1197 (error "In the ~A ~:[result~;argument~] to VOP ~S,~@
1198 none of the SCs allowed by the operand type ~S can ~
1199 directly be loaded~@
1200 into any of the restriction's SCs:~% ~S~:[~;~@
1201 [* type operand must allow T's SCs.]~]"
1202 (operand-parse-name op) load-p (vop-parse-name parse)
1203 ptype
1204 scs (eq type '*)))))
1206 (dolist (sc scs)
1207 (unless (or (eq type '*)
1208 (dolist (ptype ptypes nil)
1209 (when (sc-allowed-by-primitive-type
1210 (sc-or-lose sc)
1211 (primitive-type-or-lose ptype))
1212 (return t))))
1213 (warn "~:[Result~;Argument~] ~A to VOP ~S~@
1214 has SC restriction ~S which is ~
1215 not allowed by the operand type:~% ~S"
1216 load-p (operand-parse-name op) (vop-parse-name parse)
1217 sc type)))))
1219 (values))
1221 ;;; If the operand types are specified, then check the number specified
1222 ;;; against the number of defined operands.
1223 (defun check-operand-types (parse ops more-op types load-p)
1224 (declare (type vop-parse parse) (list ops)
1225 (type (or list (member :unspecified)) types)
1226 (type (or operand-parse null) more-op))
1227 (unless (eq types :unspecified)
1228 (let ((num (+ (length ops) (if more-op 1 0))))
1229 (unless (= (count-if-not (lambda (x)
1230 (and (consp x)
1231 (eq (car x) :constant)))
1232 types)
1233 num)
1234 (error "expected ~W ~:[result~;argument~] type~P: ~S"
1235 num load-p types num)))
1237 (when more-op
1238 (let ((mtype (car (last types))))
1239 (when (and (consp mtype) (eq (first mtype) :constant))
1240 (error "can't use :CONSTANT on VOP more args")))))
1242 (when (vop-parse-translate parse)
1243 (let ((types (specify-operand-types types ops more-op)))
1244 (mapc (lambda (x y)
1245 (check-operand-type-scs parse x y load-p))
1246 (if more-op (butlast ops) ops)
1247 (remove-if (lambda (x)
1248 (and (consp x)
1249 (eq (car x) ':constant)))
1250 (if more-op (butlast types) types)))))
1252 (values))
1254 ;;; Compute stuff that can only be computed after we are done parsing
1255 ;;; everying. We set the VOP-PARSE-OPERANDS, and do various error checks.
1256 (defun grovel-vop-operands (parse)
1257 (declare (type vop-parse parse))
1259 (setf (vop-parse-operands parse)
1260 (append (vop-parse-args parse)
1261 (if (vop-parse-more-args parse)
1262 (list (vop-parse-more-args parse)))
1263 (vop-parse-results parse)
1264 (if (vop-parse-more-results parse)
1265 (list (vop-parse-more-results parse)))
1266 (vop-parse-temps parse)))
1268 (check-operand-types parse
1269 (vop-parse-args parse)
1270 (vop-parse-more-args parse)
1271 (vop-parse-arg-types parse)
1274 (check-operand-types parse
1275 (vop-parse-results parse)
1276 (vop-parse-more-results parse)
1277 (vop-parse-result-types parse)
1278 nil)
1280 (values))
1282 ;;;; function translation stuff
1284 ;;; Return forms to establish this VOP as a IR2 translation template
1285 ;;; for the :TRANSLATE functions specified in the VOP-PARSE. We also
1286 ;;; set the PREDICATE attribute for each translated function when the
1287 ;;; VOP is conditional, causing IR1 conversion to ensure that a call
1288 ;;; to the translated is always used in a predicate position.
1289 (defun set-up-fun-translation (parse n-template)
1290 (declare (type vop-parse parse))
1291 (mapcar (lambda (name)
1292 `(let ((info (fun-info-or-lose ',name)))
1293 (setf (fun-info-templates info)
1294 (adjoin-template ,n-template (fun-info-templates info)))
1295 ,@(when (vop-parse-conditional-p parse)
1296 '((setf (fun-info-attributes info)
1297 (attributes-union
1298 (ir1-attributes predicate)
1299 (fun-info-attributes info)))))))
1300 (vop-parse-translate parse)))
1302 ;;; Return a form that can be evaluated to get the TEMPLATE operand type
1303 ;;; restriction from the given specification.
1304 (defun make-operand-type (type)
1305 (cond ((eq type '*) ''*)
1306 ((symbolp type)
1307 ``(:or ,(primitive-type-or-lose ',type)))
1309 (ecase (car type)
1310 (:or
1311 ``(:or ,,@(mapcar (lambda (type)
1312 `(primitive-type-or-lose ',type))
1313 (rest type))))
1314 (:constant
1315 ``(:constant ,(named-lambda (vop-arg-typep) (x)
1316 ;; Can't handle SATISFIES during XC
1317 ,(if (and (consp (second type))
1318 (eq (caadr type) 'satisfies))
1319 `(,(cadadr type) x)
1320 `(sb!xc:typep x ',(second type))))
1321 ,',(second type)))))))
1323 (defun specify-operand-types (types ops more-ops)
1324 (if (eq types :unspecified)
1325 (make-list (+ (length ops) (if more-ops 1 0)) :initial-element '*)
1326 types))
1328 ;;; Return a list of forms to use as &KEY args to MAKE-VOP-INFO for
1329 ;;; setting up the template argument and result types. Here we make an
1330 ;;; initial dummy TEMPLATE-TYPE, since it is awkward to compute the
1331 ;;; type until the template has been made.
1332 (defun make-vop-info-types (parse)
1333 (let* ((more-args (vop-parse-more-args parse))
1334 (all-args (specify-operand-types (vop-parse-arg-types parse)
1335 (vop-parse-args parse)
1336 more-args))
1337 (args (if more-args (butlast all-args) all-args))
1338 (more-arg (when more-args (car (last all-args))))
1339 (more-results (vop-parse-more-results parse))
1340 (all-results (specify-operand-types (vop-parse-result-types parse)
1341 (vop-parse-results parse)
1342 more-results))
1343 (results (if more-results (butlast all-results) all-results))
1344 (more-result (when more-results (car (last all-results))))
1345 (conditional (vop-parse-conditional-p parse)))
1347 `(:type (specifier-type '(function () nil))
1348 :arg-types (list ,@(mapcar #'make-operand-type args))
1349 :more-args-type ,(when more-args (make-operand-type more-arg))
1350 :result-types ,(cond ((eq conditional t)
1351 :conditional)
1352 (conditional
1353 `'(:conditional . ,conditional))
1355 `(list ,@(mapcar #'make-operand-type results))))
1356 :more-results-type ,(when more-results
1357 (make-operand-type more-result)))))
1359 ;;;; setting up VOP-INFO
1361 (eval-when (:compile-toplevel :load-toplevel :execute)
1362 (defparameter *slot-inherit-alist*
1363 '((:generator-function . vop-info-generator-function))))
1365 ;;; This is something to help with inheriting VOP-INFO slots. We
1366 ;;; return a keyword/value pair that can be passed to the constructor.
1367 ;;; SLOT is the keyword name of the slot, Parse is a form that
1368 ;;; evaluates to the VOP-PARSE structure for the VOP inherited. If
1369 ;;; PARSE is NIL, then we do nothing. If the TEST form evaluates to
1370 ;;; true, then we return a form that selects the named slot from the
1371 ;;; VOP-INFO structure corresponding to PARSE. Otherwise, we return
1372 ;;; the FORM so that the slot is recomputed.
1373 (defmacro inherit-vop-info (slot parse test form)
1374 `(if (and ,parse ,test)
1375 (list ,slot `(,',(or (cdr (assoc slot *slot-inherit-alist*))
1376 (error "unknown slot ~S" slot))
1377 (template-or-lose ',(vop-parse-name ,parse))))
1378 (list ,slot ,form)))
1380 ;;; Return a form that creates a VOP-INFO structure which describes VOP.
1381 (defun set-up-vop-info (iparse parse)
1382 (declare (type vop-parse parse) (type (or vop-parse null) iparse))
1383 (let ((same-operands
1384 (and iparse
1385 (equal (vop-parse-operands parse)
1386 (vop-parse-operands iparse))
1387 (equal (vop-parse-info-args iparse)
1388 (vop-parse-info-args parse))))
1389 (variant (vop-parse-variant parse)))
1391 (let ((nvars (length (vop-parse-variant-vars parse))))
1392 (unless (= (length variant) nvars)
1393 (error "expected ~W variant values: ~S" nvars variant)))
1395 `(make-vop-info
1396 :name ',(vop-parse-name parse)
1397 ,@(make-vop-info-types parse)
1398 :guard ,(when (vop-parse-guard parse)
1399 `(lambda () ,(vop-parse-guard parse)))
1400 :note ',(vop-parse-note parse)
1401 :info-arg-count ,(length (vop-parse-info-args parse))
1402 :ltn-policy ',(vop-parse-ltn-policy parse)
1403 :save-p ',(vop-parse-save-p parse)
1404 :move-args ',(vop-parse-move-args parse)
1405 :effects (vop-attributes ,@(vop-parse-effects parse))
1406 :affected (vop-attributes ,@(vop-parse-affected parse))
1407 ,@(make-costs-and-restrictions parse)
1408 ,@(make-emit-function-and-friends parse)
1409 ,@(inherit-vop-info :generator-function iparse
1410 (and same-operands
1411 (equal (vop-parse-body parse) (vop-parse-body iparse)))
1412 (unless (eq (vop-parse-body parse) :unspecified)
1413 (make-generator-function parse)))
1414 :variant (list ,@variant))))
1416 ;;; Define the symbol NAME to be a Virtual OPeration in the compiler.
1417 ;;; If specified, INHERITS is the name of a VOP that we default
1418 ;;; unspecified information from. Each SPEC is a list beginning with a
1419 ;;; keyword indicating the interpretation of the other forms in the
1420 ;;; SPEC:
1422 ;;; :ARGS {(Name {Key Value}*)}*
1423 ;;; :RESULTS {(Name {Key Value}*)}*
1424 ;;; The Args and Results are specifications of the operand TNs passed
1425 ;;; to the VOP. If there is an inherited VOP, any unspecified options
1426 ;;; are defaulted from the inherited argument (or result) of the same
1427 ;;; name. The following operand options are defined:
1429 ;;; :SCs (SC*)
1430 ;;; :SCs specifies good SCs for this operand. Other SCs will
1431 ;;; be penalized according to move costs. A load TN will be
1432 ;;; allocated if necessary, guaranteeing that the operand is
1433 ;;; always one of the specified SCs.
1435 ;;; :LOAD-TN Load-Name
1436 ;;; Load-Name is bound to the load TN allocated for this
1437 ;;; operand, or to NIL if no load TN was allocated.
1439 ;;; :LOAD-IF EXPRESSION
1440 ;;; Controls whether automatic operand loading is done.
1441 ;;; EXPRESSION is evaluated with the fixed operand TNs bound.
1442 ;;; If EXPRESSION is true, then loading is done and the variable
1443 ;;; is bound to the load TN in the generator body. Otherwise,
1444 ;;; loading is not done, and the variable is bound to the actual
1445 ;;; operand.
1447 ;;; :MORE T-or-NIL
1448 ;;; If specified, NAME is bound to the TN-REF for the first
1449 ;;; argument or result following the fixed arguments or results.
1450 ;;; A :MORE operand must appear last, and cannot be targeted or
1451 ;;; restricted.
1453 ;;; :TARGET Operand
1454 ;;; This operand is targeted to the named operand, indicating a
1455 ;;; desire to pack in the same location. Not legal for results.
1457 ;;; :FROM Time-Spec
1458 ;;; :TO Time-Spec
1459 ;;; Specify the beginning or end of the operand's lifetime.
1460 ;;; :FROM can only be used with results, and :TO only with
1461 ;;; arguments. The default for the N'th argument/result is
1462 ;;; (:ARGUMENT N)/(:RESULT N). These options are necessary
1463 ;;; primarily when operands are read or written out of order.
1465 ;;; :CONDITIONAL [Condition-descriptor+]
1466 ;;; This is used in place of :RESULTS with conditional branch VOPs.
1467 ;;; There are no result values: the result is a transfer of control.
1468 ;;; The target label is passed as the first :INFO arg. The second
1469 ;;; :INFO arg is true if the sense of the test should be negated.
1470 ;;; A side effect is to set the PREDICATE attribute for functions
1471 ;;; in the :TRANSLATE option.
1473 ;;; If some condition descriptors are provided, this is a flag-setting
1474 ;;; VOP. Descriptors are interpreted in an architecture-dependent
1475 ;;; manner. See the BRANCH-IF VOP in $ARCH/pred.lisp.
1477 ;;; :TEMPORARY ({Key Value}*) Name*
1478 ;;; Allocate a temporary TN for each Name, binding that variable to
1479 ;;; the TN within the body of the generators. In addition to :TARGET
1480 ;;; (which is is the same as for operands), the following options are
1481 ;;; defined:
1483 ;;; :SC SC-Name
1484 ;;; :OFFSET SB-Offset
1485 ;;; Force the temporary to be allocated in the specified SC
1486 ;;; with the specified offset. Offset is evaluated at
1487 ;;; macroexpand time. If Offset is omitted, the register
1488 ;;; allocator chooses a free location in SC. If both SC and
1489 ;;; Offset are omitted, then the temporary is packed according
1490 ;;; to its primitive type.
1492 ;;; :FROM Time-Spec
1493 ;;; :TO Time-Spec
1494 ;;; Similar to the argument/result option, this specifies the
1495 ;;; start and end of the temporaries' lives. The defaults are
1496 ;;; :LOAD and :SAVE, i.e. the duration of the VOP. The other
1497 ;;; intervening phases are :ARGUMENT, :EVAL and :RESULT.
1498 ;;; Non-zero sub-phases can be specified by a list, e.g. by
1499 ;;; default the second argument's life ends at (:ARGUMENT 1).
1501 ;;; :GENERATOR Cost Form*
1502 ;;; Specifies the translation into assembly code. Cost is the
1503 ;;; estimated cost of the code emitted by this generator. The body
1504 ;;; is arbitrary Lisp code that emits the assembly language
1505 ;;; translation of the VOP. An ASSEMBLE form is wrapped around
1506 ;;; the body, so code may be emitted by using the local INST macro.
1507 ;;; During the evaluation of the body, the names of the operands
1508 ;;; and temporaries are bound to the actual TNs.
1510 ;;; :EFFECTS Effect*
1511 ;;; :AFFECTED Effect*
1512 ;;; Specifies the side effects that this VOP has and the side
1513 ;;; effects that effect its execution. If unspecified, these
1514 ;;; default to the worst case.
1516 ;;; :INFO Name*
1517 ;;; Define some magic arguments that are passed directly to the code
1518 ;;; generator. The corresponding trailing arguments to VOP or
1519 ;;; %PRIMITIVE are stored in the VOP structure. Within the body
1520 ;;; of the generators, the named variables are bound to these
1521 ;;; values. Except in the case of :CONDITIONAL VOPs, :INFO arguments
1522 ;;; cannot be specified for VOPS that are the direct translation
1523 ;;; for a function (specified by :TRANSLATE).
1525 ;;; :IGNORE Name*
1526 ;;; Causes the named variables to be declared IGNORE in the
1527 ;;; generator body.
1529 ;;; :VARIANT Thing*
1530 ;;; :VARIANT-VARS Name*
1531 ;;; These options provide a way to parameterize families of VOPs
1532 ;;; that differ only trivially. :VARIANT makes the specified
1533 ;;; evaluated Things be the "variant" associated with this VOP.
1534 ;;; :VARIANT-VARS causes the named variables to be bound to the
1535 ;;; corresponding Things within the body of the generator.
1537 ;;; :VARIANT-COST Cost
1538 ;;; Specifies the cost of this VOP, overriding the cost of any
1539 ;;; inherited generator.
1541 ;;; :NOTE {String | NIL}
1542 ;;; A short noun-like phrase describing what this VOP "does", i.e.
1543 ;;; the implementation strategy. If supplied, efficiency notes will
1544 ;;; be generated when type uncertainty prevents :TRANSLATE from
1545 ;;; working. NIL inhibits any efficiency note.
1547 ;;; :ARG-TYPES {* | PType | (:OR PType*) | (:CONSTANT Type)}*
1548 ;;; :RESULT-TYPES {* | PType | (:OR PType*)}*
1549 ;;; Specify the template type restrictions used for automatic
1550 ;;; translation. If there is a :MORE operand, the last type is the
1551 ;;; more type. :CONSTANT specifies that the argument must be a
1552 ;;; compile-time constant of the specified Lisp type. The constant
1553 ;;; values of :CONSTANT arguments are passed as additional :INFO
1554 ;;; arguments rather than as :ARGS.
1556 ;;; :TRANSLATE Name*
1557 ;;; This option causes the VOP template to be entered as an IR2
1558 ;;; translation for the named functions.
1560 ;;; :POLICY {:SMALL | :FAST | :SAFE | :FAST-SAFE}
1561 ;;; Specifies the policy under which this VOP is the best translation.
1563 ;;; :GUARD Form
1564 ;;; Specifies a Form that is evaluated in the global environment.
1565 ;;; If form returns NIL, then emission of this VOP is prohibited
1566 ;;; even when all other restrictions are met.
1568 ;;; :VOP-VAR Name
1569 ;;; :NODE-VAR Name
1570 ;;; In the generator, bind the specified variable to the VOP or
1571 ;;; the Node that generated this VOP.
1573 ;;; :SAVE-P {NIL | T | :COMPUTE-ONLY | :FORCE-TO-STACK}
1574 ;;; Indicates how a VOP wants live registers saved.
1576 ;;; :MOVE-ARGS {NIL | :FULL-CALL | :LOCAL-CALL | :KNOWN-RETURN}
1577 ;;; Indicates if and how the more args should be moved into a
1578 ;;; different frame.
1579 (defmacro define-vop ((name &optional inherits) &body specs)
1580 (declare (type symbol name))
1581 ;; Parse the syntax into a VOP-PARSE structure, and then expand into
1582 ;; code that creates the appropriate VOP-INFO structure at load time.
1583 ;; We implement inheritance by copying the VOP-PARSE structure for
1584 ;; the inherited structure.
1585 (let* ((inherited-parse (when inherits
1586 (vop-parse-or-lose inherits)))
1587 (parse (if inherits
1588 (copy-vop-parse inherited-parse)
1589 (make-vop-parse)))
1590 (n-res (gensym)))
1591 (setf (vop-parse-name parse) name)
1592 (setf (vop-parse-inherits parse) inherits)
1594 (parse-define-vop parse specs)
1595 (grovel-vop-operands parse)
1597 `(progn
1598 (eval-when (:compile-toplevel :load-toplevel :execute)
1599 (setf (gethash ',name *backend-parsed-vops*)
1600 ',parse))
1602 (let ((,n-res ,(set-up-vop-info inherited-parse parse)))
1603 (store-vop-info ,n-res)
1604 ,@(set-up-fun-translation parse n-res))
1605 (let ((source-location (source-location)))
1606 (when source-location
1607 (setf (info :source-location :vop ',name) source-location)))
1608 ',name)))
1610 (defun store-vop-info (vop-info)
1611 ;; This is an inefficent way to perform coalescing, but it doesn't matter.
1612 (let* ((my-type-spec (template-type-specifier vop-info))
1613 (my-type (specifier-type my-type-spec)))
1614 (unless (block found
1615 (maphash (lambda (name other)
1616 (declare (ignore name))
1617 ;; we get better coaelesecing by TYPE= rather than
1618 ;; EQUALP on (template-type-specifier vop-info)
1619 ;; because some types have multiple spellings.
1620 (when (type= (vop-info-type other) my-type)
1621 (setf (vop-info-type vop-info) (vop-info-type other))
1622 (return-from found t)))
1623 *backend-template-names*))
1624 (setf (vop-info-type vop-info) (specifier-type my-type-spec))))
1625 (flet ((find-equalp (accessor)
1626 ;; Read the slot from VOP-INFO and try to find any other vop-info
1627 ;; that has an EQUALP value in that slot, returning that value.
1628 ;; Failing that, try again at a finer grain.
1629 (let ((my-val (funcall accessor vop-info))) ; list of vectors
1630 (maphash (lambda (name other)
1631 (declare (ignore name))
1632 (let ((other-val (funcall accessor other)))
1633 (when (equalp other-val my-val)
1634 (return-from find-equalp other-val))))
1635 *backend-template-names*)
1636 (unless (and (listp my-val) (vectorp (car my-val)))
1637 (return-from find-equalp my-val))
1638 (mapl (lambda (cell)
1639 (let ((my-vector (car cell)))
1640 (block found
1641 (maphash (lambda (name other)
1642 (declare (ignore name))
1643 (dolist (other-vector
1644 (funcall accessor other))
1645 (when (equalp other-vector my-vector)
1646 (rplaca cell other-vector)
1647 (return-from found))))
1648 *backend-template-names*))))
1649 (copy-list my-val))))) ; was a quoted constant, don't mutate
1650 (macrolet ((try-coalescing (accessor)
1651 `(setf (,accessor vop-info) (find-equalp #',accessor))))
1652 (try-coalescing vop-info-arg-types)
1653 (try-coalescing vop-info-arg-costs)
1654 (try-coalescing vop-info-arg-load-scs)
1655 (try-coalescing vop-info-result-types)
1656 (try-coalescing vop-info-result-costs)
1657 (try-coalescing vop-info-result-load-scs)
1658 (try-coalescing vop-info-more-arg-costs)
1659 (try-coalescing vop-info-more-result-costs)
1660 (try-coalescing vop-info-temps)
1661 (try-coalescing vop-info-ref-ordering)
1662 (try-coalescing vop-info-targets)))
1663 (setf (gethash (vop-info-name vop-info) *backend-template-names*)
1664 vop-info))
1666 ;;;; emission macros
1668 ;;; Return code to make a list of VOP arguments or results, linked by
1669 ;;; TN-REF-ACROSS. The first value is code, the second value is LET*
1670 ;;; forms, and the third value is a variable that evaluates to the
1671 ;;; head of the list, or NIL if there are no operands. Fixed is a list
1672 ;;; of forms that evaluate to TNs for the fixed operands. TN-REFS will
1673 ;;; be made for these operands according using the specified value of
1674 ;;; WRITE-P. More is an expression that evaluates to a list of TN-REFS
1675 ;;; that will be made the tail of the list. If it is constant NIL,
1676 ;;; then we don't bother to set the tail.
1677 (defun make-operand-list (fixed more write-p)
1678 (collect ((forms)
1679 (binds))
1680 (let ((n-head nil)
1681 (n-prev nil))
1682 (dolist (op fixed)
1683 (let ((n-ref (gensym)))
1684 (binds `(,n-ref (reference-tn ,op ,write-p)))
1685 (if n-prev
1686 (forms `(setf (tn-ref-across ,n-prev) ,n-ref))
1687 (setq n-head n-ref))
1688 (setq n-prev n-ref)))
1690 (when more
1691 (let ((n-more (gensym)))
1692 (binds `(,n-more ,more))
1693 (if n-prev
1694 (forms `(setf (tn-ref-across ,n-prev) ,n-more))
1695 (setq n-head n-more))))
1697 (values (forms) (binds) n-head))))
1699 ;;; Emit-Template Node Block Template Args Results [Info]
1701 ;;; Call the emit function for TEMPLATE, linking the result in at the
1702 ;;; end of BLOCK.
1703 (defmacro emit-template (node block template args results &optional info)
1704 `(emit-and-insert-vop ,node ,block ,template ,args ,results nil
1705 ,@(when info `(,info))))
1707 ;;; VOP Name Node Block Arg* Info* Result*
1709 ;;; Emit the VOP (or other template) NAME at the end of the IR2-BLOCK
1710 ;;; BLOCK, using NODE for the source context. The interpretation of
1711 ;;; the remaining arguments depends on the number of operands of
1712 ;;; various kinds that are declared in the template definition. VOP
1713 ;;; cannot be used for templates that have more-args or more-results,
1714 ;;; since the number of arguments and results is indeterminate for
1715 ;;; these templates. Use VOP* instead.
1717 ;;; ARGS and RESULTS are the TNs that are to be referenced by the
1718 ;;; template as arguments and results. If the template has
1719 ;;; codegen-info arguments, then the appropriate number of INFO forms
1720 ;;; following the arguments are used for codegen info.
1721 (defmacro vop (name node block &rest operands)
1722 (let* ((parse (vop-parse-or-lose name))
1723 (arg-count (length (vop-parse-args parse)))
1724 (result-count (length (vop-parse-results parse)))
1725 (info-count (length (vop-parse-info-args parse)))
1726 (noperands (+ arg-count result-count info-count))
1727 (n-node (gensym))
1728 (n-block (gensym))
1729 (n-template (gensym)))
1731 (when (or (vop-parse-more-args parse) (vop-parse-more-results parse))
1732 (error "cannot use VOP with variable operand count templates"))
1733 (unless (= noperands (length operands))
1734 (error "called with ~W operands, but was expecting ~W"
1735 (length operands) noperands))
1737 (multiple-value-bind (acode abinds n-args)
1738 (make-operand-list (subseq operands 0 arg-count) nil nil)
1739 (multiple-value-bind (rcode rbinds n-results)
1740 (make-operand-list (subseq operands (+ arg-count info-count)) nil t)
1742 (collect ((ibinds)
1743 (ivars))
1744 (dolist (info (subseq operands arg-count (+ arg-count info-count)))
1745 (let ((temp (gensym)))
1746 (ibinds `(,temp ,info))
1747 (ivars temp)))
1749 `(let* ((,n-node ,node)
1750 (,n-block ,block)
1751 (,n-template (template-or-lose ',name))
1752 ,@abinds
1753 ,@(ibinds)
1754 ,@rbinds)
1755 ,@acode
1756 ,@rcode
1757 (emit-template ,n-node ,n-block ,n-template ,n-args
1758 ,n-results
1759 ,@(when (ivars)
1760 `((list ,@(ivars)))))
1761 (values)))))))
1763 ;;; VOP* Name Node Block (Arg* More-Args) (Result* More-Results) Info*
1765 ;;; This is like VOP, but allows for emission of templates with
1766 ;;; arbitrary numbers of arguments, and for emission of templates
1767 ;;; using already-created TN-REF lists.
1769 ;;; The ARGS and RESULTS are TNs to be referenced as the first
1770 ;;; arguments and results to the template. More-Args and More-Results
1771 ;;; are heads of TN-REF lists that are added onto the end of the
1772 ;;; TN-REFS for the explicitly supplied operand TNs. The TN-REFS for
1773 ;;; the more operands must have the TN and WRITE-P slots correctly
1774 ;;; initialized.
1776 ;;; As with VOP, the INFO forms are evaluated and passed as codegen
1777 ;;; info arguments.
1778 (defmacro vop* (name node block args results &rest info)
1779 (declare (type cons args results))
1780 (let* ((parse (vop-parse-or-lose name))
1781 (arg-count (length (vop-parse-args parse)))
1782 (result-count (length (vop-parse-results parse)))
1783 (info-count (length (vop-parse-info-args parse)))
1784 (fixed-args (butlast args))
1785 (fixed-results (butlast results))
1786 (n-node (gensym))
1787 (n-block (gensym))
1788 (n-template (gensym)))
1790 (unless (or (vop-parse-more-args parse)
1791 (<= (length fixed-args) arg-count))
1792 (error "too many fixed arguments"))
1793 (unless (or (vop-parse-more-results parse)
1794 (<= (length fixed-results) result-count))
1795 (error "too many fixed results"))
1796 (unless (= (length info) info-count)
1797 (error "expected ~W info args" info-count))
1799 (multiple-value-bind (acode abinds n-args)
1800 (make-operand-list fixed-args (car (last args)) nil)
1801 (multiple-value-bind (rcode rbinds n-results)
1802 (make-operand-list fixed-results (car (last results)) t)
1804 `(let* ((,n-node ,node)
1805 (,n-block ,block)
1806 (,n-template (template-or-lose ',name))
1807 ,@abinds
1808 ,@rbinds)
1809 ,@acode
1810 ,@rcode
1811 (emit-template ,n-node ,n-block ,n-template ,n-args ,n-results
1812 ,@(when info
1813 `((list ,@info))))
1814 (values))))))
1816 ;;;; miscellaneous macros
1818 ;;; SC-Case TN {({(SC-Name*) | SC-Name | T} Form*)}*
1820 ;;; Case off of TN's SC. The first clause containing TN's SC is
1821 ;;; evaluated, returning the values of the last form. A clause
1822 ;;; beginning with T specifies a default. If it appears, it must be
1823 ;;; last. If no default is specified, and no clause matches, then an
1824 ;;; error is signalled.
1825 (defmacro sc-case (tn &body forms)
1826 (let ((n-sc (gensym))
1827 (n-tn (gensym)))
1828 (collect ((clauses))
1829 (do ((cases forms (rest cases)))
1830 ((null cases)
1831 (clauses `(t (error "unknown SC to SC-CASE for ~S:~% ~S" ,n-tn
1832 (sc-name (tn-sc ,n-tn))))))
1833 (let ((case (first cases)))
1834 (when (atom case)
1835 (error "illegal SC-CASE clause: ~S" case))
1836 (let ((head (first case)))
1837 (when (eq head t)
1838 (when (rest cases)
1839 (error "T case is not last in SC-CASE."))
1840 (clauses `(t nil ,@(rest case)))
1841 (return))
1842 (clauses `((or ,@(mapcar (lambda (x)
1843 `(eql ,(sc-number-or-lose x) ,n-sc))
1844 (if (atom head) (list head) head)))
1845 nil ,@(rest case))))))
1847 `(let* ((,n-tn ,tn)
1848 (,n-sc (sc-number (tn-sc ,n-tn))))
1849 (cond ,@(clauses))))))
1851 ;;; Return true if TNs SC is any of the named SCs, false otherwise.
1852 (defmacro sc-is (tn &rest scs)
1853 (once-only ((n-sc `(sc-number (tn-sc ,tn))))
1854 `(or ,@(mapcar (lambda (x)
1855 `(eql ,n-sc ,(sc-number-or-lose x)))
1856 scs))))
1858 ;;; Iterate over the IR2 blocks in component, in emission order.
1859 (defmacro do-ir2-blocks ((block-var component &optional result)
1860 &body forms)
1861 `(do ((,block-var (block-info (component-head ,component))
1862 (ir2-block-next ,block-var)))
1863 ((null ,block-var) ,result)
1864 ,@forms))
1866 ;;; Iterate over all the TNs live at some point, with the live set
1867 ;;; represented by a local conflicts bit-vector and the IR2-BLOCK
1868 ;;; containing the location.
1869 (defmacro do-live-tns ((tn-var live block &optional result) &body body)
1870 (with-unique-names (conf bod i ltns)
1871 (once-only ((n-live live)
1872 (n-block block))
1873 `(block nil
1874 (flet ((,bod (,tn-var) ,@body))
1875 ;; Do component-live TNs.
1876 (dolist (,tn-var (ir2-component-component-tns
1877 (component-info
1878 (block-component
1879 (ir2-block-block ,n-block)))))
1880 (,bod ,tn-var))
1882 (let ((,ltns (ir2-block-local-tns ,n-block)))
1883 ;; Do TNs always-live in this block and live :MORE TNs.
1884 (do ((,conf (ir2-block-global-tns ,n-block)
1885 (global-conflicts-next-blockwise ,conf)))
1886 ((null ,conf))
1887 (when (or (eq (global-conflicts-kind ,conf) :live)
1888 (let ((,i (global-conflicts-number ,conf)))
1889 (and (eq (svref ,ltns ,i) :more)
1890 (not (zerop (sbit ,n-live ,i))))))
1891 (,bod (global-conflicts-tn ,conf))))
1892 ;; Do TNs locally live in the designated live set.
1893 (dotimes (,i (ir2-block-local-tn-count ,n-block) ,result)
1894 (unless (zerop (sbit ,n-live ,i))
1895 (let ((,tn-var (svref ,ltns ,i)))
1896 (when (and ,tn-var (not (eq ,tn-var :more)))
1897 (,bod ,tn-var)))))))))))
1899 ;;; Iterate over all the IR2 blocks in PHYSENV, in emit order.
1900 (defmacro do-physenv-ir2-blocks ((block-var physenv &optional result)
1901 &body body)
1902 (once-only ((n-physenv physenv))
1903 (once-only ((n-first `(lambda-block (physenv-lambda ,n-physenv))))
1904 (once-only ((n-tail `(block-info
1905 (component-tail
1906 (block-component ,n-first)))))
1907 `(do ((,block-var (block-info ,n-first)
1908 (ir2-block-next ,block-var)))
1909 ((or (eq ,block-var ,n-tail)
1910 (not (eq (ir2-block-physenv ,block-var) ,n-physenv)))
1911 ,result)
1912 ,@body)))))