Eliminate COLD-FSET. It's just fop-funcall of %DEFUN
[sbcl.git] / src / compiler / target-main.lisp
blob28e28a78488cd59926f84095688c62e88ea26fbf
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 ;;;
32 ;;; If ERRORP is true signals an error immediately -- otherwise returns
33 ;;; a function that will signal the error.
34 (defun actually-compile (name definition *lexenv* source-info tlf errorp)
35 (let ((source-paths (when source-info *source-paths*)))
36 (with-compilation-values
37 (sb!xc:with-compilation-unit ()
38 ;; FIXME: These bindings were copied from SUB-COMPILE-FILE with
39 ;; few changes. Once things are stable, the shared bindings
40 ;; probably be merged back together into some shared utility
41 ;; macro, or perhaps both merged into one of the existing utility
42 ;; macros SB-C::WITH-COMPILATION-VALUES or
43 ;; CL:WITH-COMPILATION-UNIT.
44 (with-source-paths
45 (prog* ((tlf (or tlf 0))
46 ;; If we have a source-info from LOAD, we will
47 ;; also have a source-paths already set up -- so drop
48 ;; the ones from WITH-COMPILATION-VALUES.
49 (*source-paths* (or source-paths *source-paths*))
50 (form (get-lambda-to-compile definition))
51 (*source-info* (or source-info
52 (make-lisp-source-info
53 form :parent *source-info*)))
54 (*toplevel-lambdas* ())
55 (*block-compile* nil)
56 (*allow-instrumenting* nil)
57 (*code-coverage-records* nil)
58 (*code-coverage-blocks* nil)
59 (*current-path* nil)
60 (*last-source-context* nil)
61 (*last-original-source* nil)
62 (*last-source-form* nil)
63 (*last-format-string* nil)
64 (*last-format-args* nil)
65 (*last-message-count* 0)
66 (*last-error-context* nil)
67 (*gensym-counter* 0)
68 ;; KLUDGE: This rebinding of policy is necessary so that
69 ;; forms such as LOCALLY at the REPL actually extend the
70 ;; compilation policy correctly. However, there is an
71 ;; invariant that is potentially violated: future
72 ;; refactoring must not allow this to be done in the file
73 ;; compiler. At the moment we're clearly alright, as we
74 ;; call %COMPILE with a core-object, not a fasl-stream,
75 ;; but caveat future maintainers. -- CSR, 2002-10-27
76 (*policy* (lexenv-policy *lexenv*))
77 ;; see above
78 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
79 ;; ditto
80 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*))
81 ;; FIXME: ANSI doesn't say anything about CL:COMPILE
82 ;; interacting with these variables, so we shouldn't. As
83 ;; of SBCL 0.6.7, COMPILE-FILE controls its verbosity by
84 ;; binding these variables, so as a quick hack we do so
85 ;; too. But a proper implementation would have verbosity
86 ;; controlled by function arguments and lexical variables.
87 (*compile-verbose* nil)
88 (*compile-print* nil)
89 (oops nil))
90 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
91 (unless source-paths
92 (find-source-paths form tlf))
93 (let ((*compiler-error-bailout*
94 (lambda (e)
95 (setf oops e)
96 ;; Unwind the compiler frames: users want the know where
97 ;; the error came from, not how the compiler got there.
98 (go :error))))
99 (return
100 (with-world-lock ()
101 (%compile form (make-core-object)
102 :name name
103 :path `(original-source-start 0 ,tlf))))))
104 :error
105 ;; Either signal the error right away, or return a function that
106 ;; will signal the corresponding COMPILED-PROGRAM-ERROR. This is so
107 ;; that we retain our earlier behaviour when called with erronous
108 ;; lambdas via %SIMPLE-EVAL. We could legally do just either one
109 ;; always, but right now keeping the old behaviour seems like less
110 ;; painful option: compiler.pure.lisp is full of tests that make all
111 ;; sort of assumptions about when which things are signalled. FIXME,
112 ;; probably.
113 (if errorp
114 (error oops)
115 (let ((message (princ-to-string oops))
116 (source (source-to-string form)))
117 (return
118 (lambda (&rest arguments)
119 (declare (ignore arguments))
120 (error 'compiled-program-error
121 :message message
122 :source source)))))))))))
124 (defun compile-in-lexenv (name definition lexenv
125 &optional source-info tlf errorp)
126 (multiple-value-bind (compiled-definition warnings-p failure-p)
127 (cond
128 #!+sb-eval
129 ((sb!eval:interpreted-function-p definition)
130 (multiple-value-bind (definition lexenv)
131 (sb!eval:prepare-for-compile definition)
132 (actually-compile name definition lexenv source-info tlf errorp)))
133 ((compiled-function-p definition)
134 (values definition nil nil))
136 (actually-compile name definition lexenv source-info tlf errorp)))
137 (check-type compiled-definition compiled-function)
138 (cond (name
139 (if (and (symbolp name)
140 (macro-function name))
141 (setf (macro-function name) compiled-definition)
142 (setf (fdefinition name) compiled-definition))
143 (values name warnings-p failure-p))
145 (values compiled-definition warnings-p failure-p)))))
147 (defun compile (name &optional (definition (or (and (symbolp name)
148 (macro-function name))
149 (fdefinition name))))
150 #!+sb-doc
151 "Produce a compiled function from DEFINITION. If DEFINITION is a
152 lambda-expression, it is coerced to a function. If DEFINITION is an
153 interpreted function, it is compiled. If DEFINITION is already a compiled
154 function, it is used as-is. (Future versions of SBCL might try to
155 recompile the existing definition, but this is not currently supported.)
157 If NAME is NIL, the compiled function is returned as the primary value.
158 Otherwise the resulting compiled function replaces existing function
159 definition of NAME, and NAME is returned as primary value; if NAME is a symbol
160 tha names a macro, its macro function is replaced and NAME is returned as
161 primary value.
163 Also returns secondary value which is true if any conditions of type WARNING
164 occur during the compilation, and NIL otherwise.
166 Tertiary value is true if any conditions of type ERROR, or WARNING that are
167 not STYLE-WARNINGs occur during compilation, and NIL otherwise.
169 (compile-in-lexenv name definition (make-null-lexenv)))