]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
Merge branch 'master' of http://factorcode.org/git/factor
[factor.git] / basis / cpu / x86 / 32 / 32.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien alien.c-types alien.libraries alien.syntax
4 arrays kernel fry math namespaces sequences system layouts io
5 vocabs.loader accessors init classes.struct combinators make
6 words compiler.constants compiler.codegen.fixup
7 compiler.cfg.instructions compiler.cfg.builder
8 compiler.cfg.builder.alien.boxing compiler.cfg.intrinsics
9 compiler.cfg.stack-frame cpu.x86.assembler
10 cpu.x86.assembler.operands cpu.x86 cpu.architecture vm vocabs ;
11 FROM: layouts => cell ;
12 IN: cpu.x86.32
13
14 : x86-float-regs ( -- seq )
15     "cpu.x86.sse" vocab
16     { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 }
17     { ST0 ST1 ST2 ST3 ST4 ST5 ST6 }
18     ? ;
19
20 M: x86.32 machine-registers
21     { int-regs { EAX ECX EDX EBP EBX } }
22     float-regs x86-float-regs 2array
23     2array ;
24
25 M: x86.32 ds-reg ESI ;
26 M: x86.32 rs-reg EDI ;
27 M: x86.32 stack-reg ESP ;
28 M: x86.32 frame-reg EBP ;
29
30 M: x86.32 immediate-comparand? ( obj -- ? ) drop t ;
31
32 M:: x86.32 %load-vector ( dst val rep -- )
33     dst 0 [] rep copy-memory* val rc-absolute rel-binary-literal ;
34
35 M: x86.32 %mov-vm-ptr ( reg -- )
36     0 MOV 0 rc-absolute-cell rel-vm ;
37
38 M: x86.32 %vm-field ( dst field -- )
39     [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
40
41 M: x86.32 %set-vm-field ( dst field -- )
42     [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
43
44 M: x86.32 %vm-field-ptr ( dst field -- )
45     [ 0 MOV ] dip rc-absolute-cell rel-vm ;
46
47 M: x86.32 %mark-card
48     drop HEX: ffffffff [+] card-mark <byte> MOV
49     building get pop
50     rc-absolute-cell rel-cards-offset
51     building get push ;
52
53 M: x86.32 %mark-deck
54     drop HEX: ffffffff [+] card-mark <byte> MOV
55     building get pop
56     rc-absolute-cell rel-decks-offset
57     building get push ;
58
59 M: x86.32 pic-tail-reg EDX ;
60
61 M: x86.32 reserved-stack-space 0 ;
62
63 M: x86.32 vm-stack-space 16 ;
64
65 : save-vm-ptr ( n -- )
66     stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
67
68 M: x86.32 return-struct-in-registers? ( c-type -- ? )
69     c-type
70     [ return-in-registers?>> ]
71     [ heap-size { 1 2 4 8 } member? ] bi
72     os { linux netbsd solaris } member? not
73     and or ;
74
75 ! On x86, parameters are usually never passed in registers,
76 ! except with Microsoft's "thiscall" and "fastcall" abis
77 M: x86.32 param-regs
78     {
79         { thiscall [ { { int-regs { ECX } } { float-regs { } } } ] }
80         { fastcall [ { { int-regs { ECX EDX } } { float-regs { } } } ] }
81         [ drop { { int-regs { } } { float-regs { } } } ]
82     } case ;
83
84 ! Need a fake return-reg for floats
85 M: x86.32 return-regs
86     {
87         { int-regs { EAX EDX } }
88         { float-regs { ST0 } }
89     } ;
90
91 M: x86.32 %prologue ( n -- )
92     dup PUSH
93     0 PUSH rc-absolute-cell rel-this
94     3 cells - decr-stack-reg ;
95
96 M: x86.32 %prepare-jump
97     pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
98
99 :: load-float-return ( dst x87-insn rep -- )
100     dst register? [
101         ESP 4 SUB
102         ESP [] x87-insn execute
103         dst ESP [] rep %copy
104         ESP 4 ADD
105     ] [
106         dst ?spill-slot x87-insn execute
107     ] if ; inline
108
109 M: x86.32 %load-reg-param ( dst reg rep -- )
110     {
111         { int-rep [ int-rep %copy ] }
112         { float-rep [ drop \ FSTPS float-rep load-float-return ] }
113         { double-rep [ drop \ FSTPL double-rep load-float-return ] }
114     } case ;
115
116 :: store-float-return ( src x87-insn rep -- )
117     src register? [
118         ESP 4 SUB
119         ESP [] src rep %copy
120         ESP [] x87-insn execute
121         ESP 4 ADD
122     ] [
123         src ?spill-slot x87-insn execute
124     ] if ; inline
125
126 M: x86.32 %store-reg-param ( src reg rep -- )
127     {
128         { int-rep [ swap int-rep %copy ] }
129         { float-rep [ drop \ FLDS float-rep store-float-return ] }
130         { double-rep [ drop \ FLDL double-rep store-float-return ] }
131     } case ;
132
133 :: call-unbox-func ( src func -- )
134     EAX src tagged-rep %copy
135     4 save-vm-ptr
136     0 stack@ EAX MOV
137     func f %alien-invoke ;
138
139 M:: x86.32 %unbox ( dst src func rep -- )
140     src func call-unbox-func
141     dst rep %load-return ;
142
143 M:: x86.32 %unbox-long-long ( src out func -- )
144     EAX src int-rep %copy
145     0 stack@ EAX MOV
146     EAX out int-rep %copy
147     4 stack@ EAX MOV
148     8 save-vm-ptr
149     func f %alien-invoke ;
150
151 M:: x86.32 %box ( dst src func rep -- )
152     rep rep-size save-vm-ptr
153     src rep %store-return
154     0 stack@ rep %load-return
155     func f %alien-invoke
156     dst EAX tagged-rep %copy ;
157
158 M:: x86.32 %box-long-long ( dst src1 src2 func -- )
159     8 save-vm-ptr
160     EAX src1 int-rep %copy
161     0 stack@ EAX int-rep %copy
162     EAX src2 int-rep %copy
163     4 stack@ EAX int-rep %copy
164     func f %alien-invoke
165     dst EAX tagged-rep %copy ;
166
167 M:: x86.32 %allot-byte-array ( dst size -- )
168     4 save-vm-ptr
169     0 stack@ size MOV
170     "allot_byte_array" f %alien-invoke
171     dst EAX tagged-rep %copy ;
172
173 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
174
175 M: x86.32 %begin-callback ( -- )
176     0 save-vm-ptr
177     4 stack@ 0 MOV
178     "begin_callback" f %alien-invoke ;
179
180 M: x86.32 %alien-callback ( quot -- )
181     [ EAX ] dip %load-reference
182     EAX quot-entry-point-offset [+] CALL ;
183
184 M: x86.32 %end-callback ( -- )
185     0 save-vm-ptr
186     "end_callback" f %alien-invoke ;
187
188 GENERIC: float-function-param ( n dst src -- )
189
190 M:: spill-slot float-function-param ( n dst src -- )
191     ! We can clobber dst here since its going to contain the
192     ! final result
193     dst src double-rep %copy
194     dst n double-rep %store-stack-param ;
195
196 M:: register float-function-param ( n dst src -- )
197     src n double-rep %store-stack-param ;
198
199 M:: x86.32 %unary-float-function ( dst src func -- )
200     0 dst src float-function-param
201     func "libm" load-library %alien-invoke
202     dst double-rep %load-return ;
203
204 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
205     0 dst src1 float-function-param
206     8 dst src2 float-function-param
207     func "libm" load-library %alien-invoke
208     dst double-rep %load-return ;
209
210 : funny-large-struct-return? ( return abi -- ? )
211     #! MINGW ABI incompatibility disaster
212     [ large-struct? ] [ mingw eq? os windows? not or ] bi* and ;
213
214 M:: x86.32 stack-cleanup ( stack-size return abi -- n )
215     #! a) Functions which are stdcall/fastcall/thiscall have to
216     #! clean up the caller's stack frame.
217     #! b) Functions returning large structs on MINGW have to
218     #! fix ESP.
219     {
220         { [ abi callee-cleanup? ] [ stack-size ] }
221         { [ return abi funny-large-struct-return? ] [ 4 ] }
222         [ 0 ]
223     } cond ;
224
225 M: x86.32 %cleanup ( n -- )
226     [ ESP swap SUB ] unless-zero ;
227
228 M: x86.32 dummy-stack-params? f ;
229
230 M: x86.32 dummy-int-params? f ;
231
232 M: x86.32 dummy-fp-params? f ;
233
234 M: x86.32 long-long-on-stack? t ;
235
236 M: x86.32 float-on-stack? t ;
237
238 M: x86.32 flatten-struct-type
239     call-next-method [ first t 2array ] map ;
240
241 M: x86.32 struct-return-on-stack? os linux? not ;
242
243 check-sse