Better error when calling an undefined alien function on x86-64.
[sbcl.git] / src / compiler / x86-64 / c-call.lisp
1 ;;;; the VOPs and other necessary machine specific support
2 ;;;; routines for call-out to C
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!VM")
14
15 ;; The MOVE-ARG vop is going to store args on the stack for
16 ;; call-out. These tn's will be used for that. move-arg is normally
17 ;; used for things going down the stack but C wants to have args
18 ;; indexed in the positive direction.
19
20 (defun my-make-wired-tn (prim-type-name sc-name offset)
21   (make-wired-tn (primitive-type-or-lose prim-type-name)
22                  (sc-number-or-lose sc-name)
23                  offset))
24
25 (defstruct (arg-state (:copier nil))
26   (register-args 0)
27   (xmm-args 0)
28   (stack-frame-size 0))
29
30 (defconstant max-int-args #.(length *c-call-register-arg-offsets*))
31 (defconstant max-xmm-args #!+win32 4 #!-win32 8)
32
33 (defun int-arg (state prim-type reg-sc stack-sc)
34   (let ((reg-args (max (arg-state-register-args state)
35                        #!+win32 (arg-state-xmm-args state))))
36     (cond ((< reg-args max-int-args)
37            (setf (arg-state-register-args state) (1+ reg-args))
38            (my-make-wired-tn prim-type reg-sc
39                              (nth reg-args *c-call-register-arg-offsets*)))
40           (t
41            (let ((frame-size (arg-state-stack-frame-size state)))
42              (setf (arg-state-stack-frame-size state) (1+ frame-size))
43              (my-make-wired-tn prim-type stack-sc frame-size))))))
44
45 (define-alien-type-method (integer :arg-tn) (type state)
46   (if (alien-integer-type-signed type)
47       (int-arg state 'signed-byte-64 'signed-reg 'signed-stack)
48       (int-arg state 'unsigned-byte-64 'unsigned-reg 'unsigned-stack)))
49
50 (define-alien-type-method (system-area-pointer :arg-tn) (type state)
51   (declare (ignore type))
52   (int-arg state 'system-area-pointer 'sap-reg 'sap-stack))
53
54 (defun float-arg (state prim-type reg-sc stack-sc)
55   (let ((xmm-args (max (arg-state-xmm-args state)
56                         #!+win32 (arg-state-register-args state))))
57     (cond ((< xmm-args max-xmm-args)
58            (setf (arg-state-xmm-args state) (1+ xmm-args))
59            (my-make-wired-tn prim-type reg-sc
60                              (nth xmm-args *float-regs*)))
61           (t
62            (let ((frame-size (arg-state-stack-frame-size state)))
63              (setf (arg-state-stack-frame-size state) (1+ frame-size))
64              (my-make-wired-tn prim-type stack-sc frame-size))))))
65
66 (define-alien-type-method (double-float :arg-tn) (type state)
67   (declare (ignore type))
68   (float-arg state 'double-float 'double-reg 'double-stack))
69
70 (define-alien-type-method (single-float :arg-tn) (type state)
71   (declare (ignore type))
72   (float-arg state 'single-float 'single-reg 'single-stack))
73
74 (defstruct (result-state (:copier nil))
75   (num-results 0))
76
77 (defun result-reg-offset (slot)
78   (ecase slot
79     (0 eax-offset)
80     (1 edx-offset)))
81
82 (define-alien-type-method (integer :result-tn) (type state)
83   (let ((num-results (result-state-num-results state)))
84     (setf (result-state-num-results state) (1+ num-results))
85     (multiple-value-bind (ptype reg-sc)
86         (if (alien-integer-type-signed type)
87             (values 'signed-byte-64 'signed-reg)
88             (values 'unsigned-byte-64 'unsigned-reg))
89       (my-make-wired-tn ptype reg-sc (result-reg-offset num-results)))))
90
91 (define-alien-type-method (integer :naturalize-gen) (type alien)
92   (if (<= (alien-type-bits type) 32)
93       (if (alien-integer-type-signed type)
94           `(sign-extend ,alien ,(alien-type-bits type))
95           `(logand ,alien ,(1- (ash 1 (alien-type-bits type)))))
96       alien))
97
98 (define-alien-type-method (system-area-pointer :result-tn) (type state)
99   (declare (ignore type))
100   (let ((num-results (result-state-num-results state)))
101     (setf (result-state-num-results state) (1+ num-results))
102     (my-make-wired-tn 'system-area-pointer 'sap-reg
103                       (result-reg-offset num-results))))
104
105 (define-alien-type-method (double-float :result-tn) (type state)
106   (declare (ignore type))
107   (let ((num-results (result-state-num-results state)))
108     (setf (result-state-num-results state) (1+ num-results))
109     (my-make-wired-tn 'double-float 'double-reg num-results)))
110
111 (define-alien-type-method (single-float :result-tn) (type state)
112   (declare (ignore type))
113   (let ((num-results (result-state-num-results state)))
114     (setf (result-state-num-results state) (1+ num-results))
115     (my-make-wired-tn 'single-float 'single-reg num-results)))
116
117 (define-alien-type-method (values :result-tn) (type state)
118   (let ((values (alien-values-type-values type)))
119     (when (> (length values) 2)
120       (error "Too many result values from c-call."))
121     (mapcar (lambda (type)
122               (invoke-alien-type-method :result-tn type state))
123             values)))
124
125 (defun make-call-out-tns (type)
126   (let ((arg-state (make-arg-state)))
127     (collect ((arg-tns))
128       (dolist (arg-type (alien-fun-type-arg-types type))
129         (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
130       (values (my-make-wired-tn 'positive-fixnum 'any-reg esp-offset)
131               (* (arg-state-stack-frame-size arg-state) n-word-bytes)
132               (arg-tns)
133               (invoke-alien-type-method :result-tn
134                                         (alien-fun-type-result-type type)
135                                         (make-result-state))))))
136
137
138 (deftransform %alien-funcall ((function type &rest args) * * :node node)
139   (aver (sb!c::constant-lvar-p type))
140   (let* ((type (sb!c::lvar-value type))
141          (env (sb!c::node-lexenv node))
142          (arg-types (alien-fun-type-arg-types type))
143          (result-type (alien-fun-type-result-type type)))
144     (aver (= (length arg-types) (length args)))
145     (if (or (some #'(lambda (type)
146                       (and (alien-integer-type-p type)
147                            (> (sb!alien::alien-integer-type-bits type) 64)))
148                   arg-types)
149             (and (alien-integer-type-p result-type)
150                  (> (sb!alien::alien-integer-type-bits result-type) 64)))
151         (collect ((new-args) (lambda-vars) (new-arg-types))
152           (dolist (type arg-types)
153             (let ((arg (gensym)))
154               (lambda-vars arg)
155               (cond ((and (alien-integer-type-p type)
156                           (> (sb!alien::alien-integer-type-bits type) 64))
157                      ;; CLH: FIXME! This should really be
158                      ;; #xffffffffffffffff. nyef says: "Passing
159                      ;; 128-bit integers to ALIEN functions on x86-64
160                      ;; believed to be broken."
161                      (new-args `(logand ,arg #xffffffff))
162                      (new-args `(ash ,arg -64))
163                      (new-arg-types (parse-alien-type '(unsigned 64) env))
164                      (if (alien-integer-type-signed type)
165                          (new-arg-types (parse-alien-type '(signed 64) env))
166                          (new-arg-types (parse-alien-type '(unsigned 64) env))))
167                     (t
168                      (new-args arg)
169                      (new-arg-types type)))))
170           (cond ((and (alien-integer-type-p result-type)
171                       (> (sb!alien::alien-integer-type-bits result-type) 64))
172                  (let ((new-result-type
173                         (let ((sb!alien::*values-type-okay* t))
174                           (parse-alien-type
175                            (if (alien-integer-type-signed result-type)
176                                '(values (unsigned 64) (signed 64))
177                                '(values (unsigned 64) (unsigned 64)))
178                            env))))
179                    `(lambda (function type ,@(lambda-vars))
180                       (declare (ignore type))
181                       (multiple-value-bind (low high)
182                           (%alien-funcall function
183                                           ',(make-alien-fun-type
184                                              :arg-types (new-arg-types)
185                                              :result-type new-result-type)
186                                           ,@(new-args))
187                         (logior low (ash high 64))))))
188                 (t
189                  `(lambda (function type ,@(lambda-vars))
190                     (declare (ignore type))
191                     (%alien-funcall function
192                                     ',(make-alien-fun-type
193                                        :arg-types (new-arg-types)
194                                        :result-type result-type)
195                                     ,@(new-args))))))
196         (sb!c::give-up-ir1-transform))))
197
198 ;;; The ABI is vague about how signed sub-word integer return values
199 ;;; are handled, but since gcc versions >=4.3 no longer do sign
200 ;;; extension in the callee, we need to do it in the caller.  FIXME:
201 ;;; If the value to be extended is known to already be of the target
202 ;;; type at compile time, we can (and should) elide the extension.
203 (defknown sign-extend ((signed-byte 64) t) fixnum
204     (foldable flushable movable))
205
206 (define-vop (sign-extend)
207   (:translate sign-extend)
208   (:policy :fast-safe)
209   (:args (val :scs (signed-reg)))
210   (:arg-types signed-num (:constant fixnum))
211   (:info size)
212   (:results (res :scs (signed-reg)))
213   (:result-types fixnum)
214   (:generator 1
215    (inst movsxd res
216          (make-random-tn :kind :normal
217                          :sc (sc-or-lose (ecase size
218                                            (8 'byte-reg)
219                                            (16 'word-reg)
220                                            (32 'dword-reg)))
221                          :offset (tn-offset val)))))
222
223 #-sb-xc-host
224 (defun sign-extend (x size)
225   (declare (type (signed-byte 64) x))
226   (ecase size
227     (8 (sign-extend x size))
228     (16 (sign-extend x size))
229     (32 (sign-extend x size))))
230
231 #+sb-xc-host
232 (defun sign-extend (x size)
233   (if (logbitp (1- size) x)
234       (dpb x (byte size 0) -1)
235       x))
236
237 (define-vop (foreign-symbol-sap)
238   (:translate foreign-symbol-sap)
239   (:policy :fast-safe)
240   (:args)
241   (:arg-types (:constant simple-string))
242   (:info foreign-symbol)
243   (:results (res :scs (sap-reg)))
244   (:result-types system-area-pointer)
245   (:generator 2
246    (inst mov res (make-fixup foreign-symbol :foreign))))
247
248 #!+linkage-table
249 (define-vop (foreign-symbol-dataref-sap)
250   (:translate foreign-symbol-dataref-sap)
251   (:policy :fast-safe)
252   (:args)
253   (:arg-types (:constant simple-string))
254   (:info foreign-symbol)
255   (:results (res :scs (sap-reg)))
256   (:result-types system-area-pointer)
257   (:generator 2
258    (inst mov res (make-fixup foreign-symbol :foreign-dataref))))
259
260 (define-vop (call-out)
261   (:args (function :scs (sap-reg)
262                    :target rbx)
263          (args :more t))
264   (:results (results :more t))
265   ;; RBX is used to first load the address, allowing the debugger to
266   ;; determine which alien was accessed in case it's undefined.
267   (:temporary (:sc sap-reg :offset rbx-offset) rbx)
268   (:temporary (:sc unsigned-reg :offset rax-offset :to :result) rax)
269   ;; For safepoint builds: Force values of non-volatiles to the stack.
270   ;; These are the callee-saved registers in the native ABI, but
271   ;; safepoint-based GC needs to see all Lisp values on the stack.  Note
272   ;; that R12-R15 are non-volatile registers, but there is no need to
273   ;; spill R12 because it is our thread-base-tn.  RDI and RSI are
274   ;; non-volatile on Windows, but argument passing registers on other
275   ;; platforms.
276   #!+sb-safepoint (:temporary (:sc unsigned-reg :offset r13-offset) r13)
277   #!+sb-safepoint (:temporary (:sc unsigned-reg :offset r14-offset) r14)
278   #!+sb-safepoint (:temporary (:sc unsigned-reg :offset r15-offset) r15)
279   #!+(and sb-safepoint win32) (:temporary
280                                (:sc unsigned-reg :offset rdi-offset) rdi)
281   #!+(and sb-safepoint win32) (:temporary
282                                (:sc unsigned-reg :offset rsi-offset) rsi)
283   (:ignore results
284            #!+(and sb-safepoint win32) rdi
285            #!+(and sb-safepoint win32) rsi
286            #!+win32 args
287            #!+win32 rax
288            #!+sb-safepoint r15
289            #!+sb-safepoint r13)
290   (:vop-var vop)
291   (:save-p t)
292   (:generator 0
293     ;; ABI: Direction flag must be clear on function entry. -- JES, 2006-01-20
294     (inst cld)
295     #!+sb-safepoint
296     (progn
297       ;; Current PC - don't rely on function to keep it in a form that
298       ;; GC understands
299       (let ((label (gen-label)))
300         (inst lea r14 (make-fixup nil :code-object label))
301         (emit-label label)))
302     #!-win32
303     ;; ABI: AL contains amount of arguments passed in XMM registers
304     ;; for vararg calls.
305     (move-immediate rax
306                     (loop for tn-ref = args then (tn-ref-across tn-ref)
307                           while tn-ref
308                           count (eq (sb-name (sc-sb (tn-sc (tn-ref-tn tn-ref))))
309                                     'float-registers)))
310     #!+win32 (inst sub rsp-tn #x20) ;MS_ABI: shadow zone
311     #!+sb-safepoint
312     (progn                 ;Store SP and PC in thread struct
313       (storew rsp-tn thread-base-tn thread-saved-csp-offset)
314       (storew r14 thread-base-tn thread-pc-around-foreign-call-slot))
315     (move rbx function)
316     (inst call rbx)
317     #!+win32 (inst add rsp-tn #x20) ;MS_ABI: remove shadow space
318     #!+sb-safepoint
319     (progn
320       ;; Zeroing out
321       (inst xor r14 r14)
322       ;; Zero PC storage place. NB. CSP-then-PC: same sequence on
323       ;; entry/exit, is actually corrent.
324       (storew r14 thread-base-tn thread-saved-csp-offset)
325       (storew r14 thread-base-tn thread-pc-around-foreign-call-slot))
326     ;; To give the debugger a clue. XX not really internal-error?
327     (note-this-location vop :internal-error)))
328
329 (define-vop (alloc-number-stack-space)
330   (:info amount)
331   (:results (result :scs (sap-reg any-reg)))
332   (:result-types system-area-pointer)
333   (:generator 0
334     (aver (location= result rsp-tn))
335     (unless (zerop amount)
336       (let ((delta (logandc2 (+ amount 7) 7)))
337         (inst sub rsp-tn delta)))
338     ;; C stack must be 16 byte aligned
339     (inst and rsp-tn -16)
340     (move result rsp-tn)))
341
342 (define-vop (dealloc-number-stack-space)
343   (:info amount)
344   (:generator 0
345     (unless (zerop amount)
346       (let ((delta (logandc2 (+ amount 7) 7)))
347         (inst add rsp-tn delta)))))
348
349 (define-vop (alloc-alien-stack-space)
350   (:info amount)
351   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
352   (:results (result :scs (sap-reg any-reg)))
353   (:result-types system-area-pointer)
354   #!+sb-thread
355   (:generator 0
356     (aver (not (location= result rsp-tn)))
357     (unless (zerop amount)
358       (let ((delta (logandc2 (+ amount 7) 7)))
359         (inst mov temp
360               (make-ea :qword
361                        :disp (+ nil-value
362                                 (static-symbol-offset '*alien-stack*)
363                                 (ash symbol-tls-index-slot word-shift)
364                                 (- other-pointer-lowtag))))
365         (inst sub (make-ea :qword :base thread-base-tn
366                            :scale 1 :index temp) delta)))
367     (load-tl-symbol-value result *alien-stack*))
368   #!-sb-thread
369   (:generator 0
370     (aver (not (location= result rsp-tn)))
371     (unless (zerop amount)
372       (let ((delta (logandc2 (+ amount 7) 7)))
373         (inst sub (make-ea :qword
374                            :disp (+ nil-value
375                                     (static-symbol-offset '*alien-stack*)
376                                     (ash symbol-value-slot word-shift)
377                                     (- other-pointer-lowtag)))
378               delta)))
379     (load-symbol-value result *alien-stack*)))
380
381 (define-vop (dealloc-alien-stack-space)
382   (:info amount)
383   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
384   #!+sb-thread
385   (:generator 0
386     (unless (zerop amount)
387       (let ((delta (logandc2 (+ amount 7) 7)))
388         (inst mov temp
389               (make-ea :qword
390                        :disp (+ nil-value
391                                 (static-symbol-offset '*alien-stack*)
392                                 (ash symbol-tls-index-slot word-shift)
393                                 (- other-pointer-lowtag))))
394         (inst add (make-ea :qword :base thread-base-tn :scale 1 :index temp)
395               delta))))
396   #!-sb-thread
397   (:generator 0
398     (unless (zerop amount)
399       (let ((delta (logandc2 (+ amount 7) 7)))
400         (inst add (make-ea :qword
401                            :disp (+ nil-value
402                                     (static-symbol-offset '*alien-stack*)
403                                     (ash symbol-value-slot word-shift)
404                                     (- other-pointer-lowtag)))
405               delta)))))
406
407 ;;; not strictly part of the c-call convention, but needed for the
408 ;;; WITH-PINNED-OBJECTS macro used for "locking down" lisp objects so
409 ;;; that GC won't move them while foreign functions go to work.
410 (define-vop (touch-object)
411   (:translate touch-object)
412   (:args (object))
413   (:ignore object)
414   (:policy :fast-safe)
415   (:arg-types t)
416   (:generator 0))
417
418 ;;; Callbacks
419
420 #-sb-xc-host
421 (defun alien-callback-accessor-form (type sp offset)
422   `(deref (sap-alien (sap+ ,sp ,offset) (* ,type))))
423
424 #-sb-xc-host
425 (defun alien-callback-assembler-wrapper (index result-type argument-types)
426   (labels ((make-tn-maker (sc-name)
427              (lambda (offset)
428                (make-random-tn :kind :normal
429                                :sc (sc-or-lose sc-name)
430                                :offset offset)))
431            (out-of-registers-error ()
432              (error "Too many arguments in callback")))
433     (let* ((segment (make-segment))
434            (rax rax-tn)
435            #!+(or win32 (not sb-safepoint)) (rcx rcx-tn)
436            #!-win32 (rdi rdi-tn)
437            #!-win32 (rsi rsi-tn)
438            (rdx rdx-tn)
439            (rbp rbp-tn)
440            (rsp rsp-tn)
441            #!+win32 (r8 r8-tn)
442            (xmm0 float0-tn)
443            ([rsp] (make-ea :qword :base rsp :disp 0))
444            ;; How many arguments have been copied
445            (arg-count 0)
446            ;; How many arguments have been copied from the stack
447            (stack-argument-count #!-win32 0 #!+win32 4)
448            (gprs (mapcar (make-tn-maker 'any-reg) *c-call-register-arg-offsets*))
449            (fprs (mapcar (make-tn-maker 'double-reg)
450                          ;; Only 8 first XMM registers are used for
451                          ;; passing arguments
452                          (subseq *float-regs* 0 #!-win32 8 #!+win32 4))))
453       (assemble (segment)
454         ;; Make room on the stack for arguments.
455         (inst sub rsp (* n-word-bytes (length argument-types)))
456         ;; Copy arguments from registers to stack
457         (dolist (type argument-types)
458           (let ((integerp (not (alien-float-type-p type)))
459                 ;; A TN pointing to the stack location where the
460                 ;; current argument should be stored for the purposes
461                 ;; of ENTER-ALIEN-CALLBACK.
462                 (target-tn (make-ea :qword :base rsp
463                                    :disp (* arg-count
464                                             n-word-bytes)))
465                 ;; A TN pointing to the stack location that contains
466                 ;; the next argument passed on the stack.
467                 (stack-arg-tn (make-ea :qword :base rsp
468                                        :disp (* (+ 1
469                                                    (length argument-types)
470                                                    stack-argument-count)
471                                                 n-word-bytes))))
472             (incf arg-count)
473             (cond (integerp
474                    (let ((gpr (pop gprs)))
475                      #!+win32 (pop fprs)
476                      ;; Argument not in register, copy it from the old
477                      ;; stack location to a temporary register.
478                      (unless gpr
479                        (incf stack-argument-count)
480                        (setf gpr temp-reg-tn)
481                        (inst mov gpr stack-arg-tn))
482                      ;; Copy from either argument register or temporary
483                      ;; register to target.
484                      (inst mov target-tn gpr)))
485                   ((or (alien-single-float-type-p type)
486                        (alien-double-float-type-p type))
487                    (let ((fpr (pop fprs)))
488                      #!+win32 (pop gprs)
489                      (cond (fpr
490                             ;; Copy from float register to target location.
491                             (inst movq target-tn fpr))
492                            (t
493                             ;; Not in float register. Copy from stack to
494                             ;; temporary (general purpose) register, and
495                             ;; from there to the target location.
496                             (incf stack-argument-count)
497                             (inst mov temp-reg-tn stack-arg-tn)
498                             (inst mov target-tn temp-reg-tn)))))
499                   (t
500                    (bug "Unknown alien floating point type: ~S" type)))))
501
502         #!-sb-safepoint
503         (progn
504           ;; arg0 to FUNCALL3 (function)
505           ;;
506           ;; Indirect the access to ENTER-ALIEN-CALLBACK through
507           ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
508           ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
509           ;; Skip any SB-THREAD TLS magic, since we don't expect anyone
510           ;; to rebind the variable. -- JES, 2006-01-01
511           (inst mov rdi (+ nil-value (static-symbol-offset
512                                       'sb!alien::*enter-alien-callback*)))
513           (loadw rdi rdi symbol-value-slot other-pointer-lowtag)
514           ;; arg0 to ENTER-ALIEN-CALLBACK (trampoline index)
515           (inst mov rsi (fixnumize index))
516           ;; arg1 to ENTER-ALIEN-CALLBACK (pointer to argument vector)
517           (inst mov rdx rsp)
518           ;; add room on stack for return value
519           (inst sub rsp 8)
520           ;; arg2 to ENTER-ALIEN-CALLBACK (pointer to return value)
521           (inst mov rcx rsp)
522
523           ;; Make new frame
524           (inst push rbp)
525           (inst mov  rbp rsp)
526
527           ;; Call
528           (inst mov  rax (foreign-symbol-address "funcall3"))
529           (inst call rax)
530
531           ;; Back! Restore frame
532           (inst mov rsp rbp)
533           (inst pop rbp))
534
535         #!+sb-safepoint
536         (progn
537           ;; arg0 to ENTER-ALIEN-CALLBACK (trampoline index)
538           (inst mov #!-win32 rdi #!+win32 rcx (fixnumize index))
539           ;; arg1 to ENTER-ALIEN-CALLBACK (pointer to argument vector)
540           (inst mov #!-win32 rsi #!+win32 rdx rsp)
541           ;; add room on stack for return value
542           (inst sub rsp 8)
543           ;; arg2 to ENTER-ALIEN-CALLBACK (pointer to return value)
544           (inst mov #!-win32 rdx #!+win32 r8 rsp)
545           ;; Make new frame
546           (inst push rbp)
547           (inst mov  rbp rsp)
548           #!+win32 (inst sub rsp #x20)
549           #!+win32 (inst and rsp #x-20)
550           ;; Call
551           (inst mov rax (foreign-symbol-address "callback_wrapper_trampoline"))
552           (inst call rax)
553           ;; Back! Restore frame
554           (inst mov rsp rbp)
555           (inst pop rbp))
556
557         ;; Result now on top of stack, put it in the right register
558         (cond
559           ((or (alien-integer-type-p result-type)
560                (alien-pointer-type-p result-type)
561                (alien-type-= #.(parse-alien-type 'system-area-pointer nil)
562                              result-type))
563            (inst mov rax [rsp]))
564           ((or (alien-single-float-type-p result-type)
565                (alien-double-float-type-p result-type))
566            (inst movq xmm0 [rsp]))
567           ((alien-void-type-p result-type))
568           (t
569            (error "unrecognized alien type: ~A" result-type)))
570
571         ;; Pop the arguments and the return value from the stack to get
572         ;; the return address at top of stack.
573         (inst add rsp (* (1+ (length argument-types)) n-word-bytes))
574         ;; Return
575         (inst ret))
576       (finalize-segment segment)
577       ;; Now that the segment is done, convert it to a static
578       ;; vector we can point foreign code to.
579       (let ((buffer (sb!assem::segment-buffer segment)))
580         (make-static-vector (length buffer)
581                             :element-type '(unsigned-byte 8)
582                             :initial-contents buffer)))))