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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5