]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/instructions/instructions.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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 classes.union
5 compiler.units alien byte-arrays compiler.constants combinators
6 compiler.cfg.registers compiler.cfg.instructions.syntax ;
7 IN: compiler.cfg.instructions
8
9 <<
10 SYMBOL: insn-classes
11 V{ } clone insn-classes set-global
12 >>
13
14 : new-insn ( ... class -- insn ) f swap boa ; inline
15
16 ! Virtual CPU instructions, used by CFG and machine IRs
17 TUPLE: insn ;
18
19 ! Instructions which are referentially transparent; used for
20 ! value numbering
21 TUPLE: pure-insn < insn ;
22
23 ! Stack operations
24 INSN: ##load-immediate
25 def: dst/int-rep
26 constant: val ;
27
28 INSN: ##load-reference
29 def: dst/int-rep
30 constant: obj ;
31
32 INSN: ##peek
33 def: dst/int-rep
34 literal: loc ;
35
36 INSN: ##replace
37 use: src/int-rep
38 literal: loc ;
39
40 INSN: ##inc-d
41 literal: n ;
42
43 INSN: ##inc-r
44 literal: n ;
45
46 ! Subroutine calls
47 INSN: ##call
48 literal: word ;
49
50 INSN: ##jump
51 literal: word ;
52
53 INSN: ##return ;
54
55 ! Dummy instruction that simply inhibits TCO
56 INSN: ##no-tco ;
57
58 ! Jump tables
59 INSN: ##dispatch
60 use: src/int-rep
61 temp: temp/int-rep ;
62
63 ! Slot access
64 INSN: ##slot
65 def: dst/int-rep
66 use: obj/int-rep slot/int-rep
67 literal: tag
68 temp: temp/int-rep ;
69
70 INSN: ##slot-imm
71 def: dst/int-rep
72 use: obj/int-rep
73 literal: slot tag ;
74
75 INSN: ##set-slot
76 use: src/int-rep obj/int-rep slot/int-rep
77 literal: tag
78 temp: temp/int-rep ;
79
80 INSN: ##set-slot-imm
81 use: src/int-rep obj/int-rep
82 literal: slot tag ;
83
84 ! String element access
85 INSN: ##string-nth
86 def: dst/int-rep
87 use: obj/int-rep index/int-rep
88 temp: temp/int-rep ;
89
90 INSN: ##set-string-nth-fast
91 use: src/int-rep obj/int-rep index/int-rep
92 temp: temp/int-rep ;
93
94 PURE-INSN: ##copy
95 def: dst
96 use: src
97 literal: rep ;
98
99 ! Integer arithmetic
100 PURE-INSN: ##add
101 def: dst/int-rep
102 use: src1/int-rep src2/int-rep ;
103
104 PURE-INSN: ##add-imm
105 def: dst/int-rep
106 use: src1/int-rep
107 constant: src2 ;
108
109 PURE-INSN: ##sub
110 def: dst/int-rep
111 use: src1/int-rep src2/int-rep ;
112
113 PURE-INSN: ##sub-imm
114 def: dst/int-rep
115 use: src1/int-rep
116 constant: src2 ;
117
118 PURE-INSN: ##mul
119 def: dst/int-rep
120 use: src1/int-rep src2/int-rep ;
121
122 PURE-INSN: ##mul-imm
123 def: dst/int-rep
124 use: src1/int-rep
125 constant: src2 ;
126
127 PURE-INSN: ##and
128 def: dst/int-rep
129 use: src1/int-rep src2/int-rep ;
130
131 PURE-INSN: ##and-imm
132 def: dst/int-rep
133 use: src1/int-rep
134 constant: src2 ;
135
136 PURE-INSN: ##or
137 def: dst/int-rep
138 use: src1/int-rep src2/int-rep ;
139
140 PURE-INSN: ##or-imm
141 def: dst/int-rep
142 use: src1/int-rep
143 constant: src2 ;
144
145 PURE-INSN: ##xor
146 def: dst/int-rep
147 use: src1/int-rep src2/int-rep ;
148
149 PURE-INSN: ##xor-imm
150 def: dst/int-rep
151 use: src1/int-rep
152 constant: src2 ;
153
154 PURE-INSN: ##shl
155 def: dst/int-rep
156 use: src1/int-rep src2/int-rep ;
157
158 PURE-INSN: ##shl-imm
159 def: dst/int-rep
160 use: src1/int-rep
161 constant: src2 ;
162
163 PURE-INSN: ##shr
164 def: dst/int-rep
165 use: src1/int-rep src2/int-rep ;
166
167 PURE-INSN: ##shr-imm
168 def: dst/int-rep
169 use: src1/int-rep
170 constant: src2 ;
171
172 PURE-INSN: ##sar
173 def: dst/int-rep
174 use: src1/int-rep src2/int-rep ;
175
176 PURE-INSN: ##sar-imm
177 def: dst/int-rep
178 use: src1/int-rep
179 constant: src2 ;
180
181 PURE-INSN: ##min
182 def: dst/int-rep
183 use: src1/int-rep src2/int-rep ;
184
185 PURE-INSN: ##max
186 def: dst/int-rep
187 use: src1/int-rep src2/int-rep ;
188
189 PURE-INSN: ##not
190 def: dst/int-rep
191 use: src/int-rep ;
192
193 PURE-INSN: ##log2
194 def: dst/int-rep
195 use: src/int-rep ;
196
197 ! Bignum/integer conversion
198 PURE-INSN: ##integer>bignum
199 def: dst/int-rep
200 use: src/int-rep
201 temp: temp/int-rep ;
202
203 PURE-INSN: ##bignum>integer
204 def: dst/int-rep
205 use: src/int-rep
206 temp: temp/int-rep ;
207
208 ! Float arithmetic
209 PURE-INSN: ##unbox-float
210 def: dst/double-rep
211 use: src/int-rep ;
212
213 PURE-INSN: ##box-float
214 def: dst/int-rep
215 use: src/double-rep
216 temp: temp/int-rep ;
217
218 PURE-INSN: ##add-float
219 def: dst/double-rep
220 use: src1/double-rep src2/double-rep ;
221
222 PURE-INSN: ##sub-float
223 def: dst/double-rep
224 use: src1/double-rep src2/double-rep ;
225
226 PURE-INSN: ##mul-float
227 def: dst/double-rep
228 use: src1/double-rep src2/double-rep ;
229
230 PURE-INSN: ##div-float
231 def: dst/double-rep
232 use: src1/double-rep src2/double-rep ;
233
234 PURE-INSN: ##min-float
235 def: dst/double-rep
236 use: src1/double-rep src2/double-rep ;
237
238 PURE-INSN: ##max-float
239 def: dst/double-rep
240 use: src1/double-rep src2/double-rep ;
241
242 PURE-INSN: ##sqrt
243 def: dst/double-rep
244 use: src/double-rep ;
245
246 ! libc intrinsics
247 PURE-INSN: ##unary-float-function
248 def: dst/double-rep
249 use: src/double-rep
250 literal: func ;
251
252 PURE-INSN: ##binary-float-function
253 def: dst/double-rep
254 use: src1/double-rep src2/double-rep
255 literal: func ;
256
257 ! Single/double float conversion
258 PURE-INSN: ##single>double-float
259 def: dst/double-rep
260 use: src/float-rep ;
261
262 PURE-INSN: ##double>single-float
263 def: dst/float-rep
264 use: src/double-rep ;
265
266 ! Float/integer conversion
267 PURE-INSN: ##float>integer
268 def: dst/int-rep
269 use: src/double-rep ;
270
271 PURE-INSN: ##integer>float
272 def: dst/double-rep
273 use: src/int-rep ;
274
275 ! SIMD operations
276
277 PURE-INSN: ##box-vector
278 def: dst/int-rep
279 use: src
280 literal: rep
281 temp: temp/int-rep ;
282
283 PURE-INSN: ##unbox-vector
284 def: dst
285 use: src/int-rep
286 literal: rep ;
287
288 PURE-INSN: ##broadcast-vector
289 def: dst
290 use: src/scalar-rep
291 literal: rep ;
292
293 PURE-INSN: ##gather-vector-2
294 def: dst
295 use: src1/scalar-rep src2/scalar-rep
296 literal: rep ;
297
298 PURE-INSN: ##gather-vector-4
299 def: dst
300 use: src1/scalar-rep src2/scalar-rep src3/scalar-rep src4/scalar-rep
301 literal: rep ;
302
303 PURE-INSN: ##add-vector
304 def: dst
305 use: src1 src2
306 literal: rep ;
307
308 PURE-INSN: ##sub-vector
309 def: dst
310 use: src1 src2
311 literal: rep ;
312
313 PURE-INSN: ##mul-vector
314 def: dst
315 use: src1 src2
316 literal: rep ;
317
318 PURE-INSN: ##div-vector
319 def: dst
320 use: src1 src2
321 literal: rep ;
322
323 PURE-INSN: ##min-vector
324 def: dst
325 use: src1 src2
326 literal: rep ;
327
328 PURE-INSN: ##max-vector
329 def: dst
330 use: src1 src2
331 literal: rep ;
332
333 PURE-INSN: ##sqrt-vector
334 def: dst
335 use: src
336 literal: rep ;
337
338 PURE-INSN: ##horizontal-add-vector
339 def: dst/scalar-rep
340 use: src
341 literal: rep ;
342
343 ! Boxing and unboxing aliens
344 PURE-INSN: ##box-alien
345 def: dst/int-rep
346 use: src/int-rep
347 temp: temp/int-rep ;
348
349 PURE-INSN: ##box-displaced-alien
350 def: dst/int-rep
351 use: displacement/int-rep base/int-rep
352 temp: temp1/int-rep temp2/int-rep
353 literal: base-class ;
354
355 PURE-INSN: ##unbox-any-c-ptr
356 def: dst/int-rep
357 use: src/int-rep
358 temp: temp/int-rep ;
359
360 : ##unbox-f ( dst src -- ) drop 0 ##load-immediate ;
361 : ##unbox-byte-array ( dst src -- ) byte-array-offset ##add-imm ;
362
363 PURE-INSN: ##unbox-alien
364 def: dst/int-rep
365 use: src/int-rep ;
366
367 : ##unbox-c-ptr ( dst src class temp -- )
368     {
369         { [ over \ f class<= ] [ 2drop ##unbox-f ] }
370         { [ over simple-alien class<= ] [ 2drop ##unbox-alien ] }
371         { [ over byte-array class<= ] [ 2drop ##unbox-byte-array ] }
372         [ nip ##unbox-any-c-ptr ]
373     } cond ;
374
375 ! Alien accessors
376 INSN: ##alien-unsigned-1
377 def: dst/int-rep
378 use: src/int-rep ;
379
380 INSN: ##alien-unsigned-2
381 def: dst/int-rep
382 use: src/int-rep ;
383
384 INSN: ##alien-unsigned-4
385 def: dst/int-rep
386 use: src/int-rep ;
387
388 INSN: ##alien-signed-1
389 def: dst/int-rep
390 use: src/int-rep ;
391
392 INSN: ##alien-signed-2
393 def: dst/int-rep
394 use: src/int-rep ;
395
396 INSN: ##alien-signed-4
397 def: dst/int-rep
398 use: src/int-rep ;
399
400 INSN: ##alien-cell
401 def: dst/int-rep
402 use: src/int-rep ;
403
404 INSN: ##alien-float
405 def: dst/float-rep
406 use: src/int-rep ;
407
408 INSN: ##alien-double
409 def: dst/double-rep
410 use: src/int-rep ;
411
412 INSN: ##alien-vector
413 def: dst
414 use: src/int-rep
415 literal: rep ;
416
417 INSN: ##set-alien-integer-1
418 use: src/int-rep value/int-rep ;
419
420 INSN: ##set-alien-integer-2
421 use: src/int-rep value/int-rep ;
422
423 INSN: ##set-alien-integer-4
424 use: src/int-rep value/int-rep ;
425
426 INSN: ##set-alien-cell
427 use: src/int-rep value/int-rep ;
428
429 INSN: ##set-alien-float
430 use: src/int-rep value/float-rep ;
431
432 INSN: ##set-alien-double
433 use: src/int-rep value/double-rep ;
434
435 INSN: ##set-alien-vector
436 use: src/int-rep value
437 literal: rep ;
438
439 ! Memory allocation
440 INSN: ##allot
441 def: dst/int-rep
442 literal: size class
443 temp: temp/int-rep ;
444
445 INSN: ##write-barrier
446 use: src/int-rep
447 temp: card#/int-rep table/int-rep ;
448
449 INSN: ##alien-global
450 def: dst/int-rep
451 literal: symbol library ;
452
453 ! FFI
454 INSN: ##alien-invoke
455 literal: params stack-frame ;
456
457 INSN: ##alien-indirect
458 literal: params stack-frame ;
459
460 INSN: ##alien-callback
461 literal: params stack-frame ;
462
463 INSN: ##callback-return
464 literal: params ;
465
466 ! Instructions used by CFG IR only.
467 INSN: ##prologue ;
468 INSN: ##epilogue ;
469
470 INSN: ##branch ;
471
472 INSN: ##phi
473 def: dst
474 literal: inputs ;
475
476 ! Conditionals
477 INSN: ##compare-branch
478 use: src1/int-rep src2/int-rep
479 literal: cc ;
480
481 INSN: ##compare-imm-branch
482 use: src1/int-rep
483 constant: src2
484 literal: cc ;
485
486 PURE-INSN: ##compare
487 def: dst/int-rep
488 use: src1/int-rep src2/int-rep
489 literal: cc
490 temp: temp/int-rep ;
491
492 PURE-INSN: ##compare-imm
493 def: dst/int-rep
494 use: src1/int-rep
495 constant: src2
496 literal: cc
497 temp: temp/int-rep ;
498
499 INSN: ##compare-float-ordered-branch
500 use: src1/double-rep src2/double-rep
501 literal: cc ;
502
503 INSN: ##compare-float-unordered-branch
504 use: src1/double-rep src2/double-rep
505 literal: cc ;
506
507 PURE-INSN: ##compare-float-ordered
508 def: dst/int-rep
509 use: src1/double-rep src2/double-rep
510 literal: cc
511 temp: temp/int-rep ;
512
513 PURE-INSN: ##compare-float-unordered
514 def: dst/int-rep
515 use: src1/double-rep src2/double-rep
516 literal: cc
517 temp: temp/int-rep ;
518
519 ! Overflowing arithmetic
520 INSN: ##fixnum-add
521 def: dst/int-rep
522 use: src1/int-rep src2/int-rep ;
523
524 INSN: ##fixnum-sub
525 def: dst/int-rep
526 use: src1/int-rep src2/int-rep ;
527
528 INSN: ##fixnum-mul
529 def: dst/int-rep
530 use: src1/int-rep src2/int-rep ;
531
532 INSN: ##gc
533 temp: temp1/int-rep temp2/int-rep
534 literal: data-values tagged-values uninitialized-locs ;
535
536 INSN: ##save-context
537 temp: temp1/int-rep temp2/int-rep
538 literal: callback-allowed? ;
539
540 ! Instructions used by machine IR only.
541 INSN: _prologue
542 literal: stack-frame ;
543
544 INSN: _epilogue
545 literal: stack-frame ;
546
547 INSN: _label
548 literal: label ;
549
550 INSN: _branch
551 literal: label ;
552
553 INSN: _loop-entry ;
554
555 INSN: _dispatch
556 use: src/int-rep
557 temp: temp ;
558
559 INSN: _dispatch-label
560 literal: label ;
561
562 INSN: _compare-branch
563 literal: label
564 use: src1/int-rep src2/int-rep
565 literal: cc ;
566
567 INSN: _compare-imm-branch
568 literal: label
569 use: src1/int-rep
570 constant: src2
571 literal: cc ;
572
573 INSN: _compare-float-unordered-branch
574 literal: label
575 use: src1/int-rep src2/int-rep
576 literal: cc ;
577
578 INSN: _compare-float-ordered-branch
579 literal: label
580 use: src1/int-rep src2/int-rep
581 literal: cc ;
582
583 ! Overflowing arithmetic
584 INSN: _fixnum-add
585 literal: label
586 def: dst/int-rep
587 use: src1/int-rep src2/int-rep ;
588
589 INSN: _fixnum-sub
590 literal: label
591 def: dst/int-rep
592 use: src1/int-rep src2/int-rep ;
593
594 INSN: _fixnum-mul
595 literal: label
596 def: dst/int-rep
597 use: src1/int-rep src2/int-rep ;
598
599 TUPLE: spill-slot n ; C: <spill-slot> spill-slot
600
601 INSN: _gc
602 temp: temp1 temp2
603 literal: data-values tagged-values uninitialized-locs ;
604
605 ! These instructions operate on machine registers and not
606 ! virtual registers
607 INSN: _spill
608 use: src
609 literal: rep n ;
610
611 INSN: _reload
612 def: dst
613 literal: rep n ;
614
615 INSN: _spill-area-size
616 literal: n ;
617
618 UNION: ##allocation
619 ##allot
620 ##box-float
621 ##box-vector
622 ##box-alien
623 ##box-displaced-alien
624 ##integer>bignum ;
625
626 ! For alias analysis
627 UNION: ##read ##slot ##slot-imm ;
628 UNION: ##write ##set-slot ##set-slot-imm ;
629
630 ! Instructions that kill all live vregs but cannot trigger GC
631 UNION: partial-sync-insn
632 ##unary-float-function
633 ##binary-float-function ;
634
635 ! Instructions that kill all live vregs
636 UNION: kill-vreg-insn
637 ##call
638 ##prologue
639 ##epilogue
640 ##alien-invoke
641 ##alien-indirect
642 ##alien-callback ;
643
644 ! Instructions that have complex expansions and require that the
645 ! output registers are not equal to any of the input registers
646 UNION: def-is-use-insn
647 ##integer>bignum
648 ##bignum>integer
649 ##unbox-any-c-ptr ;
650
651 SYMBOL: vreg-insn
652
653 [
654     vreg-insn
655     insn-classes get [
656         "insn-slots" word-prop [ type>> { def use temp } memq? ] any?
657     ] filter
658     define-union-class
659 ] with-compilation-unit