Enforce consistency between DEFINE-COLD-FOP and DEFINE-FOP.
[sbcl.git] / src / compiler / early-c.lisp
blobdc1f7c821b5c013a1207884d237be361ba5f25e0
1 ;;;; This file contains compiler code and compiler-related stuff which
2 ;;;; can be built early on. Some of the stuff may be here because it's
3 ;;;; needed early on, some other stuff (e.g. constants) just because
4 ;;;; it might as well be done early so we don't have to think about
5 ;;;; whether it's done early enough.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!C")
18 ;;; ANSI limits on compilation
19 (def!constant sb!xc:call-arguments-limit sb!xc:most-positive-fixnum
20 #!+sb-doc
21 "The exclusive upper bound on the number of arguments which may be passed
22 to a function, including &REST args.")
23 (def!constant sb!xc:lambda-parameters-limit sb!xc:most-positive-fixnum
24 #!+sb-doc
25 "The exclusive upper bound on the number of parameters which may be specified
26 in a given lambda list. This is actually the limit on required and &OPTIONAL
27 parameters. With &KEY and &AUX you can get more.")
28 (def!constant sb!xc:multiple-values-limit sb!xc:most-positive-fixnum
29 #!+sb-doc
30 "The exclusive upper bound on the number of multiple VALUES that you can
31 return.")
33 ;;;; cross-compiler-only versions of CL special variables, so that we
34 ;;;; don't have weird interactions with the host compiler
36 (defvar sb!xc:*compile-file-pathname*)
37 (defvar sb!xc:*compile-file-truename*)
38 (defvar sb!xc:*compile-print*)
39 (defvar sb!xc:*compile-verbose*)
41 ;;;; miscellaneous types used both in the cross-compiler and on the target
43 ;;;; FIXME: The INDEX and LAYOUT-DEPTHOID definitions probably belong
44 ;;;; somewhere else, not "early-c", since they're after all not part
45 ;;;; of the compiler.
47 ;;; the type of LAYOUT-DEPTHOID slot values
48 (def!type layout-depthoid () '(or index (integer -1 -1)))
50 ;;; An INLINEP value describes how a function is called. The values
51 ;;; have these meanings:
52 ;;; NIL No declaration seen: do whatever you feel like, but don't
53 ;;; dump an inline expansion.
54 ;;; :NOTINLINE NOTINLINE declaration seen: always do full function call.
55 ;;; :INLINE INLINE declaration seen: save expansion, expanding to it
56 ;;; if policy favors.
57 ;;; :MAYBE-INLINE
58 ;;; Retain expansion, but only use it opportunistically.
59 ;;; :MAYBE-INLINE is quite different from :INLINE. As explained
60 ;;; by APD on #lisp 2005-11-26: "MAYBE-INLINE lambda is
61 ;;; instantiated once per component, INLINE - for all
62 ;;; references (even under #'without FUNCALL)."
63 (deftype inlinep ()
64 '(member :inline :maybe-inline :notinline nil))
65 (defparameter *inlinep-translations*
66 '((inline . :inline)
67 (notinline . :notinline)
68 (maybe-inline . :maybe-inline)))
70 ;;; the lexical environment we are currently converting in
71 (defvar *lexenv*)
72 (declaim (type lexenv *lexenv*))
74 ;;; *FREE-VARS* translates from the names of variables referenced
75 ;;; globally to the LEAF structures for them. *FREE-FUNS* is like
76 ;;; *FREE-VARS*, only it deals with function names.
77 (defvar *free-vars*)
78 (defvar *free-funs*)
79 (declaim (type hash-table *free-vars* *free-funs*))
81 ;;; We use the same CONSTANT structure to represent all equal anonymous
82 ;;; constants. This hashtable translates from constants to the LEAFs that
83 ;;; represent them.
84 (defvar *constants*)
85 (declaim (type hash-table *constants*))
87 ;;; *ALLOW-INSTRUMENTING* controls whether we should allow the
88 ;;; insertion of instrumenting code (like a (CATCH ...)) around code
89 ;;; to allow the debugger RETURN and STEP commands to function (we
90 ;;; disallow it for internal stuff).
91 (defvar *allow-instrumenting*)
93 ;;; miscellaneous forward declarations
94 (defvar *code-segment*)
95 #!+sb-dyncount (defvar *collect-dynamic-statistics*)
96 (defvar *component-being-compiled*)
97 (defvar *compiler-error-context*)
98 (defvar *compiler-error-count*)
99 (defvar *compiler-warning-count*)
100 (defvar *compiler-style-warning-count*)
101 (defvar *compiler-note-count*)
102 (defvar *compiler-trace-output*)
103 (defvar *constraint-universe*)
104 (defvar *current-path*)
105 (defvar *current-component*)
106 (defvar *delayed-ir1-transforms*)
107 (defvar *eval-tlf-index*)
108 (defvar *handled-conditions*)
109 (defvar *disabled-package-locks*)
110 (defvar *policy*)
111 (defvar *macro-policy* nil)
112 (defvar *dynamic-counts-tn*)
113 (defvar *elsewhere*)
114 (defvar *event-info*)
115 (defvar *event-note-threshold*)
116 (defvar *failure-p*)
117 (defvar *fixup-notes*)
118 #!+inline-constants
119 (progn
120 (defvar *constant-segment*)
121 (defvar *constant-table*)
122 (defvar *constant-vector*))
123 (defvar *lexenv*)
124 (defvar *source-info*)
125 (defvar *source-plist*)
126 (defvar *source-namestring*)
127 (defvar *undefined-warnings*)
128 (defvar *warnings-p*)
129 (defvar *lambda-conversions*)
131 (defvar *stack-allocate-dynamic-extent* t
132 #!+sb-doc
133 "If true (the default), the compiler respects DYNAMIC-EXTENT declarations
134 and stack allocates otherwise inaccessible parts of the object whenever
135 possible. Potentially long (over one page in size) vectors are, however, not
136 stack allocated except in zero SAFETY code, as such a vector could overflow
137 the stack without triggering overflow protection.")
139 (!begin-collecting-cold-init-forms)
140 ;;; This lock is seized in the compiler, and related areas -- like the
141 ;;; classoid/layout/class system.
142 (defglobal **world-lock** nil)
143 (!cold-init-forms
144 (setf **world-lock** (sb!thread:make-mutex :name "World Lock")))
145 (!defun-from-collected-cold-init-forms !world-lock-cold-init)
147 (defmacro with-world-lock (() &body body)
148 `(sb!thread:with-recursive-lock (**world-lock**)
149 ,@body))
151 (declaim (type fixnum *compiler-sset-counter*))
152 (defvar *compiler-sset-counter* 0)
154 ;;; unique ID for the next object created (to let us track object
155 ;;; identity even across GC, useful for understanding weird compiler
156 ;;; bugs where something is supposed to be unique but is instead
157 ;;; exists as duplicate objects)
158 #!+sb-show
159 (progn
160 (defvar *object-id-counter* 0)
161 (defun new-object-id ()
162 (prog1
163 *object-id-counter*
164 (incf *object-id-counter*))))
166 ;;;; miscellaneous utilities
168 ;;; Delete any undefined warnings for NAME and KIND. This is for the
169 ;;; benefit of the compiler, but it's sometimes called from stuff like
170 ;;; type-defining code which isn't logically part of the compiler.
171 (declaim (ftype (function ((or symbol cons) keyword) (values))
172 note-name-defined))
173 (defun note-name-defined (name kind)
174 ;; We do this BOUNDP check because this function can be called when
175 ;; not in a compilation unit (as when loading top level forms).
176 (when (boundp '*undefined-warnings*)
177 (setq *undefined-warnings*
178 (delete-if (lambda (x)
179 (and (equal (undefined-warning-name x) name)
180 (eq (undefined-warning-kind x) kind)))
181 *undefined-warnings*)))
182 (values))
184 ;;; to be called when a variable is lexically bound
185 (declaim (ftype (function (symbol) (values)) note-lexical-binding))
186 (defun note-lexical-binding (symbol)
187 ;; This check is intended to protect us from getting silently
188 ;; burned when we define
189 ;; foo.lisp:
190 ;; (DEFVAR *FOO* -3)
191 ;; (DEFUN FOO (X) (+ X *FOO*))
192 ;; bar.lisp:
193 ;; (DEFUN BAR (X)
194 ;; (LET ((*FOO* X))
195 ;; (FOO 14)))
196 ;; and then we happen to compile bar.lisp before foo.lisp.
197 (when (looks-like-name-of-special-var-p symbol)
198 ;; FIXME: should be COMPILER-STYLE-WARNING?
199 (style-warn 'asterisks-around-lexical-variable-name
200 :format-control
201 "using the lexical binding of the symbol ~
202 ~/sb-impl::print-symbol-with-prefix/, not the~@
203 dynamic binding"
204 :format-arguments (list symbol)))
205 (values))
207 (def!struct (debug-name-marker (:make-load-form-fun dump-debug-name-marker)
208 (:print-function print-debug-name-marker)))
210 (defvar *debug-name-level* 4)
211 (defvar *debug-name-length* 12)
212 (defvar *debug-name-punt*)
213 (defvar *debug-name-sharp*)
214 (defvar *debug-name-ellipsis*)
216 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
217 (defun dump-debug-name-marker (marker &optional env)
218 (declare (ignore env))
219 (cond ((eq marker *debug-name-sharp*)
220 `(if (boundp '*debug-name-sharp*)
221 *debug-name-sharp*
222 (make-debug-name-marker)))
223 ((eq marker *debug-name-ellipsis*)
224 `(if (boundp '*debug-name-ellipsis*)
225 *debug-name-ellipsis*
226 (make-debug-name-marker)))
228 (warn "Dumping unknown debug-name marker.")
229 '(make-debug-name-marker)))))
231 (defun print-debug-name-marker (marker stream level)
232 (declare (ignore level))
233 (cond ((eq marker *debug-name-sharp*)
234 (write-char #\# stream))
235 ((eq marker *debug-name-ellipsis*)
236 (write-string "..." stream))
238 (write-string "???" stream))))
240 (setf *debug-name-sharp* (make-debug-name-marker)
241 *debug-name-ellipsis* (make-debug-name-marker))
243 (defun debug-name (type thing &optional context)
244 (let ((*debug-name-punt* nil))
245 (labels ((walk (x)
246 (typecase x
247 (cons
248 (if (plusp *debug-name-level*)
249 (let ((*debug-name-level* (1- *debug-name-level*)))
250 (do ((tail (cdr x) (cdr tail))
251 (name (cons (walk (car x)) nil)
252 (cons (walk (car tail)) name))
253 (n (1- *debug-name-length*) (1- n)))
254 ((or (not (consp tail))
255 (not (plusp n))
256 *debug-name-punt*)
257 (cond (*debug-name-punt*
258 (setf *debug-name-punt* nil)
259 (nreverse name))
260 ((atom tail)
261 (nconc (nreverse name) (walk tail)))
263 (setf *debug-name-punt* t)
264 (nconc (nreverse name) (list *debug-name-ellipsis*)))))))
265 *debug-name-sharp*))
266 ((or symbol number string)
269 (type-of x)))))
270 (let ((name (list* type (walk thing) (when context (name-context)))))
271 (when (legal-fun-name-p name)
272 (bug "~S is a legal function name, and cannot be used as a ~
273 debug name." name))
274 name))))