]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/instructions/instructions.factor
Fix conflict
[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: ##saturated-add-vector
309 def: dst
310 use: src1 src2
311 literal: rep ;
312
313 PURE-INSN: ##add-sub-vector
314 def: dst
315 use: src1 src2
316 literal: rep ;
317
318 PURE-INSN: ##sub-vector
319 def: dst
320 use: src1 src2
321 literal: rep ;
322
323 PURE-INSN: ##saturated-sub-vector
324 def: dst
325 use: src1 src2
326 literal: rep ;
327
328 PURE-INSN: ##mul-vector
329 def: dst
330 use: src1 src2
331 literal: rep ;
332
333 PURE-INSN: ##saturated-mul-vector
334 def: dst
335 use: src1 src2
336 literal: rep ;
337
338 PURE-INSN: ##div-vector
339 def: dst
340 use: src1 src2
341 literal: rep ;
342
343 PURE-INSN: ##min-vector
344 def: dst
345 use: src1 src2
346 literal: rep ;
347
348 PURE-INSN: ##max-vector
349 def: dst
350 use: src1 src2
351 literal: rep ;
352
353 PURE-INSN: ##sqrt-vector
354 def: dst
355 use: src
356 literal: rep ;
357
358 PURE-INSN: ##horizontal-add-vector
359 def: dst/scalar-rep
360 use: src
361 literal: rep ;
362
363 ! Boxing and unboxing aliens
364 PURE-INSN: ##box-alien
365 def: dst/int-rep
366 use: src/int-rep
367 temp: temp/int-rep ;
368
369 PURE-INSN: ##box-displaced-alien
370 def: dst/int-rep
371 use: displacement/int-rep base/int-rep
372 temp: temp1/int-rep temp2/int-rep
373 literal: base-class ;
374
375 PURE-INSN: ##unbox-any-c-ptr
376 def: dst/int-rep
377 use: src/int-rep
378 temp: temp/int-rep ;
379
380 : ##unbox-f ( dst src -- ) drop 0 ##load-immediate ;
381 : ##unbox-byte-array ( dst src -- ) byte-array-offset ##add-imm ;
382
383 PURE-INSN: ##unbox-alien
384 def: dst/int-rep
385 use: src/int-rep ;
386
387 : ##unbox-c-ptr ( dst src class temp -- )
388     {
389         { [ over \ f class<= ] [ 2drop ##unbox-f ] }
390         { [ over simple-alien class<= ] [ 2drop ##unbox-alien ] }
391         { [ over byte-array class<= ] [ 2drop ##unbox-byte-array ] }
392         [ nip ##unbox-any-c-ptr ]
393     } cond ;
394
395 ! Alien accessors
396 INSN: ##alien-unsigned-1
397 def: dst/int-rep
398 use: src/int-rep ;
399
400 INSN: ##alien-unsigned-2
401 def: dst/int-rep
402 use: src/int-rep ;
403
404 INSN: ##alien-unsigned-4
405 def: dst/int-rep
406 use: src/int-rep ;
407
408 INSN: ##alien-signed-1
409 def: dst/int-rep
410 use: src/int-rep ;
411
412 INSN: ##alien-signed-2
413 def: dst/int-rep
414 use: src/int-rep ;
415
416 INSN: ##alien-signed-4
417 def: dst/int-rep
418 use: src/int-rep ;
419
420 INSN: ##alien-cell
421 def: dst/int-rep
422 use: src/int-rep ;
423
424 INSN: ##alien-float
425 def: dst/float-rep
426 use: src/int-rep ;
427
428 INSN: ##alien-double
429 def: dst/double-rep
430 use: src/int-rep ;
431
432 INSN: ##alien-vector
433 def: dst
434 use: src/int-rep
435 literal: rep ;
436
437 INSN: ##set-alien-integer-1
438 use: src/int-rep value/int-rep ;
439
440 INSN: ##set-alien-integer-2
441 use: src/int-rep value/int-rep ;
442
443 INSN: ##set-alien-integer-4
444 use: src/int-rep value/int-rep ;
445
446 INSN: ##set-alien-cell
447 use: src/int-rep value/int-rep ;
448
449 INSN: ##set-alien-float
450 use: src/int-rep value/float-rep ;
451
452 INSN: ##set-alien-double
453 use: src/int-rep value/double-rep ;
454
455 INSN: ##set-alien-vector
456 use: src/int-rep value
457 literal: rep ;
458
459 ! Memory allocation
460 INSN: ##allot
461 def: dst/int-rep
462 literal: size class
463 temp: temp/int-rep ;
464
465 INSN: ##write-barrier
466 use: src/int-rep
467 temp: card#/int-rep table/int-rep ;
468
469 INSN: ##alien-global
470 def: dst/int-rep
471 literal: symbol library ;
472
473 INSN: ##vm-field-ptr
474 def: dst/int-rep
475 literal: fieldname ;
476
477 ! FFI
478 INSN: ##alien-invoke
479 literal: params stack-frame ;
480
481 INSN: ##alien-indirect
482 literal: params stack-frame ;
483
484 INSN: ##alien-callback
485 literal: params stack-frame ;
486
487 INSN: ##callback-return
488 literal: params ;
489
490 ! Instructions used by CFG IR only.
491 INSN: ##prologue ;
492 INSN: ##epilogue ;
493
494 INSN: ##branch ;
495
496 INSN: ##phi
497 def: dst
498 literal: inputs ;
499
500 ! Conditionals
501 INSN: ##compare-branch
502 use: src1/int-rep src2/int-rep
503 literal: cc ;
504
505 INSN: ##compare-imm-branch
506 use: src1/int-rep
507 constant: src2
508 literal: cc ;
509
510 PURE-INSN: ##compare
511 def: dst/int-rep
512 use: src1/int-rep src2/int-rep
513 literal: cc
514 temp: temp/int-rep ;
515
516 PURE-INSN: ##compare-imm
517 def: dst/int-rep
518 use: src1/int-rep
519 constant: src2
520 literal: cc
521 temp: temp/int-rep ;
522
523 INSN: ##compare-float-ordered-branch
524 use: src1/double-rep src2/double-rep
525 literal: cc ;
526
527 INSN: ##compare-float-unordered-branch
528 use: src1/double-rep src2/double-rep
529 literal: cc ;
530
531 PURE-INSN: ##compare-float-ordered
532 def: dst/int-rep
533 use: src1/double-rep src2/double-rep
534 literal: cc
535 temp: temp/int-rep ;
536
537 PURE-INSN: ##compare-float-unordered
538 def: dst/int-rep
539 use: src1/double-rep src2/double-rep
540 literal: cc
541 temp: temp/int-rep ;
542
543 ! Overflowing arithmetic
544 INSN: ##fixnum-add
545 def: dst/int-rep
546 use: src1/int-rep src2/int-rep ;
547
548 INSN: ##fixnum-sub
549 def: dst/int-rep
550 use: src1/int-rep src2/int-rep ;
551
552 INSN: ##fixnum-mul
553 def: dst/int-rep
554 use: src1/int-rep src2/int-rep ;
555
556 INSN: ##gc
557 temp: temp1/int-rep temp2/int-rep
558 literal: data-values tagged-values uninitialized-locs ;
559
560 INSN: ##save-context
561 temp: temp1/int-rep temp2/int-rep
562 literal: callback-allowed? ;
563
564 ! Instructions used by machine IR only.
565 INSN: _prologue
566 literal: stack-frame ;
567
568 INSN: _epilogue
569 literal: stack-frame ;
570
571 INSN: _label
572 literal: label ;
573
574 INSN: _branch
575 literal: label ;
576
577 INSN: _loop-entry ;
578
579 INSN: _dispatch
580 use: src/int-rep
581 temp: temp ;
582
583 INSN: _dispatch-label
584 literal: label ;
585
586 INSN: _compare-branch
587 literal: label
588 use: src1/int-rep src2/int-rep
589 literal: cc ;
590
591 INSN: _compare-imm-branch
592 literal: label
593 use: src1/int-rep
594 constant: src2
595 literal: cc ;
596
597 INSN: _compare-float-unordered-branch
598 literal: label
599 use: src1/int-rep src2/int-rep
600 literal: cc ;
601
602 INSN: _compare-float-ordered-branch
603 literal: label
604 use: src1/int-rep src2/int-rep
605 literal: cc ;
606
607 ! Overflowing arithmetic
608 INSN: _fixnum-add
609 literal: label
610 def: dst/int-rep
611 use: src1/int-rep src2/int-rep ;
612
613 INSN: _fixnum-sub
614 literal: label
615 def: dst/int-rep
616 use: src1/int-rep src2/int-rep ;
617
618 INSN: _fixnum-mul
619 literal: label
620 def: dst/int-rep
621 use: src1/int-rep src2/int-rep ;
622
623 TUPLE: spill-slot n ; C: <spill-slot> spill-slot
624
625 INSN: _gc
626 temp: temp1 temp2
627 literal: data-values tagged-values uninitialized-locs ;
628
629 ! These instructions operate on machine registers and not
630 ! virtual registers
631 INSN: _spill
632 use: src
633 literal: rep n ;
634
635 INSN: _reload
636 def: dst
637 literal: rep n ;
638
639 INSN: _spill-area-size
640 literal: n ;
641
642 UNION: ##allocation
643 ##allot
644 ##box-float
645 ##box-vector
646 ##box-alien
647 ##box-displaced-alien
648 ##integer>bignum ;
649
650 ! For alias analysis
651 UNION: ##read ##slot ##slot-imm ;
652 UNION: ##write ##set-slot ##set-slot-imm ;
653
654 ! Instructions that kill all live vregs but cannot trigger GC
655 UNION: partial-sync-insn
656 ##unary-float-function
657 ##binary-float-function ;
658
659 ! Instructions that kill all live vregs
660 UNION: kill-vreg-insn
661 ##call
662 ##prologue
663 ##epilogue
664 ##alien-invoke
665 ##alien-indirect
666 ##alien-callback ;
667
668 ! Instructions that have complex expansions and require that the
669 ! output registers are not equal to any of the input registers
670 UNION: def-is-use-insn
671 ##integer>bignum
672 ##bignum>integer
673 ##unbox-any-c-ptr ;
674
675 SYMBOL: vreg-insn
676
677 [
678     vreg-insn
679     insn-classes get [
680         "insn-slots" word-prop [ type>> { def use temp } memq? ] any?
681     ] filter
682     define-union-class
683 ] with-compilation-unit