]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/instructions/instructions.factor
merge project-euler.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: ##load-constant
33 def: dst/int-rep
34 constant: obj ;
35
36 INSN: ##peek
37 def: dst/int-rep
38 literal: loc ;
39
40 INSN: ##replace
41 use: src/int-rep
42 literal: loc ;
43
44 INSN: ##inc-d
45 literal: n ;
46
47 INSN: ##inc-r
48 literal: n ;
49
50 ! Subroutine calls
51 INSN: ##call
52 literal: word ;
53
54 INSN: ##jump
55 literal: word ;
56
57 INSN: ##return ;
58
59 ! Dummy instruction that simply inhibits TCO
60 INSN: ##no-tco ;
61
62 ! Jump tables
63 INSN: ##dispatch
64 use: src/int-rep
65 temp: temp/int-rep ;
66
67 ! Slot access
68 INSN: ##slot
69 def: dst/int-rep
70 use: obj/int-rep slot/int-rep ;
71
72 INSN: ##slot-imm
73 def: dst/int-rep
74 use: obj/int-rep
75 literal: slot tag ;
76
77 INSN: ##set-slot
78 use: src/int-rep obj/int-rep slot/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: ##neg
194 def: dst/int-rep
195 use: src/int-rep ;
196
197 PURE-INSN: ##log2
198 def: dst/int-rep
199 use: src/int-rep ;
200
201 ! Float arithmetic
202 PURE-INSN: ##add-float
203 def: dst/double-rep
204 use: src1/double-rep src2/double-rep ;
205
206 PURE-INSN: ##sub-float
207 def: dst/double-rep
208 use: src1/double-rep src2/double-rep ;
209
210 PURE-INSN: ##mul-float
211 def: dst/double-rep
212 use: src1/double-rep src2/double-rep ;
213
214 PURE-INSN: ##div-float
215 def: dst/double-rep
216 use: src1/double-rep src2/double-rep ;
217
218 PURE-INSN: ##min-float
219 def: dst/double-rep
220 use: src1/double-rep src2/double-rep ;
221
222 PURE-INSN: ##max-float
223 def: dst/double-rep
224 use: src1/double-rep src2/double-rep ;
225
226 PURE-INSN: ##sqrt
227 def: dst/double-rep
228 use: src/double-rep ;
229
230 ! libc intrinsics
231 PURE-INSN: ##unary-float-function
232 def: dst/double-rep
233 use: src/double-rep
234 literal: func ;
235
236 PURE-INSN: ##binary-float-function
237 def: dst/double-rep
238 use: src1/double-rep src2/double-rep
239 literal: func ;
240
241 ! Single/double float conversion
242 PURE-INSN: ##single>double-float
243 def: dst/double-rep
244 use: src/float-rep ;
245
246 PURE-INSN: ##double>single-float
247 def: dst/float-rep
248 use: src/double-rep ;
249
250 ! Float/integer conversion
251 PURE-INSN: ##float>integer
252 def: dst/int-rep
253 use: src/double-rep ;
254
255 PURE-INSN: ##integer>float
256 def: dst/double-rep
257 use: src/int-rep ;
258
259 ! SIMD operations
260 PURE-INSN: ##zero-vector
261 def: dst
262 literal: rep ;
263
264 PURE-INSN: ##fill-vector
265 def: dst
266 literal: rep ;
267
268 PURE-INSN: ##gather-vector-2
269 def: dst
270 use: src1/scalar-rep src2/scalar-rep
271 literal: rep ;
272
273 PURE-INSN: ##gather-vector-4
274 def: dst
275 use: src1/scalar-rep src2/scalar-rep src3/scalar-rep src4/scalar-rep
276 literal: rep ;
277
278 PURE-INSN: ##shuffle-vector
279 def: dst
280 use: src shuffle
281 literal: rep ;
282
283 PURE-INSN: ##shuffle-vector-imm
284 def: dst
285 use: src
286 literal: shuffle rep ;
287
288 PURE-INSN: ##tail>head-vector
289 def: dst
290 use: src
291 literal: rep ;
292
293 PURE-INSN: ##merge-vector-head
294 def: dst
295 use: src1 src2
296 literal: rep ;
297
298 PURE-INSN: ##merge-vector-tail
299 def: dst
300 use: src1 src2
301 literal: rep ;
302
303 PURE-INSN: ##signed-pack-vector
304 def: dst
305 use: src1 src2
306 literal: rep ;
307
308 PURE-INSN: ##unsigned-pack-vector
309 def: dst
310 use: src1 src2
311 literal: rep ;
312
313 PURE-INSN: ##unpack-vector-head
314 def: dst
315 use: src
316 literal: rep ;
317
318 PURE-INSN: ##unpack-vector-tail
319 def: dst
320 use: src
321 literal: rep ;
322
323 PURE-INSN: ##integer>float-vector
324 def: dst
325 use: src
326 literal: rep ;
327
328 PURE-INSN: ##float>integer-vector
329 def: dst
330 use: src
331 literal: rep ;
332
333 PURE-INSN: ##compare-vector
334 def: dst
335 use: src1 src2
336 literal: rep cc ;
337
338 PURE-INSN: ##test-vector
339 def: dst/int-rep
340 use: src1
341 temp: temp/int-rep
342 literal: rep vcc ;
343
344 INSN: ##test-vector-branch
345 use: src1
346 temp: temp/int-rep
347 literal: rep vcc ;
348
349 INSN: _test-vector-branch
350 literal: label
351 use: src1
352 temp: temp/int-rep
353 literal: rep vcc ;
354
355 PURE-INSN: ##add-vector
356 def: dst
357 use: src1 src2
358 literal: rep ;
359
360 PURE-INSN: ##saturated-add-vector
361 def: dst
362 use: src1 src2
363 literal: rep ;
364
365 PURE-INSN: ##add-sub-vector
366 def: dst
367 use: src1 src2
368 literal: rep ;
369
370 PURE-INSN: ##sub-vector
371 def: dst
372 use: src1 src2
373 literal: rep ;
374
375 PURE-INSN: ##saturated-sub-vector
376 def: dst
377 use: src1 src2
378 literal: rep ;
379
380 PURE-INSN: ##mul-vector
381 def: dst
382 use: src1 src2
383 literal: rep ;
384
385 PURE-INSN: ##saturated-mul-vector
386 def: dst
387 use: src1 src2
388 literal: rep ;
389
390 PURE-INSN: ##div-vector
391 def: dst
392 use: src1 src2
393 literal: rep ;
394
395 PURE-INSN: ##min-vector
396 def: dst
397 use: src1 src2
398 literal: rep ;
399
400 PURE-INSN: ##max-vector
401 def: dst
402 use: src1 src2
403 literal: rep ;
404
405 PURE-INSN: ##dot-vector
406 def: dst/scalar-rep
407 use: src1 src2
408 literal: rep ;
409
410 PURE-INSN: ##horizontal-add-vector
411 def: dst/scalar-rep
412 use: src
413 literal: rep ;
414
415 PURE-INSN: ##horizontal-sub-vector
416 def: dst/scalar-rep
417 use: src
418 literal: rep ;
419
420 PURE-INSN: ##horizontal-shl-vector
421 def: dst
422 use: src1
423 literal: src2 rep ;
424
425 PURE-INSN: ##horizontal-shr-vector
426 def: dst
427 use: src1
428 literal: src2 rep ;
429
430 PURE-INSN: ##abs-vector
431 def: dst
432 use: src
433 literal: rep ;
434
435 PURE-INSN: ##sqrt-vector
436 def: dst
437 use: src
438 literal: rep ;
439
440 PURE-INSN: ##and-vector
441 def: dst
442 use: src1 src2
443 literal: rep ;
444
445 PURE-INSN: ##andn-vector
446 def: dst
447 use: src1 src2
448 literal: rep ;
449
450 PURE-INSN: ##or-vector
451 def: dst
452 use: src1 src2
453 literal: rep ;
454
455 PURE-INSN: ##xor-vector
456 def: dst
457 use: src1 src2
458 literal: rep ;
459
460 PURE-INSN: ##not-vector
461 def: dst
462 use: src
463 literal: rep ;
464
465 PURE-INSN: ##shl-vector
466 def: dst
467 use: src1 src2/int-scalar-rep
468 literal: rep ;
469
470 PURE-INSN: ##shr-vector
471 def: dst
472 use: src1 src2/int-scalar-rep
473 literal: rep ;
474
475 ! Scalar/vector conversion
476 PURE-INSN: ##scalar>integer
477 def: dst/int-rep
478 use: src
479 literal: rep ;
480
481 PURE-INSN: ##integer>scalar
482 def: dst
483 use: src/int-rep
484 literal: rep ;
485
486 PURE-INSN: ##vector>scalar
487 def: dst/scalar-rep
488 use: src
489 literal: rep ;
490
491 PURE-INSN: ##scalar>vector
492 def: dst
493 use: src/scalar-rep
494 literal: rep ;
495
496 ! Boxing and unboxing aliens
497 PURE-INSN: ##box-alien
498 def: dst/int-rep
499 use: src/int-rep
500 temp: temp/int-rep ;
501
502 PURE-INSN: ##box-displaced-alien
503 def: dst/int-rep
504 use: displacement/int-rep base/int-rep
505 temp: temp1/int-rep temp2/int-rep
506 literal: base-class ;
507
508 PURE-INSN: ##unbox-any-c-ptr
509 def: dst/int-rep
510 use: src/int-rep
511 temp: temp/int-rep ;
512
513 : ##unbox-f ( dst src -- ) drop 0 ##load-immediate ;
514 : ##unbox-byte-array ( dst src -- ) byte-array-offset ##add-imm ;
515
516 PURE-INSN: ##unbox-alien
517 def: dst/int-rep
518 use: src/int-rep ;
519
520 : ##unbox-c-ptr ( dst src class temp -- )
521     {
522         { [ over \ f class<= ] [ 2drop ##unbox-f ] }
523         { [ over simple-alien class<= ] [ 2drop ##unbox-alien ] }
524         { [ over byte-array class<= ] [ 2drop ##unbox-byte-array ] }
525         [ nip ##unbox-any-c-ptr ]
526     } cond ;
527
528 ! Alien accessors
529 INSN: ##alien-unsigned-1
530 def: dst/int-rep
531 use: src/int-rep
532 literal: offset ;
533
534 INSN: ##alien-unsigned-2
535 def: dst/int-rep
536 use: src/int-rep
537 literal: offset ;
538
539 INSN: ##alien-unsigned-4
540 def: dst/int-rep
541 use: src/int-rep
542 literal: offset ;
543
544 INSN: ##alien-signed-1
545 def: dst/int-rep
546 use: src/int-rep
547 literal: offset ;
548
549 INSN: ##alien-signed-2
550 def: dst/int-rep
551 use: src/int-rep
552 literal: offset ;
553
554 INSN: ##alien-signed-4
555 def: dst/int-rep
556 use: src/int-rep
557 literal: offset ;
558
559 INSN: ##alien-cell
560 def: dst/int-rep
561 use: src/int-rep
562 literal: offset ;
563
564 INSN: ##alien-float
565 def: dst/float-rep
566 use: src/int-rep
567 literal: offset ;
568
569 INSN: ##alien-double
570 def: dst/double-rep
571 use: src/int-rep
572 literal: offset ;
573
574 INSN: ##alien-vector
575 def: dst
576 use: src/int-rep
577 literal: offset rep ;
578
579 INSN: ##set-alien-integer-1
580 use: src/int-rep
581 literal: offset
582 use: value/int-rep ;
583
584 INSN: ##set-alien-integer-2
585 use: src/int-rep
586 literal: offset
587 use: value/int-rep ;
588
589 INSN: ##set-alien-integer-4
590 use: src/int-rep
591 literal: offset
592 use: value/int-rep ;
593
594 INSN: ##set-alien-cell
595 use: src/int-rep
596 literal: offset
597 use: value/int-rep ;
598
599 INSN: ##set-alien-float
600 use: src/int-rep
601 literal: offset
602 use: value/float-rep ;
603
604 INSN: ##set-alien-double
605 use: src/int-rep
606 literal: offset
607 use: value/double-rep ;
608
609 INSN: ##set-alien-vector
610 use: src/int-rep
611 literal: offset
612 use: value
613 literal: rep ;
614
615 ! Memory allocation
616 INSN: ##allot
617 def: dst/int-rep
618 literal: size class
619 temp: temp/int-rep ;
620
621 INSN: ##write-barrier
622 use: src/int-rep
623 temp: card#/int-rep table/int-rep ;
624
625 INSN: ##alien-global
626 def: dst/int-rep
627 literal: symbol library ;
628
629 INSN: ##vm-field-ptr
630 def: dst/int-rep
631 literal: field-name ;
632
633 ! FFI
634 INSN: ##alien-invoke
635 literal: params stack-frame ;
636
637 INSN: ##alien-indirect
638 literal: params stack-frame ;
639
640 INSN: ##alien-callback
641 literal: params stack-frame ;
642
643 INSN: ##callback-return
644 literal: params ;
645
646 ! Instructions used by CFG IR only.
647 INSN: ##prologue ;
648 INSN: ##epilogue ;
649
650 INSN: ##branch ;
651
652 INSN: ##phi
653 def: dst
654 literal: inputs ;
655
656 ! Conditionals
657 INSN: ##compare-branch
658 use: src1/int-rep src2/int-rep
659 literal: cc ;
660
661 INSN: ##compare-imm-branch
662 use: src1/int-rep
663 constant: src2
664 literal: cc ;
665
666 PURE-INSN: ##compare
667 def: dst/int-rep
668 use: src1/int-rep src2/int-rep
669 literal: cc
670 temp: temp/int-rep ;
671
672 PURE-INSN: ##compare-imm
673 def: dst/int-rep
674 use: src1/int-rep
675 constant: src2
676 literal: cc
677 temp: temp/int-rep ;
678
679 INSN: ##compare-float-ordered-branch
680 use: src1/double-rep src2/double-rep
681 literal: cc ;
682
683 INSN: ##compare-float-unordered-branch
684 use: src1/double-rep src2/double-rep
685 literal: cc ;
686
687 PURE-INSN: ##compare-float-ordered
688 def: dst/int-rep
689 use: src1/double-rep src2/double-rep
690 literal: cc
691 temp: temp/int-rep ;
692
693 PURE-INSN: ##compare-float-unordered
694 def: dst/int-rep
695 use: src1/double-rep src2/double-rep
696 literal: cc
697 temp: temp/int-rep ;
698
699 ! Overflowing arithmetic
700 INSN: ##fixnum-add
701 def: dst/int-rep
702 use: src1/int-rep src2/int-rep ;
703
704 INSN: ##fixnum-sub
705 def: dst/int-rep
706 use: src1/int-rep src2/int-rep ;
707
708 INSN: ##fixnum-mul
709 def: dst/int-rep
710 use: src1/int-rep src2/int-rep ;
711
712 INSN: ##gc
713 temp: temp1/int-rep temp2/int-rep
714 literal: size data-values tagged-values uninitialized-locs ;
715
716 INSN: ##save-context
717 temp: temp1/int-rep temp2/int-rep
718 literal: callback-allowed? ;
719
720 ! Instructions used by machine IR only.
721 INSN: _prologue
722 literal: stack-frame ;
723
724 INSN: _epilogue
725 literal: stack-frame ;
726
727 INSN: _label
728 literal: label ;
729
730 INSN: _branch
731 literal: label ;
732
733 INSN: _loop-entry ;
734
735 INSN: _dispatch
736 use: src/int-rep
737 temp: temp ;
738
739 INSN: _dispatch-label
740 literal: label ;
741
742 INSN: _compare-branch
743 literal: label
744 use: src1/int-rep src2/int-rep
745 literal: cc ;
746
747 INSN: _compare-imm-branch
748 literal: label
749 use: src1/int-rep
750 constant: src2
751 literal: cc ;
752
753 INSN: _compare-float-unordered-branch
754 literal: label
755 use: src1/int-rep src2/int-rep
756 literal: cc ;
757
758 INSN: _compare-float-ordered-branch
759 literal: label
760 use: src1/int-rep src2/int-rep
761 literal: cc ;
762
763 ! Overflowing arithmetic
764 INSN: _fixnum-add
765 literal: label
766 def: dst/int-rep
767 use: src1/int-rep src2/int-rep ;
768
769 INSN: _fixnum-sub
770 literal: label
771 def: dst/int-rep
772 use: src1/int-rep src2/int-rep ;
773
774 INSN: _fixnum-mul
775 literal: label
776 def: dst/int-rep
777 use: src1/int-rep src2/int-rep ;
778
779 TUPLE: spill-slot { n integer } ;
780 C: <spill-slot> spill-slot
781
782 ! These instructions operate on machine registers and not
783 ! virtual registers
784 INSN: _spill
785 use: src
786 literal: rep dst ;
787
788 INSN: _reload
789 def: dst
790 literal: rep src ;
791
792 INSN: _spill-area-size
793 literal: n ;
794
795 UNION: ##allocation
796 ##allot
797 ##box-alien
798 ##box-displaced-alien ;
799
800 ! For alias analysis
801 UNION: ##read ##slot ##slot-imm ##vm-field-ptr ##alien-global ;
802 UNION: ##write ##set-slot ##set-slot-imm ;
803
804 ! Instructions that kill all live vregs but cannot trigger GC
805 UNION: partial-sync-insn
806 ##unary-float-function
807 ##binary-float-function ;
808
809 ! Instructions that kill all live vregs
810 UNION: kill-vreg-insn
811 ##call
812 ##prologue
813 ##epilogue
814 ##alien-invoke
815 ##alien-indirect
816 ##alien-callback ;
817
818 ! Instructions that have complex expansions and require that the
819 ! output registers are not equal to any of the input registers
820 UNION: def-is-use-insn
821 ##box-alien
822 ##box-displaced-alien
823 ##string-nth
824 ##unbox-any-c-ptr ;
825
826 SYMBOL: vreg-insn
827
828 [
829     vreg-insn
830     insn-classes get [
831         "insn-slots" word-prop [ type>> { def use temp } memq? ] any?
832     ] filter
833     define-union-class
834 ] with-compilation-unit