UPGRADED-ARRAY-ELEMENT-TYPE: more thoroughly signal errors on unknown types.
[sbcl.git] / src / compiler / x86 / call.lisp
index d9fdc1a..b0bbd96 100644 (file)
@@ -15,7 +15,7 @@
 
 ;;; Return a wired TN describing the N'th full call argument passing
 ;;; location.
-(!def-vm-support-routine standard-arg-location (n)
+(defun standard-arg-location (n)
   (declare (type unsigned-byte n))
   (if (< n register-arg-count)
       (make-wired-tn *backend-t-primitive-type* descriptor-reg-sc-number
@@ -26,7 +26,7 @@
 ;;;
 ;;; Always wire the return PC location to the stack in its standard
 ;;; location.
-(!def-vm-support-routine make-return-pc-passing-location (standard)
+(defun make-return-pc-passing-location (standard)
   (declare (ignore standard))
   (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
                  sap-stack-sc-number return-pc-save-offset))
@@ -38,7 +38,7 @@
 ;;; because we want to be able to assume it's always there. Besides,
 ;;; the x86 doesn't have enough registers to really make it profitable
 ;;; to pass it in a register.
-(!def-vm-support-routine make-old-fp-passing-location (standard)
+(defun make-old-fp-passing-location (standard)
   (declare (ignore standard))
   (make-wired-tn *fixnum-primitive-type* control-stack-sc-number
                  ocfp-save-offset))
 ;;;
 ;;; Without using a save-tn - which does not make much sense if it is
 ;;; wired to the stack?
-(!def-vm-support-routine make-old-fp-save-location (physenv)
+(defun make-old-fp-save-location (physenv)
   (physenv-debug-live-tn (make-wired-tn *fixnum-primitive-type*
                                         control-stack-sc-number
                                         ocfp-save-offset)
                          physenv))
-(!def-vm-support-routine make-return-pc-save-location (physenv)
+(defun make-return-pc-save-location (physenv)
   (physenv-debug-live-tn
    (make-wired-tn (primitive-type-or-lose 'system-area-pointer)
                   sap-stack-sc-number return-pc-save-offset)
 ;;; Make a TN for the standard argument count passing location. We only
 ;;; need to make the standard location, since a count is never passed when we
 ;;; are using non-standard conventions.
-(!def-vm-support-routine make-arg-count-location ()
+(defun make-arg-count-location ()
   (make-wired-tn *fixnum-primitive-type* any-reg-sc-number ecx-offset))
 
 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
 ;;; once per component, and is component-live.
-(!def-vm-support-routine make-nfp-tn ()
+(defun make-nfp-tn ()
   (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
 
-(!def-vm-support-routine make-stack-pointer-tn ()
+(defun make-stack-pointer-tn ()
   (make-normal-tn *fixnum-primitive-type*))
 
-(!def-vm-support-routine make-number-stack-pointer-tn ()
+(defun make-number-stack-pointer-tn ()
   (make-restricted-tn *fixnum-primitive-type* ignore-me-sc-number))
 
 ;;; Return a list of TNs that can be used to represent an unknown-values
 ;;; continuation within a function.
-(!def-vm-support-routine make-unknown-values-locations ()
+(defun make-unknown-values-locations ()
   (list (make-stack-pointer-tn)
         (make-normal-tn *fixnum-primitive-type*)))
 
@@ -87,7 +87,7 @@
 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We
 ;;; push placeholder entries in the CONSTANTS to leave room for
 ;;; additional noise in the code object header.
-(!def-vm-support-routine select-component-format (component)
+(defun select-component-format (component)
   (declare (type component component))
   ;; The 1+ here is because for the x86 the first constant is a
   ;; pointer to a list of fixups, or NIL if the code object has none.
 
     ;; Allocate the space on the stack.
     ;; stack = ebp + sp->fp-offset - (max 3 frame-size) - (nargs - fixed)
+    ;;
+    ;; Problem: this might leave some &more args outside esp, so
+    ;; clamp the movement for now.  If fixed > frame-size, reset
+    ;; esp to the end of the current &more args (which *should*
+    ;; be a noop?), and only set esp to its final value after the
+    ;; stack-stack memmove loop.  Otherwise, an unlucky signal
+    ;; could end up overwriting the &more arguments before they're
+    ;; moved in their final place.
     (inst lea ebx-tn
           (make-ea :dword :base ebp-tn
-                   :disp (* n-word-bytes
-                            (- (+ sp->fp-offset fixed)
-                               (max 3 (sb-allocated-size 'stack))))))
-    (inst sub ebx-tn ecx-tn)  ; Got the new stack in ebx
+                          :disp (* n-word-bytes
+                                   (- sp->fp-offset
+                                      (max 0
+                                           (- (max 3 (sb-allocated-size 'stack))
+                                              fixed))))))
+    (inst sub ebx-tn ecx-tn)          ; Got the new stack in ebx
     (inst mov esp-tn ebx-tn)
 
     ;; Now: nargs>=1 && nargs>fixed
            ;; Number to copy = nargs-fixed
            (inst sub ecx-tn (fixnumize fixed))))
 
-    ;; Save edi and esi register args.
-    (inst push edi-tn)
-    (inst push esi-tn)
-    (inst push ebx-tn)
-    ;; Okay, we have pushed the register args. We can trash them
-    ;; now.
-
-    ;; Initialize src to be end of args.
-    (inst lea esi-tn (make-ea :dword :base ebp-tn
-                              :disp (* sp->fp-offset n-word-bytes)))
-    (inst sub esi-tn ebx-tn)
-
-    ;; We need to copy from downwards up to avoid overwriting some of
-    ;; the yet uncopied args. So we need to use EBX as the copy index
-    ;; and ECX as the loop counter, rather than using ECX for both.
-    (inst xor ebx-tn ebx-tn)
-
-    ;; We used to use REP MOVS here, but on modern x86 it performs
-    ;; much worse than an explicit loop for small blocks.
-    COPY-LOOP
-    (inst mov edi-tn (make-ea :dword :base esi-tn :index ebx-tn))
-    ;; The :DISP is to account for the registers saved on the stack
-    (inst mov (make-ea :dword :base esp-tn :disp (* 3 n-word-bytes)
-                       :index ebx-tn)
-          edi-tn)
-    (inst add ebx-tn n-word-bytes)
-    (inst sub ecx-tn n-word-bytes)
-    (inst jmp :nz COPY-LOOP)
-
-    ;; So now we need to restore EDI and ESI.
-    (inst pop ebx-tn)
-    (inst pop esi-tn)
-    (inst pop edi-tn)
-
+    (let ((delta (* n-word-bytes
+                    (- (max 3 (sb-allocated-size 'stack))
+                       fixed)))
+          (LOOP (gen-label)))
+      (cond ((zerop delta)
+             ;; nothing to move!
+             )
+            ((minusp delta)
+             ;; stack frame smaller than fixed; moving args to higher
+             ;; addresses (stack grows downard), so copy from the
+             ;; end.  Moreover, because we'd have to shrink the frame,
+             ;; esp currently points at the end of the source args.
+             (inst push ebx-tn)
+
+             (emit-label LOOP)
+             (inst sub ecx-tn n-word-bytes)
+             (inst mov ebx-tn (make-ea :dword
+                                       :base esp-tn :index ecx-tn
+                                       ;; compensate for PUSH above
+                                       :disp n-word-bytes))
+             (inst mov (make-ea :dword
+                                :base esp-tn :index ecx-tn
+                                ;; compensate for PUSH, and
+                                ;; add (abs delta)
+                                :disp (- n-word-bytes delta))
+                   ebx-tn)
+             (inst jmp :nz LOOP)
+
+             (inst pop ebx-tn))
+            ((plusp delta)
+             ;; stack frame larger than fixed. Moving args to lower
+             ;; addresses, so copy from the lowest address.  esp
+             ;; already points to the lowest address of the destination.
+             (inst push ebx-tn)
+             (inst push esi-tn)
+
+             (inst xor ebx-tn ebx-tn)
+             (emit-label LOOP)
+             (inst mov esi-tn (make-ea :dword
+                                       :base esp-tn :index ebx-tn
+                                       ;; PUSHed 2 words
+                                       :disp (+ (* 2 n-word-bytes)
+                                                delta)))
+             (inst mov (make-ea :dword
+                                :base esp-tn :index ebx-tn
+                                :disp (* 2 n-word-bytes))
+                   esi-tn)
+             (inst add ebx-tn n-word-bytes)
+             (inst sub ecx-tn n-word-bytes)
+             (inst jmp :nz LOOP)
+
+             (inst pop esi-tn)
+             (inst pop ebx-tn))))
     DO-REGS
+    ;; stack can now be set to its final size
+    (when (< (max 3 (sb-allocated-size 'stack)) fixed)
+      (inst add esp-tn (* n-word-bytes
+                          (- fixed
+                             (max 3 (sb-allocated-size 'stack))))))
 
     ;; Restore ECX
     (inst mov ecx-tn ebx-tn)
   ;; register on -SB-THREAD.
   #!+sb-thread
   (progn
-    (with-tls-ea (EA :base :unused
+    #!+win32 (inst push eax-tn)
+    (with-tls-ea (EA :base #!+win32 eax-tn #!-win32 :unused
                      :disp-type :constant
                      :disp (* thread-stepping-slot n-word-bytes))
-      (inst cmp EA nil-value :maybe-fs)))
+      (inst cmp EA nil-value :maybe-fs))
+    #!+win32 (inst pop eax-tn))
   #!-sb-thread
   (inst cmp (make-ea-for-symbol-value sb!impl::*stepping*)
         nil-value))