/[cmucl]/src/code/lispinit.lisp
ViewVC logotype

Contents of /src/code/lispinit.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.66 - (show annotations)
Wed Jul 10 16:15:59 2002 UTC (11 years, 9 months ago) by toy
Branch: MAIN
Changes since 1.65: +7 -3 lines
From Eric Marsden:

 * (pcl:class-precedence-list (find-class 'null)) should have symbol
   occurring before list, as per CLHS.

 * minor changes to certain error conditions: eg trying to set
   DEFAULT-DIRECTORY to a non-existent directory should raise a
   condition of type FILE-ERROR. (Some of these changes are from
   Alexey Dejneka, via SBCL).

 * cleanup of some Hemlock-related symbols that shouldn't be exported
   when :no-hemlock is defined.
1 ;;; -*- Mode: Lisp; Package: Lisp; Log: code.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/code/lispinit.lisp,v 1.66 2002/07/10 16:15:59 toy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Initialization stuff for CMU Common Lisp, plus some other random functions
13 ;;; that we don't have any better place for.
14 ;;;
15 ;;; Written by Skef Wholey and Rob MacLachlan.
16 ;;;
17 (in-package :lisp)
18
19 (export '(most-positive-fixnum most-negative-fixnum sleep
20 ++ +++ ** *** // ///))
21
22 (in-package :system)
23 (export '(compiler-version scrub-control-stack))
24
25 (in-package :extensions)
26 (export '(quit *prompt*))
27
28 (in-package :lisp)
29
30 ;;; Make the error system enable interrupts.
31
32 (defconstant most-positive-fixnum #.vm:target-most-positive-fixnum
33 "The fixnum closest in value to positive infinity.")
34
35 (defconstant most-negative-fixnum #.vm:target-most-negative-fixnum
36 "The fixnum closest in value to negative infinity.")
37
38
39 ;;; Random information:
40
41 (defvar *lisp-implementation-version* "4.0(?)")
42
43
44 ;;; Must be initialized in %INITIAL-FUNCTION before the DEFVAR runs...
45 (declaim
46 #-gengc
47 (special *gc-inhibit* *already-maybe-gcing*
48 *need-to-collect-garbage* *gc-verbose*
49 *before-gc-hooks* *after-gc-hooks*
50 #+x86 *pseudo-atomic-atomic*
51 #+x86 *pseudo-atomic-interrupted*
52 unix::*interrupts-enabled*
53 unix::*interrupt-pending*
54 *type-system-initialized*)
55 #+gengc
56 (special *gc-verbose* *before-gc-hooks* *after-gc-hooks*
57 *type-system-initialized*))
58
59
60 ;;;; Random magic specials.
61
62
63 ;;; These are filled in by Genesis.
64
65 #-gengc
66 (progn
67
68 (defvar *current-catch-block*)
69 (defvar *current-unwind-protect-block*)
70 (defvar *free-interrupt-context-index*)
71
72 ); #-gengc progn
73
74
75 ;;;; Random stuff that needs to be in the cold load which would otherwise be
76 ;;;; byte-compiled.
77 ;;;;
78 (defvar hi::*in-the-editor* nil)
79
80 ;;;; Called by defmacro expanders...
81
82 ;;; VERIFY-KEYWORDS -- internal
83 ;;;
84 ;;; Determine if key-list is a valid list of keyword/value pairs. Do not
85 ;;; signal the error directly, 'cause we don't know how it should be signaled.
86 ;;;
87
88 (defun verify-keywords (key-list valid-keys allow-other-keys)
89 (do ((already-processed nil)
90 (unknown-keyword nil)
91 (remaining key-list (cddr remaining)))
92 ((null remaining)
93 (if (and unknown-keyword
94 (not allow-other-keys)
95 (not (lookup-keyword :allow-other-keys key-list)))
96 (values :unknown-keyword (list unknown-keyword valid-keys))
97 (values nil nil)))
98 (cond ((not (and (consp remaining) (listp (cdr remaining))))
99 (return (values :dotted-list key-list)))
100 ((null (cdr remaining))
101 (return (values :odd-length key-list)))
102 #+nil ;; Not ANSI compliant to disallow duplicate keywords.
103 ((member (car remaining) already-processed)
104 (return (values :duplicate (car remaining))))
105 ((or (eq (car remaining) :allow-other-keys)
106 (member (car remaining) valid-keys))
107 (push (car remaining) already-processed))
108 (t
109 (setf unknown-keyword (car remaining))))))
110
111 (defun lookup-keyword (keyword key-list)
112 (do ((remaining key-list (cddr remaining)))
113 ((endp remaining))
114 (when (eq keyword (car remaining))
115 (return (cadr remaining)))))
116 ;;;
117 (defun keyword-supplied-p (keyword key-list)
118 (do ((remaining key-list (cddr remaining)))
119 ((endp remaining))
120 (when (eq keyword (car remaining))
121 (return t))))
122
123 (in-package "CONDITIONS")
124
125 (defvar *break-on-signals* nil
126 "When (typep condition *break-on-signals*) is true, then calls to SIGNAL will
127 enter the debugger prior to signalling that condition.")
128
129 (defun signal (datum &rest arguments)
130 "Invokes the signal facility on a condition formed from datum and arguments.
131 If the condition is not handled, nil is returned. If
132 (TYPEP condition *BREAK-ON-SIGNALS*) is true, the debugger is invoked before
133 any signalling is done."
134 (let ((condition (coerce-to-condition datum arguments
135 'simple-condition 'signal))
136 (*handler-clusters* *handler-clusters*))
137 (let ((obos *break-on-signals*)
138 (*break-on-signals* nil))
139 (when (typep condition obos)
140 (break "~A~%Break entered because of *break-on-signals* (now NIL.)"
141 condition)))
142 (loop
143 (unless *handler-clusters* (return))
144 (let ((cluster (pop *handler-clusters*)))
145 (dolist (handler cluster)
146 (when (typep condition (car handler))
147 (funcall (cdr handler) condition)))))
148 nil))
149
150 ;;; COERCE-TO-CONDITION is used in SIGNAL, ERROR, CERROR, WARN, and
151 ;;; INVOKE-DEBUGGER for parsing the hairy argument conventions into a single
152 ;;; argument that's directly usable by all the other routines.
153 ;;;
154 (defun coerce-to-condition (datum arguments default-type function-name)
155 (cond ((typep datum 'condition)
156 (if arguments
157 (cerror "Ignore the additional arguments."
158 'simple-type-error
159 :datum arguments
160 :expected-type 'null
161 :format-control "You may not supply additional arguments ~
162 when giving ~S to ~S."
163 :format-arguments (list datum function-name)))
164 datum)
165 ((symbolp datum) ;Roughly, (subtypep datum 'condition).
166 (apply #'make-condition datum arguments))
167 ((or (stringp datum) (functionp datum))
168 (make-condition default-type
169 :format-control datum
170 :format-arguments arguments))
171 (t
172 (error 'simple-type-error
173 :datum datum
174 :expected-type '(or symbol string)
175 :format-control "Bad argument to ~S: ~S"
176 :format-arguments (list function-name datum)))))
177
178 (defun error (datum &rest arguments)
179 "Invokes the signal facility on a condition formed from datum and arguments.
180 If the condition is not handled, the debugger is invoked."
181 (kernel:infinite-error-protect
182 (let ((condition (coerce-to-condition datum arguments
183 'simple-error 'error))
184 (debug:*stack-top-hint* debug:*stack-top-hint*))
185 (unless (and (condition-function-name condition) debug:*stack-top-hint*)
186 (multiple-value-bind
187 (name frame)
188 (kernel:find-caller-name)
189 (unless (condition-function-name condition)
190 (setf (condition-function-name condition) name))
191 (unless debug:*stack-top-hint*
192 (setf debug:*stack-top-hint* frame))))
193 (let ((debug:*stack-top-hint* nil))
194 (signal condition))
195 (invoke-debugger condition))))
196
197 ;;; CERROR must take care to not use arguments when datum is already a
198 ;;; condition object.
199 ;;;
200 (defun cerror (continue-string datum &rest arguments)
201 (kernel:infinite-error-protect
202 (with-simple-restart
203 (continue "~A" (apply #'format nil continue-string arguments))
204 (let ((condition (if (typep datum 'condition)
205 datum
206 (coerce-to-condition datum arguments
207 'simple-error 'error)))
208 (debug:*stack-top-hint* debug:*stack-top-hint*))
209 (unless (and (condition-function-name condition)
210 debug:*stack-top-hint*)
211 (multiple-value-bind
212 (name frame)
213 (kernel:find-caller-name)
214 (unless (condition-function-name condition)
215 (setf (condition-function-name condition) name))
216 (unless debug:*stack-top-hint*
217 (setf debug:*stack-top-hint* frame))))
218 (with-condition-restarts condition (list (find-restart 'continue))
219 (let ((debug:*stack-top-hint* nil))
220 (signal condition))
221 (invoke-debugger condition)))))
222 nil)
223
224 (defun break (&optional (datum "Break") &rest arguments)
225 "Prints a message and invokes the debugger without allowing any possibility
226 of condition handling occurring."
227 (kernel:infinite-error-protect
228 (with-simple-restart (continue "Return from BREAK.")
229 (let ((debug:*stack-top-hint*
230 (or debug:*stack-top-hint*
231 (nth-value 1 (kernel:find-caller-name)))))
232 (invoke-debugger
233 (coerce-to-condition datum arguments 'simple-condition 'break)))))
234 nil)
235
236 (defun warn (datum &rest arguments)
237 "Warns about a situation by signalling a condition formed by datum and
238 arguments. While the condition is being signaled, a muffle-warning restart
239 exists that causes WARN to immediately return nil."
240 (kernel:infinite-error-protect
241 (let ((condition (coerce-to-condition datum arguments
242 'simple-warning 'warn)))
243 (check-type condition warning "a warning condition")
244 (restart-case (signal condition)
245 (muffle-warning ()
246 :report "Skip warning."
247 (return-from warn nil)))
248 (format *error-output* "~&~@<Warning: ~3i~:_~A~:>~%" condition)))
249 nil)
250
251 (in-package "LISP")
252
253
254 ;;; %Initial-Function is called when a cold system starts up. First we zoom
255 ;;; down the *Lisp-Initialization-Functions* doing things that wanted to happen
256 ;;; at "load time." Then we initialize the various subsystems and call the
257 ;;; read-eval-print loop. The top-level Read-Eval-Print loop is executed until
258 ;;; someone (most likely the Quit function) throws to the tag
259 ;;; %End-Of-The-World. We quit this way so that all outstanding cleanup forms
260 ;;; in Unwind-Protects will get executed.
261
262 (declaim (special *lisp-initialization-functions*
263 *load-time-values*))
264
265 (eval-when (compile)
266 (defmacro print-and-call (name)
267 `(progn
268 (%primitive print ,(symbol-name name))
269 (,name))))
270 #+nil
271 (defun hexstr(thing)
272 (let ((addr (kernel:get-lisp-obj-address thing))
273 (str (make-string 10)))
274 (setf (char str 0) #\0
275 (char str 1) #\x)
276 (dotimes (i 8)
277 (let* ((nib (ldb (byte 4 0) addr))
278 (chr (char "0123456789abcdef" nib)))
279 (declare (type (unsigned-byte 4) nib)
280 (base-char chr))
281 (setf (char str (- 9 i)) chr
282 addr (ash addr -4))))
283 str))
284
285 (defun %initial-function ()
286 "Gives the world a shove and hopes it spins."
287 (%primitive print "In initial-function, and running.")
288 #-gengc (setf *already-maybe-gcing* t)
289 #-gengc (setf *gc-inhibit* t)
290 #-gengc (setf *need-to-collect-garbage* nil)
291 (setf *gc-verbose* #-gengc t #+gengc nil)
292 (setf *before-gc-hooks* nil)
293 (setf *after-gc-hooks* nil)
294 #-gengc (setf unix::*interrupts-enabled* t)
295 #-gengc (setf unix::*interrupt-pending* nil)
296 (setf *type-system-initialized* nil)
297 (setf *break-on-signals* nil)
298 #+gengc (setf conditions::*handler-clusters* nil)
299
300 ;; Many top-level forms call INFO, (SETF INFO).
301 (print-and-call c::globaldb-init)
302
303 ;; Set up the fdefn database.
304 (print-and-call fdefn-init)
305
306 ;; Some of the random top-level forms call Make-Array, which calls Subtypep
307 (print-and-call typedef-init)
308 (print-and-call class-init)
309 (print-and-call type-init)
310
311 (let ((funs (nreverse *lisp-initialization-functions*)))
312 (%primitive print "Calling top-level forms.")
313 (dolist (fun funs) #+nil (%primitive print (hexstr fun))
314 (typecase fun
315 (function
316 (funcall fun))
317 (cons
318 (case (car fun)
319 (:load-time-value
320 (setf (svref *load-time-values* (third fun))
321 (funcall (second fun))))
322 (:load-time-value-fixup
323 #-gengc
324 (setf (sap-ref-32 (second fun) 0)
325 (get-lisp-obj-address
326 (svref *load-time-values* (third fun))))
327 #+gengc
328 (do-load-time-value-fixup (second fun) (third fun) (fourth fun)))
329 #+(and x86 gencgc)
330 (:load-time-code-fixup
331 (vm::do-load-time-code-fixup (second fun) (third fun) (fourth fun)
332 (fifth fun)))
333 (t
334 (%primitive print
335 "Bogus fixup in *lisp-initialization-functions*")
336 (%halt))))
337 (t
338 (%primitive print
339 "Bogus function in *lisp-initialization-functions*")
340 (%halt)))))
341 (makunbound '*lisp-initialization-functions*) ; So it gets GC'ed.
342 (makunbound '*load-time-values*)
343
344 ;; Only do this after top level forms have run, 'cause thats where
345 ;; deftypes are.
346 (setf *type-system-initialized* t)
347
348 (print-and-call os-init)
349 (print-and-call filesys-init)
350
351 (print-and-call reader-init)
352 ;; Note: sharpm and backq not yet loaded, so this is not the final RT.
353 (setf *readtable* (copy-readtable std-lisp-readtable))
354
355 (print-and-call stream-init)
356 (print-and-call loader-init)
357 (print-and-call package-init)
358 (print-and-call kernel::signal-init)
359 (setf (alien:extern-alien "internal_errors_enabled" boolean) t)
360
361 (set-floating-point-modes :traps '(:overflow #-x86 :underflow :invalid
362 :divide-by-zero))
363 ;; This is necessary because some of the initial top level forms might
364 ;; have changed the compliation policy in strange ways.
365 (print-and-call c::proclaim-init)
366
367 (print-and-call kernel::class-finalize)
368
369 (%primitive print "Done initializing.")
370
371 #-gengc (setf *already-maybe-gcing* nil)
372 #+gengc (setf *gc-verbose* t)
373 (terpri)
374 (princ "CMU Common Lisp kernel core image ")
375 (princ (lisp-implementation-version))
376 (princ ".")
377 (terpri)
378 (princ "[You are in the LISP package.]")
379 (terpri)
380 (let ((wot
381 (catch '%end-of-the-world
382 (loop
383 (%top-level)
384 (write-line "You're certainly a clever child.")))))
385 (unix:unix-exit wot)))
386
387 #+gengc
388 (defun do-load-time-value-fixup (object offset index)
389 (declare (type index offset))
390 (macrolet ((lose (msg)
391 `(progn
392 (%primitive print ,msg)
393 (%halt))))
394 (let ((value (svref *load-time-values* index)))
395 (typecase object
396 (list
397 (case offset
398 (0 (setf (car object) value))
399 (1 (setf (cdr object) value))
400 (t (lose "Bogus offset in cons cell."))))
401 (instance
402 (setf (%instance-ref object (- offset vm:instance-slots-offset))
403 value))
404 (code-component
405 (setf (code-header-ref object offset) value))
406 (simple-vector
407 (setf (svref object (- offset vm:vector-data-offset)) value))
408 (t
409 (lose "Unknown kind of object for load-time-value fixup."))))))
410
411
412 ;;;; Initialization functions:
413
414 ;;; Print seems to not like x86 NPX denormal floats like
415 ;;; least-negative-single-float, so the :underflow exceptions
416 ;;; is disabled by default. Joe User can explicitly enable them
417 ;;; if desired.
418
419 (defun reinit ()
420 (without-interrupts
421 (without-gcing
422 (os-init)
423 (stream-reinit)
424 (kernel::signal-init)
425 (gc-init)
426 (setf (alien:extern-alien "internal_errors_enabled" boolean) t)
427 (set-floating-point-modes :traps
428 '(:overflow #-x86 :underflow :invalid
429 :divide-by-zero))
430 ;; Clear pseudo atomic in case this core wasn't compiled with support.
431 #+x86 (setf lisp::*pseudo-atomic-atomic* 0))))
432
433
434 ;;;; Miscellaneous external functions:
435
436 (defvar *cleanup-functions* nil
437 "Functions to be invoked during cleanup at Lisp exit.")
438
439 ;;; Quit gets us out, one way or another.
440
441 (defun quit (&optional recklessly-p)
442 "Terminates the current Lisp. Things are cleaned up unless Recklessly-P is
443 non-Nil."
444 (if recklessly-p
445 (unix:unix-exit 0)
446 (progn
447 (mapc (lambda (fn) (ignore-errors (funcall fn))) *cleanup-functions*)
448 (throw '%end-of-the-world 0))))
449
450
451 #-mp ; Multi-processing version defined in multi-proc.lisp.
452 (defun sleep (n)
453 "This function causes execution to be suspended for N seconds. N may
454 be any non-negative, non-complex number."
455 (when (or (not (realp n))
456 (minusp n))
457 (error 'simple-type-error
458 :format-control
459 "Invalid argument to SLEEP: ~S.~%~
460 Must be a non-negative, non-complex number."
461 :format-arguments (list n)
462 :datum n
463 :expected-type '(real 0)))
464 (multiple-value-bind (sec usec)
465 (if (integerp n)
466 (values n 0)
467 (multiple-value-bind (sec frac) (truncate n)
468 (values sec (truncate frac 1e-6))))
469 (unix:unix-select 0 0 0 0 sec usec))
470 nil)
471
472 ;;;; SCRUB-CONTROL-STACK
473
474
475 (defconstant bytes-per-scrub-unit 2048)
476
477 ;;; Scrub-control-stack.
478 ;;;
479 #-x86
480 (defun scrub-control-stack ()
481 "Zero the unused portion of the control stack so that old objects are not
482 kept alive because of uninitialized stack variables."
483 (declare (optimize (speed 3) (safety 0))
484 (values (unsigned-byte 20)))
485 (labels
486 ((scrub (ptr offset count)
487 (declare (type system-area-pointer ptr)
488 (type (unsigned-byte 16) offset)
489 (type (unsigned-byte 20) count)
490 (values (unsigned-byte 20)))
491 (cond ((= offset bytes-per-scrub-unit)
492 (look (sap+ ptr bytes-per-scrub-unit) 0 count))
493 (t
494 (setf (sap-ref-32 ptr offset) 0)
495 (scrub ptr (+ offset vm:word-bytes) count))))
496 (look (ptr offset count)
497 (declare (type system-area-pointer ptr)
498 (type (unsigned-byte 16) offset)
499 (type (unsigned-byte 20) count)
500 (values (unsigned-byte 20)))
501 (cond ((= offset bytes-per-scrub-unit)
502 count)
503 ((zerop (sap-ref-32 ptr offset))
504 (look ptr (+ offset vm:word-bytes) count))
505 (t
506 (scrub ptr offset (+ count vm:word-bytes))))))
507 (let* ((csp (sap-int (c::control-stack-pointer-sap)))
508 (initial-offset (logand csp (1- bytes-per-scrub-unit))))
509 (declare (type (unsigned-byte 32) csp))
510 (scrub (int-sap (- csp initial-offset))
511 (* (floor initial-offset vm:word-bytes) vm:word-bytes)
512 0))))
513
514 ;;; Scrub-control-stack.
515 ;;;
516 ;;; On the x86 port the stack grows downwards, and to support grow on
517 ;;; demand stacks the stack must be decreased as it is scrubbed.
518 ;;;
519 #+x86
520 (defun scrub-control-stack ()
521 "Zero the unused portion of the control stack so that old objects are not
522 kept alive because of uninitialized stack variables."
523 (scrub-control-stack))
524
525
526 ;;;; TOP-LEVEL loop.
527
528 (defvar / nil
529 "Holds a list of all the values returned by the most recent top-level EVAL.")
530 (defvar // nil "Gets the previous value of / when a new value is computed.")
531 (defvar /// nil "Gets the previous value of // when a new value is computed.")
532 (defvar * nil "Holds the value of the most recent top-level EVAL.")
533 (defvar ** nil "Gets the previous value of * when a new value is computed.")
534 (defvar *** nil "Gets the previous value of ** when a new value is computed.")
535 (defvar + nil "Holds the value of the most recent top-level READ.")
536 (defvar ++ nil "Gets the previous value of + when a new value is read.")
537 (defvar +++ nil "Gets the previous value of ++ when a new value is read.")
538 (defvar - nil "Holds the form curently being evaluated.")
539 (defvar *prompt* "* "
540 "The top-level prompt string. This also may be a function of no arguments
541 that returns a simple-string.")
542 (defvar *in-top-level-catcher* nil
543 "True if we are within the Top-Level-Catcher. This is used by interrupt
544 handlers to see whether it is o.k. to throw.")
545
546 (defun interactive-eval (form)
547 "Evaluate FORM, returning whatever it returns but adjust ***, **, *, +++, ++,
548 +, ///, //, /, and -."
549 (setf - form)
550 (let ((results (multiple-value-list
551 (if (and (fboundp 'commandp)(funcall 'commandp form))
552 (funcall 'invoke-command-interactive form)
553 (eval form)))))
554 (finish-standard-output-streams)
555 (setf /// //
556 // /
557 / results
558 *** **
559 ** *
560 * (car results)))
561 (setf +++ ++
562 ++ +
563 + -)
564 (unless (boundp '*)
565 ;; The bogon returned an unbound marker.
566 (setf * nil)
567 (cerror "Go on with * set to NIL."
568 "EVAL returned an unbound marker."))
569 (values-list /))
570
571
572 (defconstant eofs-before-quit 10)
573
574 (defun %top-level ()
575 "Top-level READ-EVAL-PRINT loop. Do not call this."
576 (let ((* nil) (** nil) (*** nil)
577 (- nil) (+ nil) (++ nil) (+++ nil)
578 (/// nil) (// nil) (/ nil)
579 (magic-eof-cookie (cons :eof nil))
580 (number-of-eofs 0))
581 (loop
582 (with-simple-restart (abort "Return to Top-Level.")
583 (catch 'top-level-catcher
584 (unix:unix-sigsetmask 0)
585 (let ((*in-top-level-catcher* t))
586 (loop
587 (scrub-control-stack)
588 (fresh-line)
589 (princ (if (functionp *prompt*)
590 (funcall *prompt*)
591 *prompt*))
592 (force-output)
593 (let ((form (read *standard-input* nil magic-eof-cookie)))
594 (cond ((not (eq form magic-eof-cookie))
595 (let ((results
596 (multiple-value-list (interactive-eval form))))
597 (dolist (result results)
598 (fresh-line)
599 (prin1 result)))
600 (setf number-of-eofs 0))
601 ((eql (incf number-of-eofs) 1)
602 (if *batch-mode*
603 (quit)
604 (let ((stream (make-synonym-stream '*terminal-io*)))
605 (setf *standard-input* stream)
606 (setf *standard-output* stream)
607 (format t "~&Received EOF on *standard-input*, ~
608 switching to *terminal-io*.~%"))))
609 ((> number-of-eofs eofs-before-quit)
610 (format t "~&Received more than ~D EOFs; Aborting.~%"
611 eofs-before-quit)
612 (quit))
613 (t
614 (format t "~&Received EOF.~%")))))))))))
615
616
617 ;;; %Halt -- Interface
618 ;;;
619 ;;; A convenient way to get into the assembly level debugger.
620 ;;;
621 (defun %halt ()
622 (%primitive halt))

  ViewVC Help
Powered by ViewVC 1.1.5