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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations)
Wed Sep 19 11:46:17 1990 UTC (23 years, 7 months ago) by ram
Branch: MAIN
Changes since 1.9: +13 -2 lines
Made DEF-IR1-TRANSLATOR set the SYMBOL-FUNCTION of special forms to 
a bogus function
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 ram 1.10 ;;; 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 wlott 1.1 ;;; 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 ram 1.7 (let ((,n-env *lexical-environment*))
165 wlott 1.1 ,@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 ram 1.10 (setf (info function kind ',name) ,kind)
173     #+new-compiler
174     ,@(when (eq kind :special-form)
175     `((setf (symbol-function ',name) #'special-form-function)))))))
176 wlott 1.1
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 ram 1.7 (let ((,n-env *lexical-environment*))
208 wlott 1.1 ,@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 ram 1.7 (let ((,n-env *lexical-environment*))
231 wlott 1.1 ,@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 ram 1.3 (eval-when (#-new-compiler compile load eval)
246 wlott 1.1
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     `(progn
439     (unless (policy ,n-node ,policy) (give-up))
440     ,@body)
441     body)
442     n-args '(give-up))
443     (let ((stuff
444     `((,n-node)
445     (let* ((,n-args (basic-combination-args ,n-node))
446     ,@(when result
447     `((,result (node-cont ,n-node)))))
448     (multiple-value-bind (,n-lambda ,n-decls)
449     ,parsed-form
450     (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
451     ,n-lambda
452     `(lambda ,',lambda-list
453     (declare (ignorable ,@',vars))
454     ,@,n-decls
455     ,,n-lambda)))))))
456     (if defun-only
457     `(defun ,name ,@stuff)
458     `(%deftransform
459     ',name
460     '(function ,arg-types ,result-type)
461     #'(lambda ,@stuff)))))))
462    
463     ;;;; Defknown, Defoptimizer:
464    
465     ;;; Defknown -- Interface
466     ;;;
467     ;;; This macro should be the way that all implementation independent
468     ;;; information about functions is made known to the compiler.
469     ;;;
470     (defmacro defknown (name arg-types result-type &optional (attributes '(any))
471     &rest keys)
472     "Defknown Name Arg-Types Result-Type [Attributes] {Key Value}*
473     Declare the function Name to be a known function. We construct a type
474     specifier for the function by wrapping (FUNCTION ...) around the Arg-Types
475     and Result-Type. Attributes is a an unevaluated list of the boolean
476     attributes that the function has. These attributes are meaningful here:
477     call
478     May call functions that are passed as arguments. In order to determine
479     what other effects are present, we must find the effects of all arguments
480     that may be functions.
481    
482     unsafe
483     May incorporate arguments in the result or somehow pass them upward.
484    
485     unwind
486     May fail to return during correct execution. Errors are O.K.
487    
488     any
489     The (default) worst case. Includes all the other bad things, plus any
490     other possible bad thing.
491    
492     foldable
493     May be constant-folded. The function has no side effects, but may be
494     affected by side effects on the arguments. e.g. SVREF, MAPC.
495    
496     flushable
497     May be eliminated if value is unused. The function has no side effects
498     except possibly CONS. If a function is defined to signal errors, then
499     it is not flushable even if it is movable or foldable.
500    
501     movable
502     May be moved with impunity. Has no side effects except possibly CONS,
503     and is affected only by its arguments.
504    
505     predicate
506     A true predicate likely to be open-coded. This is a hint to IR1
507     conversion that it should ensure calls always appear as an IF test.
508     Not usually specified to Defknown, since this is implementation
509     dependent, and is usually automatically set by the Define-VOP
510     :Conditional option.
511    
512     Name may also be a list of names, in which case the same information is given
513     to all the names. The keywords specify the initial values for various
514     optimizers that the function might have."
515     (when (and (intersection attributes '(any call unwind))
516     (intersection attributes '(movable)))
517     (error "Function cannot have both good and bad attributes: ~S" attributes))
518    
519     `(%defknown ',(if (and (consp name)
520     (not (eq (car name) 'setf)))
521     name
522     (list name))
523     '(function ,arg-types ,result-type)
524     (ir1-attributes ,@(if (member 'any attributes)
525     (union '(call unsafe unwind) attributes)
526     attributes))
527     ,@keys))
528    
529    
530     ;;; Defoptimizer -- Interface
531     ;;;
532     ;;; Create a function which parses combination args according to a
533     ;;; Lambda-List, optionally storing it in a function-info slot.
534     ;;;
535     (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
536     &rest vars)
537     &body body)
538     "Defoptimizer (Function Kind) (Lambda-List [Node-Var] Var*)
539     Declaration* Form*
540     Define some Kind of optimizer for the named Function. Function must be a
541     known function. Lambda-List is used to parse the arguments to the
542     combination as in Deftransform. If the argument syntax is invalid or there
543     are non-constant keys, then we simply return NIL.
544    
545     The function is DEFUN'ed as Function-Kind-OPTIMIZER. Possible kinds are
546     DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If a symbol is
547     specified instead of a (Function Kind) list, then we just do a DEFUN with the
548     symbol as its name, and don't do anything with the definition. This is
549     useful for creating optimizers to be passed by name to DEFKNOWN.
550    
551     If supplied, Node-Var is bound to the combination node being optimized. If
552     additional Vars are supplied, then they are used as the rest of the optimizer
553     function's lambda-list. LTN-ANNOTATE methods are passed an additional POLICY
554     argument, and IR2-CONVERT methods are passed an additional IR2-BLOCK
555     argument."
556    
557     (let ((name (if (symbolp what) what
558     (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
559    
560     (let ((n-args (gensym)))
561     `(progn
562     (defun ,name (,n-node ,@vars)
563     (let ((,n-args (basic-combination-args ,n-node)))
564     ,(parse-deftransform lambda-list body n-args
565     `(return-from ,name nil))))
566     ,@(when (consp what)
567     `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
568     (function-info-or-lose ',(first what)))
569     #',name)))))))
570    
571    
572     ;;;; IR groveling macros:
573    
574     ;;; Do-Blocks, Do-Blocks-Backwards -- Interface
575     ;;;
576     (defmacro do-blocks ((block-var component &optional ends result) &body body)
577     "Do-Blocks (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
578     Iterate over the blocks in a component, binding Block-Var to each block in
579     turn. The value of Ends determines whether to iterate over dummy head and
580     tail blocks:
581     NIL -- Skip Head and Tail (the default)
582     :Head -- Do head but skip tail
583     :Tail -- Do tail but skip head
584     :Both -- Do both head and tail
585    
586     If supplied, Result-Form is the value to return."
587     (unless (member ends '(nil :head :tail :both))
588     (error "Losing Ends value: ~S." ends))
589     (let ((n-component (gensym))
590     (n-tail (gensym)))
591     `(let* ((,n-component ,component)
592     (,n-tail ,(if (member ends '(:both :tail))
593     nil
594     `(component-tail ,n-component))))
595     (do ((,block-var ,(if (member ends '(:both :head))
596     `(component-head ,n-component)
597     `(block-next (component-head ,n-component)))
598     (block-next ,block-var)))
599     ((eq ,block-var ,n-tail) ,result)
600     ,@body))))
601     ;;;
602     (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
603     "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
604     Like Do-Blocks, only iterate over the blocks in reverse order."
605     (unless (member ends '(nil :head :tail :both))
606     (error "Losing Ends value: ~S." ends))
607     (let ((n-component (gensym))
608     (n-head (gensym)))
609     `(let* ((,n-component ,component)
610     (,n-head ,(if (member ends '(:both :head))
611     nil
612     `(component-head ,n-component))))
613     (do ((,block-var ,(if (member ends '(:both :tail))
614     `(component-tail ,n-component)
615     `(block-prev (component-tail ,n-component)))
616     (block-prev ,block-var)))
617     ((eq ,block-var ,n-head) ,result)
618     ,@body))))
619    
620    
621     ;;; Do-Uses -- Interface
622     ;;;
623     ;;; Could change it not to replicate the code someday perhaps...
624     ;;;
625     (defmacro do-uses ((node-var continuation &optional result) &body body)
626     "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
627     Iterate over the uses of Continuation, binding Node to each one succesively."
628     (once-only ((n-cont continuation))
629     `(ecase (continuation-kind ,n-cont)
630     (:unused)
631     (:inside-block
632     (block nil
633     (let ((,node-var (continuation-use ,n-cont)))
634     ,@body
635     ,result)))
636     ((:block-start :deleted-block-start)
637     (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
638     ,result)
639     ,@body)))))
640    
641    
642     ;;; Do-Nodes, Do-Nodes-Backwards -- Interface
643     ;;;
644     ;;; In the forward case, we terminate on Last-Cont so that we don't have to
645     ;;; worry about our termination condition being changed when new code is added
646     ;;; during the iteration. In the backward case, we do NODE-PREV before
647     ;;; evaluating the body so that we can keep going when the current node is
648     ;;; deleted.
649     ;;;
650     (defmacro do-nodes ((node-var cont-var block &optional result) &body body)
651     "Do-Nodes (Node-Var Cont-Var Block [Result]) {Declaration}* {Form}*
652     Iterate over the nodes in Block, binding Node-Var to the each node and
653     Cont-Var to the node's Cont."
654     (let ((n-block (gensym))
655     (n-last-cont (gensym)))
656     `(let* ((,n-block ,block)
657     (,n-last-cont (node-cont (block-last ,n-block))))
658     (do* ((,node-var (continuation-next (block-start ,n-block))
659     (continuation-next ,cont-var))
660     (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
661     (())
662     ,@body
663     (when (eq ,cont-var ,n-last-cont)
664     (return ,result))))))
665     ;;;
666     (defmacro do-nodes-backwards ((node-var cont-var block &optional result)
667     &body body)
668     "Do-Nodes-Backwards (Node-Var Cont-Var Block [Result]) {Declaration}* {Form}*
669     Like Do-Nodes, only iterates in reverse order."
670     (let ((n-block (gensym))
671     (n-start (gensym))
672     (n-last (gensym))
673     (n-next (gensym)))
674     `(let* ((,n-block ,block)
675     (,n-start (block-start ,n-block))
676     (,n-last (block-last ,n-block)))
677     (do* ((,cont-var (node-cont ,n-last) ,n-next)
678     (,node-var ,n-last (continuation-use ,cont-var))
679     (,n-next (node-prev ,node-var) (node-prev ,node-var)))
680     (())
681     ,@body
682     (when (eq ,n-next ,n-start)
683     (return ,result))))))
684    
685    
686     ;;; With-IR1-Environment -- Interface
687     ;;;
688     ;;; The lexical environment is presumably already null...
689     ;;;
690     (defmacro with-ir1-environment (node &rest forms)
691     "With-IR1-Environment Node Form*
692     Bind the IR1 context variables so that IR1 conversion can be done after the
693 ram 1.7 main conversion pass has finished."
694     (let ((n-node (gensym)))
695 wlott 1.1 `(let* ((,n-node ,node)
696 ram 1.7 (*current-component* (block-component (node-block ,n-node)))
697     (*lexical-environment* (node-lexenv ,n-node))
698     (*current-path* (node-source-path ,n-node)))
699 wlott 1.1 ,@forms)))
700    
701    
702     ;;; WITH-IR1-NAMESPACE -- Interface
703     ;;;
704     ;;; Bind the hashtables used for keeping track of global variables,
705     ;;; functions, &c.
706     ;;;
707     (defmacro with-ir1-namespace (&body forms)
708     `(let ((*free-variables* (make-hash-table :test #'eq))
709     (*free-functions* (make-hash-table :test #'equal))
710     (*constants* (make-hash-table :test #'equal))
711 ram 1.9 (*source-paths* (make-hash-table :test #'eq))
712     (*failed-optimizations* (make-hash-table :test #'eq)))
713 wlott 1.1 ,@forms))
714    
715 ram 1.7
716     ;;; LEXENV-FIND -- Interface
717     ;;;
718     (defmacro lexenv-find (name slot &key test)
719     "LEXENV-FIND Name Slot {Key Value}*
720     Look up Name in the lexical environment namespace designated by Slot,
721     returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
722     may be used to determine the name equality predicate."
723     (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot)
724     *lexical-environment*)
725     ,@(when test `(:test ,test)))))
726     `(if ,n-res
727     (values (cdr ,n-res) t)
728     (values nil nil))))
729    
730 wlott 1.1
731     ;;;; The Defprinter macro:
732    
733     (defvar *defprint-pretty* nil
734     "If true, defprinter print functions print each slot on a separate line.")
735    
736    
737     ;;; Defprinter-Prin1, Defprinter-Princ -- Internal
738     ;;;
739     ;;; These functions are called by the expansion of the Defprinter
740     ;;; macro to do the actual printing.
741     ;;;
742     (proclaim '(ftype (function (symbol t stream fixnum) void)
743     defprinter-prin1 defprinter-princ))
744     (defun defprinter-prin1 (name value stream indent)
745     (if *defprint-pretty*
746     (format stream "~&~VT ~A:~%~VT ~S" indent name indent value)
747     (format stream " ~A= ~S" name value)))
748     ;;;
749     (defun defprinter-princ (name value stream indent)
750     (if *defprint-pretty*
751     (format stream "~&~VT ~A:~%~VT ~A" indent name indent value)
752     (format stream " ~A= ~A" name value)))
753    
754     ;;; Start-Defprinter, Finish-Defprinter -- Internal
755     ;;;
756     ;;; Start and finish the the printing of a defprinter function.
757     ;;;
758     (defun start-defprinter (name stream indent object)
759 ram 1.6 (declare (symbol name) (stream stream) (type (or index null) indent))
760 wlott 1.1 (declare (ignore indent))
761     (format stream "#<~S ~X" name (system:%primitive make-fixnum object)))
762     ;;;
763     (defun finish-defprinter (name stream indent)
764 ram 1.6 (declare (symbol name) (stream stream) (type (or index null) indent))
765 wlott 1.1 (declare (ignore name))
766 ram 1.6 (if (and *defprint-pretty* indent)
767 wlott 1.1 (format stream ">~%~VT" indent)
768     (format stream ">")))
769    
770     (defmacro defprinter (name &rest slots)
771     "Defprinter Name Slot-Desc*
772     Define some kind of reasonable defstruct structure-print function. Name
773     is the name of the structure. We define a function %PRINT-name which
774     prints the slots in the structure in the way described by the Slot-Descs.
775     Each Slot-Desc can be a slot name, indicating that the slot should simply
776     be printed. A Slot-Desc may also be a list of a slot name and other stuff.
777     The other stuff is composed of keywords followed by expressions. The
778     expressions are evaluated with the variable which is the slot name bound
779     to the value of the slot. These keywords are defined:
780    
781     :PRIN1 Print the value of the expression instead of the slot value.
782     :PRINC Like :PRIN1, only princ the value
783     :TEST Only print something if the test is true.
784    
785     If no printing thing is specified then the slot value is printed as PRIN1.
786    
787     The structure being printed is bound to Structure and the stream is bound to
788     Stream."
789    
790     (let ((n-indent (gensym)))
791     (flet ((sref (slot) `(,(symbolicate name "-" slot) structure)))
792     (collect ((prints))
793     (dolist (slot slots)
794     (if (atom slot)
795     (prints `(defprinter-prin1 ',slot ,(sref slot) stream ,n-indent))
796     (let ((sname (first slot))
797     (test t))
798     (collect ((stuff))
799     (do ((option (rest slot) (cddr option)))
800     ((null option)
801     (prints
802     `(let ((,sname ,(sref sname)))
803     (when ,test
804     ,@(or (stuff)
805     `((defprinter-prin1 ',sname ,sname
806     stream ,n-indent)))))))
807     (case (first option)
808     (:prin1
809     (stuff `(defprinter-prin1 ',sname ,(second option)
810     stream ,n-indent)))
811     (:princ
812     (stuff `(defprinter-princ ',sname ,(second option)
813     stream ,n-indent)))
814     (:test (setq test (second option)))
815     (t
816     (error "Losing Defprinter option: ~S." (first option)))))))))
817    
818     `(defun ,(symbolicate "%PRINT-" name) (structure stream depth)
819     (let ((,n-indent (lisp::charpos stream)))
820     (start-defprinter ',name stream ,n-indent structure)
821     (let ((*print-level* (if *print-level* (- *print-level* depth 1))))
822     (unless (and *print-level* (<= *print-level* 0))
823     ,@(prints))
824     (finish-defprinter ',name stream ,n-indent)
825     nil)))))))
826    
827    
828     ;;;; Boolean attribute utilities:
829     ;;;
830     ;;; We need to maintain various sets of boolean attributes for known
831     ;;; functions and VOPs. To save space and allow for quick set operations, we
832     ;;; represent them as bits in a fixnum.
833     ;;;
834    
835     (deftype attributes () 'fixnum)
836    
837 ram 1.7 (eval-when (compile load eval)
838 wlott 1.1 ;;; Compute-Attribute-Mask -- Internal
839     ;;;
840     ;;; Given a list of attribute names and an alist that translates them to
841     ;;; masks, return the OR of the masks.
842     ;;;
843     (defun compute-attribute-mask (names alist)
844     (collect ((res 0 logior))
845     (dolist (name names)
846     (let ((mask (cdr (assoc name alist))))
847     (unless mask
848     (error "Unknown attribute name: ~S." name))
849     (res mask)))
850     (res)))
851    
852     ); Eval-When (Compile Load Eval)
853    
854     ;;; Def-Boolean-Attribute -- Interface
855     ;;;
856     ;;; Parse the specification and generate some accessor macros.
857     ;;;
858     (defmacro def-boolean-attribute (name &rest attribute-names)
859     "Def-Boolean-Attribute Name Attribute-Name*
860     Define a new class of boolean attributes, with the attributes havin the
861     specified Attribute-Names. Name is the name of the class, which is used to
862     generate some macros to manipulate sets of the attributes:
863    
864     NAME-attributep attributes attribute-name*
865     Return true if one of the named attributes is present, false otherwise.
866 ram 1.7 When set with SETF, updates the place Attributes setting or clearing the
867     specified attributes.
868 wlott 1.1
869     NAME-attributes attribute-name*
870     Return a set of the named attributes."
871    
872 ram 1.7 (let ((const-name (symbolicate name "-ATTRIBUTE-TRANSLATIONS"))
873     (test-name (symbolicate name "-ATTRIBUTEP")))
874 wlott 1.1 (collect ((alist))
875     (do ((mask 1 (ash mask 1))
876     (names attribute-names (cdr names)))
877     ((null names))
878     (alist (cons (car names) mask)))
879    
880     `(progn
881 ram 1.7 (eval-when (compile load eval)
882     (defconstant ,const-name ',(alist)))
883    
884     (defmacro ,test-name (attributes &rest attribute-names)
885     "Automagically generated boolean attribute test function. See
886     Def-Boolean-Attribute."
887     `(logtest ,(compute-attribute-mask attribute-names ,const-name)
888     (the attributes ,attributes)))
889 wlott 1.1
890 ram 1.7 (define-setf-method ,test-name (place &rest attributes
891     &environment env)
892    
893     "Automagically generated boolean attribute setter. See
894     Def-Boolean-Attribute."
895     (multiple-value-bind (temps values stores set get)
896     (lisp::foo-get-setf-method place env)
897     (let ((newval (gensym))
898     (n-place (gensym))
899     (mask (compute-attribute-mask attributes ,const-name)))
900     (values `(,@temps ,n-place)
901     `(,@values ,get)
902     `(,newval)
903     `(let ((,(first stores)
904     (if ,newval
905     (logior ,n-place ,mask)
906     (logand ,n-place ,(lognot mask)))))
907     ,set
908     ,newval)
909     `(,',test-name ,n-place ,@attributes)))))
910    
911     (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
912     "Automagically generated boolean attribute creation function. See
913     Def-Boolean-Attribute."
914     (compute-attribute-mask attribute-names ,const-name))))))
915 wlott 1.1
916    
917     ;;; Attributes-Union, Attributes-Intersection, Attributes= -- Interface
918     ;;;
919     ;;; And now for some gratuitous pseudo-abstraction...
920     ;;;
921     (defmacro attributes-union (&rest attributes)
922     "Returns the union of all the sets of boolean attributes which are its
923     arguments."
924     `(the attributes
925     (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
926     ;;;
927     (defmacro attributes-intersection (&rest attributes)
928     "Returns the intersection of all the sets of boolean attributes which are its
929     arguments."
930     `(the attributes
931     (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
932     ;;;
933     (proclaim '(inline attributes=))
934     (proclaim '(function attributes= (attributes attributes) boolean))
935     (defun attributes= (attr1 attr2)
936     "Returns true if the attributes present in Attr1 are indentical to those in
937     Attr2."
938     (eql attr1 attr2))
939    
940    
941     ;;;; The Event statistics/trace utility:
942    
943 ram 1.3 (eval-when (#-new-compiler compile load eval)
944 wlott 1.1
945     (defstruct event-info
946     ;;
947     ;; The name of this event.
948     (name nil :type symbol)
949     ;;
950     ;; The string rescribing this event.
951     (description nil :type string)
952     ;;
953     ;; The name of the variable we stash this in.
954     (var nil :type symbol)
955     ;;
956     ;; The number of times this event has happened.
957     (count 0 :type fixnum)
958     ;;
959     ;; The level of significance of this event.
960     (level nil :type unsigned-byte)
961     ;;
962     ;; If true, a function that gets called with the node that the event happened
963     ;; to.
964     (action nil :type (or function null)))
965    
966    
967     ;;; A hashtable from event names to event-info structures.
968     ;;;
969     (defvar *event-info* (make-hash-table :test #'eq))
970    
971    
972     ;;; Event-Info-Or-Lose -- Internal
973     ;;;
974     ;;; Return the event info for Name or die trying.
975     ;;;
976     (proclaim '(function event-info-or-lose (t) event-info))
977     (defun event-info-or-lose (name)
978     (let ((res (gethash name *event-info*)))
979     (unless res
980     (error "~S is not the name of an event." name))
981     res))
982    
983     ); Eval-When (Compile Load Eval)
984    
985    
986     ;;; Event-Count, Event-Action, Event-Level -- Interface
987     ;;;
988     (proclaim '(function event-count (symbol) fixnum))
989     (defun event-count (name)
990     "Return the number of times that Event has happened."
991     (event-info-count (event-info-or-lose name)))
992     ;;;
993     (proclaim '(function event-action (symbol) (or function null)))
994     (defun event-action (name)
995     "Return the function that is called when Event happens. If this is null,
996     there is no action. The function is passed the node to which the event
997     happened, or NIL if there is no relevant node. This may be set with SETF."
998     (event-info-action (event-info-or-lose name)))
999     ;;;
1000     (proclaim '(function %set-event-action (symbol (or function null)) (or function null)))
1001     (defun %set-event-action (name new-value)
1002     (setf (event-info-action (event-info-or-lose name))
1003     new-value))
1004     ;;;
1005     (defsetf event-action %set-event-action)
1006     ;;;
1007     (proclaim '(function event-level (symbol) unsigned-byte))
1008     (defun event-level (name)
1009     "Return the non-negative integer which represents the level of significance
1010     of the event Name. This is used to determine whether to print a message when
1011     the event happens. This may be set with SETF."
1012     (event-info-level (event-info-or-lose name)))
1013     ;;;
1014     (proclaim '(function %set-event-level (symbol unsigned-byte) unsigned-byte))
1015     (defun %set-event-level (name new-value)
1016     (setf (event-info-level (event-info-or-lose name))
1017     new-value))
1018     ;;;
1019     (defsetf event-level %set-event-level)
1020    
1021    
1022     ;;; Defevent -- Interface
1023     ;;;
1024     ;;; Make an event-info structure and stash it in a variable so we can get at
1025     ;;; it quickly.
1026     ;;;
1027     (defmacro defevent (name description &optional (level 0))
1028     "Defevent Name Description
1029     Define a new kind of event. Name is a symbol which names the event and
1030     Description is a string which describes the event. Level (default 0) is the
1031     level of significance associated with this event; it is used to determine
1032     whether to print a Note when the event happens."
1033     (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
1034     `(eval-when (compile load eval)
1035     (defvar ,var-name
1036     (make-event-info :name ',name :description ',description :var ',var-name
1037     :level ,level))
1038     (setf (gethash ',name *event-info*) ,var-name)
1039     ',name)))
1040    
1041     (proclaim '(type unsigned-byte *event-note-threshold*))
1042     (defvar *event-note-threshold* 1
1043     "This variable is a non-negative integer specifying the lowest level of
1044     event that will print a Note when it occurs.")
1045    
1046     ;;; Event -- Interface
1047     ;;;
1048     ;;; Increment the counter and do any action. Mumble about the event if
1049     ;;; policy indicates.
1050     ;;;
1051     (defmacro event (name &optional node)
1052     "Event Name Node
1053     Note that the event with the specified Name has happened. Node is evaluated
1054     to determine the node to which the event happened."
1055     `(%event ,(event-info-var (event-info-or-lose name)) ,node))
1056     ;;;
1057     (proclaim '(function %event (event-info (or node null))))
1058     (defun %event (info node)
1059     (incf (event-info-count info))
1060     (when (and (>= (event-info-level info) *event-note-threshold*)
1061     (if node
1062     (policy node (= brevity 0))
1063     (policy nil (= brevity 0))))
1064     (let ((*compiler-error-context* node))
1065     (compiler-note (event-info-description info))))
1066    
1067     (let ((action (event-info-action info)))
1068     (when action (funcall action node))))
1069    
1070    
1071     ;;; Event-Statistics, Clear-Statistics -- Interface
1072     ;;;
1073     (proclaim '(function event-statistics (&optional unsigned-byte stream) void))
1074     (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
1075     "Print a listing of events and their counts, sorted by the count. Events
1076     that happened fewer than Min-Count times will not be printed. Stream is the
1077     stream to write to."
1078     (collect ((info))
1079     (maphash #'(lambda (k v)
1080     (declare (ignore k))
1081     (when (>= (event-info-count v) min-count)
1082     (info v)))
1083     *event-info*)
1084     (dolist (event (sort (info) #'> :key #'event-info-count))
1085     (format stream "~6D: ~A~%" (event-info-count event)
1086     (event-info-description event)))
1087     (values)))
1088     ;;;
1089     (proclaim '(function clear-statistics () void))
1090     (defun clear-statistics ()
1091     (maphash #'(lambda (k v)
1092     (declare (ignore k))
1093     (setf (event-info-count v) 0))
1094     *event-info*)
1095     (values))
1096    

  ViewVC Help
Powered by ViewVC 1.1.5