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 ;
14 : x86-float-regs ( -- seq )
16 { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 }
17 { ST0 ST1 ST2 ST3 ST4 ST5 ST6 }
20 M: x86.32 machine-registers
21 { int-regs { EAX ECX EDX EBP EBX } }
22 float-regs x86-float-regs 2array
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 ;
30 M: x86.32 immediate-comparand? ( obj -- ? ) drop t ;
32 M:: x86.32 %load-vector ( dst val rep -- )
33 dst 0 [] rep copy-memory* val rc-absolute rel-binary-literal ;
35 M: x86.32 %mov-vm-ptr ( reg -- )
36 0 MOV 0 rc-absolute-cell rel-vm ;
38 M: x86.32 %vm-field ( dst field -- )
39 [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
41 M: x86.32 %set-vm-field ( dst field -- )
42 [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
44 M: x86.32 %vm-field-ptr ( dst field -- )
45 [ 0 MOV ] dip rc-absolute-cell rel-vm ;
48 drop HEX: ffffffff [+] card-mark <byte> MOV
50 rc-absolute-cell rel-cards-offset
54 drop HEX: ffffffff [+] card-mark <byte> MOV
56 rc-absolute-cell rel-decks-offset
59 M: x86.32 pic-tail-reg EDX ;
61 M: x86.32 reserved-stack-space 0 ;
63 M: x86.32 vm-stack-space 16 ;
65 : save-vm-ptr ( n -- )
66 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
68 M: x86.32 return-struct-in-registers? ( c-type -- ? )
70 [ return-in-registers?>> ]
71 [ heap-size { 1 2 4 8 } member? ] bi
72 os { linux netbsd solaris } member? not
75 ! On x86, parameters are usually never passed in registers,
76 ! except with Microsoft's "thiscall" and "fastcall" abis
79 { thiscall [ { { int-regs { ECX } } { float-regs { } } } ] }
80 { fastcall [ { { int-regs { ECX EDX } } { float-regs { } } } ] }
81 [ drop { { int-regs { } } { float-regs { } } } ]
84 ! Need a fake return-reg for floats
87 { int-regs { EAX EDX } }
88 { float-regs { ST0 } }
91 M: x86.32 %prologue ( n -- )
93 0 PUSH rc-absolute-cell rel-this
94 3 cells - decr-stack-reg ;
96 M: x86.32 %prepare-jump
97 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
99 M: x86.32 %load-stack-param ( dst rep n -- )
100 next-stack@ swap pick register? [ %copy ] [
102 { int-rep [ [ EAX ] dip MOV ?spill-slot EAX MOV ] }
103 { float-rep [ FLDS ?spill-slot FSTPS ] }
104 { double-rep [ FLDL ?spill-slot FSTPL ] }
108 M: x86.32 %store-stack-param ( src rep n -- )
109 stack@ swap pick register? [ [ swap ] dip %copy ] [
111 { int-rep [ [ [ EAX ] dip ?spill-slot MOV ] [ EAX MOV ] bi* ] }
112 { float-rep [ [ ?spill-slot FLDS ] [ FSTPS ] bi* ] }
113 { double-rep [ [ ?spill-slot FLDL ] [ FSTPL ] bi* ] }
117 :: load-float-return ( dst x87-insn rep -- )
120 ESP [] x87-insn execute
124 dst ?spill-slot x87-insn execute
127 M: x86.32 %load-reg-param ( vreg rep reg -- )
129 { int-rep [ int-rep %copy ] }
130 { float-rep [ drop \ FSTPS float-rep load-float-return ] }
131 { double-rep [ drop \ FSTPL double-rep load-float-return ] }
134 :: store-float-return ( src x87-insn rep -- )
138 ESP [] x87-insn execute
141 src ?spill-slot x87-insn execute
144 M: x86.32 %store-reg-param ( vreg rep reg -- )
146 { int-rep [ swap int-rep %copy ] }
147 { float-rep [ drop \ FLDS float-rep store-float-return ] }
148 { double-rep [ drop \ FLDL double-rep store-float-return ] }
151 M: x86.32 %discard-reg-param ( rep reg -- )
154 { float-rep [ ST0 FSTP ] }
155 { double-rep [ ST0 FSTP ] }
158 :: call-unbox-func ( src func -- )
159 EAX src tagged-rep %copy
164 M:: x86.32 %unbox ( dst src func rep -- )
165 src func call-unbox-func
166 dst rep %load-return ;
168 M:: x86.32 %unbox-long-long ( dst1 dst2 src func -- )
169 src int-rep 0 %store-stack-param
172 dst1 EAX int-rep %copy
173 dst2 EDX int-rep %copy ;
175 M:: x86.32 %box ( dst src func rep gc-map -- )
176 src rep 0 %store-stack-param
177 rep rep-size save-vm-ptr
178 func f gc-map %c-invoke
179 dst EAX tagged-rep %copy ;
181 M:: x86.32 %box-long-long ( dst src1 src2 func gc-map -- )
182 src1 int-rep 0 %store-stack-param
183 src2 int-rep 4 %store-stack-param
185 func f gc-map %c-invoke
186 dst EAX tagged-rep %copy ;
189 [ 0 CALL rc-relative rel-dlsym ] dip gc-map-here ;
191 M: x86.32 %begin-callback ( -- )
194 "begin_callback" f f %c-invoke ;
196 M: x86.32 %end-callback ( -- )
198 "end_callback" f f %c-invoke ;
200 M:: x86.32 %unary-float-function ( dst src func -- )
201 src double-rep 0 %store-stack-param
202 func "libm" load-library f %c-invoke
203 dst double-rep %load-return ;
205 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
206 src1 double-rep 0 %store-stack-param
207 src2 double-rep 8 %store-stack-param
208 func "libm" load-library f %c-invoke
209 dst double-rep %load-return ;
211 : funny-large-struct-return? ( return abi -- ? )
212 #! MINGW ABI incompatibility disaster
213 [ large-struct? ] [ mingw eq? os windows? not or ] bi* and ;
215 M: x86.32 %prepare-var-args ( -- ) ;
217 M:: x86.32 stack-cleanup ( stack-size return abi -- n )
218 #! a) Functions which are stdcall/fastcall/thiscall have to
219 #! clean up the caller's stack frame.
220 #! b) Functions returning large structs on MINGW have to
223 { [ abi callee-cleanup? ] [ stack-size ] }
224 { [ return abi funny-large-struct-return? ] [ 4 ] }
228 M: x86.32 %cleanup ( n -- )
229 [ ESP swap SUB ] unless-zero ;
231 M: x86.32 dummy-stack-params? f ;
233 M: x86.32 dummy-int-params? f ;
235 M: x86.32 dummy-fp-params? f ;
237 M: x86.32 long-long-on-stack? t ;
239 M: x86.32 float-on-stack? t ;
241 M: x86.32 flatten-struct-type
242 call-next-method [ first t 2array ] map ;
244 M: x86.32 struct-return-on-stack? os linux? not ;