factor/basis/compiler/cfg/instructions/instructions.factor

802 lines
13 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2010 Slava Pestov.
2008-09-10 23:11:03 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-10-17 16:35:04 -04:00
USING: assocs accessors arrays kernel sequences namespaces words
2010-04-22 04:21:23 -04:00
math math.order layouts classes.union compiler.units alien
byte-arrays combinators compiler.cfg.registers
compiler.cfg.instructions.syntax ;
2008-09-15 02:54:48 -04:00
IN: compiler.cfg.instructions
2008-09-10 23:11:03 -04:00
<<
SYMBOL: insn-classes
V{ } clone insn-classes set-global
>>
: new-insn ( ... class -- insn ) f swap boa ; inline
2008-09-10 23:11:03 -04:00
! Virtual CPU instructions, used by CFG and machine IRs
TUPLE: insn ;
2008-09-10 23:11:03 -04:00
! Instructions which are referentially transparent; used for
! value numbering
TUPLE: pure-insn < insn ;
2008-10-20 02:56:28 -04:00
2010-04-21 03:08:52 -04:00
! Constants
INSN: ##load-integer
def: dst/int-rep
literal: val ;
2008-10-20 02:56:28 -04:00
INSN: ##load-reference
def: dst/tagged-rep
literal: obj ;
2008-10-20 02:56:28 -04:00
! These three are inserted by representation selection
2010-04-21 03:08:52 -04:00
INSN: ##load-tagged
def: dst/tagged-rep
literal: val ;
INSN: ##load-double
def: dst/double-rep
literal: val ;
INSN: ##load-vector
def: dst
literal: val rep ;
2010-04-21 03:08:52 -04:00
! Stack operations
INSN: ##peek
def: dst/tagged-rep
literal: loc ;
2008-10-20 02:56:28 -04:00
INSN: ##replace
use: src/tagged-rep
literal: loc ;
2008-09-10 23:11:03 -04:00
INSN: ##inc-d
literal: n ;
2008-10-20 02:56:28 -04:00
INSN: ##inc-r
literal: n ;
2008-10-20 02:56:28 -04:00
! Subroutine calls
INSN: ##call
literal: word ;
2008-10-20 02:56:28 -04:00
INSN: ##jump
literal: word ;
2008-09-10 23:11:03 -04:00
2008-10-07 17:13:29 -04:00
INSN: ##return ;
! Dummy instruction that simply inhibits TCO
INSN: ##no-tco ;
2008-09-10 23:11:03 -04:00
! Jump tables
INSN: ##dispatch
2010-04-21 03:08:52 -04:00
use: src/int-rep
temp: temp/int-rep ;
2008-09-10 23:11:03 -04:00
2008-10-20 02:56:28 -04:00
! Slot access
INSN: ##slot
def: dst/tagged-rep
use: obj/tagged-rep slot/int-rep
literal: scale tag ;
INSN: ##slot-imm
def: dst/tagged-rep
use: obj/tagged-rep
literal: slot tag ;
INSN: ##set-slot
use: src/tagged-rep obj/tagged-rep slot/int-rep
literal: scale tag ;
INSN: ##set-slot-imm
use: src/tagged-rep obj/tagged-rep
literal: slot tag ;
2008-10-20 02:56:28 -04:00
2010-04-21 03:08:52 -04:00
! Register transfers
INSN: ##copy
def: dst
use: src
literal: rep ;
2010-04-21 03:08:52 -04:00
PURE-INSN: ##tagged>integer
def: dst/int-rep
use: src/tagged-rep ;
2008-10-20 02:56:28 -04:00
! Integer arithmetic
PURE-INSN: ##add
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##add-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##sub
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##sub-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##mul
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##mul-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##and
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##and-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##or
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##or-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##xor
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##xor-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##shl
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##shl-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##shr
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##shr-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##sar
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##sar-imm
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep
literal: src2 ;
PURE-INSN: ##min
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##max
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src1/int-rep src2/int-rep ;
PURE-INSN: ##not
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src/int-rep ;
2009-09-28 18:31:34 -04:00
PURE-INSN: ##neg
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src/int-rep ;
2009-09-28 18:31:34 -04:00
PURE-INSN: ##log2
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src/int-rep ;
2008-10-20 02:56:28 -04:00
! Float arithmetic
PURE-INSN: ##add-float
def: dst/double-rep
use: src1/double-rep src2/double-rep ;
PURE-INSN: ##sub-float
def: dst/double-rep
use: src1/double-rep src2/double-rep ;
PURE-INSN: ##mul-float
def: dst/double-rep
use: src1/double-rep src2/double-rep ;
PURE-INSN: ##div-float
def: dst/double-rep
use: src1/double-rep src2/double-rep ;
PURE-INSN: ##min-float
def: dst/double-rep
use: src1/double-rep src2/double-rep ;
PURE-INSN: ##max-float
def: dst/double-rep
use: src1/double-rep src2/double-rep ;
PURE-INSN: ##sqrt
def: dst/double-rep
use: src/double-rep ;
2008-10-20 02:56:28 -04:00
! libc intrinsics
PURE-INSN: ##unary-float-function
def: dst/double-rep
use: src/double-rep
literal: func ;
PURE-INSN: ##binary-float-function
def: dst/double-rep
use: src1/double-rep src2/double-rep
literal: func ;
! Single/double float conversion
PURE-INSN: ##single>double-float
def: dst/double-rep
use: src/float-rep ;
PURE-INSN: ##double>single-float
def: dst/float-rep
use: src/double-rep ;
2008-10-20 02:56:28 -04:00
! Float/integer conversion
PURE-INSN: ##float>integer
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src/double-rep ;
PURE-INSN: ##integer>float
def: dst/double-rep
2010-04-21 03:08:52 -04:00
use: src/int-rep ;
2008-10-20 02:56:28 -04:00
! SIMD operations
2009-09-28 18:31:34 -04:00
PURE-INSN: ##zero-vector
def: dst
literal: rep ;
2009-10-02 21:04:28 -04:00
PURE-INSN: ##fill-vector
def: dst
literal: rep ;
PURE-INSN: ##gather-vector-2
def: dst
use: src1/scalar-rep src2/scalar-rep
literal: rep ;
PURE-INSN: ##gather-vector-4
def: dst
use: src1/scalar-rep src2/scalar-rep src3/scalar-rep src4/scalar-rep
literal: rep ;
2009-09-28 18:31:34 -04:00
PURE-INSN: ##shuffle-vector
def: dst
use: src shuffle
literal: rep ;
PURE-INSN: ##shuffle-vector-imm
def: dst
2009-09-28 18:31:34 -04:00
use: src
literal: shuffle rep ;
PURE-INSN: ##tail>head-vector
def: dst
use: src
literal: rep ;
PURE-INSN: ##merge-vector-head
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##merge-vector-tail
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##signed-pack-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##unsigned-pack-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##unpack-vector-head
def: dst
use: src
literal: rep ;
PURE-INSN: ##unpack-vector-tail
def: dst
use: src
literal: rep ;
PURE-INSN: ##integer>float-vector
def: dst
use: src
literal: rep ;
PURE-INSN: ##float>integer-vector
def: dst
use: src
literal: rep ;
PURE-INSN: ##compare-vector
def: dst
use: src1 src2
literal: rep cc ;
PURE-INSN: ##test-vector
def: dst/tagged-rep
use: src1
temp: temp/int-rep
literal: rep vcc ;
INSN: ##test-vector-branch
use: src1
temp: temp/int-rep
literal: rep vcc ;
INSN: _test-vector-branch
literal: label
use: src1
temp: temp/int-rep
literal: rep vcc ;
PURE-INSN: ##add-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##saturated-add-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##add-sub-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##sub-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##saturated-sub-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##mul-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##mul-high-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##mul-horizontal-add-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##saturated-mul-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##div-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##min-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##max-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##avg-vector
def: dst
use: src1 src2
literal: rep ;
2009-09-28 18:31:34 -04:00
PURE-INSN: ##dot-vector
def: dst/scalar-rep
use: src1 src2
literal: rep ;
PURE-INSN: ##sad-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##horizontal-add-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##horizontal-sub-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##horizontal-shl-vector-imm
def: dst
use: src1
literal: src2 rep ;
PURE-INSN: ##horizontal-shr-vector-imm
def: dst
use: src1
literal: src2 rep ;
PURE-INSN: ##abs-vector
def: dst
use: src
literal: rep ;
PURE-INSN: ##sqrt-vector
def: dst
use: src
literal: rep ;
PURE-INSN: ##and-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##andn-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##or-vector
def: dst
use: src1 src2
literal: rep ;
PURE-INSN: ##xor-vector
def: dst
use: src1 src2
literal: rep ;
2009-10-02 21:04:28 -04:00
PURE-INSN: ##not-vector
def: dst
use: src
literal: rep ;
PURE-INSN: ##shl-vector-imm
def: dst
use: src1
literal: src2 rep ;
PURE-INSN: ##shr-vector-imm
def: dst
use: src1
literal: src2 rep ;
PURE-INSN: ##shl-vector
def: dst
use: src1 src2/int-scalar-rep
literal: rep ;
PURE-INSN: ##shr-vector
def: dst
use: src1 src2/int-scalar-rep
literal: rep ;
! Scalar/vector conversion
PURE-INSN: ##scalar>integer
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src
literal: rep ;
PURE-INSN: ##integer>scalar
def: dst
2010-04-21 03:08:52 -04:00
use: src/int-rep
literal: rep ;
PURE-INSN: ##vector>scalar
def: dst/scalar-rep
use: src
literal: rep ;
PURE-INSN: ##scalar>vector
def: dst
use: src/scalar-rep
literal: rep ;
! Boxing and unboxing aliens
PURE-INSN: ##box-alien
def: dst/tagged-rep
2010-04-21 03:08:52 -04:00
use: src/int-rep
temp: temp/int-rep ;
PURE-INSN: ##box-displaced-alien
def: dst/tagged-rep
2010-04-22 18:02:56 -04:00
use: displacement/int-rep base/tagged-rep
temp: temp/int-rep
literal: base-class ;
PURE-INSN: ##unbox-any-c-ptr
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src/tagged-rep ;
PURE-INSN: ##unbox-alien
2010-04-21 03:08:52 -04:00
def: dst/int-rep
use: src/tagged-rep ;
! Raw memory accessors
INSN: ##load-memory
def: dst
use: base/int-rep displacement/int-rep
literal: scale offset rep c-type ;
INSN: ##load-memory-imm
def: dst
use: base/int-rep
literal: offset rep c-type ;
INSN: ##store-memory
use: src base/int-rep displacement/int-rep
literal: scale offset rep c-type ;
INSN: ##store-memory-imm
use: src base/int-rep
literal: offset rep c-type ;
! Memory allocation
INSN: ##allot
def: dst/tagged-rep
literal: size class
temp: temp/int-rep ;
INSN: ##write-barrier
2010-04-21 03:08:52 -04:00
use: src/tagged-rep slot/int-rep
literal: scale tag
temp: temp1/int-rep temp2/int-rep ;
INSN: ##write-barrier-imm
use: src/tagged-rep
literal: slot tag
temp: temp1/int-rep temp2/int-rep ;
INSN: ##alien-global
2010-04-21 03:08:52 -04:00
def: dst/int-rep
literal: symbol library ;
INSN: ##vm-field
def: dst/tagged-rep
literal: offset ;
INSN: ##set-vm-field
2010-04-22 18:02:56 -04:00
use: src/tagged-rep
literal: offset ;
2008-09-10 23:11:03 -04:00
! FFI
INSN: ##alien-invoke
literal: params stack-frame ;
INSN: ##alien-indirect
literal: params stack-frame ;
INSN: ##alien-assembly
literal: params stack-frame ;
INSN: ##alien-callback
literal: params stack-frame ;
2008-09-17 01:46:38 -04:00
! Instructions used by CFG IR only.
INSN: ##prologue ;
INSN: ##epilogue ;
2008-09-15 02:54:48 -04:00
2008-09-17 01:46:38 -04:00
INSN: ##branch ;
2008-09-15 02:54:48 -04:00
INSN: ##phi
def: dst
literal: inputs ;
2010-04-22 04:21:23 -04:00
! Tagged conditionals
INSN: ##compare-branch
use: src1/tagged-rep src2/tagged-rep
literal: cc ;
INSN: ##compare-imm-branch
use: src1/tagged-rep
literal: src2 cc ;
PURE-INSN: ##compare
def: dst/tagged-rep
use: src1/tagged-rep src2/tagged-rep
literal: cc
temp: temp/int-rep ;
PURE-INSN: ##compare-imm
def: dst/tagged-rep
use: src1/tagged-rep
literal: src2 cc
2010-04-22 04:21:23 -04:00
temp: temp/int-rep ;
! Integer conditionals
INSN: ##compare-integer-branch
use: src1/int-rep src2/int-rep
literal: cc ;
INSN: ##compare-integer-imm-branch
use: src1/int-rep
literal: src2 cc ;
2010-04-22 04:21:23 -04:00
PURE-INSN: ##compare-integer
def: dst/tagged-rep
use: src1/int-rep src2/int-rep
literal: cc
temp: temp/int-rep ;
2010-04-22 04:21:23 -04:00
PURE-INSN: ##compare-integer-imm
def: dst/tagged-rep
use: src1/int-rep
literal: src2 cc
2010-04-22 04:21:23 -04:00
temp: temp/int-rep ;
! Float conditionals
INSN: ##compare-float-ordered-branch
use: src1/double-rep src2/double-rep
literal: cc ;
INSN: ##compare-float-unordered-branch
use: src1/double-rep src2/double-rep
literal: cc ;
PURE-INSN: ##compare-float-ordered
def: dst/tagged-rep
use: src1/double-rep src2/double-rep
literal: cc
temp: temp/int-rep ;
PURE-INSN: ##compare-float-unordered
def: dst/tagged-rep
use: src1/double-rep src2/double-rep
literal: cc
temp: temp/int-rep ;
2008-10-20 02:56:28 -04:00
! Overflowing arithmetic
INSN: ##fixnum-add
def: dst/tagged-rep
2010-04-27 10:51:00 -04:00
use: src1/tagged-rep src2/tagged-rep
literal: cc ;
INSN: ##fixnum-sub
def: dst/tagged-rep
2010-04-27 10:51:00 -04:00
use: src1/tagged-rep src2/tagged-rep
literal: cc ;
2008-09-11 03:05:22 -04:00
INSN: ##fixnum-mul
def: dst/tagged-rep
2010-04-27 10:51:00 -04:00
use: src1/tagged-rep src2/int-rep
literal: cc ;
2009-05-31 19:21:11 -04:00
INSN: ##save-context
temp: temp1/int-rep temp2/int-rep ;
2010-04-27 10:51:00 -04:00
! GC checks
INSN: ##check-nursery-branch
literal: size cc
temp: temp1/int-rep temp2/int-rep ;
INSN: ##call-gc
literal: gc-roots ;
2010-04-28 03:35:46 -04:00
! Spills and reloads, inserted by register allocator
TUPLE: spill-slot { n integer } ;
C: <spill-slot> spill-slot
INSN: ##spill
use: src
literal: rep dst ;
INSN: ##reload
def: dst
literal: rep src ;
2008-09-11 03:05:22 -04:00
! Instructions used by machine IR only.
2010-04-28 03:35:46 -04:00
INSN: _spill-area-size
literal: n ;
INSN: _prologue
literal: stack-frame ;
INSN: _epilogue
literal: stack-frame ;
2008-09-11 03:05:22 -04:00
INSN: _label
literal: label ;
INSN: _branch
literal: label ;
2008-09-11 03:05:22 -04:00
INSN: _loop-entry ;
2008-09-17 01:46:38 -04:00
INSN: _dispatch-label
literal: label ;
2010-04-27 10:51:00 -04:00
INSN: _conditional-branch
literal: label insn ;
UNION: ##allocation
##allot
##box-alien
##box-displaced-alien ;
2010-04-27 10:51:00 -04:00
UNION: conditional-branch-insn
##compare-branch
##compare-imm-branch
##compare-integer-branch
##compare-integer-imm-branch
##compare-float-ordered-branch
##compare-float-unordered-branch
##test-vector-branch
##check-nursery-branch
##fixnum-add
##fixnum-sub
##fixnum-mul ;
! For alias analysis
UNION: ##read ##slot ##slot-imm ##vm-field ##alien-global ;
UNION: ##write ##set-slot ##set-slot-imm ##set-vm-field ;
2010-04-27 10:51:00 -04:00
! Instructions that clobber registers
UNION: clobber-insn
##call-gc
##unary-float-function
##binary-float-function ;
! Instructions that kill all live vregs
UNION: kill-vreg-insn
##call
##prologue
##epilogue
##alien-invoke
##alien-indirect
##alien-callback ;
! Instructions that have complex expansions and require that the
! output registers are not equal to any of the input registers
UNION: def-is-use-insn
##box-alien
##box-displaced-alien
##unbox-any-c-ptr ;
SYMBOL: vreg-insn
[
vreg-insn
insn-classes get [
"insn-slots" word-prop [ type>> { def use temp } member-eq? ] any?
] filter
define-union-class
] with-compilation-unit