| 1 |
;;;; |
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll |
| 5 |
;;;; Copyright (c) 2021, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
;;;; |
| 10 |
;;;; CMPDATA Data segment. |
| 11 |
;;;; |
| 12 |
;;;; Each lisp compiled file consists on code and a data section. Whenever an |
| 13 |
;;;; #'in-package toplevel form is found, a read-time evaluated expression is |
| 14 |
;;;; inserted in the data section which changes the current package for the |
| 15 |
;;;; rest of it. This way it is possible to save some space by writing the |
| 16 |
;;;; symbol's package only when it does not belong to the current package. |
| 17 |
|
| 18 |
(in-package #:compiler) |
| 19 |
|
| 20 |
(defun add-load-form (object location) |
| 21 |
(multiple-value-bind (make-form init-form) (make-load-form object) |
| 22 |
(setf (gethash object *load-objects*) location) |
| 23 |
(let (deferred) |
| 24 |
(when make-form |
| 25 |
(let ((*objects-init-deferred* nil) |
| 26 |
(*objects-being-created* (list* object *objects-being-created*))) |
| 27 |
(push (make-c1form* 'MAKE-FORM :args location (c1expr make-form)) *make-forms*) |
| 28 |
(setf deferred (nreverse *objects-init-deferred*)))) |
| 29 |
(flet ((maybe-init (loc init) |
| 30 |
(handler-case |
| 31 |
(push (make-c1form* 'INIT-FORM :args loc (c1expr init)) *make-forms*) |
| 32 |
(circular-dependency (c) |
| 33 |
(if *objects-being-created* |
| 34 |
(push (cons location init-form) *objects-init-deferred*) |
| 35 |
(error c)))))) |
| 36 |
(loop for (loc . init) in deferred |
| 37 |
do (maybe-init loc init) |
| 38 |
finally (when init-form |
| 39 |
(maybe-init location init-form))))))) |
| 40 |
|
| 41 |
(defun data-empty-loc () |
| 42 |
(add-object *empty-loc* :duplicate t :permanent t)) |
| 43 |
|
| 44 |
(defun find-vv (object &key permanent (errorp t)) |
| 45 |
(let* ((test (if si:*compiler-constants* 'eq 'equal-with-circularity)) |
| 46 |
(item (find object *referenced-objects* :test test :key #'vv-value))) |
| 47 |
(if (null item) |
| 48 |
(when errorp |
| 49 |
(cmperr "Unable to find object ~s." object)) |
| 50 |
(when permanent |
| 51 |
;; Objects are put in the appropriate storage in the second pass and |
| 52 |
;; we still can move them. If we look for a permanent object that was |
| 53 |
;; allocated as a temporary one, then change it to permanent. |
| 54 |
(setf (vv-permanent-p item) t))) |
| 55 |
item)) |
| 56 |
|
| 57 |
(defun push-vv (&rest args &key permanent-p value) |
| 58 |
(let ((vv (apply #'make-vv args))) |
| 59 |
(vector-push-extend vv *referenced-objects*) |
| 60 |
vv)) |
| 61 |
|
| 62 |
(defun add-object (object &key |
| 63 |
(duplicate nil) |
| 64 |
(used-p nil) |
| 65 |
(always nil) |
| 66 |
(permanent (or (symbolp object) |
| 67 |
*permanent-data*)) |
| 68 |
&aux load-form-p) |
| 69 |
(unless (eq *compiler-phase* 't1) |
| 70 |
(cmperr "ADD-OBJECT: Unable to internalize objects in ~a phase." |
| 71 |
*compiler-phase*)) |
| 72 |
(let* ((item (find-vv object :permanent permanent :errorp nil)) |
| 73 |
(vv (cond |
| 74 |
((and item duplicate) |
| 75 |
(push-vv :permanent-p permanent :value object)) |
| 76 |
(item |
| 77 |
(when (member object *objects-being-created*) |
| 78 |
(error 'circular-dependency :form object)) |
| 79 |
item) |
| 80 |
(t |
| 81 |
(let ((vv (push-vv :permanent-p permanent :value object))) |
| 82 |
(when (and (null si:*compiler-constants*) |
| 83 |
(si:need-to-make-load-form-p object)) |
| 84 |
(add-load-form object vv)) |
| 85 |
vv))))) |
| 86 |
(when always |
| 87 |
(setf (vv-always vv) t)) |
| 88 |
(when (or duplicate used-p) |
| 89 |
(setf (vv-used-p vv) t)) |
| 90 |
vv)) |
| 91 |
|
| 92 |
(defun add-fname (fname) |
| 93 |
(check-type fname function-name) |
| 94 |
(when (consp fname) |
| 95 |
(add-object (cadr fname) :duplicate nil :permanent t)) |
| 96 |
(add-object fname :duplicate nil :permanent t)) |
| 97 |
|
| 98 |
(defun add-symbol (symbol) |
| 99 |
(check-type symbol symbol) |
| 100 |
(add-object symbol :duplicate nil :permanent t)) |