1 | ;;;-*-Mode: LISP; Package: CCL -*- |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 1994-2001 Digitool, Inc |
---|
4 | ;;; This file is part of OpenMCL. |
---|
5 | ;;; |
---|
6 | ;;; OpenMCL is licensed under the terms of the Lisp Lesser GNU Public |
---|
7 | ;;; License , known as the LLGPL and distributed with OpenMCL as the |
---|
8 | ;;; file "LICENSE". The LLGPL consists of a preamble and the LGPL, |
---|
9 | ;;; which is distributed with OpenMCL as the file "LGPL". Where these |
---|
10 | ;;; conflict, the preamble takes precedence. |
---|
11 | ;;; |
---|
12 | ;;; OpenMCL is referenced in the preamble as the "LIBRARY." |
---|
13 | ;;; |
---|
14 | ;;; The LLGPL is also available online at |
---|
15 | ;;; http://opensource.franz.com/preamble.html |
---|
16 | |
---|
17 | (in-package "CCL") |
---|
18 | |
---|
19 | ;; :lib:nfcomp.lisp - New fasl compiler. |
---|
20 | |
---|
21 | (eval-when (:compile-toplevel :load-toplevel :execute) |
---|
22 | (require 'level-2)) |
---|
23 | |
---|
24 | (require 'optimizers) |
---|
25 | (require 'hash) |
---|
26 | |
---|
27 | (eval-when (:compile-toplevel :execute) |
---|
28 | |
---|
29 | (require 'backquote) |
---|
30 | (require 'defstruct-macros) |
---|
31 | |
---|
32 | (defmacro short-fixnum-p (fixnum) |
---|
33 | `(and (fixnump ,fixnum) (< (integer-length ,fixnum) 16))) |
---|
34 | |
---|
35 | (require "FASLENV" "ccl:xdump;faslenv") |
---|
36 | |
---|
37 | #+ppc32-target |
---|
38 | (require "PPC32-ARCH") |
---|
39 | #+ppc64-target |
---|
40 | (require "PPC64-ARCH") |
---|
41 | #+x8632-target |
---|
42 | (require "X8632-ARCH") |
---|
43 | #+x8664-target |
---|
44 | (require "X8664-ARCH") |
---|
45 | ) ;eval-when (:compile-toplevel :execute) |
---|
46 | |
---|
47 | |
---|
48 | ;File compiler options. Not all of these need to be exported/documented, but |
---|
49 | ;they should be in the product just in case we need them for patches.... |
---|
50 | (defvar *fasl-save-local-symbols* t) |
---|
51 | (defvar *fasl-save-doc-strings* t) |
---|
52 | (defvar *fasl-save-definitions* nil) |
---|
53 | |
---|
54 | (defvar *fasl-deferred-warnings* nil) |
---|
55 | (defvar *fasl-non-style-warnings-signalled-p* nil) |
---|
56 | (defvar *fasl-warnings-signalled-p* nil) |
---|
57 | |
---|
58 | (defvar *compile-verbose* nil ; Might wind up getting called *compile-FILE-verbose* |
---|
59 | "The default for the :VERBOSE argument to COMPILE-FILE.") |
---|
60 | (defvar *compile-file-pathname* nil |
---|
61 | "The defaulted pathname of the file currently being compiled, or NIL if not |
---|
62 | compiling.") ; pathname of src arg to COMPILE-FILE |
---|
63 | (defvar *compile-file-truename* nil |
---|
64 | "The TRUENAME of the file currently being compiled, or NIL if not |
---|
65 | compiling.") ; truename ... |
---|
66 | (defvar *fasl-target* (backend-name *host-backend*)) |
---|
67 | (defvar *fasl-backend* *host-backend*) |
---|
68 | (defvar *fasl-host-big-endian* |
---|
69 | (arch::target-big-endian (backend-target-arch *host-backend*))) |
---|
70 | (defvar *fasl-target-big-endian* *fasl-host-big-endian*) |
---|
71 | (defvar *fcomp-external-format* :default) |
---|
72 | |
---|
73 | (defvar *fasl-break-on-program-errors* #+ccl-0711 nil #-ccl-0711 :defer |
---|
74 | "Controls what happens when the compiler detects PROGRAM-ERROR's during file compilation. |
---|
75 | |
---|
76 | If T, the compiler signals an error immediately when it detects the program-error. |
---|
77 | |
---|
78 | If :DEFER, program errors are reported as compiler warnings, and in addition, an error |
---|
79 | is signalled at the end of file compilation. This allows all warnings for the file |
---|
80 | to be reported, but prevents the creation of a fasl file. |
---|
81 | |
---|
82 | If NIL, program errors are treated the same as any other error condition detected by |
---|
83 | the compiler, i.e. they are reported as compiler warnings and do not cause any |
---|
84 | error to be signalled at compile time.") |
---|
85 | |
---|
86 | |
---|
87 | (defvar *compile-print* nil ; Might wind up getting called *compile-FILE-print* |
---|
88 | "The default for the :PRINT argument to COMPILE-FILE.") |
---|
89 | |
---|
90 | ;Note: errors need to rebind this to NIL if they do any reading without |
---|
91 | ; unwinding the stack! |
---|
92 | (declaim (special *compiling-file*)) ; defined in l1-init. |
---|
93 | |
---|
94 | (defvar *fasl-source-file* nil "Name of file currently being read from. |
---|
95 | Will differ from *compiling-file* during an INCLUDE") |
---|
96 | |
---|
97 | (defparameter *fasl-package-qualified-symbols* '(*loading-file-source-file* set-package %define-package) |
---|
98 | "These symbols are always fasdumped with full package qualification.") |
---|
99 | |
---|
100 | (defun setup-target-features (backend features) |
---|
101 | (if (eq backend *host-backend*) |
---|
102 | features |
---|
103 | (let* ((new nil) |
---|
104 | (nope (backend-target-specific-features *host-backend*))) |
---|
105 | (dolist (f features) |
---|
106 | (unless (memq f nope) (pushnew f new))) |
---|
107 | (dolist (f (backend-target-specific-features backend) |
---|
108 | (progn (pushnew :cross-compiling new) new)) |
---|
109 | (pushnew f new))))) |
---|
110 | |
---|
111 | (defun compile-file-pathname (pathname &rest ignore &key output-file &allow-other-keys) |
---|
112 | "Return a pathname describing what file COMPILE-FILE would write to given |
---|
113 | these arguments." |
---|
114 | (declare (ignore ignore)) |
---|
115 | (setq pathname (merge-pathnames pathname)) |
---|
116 | (merge-pathnames (if output-file |
---|
117 | (merge-pathnames output-file *.fasl-pathname*) |
---|
118 | *.fasl-pathname*) |
---|
119 | pathname)) |
---|
120 | |
---|
121 | (defun compile-file (src &key output-file |
---|
122 | (verbose *compile-verbose*) |
---|
123 | (print *compile-print*) |
---|
124 | load |
---|
125 | features |
---|
126 | (target *fasl-target* target-p) |
---|
127 | (save-local-symbols *fasl-save-local-symbols*) |
---|
128 | (save-doc-strings *fasl-save-doc-strings*) |
---|
129 | (save-definitions *fasl-save-definitions*) |
---|
130 | (save-source-locations *save-source-locations*) |
---|
131 | (external-format :default) |
---|
132 | force |
---|
133 | ;; src may be a temp file with a section of the real source, |
---|
134 | ;; then this is the real source file name. |
---|
135 | compile-file-original-truename |
---|
136 | (compile-file-original-buffer-offset 0) |
---|
137 | (break-on-program-errors (if compile-file-original-truename |
---|
138 | t ;; really SLIME being interactive... |
---|
139 | *fasl-break-on-program-errors*))) |
---|
140 | "Compile SRC, producing a corresponding fasl file and returning its filename." |
---|
141 | (let* ((backend *target-backend*)) |
---|
142 | (when (and target-p (not (setq backend (find-backend target)))) |
---|
143 | (warn "Unknown :TARGET : ~S. Reverting to ~s ..." target *fasl-target*) |
---|
144 | (setq target *fasl-target* backend *target-backend*)) |
---|
145 | (multiple-value-bind (output-file truename warnings-p serious-p) |
---|
146 | (loop |
---|
147 | (restart-case |
---|
148 | (return (%compile-file src output-file verbose print features |
---|
149 | save-local-symbols save-doc-strings save-definitions |
---|
150 | save-source-locations break-on-program-errors |
---|
151 | force backend external-format |
---|
152 | compile-file-original-truename compile-file-original-buffer-offset)) |
---|
153 | (retry-compile-file () |
---|
154 | :report (lambda (stream) (format stream "Retry compiling ~s" src)) |
---|
155 | nil) |
---|
156 | (skip-compile-file () |
---|
157 | :report (lambda (stream) |
---|
158 | (if load |
---|
159 | (format stream "Skip compiling and loading ~s" src) |
---|
160 | (format stream "Skip compiling ~s" src))) |
---|
161 | (return-from compile-file)))) |
---|
162 | (when load (load output-file :verbose (or verbose *load-verbose*))) |
---|
163 | (values truename warnings-p serious-p)))) |
---|
164 | |
---|
165 | |
---|
166 | (defvar *fasl-compile-time-env* nil) |
---|
167 | |
---|
168 | (defun %compile-file (src output-file verbose print features |
---|
169 | save-local-symbols save-doc-strings save-definitions |
---|
170 | save-source-locations break-on-program-errors |
---|
171 | force target-backend external-format |
---|
172 | compile-file-original-truename compile-file-original-buffer-offset) |
---|
173 | (let* ((orig-src (merge-pathnames src)) |
---|
174 | (output-default-type (backend-target-fasl-pathname target-backend)) |
---|
175 | (*fasl-non-style-warnings-signalled-p* nil) |
---|
176 | (*fasl-warnings-signalled-p* nil)) |
---|
177 | (setq src (fcomp-find-file orig-src)) |
---|
178 | (let* ((newtype (pathname-type src))) |
---|
179 | (when (and newtype (not (pathname-type orig-src))) |
---|
180 | (setq orig-src (merge-pathnames orig-src (make-pathname :type newtype :defaults nil))))) |
---|
181 | (setq output-file (merge-pathnames |
---|
182 | (if output-file ; full-pathname in case output-file is relative |
---|
183 | (full-pathname (merge-pathnames output-file output-default-type) :no-error nil) |
---|
184 | output-default-type) |
---|
185 | orig-src)) |
---|
186 | ;; This should not be necessary, but it is. |
---|
187 | (setq output-file (namestring output-file)) |
---|
188 | (when (physical-pathname-p orig-src) ; only back-translate to things likely to exist at load time |
---|
189 | (setq orig-src (back-translate-pathname orig-src '("home" "ccl")))) |
---|
190 | (when (and (not force) |
---|
191 | (probe-file output-file) |
---|
192 | (not (fasl-file-p output-file))) |
---|
193 | (cerror "overwrite it anyway" |
---|
194 | "Compile destination ~S is not a ~A file!" |
---|
195 | output-file (pathname-type |
---|
196 | (backend-target-fasl-pathname |
---|
197 | *target-backend*)))) |
---|
198 | (let* ((*features* (append (if (listp features) features (list features)) (setup-target-features target-backend *features*))) |
---|
199 | (*fasl-deferred-warnings* nil) ; !!! WITH-COMPILATION-UNIT ... |
---|
200 | (*fasl-save-local-symbols* save-local-symbols) |
---|
201 | (*save-source-locations* save-source-locations) |
---|
202 | (*fasl-save-doc-strings* save-doc-strings) |
---|
203 | (*fasl-save-definitions* save-definitions) |
---|
204 | (*fasl-break-on-program-errors* break-on-program-errors) |
---|
205 | (*fcomp-warnings-header* nil) |
---|
206 | (*compile-file-pathname* orig-src) |
---|
207 | (*compile-file-truename* (truename src)) |
---|
208 | (*package* *package*) |
---|
209 | (*readtable* *readtable*) |
---|
210 | (*compile-print* print) |
---|
211 | (*compile-verbose* verbose) |
---|
212 | (*fasl-target* (backend-name target-backend)) |
---|
213 | (*fasl-backend* target-backend) |
---|
214 | (*fasl-target-big-endian* (arch::target-big-endian |
---|
215 | (backend-target-arch target-backend))) |
---|
216 | (*target-ftd* (backend-target-foreign-type-data target-backend)) |
---|
217 | (defenv (new-definition-environment)) |
---|
218 | (lexenv (new-lexical-environment defenv)) |
---|
219 | (*fasl-compile-time-env* (new-lexical-environment (new-definition-environment))) |
---|
220 | (*fcomp-external-format* external-format) |
---|
221 | (forms nil)) |
---|
222 | (let* ((*outstanding-deferred-warnings* (%defer-warnings nil))) |
---|
223 | (rplacd (defenv.type defenv) *outstanding-deferred-warnings*) |
---|
224 | (setf (defenv.defined defenv) (deferred-warnings.defs *outstanding-deferred-warnings*)) |
---|
225 | |
---|
226 | (setq forms (fcomp-file src |
---|
227 | (or compile-file-original-truename orig-src) |
---|
228 | compile-file-original-buffer-offset |
---|
229 | lexenv)) |
---|
230 | |
---|
231 | (setf (deferred-warnings.warnings *outstanding-deferred-warnings*) |
---|
232 | (append *fasl-deferred-warnings* (deferred-warnings.warnings *outstanding-deferred-warnings*))) |
---|
233 | (when *compile-verbose* (fresh-line)) |
---|
234 | (multiple-value-bind (any harsh) (report-deferred-warnings) |
---|
235 | (setq *fasl-warnings-signalled-p* (or *fasl-warnings-signalled-p* any) |
---|
236 | *fasl-non-style-warnings-signalled-p* (if (eq harsh :very) :very |
---|
237 | (or *fasl-non-style-warnings-signalled-p* harsh))))) |
---|
238 | (when (and *fasl-break-on-program-errors* (eq *fasl-non-style-warnings-signalled-p* :very)) |
---|
239 | (cerror "create the output file despite the errors" |
---|
240 | "Serious errors encountered during compilation of ~s" |
---|
241 | src)) |
---|
242 | (fasl-scan-forms-and-dump-file forms output-file lexenv) |
---|
243 | (values output-file |
---|
244 | (truename (pathname output-file)) |
---|
245 | *fasl-warnings-signalled-p* |
---|
246 | (and *fasl-non-style-warnings-signalled-p* t))))) |
---|
247 | |
---|
248 | (defvar *fcomp-locked-hash-tables*) |
---|
249 | (defvar *fcomp-load-forms-environment* nil) |
---|
250 | |
---|
251 | ; This is separated out so that dump-forms-to-file can use it |
---|
252 | (defun fasl-scan-forms-and-dump-file (forms output-file &optional env) |
---|
253 | (let ((*fcomp-locked-hash-tables* nil) |
---|
254 | (*fcomp-load-forms-environment* env)) |
---|
255 | (unwind-protect |
---|
256 | (multiple-value-bind (hash gnames goffsets) (fasl-scan forms) |
---|
257 | (fasl-dump-file gnames goffsets forms hash output-file)) |
---|
258 | (fasl-unlock-hash-tables)))) |
---|
259 | |
---|
260 | #-bccl |
---|
261 | (defun nfcomp (src &optional dest &rest keys) |
---|
262 | (when (keywordp dest) (setq keys (cons dest keys) dest nil)) |
---|
263 | (apply #'compile-file src :output-file dest keys)) |
---|
264 | |
---|
265 | #-bccl |
---|
266 | (%fhave 'fcomp #'nfcomp) |
---|
267 | |
---|
268 | (defparameter *default-file-compilation-policy* (new-compiler-policy)) |
---|
269 | |
---|
270 | (defun current-file-compiler-policy () |
---|
271 | *default-file-compilation-policy*) |
---|
272 | |
---|
273 | (defun set-current-file-compiler-policy (&optional new-policy) |
---|
274 | (setq *default-file-compilation-policy* |
---|
275 | (if new-policy (require-type new-policy 'compiler-policy) (new-compiler-policy)))) |
---|
276 | |
---|
277 | (defparameter *compile-time-evaluation-policy* |
---|
278 | (new-compiler-policy :force-boundp-checks t)) |
---|
279 | |
---|
280 | (defun %compile-time-eval (form env) |
---|
281 | (declare (ignore env)) |
---|
282 | (let* ((*target-backend* *host-backend*) |
---|
283 | (*loading-toplevel-location* (or (fcomp-source-note form) |
---|
284 | *loading-toplevel-location*)) |
---|
285 | (lambda `(lambda () ,form))) |
---|
286 | (fcomp-note-source-transformation form lambda) |
---|
287 | ;; The HANDLER-BIND here is supposed to note WARNINGs that're |
---|
288 | ;; signaled during (eval-when (:compile-toplevel) processing; this |
---|
289 | ;; in turn is supposed to satisfy a pedantic interpretation of the |
---|
290 | ;; spec's requirement that COMPILE-FILE's second and third return |
---|
291 | ;; values reflect (all) conditions "detected by the compiler." |
---|
292 | ;; (It's kind of sad that CL language design is influenced so |
---|
293 | ;; strongly by the views of pedants these days.) |
---|
294 | (handler-bind ((warning (lambda (c) |
---|
295 | (setq *fasl-warnings-signalled-p* t) |
---|
296 | (unless (typep c 'style-warning) |
---|
297 | (setq *fasl-non-style-warnings-signalled-p* t)) |
---|
298 | (signal c)))) |
---|
299 | (funcall (compile-named-function |
---|
300 | lambda |
---|
301 | :source-notes *fcomp-source-note-map* |
---|
302 | :env *fasl-compile-time-env* |
---|
303 | :policy *compile-time-evaluation-policy*))))) |
---|
304 | |
---|
305 | |
---|
306 | ;;; No methods by default, not even for structures. This really sux. |
---|
307 | (defgeneric make-load-form (object &optional environment)) |
---|
308 | |
---|
309 | ;;; Well, no usable methods by default. How this is better than |
---|
310 | ;;; getting a NO-APPLICABLE-METHOD error frankly escapes me, |
---|
311 | (defun no-make-load-form-for (object) |
---|
312 | (error "No ~S method is defined for ~s" 'make-load-form object)) |
---|
313 | |
---|
314 | (defmethod make-load-form ((s standard-object) &optional environment) |
---|
315 | (declare (ignore environment)) |
---|
316 | (no-make-load-form-for s)) |
---|
317 | |
---|
318 | (defmethod make-load-form ((s structure-object) &optional environment) |
---|
319 | (declare (ignore environment)) |
---|
320 | (no-make-load-form-for s)) |
---|
321 | |
---|
322 | (defmethod make-load-form ((c condition) &optional environment) |
---|
323 | (declare (ignore environment)) |
---|
324 | (no-make-load-form-for c)) |
---|
325 | |
---|
326 | (defmethod make-load-form ((c class) &optional environment) |
---|
327 | (let* ((name (class-name c)) |
---|
328 | (found (if name (find-class name nil environment)))) |
---|
329 | (if (eq found c) |
---|
330 | `(find-class ',name) |
---|
331 | (error "Class ~s does not have a proper name." c)))) |
---|
332 | |
---|
333 | |
---|
334 | ;;;; FCOMP-FILE - read & compile file |
---|
335 | ;;;; Produces a list of (opcode . args) to run on loading, intermixed |
---|
336 | ;;;; with read packages. |
---|
337 | |
---|
338 | (defparameter *fasl-eof-forms* nil) |
---|
339 | |
---|
340 | (defparameter cfasl-load-time-eval-sym (make-symbol "LOAD-TIME-EVAL")) |
---|
341 | (%macro-have cfasl-load-time-eval-sym |
---|
342 | #'(lambda (call env) (declare (ignore env)) (list 'eval (list 'quote call)))) |
---|
343 | ;Make it a constant so compiler will barf if try to bind it, e.g. (LET #,foo ...) |
---|
344 | (define-constant cfasl-load-time-eval-sym cfasl-load-time-eval-sym) |
---|
345 | |
---|
346 | |
---|
347 | (defparameter *reading-for-cfasl* nil "Used by the reader for #,") |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | (declaim (special *nx-compile-time-types* |
---|
352 | ;The following are the global proclaimed values. Since compile-file binds |
---|
353 | ;them, this means you can't ever globally proclaim these things from within a |
---|
354 | ;file compile (e.g. from within eval-when compile, or loading a file) - the |
---|
355 | ;proclamations get lost when compile-file exits. This is sort of intentional |
---|
356 | ;(or at least the set of things which fall in this category as opposed to |
---|
357 | ;having a separate compile-time variable is sort of intentional). |
---|
358 | *nx-proclaimed-inline* ; inline and notinline |
---|
359 | *nx-proclaimed-ignore* ; ignore and unignore |
---|
360 | *nx-known-declarations* ; declaration |
---|
361 | *nx-speed* ; optimize speed |
---|
362 | *nx-space* ; optimize space |
---|
363 | *nx-safety* ; optimize safety |
---|
364 | *nx-cspeed*)) ; optimize compiler-speed |
---|
365 | |
---|
366 | (defvar *fcomp-load-time*) |
---|
367 | (defvar *fcomp-inside-eval-always* nil) |
---|
368 | (defvar *fcomp-eval-always-functions* nil) ; used by the LISP package |
---|
369 | (defvar *fcomp-output-list*) |
---|
370 | (defvar *fcomp-toplevel-forms*) |
---|
371 | (defvar *fcomp-source-note-map* nil) |
---|
372 | (defvar *fcomp-loading-toplevel-location*) |
---|
373 | (defvar *fcomp-warnings-header*) |
---|
374 | (defvar *fcomp-stream-position* nil) |
---|
375 | (defvar *fcomp-previous-position* nil) |
---|
376 | (defvar *fcomp-indentation*) |
---|
377 | (defvar *fcomp-print-handler-plist* nil) |
---|
378 | (defvar *fcomp-last-compile-print* |
---|
379 | '(INCLUDE (NIL . T) |
---|
380 | DEFSTRUCT ("Defstruct" . T) |
---|
381 | DEFCONSTANT "Defconstant" |
---|
382 | DEFSETF "Defsetf" |
---|
383 | DEFTYPE "Deftype" |
---|
384 | DEFCLASS "Defclass" |
---|
385 | DEFGENERIC "Defgeneric" |
---|
386 | DEFMETHOD "Defmethod" |
---|
387 | DEFMACRO "Defmacro" |
---|
388 | DEFPARAMETER "Defparameter" |
---|
389 | DEFVAR "Defvar" |
---|
390 | DEFUN "")) |
---|
391 | |
---|
392 | (setf (getf *fcomp-print-handler-plist* 'defun) "" |
---|
393 | (getf *fcomp-print-handler-plist* 'defvar) "Defvar" |
---|
394 | (getf *fcomp-print-handler-plist* 'defparameter) "Defparameter" |
---|
395 | (getf *fcomp-print-handler-plist* 'defmacro) "Defmacro" |
---|
396 | (getf *fcomp-print-handler-plist* 'defmethod) "Defmethod" ; really want more than name (use the function option) |
---|
397 | (getf *fcomp-print-handler-plist* 'defgeneric) "Defgeneric" |
---|
398 | (getf *fcomp-print-handler-plist* 'defclass) "Defclass" |
---|
399 | (getf *fcomp-print-handler-plist* 'deftype) "Deftype" |
---|
400 | (getf *fcomp-print-handler-plist* 'defsetf) "Defsetf" |
---|
401 | (getf *fcomp-print-handler-plist* 'defconstant) "Defconstant" |
---|
402 | (getf *fcomp-print-handler-plist* 'defstruct) '("Defstruct" . t) |
---|
403 | (getf *fcomp-print-handler-plist* 'include) '(nil . t)) |
---|
404 | |
---|
405 | |
---|
406 | (defun fcomp-file (filename orig-file orig-offset env) ; orig-file is back-translated |
---|
407 | (let* ((*package* *package*) |
---|
408 | (*compiling-file* filename) |
---|
409 | (*nx-compile-time-types* *nx-compile-time-types*) |
---|
410 | (*nx-proclaimed-inline* *nx-proclaimed-inline*) |
---|
411 | (*nx-known-declarations* *nx-known-declarations*) |
---|
412 | (*nx-proclaimed-ignore* *nx-proclaimed-ignore*) |
---|
413 | (*nx-speed* *nx-speed*) |
---|
414 | (*nx-space* *nx-space*) |
---|
415 | (*nx-debug* *nx-debug*) |
---|
416 | (*nx-safety* *nx-safety*) |
---|
417 | (*nx-cspeed* *nx-cspeed*) |
---|
418 | (*fcomp-load-time* t) |
---|
419 | (*fcomp-output-list* nil) |
---|
420 | (*fcomp-indentation* 0) |
---|
421 | (*fcomp-last-compile-print* (cons nil (cons nil nil)))) |
---|
422 | (push (list $fasl-platform (backend-target-platform *fasl-backend*)) *fcomp-output-list*) |
---|
423 | (fcomp-read-loop filename orig-file orig-offset env :not-compile-time) |
---|
424 | (nreverse *fcomp-output-list*))) |
---|
425 | |
---|
426 | (defun fcomp-find-file (file &aux path) |
---|
427 | (unless (or (setq path (probe-file file)) |
---|
428 | (setq path (probe-file (merge-pathnames file *.lisp-pathname*)))) |
---|
429 | (error 'file-error :pathname file :error-type "File ~S not found")) |
---|
430 | (namestring path)) |
---|
431 | |
---|
432 | ;;; orig-file is back-translated when from fcomp-file |
---|
433 | ;;; when from fcomp-include it's included filename merged with *compiling-file* |
---|
434 | ;;; which is not back translated |
---|
435 | (defun fcomp-read-loop (filename orig-file orig-offset env processing-mode) |
---|
436 | (when *compile-verbose* |
---|
437 | (format t "~&;~A ~S..." |
---|
438 | (if (eq filename *compiling-file*) "Compiling" " Including") |
---|
439 | filename)) |
---|
440 | (with-open-file (stream filename |
---|
441 | :element-type 'base-char |
---|
442 | :external-format *fcomp-external-format*) |
---|
443 | (let* ((old-file (and (neq filename *compiling-file*) *fasl-source-file*)) |
---|
444 | (*fasl-source-file* filename) |
---|
445 | (*fcomp-toplevel-forms* nil) |
---|
446 | (*fasl-eof-forms* nil) |
---|
447 | (*loading-file-source-file* (namestring orig-file)) |
---|
448 | (*fcomp-source-note-map* (and *save-source-locations* |
---|
449 | (make-hash-table :test #'eq :shared nil))) |
---|
450 | (*loading-toplevel-location* nil) |
---|
451 | (*fcomp-loading-toplevel-location* nil) |
---|
452 | (eofval (cons nil nil)) |
---|
453 | (read-package nil) |
---|
454 | form) |
---|
455 | |
---|
456 | (fcomp-output-form $fasl-src env *loading-file-source-file*) |
---|
457 | (let* ((*fcomp-previous-position* nil)) |
---|
458 | (loop |
---|
459 | (let* ((*fcomp-stream-position* (file-position stream)) |
---|
460 | (*nx-warnings* nil)) ;; catch any warnings from :compile-toplevel forms |
---|
461 | (unless (eq read-package *package*) |
---|
462 | (fcomp-compile-toplevel-forms env) |
---|
463 | (setq read-package *package*)) |
---|
464 | (let ((*reading-for-cfasl* |
---|
465 | (and *fcomp-load-time* cfasl-load-time-eval-sym))) |
---|
466 | (declare (special *reading-for-cfasl*)) |
---|
467 | (let ((pos (file-position stream))) |
---|
468 | (handler-bind |
---|
469 | ((error #'(lambda (c) ; we should distinguish read errors from others? |
---|
470 | (format *error-output* "~&Read error between positions ~a and ~a in ~a." pos (file-position stream) filename) |
---|
471 | (signal c)))) |
---|
472 | (multiple-value-setq (form *loading-toplevel-location*) |
---|
473 | (read-recording-source stream |
---|
474 | :eofval eofval |
---|
475 | :file-name *loading-file-source-file* |
---|
476 | :start-offset orig-offset |
---|
477 | :map *fcomp-source-note-map* |
---|
478 | :save-source-text (neq *save-source-locations* :no-text)))))) |
---|
479 | (when (eq eofval form) |
---|
480 | (require-type *loading-toplevel-location* 'null) |
---|
481 | (return)) |
---|
482 | (fcomp-form form env processing-mode) |
---|
483 | (fcomp-signal-or-defer-warnings *nx-warnings* env) |
---|
484 | (setq *fcomp-previous-position* *fcomp-stream-position*)))) |
---|
485 | (while (setq form *fasl-eof-forms*) |
---|
486 | (setq *fasl-eof-forms* nil) |
---|
487 | (fcomp-form-list form env processing-mode)) |
---|
488 | (when old-file |
---|
489 | (fcomp-output-form $fasl-src env (namestring *compile-file-pathname*))) |
---|
490 | (fcomp-compile-toplevel-forms env)))) |
---|
491 | |
---|
492 | |
---|
493 | |
---|
494 | (defun fcomp-form (form env processing-mode |
---|
495 | &aux print-stuff |
---|
496 | (load-time (and processing-mode (neq processing-mode :compile-time))) |
---|
497 | (compile-time-too (or (eq processing-mode :compile-time) |
---|
498 | (eq processing-mode :compile-time-too)))) |
---|
499 | (let* ((*fcomp-indentation* *fcomp-indentation*) |
---|
500 | (*compile-print* *compile-print*)) |
---|
501 | (when *compile-print* |
---|
502 | (cond ((and (consp form) (setq print-stuff (getf *fcomp-print-handler-plist* (car form)))) |
---|
503 | (rplaca (rplacd (cdr *fcomp-last-compile-print*) nil) nil) |
---|
504 | (rplaca *fcomp-last-compile-print* nil) |
---|
505 | (let ((print-recurse nil)) |
---|
506 | (when (consp print-stuff) |
---|
507 | (setq print-recurse (cdr print-stuff) print-stuff (car print-stuff))) |
---|
508 | (cond ((stringp print-stuff) |
---|
509 | (if (equal print-stuff "") |
---|
510 | (format t "~&~vT~S~%" *fcomp-indentation* (second form)) |
---|
511 | (format t "~&~vT~S [~A]~%" *fcomp-indentation* (second form) print-stuff))) |
---|
512 | ((not (null print-stuff)) |
---|
513 | (format t "~&~vT" *fcomp-indentation*) |
---|
514 | (funcall print-stuff form *standard-output*) |
---|
515 | (terpri *standard-output*))) |
---|
516 | (if print-recurse |
---|
517 | (setq *fcomp-indentation* (+ *fcomp-indentation* 4)) |
---|
518 | (setq *compile-print* nil)))) |
---|
519 | (t (unless (and (eq load-time (car *fcomp-last-compile-print*)) |
---|
520 | (eq compile-time-too (cadr *fcomp-last-compile-print*)) |
---|
521 | (eq *fcomp-indentation* (cddr *fcomp-last-compile-print*))) |
---|
522 | (rplaca *fcomp-last-compile-print* load-time) |
---|
523 | (rplaca (rplacd (cdr *fcomp-last-compile-print*) compile-time-too) *fcomp-indentation*) |
---|
524 | (format t "~&~vTToplevel Forms...~A~%" |
---|
525 | *fcomp-indentation* |
---|
526 | (if load-time |
---|
527 | (if compile-time-too |
---|
528 | " (Compiletime, Loadtime)" |
---|
529 | "") |
---|
530 | (if compile-time-too |
---|
531 | " (Compiletime)" |
---|
532 | ""))))))) |
---|
533 | (fcomp-form-1 form env processing-mode))) |
---|
534 | |
---|
535 | (defun fcomp-form-1 (form env processing-mode &aux sym body) |
---|
536 | (if (consp form) (setq sym (%car form) body (%cdr form))) |
---|
537 | (case sym |
---|
538 | (progn (fcomp-form-list body env processing-mode)) |
---|
539 | (eval-when (fcomp-eval-when form env processing-mode)) |
---|
540 | (compiler-let (fcomp-compiler-let form env processing-mode)) |
---|
541 | (locally (fcomp-locally form env processing-mode)) |
---|
542 | (macrolet (fcomp-macrolet form env processing-mode)) |
---|
543 | (symbol-macrolet (fcomp-symbol-macrolet form env processing-mode)) |
---|
544 | ((%include include) (fcomp-include form env processing-mode)) |
---|
545 | (t |
---|
546 | ;;Need to macroexpand to see if get more progn's/eval-when's and so should |
---|
547 | ;;stay at toplevel. But don't expand if either the evaluator or the |
---|
548 | ;;compiler might not - better safe than sorry... |
---|
549 | ;; Good advice, but the hard part is knowing which is which. |
---|
550 | (cond |
---|
551 | ((and (non-nil-symbol-p sym) |
---|
552 | (macro-function sym env) |
---|
553 | (not (compiler-macro-function sym env)) |
---|
554 | (not (eq sym '%defvar-init)) ; a macro that we want to special-case |
---|
555 | (multiple-value-bind (new win) (fcomp-macroexpand-1 form env) |
---|
556 | (if win (setq form new)) |
---|
557 | win)) |
---|
558 | (fcomp-form form env processing-mode)) |
---|
559 | ((and (not *fcomp-inside-eval-always*) |
---|
560 | (memq sym *fcomp-eval-always-functions*)) |
---|
561 | (let* ((*fcomp-inside-eval-always* t) |
---|
562 | (new `(eval-when (:execute :compile-toplevel :load-toplevel) ,form))) |
---|
563 | (fcomp-form-1 new env processing-mode))) |
---|
564 | (t |
---|
565 | (when (or (eq processing-mode :compile-time) (eq processing-mode :compile-time-too)) |
---|
566 | (%compile-time-eval form env)) |
---|
567 | (when (and processing-mode (neq processing-mode :compile-time)) |
---|
568 | (case sym |
---|
569 | ((%defconstant) (fcomp-load-%defconstant form env)) |
---|
570 | ((%defparameter) (fcomp-load-%defparameter form env)) |
---|
571 | ((%defvar %defvar-init) (fcomp-load-defvar form env)) |
---|
572 | ((%defun) (fcomp-load-%defun form env)) |
---|
573 | ((set-package %define-package) |
---|
574 | (fcomp-random-toplevel-form form env) |
---|
575 | (fcomp-compile-toplevel-forms env)) |
---|
576 | ((%macro) (fcomp-load-%macro form env)) |
---|
577 | ;; ((%deftype) (fcomp-load-%deftype form)) |
---|
578 | ;; ((define-setf-method) (fcomp-load-define-setf-method form)) |
---|
579 | (t (fcomp-random-toplevel-form form env))))))))) |
---|
580 | |
---|
581 | (defun fcomp-form-list (forms env processing-mode) |
---|
582 | (let* ((outer *loading-toplevel-location*)) |
---|
583 | (dolist (form forms) |
---|
584 | (setq *loading-toplevel-location* (or (fcomp-source-note form) outer)) |
---|
585 | (fcomp-form form env processing-mode)) |
---|
586 | (setq *loading-toplevel-location* outer))) |
---|
587 | |
---|
588 | (defun fcomp-compiler-let (form env processing-mode &aux vars varinits (body (%cdr form))) |
---|
589 | (fcomp-compile-toplevel-forms env) |
---|
590 | (dolist (pair (car body)) |
---|
591 | (push (nx-pair-name pair) vars) |
---|
592 | (push (%compile-time-eval (nx-pair-initform pair) env) varinits)) |
---|
593 | (progv (nreverse vars) (nreverse varinits) |
---|
594 | (fcomp-form-list (cdr body) env processing-mode) |
---|
595 | (fcomp-compile-toplevel-forms env))) |
---|
596 | |
---|
597 | (defun fcomp-locally (form env processing-mode &aux (body (%cdr form))) |
---|
598 | (fcomp-compile-toplevel-forms env) |
---|
599 | (multiple-value-bind (body decls) (parse-body body env) |
---|
600 | (let* ((env (augment-environment env :declare (decl-specs-from-declarations decls)))) |
---|
601 | (fcomp-form-list body env processing-mode) |
---|
602 | (fcomp-compile-toplevel-forms env)))) |
---|
603 | |
---|
604 | (defun fcomp-macrolet (form env processing-mode &aux (body (%cdr form))) |
---|
605 | (fcomp-compile-toplevel-forms env) |
---|
606 | (let ((outer-env (augment-environment env |
---|
607 | :macro |
---|
608 | (mapcar #'(lambda (m) |
---|
609 | (destructuring-bind (name arglist &body body) m |
---|
610 | (list name (enclose (parse-macro name arglist body env) |
---|
611 | env)))) |
---|
612 | (car body))))) |
---|
613 | (multiple-value-bind (body decls) (parse-body (cdr body) outer-env) |
---|
614 | (let* ((env (augment-environment |
---|
615 | outer-env |
---|
616 | :declare (decl-specs-from-declarations decls)))) |
---|
617 | (fcomp-form-list body env processing-mode) |
---|
618 | (fcomp-compile-toplevel-forms env))))) |
---|
619 | |
---|
620 | (defun fcomp-symbol-macrolet (form env processing-mode &aux (body (%cdr form))) |
---|
621 | (fcomp-compile-toplevel-forms env) |
---|
622 | (let* ((outer-env (augment-environment env :symbol-macro (car body)))) |
---|
623 | (multiple-value-bind (body decls) (parse-body (cdr body) env) |
---|
624 | (let* ((env (augment-environment outer-env |
---|
625 | :declare (decl-specs-from-declarations decls)))) |
---|
626 | (fcomp-form-list body env processing-mode) |
---|
627 | (fcomp-compile-toplevel-forms env))))) |
---|
628 | |
---|
629 | (defun fcomp-eval-when (form env processing-mode &aux (body (%cdr form)) (eval-times (pop body))) |
---|
630 | (let* ((compile-time-too (eq processing-mode :compile-time-too)) |
---|
631 | (compile-time-only (eq processing-mode :compile-time)) |
---|
632 | (at-compile-time nil) |
---|
633 | (at-load-time nil) |
---|
634 | (at-eval-time nil)) |
---|
635 | (dolist (when eval-times) |
---|
636 | (if (or (eq when 'compile) (eq when :compile-toplevel)) |
---|
637 | (setq at-compile-time t) |
---|
638 | (if (or (eq when 'eval) (eq when :execute)) |
---|
639 | (setq at-eval-time t) |
---|
640 | (if (or (eq when 'load) (eq when :load-toplevel)) |
---|
641 | (setq at-load-time t) |
---|
642 | (warn "Unknown EVAL-WHEN time ~s in ~S while compiling ~S." |
---|
643 | when eval-times *fasl-source-file*))))) |
---|
644 | (fcomp-compile-toplevel-forms env) ; always flush the suckers |
---|
645 | (cond (compile-time-only |
---|
646 | (if at-eval-time (fcomp-form-list body env :compile-time))) |
---|
647 | (at-load-time |
---|
648 | (fcomp-form-list body env (if (or at-compile-time (and at-eval-time compile-time-too)) |
---|
649 | :compile-time-too |
---|
650 | :not-compile-time))) |
---|
651 | ((or at-compile-time (and at-eval-time compile-time-too)) |
---|
652 | (fcomp-form-list body env :compile-time)))) |
---|
653 | (fcomp-compile-toplevel-forms env)) |
---|
654 | |
---|
655 | (defun fcomp-include (form env processing-mode &aux file) |
---|
656 | (fcomp-compile-toplevel-forms env) |
---|
657 | (verify-arg-count form 1 1) |
---|
658 | (setq file (nx-transform (%cadr form) env)) |
---|
659 | (unless (constantp file) (report-bad-arg file '(or string pathname))) |
---|
660 | (let ((actual (merge-pathnames (eval-constant file) |
---|
661 | (directory-namestring *compiling-file*)))) |
---|
662 | (when *compile-print* (format t "~&~vTIncluding file ~A~%" *fcomp-indentation* actual)) |
---|
663 | (let ((*fcomp-indentation* (+ 4 *fcomp-indentation*)) |
---|
664 | (*package* *package*)) |
---|
665 | (fcomp-read-loop (fcomp-find-file actual) actual 0 env processing-mode) |
---|
666 | (fcomp-output-form $fasl-src env *loading-file-source-file*)) |
---|
667 | (when *compile-print* (format t "~&~vTFinished included file ~A~%" *fcomp-indentation* actual)))) |
---|
668 | |
---|
669 | (defun define-compile-time-constant (symbol initform env) |
---|
670 | (note-variable-info symbol t env) |
---|
671 | (let ((compile-time-defenv (definition-environment *fasl-compile-time-env*)) |
---|
672 | (definition-env (definition-environment env))) |
---|
673 | (when (or compile-time-defenv definition-env) |
---|
674 | (multiple-value-bind (value error) |
---|
675 | (ignore-errors (values (%compile-time-eval initform env) nil)) |
---|
676 | (when error |
---|
677 | (warn "Compile-time evaluation of DEFCONSTANT initial value form for ~S while ~ |
---|
678 | compiling ~S signalled the error: ~&~A" symbol *fasl-source-file* error)) |
---|
679 | (let ((cell (cons symbol (if error (%unbound-marker-8) value)))) |
---|
680 | (when definition-env |
---|
681 | (push cell (defenv.constants definition-env))) |
---|
682 | (when compile-time-defenv |
---|
683 | (push cell (defenv.constants compile-time-defenv)))))) |
---|
684 | symbol)) |
---|
685 | |
---|
686 | (defun fcomp-load-%defconstant (form env) |
---|
687 | (destructuring-bind (sym valform &optional doc) (cdr form) |
---|
688 | (unless *fasl-save-doc-strings* |
---|
689 | (setq doc nil)) |
---|
690 | (if (quoted-form-p sym) |
---|
691 | (setq sym (%cadr sym))) |
---|
692 | (if (and (typep sym 'symbol) (or (quoted-form-p valform) (self-evaluating-p valform))) |
---|
693 | (fcomp-output-form $fasl-defconstant env sym (eval-constant valform) (eval-constant doc)) |
---|
694 | (fcomp-random-toplevel-form form env)))) |
---|
695 | |
---|
696 | (defun fcomp-load-%defparameter (form env) |
---|
697 | (destructuring-bind (sym valform &optional doc) (cdr form) |
---|
698 | (unless *fasl-save-doc-strings* |
---|
699 | (setq doc nil)) |
---|
700 | (if (quoted-form-p sym) |
---|
701 | (setq sym (%cadr sym))) |
---|
702 | (let* ((sym-p (typep sym 'symbol)) |
---|
703 | (fn (and sym-p (fcomp-function-arg valform env)))) |
---|
704 | (if (and sym-p (or fn (constantp valform))) |
---|
705 | (fcomp-output-form $fasl-defparameter env sym (or fn (eval-constant valform)) (eval-constant doc)) |
---|
706 | (fcomp-random-toplevel-form form env))))) |
---|
707 | |
---|
708 | ; Both the simple %DEFVAR and the initial-value case (%DEFVAR-INIT) come here. |
---|
709 | ; Only try to dump this as a special fasl operator if the initform is missing |
---|
710 | ; or is "harmless" to evaluate whether needed or not (constant or function.) |
---|
711 | ; Hairier initforms could be handled by another fasl operator that takes a thunk |
---|
712 | ; and conditionally calls it. |
---|
713 | (defun fcomp-load-defvar (form env) |
---|
714 | (destructuring-bind (sym &optional (valform nil val-p) doc) (cdr form) |
---|
715 | (unless *fasl-save-doc-strings* |
---|
716 | (setq doc nil)) |
---|
717 | (if (quoted-form-p sym) ; %defvar quotes its arg, %defvar-init doesn't. |
---|
718 | (setq sym (%cadr sym))) |
---|
719 | (let* ((sym-p (typep sym 'symbol))) |
---|
720 | (if (and sym-p (not val-p)) |
---|
721 | (fcomp-output-form $fasl-defvar env sym) |
---|
722 | (let* ((fn (if sym-p (fcomp-function-arg valform env)))) |
---|
723 | (if (and sym-p (or fn (constantp valform))) |
---|
724 | (fcomp-output-form $fasl-defvar-init env sym (or fn (eval-constant valform)) (eval-constant doc)) |
---|
725 | (fcomp-random-toplevel-form form env))))))) |
---|
726 | |
---|
727 | (defun define-compile-time-macro (name lambda-expression env) |
---|
728 | (let ((compile-time-defenv (definition-environment *fasl-compile-time-env*)) |
---|
729 | (definition-env (definition-environment env))) |
---|
730 | (when (or definition-env compile-time-defenv) |
---|
731 | (let ((cell (list* name |
---|
732 | 'macro |
---|
733 | (compile-named-function lambda-expression :name name :env env)))) |
---|
734 | (when compile-time-defenv |
---|
735 | (push cell (defenv.functions compile-time-defenv))) |
---|
736 | (when definition-env |
---|
737 | (push cell (defenv.functions definition-env)))) |
---|
738 | (record-function-info name (%cons-def-info 'defmacro) env)) |
---|
739 | name)) |
---|
740 | |
---|
741 | (defun define-compile-time-symbol-macro (name expansion env) |
---|
742 | (let ((compile-time-defenv (definition-environment *fasl-compile-time-env*)) |
---|
743 | (definition-env (definition-environment env))) |
---|
744 | (when (or definition-env compile-time-defenv) |
---|
745 | (let ((cell (cons name expansion))) |
---|
746 | (when compile-time-defenv |
---|
747 | (push cell (defenv.functions compile-time-defenv))) |
---|
748 | (when definition-env |
---|
749 | (push cell (defenv.functions definition-env))))) |
---|
750 | name)) |
---|
751 | |
---|
752 | |
---|
753 | (defun fcomp-proclaim-type (type syms) |
---|
754 | (dolist (sym syms) |
---|
755 | (if (symbolp sym) |
---|
756 | (push (cons sym type) *nx-compile-time-types*) |
---|
757 | (warn "~S isn't a symbol in ~S type declaration while compiling ~S." |
---|
758 | sym type *fasl-source-file*)))) |
---|
759 | |
---|
760 | (defun compile-time-proclamation (specs env &aux sym (defenv (definition-environment env))) |
---|
761 | (when defenv |
---|
762 | (dolist (spec specs) |
---|
763 | (setq sym (pop spec)) |
---|
764 | (case sym |
---|
765 | (type |
---|
766 | (fcomp-proclaim-type (car spec) (cdr spec))) |
---|
767 | (special |
---|
768 | (dolist (sym spec) |
---|
769 | (push (cons (require-type sym 'symbol) nil) (defenv.specials defenv)))) |
---|
770 | (notspecial |
---|
771 | (let ((specials (defenv.specials defenv))) |
---|
772 | (dolist (sym spec (setf (defenv.specials defenv) specials)) |
---|
773 | (let ((pair (assq sym specials))) |
---|
774 | (when pair (setq specials (nremove pair specials))))))) |
---|
775 | (optimize |
---|
776 | (%proclaim-optimize spec)) |
---|
777 | (inline |
---|
778 | (dolist (sym spec) |
---|
779 | (push (cons (maybe-setf-function-name sym) (cons 'inline 'inline)) (lexenv.fdecls defenv)))) |
---|
780 | (notinline |
---|
781 | (dolist (sym spec) |
---|
782 | (unless (compiler-special-form-p sym) |
---|
783 | (push (cons (maybe-setf-function-name sym) (cons 'inline 'notinline)) (lexenv.fdecls defenv))))) |
---|
784 | (declaration |
---|
785 | (dolist (sym spec) |
---|
786 | (pushnew (require-type sym 'symbol) *nx-known-declarations*))) |
---|
787 | (ignore |
---|
788 | (dolist (sym spec) |
---|
789 | (push (cons (require-type sym 'symbol) t) *nx-proclaimed-ignore*))) |
---|
790 | (unignore |
---|
791 | (dolist (sym spec) |
---|
792 | (push (cons (require-type sym 'symbol) nil) *nx-proclaimed-ignore*))) |
---|
793 | (ftype |
---|
794 | (let ((ftype (car spec)) |
---|
795 | (fnames (cdr spec))) |
---|
796 | ;; ----- this part may be redundant, now that the lexenv.fdecls part is being done |
---|
797 | (if (and (consp ftype) |
---|
798 | (consp fnames) |
---|
799 | (eq (%car ftype) 'function)) |
---|
800 | (dolist (fname fnames) |
---|
801 | (note-function-info fname nil env))) |
---|
802 | (dolist (fname fnames) |
---|
803 | (push (list* (maybe-setf-function-name fname) sym ftype) (lexenv.fdecls defenv))))) |
---|
804 | (otherwise |
---|
805 | (if (memq (if (consp sym) (%car sym) sym) *cl-types*) |
---|
806 | (fcomp-proclaim-type sym spec) ; A post-cltl2 cleanup issue changes this |
---|
807 | nil) ; ---- probably ought to complain |
---|
808 | ))))) |
---|
809 | |
---|
810 | (defun fcomp-load-%defun (form env) |
---|
811 | (destructuring-bind (fn &optional doc) (cdr form) |
---|
812 | (unless *fasl-save-doc-strings* |
---|
813 | (if (consp doc) |
---|
814 | (if (and (eq (car doc) 'quote) (consp (cadr doc))) |
---|
815 | (setf (car (cadr doc)) nil)) |
---|
816 | (setq doc nil))) |
---|
817 | (when (and (consp fn) (eq (%car fn) 'nfunction)) |
---|
818 | (note-function-info (cadr fn) (caddr fn) env)) |
---|
819 | (if (and (constantp doc) |
---|
820 | (setq fn (fcomp-function-arg fn env))) |
---|
821 | (progn |
---|
822 | (setq doc (eval-constant doc)) |
---|
823 | (fcomp-output-form $fasl-defun env fn doc)) |
---|
824 | (fcomp-random-toplevel-form form env)))) |
---|
825 | |
---|
826 | (defun fcomp-load-%macro (form env &aux fn doc) |
---|
827 | (verify-arg-count form 1 2) |
---|
828 | (if (and (constantp (setq doc (caddr form))) |
---|
829 | (setq fn (fcomp-function-arg (cadr form) env))) |
---|
830 | (progn |
---|
831 | (setq doc (eval-constant doc)) |
---|
832 | (fcomp-output-form $fasl-macro env fn doc)) |
---|
833 | (fcomp-random-toplevel-form form env))) |
---|
834 | |
---|
835 | (defun define-compile-time-structure (sd refnames predicate env) |
---|
836 | (let ((defenv (definition-environment env))) |
---|
837 | (when defenv |
---|
838 | (when (non-nil-symbolp (sd-name sd)) |
---|
839 | (push (make-instance 'compile-time-class :name (sd-name sd)) |
---|
840 | (defenv.classes defenv))) |
---|
841 | (setf (defenv.structures defenv) (alist-adjoin (sd-name sd) sd (defenv.structures defenv))) |
---|
842 | (let* ((structrefs (defenv.structrefs defenv))) |
---|
843 | (when (and (null (sd-type sd)) |
---|
844 | predicate) |
---|
845 | (setq structrefs (alist-adjoin predicate (sd-name sd) structrefs))) |
---|
846 | (dolist (slot (sd-slots sd)) |
---|
847 | (unless (fixnump (ssd-name slot)) |
---|
848 | (setq structrefs |
---|
849 | (alist-adjoin (if refnames (pop refnames) (ssd-name slot)) |
---|
850 | (ssd-type-and-refinfo slot) |
---|
851 | structrefs)))) |
---|
852 | (setf (defenv.structrefs defenv) structrefs))))) |
---|
853 | |
---|
854 | (defun fcomp-source-note (form &aux (notes *fcomp-source-note-map*)) |
---|
855 | (and notes (gethash form notes))) |
---|
856 | |
---|
857 | (defun fcomp-note-source-transformation (original new) |
---|
858 | (let* ((*nx-source-note-map* *fcomp-source-note-map*)) |
---|
859 | (nx-note-source-transformation original new))) |
---|
860 | |
---|
861 | (defun fcomp-macroexpand-1 (form env) |
---|
862 | (let* ((*nx-source-note-map* *fcomp-source-note-map*)) |
---|
863 | (multiple-value-bind (new win) |
---|
864 | (macroexpand-1 form env) |
---|
865 | (when win |
---|
866 | (nx-note-source-transformation form new)) |
---|
867 | (values new win)))) |
---|
868 | |
---|
869 | (defun fcomp-transform (form env) |
---|
870 | (let* ((*nx-source-note-map* *fcomp-source-note-map*)) |
---|
871 | (nx-transform form env))) |
---|
872 | |
---|
873 | |
---|
874 | (defun fcomp-random-toplevel-form (form env) |
---|
875 | (unless (constantp form) |
---|
876 | (unless (or (atom form) |
---|
877 | (compiler-special-form-p (%car form))) |
---|
878 | ;;Pre-compile any lfun args. This is an efficiency hack, since compiler |
---|
879 | ;;reentering itself for inner lambdas tends to be more expensive than |
---|
880 | ;;top-level compiles. |
---|
881 | ;;This assumes the form has been macroexpanded, or at least none of the |
---|
882 | ;;non-evaluated macro arguments could look like functions. |
---|
883 | (let ((new-form (make-list (length form)))) |
---|
884 | (declare (dynamic-extent new-form)) |
---|
885 | (loop for arg in (%cdr form) for newptr on (%cdr new-form) |
---|
886 | do (setf (%car newptr) |
---|
887 | (multiple-value-bind (new win) (fcomp-transform arg env) |
---|
888 | (let ((lfun (fcomp-function-arg new env))) |
---|
889 | (when lfun |
---|
890 | (setq new `',lfun win t) |
---|
891 | (fcomp-note-source-transformation arg new))) |
---|
892 | (if win new arg)))) |
---|
893 | (unless (every #'eq (%cdr form) (%cdr new-form)) |
---|
894 | (setf (%car new-form) (%car form)) |
---|
895 | (fcomp-note-source-transformation form (setq form (copy-list new-form)))))) |
---|
896 | (fcomp-ensure-source env) |
---|
897 | (push form *fcomp-toplevel-forms*))) |
---|
898 | |
---|
899 | (defun fcomp-function-arg (expr env) |
---|
900 | (when (consp expr) |
---|
901 | (multiple-value-bind (lambda-expr name win) |
---|
902 | (cond ((and (eq (%car expr) 'nfunction) |
---|
903 | (lambda-expression-p (cadr (%cdr expr)))) |
---|
904 | (values (%caddr expr) (%cadr expr) t)) |
---|
905 | ((and (eq (%car expr) 'function) |
---|
906 | (lambda-expression-p (car (%cdr expr)))) |
---|
907 | (values (%cadr expr) nil t))) |
---|
908 | (when win |
---|
909 | (fcomp-named-function lambda-expr name env |
---|
910 | (or (fcomp-source-note expr) |
---|
911 | (fcomp-source-note lambda-expr) |
---|
912 | *loading-toplevel-location*)))))) |
---|
913 | |
---|
914 | (defun fcomp-compile-toplevel-forms (env) |
---|
915 | (when *fcomp-toplevel-forms* |
---|
916 | (let* ((forms (nreverse *fcomp-toplevel-forms*)) |
---|
917 | (*fcomp-stream-position* *fcomp-previous-position*) |
---|
918 | (lambda (if T ;; (null (cdr forms)) |
---|
919 | `(lambda () ,@forms) |
---|
920 | `(lambda () |
---|
921 | (macrolet ((load-time-value (value) |
---|
922 | (declare (ignore value)) |
---|
923 | (compiler-function-overflow))) |
---|
924 | ,@forms))))) |
---|
925 | (setq *fcomp-toplevel-forms* nil) |
---|
926 | ;(format t "~& Random toplevel form: ~s" lambda) |
---|
927 | (handler-case (fcomp-output-form |
---|
928 | $fasl-lfuncall |
---|
929 | env |
---|
930 | (fcomp-named-function lambda nil env)) |
---|
931 | (compiler-function-overflow () |
---|
932 | (if (null (cdr forms)) |
---|
933 | (error "Form ~s cannot be compiled - size exceeds compiler limitation" |
---|
934 | (%car forms)) |
---|
935 | ; else compile each half : |
---|
936 | (progn |
---|
937 | (dotimes (i (floor (length forms) 2)) |
---|
938 | (declare (fixnum i)) |
---|
939 | (push (pop forms) *fcomp-toplevel-forms*)) |
---|
940 | (fcomp-compile-toplevel-forms env) |
---|
941 | (setq *fcomp-toplevel-forms* (nreverse forms)) |
---|
942 | (fcomp-compile-toplevel-forms env)))))))) |
---|
943 | |
---|
944 | (defun fcomp-ensure-source (env) |
---|
945 | ;; if source location saving is off, both values are NIL, so this will do nothing, |
---|
946 | ;; don't need to check explicitly. |
---|
947 | (unless (eq *fcomp-loading-toplevel-location* *loading-toplevel-location*) |
---|
948 | (setq *fcomp-loading-toplevel-location* *loading-toplevel-location*) |
---|
949 | (fcomp-output-form $fasl-toplevel-location env *loading-toplevel-location*))) |
---|
950 | |
---|
951 | (defun fcomp-output-form (opcode env &rest args) |
---|
952 | (fcomp-ensure-source env) |
---|
953 | (when *fcomp-toplevel-forms* (fcomp-compile-toplevel-forms env)) |
---|
954 | (push (cons opcode args) *fcomp-output-list*)) |
---|
955 | |
---|
956 | |
---|
957 | ;;; Compile a lambda expression for the sole purpose of putting it in a fasl |
---|
958 | ;;; file. The result will not be funcalled. This really shouldn't bother |
---|
959 | ;;; making an lfun, but it's simpler this way... |
---|
960 | (defun fcomp-named-function (def name env &optional source-note) |
---|
961 | (let* ((env (new-lexical-environment env)) |
---|
962 | (*nx-break-on-program-errors* (not (memq *fasl-break-on-program-errors* '(nil :defer))))) |
---|
963 | (multiple-value-bind (lfun warnings) |
---|
964 | (compile-named-function def |
---|
965 | :name name |
---|
966 | :env env |
---|
967 | :function-note source-note |
---|
968 | :keep-lambda *fasl-save-definitions* |
---|
969 | :keep-symbols *fasl-save-local-symbols* |
---|
970 | :policy *default-file-compilation-policy* |
---|
971 | :source-notes *fcomp-source-note-map* |
---|
972 | :load-time-eval-token cfasl-load-time-eval-sym |
---|
973 | :target *fasl-target*) |
---|
974 | (fcomp-signal-or-defer-warnings warnings env) |
---|
975 | lfun))) |
---|
976 | |
---|
977 | ; For now, defer only UNDEFINED-FUNCTION-REFERENCEs, signal all others via WARN. |
---|
978 | ; Well, maybe not WARN, exactly. |
---|
979 | (defun fcomp-signal-or-defer-warnings (warnings env) |
---|
980 | (let ((init (null *fcomp-warnings-header*)) |
---|
981 | (some *fasl-warnings-signalled-p*) |
---|
982 | (harsh *fasl-non-style-warnings-signalled-p*)) |
---|
983 | (dolist (w warnings) |
---|
984 | (setf (compiler-warning-file-name w) *fasl-source-file*) |
---|
985 | (setf (compiler-warning-stream-position w) *fcomp-stream-position*) |
---|
986 | (if (and (typep w 'undefined-function-reference) |
---|
987 | (eq w (setq w (macro-too-late-p w env)))) |
---|
988 | (push w *fasl-deferred-warnings*) |
---|
989 | (progn |
---|
990 | (multiple-value-setq (harsh some *fcomp-warnings-header*) |
---|
991 | (signal-compiler-warning w init *fcomp-warnings-header* harsh some)) |
---|
992 | (setq init nil)))) |
---|
993 | (setq *fasl-warnings-signalled-p* some |
---|
994 | *fasl-non-style-warnings-signalled-p* harsh))) |
---|
995 | |
---|
996 | ; If W is an UNDEFINED-FUNCTION-REFERENCE which refers to a macro (either at compile-time in ENV |
---|
997 | ; or globally), cons up a MACRO-USED-BEFORE-DEFINITION warning and return it; else return W. |
---|
998 | |
---|
999 | (defun macro-too-late-p (w env) |
---|
1000 | (let* ((args (compiler-warning-args w)) |
---|
1001 | (name (car args))) |
---|
1002 | (if (or (macro-function name) |
---|
1003 | (let* ((defenv (definition-environment env)) |
---|
1004 | (info (if defenv (assq name (defenv.functions defenv))))) |
---|
1005 | (and (consp (cdr info)) |
---|
1006 | (eq 'macro (cadr info))))) |
---|
1007 | (make-instance 'macro-used-before-definition |
---|
1008 | :file-name (compiler-warning-file-name w) |
---|
1009 | :function-name (compiler-warning-function-name w) |
---|
1010 | :warning-type ':macro-used-before-definition |
---|
1011 | :args args) |
---|
1012 | w))) |
---|
1013 | |
---|
1014 | |
---|
1015 | |
---|
1016 | ;;;; fasl-scan - dumping reference counting |
---|
1017 | ;;;; |
---|
1018 | ;;;; |
---|
1019 | ;These should be constants, but it's too much trouble when need to change 'em. |
---|
1020 | (defparameter FASL-FILE-ID #xFF00) ;Overall file format, shouldn't change much |
---|
1021 | (defparameter FASL-VERSION #xFF54) ;Fasl block format. ($fasl-vers) |
---|
1022 | |
---|
1023 | (defvar *fasdump-hash*) |
---|
1024 | (defvar *fasdump-read-package*) |
---|
1025 | (defvar *fasdump-global-offsets*) |
---|
1026 | (defvar *make-load-form-hash*) |
---|
1027 | |
---|
1028 | ;;;Return a hash table containing subexp's which are referenced more than once. |
---|
1029 | (defun fasl-scan (forms) |
---|
1030 | (let* ((*fasdump-hash* (make-hash-table :size (length forms) ; Crude estimate |
---|
1031 | :rehash-threshold 0.9 |
---|
1032 | :test 'eq |
---|
1033 | :shared nil)) |
---|
1034 | (*make-load-form-hash* (make-hash-table :test 'eq :shared nil)) |
---|
1035 | (*fasdump-read-package* nil) |
---|
1036 | (*fasdump-global-offsets* nil) |
---|
1037 | (gsymbols nil)) |
---|
1038 | (dolist (op forms) |
---|
1039 | (if (packagep op) ; old magic treatment of *package* |
---|
1040 | (setq *fasdump-read-package* op) |
---|
1041 | (dolist (arg (cdr op)) (fasl-scan-form arg)))) |
---|
1042 | |
---|
1043 | #-bccl (when (eq *compile-verbose* :debug) |
---|
1044 | (format t "~&~S forms, ~S entries -> " |
---|
1045 | (length forms) |
---|
1046 | (hash-table-count *fasdump-hash*))) |
---|
1047 | (maphash #'(lambda (key val) |
---|
1048 | (when (%izerop val) (remhash key *fasdump-hash*))) |
---|
1049 | *fasdump-hash*) |
---|
1050 | #-bccl (when (eq *compile-verbose* :debug) |
---|
1051 | (format t "~S." (hash-table-count *fasdump-hash*))) |
---|
1052 | (values *fasdump-hash* |
---|
1053 | gsymbols |
---|
1054 | *fasdump-global-offsets*))) |
---|
1055 | |
---|
1056 | ;;; During scanning, *fasdump-hash* values are one of the following: |
---|
1057 | ;;; nil - form hasn't been referenced yet. |
---|
1058 | ;;; 0 - form has been referenced exactly once |
---|
1059 | ;;; T - form has been referenced more than once |
---|
1060 | ;;; (load-form scanning-p referenced-p initform) |
---|
1061 | ;;; form should be replaced by load-form |
---|
1062 | ;;; scanning-p is true while we're scanning load-form |
---|
1063 | ;;; referenced-p is nil if unreferenced, |
---|
1064 | ;;; T if referenced but not dumped yet, |
---|
1065 | ;;; 0 if dumped already (fasl-dump-form uses this) |
---|
1066 | ;;; initform is a compiled version of the user's initform |
---|
1067 | (defun fasl-scan-form (form) |
---|
1068 | (when form |
---|
1069 | (let ((info (gethash form *fasdump-hash*))) |
---|
1070 | (cond ((null info) |
---|
1071 | (fasl-scan-dispatch form)) |
---|
1072 | ((eql info 0) |
---|
1073 | (puthash form *fasdump-hash* t)) |
---|
1074 | ((listp info) ; a make-load-form form |
---|
1075 | (when (cadr info) |
---|
1076 | (error "Circularity in ~S for ~S" 'make-load-form form)) |
---|
1077 | (let ((referenced-cell (cddr info))) |
---|
1078 | (setf (car referenced-cell) t) ; referenced-p |
---|
1079 | (setf (gethash (car info) *fasdump-hash*) t))))))) |
---|
1080 | |
---|
1081 | |
---|
1082 | |
---|
1083 | |
---|
1084 | (defun fasl-scan-dispatch (exp) |
---|
1085 | (when exp |
---|
1086 | (let ((type-code (typecode exp))) |
---|
1087 | (declare (fixnum type-code)) |
---|
1088 | (case type-code |
---|
1089 | (#.target::tag-fixnum |
---|
1090 | (fasl-scan-fixnum exp)) |
---|
1091 | (#.target::fulltag-cons (fasl-scan-list exp)) |
---|
1092 | #+ppc32-target |
---|
1093 | (#.ppc32::tag-imm) |
---|
1094 | #+ppc64-target |
---|
1095 | ((#.ppc64::fulltag-imm-0 |
---|
1096 | #.ppc64::fulltag-imm-1 |
---|
1097 | #.ppc64::fulltag-imm-2 |
---|
1098 | #.ppc64::fulltag-imm-3)) |
---|
1099 | #+x8632-target |
---|
1100 | (#.x8632::tag-imm) |
---|
1101 | #+x8664-target |
---|
1102 | ((#.x8664::fulltag-imm-0 |
---|
1103 | #.x8664::fulltag-imm-1)) |
---|
1104 | (t |
---|
1105 | (if |
---|
1106 | #+ppc32-target |
---|
1107 | (= (the fixnum (logand type-code ppc32::full-tag-mask)) ppc32::fulltag-immheader) |
---|
1108 | #+ppc64-target |
---|
1109 | (= (the fixnum (logand type-code ppc64::lowtagmask)) ppc64::lowtag-immheader) |
---|
1110 | #+x8632-target |
---|
1111 | (= (the fixnum (logand type-code x8632::fulltagmask)) x8632::fulltag-immheader) |
---|
1112 | #+x8664-target |
---|
1113 | (and (= (the fixnum (lisptag exp)) x8664::tag-misc) |
---|
1114 | (logbitp (the (unsigned-byte 16) (logand type-code x8664::fulltagmask)) |
---|
1115 | (logior (ash 1 x8664::fulltag-immheader-0) |
---|
1116 | (ash 1 x8664::fulltag-immheader-1) |
---|
1117 | (ash 1 x8664::fulltag-immheader-2)))) |
---|
1118 | (case type-code |
---|
1119 | ((#.target::subtag-macptr #.target::subtag-dead-macptr) (unless (%null-ptr-p exp) (fasl-unknown exp))) |
---|
1120 | (t (fasl-scan-ref exp))) |
---|
1121 | (case type-code |
---|
1122 | ((#.target::subtag-pool #.target::subtag-weak #.target::subtag-lock) (fasl-unknown exp)) |
---|
1123 | (#+ppc-target #.target::subtag-symbol |
---|
1124 | #+x8632-target #.target::subtag-symbol |
---|
1125 | #+x8664-target #.target::tag-symbol (fasl-scan-symbol exp)) |
---|
1126 | ((#.target::subtag-instance #.target::subtag-struct) |
---|
1127 | (fasl-scan-user-form exp)) |
---|
1128 | (#.target::subtag-package (fasl-scan-ref exp)) |
---|
1129 | (#.target::subtag-istruct |
---|
1130 | (if (memq (istruct-type-name exp) *istruct-make-load-form-types*) |
---|
1131 | (progn |
---|
1132 | (if (hash-table-p exp) |
---|
1133 | (fasl-lock-hash-table exp)) |
---|
1134 | (fasl-scan-user-form exp)) |
---|
1135 | (fasl-scan-gvector exp))) |
---|
1136 | #+x8632-target |
---|
1137 | (#.target::subtag-function (fasl-scan-clfun exp)) |
---|
1138 | #+x8664-target |
---|
1139 | (#.target::tag-function (fasl-scan-clfun exp)) |
---|
1140 | (t (fasl-scan-gvector exp))))))))) |
---|
1141 | |
---|
1142 | |
---|
1143 | (defun fasl-scan-ref (form) |
---|
1144 | (puthash form *fasdump-hash* 0)) |
---|
1145 | |
---|
1146 | (defun fasl-scan-fixnum (fixnum) |
---|
1147 | (unless (short-fixnum-p fixnum) (fasl-scan-ref fixnum))) |
---|
1148 | |
---|
1149 | (defparameter *istruct-make-load-form-types* |
---|
1150 | '(lexical-environment shared-library-descriptor shared-library-entry-point |
---|
1151 | external-entry-point foreign-variable |
---|
1152 | ctype unknown-ctype class-ctype foreign-ctype union-ctype member-ctype |
---|
1153 | array-ctype numeric-ctype hairy-ctype named-ctype constant-ctype args-ctype |
---|
1154 | hash-table package-ref type-cell class-cell slot-id)) |
---|
1155 | |
---|
1156 | |
---|
1157 | |
---|
1158 | |
---|
1159 | (defun fasl-scan-gvector (vec) |
---|
1160 | (fasl-scan-ref vec) |
---|
1161 | (dotimes (i (uvsize vec)) |
---|
1162 | (declare (fixnum i)) |
---|
1163 | (fasl-scan-form (%svref vec i)))) |
---|
1164 | |
---|
1165 | #+x86-target |
---|
1166 | (defun fasl-scan-clfun (f) |
---|
1167 | (let* ((fv (function-to-function-vector f)) |
---|
1168 | (size (uvsize fv)) |
---|
1169 | (ncode-words (%function-code-words f))) |
---|
1170 | (fasl-scan-ref f) |
---|
1171 | (do* ((k ncode-words (1+ k))) |
---|
1172 | ((= k size)) |
---|
1173 | (fasl-scan-form (uvref fv k))))) |
---|
1174 | |
---|
1175 | (defun funcall-lfun-p (form) |
---|
1176 | (and (listp form) |
---|
1177 | (eq (%car form) 'funcall) |
---|
1178 | (listp (%cdr form)) |
---|
1179 | (or (functionp (%cadr form)) |
---|
1180 | (eql (typecode (%cadr form)) target::subtag-xfunction)) |
---|
1181 | (null (%cddr form)))) |
---|
1182 | |
---|
1183 | ;;; We currently represent istruct-cells as conses. That's not |
---|
1184 | ;;; incredibly efficient (among other things, we have to do this |
---|
1185 | ;;; check when scanning/dumping any list), but it's probably not |
---|
1186 | ;;; worth burning a tag on them. There are currently about 50 |
---|
1187 | ;;; entries on the *istruct-cells* list. |
---|
1188 | (defun istruct-cell-p (x) |
---|
1189 | (and (consp x) |
---|
1190 | (typep (%car x) 'symbol) |
---|
1191 | (atom (%cdr x)) |
---|
1192 | (not (null (memq x *istruct-cells*))))) |
---|
1193 | |
---|
1194 | (defun fasl-scan-list (list) |
---|
1195 | (cond ((eq (%car list) cfasl-load-time-eval-sym) |
---|
1196 | (let ((form (car (%cdr list)))) |
---|
1197 | (fasl-scan-form (if (funcall-lfun-p form) |
---|
1198 | (%cadr form) |
---|
1199 | form)))) |
---|
1200 | ((istruct-cell-p list) |
---|
1201 | (fasl-scan-form (%car list))) |
---|
1202 | (t (when list |
---|
1203 | (fasl-scan-ref list) |
---|
1204 | (fasl-scan-form (%car list)) |
---|
1205 | (fasl-scan-form (%cdr list)))))) |
---|
1206 | |
---|
1207 | (defun fasl-scan-user-form (form) |
---|
1208 | (when (or (source-note-p form) |
---|
1209 | (code-note-p form)) |
---|
1210 | (return-from fasl-scan-user-form (fasl-scan-gvector form))) |
---|
1211 | (multiple-value-bind (load-form init-form) (make-load-form form *fcomp-load-forms-environment*) |
---|
1212 | (labels ((simple-load-form (form) |
---|
1213 | (or (atom form) |
---|
1214 | (let ((function (car form))) |
---|
1215 | (or (eq function 'quote) |
---|
1216 | (and (symbolp function) |
---|
1217 | ;; using fboundp instead of symbol-function |
---|
1218 | ;; see comments in symbol-function |
---|
1219 | (or (functionp (fboundp function)) |
---|
1220 | (eq function 'progn)) |
---|
1221 | ;; (every #'simple-load-form (cdr form)) |
---|
1222 | (dolist (arg (cdr form) t) |
---|
1223 | (unless (simple-load-form arg) |
---|
1224 | (return nil)))))))) |
---|
1225 | (load-time-eval-form (load-form form type) |
---|
1226 | (cond ((quoted-form-p load-form) |
---|
1227 | (%cadr load-form)) |
---|
1228 | ((self-evaluating-p load-form) |
---|
1229 | load-form) |
---|
1230 | ((simple-load-form load-form) |
---|
1231 | `(,cfasl-load-time-eval-sym ,load-form)) |
---|
1232 | (t (multiple-value-bind (lfun warnings) |
---|
1233 | (or |
---|
1234 | (gethash load-form *make-load-form-hash*) |
---|
1235 | (fcomp-named-function `(lambda () ,load-form) nil nil)) |
---|
1236 | (when warnings |
---|
1237 | (cerror "Ignore the warnings" |
---|
1238 | "Compiling the ~s ~a form for~%~s~%produced warnings." |
---|
1239 | 'make-load-form type form)) |
---|
1240 | (setf (gethash load-form *make-load-form-hash*) lfun) |
---|
1241 | `(,cfasl-load-time-eval-sym (funcall ,lfun))))))) |
---|
1242 | (declare (dynamic-extent #'simple-load-form #'load-time-eval-form)) |
---|
1243 | (let* ((compiled-initform |
---|
1244 | (and init-form (load-time-eval-form init-form form "initialization"))) |
---|
1245 | (info (list (load-time-eval-form load-form form "creation") |
---|
1246 | T ; scanning-p |
---|
1247 | nil ; referenced-p |
---|
1248 | compiled-initform ;initform-info |
---|
1249 | ))) |
---|
1250 | (puthash form *fasdump-hash* info) |
---|
1251 | (fasl-scan-form (%car info)) |
---|
1252 | (setf (cadr info) nil) ; no longer scanning load-form |
---|
1253 | (when init-form |
---|
1254 | (fasl-scan-form compiled-initform)))))) |
---|
1255 | |
---|
1256 | (defun fasl-scan-symbol (form) |
---|
1257 | (fasl-scan-ref form) |
---|
1258 | (fasl-scan-form (symbol-package form))) |
---|
1259 | |
---|
1260 | |
---|
1261 | |
---|
1262 | ;;;; Pass 3 - dumping |
---|
1263 | ;;;; |
---|
1264 | ;;;; |
---|
1265 | (defvar *fasdump-epush*) |
---|
1266 | (defvar *fasdump-stream*) |
---|
1267 | (defvar *fasdump-eref*) |
---|
1268 | |
---|
1269 | (defun fasl-dump-file (gnames goffsets forms hash filename) |
---|
1270 | (let ((opened? nil) |
---|
1271 | (finished? nil)) |
---|
1272 | (unwind-protect |
---|
1273 | (with-open-file (*fasdump-stream* filename :direction :output |
---|
1274 | :element-type '(unsigned-byte 8) |
---|
1275 | :if-exists :supersede |
---|
1276 | :if-does-not-exist :create) |
---|
1277 | (setq opened? t) |
---|
1278 | (fasl-set-filepos 0) |
---|
1279 | (fasl-out-word 0) ;Will become the ID word |
---|
1280 | (fasl-out-word 1) ;One block in the file |
---|
1281 | (fasl-out-long 12) ;Block starts at file pos 12 |
---|
1282 | (fasl-out-long 0) ;Length will go here |
---|
1283 | (fasl-dump-block gnames goffsets forms hash) ;Write the block |
---|
1284 | (let ((pos (fasl-filepos))) |
---|
1285 | (fasl-set-filepos 8) ;Back to length longword |
---|
1286 | (fasl-out-long (- pos 12))) ;Write length |
---|
1287 | (fasl-set-filepos 0) ;Seem to have won, make us legal |
---|
1288 | (fasl-out-word FASL-FILE-ID) |
---|
1289 | (setq finished? t) |
---|
1290 | filename) |
---|
1291 | (when (and opened? (not finished?)) |
---|
1292 | (delete-file filename))))) |
---|
1293 | |
---|
1294 | (defun fasl-dump-block (gnames goffsets forms hash) |
---|
1295 | (let ((etab-size (hash-table-count hash))) |
---|
1296 | (when (> etab-size 65535) |
---|
1297 | (error "Too many multiply-referenced objects in fasl file.~%Limit is ~d. Were ~d." 65535 etab-size)) |
---|
1298 | (fasl-out-word FASL-VERSION) ; Word 0 |
---|
1299 | (fasl-out-long 0) |
---|
1300 | (fasl-out-byte $fasl-vetab-alloc) |
---|
1301 | (fasl-out-count etab-size) |
---|
1302 | (fasl-dump gnames goffsets forms hash) |
---|
1303 | (fasl-out-byte $fasl-end))) |
---|
1304 | |
---|
1305 | (defun fasl-dump (gnames goffsets forms hash) |
---|
1306 | (let* ((*fasdump-hash* hash) |
---|
1307 | (*fasdump-read-package* nil) |
---|
1308 | (*fasdump-epush* nil) |
---|
1309 | (*fasdump-eref* -1) |
---|
1310 | (*fasdump-global-offsets* goffsets)) |
---|
1311 | (when gnames |
---|
1312 | (fasl-out-byte $fasl-globals) |
---|
1313 | (fasl-dump-form gnames)) |
---|
1314 | (dolist (op forms) |
---|
1315 | (if (packagep op) |
---|
1316 | (setq *fasdump-read-package* op) |
---|
1317 | (progn |
---|
1318 | (fasl-out-byte (car op)) |
---|
1319 | (dolist (arg (cdr op)) (fasl-dump-form arg))))))) |
---|
1320 | |
---|
1321 | ;;;During dumping, *fasdump-hash* values are one of the following: |
---|
1322 | ;;; nil - form has no load form, is referenced at most once. |
---|
1323 | ;;; fixnum - form has already been dumped, fixnum is the etab index. |
---|
1324 | ;;; T - form hasn't been dumped yet, is referenced more than once. |
---|
1325 | ;;; (load-form . nil) - form should be replaced by load-form. |
---|
1326 | (defun fasl-dump-form (form) |
---|
1327 | (let ((info (gethash form *fasdump-hash*))) |
---|
1328 | (cond ((fixnump info) |
---|
1329 | (fasl-out-byte $fasl-veref) |
---|
1330 | (fasl-out-count info)) |
---|
1331 | ((consp info) |
---|
1332 | (fasl-dump-user-form form info)) |
---|
1333 | (t |
---|
1334 | (setq *fasdump-epush* info) |
---|
1335 | (fasl-dump-dispatch form))))) |
---|
1336 | |
---|
1337 | (defun fasl-dump-user-form (form info) |
---|
1338 | (let* ((load-form (car info)) |
---|
1339 | (referenced-p (caddr info)) |
---|
1340 | (initform (cadddr info))) |
---|
1341 | (when referenced-p |
---|
1342 | (unless (gethash load-form *fasdump-hash*) |
---|
1343 | (error "~s was not in ~s. This shouldn't happen." 'load-form '*fasdump-hash*))) |
---|
1344 | (when initform |
---|
1345 | (fasl-out-byte $fasl-prog1)) ; ignore the initform |
---|
1346 | (fasl-dump-form load-form) |
---|
1347 | (when referenced-p |
---|
1348 | (setf (gethash form *fasdump-hash*) (gethash load-form *fasdump-hash*))) |
---|
1349 | (when initform |
---|
1350 | (fasl-dump-form initform)))) |
---|
1351 | |
---|
1352 | (defun fasl-out-opcode (opcode form) |
---|
1353 | (if *fasdump-epush* |
---|
1354 | (progn |
---|
1355 | (setq *fasdump-epush* nil) |
---|
1356 | (fasl-out-byte (fasl-epush-op opcode)) |
---|
1357 | (fasl-dump-epush form)) |
---|
1358 | (fasl-out-byte opcode))) |
---|
1359 | |
---|
1360 | (defun fasl-dump-epush (form) |
---|
1361 | #-bccl (when (fixnump (gethash form *fasdump-hash*)) |
---|
1362 | (error "Bug! Duplicate epush for ~S" form)) |
---|
1363 | (puthash form *fasdump-hash* (setq *fasdump-eref* (1+ *fasdump-eref*)))) |
---|
1364 | |
---|
1365 | |
---|
1366 | (defun fasl-dump-dispatch (exp) |
---|
1367 | (etypecase exp |
---|
1368 | ((signed-byte 16) (fasl-dump-s16 exp)) |
---|
1369 | ((signed-byte 32) (fasl-dump-s32 exp)) |
---|
1370 | ((signed-byte 64) (fasl-dump-s64 exp)) |
---|
1371 | (bignum (fasl-dump-32-bit-ivector exp $fasl-bignum32)) |
---|
1372 | (character (fasl-dump-char exp)) |
---|
1373 | (list (fasl-dump-list exp)) |
---|
1374 | (immediate (fasl-dump-t_imm exp)) |
---|
1375 | (double-float (fasl-dump-dfloat exp)) |
---|
1376 | (single-float (fasl-dump-sfloat exp)) |
---|
1377 | (simple-string (let* ((n (length exp))) |
---|
1378 | (fasl-out-opcode $fasl-nvstr exp) |
---|
1379 | (fasl-out-count n) |
---|
1380 | (fasl-out-simple-string exp 0 n))) |
---|
1381 | (simple-bit-vector (fasl-dump-bit-vector exp)) |
---|
1382 | ((simple-array (unsigned-byte 8) (*)) |
---|
1383 | (fasl-dump-8-bit-ivector exp $fasl-u8-vector)) |
---|
1384 | ((simple-array (signed-byte 8) (*)) |
---|
1385 | (fasl-dump-8-bit-ivector exp $fasl-s8-vector)) |
---|
1386 | ((simple-array (unsigned-byte 16) (*)) |
---|
1387 | (fasl-dump-16-bit-ivector exp $fasl-u16-vector)) |
---|
1388 | ((simple-array (signed-byte 16) (*)) |
---|
1389 | (fasl-dump-16-bit-ivector exp $fasl-s16-vector)) |
---|
1390 | ((simple-array (unsigned-byte 32) (*)) |
---|
1391 | (fasl-dump-32-bit-ivector exp $fasl-u32-vector)) |
---|
1392 | ((simple-array (signed-byte 32) (*)) |
---|
1393 | (fasl-dump-32-bit-ivector exp $fasl-s32-vector)) |
---|
1394 | ((simple-array single-float (*)) |
---|
1395 | (fasl-dump-32-bit-ivector exp $fasl-single-float-vector)) |
---|
1396 | ((simple-array double-float (*)) |
---|
1397 | (fasl-dump-double-float-vector exp)) |
---|
1398 | (symbol (fasl-dump-symbol exp)) |
---|
1399 | (package (fasl-dump-package exp)) |
---|
1400 | (function (fasl-dump-function exp)) |
---|
1401 | (xfunction (fasl-dump-function exp)) |
---|
1402 | (code-vector (fasl-dump-codevector exp)) |
---|
1403 | (xcode-vector (fasl-dump-codevector exp)) |
---|
1404 | (simple-vector (fasl-dump-gvector exp $fasl-t-vector)) |
---|
1405 | (ratio (fasl-dump-ratio exp)) |
---|
1406 | (complex (fasl-dump-complex exp)) |
---|
1407 | #+(and 64-bit-target (not cross-compiling)) |
---|
1408 | ((simple-array (unsigned-byte 64) (*)) |
---|
1409 | (fasl-dump-64-bit-ivector exp $fasl-u64-vector)) |
---|
1410 | #+(and 64-bit-target (not cross-compiling)) |
---|
1411 | ((simple-array (signed-byte 64) (*)) |
---|
1412 | (fasl-dump-64-bit-ivector exp $fasl-s64-vector)) |
---|
1413 | (ivector |
---|
1414 | (unless (eq (backend-target-arch-name *target-backend*) |
---|
1415 | (backend-target-arch-name *host-backend*)) |
---|
1416 | (error "can't cross-compile constant reference to ~s" exp)) |
---|
1417 | (let* ((typecode (typecode exp)) |
---|
1418 | (n (uvsize exp)) |
---|
1419 | (nb (subtag-bytes typecode n))) |
---|
1420 | (declare (fixnum n nb typecode)) |
---|
1421 | (fasl-out-opcode $fasl-vivec exp) |
---|
1422 | (fasl-out-byte typecode) |
---|
1423 | (fasl-out-count n) |
---|
1424 | (fasl-out-ivect exp 0 nb))) |
---|
1425 | (vector (fasl-dump-gvector exp $fasl-vector-header)) |
---|
1426 | (array (fasl-dump-gvector exp $fasl-array-header)) |
---|
1427 | |
---|
1428 | (gvector |
---|
1429 | (if (= (typecode exp) target::subtag-istruct) |
---|
1430 | (fasl-dump-gvector exp $fasl-istruct) |
---|
1431 | (progn |
---|
1432 | (unless (eq (backend-target-arch-name *target-backend*) |
---|
1433 | (backend-target-arch-name *host-backend*)) |
---|
1434 | (error "can't cross-compile constant reference to ~s" exp)) |
---|
1435 | (let* ((typecode (typecode exp)) |
---|
1436 | (n (uvsize exp))) |
---|
1437 | (declare (fixnum n typecode)) |
---|
1438 | (fasl-out-opcode $fasl-vgvec exp) |
---|
1439 | (fasl-out-byte typecode) |
---|
1440 | (fasl-out-count n) |
---|
1441 | (dotimes (i n) |
---|
1442 | (fasl-dump-form (%svref exp i))))))))) |
---|
1443 | |
---|
1444 | (defun fasl-dump-gvector (v op) |
---|
1445 | (let* ((n (uvsize v))) |
---|
1446 | (fasl-out-opcode op v) |
---|
1447 | (fasl-out-count n) |
---|
1448 | (dotimes (i n) |
---|
1449 | (fasl-dump-form (%svref v i))))) |
---|
1450 | |
---|
1451 | (defun fasl-dump-ratio (v) |
---|
1452 | (fasl-out-opcode $fasl-ratio v) |
---|
1453 | (fasl-dump-form (%svref v target::ratio.numer-cell)) |
---|
1454 | (fasl-dump-form (%svref v target::ratio.denom-cell))) |
---|
1455 | |
---|
1456 | (defun fasl-dump-complex (v) |
---|
1457 | (fasl-out-opcode $fasl-complex v) |
---|
1458 | (fasl-dump-form (%svref v target::complex.realpart-cell)) |
---|
1459 | (fasl-dump-form (%svref v target::complex.imagpart-cell))) |
---|
1460 | |
---|
1461 | (defun fasl-dump-bit-vector (v) |
---|
1462 | (let* ((n (uvsize v))) |
---|
1463 | (fasl-out-opcode $fasl-bit-vector v) |
---|
1464 | (fasl-out-count n) |
---|
1465 | (if (eq *fasl-host-big-endian* *fasl-target-big-endian*) |
---|
1466 | (let* ((nb (ash (+ n 7) -3))) |
---|
1467 | (fasl-out-ivect v 0 nb)) |
---|
1468 | (compiler-bug "need to byte-swap ~a" v)))) |
---|
1469 | |
---|
1470 | (defun fasl-dump-8-bit-ivector (v op) |
---|
1471 | (let* ((n (uvsize v))) |
---|
1472 | (fasl-out-opcode op v) |
---|
1473 | (fasl-out-count n) |
---|
1474 | (let* ((nb n)) |
---|
1475 | (fasl-out-ivect v 0 nb)))) |
---|
1476 | |
---|
1477 | (defun fasl-dump-16-bit-ivector (v op) |
---|
1478 | (let* ((n (uvsize v))) |
---|
1479 | (fasl-out-opcode op v) |
---|
1480 | (fasl-out-count n) |
---|
1481 | (if (eq *fasl-host-big-endian* *fasl-target-big-endian*) |
---|
1482 | (let* ((nb (ash n 1))) |
---|
1483 | (fasl-out-ivect v 0 nb)) |
---|
1484 | (dotimes (i n) |
---|
1485 | (let* ((k (uvref v i))) |
---|
1486 | (fasl-out-byte (ldb (byte 8 0) k)) |
---|
1487 | (fasl-out-byte (ldb (byte 8 8) k))))))) |
---|
1488 | |
---|
1489 | (defun fasl-dump-32-bit-ivector (v op) |
---|
1490 | (let* ((n (uvsize v))) |
---|
1491 | (fasl-out-opcode op v) |
---|
1492 | (fasl-out-count n) |
---|
1493 | (if (eq *fasl-host-big-endian* *fasl-target-big-endian*) |
---|
1494 | (let* ((nb (ash n 2))) |
---|
1495 | (fasl-out-ivect v 0 nb)) |
---|
1496 | (dotimes (i n) |
---|
1497 | (let* ((k (uvref v i))) |
---|
1498 | (fasl-out-byte (ldb (byte 8 0) k)) |
---|
1499 | (fasl-out-byte (ldb (byte 8 8) k)) |
---|
1500 | (fasl-out-byte (ldb (byte 8 16) k)) |
---|
1501 | (fasl-out-byte (ldb (byte 8 24) k))))))) |
---|
1502 | |
---|
1503 | |
---|
1504 | (defun fasl-dump-64-bit-ivector (v op) |
---|
1505 | (let* ((n (uvsize v))) |
---|
1506 | (fasl-out-opcode op v) |
---|
1507 | (fasl-out-count n) |
---|
1508 | (if (eq *fasl-host-big-endian* *fasl-target-big-endian*) |
---|
1509 | (let* ((nb (ash n 3))) |
---|
1510 | (fasl-out-ivect v 0 nb)) |
---|
1511 | (compiler-bug "need to byte-swap ~a" v)))) |
---|
1512 | |
---|
1513 | (defun fasl-dump-double-float-vector (v) |
---|
1514 | (let* ((n (uvsize v))) |
---|
1515 | (fasl-out-opcode $fasl-double-float-vector v) |
---|
1516 | (fasl-out-count n) |
---|
1517 | (if (eq *fasl-host-big-endian* *fasl-target-big-endian*) |
---|
1518 | (let* ((nb (ash n 3))) |
---|
1519 | (fasl-out-ivect v (- target::misc-dfloat-offset |
---|
1520 | target::misc-data-offset) nb)) |
---|
1521 | (compiler-bug "need to byte-swap ~a" v)))) |
---|
1522 | |
---|
1523 | ;;; This is used to dump functions and "xfunctions". |
---|
1524 | ;;; If we're cross-compiling, we shouldn't reference any |
---|
1525 | ;;; (host) functions as constants; try to detect that |
---|
1526 | ;;; case. |
---|
1527 | #-x86-target |
---|
1528 | (defun fasl-dump-function (f) |
---|
1529 | (if (and (not (eq *fasl-backend* *host-backend*)) |
---|
1530 | (typep f 'function)) |
---|
1531 | (compiler-bug "Dumping a native function constant ~s during cross-compilation." f)) |
---|
1532 | (if (and (= (typecode f) target::subtag-xfunction) |
---|
1533 | (= (typecode (uvref f 0)) target::subtag-u8-vector)) |
---|
1534 | (fasl-xdump-clfun f) |
---|
1535 | (let* ((n (uvsize f))) |
---|
1536 | (fasl-out-opcode $fasl-function f) |
---|
1537 | (fasl-out-count n) |
---|
1538 | (dotimes (i n) |
---|
1539 | (fasl-dump-form (%svref f i)))))) |
---|
1540 | |
---|
1541 | #+x86-target |
---|
1542 | (defun fasl-dump-function (f) |
---|
1543 | (if (and (not (eq *fasl-backend* *host-backend*)) |
---|
1544 | (typep f 'function)) |
---|
1545 | (compiler-bug "Dumping a native function constant ~s during cross-compilation." f)) |
---|
1546 | (if (and (= (typecode f) target::subtag-xfunction) |
---|
1547 | (= (typecode (uvref f 0)) target::subtag-u8-vector)) |
---|
1548 | (fasl-xdump-clfun f) |
---|
1549 | (let* ((code-size (%function-code-words f)) |
---|
1550 | (function-vector (function-to-function-vector f)) |
---|
1551 | (function-size (uvsize function-vector))) |
---|
1552 | (fasl-out-opcode $fasl-clfun f) |
---|
1553 | (fasl-out-count function-size) |
---|
1554 | (fasl-out-count code-size) |
---|
1555 | (fasl-out-ivect function-vector 0 (ash code-size target::word-shift)) |
---|
1556 | (do* ((k code-size (1+ k))) |
---|
1557 | ((= k function-size)) |
---|
1558 | (declare (fixnum k)) |
---|
1559 | (fasl-dump-form (uvref function-vector k)))))) |
---|
1560 | |
---|
1561 | |
---|
1562 | |
---|
1563 | |
---|
1564 | ;;; Write a "concatenated function". |
---|
1565 | (defun fasl-xdump-clfun (f) |
---|
1566 | (target-arch-case |
---|
1567 | (:x8632 |
---|
1568 | (let* ((code (uvref f 0)) |
---|
1569 | (function-size (ash (uvsize code) -2)) |
---|
1570 | (imm-words (dpb (uvref code 1) (byte 8 8) (uvref code 0))) |
---|
1571 | (imm-bytes (ash imm-words 2)) |
---|
1572 | (other-words (- function-size imm-words))) |
---|
1573 | (assert (= other-words (1- (uvsize f)))) |
---|
1574 | (fasl-out-opcode $fasl-clfun f) |
---|
1575 | (fasl-out-count function-size) |
---|
1576 | (fasl-out-count imm-words) |
---|
1577 | (fasl-out-ivect code 0 imm-bytes) |
---|
1578 | (do ((i 1 (1+ i)) |
---|
1579 | (n (uvsize f))) |
---|
1580 | ((= i n)) |
---|
1581 | (declare (fixnum i n)) |
---|
1582 | (fasl-dump-form (%svref f i))))) |
---|
1583 | (:x8664 |
---|
1584 | (let* ((code (uvref f 0)) |
---|
1585 | (code-size (dpb (uvref code 3) |
---|
1586 | (byte 8 24) |
---|
1587 | (dpb (uvref code 2) |
---|
1588 | (byte 8 16) |
---|
1589 | (dpb (uvref code 1) |
---|
1590 | (byte 8 8) |
---|
1591 | (uvref code 0))))) |
---|
1592 | (function-size (ash (uvsize code) -3))) |
---|
1593 | (assert (= (- function-size code-size) (1- (uvsize f)))) |
---|
1594 | (fasl-out-opcode $fasl-clfun f) |
---|
1595 | (fasl-out-count function-size) |
---|
1596 | (fasl-out-count code-size) |
---|
1597 | (fasl-out-ivect code 0 (ash code-size 3)) |
---|
1598 | (do* ((i 1 (1+ i)) |
---|
1599 | (n (uvsize f))) |
---|
1600 | ((= i n)) |
---|
1601 | (declare (fixnum i n)) |
---|
1602 | (fasl-dump-form (%svref f i))))))) |
---|
1603 | |
---|
1604 | (defun fasl-dump-codevector (c) |
---|
1605 | (if (and (not (eq *fasl-backend* *host-backend*)) |
---|
1606 | (typep c 'code-vector)) |
---|
1607 | (compiler-bug "Dumping a native code-vector constant ~s during cross-compilation." c)) |
---|
1608 | (let* ((n (uvsize c))) |
---|
1609 | (fasl-out-opcode $fasl-code-vector c) |
---|
1610 | (fasl-out-count n) |
---|
1611 | (fasl-out-ivect c))) |
---|
1612 | |
---|
1613 | (defun fasl-dump-t_imm (imm) |
---|
1614 | (fasl-out-opcode $fasl-timm imm) |
---|
1615 | (fasl-out-long (%address-of imm))) |
---|
1616 | |
---|
1617 | (defun fasl-dump-char (char) ; << maybe not |
---|
1618 | (let ((code (%char-code char))) |
---|
1619 | (fasl-out-opcode $fasl-char char) |
---|
1620 | (fasl-out-count code))) |
---|
1621 | |
---|
1622 | ;;; Always write big-endian. |
---|
1623 | (defun fasl-dump-s16 (s16) |
---|
1624 | (fasl-out-opcode $fasl-word-fixnum s16) |
---|
1625 | (fasl-out-word s16)) |
---|
1626 | |
---|
1627 | ;;; Always write big-endian |
---|
1628 | (defun fasl-dump-s32 (s32) |
---|
1629 | (fasl-out-opcode $fasl-s32 s32) |
---|
1630 | (fasl-out-word (ldb (byte 16 16) s32)) |
---|
1631 | (fasl-out-word (ldb (byte 16 0) s32))) |
---|
1632 | |
---|
1633 | ;;; Always write big-endian |
---|
1634 | (defun fasl-dump-s64 (s64) |
---|
1635 | (fasl-out-opcode $fasl-s64 s64) |
---|
1636 | (fasl-out-word (ldb (byte 16 48) s64)) |
---|
1637 | (fasl-out-word (ldb (byte 16 32) s64)) |
---|
1638 | (fasl-out-word (ldb (byte 16 16) s64)) |
---|
1639 | (fasl-out-word (ldb (byte 16 0) s64))) |
---|
1640 | |
---|
1641 | |
---|
1642 | |
---|
1643 | (defun fasl-dump-dfloat (float) |
---|
1644 | (fasl-out-opcode $fasl-dfloat float) |
---|
1645 | (multiple-value-bind (high low) (double-float-bits float) |
---|
1646 | (fasl-out-long high) |
---|
1647 | (fasl-out-long low))) |
---|
1648 | |
---|
1649 | (defun fasl-dump-sfloat (float) |
---|
1650 | (fasl-out-opcode $fasl-sfloat float) |
---|
1651 | (fasl-out-long (single-float-bits float))) |
---|
1652 | |
---|
1653 | |
---|
1654 | (defun fasl-dump-package (pkg) |
---|
1655 | (let ((name (package-name pkg))) |
---|
1656 | (fasl-out-opcode $fasl-nvpkg pkg) |
---|
1657 | (fasl-out-nvstring name))) |
---|
1658 | |
---|
1659 | |
---|
1660 | |
---|
1661 | (defun fasl-dump-list (list) |
---|
1662 | (cond ((null list) (fasl-out-opcode $fasl-nil list)) |
---|
1663 | ((eq (%car list) cfasl-load-time-eval-sym) |
---|
1664 | (let* ((form (car (%cdr list))) |
---|
1665 | (opcode $fasl-eval)) |
---|
1666 | (when (funcall-lfun-p form) |
---|
1667 | (setq opcode $fasl-lfuncall |
---|
1668 | form (%cadr form))) |
---|
1669 | (if *fasdump-epush* |
---|
1670 | (progn |
---|
1671 | (fasl-out-byte (fasl-epush-op opcode)) |
---|
1672 | (fasl-dump-form form) |
---|
1673 | (fasl-dump-epush list)) |
---|
1674 | (progn |
---|
1675 | (fasl-out-byte opcode) |
---|
1676 | (fasl-dump-form form))))) |
---|
1677 | ((istruct-cell-p list) |
---|
1678 | (fasl-out-opcode $fasl-istruct-cell (car list)) |
---|
1679 | (fasl-dump-symbol (car list))) |
---|
1680 | (t (fasl-dump-cons list)))) |
---|
1681 | |
---|
1682 | (defun fasl-dump-cons (cons &aux (end cons) (cdr-len 0)) |
---|
1683 | (declare (fixnum cdr-len)) |
---|
1684 | (while (and (consp (setq end (%cdr end))) |
---|
1685 | (null (gethash end *fasdump-hash*))) |
---|
1686 | (incf cdr-len)) |
---|
1687 | (if (eql 0 cdr-len) |
---|
1688 | (fasl-out-opcode $fasl-cons cons) |
---|
1689 | (progn |
---|
1690 | (fasl-out-opcode (if end $fasl-vlist* $fasl-vlist) cons) |
---|
1691 | (fasl-out-count cdr-len))) |
---|
1692 | (dotimes (i (the fixnum (1+ cdr-len))) |
---|
1693 | (fasl-dump-form (%car cons)) |
---|
1694 | (setq cons (%cdr cons))) |
---|
1695 | (when (or (eql 0 cdr-len) end) ;cons or list* |
---|
1696 | (fasl-dump-form end))) |
---|
1697 | |
---|
1698 | |
---|
1699 | |
---|
1700 | (defun fasl-dump-symbol (sym) |
---|
1701 | (let* ((pkg (symbol-package sym)) |
---|
1702 | (name (symbol-name sym)) |
---|
1703 | (idx (let* ((i (%svref (symptr->symvector (%symbol->symptr sym)) target::symbol.binding-index-cell))) |
---|
1704 | (declare (fixnum i)) |
---|
1705 | (unless (zerop i) i)))) |
---|
1706 | (cond ((null pkg) |
---|
1707 | (progn |
---|
1708 | (fasl-out-opcode (if idx $fasl-nvmksym-special $fasl-nvmksym) sym) |
---|
1709 | (fasl-out-nvstring name))) |
---|
1710 | (*fasdump-epush* |
---|
1711 | (progn |
---|
1712 | (fasl-out-byte (fasl-epush-op (if idx |
---|
1713 | $fasl-nvpkg-intern-special |
---|
1714 | $fasl-nvpkg-intern))) |
---|
1715 | (fasl-dump-form pkg) |
---|
1716 | (fasl-dump-epush sym) |
---|
1717 | (fasl-out-nvstring name))) |
---|
1718 | (t |
---|
1719 | (progn |
---|
1720 | (fasl-out-byte (if idx |
---|
1721 | $fasl-nvpkg-intern-special |
---|
1722 | $fasl-nvpkg-intern)) |
---|
1723 | (fasl-dump-form pkg) |
---|
1724 | (fasl-out-nvstring name)))))) |
---|
1725 | |
---|
1726 | |
---|
1727 | (defun fasl-unknown (exp) |
---|
1728 | (error "Can't dump ~S - unknown type" exp)) |
---|
1729 | |
---|
1730 | (defun fasl-out-simple-string (str start end) |
---|
1731 | (declare (simple-string str) (fixnum start end)) |
---|
1732 | (do* ((k start (1+ k))) |
---|
1733 | ((= k end)) |
---|
1734 | (declare (fixnum k)) |
---|
1735 | (fasl-out-count (char-code (schar str k))))) |
---|
1736 | |
---|
1737 | (defun fasl-out-nvstring (str) |
---|
1738 | (fasl-out-count (length str)) |
---|
1739 | (fasl-out-simple-string str 0 (length str))) |
---|
1740 | |
---|
1741 | (defun fasl-out-ivect (iv &optional |
---|
1742 | (start 0) |
---|
1743 | (nb |
---|
1744 | (subtag-bytes (typecode iv) (uvsize iv)))) |
---|
1745 | (stream-write-ivector *fasdump-stream* iv start nb)) |
---|
1746 | |
---|
1747 | |
---|
1748 | (defun fasl-out-long (long) |
---|
1749 | (fasl-out-word (ash long -16)) |
---|
1750 | (fasl-out-word (logand long #xFFFF))) |
---|
1751 | |
---|
1752 | (defun fasl-out-word (word) |
---|
1753 | (fasl-out-byte (ash word -8)) |
---|
1754 | (fasl-out-byte word)) |
---|
1755 | |
---|
1756 | (defun fasl-out-byte (byte) |
---|
1757 | (write-byte (%ilogand2 byte #xFF) *fasdump-stream*)) |
---|
1758 | |
---|
1759 | ;;; Write an unsigned integer in 7-bit chunks. |
---|
1760 | (defun fasl-out-count (val) |
---|
1761 | (do* ((b (ldb (byte 7 0) val) (ldb (byte 7 0) val)) |
---|
1762 | (done nil)) |
---|
1763 | (done) |
---|
1764 | (when (zerop (setq val (ash val -7))) |
---|
1765 | (setq b (logior #x80 b) done t)) |
---|
1766 | (fasl-out-byte b))) |
---|
1767 | |
---|
1768 | (defun fasl-filepos () |
---|
1769 | (file-position *fasdump-stream*)) |
---|
1770 | |
---|
1771 | (defun fasl-set-filepos (pos) |
---|
1772 | (file-position *fasdump-stream* pos) |
---|
1773 | #-bccl (unless (eq (file-position *fasdump-stream*) pos) |
---|
1774 | (error "Unable to set file position to ~S" pos))) |
---|
1775 | |
---|
1776 | ;;; Concatenate fasl files. |
---|
1777 | |
---|
1778 | ;;; Format of a fasl file as expected by the fasloader. |
---|
1779 | ;;; |
---|
1780 | ;;; #xFF00 2 bytes - File version |
---|
1781 | ;;; Block Count 2 bytes - Number of blocks in the file |
---|
1782 | ;;; addr[0] 4 bytes - address of 0th block |
---|
1783 | ;;; length[0] 4 bytes - length of 0th block |
---|
1784 | ;;; addr[1] 4 bytes - address of 1st block |
---|
1785 | ;;; length[1] 4 bytes - length of 1st block |
---|
1786 | ;;; ... |
---|
1787 | ;;; addr[n-1] 4 bytes |
---|
1788 | ;;; length[n-1] 4 bytes |
---|
1789 | ;;; length[0] + length[1] + ... + length [n-1] bytes of data |
---|
1790 | |
---|
1791 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
---|
1792 | ;; |
---|
1793 | ;; (fasl-concatenate out-file fasl-files &key :if-exists) |
---|
1794 | ;; |
---|
1795 | ;; out-file name of file in which to store the concatenation |
---|
1796 | ;; fasl-files list of names of fasl files to concatenate |
---|
1797 | ;; if-exists as for OPEN, defaults to :error |
---|
1798 | ;; |
---|
1799 | ;; function result: pathname to the output file. |
---|
1800 | ;; It works to use the output of one invocation of fasl-concatenate |
---|
1801 | ;; as an input of another invocation. |
---|
1802 | ;; |
---|
1803 | (defun fasl-concatenate (out-file fasl-files &key (if-exists :error)) |
---|
1804 | (%fasl-concatenate out-file fasl-files if-exists (pathname-type *.fasl-pathname*))) |
---|
1805 | |
---|
1806 | (defun %fasl-concatenate (out-file fasl-files if-exists file-ext) |
---|
1807 | (let ((count 0) |
---|
1808 | (created? nil) |
---|
1809 | (finished? nil) |
---|
1810 | (ext-pathname (make-pathname :type file-ext))) |
---|
1811 | (declare (fixnum count)) |
---|
1812 | (flet ((fasl-read-halfword (f) |
---|
1813 | (dpb (read-byte f) (byte 8 8) (read-byte f))) |
---|
1814 | (fasl-write-halfword (h f) |
---|
1815 | (write-byte (ldb (byte 8 8) h) f) |
---|
1816 | (write-byte (ldb (byte 8 0) h) f) |
---|
1817 | h)) |
---|
1818 | (flet ((fasl-read-fullword (f) |
---|
1819 | (dpb (fasl-read-halfword f) (byte 16 16) (fasl-read-halfword f))) |
---|
1820 | (fasl-write-fullword (w f) |
---|
1821 | (fasl-write-halfword (ldb (byte 16 16) w) f) |
---|
1822 | (fasl-write-halfword (ldb (byte 16 0) w) f) |
---|
1823 | w)) |
---|
1824 | (dolist (file fasl-files) |
---|
1825 | (setq file (merge-pathnames file ext-pathname)) |
---|
1826 | (unless (equal (pathname-type file) file-ext) |
---|
1827 | (error "Not a ~A file: ~s" file-ext file)) |
---|
1828 | (with-open-file (instream file :element-type '(unsigned-byte 8)) |
---|
1829 | (unless (eql fasl-file-id (fasl-read-halfword instream)) |
---|
1830 | (error "Bad ~A file ID in ~s" file-ext file)) |
---|
1831 | (incf count (fasl-read-halfword instream)))) |
---|
1832 | (unwind-protect |
---|
1833 | (with-open-file (outstream |
---|
1834 | (setq out-file (merge-pathnames out-file ext-pathname)) |
---|
1835 | :element-type '(unsigned-byte 8) |
---|
1836 | :direction :output |
---|
1837 | :if-does-not-exist :create |
---|
1838 | :if-exists if-exists) |
---|
1839 | (setq created? t) |
---|
1840 | (let ((addr-address 4) |
---|
1841 | (data-address (+ 4 (* count 8)))) |
---|
1842 | (fasl-write-halfword 0 outstream) ; will be $fasl-id |
---|
1843 | (fasl-write-halfword count outstream) |
---|
1844 | (dotimes (i (* 2 count)) |
---|
1845 | (fasl-write-fullword 0 outstream)) ; for addresses/lengths |
---|
1846 | (dolist (file fasl-files) |
---|
1847 | (with-open-file (instream (merge-pathnames file ext-pathname) |
---|
1848 | :element-type '(unsigned-byte 8)) |
---|
1849 | (fasl-read-halfword instream) ; skip ID |
---|
1850 | (let* ((fasl-count (fasl-read-halfword instream)) |
---|
1851 | (addrs (make-array fasl-count)) |
---|
1852 | (sizes (make-array fasl-count)) |
---|
1853 | addr0) |
---|
1854 | (declare (fixnum fasl-count) |
---|
1855 | (dynamic-extent addrs sizes)) |
---|
1856 | (dotimes (i fasl-count) |
---|
1857 | (setf (svref addrs i) (fasl-read-fullword instream) |
---|
1858 | (svref sizes i) (fasl-read-fullword instream))) |
---|
1859 | (setq addr0 (svref addrs 0)) |
---|
1860 | (file-position outstream addr-address) |
---|
1861 | (dotimes (i fasl-count) |
---|
1862 | (fasl-write-fullword |
---|
1863 | (+ data-address (- (svref addrs i) addr0)) |
---|
1864 | outstream) |
---|
1865 | (fasl-write-fullword (svref sizes i) outstream) |
---|
1866 | (incf addr-address 8)) |
---|
1867 | (file-position outstream data-address) |
---|
1868 | (dotimes (i fasl-count) |
---|
1869 | (file-position instream (svref addrs i)) |
---|
1870 | (let ((fasl-length (svref sizes i))) |
---|
1871 | (dotimes (j fasl-length) |
---|
1872 | (write-byte (read-byte instream) outstream)) |
---|
1873 | (incf data-address fasl-length)))))) |
---|
1874 | (stream-length outstream data-address) |
---|
1875 | (file-position outstream 0) |
---|
1876 | (fasl-write-halfword fasl-file-id outstream) |
---|
1877 | (setq finished? t))) |
---|
1878 | (when (and created? (not finished?)) |
---|
1879 | (delete-file out-file)))) |
---|
1880 | out-file))) |
---|
1881 | |
---|
1882 | ;;; Cross-compilation environment stuff. Some of this involves |
---|
1883 | ;;; setting up the TARGET and OS packages. |
---|
1884 | (defun ensure-package-nickname (name package) |
---|
1885 | (let* ((old (find-package name))) |
---|
1886 | (unless (eq old package) |
---|
1887 | (rename-package old (package-name old) (delete name (package-nicknames old) :test #'string=)) |
---|
1888 | (rename-package package (package-name package) (cons name (package-nicknames package))) |
---|
1889 | old))) |
---|
1890 | |
---|
1891 | (defmacro with-cross-compilation-package ((name target) &body body) |
---|
1892 | (let* ((old-package (gensym)) |
---|
1893 | (name-var (gensym)) |
---|
1894 | (target-var (gensym))) |
---|
1895 | `(let* ((,name-var ,name) |
---|
1896 | (,target-var ,target) |
---|
1897 | (,old-package (ensure-package-nickname ,name-var ,target-var))) |
---|
1898 | (unwind-protect |
---|
1899 | (progn ,@body) |
---|
1900 | (when ,old-package (ensure-package-nickname ,name-var |
---|
1901 | ,old-package)))))) |
---|
1902 | |
---|
1903 | (defun %with-cross-compilation-target (target thunk) |
---|
1904 | (let* ((backend (find-backend target))) |
---|
1905 | (if (null backend) |
---|
1906 | (error "No known compilation target named ~s." target) |
---|
1907 | (let* ((arch (backend-target-arch backend)) |
---|
1908 | (arch-package-name (arch::target-package-name arch)) |
---|
1909 | (ftd (backend-target-foreign-type-data backend)) |
---|
1910 | (ftd-package-name (ftd-interface-package-name ftd))) |
---|
1911 | (or (find-package arch-package-name) |
---|
1912 | (make-package arch-package-name)) |
---|
1913 | (or (find-package ftd-package-name) |
---|
1914 | (make-package ftd-package-name :use "COMMON-LISP")) |
---|
1915 | (with-cross-compilation-package ("OS" ftd-package-name) |
---|
1916 | (with-cross-compilation-package ("TARGET" arch-package-name) |
---|
1917 | (let* ((*target-ftd* ftd)) |
---|
1918 | (funcall thunk)))))))) |
---|
1919 | |
---|
1920 | (defmacro with-cross-compilation-target ((target) &body body) |
---|
1921 | `(%with-cross-compilation-target ,target #'(lambda () ,@body))) |
---|
1922 | |
---|
1923 | |
---|
1924 | |
---|
1925 | |
---|
1926 | (provide 'nfcomp) |
---|
1927 | |
---|