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