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