]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/codegen/codegen.factor
Fixing failing unit tests in compiler.tree.propagation due to constraints
[factor.git] / basis / compiler / codegen / codegen.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces make math math.order math.parser sequences accessors
4 kernel kernel.private layouts assocs words summary arrays
5 combinators classes.algebra alien alien.c-types alien.structs
6 alien.strings alien.arrays alien.complex alien.libraries sets libc
7 continuations.private fry cpu.architecture classes locals
8 source-files.errors slots parser generic.parser
9 compiler.errors
10 compiler.alien
11 compiler.constants
12 compiler.cfg
13 compiler.cfg.instructions
14 compiler.cfg.stack-frame
15 compiler.cfg.registers
16 compiler.cfg.builder
17 compiler.codegen.fixup
18 compiler.utilities ;
19 IN: compiler.codegen
20
21 SYMBOL: insn-counts
22
23 H{ } clone insn-counts set-global
24
25 GENERIC: generate-insn ( insn -- )
26
27 TUPLE: asm label code calls ;
28
29 SYMBOL: calls
30
31 : add-call ( word -- )
32     #! Compile this word later.
33     calls get push ;
34
35 SYMBOL: compiling-word
36
37 : compiled-stack-traces? ( -- ? ) 67 getenv ;
38
39 ! Mapping _label IDs to label instances
40 SYMBOL: labels
41
42 : init-generator ( word -- )
43     H{ } clone labels set
44     V{ } clone calls set
45     compiling-word set
46     compiled-stack-traces? [ compiling-word get add-literal ] when ;
47
48 : generate-insns ( asm -- code )
49     [
50         [ word>> init-generator ]
51         [
52             instructions>>
53             [
54                 [ class insn-counts get inc-at ]
55                 [ generate-insn ]
56                 bi
57             ] each
58         ] bi
59     ] with-fixup ;
60
61 : generate ( mr -- asm )
62     [
63         [ label>> ] [ generate-insns ] bi calls get
64         asm boa
65     ] with-scope ;
66
67 : lookup-label ( id -- label )
68     labels get [ drop <label> ] cache ;
69
70 ! Special cases
71 M: ##no-tco generate-insn drop ;
72
73 M: ##call generate-insn
74     word>> dup sub-primitive>>
75     [ first % ] [ [ add-call ] [ %call ] bi ] ?if ;
76
77 M: ##jump generate-insn word>> [ add-call ] [ %jump ] bi ;
78
79 M: _dispatch-label generate-insn
80     label>> lookup-label
81     cell 0 <repetition> %
82     rc-absolute-cell label-fixup ;
83
84 M: _prologue generate-insn
85     stack-frame>> [ stack-frame set ] [ total-size>> %prologue ] bi ;
86
87 M: _epilogue generate-insn
88     stack-frame>> total-size>> %epilogue ;
89
90 M: _spill-area-size generate-insn drop ;
91
92 ! Some meta-programming to generate simple code generators, where
93 ! the instruction is unpacked and then a %word is called
94 <<
95
96 : insn-slot-quot ( spec -- quot )
97     name>> [ reader-word ] [ "label" = ] bi
98     [ \ lookup-label [ ] 2sequence ] [ [ ] 1sequence ] if ;
99
100 : codegen-method-body ( class word -- quot )
101     [
102         "insn-slots" word-prop
103         [ insn-slot-quot ] map cleave>quot
104     ] dip suffix ;
105
106 SYNTAX: CODEGEN:
107     scan-word [ \ generate-insn create-method-in ] keep scan-word
108     codegen-method-body define ;
109 >>
110
111 CODEGEN: ##load-immediate %load-immediate
112 CODEGEN: ##load-reference %load-reference
113 CODEGEN: ##peek %peek
114 CODEGEN: ##replace %replace
115 CODEGEN: ##inc-d %inc-d
116 CODEGEN: ##inc-r %inc-r
117 CODEGEN: ##return %return
118 CODEGEN: ##slot %slot
119 CODEGEN: ##slot-imm %slot-imm
120 CODEGEN: ##set-slot %set-slot
121 CODEGEN: ##set-slot-imm %set-slot-imm
122 CODEGEN: ##string-nth %string-nth
123 CODEGEN: ##set-string-nth-fast %set-string-nth-fast
124 CODEGEN: ##add %add
125 CODEGEN: ##add-imm %add-imm
126 CODEGEN: ##sub %sub
127 CODEGEN: ##sub-imm %sub-imm
128 CODEGEN: ##mul %mul
129 CODEGEN: ##mul-imm %mul-imm
130 CODEGEN: ##and %and
131 CODEGEN: ##and-imm %and-imm
132 CODEGEN: ##or %or
133 CODEGEN: ##or-imm %or-imm
134 CODEGEN: ##xor %xor
135 CODEGEN: ##xor-imm %xor-imm
136 CODEGEN: ##shl %shl
137 CODEGEN: ##shl-imm %shl-imm
138 CODEGEN: ##shr %shr
139 CODEGEN: ##shr-imm %shr-imm
140 CODEGEN: ##sar %sar
141 CODEGEN: ##sar-imm %sar-imm
142 CODEGEN: ##min %min
143 CODEGEN: ##max %max
144 CODEGEN: ##not %not
145 CODEGEN: ##log2 %log2
146 CODEGEN: ##copy %copy
147 CODEGEN: ##integer>bignum %integer>bignum
148 CODEGEN: ##bignum>integer %bignum>integer
149 CODEGEN: ##unbox-float %unbox-float
150 CODEGEN: ##box-float %box-float
151 CODEGEN: ##add-float %add-float
152 CODEGEN: ##sub-float %sub-float
153 CODEGEN: ##mul-float %mul-float
154 CODEGEN: ##div-float %div-float
155 CODEGEN: ##min-float %min-float
156 CODEGEN: ##max-float %max-float
157 CODEGEN: ##sqrt %sqrt
158 CODEGEN: ##unary-float-function %unary-float-function
159 CODEGEN: ##binary-float-function %binary-float-function
160 CODEGEN: ##single>double-float %single>double-float
161 CODEGEN: ##double>single-float %double>single-float
162 CODEGEN: ##integer>float %integer>float
163 CODEGEN: ##float>integer %float>integer
164 CODEGEN: ##unbox-vector %unbox-vector
165 CODEGEN: ##broadcast-vector %broadcast-vector
166 CODEGEN: ##gather-vector-2 %gather-vector-2
167 CODEGEN: ##gather-vector-4 %gather-vector-4
168 CODEGEN: ##box-vector %box-vector
169 CODEGEN: ##add-vector %add-vector
170 CODEGEN: ##sub-vector %sub-vector
171 CODEGEN: ##mul-vector %mul-vector
172 CODEGEN: ##div-vector %div-vector
173 CODEGEN: ##min-vector %min-vector
174 CODEGEN: ##max-vector %max-vector
175 CODEGEN: ##sqrt-vector %sqrt-vector
176 CODEGEN: ##horizontal-add-vector %horizontal-add-vector
177 CODEGEN: ##box-alien %box-alien
178 CODEGEN: ##box-displaced-alien %box-displaced-alien
179 CODEGEN: ##unbox-alien %unbox-alien
180 CODEGEN: ##unbox-any-c-ptr %unbox-any-c-ptr
181 CODEGEN: ##alien-unsigned-1 %alien-unsigned-1
182 CODEGEN: ##alien-unsigned-2 %alien-unsigned-2
183 CODEGEN: ##alien-unsigned-4 %alien-unsigned-4
184 CODEGEN: ##alien-signed-1 %alien-signed-1
185 CODEGEN: ##alien-signed-2 %alien-signed-2
186 CODEGEN: ##alien-signed-4 %alien-signed-4
187 CODEGEN: ##alien-cell %alien-cell
188 CODEGEN: ##alien-float %alien-float
189 CODEGEN: ##alien-double %alien-double
190 CODEGEN: ##alien-vector %alien-vector
191 CODEGEN: ##set-alien-integer-1 %set-alien-integer-1
192 CODEGEN: ##set-alien-integer-2 %set-alien-integer-2
193 CODEGEN: ##set-alien-integer-4 %set-alien-integer-4
194 CODEGEN: ##set-alien-cell %set-alien-cell
195 CODEGEN: ##set-alien-float %set-alien-float
196 CODEGEN: ##set-alien-double %set-alien-double
197 CODEGEN: ##set-alien-vector %set-alien-vector
198 CODEGEN: ##allot %allot
199 CODEGEN: ##write-barrier %write-barrier
200 CODEGEN: ##compare %compare
201 CODEGEN: ##compare-imm %compare-imm
202 CODEGEN: ##compare-float-ordered %compare-float-ordered
203 CODEGEN: ##compare-float-unordered %compare-float-unordered
204 CODEGEN: ##save-context %save-context
205
206 CODEGEN: _fixnum-add %fixnum-add
207 CODEGEN: _fixnum-sub %fixnum-sub
208 CODEGEN: _fixnum-mul %fixnum-mul
209 CODEGEN: _label resolve-label
210 CODEGEN: _branch %jump-label
211 CODEGEN: _compare-branch %compare-branch
212 CODEGEN: _compare-imm-branch %compare-imm-branch
213 CODEGEN: _compare-float-ordered-branch %compare-float-ordered-branch
214 CODEGEN: _compare-float-unordered-branch %compare-float-unordered-branch
215 CODEGEN: _dispatch %dispatch
216 CODEGEN: _spill %spill
217 CODEGEN: _reload %reload
218
219 ! ##gc
220 : wipe-locs ( locs temp -- )
221     '[
222         _
223         [ 0 %load-immediate ]
224         [ swap [ %replace ] with each ] bi
225     ] unless-empty ;
226
227 GENERIC# save-gc-root 1 ( gc-root operand temp -- )
228
229 M:: spill-slot save-gc-root ( gc-root operand temp -- )
230     temp int-rep operand n>> %reload
231     gc-root temp %save-gc-root ;
232
233 M: object save-gc-root drop %save-gc-root ;
234
235 : save-gc-roots ( gc-roots temp -- ) '[ _ save-gc-root ] assoc-each ;
236
237 : save-data-regs ( data-regs -- ) [ first3 %spill ] each ;
238
239 GENERIC# load-gc-root 1 ( gc-root operand temp -- )
240
241 M:: spill-slot load-gc-root ( gc-root operand temp -- )
242     gc-root temp %load-gc-root
243     temp int-rep operand n>> %spill ;
244
245 M: object load-gc-root drop %load-gc-root ;
246
247 : load-gc-roots ( gc-roots temp -- ) '[ _ load-gc-root ] assoc-each ;
248
249 : load-data-regs ( data-regs -- ) [ first3 %reload ] each ;
250
251 M: _gc generate-insn
252     "no-gc" define-label
253     {
254         [ [ "no-gc" get ] dip [ temp1>> ] [ temp2>> ] bi %check-nursery ]
255         [ [ uninitialized-locs>> ] [ temp1>> ] bi wipe-locs ]
256         [ data-values>> save-data-regs ]
257         [ [ tagged-values>> ] [ temp1>> ] bi save-gc-roots ]
258         [ [ temp1>> ] [ temp2>> ] bi t %save-context ]
259         [ tagged-values>> length %call-gc ]
260         [ [ tagged-values>> ] [ temp1>> ] bi load-gc-roots ]
261         [ data-values>> load-data-regs ]
262     } cleave
263     "no-gc" resolve-label ;
264
265 M: _loop-entry generate-insn drop %loop-entry ;
266
267 M: ##alien-global generate-insn
268     [ dst>> ] [ symbol>> ] [ library>> ] tri
269     %alien-global ;
270
271 ! ##alien-invoke
272 GENERIC: next-fastcall-param ( rep -- )
273
274 : ?dummy-stack-params ( rep -- )
275     dummy-stack-params? [ rep-size cell align stack-params +@ ] [ drop ] if ;
276
277 : ?dummy-int-params ( rep -- )
278     dummy-int-params? [ rep-size cell /i 1 max int-regs +@ ] [ drop ] if ;
279
280 : ?dummy-fp-params ( rep -- )
281     drop dummy-fp-params? [ float-regs inc ] when ;
282
283 M: int-rep next-fastcall-param
284     int-regs inc [ ?dummy-stack-params ] [ ?dummy-fp-params ] bi ;
285
286 M: float-rep next-fastcall-param
287     float-regs inc [ ?dummy-stack-params ] [ ?dummy-int-params ] bi ;
288
289 M: double-rep next-fastcall-param
290     float-regs inc [ ?dummy-stack-params ] [ ?dummy-int-params ] bi ;
291
292 GENERIC: reg-class-full? ( reg-class -- ? )
293
294 M: stack-params reg-class-full? drop t ;
295
296 M: reg-class reg-class-full?
297     [ get ] [ param-regs length ] bi >= ;
298
299 : alloc-stack-param ( rep -- n reg-class rep )
300     stack-params get
301     [ rep-size cell align stack-params +@ ] dip
302     stack-params dup ;
303
304 : alloc-fastcall-param ( rep -- n reg-class rep )
305     [ [ reg-class-of get ] [ reg-class-of ] [ next-fastcall-param ] tri ] keep ;
306
307 : alloc-parameter ( parameter -- reg rep )
308     c-type-rep dup reg-class-of reg-class-full?
309     [ alloc-stack-param ] [ alloc-fastcall-param ] if
310     [ param-reg ] dip ;
311
312 : (flatten-int-type) ( size -- seq )
313     cell /i "void*" c-type <repetition> ;
314
315 GENERIC: flatten-value-type ( type -- types )
316
317 M: object flatten-value-type 1array ;
318
319 M: struct-type flatten-value-type ( type -- types )
320     stack-size cell align (flatten-int-type) ;
321
322 M: long-long-type flatten-value-type ( type -- types )
323     stack-size cell align (flatten-int-type) ;
324
325 : flatten-value-types ( params -- params )
326     #! Convert value type structs to consecutive void*s.
327     [
328         0 [
329             c-type
330             [ parameter-align (flatten-int-type) % ] keep
331             [ stack-size cell align + ] keep
332             flatten-value-type %
333         ] reduce drop
334     ] { } make ;
335
336 : each-parameter ( parameters quot -- )
337     [ [ parameter-sizes nip ] keep ] dip 2each ; inline
338
339 : reverse-each-parameter ( parameters quot -- )
340     [ [ parameter-sizes nip ] keep ] dip 2reverse-each ; inline
341
342 : reset-fastcall-counts ( -- )
343     { int-regs float-regs stack-params } [ 0 swap set ] each ;
344
345 : with-param-regs ( quot -- )
346     #! In quot you can call alloc-parameter
347     [ reset-fastcall-counts call ] with-scope ; inline
348
349 : move-parameters ( node word -- )
350     #! Moves values from C stack to registers (if word is
351     #! %load-param-reg) and registers to C stack (if word is
352     #! %save-param-reg).
353     [ alien-parameters flatten-value-types ]
354     [ '[ alloc-parameter _ execute ] ]
355     bi* each-parameter ; inline
356
357 : unbox-parameters ( offset node -- )
358     parameters>> [
359         %prepare-unbox [ over + ] dip unbox-parameter
360     ] reverse-each-parameter drop ;
361
362 : prepare-box-struct ( node -- offset )
363     #! Return offset on C stack where to store unboxed
364     #! parameters. If the C function is returning a structure,
365     #! the first parameter is an implicit target area pointer,
366     #! so we need to use a different offset.
367     return>> large-struct?
368     [ %prepare-box-struct cell ] [ 0 ] if ;
369
370 : objects>registers ( params -- )
371     #! Generate code for unboxing a list of C types, then
372     #! generate code for moving these parameters to registers on
373     #! architectures where parameters are passed in registers.
374     [
375         [ prepare-box-struct ] keep
376         [ unbox-parameters ] keep
377         \ %load-param-reg move-parameters
378     ] with-param-regs ;
379
380 : box-return* ( node -- )
381     return>> [ ] [ box-return ] if-void ;
382
383 : check-dlsym ( symbols dll -- )
384     dup dll-valid? [
385         dupd '[ _ dlsym ] any?
386         [ drop ] [ compiling-word get no-such-symbol ] if
387     ] [
388         dll-path compiling-word get no-such-library drop
389     ] if ;
390
391 : stdcall-mangle ( symbol params -- symbol )
392     parameters>> parameter-sizes drop number>string "@" glue ;
393
394 : alien-invoke-dlsym ( params -- symbols dll )
395     [ [ function>> dup ] keep stdcall-mangle 2array ]
396     [ library>> library dup [ dll>> ] when ]
397     bi 2dup check-dlsym ;
398
399 M: ##alien-invoke generate-insn
400     params>>
401     ! Unbox parameters
402     dup objects>registers
403     %prepare-var-args
404     ! Call function
405     dup alien-invoke-dlsym %alien-invoke
406     ! Box return value
407     dup %cleanup
408     box-return* ;
409
410 ! ##alien-indirect
411 M: ##alien-indirect generate-insn
412     params>>
413     ! Save alien at top of stack to temporary storage
414     %prepare-alien-indirect
415     ! Unbox parameters
416     dup objects>registers
417     %prepare-var-args
418     ! Call alien in temporary storage
419     %alien-indirect
420     ! Box return value
421     dup %cleanup
422     box-return* ;
423
424 ! ##alien-callback
425 : box-parameters ( params -- )
426     alien-parameters [ box-parameter ] each-parameter ;
427
428 : registers>objects ( node -- )
429     ! Generate code for boxing input parameters in a callback.
430     [
431         dup \ %save-param-reg move-parameters
432         "nest_stacks" f %alien-invoke
433         box-parameters
434     ] with-param-regs ;
435
436 TUPLE: callback-context ;
437
438 : current-callback ( -- id ) 2 getenv ;
439
440 : wait-to-return ( token -- )
441     dup current-callback eq? [
442         drop
443     ] [
444         yield-hook get call( -- ) wait-to-return
445     ] if ;
446
447 : do-callback ( quot token -- )
448     init-catchstack
449     [ 2 setenv call ] keep
450     wait-to-return ; inline
451
452 : callback-return-quot ( ctype -- quot )
453     return>> {
454         { [ dup "void" = ] [ drop [ ] ] }
455         { [ dup large-struct? ] [ heap-size '[ _ memcpy ] ] }
456         [ c-type c-type-unboxer-quot ]
457     } cond ;
458
459 : callback-prep-quot ( params -- quot )
460     parameters>> [ c-type c-type-boxer-quot ] map spread>quot ;
461
462 : wrap-callback-quot ( params -- quot )
463     [
464         [ callback-prep-quot ]
465         [ quot>> ]
466         [ callback-return-quot ] tri 3append ,
467         [ callback-context new do-callback ] %
468     ] [ ] make ;
469
470 : %unnest-stacks ( -- ) "unnest_stacks" f %alien-invoke ;
471
472 M: ##callback-return generate-insn
473     #! All the extra book-keeping for %unwind is only for x86.
474     #! On other platforms its an alias for %return.
475     params>> %callback-return ;
476
477 M: ##alien-callback generate-insn
478     params>>
479     [ registers>objects ]
480     [ wrap-callback-quot %alien-callback ]
481     [ alien-return [ %unnest-stacks ] [ %callback-value ] if-void ]
482     tri ;