(in-package "SB!VM")
\f
-;;;; allocation
+;;;; Note: On other platforms ALLOCATE-VECTOR is an assembly routine,
+;;;; but on X86-64 it is a VOP.
-(define-assembly-routine (allocate-vector
- (:policy :fast-safe)
- (:translate allocate-vector)
- (:arg-types positive-fixnum
- positive-fixnum
- positive-fixnum))
- ((:arg type unsigned-reg eax-offset)
- (:arg length any-reg ebx-offset)
- (:arg words any-reg ecx-offset)
- (:res result descriptor-reg edx-offset))
- (inst mov result (+ (1- (ash 1 n-lowtag-bits))
- (* vector-data-offset n-word-bytes)))
- (inst add result words)
- (inst and result (lognot lowtag-mask))
- (pseudo-atomic
- (allocation result result)
- (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
- (storew type result 0 other-pointer-lowtag)
- (storew length result vector-length-slot other-pointer-lowtag))
- (inst ret))
-\f
;;;; Note: CMU CL had assembly language primitives for hashing strings,
;;;; but SBCL doesn't.
\f
;;;; special-purpose inline allocators
+;;; ALLOCATE-VECTOR
+(define-vop (allocate-vector-on-heap)
+ (:args (type :scs (unsigned-reg))
+ (length :scs (any-reg))
+ (words :scs (any-reg)))
+ (:results (result :scs (descriptor-reg) :from :load))
+ (:arg-types positive-fixnum
+ positive-fixnum
+ positive-fixnum)
+ (:policy :fast-safe)
+ (:generator 100
+ (inst lea result (make-ea :byte :base words :disp
+ (+ (1- (ash 1 n-lowtag-bits))
+ (* vector-data-offset n-word-bytes))))
+ (inst and result (lognot lowtag-mask))
+ (pseudo-atomic
+ (allocation result result)
+ (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
+ (storew type result 0 other-pointer-lowtag)
+ (storew length result vector-length-slot other-pointer-lowtag))))
+
+(define-vop (allocate-vector-on-stack)
+ (:args (type :scs (unsigned-reg))
+ (length :scs (any-reg))
+ (words :scs (any-reg) :target ecx))
+ (:temporary (:sc any-reg :offset ecx-offset :from (:argument 2)) ecx)
+ (:temporary (:sc any-reg :offset eax-offset :from (:argument 2)) zero)
+ (:temporary (:sc any-reg :offset edi-offset :from (:argument 0)) res)
+ (:results (result :scs (descriptor-reg) :from :load))
+ (:arg-types positive-fixnum
+ positive-fixnum
+ positive-fixnum)
+ (:translate allocate-vector)
+ (:policy :fast-safe)
+ (:node-var node)
+ (:generator 100
+ (inst lea result (make-ea :byte :base words :disp
+ (+ (1- (ash 1 n-lowtag-bits))
+ (* vector-data-offset n-word-bytes))))
+ (inst and result (lognot lowtag-mask))
+ ;; FIXME: It would be good to check for stack overflow here.
+ (move ecx words)
+ (inst shr ecx n-fixnum-tag-bits)
+ (allocation result result node t)
+ (inst cld)
+ (inst lea res
+ (make-ea :byte :base result :disp (* vector-data-offset n-word-bytes)))
+ (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
+ (storew type result 0 other-pointer-lowtag)
+ (storew length result vector-length-slot other-pointer-lowtag)
+ (inst xor zero zero)
+ (inst rep)
+ (inst stos zero)))
+
+(in-package "SB!C")
+
+(defoptimizer (allocate-vector stack-allocate-result)
+ ((type length words) node)
+ (ecase (policy node stack-allocate-vector)
+ (0 nil)
+ ((1 2)
+ ;; a vector object should fit in one page
+ (values-subtypep (lvar-derived-type words)
+ (load-time-value
+ (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-size*
+ sb!vm:n-word-bytes)
+ sb!vm:vector-data-offset))))))
+ (3 t)))
+
+(defoptimizer (allocate-vector ltn-annotate) ((type length words) call ltn-policy)
+ (let ((args (basic-combination-args call))
+ (template (template-or-lose (if (awhen (node-lvar call)
+ (lvar-dynamic-extent it))
+ 'sb!vm::allocate-vector-on-stack
+ 'sb!vm::allocate-vector-on-heap))))
+ (dolist (arg args)
+ (setf (lvar-info arg)
+ (make-ir2-lvar (primitive-type (lvar-type arg)))))
+ (unless (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
+ (ltn-default-call call)
+ (return-from allocate-vector-ltn-annotate-optimizer (values)))
+ (setf (basic-combination-info call) template)
+ (setf (node-tail-p call) nil)
+
+ (dolist (arg args)
+ (annotate-1-value-lvar arg))))
+
+(in-package "SB!VM")
+
+;;;
(define-vop (allocate-code-object)
(:args (boxed-arg :scs (any-reg) :target boxed)
(unboxed-arg :scs (any-reg) :target unboxed))