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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5