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