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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.35 - (show annotations)
Wed Jul 21 14:29:56 1993 UTC (20 years, 9 months ago) by ram
Branch: MAIN
Changes since 1.34: +11 -1 lines
Added with-compiler-error-handlers.
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.35 1993/07/21 14:29:56 ram 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 (when :native))
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 :When {:Native | :Byte | :Both}
458 - Indicates whether this transform applies to native code,
459 byte-code or both (default :native.)"
460
461 (when (and eval-name defun-only)
462 (error "Can't specify both DEFUN-ONLY and EVAL-NAME."))
463 (let ((n-args (gensym))
464 (n-node (or node (gensym)))
465 (n-decls (gensym))
466 (n-lambda (gensym))
467 (body `(,@decls ,@body)))
468 (multiple-value-bind (parsed-form vars)
469 (parse-deftransform
470 lambda-list
471 (if policy
472 `((unless (policy ,n-node ,policy) (give-up))
473 ,@body)
474 body)
475 n-args '(give-up))
476 (let ((stuff
477 `((,n-node)
478 (let* ((,n-args (basic-combination-args ,n-node))
479 ,@(when result
480 `((,result (node-cont ,n-node)))))
481 (multiple-value-bind (,n-lambda ,n-decls)
482 ,parsed-form
483 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
484 ,n-lambda
485 `(lambda ,',lambda-list
486 (declare (ignorable ,@',vars))
487 ,@,n-decls
488 ,,n-lambda)))))))
489 (if defun-only
490 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
491 `(%deftransform
492 ,(if eval-name name `',name)
493 ,(if eval-name
494 ``(function ,,arg-types ,,result-type)
495 `'(function ,arg-types ,result-type))
496 #'(lambda ,@stuff)
497 ,doc
498 ,(if important t nil)
499 ,when))))))
500
501 ;;;; Defknown, Defoptimizer:
502
503 ;;; Defknown -- Interface
504 ;;;
505 ;;; This macro should be the way that all implementation independent
506 ;;; information about functions is made known to the compiler.
507 ;;;
508 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
509 &rest keys)
510 "Defknown Name Arg-Types Result-Type [Attributes] {Key Value}*
511 Declare the function Name to be a known function. We construct a type
512 specifier for the function by wrapping (FUNCTION ...) around the Arg-Types
513 and Result-Type. Attributes is a an unevaluated list of the boolean
514 attributes that the function has. These attributes are meaningful here:
515 call
516 May call functions that are passed as arguments. In order to determine
517 what other effects are present, we must find the effects of all arguments
518 that may be functions.
519
520 unsafe
521 May incorporate arguments in the result or somehow pass them upward.
522
523 unwind
524 May fail to return during correct execution. Errors are O.K.
525
526 any
527 The (default) worst case. Includes all the other bad things, plus any
528 other possible bad thing.
529
530 foldable
531 May be constant-folded. The function has no side effects, but may be
532 affected by side effects on the arguments. e.g. SVREF, MAPC.
533
534 flushable
535 May be eliminated if value is unused. The function has no side effects
536 except possibly CONS. If a function is defined to signal errors, then
537 it is not flushable even if it is movable or foldable.
538
539 movable
540 May be moved with impunity. Has no side effects except possibly CONS,
541 and is affected only by its arguments.
542
543 predicate
544 A true predicate likely to be open-coded. This is a hint to IR1
545 conversion that it should ensure calls always appear as an IF test.
546 Not usually specified to Defknown, since this is implementation
547 dependent, and is usually automatically set by the Define-VOP
548 :Conditional option.
549
550 Name may also be a list of names, in which case the same information is given
551 to all the names. The keywords specify the initial values for various
552 optimizers that the function might have."
553 (when (and (intersection attributes '(any call unwind))
554 (intersection attributes '(movable)))
555 (error "Function cannot have both good and bad attributes: ~S" attributes))
556
557 `(%defknown ',(if (and (consp name)
558 (not (eq (car name) 'setf)))
559 name
560 (list name))
561 '(function ,arg-types ,result-type)
562 (ir1-attributes ,@(if (member 'any attributes)
563 (union '(call unsafe unwind) attributes)
564 attributes))
565 ,@keys))
566
567
568 ;;; Defoptimizer -- Interface
569 ;;;
570 ;;; Create a function which parses combination args according to a
571 ;;; Lambda-List, optionally storing it in a function-info slot.
572 ;;;
573 (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
574 &rest vars)
575 &body body)
576 "Defoptimizer (Function Kind) (Lambda-List [Node-Var] Var*)
577 Declaration* Form*
578 Define some Kind of optimizer for the named Function. Function must be a
579 known function. Lambda-List is used to parse the arguments to the
580 combination as in Deftransform. If the argument syntax is invalid or there
581 are non-constant keys, then we simply return NIL.
582
583 The function is DEFUN'ed as Function-Kind-OPTIMIZER. Possible kinds are
584 DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If a symbol is
585 specified instead of a (Function Kind) list, then we just do a DEFUN with the
586 symbol as its name, and don't do anything with the definition. This is
587 useful for creating optimizers to be passed by name to DEFKNOWN.
588
589 If supplied, Node-Var is bound to the combination node being optimized. If
590 additional Vars are supplied, then they are used as the rest of the optimizer
591 function's lambda-list. LTN-ANNOTATE methods are passed an additional POLICY
592 argument, and IR2-CONVERT methods are passed an additional IR2-BLOCK
593 argument."
594
595 (let ((name (if (symbolp what) what
596 (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
597
598 (let ((n-args (gensym)))
599 `(progn
600 (defun ,name (,n-node ,@vars)
601 (let ((,n-args (basic-combination-args ,n-node)))
602 ,(parse-deftransform lambda-list body n-args
603 `(return-from ,name nil))))
604 ,@(when (consp what)
605 `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
606 (function-info-or-lose ',(first what)))
607 #',name)))))))
608
609
610 ;;;; IR groveling macros:
611
612 ;;; Do-Blocks, Do-Blocks-Backwards -- Interface
613 ;;;
614 (defmacro do-blocks ((block-var component &optional ends result) &body body)
615 "Do-Blocks (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
616 Iterate over the blocks in a component, binding Block-Var to each block in
617 turn. The value of Ends determines whether to iterate over dummy head and
618 tail blocks:
619 NIL -- Skip Head and Tail (the default)
620 :Head -- Do head but skip tail
621 :Tail -- Do tail but skip head
622 :Both -- Do both head and tail
623
624 If supplied, Result-Form is the value to return."
625 (unless (member ends '(nil :head :tail :both))
626 (error "Losing Ends value: ~S." ends))
627 (let ((n-component (gensym))
628 (n-tail (gensym)))
629 `(let* ((,n-component ,component)
630 (,n-tail ,(if (member ends '(:both :tail))
631 nil
632 `(component-tail ,n-component))))
633 (do ((,block-var ,(if (member ends '(:both :head))
634 `(component-head ,n-component)
635 `(block-next (component-head ,n-component)))
636 (block-next ,block-var)))
637 ((eq ,block-var ,n-tail) ,result)
638 ,@body))))
639 ;;;
640 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
641 "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
642 Like Do-Blocks, only iterate over the blocks in reverse order."
643 (unless (member ends '(nil :head :tail :both))
644 (error "Losing Ends value: ~S." ends))
645 (let ((n-component (gensym))
646 (n-head (gensym)))
647 `(let* ((,n-component ,component)
648 (,n-head ,(if (member ends '(:both :head))
649 nil
650 `(component-head ,n-component))))
651 (do ((,block-var ,(if (member ends '(:both :tail))
652 `(component-tail ,n-component)
653 `(block-prev (component-tail ,n-component)))
654 (block-prev ,block-var)))
655 ((eq ,block-var ,n-head) ,result)
656 ,@body))))
657
658
659 ;;; Do-Uses -- Interface
660 ;;;
661 ;;; Could change it not to replicate the code someday perhaps...
662 ;;;
663 (defmacro do-uses ((node-var continuation &optional result) &body body)
664 "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
665 Iterate over the uses of Continuation, binding Node to each one succesively."
666 (once-only ((n-cont continuation))
667 `(ecase (continuation-kind ,n-cont)
668 (:unused)
669 (:inside-block
670 (block nil
671 (let ((,node-var (continuation-use ,n-cont)))
672 ,@body
673 ,result)))
674 ((:block-start :deleted-block-start)
675 (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
676 ,result)
677 ,@body)))))
678
679
680 ;;; Do-Nodes, Do-Nodes-Backwards -- Interface
681 ;;;
682 ;;; In the forward case, we terminate on Last-Cont so that we don't have to
683 ;;; worry about our termination condition being changed when new code is added
684 ;;; during the iteration. In the backward case, we do NODE-PREV before
685 ;;; evaluating the body so that we can keep going when the current node is
686 ;;; deleted.
687 ;;;
688 ;;; When Restart-P is supplied to DO-NODES, we start iterating over again at
689 ;;; the beginning of the block when we run into a continuation whose block
690 ;;; differs from the one we are trying to iterate over, either beacuse the
691 ;;; block was split, or because a node was deleted out from under us (hence its
692 ;;; block is NIL.) If the block start is deleted, we just punt. With
693 ;;; Restart-P, we are also more careful about termination, re-indirecting the
694 ;;; BLOCK-LAST each time.
695 ;;;
696 (defmacro do-nodes ((node-var cont-var block &key restart-p) &body body)
697 "Do-Nodes (Node-Var Cont-Var Block {Key Value}*) {Declaration}* {Form}*
698 Iterate over the nodes in Block, binding Node-Var to the each node and
699 Cont-Var to the node's Cont. The only keyword option is Restart-P, which
700 causes iteration to be restarted when a node is deleted out from under us (if
701 not supplied, this is an error.)"
702 (let ((n-block (gensym))
703 (n-last-cont (gensym)))
704 `(let* ((,n-block ,block)
705 ,@(unless restart-p
706 `((,n-last-cont (node-cont (block-last ,n-block))))))
707 (do* ((,node-var (continuation-next (block-start ,n-block))
708 ,(if restart-p
709 `(cond
710 ((eq (continuation-block ,cont-var) ,n-block)
711 (assert (continuation-next ,cont-var))
712 (continuation-next ,cont-var))
713 (t
714 (let ((start (block-start ,n-block)))
715 (unless (eq (continuation-kind start)
716 :block-start)
717 (return nil))
718 (continuation-next start))))
719 `(continuation-next ,cont-var)))
720 (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
721 (())
722 ,@body
723 (when ,(if restart-p
724 `(eq ,node-var (block-last ,n-block))
725 `(eq ,cont-var ,n-last-cont))
726 (return nil))))))
727 ;;;
728 (defmacro do-nodes-backwards ((node-var cont-var block) &body body)
729 "Do-Nodes-Backwards (Node-Var Cont-Var Block) {Declaration}* {Form}*
730 Like Do-Nodes, only iterates in reverse order."
731 (let ((n-block (gensym))
732 (n-start (gensym))
733 (n-last (gensym))
734 (n-next (gensym)))
735 `(let* ((,n-block ,block)
736 (,n-start (block-start ,n-block))
737 (,n-last (block-last ,n-block)))
738 (do* ((,cont-var (node-cont ,n-last) ,n-next)
739 (,node-var ,n-last (continuation-use ,cont-var))
740 (,n-next (node-prev ,node-var) (node-prev ,node-var)))
741 (())
742 ,@body
743 (when (eq ,n-next ,n-start)
744 (return nil))))))
745
746
747 ;;; With-IR1-Environment -- Interface
748 ;;;
749 ;;; The lexical environment is presumably already null...
750 ;;;
751 (defmacro with-ir1-environment (node &rest forms)
752 "With-IR1-Environment Node Form*
753 Bind the IR1 context variables so that IR1 conversion can be done after the
754 main conversion pass has finished."
755 (let ((n-node (gensym)))
756 `(let* ((,n-node ,node)
757 (*current-component* (block-component (node-block ,n-node)))
758 (*lexical-environment* (node-lexenv ,n-node))
759 (*current-path* (node-source-path ,n-node)))
760 ,@forms)))
761
762
763 ;;; WITH-IR1-NAMESPACE -- Interface
764 ;;;
765 ;;; Bind the hashtables used for keeping track of global variables,
766 ;;; functions, &c.
767 ;;;
768 (defmacro with-ir1-namespace (&body forms)
769 `(let ((*free-variables* (make-hash-table :test #'eq))
770 (*free-functions* (make-hash-table :test #'equal))
771 (*constants* (make-hash-table :test #'equal))
772 (*source-paths* (make-hash-table :test #'eq)))
773 ,@forms))
774
775
776 ;;; LEXENV-FIND -- Interface
777 ;;;
778 (defmacro lexenv-find (name slot &key test)
779 "LEXENV-FIND Name Slot {Key Value}*
780 Look up Name in the lexical environment namespace designated by Slot,
781 returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
782 may be used to determine the name equality predicate."
783 (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot)
784 *lexical-environment*)
785 ,@(when test `(:test ,test)))))
786 `(if ,n-res
787 (values (cdr ,n-res) t)
788 (values nil nil))))
789
790
791 ;;;; The Defprinter macro:
792
793 (defvar *defprint-pretty* nil
794 "If true, defprinter print functions print each slot on a separate line.")
795
796
797 ;;; Defprinter-Prin1, Defprinter-Princ -- Internal
798 ;;;
799 ;;; These functions are called by the expansion of the Defprinter
800 ;;; macro to do the actual printing.
801 ;;;
802 (proclaim '(ftype (function (symbol t stream &optional t) void)
803 defprinter-prin1 defprinter-princ))
804 (defun defprinter-prin1 (name value stream &optional indent)
805 (declare (ignore indent))
806 (write-string " " stream)
807 (when *print-pretty*
808 (pprint-newline :linear stream))
809 (princ name stream)
810 (write-string "= " stream)
811 (prin1 value stream))
812 ;;;
813 (defun defprinter-princ (name value stream &optional indent)
814 (declare (ignore indent))
815 (write-string " " stream)
816 (when *print-pretty*
817 (pprint-newline :linear stream))
818 (princ name stream)
819 (write-string "= " stream)
820 (princ value stream))
821
822 (defmacro defprinter (name &rest slots)
823 "Defprinter Name Slot-Desc*
824 Define some kind of reasonable defstruct structure-print function. Name
825 is the name of the structure. We define a function %PRINT-name which
826 prints the slots in the structure in the way described by the Slot-Descs.
827 Each Slot-Desc can be a slot name, indicating that the slot should simply
828 be printed. A Slot-Desc may also be a list of a slot name and other stuff.
829 The other stuff is composed of keywords followed by expressions. The
830 expressions are evaluated with the variable which is the slot name bound
831 to the value of the slot. These keywords are defined:
832
833 :PRIN1 Print the value of the expression instead of the slot value.
834 :PRINC Like :PRIN1, only princ the value
835 :TEST Only print something if the test is true.
836
837 If no printing thing is specified then the slot value is printed as PRIN1.
838
839 The structure being printed is bound to Structure and the stream is bound to
840 Stream."
841
842 (flet ((sref (slot) `(,(symbolicate name "-" slot) structure)))
843 (collect ((prints))
844 (dolist (slot slots)
845 (if (atom slot)
846 (prints `(defprinter-prin1 ',slot ,(sref slot) stream))
847 (let ((sname (first slot))
848 (test t))
849 (collect ((stuff))
850 (do ((option (rest slot) (cddr option)))
851 ((null option)
852 (prints
853 `(let ((,sname ,(sref sname)))
854 (when ,test
855 ,@(or (stuff)
856 `((defprinter-prin1 ',sname ,sname
857 stream)))))))
858 (case (first option)
859 (:prin1
860 (stuff `(defprinter-prin1 ',sname ,(second option)
861 stream)))
862 (:princ
863 (stuff `(defprinter-princ ',sname ,(second option)
864 stream)))
865 (:test (setq test (second option)))
866 (t
867 (error "Losing Defprinter option: ~S."
868 (first option)))))))))
869
870 `(defun ,(symbolicate "%PRINT-" name) (structure stream depth)
871 (flet ((do-prints (stream)
872 (declare (ignorable stream))
873 ,@(prints)))
874 (cond (*print-readably*
875 (error "~S cannot be printed readably." structure))
876 ((and *print-level* (>= depth *print-level*))
877 (format stream "#<~S ~X>"
878 ',name
879 (get-lisp-obj-address structure)))
880 (*print-pretty*
881 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
882 (pprint-indent :current 2 stream)
883 (prin1 ',name stream)
884 (write-char #\space stream)
885 (let ((*print-base* 16)
886 (*print-radix* t))
887 (prin1 (get-lisp-obj-address structure) stream))
888 (do-prints stream)))
889 (t
890 (descend-into (stream)
891 (format stream "#<~S ~X"
892 ',name
893 (get-lisp-obj-address structure))
894 (do-prints stream)
895 (format stream ">")))))
896 nil))))
897
898
899 ;;;; Boolean attribute utilities:
900 ;;;
901 ;;; We need to maintain various sets of boolean attributes for known
902 ;;; functions and VOPs. To save space and allow for quick set operations, we
903 ;;; represent them as bits in a fixnum.
904 ;;;
905
906 (deftype attributes () 'fixnum)
907
908 (eval-when (compile load eval)
909 ;;; Compute-Attribute-Mask -- Internal
910 ;;;
911 ;;; Given a list of attribute names and an alist that translates them to
912 ;;; masks, return the OR of the masks.
913 ;;;
914 (defun compute-attribute-mask (names alist)
915 (collect ((res 0 logior))
916 (dolist (name names)
917 (let ((mask (cdr (assoc name alist))))
918 (unless mask
919 (error "Unknown attribute name: ~S." name))
920 (res mask)))
921 (res)))
922
923 ); Eval-When (Compile Load Eval)
924
925 ;;; Def-Boolean-Attribute -- Interface
926 ;;;
927 ;;; Parse the specification and generate some accessor macros.
928 ;;;
929 (defmacro def-boolean-attribute (name &rest attribute-names)
930 "Def-Boolean-Attribute Name Attribute-Name*
931 Define a new class of boolean attributes, with the attributes havin the
932 specified Attribute-Names. Name is the name of the class, which is used to
933 generate some macros to manipulate sets of the attributes:
934
935 NAME-attributep attributes attribute-name*
936 Return true if one of the named attributes is present, false otherwise.
937 When set with SETF, updates the place Attributes setting or clearing the
938 specified attributes.
939
940 NAME-attributes attribute-name*
941 Return a set of the named attributes."
942
943 (let ((const-name (symbolicate name "-ATTRIBUTE-TRANSLATIONS"))
944 (test-name (symbolicate name "-ATTRIBUTEP")))
945 (collect ((alist))
946 (do ((mask 1 (ash mask 1))
947 (names attribute-names (cdr names)))
948 ((null names))
949 (alist (cons (car names) mask)))
950
951 `(progn
952 (eval-when (compile load eval)
953 (defconstant ,const-name ',(alist)))
954
955 (defmacro ,test-name (attributes &rest attribute-names)
956 "Automagically generated boolean attribute test function. See
957 Def-Boolean-Attribute."
958 `(logtest ,(compute-attribute-mask attribute-names ,const-name)
959 (the attributes ,attributes)))
960
961 (define-setf-method ,test-name (place &rest attributes
962 &environment env)
963
964 "Automagically generated boolean attribute setter. See
965 Def-Boolean-Attribute."
966 (multiple-value-bind (temps values stores set get)
967 (get-setf-method place env)
968 (let ((newval (gensym))
969 (n-place (gensym))
970 (mask (compute-attribute-mask attributes ,const-name)))
971 (values `(,@temps ,n-place)
972 `(,@values ,get)
973 `(,newval)
974 `(let ((,(first stores)
975 (if ,newval
976 (logior ,n-place ,mask)
977 (logand ,n-place ,(lognot mask)))))
978 ,set
979 ,newval)
980 `(,',test-name ,n-place ,@attributes)))))
981
982 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
983 "Automagically generated boolean attribute creation function. See
984 Def-Boolean-Attribute."
985 (compute-attribute-mask attribute-names ,const-name))))))
986
987
988 ;;; Attributes-Union, Attributes-Intersection, Attributes= -- Interface
989 ;;;
990 ;;; And now for some gratuitous pseudo-abstraction...
991 ;;;
992 (defmacro attributes-union (&rest attributes)
993 "Returns the union of all the sets of boolean attributes which are its
994 arguments."
995 `(the attributes
996 (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
997 ;;;
998 (defmacro attributes-intersection (&rest attributes)
999 "Returns the intersection of all the sets of boolean attributes which are its
1000 arguments."
1001 `(the attributes
1002 (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
1003 ;;;
1004 (proclaim '(inline attributes=))
1005 (proclaim '(function attributes= (attributes attributes) boolean))
1006 (defun attributes= (attr1 attr2)
1007 "Returns true if the attributes present in Attr1 are indentical to those in
1008 Attr2."
1009 (eql attr1 attr2))
1010
1011
1012 ;;;; The Event statistics/trace utility:
1013
1014 (eval-when (compile load eval)
1015
1016 (defstruct event-info
1017 ;;
1018 ;; The name of this event.
1019 (name (required-argument) :type symbol)
1020 ;;
1021 ;; The string rescribing this event.
1022 (description (required-argument) :type string)
1023 ;;
1024 ;; The name of the variable we stash this in.
1025 (var (required-argument) :type symbol)
1026 ;;
1027 ;; The number of times this event has happened.
1028 (count 0 :type fixnum)
1029 ;;
1030 ;; The level of significance of this event.
1031 (level (required-argument) :type unsigned-byte)
1032 ;;
1033 ;; If true, a function that gets called with the node that the event happened
1034 ;; to.
1035 (action nil :type (or function null)))
1036
1037 ;;; A hashtable from event names to event-info structures.
1038 ;;;
1039 (defvar *event-info* (make-hash-table :test #'eq))
1040
1041
1042 ;;; Event-Info-Or-Lose -- Internal
1043 ;;;
1044 ;;; Return the event info for Name or die trying.
1045 ;;;
1046 (proclaim '(function event-info-or-lose (t) event-info))
1047 (defun event-info-or-lose (name)
1048 (let ((res (gethash name *event-info*)))
1049 (unless res
1050 (error "~S is not the name of an event." name))
1051 res))
1052
1053 ); Eval-When (Compile Load Eval)
1054
1055
1056 ;;; Event-Count, Event-Action, Event-Level -- Interface
1057 ;;;
1058 (proclaim '(function event-count (symbol) fixnum))
1059 (defun event-count (name)
1060 "Return the number of times that Event has happened."
1061 (event-info-count (event-info-or-lose name)))
1062 ;;;
1063 (proclaim '(function event-action (symbol) (or function null)))
1064 (defun event-action (name)
1065 "Return the function that is called when Event happens. If this is null,
1066 there is no action. The function is passed the node to which the event
1067 happened, or NIL if there is no relevant node. This may be set with SETF."
1068 (event-info-action (event-info-or-lose name)))
1069 ;;;
1070 (proclaim '(function %set-event-action (symbol (or function null)) (or function null)))
1071 (defun %set-event-action (name new-value)
1072 (setf (event-info-action (event-info-or-lose name))
1073 new-value))
1074 ;;;
1075 (defsetf event-action %set-event-action)
1076 ;;;
1077 (proclaim '(function event-level (symbol) unsigned-byte))
1078 (defun event-level (name)
1079 "Return the non-negative integer which represents the level of significance
1080 of the event Name. This is used to determine whether to print a message when
1081 the event happens. This may be set with SETF."
1082 (event-info-level (event-info-or-lose name)))
1083 ;;;
1084 (proclaim '(function %set-event-level (symbol unsigned-byte) unsigned-byte))
1085 (defun %set-event-level (name new-value)
1086 (setf (event-info-level (event-info-or-lose name))
1087 new-value))
1088 ;;;
1089 (defsetf event-level %set-event-level)
1090
1091
1092 ;;; Defevent -- Interface
1093 ;;;
1094 ;;; Make an event-info structure and stash it in a variable so we can get at
1095 ;;; it quickly.
1096 ;;;
1097 (defmacro defevent (name description &optional (level 0))
1098 "Defevent Name Description
1099 Define a new kind of event. Name is a symbol which names the event and
1100 Description is a string which describes the event. Level (default 0) is the
1101 level of significance associated with this event; it is used to determine
1102 whether to print a Note when the event happens."
1103 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
1104 `(eval-when (compile load eval)
1105 (defvar ,var-name
1106 (make-event-info :name ',name :description ',description :var ',var-name
1107 :level ,level))
1108 (setf (gethash ',name *event-info*) ,var-name)
1109 ',name)))
1110
1111 (proclaim '(type unsigned-byte *event-note-threshold*))
1112 (defvar *event-note-threshold* 1
1113 "This variable is a non-negative integer specifying the lowest level of
1114 event that will print a Note when it occurs.")
1115
1116 ;;; Event -- Interface
1117 ;;;
1118 ;;; Increment the counter and do any action. Mumble about the event if
1119 ;;; policy indicates.
1120 ;;;
1121 (defmacro event (name &optional node)
1122 "Event Name Node
1123 Note that the event with the specified Name has happened. Node is evaluated
1124 to determine the node to which the event happened."
1125 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
1126 ;;;
1127 (proclaim '(function %event (event-info (or node null))))
1128 (defun %event (info node)
1129 (incf (event-info-count info))
1130 (when (and (>= (event-info-level info) *event-note-threshold*)
1131 (if node
1132 (policy node (= brevity 0))
1133 (policy nil (= brevity 0))))
1134 (let ((*compiler-error-context* node))
1135 (compiler-note (event-info-description info))))
1136
1137 (let ((action (event-info-action info)))
1138 (when action (funcall action node))))
1139
1140
1141 ;;; Event-Statistics, Clear-Statistics -- Interface
1142 ;;;
1143 (proclaim '(function event-statistics (&optional unsigned-byte stream) void))
1144 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
1145 "Print a listing of events and their counts, sorted by the count. Events
1146 that happened fewer than Min-Count times will not be printed. Stream is the
1147 stream to write to."
1148 (collect ((info))
1149 (maphash #'(lambda (k v)
1150 (declare (ignore k))
1151 (when (>= (event-info-count v) min-count)
1152 (info v)))
1153 *event-info*)
1154 (dolist (event (sort (info) #'> :key #'event-info-count))
1155 (format stream "~6D: ~A~%" (event-info-count event)
1156 (event-info-description event)))
1157 (values)))
1158 ;;;
1159 (proclaim '(function clear-statistics () void))
1160 (defun clear-statistics ()
1161 (maphash #'(lambda (k v)
1162 (declare (ignore k))
1163 (setf (event-info-count v) 0))
1164 *event-info*)
1165 (values))
1166
1167
1168 ;;;; WITH-COMPILER-ERROR-HANDLERS -- Interface
1169 ;;;
1170 ;;; Establish the compiler's condition handlers.
1171 ;;;
1172 (defmacro with-compiler-error-handlers (&body body)
1173 `(handler-bind ((compiler-error #'compiler-error-handler)
1174 (style-warning #'compiler-style-warning-handler)
1175 (warning #'compiler-warning-handler))
1176 ,@body))

  ViewVC Help
Powered by ViewVC 1.1.5