1 ;;;; the VM definition of function call for MIPS
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;;; Interfaces to IR2 conversion:
16 ;;; Return a wired TN describing the N'th full call argument passing
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*
23 (elt *register-arg-offsets* n))
24 (make-wired-tn *backend-t-primitive-type*
25 control-stack-arg-scn n)))
28 ;;; Make a passing location TN for a local call return PC. If standard is
29 ;;; true, then use the standard (full call) location, otherwise use any legal
30 ;;; location. Even in the non-standard case, this may be restricted by a
31 ;;; desire to use a subroutine call instruction.
32 (!def-vm-support-routine make-return-pc-passing-location (standard)
34 (make-wired-tn *backend-t-primitive-type* register-arg-scn lra-offset)
35 (make-restricted-tn *backend-t-primitive-type* register-arg-scn)))
37 ;;; This is similar to MAKE-RETURN-PC-PASSING-LOCATION, but makes a
38 ;;; location to pass OLD-FP in. This is (obviously) wired in the
39 ;;; standard convention, but is totally unrestricted in non-standard
40 ;;; conventions, since we can always fetch it off of the stack using
42 (!def-vm-support-routine make-old-fp-passing-location (standard)
44 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn ocfp-offset)
45 (make-normal-tn *fixnum-primitive-type*)))
47 ;;; Make the TNs used to hold OLD-FP and RETURN-PC within the current
48 ;;; function. We treat these specially so that the debugger can find
49 ;;; them at a known location.
50 (!def-vm-support-routine make-old-fp-save-location (env)
52 (physenv-debug-live-tn (make-normal-tn *fixnum-primitive-type*) env)
53 (make-wired-tn *fixnum-primitive-type*
56 (!def-vm-support-routine make-return-pc-save-location (env)
57 (let ((ptype *backend-t-primitive-type*))
59 (physenv-debug-live-tn (make-normal-tn ptype) env)
60 (make-wired-tn ptype control-stack-arg-scn lra-save-offset))))
62 ;;; Make a TN for the standard argument count passing location. We only
63 ;;; need to make the standard location, since a count is never passed when we
64 ;;; are using non-standard conventions.
65 (!def-vm-support-routine make-arg-count-location ()
66 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nargs-offset))
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 ()
73 (make-wired-tn *fixnum-primitive-type* immediate-arg-scn nfp-offset)))
75 (!def-vm-support-routine make-stack-pointer-tn ()
76 (make-normal-tn *fixnum-primitive-type*))
78 (!def-vm-support-routine make-number-stack-pointer-tn ()
79 (make-normal-tn *fixnum-primitive-type*))
81 ;;; Return a list of TNs that can be used to represent an unknown-values
82 ;;; continuation within a function.
83 (!def-vm-support-routine make-unknown-values-locations ()
84 (list (make-stack-pointer-tn)
85 (make-normal-tn *fixnum-primitive-type*)))
88 ;;; This function is called by the ENTRY-ANALYZE phase, allowing
89 ;;; VM-dependent initialization of the IR2-COMPONENT structure. We push
90 ;;; placeholder entries in the Constants to leave room for additional
91 ;;; noise in the code object header.
92 (!def-vm-support-routine select-component-format (component)
93 (declare (type component component))
94 (dotimes (i code-constants-offset)
95 (vector-push-extend nil
96 (ir2-component-constants (component-info component))))
102 ;;; BYTES-NEEDED-FOR-NON-DESCRIPTOR-STACK-FRAME -- internal
104 ;;; Return the number of bytes needed for the current non-descriptor stack
105 ;;; frame. Non-descriptor stack frames must be multiples of 8 bytes on
108 (defun bytes-needed-for-non-descriptor-stack-frame ()
109 (* (logandc2 (1+ (sb-allocated-size 'non-descriptor-stack)) 1)
112 ;;; Used for setting up the Old-FP in local call.
114 (define-vop (current-fp)
115 (:results (val :scs (any-reg)))
119 ;;; Used for computing the caller's NFP for use in known-values return. Only
120 ;;; works assuming there is no variable size stuff on the nstack.
122 (define-vop (compute-old-nfp)
123 (:results (val :scs (any-reg)))
126 (let ((nfp (current-nfp-tn vop)))
128 (inst addu val nfp (bytes-needed-for-non-descriptor-stack-frame))))))
131 (define-vop (xep-allocate-frame)
132 (:info start-lab copy-more-arg-follows)
133 (:ignore copy-more-arg-follows)
135 (:temporary (:scs (non-descriptor-reg)) temp)
137 ;; Make sure the function is aligned, and drop a label pointing to this
139 (align n-lowtag-bits)
140 (trace-table-entry trace-table-fun-prologue)
141 (emit-label start-lab)
142 ;; Allocate function header.
143 (inst simple-fun-header-word)
144 (dotimes (i (1- simple-fun-code-offset))
146 ;; The start of the actual code.
147 ;; Compute CODE from the address of this entry point.
148 (let ((entry-point (gen-label)))
149 (emit-label entry-point)
150 (inst compute-code-from-lip code-tn lip-tn entry-point temp)
151 ;; ### We should also save it on the stack so that the garbage collector
152 ;; won't forget about us if we call anyone else.
154 ;; Build our stack frames.
155 (inst addu csp-tn cfp-tn
156 (* n-word-bytes (sb-allocated-size 'control-stack)))
157 (let ((nfp (current-nfp-tn vop)))
159 (inst addu nsp-tn nsp-tn
160 (- (bytes-needed-for-non-descriptor-stack-frame)))
162 (trace-table-entry trace-table-normal)))
164 (define-vop (allocate-frame)
165 (:results (res :scs (any-reg))
166 (nfp :scs (any-reg)))
169 (trace-table-entry trace-table-fun-prologue)
171 (inst addu csp-tn csp-tn
172 (* n-word-bytes (sb-allocated-size 'control-stack)))
173 (when (ir2-physenv-number-stack-p callee)
174 (inst addu nsp-tn nsp-tn
175 (- (bytes-needed-for-non-descriptor-stack-frame)))
177 (trace-table-entry trace-table-normal)))
179 ;;; Allocate a partial frame for passing stack arguments in a full call. Nargs
180 ;;; is the number of arguments passed. If no stack arguments are passed, then
181 ;;; we don't have to do anything.
183 (define-vop (allocate-full-call-frame)
185 (:results (res :scs (any-reg)))
187 (when (> nargs register-arg-count)
189 (inst addu csp-tn csp-tn (* nargs n-word-bytes)))))
194 ;;; Emit code needed at the return-point from an unknown-values call for a
195 ;;; fixed number of values. Values is the head of the TN-Ref list for the
196 ;;; locations that the values are to be received into. Nvals is the number of
197 ;;; values that are to be received (should equal the length of Values).
199 ;;; MOVE-TEMP is a DESCRIPTOR-REG TN used as a temporary.
201 ;;; This code exploits the fact that in the unknown-values convention, a
202 ;;; single value return returns at the return PC + 8, whereas a return of other
203 ;;; than one value returns directly at the return PC.
205 ;;; If 0 or 1 values are expected, then we just emit an instruction to reset
206 ;;; the SP (which will only be executed when other than 1 value is returned.)
208 ;;; In the general case, we have to do three things:
209 ;;; -- Default unsupplied register values. This need only be done when a
210 ;;; single value is returned, since register values are defaulted by the
211 ;;; callee in the non-single case.
212 ;;; -- Default unsupplied stack values. This needs to be done whenever there
213 ;;; are stack values.
214 ;;; -- Reset SP. This must be done whenever other than 1 value is returned,
215 ;;; regardless of the number of values desired.
217 ;;; The general-case code looks like this:
219 b regs-defaulted ; Skip if MVs
222 move a1 null-tn ; Default register values
224 loadi nargs 1 ; Force defaulting of stack values
225 move ocfp csp ; Set up args for SP resetting
228 subu temp nargs register-arg-count
230 bltz temp default-value-7 ; jump to default code
232 loadw move-temp ocfp-tn 6 ; Move value to correct location.
233 store-stack-tn val4-tn move-temp
235 bltz temp default-value-8
237 loadw move-temp ocfp-tn 7
238 store-stack-tn val5-tn move-temp
243 move csp ocfp ; Reset SP.
248 store-stack-tn val4-tn null-tn ; Nil out 7'th value. (first on stack)
251 store-stack-tn val5-tn null-tn ; Nil out 8'th value.
259 (defun default-unknown-values (vop values nvals move-temp temp lra-label)
260 (declare (type (or tn-ref null) values)
261 (type unsigned-byte nvals) (type tn move-temp temp))
264 ;; Note that this is a single-value return point. This is actually
265 ;; the multiple-value entry point for a single desired value, but
266 ;; the code location has to be here, or the debugger backtrace
268 (without-scheduling ()
269 (note-this-location vop :single-value-return)
270 (inst move csp-tn ocfp-tn)
273 (inst compute-code-from-lra code-tn code-tn lra-label temp)))
274 (let ((regs-defaulted (gen-label))
275 (defaulting-done (gen-label))
276 (default-stack-vals (gen-label)))
277 (without-scheduling ()
278 ;; Note that this is an unknown-values return point.
279 (note-this-location vop :unknown-return)
280 ;; Branch off to the MV case.
281 (inst b regs-defaulted)
282 ;; If there are no stack results, clear the stack now.
283 (if (> nvals register-arg-count)
284 (inst addu temp nargs-tn (fixnumize (- register-arg-count)))
285 (move csp-tn ocfp-tn t)))
287 ;; Do the single value case.
289 (val (tn-ref-across values) (tn-ref-across val)))
290 ((= i (min nvals register-arg-count)))
291 (move (tn-ref-tn val) null-tn))
292 (when (> nvals register-arg-count)
293 (inst b default-stack-vals)
294 (move ocfp-tn csp-tn t))
296 (emit-label regs-defaulted)
298 (when (> nvals register-arg-count)
299 ;; If there are stack results, we have to default them
300 ;; and clear the stack.
301 (collect ((defaults))
302 (do ((i register-arg-count (1+ i))
303 (val (do ((i 0 (1+ i))
304 (val values (tn-ref-across val)))
305 ((= i register-arg-count) val))
306 (tn-ref-across val)))
309 (let ((default-lab (gen-label))
310 (tn (tn-ref-tn val)))
311 (defaults (cons default-lab tn))
313 (inst blez temp default-lab)
314 (inst lw move-temp ocfp-tn (* i n-word-bytes))
315 (inst addu temp temp (fixnumize -1))
316 (store-stack-tn tn move-temp)))
318 (emit-label defaulting-done)
319 (move csp-tn ocfp-tn)
321 (let ((defaults (defaults)))
323 (assemble (*elsewhere*)
324 (emit-label default-stack-vals)
325 (do ((remaining defaults (cdr remaining)))
327 (let ((def (car remaining)))
328 (emit-label (car def))
329 (when (null (cdr remaining))
330 (inst b defaulting-done))
331 (store-stack-tn (cdr def) null-tn)))))))
334 (inst compute-code-from-lra code-tn code-tn lra-label temp))))
338 ;;;; Unknown values receiving:
340 ;;; Emit code needed at the return point for an unknown-values call for an
341 ;;; arbitrary number of values.
343 ;;; We do the single and non-single cases with no shared code: there doesn't
344 ;;; seem to be any potential overlap, and receiving a single value is more
345 ;;; important efficiency-wise.
347 ;;; When there is a single value, we just push it on the stack, returning
348 ;;; the old SP and 1.
350 ;;; When there is a variable number of values, we move all of the argument
351 ;;; registers onto the stack, and return Args and Nargs.
353 ;;; Args and Nargs are TNs wired to the named locations. We must
354 ;;; explicitly allocate these TNs, since their lifetimes overlap with the
355 ;;; results Start and Count (also, it's nice to be able to target them).
357 (defun receive-unknown-values (args nargs start count lra-label temp)
358 (declare (type tn args nargs start count temp))
359 (let ((variable-values (gen-label))
361 (without-scheduling ()
362 (inst b variable-values)
366 (inst compute-code-from-lra code-tn code-tn lra-label temp))
367 (inst addu csp-tn csp-tn 4)
368 (storew (first *register-arg-tns*) csp-tn -1)
369 (inst addu start csp-tn -4)
370 (inst li count (fixnumize 1))
374 (assemble (*elsewhere*)
375 (trace-table-entry trace-table-fun-prologue)
376 (emit-label variable-values)
378 (inst compute-code-from-lra code-tn code-tn lra-label temp))
379 (do ((arg *register-arg-tns* (rest arg))
382 (storew (first arg) args i))
386 (trace-table-entry trace-table-normal)))
390 ;;; VOP that can be inherited by unknown values receivers. The main thing this
391 ;;; handles is allocation of the result temporaries.
393 (define-vop (unknown-values-receiver)
395 (start :scs (any-reg))
396 (count :scs (any-reg)))
397 (:temporary (:sc descriptor-reg :offset ocfp-offset
398 :from :eval :to (:result 0))
400 (:temporary (:sc any-reg :offset nargs-offset
401 :from :eval :to (:result 1))
403 (:temporary (:scs (non-descriptor-reg)) temp))
407 ;;;; Local call with unknown values convention return:
409 ;;; Non-TR local call for a fixed number of values passed according to the
410 ;;; unknown values convention.
412 ;;; Args are the argument passing locations, which are specified only to
413 ;;; terminate their lifetimes in the caller.
415 ;;; Values are the return value locations (wired to the standard passing
418 ;;; Save is the save info, which we can ignore since saving has been done.
419 ;;; Return-PC is the TN that the return PC should be passed in.
420 ;;; Target is a continuation pointing to the start of the called function.
421 ;;; Nvals is the number of values received.
423 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
424 ;;; registers may be tied up by the more operand. Instead, we use
425 ;;; MAYBE-LOAD-STACK-TN.
427 (define-vop (call-local)
431 (:results (values :more t))
433 (:move-args :local-call)
434 (:info arg-locs callee target nvals)
436 (:temporary (:scs (descriptor-reg) :from :eval) move-temp)
437 (:temporary (:scs (non-descriptor-reg)) temp)
438 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
439 (:temporary (:sc any-reg :offset ocfp-offset :from :eval) ocfp)
440 (:ignore arg-locs args ocfp)
442 (let ((label (gen-label))
443 (cur-nfp (current-nfp-tn vop)))
445 (store-stack-tn nfp-save cur-nfp))
446 (let ((callee-nfp (callee-nfp-tn callee)))
448 (maybe-load-stack-tn callee-nfp nfp)))
449 (maybe-load-stack-tn cfp-tn fp)
450 (trace-table-entry trace-table-call-site)
451 (inst compute-lra-from-code
452 (callee-return-pc-tn callee) code-tn label temp)
453 (note-this-location vop :call-site)
456 (trace-table-entry trace-table-normal)
457 (emit-return-pc label)
458 (default-unknown-values vop values nvals move-temp temp label)
460 (load-stack-tn cur-nfp nfp-save)))))
463 ;;; Non-TR local call for a variable number of return values passed according
464 ;;; to the unknown values convention. The results are the start of the values
465 ;;; glob and the number of values received.
467 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
468 ;;; registers may be tied up by the more operand. Instead, we use
469 ;;; MAYBE-LOAD-STACK-TN.
471 (define-vop (multiple-call-local unknown-values-receiver)
476 (:move-args :local-call)
477 (:info save callee target)
480 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
482 (let ((label (gen-label))
483 (cur-nfp (current-nfp-tn vop)))
485 (store-stack-tn nfp-save cur-nfp))
486 (let ((callee-nfp (callee-nfp-tn callee)))
488 (maybe-load-stack-tn callee-nfp nfp)))
489 (maybe-load-stack-tn cfp-tn fp)
490 (trace-table-entry trace-table-call-site)
491 (inst compute-lra-from-code
492 (callee-return-pc-tn callee) code-tn label temp)
493 (note-this-location vop :call-site)
496 (trace-table-entry trace-table-normal)
497 (emit-return-pc label)
498 (note-this-location vop :unknown-return)
499 (receive-unknown-values values-start nvals start count label temp)
501 (load-stack-tn cur-nfp nfp-save)))))
504 ;;;; Local call with known values return:
506 ;;; Non-TR local call with known return locations. Known-value return works
507 ;;; just like argument passing in local call.
509 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
510 ;;; registers may be tied up by the more operand. Instead, we use
511 ;;; MAYBE-LOAD-STACK-TN.
513 (define-vop (known-call-local)
517 (:results (res :more t))
518 (:move-args :local-call)
520 (:info save callee target)
521 (:ignore args res save)
523 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
524 (:temporary (:scs (non-descriptor-reg)) temp)
526 (let ((label (gen-label))
527 (cur-nfp (current-nfp-tn vop)))
529 (store-stack-tn nfp-save cur-nfp))
530 (let ((callee-nfp (callee-nfp-tn callee)))
532 (maybe-load-stack-tn callee-nfp nfp)))
533 (maybe-load-stack-tn cfp-tn fp)
534 (trace-table-entry trace-table-call-site)
535 (inst compute-lra-from-code
536 (callee-return-pc-tn callee) code-tn label temp)
537 (note-this-location vop :call-site)
540 (trace-table-entry trace-table-normal)
541 (emit-return-pc label)
542 (note-this-location vop :known-return)
544 (load-stack-tn cur-nfp nfp-save)))))
546 ;;; Return from known values call. We receive the return locations as
547 ;;; arguments to terminate their lifetimes in the returning function. We
548 ;;; restore FP and CSP and jump to the Return-PC.
550 ;;; Note: we can't use normal load-tn allocation for the fixed args, since all
551 ;;; registers may be tied up by the more operand. Instead, we use
552 ;;; MAYBE-LOAD-STACK-TN.
554 (define-vop (known-return)
555 (:args (ocfp :target ocfp-temp)
556 (return-pc :target return-pc-temp)
558 (:temporary (:sc any-reg :from (:argument 0)) ocfp-temp)
559 (:temporary (:sc descriptor-reg :from (:argument 1))
561 (:temporary (:scs (interior-reg)) lip)
562 (:move-args :known-return)
564 (:ignore val-locs vals)
567 (trace-table-entry trace-table-fun-epilogue)
568 (maybe-load-stack-tn ocfp-temp ocfp)
569 (maybe-load-stack-tn return-pc-temp return-pc)
571 (let ((cur-nfp (current-nfp-tn vop)))
573 (inst addu nsp-tn cur-nfp
574 (bytes-needed-for-non-descriptor-stack-frame))))
575 (inst addu lip return-pc-temp (- n-word-bytes other-pointer-lowtag))
577 (move cfp-tn ocfp-temp t)
578 (trace-table-entry trace-table-normal)))
583 ;;; There is something of a cross-product effect with full calls. Different
584 ;;; versions are used depending on whether we know the number of arguments or
585 ;;; the name of the called function, and whether we want fixed values, unknown
586 ;;; values, or a tail call.
588 ;;; In full call, the arguments are passed creating a partial frame on the
589 ;;; stack top and storing stack arguments into that frame. On entry to the
590 ;;; callee, this partial frame is pointed to by FP. If there are no stack
591 ;;; arguments, we don't bother allocating a partial frame, and instead set FP
592 ;;; to SP just before the call.
594 ;;; This macro helps in the definition of full call VOPs by avoiding code
595 ;;; replication in defining the cross-product VOPs.
597 ;;; Name is the name of the VOP to define.
599 ;;; Named is true if the first argument is a symbol whose global function
600 ;;; definition is to be called.
602 ;;; Return is either :Fixed, :Unknown or :Tail:
603 ;;; -- If :Fixed, then the call is for a fixed number of values, returned in
604 ;;; the standard passing locations (passed as result operands).
605 ;;; -- If :Unknown, then the result values are pushed on the stack, and the
606 ;;; result values are specified by the Start and Count as in the
607 ;;; unknown-values continuation representation.
608 ;;; -- If :Tail, then do a tail-recursive call. No values are returned.
609 ;;; The Ocfp and Return-PC are passed as the second and third arguments.
611 ;;; In non-tail calls, the pointer to the stack arguments is passed as the last
612 ;;; fixed argument. If Variable is false, then the passing locations are
613 ;;; passed as a more arg. Variable is true if there are a variable number of
614 ;;; arguments passed on the stack. Variable cannot be specified with :Tail
615 ;;; return. TR variable argument call is implemented separately.
617 ;;; In tail call with fixed arguments, the passing locations are passed as a
618 ;;; more arg, but there is no new-FP, since the arguments have been set up in
619 ;;; the current frame.
621 (defmacro define-full-call (name named return variable)
622 (aver (not (and variable (eq return :tail))))
624 ,@(when (eq return :unknown)
625 '(unknown-values-receiver)))
627 ,@(unless (eq return :tail)
628 '((new-fp :scs (any-reg) :to :eval)))
631 '(name :target name-pass)
632 '(arg-fun :target lexenv))
634 ,@(when (eq return :tail)
635 '((ocfp :target ocfp-pass)
636 (return-pc :target return-pc-pass)))
638 ,@(unless variable '((args :more t :scs (descriptor-reg)))))
640 ,@(when (eq return :fixed)
641 '((:results (values :more t))))
643 (:save-p ,(if (eq return :tail) :compute-only t))
645 ,@(unless (or (eq return :tail) variable)
646 '((:move-args :full-call)))
649 (:info ,@(unless (or variable (eq return :tail)) '(arg-locs))
650 ,@(unless variable '(nargs))
651 ,@(when (eq return :fixed) '(nvals)))
653 (:ignore ,@(unless (or variable (eq return :tail)) '(arg-locs))
654 ,@(unless variable '(args)))
656 (:temporary (:sc descriptor-reg
659 ,@(unless (eq return :fixed)
663 (:temporary (:sc descriptor-reg
665 :from (:argument ,(if (eq return :tail) 2 1))
670 `((:temporary (:sc descriptor-reg :offset fdefn-offset
671 :from (:argument ,(if (eq return :tail) 0 1))
675 `((:temporary (:sc descriptor-reg :offset lexenv-offset
676 :from (:argument ,(if (eq return :tail) 0 1))
679 (:temporary (:scs (descriptor-reg) :from (:argument 0) :to :eval)
682 (:temporary (:sc any-reg :offset nargs-offset :to :eval)
686 (mapcar #'(lambda (name offset)
687 `(:temporary (:sc descriptor-reg
691 register-arg-names *register-arg-offsets*))
692 ,@(when (eq return :fixed)
693 '((:temporary (:scs (descriptor-reg) :from :eval) move-temp)))
695 ,@(unless (eq return :tail)
696 '((:temporary (:scs (non-descriptor-reg)) temp)
697 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)))
699 (:temporary (:sc interior-reg :offset lip-offset) entry-point)
701 (:generator ,(+ (if named 5 0)
703 (if (eq return :tail) 0 10)
705 (if (eq return :unknown) 25 0))
706 (let* ((cur-nfp (current-nfp-tn vop))
707 ,@(unless (eq return :tail)
708 '((lra-label (gen-label))))
712 ,@(if (eq return :tail)
713 '((unless (location= ocfp ocfp-pass)
715 (unless (location= return-pc
725 (flet ((do-next-filler ()
726 (let* ((next (pop filler))
727 (what (if (consp next) (car next) next)))
731 `((inst subu nargs-pass csp-tn new-fp)
733 (mapcar #'(lambda (name)
734 `(inst lw ,name new-fp
737 register-arg-names)))
738 '((inst li nargs-pass (fixnumize nargs)))))
739 ,@(if (eq return :tail)
743 (move ocfp-pass ocfp t))
745 (inst lw ocfp-pass cfp-tn
746 (ash (tn-offset ocfp)
751 (move return-pc-pass return-pc t))
753 (inst lw return-pc-pass cfp-tn
754 (ash (tn-offset return-pc)
757 (inst addu nsp-tn cur-nfp
758 (bytes-needed-for-non-descriptor-stack-frame))))
760 (inst compute-lra-from-code
761 return-pc-pass code-tn lra-label temp))
763 (store-stack-tn nfp-save cur-nfp))
765 (move ocfp-pass cfp-tn t))
768 '(move cfp-tn new-fp)
769 '(if (> nargs register-arg-count)
771 (move cfp-tn csp-tn)))
772 (trace-table-entry trace-table-call-site))))
778 (descriptor-reg (move name-pass name))
780 (inst lw name-pass cfp-tn
781 (ash (tn-offset name) word-shift))
784 (inst lw name-pass code-tn
785 (- (ash (tn-offset name) word-shift)
786 other-pointer-lowtag))
788 (inst lw entry-point name-pass
789 (- (ash fdefn-raw-addr-slot word-shift)
790 other-pointer-lowtag))
793 (descriptor-reg (move lexenv arg-fun))
795 (inst lw lexenv cfp-tn
796 (ash (tn-offset arg-fun) word-shift))
799 (inst lw lexenv code-tn
800 (- (ash (tn-offset arg-fun) word-shift)
801 other-pointer-lowtag))
803 (inst lw function lexenv
804 (- (ash closure-fun-slot word-shift)
807 (inst addu entry-point function
808 (- (ash simple-fun-code-offset word-shift)
809 fun-pointer-lowtag))))
816 (note-this-location vop :call-site)
822 '((trace-table-entry trace-table-normal)
823 (emit-return-pc lra-label)
824 (default-unknown-values vop values nvals
825 move-temp temp lra-label)
827 (load-stack-tn cur-nfp nfp-save))))
829 '((trace-table-entry trace-table-normal)
830 (emit-return-pc lra-label)
831 (note-this-location vop :unknown-return)
832 (receive-unknown-values values-start nvals start count
835 (load-stack-tn cur-nfp nfp-save))))
839 (define-full-call call nil :fixed nil)
840 (define-full-call call-named t :fixed nil)
841 (define-full-call multiple-call nil :unknown nil)
842 (define-full-call multiple-call-named t :unknown nil)
843 (define-full-call tail-call nil :tail nil)
844 (define-full-call tail-call-named t :tail nil)
846 (define-full-call call-variable nil :fixed t)
847 (define-full-call multiple-call-variable nil :unknown t)
850 ;;; Defined separately, since needs special code that BLT's the arguments
853 (define-vop (tail-call-variable)
855 (args-arg :scs (any-reg) :target args)
856 (function-arg :scs (descriptor-reg) :target lexenv)
857 (ocfp-arg :scs (any-reg) :target ocfp)
858 (lra-arg :scs (descriptor-reg) :target lra))
860 (:temporary (:sc any-reg :offset nl0-offset :from (:argument 0)) args)
861 (:temporary (:sc any-reg :offset lexenv-offset :from (:argument 1)) lexenv)
862 (:temporary (:sc any-reg :offset ocfp-offset :from (:argument 2)) ocfp)
863 (:temporary (:sc any-reg :offset lra-offset :from (:argument 3)) lra)
869 ;; Move these into the passing locations if they are not already there.
871 (move lexenv function-arg)
875 ;; Clear the number stack if anything is there and jump to the
876 ;; assembly-routine that does the bliting.
877 (inst j (make-fixup 'tail-call-variable :assembly-routine))
878 (let ((cur-nfp (current-nfp-tn vop)))
880 (inst addu nsp-tn cur-nfp
881 (bytes-needed-for-non-descriptor-stack-frame))
885 ;;;; Unknown values return:
887 ;;; Return a single value using the unknown-values convention.
889 (define-vop (return-single)
890 (:args (ocfp :scs (any-reg))
891 (return-pc :scs (descriptor-reg))
894 (:temporary (:scs (interior-reg)) lip)
897 ;; Clear the number stack.
898 (trace-table-entry trace-table-fun-epilogue)
899 (let ((cur-nfp (current-nfp-tn vop)))
901 (inst addu nsp-tn cur-nfp
902 (bytes-needed-for-non-descriptor-stack-frame))))
903 ;; Clear the control stack, and restore the frame pointer.
907 (lisp-return return-pc lip :offset 2)
908 (trace-table-entry trace-table-normal)))
911 ;;; Do unknown-values return of a fixed number of values. The Values are
912 ;;; required to be set up in the standard passing locations. Nvals is the
913 ;;; number of values returned.
915 ;;; If returning a single value, then deallocate the current frame, restore
916 ;;; FP and jump to the single-value entry at Return-PC + 8.
918 ;;; If returning other than one value, then load the number of values returned,
919 ;;; NIL out unsupplied values registers, restore FP and return at Return-PC.
920 ;;; When there are stack values, we must initialize the argument pointer to
921 ;;; point to the beginning of the values block (which is the beginning of the
925 (:args (ocfp :scs (any-reg))
926 (return-pc :scs (descriptor-reg) :to (:eval 1))
930 (:temporary (:sc descriptor-reg :offset a0-offset :from (:eval 0)) a0)
931 (:temporary (:sc descriptor-reg :offset a1-offset :from (:eval 0)) a1)
932 (:temporary (:sc descriptor-reg :offset a2-offset :from (:eval 0)) a2)
933 (:temporary (:sc descriptor-reg :offset a3-offset :from (:eval 0)) a3)
934 (:temporary (:sc descriptor-reg :offset a4-offset :from (:eval 0)) a4)
935 (:temporary (:sc descriptor-reg :offset a5-offset :from (:eval 0)) a5)
936 (:temporary (:sc any-reg :offset nargs-offset) nargs)
937 (:temporary (:sc any-reg :offset ocfp-offset) val-ptr)
938 (:temporary (:scs (interior-reg)) lip)
941 ;; Clear the number stack.
942 (trace-table-entry trace-table-fun-epilogue)
943 (let ((cur-nfp (current-nfp-tn vop)))
945 (inst addu nsp-tn cur-nfp
946 (bytes-needed-for-non-descriptor-stack-frame))))
947 ;; Establish the values pointer and values count.
948 (move val-ptr cfp-tn)
949 (inst li nargs (fixnumize nvals))
950 ;; restore the frame pointer and clear as much of the control
951 ;; stack as possible.
953 (inst addu csp-tn val-ptr (* nvals n-word-bytes))
954 ;; pre-default any argument register that need it.
955 (when (< nvals register-arg-count)
956 (dolist (reg (subseq (list a0 a1 a2 a3 a4 a5) nvals))
959 (lisp-return return-pc lip)
960 (trace-table-entry trace-table-normal)))
962 ;;; Do unknown-values return of an arbitrary number of values (passed on the
963 ;;; stack.) We check for the common case of a single return value, and do that
964 ;;; inline using the normal single value return convention. Otherwise, we
965 ;;; branch off to code that calls an assembly-routine.
967 (define-vop (return-multiple)
968 (:args (ocfp-arg :scs (any-reg) :target ocfp)
969 (lra-arg :scs (descriptor-reg) :target lra)
970 (vals-arg :scs (any-reg) :target vals)
971 (nvals-arg :scs (any-reg) :target nvals))
973 (:temporary (:sc any-reg :offset nl1-offset :from (:argument 0)) ocfp)
974 (:temporary (:sc descriptor-reg :offset lra-offset :from (:argument 1)) lra)
975 (:temporary (:sc any-reg :offset nl0-offset :from (:argument 2)) vals)
976 (:temporary (:sc any-reg :offset nargs-offset :from (:argument 3)) nvals)
977 (:temporary (:sc descriptor-reg :offset a0-offset) a0)
978 (:temporary (:scs (interior-reg)) lip)
983 (trace-table-entry trace-table-fun-epilogue)
984 (let ((not-single (gen-label)))
985 ;; Clear the number stack.
986 (let ((cur-nfp (current-nfp-tn vop)))
988 (inst addu nsp-tn cur-nfp
989 (bytes-needed-for-non-descriptor-stack-frame))))
991 ;; Check for the single case.
992 (inst li a0 (fixnumize 1))
993 (inst bne nvals-arg a0 not-single)
994 (inst lw a0 vals-arg)
996 ;; Return with one value.
998 (move cfp-tn ocfp-arg)
999 (lisp-return lra-arg lip :offset 2)
1001 ;; Nope, not the single case.
1002 (emit-label not-single)
1003 (move ocfp ocfp-arg)
1005 (move vals vals-arg)
1007 (inst j (make-fixup 'return-multiple :assembly-routine))
1008 (move nvals nvals-arg t))
1009 (trace-table-entry trace-table-normal)))
1016 ;;; We don't need to do anything special for regular functions.
1018 (define-vop (setup-environment)
1022 ;; Don't bother doing anything.
1025 ;;; Get the lexical environment from its passing location.
1027 (define-vop (setup-closure-environment)
1028 (:temporary (:sc descriptor-reg :offset lexenv-offset :target closure
1031 (:results (closure :scs (descriptor-reg)))
1036 (move closure lexenv)))
1038 ;;; Copy a more arg from the argument area to the end of the current frame.
1039 ;;; Fixed is the number of non-more arguments.
1041 (define-vop (copy-more-arg)
1042 (:temporary (:sc any-reg :offset nl0-offset) result)
1043 (:temporary (:sc any-reg :offset nl1-offset) count)
1044 (:temporary (:sc any-reg :offset nl2-offset) src)
1045 (:temporary (:sc any-reg :offset nl3-offset) dst)
1046 (:temporary (:sc descriptor-reg :offset l0-offset) temp)
1049 (let ((loop (gen-label))
1050 (do-regs (gen-label))
1052 (when (< fixed register-arg-count)
1053 ;; Save a pointer to the results so we can fill in register args.
1054 ;; We don't need this if there are more fixed args than reg args.
1055 (move result csp-tn))
1056 ;; Allocate the space on the stack.
1057 (cond ((zerop fixed)
1058 (inst beq nargs-tn done)
1059 (inst addu csp-tn csp-tn nargs-tn))
1061 (inst addu count nargs-tn (fixnumize (- fixed)))
1062 (inst blez count done)
1064 (inst addu csp-tn csp-tn count)))
1065 (when (< fixed register-arg-count)
1066 ;; We must stop when we run out of stack args, not when we run out of
1068 (inst addu count nargs-tn (fixnumize (- register-arg-count))))
1069 ;; Everything of interest in registers.
1070 (inst blez count do-regs)
1071 ;; Initialize dst to be end of stack.
1073 ;; Initialize src to be end of args.
1074 (inst addu src cfp-tn nargs-tn)
1077 ;; *--dst = *--src, --count
1078 (inst addu src src (- n-word-bytes))
1079 (inst addu count count (fixnumize -1))
1081 (inst addu dst dst (- n-word-bytes))
1082 (inst bgtz count loop)
1085 (emit-label do-regs)
1086 (when (< fixed register-arg-count)
1087 ;; Now we have to deposit any more args that showed up in registers.
1088 ;; We know there is at least one more arg, otherwise we would have
1089 ;; branched to done up at the top.
1090 (inst subu count nargs-tn (fixnumize (1+ fixed)))
1091 (do ((i fixed (1+ i)))
1092 ((>= i register-arg-count))
1093 ;; Is this the last one?
1094 (inst beq count done)
1095 ;; Store it relative to the pointer saved at the start.
1096 (storew (nth i *register-arg-tns*) result (- i fixed))
1098 (inst subu count (fixnumize 1))))
1099 (emit-label done))))
1102 ;;; More args are stored consequtively on the stack, starting immediately at
1103 ;;; the context pointer. The context pointer is not typed, so the lowtag is 0.
1105 (define-full-reffer more-arg * 0 0 (descriptor-reg any-reg) * %more-arg)
1108 ;;; Turn more arg (context, count) into a list.
1110 (define-vop (listify-rest-args)
1111 (:args (context-arg :target context :scs (descriptor-reg))
1112 (count-arg :target count :scs (any-reg)))
1113 (:arg-types * tagged-num)
1114 (:temporary (:scs (any-reg) :from (:argument 0)) context)
1115 (:temporary (:scs (any-reg) :from (:argument 1)) count)
1116 (:temporary (:scs (descriptor-reg) :from :eval) temp dst)
1117 (:temporary (:sc non-descriptor-reg :offset nl4-offset) pa-flag)
1118 (:results (result :scs (descriptor-reg)))
1119 (:translate %listify-rest-args)
1122 (let ((enter (gen-label))
1125 (move context context-arg)
1126 (move count count-arg)
1127 ;; Check to see if there are any arguments.
1128 (inst beq count zero-tn done)
1129 (move result null-tn t)
1131 ;; We need to do this atomically.
1132 (pseudo-atomic (pa-flag)
1133 ;; Allocate a cons (2 words) for each item.
1134 (inst or result alloc-tn list-pointer-lowtag)
1136 (inst sll temp count 1)
1138 (inst addu alloc-tn alloc-tn temp)
1140 ;; Store the current cons in the cdr of the previous cons.
1142 (inst addu dst dst (* 2 n-word-bytes))
1143 (storew dst dst -1 list-pointer-lowtag)
1147 (loadw temp context)
1148 (inst addu context context n-word-bytes)
1150 ;; Dec count, and if != zero, go back for more.
1151 (inst addu count count (fixnumize -1))
1152 (inst bne count zero-tn loop)
1154 ;; Store the value in the car (in delay slot)
1155 (storew temp dst 0 list-pointer-lowtag)
1157 ;; NIL out the last cons.
1158 (storew null-tn dst 1 list-pointer-lowtag))
1159 (emit-label done))))
1161 ;;; Return the location and size of the more arg glob created by Copy-More-Arg.
1162 ;;; Supplied is the total number of arguments supplied (originally passed in
1163 ;;; NARGS.) Fixed is the number of non-rest arguments.
1165 ;;; We must duplicate some of the work done by Copy-More-Arg, since at that
1166 ;;; time the environment is in a pretty brain-damaged state, preventing this
1167 ;;; info from being returned as values. What we do is compute
1168 ;;; supplied - fixed, and return a pointer that many words below the current
1171 (define-vop (more-arg-context)
1172 (:policy :fast-safe)
1173 (:translate sb!c::%more-arg-context)
1174 (:args (supplied :scs (any-reg)))
1175 (:arg-types tagged-num (:constant fixnum))
1177 (:results (context :scs (descriptor-reg))
1178 (count :scs (any-reg)))
1179 (:result-types t tagged-num)
1180 (:note "more-arg-context")
1182 (inst addu count supplied (fixnumize (- fixed)))
1183 (inst subu context csp-tn count)))
1186 ;;; Signal wrong argument count error if Nargs isn't = to Count.
1188 (define-vop (verify-arg-count)
1189 (:policy :fast-safe)
1190 (:translate sb!c::%verify-arg-count)
1191 (:args (nargs :scs (any-reg)))
1192 (:arg-types positive-fixnum (:constant t))
1193 (:temporary (:scs (any-reg) :type fixnum) temp)
1196 (:save-p :compute-only)
1199 (generate-error-code vop invalid-arg-count-error nargs)))
1200 (cond ((zerop count)
1201 (inst bne nargs zero-tn err-lab)
1204 (inst li temp (fixnumize count))
1205 (inst bne nargs temp err-lab)
1208 ;;; Various other error signalers.
1210 (macrolet ((frob (name error translate &rest args)
1211 `(define-vop (,name)
1213 `((:policy :fast-safe)
1214 (:translate ,translate)))
1215 (:args ,@(mapcar #'(lambda (arg)
1216 `(,arg :scs (any-reg descriptor-reg)))
1219 (:save-p :compute-only)
1221 (error-call vop ,error ,@args)))))
1222 (frob arg-count-error invalid-arg-count-error
1223 sb!c::%arg-count-error nargs)
1224 (frob type-check-error object-not-type-error sb!c::%type-check-error
1226 (frob layout-invalid-error layout-invalid-error sb!c::%layout-invalid-error
1228 (frob odd-key-args-error odd-key-args-error
1229 sb!c::%odd-key-args-error)
1230 (frob unknown-key-arg-error unknown-key-arg-error
1231 sb!c::%unknown-key-arg-error key)
1232 (frob nil-fun-returned-error nil-fun-returned-error nil fun))