Initial revision
[sbcl.git] / src / compiler / x86 / values.lisp
1 ;;;; unknown-values VOPs for the x86 VM
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13
14 (file-comment
15  "$Header$")
16
17 (define-vop (reset-stack-pointer)
18   (:args (ptr :scs (any-reg)))
19   (:generator 1
20     (move esp-tn ptr)))
21
22 ;;; Push some values onto the stack, returning the start and number of values
23 ;;; pushed as results. It is assumed that the Vals are wired to the standard
24 ;;; argument locations. Nvals is the number of values to push.
25 ;;;
26 ;;; The generator cost is pseudo-random. We could get it right by defining a
27 ;;; bogus SC that reflects the costs of the memory-to-memory moves for each
28 ;;; operand, but this seems unworthwhile.
29 (define-vop (push-values)
30   (:args (vals :more t))
31   (:temporary (:sc unsigned-reg :to (:result 0) :target start) temp)
32   (:results (start) (count))
33   (:info nvals)
34   (:generator 20
35     (move temp esp-tn)                  ; WARN pointing 1 below
36     (do ((val vals (tn-ref-across val)))
37         ((null val))
38       (inst push (tn-ref-tn val)))
39     (move start temp)
40     (inst mov count (fixnumize nvals))))
41
42 ;;; Push a list of values on the stack, returning Start and Count as used in
43 ;;; unknown values continuations.
44 (define-vop (values-list)
45   (:args (arg :scs (descriptor-reg) :target list))
46   (:arg-types list)
47   (:policy :fast-safe)
48   (:results (start :scs (any-reg))
49             (count :scs (any-reg)))
50   (:temporary (:sc descriptor-reg :from (:argument 0) :to (:result 1)) list)
51   (:temporary (:sc descriptor-reg :to (:result 1)) nil-temp)
52   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 1)) eax)
53   (:vop-var vop)
54   (:save-p :compute-only)
55   (:generator 0
56     (move list arg)
57     (move start esp-tn)                 ; WARN pointing 1 below
58     (inst mov nil-temp *nil-value*)
59
60     LOOP
61     (inst cmp list nil-temp)
62     (inst jmp :e done)
63     (pushw list cons-car-slot list-pointer-type)
64     (loadw list list cons-cdr-slot list-pointer-type)
65     (inst mov eax list)
66     (inst and al-tn lowtag-mask)
67     (inst cmp al-tn list-pointer-type)
68     (inst jmp :e loop)
69     (error-call vop bogus-argument-to-values-list-error list)
70
71     DONE
72     (inst mov count start)              ; start is high address
73     (inst sub count esp-tn)))           ; stackp is low address
74
75 ;;; Copy the more arg block to the top of the stack so we can use them
76 ;;; as function arguments.
77 ;;;
78 ;;; Accepts a context as produced by more-arg-context; points to the first
79 ;;; value on the stack, not 4 bytes above as in other contexts.
80 ;;;
81 ;;; Return a context that is 4 bytes above the first value, suitable for
82 ;;; defining a new stack frame.
83 (define-vop (%more-arg-values)
84   (:args (context :scs (descriptor-reg any-reg) :target src)
85          (skip :scs (any-reg immediate))
86          (num :scs (any-reg) :target count))
87   (:arg-types * positive-fixnum positive-fixnum)
88   (:temporary (:sc any-reg :offset esi-offset :from (:argument 0)) src)
89   (:temporary (:sc descriptor-reg :offset eax-offset) temp)
90   (:temporary (:sc unsigned-reg :offset ecx-offset) temp1)
91   (:results (start :scs (any-reg))
92             (count :scs (any-reg)))
93   (:generator 20
94     (sc-case skip
95       (immediate
96        (cond ((zerop (tn-value skip))
97               (move src context)
98               (move count num))
99              (t
100               (inst lea src (make-ea :dword :base context
101                                      :disp (- (* (tn-value skip) word-bytes))))
102               (move count num)
103               (inst sub count (* (tn-value skip) word-bytes)))))
104
105       (any-reg
106        (move src context)
107        (inst sub src skip)
108        (move count num)
109        (inst sub count skip)))
110
111     (move temp1 count)
112     (inst mov start esp-tn)
113     (inst jecxz done)  ; check for 0 count?
114
115     (inst shr temp1 word-shift) ; convert the fixnum to a count.
116
117     (inst std) ; move down the stack as more value are copied to the bottom.
118     LOOP
119     (inst lods temp)
120     (inst push temp)
121     (inst loop loop)
122
123     DONE))
124