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