cell.lisp 13.7 KB
Newer Older
ram's avatar
ram committed
1
2
3
4
5
6
7
8
9
;;; -*- Mode: LISP; Syntax: Common-Lisp; Base: 10; Package: x86 -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of CMU Common Lisp, please contact
;;; Scott Fahlman or slisp-group@cs.cmu.edu.
;;;
(ext:file-comment
10
 "$Header: src/compiler/x86/cell.lisp $")
ram's avatar
ram committed
11
12
13
14
15
16
17
18
19
;;;
;;; **********************************************************************
;;;
;;;    This file contains the VM definition of various primitive memory access
;;; VOPs for the x86.
;;;
;;; Written by William Lott.
;;;
;;; Debugged by Paul F. Werkowski Spring/Summer 1995.
20
;;; Enhancements/debugging by Douglas T. Crosher 1996,1997,1999.
ram's avatar
ram committed
21
22
23
;;; 

(in-package :x86)
24
(intl:textdomain "cmucl-x86-vm")
ram's avatar
ram committed
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


;;;; Data object ref/set stuff.

(define-vop (slot)
  (:args (object :scs (descriptor-reg)))
  (:info name offset lowtag)
  (:ignore name)
  (:results (result :scs (descriptor-reg any-reg)))
  (:generator 1
    (loadw result object offset lowtag)))

(define-vop (set-slot)
  (:args (object :scs (descriptor-reg))
	 (value :scs (descriptor-reg any-reg immediate)))
  (:info name offset lowtag)
  (:ignore name)
  (:results)
  (:generator 1
     (if (sc-is value immediate)
        (let ((val (tn-value value)))
           (etypecase val
              (integer
	       (inst mov
		     (make-ea :dword :base object
			      :disp (- (* offset word-bytes) lowtag))
51
		     (fixnumize val)))
ram's avatar
ram committed
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
	      (symbol
	       (inst mov
		     (make-ea :dword :base object
			      :disp (- (* offset word-bytes) lowtag))
		     (+ nil-value (static-symbol-offset val))))
	      (character
	       (inst mov
		     (make-ea :dword :base object
			      :disp (- (* offset word-bytes) lowtag))
		     (logior (ash (char-code val) type-bits)
			     base-char-type)))))
       ;; Else, value not immediate.
       (storew value object offset lowtag))))



;;;; Symbol hacking VOPs:

;;; these next two cf the sparc version, by jrd.

;;; The compiler likes to be able to directly SET symbols.
;;;
(define-vop (set cell-set)
  (:variant symbol-value-slot other-pointer-type))

;;; Do a cell ref with an error check for being unbound.
;;;
(define-vop (checked-cell-ref)
  (:args (object :scs (descriptor-reg) :target obj-temp))
  (:results (value :scs (descriptor-reg any-reg)))
  (:policy :fast-safe)
  (:vop-var vop)
  (:save-p :compute-only)
  (:temporary (:sc descriptor-reg :from (:argument 0)) obj-temp))



;;; With Symbol-Value, we check that the value isn't the trap object.  So
;;; Symbol-Value of NIL is NIL.
;;;
(define-vop (symbol-value)
  (:translate symbol-value)
  (:policy :fast-safe)
  (:args (object :scs (descriptor-reg) :to (:result 1)))
  (:results (value :scs (descriptor-reg any-reg)))
  (:vop-var vop)
  (:save-p :compute-only)
  (:generator 9
    (let ((err-lab (generate-error-code vop unbound-symbol-error object)))
      (loadw value object symbol-value-slot other-pointer-type)
      (inst cmp value unbound-marker-type)
      (inst jmp :e err-lab))))

(define-vop (fast-symbol-value cell-ref)
  (:variant symbol-value-slot other-pointer-type)
  (:policy :fast)
  (:translate symbol-value))

110
111
112
113
114
115
116
117
(export 'kernel::set-symbol-value-conditional "KERNEL")
(defknown kernel::set-symbol-value-conditional (symbol t t) t (unsafe))

(define-vop (set-symbol-value-conditional cell-set-conditional)
  (:translate kernel::set-symbol-value-conditional)
  (:variant symbol-value-slot other-pointer-type)
  (:policy :fast-safe))

118
119
120
121
122
123
(defknown fast-symbol-value-xadd (symbol fixnum) fixnum ())
(define-vop (fast-symbol-value-xadd cell-xadd)
  (:variant symbol-value-slot other-pointer-type)
  (:policy :fast)
  (:translate fast-symbol-value-xadd)
  (:arg-types * tagged-num))
ram's avatar
ram committed
124
125
126
127
128
129
130
131
132
133
134
135
136

(define-vop (boundp)
  (:translate boundp)
  (:policy :fast-safe)
  (:args (object :scs (descriptor-reg)))
  (:conditional)
  (:info target not-p)
  (:temporary (:sc descriptor-reg :from (:argument 0)) value)
  (:generator 9
    (loadw value object symbol-value-slot other-pointer-type)
    (inst cmp value unbound-marker-type)
    (inst jmp (if not-p :e :ne) target)))

137
138
139
140
141
(define-vop (symbol-hash)
  (:policy :fast-safe)
  (:translate symbol-hash)
  (:args (symbol :scs (descriptor-reg)))
  (:results (res :scs (any-reg)))
142
  (:result-types tagged-num)
143
144
145
146
147
148
149
  (:generator 2
    ;; the symbol-hash slot of NIL holds NIL because it is also the cdr
    ;; slot, so we have to strip off the two low bits to make sure it is
    ;; a fixnum.
    (loadw res symbol symbol-hash-slot other-pointer-type)
    (inst and res (lognot #b11))))

150
151
152
153
154

(define-vop (%set-symbol-hash cell-set)
  (:translate %set-symbol-hash)
  (:variant symbol-hash-slot other-pointer-type))

ram's avatar
ram committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

;;;; Fdefinition (fdefn) objects.

(define-vop (fdefn-function cell-ref)	; /pfw - alpha
  (:variant fdefn-function-slot other-pointer-type))

(define-vop (safe-fdefn-function)
  (:args (object :scs (descriptor-reg) :to (:result 1)))
  (:results (value :scs (descriptor-reg any-reg)))
  (:vop-var vop)
  (:save-p :compute-only)
  (:generator 10
    (loadw value object fdefn-function-slot other-pointer-type)
    (inst cmp value nil-value)
    (let ((err-lab (generate-error-code vop undefined-symbol-error object)))
      (inst jmp :e err-lab))))

172
#+nil
ram's avatar
ram committed
173
174
175
176
177
(define-vop (set-fdefn-function)
  (:policy :fast-safe)
  (:translate (setf fdefn-function))
  (:args (function :scs (descriptor-reg) :target result)
	 (fdefn :scs (descriptor-reg)))
178
  (:temporary (:sc unsigned-reg) raw)
ram's avatar
ram committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  (:temporary (:sc byte-reg) type)
  (:results (result :scs (descriptor-reg)))
  (:generator 38
    (load-type type function (- function-pointer-type))
    (inst lea raw
	  (make-ea :byte :base function
		   :disp (- (* function-code-offset word-bytes)
			    function-pointer-type)))
    (inst cmp type function-header-type)
    (inst jmp :e normal-fn)
    (inst lea raw (make-fixup (extern-alien-name "closure_tramp") :foreign))
    NORMAL-FN
    (storew function fdefn fdefn-function-slot other-pointer-type)
    (storew raw fdefn fdefn-raw-addr-slot other-pointer-type)
    (move result function)))

195
196
197
198
199
200
(define-vop (set-fdefn-function)
  (:policy :fast-safe)
  (:translate (setf fdefn-function))
  (:args (function :scs (descriptor-reg) :target result)
	 (fdefn :scs (descriptor-reg)))
  (:temporary (:sc unsigned-reg) raw)
201
  (:temporary (:sc unsigned-reg) type)
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  (:results (result :scs (descriptor-reg)))
  (:generator 38
    (load-type type function (- function-pointer-type))
    (inst lea raw
	  (make-ea :byte :base function
		   :disp (- (* function-code-offset word-bytes)
			    function-pointer-type)))
    (inst cmp type function-header-type)
    (inst jmp :e normal-fn)
    (inst lea raw (make-fixup 'closure-tramp :assembly-routine))
    NORMAL-FN
    (storew function fdefn fdefn-function-slot other-pointer-type)
    (storew raw fdefn fdefn-raw-addr-slot other-pointer-type)
    (move result function)))

#+nil
ram's avatar
ram committed
218
219
220
221
222
223
224
225
226
227
228
(define-vop (fdefn-makunbound)
  (:policy :fast-safe)
  (:translate fdefn-makunbound)
  (:args (fdefn :scs (descriptor-reg) :target result))
  (:results (result :scs (descriptor-reg)))
  (:generator 38
    (storew nil-value fdefn fdefn-function-slot other-pointer-type)
    (storew (make-fixup (extern-alien-name "undefined_tramp") :foreign)
	    fdefn fdefn-raw-addr-slot other-pointer-type)
    (move result fdefn)))

229
230
231
232
233
234
235
236
237
238
239
(define-vop (fdefn-makunbound)
  (:policy :fast-safe)
  (:translate fdefn-makunbound)
  (:args (fdefn :scs (descriptor-reg) :target result))
  (:results (result :scs (descriptor-reg)))
  (:generator 38
    (storew nil-value fdefn fdefn-function-slot other-pointer-type)
    (storew (make-fixup 'undefined-tramp :assembly-routine)
	    fdefn fdefn-raw-addr-slot other-pointer-type)
    (move result fdefn)))

ram's avatar
ram committed
240
241
242
243
244
245
246
247
248
249
250


;;;; Binding and Unbinding.

;;; BIND -- Establish VAL as a binding for SYMBOL.  Save the old value and
;;; the symbol on the binding stack and stuff the new value into the
;;; symbol.

(define-vop (bind)
  (:args (val :scs (any-reg descriptor-reg))
	 (symbol :scs (descriptor-reg)))
251
252
  (:temporary (:sc descriptor-reg) temp)
  (:temporary (:sc any-reg) bsp)
ram's avatar
ram committed
253
  (:generator 5
254
    (load-symbol-value bsp *binding-stack-pointer*)
ram's avatar
ram committed
255
256
    (loadw temp symbol symbol-value-slot other-pointer-type)
    (inst add bsp (* binding-size word-bytes))
257
    (store-symbol-value bsp *binding-stack-pointer*)
ram's avatar
ram committed
258
259
260
261
262
    (storew temp bsp (- binding-value-slot binding-size))
    (storew symbol bsp (- binding-symbol-slot binding-size))
    (storew val symbol symbol-value-slot other-pointer-type)))

(define-vop (unbind)
263
264
  (:temporary (:sc descriptor-reg) symbol value)
  (:temporary (:sc any-reg) bsp)
ram's avatar
ram committed
265
  (:generator 0
266
    (load-symbol-value bsp *binding-stack-pointer*)
ram's avatar
ram committed
267
268
269
270
271
    (loadw symbol bsp (- binding-symbol-slot binding-size))
    (loadw value bsp (- binding-value-slot binding-size))
    (storew value symbol symbol-value-slot other-pointer-type)
    (storew 0 bsp (- binding-symbol-slot binding-size))
    (inst sub bsp (* binding-size word-bytes))
272
    (store-symbol-value bsp *binding-stack-pointer*)))
ram's avatar
ram committed
273
274
275
276


(define-vop (unbind-to-here)
  (:args (where :scs (descriptor-reg any-reg)))
277
278
  (:temporary (:sc descriptor-reg) symbol value)
  (:temporary (:sc any-reg) bsp)
ram's avatar
ram committed
279
  (:generator 0
280
    (load-symbol-value bsp *binding-stack-pointer*)
ram's avatar
ram committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    (inst cmp where bsp)
    (inst jmp :e done)

    LOOP
    (loadw symbol bsp (- binding-symbol-slot binding-size))
    (inst or symbol symbol)
    (inst jmp :z skip)
    (loadw value bsp (- binding-value-slot binding-size))
    (storew value symbol symbol-value-slot other-pointer-type)
    (storew 0 bsp (- binding-symbol-slot binding-size))

    SKIP
    (inst sub bsp (* binding-size word-bytes))
    (inst cmp where bsp)
    (inst jmp :ne loop)
296
    (store-symbol-value bsp *binding-stack-pointer*)
ram's avatar
ram committed
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

    DONE))



;;;; Closure indexing.

(define-full-reffer closure-index-ref *
  closure-info-offset function-pointer-type
  (any-reg descriptor-reg) * %closure-index-ref)

(define-full-setter set-funcallable-instance-info *
  funcallable-instance-info-offset function-pointer-type
  (any-reg descriptor-reg) * %set-funcallable-instance-info)

(define-full-reffer funcallable-instance-info *
  funcallable-instance-info-offset function-pointer-type
  (descriptor-reg any-reg) * %funcallable-instance-info)

(define-vop (funcallable-instance-lexenv cell-ref)
  (:variant funcallable-instance-lexenv-slot function-pointer-type))

(define-vop (closure-ref slot-ref)
  (:variant closure-info-offset function-pointer-type))

(define-vop (closure-init slot-set)
  (:variant closure-info-offset function-pointer-type))

325

ram's avatar
ram committed
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
363
364
365

;;;; Value Cell hackery.

(define-vop (value-cell-ref cell-ref)
  (:variant value-cell-value-slot other-pointer-type))

(define-vop (value-cell-set cell-set)
  (:variant value-cell-value-slot other-pointer-type))


;;;; Structure hackery:

(define-vop (instance-length)
  (:policy :fast-safe)
  (:translate %instance-length)
  (:args (struct :scs (descriptor-reg)))
  (:results (res :scs (unsigned-reg)))
  (:result-types positive-fixnum)
  (:generator 4
    (loadw res struct 0 instance-pointer-type)
    (inst shr res type-bits)))

(define-vop (instance-ref slot-ref)
  (:variant instance-slots-offset instance-pointer-type)
  (:policy :fast-safe)
  (:translate %instance-ref)
  (:arg-types instance (:constant index)))

(define-vop (instance-set slot-set)
  (:policy :fast-safe)
  (:translate %instance-set)
  (:variant instance-slots-offset instance-pointer-type)
  (:arg-types instance (:constant index) *))

(define-full-reffer instance-index-ref * instance-slots-offset
  instance-pointer-type (any-reg descriptor-reg) * %instance-ref)

(define-full-setter instance-index-set * instance-slots-offset
  instance-pointer-type (any-reg descriptor-reg) * %instance-set)

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
(export 'kernel::%instance-set-conditional "KERNEL")
(defknown kernel::%instance-set-conditional (instance index t t) t
  (unsafe))

(define-vop (instance-set-conditional-c slot-set-conditional)
  (:policy :fast-safe)
  (:translate kernel::%instance-set-conditional)
  (:variant instance-slots-offset instance-pointer-type)
  (:arg-types instance (:constant index) * *))

(define-vop (instance-set-conditional)
  (:translate kernel::%instance-set-conditional)
  (:args (object :scs (descriptor-reg) :to :eval)
	 (slot :scs (any-reg) :to :result)
	 (old-value :scs (descriptor-reg any-reg) :target eax)
381
	 (new-value :scs (descriptor-reg any-reg)))
382
383
  (:arg-types instance positive-fixnum * *)
  (:temporary (:sc descriptor-reg :offset eax-offset
384
		   :from (:argument 2) :to :result :target result)  eax)
385
  (:results (result :scs (descriptor-reg any-reg)))
386
387
388
  (:policy :fast-safe)
  (:generator 5
    (move eax old-value)
389
    (inst cmpxchg (make-ea :dword :base object :index slot :scale 1
390
391
			   :disp (- (* instance-slots-offset word-bytes)
				    instance-pointer-type))
392
	  new-value)
393
394
    (move result eax)))

395
396
397
398
399
400
(defknown %instance-xadd (instance index fixnum) fixnum ())
(define-vop (instance-xadd-c slot-xadd)
  (:policy :fast-safe)
  (:translate %instance-xadd)
  (:variant instance-slots-offset instance-pointer-type)
  (:arg-types instance (:constant index) tagged-num))
ram's avatar
ram committed
401
402
403
404
405
406
407
408
409
410


;;;; Code object frobbing.

(define-full-reffer code-header-ref * 0 other-pointer-type
  (any-reg descriptor-reg) * code-header-ref)

(define-full-setter code-header-set * 0 other-pointer-type
  (any-reg descriptor-reg) * code-header-set)

411
412
413
414
415
416
417
418
419
420
421
422

;;;; Cons conditional setters.

(export 'kernel::rplaca-conditional "KERNEL")
(defknown kernel::rplaca-conditional (cons t t) t
  (unsafe))

(define-vop (rplaca-conditional cell-set-conditional)
  (:policy :fast-safe)
  (:translate kernel::rplaca-conditional)
  (:variant cons-car-slot list-pointer-type)
  (:arg-types list * *))
ram's avatar
ram committed
423

424
425
426
(export 'kernel::rplacd-conditional "KERNEL")
(defknown kernel::rplacd-conditional (cons t t) t
  (unsafe))
ram's avatar
ram committed
427

428
429
430
431
432
(define-vop (rplacd-conditional cell-set-conditional)
  (:policy :fast-safe)
  (:translate kernel::rplacd-conditional)
  (:variant cons-cdr-slot list-pointer-type)
  (:arg-types list * *))