/[cmucl]/src/compiler/macros.lisp
ViewVC logotype

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (show annotations)
Fri Apr 11 15:28:11 2003 UTC (11 years ago) by emarsden
Branch: MAIN
Changes since 1.49: +1 -2 lines
 - the :new-compiler and :hash-new features were announced as being
   deprecated in 18e; remove them and make code that depended on them
   enabled unconditionally.

 - if CMUCL is built with the :no-docstrings feature, docstrings are
   discarded while building. This is intended for embedded-type images.
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/macros.lisp,v 1.50 2003/04/11 15:28:11 emarsden Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Random types and macros used in writing the compiler.
13 ;;;
14 ;;; Written by Rob MacLachlan
15 ;;;
16 (in-package "C")
17
18 (export '(lisp::with-compilation-unit) "LISP")
19
20 (export '(policy symbolicate def-ir1-translator def-source-transform
21 def-primitive-translator deftransform defknown defoptimizer
22 derive-type optimizer ltn-annotate ir2-convert attributes
23 def-boolean-attribute attributes-union attributes-intersection
24 attributes=))
25
26 (declaim (special *wild-type* *universal-type* *compiler-error-context*))
27
28 ;;;; Deftypes:
29
30 ;;;
31 ;;; Inlinep is used to determine how a function is called. The values have
32 ;;; these meanings:
33 ;;; Nil No declaration seen: do whatever you feel like, but don't dump
34 ;;; an inline expansion.
35 ;;;
36 ;;; :Notinline Notinline declaration seen: always do full function call.
37 ;;;
38 ;;; :Inline Inline declaration seen: save expansion, expanding to it if
39 ;;; policy favors.
40 ;;;
41 ;;; :Maybe-Inline
42 ;;; Retain expansion, but only use it opportunistically.
43 ;;;
44 (deftype inlinep () '(member :inline :maybe-inline :notinline nil))
45
46
47 ;;;; The Policy macro:
48
49 (declaim (special *lexical-environment*))
50
51 (eval-when (compile load eval)
52 (defconstant policy-parameter-slots
53 '((speed . cookie-speed) (space . cookie-space) (safety . cookie-safety)
54 (cspeed . cookie-cspeed) (brevity . cookie-brevity)
55 (debug . cookie-debug)))
56
57 ;;; Find-Used-Parameters -- Internal
58 ;;;
59 ;;; Find all the policy parameters which are actually mentioned in Stuff,
60 ;;; returning the names in a list. We assume everything is evaluated.
61 ;;;
62 (defun find-used-parameters (stuff)
63 (if (atom stuff)
64 (if (assoc stuff policy-parameter-slots) (list stuff) ())
65 (collect ((res () nunion))
66 (dolist (arg (cdr stuff) (res))
67 (res (find-used-parameters arg))))))
68
69 ); Eval-When (Compile Load Eval)
70
71 ;;; Policy -- Public
72 ;;;
73 ;;; This macro provides some syntactic sugar for querying the settings of
74 ;;; the compiler policy parameters.
75 ;;;
76 (defmacro policy (node &rest conditions)
77 "Policy Node Condition*
78 Test whether some conditions apply to the current compiler policy for Node.
79 Each condition is a predicate form which accesses the policy values by
80 referring to them as the variables SPEED, SPACE, SAFETY, CSPEED, BREVITY and
81 DEBUG. The results of all the conditions are combined with AND and returned
82 as the result.
83
84 Node is a form which is evaluated to obtain the node which the policy is for.
85 If Node is NIL, then we use the current policy as defined by *default-cookie*
86 and *current-cookie*. This option is only well defined during IR1
87 conversion."
88 (let* ((form `(and ,@conditions))
89 (n-cookie (gensym))
90 (binds (mapcar
91 #'(lambda (name)
92 (let ((slot (cdr (assoc name policy-parameter-slots))))
93 `(,name (,slot ,n-cookie))))
94 (find-used-parameters form))))
95 `(let* ((,n-cookie (lexenv-cookie
96 ,(if node
97 `(node-lexenv ,node)
98 '*lexical-environment*)))
99 ,@binds)
100 ,form)))
101
102
103 ;;;; Source-hacking defining forms:
104
105 (eval-when (compile load eval)
106
107 ;;; Symbolicate -- Interface
108 ;;;
109 ;;; Concatenate together the names of some strings and symbols, producing
110 ;;; a symbol in the current package.
111 ;;;
112 (declaim (function symbolicate (&rest (or string symbol)) symbol))
113 (defun symbolicate (&rest things)
114 (declare (values symbol))
115 (values (intern (reduce #'(lambda (x y)
116 (concatenate 'string (string x) (string y)))
117 things))))
118
119 ); Eval-When (Compile Load Eval)
120
121 ;;; SPECIAL-FORM-FUNCTION -- Internal
122 ;;;
123 ;;; This function is stored in the SYMBOL-FUNCTION of special form names so
124 ;;; that they are FBOUND.
125 ;;;
126 (defun special-form-function (&rest stuff)
127 (declare (ignore stuff))
128 (error 'simple-undefined-function
129 :format-control "Can't funcall the SYMBOL-FUNCTION of special forms."))
130
131 ;;; CONVERT-CONDITION-INTO-COMPILER-ERROR -- Internal
132 ;;;
133 ;;; Passed to parse-defmacro when we want compiler errors instead of real
134 ;;; errors.
135 ;;;
136 (declaim (inline convert-condition-into-compiler-error))
137 (defun convert-condition-into-compiler-error (datum &rest stuff)
138 (if (stringp datum)
139 (apply #'compiler-error datum stuff)
140 (compiler-error "~A"
141 (if (symbolp datum)
142 (apply #'make-condition datum stuff)
143 datum))))
144
145 ;;; Def-IR1-Translator -- Interface
146 ;;;
147 ;;; Parse defmacro style lambda-list, setting things up so that a compiler
148 ;;; error happens if the syntax is invalid.
149 ;;;
150 (defmacro def-ir1-translator (name (lambda-list start-var cont-var
151 &key (kind :special-form))
152 &body body)
153 "Def-IR1-Translator Name (Lambda-List Start-Var Cont-Var {Key Value}*)
154 [Doc-String] Form*
155 Define a function that converts a Special-Form or other magical thing into
156 IR1. Lambda-List is a defmacro style lambda list. Start-Var and Cont-Var
157 are bound to the start and result continuations for the resulting IR1.
158 This keyword is defined:
159 Kind
160 The function kind to associate with Name (default :special-form)."
161 (let ((fn-name (symbolicate "IR1-CONVERT-" name))
162 (n-form (gensym))
163 (n-env (gensym)))
164 (multiple-value-bind
165 (body decls doc)
166 (lisp::parse-defmacro lambda-list n-form body name "special form"
167 :doc-string-allowed t
168 :environment n-env
169 :error-fun 'convert-condition-into-compiler-error)
170 `(progn
171 (declaim (function ,fn-name (continuation continuation t) void))
172 (defun ,fn-name (,start-var ,cont-var ,n-form)
173 (let ((,n-env *lexical-environment*))
174 ,@decls
175 ,body))
176 ,@(when doc
177 `((setf (documentation ',name 'function) ,doc)))
178 (setf (info function ir1-convert ',name) #',fn-name)
179 (setf (info function kind ',name) ,kind)
180 ,@(when (eq kind :special-form)
181 `((setf (symbol-function ',name) #'special-form-function)))))))
182
183
184 ;;; Def-Source-Transform -- Interface
185 ;;;
186 ;;; Similar to Def-IR1-Translator, except that we pass if the syntax is
187 ;;; invalid.
188 ;;;
189 (defmacro def-source-transform (name lambda-list &body body)
190 "Def-Source-Transform Name Lambda-List Form*
191 Define a macro-like source-to-source transformation for the function Name.
192 A source transform may \"pass\" by returning a non-nil second value. If the
193 transform passes, then the form is converted as a normal function call. If
194 the supplied arguments are not compatible with the specified lambda-list,
195 then the transform automatically passes.
196
197 Source-Transforms may only be defined for functions. Source transformation
198 is not attempted if the function is declared Notinline. Source transforms
199 should not examine their arguments. If it matters how the function is used,
200 then Deftransform should be used to define an IR1 transformation.
201
202 If the desirability of the transformation depends on the current Optimize
203 parameters, then the Policy macro should be used to determine when to pass."
204 (let ((fn-name
205 (if (listp name)
206 (collect ((pieces))
207 (dolist (piece name)
208 (pieces "-")
209 (pieces piece))
210 (apply #'symbolicate "SOURCE-TRANSFORM" (pieces)))
211 (symbolicate "SOURCE-TRANSFORM-" name)))
212 (n-form (gensym))
213 (n-env (gensym)))
214 (multiple-value-bind
215 (body decls)
216 (lisp::parse-defmacro lambda-list n-form body name "form"
217 :environment n-env
218 :error-fun `(lambda (&rest stuff)
219 (declare (ignore stuff))
220 (return-from ,fn-name
221 (values nil t))))
222 `(progn
223 (defun ,fn-name (,n-form)
224 (let ((,n-env *lexical-environment*))
225 ,@decls
226 ,body))
227 (setf (info function source-transform ',name) #',fn-name)))))
228
229
230 (defmacro def-primitive-translator (name lambda-list &body body)
231 "Def-Primitive-Translator Name Lambda-List Form*
232 Define a function that converts a use of (%PRIMITIVE Name ...) into Lisp
233 code. Lambda-List is a defmacro style lambda list."
234 (let ((fn-name (symbolicate "PRIMITIVE-TRANSLATE-" name))
235 (n-form (gensym))
236 (n-env (gensym)))
237 (multiple-value-bind
238 (body decls)
239 (lisp::parse-defmacro lambda-list n-form body name "%primitive"
240 :environment n-env
241 :error-fun 'convert-condition-into-compiler-error)
242 `(progn
243 (defun ,fn-name (,n-form)
244 (let ((,n-env *lexical-environment*))
245 ,@decls
246 ,body))
247 (setf (gethash ',name *primitive-translators*) ',fn-name)))))
248
249
250 ;;;; Lambda-list parsing utilities:
251 ;;;
252 ;;; IR1 transforms, optimizers and type inferencers need to be able to parse
253 ;;; the IR1 representation of a function call using a standard function
254 ;;; lambda-list.
255
256
257 (eval-when (compile load eval)
258
259 ;;; Parse-Deftransform -- Internal
260 ;;;
261 ;;; Given a deftransform style lambda-list, generate code that parses the
262 ;;; arguments of a combination with respect to that lambda-list. Body is the
263 ;;; the list of forms which are to be evaluated within the bindings. Args is
264 ;;; the variable that holds list of argument continuations. Error-Form is a
265 ;;; form which is evaluated when the syntax of the supplied arguments is
266 ;;; incorrect or a non-constant argument keyword is supplied. Defaults and
267 ;;; other gunk are ignored. The second value is a list of all the arguments
268 ;;; bound. We make the variables IGNORABLE so that we don't have to manually
269 ;;; declare them Ignore if their only purpose is to make the syntax work.
270 ;;;
271 (defun parse-deftransform (lambda-list body args error-form)
272 (declare (list lambda-list body) (symbol args))
273 (multiple-value-bind (req opt restp rest keyp keys allowp)
274 (parse-lambda-list lambda-list)
275 (let* ((min-args (length req))
276 (max-args (+ min-args (length opt)))
277 (n-keys (gensym)))
278 (collect ((binds)
279 (vars)
280 (pos 0 +)
281 (keywords))
282 (dolist (arg req)
283 (vars arg)
284 (binds `(,arg (nth ,(pos) ,args)))
285 (pos 1))
286
287 (dolist (arg opt)
288 (let ((var (if (atom arg) arg (first arg))))
289 (vars var)
290 (binds `(,var (nth ,(pos) ,args)))
291 (pos 1)))
292
293 (when restp
294 (vars rest)
295 (binds `(,rest (nthcdr ,(pos) ,args))))
296
297 (dolist (spec keys)
298 (if (or (atom spec) (atom (first spec)))
299 (let* ((var (if (atom spec) spec (first spec)))
300 (key (intern (symbol-name var) "KEYWORD")))
301 (vars var)
302 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
303 (keywords key))
304 (let* ((head (first spec))
305 (var (second head))
306 (key (first head)))
307 (vars var)
308 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
309 (keywords key))))
310
311 (let ((n-length (gensym))
312 (limited-legal (not (or restp keyp))))
313 (values
314 `(let ((,n-length (length ,args))
315 ,@(when keyp `((,n-keys (nthcdr ,(pos) ,args)))))
316 (unless (and
317 ,(if limited-legal
318 `(<= ,min-args ,n-length ,max-args)
319 `(<= ,min-args ,n-length))
320 ,@(when keyp
321 (if allowp
322 `((check-keywords-constant ,n-keys))
323 `((check-transform-keys ,n-keys ',(keywords))))))
324 ,error-form)
325 (let ,(binds)
326 (declare (ignorable ,@(vars)))
327 ,@body))
328 (vars)))))))
329
330 ); Eval-When (Compile Load Eval)
331
332
333 ;;;; Deftransform:
334
335 ;;; Deftransform -- Interface
336 ;;;
337 ;;; Parse the lambda-list and generate code to test the policy and
338 ;;; automatically create the result lambda.
339 ;;;
340 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
341 (result-type '*)
342 &key result policy node defun-only
343 eval-name important (when :native))
344 &body (body decls doc))
345 "Deftransform Name (Lambda-List [Arg-Types] [Result-Type] {Key Value}*)
346 Declaration* [Doc-String] Form*
347 Define an IR1 transformation for Name. An IR1 transformation computes a
348 lambda that replaces the function variable reference for the call. A
349 transform may pass (decide not to transform the call) by calling the Give-Up
350 function. Lambda-List both determines how the current call is parsed and
351 specifies the Lambda-List for the resulting lambda.
352
353 We parse the call and bind each of the lambda-list variables to the
354 continuation which represents the value of the argument. When parsing the
355 call, we ignore the defaults, and always bind the variables for unsupplied
356 arguments to NIL. If a required argument is missing, an unknown keyword is
357 supplied, or an argument keyword is not a constant, then the transform
358 automatically passes. The Declarations apply to the bindings made by
359 Deftransform at transformation time, rather than to the variables of the
360 resulting lambda. Bound-but-not-referenced warnings are suppressed for the
361 lambda-list variables. The Doc-String is used when printing efficiency notes
362 about the defined transform.
363
364 Normally, the body evaluates to a form which becomes the body of an
365 automatically constructed lambda. We make Lambda-List the lambda-list for
366 the lambda, and automatically insert declarations of the argument and result
367 types. If the second value of the body is non-null, then it is a list of
368 declarations which are to be inserted at the head of the lambda. Automatic
369 lambda generation may be inhibited by explicitly returning a lambda from the
370 body.
371
372 The Arg-Types and Result-Type are used to create a function type which the
373 call must satisfy before transformation is attempted. The function type
374 specifier is constructed by wrapping (FUNCTION ...) around these values, so
375 the lack of a restriction may be specified by omitting the argument or
376 supplying *. The argument syntax specified in the Arg-Types need not be the
377 same as that in the Lambda-List, but the transform will never happen if
378 the syntaxes can't be satisfied simultaneously. If there is an existing
379 transform for the same function that has the same type, then it is replaced
380 with the new definition.
381
382 These are the legal keyword options:
383 :Result - A variable which is bound to the result continuation.
384 :Node - A variable which is bound to the combination node for the call.
385 :Policy - A form which is supplied to the Policy macro to determine whether
386 this transformation is appropriate. If the result is false, then
387 the transform automatically passes.
388 :Eval-Name
389 - The name and argument/result types are actually forms to be
390 evaluated. Useful for getting closures that transform similar
391 functions.
392 :Defun-Only
393 - Don't actually instantiate a transform, instead just DEFUN
394 Name with the specified transform definition function. This may
395 be later instantiated with %Deftransform.
396 :Important
397 - If supplied and non-NIL, note this transform as ``important,''
398 which means effeciency notes will be generated when this
399 transform fails even if brevity=speed (but not if brevity>speed)
400 :When {:Native | :Byte | :Both}
401 - Indicates whether this transform applies to native code,
402 byte-code or both (default :native.)"
403
404 (when (and eval-name defun-only)
405 (error "Can't specify both DEFUN-ONLY and EVAL-NAME."))
406 (let ((n-args (gensym))
407 (n-node (or node (gensym)))
408 (n-decls (gensym))
409 (n-lambda (gensym))
410 (body `(,@decls ,@body)))
411 (multiple-value-bind (parsed-form vars)
412 (parse-deftransform
413 lambda-list
414 (if policy
415 `((unless (policy ,n-node ,policy) (give-up))
416 ,@body)
417 body)
418 n-args '(give-up))
419 (let ((stuff
420 `((,n-node)
421 (let* ((,n-args (basic-combination-args ,n-node))
422 ,@(when result
423 `((,result (node-cont ,n-node)))))
424 (multiple-value-bind (,n-lambda ,n-decls)
425 ,parsed-form
426 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
427 ,n-lambda
428 `(lambda ,',lambda-list
429 (declare (ignorable ,@',vars))
430 ,@,n-decls
431 ,,n-lambda)))))))
432 (if defun-only
433 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
434 `(%deftransform
435 ,(if eval-name name `',name)
436 ,(if eval-name
437 ``(function ,,arg-types ,,result-type)
438 `'(function ,arg-types ,result-type))
439 #'(lambda ,@stuff)
440 ,doc
441 ,(if important t nil)
442 ,when))))))
443
444 ;;;; Defknown, Defoptimizer:
445
446 ;;; Defknown -- Interface
447 ;;;
448 ;;; This macro should be the way that all implementation independent
449 ;;; information about functions is made known to the compiler.
450 ;;;
451 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
452 &rest keys)
453 "Defknown Name Arg-Types Result-Type [Attributes] {Key Value}*
454 Declare the function Name to be a known function. We construct a type
455 specifier for the function by wrapping (FUNCTION ...) around the Arg-Types
456 and Result-Type. Attributes is a an unevaluated list of the boolean
457 attributes that the function has. These attributes are meaningful here:
458 call
459 May call functions that are passed as arguments. In order to determine
460 what other effects are present, we must find the effects of all arguments
461 that may be functions.
462
463 unsafe
464 May incorporate arguments in the result or somehow pass them upward.
465
466 unwind
467 May fail to return during correct execution. Errors are O.K.
468
469 any
470 The (default) worst case. Includes all the other bad things, plus any
471 other possible bad thing.
472
473 foldable
474 May be constant-folded. The function has no side effects, but may be
475 affected by side effects on the arguments. e.g. SVREF, MAPC.
476
477 flushable
478 May be eliminated if value is unused. The function has no side effects
479 except possibly CONS. If a function is defined to signal errors, then
480 it is not flushable even if it is movable or foldable.
481
482 movable
483 May be moved with impunity. Has no side effects except possibly CONS,
484 and is affected only by its arguments.
485
486 predicate
487 A true predicate likely to be open-coded. This is a hint to IR1
488 conversion that it should ensure calls always appear as an IF test.
489 Not usually specified to Defknown, since this is implementation
490 dependent, and is usually automatically set by the Define-VOP
491 :Conditional option.
492
493 Name may also be a list of names, in which case the same information is given
494 to all the names. The keywords specify the initial values for various
495 optimizers that the function might have."
496 (when (and (intersection attributes '(any call unwind))
497 (intersection attributes '(movable)))
498 (error "Function cannot have both good and bad attributes: ~S" attributes))
499
500 `(%defknown ',(if (and (consp name)
501 (not (eq (car name) 'setf)))
502 name
503 (list name))
504 '(function ,arg-types ,result-type)
505 (ir1-attributes ,@(if (member 'any attributes)
506 (union '(call unsafe unwind) attributes)
507 attributes))
508 ,@keys))
509
510
511 ;;; Defoptimizer -- Interface
512 ;;;
513 ;;; Create a function which parses combination args according to a
514 ;;; Lambda-List, optionally storing it in a function-info slot.
515 ;;;
516 (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
517 &rest vars)
518 &body body)
519 "Defoptimizer (Function Kind) (Lambda-List [Node-Var] Var*)
520 Declaration* Form*
521 Define some Kind of optimizer for the named Function. Function must be a
522 known function. Lambda-List is used to parse the arguments to the
523 combination as in Deftransform. If the argument syntax is invalid or there
524 are non-constant keys, then we simply return NIL.
525
526 The function is DEFUN'ed as Function-Kind-OPTIMIZER. Possible kinds are
527 DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If a symbol is
528 specified instead of a (Function Kind) list, then we just do a DEFUN with the
529 symbol as its name, and don't do anything with the definition. This is
530 useful for creating optimizers to be passed by name to DEFKNOWN.
531
532 If supplied, Node-Var is bound to the combination node being optimized. If
533 additional Vars are supplied, then they are used as the rest of the optimizer
534 function's lambda-list. LTN-ANNOTATE methods are passed an additional POLICY
535 argument, and IR2-CONVERT methods are passed an additional IR2-BLOCK
536 argument."
537
538 (let ((name (if (symbolp what) what
539 (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
540
541 (let ((n-args (gensym)))
542 `(progn
543 (defun ,name (,n-node ,@vars)
544 (let ((,n-args (basic-combination-args ,n-node)))
545 ,(parse-deftransform lambda-list body n-args
546 `(return-from ,name nil))))
547 ,@(when (consp what)
548 `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
549 (function-info-or-lose ',(first what)))
550 #',name)))))))
551
552
553 ;;;; IR groveling macros:
554
555 ;;; Do-Blocks, Do-Blocks-Backwards -- Interface
556 ;;;
557 (defmacro do-blocks ((block-var component &optional ends result) &body body)
558 "Do-Blocks (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
559 Iterate over the blocks in a component, binding Block-Var to each block in
560 turn. The value of Ends determines whether to iterate over dummy head and
561 tail blocks:
562 NIL -- Skip Head and Tail (the default)
563 :Head -- Do head but skip tail
564 :Tail -- Do tail but skip head
565 :Both -- Do both head and tail
566
567 If supplied, Result-Form is the value to return."
568 (unless (member ends '(nil :head :tail :both))
569 (error "Losing Ends value: ~S." ends))
570 (let ((n-component (gensym))
571 (n-tail (gensym)))
572 `(let* ((,n-component ,component)
573 (,n-tail ,(if (member ends '(:both :tail))
574 nil
575 `(component-tail ,n-component))))
576 (do ((,block-var ,(if (member ends '(:both :head))
577 `(component-head ,n-component)
578 `(block-next (component-head ,n-component)))
579 (block-next ,block-var)))
580 ((eq ,block-var ,n-tail) ,result)
581 ,@body))))
582 ;;;
583 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
584 "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
585 Like Do-Blocks, only iterate over the blocks in reverse order."
586 (unless (member ends '(nil :head :tail :both))
587 (error "Losing Ends value: ~S." ends))
588 (let ((n-component (gensym))
589 (n-head (gensym)))
590 `(let* ((,n-component ,component)
591 (,n-head ,(if (member ends '(:both :head))
592 nil
593 `(component-head ,n-component))))
594 (do ((,block-var ,(if (member ends '(:both :tail))
595 `(component-tail ,n-component)
596 `(block-prev (component-tail ,n-component)))
597 (block-prev ,block-var)))
598 ((eq ,block-var ,n-head) ,result)
599 ,@body))))
600
601
602 ;;; Do-Uses -- Interface
603 ;;;
604 ;;; Could change it not to replicate the code someday perhaps...
605 ;;;
606 (defmacro do-uses ((node-var continuation &optional result) &body body)
607 "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
608 Iterate over the uses of Continuation, binding Node to each one succesively."
609 (once-only ((n-cont continuation))
610 `(ecase (continuation-kind ,n-cont)
611 (:unused)
612 (:inside-block
613 (block nil
614 (let ((,node-var (continuation-use ,n-cont)))
615 ,@body
616 ,result)))
617 ((:block-start :deleted-block-start)
618 (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
619 ,result)
620 ,@body)))))
621
622
623 ;;; Do-Nodes, Do-Nodes-Backwards -- Interface
624 ;;;
625 ;;; In the forward case, we terminate on Last-Cont so that we don't have to
626 ;;; worry about our termination condition being changed when new code is added
627 ;;; during the iteration. In the backward case, we do NODE-PREV before
628 ;;; evaluating the body so that we can keep going when the current node is
629 ;;; deleted.
630 ;;;
631 ;;; When Restart-P is supplied to DO-NODES, we start iterating over again at
632 ;;; the beginning of the block when we run into a continuation whose block
633 ;;; differs from the one we are trying to iterate over, either beacuse the
634 ;;; block was split, or because a node was deleted out from under us (hence its
635 ;;; block is NIL.) If the block start is deleted, we just punt. With
636 ;;; Restart-P, we are also more careful about termination, re-indirecting the
637 ;;; BLOCK-LAST each time.
638 ;;;
639 (defmacro do-nodes ((node-var cont-var block &key restart-p) &body body)
640 "Do-Nodes (Node-Var Cont-Var Block {Key Value}*) {Declaration}* {Form}*
641 Iterate over the nodes in Block, binding Node-Var to the each node and
642 Cont-Var to the node's Cont. The only keyword option is Restart-P, which
643 causes iteration to be restarted when a node is deleted out from under us (if
644 not supplied, this is an error.)"
645 (let ((n-block (gensym))
646 (n-last-cont (gensym)))
647 `(let* ((,n-block ,block)
648 ,@(unless restart-p
649 `((,n-last-cont (node-cont (block-last ,n-block))))))
650 (do* ((,node-var (continuation-next (block-start ,n-block))
651 ,(if restart-p
652 `(cond
653 ((eq (continuation-block ,cont-var) ,n-block)
654 (assert (continuation-next ,cont-var))
655 (continuation-next ,cont-var))
656 (t
657 (let ((start (block-start ,n-block)))
658 (unless (eq (continuation-kind start)
659 :block-start)
660 (return nil))
661 (continuation-next start))))
662 `(continuation-next ,cont-var)))
663 (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
664 (())
665 ,@body
666 (when ,(if restart-p
667 `(eq ,node-var (block-last ,n-block))
668 `(eq ,cont-var ,n-last-cont))
669 (return nil))))))
670 ;;;
671 (defmacro do-nodes-backwards ((node-var cont-var block) &body body)
672 "Do-Nodes-Backwards (Node-Var Cont-Var Block) {Declaration}* {Form}*
673 Like Do-Nodes, only iterates in reverse order."
674 (let ((n-block (gensym))
675 (n-start (gensym))
676 (n-last (gensym))
677 (n-next (gensym)))
678 `(let* ((,n-block ,block)
679 (,n-start (block-start ,n-block))
680 (,n-last (block-last ,n-block)))
681 (do* ((,cont-var (node-cont ,n-last) ,n-next)
682 (,node-var ,n-last (continuation-use ,cont-var))
683 (,n-next (node-prev ,node-var) (node-prev ,node-var)))
684 (())
685 ,@body
686 (when (eq ,n-next ,n-start)
687 (return nil))))))
688
689
690 ;;; With-IR1-Environment -- Interface
691 ;;;
692 ;;; The lexical environment is presumably already null...
693 ;;;
694 (defmacro with-ir1-environment (node &rest forms)
695 "With-IR1-Environment Node Form*
696 Bind the IR1 context variables so that IR1 conversion can be done after the
697 main conversion pass has finished."
698 (let ((n-node (gensym)))
699 `(let* ((,n-node ,node)
700 (*current-component* (block-component (node-block ,n-node)))
701 (*lexical-environment* (node-lexenv ,n-node))
702 (*current-path* (node-source-path ,n-node)))
703 ,@forms)))
704
705
706 ;;; WITH-IR1-NAMESPACE -- Interface
707 ;;;
708 ;;; Bind the hashtables used for keeping track of global variables,
709 ;;; functions, &c. Also establish condition handlers.
710 ;;;
711 (defmacro with-ir1-namespace (&body forms)
712 `(let ((*free-variables* (make-hash-table :test #'eq))
713 (*free-functions* (make-hash-table :test #'equal))
714 (*constants* (make-hash-table :test #'equal))
715 (*coalesce-constants* t)
716 (*source-paths* (make-hash-table :test #'eq)))
717 (handler-bind ((compiler-error #'compiler-error-handler)
718 (style-warning #'compiler-style-warning-handler)
719 (warning #'compiler-warning-handler))
720 ,@forms)))
721
722
723 ;;; LEXENV-FIND -- Interface
724 ;;;
725 (defmacro lexenv-find (name slot &key test)
726 "LEXENV-FIND Name Slot {Key Value}*
727 Look up Name in the lexical environment namespace designated by Slot,
728 returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
729 may be used to determine the name equality predicate."
730 (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot)
731 *lexical-environment*)
732 :test ,(or test '#'eq))))
733 `(if ,n-res
734 (values (cdr ,n-res) t)
735 (values nil nil))))
736
737 ;;;
738 ;;; LEXENV-FIND-FUNCTION -- Interface
739 ;;;
740 ;;; Find local function with name NAME in *LEXICAL-ENVIRONMENT*.
741 ;;;
742 (defun lexenv-find-function (name)
743 (lexenv-find name functions
744 :test (lambda (x y)
745 (or (equal x y)
746 (and (consp y)
747 (member (car y) '(flet labels))
748 (equal x (cadr y)))))))
749
750
751
752 ;;; With-debug-counters -- Interface
753 ;;;
754 ;;; Bind the hashtables and counters used for keeping track of
755 ;;; continuation, TN, and label IDs for the debug dumping routines.
756 ;;;
757 (defmacro with-debug-counters (&body forms)
758 `(let ((*continuation-numbers* (make-hash-table :test #'eq))
759 (*number-continuations* (make-hash-table :test #'eql))
760 (*continuation-number* 0)
761 (*tn-ids* (make-hash-table :test #'eq))
762 (*id-tns* (make-hash-table :test #'eql))
763 (*tn-id* 0)
764 (*id-labels* (make-hash-table :test #'eq))
765 (*label-ids* (make-hash-table :test #'eql))
766 (*label-id* 0))
767 ,@forms))
768
769
770 ;;;; The Defprinter macro:
771
772 (defvar *defprint-pretty* nil
773 "If true, defprinter print functions print each slot on a separate line.")
774
775
776 ;;; Defprinter-Prin1, Defprinter-Princ -- Internal
777 ;;;
778 ;;; These functions are called by the expansion of the Defprinter
779 ;;; macro to do the actual printing.
780 ;;;
781 (defun defprinter-prin1 (name value stream &optional indent)
782 (declare (symbol name) (stream stream) (ignore indent))
783 (write-string " " stream)
784 (when *print-pretty*
785 (pprint-newline :linear stream))
786 (princ name stream)
787 (write-string "= " stream)
788 (prin1 value stream))
789 ;;;
790 (defun defprinter-princ (name value stream &optional indent)
791 (declare (symbol name) (stream stream) (ignore indent))
792 (write-string " " stream)
793 (when *print-pretty*
794 (pprint-newline :linear stream))
795 (princ name stream)
796 (write-string "= " stream)
797 (princ value stream))
798
799 (defmacro defprinter (name &rest slots)
800 "Defprinter Name Slot-Desc*
801 Define some kind of reasonable defstruct structure-print function. Name
802 is the name of the structure. We define a function %PRINT-name which
803 prints the slots in the structure in the way described by the Slot-Descs.
804 Each Slot-Desc can be a slot name, indicating that the slot should simply
805 be printed. A Slot-Desc may also be a list of a slot name and other stuff.
806 The other stuff is composed of keywords followed by expressions. The
807 expressions are evaluated with the variable which is the slot name bound
808 to the value of the slot. These keywords are defined:
809
810 :PRIN1 Print the value of the expression instead of the slot value.
811 :PRINC Like :PRIN1, only princ the value
812 :TEST Only print something if the test is true.
813
814 If no printing thing is specified then the slot value is printed as PRIN1.
815
816 The structure being printed is bound to Structure and the stream is bound to
817 Stream."
818
819 (flet ((sref (slot) `(,(symbolicate name "-" slot) structure)))
820 (collect ((prints))
821 (dolist (slot slots)
822 (if (atom slot)
823 (prints `(defprinter-prin1 ',slot ,(sref slot) stream))
824 (let ((sname (first slot))
825 (test t))
826 (collect ((stuff))
827 (do ((option (rest slot) (cddr option)))
828 ((null option)
829 (prints
830 `(let ((,sname ,(sref sname)))
831 (when ,test
832 ,@(or (stuff)
833 `((defprinter-prin1 ',sname ,sname
834 stream)))))))
835 (case (first option)
836 (:prin1
837 (stuff `(defprinter-prin1 ',sname ,(second option)
838 stream)))
839 (:princ
840 (stuff `(defprinter-princ ',sname ,(second option)
841 stream)))
842 (:test (setq test (second option)))
843 (t
844 (error "Losing Defprinter option: ~S."
845 (first option)))))))))
846
847 `(defun ,(symbolicate "%PRINT-" name) (structure stream depth)
848 (flet ((do-prints (stream)
849 (declare (ignorable stream))
850 ,@(prints)))
851 (cond (*print-readably*
852 (error "~S cannot be printed readably." structure))
853 ((and *print-level* (>= depth *print-level*))
854 (format stream "#<~S ~X>"
855 ',name
856 (get-lisp-obj-address structure)))
857 (*print-pretty*
858 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
859 (pprint-indent :current 2 stream)
860 (prin1 ',name stream)
861 (write-char #\space stream)
862 (let ((*print-base* 16)
863 (*print-radix* t))
864 (prin1 (get-lisp-obj-address structure) stream))
865 (do-prints stream)))
866 (t
867 (descend-into (stream)
868 (format stream "#<~S ~X"
869 ',name
870 (get-lisp-obj-address structure))
871 (do-prints stream)
872 (format stream ">")))))
873 nil))))
874
875
876 ;;;; Boolean attribute utilities:
877 ;;;
878 ;;; We need to maintain various sets of boolean attributes for known
879 ;;; functions and VOPs. To save space and allow for quick set operations, we
880 ;;; represent them as bits in a fixnum.
881 ;;;
882
883 (deftype attributes () 'fixnum)
884
885 (eval-when (compile load eval)
886 ;;; Compute-Attribute-Mask -- Internal
887 ;;;
888 ;;; Given a list of attribute names and an alist that translates them to
889 ;;; masks, return the OR of the masks.
890 ;;;
891 (defun compute-attribute-mask (names alist)
892 (collect ((res 0 logior))
893 (dolist (name names)
894 (let ((mask (cdr (assoc name alist))))
895 (unless mask
896 (error "Unknown attribute name: ~S." name))
897 (res mask)))
898 (res)))
899
900 ); Eval-When (Compile Load Eval)
901
902 ;;; Def-Boolean-Attribute -- Interface
903 ;;;
904 ;;; Parse the specification and generate some accessor macros.
905 ;;;
906 (defmacro def-boolean-attribute (name &rest attribute-names)
907 "Def-Boolean-Attribute Name Attribute-Name*
908 Define a new class of boolean attributes, with the attributes havin the
909 specified Attribute-Names. Name is the name of the class, which is used to
910 generate some macros to manipulate sets of the attributes:
911
912 NAME-attributep attributes attribute-name*
913 Return true if one of the named attributes is present, false otherwise.
914 When set with SETF, updates the place Attributes setting or clearing the
915 specified attributes.
916
917 NAME-attributes attribute-name*
918 Return a set of the named attributes."
919
920 (let ((const-name (symbolicate name "-ATTRIBUTE-TRANSLATIONS"))
921 (test-name (symbolicate name "-ATTRIBUTEP")))
922 (collect ((alist))
923 (do ((mask 1 (ash mask 1))
924 (names attribute-names (cdr names)))
925 ((null names))
926 (alist (cons (car names) mask)))
927
928 `(progn
929 (eval-when (compile load eval)
930 (defconstant ,const-name ',(alist)))
931
932 (defmacro ,test-name (attributes &rest attribute-names)
933 "Automagically generated boolean attribute test function. See
934 Def-Boolean-Attribute."
935 `(logtest ,(compute-attribute-mask attribute-names ,const-name)
936 (the attributes ,attributes)))
937
938 (define-setf-expander ,test-name (place &rest attributes
939 &environment env)
940
941 "Automagically generated boolean attribute setter. See
942 Def-Boolean-Attribute."
943 (multiple-value-bind (temps values stores set get)
944 (get-setf-method place env)
945 (let ((newval (gensym))
946 (n-place (gensym))
947 (mask (compute-attribute-mask attributes ,const-name)))
948 (values `(,@temps ,n-place)
949 `(,@values ,get)
950 `(,newval)
951 `(let ((,(first stores)
952 (if ,newval
953 (logior ,n-place ,mask)
954 (logand ,n-place ,(lognot mask)))))
955 ,set
956 ,newval)
957 `(,',test-name ,n-place ,@attributes)))))
958
959 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
960 "Automagically generated boolean attribute creation function. See
961 Def-Boolean-Attribute."
962 (compute-attribute-mask attribute-names ,const-name))))))
963
964
965 ;;; Attributes-Union, Attributes-Intersection, Attributes= -- Interface
966 ;;;
967 ;;; And now for some gratuitous pseudo-abstraction...
968 ;;;
969 (defmacro attributes-union (&rest attributes)
970 "Returns the union of all the sets of boolean attributes which are its
971 arguments."
972 `(the attributes
973 (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
974 ;;;
975 (defmacro attributes-intersection (&rest attributes)
976 "Returns the intersection of all the sets of boolean attributes which are its
977 arguments."
978 `(the attributes
979 (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
980 ;;;
981 (declaim (inline attributes=))
982 (defun attributes= (attr1 attr2)
983 (declare (type attributes attr1 attr2))
984 "Returns true if the attributes present in Attr1 are indentical to those in
985 Attr2."
986 (eql attr1 attr2))
987
988
989 ;;;; The Event statistics/trace utility:
990
991 (eval-when (compile load eval)
992
993 (defstruct event-info
994 ;;
995 ;; The name of this event.
996 (name (required-argument) :type symbol)
997 ;;
998 ;; The string rescribing this event.
999 (description (required-argument) :type string)
1000 ;;
1001 ;; The name of the variable we stash this in.
1002 (var (required-argument) :type symbol)
1003 ;;
1004 ;; The number of times this event has happened.
1005 (count 0 :type fixnum)
1006 ;;
1007 ;; The level of significance of this event.
1008 (level (required-argument) :type unsigned-byte)
1009 ;;
1010 ;; If true, a function that gets called with the node that the event happened
1011 ;; to.
1012 (action nil :type (or function null)))
1013
1014 ;;; A hashtable from event names to event-info structures.
1015 ;;;
1016 (defvar *event-info* (make-hash-table :test #'eq))
1017
1018
1019 ;;; Event-Info-Or-Lose -- Internal
1020 ;;;
1021 ;;; Return the event info for Name or die trying.
1022 ;;;
1023 (defun event-info-or-lose (name)
1024 (declare (values event-info))
1025 (let ((res (gethash name *event-info*)))
1026 (unless res
1027 (error "~S is not the name of an event." name))
1028 res))
1029
1030 ); Eval-When (Compile Load Eval)
1031
1032
1033 ;;; Event-Count, Event-Action, Event-Level -- Interface
1034 ;;;
1035 (defun event-count (name)
1036 "Return the number of times that Event has happened."
1037 (declare (symbol name) (values fixnum))
1038 (event-info-count (event-info-or-lose name)))
1039 ;;;
1040 (defun event-action (name)
1041 "Return the function that is called when Event happens. If this is null,
1042 there is no action. The function is passed the node to which the event
1043 happened, or NIL if there is no relevant node. This may be set with SETF."
1044 (declare (symbol name) (values (or function null)))
1045 (event-info-action (event-info-or-lose name)))
1046 ;;;
1047 (defun %set-event-action (name new-value)
1048 (declare (symbol name) (type (or function null) new-value)
1049 (values (or function null)))
1050 (setf (event-info-action (event-info-or-lose name))
1051 new-value))
1052 ;;;
1053 (defsetf event-action %set-event-action)
1054 ;;;
1055 (defun event-level (name)
1056 "Return the non-negative integer which represents the level of significance
1057 of the event Name. This is used to determine whether to print a message when
1058 the event happens. This may be set with SETF."
1059 (declare (symbol name) (values unsigned-byte))
1060 (event-info-level (event-info-or-lose name)))
1061 ;;;
1062 (defun %set-event-level (name new-value)
1063 (declare (symbol name) (type unsigned-byte new-value)
1064 (values unsigned-byte))
1065 (setf (event-info-level (event-info-or-lose name))
1066 new-value))
1067 ;;;
1068 (defsetf event-level %set-event-level)
1069
1070
1071 ;;; Defevent -- Interface
1072 ;;;
1073 ;;; Make an event-info structure and stash it in a variable so we can get at
1074 ;;; it quickly.
1075 ;;;
1076 (defmacro defevent (name description &optional (level 0))
1077 "Defevent Name Description
1078 Define a new kind of event. Name is a symbol which names the event and
1079 Description is a string which describes the event. Level (default 0) is the
1080 level of significance associated with this event; it is used to determine
1081 whether to print a Note when the event happens."
1082 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
1083 `(eval-when (compile load eval)
1084 (defvar ,var-name
1085 (make-event-info :name ',name :description ',description :var ',var-name
1086 :level ,level))
1087 (setf (gethash ',name *event-info*) ,var-name)
1088 ',name)))
1089
1090 (declaim (type unsigned-byte *event-note-threshold*))
1091 (defvar *event-note-threshold* 1
1092 "This variable is a non-negative integer specifying the lowest level of
1093 event that will print a Note when it occurs.")
1094
1095 ;;; Event -- Interface
1096 ;;;
1097 ;;; Increment the counter and do any action. Mumble about the event if
1098 ;;; policy indicates.
1099 ;;;
1100 (defmacro event (name &optional node)
1101 "Event Name Node
1102 Note that the event with the specified Name has happened. Node is evaluated
1103 to determine the node to which the event happened."
1104 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
1105
1106
1107 ;;; Event-Statistics, Clear-Statistics -- Interface
1108 ;;;
1109 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
1110 (declare (type unsigned-byte min-count) (stream stream) (values))
1111 "Print a listing of events and their counts, sorted by the count. Events
1112 that happened fewer than Min-Count times will not be printed. Stream is the
1113 stream to write to."
1114 (collect ((info))
1115 (maphash #'(lambda (k v)
1116 (declare (ignore k))
1117 (when (>= (event-info-count v) min-count)
1118 (info v)))
1119 *event-info*)
1120 (dolist (event (sort (info) #'> :key #'event-info-count))
1121 (format stream "~6D: ~A~%" (event-info-count event)
1122 (event-info-description event)))
1123 (values)))
1124 ;;;
1125 (defun clear-statistics ()
1126 (declare (values))
1127 (maphash #'(lambda (k v)
1128 (declare (ignore k))
1129 (setf (event-info-count v) 0))
1130 *event-info*)
1131 (values))
1132
1133
1134 ;;;; Generic list (?) functions:
1135
1136 (declaim (inline find-in position-in map-in))
1137
1138 ;;; Find-In -- Interface
1139 ;;;
1140 (defun find-in (next element list &key (key #'identity)
1141 (test #'eql test-p) (test-not nil not-p))
1142 "Find Element in a null-terminated List linked by the accessor function
1143 Next. Key, Test and Test-Not are the same as for generic sequence
1144 functions."
1145 (when (and test-p not-p)
1146 (error "Silly to supply both :Test and :Test-Not."))
1147 (if not-p
1148 (do ((current list (funcall next current)))
1149 ((null current) nil)
1150 (unless (funcall test-not (funcall key current) element)
1151 (return current)))
1152 (do ((current list (funcall next current)))
1153 ((null current) nil)
1154 (when (funcall test (funcall key current) element)
1155 (return current)))))
1156
1157 ;;; Position-In -- Interface
1158 ;;;
1159 (defun position-in (next element list &key (key #'identity)
1160 (test #'eql test-p) (test-not nil not-p))
1161 "Return the position of Element (or NIL if absent) in a null-terminated List
1162 linked by the accessor function Next. Key, Test and Test-Not are the same as
1163 for generic sequence functions."
1164 (when (and test-p not-p)
1165 (error "Silly to supply both :Test and :Test-Not."))
1166 (if not-p
1167 (do ((current list (funcall next current))
1168 (i 0 (1+ i)))
1169 ((null current) nil)
1170 (unless (funcall test-not (funcall key current) element)
1171 (return i)))
1172 (do ((current list (funcall next current))
1173 (i 0 (1+ i)))
1174 ((null current) nil)
1175 (when (funcall test (funcall key current) element)
1176 (return i)))))
1177
1178
1179 ;;; Map-In -- Interface
1180 ;;;
1181 (defun map-in (next function list)
1182 "Map Function over the elements in a null-terminated List linked by the
1183 accessor function Next, returning a list of the results."
1184 (collect ((res))
1185 (do ((current list (funcall next current)))
1186 ((null current))
1187 (res (funcall function current)))
1188 (res)))
1189
1190
1191 ;;; Deletef-In -- Interface
1192 ;;;
1193 (defmacro deletef-in (next place item &environment env)
1194 "Deletef-In Next Place Item
1195 Delete Item from a null-terminated list linked by the accessor function Next
1196 that is stored in Place. Item must appear exactly once in the list."
1197 (multiple-value-bind
1198 (temps vals stores store access)
1199 (get-setf-method place env)
1200 (let ((n-item (gensym))
1201 (n-place (gensym))
1202 (n-current (gensym))
1203 (n-prev (gensym)))
1204 `(let* (,@(mapcar #'list temps vals)
1205 (,n-place ,access)
1206 (,n-item ,item))
1207 (if (eq ,n-place ,n-item)
1208 (let ((,(first stores) (,next ,n-place)))
1209 ,store)
1210 (do ((,n-prev ,n-place ,n-current)
1211 (,n-current (,next ,n-place)
1212 (,next ,n-current)))
1213 ((eq ,n-current ,n-item)
1214 (setf (,next ,n-prev)
1215 (,next ,n-current)))))
1216 (undefined-value)))))
1217
1218
1219 ;;; Push-In -- Interface
1220 ;;;
1221 (defmacro push-in (next item place &environment env)
1222 "Push Item onto a list linked by the accessor function Next that is stored in
1223 Place."
1224 (multiple-value-bind
1225 (temps vals stores store access)
1226 (get-setf-method place env)
1227 `(let (,@(mapcar #'list temps vals)
1228 (,(first stores) ,item))
1229 (setf (,next ,(first stores)) ,access)
1230 ,store
1231 (undefined-value))))
1232
1233
1234 ;;; EPOSITION -- Interface
1235 ;;;
1236 (defmacro eposition (&rest args)
1237 `(or (position ,@args)
1238 (error "Shouldn't happen?")))

  ViewVC Help
Powered by ViewVC 1.1.5