]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
FFI rewrite part 1: split up ##alien-invoke and friends into smaller instructions
[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
6 command-line make words compiler compiler.units
7 compiler.constants compiler.alien compiler.codegen
8 compiler.codegen.alien compiler.codegen.fixup
9 compiler.cfg.instructions compiler.cfg.builder
10 compiler.cfg.intrinsics compiler.cfg.stack-frame
11 cpu.x86.assembler cpu.x86.assembler.operands cpu.x86
12 cpu.architecture vm ;
13 FROM: layouts => cell ;
14 IN: cpu.x86.32
15
16 M: x86.32 machine-registers
17     {
18         { int-regs { EAX ECX EDX EBP EBX } }
19         { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
20     } ;
21
22 M: x86.32 ds-reg ESI ;
23 M: x86.32 rs-reg EDI ;
24 M: x86.32 stack-reg ESP ;
25 M: x86.32 frame-reg EBP ;
26 M: x86.32 temp-reg ECX ;
27
28 M: x86.32 immediate-comparand? ( obj -- ? ) drop t ;
29
30 M:: x86.32 %load-vector ( dst val rep -- )
31     dst 0 [] rep copy-memory* val rc-absolute rel-binary-literal ;
32
33 M: x86.32 %load-float ( dst val -- )
34     <float> float-rep %load-vector ;
35
36 M: x86.32 %load-double ( dst val -- )
37     <double> double-rep %load-vector ;
38
39 M: x86.32 %mov-vm-ptr ( reg -- )
40     0 MOV 0 rc-absolute-cell rel-vm ;
41
42 M: x86.32 %vm-field ( dst field -- )
43     [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
44
45 M: x86.32 %set-vm-field ( dst field -- )
46     [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
47
48 M: x86.32 %vm-field-ptr ( dst field -- )
49     [ 0 MOV ] dip rc-absolute-cell rel-vm ;
50
51 : local@ ( n -- op )
52     stack-frame get extra-stack-space dup 16 assert= + stack@ ;
53
54 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
55
56 M: x86.32 %mark-card
57     drop HEX: ffffffff [+] card-mark <byte> MOV
58     building get pop
59     rc-absolute-cell rel-cards-offset
60     building get push ;
61
62 M: x86.32 %mark-deck
63     drop HEX: ffffffff [+] card-mark <byte> MOV
64     building get pop
65     rc-absolute-cell rel-decks-offset
66     building get push ;
67
68 M:: x86.32 %dispatch ( src temp -- )
69     ! Load jump table base.
70     temp src HEX: ffffffff [+] LEA
71     building get length :> start
72     0 rc-absolute-cell rel-here
73     ! Go
74     temp HEX: 7f [+] JMP
75     building get length :> end
76     ! Fix up the displacement above
77     cell alignment
78     [ end start - + building get dup pop* push ]
79     [ (align-code) ]
80     bi ;
81
82 M: x86.32 pic-tail-reg EDX ;
83
84 M: x86.32 reserved-stack-space 0 ;
85
86 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
87
88 : save-vm-ptr ( n -- )
89     stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
90
91 M: x86.32 return-struct-in-registers? ( c-type -- ? )
92     c-type
93     [ return-in-registers?>> ]
94     [ heap-size { 1 2 4 8 } member? ] bi
95     os { linux netbsd solaris } member? not
96     and or ;
97
98 : struct-return@ ( n -- operand )
99     [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
100
101 ! On x86, parameters are usually never passed in registers, except with Microsoft's
102 ! "thiscall" and "fastcall" abis
103 M: int-regs return-reg drop EAX ;
104 M: float-regs param-regs 2drop { } ;
105
106 M: int-regs param-regs
107     nip {
108         { thiscall [ { ECX     } ] }
109         { fastcall [ { ECX EDX } ] }
110         [ drop { } ]
111     } case ;
112
113 GENERIC: load-return-reg ( src rep -- )
114 GENERIC: store-return-reg ( dst rep -- )
115
116 M: stack-params load-return-reg drop EAX swap MOV ;
117 M: stack-params store-return-reg drop EAX MOV ;
118
119 M: int-rep load-return-reg drop EAX swap MOV ;
120 M: int-rep store-return-reg drop EAX MOV ;
121
122 M: float-rep load-return-reg drop FLDS ;
123 M: float-rep store-return-reg drop FSTPS ;
124
125 M: double-rep load-return-reg drop FLDL ;
126 M: double-rep store-return-reg drop FSTPL ;
127
128 M: x86.32 %prologue ( n -- )
129     dup PUSH
130     0 PUSH rc-absolute-cell rel-this
131     3 cells - decr-stack-reg ;
132
133 M: x86.32 %prepare-jump
134     pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
135
136 M: stack-params copy-register*
137     drop
138     {
139         { [ dup  integer? ] [ EAX swap next-stack@ MOV  EAX MOV ] }
140         { [ over integer? ] [ EAX swap MOV              param@ EAX MOV ] }
141     } cond ;
142
143 M: x86.32 %save-param-reg [ local@ ] 2dip %copy ;
144
145 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
146
147 : (%box) ( n rep -- )
148     #! If n is f, push the return register onto the stack; we
149     #! are boxing a return value of a C function. If n is an
150     #! integer, push [ESP+n] on the stack; we are boxing a
151     #! parameter being passed to a callback from C.
152     over [ [ local@ ] dip load-return-reg ] [ 2drop ] if ;
153
154 M:: x86.32 %box ( n rep func -- )
155     n rep (%box)
156     rep rep-size save-vm-ptr
157     0 stack@ rep store-return-reg
158     func f %alien-invoke ;
159
160 : (%box-long-long) ( n -- )
161     [
162         EDX over next-stack@ MOV
163         EAX swap cell - next-stack@ MOV 
164     ] when* ;
165
166 M: x86.32 %box-long-long ( n func -- )
167     [ (%box-long-long) ] dip
168     8 save-vm-ptr
169     4 stack@ EDX MOV
170     0 stack@ EAX MOV
171     f %alien-invoke ;
172
173 M:: x86.32 %box-large-struct ( n c-type -- )
174     EDX n struct-return@ LEA
175     8 save-vm-ptr
176     4 stack@ c-type heap-size MOV
177     0 stack@ EDX MOV
178     "from_value_struct" f %alien-invoke ;
179
180 M: x86.32 %prepare-box-struct ( -- )
181     ! Compute target address for value struct return
182     EAX f struct-return@ LEA
183     ! Store it as the first parameter
184     0 local@ EAX MOV ;
185
186 M: x86.32 %box-small-struct ( c-type -- )
187     #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
188     12 save-vm-ptr
189     8 stack@ swap heap-size MOV
190     4 stack@ EDX MOV
191     0 stack@ EAX MOV
192     "from_small_struct" f %alien-invoke ;
193
194 M: x86.32 %pop-stack ( n -- )
195     EAX swap ds-reg reg-stack MOV ;
196
197 M: x86.32 %pop-context-stack ( -- )
198     temp-reg %context
199     EAX temp-reg "datastack" context-field-offset [+] MOV
200     EAX EAX [] MOV
201     temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
202
203 : call-unbox-func ( func -- )
204     4 save-vm-ptr
205     0 stack@ EAX MOV
206     f %alien-invoke ;
207
208 M: x86.32 %unbox ( n rep func -- )
209     #! The value being unboxed must already be in EAX.
210     #! If n is f, we're unboxing a return value about to be
211     #! returned by the callback. Otherwise, we're unboxing
212     #! a parameter to a C function about to be called.
213     call-unbox-func
214     ! Store the return value on the C stack
215     over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
216
217 M: x86.32 %unbox-long-long ( n func -- )
218     call-unbox-func
219     ! Store the return value on the C stack
220     [
221         [ local@ EAX MOV ]
222         [ 4 + local@ EDX MOV ] bi
223     ] when* ;
224
225 : %unbox-struct-1 ( -- )
226     #! Alien must be in EAX.
227     4 save-vm-ptr
228     0 stack@ EAX MOV
229     "alien_offset" f %alien-invoke
230     ! Load first cell
231     EAX EAX [] MOV ;
232
233 : %unbox-struct-2 ( -- )
234     #! Alien must be in EAX.
235     4 save-vm-ptr
236     0 stack@ EAX MOV
237     "alien_offset" f %alien-invoke
238     ! Load second cell
239     EDX EAX 4 [+] MOV
240     ! Load first cell
241     EAX EAX [] MOV ;
242
243 M: x86 %unbox-small-struct ( size -- )
244     #! Alien must be in EAX.
245     heap-size cell align cell /i {
246         { 1 [ %unbox-struct-1 ] }
247         { 2 [ %unbox-struct-2 ] }
248     } case ;
249
250 M:: x86.32 %unbox-large-struct ( n c-type -- )
251     ! Alien must be in EAX.
252     ! Compute destination address
253     EDX n local@ LEA
254     12 save-vm-ptr
255     8 stack@ c-type heap-size MOV
256     4 stack@ EDX MOV
257     0 stack@ EAX MOV
258     "to_value_struct" f %alien-invoke ;
259
260 M: x86.32 %prepare-alien-indirect ( -- )
261     EAX ds-reg [] MOV
262     ds-reg 4 SUB
263     4 save-vm-ptr
264     0 stack@ EAX MOV
265     "pinned_alien_offset" f %alien-invoke
266     EBP EAX MOV ;
267
268 M: x86.32 %alien-indirect ( -- )
269     EBP CALL ;
270
271 M: x86.32 %begin-callback ( -- )
272     0 save-vm-ptr
273     ESP 4 [+] 0 MOV
274     "begin_callback" f %alien-invoke ;
275
276 M: x86.32 %alien-callback ( quot -- )
277     EAX EDX %restore-context
278     EAX swap %load-reference
279     EAX quot-entry-point-offset [+] CALL
280     EAX EDX %save-context ;
281
282 M: x86.32 %end-callback ( -- )
283     0 save-vm-ptr
284     "end_callback" f %alien-invoke ;
285
286 M: x86.32 %to-nv ( -- ) 4 stack@ EAX MOV ;
287
288 M: x86.32 %from-nv ( -- ) EAX 4 stack@ MOV ;
289
290 GENERIC: float-function-param ( stack-slot dst src -- )
291
292 M:: spill-slot float-function-param ( stack-slot dst src -- )
293     ! We can clobber dst here since its going to contain the
294     ! final result
295     dst src double-rep %copy
296     stack-slot dst double-rep %copy ;
297
298 M: register float-function-param
299     nip double-rep %copy ;
300
301 : float-function-return ( reg -- )
302     ESP [] FSTPL
303     ESP [] MOVSD
304     ESP 16 ADD ;
305
306 M:: x86.32 %unary-float-function ( dst src func -- )
307     ESP -16 [+] dst src float-function-param
308     ESP 16 SUB
309     func "libm" load-library %alien-invoke
310     dst float-function-return ;
311
312 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
313     ESP -16 [+] dst src1 float-function-param
314     ESP  -8 [+] dst src2 float-function-param
315     ESP 16 SUB
316     func "libm" load-library %alien-invoke
317     dst float-function-return ;
318
319 : funny-large-struct-return? ( params -- ? )
320     #! MINGW ABI incompatibility disaster
321     [ return>> large-struct? ]
322     [ abi>> mingw = os windows? not or ]
323     bi and ;
324
325 : stack-arg-size ( params -- n )
326     dup abi>> '[
327         alien-parameters flatten-c-types
328         [ _ alloc-parameter 2drop ] each
329         stack-params get
330     ] with-param-regs ;
331
332 M: x86.32 stack-cleanup ( params -- n )
333     #! a) Functions which are stdcall/fastcall/thiscall have to
334     #! clean up the caller's stack frame.
335     #! b) Functions returning large structs on MINGW have to
336     #! fix ESP.
337     {
338         { [ dup abi>> callee-cleanup? ] [ stack-arg-size ] }
339         { [ dup funny-large-struct-return? ] [ drop 4 ] }
340         [ drop 0 ]
341     } cond ;
342
343 M: x86.32 %cleanup ( params -- )
344     stack-cleanup [ ESP swap SUB ] unless-zero ;
345
346 M:: x86.32 %call-gc ( gc-roots -- )
347     4 save-vm-ptr
348     0 stack@ gc-roots gc-root-offsets %load-reference
349     "inline_gc" f %alien-invoke ;
350
351 M: x86.32 dummy-stack-params? f ;
352
353 M: x86.32 dummy-int-params? f ;
354
355 M: x86.32 dummy-fp-params? f ;
356
357 ! Dreadful
358 M: struct-c-type flatten-c-type stack-params (flatten-c-type) ;
359 M: long-long-type flatten-c-type stack-params (flatten-c-type) ;
360 M: c-type flatten-c-type dup rep>> int-rep? int-rep stack-params ? (flatten-c-type) ;
361
362 M: x86.32 struct-return-pointer-type
363     os linux? void* (stack-value) ? ;
364
365 check-sse