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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5