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

  ViewVC Help
Powered by ViewVC 1.1.5