0.8.0.78.vector-nil-string.7:
[sbcl.git] / src / code / cold-init.lisp
1 ;;;; cold initialization stuff, plus some other miscellaneous stuff
2 ;;;; that we don't have any better place for
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!IMPL")
14 \f
15 ;;;; burning our ships behind us
16
17 ;;; There's a fair amount of machinery which is needed only at cold
18 ;;; init time, and should be discarded before freezing the final
19 ;;; system. We discard it by uninterning the associated symbols.
20 ;;; Rather than using a special table of symbols to be uninterned,
21 ;;; which might be tedious to maintain, instead we use a hack:
22 ;;; anything whose name matches a magic character pattern is
23 ;;; uninterned.
24 ;;;
25 ;;; FIXME: Are there other tables that need to have entries removed?
26 ;;; What about symbols of the form DEF!FOO?
27 (defun !unintern-init-only-stuff ()
28   (do ((any-changes? nil nil))
29       (nil)
30     (dolist (package (list-all-packages))
31       (do-symbols (symbol package)
32         (let ((name (symbol-name symbol)))
33           (when (or (string= name "!" :end1 1 :end2 1)
34                     (and (>= (length name) 2)
35                          (string= name "*!" :end1 2 :end2 2)))
36             (/show0 "uninterning cold-init-only symbol..")
37             (/primitive-print name)
38             ;; FIXME: Is this (FIRST (LAST *INFO-ENVIRONMENT*)) really
39             ;; meant to be an idiom to use?  Is there a more obvious
40             ;; name for this? [e.g. (GLOBAL-ENVIRONMENT)?]
41             (do-info ((first (last *info-environment*))
42                             :name entry :class class :type type)
43               (when (eq entry symbol)
44                 (clear-info class type entry)))
45             (unintern symbol package)
46             (setf any-changes? t)))))
47     (unless any-changes?
48       (return))))
49 \f
50 ;;;; putting ourselves out of our misery when things become too much to bear
51
52 (declaim (ftype (function (simple-string) nil) critically-unreachable))
53 (defun !cold-lose (msg)
54   (%primitive print msg)
55   (%primitive print "too early in cold init to recover from errors")
56   (%halt))
57
58 ;;; last-ditch error reporting for things which should never happen
59 ;;; and which, if they do happen, are sufficiently likely to torpedo
60 ;;; the normal error-handling system that we want to bypass it
61 (declaim (ftype (function (simple-string) nil) critically-unreachable))
62 (defun critically-unreachable (where)
63   (%primitive print "internal error: Control should never reach here, i.e.")
64   (%primitive print where)
65   (%halt))
66 \f
67 ;;;; !COLD-INIT
68
69 ;;; a list of toplevel things set by GENESIS
70 (defvar *!reversed-cold-toplevels*)
71
72 ;;; a SIMPLE-VECTOR set by GENESIS
73 (defvar *!load-time-values*)
74
75 (eval-when (:compile-toplevel :execute)
76   ;; FIXME: Perhaps we should make SHOW-AND-CALL-AND-FMAKUNBOUND, too,
77   ;; and use it for most of the cold-init functions. (Just be careful
78   ;; not to use it for the COLD-INIT-OR-REINIT functions.)
79   (sb!xc:defmacro show-and-call (name)
80     `(progn
81        (/primitive-print ,(symbol-name name))
82        (,name))))
83
84 ;;; called when a cold system starts up
85 (defun !cold-init ()
86   #!+sb-doc "Give the world a shove and hope it spins."
87
88   (/show0 "entering !COLD-INIT")
89
90   ;; FIXME: It'd probably be cleaner to have most of the stuff here
91   ;; handled by calls like !GC-COLD-INIT, !ERROR-COLD-INIT, and
92   ;; !UNIX-COLD-INIT. And *TYPE-SYSTEM-INITIALIZED* could be changed to
93   ;; *TYPE-SYSTEM-INITIALIZED-WHEN-BOUND* so that it doesn't need to
94   ;; be explicitly set in order to be meaningful.
95   (setf *gc-notify-stream* nil
96         *before-gc-hooks* nil
97         *after-gc-hooks* nil
98         *gc-inhibit* 1
99         *need-to-collect-garbage* nil
100         sb!unix::*interrupts-enabled* t
101         sb!unix::*interrupt-pending* nil
102         *break-on-signals* nil
103         *maximum-error-depth* 10
104         *current-error-depth* 0
105         *cold-init-complete-p* nil
106         *type-system-initialized* nil)
107
108   (show-and-call !typecheckfuns-cold-init)
109
110   ;; Anyone might call RANDOM to initialize a hash value or something;
111   ;; and there's nothing which needs to be initialized in order for
112   ;; this to be initialized, so we initialize it right away.
113   (show-and-call !random-cold-init)
114
115   (show-and-call !package-cold-init)
116   
117   ;; All sorts of things need INFO and/or (SETF INFO).
118   (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
119   (show-and-call !globaldb-cold-init)
120
121   ;; This needs to be done early, but needs to be after INFO is
122   ;; initialized.
123   (show-and-call !function-names-cold-init)
124   (show-and-call !fdefn-cold-init)
125
126   ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
127   ;; the basic type machinery needs to be initialized before toplevel
128   ;; forms run.
129   (show-and-call !type-class-cold-init)
130   (show-and-call !typedefs-cold-init)
131   (show-and-call !classes-cold-init)
132   (show-and-call !early-type-cold-init)
133   (show-and-call !late-type-cold-init)
134   (show-and-call !alien-type-cold-init)
135   (show-and-call !target-type-cold-init)
136   (show-and-call !vm-type-cold-init)
137   ;; FIXME: It would be tidy to make sure that that these cold init
138   ;; functions are called in the same relative order as the toplevel
139   ;; forms of the corresponding source files.
140
141   ;;(show-and-call !package-cold-init)
142   (show-and-call !policy-cold-init-or-resanify)
143   (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
144
145   (show-and-call !early-proclaim-cold-init)
146   
147   ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
148   ;; fixups be done separately? Wouldn't that be clearer and better?
149   ;; -- WHN 19991204
150   (/show0 "doing cold toplevel forms and fixups")
151   (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
152   (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
153   (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
154   (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
155   #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
156                (/show0 "(length calculated..)")
157                (let ((hexstr (hexstr r-c-tl-length)))
158                  (/show0 "(hexstr calculated..)")
159                  (/primitive-print hexstr)))
160   (let (#!+sb-show (index-in-cold-toplevels 0))
161     #!+sb-show (declare (type fixnum index-in-cold-toplevels))
162
163     (dolist (toplevel-thing (prog1
164                                 (nreverse *!reversed-cold-toplevels*)
165                               ;; (Now that we've NREVERSEd it, it's
166                               ;; somewhat scrambled, so keep anyone
167                               ;; else from trying to get at it.)
168                               (makunbound '*!reversed-cold-toplevels*)))
169       #!+sb-show
170       (when (zerop (mod index-in-cold-toplevels 1024))
171         (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
172         (/hexstr index-in-cold-toplevels))
173       #!+sb-show
174       (setf index-in-cold-toplevels
175             (the fixnum (1+ index-in-cold-toplevels)))
176       (typecase toplevel-thing
177         (function
178          (funcall toplevel-thing))
179         (cons
180          (case (first toplevel-thing)
181            (:load-time-value
182             (setf (svref *!load-time-values* (third toplevel-thing))
183                   (funcall (second toplevel-thing))))
184            (:load-time-value-fixup
185             (setf (sap-ref-32 (second toplevel-thing) 0)
186                   (get-lisp-obj-address
187                    (svref *!load-time-values* (third toplevel-thing)))))
188            #!+(and x86 gencgc)
189            (:load-time-code-fixup
190             (sb!vm::!envector-load-time-code-fixup (second toplevel-thing)
191                                                    (third  toplevel-thing)
192                                                    (fourth toplevel-thing)
193                                                    (fifth  toplevel-thing)))
194            (t
195             (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
196         (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
197   (/show0 "done with loop over cold toplevel forms and fixups")
198
199   ;; Set sane values again, so that the user sees sane values instead
200   ;; of whatever is left over from the last DECLAIM/PROCLAIM.
201   (show-and-call !policy-cold-init-or-resanify)
202
203   ;; Only do this after toplevel forms have run, 'cause that's where
204   ;; DEFTYPEs are.
205   (setf *type-system-initialized* t)
206
207   ;; now that the type system is definitely initialized, fixup UNKNOWN
208   ;; types that have crept in.
209   (show-and-call !fixup-type-cold-init)
210   ;; run the PROCLAIMs.
211   (show-and-call !late-proclaim-cold-init)
212   
213   (show-and-call os-cold-init-or-reinit)
214
215   (show-and-call stream-cold-init-or-reset)
216   (show-and-call !loader-cold-init)
217   (show-and-call signal-cold-init-or-reinit)
218   (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
219
220   ;; FIXME: This list of modes should be defined in one place and
221   ;; explicitly shared between here and REINIT.
222
223   ;; Why was this marked #!+alpha?  CMUCL does it here on all architectures
224   (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
225
226   (show-and-call !class-finalize)
227
228   ;; The reader and printer are initialized very late, so that they
229   ;; can do hairy things like invoking the compiler as part of their
230   ;; initialization.
231   (show-and-call !reader-cold-init)
232   (let ((*readtable* *standard-readtable*))
233     (show-and-call !sharpm-cold-init)
234     (show-and-call !backq-cold-init))
235   (setf *readtable* (copy-readtable *standard-readtable*))
236   (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
237   (sb!pretty:!pprint-cold-init)
238
239   ;; the ANSI-specified initial value of *PACKAGE*
240   (setf *package* (find-package "COMMON-LISP-USER"))
241
242   (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
243   (setf *cold-init-complete-p* t)
244
245   ;; The system is finally ready for GC.
246   (/show0 "enabling GC")
247   (gc-on)
248   (/show0 "doing first GC")
249   (gc :full t)
250   (/show0 "back from first GC")
251
252   ;; The show is on.
253   (terpri)
254   (/show0 "going into toplevel loop")
255   (handling-end-of-the-world 
256     (toplevel-init)
257     (critically-unreachable "after TOPLEVEL-INIT")))
258
259 (defun quit (&key recklessly-p
260                   (unix-code 0 unix-code-p)
261                   (unix-status unix-code))
262   #!+sb-doc
263   "Terminate the current Lisp. Things are cleaned up (with UNWIND-PROTECT
264   and so forth) unless RECKLESSLY-P is non-NIL. On UNIX-like systems,
265   UNIX-STATUS is used as the status code."
266   (declare (type (signed-byte 32) unix-status unix-code))
267   (/show0 "entering QUIT")
268   ;; FIXME: UNIX-CODE was deprecated in sbcl-0.6.8, after having been
269   ;; around for less than a year. It should be safe to remove it after
270   ;; a year.
271   (when unix-code-p
272     (warn "The UNIX-CODE argument is deprecated. Use the UNIX-STATUS argument
273 instead (which is another name for the same thing)."))
274   (if recklessly-p
275       (sb!unix:unix-exit unix-status)
276       (throw '%end-of-the-world unix-status))
277   (critically-unreachable "after trying to die in QUIT"))
278 \f
279 ;;;; initialization functions
280
281 (defun reinit ()
282   (without-interrupts
283     (without-gcing
284       (os-cold-init-or-reinit)
285       (stream-reinit)
286       (signal-cold-init-or-reinit)
287       (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
288       ;; PRINT seems not to like x86 NPX denormal floats like
289       ;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
290       ;; disabled by default. Joe User can explicitly enable them if
291       ;; desired.
292       (set-floating-point-modes :traps '(:overflow :invalid :divide-by-zero))
293
294       ;; Clear pseudo atomic in case this core wasn't compiled with
295       ;; support.
296       ;;
297       ;; FIXME: In SBCL our cores are always compiled with support. So
298       ;; we don't need to do this, do we? At least not for this
299       ;; reason.. (Perhaps we should do it anyway in case someone
300       ;; manages to save an image from within a pseudo-atomic-atomic
301       ;; operation?)
302       #!+x86 (setf *pseudo-atomic-atomic* 0)))
303   (gc-on)
304   (gc))
305 \f
306 ;;;; some support for any hapless wretches who end up debugging cold
307 ;;;; init code
308
309 ;;; Decode THING into hexadecimal notation using only machinery
310 ;;; available early in cold init.
311 #!+sb-show
312 (defun hexstr (thing)
313   (/noshow0 "entering HEXSTR")
314   (let ((addr (get-lisp-obj-address thing))
315         (str (make-string 10)))
316     (/noshow0 "ADDR and STR calculated")
317     (setf (char str 0) #\0
318           (char str 1) #\x)
319     (/noshow0 "CHARs 0 and 1 set")
320     (dotimes (i 8)
321       (/noshow0 "at head of DOTIMES loop")
322       (let* ((nibble (ldb (byte 4 0) addr))
323              (chr (char "0123456789abcdef" nibble)))
324         (declare (type (unsigned-byte 4) nibble)
325                  (base-char chr))
326         (/noshow0 "NIBBLE and CHR calculated")
327         (setf (char str (- 9 i)) chr
328               addr (ash addr -4))))
329     str))
330
331 #!+sb-show
332 (defun cold-print (x)
333   (typecase x
334     (simple-string (sb!sys:%primitive print x))
335     (symbol (sb!sys:%primitive print (symbol-name x)))
336     (list (let ((count 0))
337             (sb!sys:%primitive print "list:")
338             (dolist (i x)
339               (when (>= (incf count) 4)
340                 (sb!sys:%primitive print "...")
341                 (return))
342               (cold-print i))))
343     (t (sb!sys:%primitive print (hexstr x)))))