x86-64: Treat more symbols as having immediate storage class
[sbcl.git] / src / compiler / early-c.lisp
blobba2c2bd69c868bc9ddc2341faa60f1eae264b659
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 (defconstant sb!xc:call-arguments-limit sb!xc:most-positive-fixnum
20 "The exclusive upper bound on the number of arguments which may be passed
21 to a function, including &REST args.")
22 (defconstant sb!xc:lambda-parameters-limit sb!xc:most-positive-fixnum
23 "The exclusive upper bound on the number of parameters which may be specified
24 in a given lambda list. This is actually the limit on required and &OPTIONAL
25 parameters. With &KEY and &AUX you can get more.")
26 (defconstant sb!xc:multiple-values-limit sb!xc:most-positive-fixnum
27 "The exclusive upper bound on the number of multiple VALUES that you can
28 return.")
30 ;;;; cross-compiler-only versions of CL special variables, so that we
31 ;;;; don't have weird interactions with the host compiler
33 (defvar sb!xc:*compile-file-pathname*)
34 (defvar sb!xc:*compile-file-truename*)
35 (defvar sb!xc:*compile-print*)
36 (defvar sb!xc:*compile-verbose*)
38 ;;;; miscellaneous types used both in the cross-compiler and on the target
40 ;;;; FIXME: The INDEX and LAYOUT-DEPTHOID definitions probably belong
41 ;;;; somewhere else, not "early-c", since they're after all not part
42 ;;;; of the compiler.
44 ;;; the type of LAYOUT-DEPTHOID slot values
45 (def!type layout-depthoid () '(or index (integer -1 -1)))
46 (def!type layout-bitmap ()
47 ;; FIXME: Probably should exclude negative bignum
48 #!+compact-instance-header 'integer
49 #!-compact-instance-header '(and integer (not (eql 0))))
51 ;;; An INLINEP value describes how a function is called. The values
52 ;;; have these meanings:
53 ;;; NIL No declaration seen: do whatever you feel like, but don't
54 ;;; dump an inline expansion.
55 ;;; :NOTINLINE NOTINLINE declaration seen: always do full function call.
56 ;;; :INLINE INLINE declaration seen: save expansion, expanding to it
57 ;;; if policy favors.
58 ;;; :MAYBE-INLINE
59 ;;; Retain expansion, but only use it opportunistically.
60 ;;; :MAYBE-INLINE is quite different from :INLINE. As explained
61 ;;; by APD on #lisp 2005-11-26: "MAYBE-INLINE lambda is
62 ;;; instantiated once per component, INLINE - for all
63 ;;; references (even under #'without FUNCALL)."
64 (deftype inlinep ()
65 '(member :inline :maybe-inline :notinline nil))
66 (defconstant-eqx +inlinep-translations+
67 '((inline . :inline)
68 (notinline . :notinline)
69 (maybe-inline . :maybe-inline))
70 #'equal)
72 ;;; *FREE-VARS* translates from the names of variables referenced
73 ;;; globally to the LEAF structures for them. *FREE-FUNS* is like
74 ;;; *FREE-VARS*, only it deals with function names.
75 (defvar *free-vars*)
76 (defvar *free-funs*)
77 (declaim (type hash-table *free-vars* *free-funs*))
79 ;;; We use the same CONSTANT structure to represent all equal anonymous
80 ;;; constants. This hashtable translates from constants to the LEAFs that
81 ;;; represent them.
82 (defvar *constants*)
83 (declaim (type hash-table *constants*))
85 ;;; *ALLOW-INSTRUMENTING* controls whether we should allow the
86 ;;; insertion of instrumenting code (like a (CATCH ...)) around code
87 ;;; to allow the debugger RETURN and STEP commands to function (we
88 ;;; disallow it for internal stuff).
89 (defvar *allow-instrumenting*)
91 ;;; miscellaneous forward declarations
92 (defvar *code-segment*)
93 ;; FIXME: this is a kludge due to the absence of a 'vop' argument
94 ;; to ALLOCATION-TRAMP in the x86-64 backend.
95 (defvar *code-is-immobile*)
96 #!+sb-dyncount (defvar *collect-dynamic-statistics*)
97 (defvar *component-being-compiled*)
98 (defvar *compiler-error-context*)
99 (defvar *compiler-error-count*)
100 (defvar *compiler-warning-count*)
101 (defvar *compiler-style-warning-count*)
102 (defvar *compiler-note-count*)
103 (defvar *compiler-trace-output*)
104 (defvar *constraint-universe*)
105 (defvar *current-path*)
106 (defvar *current-component*)
107 (defvar *delayed-ir1-transforms*)
108 (defvar *eval-tlf-index*)
109 (defvar *dynamic-counts-tn*)
110 (defvar *elsewhere*)
111 (defvar *event-info*)
112 (defvar *event-note-threshold*)
113 (defvar *failure-p*)
114 (defvar *fixup-notes*)
115 #!+inline-constants
116 (progn
117 (defvar *unboxed-constants*)
118 (defstruct (unboxed-constants (:conc-name constant-)
119 (:predicate nil) (:copier nil))
120 (table (make-hash-table :test #'equal) :read-only t)
121 (segment
122 (sb!assem:make-segment :type :elsewhere
123 :run-scheduler nil
124 :inst-hook (default-segment-inst-hook)
125 :alignment 0) :read-only t)
126 (vector (make-array 16 :adjustable t :fill-pointer 0) :read-only t))
127 (declaim (freeze-type unboxed-constants)))
128 (defvar *source-info*)
129 (defvar *source-plist*)
130 (defvar *source-namestring*)
131 (defvar *undefined-warnings*)
132 (defvar *warnings-p*)
133 (defvar *lambda-conversions*)
134 (defvar *compile-object* nil)
136 (defvar *stack-allocate-dynamic-extent* t
137 "If true (the default), the compiler respects DYNAMIC-EXTENT declarations
138 and stack allocates otherwise inaccessible parts of the object whenever
139 possible. Potentially long (over one page in size) vectors are, however, not
140 stack allocated except in zero SAFETY code, as such a vector could overflow
141 the stack without triggering overflow protection.")
143 (!begin-collecting-cold-init-forms)
144 ;;; This lock is seized in the compiler, and related areas -- like the
145 ;;; classoid/layout/class system.
146 (defglobal **world-lock** nil)
147 (!cold-init-forms
148 (setf **world-lock** (sb!thread:make-mutex :name "World Lock")))
149 (!defun-from-collected-cold-init-forms !world-lock-cold-init)
151 (defmacro with-world-lock (() &body body)
152 `(sb!thread:with-recursive-lock (**world-lock**)
153 ,@body))
155 (declaim (type fixnum *compiler-sset-counter*))
156 (defvar *compiler-sset-counter* 0)
158 ;;; unique ID for the next object created (to let us track object
159 ;;; identity even across GC, useful for understanding weird compiler
160 ;;; bugs where something is supposed to be unique but is instead
161 ;;; exists as duplicate objects)
162 #!+sb-show
163 (progn
164 (defvar *object-id-counter* 0)
165 (defun new-object-id ()
166 (prog1
167 *object-id-counter*
168 (incf *object-id-counter*))))
170 ;;;; miscellaneous utilities
172 ;;; This is for "observers" who want to know if type names have been added.
173 ;;; Rather than registering listeners, they can detect changes by comparing
174 ;;; their stored nonce to the current nonce. Additionally the observers
175 ;;; can detect whether function definitions have occurred.
176 (declaim (fixnum *type-cache-nonce*))
177 (!defglobal *type-cache-nonce* 0)
179 (def!struct (undefined-warning
180 #-no-ansi-print-object
181 (:print-object (lambda (x s)
182 (print-unreadable-object (x s :type t)
183 (prin1 (undefined-warning-name x) s))))
184 (:copier nil))
185 ;; the name of the unknown thing
186 (name nil :type (or symbol list))
187 ;; the kind of reference to NAME
188 (kind (missing-arg) :type (member :function :type :variable))
189 ;; the number of times this thing was used
190 (count 0 :type unsigned-byte)
191 ;; a list of COMPILER-ERROR-CONTEXT structures describing places
192 ;; where this thing was used. Note that we only record the first
193 ;; *UNDEFINED-WARNING-LIMIT* calls.
194 (warnings () :type list))
196 ;;; Delete any undefined warnings for NAME and KIND. This is for the
197 ;;; benefit of the compiler, but it's sometimes called from stuff like
198 ;;; type-defining code which isn't logically part of the compiler.
199 (declaim (ftype (function ((or symbol cons) keyword) (values))
200 note-name-defined))
201 (defun note-name-defined (name kind)
202 #-sb-xc-host (atomic-incf *type-cache-nonce*)
203 ;; We do this BOUNDP check because this function can be called when
204 ;; not in a compilation unit (as when loading top level forms).
205 (when (boundp '*undefined-warnings*)
206 (let ((name (uncross name)))
207 (setq *undefined-warnings*
208 (delete-if (lambda (x)
209 (and (equal (undefined-warning-name x) name)
210 (eq (undefined-warning-kind x) kind)))
211 *undefined-warnings*))))
212 (values))
214 ;;; to be called when a variable is lexically bound
215 (declaim (ftype (function (symbol) (values)) note-lexical-binding))
216 (defun note-lexical-binding (symbol)
217 ;; This check is intended to protect us from getting silently
218 ;; burned when we define
219 ;; foo.lisp:
220 ;; (DEFVAR *FOO* -3)
221 ;; (DEFUN FOO (X) (+ X *FOO*))
222 ;; bar.lisp:
223 ;; (DEFUN BAR (X)
224 ;; (LET ((*FOO* X))
225 ;; (FOO 14)))
226 ;; and then we happen to compile bar.lisp before foo.lisp.
227 (when (looks-like-name-of-special-var-p symbol)
228 ;; FIXME: should be COMPILER-STYLE-WARNING?
229 (style-warn 'asterisks-around-lexical-variable-name
230 :format-control
231 "using the lexical binding of the symbol ~
232 ~/sb-impl::print-symbol-with-prefix/, not the~@
233 dynamic binding"
234 :format-arguments (list symbol)))
235 (values))
237 (def!struct (debug-name-marker (:print-function print-debug-name-marker)
238 (:copier nil)))
240 (defvar *debug-name-level* 4)
241 (defvar *debug-name-length* 12)
242 (defvar *debug-name-punt*)
243 (defvar *debug-name-sharp*)
244 (defvar *debug-name-ellipsis*)
246 (defmethod make-load-form ((marker debug-name-marker) &optional env)
247 (declare (ignore env))
248 (cond ((eq marker *debug-name-sharp*)
249 `(if (boundp '*debug-name-sharp*)
250 *debug-name-sharp*
251 (make-debug-name-marker)))
252 ((eq marker *debug-name-ellipsis*)
253 `(if (boundp '*debug-name-ellipsis*)
254 *debug-name-ellipsis*
255 (make-debug-name-marker)))
257 (warn "Dumping unknown debug-name marker.")
258 '(make-debug-name-marker))))
260 (defun print-debug-name-marker (marker stream level)
261 (declare (ignore level))
262 (cond ((eq marker *debug-name-sharp*)
263 (write-char #\# stream))
264 ((eq marker *debug-name-ellipsis*)
265 (write-string "..." stream))
267 (write-string "???" stream))))
269 (setf *debug-name-sharp* (make-debug-name-marker)
270 *debug-name-ellipsis* (make-debug-name-marker))
272 (declaim (ftype (sfunction () list) name-context))
273 (defun debug-name (type thing &optional context)
274 (let ((*debug-name-punt* nil))
275 (labels ((walk (x)
276 (typecase x
277 (cons
278 (if (plusp *debug-name-level*)
279 (let ((*debug-name-level* (1- *debug-name-level*)))
280 (do ((tail (cdr x) (cdr tail))
281 (name (cons (walk (car x)) nil)
282 (cons (walk (car tail)) name))
283 (n (1- *debug-name-length*) (1- n)))
284 ((or (not (consp tail))
285 (not (plusp n))
286 *debug-name-punt*)
287 (cond (*debug-name-punt*
288 (setf *debug-name-punt* nil)
289 (nreverse name))
290 ((atom tail)
291 (nconc (nreverse name) (walk tail)))
293 (setf *debug-name-punt* t)
294 (nconc (nreverse name) (list *debug-name-ellipsis*)))))))
295 *debug-name-sharp*))
296 ((or symbol number string)
299 (type-of x)))))
300 (let ((name (list* type (walk thing) (when context (name-context)))))
301 (when (legal-fun-name-p name)
302 (bug "~S is a legal function name, and cannot be used as a ~
303 debug name." name))
304 name))))
306 ;;; Set this to NIL to inhibit assembly-level optimization. (For
307 ;;; compiler debugging, rather than policy control.)
308 (defvar *assembly-optimize* t)
310 (in-package "SB!ALIEN")
312 ;;; Information describing a heap-allocated alien.
313 (def!struct (heap-alien-info (:copier nil))
314 ;; The type of this alien.
315 (type (missing-arg) :type alien-type)
316 ;; Its name.
317 (alien-name (missing-arg) :type simple-string)
318 ;; Data or code?
319 (datap (missing-arg) :type boolean))
320 (!set-load-form-method heap-alien-info (:xc :target))