-
Notifications
You must be signed in to change notification settings - Fork 8
/
common-vm.lisp
362 lines (302 loc) · 11.7 KB
/
common-vm.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(defpackage :vm
(:use :cl :sb-ext :sb-vm)
(:export :copy-mem
:primobj-or-lose
:all-primitive-objects
:sap-ref-lispobj
:lispobj-sap
:lispobj-ref-slot
:real-widetag-of
:tag-case
:with-primobj-data
:dump-object-fasl
:funs-in-fun
:make-env-list
:lexenv-p
:make-null-lexenv
:copy-lexenv
:extend-lexenv
:get-env
:set-env))
(in-package :vm)
(defparameter *reexport*
'(sb-sys:sap-ref-word sb-sys:sap-ref-16 sb-sys:sap-ref-32
sb-sys:sap-ref-8 sb-sys:sap-ref-octets sb-sys:int-sap
sb-vm:n-word-bits sb-kernel:get-lisp-obj-address
sb-sys:vector-sap sb-sys:vector-sap sb-sys:with-pinned-objects
sb-vm:lowtag-mask sb-vm:primitive-object-name
sb-vm:widetag-mask sb-vm:n-widetag-bits sb-kernel:lowtag-of
sb-vm:primitive-object-lowtag sb-vm:primitive-object-widetag
sb-vm:primitive-object-variable-length-p
sb-vm:primitive-object-size sb-vm:primitive-object-slots ))
(import *reexport*)
(export *reexport*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SAP Transfers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(declaim (inline copy-mem))
(defun copy-mem (from to length)
(loop for i below length by sb-vm:n-word-bytes
do (setf (sb-sys:sap-ref-word to i)
(sb-sys:sap-ref-word from i))))
#+()
(defun count-heap-instances (class)
(let ((stat (make-object-type-stat :type (class-name class))))
(sb-vm::map-allocated-objects (lambda (object type-code size)
(when (and (= type-code 82)
(eq (class-of object) class))
(incf (object-type-stat-count stat))
(incf (object-type-stat-total-size stat) size)))
:dynamic)
stat))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Introspection
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun listify (x)
(if (listp x)
x
(list x)))
(defun all-primitive-objects ()
sb-vm:*primitive-objects*)
(defun all-primitive-object-names ()
(mapcar #'sb-vm:primitive-object-name sb-vm:*primitive-objects*))
;; (SB-VM::THREAD SB-VM::COMPLEX-DOUBLE-FLOAT SB-VM::COMPLEX-SINGLE-FLOAT SYMBOL
;; SB-C:CATCH-BLOCK SB-VM::UNWIND-BLOCK SB-VM::BINDING SB-EXT:WEAK-POINTER
;; SB-VM::SAP SB-VM::VALUE-CELL SB-KERNEL:FUNCALLABLE-INSTANCE SB-KERNEL:CLOSURE
;; SB-VM::RETURN-PC SB-KERNEL:SIMPLE-FUN SB-KERNEL:FDEFN SB-VM::CODE VECTOR ARRAY
;; COMPLEX DOUBLE-FLOAT RATIO BIGNUM SB-KERNEL:INSTANCE CONS))
(defun primobj-or-lose (name &key (test #'eq))
(or (find name
sb-vm:*primitive-objects*
:key #'sb-vm:primitive-object-name
:test test)
(error "Unknown primitive object type: ~A" name)))
(declaim (inline sap-ref-lispobj))
(defun sap-ref-lispobj (sap &optional (offset 0))
(declare (type sb-sys:system-area-pointer sap)
(type (signed-byte #.sb-vm:n-word-bits) offset))
(sb-kernel:%make-lisp-obj (sb-sys:sap-ref-word sap offset)))
(declaim (inline lispobj-sap))
(defun lispobj-sap (x)
(sb-sys:int-sap (sb-kernel:get-lisp-obj-address x)))
(declaim (inline lispobj-ref-slot))
(defun lispobj-ref-slot (x slot)
(declare (type (and unsigned-byte fixnum) slot))
(let* ((aligned-addr (logandc2 (sb-kernel:get-lisp-obj-address x)
sb-vm:lowtag-mask)))
(sap-ref-lispobj (sb-sys:int-sap aligned-addr)
(* sb-vm:n-word-bytes slot))))
;; (lispobj-sap *features*)
;; (lispobj-sap (make-hash-table ))
(declaim (inline real-widetag-of))
(defun real-widetag-of (x)
(let* ((addr (sb-kernel:get-lisp-obj-address x))
(lowtag (logand addr sb-vm:lowtag-mask))
(header (sb-sys:sap-ref-word (sb-sys:int-sap addr)
(- lowtag))))
(values (logand header sb-vm:widetag-mask)
(ash header (- sb-vm:n-widetag-bits)))))
(defmacro %widetag-case ((object &optional default-op) &body specs)
(let ((cases (make-hash-table)))
(loop for (type . spec) in (mapcar 'listify specs)
for case = (typecase type
(cons (cdr type))
(t (symbol-value
(sb-vm:primitive-object-widetag
(primobj-or-lose type :test #'string=)))))
do (let ((case (if (member case '(t nil otherwise))
t
case)))
(assert (null (gethash case cases)))
(setf (gethash case cases)
(or spec `((,default-op ,type ,object))))))
`(,(if (gethash t cases) 'case 'ecase) (real-widetag-of ,object)
,@(sort (loop
for tag being the hash-key in cases
using (hash-value spec)
collect `(,tag ,@spec))
(lambda (x y)
(if (and (numberp x)
(numberp y))
(< x y)
(numberp x)))
:key #'car))))
(defmacro tag-case ((object &optional default-op) &body specs)
(let ((cases (make-hash-table))
(default nil)
(_obj (gensym "OBJ")))
(loop for spec in (mapcar 'listify specs)
for type = (first spec)
for case = (typecase type
((member t otherwise) type)
(cons (car type))
(t (symbol-value
(sb-vm:primitive-object-lowtag
(primobj-or-lose type :test #'string=)))))
do (cond ((member case '(t otherwise nil))
(assert (null default))
(setf default (rest spec)))
(t (push spec (gethash case cases)))))
`(let ((,_obj ,object))
(,(if default 'case 'ecase) (sb-kernel:lowtag-of ,_obj)
,@(sort (loop
for tag being the hash-key in cases
using (hash-value specs)
collect `(,tag (%widetag-case (,_obj ,default-op)
,@(nreverse specs))))
#'< :key #'first)
,@(when default
`((t ,@default)))))))
(defmacro with-primobj-data ((name obj &key data length)
&body body)
(let* ((objdef (primobj-or-lose name :test #'string=))
(var-length-p (sb-vm:primitive-object-variable-length-p objdef))
(_obj (gensym "OBJ")))
(when (and var-length-p (not data))
(setf data (gensym "DATA")))
`(let* ((,_obj ,obj)
,@(when data
`((,data (nth-value 1 (real-widetag-of ,_obj)))))
,@(when length
`((,length ,(if var-length-p
`(1+ ,data)
(sb-vm:primitive-object-size objdef))))))
(declare (ignorable ,_obj))
,@body)))
(defmacro with-fasl-vector-output ((out) &body thing)
`(let ((sb-fasl::*LOAD-CODE-VERBOSE* t))
(flex:with-output-to-sequence (,out)
(sb-fasl::dump-object ,@thing
(sb-fasl::make-fasl-output :stream ,out)))))
;; (with-fasl-vector-output (s)
;; "string")
;; (with-fasl-vector-output (s)
;; :keyword)
(defun dump-object-fasl (object)
(with-fasl-vector-output (o)
object))
;;(sb-vm:print-allocated-objects :static)
;;(sb-vm:print-allocated-objects :dynamic)
(defun lexenv-p (obj)
(sb-c::lexenv-p obj))
(defun make-null-lexenv ()
(sb-c::make-null-lexenv))
(defun copy-lexenv (lexenv)
(sb-c::make-lexenv :default lexenv))
(defun extend-lexenv (lexenv &key funs vars)
(sb-c::make-lexenv :default lexenv
:vars vars
:funs funs))
(defmacro get-env (&environment env)
`(quote ,(list (copy-list (sb-c::lexenv-vars env))
(copy-list (sb-c::lexenv-funs env)))))
(defmacro set-env (var &environment env)
(set var (list (copy-list (sb-c::lexenv-vars env))
(copy-list (sb-c::lexenv-funs env))))
nil)
(defun funs-in-fun (fun)
(sb-c::lexenv-funs (sb-c::functional-lexenv (cdr fun))))
(defun special-varp (var)
(etypecase var
(sb-c::lambda-var
(sb-c::lambda-var-specvar var))
(sb-c::global-var
(eq :special (sb-c::global-var-kind var)))))
(defpackage "S"
(:use )
(:export #:d ; dyn-bind
#:c ; catch
#:f ; closure (function)
#:_ ; ignored value
))
(defmacro %%make-env-list (ignore-unused-p positions
&environment env)
(let* ((vars (sb-c::lexenv-vars env))
(tmps (mapcar 'rest
(sort (loop
for position in (reverse positions)
for (name . var) in (reverse vars)
when position
collect (list* position
(gensym (symbol-name name))
var))
'< :key 'first))))
(setf (sb-c::lexenv-vars env)
(append tmps
vars))
`(list ,@(mapcar (if ignore-unused-p
(lambda (tmp)
(if (sb-c::lambda-var-refs (cdr tmp))
(car tmp)
''s:_))
'car)
tmps))))
(defmacro make-env-list ((&key ignore-unused) &rest positions)
(let ((x (gensym "RANDOM-BINDING")))
`(let (,x) ;; needed to make sure we get a fresh lexenv object
(declare (ignore ,x)) ;; just for the expression
(%%make-env-list ,ignore-unused ,positions))))
;; (primobj-or-lose 'sb-vm::binding)
;; #S(SB-VM:PRIMITIVE-OBJECT
;; :NAME SB-VM::BINDING
;; :WIDETAG NIL
;; :LOWTAG NIL
;; :OPTIONS NIL
;; :SLOTS (#S(SB-VM::PRIM-OBJECT-SLOT
;; :NAME SB-VM::VALUE
;; :DOCS NIL
;; :REST-P NIL
;; :OFFSET 0
;; :OPTIONS NIL)
;; #S(SB-VM::PRIM-OBJECT-SLOT
;; :NAME SYMBOL
;; :DOCS NIL
;; :REST-P NIL
;; :OFFSET 1
;; :OPTIONS NIL))
;; :SIZE 2
;; :VARIABLE-LENGTH-P NIL)
;; (primobj-or-lose 'cons)
;; #S(SB-VM:PRIMITIVE-OBJECT
;; :NAME CONS
;; :WIDETAG NIL
;; :LOWTAG SB-VM:LIST-POINTER-LOWTAG
;; :OPTIONS NIL
;; :SLOTS (#S(SB-VM::PRIM-OBJECT-SLOT
;; :NAME CAR
;; :DOCS NIL
;; :REST-P NIL
;; :OFFSET 0
;; :OPTIONS (:REF-TRANS CAR :SET-TRANS SB-KERNEL:%RPLACA :INIT :ARG
;; :CAS-TRANS SB-KERNEL:%COMPARE-AND-SWAP-CAR))
;; #S(SB-VM::PRIM-OBJECT-SLOT
;; :NAME CDR
;; :DOCS NIL
;; :REST-P NIL
;; :OFFSET 1
;; :OPTIONS (:REF-TRANS CDR :SET-TRANS SB-KERNEL:%RPLACD :INIT :ARG
;; :CAS-TRANS SB-KERNEL:%COMPARE-AND-SWAP-CDR)))
;; :SIZE 2
;; :VARIABLE-LENGTH-P NIL)
;; (primobj-or-lose 'sb-kernel:closure)
;; #S(SB-VM:PRIMITIVE-OBJECT
;; :NAME SB-KERNEL:CLOSURE
;; :WIDETAG SB-VM:CLOSURE-HEADER-WIDETAG
;; :LOWTAG SB-VM:FUN-POINTER-LOWTAG
;; :OPTIONS NIL
;; :SLOTS (#S(SB-VM::PRIM-OBJECT-SLOT
;; :NAME SB-VM::FUN
;; :DOCS NIL
;; :REST-P NIL
;; :OFFSET 1
;; :OPTIONS (:INIT :ARG :REF-TRANS SB-KERNEL:%CLOSURE-FUN))
;; #S(SB-VM::PRIM-OBJECT-SLOT
;; :NAME SB-INT:INFO
;; :DOCS NIL
;; :REST-P T
;; :OFFSET 2
;; :OPTIONS NIL))
;; :SIZE 2
;; :VARIABLE-LENGTH-P T)