Debian Codegraph @

scheme/ikarus.intel-assembler.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17
18 (library (ikarus.intel-assembler)
19 (export assemble-sources code-entry-adjustment)
20 (import
21 (ikarus)
22 (rnrs bytevectors)
23 (except (ikarus.code-objects) procedure-annotation)
24 (ikarus system $pairs))
25
26
27 (module (wordsize)
28 (import (ikarus include))
29 (include "ikarus.config.ss"))
30
31 (define fold
32 (lambda (f init ls)
33 (cond
34 [(null? ls) init]
35 [else
36 (f (car ls) (fold f init (cdr ls)))])))
37
38
39 (define convert-instructions
40 (lambda (ls)
41 (parameterize ([local-labels (uncover-local-labels ls)])
42 (fold convert-instruction '() ls))))
43
44 (define register-mapping
45 ;;; reg cls idx REX.R
46 '([%eax 32 0 #f]
47 [%ecx 32 1 #f]
48 [%edx 32 2 #f]
49 [%ebx 32 3 #f]
50 [%esp 32 4 #f]
51 [%ebp 32 5 #f]
52 [%esi 32 6 #f]
53 [%edi 32 7 #f]
54 [%r8 32 0 #t]
55 [%r9 32 1 #t]
56 [%r10 32 2 #t]
57 [%r11 32 3 #t]
58 [%r12 32 4 #t]
59 [%r13 32 5 #t]
60 [%r14 32 6 #t]
61 [%r15 32 7 #t]
62 [%al 8 0 #f]
63 [%cl 8 1 #f]
64 [%dl 8 2 #f]
65 [%bl 8 3 #f]
66 [%ah 8 4 #f]
67 [%ch 8 5 #f]
68 [%dh 8 6 #f]
69 [%bh 8 7 #f]
70 [/0 0 0 #f]
71 [/1 0 1 #f]
72 [/2 0 2 #f]
73 [/3 0 3 #f]
74 [/4 0 4 #f]
75 [/5 0 5 #f]
76 [/6 0 6 #f]
77 [/7 0 7 #f]
78 [xmm0 xmm 0 #f]
79 [xmm1 xmm 1 #f]
80 [xmm2 xmm 2 #f]
81 [xmm3 xmm 3 #f]
82 [xmm4 xmm 4 #f]
83 [xmm5 xmm 5 #f]
84 [xmm6 xmm 6 #f]
85 [xmm7 xmm 7 #f]
86 [%r8l 8 0 #t]
87 [%r9l 8 1 #t]
88 [%r10l 8 2 #t]
89 [%r11l 8 3 #t]
90 [%r12l 8 4 #t]
91 [%r13l 8 5 #t]
92 [%r14l 8 6 #t]
93 [%r15l 8 7 #t]
94
95 ))
96
97 (define register-index
98 (lambda (x)
99 (cond
100 [(assq x register-mapping) => caddr]
101 [else (die 'register-index "not a register" x)])))
102
103 (define reg32?
104 (lambda (x)
105 (cond
106 [(assq x register-mapping) =>
107 (lambda (x) (eqv? (cadr x) 32))]
108 [else #f])))
109
110 (define reg8?
111 (lambda (x)
112 (cond
113 [(assq x register-mapping) =>
114 (lambda (x) (eqv? (cadr x) 8))]
115 [else #f])))
116
117 (define xmmreg?
118 (lambda (x)
119 (cond
120 [(assq x register-mapping) =>
121 (lambda (x) (eqv? (cadr x) 'xmm))]
122 [else #f])))
123
124 (define reg?
125 (lambda (x)
126 (assq x register-mapping)))
127
128 (define reg-requires-REX?
129 (lambda (x)
130 (cond
131 [(assq x register-mapping) => cadddr]
132 [else (error 'reg-required-REX? "not a reg" x)])))
133
134 (define-syntax with-args
135 (syntax-rules (lambda)
136 [(_ x (lambda (a0 a1) b b* ...))
137 (let ([t x])
138 (if (pair? t)
139 (let ([t ($cdr t)])
140 (if (pair? t)
141 (let ([a0 ($car t)] [t ($cdr t)])
142 (if (pair? t)
143 (let ([a1 ($car t)])
144 (if (null? ($cdr t))
145 (let () b b* ...)
146 (die 'with-args "too many args")))
147 (die 'with-args "too few args")))
148 (die 'with-args "too few args")))
149 (die 'with-args "too few args")))]))
150
151
152
153 (define-syntax byte
154 (syntax-rules ()
155 [(_ x)
156 (let ([t x])
157 (if (integer? t)
158 (bitwise-and t 255)
159 (error 'byte "invalid" t '(byte x))))]))
160
161
162 (define word
163 (lambda (x)
164 (cons 'word x)))
165
166 (define reloc-word
167 (lambda (x)
168 (cons 'reloc-word x)))
169
170 (define reloc-word+
171 (lambda (x d)
172 (cons* 'reloc-word+ x d)))
173
174 (define byte?
175 (lambda (x)
176 (and (fixnum? x)
177 (fx<= x 127)
178 (fx<= -128 x))))
179
180 (define mem?
181 (lambda (x)
182 (and (pair? x)
183 (eq? (car x) 'disp))))
184
185 (define small-disp?
186 (lambda (x)
187 (and (mem? x)
188 (byte? (cadr x)))))
189
190
191 (define CODE
192 (lambda (n ac)
193 (cons (byte n) ac)))
194
195 (define CODE+r
196 (lambda (n r ac)
197 (cons (byte (fxlogor n (register-index r))) ac)))
198
199 (define ModRM
200 (lambda (mod reg r/m ac)
201 (cons (byte (fxlogor
202 (register-index r/m)
203 (fxlogor
204 (fxsll (register-index reg) 3)
205 (fxsll mod 6))))
206 (if (and (not (fx= mod 3)) (eq? r/m '%esp))
207 (cons (byte #x24) ac)
208 ac))))
209 (define IMM32
210 (lambda (n ac)
211 (cond
212 [(= wordsize 4) (IMM n ac)]
213 [(imm32? n)
214 (cons*
215 (byte n)
216 (byte (sra n 8))
217 (byte (sra n 16))
218 (byte (sra n 24))
219 ac)]
220 [(label? n)
221 (cond
222 [(local-label? (label-name n))
223 (cons (cons 'local-relative (label-name n)) ac)]
224 [else
225 (cons (cons 'relative (label-name n)) ac)])]
226 [else (die 'IMM32 "invalid" n)])))
227
228 (define IMM
229 (lambda (n ac)
230 (cond
231 [(int? n)
232 (case wordsize
233 [(4)
234 (cons*
235 (byte n)
236 (byte (sra n 8))
237 (byte (sra n 16))
238 (byte (sra n 24))
239 ac)]
240 [else
241 (cons*
242 (byte n)
243 (byte (sra n 8))
244 (byte (sra n 16))
245 (byte (sra n 24))
246 (byte (sra n 32))
247 (byte (sra n 40))
248 (byte (sra n 48))
249 (byte (sra n 56))
250 ac)])]
251 [(obj? n)
252 (let ([v (cadr n)])
253 (if (immediate? v)
254 (cons (word v) ac)
255 (cons (reloc-word v) ac)))]
256 [(obj+? n)
257 (let ([v (cadr n)] [d (caddr n)])
258 (cons (reloc-word+ v d) ac))]
259 [(label-address? n)
260 (cons (cons 'label-addr (label-name n)) ac)]
261 [(foreign? n)
262 (cons (cons 'foreign-label (label-name n)) ac)]
263 [(label? n)
264 (cond
265 [(local-label? (label-name n))
266 (cons (cons 'local-relative (label-name n)) ac)]
267 [else
268 (cons (cons 'relative (label-name n)) ac)])]
269 ;(cons (cons 'relative (label-name n)) ac)]
270 [else (die 'IMM "invalid" n)])))
271
272
273 (define IMM8
274 (lambda (n ac)
275 (cond
276 [(int? n)
277 (cons* (byte n) ac)]
278 [else (die 'IMM8 "invalid" n)])))
279
280
281 (define imm?
282 (lambda (x)
283 (or (int? x)
284 (obj? x)
285 (obj+? x)
286 (label-address? x)
287 (foreign? x)
288 (label? x))))
289
290 (define foreign?
291 (lambda (x)
292 (and (pair? x) (eq? (car x) 'foreign-label))))
293
294
295 (define imm8?
296 (lambda (x)
297 (and (int? x) (byte? x))))
298
299 (define label?
300 (lambda (x)
301 (and (pair? x) (eq? (car x) 'label))))
302
303 (define label-address?
304 (lambda (x)
305 (and (pair? x) (eq? (car x) 'label-address))))
306
307 (define label-name
308 (lambda (x) (cadr x)))
309
310 (define int? integer?)
311
312 (define obj?
313 (lambda (x)
314 (and (pair? x) (eq? (car x) 'obj))))
315
316 (define obj+?
317 (lambda (x)
318 (and (pair? x) (eq? (car x) 'obj+))))
319
320 (define CODErri
321 (lambda (c d s i ac)
322 (cond
323 [(imm8? i)
324 (CODE c (ModRM 1 d s (IMM8 i ac)))]
325 [(imm? i)
326 (CODE c (ModRM 2 d s (IMM i ac)))]
327 [else (die 'CODErri "invalid i" i)])))
328
329 (define CODErr
330 (lambda (c r1 r2 ac)
331 (CODE c (ModRM 3 r1 r2 ac))))
332
333 (define RegReg
334 (lambda (r1 r2 r3 ac)
335 (cond
336 [(eq? r3 '%esp) (die 'assembler "BUG: invalid src %esp")]
337 [(eq? r1 '%ebp) (die 'assembler "BUG: invalid src %ebp")]
338 [else
339 (cons*
340 (byte (fxlogor 4 (fxsll (register-index r1) 3)))
341 (byte (fxlogor (register-index r2)
342 (fxsll (register-index r3) 3)))
343 ac)])))
344
345 (define IMM*2
346 (lambda (i1 i2 ac)
347 (cond
348 [(and (int? i1) (obj? i2))
349 (let ([d i1] [v (cadr i2)])
350 (cons (reloc-word+ v d) ac))]
351 [(and (int? i2) (obj? i1)) (IMM*2 i2 i1 ac)]
352 [(and (int? i1) (int? i2))
353 (IMM (bitwise-and (+ i1 i2)
354 (- (expt 2 (* wordsize 8)) 1))
355 ac)]
356 [else (die 'assemble "invalid IMM*2" i1 i2)])))
357
358 (define (SIB s i b ac)
359 (cons (byte
360 (fxlogor
361 (register-index b)
362 (fxlogor
363 (fxsll (register-index i) 3)
364 (fxsll s 6))))
365 ac))
366
367 (define (imm32? x)
368 (case wordsize
369 [(4) (imm? x)]
370 [(8)
371 (and (integer? x)
372 (<= (- (expt 2 31)) x (- (expt 2 31) 1)))]
373 [else (error 'imm32? "invalid wordsize" wordsize)]))
374
375 (define *cogen* (gensym "*cogen*"))
376
377 (define-syntax add-instruction
378 (syntax-rules ()
379 [(_ (name instr ac args ...) b b* ...)
380 (putprop 'name *cogen*
381 (cons (length '(args ...))
382 (lambda (instr ac args ...) b b* ...)))]))
383
384 (define-syntax add-instructions
385 (syntax-rules ()
386 [(_ instr ac [(name* arg** ...) b* b** ...] ...)
387 (begin
388 (add-instruction (name* instr ac arg** ...) b* b** ...) ...)]))
389
390
391 (define (convert-instruction a ac)
392 (cond
393 [(getprop (car a) *cogen*) =>
394 (lambda (p)
395 (let ([n (car p)] [proc (cdr p)] [args (cdr a)])
396 (cond
397 [(fx= n 2)
398 (if (fx= (length args) 2)
399 (proc a ac (car args) (cadr args))
400 (die 'convert-instruction "incorrect args" a))]
401 [(fx= n 1)
402 (if (fx= (length args) 1)
403 (proc a ac (car args))
404 (die 'convert-instruction "incorrect args" a))]
405 [(fx= n 0)
406 (if (fx= (length args) 0)
407 (proc a ac)
408 (die 'convert-instruction "incorrect args" a))]
409 [else
410 (if (fx= (length args) n)
411 (apply proc a ac args)
412 (die 'convert-instruction "incorrect args" a))])))]
413 [(eq? (car a) 'seq)
414 (fold convert-instruction ac (cdr a))]
415 [(eq? (car a) 'pad)
416 (let ()
417 (define (find-prefix x ls)
418 (let f ([ls ls])
419 (cond
420 [(eq? ls x) '()]
421 [else
422 (let ([a (car ls)])
423 (if (and (pair? a) (eq? (car a) 'bottom-code))
424 (f (cdr ls))
425 (cons a (f (cdr ls)))))])))
426 (let ([n (cadr a)] [code (cddr a)])
427 (let ([ls (fold convert-instruction ac code)])
428 (let ([m (compute-code-size (find-prefix ac ls))])
429 (append (make-list (- n m) 0) ls)))))]
430 [else (die 'convert-instruction "unknown instruction" a)]))
431
432 (define (RM /d dst ac)
433 (cond
434 [(mem? dst)
435 (with-args dst
436 (lambda (a0 a1)
437 (cond
438 [(and (imm8? a0) (reg32? a1))
439 (ModRM 1 /d a1 (IMM8 a0 ac))]
440 [(and (imm? a0) (reg32? a1))
441 (ModRM 2 /d a1 (IMM32 a0 ac))]
442 [(and (imm8? a1) (reg32? a0))
443 (ModRM 1 /d a0 (IMM8 a1 ac))]
444 [(and (imm? a1) (reg32? a0))
445 (ModRM 2 /d a0 (IMM32 a1 ac))]
446 [(and (reg32? a0) (reg32? a1))
447 (RegReg /d a0 a1 ac)]
448 [(and (imm? a0) (imm? a1))
449 (ModRM 0 /d '/5 (IMM*2 a0 a1 ac))]
450 [else (die 'RM "unhandled" a0 a1)])))]
451 [(reg? dst) (ModRM 3 /d dst ac)]
452 [else (die 'RM "unhandled" dst)]))
453
454 (module ()
455 (define who 'assembler)
456
457
458 (define (REX.R bits ac)
459 (when (eqv? wordsize 4)
460 (error 'ikarus "BUG: REX.R invalid in 32-bit mode"))
461 (cons (fxlogor #b01001000 bits) ac))
462
463 (define (REX+r r ac)
464 (cond
465 [(eqv? wordsize 4) ac]
466 [(reg-requires-REX? r) (REX.R #b001 ac)]
467 [else (REX.R #b000 ac)]))
468
469 (define (REX+RM r rm ac)
470 (define (C n ac)
471 ac)
472 ;;;(printf "CASE ~s\n" n)
473 ;;;(let f ([ac ac] [i 30])
474 ;;; (unless (or (null? ac) (= i 0))
475 ;;; (if (number? (car ac))
476 ;;; (printf " #x~x" (car ac))
477 ;;; (printf " ~s" (car ac)))
478 ;;; (f (cdr ac) (- i 1))))
479 ;;;(newline)
480 ;;;ac)
481 (cond
482 [(eqv? wordsize 4) ac]
483 [(mem? rm)
484 (if (reg-requires-REX? r)
485 (with-args rm
486 (lambda (a0 a1)
487 (cond
488 [(and (imm? a0) (reg32? a1))
489 (if (reg-requires-REX? a1)
490 (REX.R #b101 ac)
491 (REX.R #b100 ac))]
492 [(and (imm? a1) (reg32? a0))
493 (if (reg-requires-REX? a0)
494 (REX.R #b101 ac)
495 (REX.R #b100 ac))]
496 [(and (reg32? a0) (reg32? a1))
497 (if (reg-requires-REX? a0)
498 (if (reg-requires-REX? a1)
499 (REX.R #b111 ac)
500 (REX.R #b110 ac))
501 (if (reg-requires-REX? a1)
502 (REX.R #b101 ac)
503 (REX.R #b100 ac)))]
504 [(and (imm? a0) (imm? a1))
505 (error 'REC+RM "not here 4")
506 (error 'REX+RM "unhandledb" a1)]
507 [else (die 'REX+RM "unhandled" a0 a1)])))
508 (with-args rm
509 (lambda (a0 a1)
510 (cond
511 [(and (imm? a0) (reg32? a1))
512 (if (reg-requires-REX? a1)
513 (REX.R #b001 ac)
514 (REX.R 0 ac))]
515 [(and (imm? a1) (reg32? a0))
516 (if (reg-requires-REX? a0)
517 (REX.R #b001 ac)
518 (REX.R 0 ac))]
519 [(and (reg32? a0) (reg32? a1))
520 (if (reg-requires-REX? a0)
521 (if (reg-requires-REX? a1)
522 (error 'REX+RM "unhandled x1" a0 a1)
523 (REX.R #b010 ac))
524 (if (reg-requires-REX? a1)
525 (error 'REX+RM "unhandled x3" a0 a1)
526 (REX.R 0 ac)))]
527 [(and (imm? a0) (imm? a1))
528 ;(error 'REC+RM "not here 8")
529 (REX.R 0 ac)]
530 [else (die 'REX+RM "unhandled" a0 a1)]))))]
531 [(reg? rm)
532 (let* ([bits 0]
533 [bits
534 (if (reg-requires-REX? r)
535 (fxlogor bits #b100)
536 bits)]
537 [bits
538 (if (reg-requires-REX? rm)
539 (fxlogor bits #b001)
540 bits)])
541 (REX.R bits ac))]
542 [else (die 'REX+RM "unhandled" rm)]))
543
544 (define (C c ac)
545 (case wordsize
546 [(4) (CODE c ac)]
547 [else (REX.R 0 (CODE c ac))]))
548
549 (define trace-ac
550 (let ([cache '()])
551 (lambda (ac1 what ac2)
552 (when (assembler-output)
553 (let ([diff
554 (let f ([ls ac2])
555 (cond
556 [(eq? ls ac1) '()]
557 [else (cons (car ls) (f (cdr ls)))]))])
558 (unless (member diff cache)
559 (set! cache (cons diff cache))
560 (printf "~s => ~s\n" what diff))))
561 ac2)))
562
563 (define (CR c r ac)
564 (REX+r r (CODE+r c r ac)))
565 (define (CR* c r rm ac)
566 (REX+RM r rm (CODE c (RM r rm ac))))
567 (define (CR*-no-rex c r rm ac)
568 (CODE c (RM r rm ac)))
569 (define (CCR* c0 c1 r rm ac)
570 ;(CODE c0 (CODE c1 (RM r rm ac))))
571 (REX+RM r rm (CODE c0 (CODE c1 (RM r rm ac)))))
572 (define (CCR c0 c1 r ac)
573 ;(CODE c0 (CODE+r c1 r ac)))
574 (REX+r r (CODE c0 (CODE+r c1 r ac))))
575 (define (CCCR* c0 c1 c2 r rm ac)
576 ;(CODE c0 (CODE c1 (CODE c2 (RM r rm ac)))))
577 (REX+RM r rm (CODE c0 (CODE c1 (CODE c2 (RM r rm ac))))))
578
579
580 (define (CCI32 c0 c1 i32 ac)
581 (CODE c0 (CODE c1 (IMM32 i32 ac))))
582
583
584 (define (dotrace instr orig ls)
585 (printf "TRACE: ~s ~s\n" instr
586 (let f ([ls ls])
587 (if (eq? ls orig)
588 '()
589 (cons (car ls) (f (cdr ls))))))
590 ls)
591
592 (define (jmp-pc-relative code0 code1 dst ac)
593 (when (= wordsize 4)
594 (error 'intel-assembler "no pc-relative jumps in 32-bit mode"))
595 (let ([g (gensym)])
596 (CODE code0
597 (CODE code1
598 (cons*
599 `(local-relative . ,g)
600 `(bottom-code
601 (label . ,g)
602 (label-addr . ,(label-name dst)))
603 ac)))))
604
605 (add-instructions instr ac
606 [(ret) (CODE #xC3 ac)]
607 [(cltd) (C #x99 ac)]
608 [(movl src dst)
609 (cond
610 [(and (imm? src) (reg? dst)) (CR #xB8 dst (IMM src ac))]
611 [(and (imm? src) (mem? dst)) (CR* #xC7 '/0 dst (IMM32 src ac))]
612 [(and (reg? src) (reg? dst)) (CR* #x89 src dst ac)]
613 [(and (reg? src) (mem? dst)) (CR* #x89 src dst ac)]
614 [(and (mem? src) (reg? dst)) (CR* #x8B dst src ac)]
615 [else (die who "invalid" instr)])]
616 [(mov32 src dst)
617 ;;; FIXME
618 (cond
619 [(and (imm? src) (reg? dst))
620 (error 'mov32 "here1")
621 (CR #xB8 dst (IMM32 src ac))]
622 [(and (imm? src) (mem? dst)) (CR*-no-rex #xC7 '/0 dst (IMM32 src ac))]
623 [(and (reg? src) (reg? dst))
624 (error 'mov32 "here3")
625 (CR* #x89 src dst ac)]
626 [(and (reg? src) (mem? dst)) (CR*-no-rex #x89 src dst ac)]
627 [(and (mem? src) (reg? dst))
628 (if (= wordsize 4)
629 (CR* #x8B dst src ac)
630 (CR*-no-rex #x8B dst src ac))]
631 [else (die who "invalid" instr)])]
632 [(movb src dst)
633 (cond
634 [(and (imm8? src) (mem? dst)) (CR* #xC6 '/0 dst (IMM8 src ac))]
635 [(and (reg8? src) (mem? dst)) (CR* #x88 src dst ac)]
636 [(and (mem? src) (reg8? dst)) (CR* #x8A dst src ac)]
637 [else (die who "invalid" instr)])]
638 [(addl src dst)
639 (cond
640 [(and (imm8? src) (reg? dst)) (CR* #x83 '/0 dst (IMM8 src ac))]
641 [(and (imm32? src) (eq? dst '%eax)) (C #x05 (IMM32 src ac))]
642 [(and (imm32? src) (reg? dst)) (CR* #x81 '/0 dst (IMM32 src ac))]
643 [(and (reg? src) (reg? dst)) (CR* #x01 src dst ac)]
644 [(and (mem? src) (reg? dst)) (CR* #x03 dst src ac)]
645 [(and (imm32? src) (mem? dst)) (CR* #x81 '/0 dst (IMM32 src ac))]
646 [(and (reg? src) (mem? dst)) (CR* #x01 src dst ac)]
647 [else (die who "invalid" instr)])]
648 [(subl src dst)
649 (cond
650 [(and (imm8? src) (reg? dst)) (CR* #x83 '/5 dst (IMM8 src ac))]
651 [(and (imm32? src) (eq? dst '%eax)) (C #x2D (IMM32 src ac))]
652 [(and (imm32? src) (reg? dst)) (CR* #x81 '/5 dst (IMM32 src ac))]
653 [(and (reg? src) (reg? dst)) (CR* #x29 src dst ac)]
654 [(and (mem? src) (reg? dst)) (CR* #x2B dst src ac)]
655 [(and (imm32? src) (mem? dst)) (CR* #x81 '/5 dst (IMM32 src ac))]
656 [(and (reg? src) (mem? dst)) (CR* #x29 src dst ac)]
657 [else (die who "invalid" instr)])]
658 [(sall src dst)
659 (cond
660 [(and (equal? 1 src) (reg? dst)) (CR* #xD1 '/4 dst ac)]
661 [(and (imm8? src) (reg? dst)) (CR* #xC1 '/4 dst (IMM8 src ac))]
662 [(and (imm8? src) (mem? dst)) (CR* #xC1 '/4 dst (IMM8 src ac))]
663 [(and (eq? src '%cl) (reg? dst)) (CR* #xD3 '/4 dst ac)]
664 [(and (eq? src '%cl) (mem? dst)) (CR* #xD3 '/4 dst ac)]
665 [else (die who "invalid" instr)])]
666 [(shrl src dst)
667 (cond
668 [(and (equal? 1 src) (reg? dst)) (CR* #xD1 '/5 dst ac)]
669 [(and (imm8? src) (reg? dst)) (CR* #xC1 '/5 dst (IMM8 src ac))]
670 [(and (eq? src '%cl) (reg? dst)) (CR* #xD3 '/5 dst ac)]
671 [(and (imm8? src) (mem? dst)) (CR* #xC1 '/5 dst (IMM8 src ac))]
672 [(and (eq? src '%cl) (mem? dst)) (CR* #xD3 '/5 dst ac)]
673 [else (die who "invalid" instr)])]
674 [(sarl src dst)
675 (cond
676 [(and (equal? 1 src) (reg? dst)) (CR* #xD1 '/7 dst ac)]
677 [(and (imm8? src) (reg? dst)) (CR* #xC1 '/7 dst (IMM8 src ac))]
678 [(and (imm8? src) (mem? dst)) (CR* #xC1 '/7 dst (IMM8 src ac))]
679 [(and (eq? src '%cl) (reg? dst)) (CR* #xD3 '/7 dst ac)]
680 [(and (eq? src '%cl) (mem? dst)) (CR* #xD3 '/7 dst ac)]
681 [else (die who "invalid" instr)])]
682 [(andl src dst)
683 (cond
684 [(and (imm32? src) (mem? dst)) (CR* #x81 '/4 dst (IMM32 src ac))]
685 [(and (imm8? src) (reg? dst)) (CR* #x83 '/4 dst (IMM8 src ac))]
686 [(and (imm32? src) (eq? dst '%eax)) (C #x25 (IMM32 src ac))]
687 [(and (imm32? src) (reg? dst)) (CR* #x81 '/4 dst (IMM32 src ac))]
688 [(and (reg? src) (reg? dst)) (CR* #x21 src dst ac)]
689 [(and (reg? src) (mem? dst)) (CR* #x21 src dst ac)]
690 [(and (mem? src) (reg? dst)) (CR* #x23 dst src ac)]
691 [else (die who "invalid" instr)])]
692 [(orl src dst)
693 (cond
694 [(and (imm32? src) (mem? dst)) (CR* #x81 '/1 dst (IMM32 src ac))]
695 [(and (reg? src) (mem? dst)) (CR* #x09 src dst ac)]
696 [(and (imm8? src) (reg? dst)) (CR* #x83 '/1 dst (IMM8 src ac))]
697 [(and (imm32? src) (eq? dst '%eax)) (C #x0D (IMM32 src ac))]
698 [(and (imm32? src) (reg? dst)) (CR* #x81 '/1 dst (IMM32 src ac))]
699 [(and (reg? src) (reg? dst)) (CR* #x09 src dst ac)]
700 [(and (mem? src) (reg? dst)) (CR* #x0B dst src ac)]
701 [else (die who "invalid" instr)])]
702 [(xorl src dst)
703 (cond
704 [(and (imm8? src) (reg? dst)) (CR* #x83 '/6 dst (IMM8 src ac))]
705 [(and (imm8? src) (mem? dst)) (CR* #x83 '/6 dst (IMM8 src ac))]
706 [(and (imm32? src) (eq? dst '%eax)) (C #x35 (IMM32 src ac))]
707 [(and (reg? src) (reg? dst)) (CR* #x31 src dst ac)]
708 [(and (mem? src) (reg? dst)) (CR* #x33 dst src ac)]
709 [(and (reg? src) (mem? dst)) (CR* #x31 src dst ac)]
710 [else (die who "invalid" instr)])]
711 [(leal src dst)
712 (cond
713 [(and (mem? src) (reg? dst)) (CR* #x8D dst src ac)]
714 [else (die who "invalid" instr)])]
715 [(cmpl src dst)
716 (cond
717 [(and (imm8? src) (reg? dst)) (CR* #x83 '/7 dst (IMM8 src ac))]
718 [(and (imm32? src) (eq? dst '%eax)) (C #x3D (IMM32 src ac))]
719 [(and (imm32? src) (reg? dst)) (CR* #x81 '/7 dst (IMM32 src ac))]
720 [(and (reg? src) (reg? dst)) (CR* #x39 src dst ac)]
721 [(and (mem? src) (reg? dst)) (CR* #x3B dst src ac)]
722 [(and (imm8? src) (mem? dst)) (CR* #x83 '/7 dst (IMM8 src ac))]
723 [(and (imm32? src) (mem? dst)) (CR* #x81 '/7 dst (IMM32 src ac))]
724 [else (die who "invalid" instr)])]
725 [(imull src dst)
726 (cond
727 [(and (imm8? src) (reg? dst)) (CR* #x6B dst dst (IMM8 src ac))]
728 [(and (imm32? src) (reg? dst)) (CR* #x69 dst dst (IMM32 src ac))]
729 [(and (reg? src) (reg? dst)) (CCR* #x0F #xAF dst src ac)]
730 [(and (mem? src) (reg? dst)) (CCR* #x0F #xAF dst src ac)]
731 [else (die who "invalid" instr)])]
732 [(idivl dst)
733 (cond
734 [(reg? dst) (CR* #xF7 '/7 dst ac)]
735 [(mem? dst) (CR* #xF7 '/7 dst ac)]
736 [else (die who "invalid" instr)])]
737 [(pushl dst)
738 (cond
739 [(imm8? dst) (CODE #x6A (IMM8 dst ac))]
740 [(imm32? dst) (CODE #x68 (IMM32 dst ac))]
741 [(reg? dst) (CR #x50 dst ac)]
742 [(mem? dst) (CR* #xFF '/6 dst ac)]
743 [else (die who "invalid" instr)])]
744 [(popl dst)
745 (cond
746 [(reg? dst) (CR #x58 dst ac)]
747 [(mem? dst) (CR* #x8F '/0 dst ac)]
748 [else (die who "invalid" instr)])]
749 [(notl dst)
750 (cond
751 [(reg? dst) (CR* #xF7 '/2 dst ac)]
752 [(mem? dst) (CR* #xF7 '/7 dst ac)]
753 [else (die who "invalid" instr)])]
754 [(bswap dst)
755 (cond
756 [(reg? dst) (CCR #x0F #xC8 dst ac)]
757 [else (die who "invalid" instr)])]
758 [(negl dst)
759 (cond
760 [(reg? dst) (CR* #xF7 '/3 dst ac)]
761 [else (die who "invalid" instr)])]
762 [(jmp dst)
763 (cond
764 [(and (label? dst) (local-label? (label-name dst)))
765 (CODE #xE9 (cons `(local-relative . ,(label-name dst)) ac))]
766 [(imm? dst)
767 (if (= wordsize 4)
768 (CODE #xE9 (IMM32 dst ac))
769 (jmp-pc-relative #xFF #x25 dst ac))]
770 [(mem? dst) (CR* #xFF '/4 dst ac)]
771 [else (die who "invalid jmp target" dst)])]
772 [(call dst)
773 (cond
774 [(and (label? dst) (local-label? (label-name dst)))
775 (CODE #xE8 (cons `(local-relative . ,(label-name dst)) ac))]
776 [(imm? dst)
777 (if (= wordsize 4)
778 (CODE #xE8 (IMM32 dst ac))
779 (jmp-pc-relative #xFF #x15 dst ac))]
780 [(mem? dst) (CR* #xFF '/2 dst ac)]
781 [(reg? dst) (CR* #xFF '/2 dst ac)]
782 [else (die who "invalid jmp target" dst)])]
783 [(movsd src dst)
784 (cond
785 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF2 #x0F #x10 dst src ac)]
786 [(and (xmmreg? src) (mem? dst)) (CCCR* #xF2 #x0F #x11 src dst ac)]
787 [else (die who "invalid" instr)])]
788 [(cvtsi2sd src dst)
789 (cond
790 [(and (xmmreg? dst) (reg? src)) (CCCR* #xF2 #x0F #x2A src dst ac)]
791 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF2 #x0F #x2A dst src ac)]
792 [else (die who "invalid" instr)])]
793 [(cvtsd2ss src dst)
794 (cond
795 [(and (xmmreg? dst) (xmmreg? src)) (CCCR* #xF2 #x0F #x5A src dst ac)]
796 [else (die who "invalid" instr)])]
797 [(cvtss2sd src dst)
798 (cond
799 [(and (xmmreg? dst) (xmmreg? src)) (CCCR* #xF3 #x0F #x5A src dst ac)]
800 [else (die who "invalid" instr)])]
801 [(movss src dst)
802 (cond
803 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF3 #x0F #x10 dst src ac)]
804 [(and (xmmreg? src) (mem? dst)) (CCCR* #xF3 #x0F #x11 src dst ac)]
805 [else (die who "invalid" instr)])]
806 [(addsd src dst)
807 (cond
808 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF2 #x0F #x58 dst src ac)]
809 [else (die who "invalid" instr)])]
810 [(subsd src dst)
811 (cond
812 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF2 #x0F #x5C dst src ac)]
813 [else (die who "invalid" instr)])]
814 [(mulsd src dst)
815 (cond
816 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF2 #x0F #x59 dst src ac)]
817 [else (die who "invalid" instr)])]
818 [(divsd src dst)
819 (cond
820 [(and (xmmreg? dst) (mem? src)) (CCCR* #xF2 #x0F #x5E dst src ac)]
821 [else (die who "invalid" instr)])]
822 [(ucomisd src dst)
823 (cond
824 [(and (xmmreg? dst) (mem? src)) (CCCR* #x66 #x0F #x2E dst src ac)]
825 [else (die who "invalid" instr)])]
826 [(ja dst) (CCI32 #x0F #x87 dst ac)]
827 [(jae dst) (CCI32 #x0F #x83 dst ac)]
828 [(jb dst) (CCI32 #x0F #x82 dst ac)]
829 [(jbe dst) (CCI32 #x0F #x86 dst ac)]
830 [(jg dst) (CCI32 #x0F #x8F dst ac)]
831 [(jge dst) (CCI32 #x0F #x8D dst ac)]
832 [(jl dst) (CCI32 #x0F #x8C dst ac)]
833 [(jle dst) (CCI32 #x0F #x8E dst ac)]
834 [(je dst) (CCI32 #x0F #x84 dst ac)]
835 [(jna dst) (CCI32 #x0F #x86 dst ac)]
836 [(jnae dst) (CCI32 #x0F #x82 dst ac)]
837 [(jnb dst) (CCI32 #x0F #x83 dst ac)]
838 [(jnbe dst) (CCI32 #x0F #x87 dst ac)]
839 [(jng dst) (CCI32 #x0F #x8E dst ac)]
840 [(jnge dst) (CCI32 #x0F #x8C dst ac)]
841 [(jnl dst) (CCI32 #x0F #x8D dst ac)]
842 [(jnle dst) (CCI32 #x0F #x8F dst ac)]
843 [(jne dst) (CCI32 #x0F #x85 dst ac)]
844 [(jo dst) (CCI32 #x0F #x80 dst ac)]
845 [(jp dst) (CCI32 #x0F #x8A dst ac)]
846 [(jnp dst) (CCI32 #x0F #x8B dst ac)]
847 [(byte x)
848 (unless (byte? x) (die who "not a byte" x))
849 (cons (byte x) ac)]
850 [(byte-vector x) (append (map (lambda (x) (byte x)) (vector->list x)) ac)]
851 [(int a) (IMM a ac)]
852 [(label L)
853 (unless (symbol? L) (die who "label is not a symbol" L))
854 (cons (cons 'label L) ac)]
855 [(label-address L)
856 (unless (symbol? L) (die who "label-address is not a symbol" L))
857 (cons (cons 'label-addr L) ac)]
858 [(current-frame-offset)
859 (cons '(current-frame-offset) ac)]
860 [(nop) ac]
861
862 ))
863
864
865 (define compute-code-size
866 (lambda (ls)
867 (fold (lambda (x ac)
868 (if (fixnum? x)
869 (fx+ ac 1)
870 (case (car x)
871 [(byte) (fx+ ac 1)]
872 [(relative local-relative)
873 (fx+ ac 4)]
874 [(label) ac]
875 [(word reloc-word reloc-word+ label-addr
876 current-frame-offset foreign-label)
877 (fx+ ac wordsize)]
878 [(bottom-code)
879 (fx+ ac (compute-code-size (cdr x)))]
880 [else (die 'compute-code-size "unknown instr" x)])))
881 0
882 ls)))
883
884 (define set-label-loc!
885 (lambda (x loc)
886 (when (getprop x '*label-loc*)
887 (die 'compile "label is already defined" x))
888 (putprop x '*label-loc* loc)))
889
890 (define label-loc
891 (lambda (x)
892 (or (getprop x '*label-loc*)
893 (die 'compile "undefined label" x))))
894
895 (define unset-label-loc!
896 (lambda (x)
897 (remprop x '*label-loc*)))
898
899
900 (define set-code-word!
901 (lambda (code idx x)
902 (cond
903 [(fixnum? x)
904 (case wordsize
905 [(4)
906 (code-set! code (fx+ idx 0) (fxsll (fxlogand x #x3F) 2))
907 (code-set! code (fx+ idx 1) (fxlogand (fxsra x 6) #xFF))
908 (code-set! code (fx+ idx 2) (fxlogand (fxsra x 14) #xFF))
909 (code-set! code (fx+ idx 3) (fxlogand (fxsra x 22) #xFF))]
910 [else
911 (code-set! code (fx+ idx 0) (fxsll (fxlogand x #x1F) 3))
912 (code-set! code (fx+ idx 1) (fxlogand (fxsra x 5) #xFF))
913 (code-set! code (fx+ idx 2) (fxlogand (fxsra x 13) #xFF))
914 (code-set! code (fx+ idx 3) (fxlogand (fxsra x 21) #xFF))
915 (code-set! code (fx+ idx 4) (fxlogand (fxsra x 29) #xFF))
916 (code-set! code (fx+ idx 5) (fxlogand (fxsra x 37) #xFF))
917 (code-set! code (fx+ idx 6) (fxlogand (fxsra x 45) #xFF))
918 (code-set! code (fx+ idx 7) (fxlogand (fxsra x 53) #xFF))])]
919 [else (die 'set-code-word! "unhandled" x)])))
920
921 (define local-labels (make-parameter '()))
922 (define (local-label? x) (and (memq x (local-labels)) #t))
923
924 (define (uncover-local-labels ls)
925 (define locals '())
926 (define find
927 (lambda (x)
928 (when (pair? x)
929 (case (car x)
930 [(label)
931 (set! locals (cons (label-name x) locals))]
932 [(seq pad)
933 (for-each find (cdr x))]))))
934 (for-each find ls)
935 locals)
936
937 (define (optimize-local-jumps ls)
938 (define locals '())
939 (define g (gensym))
940 (define (mark x)
941 (when (pair? x)
942 (case (car x)
943 [(label)
944 (putprop (cdr x) g 'local)
945 (set! locals (cons (cdr x) locals))]
946 [(bottom-code) (for-each mark (cdr x))])))
947 (define (opt x)
948 (when (pair? x)
949 (case (car x)
950 [(relative)
951 (when (eq? (getprop (cdr x) g) 'local)
952 (set-car! x 'local-relative))]
953 [(bottom-code) (for-each opt (cdr x))])))
954 (for-each mark ls)
955 (for-each opt ls)
956 (for-each (lambda (x) (remprop x g)) locals)
957 ls)
958
959
960
961 (define whack-instructions
962 (lambda (x ls)
963 (define f
964 (lambda (ls idx reloc bot*)
965 (cond
966 [(null? ls)
967 (if (null? bot*)
968 reloc
969 (f (car bot*) idx reloc (cdr bot*)))]
970 [else
971 (let ([a (car ls)])
972 (if (fixnum? a)
973 (begin
974 (code-set! x idx a)
975 (f (cdr ls) (fxadd1 idx) reloc bot*))
976 (case (car a)
977 [(byte)
978 (code-set! x idx (cdr a))
979 (f (cdr ls) (fx+ idx 1) reloc bot*)]
980 [(relative local-relative)
981 (f (cdr ls) (fx+ idx 4) (cons (cons idx a) reloc) bot*)]
982 [(reloc-word reloc-word+ label-addr foreign-label)
983 (f (cdr ls) (fx+ idx wordsize) (cons (cons idx a) reloc) bot*)]
984 [(word)
985 (let ([v (cdr a)])
986 (set-code-word! x idx v)
987 (f (cdr ls) (fx+ idx wordsize) reloc bot*))]
988 [(current-frame-offset)
989 (set-code-word! x idx idx) ;;; FIXME 64bit
990 (f (cdr ls) (fx+ idx wordsize) reloc bot*)]
991 [(label)
992 (set-label-loc! (cdr a) (list x idx))
993 (f (cdr ls) idx reloc bot*)]
994 [(bottom-code)
995 (f (cdr ls) idx reloc (cons (cdr a) bot*))]
996 [else
997 (die 'whack-instructions "unknown instr" a)])))])))
998 (f ls 0 '() '())))
999
1000 (define compute-reloc-size
1001 (lambda (ls)
1002 (fold (lambda (x ac)
1003 (if (fixnum? x)
1004 ac
1005 (case (car x)
1006 [(word byte label current-frame-offset local-relative) ac]
1007 [(reloc-word foreign-label) (fx+ ac 2)]
1008 [(relative reloc-word+ label-addr) (fx+ ac 3)]
1009 [(bottom-code) (fx+ ac (compute-reloc-size (cdr x)))]
1010 [else (die 'compute-reloc-size "unknown instr" x)])))
1011 0
1012 ls)))
1013
1014 (define foreign-string->bytevector
1015 (let ([mem '()])
1016 (lambda (x)
1017 (let f ([ls mem])
1018 (cond
1019 [(null? ls)
1020 (let ([bv (string->utf8 x)])
1021 (set! mem (cons (cons x bv) mem))
1022 bv)]
1023 [(string=? x (caar ls)) (cdar ls)]
1024 [else (f (cdr ls))])))))
1025
1026
1027 (define code-entry-adjustment
1028 (let ([v #f])
1029 (case-lambda
1030 [() (or v (die 'code-entry-adjustment "uninitialized"))]
1031 [(x) (set! v x)])))
1032
1033 (define whack-reloc
1034 (lambda (thunk?-label code vec)
1035 (define reloc-idx 0)
1036 (lambda (r)
1037 (let ([idx (car r)] [type (cadr r)]
1038 [v
1039 (let ([v (cddr r)])
1040 (cond
1041 [(thunk?-label v) =>
1042 (lambda (label)
1043 (let ([p (label-loc label)])
1044 (cond
1045 [(fx= (length p) 2)
1046 (let ([code (car p)] [idx (cadr p)])
1047 (unless (fx= idx 0)
1048 (die 'whack-reloc
1049 "cannot create a thunk pointing"
1050 idx))
1051 (let ([thunk (code->thunk code)])
1052 (set-cdr! (cdr p) (list thunk))
1053 thunk))]
1054 [else (caddr p)])))]
1055 [else v]))])
1056 (case type
1057 [(reloc-word)
1058 (vector-set! vec reloc-idx (fxsll idx 2))
1059 (vector-set! vec (fx+ reloc-idx 1) v)
1060 (set! reloc-idx (fx+ reloc-idx 2))]
1061 [(foreign-label)
1062 ;;; FIXME: converted strings should be memoized.
1063 ;;; wait for equal? hash tables.
1064 (let ([name
1065 (if (string? v)
1066 (foreign-string->bytevector v)
1067 (die 'whack-reloc "not a string" v))])
1068 (vector-set! vec reloc-idx (fxlogor 1 (fxsll idx 2)))
1069 (vector-set! vec (fx+ reloc-idx 1) name)
1070 (set! reloc-idx (fx+ reloc-idx 2)))]
1071 [(reloc-word+)
1072 (let ([obj (car v)] [disp (cdr v)])
1073 (vector-set! vec reloc-idx (fxlogor 2 (fxsll idx 2)))
1074 (vector-set! vec (fx+ reloc-idx 1) disp)
1075 (vector-set! vec (fx+ reloc-idx 2) obj)
1076 (set! reloc-idx (fx+ reloc-idx 3)))]
1077 [(label-addr)
1078 (let ([loc (label-loc v)])
1079 (let ([obj (car loc)] [disp (cadr loc)])
1080 (vector-set! vec reloc-idx (fxlogor 2 (fxsll idx 2)))
1081 (vector-set! vec (fx+ reloc-idx 1)
1082 (fx+ disp (code-entry-adjustment)))
1083 (vector-set! vec (fx+ reloc-idx 2) obj)))
1084 (set! reloc-idx (fx+ reloc-idx 3))]
1085 [(local-relative)
1086 (let ([loc (label-loc v)])
1087 (let ([obj (car loc)] [disp (cadr loc)])
1088 (unless (eq? obj code)
1089 (die 'whack-reloc "local-relative differ"))
1090 (let ([rel (fx- disp (fx+ idx 4))])
1091 (code-set! code (fx+ idx 0) (fxlogand rel #xFF))
1092 (code-set! code (fx+ idx 1) (fxlogand (fxsra rel 8) #xFF))
1093 (code-set! code (fx+ idx 2) (fxlogand (fxsra rel 16) #xFF))
1094 (code-set! code (fx+ idx 3) (fxlogand (fxsra rel 24) #xFF)))))]
1095 [(relative)
1096 (let ([loc (label-loc v)])
1097 (let ([obj (car loc)] [disp (cadr loc)])
1098 (unless (and (code? obj) (fixnum? disp))
1099 (die 'whack-reloc "invalid relative jump obj/disp" obj disp))
1100 (vector-set! vec reloc-idx (fxlogor 3 (fxsll idx 2)))
1101 (vector-set! vec (fx+ reloc-idx 1)
1102 (fx+ disp (code-entry-adjustment)))
1103 (vector-set! vec (fx+ reloc-idx 2) obj)))
1104 (set! reloc-idx (fx+ reloc-idx 3))]
1105 [else (die 'whack-reloc "invalid reloc type" type)]))
1106 )))
1107
1108
1109
1110 (define assemble-sources
1111 (lambda (thunk?-label ls*)
1112 (define (code-list ls)
1113 (if (let ([a (cadr ls)])
1114 (and (pair? a) (eq? (car a) 'name)))
1115 (cddr ls)
1116 (cdr ls)))
1117 (define (code-name ls)
1118 (let ([a (cadr ls)])
1119 (if (and (pair? a) (eq? (car a) 'name))
1120 (cadr a)
1121 #f)))
1122 (let ([closure-size* (map car ls*)]
1123 [code-name* (map code-name ls*)]
1124 [ls* (map code-list ls*)])
1125 (let* ([ls* (map convert-instructions ls*)]
1126 [ls* (map optimize-local-jumps ls*)])
1127 (let ([n* (map compute-code-size ls*)]
1128 [m* (map compute-reloc-size ls*)])
1129 (let ([code* (map make-code n* closure-size*)]
1130 [relv* (map make-vector m*)])
1131 (let ([reloc** (map whack-instructions code* ls*)])
1132 (for-each
1133 (lambda (foo reloc*)
1134 (for-each (whack-reloc thunk?-label (car foo) (cdr foo)) reloc*))
1135 (map cons code* relv*) reloc**)
1136 (for-each set-code-reloc-vector! code* relv*)
1137 (for-each (lambda (code name)
1138 (when name
1139 (set-code-annotation! code name)))
1140 code* code-name*)
1141 code*)))))))
1142
1143
1144 )
1145