1.0.23.36: typecheck :ALLOCATION :CLASS slot initforms in safe code
[sbcl/tcr.git] / src / compiler / target-main.lisp
blobaa011e1a84308c4da53a1c7bb97ac64aa056374c
1 ;;;; functions from classic CMU CL src/compiler/main.lisp which are
2 ;;;; needed only (and which may make sense only) on the
3 ;;;; cross-compilation target, not the cross-compilation host
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
14 (in-package "SB!C")
16 ;;;; CL:COMPILE
18 (defun get-lambda-to-compile (definition-designator)
19 (if (consp definition-designator)
20 definition-designator
21 (multiple-value-bind (definition env-p)
22 (function-lambda-expression definition-designator)
23 (when env-p
24 (error "~S was defined in a non-null environment."
25 definition-designator))
26 (unless definition
27 (error "can't find a definition for ~S" definition-designator))
28 definition)))
30 ;;; Handle the nontrivial case of CL:COMPILE.
31 (defun actually-compile (name definition *lexenv*)
32 (with-compilation-values
33 (sb!xc:with-compilation-unit ()
34 ;; FIXME: These bindings were copied from SUB-COMPILE-FILE with
35 ;; few changes. Once things are stable, the shared bindings
36 ;; probably be merged back together into some shared utility
37 ;; macro, or perhaps both merged into one of the existing utility
38 ;; macros SB-C::WITH-COMPILATION-VALUES or
39 ;; CL:WITH-COMPILATION-UNIT.
40 (let* (;; FIXME: Do we need the *INFO-ENVIRONMENT* rebinding
41 ;; here? It's a literal translation of the old CMU CL
42 ;; rebinding to (OR *BACKEND-INFO-ENVIRONMENT*
43 ;; *INFO-ENVIRONMENT*), and it's not obvious whether the
44 ;; rebinding to itself is needed now that SBCL doesn't
45 ;; need *BACKEND-INFO-ENVIRONMENT*.
46 (*info-environment* *info-environment*)
47 (form (get-lambda-to-compile definition))
48 (*source-info* (make-lisp-source-info form :parent *source-info*))
49 (*toplevel-lambdas* ())
50 (*block-compile* nil)
51 (*allow-instrumenting* nil)
52 (*code-coverage-records* nil)
53 (*code-coverage-blocks* nil)
54 (*compiler-error-bailout*
55 (lambda (&optional error)
56 (declare (ignore error))
57 (compiler-mumble
58 "~2&fatal error, aborting compilation~%")
59 (return-from actually-compile (values nil t nil))))
60 (*current-path* nil)
61 (*last-source-context* nil)
62 (*last-original-source* nil)
63 (*last-source-form* nil)
64 (*last-format-string* nil)
65 (*last-format-args* nil)
66 (*last-message-count* 0)
67 (*last-error-context* nil)
68 (*gensym-counter* 0)
69 ;; KLUDGE: This rebinding of policy is necessary so that
70 ;; forms such as LOCALLY at the REPL actually extend the
71 ;; compilation policy correctly. However, there is an
72 ;; invariant that is potentially violated: future
73 ;; refactoring must not allow this to be done in the file
74 ;; compiler. At the moment we're clearly alright, as we
75 ;; call %COMPILE with a core-object, not a fasl-stream,
76 ;; but caveat future maintainers. -- CSR, 2002-10-27
77 (*policy* (lexenv-policy *lexenv*))
78 ;; see above
79 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
80 ;; ditto
81 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*))
82 ;; FIXME: ANSI doesn't say anything about CL:COMPILE
83 ;; interacting with these variables, so we shouldn't. As
84 ;; of SBCL 0.6.7, COMPILE-FILE controls its verbosity by
85 ;; binding these variables, so as a quick hack we do so
86 ;; too. But a proper implementation would have verbosity
87 ;; controlled by function arguments and lexical variables.
88 (*compile-verbose* nil)
89 (*compile-print* nil))
90 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
91 (clear-stuff)
92 (find-source-paths form 0)
93 (%compile form (make-core-object)
94 :name name
95 :path '(original-source-start 0 0)))))))
97 (defun compile-in-lexenv (name definition lexenv)
98 (multiple-value-bind (compiled-definition warnings-p failure-p)
99 (cond
100 #!+sb-eval
101 ((sb!eval:interpreted-function-p definition)
102 (multiple-value-bind (definition lexenv)
103 (sb!eval:prepare-for-compile definition)
104 (actually-compile name definition lexenv)))
105 ((compiled-function-p definition)
106 (values definition nil nil))
107 (t (actually-compile name definition lexenv)))
108 (cond (name
109 (if (and (symbolp name)
110 (macro-function name))
111 (setf (macro-function name) compiled-definition)
112 (setf (fdefinition name) compiled-definition))
113 (values name warnings-p failure-p))
115 (values compiled-definition warnings-p failure-p)))))
117 (defun compile (name &optional (definition (or (macro-function name)
118 (fdefinition name))))
119 #!+sb-doc
120 "Coerce DEFINITION (by default, the function whose name is NAME)
121 to a compiled function, returning (VALUES THING WARNINGS-P FAILURE-P),
122 where if NAME is NIL, THING is the result of compilation, and
123 otherwise THING is NAME. When NAME is not NIL, the compiled function
124 is also set into (MACRO-FUNCTION NAME) if NAME names a macro, or into
125 (FDEFINITION NAME) otherwise."
126 (multiple-value-bind (function warnings-p failure-p)
127 (compile-in-lexenv name definition (make-null-lexenv))
128 (values (or function
129 name
130 (lambda (&rest arguments)
131 (error 'simple-program-error
132 :format-control
133 "Called function compiled with errors. Original ~
134 definition:~% ~S~@[~%Arguments:~% ~{ ~S~}~]"
135 :format-arguments (list definition arguments))))
136 warnings-p
137 failure-p)))