]> 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.x86.assembler.operands
5 cpu.architecture kernel kernel.private math memory namespaces make
6 sequences 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@ ( n -- op ) spill-offset param@ ;
34
35 : gc-root@ ( n -- op ) gc-root-offset param@ ;
36
37 : decr-stack-reg ( n -- )
38     dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
39
40 : incr-stack-reg ( n -- )
41     dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
42
43 : align-stack ( n -- n' )
44     os macosx? cpu x86.64? or [ 16 align ] when ;
45
46 M: x86 stack-frame-size ( stack-frame -- i )
47     (stack-frame-size) 3 cells reserved-area-size + + align-stack ;
48
49 ! Must be a volatile register not used for parameter passing, for safe
50 ! use in calls in and out of C
51 HOOK: temp-reg cpu ( -- reg )
52
53 ! Fastcall calling convention
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 dup 0 = [ drop dup XOR ] [ MOV ] if ;
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     2over eq? [ nip ADD ] [ [+] LEA ] if ;
112 M: x86 %add-imm 2over eq? [ nip ADD ] [ [+] LEA ] if ;
113 M: x86 %sub     nip SUB ;
114 M: x86 %sub-imm 2over eq? [ nip SUB ] [ neg [+] LEA ] if ;
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
127 M: x86 %min     nip [ CMP ] [ CMOVG ] 2bi ;
128 M: x86 %max     nip [ CMP ] [ CMOVL ] 2bi ;
129
130 M: x86 %not     drop NOT ;
131 M: x86 %log2    BSR ;
132
133 :: overflow-template ( label dst src1 src2 insn -- )
134     src1 src2 insn call
135     label JO ; inline
136
137 M: x86 %fixnum-add ( label dst src1 src2 -- )
138     [ ADD ] overflow-template ;
139
140 M: x86 %fixnum-sub ( label dst src1 src2 -- )
141     [ SUB ] overflow-template ;
142
143 M: x86 %fixnum-mul ( label dst src1 src2 -- )
144     [ swap IMUL2 ] overflow-template ;
145
146 : bignum@ ( reg n -- op )
147     cells bignum tag-number - [+] ; inline
148
149 M:: x86 %integer>bignum ( dst src temp -- )
150     #! on entry, inreg is a signed 32-bit quantity
151     #! exits with tagged ptr to bignum in outreg
152     #! 1 cell header, 1 cell length, 1 cell sign, + digits
153     #! length is the # of digits + sign
154     [
155         "end" define-label
156         ! Load cached zero value
157         dst 0 >bignum %load-reference
158         src 0 CMP
159         ! Is it zero? Then just go to the end and return this zero
160         "end" get JE
161         ! Allocate a bignum
162         dst 4 cells bignum temp %allot
163         ! Write length
164         dst 1 bignum@ 2 tag-fixnum MOV
165         ! Store value
166         dst 3 bignum@ src MOV
167         ! Compute sign
168         temp src MOV
169         temp cell-bits 1 - SAR
170         temp 1 AND
171         ! Store sign
172         dst 2 bignum@ temp MOV
173         ! Make negative value positive
174         temp temp ADD
175         temp NEG
176         temp 1 ADD
177         src temp IMUL2
178         ! Store the bignum
179         dst 3 bignum@ temp MOV
180         "end" resolve-label
181     ] with-scope ;
182
183 M:: x86 %bignum>integer ( dst src temp -- )
184     [
185         "end" define-label
186         ! load length
187         temp src 1 bignum@ MOV
188         ! if the length is 1, its just the sign and nothing else,
189         ! so output 0
190         dst 0 MOV
191         temp 1 tag-fixnum CMP
192         "end" get JE
193         ! load the value
194         dst src 3 bignum@ MOV
195         ! load the sign
196         temp src 2 bignum@ MOV
197         ! convert it into -1 or 1
198         temp temp ADD
199         temp NEG
200         temp 1 ADD
201         ! make dst signed
202         temp dst IMUL2
203         "end" resolve-label
204     ] with-scope ;
205
206 M: x86 %add-float nip ADDSD ;
207 M: x86 %sub-float nip SUBSD ;
208 M: x86 %mul-float nip MULSD ;
209 M: x86 %div-float nip DIVSD ;
210 M: x86 %min-float nip MINSD ;
211 M: x86 %max-float nip MAXSD ;
212 M: x86 %sqrt SQRTSD ;
213
214 M: x86 %integer>float CVTSI2SD ;
215 M: x86 %float>integer CVTTSD2SI ;
216
217 GENERIC: copy-register* ( dst src rep -- )
218
219 M: int-rep copy-register* drop MOV ;
220 M: tagged-rep copy-register* drop MOV ;
221 M: single-float-rep copy-register* drop MOVSS ;
222 M: double-float-rep copy-register* drop MOVSD ;
223
224 : copy-register ( dst src rep -- )
225     2over eq? [ 3drop ] [ copy-register* ] if ;
226
227 M: x86 %copy ( dst src rep -- ) copy-register ;
228
229 M: x86 %unbox-float ( dst src -- )
230     float-offset [+] MOVSD ;
231
232 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
233     [
234         { "is-byte-array" "end" "start" } [ define-label ] each
235         dst 0 MOV
236         temp src MOV
237         ! We come back here with displaced aliens
238         "start" resolve-label
239         ! Is the object f?
240         temp \ f tag-number CMP
241         "end" get JE
242         ! Is the object an alien?
243         temp header-offset [+] alien type-number tag-fixnum CMP
244         "is-byte-array" get JNE
245         ! If so, load the offset and add it to the address
246         dst temp alien-offset [+] ADD
247         ! Now recurse on the underlying alien
248         temp temp underlying-alien-offset [+] MOV
249         "start" get JMP
250         "is-byte-array" resolve-label
251         ! Add byte array address to address being computed
252         dst temp ADD
253         ! Add an offset to start of byte array's data
254         dst byte-array-offset ADD
255         "end" resolve-label
256     ] with-scope ;
257
258 M:: x86 %box-float ( dst src temp -- )
259     dst 16 float temp %allot
260     dst float-offset [+] src MOVSD ;
261
262 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
263
264 :: %allot-alien ( dst displacement base temp -- )
265     dst 4 cells alien temp %allot
266     dst 1 alien@ base MOV ! alien
267     dst 2 alien@ \ f tag-number MOV ! expired
268     dst 3 alien@ displacement MOV ! displacement
269     ;
270
271 M:: x86 %box-alien ( dst src temp -- )
272     [
273         "end" define-label
274         dst \ f tag-number MOV
275         src 0 CMP
276         "end" get JE
277         dst src \ f tag-number temp %allot-alien
278         "end" resolve-label
279     ] with-scope ;
280
281 M:: x86 %box-displaced-alien ( dst displacement base temp -- )
282     [
283         "end" define-label
284         "ok" define-label
285         ! If displacement is zero, return the base
286         dst base MOV
287         displacement 0 CMP
288         "end" get JE
289         ! If base is already a displaced alien, unpack it
290         base \ f tag-number CMP
291         "ok" get JE
292         base header-offset [+] alien type-number tag-fixnum CMP
293         "ok" get JNE
294         ! displacement += base.displacement
295         displacement base 3 alien@ ADD
296         ! base = base.base
297         base base 1 alien@ MOV
298         "ok" resolve-label
299         dst displacement base temp %allot-alien
300         "end" resolve-label
301     ] with-scope ;
302
303 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
304 ! On x86-64, all registers have 8-bit versions. However, a similar
305 ! problem arises for shifts, where the shift count must be in CL, and
306 ! so one day I will fix this properly by adding precoloring to the
307 ! register allocator.
308
309 HOOK: has-small-reg? cpu ( reg size -- ? )
310
311 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
312
313 M: x86.32 has-small-reg?
314     {
315         { 8 [ have-byte-regs memq? ] }
316         { 16 [ drop t ] }
317         { 32 [ drop t ] }
318     } case ;
319
320 M: x86.64 has-small-reg? 2drop t ;
321
322 : small-reg-that-isn't ( exclude -- reg' )
323     [ have-byte-regs ] dip
324     [ native-version-of ] map
325     '[ _ memq? not ] find nip ;
326
327 : with-save/restore ( reg quot -- )
328     [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
329
330 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
331     ! If the destination register overlaps a small register with
332     ! 'size' bits, we call the quot with that. Otherwise, we find a
333     ! small register that is not in exclude, and call quot, saving and
334     ! restoring the small register.
335     dst size has-small-reg? [ dst quot call ] [
336         exclude small-reg-that-isn't
337         [ quot call ] with-save/restore
338     ] if ; inline
339
340 : ?MOV ( dst src -- )
341     2dup = [ 2drop ] [ MOV ] if ; inline
342
343 M:: x86 %string-nth ( dst src index temp -- )
344     ! We request a small-reg of size 8 since those of size 16 are
345     ! a superset.
346     "end" define-label
347     dst { src index temp } 8 [| new-dst |
348         ! Load the least significant 7 bits into new-dst.
349         ! 8th bit indicates whether we have to load from
350         ! the aux vector or not.
351         temp src index [+] LEA
352         new-dst 8-bit-version-of temp string-offset [+] MOV
353         new-dst new-dst 8-bit-version-of MOVZX
354         ! Do we have to look at the aux vector?
355         new-dst HEX: 80 CMP
356         "end" get JL
357         ! Yes, this is a non-ASCII character. Load aux vector
358         temp src string-aux-offset [+] MOV
359         new-dst temp XCHG
360         ! Compute index
361         new-dst index ADD
362         new-dst index ADD
363         ! Load high 16 bits
364         new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
365         new-dst new-dst 16-bit-version-of MOVZX
366         new-dst 7 SHL
367         ! Compute code point
368         new-dst temp XOR
369         "end" resolve-label
370         dst new-dst ?MOV
371     ] with-small-register ;
372
373 M:: x86 %set-string-nth-fast ( ch str index temp -- )
374     ch { index str temp } 8 [| new-ch |
375         new-ch ch ?MOV
376         temp str index [+] LEA
377         temp string-offset [+] new-ch 8-bit-version-of MOV
378     ] with-small-register ;
379
380 :: %alien-integer-getter ( dst src size quot -- )
381     dst { src } size [| new-dst |
382         new-dst dup size n-bit-version-of dup src [] MOV
383         quot call
384         dst new-dst ?MOV
385     ] with-small-register ; inline
386
387 : %alien-unsigned-getter ( dst src size -- )
388     [ MOVZX ] %alien-integer-getter ; inline
389
390 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
391 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
392 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
393
394 : %alien-signed-getter ( dst src size -- )
395     [ MOVSX ] %alien-integer-getter ; inline
396
397 M: x86 %alien-signed-1 8 %alien-signed-getter ;
398 M: x86 %alien-signed-2 16 %alien-signed-getter ;
399 M: x86 %alien-signed-4 32 %alien-signed-getter ;
400
401 M: x86 %alien-cell [] MOV ;
402 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
403 M: x86 %alien-double [] MOVSD ;
404
405 :: %alien-integer-setter ( ptr value size -- )
406     value { ptr } size [| new-value |
407         new-value value ?MOV
408         ptr [] new-value size n-bit-version-of MOV
409     ] with-small-register ; inline
410
411 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
412 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
413 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
414 M: x86 %set-alien-cell [ [] ] dip MOV ;
415 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
416 M: x86 %set-alien-double [ [] ] dip MOVSD ;
417
418 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
419
420 :: emit-shift ( dst src1 src2 quot -- )
421     src2 shift-count? [
422         dst CL quot call
423     ] [
424         dst shift-count? [
425             dst src2 XCHG
426             src2 CL quot call
427             dst src2 XCHG
428         ] [
429             ECX native-version-of [
430                 CL src2 MOV
431                 drop dst CL quot call
432             ] with-save/restore
433         ] if
434     ] if ; inline
435
436 M: x86 %shl [ SHL ] emit-shift ;
437 M: x86 %shr [ SHR ] emit-shift ;
438 M: x86 %sar [ SAR ] emit-shift ;
439
440 : load-zone-ptr ( reg -- )
441     #! Load pointer to start of zone array
442     0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
443
444 : load-allot-ptr ( nursery-ptr allot-ptr -- )
445     [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
446
447 : inc-allot-ptr ( nursery-ptr n -- )
448     [ cell [+] ] dip 8 align ADD ;
449
450 : store-header ( temp class -- )
451     [ [] ] [ type-number tag-fixnum ] bi* MOV ;
452
453 : store-tagged ( dst tag -- )
454     tag-number OR ;
455
456 M:: x86 %allot ( dst size class nursery-ptr -- )
457     nursery-ptr dst load-allot-ptr
458     dst class store-header
459     dst class store-tagged
460     nursery-ptr size inc-allot-ptr ;
461
462 M:: x86 %write-barrier ( src card# table -- )
463     #! Mark the card pointed to by vreg.
464     ! Mark the card
465     card# src MOV
466     card# card-bits SHR
467     table "cards_offset" f %alien-global
468     table table [] MOV
469     table card# [+] card-mark <byte> MOV
470
471     ! Mark the card deck
472     card# deck-bits card-bits - SHR
473     table "decks_offset" f %alien-global
474     table table [] MOV
475     table card# [+] card-mark <byte> MOV ;
476
477 M:: x86 %check-nursery ( label temp1 temp2 -- )
478     temp1 load-zone-ptr
479     temp2 temp1 cell [+] MOV
480     temp2 1024 ADD
481     temp1 temp1 3 cells [+] MOV
482     temp2 temp1 CMP
483     label JLE ;
484
485 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
486
487 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
488
489 M:: x86 %call-gc ( gc-root-count -- )
490     ! Pass pointer to start of GC roots as first parameter
491     param-reg-1 gc-root-base param@ LEA
492     ! Pass number of roots as second parameter
493     param-reg-2 gc-root-count MOV
494     ! Call GC
495     %prepare-alien-invoke
496     "inline_gc" f %alien-invoke ;
497
498 M: x86 %alien-global
499     [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
500
501 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
502
503 :: %boolean ( dst temp word -- )
504     dst \ f tag-number MOV
505     temp 0 MOV \ t rc-absolute-cell rel-immediate
506     dst temp word execute ; inline
507
508 M: x86 %compare ( dst temp cc src1 src2 -- )
509     CMP {
510         { cc< [ \ CMOVL %boolean ] }
511         { cc<= [ \ CMOVLE %boolean ] }
512         { cc> [ \ CMOVG %boolean ] }
513         { cc>= [ \ CMOVGE %boolean ] }
514         { cc= [ \ CMOVE %boolean ] }
515         { cc/= [ \ CMOVNE %boolean ] }
516     } case ;
517
518 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
519     %compare ;
520
521 M: x86 %compare-float ( dst temp cc src1 src2 -- )
522     UCOMISD {
523         { cc< [ \ CMOVB %boolean ] }
524         { cc<= [ \ CMOVBE %boolean ] }
525         { cc> [ \ CMOVA %boolean ] }
526         { cc>= [ \ CMOVAE %boolean ] }
527         { cc= [ \ CMOVE %boolean ] }
528         { cc/= [ \ CMOVNE %boolean ] }
529     } case ;
530
531 M: x86 %compare-branch ( label cc src1 src2 -- )
532     CMP {
533         { cc< [ JL ] }
534         { cc<= [ JLE ] }
535         { cc> [ JG ] }
536         { cc>= [ JGE ] }
537         { cc= [ JE ] }
538         { cc/= [ JNE ] }
539     } case ;
540
541 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
542     %compare-branch ;
543
544 M: x86 %compare-float-branch ( label cc src1 src2 -- )
545     UCOMISD {
546         { cc< [ JB ] }
547         { cc<= [ JBE ] }
548         { cc> [ JA ] }
549         { cc>= [ JAE ] }
550         { cc= [ JE ] }
551         { cc/= [ JNE ] }
552     } case ;
553
554 M: x86 %spill ( src n rep -- ) [ spill@ swap ] dip copy-register ;
555 M: x86 %reload ( dst n rep -- ) [ spill@ ] dip copy-register ;
556
557 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
558
559 M: x86 %prepare-alien-invoke
560     #! Save Factor stack pointers in case the C code calls a
561     #! callback which does a GC, which must reliably trace
562     #! all roots.
563     temp-reg "stack_chain" f %alien-global
564     temp-reg temp-reg [] MOV
565     temp-reg [] stack-reg MOV
566     temp-reg [] cell SUB
567     temp-reg 2 cells [+] ds-reg MOV
568     temp-reg 3 cells [+] rs-reg MOV ;
569
570 M: x86 value-struct? drop t ;
571
572 M: x86 small-enough? ( n -- ? )
573     HEX: -80000000 HEX: 7fffffff between? ;
574
575 : next-stack@ ( n -- operand )
576     #! nth parameter from the next stack frame. Used to box
577     #! input values to callbacks; the callback has its own
578     #! stack frame set up, and we want to read the frame
579     #! set up by the caller.
580     stack-frame get total-size>> + stack@ ;
581
582 : enable-sse2 ( -- )
583     enable-float-intrinsics
584     enable-fsqrt
585     enable-float-min/max ;
586
587 enable-min/max