]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/x86.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / cpu / x86 / x86.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs alien alien.c-types arrays strings
4 cpu.x86.assembler cpu.x86.assembler.private cpu.architecture
5 kernel kernel.private math memory namespaces make sequences
6 words system layouts combinators math.order fry locals
7 compiler.constants
8 compiler.cfg.registers
9 compiler.cfg.instructions
10 compiler.cfg.intrinsics
11 compiler.cfg.comparisons
12 compiler.cfg.stack-frame
13 compiler.codegen
14 compiler.codegen.fixup ;
15 IN: cpu.x86
16
17 << enable-fixnum-log2 >>
18
19 ! Add some methods to the assembler to be more useful to the backend
20 M: label JMP 0 JMP rc-relative label-fixup ;
21 M: label JUMPcc [ 0 ] dip JUMPcc rc-relative label-fixup ;
22
23 M: x86 two-operand? t ;
24
25 HOOK: stack-reg cpu ( -- reg )
26
27 HOOK: reserved-area-size cpu ( -- n )
28
29 : stack@ ( n -- op ) stack-reg swap [+] ;
30
31 : param@ ( n -- op ) reserved-area-size + stack@ ;
32
33 : spill-integer@ ( n -- op ) spill-integer-offset param@ ;
34
35 : spill-float@ ( n -- op ) spill-float-offset param@ ;
36
37 : gc-root@ ( n -- op ) gc-root-offset param@ ;
38
39 : decr-stack-reg ( n -- )
40     dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
41
42 : incr-stack-reg ( n -- )
43     dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
44
45 : align-stack ( n -- n' )
46     os macosx? cpu x86.64? or [ 16 align ] when ;
47
48 M: x86 stack-frame-size ( stack-frame -- i )
49     (stack-frame-size) 3 cells reserved-area-size + + align-stack ;
50
51 HOOK: temp-reg-1 cpu ( -- reg )
52 HOOK: temp-reg-2 cpu ( -- reg )
53
54 HOOK: param-reg-1 cpu ( -- reg )
55 HOOK: param-reg-2 cpu ( -- reg )
56
57 HOOK: pic-tail-reg cpu ( -- reg )
58
59 M: x86 %load-immediate MOV ;
60
61 M: x86 %load-reference swap 0 MOV rc-absolute-cell rel-immediate ;
62
63 HOOK: ds-reg cpu ( -- reg )
64 HOOK: rs-reg cpu ( -- reg )
65
66 : reg-stack ( n reg -- op ) swap cells neg [+] ;
67
68 GENERIC: loc>operand ( loc -- operand )
69
70 M: ds-loc loc>operand n>> ds-reg reg-stack ;
71 M: rs-loc loc>operand n>> rs-reg reg-stack ;
72
73 M: x86 %peek loc>operand MOV ;
74 M: x86 %replace loc>operand swap MOV ;
75 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ; inline
76 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
77 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
78
79 M: x86 %call ( word -- ) 0 CALL rc-relative rel-word-pic ;
80
81 : xt-tail-pic-offset ( -- n )
82     #! See the comment in vm/cpu-x86.hpp
83     cell 4 + 1 + ; inline
84
85 M: x86 %jump ( word -- )
86     pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here
87     0 JMP rc-relative rel-word-pic-tail ;
88
89 M: x86 %jump-label ( label -- ) 0 JMP rc-relative label-fixup ;
90
91 M: x86 %return ( -- ) 0 RET ;
92
93 : code-alignment ( align -- n )
94     [ building get length dup ] dip align swap - ;
95
96 : align-code ( n -- )
97     0 <repetition> % ;
98
99 :: (%slot) ( obj slot tag temp -- op )
100     temp slot obj [+] LEA
101     temp tag neg [+] ; inline
102
103 :: (%slot-imm) ( obj slot tag -- op )
104     obj slot cells tag - [+] ; inline
105
106 M: x86 %slot ( dst obj slot tag temp -- ) (%slot) MOV ;
107 M: x86 %slot-imm ( dst obj slot tag -- ) (%slot-imm) MOV ;
108 M: x86 %set-slot ( src obj slot tag temp -- ) (%slot) swap MOV ;
109 M: x86 %set-slot-imm ( src obj slot tag -- ) (%slot-imm) swap MOV ;
110
111 M: x86 %add     [+] LEA ;
112 M: x86 %add-imm [+] LEA ;
113 M: x86 %sub     nip SUB ;
114 M: x86 %sub-imm neg [+] LEA ;
115 M: x86 %mul     nip swap IMUL2 ;
116 M: x86 %mul-imm IMUL3 ;
117 M: x86 %and     nip AND ;
118 M: x86 %and-imm nip AND ;
119 M: x86 %or      nip OR ;
120 M: x86 %or-imm  nip OR ;
121 M: x86 %xor     nip XOR ;
122 M: x86 %xor-imm nip XOR ;
123 M: x86 %shl-imm nip SHL ;
124 M: x86 %shr-imm nip SHR ;
125 M: x86 %sar-imm nip SAR ;
126 M: x86 %not     drop NOT ;
127 M: x86 %log2    BSR ;
128
129 : ?MOV ( dst src -- )
130     2dup = [ 2drop ] [ MOV ] if ; inline
131
132 :: overflow-template ( label dst src1 src2 insn -- )
133     src1 src2 insn call
134     label JO ; inline
135
136 M: x86 %fixnum-add ( label dst src1 src2 -- )
137     [ ADD ] overflow-template ;
138
139 M: x86 %fixnum-sub ( label dst src1 src2 -- )
140     [ SUB ] overflow-template ;
141
142 M: x86 %fixnum-mul ( label dst src1 src2 -- )
143     [ swap IMUL2 ] overflow-template ;
144
145 : bignum@ ( reg n -- op )
146     cells bignum tag-number - [+] ; inline
147
148 M:: x86 %integer>bignum ( dst src temp -- )
149     #! on entry, inreg is a signed 32-bit quantity
150     #! exits with tagged ptr to bignum in outreg
151     #! 1 cell header, 1 cell length, 1 cell sign, + digits
152     #! length is the # of digits + sign
153     [
154         "end" define-label
155         ! Load cached zero value
156         dst 0 >bignum %load-reference
157         src 0 CMP
158         ! Is it zero? Then just go to the end and return this zero
159         "end" get JE
160         ! Allocate a bignum
161         dst 4 cells bignum temp %allot
162         ! Write length
163         dst 1 bignum@ 2 tag-fixnum MOV
164         ! Store value
165         dst 3 bignum@ src MOV
166         ! Compute sign
167         temp src MOV
168         temp cell-bits 1- SAR
169         temp 1 AND
170         ! Store sign
171         dst 2 bignum@ temp MOV
172         ! Make negative value positive
173         temp temp ADD
174         temp NEG
175         temp 1 ADD
176         src temp IMUL2
177         ! Store the bignum
178         dst 3 bignum@ temp MOV
179         "end" resolve-label
180     ] with-scope ;
181
182 M:: x86 %bignum>integer ( dst src temp -- )
183     [
184         "end" define-label
185         ! load length
186         temp src 1 bignum@ MOV
187         ! if the length is 1, its just the sign and nothing else,
188         ! so output 0
189         dst 0 MOV
190         temp 1 tag-fixnum CMP
191         "end" get JE
192         ! load the value
193         dst src 3 bignum@ MOV
194         ! load the sign
195         temp src 2 bignum@ MOV
196         ! convert it into -1 or 1
197         temp temp ADD
198         temp NEG
199         temp 1 ADD
200         ! make dst signed
201         temp dst IMUL2
202         "end" resolve-label
203     ] with-scope ;
204
205 M: x86 %add-float nip ADDSD ;
206 M: x86 %sub-float nip SUBSD ;
207 M: x86 %mul-float nip MULSD ;
208 M: x86 %div-float nip DIVSD ;
209
210 M: x86 %integer>float CVTSI2SD ;
211 M: x86 %float>integer CVTTSD2SI ;
212
213 M: x86 %copy ( dst src -- ) ?MOV ;
214
215 M: x86 %copy-float ( dst src -- )
216     2dup = [ 2drop ] [ MOVSD ] if ;
217
218 M: x86 %unbox-float ( dst src -- )
219     float-offset [+] MOVSD ;
220
221 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
222     [
223         { "is-byte-array" "end" "start" } [ define-label ] each
224         dst 0 MOV
225         temp src MOV
226         ! We come back here with displaced aliens
227         "start" resolve-label
228         ! Is the object f?
229         temp \ f tag-number CMP
230         "end" get JE
231         ! Is the object an alien?
232         temp header-offset [+] alien type-number tag-fixnum CMP
233         "is-byte-array" get JNE
234         ! If so, load the offset and add it to the address
235         dst temp alien-offset [+] ADD
236         ! Now recurse on the underlying alien
237         temp temp underlying-alien-offset [+] MOV
238         "start" get JMP
239         "is-byte-array" resolve-label
240         ! Add byte array address to address being computed
241         dst temp ADD
242         ! Add an offset to start of byte array's data
243         dst byte-array-offset ADD
244         "end" resolve-label
245     ] with-scope ;
246
247 M:: x86 %box-float ( dst src temp -- )
248     dst 16 float temp %allot
249     dst float-offset [+] src MOVSD ;
250
251 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
252
253 M:: x86 %box-alien ( dst src temp -- )
254     [
255         "end" define-label
256         dst \ f tag-number MOV
257         src 0 CMP
258         "end" get JE
259         dst 4 cells alien temp %allot
260         dst 1 alien@ \ f tag-number MOV
261         dst 2 alien@ \ f tag-number MOV
262         ! Store src in alien-offset slot
263         dst 3 alien@ src MOV
264         "end" resolve-label
265     ] with-scope ;
266
267 : small-reg-8 ( reg -- reg' )
268     H{
269         { EAX RAX }
270         { ECX RCX }
271         { EDX RDX }
272         { EBX RBX }
273         { ESP RSP }
274         { EBP RBP }
275         { ESI RSP }
276         { EDI RDI }
277
278         { RAX RAX }
279         { RCX RCX }
280         { RDX RDX }
281         { RBX RBX }
282         { RSP RSP }
283         { RBP RBP }
284         { RSI RSP }
285         { RDI RDI }
286     } at ; inline
287
288 : small-reg-4 ( reg -- reg' )
289     small-reg-8 H{
290         { RAX EAX }
291         { RCX ECX }
292         { RDX EDX }
293         { RBX EBX }
294         { RSP ESP }
295         { RBP EBP }
296         { RSI ESP }
297         { RDI EDI }
298     } at ; inline
299
300 : small-reg-2 ( reg -- reg' )
301     small-reg-4 H{
302         { EAX AX }
303         { ECX CX }
304         { EDX DX }
305         { EBX BX }
306         { ESP SP }
307         { EBP BP }
308         { ESI SI }
309         { EDI DI }
310     } at ; inline
311
312 : small-reg-1 ( reg -- reg' )
313     small-reg-4 {
314         { EAX AL }
315         { ECX CL }
316         { EDX DL }
317         { EBX BL }
318     } at ; inline
319
320 : small-reg ( reg size -- reg' )
321     {
322         { 1 [ small-reg-1 ] }
323         { 2 [ small-reg-2 ] }
324         { 4 [ small-reg-4 ] }
325         { 8 [ small-reg-8 ] }
326     } case ;
327
328 HOOK: small-regs cpu ( -- regs )
329
330 M: x86.32 small-regs { EAX ECX EDX EBX } ;
331 M: x86.64 small-regs { RAX RCX RDX RBX } ;
332
333 HOOK: small-reg-native cpu ( reg -- reg' )
334
335 M: x86.32 small-reg-native small-reg-4 ;
336 M: x86.64 small-reg-native small-reg-8 ;
337
338 : small-reg-that-isn't ( exclude -- reg' )
339     small-regs swap [ small-reg-native ] map '[ _ memq? not ] find nip ;
340
341 : with-save/restore ( reg quot -- )
342     [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
343
344 :: with-small-register ( dst exclude quot: ( new-dst -- ) -- )
345     #! If the destination register overlaps a small register, we
346     #! call the quot with that. Otherwise, we find a small
347     #! register that is not in exclude, and call quot, saving
348     #! and restoring the small register.
349     dst small-reg-native small-regs memq? [ dst quot call ] [
350         exclude small-reg-that-isn't
351         [ quot call ] with-save/restore
352     ] if ; inline
353
354 M:: x86 %string-nth ( dst src index temp -- )
355     "end" define-label
356     dst { src index temp } [| new-dst |
357         ! Load the least significant 7 bits into new-dst.
358         ! 8th bit indicates whether we have to load from
359         ! the aux vector or not.
360         temp src index [+] LEA
361         new-dst 1 small-reg temp string-offset [+] MOV
362         new-dst new-dst 1 small-reg MOVZX
363         ! Do we have to look at the aux vector?
364         new-dst HEX: 80 CMP
365         "end" get JL
366         ! Yes, this is a non-ASCII character. Load aux vector
367         temp src string-aux-offset [+] MOV
368         new-dst temp XCHG
369         ! Compute index
370         new-dst index ADD
371         new-dst index ADD
372         ! Load high 16 bits
373         new-dst 2 small-reg new-dst byte-array-offset [+] MOV
374         new-dst new-dst 2 small-reg MOVZX
375         new-dst 7 SHL
376         ! Compute code point
377         new-dst temp XOR
378         "end" resolve-label
379         dst new-dst ?MOV
380     ] with-small-register ;
381
382 M:: x86 %set-string-nth-fast ( ch str index temp -- )
383     ch { index str temp } [| new-ch |
384         new-ch ch ?MOV
385         temp str index [+] LEA
386         temp string-offset [+] new-ch 1 small-reg MOV
387     ] with-small-register ;
388
389 :: %alien-integer-getter ( dst src size quot -- )
390     dst { src } [| new-dst |
391         new-dst dup size small-reg dup src [] MOV
392         quot call
393         dst new-dst ?MOV
394     ] with-small-register ; inline
395
396 : %alien-unsigned-getter ( dst src size -- )
397     [ MOVZX ] %alien-integer-getter ; inline
398
399 M: x86 %alien-unsigned-1 1 %alien-unsigned-getter ;
400 M: x86 %alien-unsigned-2 2 %alien-unsigned-getter ;
401
402 : %alien-signed-getter ( dst src size -- )
403     [ MOVSX ] %alien-integer-getter ; inline
404
405 M: x86 %alien-signed-1 1 %alien-signed-getter ;
406 M: x86 %alien-signed-2 2 %alien-signed-getter ;
407 M: x86 %alien-signed-4 4 %alien-signed-getter ;
408
409 M: x86 %alien-unsigned-4 4 [ 2drop ] %alien-integer-getter ;
410
411 M: x86 %alien-cell [] MOV ;
412 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
413 M: x86 %alien-double [] MOVSD ;
414
415 :: %alien-integer-setter ( ptr value size -- )
416     value { ptr } [| new-value |
417         new-value value ?MOV
418         ptr [] new-value size small-reg MOV
419     ] with-small-register ; inline
420
421 M: x86 %set-alien-integer-1 1 %alien-integer-setter ;
422 M: x86 %set-alien-integer-2 2 %alien-integer-setter ;
423 M: x86 %set-alien-integer-4 4 %alien-integer-setter ;
424 M: x86 %set-alien-cell [ [] ] dip MOV ;
425 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
426 M: x86 %set-alien-double [ [] ] dip MOVSD ;
427
428 : load-zone-ptr ( reg -- )
429     #! Load pointer to start of zone array
430     0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
431
432 : load-allot-ptr ( nursery-ptr allot-ptr -- )
433     [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
434
435 : inc-allot-ptr ( nursery-ptr n -- )
436     [ cell [+] ] dip 8 align ADD ;
437
438 : store-header ( temp class -- )
439     [ [] ] [ type-number tag-fixnum ] bi* MOV ;
440
441 : store-tagged ( dst tag -- )
442     tag-number OR ;
443
444 M:: x86 %allot ( dst size class nursery-ptr -- )
445     nursery-ptr dst load-allot-ptr
446     dst class store-header
447     dst class store-tagged
448     nursery-ptr size inc-allot-ptr ;
449
450 M:: x86 %write-barrier ( src card# table -- )
451     #! Mark the card pointed to by vreg.
452     ! Mark the card
453     card# src MOV
454     card# card-bits SHR
455     table "cards_offset" f %alien-global
456     table table [] MOV
457     table card# [+] card-mark <byte> MOV
458
459     ! Mark the card deck
460     card# deck-bits card-bits - SHR
461     table "decks_offset" f %alien-global
462     table table [] MOV
463     table card# [+] card-mark <byte> MOV ;
464
465 :: check-nursery ( temp1 temp2 -- )
466     temp1 load-zone-ptr
467     temp2 temp1 cell [+] MOV
468     temp2 1024 ADD
469     temp1 temp1 3 cells [+] MOV
470     temp2 temp1 CMP ;
471
472 GENERIC# save-gc-root 1 ( gc-root operand temp -- )
473
474 M:: spill-slot save-gc-root ( gc-root spill-slot temp -- )
475     temp spill-slot n>> spill-integer@ MOV
476     gc-root gc-root@ temp MOV ;
477
478 M:: word save-gc-root ( gc-root register temp -- )
479     gc-root gc-root@ register MOV ;
480
481 : save-gc-roots ( gc-roots temp -- )
482     '[ _ save-gc-root ] assoc-each ;
483
484 GENERIC# load-gc-root 1 ( gc-root operand temp -- )
485
486 M:: spill-slot load-gc-root ( gc-root spill-slot temp -- )
487     temp gc-root gc-root@ MOV
488     spill-slot n>> spill-integer@ temp MOV ;
489
490 M:: word load-gc-root ( gc-root register temp -- )
491     register gc-root gc-root@ MOV ;
492
493 : load-gc-roots ( gc-roots temp -- )
494     '[ _ load-gc-root ] assoc-each ;
495
496 :: call-gc ( gc-root-count -- )
497     ! Pass pointer to start of GC roots as first parameter
498     param-reg-1 gc-root-base param@ LEA
499     ! Pass number of roots as second parameter
500     param-reg-2 gc-root-count MOV
501     ! Call GC
502     %prepare-alien-invoke
503     "inline_gc" f %alien-invoke ;
504
505 M:: x86 %gc ( temp1 temp2 gc-roots gc-root-count -- )
506     "end" define-label
507     temp1 temp2 check-nursery
508     "end" get JLE
509     gc-roots temp1 save-gc-roots
510     gc-root-count call-gc
511     gc-roots temp1 load-gc-roots
512     "end" resolve-label ;
513
514 M: x86 %alien-global
515     [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
516
517 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
518
519 :: %boolean ( dst temp word -- )
520     dst \ f tag-number MOV
521     temp 0 MOV \ t rc-absolute-cell rel-immediate
522     dst temp word execute ; inline
523
524 M: x86 %compare ( dst temp cc src1 src2 -- )
525     CMP {
526         { cc< [ \ CMOVL %boolean ] }
527         { cc<= [ \ CMOVLE %boolean ] }
528         { cc> [ \ CMOVG %boolean ] }
529         { cc>= [ \ CMOVGE %boolean ] }
530         { cc= [ \ CMOVE %boolean ] }
531         { cc/= [ \ CMOVNE %boolean ] }
532     } case ;
533
534 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
535     %compare ;
536
537 M: x86 %compare-float ( dst temp cc src1 src2 -- )
538     UCOMISD {
539         { cc< [ \ CMOVB %boolean ] }
540         { cc<= [ \ CMOVBE %boolean ] }
541         { cc> [ \ CMOVA %boolean ] }
542         { cc>= [ \ CMOVAE %boolean ] }
543         { cc= [ \ CMOVE %boolean ] }
544         { cc/= [ \ CMOVNE %boolean ] }
545     } case ;
546
547 M: x86 %compare-branch ( label cc src1 src2 -- )
548     CMP {
549         { cc< [ JL ] }
550         { cc<= [ JLE ] }
551         { cc> [ JG ] }
552         { cc>= [ JGE ] }
553         { cc= [ JE ] }
554         { cc/= [ JNE ] }
555     } case ;
556
557 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
558     %compare-branch ;
559
560 M: x86 %compare-float-branch ( label cc src1 src2 -- )
561     UCOMISD {
562         { cc< [ JB ] }
563         { cc<= [ JBE ] }
564         { cc> [ JA ] }
565         { cc>= [ JAE ] }
566         { cc= [ JE ] }
567         { cc/= [ JNE ] }
568     } case ;
569
570 M: x86 %spill-integer ( src n -- ) spill-integer@ swap MOV ;
571 M: x86 %reload-integer ( dst n -- ) spill-integer@ MOV ;
572
573 M: x86 %spill-float ( src n -- ) spill-float@ swap MOVSD ;
574 M: x86 %reload-float ( dst n -- ) spill-float@ MOVSD ;
575
576 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
577
578 M: int-regs %save-param-reg drop [ param@ ] dip MOV ;
579 M: int-regs %load-param-reg drop swap param@ MOV ;
580
581 GENERIC: MOVSS/D ( dst src reg-class -- )
582
583 M: single-float-regs MOVSS/D drop MOVSS ;
584 M: double-float-regs MOVSS/D drop MOVSD ;
585
586 M: float-regs %save-param-reg [ param@ ] 2dip MOVSS/D ;
587 M: float-regs %load-param-reg [ swap param@ ] dip MOVSS/D ;
588
589 GENERIC: push-return-reg ( reg-class -- )
590 GENERIC: load-return-reg ( n reg-class -- )
591 GENERIC: store-return-reg ( n reg-class -- )
592
593 M: x86 %prepare-alien-invoke
594     #! Save Factor stack pointers in case the C code calls a
595     #! callback which does a GC, which must reliably trace
596     #! all roots.
597     temp-reg-1 "stack_chain" f %alien-global
598     temp-reg-1 temp-reg-1 [] MOV
599     temp-reg-1 [] stack-reg MOV
600     temp-reg-1 [] cell SUB
601     temp-reg-1 2 cells [+] ds-reg MOV
602     temp-reg-1 3 cells [+] rs-reg MOV ;
603
604 M: x86 value-struct? drop t ;
605
606 M: x86 small-enough? ( n -- ? )
607     HEX: -80000000 HEX: 7fffffff between? ;
608
609 : next-stack@ ( n -- operand )
610     #! nth parameter from the next stack frame. Used to box
611     #! input values to callbacks; the callback has its own
612     #! stack frame set up, and we want to read the frame
613     #! set up by the caller.
614     stack-frame get total-size>> + stack@ ;