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

Contents of /src/code/lispinit.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5