]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/x86.factor
Merge commit 'origin/master' into emacs
[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-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 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 : ?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 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
268 ! On x86-64, all registers have 8-bit versions. However, a similar
269 ! problem arises for shifts, where the shift count must be in CL, and
270 ! so one day I will fix this properly by adding precoloring to the
271 ! register allocator.
272
273 HOOK: has-small-reg? cpu ( reg size -- ? )
274
275 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
276
277 M: x86.32 has-small-reg?
278     {
279         { 8 [ have-byte-regs memq? ] }
280         { 16 [ drop t ] }
281         { 32 [ drop t ] }
282     } case ;
283
284 M: x86.64 has-small-reg? 2drop t ;
285
286 : small-reg-that-isn't ( exclude -- reg' )
287     [ have-byte-regs ] dip
288     [ native-version-of ] map
289     '[ _ memq? not ] find nip ;
290
291 : with-save/restore ( reg quot -- )
292     [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
293
294 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
295     ! If the destination register overlaps a small register with
296     ! 'size' bits, we call the quot with that. Otherwise, we find a
297     ! small register that is not in exclude, and call quot, saving and
298     ! restoring the small register.
299     dst size has-small-reg? [ dst quot call ] [
300         exclude small-reg-that-isn't
301         [ quot call ] with-save/restore
302     ] if ; inline
303
304 M:: x86 %string-nth ( dst src index temp -- )
305     ! We request a small-reg of size 8 since those of size 16 are
306     ! a superset.
307     "end" define-label
308     dst { src index temp } 8 [| new-dst |
309         ! Load the least significant 7 bits into new-dst.
310         ! 8th bit indicates whether we have to load from
311         ! the aux vector or not.
312         temp src index [+] LEA
313         new-dst 8-bit-version-of temp string-offset [+] MOV
314         new-dst new-dst 8-bit-version-of MOVZX
315         ! Do we have to look at the aux vector?
316         new-dst HEX: 80 CMP
317         "end" get JL
318         ! Yes, this is a non-ASCII character. Load aux vector
319         temp src string-aux-offset [+] MOV
320         new-dst temp XCHG
321         ! Compute index
322         new-dst index ADD
323         new-dst index ADD
324         ! Load high 16 bits
325         new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
326         new-dst new-dst 16-bit-version-of MOVZX
327         new-dst 7 SHL
328         ! Compute code point
329         new-dst temp XOR
330         "end" resolve-label
331         dst new-dst ?MOV
332     ] with-small-register ;
333
334 M:: x86 %set-string-nth-fast ( ch str index temp -- )
335     ch { index str temp } 8 [| new-ch |
336         new-ch ch ?MOV
337         temp str index [+] LEA
338         temp string-offset [+] new-ch 8-bit-version-of MOV
339     ] with-small-register ;
340
341 :: %alien-integer-getter ( dst src size quot -- )
342     dst { src } size [| new-dst |
343         new-dst dup size n-bit-version-of dup src [] MOV
344         quot call
345         dst new-dst ?MOV
346     ] with-small-register ; inline
347
348 : %alien-unsigned-getter ( dst src size -- )
349     [ MOVZX ] %alien-integer-getter ; inline
350
351 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
352 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
353 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
354
355 : %alien-signed-getter ( dst src size -- )
356     [ MOVSX ] %alien-integer-getter ; inline
357
358 M: x86 %alien-signed-1 8 %alien-signed-getter ;
359 M: x86 %alien-signed-2 16 %alien-signed-getter ;
360 M: x86 %alien-signed-4 32 %alien-signed-getter ;
361
362 M: x86 %alien-cell [] MOV ;
363 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
364 M: x86 %alien-double [] MOVSD ;
365
366 :: %alien-integer-setter ( ptr value size -- )
367     value { ptr } size [| new-value |
368         new-value value ?MOV
369         ptr [] new-value size n-bit-version-of MOV
370     ] with-small-register ; inline
371
372 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
373 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
374 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
375 M: x86 %set-alien-cell [ [] ] dip MOV ;
376 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
377 M: x86 %set-alien-double [ [] ] dip MOVSD ;
378
379 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
380
381 :: emit-shift ( dst src1 src2 quot -- )
382     src2 shift-count? [
383         dst CL quot call
384     ] [
385         dst shift-count? [
386             dst src2 XCHG
387             src2 CL quot call
388             dst src2 XCHG
389         ] [
390             ECX native-version-of [
391                 CL src2 MOV
392                 drop dst CL quot call
393             ] with-save/restore
394         ] if
395     ] if ; inline
396
397 M: x86 %shl [ SHL ] emit-shift ;
398 M: x86 %shr [ SHR ] emit-shift ;
399 M: x86 %sar [ SAR ] emit-shift ;
400
401 : load-zone-ptr ( reg -- )
402     #! Load pointer to start of zone array
403     0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
404
405 : load-allot-ptr ( nursery-ptr allot-ptr -- )
406     [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
407
408 : inc-allot-ptr ( nursery-ptr n -- )
409     [ cell [+] ] dip 8 align ADD ;
410
411 : store-header ( temp class -- )
412     [ [] ] [ type-number tag-fixnum ] bi* MOV ;
413
414 : store-tagged ( dst tag -- )
415     tag-number OR ;
416
417 M:: x86 %allot ( dst size class nursery-ptr -- )
418     nursery-ptr dst load-allot-ptr
419     dst class store-header
420     dst class store-tagged
421     nursery-ptr size inc-allot-ptr ;
422
423 M:: x86 %write-barrier ( src card# table -- )
424     #! Mark the card pointed to by vreg.
425     ! Mark the card
426     card# src MOV
427     card# card-bits SHR
428     table "cards_offset" f %alien-global
429     table table [] MOV
430     table card# [+] card-mark <byte> MOV
431
432     ! Mark the card deck
433     card# deck-bits card-bits - SHR
434     table "decks_offset" f %alien-global
435     table table [] MOV
436     table card# [+] card-mark <byte> MOV ;
437
438 M:: x86 %check-nursery ( label temp1 temp2 -- )
439     temp1 load-zone-ptr
440     temp2 temp1 cell [+] MOV
441     temp2 1024 ADD
442     temp1 temp1 3 cells [+] MOV
443     temp2 temp1 CMP
444     label JLE ;
445
446 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
447
448 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
449
450 M:: x86 %call-gc ( gc-root-count -- )
451     ! Pass pointer to start of GC roots as first parameter
452     param-reg-1 gc-root-base param@ LEA
453     ! Pass number of roots as second parameter
454     param-reg-2 gc-root-count MOV
455     ! Call GC
456     %prepare-alien-invoke
457     "inline_gc" f %alien-invoke ;
458
459 M: x86 %alien-global
460     [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
461
462 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
463
464 :: %boolean ( dst temp word -- )
465     dst \ f tag-number MOV
466     temp 0 MOV \ t rc-absolute-cell rel-immediate
467     dst temp word execute ; inline
468
469 M: x86 %compare ( dst temp cc src1 src2 -- )
470     CMP {
471         { cc< [ \ CMOVL %boolean ] }
472         { cc<= [ \ CMOVLE %boolean ] }
473         { cc> [ \ CMOVG %boolean ] }
474         { cc>= [ \ CMOVGE %boolean ] }
475         { cc= [ \ CMOVE %boolean ] }
476         { cc/= [ \ CMOVNE %boolean ] }
477     } case ;
478
479 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
480     %compare ;
481
482 M: x86 %compare-float ( dst temp cc src1 src2 -- )
483     UCOMISD {
484         { cc< [ \ CMOVB %boolean ] }
485         { cc<= [ \ CMOVBE %boolean ] }
486         { cc> [ \ CMOVA %boolean ] }
487         { cc>= [ \ CMOVAE %boolean ] }
488         { cc= [ \ CMOVE %boolean ] }
489         { cc/= [ \ CMOVNE %boolean ] }
490     } case ;
491
492 M: x86 %compare-branch ( label cc src1 src2 -- )
493     CMP {
494         { cc< [ JL ] }
495         { cc<= [ JLE ] }
496         { cc> [ JG ] }
497         { cc>= [ JGE ] }
498         { cc= [ JE ] }
499         { cc/= [ JNE ] }
500     } case ;
501
502 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
503     %compare-branch ;
504
505 M: x86 %compare-float-branch ( label cc src1 src2 -- )
506     UCOMISD {
507         { cc< [ JB ] }
508         { cc<= [ JBE ] }
509         { cc> [ JA ] }
510         { cc>= [ JAE ] }
511         { cc= [ JE ] }
512         { cc/= [ JNE ] }
513     } case ;
514
515 M: x86 %spill-integer ( src n -- ) spill-integer@ swap MOV ;
516 M: x86 %reload-integer ( dst n -- ) spill-integer@ MOV ;
517
518 M: x86 %spill-float ( src n -- ) spill-float@ swap MOVSD ;
519 M: x86 %reload-float ( dst n -- ) spill-float@ MOVSD ;
520
521 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
522
523 M: int-regs %save-param-reg drop [ param@ ] dip MOV ;
524 M: int-regs %load-param-reg drop swap param@ MOV ;
525
526 GENERIC: MOVSS/D ( dst src reg-class -- )
527
528 M: single-float-regs MOVSS/D drop MOVSS ;
529 M: double-float-regs MOVSS/D drop MOVSD ;
530
531 M: float-regs %save-param-reg [ param@ ] 2dip MOVSS/D ;
532 M: float-regs %load-param-reg [ swap param@ ] dip MOVSS/D ;
533
534 GENERIC: push-return-reg ( reg-class -- )
535 GENERIC: load-return-reg ( n reg-class -- )
536 GENERIC: store-return-reg ( n reg-class -- )
537
538 M: x86 %prepare-alien-invoke
539     #! Save Factor stack pointers in case the C code calls a
540     #! callback which does a GC, which must reliably trace
541     #! all roots.
542     temp-reg-1 "stack_chain" f %alien-global
543     temp-reg-1 temp-reg-1 [] MOV
544     temp-reg-1 [] stack-reg MOV
545     temp-reg-1 [] cell SUB
546     temp-reg-1 2 cells [+] ds-reg MOV
547     temp-reg-1 3 cells [+] rs-reg MOV ;
548
549 M: x86 value-struct? drop t ;
550
551 M: x86 small-enough? ( n -- ? )
552     HEX: -80000000 HEX: 7fffffff between? ;
553
554 : next-stack@ ( n -- operand )
555     #! nth parameter from the next stack frame. Used to box
556     #! input values to callbacks; the callback has its own
557     #! stack frame set up, and we want to read the frame
558     #! set up by the caller.
559     stack-frame get total-size>> + stack@ ;