primarily intending to integrate Colin Walter's O(N) map code and
[sbcl.git] / src / compiler / early-c.lisp
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.
6
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.
15
16 (in-package "SB!C")
17
18 (file-comment
19   "$Header$")
20
21 ;;; FIXME: shouldn't SB-C::&MORE be in this list?
22 (defconstant sb!xc:lambda-list-keywords
23   '(&optional &rest &key &aux &body &whole &allow-other-keys &environment)
24   #!+sb-doc
25   "symbols which are magical in a lambda list")
26 \f
27 ;;;; cross-compiler-only versions of CL special variables, so that we
28 ;;;; don't have weird interactions with the host compiler
29
30 (defvar sb!xc:*compile-file-pathname*)
31 (defvar sb!xc:*compile-file-truename*)
32 (defvar sb!xc:*compile-print*)
33 (defvar sb!xc:*compile-verbose*)
34 \f
35 ;;;; miscellaneous types used both in the cross-compiler and on the target
36
37 ;;;; FIXME: The INDEX and LAYOUT-DEPTHOID definitions probably belong
38 ;;;; somewhere else, not "early-c", since they're after all not part
39 ;;;; of the compiler.
40
41 ;;; the type of LAYOUT-DEPTHOID slot values
42 (def!type sb!kernel::layout-depthoid () '(or index (integer -1 -1)))
43
44 ;;; a value for an optimization declaration
45 (def!type sb!c::cookie-quality () '(or (rational 0 3) null))
46 \f
47 ;;; A COOKIE holds information about the compilation environment for a
48 ;;; node. See the LEXENV definition for a description of how it is
49 ;;; used.
50 (def!struct (cookie (:copier nil))
51   (speed   nil :type cookie-quality)
52   (space   nil :type cookie-quality)
53   (safety  nil :type cookie-quality)
54   (cspeed  nil :type cookie-quality)
55   (brevity nil :type cookie-quality)
56   (debug   nil :type cookie-quality))
57
58 ;;; KLUDGE: This needs to be executable in cold init toplevel forms, earlier
59 ;;; than the default copier closure created by DEFSTRUCT toplevel forms would
60 ;;; be available, and earlier than LAYOUT-INFO is initialized (which is a
61 ;;; prerequisite for COPY-STRUCTURE to work), so we define it explicitly using
62 ;;; DEFUN, so that it can be installed by the cold loader, and using
63 ;;; hand-written, hand-maintained slot-by-slot copy it doesn't need to call
64 ;;; COPY-STRUCTURE. -- WHN 19991019
65 (defun copy-cookie (cookie)
66   (make-cookie :speed   (cookie-speed   cookie)
67                :space   (cookie-space   cookie)
68                :safety  (cookie-safety  cookie)
69                :cspeed  (cookie-cspeed  cookie)
70                :brevity (cookie-brevity cookie)
71                :debug   (cookie-debug   cookie)))
72
73 ;;; *DEFAULT-COOKIE* holds the current global compiler policy information.
74 ;;; Whenever the policy is changed, we copy the structure so that old uses will
75 ;;; still get the old values. *DEFAULT-INTERFACE-COOKIE* holds any values
76 ;;; specified by an OPTIMIZE-INTERFACE declaration.
77 ;;;
78 ;;; FIXME: Why isn't COOKIE called POLICY?
79 (declaim (type cookie *default-cookie* *default-interface-cookie*))
80 (defvar *default-cookie*)          ; initialized in cold init
81 (defvar *default-interface-cookie*) ; initialized in cold init
82
83 ;;; possible values for the INLINE-ness of a function.
84 (deftype inlinep ()
85   '(member :inline :maybe-inline :notinline nil))
86 (defconstant inlinep-translations
87   '((inline . :inline)
88     (notinline . :notinline)
89     (maybe-inline . :maybe-inline)))
90
91 ;;; The lexical environment we are currently converting in.
92 (defvar *lexenv*)
93 (declaim (type lexenv *lexenv*))
94
95 ;;; *FREE-VARIABLES* translates from the names of variables referenced
96 ;;; globally to the LEAF structures for them. *FREE-FUNCTIONS* is like
97 ;;; *FREE-VARIABLES*, only it deals with function names.
98 (defvar *free-variables*)
99 (defvar *free-functions*)
100 (declaim (hash-table *free-variables* *free-functions*))
101
102 ;;; We use the same Constant structure to represent all equal anonymous
103 ;;; constants. This hashtable translates from constants to the Leafs that
104 ;;; represent them.
105 (defvar *constants*)
106 (declaim (hash-table *constants*))
107
108 ;;; miscellaneous forward declarations
109 (defvar *code-segment*)
110 #!+sb-dyncount (defvar *collect-dynamic-statistics*)
111 (defvar *component-being-compiled*)
112 (defvar *compiler-error-context*)
113 (defvar *compiler-error-count*)
114 (defvar *compiler-warning-count*)
115 (defvar *compiler-style-warning-count*)
116 (defvar *compiler-note-count*)
117 (defvar *converting-for-interpreter*)
118 (defvar *count-vop-usages*)
119 (defvar *current-path*)
120 (defvar *current-component*)
121 (defvar *default-cookie*)
122 (defvar *default-interface-cookie*)
123 (defvar *dynamic-counts-tn*)
124 (defvar *elsewhere*)
125 (defvar *event-info*)
126 (defvar *event-note-threshold*)
127 (defvar *failure-p*)
128 (defvar *fixups*)
129 (defvar *in-pack*)
130 (defvar *info-environment*)
131 (defvar *lexenv*)
132 (defvar *source-info*)
133 (defvar *trace-table*)
134 (defvar *undefined-warnings*)
135 (defvar *warnings-p*)
136 \f
137 ;;;; miscellaneous utilities
138
139 ;;; Delete any undefined warnings for NAME and KIND. This is for the
140 ;;; benefit of the compiler, but it's sometimes called from stuff like
141 ;;; type-defining code which isn't logically part of the compiler.
142 (declaim (ftype (function ((or symbol cons) keyword) (values))
143                 note-name-defined))
144 (defun note-name-defined (name kind)
145   ;; We do this BOUNDP check because this function can be called when
146   ;; not in a compilation unit (as when loading top-level forms).
147   (when (boundp '*undefined-warnings*)
148     (setq *undefined-warnings*
149           (delete-if (lambda (x)
150                        (and (equal (undefined-warning-name x) name)
151                             (eq (undefined-warning-kind x) kind)))
152                      *undefined-warnings*)))
153   (values))
154
155 ;;; to be called when a variable is lexically bound
156 (declaim (ftype (function (symbol) (values)) note-lexical-binding))
157 (defun note-lexical-binding (symbol)
158   (let ((name (symbol-name symbol)))
159     ;; This check is intended to protect us from getting silently burned when
160     ;; we define
161     ;;   foo.lisp:
162     ;;     (DEFVAR *FOO*)
163     ;;     (DEFUN FOO (X) (1+ X *FOO*))
164     ;;   bar.lisp:
165     ;;     (DEFUN BAR (X)
166     ;;       (LET ((*FOO* X))
167     ;;         (FOO 14)))
168     ;; and then we happen to compile bar.lisp before foo.lisp.
169     (when (and (char= #\* (aref name 0))
170                (char= #\* (aref name (1- (length name)))))
171       (style-warn "using the lexical binding of the symbol ~S, not the~@
172 dynamic binding, even though the symbol name follows the usual naming~@
173 convention (names like *FOO*) for special variables" symbol)))
174   (values))