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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5