Debian Codegraph @

src/gen-native.scm

raw
1 ;;;
2 ;;; Generate native code vector for FFI
3 ;;;
4
5 ;; Currently, the generated code should be manually copy&pasted into
6 ;; libnative.scm. Eventually we'll streamline the build process to automate it.
7
8 (use scheme.list)
9 (use util.match)
10 (use gauche.uvector)
11 (use file.util)
12 (use lang.asm.x86_64)
13
14 ;;;
15 ;;; Common Prologue
16 ;;;
17
18 (define (emit-header port)
19 (display ";; libnative.scm supplemental code.\n" port)
20 (display ";; Generated automatically by gen-native.scm. DO NOT EDIT.\n" port)
21 (display "\n" port))
22
23 ;;; For SYSV AMD64 calling convention: Section 3.2 of
24 ;;; http://refspecs.linux-foundation.org/elf/x86_64-abi-0.95.pdf
25
26 (define (gen-stub-amd64 port)
27 ;; When all args can be on registers
28 ;; The code tail jumps to the target, so the target's return directly
29 ;; returns to the caller of call-amd64. Thus we can reuse the same
30 ;; codepad area without worrying the recursive calls overwrite active
31 ;; code.
32 (define-values (reg-code reg-labels)
33 (asm '(func: (.dataq 0)
34 farg0: (.dataq 0)
35 farg1: (.dataq 0)
36 farg2: (.dataq 0)
37 farg3: (.dataq 0)
38 farg4: (.dataq 0)
39 farg5: (.dataq 0)
40 farg6: (.dataq 0)
41 farg7: (.dataq 0)
42 entry6f7: (movsd (farg7:) %xmm7)
43 entry6f6: (movsd (farg6:) %xmm6)
44 entry6f5: (movsd (farg5:) %xmm5)
45 entry6f4: (movsd (farg4:) %xmm4)
46 entry6f3: (movsd (farg3:) %xmm3)
47 entry6f2: (movsd (farg2:) %xmm2)
48 entry6f1: (movsd (farg1:) %xmm1)
49 entry6f0: (movsd (farg0:) %xmm0)
50 entry6: (movq #x0123456789 %r9) ; imm64 to be patched
51 entry5: (movq #x0123456789 %r8) ; ditto
52 entry4: (movq #x0123456789 %rcx) ; ditto
53 entry3: (movq #x0123456789 %rdx) ; ditto
54 entry2: (movq #x0123456789 %rsi) ; ditto
55 entry1: (movq #x0123456789 %rdi) ; ditto
56 entry0: (movb 0 %al) ; imm8 to be patched
57 (jmp (func:))
58 end:)))
59 ;; Spill case.
60 (define-values (spill-code spill-labels)
61 (asm '(func: (.dataq 0)
62 farg0: (.dataq 0)
63 farg1: (.dataq 0)
64 farg2: (.dataq 0)
65 farg3: (.dataq 0)
66 farg4: (.dataq 0)
67 farg5: (.dataq 0)
68 farg6: (.dataq 0)
69 farg7: (.dataq 0)
70 entry6f7: (movsd (farg7:) %xmm7)
71 entry6f6: (movsd (farg6:) %xmm6)
72 entry6f5: (movsd (farg5:) %xmm5)
73 entry6f4: (movsd (farg4:) %xmm4)
74 entry6f3: (movsd (farg3:) %xmm3)
75 entry6f2: (movsd (farg2:) %xmm2)
76 entry6f1: (movsd (farg1:) %xmm1)
77 entry6f0: (movsd (farg0:) %xmm0)
78 entry6: (movq #x0123456789 %r9) ; imm64 to be patched
79 entry5: (movq #x0123456789 %r8) ; ditto
80 entry4: (movq #x0123456789 %rcx) ; ditto
81 entry3: (movq #x0123456789 %rdx) ; ditto
82 init: (movq #x01234567 %rax) ; imm32 to be patched
83 (leaq (spill: %rip) %rsi)
84 loop: (movq (%rsi) %rdi)
85 (push %rdi)
86 (addq 8 %rsi)
87 (subq 8 %rax)
88 (jnz loop:)
89 entry2: (movq #x0123456789 %rsi) ; ditto
90 entry1: (movq #x0123456789 %rdi) ; ditto
91 entry0: (movb 0 %al) ; imm8 to be patched
92 (call (func:))
93 epilogue: (addq #x01234567 %rsp) ; imm32 to be patched
94 (ret)
95 (.align 8)
96 spill:))) ; spill data to be filled
97
98 (define (entry-offsets labels) ;; numargs -> code vector offset
99 (map (cut assq-ref labels <>)
100 '(entry0: entry1: entry2: entry3: entry4: entry5: entry6:
101 entry6f0: entry6f1: entry6f2: entry6f3:
102 entry6f4: entry6f5: entry6f6: entry6f7:)))
103 (define (farg-offsets labels) ;; farg# -> offset
104 (map (cut assq-ref labels <>)
105 '(farg0: farg1: farg2: farg3: farg4: farg5: farg6: farg7:)))
106 (define (end-addr labels) (assq-ref labels 'end:))
107 (display ";; Register-only calling" port)
108 (display ";; label offset\n" port)
109 (dolist [p reg-labels]
110 (format port ";; ~10a ~3d\n" (car p) (cdr p)))
111 (pprint `(define *amd64-call-reg-code*
112 ',(list->u8vector reg-code))
113 :port port
114 :controls (make-write-controls :pretty #t :width 75
115 :base 16 :radix #t))
116
117 (display ";; Spill-to-stack case" port)
118 (display ";; label offset\n" port)
119 (dolist [p spill-labels]
120 (format port ";; ~10a ~3d\n" (car p) (cdr p)))
121 (pprint `(define *amd64-call-spill-code*
122 ',(list->u8vector spill-code))
123 :port port
124 :controls (make-write-controls :pretty #t :width 75
125 :base 16 :radix #t))
126
127 ;; (call-amd64 <dlptr> args rettype)
128 ;; args : ((type value) ...)
129 ;; NB: In the final form, we won't expose this function to the user; it's
130 ;; too error-prone. You can wreck havoc just by passing a wrong type.
131 ;; Instead, we'll require the user to parse the C function declaration
132 ;; and we automatically extract the type info.
133 (pprint
134 `(define call-amd64
135 (^[ptr args rettype]
136 (let* ([num-iargs (count (^p (memq (car p) '(o p i s))) args)]
137 [num-fargs (count (^p (memq (car p) '(f d))) args)]
138 [num-spills (+ (max 0 (- num-iargs 6))
139 (max 0 (- num-fargs 8)))])
140 (if (zero? num-spills)
141 (call-amd64-regs ptr args num-iargs num-fargs rettype)
142 (call-amd64-spill ptr args
143 (min num-iargs 6)
144 (min num-fargs 8)
145 num-spills rettype)))))
146 :port port)
147 (pprint
148 `(define call-amd64-regs
149 (let ((%%call-native (module-binding-ref 'gauche.bootstrap '%%call-native))
150 (entry-offsets ',(entry-offsets reg-labels))
151 (farg-offsets ',(farg-offsets reg-labels)))
152 (^[ptr args num-iargs num-fargs rettype]
153 (let* ([effective-nargs (if (zero? num-fargs)
154 num-iargs
155 (+ 6 num-fargs))]
156 [entry (~ entry-offsets effective-nargs)]
157 [patcher
158 (let loop ([args args] [icount 0] [fcount 0] [r '()])
159 (cond [(null? args) r]
160 [(memq (caar args) '(o p i s))
161 (loop (cdr args) (+ icount 1) fcount
162 ;; +2 is offset of immediate field
163 (cons `(,(+ (~ entry-offsets (+ 1 icount)) 2)
164 ,@(car args))
165 r))]
166 [(memq (caar args) '(f d))
167 (loop (cdr args) icount (+ fcount 1)
168 (cons `(,(~ farg-offsets fcount) ,@(car args))
169 r))]
170 [else (error "bad arg entry:" (car args))]))])
171 (%%call-native entry 0
172 *amd64-call-reg-code*
173 entry
174 ,(end-addr reg-labels)
175 entry
176 (list* `(0 p ,ptr)
177 `(,(+ (~ entry-offsets 0) 1) b ,num-fargs)
178 patcher)
179 rettype)))))
180 :port port)
181 (pprint
182 `(define call-amd64-spill
183 (let ((%%call-native (module-binding-ref 'gauche.bootstrap '%%call-native))
184 (entry-offsets ',(entry-offsets spill-labels))
185 (farg-offsets ',(farg-offsets spill-labels))
186 (spill-offset (^n (+ ,(assq-ref spill-labels 'spill:)
187 (* n 8)))))
188 (^[ptr args num-iargs num-fargs num-spills rettype]
189 (let* ([effective-nargs (if (zero? num-fargs)
190 num-iargs
191 (+ 6 num-fargs))]
192 [entry (~ entry-offsets effective-nargs)]
193 [patcher
194 (let loop ([args args] [icount 0] [fcount 0] [scount 0]
195 [r '()])
196 (cond [(null? args) r]
197 [(memq (caar args) '(o p i s))
198 (if (< icount 6)
199 (loop (cdr args) (+ icount 1) fcount scount
200 ;; +2 is offset of immediate field
201 (cons `(,(+ (~ entry-offsets (+ icount 1)) 2)
202 ,@(car args))
203 r))
204 (loop (cdr args) (+ icount 1) fcount
205 (+ scount 1)
206 (cons `(,(spill-offset (- num-spills scount 1)) ,@(car args))
207 r)))]
208 [(memq (caar args) '(d))
209 (if (< fcount 8)
210 (loop (cdr args) icount (+ fcount 1) scount
211 (cons `(,(~ farg-offsets fcount) ,@(car args))
212 r))
213 (loop (cdr args) icount (+ fcount 1)
214 (+ scount 1)
215 (cons `(,(spill-offset (- num-spills scount 1)) ,@(car args))
216 r)))]
217 [else (error "bad arg entry:" (car args))]))])
218 (%%call-native entry
219 (+ ,(assq-ref spill-labels 'spill:)
220 (* num-spills 8))
221 *amd64-call-spill-code*
222 entry
223 ,(assq-ref spill-labels 'spill:)
224 entry
225 (list* `(0 p ,ptr)
226 `(,(+ (~ entry-offsets 0) 1) b ,num-fargs)
227 ;; +3 for movq imm32 offset
228 `(,',(+ (assq-ref spill-labels 'init:) 3)
229 i32 ,(* 8 num-spills))
230 ;; +3 for addq imm32 offset
231 `(,',(+ (assq-ref spill-labels 'epilogue:) 3)
232 i32 ,(* 8 num-spills))
233 patcher)
234 rettype)))))
235 :port port)
236 )
237
238 ;;; For Windows x86_64 calling convention:
239 ;;; https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-160
240
241 (define (gen-stub-winx64 port)
242 (define-values (reg-code reg-labels)
243 (asm '(func: (.dataq 0)
244 farg0: (.dataq 0)
245 farg1: (.dataq 0)
246 farg2: (.dataq 0)
247 farg3: (.dataq 0)
248 entry4f3:(movsd (farg3:) %xmm3)
249 entry4f2:(movsd (farg2:) %xmm2)
250 entry4f1:(movsd (farg1:) %xmm1)
251 entry4f0:(movsd (farg0:) %xmm0)
252 entry4: (movq #x0123456789 %r9) ; imm64 to be patched
253 entry3: (movq #x0123456789 %r8) ; ditto
254 entry2: (movq #x0123456789 %rdx) ; ditto
255 entry1: (movq #x0123456789 %rcx) ; ditto
256 entry0: (addq 32 %rsp) ; %rcx-%r9 save area
257 (call (func:))
258 (addq -32 %rsp)
259 (ret)
260 end:)))
261
262 (define (entry-offsets labels) ;; numargs -> code vector offset
263 (map (cut assq-ref labels <>)
264 '(entry0: entry1: entry2: entry3: entry4:
265 entry4f0: entry4f1: entry4f2: entry4f3:)))
266 (define (farg-offsets labels) ;; farg# -> offset
267 (map (cut assq-ref labels <>)
268 '(farg0: farg1: farg2: farg3:)))
269 (define (end-addr labels) (assq-ref labels 'end:))
270 (display ";; Register-only calling" port)
271 (display ";; label offset\n" port)
272 (dolist [p reg-labels]
273 (format port ";; ~10a ~3d\n" (car p) (cdr p)))
274 (pprint `(define *winx64-call-reg-code*
275 ',(list->u8vector reg-code))
276 :port port
277 :controls (make-write-controls :pretty #t :width 75
278 :base 16 :radix #t))
279
280 ;; (call-winx64 <dlptr> args rettype)
281 ;; args : ((type value) ...)
282 (pprint
283 `(define call-winx64
284 (^[ptr args rettype]
285 (let* ([num-iargs (count (^p (memq (car p) '(o p i s))) args)]
286 [num-fargs (count (^p (memq (car p) '(f d))) args)]
287 [num-spills (+ (max 0 (- num-iargs 4))
288 (max 0 (- num-fargs 4)))])
289 (if (zero? num-spills)
290 (call-amd64-regs ptr args num-iargs num-fargs rettype)
291 (error "too many arguments (for now)")))))
292 :port port)
293 (pprint
294 `(define call-winx64-regs
295 (let ((%%call-native (module-binding-ref 'gauche.bootstrap '%%call-native))
296 (entry-offsets ',(entry-offsets reg-labels))
297 (farg-offsets ',(farg-offsets reg-labels)))
298 (^[ptr args num-iargs num-fargs rettype]
299 (let* ([effective-nargs (if (zero? num-fargs)
300 num-iargs
301 (+ 4 num-fargs))]
302 [entry (~ entry-offsets effective-nargs)]
303 [patcher
304 (let loop ([args args] [icount 0] [fcount 0] [r '()])
305 (cond [(null? args) r]
306 [(memq (caar args) '(o p i s))
307 (loop (cdr args) (+ icount 1) fcount
308 ;; +2 is offset of immediate field
309 (cons `(,(+ (~ entry-offsets (+ 1 icount)) 2)
310 ,@(car args))
311 r))]
312 [(memq (caar args) '(f d))
313 (loop (cdr args) icount (+ fcount 1)
314 (cons `(,(~ farg-offsets fcount) ,@(car args))
315 r))]
316 [else (error "bad arg entry:" (car args))]))])
317 (%%call-native entry 0
318 *winx64-call-reg-code*
319 entry
320 ,(end-addr reg-labels)
321 entry
322 (list* `(0 p ,ptr)
323 `(,(+ (~ entry-offsets 0) 1) b ,num-fargs)
324 patcher)
325 rettype)))))
326 :port port)
327 )
328
329 ;;;
330 ;;; gosh ./gen-native.scm <dir>
331 ;;;
332 (define (main args)
333 (match (cdr args)
334 [(dir) (call-with-temporary-file
335 (^[port tmpname]
336 (emit-header port)
337 (gen-stub-amd64 port)
338 (gen-stub-winx64 port)
339 (close-output-port port)
340 (sys-rename tmpname #"~|dir|/native-supp.scm"))
341 :directory dir :prefix "native-supp.scm")]
342 [else (exit 1 "Usage: gosh ./gen-native.scm <directory>")])
343 0)