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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5