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