0.9.10.1: Unicode character names -- aka More Bloat
[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) !cold-lose))
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 *after-gc-hooks* nil
96         *gc-inhibit* t
97         *gc-pending* nil
98         #!+sb-thread *stop-for-gc-pending* #!+sb-thread nil
99         sb!unix::*interrupts-enabled* t
100         sb!unix::*interrupt-pending* nil
101         *break-on-signals* nil
102         *maximum-error-depth* 10
103         *current-error-depth* 0
104         *cold-init-complete-p* nil
105         *type-system-initialized* nil)
106
107   (show-and-call thread-init-or-reinit)
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 !character-database-cold-init)
116   (show-and-call !character-name-database-cold-init)
117
118   (show-and-call !early-package-cold-init)
119   (show-and-call !package-cold-init)
120
121   ;; All sorts of things need INFO and/or (SETF INFO).
122   (/show0 "about to SHOW-AND-CALL !GLOBALDB-COLD-INIT")
123   (show-and-call !globaldb-cold-init)
124
125   ;; This needs to be done early, but needs to be after INFO is
126   ;; initialized.
127   (show-and-call !function-names-cold-init)
128   (show-and-call !fdefn-cold-init)
129
130   ;; Various toplevel forms call MAKE-ARRAY, which calls SUBTYPEP, so
131   ;; the basic type machinery needs to be initialized before toplevel
132   ;; forms run.
133   (show-and-call !type-class-cold-init)
134   (show-and-call !typedefs-cold-init)
135   (show-and-call !classes-cold-init)
136   (show-and-call !early-type-cold-init)
137   (show-and-call !late-type-cold-init)
138   (show-and-call !alien-type-cold-init)
139   (show-and-call !target-type-cold-init)
140   (show-and-call !vm-type-cold-init)
141   ;; FIXME: It would be tidy to make sure that that these cold init
142   ;; functions are called in the same relative order as the toplevel
143   ;; forms of the corresponding source files.
144
145   ;;(show-and-call !package-cold-init)
146   (show-and-call !policy-cold-init-or-resanify)
147   (/show0 "back from !POLICY-COLD-INIT-OR-RESANIFY")
148
149   (show-and-call !early-proclaim-cold-init)
150
151   ;; KLUDGE: Why are fixups mixed up with toplevel forms? Couldn't
152   ;; fixups be done separately? Wouldn't that be clearer and better?
153   ;; -- WHN 19991204
154   (/show0 "doing cold toplevel forms and fixups")
155   (/show0 "(LISTP *!REVERSED-COLD-TOPLEVELS*)=..")
156   (/hexstr (if (listp *!reversed-cold-toplevels*) "true" "NIL"))
157   (/show0 "about to calculate (LENGTH *!REVERSED-COLD-TOPLEVELS*)")
158   (/show0 "(LENGTH *!REVERSED-COLD-TOPLEVELS*)=..")
159   #!+sb-show (let ((r-c-tl-length (length *!reversed-cold-toplevels*)))
160                (/show0 "(length calculated..)")
161                (let ((hexstr (hexstr r-c-tl-length)))
162                  (/show0 "(hexstr calculated..)")
163                  (/primitive-print hexstr)))
164   (let (#!+sb-show (index-in-cold-toplevels 0))
165     #!+sb-show (declare (type fixnum index-in-cold-toplevels))
166
167     (dolist (toplevel-thing (prog1
168                                 (nreverse *!reversed-cold-toplevels*)
169                               ;; (Now that we've NREVERSEd it, it's
170                               ;; somewhat scrambled, so keep anyone
171                               ;; else from trying to get at it.)
172                               (makunbound '*!reversed-cold-toplevels*)))
173       #!+sb-show
174       (when (zerop (mod index-in-cold-toplevels 1024))
175         (/show0 "INDEX-IN-COLD-TOPLEVELS=..")
176         (/hexstr index-in-cold-toplevels))
177       #!+sb-show
178       (setf index-in-cold-toplevels
179             (the fixnum (1+ index-in-cold-toplevels)))
180       (typecase toplevel-thing
181         (function
182          (funcall toplevel-thing))
183         (cons
184          (case (first toplevel-thing)
185            (:load-time-value
186             (setf (svref *!load-time-values* (third toplevel-thing))
187                   (funcall (second toplevel-thing))))
188            (:load-time-value-fixup
189             (setf (sap-ref-word (second toplevel-thing) 0)
190                   (get-lisp-obj-address
191                    (svref *!load-time-values* (third toplevel-thing)))))
192            #!+(and (or x86 x86-64) gencgc)
193            (:load-time-code-fixup
194             (sb!vm::!envector-load-time-code-fixup (second toplevel-thing)
195                                                    (third  toplevel-thing)
196                                                    (fourth toplevel-thing)
197                                                    (fifth  toplevel-thing)))
198            (t
199             (!cold-lose "bogus fixup code in *!REVERSED-COLD-TOPLEVELS*"))))
200         (t (!cold-lose "bogus function in *!REVERSED-COLD-TOPLEVELS*")))))
201   (/show0 "done with loop over cold toplevel forms and fixups")
202
203   ;; Set sane values again, so that the user sees sane values instead
204   ;; of whatever is left over from the last DECLAIM/PROCLAIM.
205   (show-and-call !policy-cold-init-or-resanify)
206
207   ;; Only do this after toplevel forms have run, 'cause that's where
208   ;; DEFTYPEs are.
209   (setf *type-system-initialized* t)
210
211   ;; now that the type system is definitely initialized, fixup UNKNOWN
212   ;; types that have crept in.
213   (show-and-call !fixup-type-cold-init)
214   ;; run the PROCLAIMs.
215   (show-and-call !late-proclaim-cold-init)
216
217   (show-and-call os-cold-init-or-reinit)
218
219   (show-and-call stream-cold-init-or-reset)
220   (show-and-call !loader-cold-init)
221   (show-and-call !foreign-cold-init)
222   #!-win32 (show-and-call signal-cold-init-or-reinit)
223   (/show0 "enabling internal errors")
224   (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
225
226   ;; FIXME: This list of modes should be defined in one place and
227   ;; explicitly shared between here and REINIT.
228
229   ;; FIXME: For some unknown reason, NetBSD/x86 won't run with the
230   ;; :invalid trap enabled. That should be fixed, but not today...
231   ;; PEM -- April 5, 2004
232   (set-floating-point-modes
233    :traps '(:overflow #!-netbsd :invalid :divide-by-zero))
234
235   (show-and-call !class-finalize)
236
237   ;; The reader and printer are initialized very late, so that they
238   ;; can do hairy things like invoking the compiler as part of their
239   ;; initialization.
240   (show-and-call !reader-cold-init)
241   (let ((*readtable* *standard-readtable*))
242     (show-and-call !sharpm-cold-init)
243     (show-and-call !backq-cold-init))
244   (setf *readtable* (copy-readtable *standard-readtable*))
245   (setf sb!debug:*debug-readtable* (copy-readtable *standard-readtable*))
246   (sb!pretty:!pprint-cold-init)
247
248   ;; the ANSI-specified initial value of *PACKAGE*
249   (setf *package* (find-package "COMMON-LISP-USER"))
250
251   (/show0 "done initializing, setting *COLD-INIT-COMPLETE-P*")
252   (setf *cold-init-complete-p* t)
253
254   ;; The system is finally ready for GC.
255   (/show0 "enabling GC")
256   (gc-on)
257   (/show0 "doing first GC")
258   (gc :full t)
259   (/show0 "back from first GC")
260
261   ;; The show is on.
262   (terpri)
263   (/show0 "going into toplevel loop")
264   (handling-end-of-the-world
265     (toplevel-init)
266     (critically-unreachable "after TOPLEVEL-INIT")))
267
268 (defun quit (&key recklessly-p (unix-status 0))
269   #!+sb-doc
270   "Terminate the current Lisp. Things are cleaned up (with
271 UNWIND-PROTECT and so forth) unless RECKLESSLY-P is non-NIL. On
272 UNIX-like systems, UNIX-STATUS is used as the status code."
273   (declare (type (signed-byte 32) unix-status))
274   (/show0 "entering QUIT")
275   (if recklessly-p
276       (sb!unix:unix-exit unix-status)
277       (throw '%end-of-the-world unix-status))
278   (critically-unreachable "after trying to die in QUIT"))
279 \f
280 ;;;; initialization functions
281
282 (defun thread-init-or-reinit ()
283   (sb!thread::init-initial-thread)
284   (sb!thread::init-job-control)
285   (sb!thread::get-foreground))
286
287 (defun reinit ()
288   (setf *default-external-format* nil)
289   (without-interrupts
290     (without-gcing
291         (os-cold-init-or-reinit)
292       (thread-init-or-reinit)
293       (stream-reinit)
294       #!-win32 (signal-cold-init-or-reinit)
295       (setf (sb!alien:extern-alien "internal_errors_enabled" boolean) t)
296       ;; PRINT seems not to like x86 NPX denormal floats like
297       ;; LEAST-NEGATIVE-SINGLE-FLOAT, so the :UNDERFLOW exceptions are
298       ;; disabled by default. Joe User can explicitly enable them if
299       ;; desired.
300       ;;
301       ;; see also comment at the previous SET-FLOATING-POINT-MODES
302       ;; call site.
303       (set-floating-point-modes
304        :traps '(:overflow #!-netbsd :invalid :divide-by-zero))))
305   (gc-reinit)
306   ;; make sure TIME works correctly from saved cores
307   (setf *internal-real-time-base-seconds* nil)
308   (foreign-reinit)
309   (dolist (hook *init-hooks*)
310     (with-simple-restart (continue "Skip this initialization hook.")
311       (funcall hook))))
312 \f
313 ;;;; some support for any hapless wretches who end up debugging cold
314 ;;;; init code
315
316 ;;; Decode THING into hexadecimal notation using only machinery
317 ;;; available early in cold init.
318 #!+sb-show
319 (defun hexstr (thing)
320   (/noshow0 "entering HEXSTR")
321   (let ((addr (get-lisp-obj-address thing))
322         (str (make-string 10 :element-type 'base-char)))
323     (/noshow0 "ADDR and STR calculated")
324     (setf (char str 0) #\0
325           (char str 1) #\x)
326     (/noshow0 "CHARs 0 and 1 set")
327     (dotimes (i 8)
328       (/noshow0 "at head of DOTIMES loop")
329       (let* ((nibble (ldb (byte 4 0) addr))
330              (chr (char "0123456789abcdef" nibble)))
331         (declare (type (unsigned-byte 4) nibble)
332                  (base-char chr))
333         (/noshow0 "NIBBLE and CHR calculated")
334         (setf (char str (- 9 i)) chr
335               addr (ash addr -4))))
336     str))
337
338 #!+sb-show
339 (defun cold-print (x)
340   (typecase x
341     (simple-string (sb!sys:%primitive print x))
342     (symbol (sb!sys:%primitive print (symbol-name x)))
343     (list (let ((count 0))
344             (sb!sys:%primitive print "list:")
345             (dolist (i x)
346               (when (>= (incf count) 4)
347                 (sb!sys:%primitive print "...")
348                 (return))
349               (cold-print i))))
350     (t (sb!sys:%primitive print (hexstr x)))))