1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces make math math.order math.parser sequences
4 accessors kernel layouts assocs words summary arrays combinators
5 classes.algebra sets continuations.private fry cpu.architecture
6 classes classes.struct locals slots parser generic.parser
7 strings quotations hashtables
10 compiler.cfg.linearization
11 compiler.cfg.instructions
12 compiler.cfg.comparisons
13 compiler.cfg.stack-frame
14 compiler.cfg.registers
16 compiler.codegen.fixup
18 FROM: namespaces => set ;
23 H{ } clone insn-counts set-global
25 GENERIC: generate-insn ( insn -- )
30 : lookup-label ( bb -- label )
31 labels get [ drop <label> ] cache ;
33 : useless-branch? ( bb successor -- ? )
34 ! If our successor immediately follows us in linearization
35 ! order then we don't need to branch.
36 [ block-number ] bi@ 1 - = ; inline
38 : emit-branch ( bb successor -- )
40 [ 2drop ] [ nip lookup-label %jump-label ] if ;
42 M: ##branch generate-insn
43 drop basic-block get dup successors>> first emit-branch ;
45 GENERIC: generate-conditional-insn ( label insn -- )
47 GENERIC: negate-insn-cc ( insn -- )
49 M: conditional-branch-insn negate-insn-cc
50 [ negate-cc ] change-cc drop ;
52 M: ##test-vector-branch negate-insn-cc
53 [ negate-vcc ] change-vcc drop ;
55 M:: conditional-branch-insn generate-insn ( insn -- )
57 bb successors>> first2 :> ( first second )
58 bb second useless-branch?
60 [ bb first second insn negate-insn-cc ] if
61 lookup-label insn generate-conditional-insn
64 : %dispatch-label ( label -- )
66 rc-absolute-cell label-fixup ;
68 M: ##dispatch generate-insn
69 [ src>> ] [ temp>> ] bi %dispatch
70 basic-block get successors>>
71 [ lookup-label %dispatch-label ] each ;
73 : generate-block ( bb -- )
75 [ lookup-label resolve-label ]
78 [ class insn-counts get inc-at ]
84 : generate ( cfg -- code )
88 [ number-blocks ] [ [ generate-block ] each ] bi
92 M: ##no-tco generate-insn drop ;
94 M: ##stack-frame generate-insn drop ;
96 M: ##prologue generate-insn
99 [ [ stack-frame set ] [ total-size>> %prologue ] bi ] when* ;
101 M: ##epilogue generate-insn
103 cfg get stack-frame>> [ total-size>> %epilogue ] when* ;
105 ! Some meta-programming to generate simple code generators, where
106 ! the instruction is unpacked and then a %word is called
109 : insn-slot-quot ( spec -- quot )
110 name>> reader-word 1quotation ;
112 : codegen-method-body ( class word -- quot )
114 "insn-slots" word-prop
115 [ insn-slot-quot ] map cleave>quot
119 scan-word [ \ generate-insn create-method-in ] keep scan-word
120 codegen-method-body define ;
124 CODEGEN: ##load-integer %load-immediate
125 CODEGEN: ##load-tagged %load-immediate
126 CODEGEN: ##load-reference %load-reference
127 CODEGEN: ##load-float %load-float
128 CODEGEN: ##load-double %load-double
129 CODEGEN: ##load-vector %load-vector
130 CODEGEN: ##peek %peek
131 CODEGEN: ##replace %replace
132 CODEGEN: ##replace-imm %replace-imm
133 CODEGEN: ##inc-d %inc-d
134 CODEGEN: ##inc-r %inc-r
135 CODEGEN: ##call %call
136 CODEGEN: ##jump %jump
137 CODEGEN: ##return %return
138 CODEGEN: ##slot %slot
139 CODEGEN: ##slot-imm %slot-imm
140 CODEGEN: ##set-slot %set-slot
141 CODEGEN: ##set-slot-imm %set-slot-imm
143 CODEGEN: ##add-imm %add-imm
145 CODEGEN: ##sub-imm %sub-imm
147 CODEGEN: ##mul-imm %mul-imm
149 CODEGEN: ##and-imm %and-imm
151 CODEGEN: ##or-imm %or-imm
153 CODEGEN: ##xor-imm %xor-imm
155 CODEGEN: ##shl-imm %shl-imm
157 CODEGEN: ##shr-imm %shr-imm
159 CODEGEN: ##sar-imm %sar-imm
164 CODEGEN: ##log2 %log2
165 CODEGEN: ##bit-count %bit-count
166 CODEGEN: ##copy %copy
167 CODEGEN: ##tagged>integer %tagged>integer
168 CODEGEN: ##add-float %add-float
169 CODEGEN: ##sub-float %sub-float
170 CODEGEN: ##mul-float %mul-float
171 CODEGEN: ##div-float %div-float
172 CODEGEN: ##min-float %min-float
173 CODEGEN: ##max-float %max-float
174 CODEGEN: ##sqrt %sqrt
175 CODEGEN: ##unary-float-function %unary-float-function
176 CODEGEN: ##binary-float-function %binary-float-function
177 CODEGEN: ##single>double-float %single>double-float
178 CODEGEN: ##double>single-float %double>single-float
179 CODEGEN: ##integer>float %integer>float
180 CODEGEN: ##float>integer %float>integer
181 CODEGEN: ##zero-vector %zero-vector
182 CODEGEN: ##fill-vector %fill-vector
183 CODEGEN: ##gather-vector-2 %gather-vector-2
184 CODEGEN: ##gather-vector-4 %gather-vector-4
185 CODEGEN: ##gather-int-vector-2 %gather-int-vector-2
186 CODEGEN: ##gather-int-vector-4 %gather-int-vector-4
187 CODEGEN: ##select-vector %select-vector
188 CODEGEN: ##shuffle-vector-imm %shuffle-vector-imm
189 CODEGEN: ##shuffle-vector-halves-imm %shuffle-vector-halves-imm
190 CODEGEN: ##shuffle-vector %shuffle-vector
191 CODEGEN: ##tail>head-vector %tail>head-vector
192 CODEGEN: ##merge-vector-head %merge-vector-head
193 CODEGEN: ##merge-vector-tail %merge-vector-tail
194 CODEGEN: ##float-pack-vector %float-pack-vector
195 CODEGEN: ##signed-pack-vector %signed-pack-vector
196 CODEGEN: ##unsigned-pack-vector %unsigned-pack-vector
197 CODEGEN: ##unpack-vector-head %unpack-vector-head
198 CODEGEN: ##unpack-vector-tail %unpack-vector-tail
199 CODEGEN: ##integer>float-vector %integer>float-vector
200 CODEGEN: ##float>integer-vector %float>integer-vector
201 CODEGEN: ##compare-vector %compare-vector
202 CODEGEN: ##test-vector %test-vector
203 CODEGEN: ##add-vector %add-vector
204 CODEGEN: ##saturated-add-vector %saturated-add-vector
205 CODEGEN: ##add-sub-vector %add-sub-vector
206 CODEGEN: ##sub-vector %sub-vector
207 CODEGEN: ##saturated-sub-vector %saturated-sub-vector
208 CODEGEN: ##mul-vector %mul-vector
209 CODEGEN: ##mul-high-vector %mul-high-vector
210 CODEGEN: ##mul-horizontal-add-vector %mul-horizontal-add-vector
211 CODEGEN: ##saturated-mul-vector %saturated-mul-vector
212 CODEGEN: ##div-vector %div-vector
213 CODEGEN: ##min-vector %min-vector
214 CODEGEN: ##max-vector %max-vector
215 CODEGEN: ##avg-vector %avg-vector
216 CODEGEN: ##dot-vector %dot-vector
217 CODEGEN: ##sad-vector %sad-vector
218 CODEGEN: ##sqrt-vector %sqrt-vector
219 CODEGEN: ##horizontal-add-vector %horizontal-add-vector
220 CODEGEN: ##horizontal-sub-vector %horizontal-sub-vector
221 CODEGEN: ##horizontal-shl-vector-imm %horizontal-shl-vector-imm
222 CODEGEN: ##horizontal-shr-vector-imm %horizontal-shr-vector-imm
223 CODEGEN: ##abs-vector %abs-vector
224 CODEGEN: ##and-vector %and-vector
225 CODEGEN: ##andn-vector %andn-vector
226 CODEGEN: ##or-vector %or-vector
227 CODEGEN: ##xor-vector %xor-vector
228 CODEGEN: ##not-vector %not-vector
229 CODEGEN: ##shl-vector-imm %shl-vector-imm
230 CODEGEN: ##shr-vector-imm %shr-vector-imm
231 CODEGEN: ##shl-vector %shl-vector
232 CODEGEN: ##shr-vector %shr-vector
233 CODEGEN: ##integer>scalar %integer>scalar
234 CODEGEN: ##scalar>integer %scalar>integer
235 CODEGEN: ##vector>scalar %vector>scalar
236 CODEGEN: ##scalar>vector %scalar>vector
237 CODEGEN: ##box-alien %box-alien
238 CODEGEN: ##box-displaced-alien %box-displaced-alien
239 CODEGEN: ##unbox-alien %unbox-alien
240 CODEGEN: ##unbox-any-c-ptr %unbox-any-c-ptr
241 CODEGEN: ##load-memory %load-memory
242 CODEGEN: ##load-memory-imm %load-memory-imm
243 CODEGEN: ##store-memory %store-memory
244 CODEGEN: ##store-memory-imm %store-memory-imm
245 CODEGEN: ##allot %allot
246 CODEGEN: ##write-barrier %write-barrier
247 CODEGEN: ##write-barrier-imm %write-barrier-imm
248 CODEGEN: ##compare %compare
249 CODEGEN: ##compare-imm %compare-imm
250 CODEGEN: ##test %test
251 CODEGEN: ##test-imm %test-imm
252 CODEGEN: ##compare-integer %compare
253 CODEGEN: ##compare-integer-imm %compare-integer-imm
254 CODEGEN: ##compare-float-ordered %compare-float-ordered
255 CODEGEN: ##compare-float-unordered %compare-float-unordered
256 CODEGEN: ##save-context %save-context
257 CODEGEN: ##restore-context %restore-context
258 CODEGEN: ##vm-field %vm-field
259 CODEGEN: ##set-vm-field %set-vm-field
260 CODEGEN: ##alien-global %alien-global
261 CODEGEN: ##call-gc %call-gc
262 CODEGEN: ##spill %spill
263 CODEGEN: ##reload %reload
265 ! Conditional branches
269 scan-word [ \ generate-conditional-insn create-method-in ] keep scan-word
270 codegen-method-body define ;
274 CONDITIONAL: ##compare-branch %compare-branch
275 CONDITIONAL: ##compare-imm-branch %compare-imm-branch
276 CONDITIONAL: ##compare-integer-branch %compare-branch
277 CONDITIONAL: ##compare-integer-imm-branch %compare-integer-imm-branch
278 CONDITIONAL: ##test-branch %test-branch
279 CONDITIONAL: ##test-imm-branch %test-imm-branch
280 CONDITIONAL: ##compare-float-ordered-branch %compare-float-ordered-branch
281 CONDITIONAL: ##compare-float-unordered-branch %compare-float-unordered-branch
282 CONDITIONAL: ##test-vector-branch %test-vector-branch
283 CONDITIONAL: ##check-nursery-branch %check-nursery-branch
284 CONDITIONAL: ##fixnum-add %fixnum-add
285 CONDITIONAL: ##fixnum-sub %fixnum-sub
286 CONDITIONAL: ##fixnum-mul %fixnum-mul
289 CODEGEN: ##unbox %unbox
290 CODEGEN: ##unbox-long-long %unbox-long-long
291 CODEGEN: ##store-reg-param %store-reg-param
292 CODEGEN: ##store-stack-param %store-stack-param
293 CODEGEN: ##load-reg-param %load-reg-param
294 CODEGEN: ##load-stack-param %load-stack-param
295 CODEGEN: ##local-allot %local-allot
297 CODEGEN: ##box-long-long %box-long-long
298 CODEGEN: ##allot-byte-array %allot-byte-array
299 CODEGEN: ##prepare-var-args %prepare-var-args
300 CODEGEN: ##alien-invoke %alien-invoke
301 CODEGEN: ##cleanup %cleanup
302 CODEGEN: ##alien-indirect %alien-indirect
303 CODEGEN: ##begin-callback %begin-callback
304 CODEGEN: ##alien-callback %alien-callback
305 CODEGEN: ##end-callback %end-callback
307 M: ##alien-assembly generate-insn quot>> call( -- ) ;