1;;;; function call for the x86 VM
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
14(defconstant arg-count-sc (make-sc-offset any-reg-sc-number rcx-offset))
15(defconstant closure-sc (make-sc-offset any-reg-sc-number rax-offset))
16
17;;; Make a passing location TN for a local call return PC.
18;;;
19;;; Always wire the return PC location to the stack in its standard
20;;; location.
21(defun make-return-pc-passing-location (standard)
22  (declare (ignore standard))
23  (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
24                 sap-stack-sc-number return-pc-save-offset))
25
26(defconstant return-pc-passing-offset
27  (make-sc-offset sap-stack-sc-number return-pc-save-offset))
28
29;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
30;;; location to pass OLD-FP in.
31;;;
32;;; This is wired in both the standard and the local-call conventions,
33;;; because we want to be able to assume it's always there. Besides,
34;;; the x86 doesn't have enough registers to really make it profitable
35;;; to pass it in a register.
36(defun make-old-fp-passing-location (standard)
37  (declare (ignore standard))
38  (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
39                 ocfp-save-offset))
40
41(defconstant old-fp-passing-offset
42  (make-sc-offset control-stack-sc-number ocfp-save-offset))
43
44;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
45;;; function. We treat these specially so that the debugger can find
46;;; them at a known location.
47;;;
48;;; Without using a save-tn - which does not make much sense if it is
49;;; wired to the stack?
50(defun make-old-fp-save-location (physenv)
51  (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
52                                        control-stack-sc-number
53                                        ocfp-save-offset)
54                         physenv))
55(defun make-return-pc-save-location (physenv)
56  (physenv-debug-live-tn
57   (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
58                  sap-stack-sc-number return-pc-save-offset)
59   physenv))
60
61;;; Make a TN for the standard argument count passing location. We only
62;;; need to make the standard location, since a count is never passed when we
63;;; are using non-standard conventions.
64(defun make-arg-count-location ()
65  (make-wired-tn *fixnum-primitive-type* any-reg-sc-number rcx-offset))
66
67;;;; frame hackery
68
69;;; This is used for setting up the Old-FP in local call.
70(define-vop (current-fp)
71  (:results (val :scs (any-reg control-stack)))
72  (:generator 1
73    (move val rbp-tn)))
74
75;;; We don't have a separate NFP, so we don't need to do anything here.
76(define-vop (compute-old-nfp)
77  (:results (val))
78  (:ignore val)
79  (:generator 1
80    nil))
81
82;;; Accessing a slot from an earlier stack frame is definite hackery.
83(define-vop (ancestor-frame-ref)
84  (:args (frame-pointer :scs (descriptor-reg))
85         (variable-home-tn :load-if nil))
86  (:results (value :scs (descriptor-reg any-reg)))
87  (:policy :fast-safe)
88  (:generator 4
89    (aver (sc-is variable-home-tn control-stack))
90    (loadw value frame-pointer
91           (frame-word-offset (tn-offset variable-home-tn)))))
92(define-vop (ancestor-frame-set)
93  (:args (frame-pointer :scs (descriptor-reg))
94         (value :scs (descriptor-reg any-reg)))
95  (:results (variable-home-tn :load-if nil))
96  (:policy :fast-safe)
97  (:generator 4
98    (aver (sc-is variable-home-tn control-stack))
99    (storew value frame-pointer
100            (frame-word-offset (tn-offset variable-home-tn)))))
101
102(macrolet ((define-frame-op
103               (suffix sc stack-sc instruction
104                &optional (ea
105                           `(make-ea :qword
106                                     :base frame-pointer
107                                     :disp (frame-byte-offset
108                                            (tn-offset variable-home-tn)))))
109               (let ((reffer (symbolicate 'ancestor-frame-ref '/ suffix))
110                     (setter (symbolicate 'ancestor-frame-set '/ suffix)))
111                 `(progn
112                    (define-vop (,reffer ancestor-frame-ref)
113                      (:results (value :scs (,sc)))
114                      (:generator 4
115                        (aver (sc-is variable-home-tn ,stack-sc))
116                        (inst ,instruction value
117                              ,ea)))
118                    (define-vop (,setter ancestor-frame-set)
119                      (:args (frame-pointer :scs (descriptor-reg))
120                             (value :scs (,sc)))
121                      (:generator 4
122                        (aver (sc-is variable-home-tn ,stack-sc))
123                        (inst ,instruction ,ea value)))))))
124  (define-frame-op double-float double-reg double-stack movsd)
125  (define-frame-op single-float single-reg single-stack movss)
126  (define-frame-op complex-double-float complex-double-reg complex-double-stack
127    movupd (ea-for-cdf-data-stack variable-home-tn frame-pointer))
128  (define-frame-op complex-single-float complex-single-reg complex-single-stack
129    movq   (ea-for-csf-data-stack variable-home-tn frame-pointer))
130  (define-frame-op signed-byte-64 signed-reg signed-stack mov)
131  (define-frame-op unsigned-byte-64 unsigned-reg unsigned-stack mov)
132  (define-frame-op system-area-pointer sap-reg sap-stack mov))
133
134(defun primitive-type-indirect-cell-type (ptype)
135  (declare (type primitive-type ptype))
136  (macrolet ((foo (&body data)
137                 `(case (primitive-type-name ptype)
138                    ,@(loop for (name stack-sc ref set) in data
139                            collect
140                            `(,name
141                               (load-time-value
142                                (list (primitive-type-or-lose ',name)
143                                      (sc-or-lose ',stack-sc)
144                                      (lambda (node block fp value res)
145                                        (sb!c::vop ,ref node block
146                                                   fp value res))
147                                      (lambda (node block fp new-val value)
148                                        (sb!c::vop ,set node block
149                                                   fp new-val value)))))))))
150    (foo (double-float double-stack
151                       ancestor-frame-ref/double-float
152                       ancestor-frame-set/double-float)
153         (single-float single-stack
154                       ancestor-frame-ref/single-float
155                       ancestor-frame-set/single-float)
156         (complex-double-float complex-double-stack
157                               ancestor-frame-ref/complex-double-float
158                               ancestor-frame-set/complex-double-float)
159         (complex-single-float complex-single-stack
160                               ancestor-frame-ref/complex-single-float
161                               ancestor-frame-set/complex-single-float)
162         (signed-byte-64 signed-stack
163                         ancestor-frame-ref/signed-byte-64
164                         ancestor-frame-set/signed-byte-64)
165         (unsigned-byte-64 unsigned-stack
166                           ancestor-frame-ref/unsigned-byte-64
167                           ancestor-frame-set/unsigned-byte-64)
168         (unsigned-byte-63 unsigned-stack
169                           ancestor-frame-ref/unsigned-byte-64
170                           ancestor-frame-set/unsigned-byte-64)
171         (system-area-pointer sap-stack
172                              ancestor-frame-ref/system-area-pointer
173                              ancestor-frame-set/system-area-pointer))))
174
175(define-vop (xep-allocate-frame)
176  (:info start-lab)
177  (:generator 1
178    (emit-alignment n-lowtag-bits)
179    (emit-label start-lab)
180    ;; Skip space for the function header.
181    (inst simple-fun-header-word)
182    (dotimes (i (* n-word-bytes (1- simple-fun-code-offset)))
183      (inst byte 0))
184
185    ;; The start of the actual code.
186    ;; Save the return-pc.
187    (popw rbp-tn (frame-word-offset return-pc-save-offset))))
188
189(define-vop (xep-setup-sp)
190  (:generator 1
191    (inst lea rsp-tn
192          (make-ea :qword :base rbp-tn
193                          :disp (- (* n-word-bytes
194                                      (- (max 3 (sb-allocated-size 'stack))
195                                         sp->fp-offset)))))))
196
197;;; This is emitted directly before either a known-call-local, call-local,
198;;; or a multiple-call-local. All it does is allocate stack space for the
199;;; callee (who has the same size stack as us).
200(define-vop (allocate-frame)
201  (:results (res :scs (any-reg))
202            (nfp))
203  (:info callee)
204  (:ignore nfp callee)
205  (:generator 2
206    (inst lea res (make-ea :qword :base rsp-tn
207                           :disp (- (* sp->fp-offset n-word-bytes))))
208    (inst sub rsp-tn (* n-word-bytes (sb-allocated-size 'stack)))))
209
210;;; Allocate a partial frame for passing stack arguments in a full
211;;; call. NARGS is the number of arguments passed. We allocate at
212;;; least 3 slots, because the XEP noise is going to want to use them
213;;; before it can extend the stack.
214(define-vop (allocate-full-call-frame)
215  (:info nargs)
216  (:results (res :scs (any-reg)))
217  (:generator 2
218    (inst lea res (make-ea :qword :base rsp-tn
219                           :disp (- (* sp->fp-offset n-word-bytes))))
220    (inst sub rsp-tn (* (max nargs 3) n-word-bytes))))
221
222;;; Emit code needed at the return-point from an unknown-values call
223;;; for a fixed number of values. Values is the head of the TN-REF
224;;; list for the locations that the values are to be received into.
225;;; Nvals is the number of values that are to be received (should
226;;; equal the length of Values).
227;;;
228;;; If 0 or 1 values are expected, then we just emit an instruction to
229;;; reset the SP (which will only be executed when other than 1 value
230;;; is returned.)
231;;;
232;;; In the general case we have to do three things:
233;;;  -- Default unsupplied register values. This need only be done
234;;;     when a single value is returned, since register values are
235;;;     defaulted by the called in the non-single case.
236;;;  -- Default unsupplied stack values. This needs to be done whenever
237;;;     there are stack values.
238;;;  -- Reset SP. This must be done whenever other than 1 value is
239;;;     returned, regardless of the number of values desired.
240(defun default-unknown-values (vop values nvals node)
241  (declare (type (or tn-ref null) values)
242           (type unsigned-byte nvals))
243  (let ((type (sb!c::basic-combination-derived-type node)))
244    (cond
245      ((<= nvals 1)
246       (note-this-location vop :single-value-return)
247       (cond
248         ((<= (sb!kernel:values-type-max-value-count type)
249              register-arg-count)
250          (when (and (named-type-p type)
251                     (eq nil (named-type-name type)))
252            ;; The function never returns, it may happen that the code
253            ;; ends right here leavig the :SINGLE-VALUE-RETURN note
254            ;; dangling. Let's emit a NOP.
255            (inst nop)))
256         ((not (sb!kernel:values-type-may-be-single-value-p type))
257          (inst mov rsp-tn rbx-tn))
258         (t
259          (inst cmov :c rsp-tn rbx-tn))))
260      ((<= nvals register-arg-count)
261       (note-this-location vop :unknown-return)
262       (when (sb!kernel:values-type-may-be-single-value-p type)
263         (let ((regs-defaulted (gen-label)))
264           (inst jmp :c regs-defaulted)
265           ;; Default the unsupplied registers.
266           (let* ((2nd-tn-ref (tn-ref-across values))
267                  (2nd-tn (tn-ref-tn 2nd-tn-ref)))
268             (inst mov 2nd-tn nil-value)
269             (when (> nvals 2)
270               (loop
271                for tn-ref = (tn-ref-across 2nd-tn-ref)
272                then (tn-ref-across tn-ref)
273                for count from 2 below register-arg-count
274                do (inst mov (tn-ref-tn tn-ref) 2nd-tn))))
275           (inst mov rbx-tn rsp-tn)
276           (emit-label regs-defaulted)))
277       (when (< register-arg-count
278                (sb!kernel:values-type-max-value-count type))
279         (inst mov rsp-tn rbx-tn)))
280      ((<= nvals 7)
281       ;; The number of bytes depends on the relative jump instructions.
282       ;; Best case is 31+(n-3)*14, worst case is 35+(n-3)*18. For
283       ;; NVALS=6 that is 73/89 bytes, and for NVALS=7 that is 87/107
284       ;; bytes which is likely better than using the blt below.
285       (let ((regs-defaulted (gen-label))
286             (defaulting-done (gen-label))
287             (default-stack-slots (gen-label)))
288         (note-this-location vop :unknown-return)
289         (inst mov rax-tn nil-value)
290         ;; Branch off to the MV case.
291         (inst jmp :c regs-defaulted)
292         ;; Do the single value case.
293         ;; Default the register args
294         (do ((i 1 (1+ i))
295              (val (tn-ref-across values) (tn-ref-across val)))
296             ((= i (min nvals register-arg-count)))
297           (inst mov (tn-ref-tn val) rax-tn))
298         ;; Fake other registers so it looks like we returned with all the
299         ;; registers filled in.
300         (move rbx-tn rsp-tn)
301         (inst jmp default-stack-slots)
302         (emit-label regs-defaulted)
303         (collect ((defaults))
304           (do ((i register-arg-count (1+ i))
305                (val (do ((i 0 (1+ i))
306                          (val values (tn-ref-across val)))
307                         ((= i register-arg-count) val))
308                     (tn-ref-across val)))
309               ((null val))
310             (let ((default-lab (gen-label))
311                   (tn (tn-ref-tn val))
312                   (first-stack-arg-p (= i register-arg-count)))
313               (defaults (cons default-lab
314                               (cons tn first-stack-arg-p)))
315               (inst cmp rcx-tn (fixnumize i))
316               (inst jmp :be default-lab)
317               (when first-stack-arg-p
318                 ;; There are stack args so the frame of the callee is
319                 ;; still there, save RDX in its first slot temporalily.
320                 (storew rdx-tn rbx-tn (frame-word-offset sp->fp-offset)))
321               (loadw rdx-tn rbx-tn (frame-word-offset (+ sp->fp-offset i)))
322               (inst mov tn rdx-tn)))
323           (emit-label defaulting-done)
324           (loadw rdx-tn rbx-tn (frame-word-offset sp->fp-offset))
325           (move rsp-tn rbx-tn)
326           (let ((defaults (defaults)))
327             (when defaults
328               (assemble (*elsewhere*)
329                 (emit-label default-stack-slots)
330                 (dolist (default defaults)
331                   (emit-label (car default))
332                   (when (cddr default)
333                     ;; We are setting the first stack argument to NIL.
334                     ;; The callee's stack frame is dead, save RDX by
335                     ;; pushing it to the stack, it will end up at same
336                     ;; place as in the (STOREW RDX-TN RBX-TN -1) case
337                     ;; above.
338                     (inst push rdx-tn))
339                   (inst mov (second default) rax-tn))
340                 (inst jmp defaulting-done)))))))
341      (t
342       (let ((regs-defaulted (gen-label))
343             (restore-edi (gen-label))
344             (no-stack-args (gen-label))
345             (default-stack-vals (gen-label))
346             (count-okay (gen-label)))
347         (note-this-location vop :unknown-return)
348         ;; Branch off to the MV case.
349         (inst jmp :c regs-defaulted)
350         ;; Default the register args, and set up the stack as if we
351         ;; entered the MV return point.
352         (inst mov rbx-tn rsp-tn)
353         (inst mov rdi-tn nil-value)
354         (inst mov rsi-tn rdi-tn)
355         ;; Compute a pointer to where to put the [defaulted] stack values.
356         (emit-label no-stack-args)
357         (inst push rdx-tn)
358         (inst push rdi-tn)
359         (inst lea rdi-tn
360               (make-ea :qword :base rbp-tn
361                        :disp (frame-byte-offset register-arg-count)))
362         ;; Load RAX with NIL so we can quickly store it, and set up
363         ;; stuff for the loop.
364         (inst mov rax-tn nil-value)
365         (inst std)
366         (inst mov rcx-tn (- nvals register-arg-count))
367         ;; Jump into the default loop.
368         (inst jmp default-stack-vals)
369         ;; The regs are defaulted. We need to copy any stack arguments,
370         ;; and then default the remaining stack arguments.
371         (emit-label regs-defaulted)
372         ;; Compute the number of stack arguments, and if it's zero or
373         ;; less, don't copy any stack arguments.
374         (inst sub rcx-tn (fixnumize register-arg-count))
375         (inst jmp :le no-stack-args)
376         ;; Save EDI.
377         (storew rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
378         ;; Throw away any unwanted args.
379         (inst cmp rcx-tn (fixnumize (- nvals register-arg-count)))
380         (inst jmp :be count-okay)
381         (inst mov rcx-tn (fixnumize (- nvals register-arg-count)))
382         (emit-label count-okay)
383         ;; Save the number of stack values.
384         (inst mov rax-tn rcx-tn)
385         ;; Compute a pointer to where the stack args go.
386         (inst lea rdi-tn
387               (make-ea :qword :base rbp-tn
388                        :disp (frame-byte-offset register-arg-count)))
389         ;; Save ESI, and compute a pointer to where the args come from.
390         (storew rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
391         (inst lea rsi-tn
392               (make-ea :qword :base rbx-tn
393                        :disp (frame-byte-offset
394                               (+ sp->fp-offset register-arg-count))))
395         ;; Do the copy.
396         (inst shr rcx-tn n-fixnum-tag-bits)   ; make word count
397         (inst std)
398         (inst rep)
399         (inst movs :qword)
400         ;; Restore RSI.
401         (loadw rsi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 2)))
402         ;; Now we have to default the remaining args. Find out how many.
403         (inst sub rax-tn (fixnumize (- nvals register-arg-count)))
404         (inst neg rax-tn)
405         ;; If none, then just blow out of here.
406         (inst jmp :le restore-edi)
407         (inst mov rcx-tn rax-tn)
408         (inst shr rcx-tn n-fixnum-tag-bits)   ; word count
409         ;; Load RAX with NIL for fast storing.
410         (inst mov rax-tn nil-value)
411         ;; Do the store.
412         (emit-label default-stack-vals)
413         (inst rep)
414         (inst stos rax-tn)
415         ;; Restore EDI, and reset the stack.
416         (emit-label restore-edi)
417         (loadw rdi-tn rbx-tn (frame-word-offset (+ sp->fp-offset 1)))
418         (inst mov rsp-tn rbx-tn)
419         (inst cld)))))
420  (values))
421
422;;;; unknown values receiving
423
424;;; Emit code needed at the return point for an unknown-values call
425;;; for an arbitrary number of values.
426;;;
427;;; We do the single and non-single cases with no shared code: there
428;;; doesn't seem to be any potential overlap, and receiving a single
429;;; value is more important efficiency-wise.
430;;;
431;;; When there is a single value, we just push it on the stack,
432;;; returning the old SP and 1.
433;;;
434;;; When there is a variable number of values, we move all of the
435;;; argument registers onto the stack, and return ARGS and NARGS.
436;;;
437;;; ARGS and NARGS are TNs wired to the named locations. We must
438;;; explicitly allocate these TNs, since their lifetimes overlap with
439;;; the results start and count. (Also, it's nice to be able to target
440;;; them.)
441(defun receive-unknown-values (args nargs start count node)
442  (declare (type tn args nargs start count))
443  (let ((type (sb!c::basic-combination-derived-type node))
444        (variable-values (gen-label))
445        (stack-values (gen-label))
446        (done (gen-label)))
447    (when (sb!kernel:values-type-may-be-single-value-p type)
448      (inst jmp :c variable-values)
449      (cond ((location= start (first *register-arg-tns*))
450             (inst push (first *register-arg-tns*))
451             (inst lea start (make-ea :qword :base rsp-tn :disp n-word-bytes)))
452            (t (inst mov start rsp-tn)
453               (inst push (first *register-arg-tns*))))
454      (inst mov count (fixnumize 1))
455      (inst jmp done)
456      (emit-label variable-values))
457    ;; The stack frame is burnt and RETurned from if there are no
458    ;; stack values. In this case quickly reallocate sufficient space.
459    (when (<= (sb!kernel:values-type-min-value-count type)
460              register-arg-count)
461      (inst cmp nargs (fixnumize register-arg-count))
462      (inst jmp :g stack-values)
463      #!+#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
464      (inst sub rsp-tn nargs)
465      #!-#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
466      (progn
467        ;; FIXME: This can't be efficient, but LEA (my first choice)
468        ;; doesn't do subtraction.
469        (inst shl nargs (- word-shift n-fixnum-tag-bits))
470        (inst sub rsp-tn nargs)
471        (inst shr nargs (- word-shift n-fixnum-tag-bits)))
472      (emit-label stack-values))
473    ;; dtc: this writes the registers onto the stack even if they are
474    ;; not needed, only the number specified in rcx are used and have
475    ;; stack allocated to them. No harm is done.
476    (loop
477      for arg in *register-arg-tns*
478      for i downfrom -1
479      for j below (sb!kernel:values-type-max-value-count type)
480      do (storew arg args i))
481    (move start args)
482    (move count nargs)
483
484    (emit-label done))
485  (values))
486
487;;; VOP that can be inherited by unknown values receivers. The main thing this
488;;; handles is allocation of the result temporaries.
489(define-vop (unknown-values-receiver)
490  (:temporary (:sc descriptor-reg :offset rbx-offset
491                   :from :eval :to (:result 0))
492              values-start)
493  (:temporary (:sc any-reg :offset rcx-offset
494               :from :eval :to (:result 1))
495              nvals)
496  (:results (start :scs (any-reg control-stack))
497            (count :scs (any-reg control-stack))))
498
499;;;; local call with unknown values convention return
500
501(defun check-ocfp-and-return-pc (old-fp return-pc)
502  #+nil
503  (format t "*known-return: old-fp ~S, tn-kind ~S; ~S ~S~%"
504          old-fp (sb!c::tn-kind old-fp) (sb!c::tn-save-tn old-fp)
505          (sb!c::tn-kind (sb!c::tn-save-tn old-fp)))
506  #+nil
507  (format t "*known-return: return-pc ~S, tn-kind ~S; ~S ~S~%"
508          return-pc (sb!c::tn-kind return-pc)
509          (sb!c::tn-save-tn return-pc)
510          (sb!c::tn-kind (sb!c::tn-save-tn return-pc)))
511  (unless (and (sc-is old-fp control-stack)
512               (= (tn-offset old-fp) ocfp-save-offset))
513    (error "ocfp not on stack in standard save location?"))
514  (unless (and (sc-is return-pc sap-stack)
515               (= (tn-offset return-pc) return-pc-save-offset))
516    (error "return-pc not on stack in standard save location?")))
517
518;;; The local call convention doesn't fit that well with x86-style
519;;; calls. Emit a header for local calls to pop the return address
520;;; in the right place.
521(defun emit-block-header (start-label trampoline-label fall-thru-p alignp)
522  (when (and fall-thru-p trampoline-label)
523    (inst jmp start-label))
524  (when trampoline-label
525    (emit-label trampoline-label)
526    (popw rbp-tn (frame-word-offset return-pc-save-offset)))
527  (when alignp
528    (emit-alignment n-lowtag-bits :long-nop))
529  (emit-label start-label))
530
531;;; Non-TR local call for a fixed number of values passed according to
532;;; the unknown values convention.
533;;;
534;;; FP is the frame pointer in install before doing the call.
535;;;
536;;; NFP would be the number-stack frame pointer if we had a separate
537;;; number stack.
538;;;
539;;; Args are the argument passing locations, which are specified only
540;;; to terminate their lifetimes in the caller.
541;;;
542;;; VALUES are the return value locations (wired to the standard
543;;; passing locations). NVALS is the number of values received.
544;;;
545;;; Save is the save info, which we can ignore since saving has been
546;;; done.
547;;;
548;;; TARGET is a continuation pointing to the start of the called
549;;; function.
550(define-vop (call-local)
551  (:args (fp)
552         (nfp)
553         (args :more t))
554  (:results (values :more t))
555  (:save-p t)
556  (:move-args :local-call)
557  (:info arg-locs callee target nvals)
558  (:vop-var vop)
559  (:ignore nfp arg-locs args callee)
560  (:node-var node)
561  (:generator 5
562    (move rbp-tn fp)
563    (note-this-location vop :call-site)
564    (inst call target)
565    (default-unknown-values vop values nvals node)))
566
567;;; Non-TR local call for a variable number of return values passed according
568;;; to the unknown values convention. The results are the start of the values
569;;; glob and the number of values received.
570(define-vop (multiple-call-local unknown-values-receiver)
571  (:args (fp)
572         (nfp)
573         (args :more t))
574  (:save-p t)
575  (:move-args :local-call)
576  (:info save callee target)
577  (:ignore args save nfp callee)
578  (:vop-var vop)
579  (:node-var node)
580  (:generator 20
581    (move rbp-tn fp)
582    (note-this-location vop :call-site)
583    (inst call target)
584    (note-this-location vop :unknown-return)
585    (receive-unknown-values values-start nvals start count node)))
586
587;;;; local call with known values return
588
589;;; Non-TR local call with known return locations. Known-value return
590;;; works just like argument passing in local call.
591;;;
592;;; Note: we can't use normal load-tn allocation for the fixed args,
593;;; since all registers may be tied up by the more operand. Instead,
594;;; we use MAYBE-LOAD-STACK-TN.
595(define-vop (known-call-local)
596  (:args (fp)
597         (nfp)
598         (args :more t))
599  (:results (res :more t))
600  (:move-args :local-call)
601  (:save-p t)
602  (:info save callee target)
603  (:ignore args res save nfp callee)
604  (:vop-var vop)
605  (:generator 5
606    (move rbp-tn fp)
607    (note-this-location vop :call-site)
608    (inst call target)
609    (note-this-location vop :known-return)))
610
611;;; From Douglas Crosher
612;;; Return from known values call. We receive the return locations as
613;;; arguments to terminate their lifetimes in the returning function. We
614;;; restore FP and CSP and jump to the Return-PC.
615(define-vop (known-return)
616  (:args (old-fp)
617         (return-pc)
618         (vals :more t))
619  (:move-args :known-return)
620  (:info val-locs)
621  (:ignore val-locs vals)
622  (:vop-var vop)
623  (:generator 6
624    (check-ocfp-and-return-pc old-fp return-pc)
625    ;; Zot all of the stack except for the old-fp and return-pc.
626    (inst mov rsp-tn rbp-tn)
627    (inst pop rbp-tn)
628    (inst ret)))
629
630;;;; full call
631;;;
632;;; There is something of a cross-product effect with full calls.
633;;; Different versions are used depending on whether we know the
634;;; number of arguments or the name of the called function, and
635;;; whether we want fixed values, unknown values, or a tail call.
636;;;
637;;; In full call, the arguments are passed creating a partial frame on
638;;; the stack top and storing stack arguments into that frame. On
639;;; entry to the callee, this partial frame is pointed to by FP.
640
641;;; This macro helps in the definition of full call VOPs by avoiding
642;;; code replication in defining the cross-product VOPs.
643;;;
644;;; NAME is the name of the VOP to define.
645;;;
646;;; NAMED is true if the first argument is an fdefinition object whose
647;;; definition is to be called.
648;;;
649;;; RETURN is either :FIXED, :UNKNOWN or :TAIL:
650;;; -- If :FIXED, then the call is for a fixed number of values, returned in
651;;;    the standard passing locations (passed as result operands).
652;;; -- If :UNKNOWN, then the result values are pushed on the stack, and the
653;;;    result values are specified by the Start and Count as in the
654;;;    unknown-values continuation representation.
655;;; -- If :TAIL, then do a tail-recursive call. No values are returned.
656;;;    The Old-Fp and Return-PC are passed as the second and third arguments.
657;;;
658;;; In non-tail calls, the pointer to the stack arguments is passed as
659;;; the last fixed argument. If Variable is false, then the passing
660;;; locations are passed as a more arg. Variable is true if there are
661;;; a variable number of arguments passed on the stack. Variable
662;;; cannot be specified with :TAIL return. TR variable argument call
663;;; is implemented separately.
664;;;
665;;; In tail call with fixed arguments, the passing locations are
666;;; passed as a more arg, but there is no new-FP, since the arguments
667;;; have been set up in the current frame.
668(macrolet ((define-full-call (name named return variable)
669            (aver (not (and variable (eq return :tail))))
670            `(define-vop (,name
671                          ,@(when (eq return :unknown)
672                              '(unknown-values-receiver)))
673               (:args
674               ,@(unless (eq return :tail)
675                   '((new-fp :scs (any-reg) :to (:argument 1))))
676
677               (fun :scs (descriptor-reg control-stack)
678                    :target rax :to (:argument 0))
679
680               ,@(when (eq return :tail)
681                   '((old-fp)
682                     (return-pc)))
683
684               ,@(unless variable '((args :more t :scs (descriptor-reg)))))
685
686               ,@(when (eq return :fixed)
687               '((:results (values :more t))))
688
689               (:save-p ,(if (eq return :tail) :compute-only t))
690
691               ,@(unless (or (eq return :tail) variable)
692               '((:move-args :full-call)))
693
694               (:vop-var vop)
695               (:info
696               ,@(unless (or variable (eq return :tail)) '(arg-locs))
697               ,@(unless variable '(nargs))
698               ,@(when (eq return :fixed) '(nvals))
699               step-instrumenting)
700
701               (:ignore
702               ,@(unless (or variable (eq return :tail)) '(arg-locs))
703               ,@(unless variable '(args)))
704
705               ;; We pass either the fdefn object (for named call) or
706               ;; the actual function object (for unnamed call) in
707               ;; RAX. With named call, closure-tramp will replace it
708               ;; with the real function and invoke the real function
709               ;; for closures. Non-closures do not need this value,
710               ;; so don't care what shows up in it.
711               (:temporary
712               (:sc descriptor-reg
713                    :offset rax-offset
714                    :from (:argument 0)
715                    :to :eval)
716               rax)
717
718               ;; We pass the number of arguments in RCX.
719               (:temporary (:sc unsigned-reg :offset rcx-offset :to :eval) rcx)
720
721               ;; With variable call, we have to load the
722               ;; register-args out of the (new) stack frame before
723               ;; doing the call. Therefore, we have to tell the
724               ;; lifetime stuff that we need to use them.
725               ,@(when variable
726                   (mapcar (lambda (name offset)
727                             `(:temporary (:sc descriptor-reg
728                                               :offset ,offset
729                                               :from (:argument 0)
730                                               :to :eval)
731                                          ,name))
732                           *register-arg-names* *register-arg-offsets*))
733
734               ,@(when (eq return :tail)
735                   '((:temporary (:sc unsigned-reg
736                                      :from (:argument 1)
737                                      :to (:argument 2))
738                                 old-fp-tmp)))
739               ,@(unless (eq return :tail)
740                   '((:node-var node)))
741
742               (:generator ,(+ (if named 5 0)
743                               (if variable 19 1)
744                               (if (eq return :tail) 0 10)
745                               15
746                               (if (eq return :unknown) 25 0))
747               ;; This has to be done before the frame pointer is
748               ;; changed! RAX stores the 'lexical environment' needed
749               ;; for closures.
750               (move rax fun)
751
752
753               ,@(if variable
754                     ;; For variable call, compute the number of
755                     ;; arguments and move some of the arguments to
756                     ;; registers.
757                     (collect ((noise))
758                              ;; Compute the number of arguments.
759                              (noise '(inst mov rcx new-fp))
760                              (noise '(inst sub rcx rsp-tn))
761                              #.(unless (= word-shift n-fixnum-tag-bits)
762                                  '(noise '(inst shr rcx
763                                            (- word-shift n-fixnum-tag-bits))))
764                              ;; Move the necessary args to registers,
765                              ;; this moves them all even if they are
766                              ;; not all needed.
767                              (loop
768                               for name in *register-arg-names*
769                               for index downfrom -1
770                               do (noise `(loadw ,name new-fp ,index)))
771                              (noise))
772                   '((if (zerop nargs)
773                         (zeroize rcx)
774                       (inst mov rcx (fixnumize nargs)))))
775               ,@(cond ((eq return :tail)
776                        '(;; Python has figured out what frame we should
777                          ;; return to so might as well use that clue.
778                          ;; This seems really important to the
779                          ;; implementation of things like
780                          ;; (without-interrupts ...)
781                          ;;
782                          ;; dtc; Could be doing a tail call from a
783                          ;; known-local-call etc in which the old-fp
784                          ;; or ret-pc are in regs or in non-standard
785                          ;; places. If the passing location were
786                          ;; wired to the stack in standard locations
787                          ;; then these moves will be un-necessary;
788                          ;; this is probably best for the x86.
789                          (sc-case old-fp
790                                   ((control-stack)
791                                    (unless (= ocfp-save-offset
792                                               (tn-offset old-fp))
793                                      ;; FIXME: FORMAT T for stale
794                                      ;; diagnostic output (several of
795                                      ;; them around here), ick
796                                      (error "** tail-call old-fp not S0~%")
797                                      (move old-fp-tmp old-fp)
798                                      (storew old-fp-tmp
799                                              rbp-tn
800                                              (frame-word-offset ocfp-save-offset))))
801                                   ((any-reg descriptor-reg)
802                                    (error "** tail-call old-fp in reg not S0~%")
803                                    (storew old-fp
804                                            rbp-tn
805                                            (frame-word-offset ocfp-save-offset))))
806
807                          ;; For tail call, we have to push the
808                          ;; return-pc so that it looks like we CALLed
809                          ;; despite the fact that we are going to JMP.
810                          (inst push return-pc)
811                          ))
812                       (t
813                        ;; For non-tail call, we have to save our
814                        ;; frame pointer and install the new frame
815                        ;; pointer. We can't load stack tns after this
816                        ;; point.
817                        `(;; Python doesn't seem to allocate a frame
818                          ;; here which doesn't leave room for the
819                          ;; ofp/ret stuff.
820
821                          ;; The variable args are on the stack and
822                          ;; become the frame, but there may be <3
823                          ;; args and 3 stack slots are assumed
824                          ;; allocate on the call. So need to ensure
825                          ;; there are at least 3 slots. This hack
826                          ;; just adds 3 more.
827                          ,(if variable
828                               '(inst sub rsp-tn (* 3 n-word-bytes)))
829
830                          ;; Bias the new-fp for use as an fp
831                          ,(if variable
832                               '(inst sub new-fp (* sp->fp-offset n-word-bytes)))
833
834                          ;; Save the fp
835                          (storew rbp-tn new-fp
836                                  (frame-word-offset ocfp-save-offset))
837
838                          (move rbp-tn new-fp) ; NB - now on new stack frame.
839                          )))
840
841               (when step-instrumenting
842                 (emit-single-step-test)
843                 (inst jmp :eq DONE)
844                 (inst break single-step-around-trap))
845               DONE
846
847               (note-this-location vop :call-site)
848
849               (inst ,(if (eq return :tail) 'jmp 'call)
850                     (make-ea :qword :base rax
851                              :disp ,(if named
852                                         '(- (* fdefn-raw-addr-slot
853                                                n-word-bytes)
854                                             other-pointer-lowtag)
855                                       '(- (* closure-fun-slot n-word-bytes)
856                                           fun-pointer-lowtag))))
857               ,@(ecase return
858                   (:fixed
859                    '((default-unknown-values vop values nvals node)))
860                   (:unknown
861                    '((note-this-location vop :unknown-return)
862                      (receive-unknown-values values-start nvals start count
863                                              node)))
864                   (:tail))))))
865
866  (define-full-call call nil :fixed nil)
867  (define-full-call call-named t :fixed nil)
868  (define-full-call multiple-call nil :unknown nil)
869  (define-full-call multiple-call-named t :unknown nil)
870  (define-full-call tail-call nil :tail nil)
871  (define-full-call tail-call-named t :tail nil)
872
873  (define-full-call call-variable nil :fixed t)
874  (define-full-call multiple-call-variable nil :unknown t))
875
876;;; This is defined separately, since it needs special code that BLT's
877;;; the arguments down. All the real work is done in the assembly
878;;; routine. We just set things up so that it can find what it needs.
879(define-vop (tail-call-variable)
880  (:args (args :scs (any-reg control-stack) :target rsi)
881         (function :scs (descriptor-reg control-stack) :target rax)
882         (old-fp)
883         (return-pc))
884  (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) rsi)
885  (:temporary (:sc unsigned-reg :offset rax-offset :from (:argument 1)) rax)
886  (:temporary (:sc unsigned-reg) call-target)
887  (:vop-var vop)
888  (:generator 75
889    (check-ocfp-and-return-pc old-fp return-pc)
890    ;; Move these into the passing locations if they are not already there.
891    (move rsi args)
892    (move rax function)
893    ;; And jump to the assembly routine.
894    (invoke-asm-routine 'jmp 'tail-call-variable vop call-target)))
895
896;;;; unknown values return
897
898;;; Return a single-value using the Unknown-Values convention.
899;;;
900;;; pfw--get wired-tn conflicts sometimes if register sc specd for args
901;;; having problems targeting args to regs -- using temps instead.
902;;;
903;;; First off, modifying the return-pc defeats the branch-prediction
904;;; optimizations on modern CPUs quite handily. Second, we can do all
905;;; this without needing a temp register. Fixed the latter, at least.
906;;; -- AB 2006/Feb/04
907(define-vop (return-single)
908  (:args (old-fp)
909         (return-pc)
910         (value))
911  (:ignore value)
912  (:generator 6
913    (check-ocfp-and-return-pc old-fp return-pc)
914    ;; Drop stack above old-fp
915    (inst mov rsp-tn rbp-tn)
916    ;; Clear the multiple-value return flag
917    (inst clc)
918    ;; Restore the old frame pointer
919    (inst pop rbp-tn)
920    ;; And return.
921    (inst ret)))
922
923;;; Do unknown-values return of a fixed (other than 1) number of
924;;; values. The VALUES are required to be set up in the standard
925;;; passing locations. NVALS is the number of values returned.
926;;;
927;;; Basically, we just load RCX with the number of values returned and
928;;; RBX with a pointer to the values, set RSP to point to the end of
929;;; the values, and jump directly to return-pc.
930(define-vop (return)
931  (:args (old-fp)
932         (return-pc :to (:eval 1))
933         (values :more t))
934  (:ignore values)
935  (:info nvals)
936  ;; In the case of other than one value, we need these registers to
937  ;; tell the caller where they are and how many there are.
938  (:temporary (:sc unsigned-reg :offset rbx-offset) rbx)
939  (:temporary (:sc unsigned-reg :offset rcx-offset) rcx)
940  ;; We need to stretch the lifetime of return-pc past the argument
941  ;; registers so that we can default the argument registers without
942  ;; trashing return-pc.
943  (:temporary (:sc unsigned-reg :offset (first *register-arg-offsets*)
944                   :from :eval) a0)
945  (:temporary (:sc unsigned-reg :offset (second *register-arg-offsets*)
946                   :from :eval) a1)
947  (:temporary (:sc unsigned-reg :offset (third *register-arg-offsets*)
948                   :from :eval) a2)
949
950  (:generator 6
951    (check-ocfp-and-return-pc old-fp return-pc)
952    (when (= nvals 1)
953      ;; This is handled in RETURN-SINGLE.
954      (error "nvalues is 1"))
955    ;; Establish the values pointer and values count.
956    (inst lea rbx (make-ea :qword :base rbp-tn
957                           :disp (* sp->fp-offset n-word-bytes)))
958    (if (zerop nvals)
959        (zeroize rcx) ; smaller
960        (inst mov rcx (fixnumize nvals)))
961    ;; Pre-default any argument register that need it.
962    (when (< nvals register-arg-count)
963      (let* ((arg-tns (nthcdr nvals (list a0 a1 a2)))
964             (first (first arg-tns)))
965        (inst mov first nil-value)
966        (dolist (tn (cdr arg-tns))
967          (inst mov tn first))))
968    ;; Set the multiple value return flag.
969    (inst stc)
970    ;; And away we go. Except that return-pc is still on the
971    ;; stack and we've changed the stack pointer. So we have to
972    ;; tell it to index off of RBX instead of RBP.
973    (cond ((<= nvals register-arg-count)
974           (inst mov rsp-tn rbp-tn)
975           (inst pop rbp-tn)
976           (inst ret))
977          (t
978           ;; Some values are on the stack after RETURN-PC and OLD-FP,
979           ;; can't return normally and some slots of the frame will
980           ;; be used as temporaries by the receiver.
981           ;;
982           ;; Clear as much of the stack as possible, but not past the
983           ;; old frame address.
984           (inst lea rsp-tn
985                 (make-ea :qword :base rbp-tn
986                          :disp (frame-byte-offset (1- nvals))))
987           (move rbp-tn old-fp)
988           (inst push (make-ea :qword :base rbx
989                               :disp (frame-byte-offset
990                                      (+ sp->fp-offset
991                                         (tn-offset return-pc)))))
992           (inst ret)))))
993
994;;; Do unknown-values return of an arbitrary number of values (passed
995;;; on the stack.) We check for the common case of a single return
996;;; value, and do that inline using the normal single value return
997;;; convention. Otherwise, we branch off to code that calls an
998;;; assembly-routine.
999;;;
1000;;; The assembly routine takes the following args:
1001;;;  RCX -- number of values to find there.
1002;;;  RSI -- pointer to where to find the values.
1003(define-vop (return-multiple)
1004  (:args (old-fp)
1005         (return-pc)
1006         (vals :scs (any-reg) :target rsi)
1007         (nvals :scs (any-reg) :target rcx))
1008  (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 2)) rsi)
1009  (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 3)) rcx)
1010  (:temporary (:sc unsigned-reg) return-asm)
1011  (:temporary (:sc descriptor-reg :offset (first *register-arg-offsets*)
1012                   :from (:eval 0)) a0)
1013  (:node-var node)
1014  (:vop-var vop)
1015  (:generator 13
1016    (check-ocfp-and-return-pc old-fp return-pc)
1017    (unless (policy node (> space speed))
1018      ;; Check for the single case.
1019      (let ((not-single (gen-label)))
1020        (inst cmp nvals (fixnumize 1))
1021        (inst jmp :ne not-single)
1022        ;; Return with one value.
1023        (loadw a0 vals -1)
1024        ;; Clear the stack until ocfp.
1025        (inst mov rsp-tn rbp-tn)
1026        ;; clear the multiple-value return flag
1027        (inst clc)
1028        ;; Out of here.
1029        (inst pop rbp-tn)
1030        (inst ret)
1031        ;; Nope, not the single case. Jump to the assembly routine.
1032        (emit-label not-single)))
1033    (move rsi vals)
1034    (move rcx nvals)
1035    (invoke-asm-routine 'jmp 'return-multiple vop return-asm)))
1036
1037;;;; XEP hackery
1038
1039;;; Get the lexical environment from its passing location.
1040(define-vop (setup-closure-environment)
1041  (:results (closure :scs (descriptor-reg)))
1042  (:info label)
1043  (:ignore label)
1044  (:generator 6
1045    ;; Get result.
1046    (move closure rax-tn)))
1047
1048;;; Copy a &MORE arg from the argument area to the end of the current
1049;;; frame. FIXED is the number of non-&MORE arguments.
1050(define-vop (copy-more-arg)
1051  (:temporary (:sc any-reg :offset r8-offset) copy-index)
1052  (:temporary (:sc any-reg :offset r9-offset) source)
1053  (:temporary (:sc descriptor-reg :offset r10-offset) temp)
1054  (:info fixed min-verified)
1055  (:generator 20
1056    ;; Avoid the copy if there are no more args.
1057    (cond ((zerop fixed)
1058           (inst jrcxz JUST-ALLOC-FRAME))
1059          ((and (eql min-verified fixed)
1060                (> fixed 1))
1061           ;; verify-arg-count will do a CMP
1062           (inst jmp :e JUST-ALLOC-FRAME))
1063          (t
1064           (inst cmp rcx-tn (fixnumize fixed))
1065           (inst jmp :be JUST-ALLOC-FRAME)))
1066
1067    ;; Create a negated copy of the number of arguments to allow us to
1068    ;; use EA calculations in order to do scaled subtraction.
1069    (inst mov temp rcx-tn)
1070    (inst neg temp)
1071
1072    ;; Allocate the space on the stack.
1073    ;; stack = rbp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
1074    ;; if we'd move SP backward, swap the meaning of rsp and source;
1075    ;; otherwise, we'd be accessing values below SP, and that's no good
1076    ;; if a signal interrupts this code sequence.  In that case, store
1077    ;; the final value in rsp after the stack-stack memmove loop.
1078    (inst lea (if (<= fixed (max 3 (sb-allocated-size 'stack)))
1079                  rsp-tn
1080                  source)
1081          (make-ea :qword :base rbp-tn
1082                          :index temp :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1083                          :disp (* n-word-bytes
1084                                   (- (+ sp->fp-offset fixed)
1085                                      (max 3 (sb-allocated-size 'stack))))))
1086
1087    ;; Now: nargs>=1 && nargs>fixed
1088
1089    (cond ((< fixed register-arg-count)
1090           ;; the code above only moves the final value of rsp in
1091           ;; rsp directly if that condition is satisfied.  Currently,
1092           ;; r-a-c is 3, so the aver is OK. If the calling convention
1093           ;; ever changes, the logic above with LEA will have to be
1094           ;; adjusted.
1095           (aver (<= fixed (max 3 (sb-allocated-size 'stack))))
1096           ;; We must stop when we run out of stack args, not when we
1097           ;; run out of more args.
1098           ;; Number to copy = nargs-3
1099           ;; Save the original count of args.
1100           (inst mov rbx-tn rcx-tn)
1101           (inst sub rbx-tn (fixnumize register-arg-count))
1102           ;; Everything of interest in registers.
1103           (inst jmp :be DO-REGS))
1104          (t
1105           ;; Number to copy = nargs-fixed
1106           (inst lea rbx-tn (make-ea :qword :base rcx-tn
1107                                     :disp (- (fixnumize fixed))))))
1108
1109    ;; Initialize R8 to be the end of args.
1110    ;; Swap with SP if necessary to mirror the previous condition
1111    (inst lea (if (<= fixed (max 3 (sb-allocated-size 'stack)))
1112                  source
1113                  rsp-tn)
1114          (make-ea :qword :base rbp-tn
1115                          :index temp :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1116                          :disp (* sp->fp-offset n-word-bytes)))
1117
1118    ;; src: rbp + temp + sp->fp
1119    ;; dst: rbp + temp + sp->fp + (fixed - (max 3 [stack-size]))
1120    (let ((delta (- fixed (max 3 (sb-allocated-size 'stack))))
1121          (loop (gen-label))
1122          (fixnum->word (ash 1 (- word-shift n-fixnum-tag-bits))))
1123      (cond ((zerop delta)) ; no-op move
1124            ((minusp delta)
1125             ;; dst is lower than src, copy forward
1126             (zeroize copy-index)
1127             ;; We used to use REP MOVS here, but on modern x86 it performs
1128             ;; much worse than an explicit loop for small blocks.
1129
1130             (emit-label loop)
1131             (inst mov temp (make-ea :qword :base source :index copy-index))
1132             (inst mov (make-ea :qword :base rsp-tn :index copy-index) temp)
1133             (inst add copy-index n-word-bytes)
1134             (inst sub rbx-tn (fixnumize 1))
1135             (inst jmp :nz loop))
1136            ((plusp delta)
1137             ;; dst is higher than src; copy backward
1138             (emit-label loop)
1139             (inst sub rbx-tn (fixnumize 1))
1140             (inst mov temp (make-ea :qword :base rsp-tn
1141                                     :index rbx-tn :scale fixnum->word))
1142             (inst mov (make-ea :qword :base source
1143                                :index rbx-tn :scale fixnum->word)
1144                   temp)
1145             (inst jmp :nz loop)
1146             ;; done with the stack--stack copy. Reset RSP to its final
1147             ;; value
1148             (inst mov rsp-tn source))))
1149    DO-REGS
1150
1151    ;; Here: nargs>=1 && nargs>fixed
1152    (when (< fixed register-arg-count)
1153      ;; Now we have to deposit any more args that showed up in
1154      ;; registers.
1155      (do ((i fixed))
1156          ( nil )
1157        ;; Store it relative to rbp
1158        (inst mov (make-ea :qword :base rbp-tn
1159                           :disp (* n-word-bytes
1160                                    (- sp->fp-offset
1161                                       (+ 1
1162                                          (- i fixed)
1163                                          (max 3 (sb-allocated-size
1164                                                  'stack))))))
1165              (nth i *register-arg-tns*))
1166
1167        (incf i)
1168        (when (>= i register-arg-count)
1169          (return))
1170
1171        ;; Don't deposit any more than there are.
1172        (if (zerop i)
1173            (inst test rcx-tn rcx-tn)
1174            (inst cmp rcx-tn (fixnumize i)))
1175        (inst jmp :eq DONE)))
1176
1177    (inst jmp DONE)
1178
1179    JUST-ALLOC-FRAME
1180    (inst lea rsp-tn
1181          (make-ea :qword :base rbp-tn
1182                   :disp (* n-word-bytes
1183                            (- sp->fp-offset
1184                               (max 3 (sb-allocated-size 'stack))))))
1185
1186    DONE))
1187
1188(define-vop (more-kw-arg)
1189  (:translate sb!c::%more-kw-arg)
1190  (:policy :fast-safe)
1191  (:args (object :scs (descriptor-reg) :to (:result 1))
1192         (index :scs (any-reg) :to (:result 1) :target keyword))
1193  (:arg-types * tagged-num)
1194  (:results (value :scs (descriptor-reg any-reg))
1195            (keyword :scs (descriptor-reg any-reg)))
1196  (:result-types * *)
1197  (:generator 4
1198     (inst mov value (make-ea :qword :base object :index index
1199                              :scale (ash 1 (- word-shift n-fixnum-tag-bits))))
1200     (inst mov keyword (make-ea :qword :base object :index index
1201                                :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1202                                :disp n-word-bytes))))
1203
1204(define-vop (more-arg/c)
1205  (:translate sb!c::%more-arg)
1206  (:policy :fast-safe)
1207  (:args (object :scs (descriptor-reg) :to (:result 1)))
1208  (:info index)
1209  (:arg-types * (:constant (signed-byte 32)))
1210  (:results (value :scs (descriptor-reg any-reg)))
1211  (:result-types *)
1212  (:generator 3
1213    (inst mov value (make-ea :qword :base object
1214                                    :disp (- (* index n-word-bytes))))))
1215
1216(define-vop (more-arg)
1217  (:translate sb!c::%more-arg)
1218  (:policy :fast-safe)
1219  (:args (object :scs (descriptor-reg) :to (:result 1))
1220         (index :scs (any-reg) :to (:result 1) :target value))
1221  (:arg-types * tagged-num)
1222  (:results (value :scs (descriptor-reg any-reg)))
1223  (:result-types *)
1224  (:generator 4
1225    (move value index)
1226    (inst neg value)
1227    (inst mov value (make-ea :qword :base object :index value
1228                             :scale (ash 1 (- word-shift n-fixnum-tag-bits))))))
1229
1230;;; Turn more arg (context, count) into a list.
1231(define-vop (listify-rest-args)
1232  (:translate %listify-rest-args)
1233  (:policy :safe)
1234  (:args (context :scs (descriptor-reg) :target src)
1235         (count :scs (any-reg) :target rcx))
1236  (:arg-types * tagged-num)
1237  (:temporary (:sc unsigned-reg :offset rsi-offset :from (:argument 0)) src)
1238  (:temporary (:sc unsigned-reg :offset rcx-offset :from (:argument 1)) rcx)
1239  (:temporary (:sc unsigned-reg :offset rax-offset) rax)
1240  (:temporary (:sc unsigned-reg) dst)
1241  (:results (result :scs (descriptor-reg)))
1242  (:node-var node)
1243  (:generator 20
1244    (let ((enter (gen-label))
1245          (loop (gen-label))
1246          (done (gen-label))
1247          (stack-allocate-p (node-stack-allocate-p node)))
1248      (move src context)
1249      (move rcx count)
1250      ;; Check to see whether there are no args, and just return NIL if so.
1251      (inst mov result nil-value)
1252      (inst jrcxz done)
1253      (inst lea dst (make-ea :qword :index rcx :scale (ash 2 (- word-shift n-fixnum-tag-bits))))
1254      (maybe-pseudo-atomic stack-allocate-p
1255       (allocation dst dst node stack-allocate-p list-pointer-lowtag)
1256       ;; Set up the result.
1257       (move result dst)
1258       ;; Jump into the middle of the loop, 'cause that's where we want
1259       ;; to start.
1260       (inst jmp enter)
1261       (emit-label loop)
1262       ;; Compute a pointer to the next cons.
1263       (inst add dst (* cons-size n-word-bytes))
1264       ;; Store a pointer to this cons in the CDR of the previous cons.
1265       (storew dst dst -1 list-pointer-lowtag)
1266       (emit-label enter)
1267       ;; Grab one value and stash it in the car of this cons.
1268       (inst mov rax (make-ea :qword :base src))
1269       (inst sub src n-word-bytes)
1270       (storew rax dst 0 list-pointer-lowtag)
1271       ;; Go back for more.
1272       (inst sub rcx (fixnumize 1))
1273       (inst jmp :nz loop)
1274       ;; NIL out the last cons.
1275       (storew nil-value dst 1 list-pointer-lowtag))
1276      (emit-label done))))
1277
1278;;; Return the location and size of the &MORE arg glob created by
1279;;; COPY-MORE-ARG. SUPPLIED is the total number of arguments supplied
1280;;; (originally passed in RCX). FIXED is the number of non-rest
1281;;; arguments.
1282;;;
1283;;; We must duplicate some of the work done by COPY-MORE-ARG, since at
1284;;; that time the environment is in a pretty brain-damaged state,
1285;;; preventing this info from being returned as values. What we do is
1286;;; compute supplied - fixed, and return a pointer that many words
1287;;; below the current stack top.
1288(define-vop (more-arg-context)
1289  (:policy :fast-safe)
1290  (:translate sb!c::%more-arg-context)
1291  (:args (supplied :scs (any-reg) :target count))
1292  (:arg-types positive-fixnum (:constant fixnum))
1293  (:info fixed)
1294  (:results (context :scs (descriptor-reg))
1295            (count :scs (any-reg)))
1296  (:result-types t tagged-num)
1297  (:note "more-arg-context")
1298  (:generator 5
1299    (move count supplied)
1300    ;; SP at this point points at the last arg pushed.
1301    ;; Point to the first more-arg, not above it.
1302    (inst lea context (make-ea :qword :base rsp-tn
1303                               :index count
1304                               :scale (ash 1 (- word-shift n-fixnum-tag-bits))
1305                               :disp (- (* (1+ fixed) n-word-bytes))))
1306    (unless (zerop fixed)
1307      (inst sub count (fixnumize fixed)))))
1308
1309(define-vop (verify-arg-count)
1310  (:policy :fast-safe)
1311  (:args (nargs :scs (any-reg)))
1312  (:arg-types positive-fixnum (:constant t) (:constant t))
1313  (:info min max)
1314  (:vop-var vop)
1315  (:save-p :compute-only)
1316  (:generator 3
1317    ;; NOTE: copy-more-arg expects this to issue a CMP for min > 1
1318    (let ((err-lab
1319            (generate-error-code vop 'invalid-arg-count-error nargs)))
1320      (flet ((check-min ()
1321               (cond ((= min 1)
1322                      (inst test nargs nargs)
1323                      (inst jmp :e err-lab))
1324                     ((plusp min)
1325                      (inst cmp nargs (fixnumize min))
1326                      (inst jmp :b err-lab)))))
1327        (cond ((not min)
1328               (if (zerop max)
1329                   (inst test nargs nargs)
1330                   (inst cmp nargs (fixnumize max)))
1331               (inst jmp :ne err-lab))
1332              (max
1333               (check-min)
1334               (inst cmp nargs (fixnumize max))
1335               (inst jmp :a err-lab))
1336              (t
1337               (check-min)))))))
1338
1339;; Signal an error about an untagged number.
1340;; These are pretty much boilerplate and could be generic except:
1341;; - the names of the SCs could differ between backends (or maybe not?)
1342;; - in the "/c" case, the older backends don't eval the errcode
1343;; And the 6 vops above ought to be generic too...
1344;; FIXME: there are still some occurrences of
1345;;  note: doing signed word to integer coercion
1346;; in regard to SB-C::%TYPE-CHECK-ERROR. Figure out why.
1347(define-vop (type-check-error/word)
1348  (:policy :fast-safe)
1349  (:translate sb!c::%type-check-error)
1350  (:args (object :scs (signed-reg unsigned-reg))
1351         ;; Types are trees of symbols, so 'any-reg' is not
1352         ;; really possible.
1353         (type :scs (any-reg descriptor-reg constant)))
1354  (:arg-types untagged-num *)
1355  (:vop-var vop)
1356  (:save-p :compute-only)
1357  ;; cost is a smidgen less than type-check-error
1358  ;; otherwise this does not get selected.
1359  (:generator 999
1360    (error-call vop 'object-not-type-error object type)))
1361(define-vop (type-check-error/word/c)
1362  (:policy :fast-safe)
1363  (:translate sb!c::%type-check-error/c)
1364  (:args (object :scs (signed-reg unsigned-reg)))
1365  (:arg-types untagged-num (:constant symbol))
1366  (:info errcode)
1367  (:vop-var vop)
1368  (:save-p :compute-only)
1369  (:generator 899 ; smidgen less than type-check-error/c
1370    (error-call vop errcode object)))
1371
1372;;; Single-stepping
1373
1374(defun emit-single-step-test ()
1375  ;; We use different ways of representing whether stepping is on on
1376  ;; +SB-THREAD / -SB-THREAD: on +SB-THREAD, we use a slot in the
1377  ;; thread structure. On -SB-THREAD we use the value of a static
1378  ;; symbol. Things are done this way, since reading a thread-local
1379  ;; slot from a symbol would require an extra register on +SB-THREAD,
1380  ;; and reading a slot from a thread structure would require an extra
1381  ;; register on -SB-THREAD. While this isn't critical for x86-64,
1382  ;; it's more serious for x86.
1383  #!+sb-thread
1384  (inst cmp (make-ea :qword
1385                     :base thread-base-tn
1386                     :disp (* thread-stepping-slot n-word-bytes))
1387        0)
1388  #!-sb-thread
1389  (inst cmp (make-ea :qword
1390                     :disp (+ nil-value (static-symbol-offset
1391                                         'sb!impl::*stepping*)
1392                              (* symbol-value-slot n-word-bytes)
1393                              (- other-pointer-lowtag)))
1394        0))
1395
1396(define-vop (step-instrument-before-vop)
1397  (:policy :fast-safe)
1398  (:vop-var vop)
1399  (:generator 3
1400     (emit-single-step-test)
1401     (inst jmp :eq DONE)
1402     (inst break single-step-before-trap)
1403     DONE
1404     (note-this-location vop :step-before-vop)))
1405