Don't transform LIST with a very large number of arguments.
[sbcl.git] / src / compiler / meta-vmdef.lisp
blobcb7375696b0e4843334475ea474c5f5e806de3a4
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 (:make-load-form-fun just-dump-it-normally)
263 #-sb-xc-host (:pure t))
264 ;; name of the operand (which we bind to the TN)
265 (name nil :type symbol)
266 ;; the way this operand is used:
267 (kind (missing-arg)
268 :type (member :argument :result :temporary
269 :more-argument :more-result))
270 ;; If true, the name of an operand that this operand is targeted to.
271 ;; This is only meaningful in :ARGUMENT and :TEMPORARY operands.
272 (target nil :type (or symbol null))
273 ;; TEMP is a temporary that holds the TN-REF for this operand.
274 (temp (make-operand-parse-temp) :type symbol)
275 ;; the time that this operand is first live and the time at which it
276 ;; becomes dead again. These are TIME-SPECs, as returned by
277 ;; PARSE-TIME-SPEC.
278 born
279 dies
280 ;; a list of the names of the SCs that this operand is allowed into.
281 ;; If false, there is no restriction.
282 (scs nil :type list)
283 ;; Variable that is bound to the load TN allocated for this operand, or to
284 ;; NIL if no load-TN was allocated.
285 (load-tn (make-operand-parse-load-tn) :type symbol)
286 ;; an expression that tests whether to do automatic operand loading
287 (load t)
288 ;; In a wired or restricted temporary this is the SC the TN is to be
289 ;; packed in. Null otherwise.
290 (sc nil :type (or symbol null))
291 ;; If non-null, we are a temp wired to this offset in SC.
292 (offset nil :type (or unsigned-byte null)))
294 ;;; A VOP-PARSE object holds everything we need to know about a VOP at
295 ;;; meta-compile time.
296 (def!struct (vop-parse
297 (:make-load-form-fun just-dump-it-normally)
298 #-sb-xc-host (:pure t))
299 ;; the name of this VOP
300 (name nil :type symbol)
301 ;; If true, then the name of the VOP we inherit from.
302 (inherits nil :type (or symbol null))
303 ;; lists of OPERAND-PARSE structures describing the arguments,
304 ;; results and temporaries of the VOP
305 (args nil :type list)
306 (results nil :type list)
307 (temps nil :type list)
308 ;; OPERAND-PARSE structures containing information about more args
309 ;; and results. If null, then there there are no more operands of
310 ;; that kind
311 (more-args nil :type (or operand-parse null))
312 (more-results nil :type (or operand-parse null))
313 ;; a list of all the above together
314 (operands nil :type list)
315 ;; names of variables that should be declared IGNORE
316 (ignores () :type list)
317 ;; true if this is a :CONDITIONAL VOP. T if a branchful VOP,
318 ;; a list of condition descriptor otherwise. See $ARCH/pred.lisp
319 ;; for more information.
320 (conditional-p nil)
321 ;; argument and result primitive types. These are pulled out of the
322 ;; operands, since we often want to change them without respecifying
323 ;; the operands.
324 (arg-types :unspecified :type (or (member :unspecified) list))
325 (result-types :unspecified :type (or (member :unspecified) list))
326 ;; the guard expression specified, or NIL if none
327 (guard nil)
328 ;; the cost of and body code for the generator
329 (cost 0 :type unsigned-byte)
330 (body :unspecified :type (or (member :unspecified) list))
331 ;; info for VOP variants. The list of forms to be evaluated to get
332 ;; the variant args for this VOP, and the list of variables to be
333 ;; bound to the variant args.
334 (variant () :type list)
335 (variant-vars () :type list)
336 ;; variables bound to the VOP and Vop-Node when in the generator body
337 (vop-var '.vop. :type symbol)
338 (node-var nil :type (or symbol null))
339 ;; a list of the names of the codegen-info arguments to this VOP
340 (info-args () :type list)
341 ;; an efficiency note associated with this VOP
342 (note nil :type (or string null))
343 ;; a list of the names of the Effects and Affected attributes for
344 ;; this VOP
345 (effects '#1=(any) :type list)
346 (affected '#1# :type list)
347 ;; a list of the names of functions this VOP is a translation of and
348 ;; the policy that allows this translation to be done. :FAST is a
349 ;; safe default, since it isn't a safe policy.
350 (translate () :type list)
351 (ltn-policy :fast :type ltn-policy)
352 ;; stuff used by life analysis
353 (save-p nil :type (member t nil :compute-only :force-to-stack))
354 ;; info about how to emit MOVE-ARG VOPs for the &MORE operand in
355 ;; call/return VOPs
356 (move-args nil :type (member nil :local-call :full-call :known-return)))
357 (defprinter (vop-parse)
358 name
359 (inherits :test inherits)
360 args
361 results
362 temps
363 (more-args :test more-args)
364 (more-results :test more-results)
365 (conditional-p :test conditional-p)
366 ignores
367 arg-types
368 result-types
369 cost
370 body
371 (variant :test variant)
372 (variant-vars :test variant-vars)
373 (info-args :test info-args)
374 (note :test note)
375 effects
376 affected
377 translate
378 ltn-policy
379 (save-p :test save-p)
380 (move-args :test move-args))
382 (defprinter (operand-parse)
383 name
384 kind
385 (target :test target)
386 born
387 dies
388 (scs :test scs)
389 (load :test load)
390 (sc :test sc)
391 (offset :test offset))
393 ;;; Make NAME be the VOP used to move values in the specified FROM-SCs
394 ;;; to the representation of the TO-SCs of each SC pair in SCS.
396 ;;; If KIND is :MOVE-ARG, then the VOP takes an extra argument,
397 ;;; which is the frame pointer of the frame to move into.
399 ;;; We record the VOP and costs for all SCs that we can move between
400 ;;; (including implicit loading).
401 (defmacro define-move-vop (name kind &rest scs)
402 (when (or (oddp (length scs)) (null scs))
403 (error "malformed SCs spec: ~S" scs))
404 (let ((accessor (or (cdr (assoc kind *sc-vop-slots*))
405 (error "unknown kind ~S" kind))))
406 `(progn
407 ,@(when (eq kind :move)
408 `((eval-when (:compile-toplevel :load-toplevel :execute)
409 (do-sc-pairs (from-sc to-sc ',scs)
410 (compute-move-costs from-sc to-sc
411 ,(vop-parse-cost
412 (vop-parse-or-lose name)))))))
414 (let ((vop (template-or-lose ',name)))
415 (do-sc-pairs (from-sc to-sc ',scs)
416 (dolist (dest-sc (cons to-sc (sc-alternate-scs to-sc)))
417 (let ((vec (,accessor dest-sc)))
418 (let ((scn (sc-number from-sc)))
419 (setf (svref vec scn)
420 (adjoin-template vop (svref vec scn))))
421 (dolist (sc (append (sc-alternate-scs from-sc)
422 (sc-constant-scs from-sc)))
423 (let ((scn (sc-number sc)))
424 (setf (svref vec scn)
425 (adjoin-template vop (svref vec scn))))))))))))
427 ;;;; miscellaneous utilities
429 ;;; Find the operand or temporary with the specifed Name in the VOP
430 ;;; Parse. If there is no such operand, signal an error. Also error if
431 ;;; the operand kind isn't one of the specified Kinds. If Error-P is
432 ;;; NIL, just return NIL if there is no such operand.
433 (defun find-operand (name parse &optional
434 (kinds '(:argument :result :temporary))
435 (error-p t))
436 (declare (symbol name) (type vop-parse parse) (list kinds))
437 (let ((found (find name (vop-parse-operands parse)
438 :key #'operand-parse-name)))
439 (if found
440 (unless (member (operand-parse-kind found) kinds)
441 (error "Operand ~S isn't one of these kinds: ~S." name kinds))
442 (when error-p
443 (error "~S is not an operand to ~S." name (vop-parse-name parse))))
444 found))
446 ;;; Get the VOP-PARSE structure for NAME or die trying. For all
447 ;;; meta-compile time uses, the VOP-PARSE should be used instead of
448 ;;; the VOP-INFO.
449 (defun vop-parse-or-lose (name)
450 (the vop-parse
451 (or (gethash name *backend-parsed-vops*)
452 (error "~S is not the name of a defined VOP." name))))
454 ;;; Return a list of LET-forms to parse a TN-REF list into the temps
455 ;;; specified by the operand-parse structures. MORE-OPERAND is the
456 ;;; OPERAND-PARSE describing any more operand, or NIL if none. REFS is
457 ;;; an expression that evaluates into the first TN-REF.
458 (defun access-operands (operands more-operand refs)
459 (declare (list operands))
460 (collect ((res))
461 (let ((prev refs))
462 (dolist (op operands)
463 (let ((n-ref (operand-parse-temp op)))
464 (res `(,n-ref ,prev))
465 (setq prev `(tn-ref-across ,n-ref))))
467 (when more-operand
468 (res `(,(operand-parse-name more-operand) ,prev))))
469 (res)))
471 ;;; This is used with ACCESS-OPERANDS to prevent warnings for TN-REF
472 ;;; temps not used by some particular function. It returns the name of
473 ;;; the last operand, or NIL if OPERANDS is NIL.
474 (defun ignore-unreferenced-temps (operands)
475 (when operands
476 (operand-parse-temp (car (last operands)))))
478 ;;; Grab an arg out of a VOP spec, checking the type and syntax and stuff.
479 (defun vop-spec-arg (spec type &optional (n 1) (last t))
480 (let ((len (length spec)))
481 (when (<= len n)
482 (error "~:R argument missing: ~S" n spec))
483 (when (and last (> len (1+ n)))
484 (error "extra junk at end of ~S" spec))
485 (let ((thing (elt spec n)))
486 (unless (typep thing type)
487 (error "~:R argument is not a ~S: ~S" n type spec))
488 thing)))
490 ;;;; time specs
492 ;;; Return a time spec describing a time during the evaluation of a
493 ;;; VOP, used to delimit operand and temporary lifetimes. The
494 ;;; representation is a cons whose CAR is the number of the evaluation
495 ;;; phase and the CDR is the sub-phase. The sub-phase is 0 in the
496 ;;; :LOAD and :SAVE phases.
497 (defun parse-time-spec (spec)
498 (let ((dspec (if (atom spec) (list spec 0) spec)))
499 (unless (and (= (length dspec) 2)
500 (typep (second dspec) 'unsigned-byte))
501 (error "malformed time specifier: ~S" spec))
503 (cons (case (first dspec)
504 (:load 0)
505 (:argument 1)
506 (:eval 2)
507 (:result 3)
508 (:save 4)
510 (error "unknown phase in time specifier: ~S" spec)))
511 (second dspec))))
513 ;;; Return true if the time spec X is the same or later time than Y.
514 (defun time-spec-order (x y)
515 (or (> (car x) (car y))
516 (and (= (car x) (car y))
517 (>= (cdr x) (cdr y)))))
519 ;;;; generation of emit functions
521 (defun compute-temporaries-description (parse)
522 (let ((temps (vop-parse-temps parse))
523 (element-type '(unsigned-byte 16)))
524 (when temps
525 (let ((results (!make-specialized-array (length temps) element-type))
526 (index 0))
527 (dolist (temp temps)
528 (declare (type operand-parse temp))
529 (let ((sc (operand-parse-sc temp))
530 (offset (operand-parse-offset temp)))
531 (aver sc)
532 (setf (aref results index)
533 (if offset
534 (+ (ash offset (1+ sc-bits))
535 (ash (sc-number-or-lose sc) 1)
537 (ash (sc-number-or-lose sc) 1))))
538 (incf index))
539 results))))
541 (defun compute-ref-ordering (parse)
542 (let* ((num-args (+ (length (vop-parse-args parse))
543 (if (vop-parse-more-args parse) 1 0)))
544 (num-results (+ (length (vop-parse-results parse))
545 (if (vop-parse-more-results parse) 1 0)))
546 (index 0))
547 (collect ((refs) (targets))
548 (dolist (op (vop-parse-operands parse))
549 (when (operand-parse-target op)
550 (unless (member (operand-parse-kind op) '(:argument :temporary))
551 (error "cannot target a ~S operand: ~S" (operand-parse-kind op)
552 (operand-parse-name op)))
553 (let ((target (find-operand (operand-parse-target op) parse
554 '(:temporary :result))))
555 ;; KLUDGE: These formulas must be consistent with those in
556 ;; EMIT-VOP, and this is currently maintained by
557 ;; hand. -- WHN 2002-01-30, paraphrasing APD
558 (targets (+ (* index max-vop-tn-refs)
559 (ecase (operand-parse-kind target)
560 (:result
561 (+ (position-or-lose target
562 (vop-parse-results parse))
563 num-args))
564 (:temporary
565 (+ (* (position-or-lose target
566 (vop-parse-temps parse))
569 num-args
570 num-results)))))))
571 (let ((born (operand-parse-born op))
572 (dies (operand-parse-dies op)))
573 (ecase (operand-parse-kind op)
574 (:argument
575 (refs (cons (cons dies nil) index)))
576 (:more-argument
577 (refs (cons (cons dies nil) index)))
578 (:result
579 (refs (cons (cons born t) index)))
580 (:more-result
581 (refs (cons (cons born t) index)))
582 (:temporary
583 (refs (cons (cons dies nil) index))
584 (incf index)
585 (refs (cons (cons born t) index))))
586 (incf index)))
587 (let* ((sorted (stable-sort (refs)
588 (lambda (x y)
589 (let ((x-time (car x))
590 (y-time (car y)))
591 (if (time-spec-order x-time y-time)
592 (if (time-spec-order y-time x-time)
593 (and (not (cdr x)) (cdr y))
594 nil)
595 t)))
596 :key #'car))
597 ;; :REF-ORDERING element type
599 ;; KLUDGE: was (MOD #.MAX-VOP-TN-REFS), which is still right
600 (oe-type '(unsigned-byte 8))
601 ;; :TARGETS element-type
603 ;; KLUDGE: was (MOD #.(* MAX-VOP-TN-REFS 2)), which does
604 ;; not correspond to the definition in
605 ;; src/compiler/vop.lisp.
606 (te-type '(unsigned-byte 16))
607 (ordering (!make-specialized-array (length sorted) oe-type)))
608 (let ((index 0))
609 (dolist (ref sorted)
610 (setf (aref ordering index) (cdr ref))
611 (incf index)))
612 `(:num-args ,num-args
613 :num-results ,num-results
614 :ref-ordering ,ordering
615 ,@(when (targets)
616 `(:targets ,(!make-specialized-array
617 (length (targets)) te-type (targets)))))))))
619 (defun make-emit-function-and-friends (parse)
620 `(:temps ,(compute-temporaries-description parse)
621 ,@(compute-ref-ordering parse)))
623 ;;;; generator functions
625 ;;; Return an alist that translates from lists of SCs we can load OP
626 ;;; from to the move function used for loading those SCs. We quietly
627 ;;; ignore restrictions to :non-packed (constant) and :unbounded SCs,
628 ;;; since we don't load into those SCs.
629 (defun find-move-funs (op load-p)
630 (collect ((funs))
631 (dolist (sc-name (operand-parse-scs op))
632 (let* ((sc (sc-or-lose sc-name))
633 (scn (sc-number sc))
634 (load-scs (append (when load-p
635 (sc-constant-scs sc))
636 (sc-alternate-scs sc))))
637 (cond
638 (load-scs
639 (dolist (alt load-scs)
640 (unless (member (sc-name alt) (operand-parse-scs op) :test #'eq)
641 (let* ((altn (sc-number alt))
642 (name (if load-p
643 (svref (sc-move-funs sc) altn)
644 (svref (sc-move-funs alt) scn)))
645 (found (or (assoc alt (funs) :test #'member)
646 (rassoc name (funs)))))
647 (unless name
648 (error "no move function defined to ~:[save~;load~] SC ~S ~
649 ~:[to~;from~] from SC ~S"
650 load-p sc-name load-p (sc-name alt)))
651 (cond (found
652 (pushnew alt (car found)))
654 (funs (cons (list alt) name))))))))
655 ((member (sb-kind (sc-sb sc)) '(:non-packed :unbounded)))
657 (error "SC ~S has no alternate~:[~; or constant~] SCs, yet it is~@
658 mentioned in the restriction for operand ~S"
659 sc-name load-p (operand-parse-name op))))))
660 (funs)))
662 ;;; Return a form to load/save the specified operand when it has a
663 ;;; load TN. For any given SC that we can load from, there must be a
664 ;;; unique load function. If all SCs we can load from have the same
665 ;;; move function, then we just call that when there is a load TN. If
666 ;;; there are multiple possible move functions, then we dispatch off
667 ;;; of the operand TN's type to see which move function to use.
668 (defun call-move-fun (parse op load-p)
669 (let ((funs (find-move-funs op load-p))
670 (load-tn (operand-parse-load-tn op)))
671 (if funs
672 (let* ((tn `(tn-ref-tn ,(operand-parse-temp op)))
673 (n-vop (or (vop-parse-vop-var parse)
674 (setf (vop-parse-vop-var parse) '.vop.)))
675 (form (if (rest funs)
676 `(sc-case ,tn
677 ,@(mapcar (lambda (x)
678 `(,(mapcar #'sc-name (car x))
679 ,(if load-p
680 `(,(cdr x) ,n-vop ,tn
681 ,load-tn)
682 `(,(cdr x) ,n-vop ,load-tn
683 ,tn))))
684 funs))
685 (if load-p
686 `(,(cdr (first funs)) ,n-vop ,tn ,load-tn)
687 `(,(cdr (first funs)) ,n-vop ,load-tn ,tn)))))
688 (if (eq (operand-parse-load op) t)
689 `(when ,load-tn ,form)
690 `(when (eq ,load-tn ,(operand-parse-name op))
691 ,form)))
692 `(when ,load-tn
693 (error "load TN allocated, but no move function?~@
694 VM definition is inconsistent, recompile and try again.")))))
696 ;;; Return the TN that we should bind to the operand's var in the
697 ;;; generator body. In general, this involves evaluating the :LOAD-IF
698 ;;; test expression.
699 (defun decide-to-load (parse op)
700 (let ((load (operand-parse-load op))
701 (load-tn (operand-parse-load-tn op))
702 (temp (operand-parse-temp op)))
703 (if (eq load t)
704 `(or ,load-tn (tn-ref-tn ,temp))
705 (collect ((binds)
706 (ignores))
707 (dolist (x (vop-parse-operands parse))
708 (when (member (operand-parse-kind x) '(:argument :result))
709 (let ((name (operand-parse-name x)))
710 (binds `(,name (tn-ref-tn ,(operand-parse-temp x))))
711 (ignores name))))
712 `(if (and ,load-tn
713 (let ,(binds)
714 (declare (ignorable ,@(ignores)))
715 ,load))
716 ,load-tn
717 (tn-ref-tn ,temp))))))
719 ;;; Make a lambda that parses the VOP TN-REFS, does automatic operand
720 ;;; loading, and runs the appropriate code generator.
721 (defun make-generator-function (parse)
722 (declare (type vop-parse parse))
723 (let ((n-vop (vop-parse-vop-var parse))
724 (operands (vop-parse-operands parse))
725 (n-info (gensym)) (n-variant (gensym)))
726 (collect ((binds)
727 (loads)
728 (saves))
729 (dolist (op operands)
730 (ecase (operand-parse-kind op)
731 ((:argument :result)
732 (let ((temp (operand-parse-temp op))
733 (name (operand-parse-name op)))
734 (cond ((and (operand-parse-load op) (operand-parse-scs op))
735 (binds `(,(operand-parse-load-tn op)
736 (tn-ref-load-tn ,temp)))
737 (binds `(,name ,(decide-to-load parse op)))
738 (if (eq (operand-parse-kind op) :argument)
739 (loads (call-move-fun parse op t))
740 (saves (call-move-fun parse op nil))))
742 (binds `(,name (tn-ref-tn ,temp)))))))
743 (:temporary
744 (binds `(,(operand-parse-name op)
745 (tn-ref-tn ,(operand-parse-temp op)))))
746 ((:more-argument :more-result))))
748 `(named-lambda (vop ,(vop-parse-name parse)) (,n-vop)
749 (let* (,@(access-operands (vop-parse-args parse)
750 (vop-parse-more-args parse)
751 `(vop-args ,n-vop))
752 ,@(access-operands (vop-parse-results parse)
753 (vop-parse-more-results parse)
754 `(vop-results ,n-vop))
755 ,@(access-operands (vop-parse-temps parse) nil
756 `(vop-temps ,n-vop))
757 ,@(when (vop-parse-info-args parse)
758 `((,n-info (vop-codegen-info ,n-vop))
759 ,@(mapcar (lambda (x) `(,x (pop ,n-info)))
760 (vop-parse-info-args parse))))
761 ,@(when (vop-parse-variant-vars parse)
762 `((,n-variant (vop-info-variant (vop-info ,n-vop)))
763 ,@(mapcar (lambda (x) `(,x (pop ,n-variant)))
764 (vop-parse-variant-vars parse))))
765 ,@(when (vop-parse-node-var parse)
766 `((,(vop-parse-node-var parse) (vop-node ,n-vop))))
767 ,@(binds))
768 (declare (ignore ,@(vop-parse-ignores parse)))
769 ,@(loads)
770 (sb!assem:assemble (*code-segment* ,n-vop)
771 ,@(vop-parse-body parse))
772 ,@(saves))))))
774 (defvar *parse-vop-operand-count*)
775 (defun make-operand-parse-temp ()
776 (without-package-locks
777 (intern (format nil "OPERAND-PARSE-TEMP-~D" *parse-vop-operand-count*)
778 (symbol-package '*parse-vop-operand-count*))))
779 (defun make-operand-parse-load-tn ()
780 (without-package-locks
781 (intern (format nil "OPERAND-PARSE-LOAD-TN-~D" *parse-vop-operand-count*)
782 (symbol-package '*parse-vop-operand-count*))))
784 ;;; Given a list of operand specifications as given to DEFINE-VOP,
785 ;;; return a list of OPERAND-PARSE structures describing the fixed
786 ;;; operands, and a single OPERAND-PARSE describing any more operand.
787 ;;; If we are inheriting a VOP, we default attributes to the inherited
788 ;;; operand of the same name.
789 (defun parse-vop-operands (parse specs kind)
790 (declare (list specs)
791 (type (member :argument :result) kind))
792 (let ((num -1)
793 (more nil))
794 (collect ((operands))
795 (dolist (spec specs)
796 (unless (and (consp spec) (symbolp (first spec)) (oddp (length spec)))
797 (error "malformed operand specifier: ~S" spec))
798 (when more
799 (error "The MORE operand isn't the last operand: ~S" specs))
800 (incf *parse-vop-operand-count*)
801 (let* ((name (first spec))
802 (old (if (vop-parse-inherits parse)
803 (find-operand name
804 (vop-parse-or-lose
805 (vop-parse-inherits parse))
806 (list kind)
807 nil)
808 nil))
809 (res (if old
810 (make-operand-parse
811 :name name
812 :kind kind
813 :target (operand-parse-target old)
814 :born (operand-parse-born old)
815 :dies (operand-parse-dies old)
816 :scs (operand-parse-scs old)
817 :load-tn (operand-parse-load-tn old)
818 :load (operand-parse-load old))
819 (ecase kind
820 (:argument
821 (make-operand-parse
822 :name (first spec)
823 :kind :argument
824 :born (parse-time-spec :load)
825 :dies (parse-time-spec `(:argument ,(incf num)))))
826 (:result
827 (make-operand-parse
828 :name (first spec)
829 :kind :result
830 :born (parse-time-spec `(:result ,(incf num)))
831 :dies (parse-time-spec :save)))))))
832 (do ((key (rest spec) (cddr key)))
833 ((null key))
834 (let ((value (second key)))
835 (case (first key)
836 (:scs
837 (aver (typep value 'list))
838 (aver (= (length value) (length (remove-duplicates value))))
839 (setf (operand-parse-scs res) (copy-list value)))
840 (:load-tn
841 (aver (typep value 'symbol))
842 (setf (operand-parse-load-tn res) value))
843 (:load-if
844 (setf (operand-parse-load res) value))
845 (:more
846 (aver (typep value 'boolean))
847 (setf (operand-parse-kind res)
848 (if (eq kind :argument) :more-argument :more-result))
849 (setf (operand-parse-load res) nil)
850 (setq more res))
851 (:target
852 (aver (typep value 'symbol))
853 (setf (operand-parse-target res) value))
854 (:from
855 (unless (eq kind :result)
856 (error "can only specify :FROM in a result: ~S" spec))
857 (setf (operand-parse-born res) (parse-time-spec value)))
858 (:to
859 (unless (eq kind :argument)
860 (error "can only specify :TO in an argument: ~S" spec))
861 (setf (operand-parse-dies res) (parse-time-spec value)))
863 (error "unknown keyword in operand specifier: ~S" spec)))))
865 (cond ((not more)
866 (operands res))
867 ((operand-parse-target more)
868 (error "cannot specify :TARGET in a :MORE operand"))
869 ((operand-parse-load more)
870 (error "cannot specify :LOAD-IF in a :MORE operand")))))
871 (values (the list (operands)) more))))
873 ;;; Parse a temporary specification, putting the OPERAND-PARSE
874 ;;; structures in the PARSE structure.
875 (defun parse-temporary (spec parse)
876 (declare (list spec)
877 (type vop-parse parse))
878 (let ((len (length spec)))
879 (unless (>= len 2)
880 (error "malformed temporary spec: ~S" spec))
881 (unless (listp (second spec))
882 (error "malformed options list: ~S" (second spec)))
883 (unless (evenp (length (second spec)))
884 (error "odd number of arguments in keyword options: ~S" spec))
885 (unless (consp (cddr spec))
886 (warn "temporary spec allocates no temps:~% ~S" spec))
887 (dolist (name (cddr spec))
888 (unless (symbolp name)
889 (error "bad temporary name: ~S" name))
890 (incf *parse-vop-operand-count*)
891 (let ((res (make-operand-parse :name name
892 :kind :temporary
893 :born (parse-time-spec :load)
894 :dies (parse-time-spec :save))))
895 (do ((opt (second spec) (cddr opt)))
896 ((null opt))
897 (case (first opt)
898 (:target
899 (setf (operand-parse-target res)
900 (vop-spec-arg opt 'symbol 1 nil)))
901 (:sc
902 (setf (operand-parse-sc res)
903 (vop-spec-arg opt 'symbol 1 nil)))
904 (:offset
905 (let ((offset (eval (second opt))))
906 (aver (typep offset 'unsigned-byte))
907 (setf (operand-parse-offset res) offset)))
908 (:from
909 (setf (operand-parse-born res) (parse-time-spec (second opt))))
910 (:to
911 (setf (operand-parse-dies res) (parse-time-spec (second opt))))
912 ;; backward compatibility...
913 (:scs
914 (let ((scs (vop-spec-arg opt 'list 1 nil)))
915 (unless (= (length scs) 1)
916 (error "must specify exactly one SC for a temporary"))
917 (setf (operand-parse-sc res) (first scs))))
918 (:type)
920 (error "unknown temporary option: ~S" opt))))
922 (unless (and (time-spec-order (operand-parse-dies res)
923 (operand-parse-born res))
924 (not (time-spec-order (operand-parse-born res)
925 (operand-parse-dies res))))
926 (error "Temporary lifetime doesn't begin before it ends: ~S" spec))
928 (unless (operand-parse-sc res)
929 (error "must specify :SC for all temporaries: ~S" spec))
931 (setf (vop-parse-temps parse)
932 (cons res
933 (remove name (vop-parse-temps parse)
934 :key #'operand-parse-name))))))
935 (values))
937 (defun compute-parse-vop-operand-count (parse)
938 (declare (type vop-parse parse))
939 (labels ((compute-count-aux (parse)
940 (declare (type vop-parse parse))
941 (if (null (vop-parse-inherits parse))
942 (length (vop-parse-operands parse))
943 (+ (length (vop-parse-operands parse))
944 (compute-count-aux
945 (vop-parse-or-lose (vop-parse-inherits parse)))))))
946 (if (null (vop-parse-inherits parse))
948 (compute-count-aux (vop-parse-or-lose (vop-parse-inherits parse))))))
950 ;;; the top level parse function: clobber PARSE to represent the
951 ;;; specified options.
952 (defun parse-define-vop (parse specs)
953 (declare (type vop-parse parse) (list specs))
954 (let ((*parse-vop-operand-count* (compute-parse-vop-operand-count parse)))
955 (dolist (spec specs)
956 (unless (consp spec)
957 (error "malformed option specification: ~S" spec))
958 (case (first spec)
959 (:args
960 (multiple-value-bind (fixed more)
961 (parse-vop-operands parse (rest spec) :argument)
962 (setf (vop-parse-args parse) fixed)
963 (setf (vop-parse-more-args parse) more)))
964 (:results
965 (multiple-value-bind (fixed more)
966 (parse-vop-operands parse (rest spec) :result)
967 (setf (vop-parse-results parse) fixed)
968 (setf (vop-parse-more-results parse) more))
969 (setf (vop-parse-conditional-p parse) nil))
970 (:conditional
971 (setf (vop-parse-result-types parse) ())
972 (setf (vop-parse-results parse) ())
973 (setf (vop-parse-more-results parse) nil)
974 (setf (vop-parse-conditional-p parse) (or (rest spec) t)))
975 (:temporary
976 (parse-temporary spec parse))
977 (:generator
978 (setf (vop-parse-cost parse)
979 (vop-spec-arg spec 'unsigned-byte 1 nil))
980 (setf (vop-parse-body parse) (cddr spec)))
981 (:effects
982 (setf (vop-parse-effects parse) (rest spec)))
983 (:affected
984 (setf (vop-parse-affected parse) (rest spec)))
985 (:info
986 (setf (vop-parse-info-args parse) (rest spec)))
987 (:ignore
988 (setf (vop-parse-ignores parse) (rest spec)))
989 (:variant
990 (setf (vop-parse-variant parse) (rest spec)))
991 (:variant-vars
992 (let ((vars (rest spec)))
993 (setf (vop-parse-variant-vars parse) vars)
994 (setf (vop-parse-variant parse)
995 (make-list (length vars) :initial-element nil))))
996 (:variant-cost
997 (setf (vop-parse-cost parse) (vop-spec-arg spec 'unsigned-byte)))
998 (:vop-var
999 (setf (vop-parse-vop-var parse) (vop-spec-arg spec 'symbol)))
1000 (:move-args
1001 (setf (vop-parse-move-args parse)
1002 (vop-spec-arg spec '(member nil :local-call :full-call
1003 :known-return))))
1004 (:node-var
1005 (setf (vop-parse-node-var parse) (vop-spec-arg spec 'symbol)))
1006 (:note
1007 (setf (vop-parse-note parse) (vop-spec-arg spec '(or string null))))
1008 (:arg-types
1009 (setf (vop-parse-arg-types parse)
1010 (parse-vop-operand-types (rest spec) t)))
1011 (:result-types
1012 (setf (vop-parse-result-types parse)
1013 (parse-vop-operand-types (rest spec) nil)))
1014 (:translate
1015 (setf (vop-parse-translate parse) (rest spec)))
1016 (:guard
1017 (setf (vop-parse-guard parse) (vop-spec-arg spec t)))
1018 ;; FIXME: :LTN-POLICY would be a better name for this. It
1019 ;; would probably be good to leave it unchanged for a while,
1020 ;; though, at least until the first port to some other
1021 ;; architecture, since the renaming would be a change to the
1022 ;; interface between
1023 (:policy
1024 (setf (vop-parse-ltn-policy parse)
1025 (vop-spec-arg spec 'ltn-policy)))
1026 (:save-p
1027 (setf (vop-parse-save-p parse)
1028 (vop-spec-arg spec
1029 '(member t nil :compute-only :force-to-stack))))
1031 (error "unknown option specifier: ~S" (first spec)))))
1032 (values)))
1034 ;;;; making costs and restrictions
1036 ;;; Given an operand, returns two values:
1037 ;;; 1. A SC-vector of the cost for the operand being in that SC,
1038 ;;; including both the costs for move functions and coercion VOPs.
1039 ;;; 2. A SC-vector holding the SC that we load into, for any SC
1040 ;;; that we can directly load from.
1042 ;;; In both vectors, unused entries are NIL. LOAD-P specifies the
1043 ;;; direction: if true, we are loading, if false we are saving.
1044 (defun compute-loading-costs (op load-p)
1045 (declare (type operand-parse op))
1046 (let ((scs (operand-parse-scs op))
1047 (costs (make-array sc-number-limit :initial-element nil))
1048 (load-scs (make-array sc-number-limit :initial-element nil)))
1049 (dolist (sc-name (reverse scs))
1050 (let* ((load-sc (sc-or-lose sc-name))
1051 (load-scn (sc-number load-sc)))
1052 (setf (svref costs load-scn) 0)
1053 (setf (svref load-scs load-scn) t)
1054 (dolist (op-sc (append (when load-p
1055 (sc-constant-scs load-sc))
1056 (sc-alternate-scs load-sc)))
1057 (let* ((op-scn (sc-number op-sc))
1058 (load (if load-p
1059 (aref (sc-load-costs load-sc) op-scn)
1060 (aref (sc-load-costs op-sc) load-scn))))
1061 (unless load
1062 (error "no move function defined to move ~:[from~;to~] SC ~
1063 ~S~%~:[to~;from~] alternate or constant SC ~S"
1064 load-p sc-name load-p (sc-name op-sc)))
1066 (let ((op-cost (svref costs op-scn)))
1067 (when (or (not op-cost) (< load op-cost))
1068 (setf (svref costs op-scn) load)))
1070 (let ((op-load (svref load-scs op-scn)))
1071 (unless (eq op-load t)
1072 (pushnew load-scn (svref load-scs op-scn))))))
1074 (dotimes (i sc-number-limit)
1075 (unless (svref costs i)
1076 (let ((op-sc (svref *backend-sc-numbers* i)))
1077 (when op-sc
1078 (let ((cost (if load-p
1079 (svref (sc-move-costs load-sc) i)
1080 (svref (sc-move-costs op-sc) load-scn))))
1081 (when cost
1082 (setf (svref costs i) cost)))))))))
1084 (values costs load-scs)))
1086 (defparameter *no-costs*
1087 (make-array sc-number-limit :initial-element 0))
1089 (defparameter *no-loads*
1090 (make-array sc-number-limit :initial-element t))
1092 ;;; Pick off the case of operands with no restrictions.
1093 (defun compute-loading-costs-if-any (op load-p)
1094 (declare (type operand-parse op))
1095 (if (operand-parse-scs op)
1096 (compute-loading-costs op load-p)
1097 (values *no-costs* *no-loads*)))
1099 (defun compute-costs-and-restrictions-list (ops load-p)
1100 (declare (list ops))
1101 (collect ((costs)
1102 (scs))
1103 (dolist (op ops)
1104 (multiple-value-bind (costs scs) (compute-loading-costs-if-any op load-p)
1105 (costs costs)
1106 (scs scs)))
1107 (values (costs) (scs))))
1109 (defun make-costs-and-restrictions (parse)
1110 (multiple-value-bind (arg-costs arg-scs)
1111 (compute-costs-and-restrictions-list (vop-parse-args parse) t)
1112 (multiple-value-bind (result-costs result-scs)
1113 (compute-costs-and-restrictions-list (vop-parse-results parse) nil)
1115 :cost ,(vop-parse-cost parse)
1117 :arg-costs ',arg-costs
1118 :arg-load-scs ',arg-scs
1119 :result-costs ',result-costs
1120 :result-load-scs ',result-scs
1122 :more-arg-costs
1123 ',(if (vop-parse-more-args parse)
1124 (compute-loading-costs-if-any (vop-parse-more-args parse) t)
1125 nil)
1127 :more-result-costs
1128 ',(if (vop-parse-more-results parse)
1129 (compute-loading-costs-if-any (vop-parse-more-results parse) nil)
1130 nil)))))
1132 ;;;; operand checking and stuff
1134 ;;; Given a list of arg/result restrictions, check for valid syntax
1135 ;;; and convert to canonical form.
1136 (defun parse-vop-operand-types (specs args-p)
1137 (declare (list specs))
1138 (labels ((primtype-alias-p (spec)
1139 (cdr (assq spec *backend-primitive-type-aliases*)))
1140 (parse-operand-type (spec)
1141 (cond ((eq spec '*) spec)
1142 ((symbolp spec)
1143 (let ((alias (primtype-alias-p spec)))
1144 (if alias
1145 (parse-operand-type alias)
1146 `(:or ,spec))))
1147 ((atom spec)
1148 (error "bad thing to be a operand type: ~S" spec))
1150 (case (first spec)
1151 (:or
1152 (collect ((results))
1153 (dolist (item (cdr spec))
1154 (unless (symbolp item)
1155 (error "bad PRIMITIVE-TYPE name in ~S: ~S"
1156 spec item))
1157 (let ((alias (primtype-alias-p item)))
1158 (if alias
1159 (let ((alias (parse-operand-type alias)))
1160 (unless (eq (car alias) :or)
1161 (error "can't include primitive-type ~
1162 alias ~S in an :OR restriction: ~S"
1163 item spec))
1164 (dolist (x (cdr alias))
1165 (results x)))
1166 (results item))))
1167 `(:or ,@(remove-duplicates (results) :test #'eq))))
1168 (:constant
1169 (unless args-p
1170 (error "can't :CONSTANT for a result"))
1171 (unless (= (length spec) 2)
1172 (error "bad :CONSTANT argument type spec: ~S" spec))
1173 spec)
1175 (error "bad thing to be a operand type: ~S" spec)))))))
1176 (mapcar #'parse-operand-type specs)))
1178 ;;; Check the consistency of OP's SC restrictions with the specified
1179 ;;; primitive-type restriction. :CONSTANT operands have already been
1180 ;;; filtered out, so only :OR and * restrictions are left.
1182 ;;; We check that every representation allowed by the type can be
1183 ;;; directly loaded into some SC in the restriction, and that the type
1184 ;;; allows every SC in the restriction. With *, we require that T
1185 ;;; satisfy the first test, and omit the second.
1186 (defun check-operand-type-scs (parse op type load-p)
1187 (declare (type vop-parse parse) (type operand-parse op))
1188 (let ((ptypes (if (eq type '*) (list t) (rest type)))
1189 (scs (operand-parse-scs op)))
1190 (when scs
1191 (multiple-value-bind (costs load-scs) (compute-loading-costs op load-p)
1192 (declare (ignore costs))
1193 (dolist (ptype ptypes)
1194 (unless (dolist (rep (primitive-type-scs
1195 (primitive-type-or-lose ptype))
1196 nil)
1197 (when (svref load-scs rep) (return t)))
1198 (error "In the ~A ~:[result~;argument~] to VOP ~S,~@
1199 none of the SCs allowed by the operand type ~S can ~
1200 directly be loaded~@
1201 into any of the restriction's SCs:~% ~S~:[~;~@
1202 [* type operand must allow T's SCs.]~]"
1203 (operand-parse-name op) load-p (vop-parse-name parse)
1204 ptype
1205 scs (eq type '*)))))
1207 (dolist (sc scs)
1208 (unless (or (eq type '*)
1209 (dolist (ptype ptypes nil)
1210 (when (sc-allowed-by-primitive-type
1211 (sc-or-lose sc)
1212 (primitive-type-or-lose ptype))
1213 (return t))))
1214 (warn "~:[Result~;Argument~] ~A to VOP ~S~@
1215 has SC restriction ~S which is ~
1216 not allowed by the operand type:~% ~S"
1217 load-p (operand-parse-name op) (vop-parse-name parse)
1218 sc type)))))
1220 (values))
1222 ;;; If the operand types are specified, then check the number specified
1223 ;;; against the number of defined operands.
1224 (defun check-operand-types (parse ops more-op types load-p)
1225 (declare (type vop-parse parse) (list ops)
1226 (type (or list (member :unspecified)) types)
1227 (type (or operand-parse null) more-op))
1228 (unless (eq types :unspecified)
1229 (let ((num (+ (length ops) (if more-op 1 0))))
1230 (unless (= (count-if-not (lambda (x)
1231 (and (consp x)
1232 (eq (car x) :constant)))
1233 types)
1234 num)
1235 (error "expected ~W ~:[result~;argument~] type~P: ~S"
1236 num load-p types num)))
1238 (when more-op
1239 (let ((mtype (car (last types))))
1240 (when (and (consp mtype) (eq (first mtype) :constant))
1241 (error "can't use :CONSTANT on VOP more args")))))
1243 (when (vop-parse-translate parse)
1244 (let ((types (specify-operand-types types ops more-op)))
1245 (mapc (lambda (x y)
1246 (check-operand-type-scs parse x y load-p))
1247 (if more-op (butlast ops) ops)
1248 (remove-if (lambda (x)
1249 (and (consp x)
1250 (eq (car x) ':constant)))
1251 (if more-op (butlast types) types)))))
1253 (values))
1255 ;;; Compute stuff that can only be computed after we are done parsing
1256 ;;; everying. We set the VOP-PARSE-OPERANDS, and do various error checks.
1257 (defun grovel-vop-operands (parse)
1258 (declare (type vop-parse parse))
1260 (setf (vop-parse-operands parse)
1261 (append (vop-parse-args parse)
1262 (if (vop-parse-more-args parse)
1263 (list (vop-parse-more-args parse)))
1264 (vop-parse-results parse)
1265 (if (vop-parse-more-results parse)
1266 (list (vop-parse-more-results parse)))
1267 (vop-parse-temps parse)))
1269 (check-operand-types parse
1270 (vop-parse-args parse)
1271 (vop-parse-more-args parse)
1272 (vop-parse-arg-types parse)
1275 (check-operand-types parse
1276 (vop-parse-results parse)
1277 (vop-parse-more-results parse)
1278 (vop-parse-result-types parse)
1279 nil)
1281 (values))
1283 ;;;; function translation stuff
1285 ;;; Return forms to establish this VOP as a IR2 translation template
1286 ;;; for the :TRANSLATE functions specified in the VOP-PARSE. We also
1287 ;;; set the PREDICATE attribute for each translated function when the
1288 ;;; VOP is conditional, causing IR1 conversion to ensure that a call
1289 ;;; to the translated is always used in a predicate position.
1290 (defun set-up-fun-translation (parse n-template)
1291 (declare (type vop-parse parse))
1292 (mapcar (lambda (name)
1293 `(let ((info (fun-info-or-lose ',name)))
1294 (setf (fun-info-templates info)
1295 (adjoin-template ,n-template (fun-info-templates info)))
1296 ,@(when (vop-parse-conditional-p parse)
1297 '((setf (fun-info-attributes info)
1298 (attributes-union
1299 (ir1-attributes predicate)
1300 (fun-info-attributes info)))))))
1301 (vop-parse-translate parse)))
1303 ;;; Return a form that can be evaluated to get the TEMPLATE operand type
1304 ;;; restriction from the given specification.
1305 (defun make-operand-type (type)
1306 (cond ((eq type '*) ''*)
1307 ((symbolp type)
1308 ``(:or ,(primitive-type-or-lose ',type)))
1310 (ecase (car type)
1311 (:or
1312 ``(:or ,,@(mapcar (lambda (type)
1313 `(primitive-type-or-lose ',type))
1314 (rest type))))
1315 (:constant
1316 ``(:constant ,(named-lambda (vop-arg-typep) (x)
1317 ;; Can't handle SATISFIES during XC
1318 ,(if (and (consp (second type))
1319 (eq (caadr type) 'satisfies))
1320 `(,(cadadr type) x)
1321 `(sb!xc:typep x ',(second type))))
1322 ,',(second type)))))))
1324 (defun specify-operand-types (types ops more-ops)
1325 (if (eq types :unspecified)
1326 (make-list (+ (length ops) (if more-ops 1 0)) :initial-element '*)
1327 types))
1329 ;;; Return a list of forms to use as &KEY args to MAKE-VOP-INFO for
1330 ;;; setting up the template argument and result types. Here we make an
1331 ;;; initial dummy TEMPLATE-TYPE, since it is awkward to compute the
1332 ;;; type until the template has been made.
1333 (defun make-vop-info-types (parse)
1334 (let* ((more-args (vop-parse-more-args parse))
1335 (all-args (specify-operand-types (vop-parse-arg-types parse)
1336 (vop-parse-args parse)
1337 more-args))
1338 (args (if more-args (butlast all-args) all-args))
1339 (more-arg (when more-args (car (last all-args))))
1340 (more-results (vop-parse-more-results parse))
1341 (all-results (specify-operand-types (vop-parse-result-types parse)
1342 (vop-parse-results parse)
1343 more-results))
1344 (results (if more-results (butlast all-results) all-results))
1345 (more-result (when more-results (car (last all-results))))
1346 (conditional (vop-parse-conditional-p parse)))
1348 `(:type (specifier-type '(function () nil))
1349 :arg-types (list ,@(mapcar #'make-operand-type args))
1350 :more-args-type ,(when more-args (make-operand-type more-arg))
1351 :result-types ,(cond ((eq conditional t)
1352 :conditional)
1353 (conditional
1354 `'(:conditional . ,conditional))
1356 `(list ,@(mapcar #'make-operand-type results))))
1357 :more-results-type ,(when more-results
1358 (make-operand-type more-result)))))
1360 ;;;; setting up VOP-INFO
1362 (eval-when (:compile-toplevel :load-toplevel :execute)
1363 (defparameter *slot-inherit-alist*
1364 '((:generator-function . vop-info-generator-function))))
1366 ;;; This is something to help with inheriting VOP-INFO slots. We
1367 ;;; return a keyword/value pair that can be passed to the constructor.
1368 ;;; SLOT is the keyword name of the slot, Parse is a form that
1369 ;;; evaluates to the VOP-PARSE structure for the VOP inherited. If
1370 ;;; PARSE is NIL, then we do nothing. If the TEST form evaluates to
1371 ;;; true, then we return a form that selects the named slot from the
1372 ;;; VOP-INFO structure corresponding to PARSE. Otherwise, we return
1373 ;;; the FORM so that the slot is recomputed.
1374 (defmacro inherit-vop-info (slot parse test form)
1375 `(if (and ,parse ,test)
1376 (list ,slot `(,',(or (cdr (assoc slot *slot-inherit-alist*))
1377 (error "unknown slot ~S" slot))
1378 (template-or-lose ',(vop-parse-name ,parse))))
1379 (list ,slot ,form)))
1381 ;;; Return a form that creates a VOP-INFO structure which describes VOP.
1382 (defun set-up-vop-info (iparse parse)
1383 (declare (type vop-parse parse) (type (or vop-parse null) iparse))
1384 (let ((same-operands
1385 (and iparse
1386 (equal (vop-parse-operands parse)
1387 (vop-parse-operands iparse))
1388 (equal (vop-parse-info-args iparse)
1389 (vop-parse-info-args parse))))
1390 (variant (vop-parse-variant parse)))
1392 (let ((nvars (length (vop-parse-variant-vars parse))))
1393 (unless (= (length variant) nvars)
1394 (error "expected ~W variant values: ~S" nvars variant)))
1396 `(make-vop-info
1397 :name ',(vop-parse-name parse)
1398 ,@(make-vop-info-types parse)
1399 :guard ,(when (vop-parse-guard parse)
1400 `(lambda () ,(vop-parse-guard parse)))
1401 :note ',(vop-parse-note parse)
1402 :info-arg-count ,(length (vop-parse-info-args parse))
1403 :ltn-policy ',(vop-parse-ltn-policy parse)
1404 :save-p ',(vop-parse-save-p parse)
1405 :move-args ',(vop-parse-move-args parse)
1406 :effects (vop-attributes ,@(vop-parse-effects parse))
1407 :affected (vop-attributes ,@(vop-parse-affected parse))
1408 ,@(make-costs-and-restrictions parse)
1409 ,@(make-emit-function-and-friends parse)
1410 ,@(inherit-vop-info :generator-function iparse
1411 (and same-operands
1412 (equal (vop-parse-body parse) (vop-parse-body iparse)))
1413 (unless (eq (vop-parse-body parse) :unspecified)
1414 (make-generator-function parse)))
1415 :variant (list ,@variant))))
1417 ;;; Define the symbol NAME to be a Virtual OPeration in the compiler.
1418 ;;; If specified, INHERITS is the name of a VOP that we default
1419 ;;; unspecified information from. Each SPEC is a list beginning with a
1420 ;;; keyword indicating the interpretation of the other forms in the
1421 ;;; SPEC:
1423 ;;; :ARGS {(Name {Key Value}*)}*
1424 ;;; :RESULTS {(Name {Key Value}*)}*
1425 ;;; The Args and Results are specifications of the operand TNs passed
1426 ;;; to the VOP. If there is an inherited VOP, any unspecified options
1427 ;;; are defaulted from the inherited argument (or result) of the same
1428 ;;; name. The following operand options are defined:
1430 ;;; :SCs (SC*)
1431 ;;; :SCs specifies good SCs for this operand. Other SCs will
1432 ;;; be penalized according to move costs. A load TN will be
1433 ;;; allocated if necessary, guaranteeing that the operand is
1434 ;;; always one of the specified SCs.
1436 ;;; :LOAD-TN Load-Name
1437 ;;; Load-Name is bound to the load TN allocated for this
1438 ;;; operand, or to NIL if no load TN was allocated.
1440 ;;; :LOAD-IF EXPRESSION
1441 ;;; Controls whether automatic operand loading is done.
1442 ;;; EXPRESSION is evaluated with the fixed operand TNs bound.
1443 ;;; If EXPRESSION is true, then loading is done and the variable
1444 ;;; is bound to the load TN in the generator body. Otherwise,
1445 ;;; loading is not done, and the variable is bound to the actual
1446 ;;; operand.
1448 ;;; :MORE T-or-NIL
1449 ;;; If specified, NAME is bound to the TN-REF for the first
1450 ;;; argument or result following the fixed arguments or results.
1451 ;;; A :MORE operand must appear last, and cannot be targeted or
1452 ;;; restricted.
1454 ;;; :TARGET Operand
1455 ;;; This operand is targeted to the named operand, indicating a
1456 ;;; desire to pack in the same location. Not legal for results.
1458 ;;; :FROM Time-Spec
1459 ;;; :TO Time-Spec
1460 ;;; Specify the beginning or end of the operand's lifetime.
1461 ;;; :FROM can only be used with results, and :TO only with
1462 ;;; arguments. The default for the N'th argument/result is
1463 ;;; (:ARGUMENT N)/(:RESULT N). These options are necessary
1464 ;;; primarily when operands are read or written out of order.
1466 ;;; :CONDITIONAL [Condition-descriptor+]
1467 ;;; This is used in place of :RESULTS with conditional branch VOPs.
1468 ;;; There are no result values: the result is a transfer of control.
1469 ;;; The target label is passed as the first :INFO arg. The second
1470 ;;; :INFO arg is true if the sense of the test should be negated.
1471 ;;; A side effect is to set the PREDICATE attribute for functions
1472 ;;; in the :TRANSLATE option.
1474 ;;; If some condition descriptors are provided, this is a flag-setting
1475 ;;; VOP. Descriptors are interpreted in an architecture-dependent
1476 ;;; manner. See the BRANCH-IF VOP in $ARCH/pred.lisp.
1478 ;;; :TEMPORARY ({Key Value}*) Name*
1479 ;;; Allocate a temporary TN for each Name, binding that variable to
1480 ;;; the TN within the body of the generators. In addition to :TARGET
1481 ;;; (which is is the same as for operands), the following options are
1482 ;;; defined:
1484 ;;; :SC SC-Name
1485 ;;; :OFFSET SB-Offset
1486 ;;; Force the temporary to be allocated in the specified SC
1487 ;;; with the specified offset. Offset is evaluated at
1488 ;;; macroexpand time. If Offset is omitted, the register
1489 ;;; allocator chooses a free location in SC. If both SC and
1490 ;;; Offset are omitted, then the temporary is packed according
1491 ;;; to its primitive type.
1493 ;;; :FROM Time-Spec
1494 ;;; :TO Time-Spec
1495 ;;; Similar to the argument/result option, this specifies the
1496 ;;; start and end of the temporaries' lives. The defaults are
1497 ;;; :LOAD and :SAVE, i.e. the duration of the VOP. The other
1498 ;;; intervening phases are :ARGUMENT, :EVAL and :RESULT.
1499 ;;; Non-zero sub-phases can be specified by a list, e.g. by
1500 ;;; default the second argument's life ends at (:ARGUMENT 1).
1502 ;;; :GENERATOR Cost Form*
1503 ;;; Specifies the translation into assembly code. Cost is the
1504 ;;; estimated cost of the code emitted by this generator. The body
1505 ;;; is arbitrary Lisp code that emits the assembly language
1506 ;;; translation of the VOP. An ASSEMBLE form is wrapped around
1507 ;;; the body, so code may be emitted by using the local INST macro.
1508 ;;; During the evaluation of the body, the names of the operands
1509 ;;; and temporaries are bound to the actual TNs.
1511 ;;; :EFFECTS Effect*
1512 ;;; :AFFECTED Effect*
1513 ;;; Specifies the side effects that this VOP has and the side
1514 ;;; effects that effect its execution. If unspecified, these
1515 ;;; default to the worst case.
1517 ;;; :INFO Name*
1518 ;;; Define some magic arguments that are passed directly to the code
1519 ;;; generator. The corresponding trailing arguments to VOP or
1520 ;;; %PRIMITIVE are stored in the VOP structure. Within the body
1521 ;;; of the generators, the named variables are bound to these
1522 ;;; values. Except in the case of :CONDITIONAL VOPs, :INFO arguments
1523 ;;; cannot be specified for VOPS that are the direct translation
1524 ;;; for a function (specified by :TRANSLATE).
1526 ;;; :IGNORE Name*
1527 ;;; Causes the named variables to be declared IGNORE in the
1528 ;;; generator body.
1530 ;;; :VARIANT Thing*
1531 ;;; :VARIANT-VARS Name*
1532 ;;; These options provide a way to parameterize families of VOPs
1533 ;;; that differ only trivially. :VARIANT makes the specified
1534 ;;; evaluated Things be the "variant" associated with this VOP.
1535 ;;; :VARIANT-VARS causes the named variables to be bound to the
1536 ;;; corresponding Things within the body of the generator.
1538 ;;; :VARIANT-COST Cost
1539 ;;; Specifies the cost of this VOP, overriding the cost of any
1540 ;;; inherited generator.
1542 ;;; :NOTE {String | NIL}
1543 ;;; A short noun-like phrase describing what this VOP "does", i.e.
1544 ;;; the implementation strategy. If supplied, efficiency notes will
1545 ;;; be generated when type uncertainty prevents :TRANSLATE from
1546 ;;; working. NIL inhibits any efficiency note.
1548 ;;; :ARG-TYPES {* | PType | (:OR PType*) | (:CONSTANT Type)}*
1549 ;;; :RESULT-TYPES {* | PType | (:OR PType*)}*
1550 ;;; Specify the template type restrictions used for automatic
1551 ;;; translation. If there is a :MORE operand, the last type is the
1552 ;;; more type. :CONSTANT specifies that the argument must be a
1553 ;;; compile-time constant of the specified Lisp type. The constant
1554 ;;; values of :CONSTANT arguments are passed as additional :INFO
1555 ;;; arguments rather than as :ARGS.
1557 ;;; :TRANSLATE Name*
1558 ;;; This option causes the VOP template to be entered as an IR2
1559 ;;; translation for the named functions.
1561 ;;; :POLICY {:SMALL | :FAST | :SAFE | :FAST-SAFE}
1562 ;;; Specifies the policy under which this VOP is the best translation.
1564 ;;; :GUARD Form
1565 ;;; Specifies a Form that is evaluated in the global environment.
1566 ;;; If form returns NIL, then emission of this VOP is prohibited
1567 ;;; even when all other restrictions are met.
1569 ;;; :VOP-VAR Name
1570 ;;; :NODE-VAR Name
1571 ;;; In the generator, bind the specified variable to the VOP or
1572 ;;; the Node that generated this VOP.
1574 ;;; :SAVE-P {NIL | T | :COMPUTE-ONLY | :FORCE-TO-STACK}
1575 ;;; Indicates how a VOP wants live registers saved.
1577 ;;; :MOVE-ARGS {NIL | :FULL-CALL | :LOCAL-CALL | :KNOWN-RETURN}
1578 ;;; Indicates if and how the more args should be moved into a
1579 ;;; different frame.
1580 (def!macro define-vop ((name &optional inherits) &body specs)
1581 (declare (type symbol name))
1582 ;; Parse the syntax into a VOP-PARSE structure, and then expand into
1583 ;; code that creates the appropriate VOP-INFO structure at load time.
1584 ;; We implement inheritance by copying the VOP-PARSE structure for
1585 ;; the inherited structure.
1586 (let* ((inherited-parse (when inherits
1587 (vop-parse-or-lose inherits)))
1588 (parse (if inherits
1589 (copy-vop-parse inherited-parse)
1590 (make-vop-parse)))
1591 (n-res (gensym)))
1592 (setf (vop-parse-name parse) name)
1593 (setf (vop-parse-inherits parse) inherits)
1595 (parse-define-vop parse specs)
1596 (grovel-vop-operands parse)
1598 `(progn
1599 (eval-when (:compile-toplevel :load-toplevel :execute)
1600 (setf (gethash ',name *backend-parsed-vops*)
1601 ',parse))
1603 (let ((,n-res ,(set-up-vop-info inherited-parse parse)))
1604 (store-vop-info ,n-res)
1605 ,@(set-up-fun-translation parse n-res))
1606 (let ((source-location (source-location)))
1607 (when source-location
1608 (setf (info :source-location :vop ',name) source-location)))
1609 ',name)))
1611 (defun store-vop-info (vop-info)
1612 ;; This is an inefficent way to perform coalescing, but it doesn't matter.
1613 (let* ((my-type-spec (template-type-specifier vop-info))
1614 (my-type (specifier-type my-type-spec)))
1615 (unless (block found
1616 (maphash (lambda (name other)
1617 (declare (ignore name))
1618 ;; we get better coaelesecing by TYPE= rather than
1619 ;; EQUALP on (template-type-specifier vop-info)
1620 ;; because some types have multiple spellings.
1621 (when (type= (vop-info-type other) my-type)
1622 (setf (vop-info-type vop-info) (vop-info-type other))
1623 (return-from found t)))
1624 *backend-template-names*))
1625 (setf (vop-info-type vop-info) (specifier-type my-type-spec))))
1626 (flet ((find-equalp (accessor)
1627 ;; Read the slot from VOP-INFO and try to find any other vop-info
1628 ;; that has an EQUALP value in that slot, returning that value.
1629 ;; Failing that, try again at a finer grain.
1630 (let ((my-val (funcall accessor vop-info))) ; list of vectors
1631 (maphash (lambda (name other)
1632 (declare (ignore name))
1633 (let ((other-val (funcall accessor other)))
1634 (when (equalp other-val my-val)
1635 (return-from find-equalp other-val))))
1636 *backend-template-names*)
1637 (unless (and (listp my-val) (vectorp (car my-val)))
1638 (return-from find-equalp my-val))
1639 (mapl (lambda (cell)
1640 (let ((my-vector (car cell)))
1641 (block found
1642 (maphash (lambda (name other)
1643 (declare (ignore name))
1644 (dolist (other-vector
1645 (funcall accessor other))
1646 (when (equalp other-vector my-vector)
1647 (rplaca cell other-vector)
1648 (return-from found))))
1649 *backend-template-names*))))
1650 (copy-list my-val))))) ; was a quoted constant, don't mutate
1651 (macrolet ((try-coalescing (accessor)
1652 `(setf (,accessor vop-info) (find-equalp #',accessor))))
1653 (try-coalescing vop-info-arg-types)
1654 (try-coalescing vop-info-arg-costs)
1655 (try-coalescing vop-info-arg-load-scs)
1656 (try-coalescing vop-info-result-types)
1657 (try-coalescing vop-info-result-costs)
1658 (try-coalescing vop-info-result-load-scs)
1659 (try-coalescing vop-info-more-arg-costs)
1660 (try-coalescing vop-info-more-result-costs)
1661 (try-coalescing vop-info-temps)
1662 (try-coalescing vop-info-ref-ordering)
1663 (try-coalescing vop-info-targets)))
1664 (setf (gethash (vop-info-name vop-info) *backend-template-names*)
1665 vop-info))
1667 ;;;; emission macros
1669 ;;; Return code to make a list of VOP arguments or results, linked by
1670 ;;; TN-REF-ACROSS. The first value is code, the second value is LET*
1671 ;;; forms, and the third value is a variable that evaluates to the
1672 ;;; head of the list, or NIL if there are no operands. Fixed is a list
1673 ;;; of forms that evaluate to TNs for the fixed operands. TN-REFS will
1674 ;;; be made for these operands according using the specified value of
1675 ;;; WRITE-P. More is an expression that evaluates to a list of TN-REFS
1676 ;;; that will be made the tail of the list. If it is constant NIL,
1677 ;;; then we don't bother to set the tail.
1678 (defun make-operand-list (fixed more write-p)
1679 (collect ((forms)
1680 (binds))
1681 (let ((n-head nil)
1682 (n-prev nil))
1683 (dolist (op fixed)
1684 (let ((n-ref (gensym)))
1685 (binds `(,n-ref (reference-tn ,op ,write-p)))
1686 (if n-prev
1687 (forms `(setf (tn-ref-across ,n-prev) ,n-ref))
1688 (setq n-head n-ref))
1689 (setq n-prev n-ref)))
1691 (when more
1692 (let ((n-more (gensym)))
1693 (binds `(,n-more ,more))
1694 (if n-prev
1695 (forms `(setf (tn-ref-across ,n-prev) ,n-more))
1696 (setq n-head n-more))))
1698 (values (forms) (binds) n-head))))
1700 ;;; Emit-Template Node Block Template Args Results [Info]
1702 ;;; Call the emit function for TEMPLATE, linking the result in at the
1703 ;;; end of BLOCK.
1704 (defmacro emit-template (node block template args results &optional info)
1705 `(emit-and-insert-vop ,node ,block ,template ,args ,results nil
1706 ,@(when info `(,info))))
1708 ;;; VOP Name Node Block Arg* Info* Result*
1710 ;;; Emit the VOP (or other template) NAME at the end of the IR2-BLOCK
1711 ;;; BLOCK, using NODE for the source context. The interpretation of
1712 ;;; the remaining arguments depends on the number of operands of
1713 ;;; various kinds that are declared in the template definition. VOP
1714 ;;; cannot be used for templates that have more-args or more-results,
1715 ;;; since the number of arguments and results is indeterminate for
1716 ;;; these templates. Use VOP* instead.
1718 ;;; ARGS and RESULTS are the TNs that are to be referenced by the
1719 ;;; template as arguments and results. If the template has
1720 ;;; codegen-info arguments, then the appropriate number of INFO forms
1721 ;;; following the arguments are used for codegen info.
1722 (defmacro vop (name node block &rest operands)
1723 (let* ((parse (vop-parse-or-lose name))
1724 (arg-count (length (vop-parse-args parse)))
1725 (result-count (length (vop-parse-results parse)))
1726 (info-count (length (vop-parse-info-args parse)))
1727 (noperands (+ arg-count result-count info-count))
1728 (n-node (gensym))
1729 (n-block (gensym))
1730 (n-template (gensym)))
1732 (when (or (vop-parse-more-args parse) (vop-parse-more-results parse))
1733 (error "cannot use VOP with variable operand count templates"))
1734 (unless (= noperands (length operands))
1735 (error "called with ~W operands, but was expecting ~W"
1736 (length operands) noperands))
1738 (multiple-value-bind (acode abinds n-args)
1739 (make-operand-list (subseq operands 0 arg-count) nil nil)
1740 (multiple-value-bind (rcode rbinds n-results)
1741 (make-operand-list (subseq operands (+ arg-count info-count)) nil t)
1743 (collect ((ibinds)
1744 (ivars))
1745 (dolist (info (subseq operands arg-count (+ arg-count info-count)))
1746 (let ((temp (gensym)))
1747 (ibinds `(,temp ,info))
1748 (ivars temp)))
1750 `(let* ((,n-node ,node)
1751 (,n-block ,block)
1752 (,n-template (template-or-lose ',name))
1753 ,@abinds
1754 ,@(ibinds)
1755 ,@rbinds)
1756 ,@acode
1757 ,@rcode
1758 (emit-template ,n-node ,n-block ,n-template ,n-args
1759 ,n-results
1760 ,@(when (ivars)
1761 `((list ,@(ivars)))))
1762 (values)))))))
1764 ;;; VOP* Name Node Block (Arg* More-Args) (Result* More-Results) Info*
1766 ;;; This is like VOP, but allows for emission of templates with
1767 ;;; arbitrary numbers of arguments, and for emission of templates
1768 ;;; using already-created TN-REF lists.
1770 ;;; The ARGS and RESULTS are TNs to be referenced as the first
1771 ;;; arguments and results to the template. More-Args and More-Results
1772 ;;; are heads of TN-REF lists that are added onto the end of the
1773 ;;; TN-REFS for the explicitly supplied operand TNs. The TN-REFS for
1774 ;;; the more operands must have the TN and WRITE-P slots correctly
1775 ;;; initialized.
1777 ;;; As with VOP, the INFO forms are evaluated and passed as codegen
1778 ;;; info arguments.
1779 (defmacro vop* (name node block args results &rest info)
1780 (declare (type cons args results))
1781 (let* ((parse (vop-parse-or-lose name))
1782 (arg-count (length (vop-parse-args parse)))
1783 (result-count (length (vop-parse-results parse)))
1784 (info-count (length (vop-parse-info-args parse)))
1785 (fixed-args (butlast args))
1786 (fixed-results (butlast results))
1787 (n-node (gensym))
1788 (n-block (gensym))
1789 (n-template (gensym)))
1791 (unless (or (vop-parse-more-args parse)
1792 (<= (length fixed-args) arg-count))
1793 (error "too many fixed arguments"))
1794 (unless (or (vop-parse-more-results parse)
1795 (<= (length fixed-results) result-count))
1796 (error "too many fixed results"))
1797 (unless (= (length info) info-count)
1798 (error "expected ~W info args" info-count))
1800 (multiple-value-bind (acode abinds n-args)
1801 (make-operand-list fixed-args (car (last args)) nil)
1802 (multiple-value-bind (rcode rbinds n-results)
1803 (make-operand-list fixed-results (car (last results)) t)
1805 `(let* ((,n-node ,node)
1806 (,n-block ,block)
1807 (,n-template (template-or-lose ',name))
1808 ,@abinds
1809 ,@rbinds)
1810 ,@acode
1811 ,@rcode
1812 (emit-template ,n-node ,n-block ,n-template ,n-args ,n-results
1813 ,@(when info
1814 `((list ,@info))))
1815 (values))))))
1817 ;;;; miscellaneous macros
1819 ;;; SC-Case TN {({(SC-Name*) | SC-Name | T} Form*)}*
1821 ;;; Case off of TN's SC. The first clause containing TN's SC is
1822 ;;; evaluated, returning the values of the last form. A clause
1823 ;;; beginning with T specifies a default. If it appears, it must be
1824 ;;; last. If no default is specified, and no clause matches, then an
1825 ;;; error is signalled.
1826 (def!macro sc-case (tn &body forms)
1827 (let ((n-sc (gensym))
1828 (n-tn (gensym)))
1829 (collect ((clauses))
1830 (do ((cases forms (rest cases)))
1831 ((null cases)
1832 (clauses `(t (error "unknown SC to SC-CASE for ~S:~% ~S" ,n-tn
1833 (sc-name (tn-sc ,n-tn))))))
1834 (let ((case (first cases)))
1835 (when (atom case)
1836 (error "illegal SC-CASE clause: ~S" case))
1837 (let ((head (first case)))
1838 (when (eq head t)
1839 (when (rest cases)
1840 (error "T case is not last in SC-CASE."))
1841 (clauses `(t nil ,@(rest case)))
1842 (return))
1843 (clauses `((or ,@(mapcar (lambda (x)
1844 `(eql ,(sc-number-or-lose x) ,n-sc))
1845 (if (atom head) (list head) head)))
1846 nil ,@(rest case))))))
1848 `(let* ((,n-tn ,tn)
1849 (,n-sc (sc-number (tn-sc ,n-tn))))
1850 (cond ,@(clauses))))))
1852 ;;; Return true if TNs SC is any of the named SCs, false otherwise.
1853 (defmacro sc-is (tn &rest scs)
1854 (once-only ((n-sc `(sc-number (tn-sc ,tn))))
1855 `(or ,@(mapcar (lambda (x)
1856 `(eql ,n-sc ,(sc-number-or-lose x)))
1857 scs))))
1859 ;;; Iterate over the IR2 blocks in component, in emission order.
1860 (defmacro do-ir2-blocks ((block-var component &optional result)
1861 &body forms)
1862 `(do ((,block-var (block-info (component-head ,component))
1863 (ir2-block-next ,block-var)))
1864 ((null ,block-var) ,result)
1865 ,@forms))
1867 ;;; Iterate over all the TNs live at some point, with the live set
1868 ;;; represented by a local conflicts bit-vector and the IR2-BLOCK
1869 ;;; containing the location.
1870 (defmacro do-live-tns ((tn-var live block &optional result) &body body)
1871 (with-unique-names (conf bod i ltns)
1872 (once-only ((n-live live)
1873 (n-block block))
1874 `(block nil
1875 (flet ((,bod (,tn-var) ,@body))
1876 ;; Do component-live TNs.
1877 (dolist (,tn-var (ir2-component-component-tns
1878 (component-info
1879 (block-component
1880 (ir2-block-block ,n-block)))))
1881 (,bod ,tn-var))
1883 (let ((,ltns (ir2-block-local-tns ,n-block)))
1884 ;; Do TNs always-live in this block and live :MORE TNs.
1885 (do ((,conf (ir2-block-global-tns ,n-block)
1886 (global-conflicts-next-blockwise ,conf)))
1887 ((null ,conf))
1888 (when (or (eq (global-conflicts-kind ,conf) :live)
1889 (let ((,i (global-conflicts-number ,conf)))
1890 (and (eq (svref ,ltns ,i) :more)
1891 (not (zerop (sbit ,n-live ,i))))))
1892 (,bod (global-conflicts-tn ,conf))))
1893 ;; Do TNs locally live in the designated live set.
1894 (dotimes (,i (ir2-block-local-tn-count ,n-block) ,result)
1895 (unless (zerop (sbit ,n-live ,i))
1896 (let ((,tn-var (svref ,ltns ,i)))
1897 (when (and ,tn-var (not (eq ,tn-var :more)))
1898 (,bod ,tn-var)))))))))))
1900 ;;; Iterate over all the IR2 blocks in PHYSENV, in emit order.
1901 (defmacro do-physenv-ir2-blocks ((block-var physenv &optional result)
1902 &body body)
1903 (once-only ((n-physenv physenv))
1904 (once-only ((n-first `(lambda-block (physenv-lambda ,n-physenv))))
1905 (once-only ((n-tail `(block-info
1906 (component-tail
1907 (block-component ,n-first)))))
1908 `(do ((,block-var (block-info ,n-first)
1909 (ir2-block-next ,block-var)))
1910 ((or (eq ,block-var ,n-tail)
1911 (not (eq (ir2-block-physenv ,block-var) ,n-physenv)))
1912 ,result)
1913 ,@body)))))