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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5