create_thread_struct: don't allocate interrupt_data separately.
[sbcl.git] / src / cold / shared.lisp
blobea994fa8082eab673746885ad82f61361c763997
1 ;;;; stuff which is not specific to any particular build phase, but
2 ;;;; used by most of them
3 ;;;;
4 ;;;; Note: It's specifically not used when bootstrapping PCL, because
5 ;;;; we do SAVE-LISP after that, and we don't want to save extraneous
6 ;;;; bootstrapping machinery into the frozen image which will
7 ;;;; subsequently be used as the mother of all Lisp sessions.
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
18 ;;; SB-COLD holds stuff used to build the initial SBCL core file
19 ;;; (including not only the final construction of the core file, but
20 ;;; also the preliminary steps like e.g. building the cross-compiler
21 ;;; and running the cross-compiler to produce target FASL files).
22 (defpackage "SB-COLD" (:use "CL"))
24 (in-package "SB-COLD")
26 ;;; If TRUE, then COMPILE-FILE is being invoked only to process
27 ;;; :COMPILE-TOPLEVEL forms, not to produce an output file.
28 (defvar *compile-for-effect-only* nil)
30 ;;; prefixes for filename stems when cross-compiling. These are quite arbitrary
31 ;;; (although of course they shouldn't collide with anything we don't want to
32 ;;; write over). In particular, they can be either relative path names (e.g.
33 ;;; "host-objects/" or absolute pathnames (e.g. "/tmp/sbcl-xc-host-objects/").
34 ;;;
35 ;;; The cross-compilation process will force the creation of these directories
36 ;;; by executing CL:ENSURE-DIRECTORIES-EXIST (on the xc host Common Lisp).
37 (defvar *host-obj-prefix*)
38 (defvar *target-obj-prefix*)
40 (defvar *target-obj-suffix*
41 ;; Target fasl files are LOADed (actually only quasi-LOADed, in
42 ;; GENESIS) only by SBCL code, and it doesn't care about particular
43 ;; extensions, so we can use something arbitrary.
44 ".lisp-obj")
45 (defvar *target-assem-obj-suffix*
46 ;; Target fasl files from SB!C:ASSEMBLE-FILE are LOADed via GENESIS.
47 ;; The source files are compiled once as assembly files and once as
48 ;; normal lisp files. In the past, they were kept separate by
49 ;; clever symlinking in the source tree, but that became less clean
50 ;; as ports to host environments without symlinks started appearing.
51 ;; In order to keep them separate, we have the assembled versions
52 ;; with a separate suffix.
53 ".assem-obj")
55 ;;; a function of one functional argument, which calls its functional argument
56 ;;; in an environment suitable for compiling the target. (This environment
57 ;;; includes e.g. a suitable *FEATURES* value.)
58 (declaim (type function *in-target-compilation-mode-fn*))
59 (defvar *in-target-compilation-mode-fn*)
61 ;;; a function with the same calling convention as CL:COMPILE-FILE, to be
62 ;;; used to translate ordinary Lisp source files into target object files
63 (declaim (type function *target-compile-file*))
64 (defvar *target-compile-file*)
66 ;;; designator for a function with the same calling convention as
67 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
68 ;;; object files
69 (defvar *target-assemble-file*)
71 ;;;; some tools
73 ;;; Take the file named X and make it into a file named Y. Sorta like
74 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
75 ;;; information from the original filename to influence the final
76 ;;; filename. (The reason that it's only sorta like UNIX is that in
77 ;;; UNIX "mv foo bar/" will work, but the analogous
78 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
79 ;;;
80 ;;; (This is a workaround for the weird behavior of Debian CMU CL
81 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
82 ;;; called "dir/dir/y". If that behavior goes away, then we should be
83 ;;; able to get rid of this function and use plain RENAME-FILE in the
84 ;;; COMPILE-STEM function above. -- WHN 19990321
85 (defun rename-file-a-la-unix (x y)
87 (let ((path ;; (Note that the TRUENAME expression here is lifted from an
88 ;; example in the ANSI spec for TRUENAME.)
89 (with-open-file (stream y :direction :output)
90 (close stream)
91 ;; From the ANSI spec: "In this case, the file is closed
92 ;; when the truename is tried, so the truename
93 ;; information is reliable."
94 (truename stream))))
95 (delete-file path)
96 (rename-file x path)))
97 (compile 'rename-file-a-la-unix)
99 ;;; other miscellaneous tools
100 (load "src/cold/read-from-file.lisp")
101 (load "src/cold/rename-package-carefully.lisp")
102 (load "src/cold/with-stuff.lisp")
104 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
105 (load "src/cold/ansify.lisp")
107 ;;;; special read-macros for building the cold system (and even for
108 ;;;; building some of our tools for building the cold system)
110 (load "src/cold/shebang.lisp")
112 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
113 ;;; not in general the same as the *FEATURES* set for the host Lisp.
114 ;;; In order to refer to target features specifically, we refer to
115 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
116 ;;; readmacros instead of the ordinary #+ and #- readmacros.
117 (setf *shebang-features*
118 (let* ((default-features
119 (funcall (compile
121 (read-from-file "local-target-features.lisp-expr"))
122 (read-from-file "base-target-features.lisp-expr")))
123 (customizer-file-name "customize-target-features.lisp")
124 (customizer (if (probe-file customizer-file-name)
125 (compile nil
126 (read-from-file customizer-file-name))
127 #'identity)))
128 (funcall customizer default-features)))
129 (let ((*print-length* nil)
130 (*print-level* nil))
131 (format t
132 "target features *SHEBANG-FEATURES*=~%~@<~S~:>~%"
133 *shebang-features*))
135 (defvar *shebang-backend-subfeatures*
136 (let* ((default-subfeatures nil)
137 (customizer-file-name "customize-backend-subfeatures.lisp")
138 (customizer (if (probe-file customizer-file-name)
139 (compile nil
140 (read-from-file customizer-file-name))
141 #'identity)))
142 (funcall customizer default-subfeatures)))
143 (let ((*print-length* nil)
144 (*print-level* nil))
145 (format t
146 "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
147 *shebang-backend-subfeatures*))
149 ;;; Call for effect of signaling an error if no target picked.
150 (target-platform-name)
152 ;;; You can get all the way through make-host-1 without either one of these
153 ;;; features, but then 'bit-bash' will fail to cross-compile.
154 (unless (intersection '(:big-endian :little-endian) *shebang-features*)
155 (warn "You'll have bad time without either endian-ness defined"))
157 ;;; Some feature combinations simply don't work, and sometimes don't
158 ;;; fail until quite a ways into the build. Pick off the more obvious
159 ;;; combinations now, and provide a description of what the actual
160 ;;; failure is (not always obvious from when the build fails).
161 (let ((feature-compatibility-tests
162 '(("(and sb-thread (not gencgc))"
163 ":SB-THREAD requires :GENCGC")
164 ("(and sb-thread (not (or ppc x86 x86-64 arm64)))"
165 ":SB-THREAD not supported on selected architecture")
166 ("(and gencgc cheneygc)"
167 ":GENCGC and :CHENEYGC are incompatible")
168 ("(and cheneygc (not (or alpha arm hppa mips ppc sparc)))"
169 ":CHENEYGC not supported on selected architecture")
170 ("(and gencgc (not (or sparc ppc x86 x86-64 arm arm64)))"
171 ":GENCGC not supported on selected architecture")
172 ("(not (or gencgc cheneygc))"
173 "One of :GENCGC or :CHENEYGC must be enabled")
174 ("(and win32 (not (and sb-thread
175 sb-safepoint sb-thruption sb-wtimer
176 sb-dynamic-core)))"
177 ":SB-WIN32 requires :SB-THREAD and related features")
178 ("(and sb-dynamic-core (not (and linkage-table sb-thread)))"
179 ;; Subtle memory corruption follows when sb-dynamic-core is
180 ;; active, and non-threaded allocation routines have not been
181 ;; updated to take the additional indirection into account.
182 ;; Let's avoid this unusual combination.
183 ":SB-DYNAMIC-CORE requires :LINKAGE-TABLE and :SB-THREAD")
184 ("(and sb-eval sb-fasteval)"
185 ;; It sorta kinda works to have both, but there should be no need,
186 ;; and it's not really supported.
187 "At most one interpreter can be selected")
188 ;; There is still hope to make multithreading on DragonFly x86-64
189 ("(and sb-thread x86 dragonfly)"
190 ":SB-THREAD not supported on selected architecture")))
191 (failed-test-descriptions nil))
192 (dolist (test feature-compatibility-tests)
193 (let ((*features* *shebang-features*))
194 (when (read-from-string (concatenate 'string "#+" (first test) "T NIL"))
195 (push (second test) failed-test-descriptions))))
196 (when failed-test-descriptions
197 (error "Feature compatibility check failed, ~S"
198 failed-test-descriptions)))
200 ;;;; cold-init-related PACKAGE and SYMBOL tools
202 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
203 ;;; it's probably a mistake if we change it (beyond changing the
204 ;;; values of special variables such as *** and +, anyway). Set up
205 ;;; machinery to warn us when/if we change it.
207 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
208 #!+sb-show
209 (progn
210 (load "src/cold/snapshot.lisp")
211 (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
213 ;;;; master list of source files and their properties
215 ;;; flags which can be used to describe properties of source files
216 (defparameter
217 *expected-stem-flags*
218 '(;; meaning: This file is not to be compiled when building the
219 ;; cross-compiler which runs on the host ANSI Lisp. ("not host
220 ;; code", i.e. does not execute on host -- but may still be
221 ;; cross-compiled by the host, so that it executes on the target)
222 :not-host
223 ;; meaning: This file is not to be compiled as part of the target
224 ;; SBCL. ("not target code" -- but still presumably host code,
225 ;; used to support the cross-compilation process)
226 :not-target
227 ;; meaning: This file must always be compiled by 'slam.lisp' even if
228 ;; the object is not out of date with respect to its source.
229 ;; Necessary if there are compile-time-too effects that are not
230 ;; reflected into make-host-2 by load-time actions of make-host-1.
231 :slam-forcibly
232 ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
233 ;; When the compiler is SBCL's COMPILE-FILE or something like it,
234 ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
235 ;; of exciting low-level information about representation selection,
236 ;; VOPs used by the compiler, and bits of assembly.
237 :trace-file
238 ;; meaning: This file is to be processed with the SBCL assembler,
239 ;; not COMPILE-FILE. (Note that this doesn't make sense unless
240 ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
241 ;; while the cross-compiler is being built in the host ANSI Lisp.)
242 :assem
243 ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
244 ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
245 ;; For now, it exists so that compilation can proceed through the
246 ;; legacy warnings in src/compiler/x86/array.lisp, which I've
247 ;; never figured out but which were apparently acceptable in CMU
248 ;; CL. Eventually, it would be great to just get rid of all
249 ;; warnings and remove support for this flag. -- WHN 19990323)
250 :ignore-failure-p))
252 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
254 (defvar *array-to-specialization* (make-hash-table :test #'eq))
256 (defmacro do-stems-and-flags ((stem flags) &body body)
257 (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
258 `(dolist (,stem-and-flags *stems-and-flags*)
259 (let ((,stem (first ,stem-and-flags))
260 (,flags (rest ,stem-and-flags)))
261 ,@body
262 (clrhash *array-to-specialization*)))))
264 ;;; Given a STEM, remap the path component "/target/" to a suitable
265 ;;; target directory.
266 (defun stem-remap-target (stem)
267 (let ((position (search "/target/" stem)))
268 (if position
269 (concatenate 'string
270 (subseq stem 0 (1+ position))
271 (target-platform-name)
272 (subseq stem (+ position 7)))
273 stem)))
274 (compile 'stem-remap-target)
276 ;;; Determine the source path for a stem.
277 (defun stem-source-path (stem)
278 (concatenate 'string "" (stem-remap-target stem) ".lisp"))
279 (compile 'stem-source-path)
281 ;;; Determine the object path for a stem/flags/mode combination.
282 (defun stem-object-path (stem flags mode)
283 (multiple-value-bind
284 (obj-prefix obj-suffix)
285 (ecase mode
286 (:host-compile
287 ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
288 ;; has the same extension that the host uses for COMPILE-FILE
289 ;; output, so we have to be careful to use the xc host's preferred
290 ;; extension.
291 (values *host-obj-prefix*
292 (concatenate 'string "."
293 (pathname-type (compile-file-pathname stem)))))
294 (:target-compile (values *target-obj-prefix*
295 (if (find :assem flags)
296 *target-assem-obj-suffix*
297 *target-obj-suffix*))))
298 (concatenate 'string obj-prefix (stem-remap-target stem) obj-suffix)))
299 (compile 'stem-object-path)
301 ;;; Check for stupid typos in FLAGS list keywords.
302 (let ((stems (make-hash-table :test 'equal)))
303 (do-stems-and-flags (stem flags)
304 ;; We do duplicate stem comparison based on the object path in
305 ;; order to cover the case of stems with an :assem flag, which
306 ;; have two entries but separate object paths for each. KLUDGE:
307 ;; We have to bind *target-obj-prefix* here because it's normally
308 ;; set up later in the build process and we don't actually care
309 ;; what it is so long as it doesn't change while we're checking
310 ;; for duplicate stems.
311 (let* ((*target-obj-prefix* "")
312 (object-path (stem-object-path stem flags :target-compile)))
313 (if (gethash object-path stems)
314 (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
315 (setf (gethash object-path stems) t)))
316 ;; FIXME: We should make sure that the :assem flag is only used
317 ;; when paired with :not-host.
318 (let ((set-difference (set-difference flags *expected-stem-flags*)))
319 (when set-difference
320 (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
321 set-difference)))))
323 ;;;; tools to compile SBCL sources to create the cross-compiler
325 ;;; a wrapper for compilation/assembly, used mostly to centralize
326 ;;; the procedure for finding full filenames from "stems"
328 ;;; Compile the source file whose basic name is STEM, using some
329 ;;; standard-for-the-SBCL-build-process procedures to generate the
330 ;;; full pathnames of source file and object file. Return the pathname
331 ;;; of the object file for STEM.
333 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
334 ;;; :HOST-COMPILE and :TARGET-COMPILE.
335 (defun compile-stem (stem flags mode)
337 (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
338 ;; Lisp Way, although it works just fine for common UNIX environments.
339 ;; Should it come to pass that the system is ported to environments
340 ;; where version numbers and so forth become an issue, it might become
341 ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
342 ;; machinery instead of just using strings. In the absence of such a
343 ;; port, it might or might be a good idea to do the rewrite.
344 ;; -- WHN 19990815
345 (src (stem-source-path stem))
346 (obj (stem-object-path stem flags mode))
347 (tmp-obj (concatenate 'string obj "-tmp"))
349 (compile-file (ecase mode
350 (:host-compile #'compile-file)
351 (:target-compile (if (find :assem flags)
352 *target-assemble-file*
353 *target-compile-file*))))
354 (trace-file (find :trace-file flags))
355 (ignore-failure-p (find :ignore-failure-p flags)))
356 (declare (type function compile-file))
358 (ensure-directories-exist obj :verbose t)
360 ;; We're about to set about building a new object file. First, we
361 ;; delete any preexisting object file in order to avoid confusing
362 ;; ourselves later should we happen to bail out of compilation
363 ;; with an error.
364 (when (probe-file obj)
365 (delete-file obj))
367 ;; Original comment:
369 ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
370 ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
371 ;; but works OK with absolute pathnames.
373 ;; following discussion on cmucl-imp 2002-07
374 ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
375 ;; absolute pathnames all the time; it is no longer clear that the
376 ;; original behaviour in CLISP was wrong or that the current
377 ;; behaviour is right; and in any case absolutifying the pathname
378 ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
379 (setf tmp-obj
380 ;; (Note that this idiom is taken from the ANSI
381 ;; documentation for TRUENAME.)
382 (with-open-file (stream tmp-obj
383 :direction :output
384 ;; Compilation would overwrite the
385 ;; temporary object anyway and overly
386 ;; strict implementations default
387 ;; to :ERROR.
388 :if-exists :supersede)
389 (close stream)
390 (truename stream)))
391 ;; and some compilers (e.g. OpenMCL) will complain if they're
392 ;; asked to write over a file that exists already (and isn't
393 ;; recognizeably a fasl file), so
394 (when (probe-file tmp-obj)
395 (delete-file tmp-obj))
397 ;; Try to use the compiler to generate a new temporary object file.
398 (flet ((report-recompile-restart (stream)
399 (format stream "Recompile file ~S" src))
400 (report-continue-restart (stream)
401 (format stream "Continue, using possibly bogus file ~S" obj)))
402 (tagbody
403 retry-compile-file
404 (multiple-value-bind (output-truename warnings-p failure-p)
405 (if trace-file
406 (funcall compile-file src :output-file tmp-obj
407 :trace-file t :allow-other-keys t)
408 (funcall compile-file src :output-file tmp-obj))
409 (declare (ignore warnings-p))
410 (cond ((not output-truename)
411 (error "couldn't compile ~S" src))
412 (failure-p
413 (if ignore-failure-p
414 (warn "ignoring FAILURE-P return value from compilation of ~S"
415 src)
416 (unwind-protect
417 (restart-case
418 (error "FAILURE-P was set when creating ~S."
419 obj)
420 (recompile ()
421 :report report-recompile-restart
422 (go retry-compile-file))
423 (continue ()
424 :report report-continue-restart
425 (setf failure-p nil)))
426 ;; Don't leave failed object files lying around.
427 (when (and failure-p (probe-file tmp-obj))
428 (delete-file tmp-obj)
429 (format t "~&deleted ~S~%" tmp-obj)))))
430 ;; Otherwise: success, just fall through.
431 (t nil)))))
433 ;; If we get to here, compilation succeeded, so it's OK to rename
434 ;; the temporary output file to the permanent object file.
435 ;; ASSEMBLE-FILE produces no output in the preload pass.
436 ;; (The compiler produces an empty file.)
437 (if (and *compile-for-effect-only* (search "/assembly/" obj))
439 (rename-file-a-la-unix tmp-obj obj))
441 ;; nice friendly traditional return value
442 (pathname obj)))
443 (compile 'compile-stem)
445 ;;; Execute function FN in an environment appropriate for compiling the
446 ;;; cross-compiler's source code in the cross-compilation host.
447 (defun in-host-compilation-mode (fn)
448 (declare (type function fn))
449 (let ((*features* (cons :sb-xc-host *features*))
450 ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
451 ;; base-target-features.lisp-expr:
452 (*shebang-features* (set-difference *shebang-features*
453 '(:sb-propagate-float-type
454 :sb-propagate-fun-type))))
455 (with-additional-nickname ("SB-XC" "SB!XC")
456 (funcall fn))))
457 (compile 'in-host-compilation-mode)
459 ;;; Process a file as source code for the cross-compiler, compiling it
460 ;;; (if necessary) in the appropriate environment, then loading it
461 ;;; into the cross-compilation host Common lisp.
462 (defun host-cload-stem (stem flags)
463 (let ((compiled-filename (in-host-compilation-mode
464 (lambda ()
465 (compile-stem stem flags :host-compile)))))
466 (load compiled-filename)))
467 (compile 'host-cload-stem)
469 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
470 (defun host-load-stem (stem flags)
471 (load (stem-object-path stem flags :host-compile)))
472 (compile 'host-load-stem)
474 ;;;; tools to compile SBCL sources to create object files which will
475 ;;;; be used to create the target SBCL .core file
477 ;;; Run the cross-compiler on a file in the source directory tree to
478 ;;; produce a corresponding file in the target object directory tree.
479 (defun target-compile-stem (stem flags)
480 (funcall *in-target-compilation-mode-fn*
481 (lambda ()
482 (compile-stem stem flags :target-compile))))
483 (compile 'target-compile-stem)
485 ;;; (This function is not used by the build process, but is intended
486 ;;; for interactive use when experimenting with the system. It runs
487 ;;; the cross-compiler on test files with arbitrary filenames, not
488 ;;; necessarily in the source tree, e.g. in "/tmp".)
489 (defun target-compile-file (filename)
490 (funcall *in-target-compilation-mode-fn*
491 (lambda ()
492 (funcall *target-compile-file* filename))))
493 (compile 'target-compile-file)
495 (defun make-assembler-package (pkg-name)
496 (when (find-package pkg-name)
497 (delete-package pkg-name))
498 (let ((pkg (make-package pkg-name
499 :use '("CL" "SB!INT" "SB!EXT" "SB!KERNEL" "SB!VM"
500 "SB!SYS" ; for SAP accessors
501 ;; Dependence of the assembler on the compiler
502 ;; feels a bit backwards, but assembly needs
503 ;; TN-SC, TN-OFFSET, etc. because the compiler
504 ;; doesn't speak the assembler's language.
505 ;; Rather vice-versa.
506 "SB!C"))))
507 ;; Both SB-ASSEM and SB-DISASSEM export these two symbols.
508 ;; Neither is shadowing-imported. If you need one, package-qualify it.
509 (shadow '("SEGMENT" "MAKE-SEGMENT") pkg)
510 (use-package '("SB!ASSEM" "SB!DISASSEM") pkg)
511 pkg))