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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5