]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/architecture/architecture.factor
Fix permission bits
[factor.git] / basis / cpu / x86 / architecture / architecture.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types arrays cpu.x86.assembler
4 cpu.x86.assembler.private cpu.architecture kernel kernel.private
5 math memory namespaces make sequences words compiler.generator
6 compiler.generator.registers compiler.generator.fixup system
7 layouts combinators compiler.constants math.order ;
8 IN: cpu.x86.architecture
9
10 HOOK: ds-reg cpu ( -- reg )
11 HOOK: rs-reg cpu ( -- reg )
12 HOOK: stack-reg cpu ( -- reg )
13 HOOK: stack-save-reg cpu ( -- reg )
14
15 : stack@ ( n -- op ) stack-reg swap [+] ;
16
17 : reg-stack ( n reg -- op ) swap cells neg [+] ;
18
19 M: ds-loc v>operand n>> ds-reg reg-stack ;
20 M: rs-loc v>operand n>> rs-reg reg-stack ;
21
22 M: int-regs %save-param-reg drop >r stack@ r> MOV ;
23 M: int-regs %load-param-reg drop swap stack@ MOV ;
24
25 GENERIC: MOVSS/D ( dst src reg-class -- )
26
27 M: single-float-regs MOVSS/D drop MOVSS ;
28
29 M: double-float-regs MOVSS/D drop MOVSD ;
30
31 M: float-regs %save-param-reg >r >r stack@ r> r> MOVSS/D ;
32 M: float-regs %load-param-reg >r swap stack@ r> MOVSS/D ;
33
34 GENERIC: push-return-reg ( reg-class -- )
35 GENERIC: load-return-reg ( stack@ reg-class -- )
36 GENERIC: store-return-reg ( stack@ reg-class -- )
37
38 ! Only used by inline allocation
39 HOOK: temp-reg-1 cpu ( -- reg )
40 HOOK: temp-reg-2 cpu ( -- reg )
41
42 HOOK: fixnum>slot@ cpu ( op -- )
43
44 HOOK: prepare-division cpu ( -- )
45
46 M: immediate load-literal v>operand swap v>operand MOV ;
47
48 M: x86 stack-frame ( n -- i )
49     3 cells + 16 align cell - ;
50
51 M: x86 %save-word-xt ( -- )
52     temp-reg v>operand 0 MOV rc-absolute-cell rel-this ;
53
54 : factor-area-size ( -- n ) 4 cells ;
55
56 M: x86 %prologue ( n -- )
57     dup cell + PUSH
58     temp-reg v>operand PUSH
59     stack-reg swap 2 cells - SUB ;
60
61 M: x86 %epilogue ( n -- )
62     stack-reg swap ADD ;
63
64 HOOK: %alien-global cpu ( symbol dll register -- )
65
66 M: x86 %prepare-alien-invoke
67     #! Save Factor stack pointers in case the C code calls a
68     #! callback which does a GC, which must reliably trace
69     #! all roots.
70     "stack_chain" f temp-reg v>operand %alien-global
71     temp-reg v>operand [] stack-reg MOV
72     temp-reg v>operand [] cell SUB
73     temp-reg v>operand 2 cells [+] ds-reg MOV
74     temp-reg v>operand 3 cells [+] rs-reg MOV ;
75
76 M: x86 %call ( label -- ) CALL ;
77
78 M: x86 %jump-label ( label -- ) JMP ;
79
80 M: x86 %jump-f ( label -- )
81     "flag" operand f v>operand CMP JE ;
82
83 : code-alignment ( -- n )
84     building get length dup cell align swap - ;
85
86 : align-code ( n -- )
87     0 <repetition> % ;
88
89 M: x86 %dispatch ( -- )
90     [
91         %epilogue-later
92         ! Load jump table base. We use a temporary register
93         ! since on AMD64 we have to load a 64-bit immediate. On
94         ! x86, this is redundant.
95         ! Untag and multiply to get a jump table offset
96         "n" operand fixnum>slot@
97         ! Add jump table base
98         "offset" operand HEX: ffffffff MOV rc-absolute-cell rel-here
99         "n" operand "offset" operand ADD
100         "n" operand HEX: 7f [+] JMP
101         ! Fix up the displacement above
102         code-alignment dup bootstrap-cell 8 = 15 9 ? +
103         building get dup pop* push
104         align-code
105     ] H{
106         { +input+ { { f "n" } } }
107         { +scratch+ { { f "offset" } } }
108         { +clobber+ { "n" } }
109     } with-template ;
110
111 M: x86 %dispatch-label ( word -- )
112     0 cell, rc-absolute-cell rel-word ;
113
114 M: x86 %unbox-float ( dst src -- )
115     [ v>operand ] bi@ float-offset [+] MOVSD ;
116
117 M: x86 %peek [ v>operand ] bi@ MOV ;
118
119 M: x86 %replace swap %peek ;
120
121 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ;
122
123 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
124
125 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
126
127 M: x86 fp-shadows-int? ( -- ? ) f ;
128
129 M: x86 value-structs? t ;
130
131 M: x86 small-enough? ( n -- ? )
132     HEX: -80000000 HEX: 7fffffff between? ;
133
134 : %untag ( reg -- ) tag-mask get bitnot AND ;
135
136 : %untag-fixnum ( reg -- ) tag-bits get SAR ;
137
138 : %tag-fixnum ( reg -- ) tag-bits get SHL ;
139
140 : temp@ ( n -- op ) stack-reg \ stack-frame get rot - [+] ;
141
142 M: x86 %return ( -- ) 0 %unwind ;
143
144 ! Alien intrinsics
145 M: x86 %unbox-byte-array ( dst src -- )
146     [ v>operand ] bi@ byte-array-offset [+] LEA ;
147
148 M: x86 %unbox-alien ( dst src -- )
149     [ v>operand ] bi@ alien-offset [+] MOV ;
150
151 M: x86 %unbox-f ( dst src -- )
152     drop v>operand 0 MOV ;
153
154 M: x86 %unbox-any-c-ptr ( dst src -- )
155     { "is-byte-array" "end" "start" } [ define-label ] each
156     ! Address is computed in ds-reg
157     ds-reg PUSH
158     ds-reg 0 MOV
159     ! Object is stored in ds-reg
160     rs-reg PUSH
161     rs-reg swap v>operand MOV
162     ! We come back here with displaced aliens
163     "start" resolve-label
164     ! Is the object f?
165     rs-reg f v>operand CMP
166     "end" get JE
167     ! Is the object an alien?
168     rs-reg header-offset [+] alien type-number tag-fixnum CMP
169     "is-byte-array" get JNE
170     ! If so, load the offset and add it to the address
171     ds-reg rs-reg alien-offset [+] ADD
172     ! Now recurse on the underlying alien
173     rs-reg rs-reg underlying-alien-offset [+] MOV
174     "start" get JMP
175     "is-byte-array" resolve-label
176     ! Add byte array address to address being computed
177     ds-reg rs-reg ADD
178     ! Add an offset to start of byte array's data
179     ds-reg byte-array-offset ADD
180     "end" resolve-label
181     ! Done, store address in destination register
182     v>operand ds-reg MOV
183     ! Restore rs-reg
184     rs-reg POP
185     ! Restore ds-reg
186     ds-reg POP ;