2 ;;; **********************************************************************
3 ;;; This code was written as part of the CMU Common Lisp project at
4 ;;; Carnegie Mellon University, and has been placed in the public domain.
8 ;;; **********************************************************************
10 ;;; This file contains the VM definition of function call for the Alpha.
12 ;;; Written by Rob MacLachlan
14 ;;; Converted for the Alpha by Sean Hallgren
20 ;;;; Interfaces to IR2 conversion:
22 ;;; Standard-Argument-Location -- Interface
24 ;;; Return a wired TN describing the N'th full call argument passing
27 (!def-vm-support-routine standard-argument-location (n)
28 (declare (type unsigned-byte n))
29 (if (< n register-arg-count)
30 (make-wired-tn *backend-t-primitive-type*
32 (elt *register-arg-offsets* n))
33 (make-wired-tn *backend-t-primitive-type*
34 control-stack-arg-scn n)))
37 ;;; Make-Return-PC-Passing-Location -- Interface
39 ;;; Make a passing location TN for a local call return PC. If standard is
40 ;;; true, then use the standard (full call) location, otherwise use any legal
41 ;;; location. Even in the non-standard case, this may be restricted by a
42 ;;; desire to use a subroutine call instruction.
44 (!def-vm-support-routine make-return-pc-passing-location (standard)
45 #!+gengc (declare (ignore standard))
48 (make-wired-tn *backend-t-primitive-type* register-arg-scn lra-offset)
49 (make-restricted-tn *backend-t-primitive-type* register-arg-scn))
51 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ra-offset))
53 ;;; Make-Old-FP-Passing-Location -- Interface
55 ;;; Similar to Make-Return-PC-Passing-Location, but makes a location to pass
56 ;;; Old-FP in. This is (obviously) wired in the standard convention, but is
57 ;;; totally unrestricted in non-standard conventions, since we can always fetch
58 ;;; it off of the stack using the arg pointer.
60 (!def-vm-support-routine make-old-fp-passing-location (standard)
62 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ocfp-offset)
63 (make-normal-tn *fixnum-primitive-type*)))
65 ;;; Make-Old-FP-Save-Location, Make-Return-PC-Save-Location -- Interface
67 ;;; Make the TNs used to hold Old-FP and Return-PC within the current
68 ;;; function. We treat these specially so that the debugger can find them at a
71 (!def-vm-support-routine make-old-fp-save-location (env)
73 (environment-debug-live-tn (make-normal-tn *fixnum-primitive-type*) env)
74 (make-wired-tn *fixnum-primitive-type*
78 (!def-vm-support-routine make-return-pc-save-location (env)
79 (let ((ptype #!-gengc *backend-t-primitive-type*
80 #!+gengc *fixnum-primitive-type*))
82 (environment-debug-live-tn (make-normal-tn ptype) env)
83 (make-wired-tn ptype control-stack-arg-scn
84 #!-gengc lra-save-offset #!+gengc ra-save-offset))))
86 ;;; Make-Argument-Count-Location -- Interface
88 ;;; Make a TN for the standard argument count passing location. We only
89 ;;; need to make the standard location, since a count is never passed when we
90 ;;; are using non-standard conventions.
92 (!def-vm-support-routine make-argument-count-location ()
93 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nargs-offset))
96 ;;; MAKE-NFP-TN -- Interface
98 ;;; Make a TN to hold the number-stack frame pointer. This is allocated
99 ;;; once per component, and is component-live.
101 (!def-vm-support-routine make-nfp-tn ()
103 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nfp-offset)))
105 ;;; MAKE-STACK-POINTER-TN ()
107 (!def-vm-support-routine make-stack-pointer-tn ()
108 (make-normal-tn *fixnum-primitive-type*))
110 ;;; MAKE-NUMBER-STACK-POINTER-TN ()
112 (!def-vm-support-routine make-number-stack-pointer-tn ()
113 (make-normal-tn *fixnum-primitive-type*))
115 ;;; Make-Unknown-Values-Locations -- Interface
117 ;;; Return a list of TNs that can be used to represent an unknown-values
118 ;;; continuation within a function.
120 (!def-vm-support-routine make-unknown-values-locations ()
121 (list (make-stack-pointer-tn)
122 (make-normal-tn *fixnum-primitive-type*)))
125 ;;; Select-Component-Format -- Interface
127 ;;; This function is called by the Entry-Analyze phase, allowing
128 ;;; VM-dependent initialization of the IR2-Component structure. We push
129 ;;; placeholder entries in the Constants to leave room for additional
130 ;;; noise in the code object header.
132 (!def-vm-support-routine select-component-format (component)
133 (declare (type component component))
134 (dotimes (i code-constants-offset)
135 (vector-push-extend nil
136 (ir2-component-constants (component-info component))))
142 ;;; BYTES-NEEDED-FOR-NON-DESCRIPTOR-STACK-FRAME -- internal
144 ;;; Return the number of bytes needed for the current non-descriptor stack
145 ;;; frame. Non-descriptor stack frames must be multiples of 8 bytes on
148 (defun bytes-needed-for-non-descriptor-stack-frame ()
149 (* (logandc2 (1+ (sb-allocated-size 'non-descriptor-stack)) 1)
152 ;;; Used for setting up the Old-FP in local call.
154 (define-vop (current-fp)
155 (:results (val :scs (any-reg)))
159 ;;; Used for computing the caller's NFP for use in known-values return. Only
160 ;;; works assuming there is no variable size stuff on the nstack.
162 (define-vop (compute-old-nfp)
163 (:results (val :scs (any-reg)))
166 (let ((nfp (current-nfp-tn vop)))
168 (inst addq nfp (bytes-needed-for-non-descriptor-stack-frame) val)))))
171 (define-vop (xep-allocate-frame)
172 (:info start-lab copy-more-arg-follows)
173 (:ignore copy-more-arg-follows)
175 (:temporary (:scs (non-descriptor-reg)) temp)
177 ;; Make sure the function is aligned, and drop a label pointing to this
180 (trace-table-entry trace-table-function-prologue)
181 (emit-label start-lab)
182 ;; Allocate function header.
183 (inst function-header-word)
184 (dotimes (i (1- function-code-offset))
186 ;; The start of the actual code.
187 ;; Compute CODE from the address of this entry point.
188 (let ((entry-point (gen-label)))
189 (emit-label entry-point)
190 (inst compute-code-from-fn code-tn lip-tn entry-point temp)
191 ;; ### We should also save it on the stack so that the garbage collector
192 ;; won't forget about us if we call anyone else.
194 ;; Build our stack frames.
195 (inst lda csp-tn (* word-bytes (sb-allocated-size 'control-stack)) cfp-tn)
196 (let ((nfp (current-nfp-tn vop)))
198 (inst subq nsp-tn (bytes-needed-for-non-descriptor-stack-frame)
201 (trace-table-entry trace-table-normal)))
203 (define-vop (allocate-frame)
204 (:results (res :scs (any-reg))
205 (nfp :scs (any-reg)))
208 (trace-table-entry trace-table-function-prologue)
210 (inst lda csp-tn (* word-bytes (sb-allocated-size 'control-stack)) csp-tn)
211 (when (ir2-environment-number-stack-p callee)
212 (inst subq nsp-tn (bytes-needed-for-non-descriptor-stack-frame)
215 (trace-table-entry trace-table-normal)))
217 ;;; Allocate a partial frame for passing stack arguments in a full call. Nargs
218 ;;; is the number of arguments passed. If no stack arguments are passed, then
219 ;;; we don't have to do anything.
221 (define-vop (allocate-full-call-frame)
223 (:results (res :scs (any-reg)))
225 (when (> nargs register-arg-count)
227 (inst lda csp-tn (* nargs word-bytes) csp-tn))))
231 ;;; Default-Unknown-Values -- Internal
233 ;;; Emit code needed at the return-point from an unknown-values call for a
234 ;;; fixed number of values. Values is the head of the TN-Ref list for the
235 ;;; locations that the values are to be received into. Nvals is the number of
236 ;;; values that are to be received (should equal the length of Values).
238 ;;; Move-Temp is a Descriptor-Reg TN used as a temporary.
240 ;;; This code exploits the fact that in the unknown-values convention, a
241 ;;; single value return returns at the return PC + 8, whereas a return of other
242 ;;; than one value returns directly at the return PC.
244 ;;; If 0 or 1 values are expected, then we just emit an instruction to reset
245 ;;; the SP (which will only be executed when other than 1 value is returned.)
247 ;;; In the general case, we have to do three things:
248 ;;; -- Default unsupplied register values. This need only be done when a
249 ;;; single value is returned, since register values are defaulted by the
250 ;;; called in the non-single case.
251 ;;; -- Default unsupplied stack values. This needs to be done whenever there
252 ;;; are stack values.
253 ;;; -- Reset SP. This must be done whenever other than 1 value is returned,
254 ;;; regardless of the number of values desired.
256 ;;; The general-case code looks like this:
258 b regs-defaulted ; Skip if MVs
261 move a1 null-tn ; Default register values
263 loadi nargs 1 ; Force defaulting of stack values
264 move ocfp csp ; Set up args for SP resetting
267 subu temp nargs register-arg-count
269 bltz temp default-value-7 ; jump to default code
271 loadw move-temp ocfp-tn 6 ; Move value to correct location.
272 store-stack-tn val4-tn move-temp
274 bltz temp default-value-8
276 loadw move-temp ocfp-tn 7
277 store-stack-tn val5-tn move-temp
282 move sp ocfp ; Reset SP.
287 store-stack-tn val4-tn null-tn ; Nil out 7'th value. (first on stack)
290 store-stack-tn val5-tn null-tn ; Nil out 8'th value.
298 (defun default-unknown-values (vop values nvals move-temp temp lra-label)
299 (declare (type (or tn-ref null) values)
300 (type unsigned-byte nvals) (type tn move-temp temp))
303 ;; Note that this is a single-value return point. This is actually
304 ;; the multiple-value entry point for a single desired value, but
305 ;; the code location has to be here, or the debugger backtrace
307 (without-scheduling ()
308 (note-this-location vop :single-value-return)
309 (move ocfp-tn csp-tn)
312 #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
313 #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp)))
314 (let ((regs-defaulted (gen-label))
315 (defaulting-done (gen-label))
316 (default-stack-vals (gen-label)))
317 (without-scheduling ()
318 ;; Note that this is an unknown-values return point.
319 (note-this-location vop :unknown-return)
320 ;; If there are no stack results, clear the stack now.
321 (if (> nvals register-arg-count)
322 (inst subq nargs-tn (fixnumize register-arg-count) temp)
323 (move ocfp-tn csp-tn))
324 ;; Branch off to the MV case.
325 (inst br zero-tn regs-defaulted))
327 ;; Do the single value case.
329 (val (tn-ref-across values) (tn-ref-across val)))
330 ((= i (min nvals register-arg-count)))
331 (move null-tn (tn-ref-tn val)))
332 (when (> nvals register-arg-count)
333 (move csp-tn ocfp-tn)
334 (inst br zero-tn default-stack-vals))
336 (emit-label regs-defaulted)
338 (when (> nvals register-arg-count)
339 ;; If there are stack results, we have to default them
340 ;; and clear the stack.
341 (collect ((defaults))
342 (do ((i register-arg-count (1+ i))
343 (val (do ((i 0 (1+ i))
344 (val values (tn-ref-across val)))
345 ((= i register-arg-count) val))
346 (tn-ref-across val)))
349 (let ((default-lab (gen-label))
350 (tn (tn-ref-tn val)))
351 (defaults (cons default-lab tn))
353 (inst blt temp default-lab)
354 (inst ldl move-temp (* i word-bytes) ocfp-tn)
355 (inst subq temp (fixnumize 1) temp)
356 (store-stack-tn tn move-temp)))
358 (emit-label defaulting-done)
359 (move ocfp-tn csp-tn)
361 (let ((defaults (defaults)))
363 (assemble (*elsewhere*)
364 (emit-label default-stack-vals)
365 (do ((remaining defaults (cdr remaining)))
367 (let ((def (car remaining)))
368 (emit-label (car def))
369 (when (null (cdr remaining))
370 (inst br zero-tn defaulting-done))
371 (store-stack-tn (cdr def) null-tn)))))))
374 #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
375 #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp))))
379 ;;;; Unknown values receiving:
381 ;;; Receive-Unknown-Values -- Internal
383 ;;; Emit code needed at the return point for an unknown-values call for an
384 ;;; arbitrary number of values.
386 ;;; We do the single and non-single cases with no shared code: there doesn't
387 ;;; seem to be any potential overlap, and receiving a single value is more
388 ;;; important efficiency-wise.
390 ;;; When there is a single value, we just push it on the stack, returning
391 ;;; the old SP and 1.
393 ;;; When there is a variable number of values, we move all of the argument
394 ;;; registers onto the stack, and return Args and Nargs.
396 ;;; Args and Nargs are TNs wired to the named locations. We must
397 ;;; explicitly allocate these TNs, since their lifetimes overlap with the
398 ;;; results Start and Count (also, it's nice to be able to target them).
400 (defun receive-unknown-values (args nargs start count lra-label temp)
401 (declare (type tn args nargs start count temp))
402 (let ((variable-values (gen-label))
404 (without-scheduling ()
405 (inst br zero-tn variable-values)
409 #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
410 #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp))
411 (inst addq csp-tn 4 csp-tn)
412 (storew (first *register-arg-tns*) csp-tn -1)
413 (inst subq csp-tn 4 start)
414 (inst li (fixnumize 1) count)
418 (assemble (*elsewhere*)
419 (emit-label variable-values)
421 #!-gengc (inst compute-code-from-lra code-tn code-tn lra-label temp)
422 #!+gengc (inst compute-code-from-ra code-tn ra-tn lra-label temp))
423 (do ((arg *register-arg-tns* (rest arg))
426 (storew (first arg) args i))
429 (inst br zero-tn done)))
433 ;;; VOP that can be inherited by unknown values receivers. The main thing this
434 ;;; handles is allocation of the result temporaries.
436 (define-vop (unknown-values-receiver)
438 (start :scs (any-reg))
439 (count :scs (any-reg)))
440 (:temporary (:sc descriptor-reg :offset ocfp-offset
441 :from :eval :to (:result 0))
443 (:temporary (:sc any-reg :offset nargs-offset
444 :from :eval :to (:result 1))
446 (:temporary (:scs (non-descriptor-reg)) temp))
450 ;;;; Local call with unknown values convention return:
452 ;;; Non-TR local call for a fixed number of values passed according to the
453 ;;; unknown values convention.
455 ;;; Args are the argument passing locations, which are specified only to
456 ;;; terminate their lifetimes in the caller.
458 ;;; Values are the return value locations (wired to the standard passing
461 ;;; Save is the save info, which we can ignore since saving has been done.
462 ;;; Return-PC is the TN that the return PC should be passed in.
463 ;;; Target is a continuation pointing to the start of the called function.
464 ;;; Nvals is the number of values received.
466 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
467 ;;; registers may be tied up by the more operand. Instead, we use
468 ;;; MAYBE-LOAD-STACK-TN.
470 (define-vop (call-local)
474 (:results (values :more t))
476 (:move-args :local-call)
477 (:info arg-locs callee target nvals)
479 (:temporary (:scs (descriptor-reg) :from :eval) move-temp)
480 (:temporary (:scs (non-descriptor-reg)) temp)
481 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
482 (:temporary (:sc any-reg :offset ocfp-offset :from :eval) ocfp)
483 (:ignore arg-locs args ocfp)
485 (let ((label (gen-label))
486 (cur-nfp (current-nfp-tn vop)))
488 (store-stack-tn nfp-save cur-nfp))
489 (let ((callee-nfp (callee-nfp-tn callee)))
490 (maybe-load-stack-nfp-tn callee-nfp nfp temp))
491 (maybe-load-stack-tn cfp-tn fp)
492 (trace-table-entry trace-table-call-site)
493 (inst compute-lra-from-code
494 (callee-return-pc-tn callee) code-tn label temp)
495 (note-this-location vop :call-site)
496 (inst br zero-tn target)
497 (trace-table-entry trace-table-normal)
498 (emit-return-pc label)
499 (default-unknown-values vop values nvals move-temp temp label)
500 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))))
503 ;;; Non-TR local call for a variable number of return values passed according
504 ;;; to the unknown values convention. The results are the start of the values
505 ;;; glob and the number of values received.
507 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
508 ;;; registers may be tied up by the more operand. Instead, we use
509 ;;; MAYBE-LOAD-STACK-TN.
511 (define-vop (multiple-call-local unknown-values-receiver)
516 (:move-args :local-call)
517 (:info save callee target)
520 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
521 (:temporary (:scs (non-descriptor-reg)) temp)
523 (let ((label (gen-label))
524 (cur-nfp (current-nfp-tn vop)))
526 (store-stack-tn nfp-save cur-nfp))
527 (let ((callee-nfp (callee-nfp-tn callee)))
528 (maybe-load-stack-nfp-tn callee-nfp nfp temp))
529 (maybe-load-stack-tn cfp-tn fp)
530 (trace-table-entry trace-table-call-site)
531 (inst compute-lra-from-code
532 (callee-return-pc-tn callee) code-tn label temp)
533 (note-this-location vop :call-site)
534 (inst bsr zero-tn target)
535 (trace-table-entry trace-table-normal)
536 (emit-return-pc label)
537 (note-this-location vop :unknown-return)
538 (receive-unknown-values values-start nvals start count label temp)
539 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))))
542 ;;;; Local call with known values return:
544 ;;; Non-TR local call with known return locations. Known-value return works
545 ;;; just like argument passing in local call.
547 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
548 ;;; registers may be tied up by the more operand. Instead, we use
549 ;;; MAYBE-LOAD-STACK-TN.
551 (define-vop (known-call-local)
555 (:results (res :more t))
556 (:move-args :local-call)
558 (:info save callee target)
559 (:ignore args res save)
561 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
562 (:temporary (:scs (non-descriptor-reg)) temp)
564 (let (#!-gengc (label (gen-label))
565 (cur-nfp (current-nfp-tn vop)))
567 (store-stack-tn nfp-save cur-nfp))
568 (let ((callee-nfp (callee-nfp-tn callee)))
569 (maybe-load-stack-nfp-tn callee-nfp nfp temp))
570 (maybe-load-stack-tn cfp-tn fp)
571 (trace-table-entry trace-table-call-site)
572 (inst compute-lra-from-code
573 (callee-return-pc-tn callee) code-tn label temp)
574 (note-this-location vop :call-site)
575 (inst bsr zero-tn target)
576 (trace-table-entry trace-table-normal)
577 (emit-return-pc label)
578 (note-this-location vop :known-return)
579 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))))
581 ;;; Return from known values call. We receive the return locations as
582 ;;; arguments to terminate their lifetimes in the returning function. We
583 ;;; restore FP and CSP and jump to the Return-PC.
585 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
586 ;;; registers may be tied up by the more operand. Instead, we use
587 ;;; MAYBE-LOAD-STACK-TN.
589 (define-vop (known-return)
590 (:args (ocfp :target ocfp-temp)
591 (return-pc :target return-pc-temp)
593 (:temporary (:sc any-reg :from (:argument 0)) ocfp-temp)
594 (:temporary (:sc #!-gengc descriptor-reg #!+gengc any-reg :from (:argument 1))
596 #!-gengc (:temporary (:scs (interior-reg)) lip)
597 (:move-args :known-return)
599 (:ignore val-locs vals)
602 (trace-table-entry trace-table-function-epilogue)
603 (maybe-load-stack-tn ocfp-temp ocfp)
604 (maybe-load-stack-tn return-pc-temp return-pc)
606 (let ((cur-nfp (current-nfp-tn vop)))
608 (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
610 (inst subq return-pc-temp (- other-pointer-type word-bytes) lip)
611 (move ocfp-temp cfp-tn)
612 (inst ret zero-tn lip 1)
613 (trace-table-entry trace-table-normal)))
618 ;;; There is something of a cross-product effect with full calls. Different
619 ;;; versions are used depending on whether we know the number of arguments or
620 ;;; the name of the called function, and whether we want fixed values, unknown
621 ;;; values, or a tail call.
623 ;;; In full call, the arguments are passed creating a partial frame on the
624 ;;; stack top and storing stack arguments into that frame. On entry to the
625 ;;; callee, this partial frame is pointed to by FP. If there are no stack
626 ;;; arguments, we don't bother allocating a partial frame, and instead set FP
627 ;;; to SP just before the call.
629 ;;; Define-Full-Call -- Internal
631 ;;; This macro helps in the definition of full call VOPs by avoiding code
632 ;;; replication in defining the cross-product VOPs.
634 ;;; Name is the name of the VOP to define.
636 ;;; Named is true if the first argument is a symbol whose global function
637 ;;; definition is to be called.
639 ;;; Return is either :Fixed, :Unknown or :Tail:
640 ;;; -- If :Fixed, then the call is for a fixed number of values, returned in
641 ;;; the standard passing locations (passed as result operands).
642 ;;; -- If :Unknown, then the result values are pushed on the stack, and the
643 ;;; result values are specified by the Start and Count as in the
644 ;;; unknown-values continuation representation.
645 ;;; -- If :Tail, then do a tail-recursive call. No values are returned.
646 ;;; The Ocfp and Return-PC are passed as the second and third arguments.
648 ;;; In non-tail calls, the pointer to the stack arguments is passed as the last
649 ;;; fixed argument. If Variable is false, then the passing locations are
650 ;;; passed as a more arg. Variable is true if there are a variable number of
651 ;;; arguments passed on the stack. Variable cannot be specified with :Tail
652 ;;; return. TR variable argument call is implemented separately.
654 ;;; In tail call with fixed arguments, the passing locations are passed as a
655 ;;; more arg, but there is no new-FP, since the arguments have been set up in
656 ;;; the current frame.
658 (defmacro define-full-call (name named return variable)
659 (assert (not (and variable (eq return :tail))))
661 ,@(when (eq return :unknown)
662 '(unknown-values-receiver)))
664 ,@(unless (eq return :tail)
665 '((new-fp :scs (any-reg) :to :eval)))
668 '(name :target name-pass)
669 '(arg-fun :target lexenv))
671 ,@(when (eq return :tail)
672 '((ocfp :target ocfp-pass)
673 (return-pc :target return-pc-pass)))
675 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
677 ,@(when (eq return :fixed)
678 '((:results (values :more t))))
680 (:save-p ,(if (eq return :tail) :compute-only t))
682 ,@(unless (or (eq return :tail) variable)
683 '((:move-args :full-call)))
686 (:info ,@(unless (or variable (eq return :tail)) '(arg-locs))
687 ,@(unless variable '(nargs))
688 ,@(when (eq return :fixed) '(nvals)))
690 (:ignore #!+gengc ,@(unless (eq return :tail) '(return-pc-pass))
691 ,@(unless (or variable (eq return :tail)) '(arg-locs))
692 ,@(unless variable '(args)))
694 (:temporary (:sc descriptor-reg
697 ,@(unless (eq return :fixed)
701 (:temporary (:sc descriptor-reg
702 :offset #!-gengc lra-offset #!+gengc ra-offset
703 :from (:argument ,(if (eq return :tail) 2 1))
708 `((:temporary (:sc descriptor-reg :offset fdefn-offset
709 :from (:argument ,(if (eq return :tail) 0 1))
713 `((:temporary (:sc descriptor-reg :offset lexenv-offset
714 :from (:argument ,(if (eq return :tail) 0 1))
718 (:temporary (:scs (descriptor-reg) :from (:argument 0) :to :eval)
721 (:temporary (:sc any-reg :offset nargs-offset :to :eval)
725 (mapcar #'(lambda (name offset)
726 `(:temporary (:sc descriptor-reg
730 register-arg-names *register-arg-offsets*))
731 ,@(when (eq return :fixed)
732 '((:temporary (:scs (descriptor-reg) :from :eval) move-temp)))
734 ,@(unless (eq return :tail)
735 '((:temporary (:scs (non-descriptor-reg)) temp)
736 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)))
738 (:temporary (:sc interior-reg :offset lip-offset) entry-point)
740 (:generator ,(+ (if named 5 0)
742 (if (eq return :tail) 0 10)
744 (if (eq return :unknown) 25 0))
745 (let* ((cur-nfp (current-nfp-tn vop))
746 ,@(unless (eq return :tail)
747 '((lra-label (gen-label))))
751 ,@(if (eq return :tail)
752 '((unless (location= ocfp ocfp-pass)
754 (unless (location= return-pc
765 (flet ((do-next-filler ()
766 (let* ((next (pop filler))
767 (what (if (consp next) (car next) next)))
771 `((inst subq csp-tn new-fp nargs-pass)
773 (mapcar #'(lambda (name)
778 register-arg-names)))
779 '((inst li (fixnumize nargs) nargs-pass))))
780 ,@(if (eq return :tail)
784 (inst move ocfp ocfp-pass))
787 (ash (tn-offset ocfp)
792 (#!-gengc descriptor-reg #!+gengc any-reg
793 (inst move return-pc return-pc-pass))
795 (inst ldl return-pc-pass
796 (ash (tn-offset return-pc)
801 (bytes-needed-for-non-descriptor-stack-frame)
805 (inst compute-lra-from-code
806 return-pc-pass code-tn lra-label temp))
808 (store-stack-tn nfp-save cur-nfp))
810 (inst move cfp-tn ocfp-pass))
813 '(move new-fp cfp-tn)
814 '(if (> nargs register-arg-count)
816 (move csp-tn cfp-tn)))
817 (trace-table-entry trace-table-call-site))))
822 (descriptor-reg (move name name-pass))
825 (ash (tn-offset name) word-shift) cfp-tn)
829 (- (ash (tn-offset name) word-shift)
830 other-pointer-type) code-tn)
832 (inst ldl entry-point
833 (- (ash fdefn-raw-addr-slot word-shift)
834 other-pointer-type) name-pass)
837 (descriptor-reg (move arg-fun lexenv))
840 (ash (tn-offset arg-fun) word-shift) cfp-tn)
844 (- (ash (tn-offset arg-fun) word-shift)
845 other-pointer-type) code-tn)
849 (- (ash closure-function-slot word-shift)
850 function-pointer-type) lexenv)
855 (- (ash function-code-offset word-shift)
856 function-pointer-type) entry-point)
858 (inst ldl entry-point
859 (- (ash closure-entry-point-slot word-shift)
860 function-pointer-type) lexenv)
868 (note-this-location vop :call-site)
870 (inst jsr zero-tn entry-point))
874 '((trace-table-entry trace-table-normal)
875 (emit-return-pc lra-label)
876 (default-unknown-values vop values nvals
877 move-temp temp lra-label)
878 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp)))
880 '((trace-table-entry trace-table-normal)
881 (emit-return-pc lra-label)
882 (note-this-location vop :unknown-return)
883 (receive-unknown-values values-start nvals start count
885 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp)))
889 (define-full-call call nil :fixed nil)
890 (define-full-call call-named t :fixed nil)
891 (define-full-call multiple-call nil :unknown nil)
892 (define-full-call multiple-call-named t :unknown nil)
893 (define-full-call tail-call nil :tail nil)
894 (define-full-call tail-call-named t :tail nil)
896 (define-full-call call-variable nil :fixed t)
897 (define-full-call multiple-call-variable nil :unknown t)
900 ;;; Defined separately, since needs special code that BLT's the arguments
903 (define-vop (tail-call-variable)
905 (args-arg :scs (any-reg) :target args)
906 (function-arg :scs (descriptor-reg) :target lexenv)
907 (ocfp-arg :scs (any-reg) :target ocfp)
908 (lra-arg :scs (#!-gengc descriptor-reg #!+gengc any-reg) :target lra))
910 (:temporary (:sc any-reg :offset nl0-offset :from (:argument 0)) args)
911 (:temporary (:sc any-reg :offset lexenv-offset :from (:argument 1)) lexenv)
912 (:temporary (:sc any-reg :offset ocfp-offset :from (:argument 2)) ocfp)
913 (:temporary (:sc any-reg :offset #!-gengc lra-offset #!+gengc ra-offset
914 :from (:argument 3)) lra)
915 (:temporary (:scs (non-descriptor-reg)) temp)
921 ;; Move these into the passing locations if they are not already there.
923 (move function-arg lexenv)
927 ;; Clear the number stack if anything is there.
928 (let ((cur-nfp (current-nfp-tn vop)))
930 (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
933 ;; And jump to the assembly-routine that does the bliting.
934 (inst li (make-fixup 'tail-call-variable :assembly-routine) temp)
935 (inst jmp zero-tn temp)))
938 ;;;; Unknown values return:
940 ;;; Return a single value using the unknown-values convention.
942 (define-vop (return-single)
943 (:args (ocfp :scs (any-reg))
944 #!-gengc (return-pc :scs (descriptor-reg))
945 #!+gengc (return-pc :scs (any-reg) :target ra)
948 #!-gengc (:temporary (:scs (interior-reg)) lip)
949 #!+gengc (:temporary (:sc any-reg :offset ra-offset :from (:argument 1)) ra)
950 #!+gengc (:temporary (:scs (any-reg) :from (:argument 1)) temp)
953 ;; Clear the number stack.
954 (trace-table-entry trace-table-function-epilogue)
955 (let ((cur-nfp (current-nfp-tn vop)))
957 (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
959 ;; Clear the control stack, and restore the frame pointer.
963 #!-gengc (lisp-return return-pc lip :offset 2)
966 (inst addq return-pc (* 2 word-bytes) temp)
967 (unless (location= ra return-pc)
968 (inst move ra return-pc))
969 (inst ret zero-tn temp 1))
970 (trace-table-entry trace-table-normal)))
973 ;;; Do unknown-values return of a fixed number of values. The Values are
974 ;;; required to be set up in the standard passing locations. Nvals is the
975 ;;; number of values returned.
977 ;;; If returning a single value, then deallocate the current frame, restore
978 ;;; FP and jump to the single-value entry at Return-PC + 8.
980 ;;; If returning other than one value, then load the number of values returned,
981 ;;; NIL out unsupplied values registers, restore FP and return at Return-PC.
982 ;;; When there are stack values, we must initialize the argument pointer to
983 ;;; point to the beginning of the values block (which is the beginning of the
987 (:args (ocfp :scs (any-reg))
988 (return-pc :scs (#!-gengc descriptor-reg #!+gengc any-reg) :to (:eval 1)
989 #!+gengc :target #!+gengc ra)
993 (:temporary (:sc descriptor-reg :offset a0-offset :from (:eval 0)) a0)
994 (:temporary (:sc descriptor-reg :offset a1-offset :from (:eval 0)) a1)
995 (:temporary (:sc descriptor-reg :offset a2-offset :from (:eval 0)) a2)
996 (:temporary (:sc descriptor-reg :offset a3-offset :from (:eval 0)) a3)
997 (:temporary (:sc descriptor-reg :offset a4-offset :from (:eval 0)) a4)
998 (:temporary (:sc descriptor-reg :offset a5-offset :from (:eval 0)) a5)
999 (:temporary (:sc any-reg :offset nargs-offset) nargs)
1000 (:temporary (:sc any-reg :offset ocfp-offset) val-ptr)
1001 #!-gengc (:temporary (:scs (interior-reg)) lip)
1002 #!+gengc (:temporary (:sc any-reg :offset ra-offset :from (:eval 1)) ra)
1005 ;; Clear the number stack.
1006 (trace-table-entry trace-table-function-epilogue)
1007 (let ((cur-nfp (current-nfp-tn vop)))
1009 (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
1011 ;; Establish the values pointer and values count.
1012 (move cfp-tn val-ptr)
1013 (inst li (fixnumize nvals) nargs)
1014 ;; restore the frame pointer and clear as much of the control
1015 ;; stack as possible.
1017 (inst addq val-ptr (* nvals word-bytes) csp-tn)
1018 ;; pre-default any argument register that need it.
1019 (when (< nvals register-arg-count)
1020 (dolist (reg (subseq (list a0 a1 a2 a3 a4 a5) nvals))
1021 (move null-tn reg)))
1023 (lisp-return return-pc lip)
1024 (trace-table-entry trace-table-normal)))
1026 ;;; Do unknown-values return of an arbitrary number of values (passed on the
1027 ;;; stack.) We check for the common case of a single return value, and do that
1028 ;;; inline using the normal single value return convention. Otherwise, we
1029 ;;; branch off to code that calls an assembly-routine.
1031 (define-vop (return-multiple)
1032 (:args (ocfp-arg :scs (any-reg) :target ocfp)
1033 #!-gengc (lra-arg :scs (descriptor-reg) :target lra)
1034 #!+gengc (return-pc :scs (any-reg) :target ra)
1035 (vals-arg :scs (any-reg) :target vals)
1036 (nvals-arg :scs (any-reg) :target nvals))
1038 (:temporary (:sc any-reg :offset nl1-offset :from (:argument 0)) ocfp)
1040 (:temporary (:sc descriptor-reg :offset lra-offset :from (:argument 1)) lra)
1042 (:temporary (:sc any-reg :offset ra-offset :from (:argument 1)) ra)
1043 (:temporary (:sc any-reg :offset nl0-offset :from (:argument 2)) vals)
1044 (:temporary (:sc any-reg :offset nargs-offset :from (:argument 3)) nvals)
1045 (:temporary (:sc descriptor-reg :offset a0-offset) a0)
1046 (:temporary (:scs (non-descriptor-reg)) temp)
1047 #!-gengc (:temporary (:scs (interior-reg)) lip)
1048 #!+gengc (:temporary (:scs (any-reg) :from (:argument 0)) temp)
1053 (trace-table-entry trace-table-function-epilogue)
1054 (let ((not-single (gen-label)))
1055 ;; Clear the number stack.
1056 (let ((cur-nfp (current-nfp-tn vop)))
1058 (inst addq cur-nfp (bytes-needed-for-non-descriptor-stack-frame)
1061 ;; Check for the single case.
1062 (inst li (fixnumize 1) a0)
1063 (inst cmpeq nvals-arg a0 temp)
1064 (inst ldl a0 0 vals-arg)
1065 (inst beq temp not-single)
1067 ;; Return with one value.
1068 (move cfp-tn csp-tn)
1069 (move ocfp-arg cfp-tn)
1070 (lisp-return lra-arg lip :offset 2)
1072 ;; Nope, not the single case.
1073 (emit-label not-single)
1074 (move ocfp-arg ocfp)
1076 (move vals-arg vals)
1077 (move nvals-arg nvals)
1078 (inst li (make-fixup 'return-multiple :assembly-routine) temp)
1079 (inst jmp zero-tn temp))
1080 (trace-table-entry trace-table-normal)))
1087 ;;; We don't need to do anything special for regular functions.
1089 (define-vop (setup-environment)
1093 ;; Don't bother doing anything.
1096 ;;; Get the lexical environment from its passing location.
1098 (define-vop (setup-closure-environment)
1099 (:temporary (:sc descriptor-reg :offset lexenv-offset :target closure
1102 (:results (closure :scs (descriptor-reg)))
1107 (move lexenv closure)))
1109 ;;; Copy a more arg from the argument area to the end of the current frame.
1110 ;;; Fixed is the number of non-more arguments.
1112 (define-vop (copy-more-arg)
1113 (:temporary (:sc any-reg :offset nl0-offset) result)
1114 (:temporary (:sc any-reg :offset nl1-offset) count)
1115 (:temporary (:sc any-reg :offset nl2-offset) src)
1116 (:temporary (:sc any-reg :offset nl4-offset) dst)
1117 (:temporary (:sc descriptor-reg :offset l0-offset) temp)
1120 (let ((loop (gen-label))
1121 (do-regs (gen-label))
1123 (when (< fixed register-arg-count)
1124 ;; Save a pointer to the results so we can fill in register args.
1125 ;; We don't need this if there are more fixed args than reg args.
1126 (move csp-tn result))
1127 ;; Allocate the space on the stack.
1128 (cond ((zerop fixed)
1129 (inst addq csp-tn nargs-tn csp-tn)
1130 (inst beq nargs-tn done))
1132 (inst subq nargs-tn (fixnumize fixed) count)
1133 (inst ble count done)
1134 (inst addq csp-tn count csp-tn)))
1135 (when (< fixed register-arg-count)
1136 ;; We must stop when we run out of stack args, not when we run out of
1138 (inst subq nargs-tn (fixnumize register-arg-count) count))
1139 ;; Initialize dst to be end of stack.
1141 ;; Everything of interest in registers.
1142 (inst ble count do-regs)
1143 ;; Initialize src to be end of args.
1144 (inst addq cfp-tn nargs-tn src)
1147 ;; *--dst = *--src, --count
1148 (inst subq src word-bytes src)
1149 (inst subq count (fixnumize 1) count)
1151 (inst subq dst word-bytes dst)
1153 (inst bgt count loop)
1155 (emit-label do-regs)
1156 (when (< fixed register-arg-count)
1157 ;; Now we have to deposit any more args that showed up in registers.
1158 ;; We know there is at least one more arg, otherwise we would have
1159 ;; branched to done up at the top.
1160 (inst subq nargs-tn (fixnumize (1+ fixed)) count)
1161 (do ((i fixed (1+ i)))
1162 ((>= i register-arg-count))
1163 ;; Store it relative to the pointer saved at the start.
1164 (storew (nth i *register-arg-tns*) result (- i fixed))
1165 ;; Is this the last one?
1166 (inst beq count done)
1168 (inst subq count (fixnumize 1) count)))
1169 (emit-label done))))
1172 ;;; More args are stored consequtively on the stack, starting immediately at
1173 ;;; the context pointer. The context pointer is not typed, so the lowtag is 0.
1175 (define-full-reffer more-arg * 0 0 (descriptor-reg any-reg) * %more-arg)
1178 ;;; Turn more arg (context, count) into a list.
1180 (define-vop (listify-rest-args)
1181 (:args (context-arg :target context :scs (descriptor-reg))
1182 (count-arg :target count :scs (any-reg)))
1183 (:arg-types * tagged-num)
1184 (:temporary (:scs (any-reg) :from (:argument 0)) context)
1185 (:temporary (:scs (any-reg) :from (:argument 1)) count)
1186 (:temporary (:scs (descriptor-reg) :from :eval) temp dst)
1187 (:results (result :scs (descriptor-reg)))
1188 (:translate %listify-rest-args)
1191 (let ((enter (gen-label))
1194 (move context-arg context)
1195 (move count-arg count)
1196 ;; Check to see if there are any arguments.
1197 (move null-tn result)
1198 (inst beq count done)
1200 ;; We need to do this atomically.
1202 ;; Allocate a cons (2 words) for each item.
1203 (inst bis alloc-tn list-pointer-type result)
1205 (inst sll count 1 temp)
1206 (inst addq alloc-tn temp alloc-tn)
1207 (inst br zero-tn enter)
1209 ;; Store the current cons in the cdr of the previous cons.
1211 (inst addq dst (* 2 word-bytes) dst)
1212 (storew dst dst -1 list-pointer-type)
1216 (loadw temp context)
1217 (inst addq context word-bytes context)
1219 ;; Store the value in the car (in delay slot)
1220 (storew temp dst 0 list-pointer-type)
1222 ;; Dec count, and if != zero, go back for more.
1223 (inst subq count (fixnumize 1) count)
1224 (inst bne count loop)
1226 ;; NIL out the last cons.
1227 (storew null-tn dst 1 list-pointer-type))
1228 (emit-label done))))
1230 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1231 ;;; Supplied is the total number of arguments supplied (originally passed in
1232 ;;; NARGS.) Fixed is the number of non-rest arguments.
1234 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1235 ;;; time the environment is in a pretty brain-damaged state, preventing this
1236 ;;; info from being returned as values. What we do is compute
1237 ;;; supplied - fixed, and return a pointer that many words below the current
1240 (define-vop (more-arg-context)
1241 (:policy :fast-safe)
1242 (:translate sb!c::%more-arg-context)
1243 (:args (supplied :scs (any-reg)))
1244 (:arg-types tagged-num (:constant fixnum))
1246 (:results (context :scs (descriptor-reg))
1247 (count :scs (any-reg)))
1248 (:result-types t tagged-num)
1249 (:note "more-arg-context")
1251 (inst subq supplied (fixnumize fixed) count)
1252 (inst subq csp-tn count context)))
1255 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1257 (define-vop (verify-argument-count)
1258 (:policy :fast-safe)
1259 (:translate sb!c::%verify-argument-count)
1260 (:args (nargs :scs (any-reg)))
1261 (:arg-types positive-fixnum (:constant t))
1262 (:temporary (:scs (any-reg) :type fixnum) temp)
1265 (:save-p :compute-only)
1268 (generate-error-code vop invalid-argument-count-error nargs)))
1269 (cond ((zerop count)
1270 (inst bne nargs err-lab))
1272 (inst subq nargs (fixnumize count) temp)
1273 (inst bne temp err-lab))))))
1275 ;;; Various other error signalers.
1277 (macrolet ((frob (name error translate &rest args)
1278 `(define-vop (,name)
1280 `((:policy :fast-safe)
1281 (:translate ,translate)))
1282 (:args ,@(mapcar #'(lambda (arg)
1283 `(,arg :scs (any-reg descriptor-reg)))
1286 (:save-p :compute-only)
1288 (error-call vop ,error ,@args)))))
1289 (frob argument-count-error invalid-argument-count-error
1290 sb!c::%argument-count-error nargs)
1291 (frob type-check-error object-not-type-error sb!c::%type-check-error
1293 (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1295 (frob odd-key-arguments-error odd-key-arguments-error
1296 sb!c::%odd-key-arguments-error)
1297 (frob unknown-key-argument-error unknown-key-argument-error
1298 sb!c::%unknown-key-argument-error key)
1299 (frob nil-function-returned-error nil-function-returned-error nil fun))