forked from g000001/Starlisp-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle-returning-pvar.lisp
228 lines (179 loc) · 7.25 KB
/
handle-returning-pvar.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: (*SIM-I COMMON-LISP-GLOBAL); Base: 10; Muser: yes -*-
(in-package :*sim-i)
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;;>
;;;> The Thinking Machines *Lisp Simulator is in the public domain.
;;;> You are free to do whatever you like with it, including but
;;;> not limited to distributing, modifying, and copying.
;;;>
;;;> *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+
;;; Author: JP Massar.
(defun *map-array (function-of-one-pvar-argument pvar)
(assert (array-pvar-p pvar) () "*map-structure was called, but pvar argument was not an array pvar")
(let ((lisp-array-holding-pvars (pvar-array pvar)))
(with-array-elements-iterated
lisp-array-holding-pvars
element-pvar
(funcall function-of-one-pvar-argument element-pvar)
)))
(defun *map-structure (function-of-one-pvar-argument pvar)
(assert (structure-pvar-p pvar) () "*map-structure was called, but pvar argument was not a structure pvar")
(let* ((front-end-structure (pvar-structure pvar))
(type-name (pvar-structure-name pvar))
(front-end-slot-accessor-names (structure-pvar-type-front-end-slot-accessors type-name))
)
(mapc
#'(lambda (accessor) (funcall function-of-one-pvar-argument (funcall accessor front-end-structure)))
front-end-slot-accessor-names
)
))
(defun clean-up-stack (start end)
(do ((pvar-list start (cdr pvar-list)))
((eq pvar-list end))
(clean-up-stack-pvar (car pvar-list))
))
(defun clean-up-stack-pvar (pvar)
(declare (type pvar pvar))
(let ((class (pvar-class pvar)))
(ecase class
(:general
(let ((array (pvar-array pvar)))
(if array
(progn
(return-pvar-array-to-pool array)
(setf (pvar-array pvar) nil)
)
(error "Internal error. Allocated general pvar on temporary list does not have pvar array")
)))
(:array
(*map-array #'(lambda (element-pvar) (*deallocate-internal element-pvar :allocate!!)) pvar)
(setf (pvar-array pvar) nil)
)
(:structure
(*map-structure #'(lambda (slot-pvar) (*deallocate-internal slot-pvar :allocate!!)) pvar)
(setf (pvar-structure pvar) nil)
))))
;;;
;;; This function determines whether the user was returning
;;; a temporary pvar. If the return-value is not a pvar, then
;;; we set the *TEMP-PVAR-LIST* back to its previous value and
;;; return the return-value. If the return-value is in fact
;;; a temporary pvar, then we must arrange to return it.
;;;
;;; This is used by *DEFUN and *LET.
;;;
;;; This function is used EVERYWHERE because it controls the
;;; temporary pvar stack so it should be optimized as hell!
;;;
(defun handle-returning-pvar
(return-value old-*temp-pvar-list* definitely-return-pvar)
(cond
;; definitely-return-pvar is set iff the value being
;; returned is a pvar bound by the *LET or *LET* that
;; is doing this call to handle-returning-pvar.
;; If the pvar is an lvalue, then it was either allocated
;; on the heap or it was allocated by a *LET or *LET*
;; which has not yet returned. Hence the pvar is not
;; to be regarded as temporary.
;; If the pvar has the property :heap-constant, then it
;; was created by !! as a temporary but it was put into
;; one of the constant hash tables and the actual pvar
;; and its array were allocated on the heap. Hence it
;; is not to be regarded as temporary.
((or definitely-return-pvar
(and return-value
(pvar-p return-value)
(not (pvar-lvalue? (the pvar return-value)))
(not (eq :heap-constant (pvar-constant? (the pvar return-value))))
))
; (print 'in-here-0)
;; the user is returning a temporary pvar.
;; Lets trade it with the first thing in the OLD-*TEMP-PVAR-LIST*
(cond
;; in the special case where the user happens
;;to be returning the first pvar in OLD-*TEMP-PVAR-LIST*,
;; we don't have to do anything at all except make
;; sure what we return has been converted into
;; a temporary pvar (since *LET variables are
;; allocated off the *TEMP-PVAR-LIST* but are not
;; marked as temporary) and then clean up what
;; is above us on the stack.
((eq return-value (first old-*temp-pvar-list*))
; (print 'in-here-1)
(setf (pvar-lvalue? (the pvar return-value)) nil)
(clean-up-stack (cdr old-*temp-pvar-list*) *temp-pvar-list*)
)
(t
;; Some random temporary pvar somewhere above where
;; we are returning to on the stack is being returned.
;; We are simply going to swap the contents of the
;; pvar being returned with the pvar at the place on
;; the stack we want to return to, then clean up the
;; stack.
; (print 'in-here-2)
(let* ((return-pvar (first old-*temp-pvar-list*))
(old-return-pvar-array (pvar-array return-pvar))
(old-return-pvar-type (pvar-type return-pvar))
(old-return-pvar-structure-name (pvar-structure-name return-pvar))
)
; (print (list 'return-pvar return-pvar))
; (print (list 'old-return-pvar-array old-return-pvar-array))
;; move pvar data to the one we are going
;; to return from the one the user passed back
;; Swap the data arrays.
(copy-pvar-slots return-pvar return-value)
(setf (pvar-array return-value) old-return-pvar-array)
(setf (pvar-type return-value) old-return-pvar-type)
(if (eq :structure old-return-pvar-type)
(setf (pvar-structure-name return-value) old-return-pvar-structure-name)
)
(setf (pvar-lvalue? return-pvar) nil)
(setq return-value return-pvar)
(clean-up-stack (cdr old-*temp-pvar-list*) *temp-pvar-list*)
))
)
;; set the *TEMP-PVAR-LIST* to the element after the one we are returning
(setq *temp-pvar-list* (cdr old-*temp-pvar-list*))
)
;; If we weren't returning a PVAR, then just 'pop' the stack
;; and 'free up' any temporary pvars that were allocated
;; while the form which this function wraps around was evaluated.
(T
(clean-up-stack old-*temp-pvar-list* *temp-pvar-list*)
(setq *temp-pvar-list* old-*temp-pvar-list*)
)
)
; (when *fuck*
; (print 'leaving)
; (dotimes (j 5) (print (nth j *temp-pvar-original-list*)))
; )
return-value
)
(defun *deallocate-internal (pvar type)
(ecase (pvar-class pvar)
(:general
(setf (vp-set-heap-pvar-arrays (pvar-vp-set pvar))
(cons (pvar-location pvar) (vp-set-heap-pvar-arrays (pvar-vp-set pvar)))
))
(:array (*map-array #'(lambda (element-pvar) (*deallocate-internal element-pvar :allocate!!)) pvar))
(:structure (*map-structure #'(lambda (slot-pvar) (*deallocate-internal slot-pvar :allocate!!)) pvar))
)
(smash-deallocated-pvar pvar)
(ecase type
(:allocate!! (setq *all-allocate!!-pvars* (delete pvar *all-allocate!!-pvars* :test #'eq)))
(:*defvar nil)
)
)
(defvar smashed-location "THIS PVAR HAS BEEN DEALLOCATED. YOU SHOULD NOT BE REFERENCING IT")
(defun set-pvar-length (pvar value)
#+*LISP-HARDWARE
(setf (pvar-length pvar) value)
#+*LISP-SIMULATOR
(declare (ignore pvar))
value
)
(defun smash-deallocated-pvar (pvar)
;; smash the pvar so that it doesn't get reused.
(set-pvar-length pvar 0)
(setf (pvar-type pvar) :general)
(setf (pvar-location pvar) smashed-location))