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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41.2.3 - (show annotations)
Sat Mar 23 18:50:24 2002 UTC (12 years ago) by pw
Branch: RELENG_18
CVS Tags: RELEASE_18d
Changes since 1.41.2.2: +11 -11 lines
Mega commit to bring RELENG_18 branch in sync with HEAD in preparation
for release tagging 18d.
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.41.2.3 2002/03/23 18:50:24 pw 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 "Can't funcall the SYMBOL-FUNCTION of special forms."))
129
130 ;;; CONVERT-CONDITION-INTO-COMPILER-ERROR -- Internal
131 ;;;
132 ;;; Passed to parse-defmacro when we want compiler errors instead of real
133 ;;; errors.
134 ;;;
135 (declaim (inline convert-condition-into-compiler-error))
136 (defun convert-condition-into-compiler-error (datum &rest stuff)
137 (if (stringp datum)
138 (apply #'compiler-error datum stuff)
139 (compiler-error "~A"
140 (if (symbolp datum)
141 (apply #'make-condition datum stuff)
142 datum))))
143
144 ;;; Def-IR1-Translator -- Interface
145 ;;;
146 ;;; Parse defmacro style lambda-list, setting things up so that a compiler
147 ;;; error happens if the syntax is invalid.
148 ;;;
149 (defmacro def-ir1-translator (name (lambda-list start-var cont-var
150 &key (kind :special-form))
151 &body body)
152 "Def-IR1-Translator Name (Lambda-List Start-Var Cont-Var {Key Value}*)
153 [Doc-String] Form*
154 Define a function that converts a Special-Form or other magical thing into
155 IR1. Lambda-List is a defmacro style lambda list. Start-Var and Cont-Var
156 are bound to the start and result continuations for the resulting IR1.
157 This keyword is defined:
158 Kind
159 The function kind to associate with Name (default :special-form)."
160 (let ((fn-name (symbolicate "IR1-CONVERT-" name))
161 (n-form (gensym))
162 (n-env (gensym)))
163 (multiple-value-bind
164 (body decls doc)
165 (lisp::parse-defmacro lambda-list n-form body name "special form"
166 :doc-string-allowed t
167 :environment n-env
168 :error-fun 'convert-condition-into-compiler-error)
169 `(progn
170 (declaim (function ,fn-name (continuation continuation t) void))
171 (defun ,fn-name (,start-var ,cont-var ,n-form)
172 (let ((,n-env *lexical-environment*))
173 ,@decls
174 ,body))
175 ,@(when doc
176 `((setf (documentation ',name 'function) ,doc)))
177 (setf (info function ir1-convert ',name) #',fn-name)
178 (setf (info function kind ',name) ,kind)
179 #+new-compiler
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
739 ;;; With-debug-counters -- Interface
740 ;;;
741 ;;; Bind the hashtables and counters used for keeping track of
742 ;;; continuation, TN, and label IDs for the debug dumping routines.
743 ;;;
744 (defmacro with-debug-counters (&body forms)
745 `(let ((*continuation-numbers* (make-hash-table :test #'eq))
746 (*number-continuations* (make-hash-table :test #'eql))
747 (*continuation-number* 0)
748 (*tn-ids* (make-hash-table :test #'eq))
749 (*id-tns* (make-hash-table :test #'eql))
750 (*tn-id* 0)
751 (*id-labels* (make-hash-table :test #'eq))
752 (*label-ids* (make-hash-table :test #'eql))
753 (*label-id* 0))
754 ,@forms))
755
756
757 ;;;; The Defprinter macro:
758
759 (defvar *defprint-pretty* nil
760 "If true, defprinter print functions print each slot on a separate line.")
761
762
763 ;;; Defprinter-Prin1, Defprinter-Princ -- Internal
764 ;;;
765 ;;; These functions are called by the expansion of the Defprinter
766 ;;; macro to do the actual printing.
767 ;;;
768 (defun defprinter-prin1 (name value stream &optional indent)
769 (declare (symbol name) (stream stream) (ignore indent))
770 (write-string " " stream)
771 (when *print-pretty*
772 (pprint-newline :linear stream))
773 (princ name stream)
774 (write-string "= " stream)
775 (prin1 value stream))
776 ;;;
777 (defun defprinter-princ (name value stream &optional indent)
778 (declare (symbol name) (stream stream) (ignore indent))
779 (write-string " " stream)
780 (when *print-pretty*
781 (pprint-newline :linear stream))
782 (princ name stream)
783 (write-string "= " stream)
784 (princ value stream))
785
786 (defmacro defprinter (name &rest slots)
787 "Defprinter Name Slot-Desc*
788 Define some kind of reasonable defstruct structure-print function. Name
789 is the name of the structure. We define a function %PRINT-name which
790 prints the slots in the structure in the way described by the Slot-Descs.
791 Each Slot-Desc can be a slot name, indicating that the slot should simply
792 be printed. A Slot-Desc may also be a list of a slot name and other stuff.
793 The other stuff is composed of keywords followed by expressions. The
794 expressions are evaluated with the variable which is the slot name bound
795 to the value of the slot. These keywords are defined:
796
797 :PRIN1 Print the value of the expression instead of the slot value.
798 :PRINC Like :PRIN1, only princ the value
799 :TEST Only print something if the test is true.
800
801 If no printing thing is specified then the slot value is printed as PRIN1.
802
803 The structure being printed is bound to Structure and the stream is bound to
804 Stream."
805
806 (flet ((sref (slot) `(,(symbolicate name "-" slot) structure)))
807 (collect ((prints))
808 (dolist (slot slots)
809 (if (atom slot)
810 (prints `(defprinter-prin1 ',slot ,(sref slot) stream))
811 (let ((sname (first slot))
812 (test t))
813 (collect ((stuff))
814 (do ((option (rest slot) (cddr option)))
815 ((null option)
816 (prints
817 `(let ((,sname ,(sref sname)))
818 (when ,test
819 ,@(or (stuff)
820 `((defprinter-prin1 ',sname ,sname
821 stream)))))))
822 (case (first option)
823 (:prin1
824 (stuff `(defprinter-prin1 ',sname ,(second option)
825 stream)))
826 (:princ
827 (stuff `(defprinter-princ ',sname ,(second option)
828 stream)))
829 (:test (setq test (second option)))
830 (t
831 (error "Losing Defprinter option: ~S."
832 (first option)))))))))
833
834 `(defun ,(symbolicate "%PRINT-" name) (structure stream depth)
835 (flet ((do-prints (stream)
836 (declare (ignorable stream))
837 ,@(prints)))
838 (cond (*print-readably*
839 (error "~S cannot be printed readably." structure))
840 ((and *print-level* (>= depth *print-level*))
841 (format stream "#<~S ~X>"
842 ',name
843 (get-lisp-obj-address structure)))
844 (*print-pretty*
845 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
846 (pprint-indent :current 2 stream)
847 (prin1 ',name stream)
848 (write-char #\space stream)
849 (let ((*print-base* 16)
850 (*print-radix* t))
851 (prin1 (get-lisp-obj-address structure) stream))
852 (do-prints stream)))
853 (t
854 (descend-into (stream)
855 (format stream "#<~S ~X"
856 ',name
857 (get-lisp-obj-address structure))
858 (do-prints stream)
859 (format stream ">")))))
860 nil))))
861
862
863 ;;;; Boolean attribute utilities:
864 ;;;
865 ;;; We need to maintain various sets of boolean attributes for known
866 ;;; functions and VOPs. To save space and allow for quick set operations, we
867 ;;; represent them as bits in a fixnum.
868 ;;;
869
870 (deftype attributes () 'fixnum)
871
872 (eval-when (compile load eval)
873 ;;; Compute-Attribute-Mask -- Internal
874 ;;;
875 ;;; Given a list of attribute names and an alist that translates them to
876 ;;; masks, return the OR of the masks.
877 ;;;
878 (defun compute-attribute-mask (names alist)
879 (collect ((res 0 logior))
880 (dolist (name names)
881 (let ((mask (cdr (assoc name alist))))
882 (unless mask
883 (error "Unknown attribute name: ~S." name))
884 (res mask)))
885 (res)))
886
887 ); Eval-When (Compile Load Eval)
888
889 ;;; Def-Boolean-Attribute -- Interface
890 ;;;
891 ;;; Parse the specification and generate some accessor macros.
892 ;;;
893 (defmacro def-boolean-attribute (name &rest attribute-names)
894 "Def-Boolean-Attribute Name Attribute-Name*
895 Define a new class of boolean attributes, with the attributes havin the
896 specified Attribute-Names. Name is the name of the class, which is used to
897 generate some macros to manipulate sets of the attributes:
898
899 NAME-attributep attributes attribute-name*
900 Return true if one of the named attributes is present, false otherwise.
901 When set with SETF, updates the place Attributes setting or clearing the
902 specified attributes.
903
904 NAME-attributes attribute-name*
905 Return a set of the named attributes."
906
907 (let ((const-name (symbolicate name "-ATTRIBUTE-TRANSLATIONS"))
908 (test-name (symbolicate name "-ATTRIBUTEP")))
909 (collect ((alist))
910 (do ((mask 1 (ash mask 1))
911 (names attribute-names (cdr names)))
912 ((null names))
913 (alist (cons (car names) mask)))
914
915 `(progn
916 (eval-when (compile load eval)
917 (defconstant ,const-name ',(alist)))
918
919 (defmacro ,test-name (attributes &rest attribute-names)
920 "Automagically generated boolean attribute test function. See
921 Def-Boolean-Attribute."
922 `(logtest ,(compute-attribute-mask attribute-names ,const-name)
923 (the attributes ,attributes)))
924
925 (define-setf-expander ,test-name (place &rest attributes
926 &environment env)
927
928 "Automagically generated boolean attribute setter. See
929 Def-Boolean-Attribute."
930 (multiple-value-bind (temps values stores set get)
931 (get-setf-method place env)
932 (let ((newval (gensym))
933 (n-place (gensym))
934 (mask (compute-attribute-mask attributes ,const-name)))
935 (values `(,@temps ,n-place)
936 `(,@values ,get)
937 `(,newval)
938 `(let ((,(first stores)
939 (if ,newval
940 (logior ,n-place ,mask)
941 (logand ,n-place ,(lognot mask)))))
942 ,set
943 ,newval)
944 `(,',test-name ,n-place ,@attributes)))))
945
946 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
947 "Automagically generated boolean attribute creation function. See
948 Def-Boolean-Attribute."
949 (compute-attribute-mask attribute-names ,const-name))))))
950
951
952 ;;; Attributes-Union, Attributes-Intersection, Attributes= -- Interface
953 ;;;
954 ;;; And now for some gratuitous pseudo-abstraction...
955 ;;;
956 (defmacro attributes-union (&rest attributes)
957 "Returns the union of all the sets of boolean attributes which are its
958 arguments."
959 `(the attributes
960 (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
961 ;;;
962 (defmacro attributes-intersection (&rest attributes)
963 "Returns the intersection of all the sets of boolean attributes which are its
964 arguments."
965 `(the attributes
966 (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
967 ;;;
968 (declaim (inline attributes=))
969 (defun attributes= (attr1 attr2)
970 (declare (type attributes attr1 attr2))
971 "Returns true if the attributes present in Attr1 are indentical to those in
972 Attr2."
973 (eql attr1 attr2))
974
975
976 ;;;; The Event statistics/trace utility:
977
978 (eval-when (compile load eval)
979
980 (defstruct event-info
981 ;;
982 ;; The name of this event.
983 (name (required-argument) :type symbol)
984 ;;
985 ;; The string rescribing this event.
986 (description (required-argument) :type string)
987 ;;
988 ;; The name of the variable we stash this in.
989 (var (required-argument) :type symbol)
990 ;;
991 ;; The number of times this event has happened.
992 (count 0 :type fixnum)
993 ;;
994 ;; The level of significance of this event.
995 (level (required-argument) :type unsigned-byte)
996 ;;
997 ;; If true, a function that gets called with the node that the event happened
998 ;; to.
999 (action nil :type (or function null)))
1000
1001 ;;; A hashtable from event names to event-info structures.
1002 ;;;
1003 (defvar *event-info* (make-hash-table :test #'eq))
1004
1005
1006 ;;; Event-Info-Or-Lose -- Internal
1007 ;;;
1008 ;;; Return the event info for Name or die trying.
1009 ;;;
1010 (defun event-info-or-lose (name)
1011 (declare (values event-info))
1012 (let ((res (gethash name *event-info*)))
1013 (unless res
1014 (error "~S is not the name of an event." name))
1015 res))
1016
1017 ); Eval-When (Compile Load Eval)
1018
1019
1020 ;;; Event-Count, Event-Action, Event-Level -- Interface
1021 ;;;
1022 (defun event-count (name)
1023 "Return the number of times that Event has happened."
1024 (declare (symbol name) (values fixnum))
1025 (event-info-count (event-info-or-lose name)))
1026 ;;;
1027 (defun event-action (name)
1028 "Return the function that is called when Event happens. If this is null,
1029 there is no action. The function is passed the node to which the event
1030 happened, or NIL if there is no relevant node. This may be set with SETF."
1031 (declare (symbol name) (values (or function null)))
1032 (event-info-action (event-info-or-lose name)))
1033 ;;;
1034 (defun %set-event-action (name new-value)
1035 (declare (symbol name) (type (or function null) new-value)
1036 (values (or function null)))
1037 (setf (event-info-action (event-info-or-lose name))
1038 new-value))
1039 ;;;
1040 (defsetf event-action %set-event-action)
1041 ;;;
1042 (defun event-level (name)
1043 "Return the non-negative integer which represents the level of significance
1044 of the event Name. This is used to determine whether to print a message when
1045 the event happens. This may be set with SETF."
1046 (declare (symbol name) (values unsigned-byte))
1047 (event-info-level (event-info-or-lose name)))
1048 ;;;
1049 (defun %set-event-level (name new-value)
1050 (declare (symbol name) (type unsigned-byte new-value)
1051 (values unsigned-byte))
1052 (setf (event-info-level (event-info-or-lose name))
1053 new-value))
1054 ;;;
1055 (defsetf event-level %set-event-level)
1056
1057
1058 ;;; Defevent -- Interface
1059 ;;;
1060 ;;; Make an event-info structure and stash it in a variable so we can get at
1061 ;;; it quickly.
1062 ;;;
1063 (defmacro defevent (name description &optional (level 0))
1064 "Defevent Name Description
1065 Define a new kind of event. Name is a symbol which names the event and
1066 Description is a string which describes the event. Level (default 0) is the
1067 level of significance associated with this event; it is used to determine
1068 whether to print a Note when the event happens."
1069 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
1070 `(eval-when (compile load eval)
1071 (defvar ,var-name
1072 (make-event-info :name ',name :description ',description :var ',var-name
1073 :level ,level))
1074 (setf (gethash ',name *event-info*) ,var-name)
1075 ',name)))
1076
1077 (declaim (type unsigned-byte *event-note-threshold*))
1078 (defvar *event-note-threshold* 1
1079 "This variable is a non-negative integer specifying the lowest level of
1080 event that will print a Note when it occurs.")
1081
1082 ;;; Event -- Interface
1083 ;;;
1084 ;;; Increment the counter and do any action. Mumble about the event if
1085 ;;; policy indicates.
1086 ;;;
1087 (defmacro event (name &optional node)
1088 "Event Name Node
1089 Note that the event with the specified Name has happened. Node is evaluated
1090 to determine the node to which the event happened."
1091 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
1092
1093
1094 ;;; Event-Statistics, Clear-Statistics -- Interface
1095 ;;;
1096 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
1097 (declare (type unsigned-byte min-count) (stream stream) (values))
1098 "Print a listing of events and their counts, sorted by the count. Events
1099 that happened fewer than Min-Count times will not be printed. Stream is the
1100 stream to write to."
1101 (collect ((info))
1102 (maphash #'(lambda (k v)
1103 (declare (ignore k))
1104 (when (>= (event-info-count v) min-count)
1105 (info v)))
1106 *event-info*)
1107 (dolist (event (sort (info) #'> :key #'event-info-count))
1108 (format stream "~6D: ~A~%" (event-info-count event)
1109 (event-info-description event)))
1110 (values)))
1111 ;;;
1112 (defun clear-statistics ()
1113 (declare (values))
1114 (maphash #'(lambda (k v)
1115 (declare (ignore k))
1116 (setf (event-info-count v) 0))
1117 *event-info*)
1118 (values))
1119
1120
1121 ;;;; Generic list (?) functions:
1122
1123 (declaim (inline find-in position-in map-in))
1124
1125 ;;; Find-In -- Interface
1126 ;;;
1127 (defun find-in (next element list &key (key #'identity)
1128 (test #'eql test-p) (test-not nil not-p))
1129 "Find Element in a null-terminated List linked by the accessor function
1130 Next. Key, Test and Test-Not are the same as for generic sequence
1131 functions."
1132 (when (and test-p not-p)
1133 (error "Silly to supply both :Test and :Test-Not."))
1134 (if not-p
1135 (do ((current list (funcall next current)))
1136 ((null current) nil)
1137 (unless (funcall test-not (funcall key current) element)
1138 (return current)))
1139 (do ((current list (funcall next current)))
1140 ((null current) nil)
1141 (when (funcall test (funcall key current) element)
1142 (return current)))))
1143
1144 ;;; Position-In -- Interface
1145 ;;;
1146 (defun position-in (next element list &key (key #'identity)
1147 (test #'eql test-p) (test-not nil not-p))
1148 "Return the position of Element (or NIL if absent) in a null-terminated List
1149 linked by the accessor function Next. Key, Test and Test-Not are the same as
1150 for generic sequence functions."
1151 (when (and test-p not-p)
1152 (error "Silly to supply both :Test and :Test-Not."))
1153 (if not-p
1154 (do ((current list (funcall next current))
1155 (i 0 (1+ i)))
1156 ((null current) nil)
1157 (unless (funcall test-not (funcall key current) element)
1158 (return i)))
1159 (do ((current list (funcall next current))
1160 (i 0 (1+ i)))
1161 ((null current) nil)
1162 (when (funcall test (funcall key current) element)
1163 (return i)))))
1164
1165
1166 ;;; Map-In -- Interface
1167 ;;;
1168 (defun map-in (next function list)
1169 "Map Function over the elements in a null-terminated List linked by the
1170 accessor function Next, returning a list of the results."
1171 (collect ((res))
1172 (do ((current list (funcall next current)))
1173 ((null current))
1174 (res (funcall function current)))
1175 (res)))
1176
1177
1178 ;;; Deletef-In -- Interface
1179 ;;;
1180 (defmacro deletef-in (next place item &environment env)
1181 "Deletef-In Next Place Item
1182 Delete Item from a null-terminated list linked by the accessor function Next
1183 that is stored in Place. Item must appear exactly once in the list."
1184 (multiple-value-bind
1185 (temps vals stores store access)
1186 (get-setf-method place env)
1187 (let ((n-item (gensym))
1188 (n-place (gensym))
1189 (n-current (gensym))
1190 (n-prev (gensym)))
1191 `(let* (,@(mapcar #'list temps vals)
1192 (,n-place ,access)
1193 (,n-item ,item))
1194 (if (eq ,n-place ,n-item)
1195 (let ((,(first stores) (,next ,n-place)))
1196 ,store)
1197 (do ((,n-prev ,n-place ,n-current)
1198 (,n-current (,next ,n-place)
1199 (,next ,n-current)))
1200 ((eq ,n-current ,n-item)
1201 (setf (,next ,n-prev)
1202 (,next ,n-current)))))
1203 (undefined-value)))))
1204
1205
1206 ;;; Push-In -- Interface
1207 ;;;
1208 (defmacro push-in (next item place &environment env)
1209 "Push Item onto a list linked by the accessor function Next that is stored in
1210 Place."
1211 (multiple-value-bind
1212 (temps vals stores store access)
1213 (get-setf-method place env)
1214 `(let (,@(mapcar #'list temps vals)
1215 (,(first stores) ,item))
1216 (setf (,next ,(first stores)) ,access)
1217 ,store
1218 (undefined-value))))
1219
1220
1221 ;;; EPOSITION -- Interface
1222 ;;;
1223 (defmacro eposition (&rest args)
1224 `(or (position ,@args)
1225 (error "Shouldn't happen?")))

  ViewVC Help
Powered by ViewVC 1.1.5