]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/instructions/instructions.factor
db configurations factored out through db.info
[factor.git] / basis / compiler / cfg / instructions / instructions.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs accessors arrays kernel sequences namespaces words
4 math math.order layouts classes.algebra alien byte-arrays
5 compiler.constants combinators compiler.cfg.registers
6 compiler.cfg.instructions.syntax ;
7 IN: compiler.cfg.instructions
8
9 : new-insn ( ... class -- insn ) [ f f ] dip boa ; inline
10
11 ! Virtual CPU instructions, used by CFG and machine IRs
12 TUPLE: insn ;
13
14 ! Instruction with no side effects; if 'out' is never read, we
15 ! can eliminate it.
16 TUPLE: ##flushable < insn { dst vreg } ;
17
18 ! Instruction which is referentially transparent; we can replace
19 ! repeated computation with a reference to a previous value
20 TUPLE: ##pure < ##flushable ;
21
22 TUPLE: ##unary < ##pure { src vreg } ;
23 TUPLE: ##unary/temp < ##unary { temp vreg } ;
24 TUPLE: ##binary < ##pure { src1 vreg } { src2 vreg } ;
25 TUPLE: ##binary-imm < ##pure { src1 vreg } { src2 integer } ;
26 TUPLE: ##commutative < ##binary ;
27 TUPLE: ##commutative-imm < ##binary-imm ;
28
29 ! Instruction only used for its side effect, produces no values
30 TUPLE: ##effect < insn { src vreg } ;
31
32 ! Read/write ops: candidates for alias analysis
33 TUPLE: ##read < ##flushable ;
34 TUPLE: ##write < ##effect ;
35
36 TUPLE: ##alien-getter < ##flushable { src vreg } ;
37 TUPLE: ##alien-setter < ##effect { value vreg } ;
38
39 ! Stack operations
40 INSN: ##load-immediate < ##pure { val integer } ;
41 INSN: ##load-reference < ##pure obj ;
42
43 GENERIC: ##load-literal ( dst value -- )
44
45 M: fixnum ##load-literal tag-fixnum ##load-immediate ;
46 M: f ##load-literal drop \ f tag-number ##load-immediate ;
47 M: object ##load-literal ##load-reference ;
48
49 INSN: ##peek < ##flushable { loc loc } ;
50 INSN: ##replace < ##effect { loc loc } ;
51 INSN: ##inc-d { n integer } ;
52 INSN: ##inc-r { n integer } ;
53
54 ! Subroutine calls
55 INSN: ##stack-frame stack-frame ;
56 INSN: ##call word ;
57 INSN: ##jump word ;
58 INSN: ##return ;
59
60 ! Dummy instruction that simply inhibits TCO
61 INSN: ##no-tco ;
62
63 ! Jump tables
64 INSN: ##dispatch src temp ;
65
66 ! Slot access
67 INSN: ##slot < ##read { obj vreg } { slot vreg } { tag integer } { temp vreg } ;
68 INSN: ##slot-imm < ##read { obj vreg } { slot integer } { tag integer } ;
69 INSN: ##set-slot < ##write { obj vreg } { slot vreg } { tag integer } { temp vreg } ;
70 INSN: ##set-slot-imm < ##write { obj vreg } { slot integer } { tag integer } ;
71
72 ! String element access
73 INSN: ##string-nth < ##flushable { obj vreg } { index vreg } { temp vreg } ;
74 INSN: ##set-string-nth-fast < ##effect { obj vreg } { index vreg } { temp vreg } ;
75
76 ! Integer arithmetic
77 INSN: ##add < ##commutative ;
78 INSN: ##add-imm < ##commutative-imm ;
79 INSN: ##sub < ##binary ;
80 INSN: ##sub-imm < ##binary-imm ;
81 INSN: ##mul < ##commutative ;
82 INSN: ##mul-imm < ##commutative-imm ;
83 INSN: ##and < ##commutative ;
84 INSN: ##and-imm < ##commutative-imm ;
85 INSN: ##or < ##commutative ;
86 INSN: ##or-imm < ##commutative-imm ;
87 INSN: ##xor < ##commutative ;
88 INSN: ##xor-imm < ##commutative-imm ;
89 INSN: ##shl < ##binary ;
90 INSN: ##shl-imm < ##binary-imm ;
91 INSN: ##shr < ##binary ;
92 INSN: ##shr-imm < ##binary-imm ;
93 INSN: ##sar < ##binary ;
94 INSN: ##sar-imm < ##binary-imm ;
95 INSN: ##not < ##unary ;
96 INSN: ##log2 < ##unary ;
97
98 : ##tag-fixnum ( dst src -- ) tag-bits get ##shl-imm ; inline
99 : ##untag-fixnum ( dst src -- ) tag-bits get ##sar-imm ; inline
100
101 ! Bignum/integer conversion
102 INSN: ##integer>bignum < ##unary/temp ;
103 INSN: ##bignum>integer < ##unary/temp ;
104
105 ! Float arithmetic
106 INSN: ##add-float < ##commutative ;
107 INSN: ##sub-float < ##binary ;
108 INSN: ##mul-float < ##commutative ;
109 INSN: ##div-float < ##binary ;
110
111 ! Float/integer conversion
112 INSN: ##float>integer < ##unary ;
113 INSN: ##integer>float < ##unary ;
114
115 ! Boxing and unboxing
116 INSN: ##copy < ##unary ;
117 INSN: ##copy-float < ##unary ;
118 INSN: ##unbox-float < ##unary ;
119 INSN: ##unbox-any-c-ptr < ##unary/temp ;
120 INSN: ##box-float < ##unary/temp ;
121 INSN: ##box-alien < ##unary/temp ;
122
123 : ##unbox-f ( dst src -- ) drop 0 ##load-immediate ;
124 : ##unbox-byte-array ( dst src -- ) byte-array-offset ##add-imm ;
125 : ##unbox-alien ( dst src -- ) 3 object tag-number ##slot-imm ;
126
127 : ##unbox-c-ptr ( dst src class temp -- )
128     {
129         { [ over \ f class<= ] [ 2drop ##unbox-f ] }
130         { [ over simple-alien class<= ] [ 2drop ##unbox-alien ] }
131         { [ over byte-array class<= ] [ 2drop ##unbox-byte-array ] }
132         [ nip ##unbox-any-c-ptr ]
133     } cond ;
134
135 ! Alien accessors
136 INSN: ##alien-unsigned-1 < ##alien-getter ;
137 INSN: ##alien-unsigned-2 < ##alien-getter ;
138 INSN: ##alien-unsigned-4 < ##alien-getter ;
139 INSN: ##alien-signed-1 < ##alien-getter ;
140 INSN: ##alien-signed-2 < ##alien-getter ;
141 INSN: ##alien-signed-4 < ##alien-getter ;
142 INSN: ##alien-cell < ##alien-getter ;
143 INSN: ##alien-float < ##alien-getter ;
144 INSN: ##alien-double < ##alien-getter ;
145
146 INSN: ##set-alien-integer-1 < ##alien-setter ;
147 INSN: ##set-alien-integer-2 < ##alien-setter ;
148 INSN: ##set-alien-integer-4 < ##alien-setter ;
149 INSN: ##set-alien-cell < ##alien-setter ;
150 INSN: ##set-alien-float < ##alien-setter ;
151 INSN: ##set-alien-double < ##alien-setter ;
152
153 ! Memory allocation
154 INSN: ##allot < ##flushable size class { temp vreg } ;
155
156 UNION: ##allocation ##allot ##box-float ##box-alien ##integer>bignum ;
157
158 INSN: ##write-barrier < ##effect card# table ;
159
160 INSN: ##alien-global < ##flushable symbol library ;
161
162 ! FFI
163 INSN: ##alien-invoke params ;
164 INSN: ##alien-indirect params ;
165 INSN: ##alien-callback params ;
166 INSN: ##callback-return params ;
167
168 ! Instructions used by CFG IR only.
169 INSN: ##prologue ;
170 INSN: ##epilogue ;
171
172 INSN: ##branch ;
173
174 INSN: ##loop-entry ;
175
176 INSN: ##phi < ##pure inputs ;
177
178 ! Conditionals
179 TUPLE: ##conditional-branch < insn { src1 vreg } { src2 vreg } cc ;
180
181 INSN: ##compare-branch < ##conditional-branch ;
182 INSN: ##compare-imm-branch { src1 vreg } { src2 integer } cc ;
183
184 INSN: ##compare < ##binary cc temp ;
185 INSN: ##compare-imm < ##binary-imm cc temp ;
186
187 INSN: ##compare-float-branch < ##conditional-branch ;
188 INSN: ##compare-float < ##binary cc temp ;
189
190 ! Overflowing arithmetic
191 TUPLE: ##fixnum-overflow < insn { dst vreg } { src1 vreg } { src2 vreg } ;
192 INSN: ##fixnum-add < ##fixnum-overflow ;
193 INSN: ##fixnum-sub < ##fixnum-overflow ;
194 INSN: ##fixnum-mul < ##fixnum-overflow ;
195
196 INSN: ##gc { temp1 vreg } { temp2 vreg } live-values ;
197
198 ! Instructions used by machine IR only.
199 INSN: _prologue stack-frame ;
200 INSN: _epilogue stack-frame ;
201
202 INSN: _label id ;
203
204 INSN: _branch label ;
205
206 INSN: _dispatch src temp ;
207 INSN: _dispatch-label label ;
208
209 TUPLE: _conditional-branch < insn label { src1 vreg } { src2 vreg } cc ;
210
211 INSN: _compare-branch < _conditional-branch ;
212 INSN: _compare-imm-branch label { src1 vreg } { src2 integer } cc ;
213
214 INSN: _compare-float-branch < _conditional-branch ;
215
216 ! Overflowing arithmetic
217 TUPLE: _fixnum-overflow < insn label { dst vreg } { src1 vreg } { src2 vreg } ;
218 INSN: _fixnum-add < _fixnum-overflow ;
219 INSN: _fixnum-sub < _fixnum-overflow ;
220 INSN: _fixnum-mul < _fixnum-overflow ;
221
222 TUPLE: spill-slot n ; C: <spill-slot> spill-slot
223
224 INSN: _gc { temp1 vreg } { temp2 vreg } gc-roots gc-root-count gc-root-size ;
225
226 ! These instructions operate on machine registers and not
227 ! virtual registers
228 INSN: _spill src class n ;
229 INSN: _reload dst class n ;
230 INSN: _copy dst src class ;
231 INSN: _spill-counts counts ;
232
233 ! Instructions that poison the stack state
234 UNION: poison-insn
235     ##jump
236     ##return
237     ##callback-return ;
238
239 ! Instructions that kill all live vregs
240 UNION: kill-vreg-insn
241     poison-insn
242     ##stack-frame
243     ##call
244     ##prologue
245     ##epilogue
246     ##alien-invoke
247     ##alien-indirect
248     ##alien-callback ;