Thou shalt not MAKE-OTHER-IMMEDIATE-TYPE.
[sbcl.git] / src / compiler / x86-64 / system.lisp
1 ;;;; x86 VM definitions of various system hacking operations
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; type frobbing VOPs
15
16 (define-vop (lowtag-of)
17   (:translate lowtag-of)
18   (:policy :fast-safe)
19   (:args (object :scs (any-reg descriptor-reg control-stack)
20                  :target result))
21   (:results (result :scs (unsigned-reg)))
22   (:result-types positive-fixnum)
23   (:generator 1
24     (move result object)
25     (inst and result lowtag-mask)))
26
27 (define-vop (widetag-of)
28   (:translate widetag-of)
29   (:policy :fast-safe)
30   (:args (object :scs (descriptor-reg)))
31   (:temporary (:sc unsigned-reg :offset rax-offset :target result
32                    :to (:result 0)) rax)
33   (:results (result :scs (unsigned-reg)))
34   (:result-types positive-fixnum)
35   (:generator 6
36     (inst movzx rax (reg-in-size object :byte))
37     (inst and al-tn lowtag-mask)
38     (inst cmp al-tn other-pointer-lowtag)
39     (inst jmp :e OTHER-PTR)
40     (inst cmp al-tn fun-pointer-lowtag)
41     (inst jmp :e FUNCTION-PTR)
42
43     ;; Pick off fixnums.
44     (inst test al-tn fixnum-tag-mask)
45     (inst jmp :e DONE)
46
47     ;; Pick off structures and list pointers.
48     (inst test al-tn 2)
49     (inst jmp :ne DONE)
50
51     ;; must be an other immediate
52     (inst movzx rax (reg-in-size object :byte))
53     (inst jmp DONE)
54
55     FUNCTION-PTR
56     (load-type rax object (- fun-pointer-lowtag))
57     (inst jmp DONE)
58
59     OTHER-PTR
60     (load-type rax object (- other-pointer-lowtag))
61
62     DONE
63     (move result rax)))
64 \f
65 (define-vop (fun-subtype)
66   (:translate fun-subtype)
67   (:policy :fast-safe)
68   (:args (function :scs (descriptor-reg)))
69   (:results (result :scs (unsigned-reg)))
70   (:result-types positive-fixnum)
71   (:generator 6
72     (load-type result function (- fun-pointer-lowtag))))
73
74 (define-vop (set-fun-subtype)
75   (:translate (setf fun-subtype))
76   (:policy :fast-safe)
77   (:args (type :scs (unsigned-reg) :target eax)
78          (function :scs (descriptor-reg)))
79   (:arg-types positive-fixnum *)
80   (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 0)
81                    :to (:result 0) :target result)
82               eax)
83   (:results (result :scs (unsigned-reg)))
84   (:result-types positive-fixnum)
85   (:generator 6
86     (move eax type)
87     (inst mov
88           (make-ea :byte :base function :disp (- fun-pointer-lowtag))
89           al-tn)
90     (move result eax)))
91
92 (define-vop (get-header-data)
93   (:translate get-header-data)
94   (:policy :fast-safe)
95   (:args (x :scs (descriptor-reg)))
96   (:results (res :scs (unsigned-reg)))
97   (:result-types positive-fixnum)
98   (:generator 6
99     (loadw res x 0 other-pointer-lowtag)
100     (inst shr res n-widetag-bits)))
101
102 (define-vop (get-closure-length)
103   (:translate get-closure-length)
104   (:policy :fast-safe)
105   (:args (x :scs (descriptor-reg)))
106   (:results (res :scs (unsigned-reg)))
107   (:result-types positive-fixnum)
108   (:generator 6
109     (loadw res x 0 fun-pointer-lowtag)
110     (inst shr res n-widetag-bits)))
111
112 (define-vop (set-header-data)
113   (:translate set-header-data)
114   (:policy :fast-safe)
115   (:args (x :scs (descriptor-reg) :target res :to (:result 0))
116          (data :scs (any-reg) :target eax))
117   (:arg-types * positive-fixnum)
118   (:results (res :scs (descriptor-reg)))
119   (:temporary (:sc unsigned-reg :offset eax-offset
120                    :from (:argument 1) :to (:result 0)) eax)
121   (:generator 6
122     (move eax data)
123     (inst shl eax (- n-widetag-bits n-fixnum-tag-bits))
124     (inst mov al-tn (make-ea :byte :base x :disp (- other-pointer-lowtag)))
125     (storew eax x 0 other-pointer-lowtag)
126     (move res x)))
127 \f
128 (define-vop (pointer-hash)
129   (:translate pointer-hash)
130   (:args (ptr :scs (any-reg descriptor-reg) :target res))
131   (:results (res :scs (any-reg descriptor-reg)))
132   (:policy :fast-safe)
133   (:generator 1
134     (move res ptr)
135     ;; Mask the lowtag, and shift the whole address into a positive
136     ;; fixnum.
137     (inst and res (lognot lowtag-mask))
138     (inst shr res 1)))
139 \f
140 ;;;; allocation
141
142 (define-vop (dynamic-space-free-pointer)
143   (:results (int :scs (sap-reg)))
144   (:result-types system-area-pointer)
145   (:translate dynamic-space-free-pointer)
146   (:policy :fast-safe)
147   (:generator 1
148     (load-symbol-value int *allocation-pointer*)))
149
150 (define-vop (binding-stack-pointer-sap)
151   (:results (int :scs (sap-reg)))
152   (:result-types system-area-pointer)
153   (:translate binding-stack-pointer-sap)
154   (:policy :fast-safe)
155   (:generator 1
156     (load-binding-stack-pointer int)))
157
158 (defknown (setf binding-stack-pointer-sap)
159     (system-area-pointer) system-area-pointer ())
160
161 (define-vop (set-binding-stack-pointer-sap)
162   (:args (new-value :scs (sap-reg) :target int))
163   (:arg-types system-area-pointer)
164   (:results (int :scs (sap-reg)))
165   (:result-types system-area-pointer)
166   (:translate (setf binding-stack-pointer-sap))
167   (:policy :fast-safe)
168   (:generator 1
169     (store-binding-stack-pointer new-value)
170     (move int new-value)))
171
172 (define-vop (control-stack-pointer-sap)
173   (:results (int :scs (sap-reg)))
174   (:result-types system-area-pointer)
175   (:translate control-stack-pointer-sap)
176   (:policy :fast-safe)
177   (:generator 1
178     (move int rsp-tn)))
179 \f
180 ;;;; code object frobbing
181
182 (define-vop (code-instructions)
183   (:translate code-instructions)
184   (:policy :fast-safe)
185   (:args (code :scs (descriptor-reg) :to (:result 0)))
186   (:results (sap :scs (sap-reg) :from (:argument 0)))
187   (:result-types system-area-pointer)
188   (:generator 10
189     (loadw sap code 0 other-pointer-lowtag)
190     (inst shr sap n-widetag-bits)
191     (inst lea sap (make-ea :byte :base code :index sap
192                            :scale n-word-bytes
193                            :disp (- other-pointer-lowtag)))))
194
195 (define-vop (compute-fun)
196   (:args (code :scs (descriptor-reg) :to (:result 0))
197          (offset :scs (signed-reg unsigned-reg) :to (:result 0)))
198   (:arg-types * positive-fixnum)
199   (:results (func :scs (descriptor-reg) :from (:argument 0)))
200   (:generator 10
201     (loadw func code 0 other-pointer-lowtag)
202     (inst shr func n-widetag-bits)
203     (inst lea func
204           (make-ea :byte :base offset :index func
205                    :scale n-word-bytes
206                    :disp (- fun-pointer-lowtag other-pointer-lowtag)))
207     (inst add func code)))
208
209 (define-vop (%simple-fun-self)
210   (:policy :fast-safe)
211   (:translate %simple-fun-self)
212   (:args (function :scs (descriptor-reg)))
213   (:results (result :scs (descriptor-reg)))
214   (:generator 3
215     (loadw result function simple-fun-self-slot fun-pointer-lowtag)
216     (inst lea result
217           (make-ea :byte :base result
218                    :disp (- fun-pointer-lowtag
219                             (* simple-fun-code-offset n-word-bytes))))))
220
221 ;;; The closure function slot is a pointer to raw code on X86 instead
222 ;;; of a pointer to the code function object itself. This VOP is used
223 ;;; to reference the function object given the closure object.
224 (define-source-transform %closure-fun (closure)
225   `(%simple-fun-self ,closure))
226
227 (define-vop (%set-fun-self)
228   (:policy :fast-safe)
229   (:translate (setf %simple-fun-self))
230   (:args (new-self :scs (descriptor-reg) :target result :to :result)
231          (function :scs (descriptor-reg) :to :result))
232   (:temporary (:sc any-reg :from (:argument 0) :to :result) temp)
233   (:results (result :scs (descriptor-reg)))
234   (:generator 3
235     (inst lea temp
236           (make-ea :byte :base new-self
237                    :disp (- (ash simple-fun-code-offset word-shift)
238                             fun-pointer-lowtag)))
239     (storew temp function simple-fun-self-slot fun-pointer-lowtag)
240     (move result new-self)))
241 \f
242 ;;;; other miscellaneous VOPs
243
244 (defknown sb!unix::receive-pending-interrupt () (values))
245 (define-vop (sb!unix::receive-pending-interrupt)
246   (:policy :fast-safe)
247   (:translate sb!unix::receive-pending-interrupt)
248   (:generator 1
249     (inst break pending-interrupt-trap)))
250
251 #!+sb-safepoint
252 (define-vop (insert-safepoint)
253   (:policy :fast-safe)
254   (:translate sb!kernel::gc-safepoint)
255   (:generator 0
256     (emit-safepoint)))
257
258 #!+sb-thread
259 (defknown current-thread-offset-sap ((unsigned-byte 64))
260   system-area-pointer (flushable))
261
262 #!+sb-thread
263 (define-vop (current-thread-offset-sap)
264   (:results (sap :scs (sap-reg)))
265   (:result-types system-area-pointer)
266   (:translate current-thread-offset-sap)
267   (:args (n :scs (unsigned-reg) :target sap))
268   (:arg-types unsigned-num)
269   (:policy :fast-safe)
270   (:generator 2
271     (inst mov sap
272           (make-ea :qword :base thread-base-tn :disp 0 :index n :scale 8))))
273
274 (define-vop (halt)
275   (:generator 1
276     (inst break halt-trap)))
277
278 (defknown float-wait () (values))
279 (define-vop (float-wait)
280   (:policy :fast-safe)
281   (:translate float-wait)
282   (:vop-var vop)
283   (:save-p :compute-only)
284   (:generator 1
285     (note-next-instruction vop :internal-error)
286     (inst wait)))
287 \f
288 ;;;; Miscellany
289
290 ;;; the RDTSC instruction (present on Pentium processors and
291 ;;; successors) allows you to access the time-stamp counter, a 64-bit
292 ;;; model-specific register that counts executed cycles. The
293 ;;; instruction returns the low cycle count in EAX and high cycle
294 ;;; count in EDX.
295 ;;;
296 ;;; In order to obtain more significant results on out-of-order
297 ;;; processors (such as the Pentium II and later), we issue a
298 ;;; serializing CPUID instruction before and after reading the cycle
299 ;;; counter. This instruction is used for its side effect of emptying
300 ;;; the processor pipeline, to ensure that the RDTSC instruction is
301 ;;; executed once all pending instructions have been completed and
302 ;;; before any others. CPUID writes to EBX and ECX in addition to EAX
303 ;;; and EDX, so they need to be added as temporaries.
304 ;;;
305 ;;; Note that cache effects mean that the cycle count can vary for
306 ;;; different executions of the same code (it counts cycles, not
307 ;;; retired instructions). Furthermore, the results are per-processor
308 ;;; and not per-process, so are unreliable on multiprocessor machines
309 ;;; where processes can migrate between processors.
310 ;;;
311 ;;; This method of obtaining a cycle count has the advantage of being
312 ;;; very fast (around 20 cycles), and of not requiring a system call.
313 ;;; However, you need to know your processor's clock speed to translate
314 ;;; this into real execution time.
315 ;;;
316 ;;; FIXME: This about the WITH-CYCLE-COUNTER interface a bit, and then
317 ;;; perhaps export it from SB-SYS.
318
319 (defknown %read-cycle-counter () (values (unsigned-byte 32) (unsigned-byte 32)) ())
320
321 (define-vop (%read-cycle-counter)
322   (:policy :fast-safe)
323   (:translate %read-cycle-counter)
324   (:temporary (:sc unsigned-reg :offset eax-offset :target lo) eax)
325   (:temporary (:sc unsigned-reg :offset edx-offset :target hi) edx)
326   (:temporary (:sc unsigned-reg :offset ebx-offset) ebx)
327   (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
328   (:ignore ebx ecx)
329   (:results (hi :scs (unsigned-reg))
330             (lo :scs (unsigned-reg)))
331   (:result-types unsigned-num unsigned-num)
332   (:generator 5
333      (zeroize eax)
334      ;; Intel docs seem quite consistent on only using CPUID before RDTSC,
335      ;; not both before and after. Go figure.
336      (inst cpuid)
337      (inst rdtsc)
338      (move lo eax)
339      (move hi edx)))
340
341 (defmacro with-cycle-counter (&body body)
342   "Returns the primary value of BODY as the primary value, and the
343 number of CPU cycles elapsed as secondary value. EXPERIMENTAL."
344   (with-unique-names (hi0 hi1 lo0 lo1)
345     `(multiple-value-bind (,hi0 ,lo0) (%read-cycle-counter)
346        (values (locally ,@body)
347                (multiple-value-bind (,hi1 ,lo1) (%read-cycle-counter)
348                  (+ (ash (- ,hi1 ,hi0) 32)
349                     (- ,lo1 ,lo0)))))))
350
351 #!+sb-dyncount
352 (define-vop (count-me)
353   (:args (count-vector :scs (descriptor-reg)))
354   (:info index)
355   (:generator 0
356     (inst inc (make-ea :qword :base count-vector
357                        :disp (- (* (+ vector-data-offset index) n-word-bytes)
358                                 other-pointer-lowtag)))))
359 \f
360 ;;;; Memory barrier support
361
362 #!+memory-barrier-vops
363 (define-vop (%compiler-barrier)
364   (:policy :fast-safe)
365   (:translate %compiler-barrier)
366   (:generator 3))
367
368 #!+memory-barrier-vops
369 (define-vop (%memory-barrier)
370   (:policy :fast-safe)
371   (:translate %memory-barrier)
372   (:generator 3
373     (inst mfence)))
374
375 #!+memory-barrier-vops
376 (define-vop (%read-barrier)
377   (:policy :fast-safe)
378   (:translate %read-barrier)
379   (:generator 3))
380
381 #!+memory-barrier-vops
382 (define-vop (%write-barrier)
383   (:policy :fast-safe)
384   (:translate %write-barrier)
385   (:generator 3))
386
387 #!+memory-barrier-vops
388 (define-vop (%data-dependency-barrier)
389   (:policy :fast-safe)
390   (:translate %data-dependency-barrier)
391   (:generator 3))
392
393 (define-vop (pause)
394   (:translate spin-loop-hint)
395   (:policy :fast-safe)
396   (:generator 0
397     (inst pause)))