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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.53 - (show annotations)
Wed Apr 7 02:47:53 2004 UTC (10 years ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2004-08, snapshot-2004-09, snapshot-2004-05, snapshot-2004-06, snapshot-2004-07, amd64-merge-start, prm-before-macosx-merge-tag, release-19a-base, release-19a-pre1, release-19a-pre3, release-19a-pre2, release-19a
Branch point for: release-19a-branch
Changes since 1.52: +108 -1 lines
Merge in the mod-arith-branch with support for x86 and sparc.
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/macros.lisp,v 1.53 2004/04/07 02:47:53 rtoy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Random types and macros used in writing the compiler.
13 ;;;
14 ;;; Written by Rob MacLachlan
15 ;;;
16 (in-package "C")
17
18 (export '(lisp::with-compilation-unit) "LISP")
19
20 (export '(policy symbolicate def-ir1-translator def-source-transform
21 def-primitive-translator deftransform defknown defoptimizer
22 derive-type optimizer ltn-annotate ir2-convert attributes
23 def-boolean-attribute attributes-union attributes-intersection
24 attributes=))
25
26 (declaim (special *wild-type* *universal-type* *compiler-error-context*))
27
28 ;;;; Deftypes:
29
30 ;;;
31 ;;; Inlinep is used to determine how a function is called. The values have
32 ;;; these meanings:
33 ;;; Nil No declaration seen: do whatever you feel like, but don't dump
34 ;;; an inline expansion.
35 ;;;
36 ;;; :Notinline Notinline declaration seen: always do full function call.
37 ;;;
38 ;;; :Inline Inline declaration seen: save expansion, expanding to it if
39 ;;; policy favors.
40 ;;;
41 ;;; :Maybe-Inline
42 ;;; Retain expansion, but only use it opportunistically.
43 ;;;
44 (deftype inlinep () '(member :inline :maybe-inline :notinline nil))
45
46
47 ;;;; The Policy macro:
48
49 (declaim (special *lexical-environment*))
50
51 (eval-when (compile load eval)
52 (defconstant policy-parameter-slots
53 '((speed . cookie-speed) (space . cookie-space) (safety . cookie-safety)
54 (cspeed . cookie-cspeed) (brevity . cookie-brevity)
55 (debug . cookie-debug)))
56
57 ;;; Find-Used-Parameters -- Internal
58 ;;;
59 ;;; Find all the policy parameters which are actually mentioned in Stuff,
60 ;;; returning the names in a list. We assume everything is evaluated.
61 ;;;
62 (defun find-used-parameters (stuff)
63 (if (atom stuff)
64 (if (assoc stuff policy-parameter-slots) (list stuff) ())
65 (collect ((res () nunion))
66 (dolist (arg (cdr stuff) (res))
67 (res (find-used-parameters arg))))))
68
69 ); Eval-When (Compile Load Eval)
70
71 ;;; Policy -- Public
72 ;;;
73 ;;; This macro provides some syntactic sugar for querying the settings of
74 ;;; the compiler policy parameters.
75 ;;;
76 (defmacro policy (node &rest conditions)
77 "Policy Node Condition*
78 Test whether some conditions apply to the current compiler policy for Node.
79 Each condition is a predicate form which accesses the policy values by
80 referring to them as the variables SPEED, SPACE, SAFETY, CSPEED, BREVITY and
81 DEBUG. The results of all the conditions are combined with AND and returned
82 as the result.
83
84 Node is a form which is evaluated to obtain the node which the policy is for.
85 If Node is NIL, then we use the current policy as defined by *default-cookie*
86 and *current-cookie*. This option is only well defined during IR1
87 conversion."
88 (let* ((form `(and ,@conditions))
89 (n-cookie (gensym))
90 (binds (mapcar
91 #'(lambda (name)
92 (let ((slot (cdr (assoc name policy-parameter-slots))))
93 `(,name (,slot ,n-cookie))))
94 (find-used-parameters form))))
95 `(let* ((,n-cookie (lexenv-cookie
96 ,(if node
97 `(node-lexenv ,node)
98 '*lexical-environment*)))
99 ,@binds)
100 ,form)))
101
102
103 ;;;; Source-hacking defining forms:
104
105 (eval-when (compile load eval)
106
107 ;;; Symbolicate -- Interface
108 ;;;
109 ;;; Concatenate together the names of some strings and symbols, producing
110 ;;; a symbol in the current package.
111 ;;;
112 (declaim (function symbolicate (&rest (or string symbol)) symbol))
113 (defun symbolicate (&rest things)
114 (declare (values symbol))
115 (values (intern (reduce #'(lambda (x y)
116 (concatenate 'string (string x) (string y)))
117 things))))
118
119 ); Eval-When (Compile Load Eval)
120
121 ;;; SPECIAL-FORM-FUNCTION -- Internal
122 ;;;
123 ;;; This function is stored in the SYMBOL-FUNCTION of special form names so
124 ;;; that they are FBOUND.
125 ;;;
126 (defun special-form-function (&rest stuff)
127 (declare (ignore stuff))
128 (error 'simple-undefined-function
129 :format-control "Can't funcall the SYMBOL-FUNCTION of special forms."))
130
131 ;;; CONVERT-CONDITION-INTO-COMPILER-ERROR -- Internal
132 ;;;
133 ;;; Passed to parse-defmacro when we want compiler errors instead of real
134 ;;; errors.
135 ;;;
136 (declaim (inline convert-condition-into-compiler-error))
137 (defun convert-condition-into-compiler-error (datum &rest stuff)
138 (if (stringp datum)
139 (apply #'compiler-error datum stuff)
140 (compiler-error "~A"
141 (if (symbolp datum)
142 (apply #'make-condition datum stuff)
143 datum))))
144
145 ;;; Def-IR1-Translator -- Interface
146 ;;;
147 ;;; Parse defmacro style lambda-list, setting things up so that a compiler
148 ;;; error happens if the syntax is invalid.
149 ;;;
150 (defmacro def-ir1-translator (name (lambda-list start-var cont-var
151 &key (kind :special-form))
152 &body body)
153 "Def-IR1-Translator Name (Lambda-List Start-Var Cont-Var {Key Value}*)
154 [Doc-String] Form*
155 Define a function that converts a Special-Form or other magical thing into
156 IR1. Lambda-List is a defmacro style lambda list. Start-Var and Cont-Var
157 are bound to the start and result continuations for the resulting IR1.
158 This keyword is defined:
159 Kind
160 The function kind to associate with Name (default :special-form)."
161 (let ((fn-name (symbolicate "IR1-CONVERT-" name))
162 (n-form (gensym))
163 (n-env (gensym)))
164 (multiple-value-bind
165 (body decls doc)
166 (lisp::parse-defmacro lambda-list n-form body name "special form"
167 :doc-string-allowed t
168 :environment n-env
169 :error-fun 'convert-condition-into-compiler-error)
170 `(progn
171 (declaim (function ,fn-name (continuation continuation t) void))
172 (defun ,fn-name (,start-var ,cont-var ,n-form)
173 (let ((,n-env *lexical-environment*))
174 ,@decls
175 ,body))
176 ,@(when doc
177 `((setf (documentation ',name 'function) ,doc)))
178 (setf (info function ir1-convert ',name) #',fn-name)
179 (setf (info function kind ',name) ,kind)
180 ,@(when (eq kind :special-form)
181 `((setf (symbol-function ',name) #'special-form-function)))))))
182
183
184 ;;; Def-Source-Transform -- Interface
185 ;;;
186 ;;; Similar to Def-IR1-Translator, except that we pass if the syntax is
187 ;;; invalid.
188 ;;;
189 (defmacro def-source-transform (name lambda-list &body body)
190 "Def-Source-Transform Name Lambda-List Form*
191 Define a macro-like source-to-source transformation for the function Name.
192 A source transform may \"pass\" by returning a non-nil second value. If the
193 transform passes, then the form is converted as a normal function call. If
194 the supplied arguments are not compatible with the specified lambda-list,
195 then the transform automatically passes.
196
197 Source-Transforms may only be defined for functions. Source transformation
198 is not attempted if the function is declared Notinline. Source transforms
199 should not examine their arguments. If it matters how the function is used,
200 then Deftransform should be used to define an IR1 transformation.
201
202 If the desirability of the transformation depends on the current Optimize
203 parameters, then the Policy macro should be used to determine when to pass."
204 (let ((fn-name
205 (if (listp name)
206 (collect ((pieces))
207 (dolist (piece name)
208 (pieces "-")
209 (pieces piece))
210 (apply #'symbolicate "SOURCE-TRANSFORM" (pieces)))
211 (symbolicate "SOURCE-TRANSFORM-" name)))
212 (n-form (gensym))
213 (n-env (gensym)))
214 (multiple-value-bind
215 (body decls)
216 (lisp::parse-defmacro lambda-list n-form body name "form"
217 :environment n-env
218 :error-fun `(lambda (&rest stuff)
219 (declare (ignore stuff))
220 (return-from ,fn-name
221 (values nil t))))
222 `(progn
223 (defun ,fn-name (,n-form)
224 (let ((,n-env *lexical-environment*))
225 ,@decls
226 ,body))
227 (setf (info function source-transform ',name) #',fn-name)))))
228
229
230 (defmacro def-primitive-translator (name lambda-list &body body)
231 "Def-Primitive-Translator Name Lambda-List Form*
232 Define a function that converts a use of (%PRIMITIVE Name ...) into Lisp
233 code. Lambda-List is a defmacro style lambda list."
234 (let ((fn-name (symbolicate "PRIMITIVE-TRANSLATE-" name))
235 (n-form (gensym))
236 (n-env (gensym)))
237 (multiple-value-bind
238 (body decls)
239 (lisp::parse-defmacro lambda-list n-form body name "%primitive"
240 :environment n-env
241 :error-fun 'convert-condition-into-compiler-error)
242 `(progn
243 (defun ,fn-name (,n-form)
244 (let ((,n-env *lexical-environment*))
245 ,@decls
246 ,body))
247 (setf (gethash ',name *primitive-translators*) ',fn-name)))))
248
249
250 ;;;; Lambda-list parsing utilities:
251 ;;;
252 ;;; IR1 transforms, optimizers and type inferencers need to be able to parse
253 ;;; the IR1 representation of a function call using a standard function
254 ;;; lambda-list.
255
256
257 (eval-when (compile load eval)
258
259 ;;; Parse-Deftransform -- Internal
260 ;;;
261 ;;; Given a deftransform style lambda-list, generate code that parses the
262 ;;; arguments of a combination with respect to that lambda-list. Body is the
263 ;;; the list of forms which are to be evaluated within the bindings. Args is
264 ;;; the variable that holds list of argument continuations. Error-Form is a
265 ;;; form which is evaluated when the syntax of the supplied arguments is
266 ;;; incorrect or a non-constant argument keyword is supplied. Defaults and
267 ;;; other gunk are ignored. The second value is a list of all the arguments
268 ;;; bound. We make the variables IGNORABLE so that we don't have to manually
269 ;;; declare them Ignore if their only purpose is to make the syntax work.
270 ;;;
271 (defun parse-deftransform (lambda-list body args error-form)
272 (declare (list lambda-list body) (symbol args))
273 (multiple-value-bind (req opt restp rest keyp keys allowp)
274 (parse-lambda-list lambda-list)
275 (let* ((min-args (length req))
276 (max-args (+ min-args (length opt)))
277 (n-keys (gensym)))
278 (collect ((binds)
279 (vars)
280 (pos 0 +)
281 (keywords))
282 (dolist (arg req)
283 (vars arg)
284 (binds `(,arg (nth ,(pos) ,args)))
285 (pos 1))
286
287 (dolist (arg opt)
288 (let ((var (if (atom arg) arg (first arg))))
289 (vars var)
290 (binds `(,var (nth ,(pos) ,args)))
291 (pos 1)))
292
293 (when restp
294 (vars rest)
295 (binds `(,rest (nthcdr ,(pos) ,args))))
296
297 (dolist (spec keys)
298 (if (or (atom spec) (atom (first spec)))
299 (let* ((var (if (atom spec) spec (first spec)))
300 (key (intern (symbol-name var) "KEYWORD")))
301 (vars var)
302 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
303 (keywords key))
304 (let* ((head (first spec))
305 (var (second head))
306 (key (first head)))
307 (vars var)
308 (binds `(,var (find-keyword-continuation ,n-keys ,key)))
309 (keywords key))))
310
311 (let ((n-length (gensym))
312 (limited-legal (not (or restp keyp))))
313 (values
314 `(let ((,n-length (length ,args))
315 ,@(when keyp `((,n-keys (nthcdr ,(pos) ,args)))))
316 (unless (and
317 ,(if limited-legal
318 `(<= ,min-args ,n-length ,max-args)
319 `(<= ,min-args ,n-length))
320 ,@(when keyp
321 (if allowp
322 `((check-keywords-constant ,n-keys))
323 `((check-transform-keys ,n-keys ',(keywords))))))
324 ,error-form)
325 (let ,(binds)
326 (declare (ignorable ,@(vars)))
327 ,@body))
328 (vars)))))))
329
330 ); Eval-When (Compile Load Eval)
331
332
333 ;;;; Deftransform:
334
335 ;;; Deftransform -- Interface
336 ;;;
337 ;;; Parse the lambda-list and generate code to test the policy and
338 ;;; automatically create the result lambda.
339 ;;;
340 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
341 (result-type '*)
342 &key result policy node defun-only
343 eval-name important (when :native))
344 &parse-body (body decls doc))
345 "Deftransform Name (Lambda-List [Arg-Types] [Result-Type] {Key Value}*)
346 Declaration* [Doc-String] Form*
347 Define an IR1 transformation for Name. An IR1 transformation computes a
348 lambda that replaces the function variable reference for the call. A
349 transform may pass (decide not to transform the call) by calling the Give-Up
350 function. Lambda-List both determines how the current call is parsed and
351 specifies the Lambda-List for the resulting lambda.
352
353 We parse the call and bind each of the lambda-list variables to the
354 continuation which represents the value of the argument. When parsing the
355 call, we ignore the defaults, and always bind the variables for unsupplied
356 arguments to NIL. If a required argument is missing, an unknown keyword is
357 supplied, or an argument keyword is not a constant, then the transform
358 automatically passes. The Declarations apply to the bindings made by
359 Deftransform at transformation time, rather than to the variables of the
360 resulting lambda. Bound-but-not-referenced warnings are suppressed for the
361 lambda-list variables. The Doc-String is used when printing efficiency notes
362 about the defined transform.
363
364 Normally, the body evaluates to a form which becomes the body of an
365 automatically constructed lambda. We make Lambda-List the lambda-list for
366 the lambda, and automatically insert declarations of the argument and result
367 types. If the second value of the body is non-null, then it is a list of
368 declarations which are to be inserted at the head of the lambda. Automatic
369 lambda generation may be inhibited by explicitly returning a lambda from the
370 body.
371
372 The Arg-Types and Result-Type are used to create a function type which the
373 call must satisfy before transformation is attempted. The function type
374 specifier is constructed by wrapping (FUNCTION ...) around these values, so
375 the lack of a restriction may be specified by omitting the argument or
376 supplying *. The argument syntax specified in the Arg-Types need not be the
377 same as that in the Lambda-List, but the transform will never happen if
378 the syntaxes can't be satisfied simultaneously. If there is an existing
379 transform for the same function that has the same type, then it is replaced
380 with the new definition.
381
382 These are the legal keyword options:
383 :Result - A variable which is bound to the result continuation.
384 :Node - A variable which is bound to the combination node for the call.
385 :Policy - A form which is supplied to the Policy macro to determine whether
386 this transformation is appropriate. If the result is false, then
387 the transform automatically passes.
388 :Eval-Name
389 - The name and argument/result types are actually forms to be
390 evaluated. Useful for getting closures that transform similar
391 functions.
392 :Defun-Only
393 - Don't actually instantiate a transform, instead just DEFUN
394 Name with the specified transform definition function. This may
395 be later instantiated with %Deftransform.
396 :Important
397 - If supplied and non-NIL, note this transform as ``important,''
398 which means effeciency notes will be generated when this
399 transform fails even if brevity=speed (but not if brevity>speed)
400 :When {:Native | :Byte | :Both}
401 - Indicates whether this transform applies to native code,
402 byte-code or both (default :native.)"
403
404 (when (and eval-name defun-only)
405 (error "Can't specify both DEFUN-ONLY and EVAL-NAME."))
406 (let ((n-args (gensym))
407 (n-node (or node (gensym)))
408 (n-decls (gensym))
409 (n-lambda (gensym))
410 (body `(,@decls ,@body)))
411 (multiple-value-bind (parsed-form vars)
412 (parse-deftransform
413 lambda-list
414 (if policy
415 `((unless (policy ,n-node ,policy) (give-up))
416 ,@body)
417 body)
418 n-args '(give-up))
419 (let ((stuff
420 `((,n-node)
421 (let* ((,n-args (basic-combination-args ,n-node))
422 ,@(when result
423 `((,result (node-cont ,n-node)))))
424 (multiple-value-bind (,n-lambda ,n-decls)
425 ,parsed-form
426 (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
427 ,n-lambda
428 `(lambda ,',lambda-list
429 (declare (ignorable ,@',vars))
430 ,@,n-decls
431 ,,n-lambda)))))))
432 (if defun-only
433 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
434 `(%deftransform
435 ,(if eval-name name `',name)
436 ,(if eval-name
437 ``(function ,,arg-types ,,result-type)
438 `'(function ,arg-types ,result-type))
439 #'(lambda ,@stuff)
440 ,doc
441 ,(if important t nil)
442 ,when))))))
443
444 ;;;; Defknown, Defoptimizer:
445
446 ;;; Defknown -- Interface
447 ;;;
448 ;;; This macro should be the way that all implementation independent
449 ;;; information about functions is made known to the compiler.
450 ;;;
451 (defmacro defknown (name arg-types result-type &optional (attributes '(any))
452 &rest keys)
453 "Defknown Name Arg-Types Result-Type [Attributes] {Key Value}*
454 Declare the function Name to be a known function. We construct a type
455 specifier for the function by wrapping (FUNCTION ...) around the Arg-Types
456 and Result-Type. Attributes is a an unevaluated list of the boolean
457 attributes that the function has. These attributes are meaningful here:
458 call
459 May call functions that are passed as arguments. In order to determine
460 what other effects are present, we must find the effects of all arguments
461 that may be functions.
462
463 unsafe
464 May incorporate arguments in the result or somehow pass them upward.
465
466 unwind
467 May fail to return during correct execution. Errors are O.K.
468
469 any
470 The (default) worst case. Includes all the other bad things, plus any
471 other possible bad thing.
472
473 foldable
474 May be constant-folded. The function has no side effects, but may be
475 affected by side effects on the arguments. e.g. SVREF, MAPC.
476
477 flushable
478 May be eliminated if value is unused. The function has no side effects
479 except possibly CONS. If a function is defined to signal errors, then
480 it is not flushable even if it is movable or foldable.
481
482 movable
483 May be moved with impunity. Has no side effects except possibly CONS,
484 and is affected only by its arguments.
485
486 predicate
487 A true predicate likely to be open-coded. This is a hint to IR1
488 conversion that it should ensure calls always appear as an IF test.
489 Not usually specified to Defknown, since this is implementation
490 dependent, and is usually automatically set by the Define-VOP
491 :Conditional option.
492
493 Name may also be a list of names, in which case the same information is given
494 to all the names. The keywords specify the initial values for various
495 optimizers that the function might have."
496 (when (and (intersection attributes '(any call unwind))
497 (intersection attributes '(movable)))
498 (error "Function cannot have both good and bad attributes: ~S" attributes))
499
500 `(%defknown ',(if (and (consp name)
501 (not (eq (car name) 'setf)))
502 name
503 (list name))
504 '(function ,arg-types ,result-type)
505 (ir1-attributes ,@(if (member 'any attributes)
506 (union '(call unsafe unwind) attributes)
507 attributes))
508 ,@keys))
509
510
511 ;;; Defoptimizer -- Interface
512 ;;;
513 ;;; Create a function which parses combination args according to a
514 ;;; Lambda-List, optionally storing it in a function-info slot.
515 ;;;
516 (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
517 &rest vars)
518 &body body)
519 "Defoptimizer (Function Kind) (Lambda-List [Node-Var] Var*)
520 Declaration* Form*
521 Define some Kind of optimizer for the named Function. Function must be a
522 known function. Lambda-List is used to parse the arguments to the
523 combination as in Deftransform. If the argument syntax is invalid or there
524 are non-constant keys, then we simply return NIL.
525
526 The function is DEFUN'ed as Function-Kind-OPTIMIZER. Possible kinds are
527 DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If a symbol is
528 specified instead of a (Function Kind) list, then we just do a DEFUN with the
529 symbol as its name, and don't do anything with the definition. This is
530 useful for creating optimizers to be passed by name to DEFKNOWN.
531
532 If supplied, Node-Var is bound to the combination node being optimized. If
533 additional Vars are supplied, then they are used as the rest of the optimizer
534 function's lambda-list. LTN-ANNOTATE methods are passed an additional POLICY
535 argument, and IR2-CONVERT methods are passed an additional IR2-BLOCK
536 argument."
537
538 (let ((name (if (symbolp what) what
539 (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
540
541 (let ((n-args (gensym)))
542 `(progn
543 (defun ,name (,n-node ,@vars)
544 (let ((,n-args (basic-combination-args ,n-node)))
545 ,(parse-deftransform lambda-list body n-args
546 `(return-from ,name nil))))
547 ,@(when (consp what)
548 `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
549 (function-info-or-lose ',(first what)))
550 #',name)))))))
551
552
553 ;;;; IR groveling macros:
554
555 ;;; Do-Blocks, Do-Blocks-Backwards -- Interface
556 ;;;
557 (defmacro do-blocks ((block-var component &optional ends result) &body body)
558 "Do-Blocks (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
559 Iterate over the blocks in a component, binding Block-Var to each block in
560 turn. The value of Ends determines whether to iterate over dummy head and
561 tail blocks:
562 NIL -- Skip Head and Tail (the default)
563 :Head -- Do head but skip tail
564 :Tail -- Do tail but skip head
565 :Both -- Do both head and tail
566
567 If supplied, Result-Form is the value to return."
568 (unless (member ends '(nil :head :tail :both))
569 (error "Losing Ends value: ~S." ends))
570 (let ((n-component (gensym))
571 (n-tail (gensym)))
572 `(let* ((,n-component ,component)
573 (,n-tail ,(if (member ends '(:both :tail))
574 nil
575 `(component-tail ,n-component))))
576 (do ((,block-var ,(if (member ends '(:both :head))
577 `(component-head ,n-component)
578 `(block-next (component-head ,n-component)))
579 (block-next ,block-var)))
580 ((eq ,block-var ,n-tail) ,result)
581 ,@body))))
582 ;;;
583 (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
584 "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
585 Like Do-Blocks, only iterate over the blocks in reverse order."
586 (unless (member ends '(nil :head :tail :both))
587 (error "Losing Ends value: ~S." ends))
588 (let ((n-component (gensym))
589 (n-head (gensym)))
590 `(let* ((,n-component ,component)
591 (,n-head ,(if (member ends '(:both :head))
592 nil
593 `(component-head ,n-component))))
594 (do ((,block-var ,(if (member ends '(:both :tail))
595 `(component-tail ,n-component)
596 `(block-prev (component-tail ,n-component)))
597 (block-prev ,block-var)))
598 ((eq ,block-var ,n-head) ,result)
599 ,@body))))
600
601
602 ;;; Do-Uses -- Interface
603 ;;;
604 ;;; Could change it not to replicate the code someday perhaps...
605 ;;;
606 (defmacro do-uses ((node-var continuation &optional result) &body body)
607 "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
608 Iterate over the uses of Continuation, binding Node to each one succesively."
609 (once-only ((n-cont continuation))
610 `(ecase (continuation-kind ,n-cont)
611 (:unused)
612 (:inside-block
613 (block nil
614 (let ((,node-var (continuation-use ,n-cont)))
615 ,@body
616 ,result)))
617 ((:block-start :deleted-block-start)
618 (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
619 ,result)
620 ,@body)))))
621
622
623 ;;; Do-Nodes, Do-Nodes-Backwards -- Interface
624 ;;;
625 ;;; In the forward case, we terminate on Last-Cont so that we don't have to
626 ;;; worry about our termination condition being changed when new code is added
627 ;;; during the iteration. In the backward case, we do NODE-PREV before
628 ;;; evaluating the body so that we can keep going when the current node is
629 ;;; deleted.
630 ;;;
631 ;;; When Restart-P is supplied to DO-NODES, we start iterating over again at
632 ;;; the beginning of the block when we run into a continuation whose block
633 ;;; differs from the one we are trying to iterate over, either beacuse the
634 ;;; block was split, or because a node was deleted out from under us (hence its
635 ;;; block is NIL.) If the block start is deleted, we just punt. With
636 ;;; Restart-P, we are also more careful about termination, re-indirecting the
637 ;;; BLOCK-LAST each time.
638 ;;;
639 (defmacro do-nodes ((node-var cont-var block &key restart-p) &body body)
640 "Do-Nodes (Node-Var Cont-Var Block {Key Value}*) {Declaration}* {Form}*
641 Iterate over the nodes in Block, binding Node-Var to the each node and
642 Cont-Var to the node's Cont. The only keyword option is Restart-P, which
643 causes iteration to be restarted when a node is deleted out from under us (if
644 not supplied, this is an error.)"
645 (let ((n-block (gensym))
646 (n-last-cont (gensym)))
647 `(let* ((,n-block ,block)
648 ,@(unless restart-p
649 `((,n-last-cont (node-cont (block-last ,n-block))))))
650 (do* ((,node-var (continuation-next (block-start ,n-block))
651 ,(if restart-p
652 `(cond
653 ((eq (continuation-block ,cont-var) ,n-block)
654 (assert (continuation-next ,cont-var))
655 (continuation-next ,cont-var))
656 (t
657 (let ((start (block-start ,n-block)))
658 (unless (eq (continuation-kind start)
659 :block-start)
660 (return nil))
661 (continuation-next start))))
662 `(continuation-next ,cont-var)))
663 (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
664 (())
665 ,@body
666 (when ,(if restart-p
667 `(eq ,node-var (block-last ,n-block))
668 `(eq ,cont-var ,n-last-cont))
669 (return nil))))))
670 ;;;
671 (defmacro do-nodes-backwards ((node-var cont-var block) &body body)
672 "Do-Nodes-Backwards (Node-Var Cont-Var Block) {Declaration}* {Form}*
673 Like Do-Nodes, only iterates in reverse order."
674 (let ((n-block (gensym))
675 (n-start (gensym))
676 (n-last (gensym))
677 (n-next (gensym)))
678 `(let* ((,n-block ,block)
679 (,n-start (block-start ,n-block))
680 (,n-last (block-last ,n-block)))
681 (do* ((,cont-var (node-cont ,n-last) ,n-next)
682 (,node-var ,n-last (continuation-use ,cont-var))
683 (,n-next (and ,node-var (node-prev ,node-var))
684 (and ,node-var (node-prev ,node-var))))
685 ((null ,node-var))
686 ,@body
687 (when (eq ,n-next ,n-start)
688 (return nil))))))
689
690
691 ;;; With-IR1-Environment -- Interface
692 ;;;
693 ;;; The lexical environment is presumably already null...
694 ;;;
695 (defmacro with-ir1-environment (node &rest forms)
696 "With-IR1-Environment Node Form*
697 Bind the IR1 context variables so that IR1 conversion can be done after the
698 main conversion pass has finished."
699 (let ((n-node (gensym)))
700 `(let* ((,n-node ,node)
701 (*current-component* (block-component (node-block ,n-node)))
702 (*lexical-environment* (node-lexenv ,n-node))
703 (*current-path* (node-source-path ,n-node)))
704 ,@forms)))
705
706
707 ;;; WITH-IR1-NAMESPACE -- Interface
708 ;;;
709 ;;; Bind the hashtables used for keeping track of global variables,
710 ;;; functions, &c. Also establish condition handlers.
711 ;;;
712 (defmacro with-ir1-namespace (&body forms)
713 `(let ((*free-variables* (make-hash-table :test #'eq))
714 (*free-functions* (make-hash-table :test #'equal))
715 (*constants* (make-hash-table :test #'equal))
716 (*coalesce-constants* t)
717 (*source-paths* (make-hash-table :test #'eq)))
718 (handler-bind ((compiler-error #'compiler-error-handler)
719 (style-warning #'compiler-style-warning-handler)
720 (warning #'compiler-warning-handler))
721 ,@forms)))
722
723
724 ;;; LEXENV-FIND -- Interface
725 ;;;
726 (defmacro lexenv-find (name slot &key test)
727 "LEXENV-FIND Name Slot {Key Value}*
728 Look up Name in the lexical environment namespace designated by Slot,
729 returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
730 may be used to determine the name equality predicate."
731 (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot)
732 *lexical-environment*)
733 :test ,(or test '#'eq))))
734 `(if ,n-res
735 (values (cdr ,n-res) t)
736 (values nil nil))))
737
738 ;;;
739 ;;; LEXENV-FIND-FUNCTION -- Interface
740 ;;;
741 ;;; Find local function with name NAME in *LEXICAL-ENVIRONMENT*.
742 ;;;
743 (defun lexenv-find-function (name)
744 (lexenv-find name functions
745 :test (lambda (x y)
746 (or (equal x y)
747 (and (consp y)
748 (member (car y) '(flet labels))
749 (equal x (cadr y)))))))
750
751
752
753 ;;; With-debug-counters -- Interface
754 ;;;
755 ;;; Bind the hashtables and counters used for keeping track of
756 ;;; continuation, TN, and label IDs for the debug dumping routines.
757 ;;;
758 (defmacro with-debug-counters (&body forms)
759 `(let ((*continuation-numbers* (make-hash-table :test #'eq))
760 (*number-continuations* (make-hash-table :test #'eql))
761 (*continuation-number* 0)
762 (*tn-ids* (make-hash-table :test #'eq))
763 (*id-tns* (make-hash-table :test #'eql))
764 (*tn-id* 0)
765 (*id-labels* (make-hash-table :test #'eq))
766 (*label-ids* (make-hash-table :test #'eql))
767 (*label-id* 0))
768 ,@forms))
769
770
771 ;;;; The Defprinter macro:
772
773 (defvar *defprint-pretty* nil
774 "If true, defprinter print functions print each slot on a separate line.")
775
776
777 ;;; Defprinter-Prin1, Defprinter-Princ -- Internal
778 ;;;
779 ;;; These functions are called by the expansion of the Defprinter
780 ;;; macro to do the actual printing.
781 ;;;
782 (defun defprinter-prin1 (name value stream &optional indent)
783 (declare (symbol name) (stream stream) (ignore indent))
784 (write-string " " stream)
785 (when *print-pretty*
786 (pprint-newline :linear stream))
787 (princ name stream)
788 (write-string "= " stream)
789 (prin1 value stream))
790 ;;;
791 (defun defprinter-princ (name value stream &optional indent)
792 (declare (symbol name) (stream stream) (ignore indent))
793 (write-string " " stream)
794 (when *print-pretty*
795 (pprint-newline :linear stream))
796 (princ name stream)
797 (write-string "= " stream)
798 (princ value stream))
799
800 (defmacro defprinter (name &rest slots)
801 "Defprinter Name Slot-Desc*
802 Define some kind of reasonable defstruct structure-print function. Name
803 is the name of the structure. We define a function %PRINT-name which
804 prints the slots in the structure in the way described by the Slot-Descs.
805 Each Slot-Desc can be a slot name, indicating that the slot should simply
806 be printed. A Slot-Desc may also be a list of a slot name and other stuff.
807 The other stuff is composed of keywords followed by expressions. The
808 expressions are evaluated with the variable which is the slot name bound
809 to the value of the slot. These keywords are defined:
810
811 :PRIN1 Print the value of the expression instead of the slot value.
812 :PRINC Like :PRIN1, only princ the value
813 :TEST Only print something if the test is true.
814
815 If no printing thing is specified then the slot value is printed as PRIN1.
816
817 The structure being printed is bound to Structure and the stream is bound to
818 Stream."
819
820 (flet ((sref (slot) `(,(symbolicate name "-" slot) structure)))
821 (collect ((prints))
822 (dolist (slot slots)
823 (if (atom slot)
824 (prints `(defprinter-prin1 ',slot ,(sref slot) stream))
825 (let ((sname (first slot))
826 (test t))
827 (collect ((stuff))
828 (do ((option (rest slot) (cddr option)))
829 ((null option)
830 (prints
831 `(let ((,sname ,(sref sname)))
832 (when ,test
833 ,@(or (stuff)
834 `((defprinter-prin1 ',sname ,sname
835 stream)))))))
836 (case (first option)
837 (:prin1
838 (stuff `(defprinter-prin1 ',sname ,(second option)
839 stream)))
840 (:princ
841 (stuff `(defprinter-princ ',sname ,(second option)
842 stream)))
843 (:test (setq test (second option)))
844 (t
845 (error "Losing Defprinter option: ~S."
846 (first option)))))))))
847
848 `(defun ,(symbolicate "%PRINT-" name) (structure stream depth)
849 (flet ((do-prints (stream)
850 (declare (ignorable stream))
851 ,@(prints)))
852 (cond (*print-readably*
853 (error "~S cannot be printed readably." structure))
854 ((and *print-level* (>= depth *print-level*))
855 (format stream "#<~S ~X>"
856 ',name
857 (get-lisp-obj-address structure)))
858 (*print-pretty*
859 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
860 (pprint-indent :current 2 stream)
861 (prin1 ',name stream)
862 (write-char #\space stream)
863 (let ((*print-base* 16)
864 (*print-radix* t))
865 (prin1 (get-lisp-obj-address structure) stream))
866 (do-prints stream)))
867 (t
868 (descend-into (stream)
869 (format stream "#<~S ~X"
870 ',name
871 (get-lisp-obj-address structure))
872 (do-prints stream)
873 (format stream ">")))))
874 nil))))
875
876
877 ;;;; Boolean attribute utilities:
878 ;;;
879 ;;; We need to maintain various sets of boolean attributes for known
880 ;;; functions and VOPs. To save space and allow for quick set operations, we
881 ;;; represent them as bits in a fixnum.
882 ;;;
883
884 (deftype attributes () 'fixnum)
885
886 (eval-when (compile load eval)
887 ;;; Compute-Attribute-Mask -- Internal
888 ;;;
889 ;;; Given a list of attribute names and an alist that translates them to
890 ;;; masks, return the OR of the masks.
891 ;;;
892 (defun compute-attribute-mask (names alist)
893 (collect ((res 0 logior))
894 (dolist (name names)
895 (let ((mask (cdr (assoc name alist))))
896 (unless mask
897 (error "Unknown attribute name: ~S." name))
898 (res mask)))
899 (res)))
900
901 ); Eval-When (Compile Load Eval)
902
903 ;;; Def-Boolean-Attribute -- Interface
904 ;;;
905 ;;; Parse the specification and generate some accessor macros.
906 ;;;
907 (defmacro def-boolean-attribute (name &rest attribute-names)
908 "Def-Boolean-Attribute Name Attribute-Name*
909 Define a new class of boolean attributes, with the attributes havin the
910 specified Attribute-Names. Name is the name of the class, which is used to
911 generate some macros to manipulate sets of the attributes:
912
913 NAME-attributep attributes attribute-name*
914 Return true if one of the named attributes is present, false otherwise.
915 When set with SETF, updates the place Attributes setting or clearing the
916 specified attributes.
917
918 NAME-attributes attribute-name*
919 Return a set of the named attributes."
920
921 (let ((const-name (symbolicate name "-ATTRIBUTE-TRANSLATIONS"))
922 (test-name (symbolicate name "-ATTRIBUTEP")))
923 (collect ((alist))
924 (do ((mask 1 (ash mask 1))
925 (names attribute-names (cdr names)))
926 ((null names))
927 (alist (cons (car names) mask)))
928
929 `(progn
930 (eval-when (compile load eval)
931 (defconstant ,const-name ',(alist)))
932
933 (defmacro ,test-name (attributes &rest attribute-names)
934 "Automagically generated boolean attribute test function. See
935 Def-Boolean-Attribute."
936 `(logtest ,(compute-attribute-mask attribute-names ,const-name)
937 (the attributes ,attributes)))
938
939 (define-setf-expander ,test-name (place &rest attributes
940 &environment env)
941
942 "Automagically generated boolean attribute setter. See
943 Def-Boolean-Attribute."
944 (multiple-value-bind (temps values stores set get)
945 (get-setf-method place env)
946 (let ((newval (gensym))
947 (n-place (gensym))
948 (mask (compute-attribute-mask attributes ,const-name)))
949 (values `(,@temps ,n-place)
950 `(,@values ,get)
951 `(,newval)
952 `(let ((,(first stores)
953 (if ,newval
954 (logior ,n-place ,mask)
955 (logand ,n-place ,(lognot mask)))))
956 ,set
957 ,newval)
958 `(,',test-name ,n-place ,@attributes)))))
959
960 (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
961 "Automagically generated boolean attribute creation function. See
962 Def-Boolean-Attribute."
963 (compute-attribute-mask attribute-names ,const-name))))))
964
965
966 ;;; Attributes-Union, Attributes-Intersection, Attributes= -- Interface
967 ;;;
968 ;;; And now for some gratuitous pseudo-abstraction...
969 ;;;
970 (defmacro attributes-union (&rest attributes)
971 "Returns the union of all the sets of boolean attributes which are its
972 arguments."
973 `(the attributes
974 (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
975 ;;;
976 (defmacro attributes-intersection (&rest attributes)
977 "Returns the intersection of all the sets of boolean attributes which are its
978 arguments."
979 `(the attributes
980 (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
981 ;;;
982 (declaim (inline attributes=))
983 (defun attributes= (attr1 attr2)
984 (declare (type attributes attr1 attr2))
985 "Returns true if the attributes present in Attr1 are indentical to those in
986 Attr2."
987 (eql attr1 attr2))
988
989
990 ;;;; The Event statistics/trace utility:
991
992 (eval-when (compile load eval)
993
994 (defstruct event-info
995 ;;
996 ;; The name of this event.
997 (name (required-argument) :type symbol)
998 ;;
999 ;; The string rescribing this event.
1000 (description (required-argument) :type string)
1001 ;;
1002 ;; The name of the variable we stash this in.
1003 (var (required-argument) :type symbol)
1004 ;;
1005 ;; The number of times this event has happened.
1006 (count 0 :type fixnum)
1007 ;;
1008 ;; The level of significance of this event.
1009 (level (required-argument) :type unsigned-byte)
1010 ;;
1011 ;; If true, a function that gets called with the node that the event happened
1012 ;; to.
1013 (action nil :type (or function null)))
1014
1015 ;;; A hashtable from event names to event-info structures.
1016 ;;;
1017 (defvar *event-info* (make-hash-table :test #'eq))
1018
1019
1020 ;;; Event-Info-Or-Lose -- Internal
1021 ;;;
1022 ;;; Return the event info for Name or die trying.
1023 ;;;
1024 (defun event-info-or-lose (name)
1025 (declare (values event-info))
1026 (let ((res (gethash name *event-info*)))
1027 (unless res
1028 (error "~S is not the name of an event." name))
1029 res))
1030
1031 ); Eval-When (Compile Load Eval)
1032
1033
1034 ;;; Event-Count, Event-Action, Event-Level -- Interface
1035 ;;;
1036 (defun event-count (name)
1037 "Return the number of times that Event has happened."
1038 (declare (symbol name) (values fixnum))
1039 (event-info-count (event-info-or-lose name)))
1040 ;;;
1041 (defun event-action (name)
1042 "Return the function that is called when Event happens. If this is null,
1043 there is no action. The function is passed the node to which the event
1044 happened, or NIL if there is no relevant node. This may be set with SETF."
1045 (declare (symbol name) (values (or function null)))
1046 (event-info-action (event-info-or-lose name)))
1047 ;;;
1048 (defun %set-event-action (name new-value)
1049 (declare (symbol name) (type (or function null) new-value)
1050 (values (or function null)))
1051 (setf (event-info-action (event-info-or-lose name))
1052 new-value))
1053 ;;;
1054 (defsetf event-action %set-event-action)
1055 ;;;
1056 (defun event-level (name)
1057 "Return the non-negative integer which represents the level of significance
1058 of the event Name. This is used to determine whether to print a message when
1059 the event happens. This may be set with SETF."
1060 (declare (symbol name) (values unsigned-byte))
1061 (event-info-level (event-info-or-lose name)))
1062 ;;;
1063 (defun %set-event-level (name new-value)
1064 (declare (symbol name) (type unsigned-byte new-value)
1065 (values unsigned-byte))
1066 (setf (event-info-level (event-info-or-lose name))
1067 new-value))
1068 ;;;
1069 (defsetf event-level %set-event-level)
1070
1071
1072 ;;; Defevent -- Interface
1073 ;;;
1074 ;;; Make an event-info structure and stash it in a variable so we can get at
1075 ;;; it quickly.
1076 ;;;
1077 (defmacro defevent (name description &optional (level 0))
1078 "Defevent Name Description
1079 Define a new kind of event. Name is a symbol which names the event and
1080 Description is a string which describes the event. Level (default 0) is the
1081 level of significance associated with this event; it is used to determine
1082 whether to print a Note when the event happens."
1083 (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
1084 `(eval-when (compile load eval)
1085 (defvar ,var-name
1086 (make-event-info :name ',name :description ',description :var ',var-name
1087 :level ,level))
1088 (setf (gethash ',name *event-info*) ,var-name)
1089 ',name)))
1090
1091 (declaim (type unsigned-byte *event-note-threshold*))
1092 (defvar *event-note-threshold* 1
1093 "This variable is a non-negative integer specifying the lowest level of
1094 event that will print a Note when it occurs.")
1095
1096 ;;; Event -- Interface
1097 ;;;
1098 ;;; Increment the counter and do any action. Mumble about the event if
1099 ;;; policy indicates.
1100 ;;;
1101 (defmacro event (name &optional node)
1102 "Event Name Node
1103 Note that the event with the specified Name has happened. Node is evaluated
1104 to determine the node to which the event happened."
1105 `(%event ,(event-info-var (event-info-or-lose name)) ,node))
1106
1107
1108 ;;; Event-Statistics, Clear-Statistics -- Interface
1109 ;;;
1110 (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
1111 (declare (type unsigned-byte min-count) (stream stream) (values))
1112 "Print a listing of events and their counts, sorted by the count. Events
1113 that happened fewer than Min-Count times will not be printed. Stream is the
1114 stream to write to."
1115 (collect ((info))
1116 (maphash #'(lambda (k v)
1117 (declare (ignore k))
1118 (when (>= (event-info-count v) min-count)
1119 (info v)))
1120 *event-info*)
1121 (dolist (event (sort (info) #'> :key #'event-info-count))
1122 (format stream "~6D: ~A~%" (event-info-count event)
1123 (event-info-description event)))
1124 (values)))
1125 ;;;
1126 (defun clear-statistics ()
1127 (declare (values))
1128 (maphash #'(lambda (k v)
1129 (declare (ignore k))
1130 (setf (event-info-count v) 0))
1131 *event-info*)
1132 (values))
1133
1134
1135 ;;;; Generic list (?) functions:
1136
1137 (declaim (inline find-in position-in map-in))
1138
1139 ;;; Find-In -- Interface
1140 ;;;
1141 (defun find-in (next element list &key (key #'identity)
1142 (test #'eql test-p) (test-not nil not-p))
1143 "Find Element in a null-terminated List linked by the accessor function
1144 Next. Key, Test and Test-Not are the same as for generic sequence
1145 functions."
1146 (when (and test-p not-p)
1147 (error "Silly to supply both :Test and :Test-Not."))
1148 (if not-p
1149 (do ((current list (funcall next current)))
1150 ((null current) nil)
1151 (unless (funcall test-not (funcall key current) element)
1152 (return current)))
1153 (do ((current list (funcall next current)))
1154 ((null current) nil)
1155 (when (funcall test (funcall key current) element)
1156 (return current)))))
1157
1158 ;;; Position-In -- Interface
1159 ;;;
1160 (defun position-in (next element list &key (key #'identity)
1161 (test #'eql test-p) (test-not nil not-p))
1162 "Return the position of Element (or NIL if absent) in a null-terminated List
1163 linked by the accessor function Next. Key, Test and Test-Not are the same as
1164 for generic sequence functions."
1165 (when (and test-p not-p)
1166 (error "Silly to supply both :Test and :Test-Not."))
1167 (if not-p
1168 (do ((current list (funcall next current))
1169 (i 0 (1+ i)))
1170 ((null current) nil)
1171 (unless (funcall test-not (funcall key current) element)
1172 (return i)))
1173 (do ((current list (funcall next current))
1174 (i 0 (1+ i)))
1175 ((null current) nil)
1176 (when (funcall test (funcall key current) element)
1177 (return i)))))
1178
1179
1180 ;;; Map-In -- Interface
1181 ;;;
1182 (defun map-in (next function list)
1183 "Map Function over the elements in a null-terminated List linked by the
1184 accessor function Next, returning a list of the results."
1185 (collect ((res))
1186 (do ((current list (funcall next current)))
1187 ((null current))
1188 (res (funcall function current)))
1189 (res)))
1190
1191
1192 ;;; Deletef-In -- Interface
1193 ;;;
1194 (defmacro deletef-in (next place item &environment env)
1195 "Deletef-In Next Place Item
1196 Delete Item from a null-terminated list linked by the accessor function Next
1197 that is stored in Place. Item must appear exactly once in the list."
1198 (multiple-value-bind
1199 (temps vals stores store access)
1200 (get-setf-method place env)
1201 (let ((n-item (gensym))
1202 (n-place (gensym))
1203 (n-current (gensym))
1204 (n-prev (gensym)))
1205 `(let* (,@(mapcar #'list temps vals)
1206 (,n-place ,access)
1207 (,n-item ,item))
1208 (if (eq ,n-place ,n-item)
1209 (let ((,(first stores) (,next ,n-place)))
1210 ,store)
1211 (do ((,n-prev ,n-place ,n-current)
1212 (,n-current (,next ,n-place)
1213 (,next ,n-current)))
1214 ((eq ,n-current ,n-item)
1215 (setf (,next ,n-prev)
1216 (,next ,n-current)))))
1217 (undefined-value)))))
1218
1219
1220 ;;; Push-In -- Interface
1221 ;;;
1222 (defmacro push-in (next item place &environment env)
1223 "Push Item onto a list linked by the accessor function Next that is stored in
1224 Place."
1225 (multiple-value-bind
1226 (temps vals stores store access)
1227 (get-setf-method place env)
1228 `(let (,@(mapcar #'list temps vals)
1229 (,(first stores) ,item))
1230 (setf (,next ,(first stores)) ,access)
1231 ,store
1232 (undefined-value))))
1233
1234
1235 ;;; EPOSITION -- Interface
1236 ;;;
1237 (defmacro eposition (&rest args)
1238 `(or (position ,@args)
1239 (error "Shouldn't happen?")))
1240
1241
1242 ;;; Modular functions
1243
1244 ;;; For a documentation, see CUT-TO-WIDTH.
1245
1246 #+modular-arith
1247 (sys:register-lisp-feature :modular-arith)
1248
1249 #+modular-arith
1250 (progn
1251 ;;; List of increasing widths
1252 (defvar *modular-funs-widths* nil)
1253 (defstruct modular-fun-info
1254 (name (required-argument) :type symbol)
1255 (width (required-argument) :type (integer 0))
1256 (lambda-list (required-argument) :type list)
1257 (prototype (required-argument) :type symbol))
1258
1259 (defun find-modular-version (fun-name width)
1260 (let ((infos (gethash fun-name kernel::*modular-funs*)))
1261 (if (listp infos)
1262 (find-if (lambda (item-width) (>= item-width width))
1263 infos
1264 :key #'modular-fun-info-width)
1265 infos)))
1266
1267 (defun %define-modular-fun (name lambda-list prototype width)
1268 (let* ((infos (the list (gethash prototype kernel::*modular-funs*)))
1269 (info (find-if (lambda (item-width) (= item-width width))
1270 infos
1271 :key #'modular-fun-info-width)))
1272 (if info
1273 (unless (and (eq name (modular-fun-info-name info))
1274 (= (length lambda-list)
1275 (length (modular-fun-info-lambda-list info))))
1276 (setf (modular-fun-info-name info) name)
1277 (warn "Redefining modular version ~S of ~S for width ~S."
1278 name prototype width))
1279 (setf (gethash prototype kernel::*modular-funs*)
1280 (merge 'list
1281 (list (make-modular-fun-info :name name
1282 :width width
1283 :lambda-list lambda-list
1284 :prototype prototype))
1285 infos
1286 #'< :key #'modular-fun-info-width))))
1287 (setq *modular-funs-widths*
1288 (merge 'list (list width) *modular-funs-widths* #'<)))
1289
1290 (defmacro define-modular-fun (name lambda-list prototype width)
1291 (check-type name symbol)
1292 (check-type prototype symbol)
1293 (check-type width unsigned-byte)
1294 (dolist (arg lambda-list)
1295 (when (member arg lambda-list-keywords)
1296 (error "Lambda list keyword ~S is not supported for ~
1297 modular function lambda lists." arg)))
1298 `(progn
1299 (%define-modular-fun ',name ',lambda-list ',prototype ,width)
1300 (defknown ,name ,(mapcar (constantly 'integer) lambda-list)
1301 (unsigned-byte ,width)
1302 (foldable flushable movable))
1303 ;; Define the modular function just in case we need it.
1304 #+nil
1305 (defun ,name ,lambda-list
1306 (flet ((prepare-argument (arg)
1307 (declare (integer arg))
1308 (etypecase arg
1309 ((unsigned-byte ,width) arg)
1310 (fixnum (logand arg ,(1- (ash 1 width))))
1311 (bignum (logand arg ,(1- (ash 1 width)))))))
1312 (,name ,@(loop for arg in lambda-list
1313 collect `(prepare-argument ,arg)))))))
1314
1315 (defun %define-good-modular-fun (name)
1316 (setf (gethash name kernel::*modular-funs*) :good)
1317 name)
1318
1319 (defmacro define-good-modular-fun (name)
1320 (check-type name symbol)
1321 `(%define-good-modular-fun ',name))
1322
1323 (defmacro define-modular-fun-optimizer
1324 (name ((&rest lambda-list) &key (width (gensym "WIDTH")))
1325 &body body)
1326 (check-type name symbol)
1327 (dolist (arg lambda-list)
1328 (when (member arg lambda-list-keywords)
1329 (error "Lambda list keyword ~S is not supported for ~
1330 modular function lambda lists." arg)))
1331 (let ((call (gensym))
1332 (args (gensym)))
1333 `(setf (gethash ',name kernel::*modular-funs*)
1334 (lambda (,call ,width)
1335 (declare (type basic-combination ,call)
1336 (type (integer 0) width))
1337 (let ((,args (basic-combination-args ,call)))
1338 (when (= (length ,args) ,(length lambda-list))
1339 (destructuring-bind ,lambda-list ,args
1340 (declare (type continuation ,@lambda-list))
1341 ,@body)))))))
1342
1343 ;;; Good modular functions. (Those that don't make the result larger.)
1344 (define-good-modular-fun logand)
1345 (define-good-modular-fun logior)
1346 ) ; modular-arith

  ViewVC Help
Powered by ViewVC 1.1.5