factor/library/compiler/vops.factor

379 lines
9.5 KiB
Factor
Raw Normal View History

2005-05-06 18:33:40 -04:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-05-09 02:34:15 -04:00
IN: compiler-backend
2005-12-04 22:06:12 -05:00
USING: arrays errors generic hashtables kernel kernel-internals
lists math memory namespaces parser sequences words ;
2005-05-06 18:33:40 -04:00
! The linear IR is the second of the two intermediate
! representations used by Factor. It is basically a high-level
! assembly language. Linear IR operations are called VOPs.
2005-05-08 20:30:38 -04:00
! This file defines all the types of VOPs. A linear IR program
! is then just a list of VOPs.
2005-05-09 02:34:15 -04:00
: <label> ( -- label )
#! Make a label.
gensym dup t "label" set-word-prop ;
: label? ( obj -- ? )
2005-09-24 15:21:17 -04:00
dup word? [ "label" word-prop ] [ drop f ] if ;
2005-05-09 02:34:15 -04:00
2005-05-06 18:33:40 -04:00
! A virtual register
TUPLE: vreg n ;
! Register classes
TUPLE: int-regs ;
2005-06-14 20:54:11 -04:00
TUPLE: float-regs size ;
GENERIC: fastcall-regs ( register-class -- n )
GENERIC: reg-class-size ( register-class -- n )
M: float-regs reg-class-size float-regs-size ;
2005-09-08 22:23:54 -04:00
! A data stack location.
TUPLE: ds-loc n ;
! A call stack location.
TUPLE: cs-loc n ;
! A pseudo-register class for parameters spilled on the stack
TUPLE: stack-params ;
2005-12-04 19:56:42 -05:00
GENERIC: v>operand
M: integer v>operand tag-bits shift ;
2005-12-04 22:06:12 -05:00
M: vreg v>operand vreg-n vregs nth ;
2005-12-04 19:56:42 -05:00
M: f v>operand address ;
2005-05-06 18:33:40 -04:00
! A virtual operation
2005-05-16 17:01:39 -04:00
TUPLE: vop inputs outputs label ;
2005-12-04 22:06:12 -05:00
: (scratch)
vop get dup vop-inputs swap vop-outputs append
[ vreg? ] subset [ v>operand ] map vregs diff ;
: scratch ( n -- reg )
#! Output a scratch register that is not used by the
#! current VOP.
\ scratch get nth ;
: with-vop ( vop quot -- )
[
swap vop set (scratch) \ scratch set call
] with-scope ; inline
2005-12-04 19:56:42 -05:00
: input ( n -- obj ) vop get vop-inputs nth ;
: input-operand ( n -- n ) input v>operand ;
: output ( n -- obj ) vop get vop-outputs nth ;
: output-operand ( n -- n ) output v>operand ;
: label ( -- label ) vop get vop-label ;
2005-05-06 18:33:40 -04:00
2005-05-16 17:01:39 -04:00
GENERIC: basic-block? ( vop -- ? )
M: vop basic-block? drop f ;
2005-12-07 21:46:54 -05:00
2005-05-16 17:01:39 -04:00
! simplifies some code
M: f basic-block? drop f ;
2005-05-09 02:34:15 -04:00
! Only on PowerPC. The %parameters node needs to reserve space
! in the stack frame.
GENERIC: stack-reserve
M: vop stack-reserve drop 0 ;
2005-05-16 17:01:39 -04:00
: make-vop ( inputs outputs label vop -- vop )
2005-05-06 18:33:40 -04:00
[ >r <vop> r> set-delegate ] keep ;
2005-05-16 17:01:39 -04:00
: empty-vop f f f ;
: label-vop ( label) >r f f r> ;
: label/src-vop ( label src) 1array swap f swap ;
: src-vop ( src) 1array f f ;
: dest-vop ( dest) 1array dup f ;
: src/dest-vop ( src dest) >r 1array r> 1array f ;
: 2-in-vop ( in1 in2) 2array f f ;
: 3-in-vop ( in1 in2 in3) 3array f f ;
: 2-in/label-vop ( in1 in2 label) >r 2array f r> ;
: 2-vop ( in dest) [ 2array ] keep 1array f ;
: 3-vop ( in1 in2 dest) >r 2array r> 1array f ;
2005-05-06 18:33:40 -04:00
! miscellanea
TUPLE: %prologue ;
C: %prologue make-vop ;
2005-05-06 18:33:40 -04:00
: %prologue empty-vop <%prologue> ;
2005-05-16 17:01:39 -04:00
TUPLE: %label ;
C: %label make-vop ;
2005-05-06 18:33:40 -04:00
: %label label-vop <%label> ;
2005-05-09 02:34:15 -04:00
! Return vops take a label that is ignored, to have the
! same stack effect as jumps. This is needed for the
! simplifier.
TUPLE: %return ;
C: %return make-vop ;
2005-09-10 00:55:46 -04:00
: %return empty-vop <%return> ;
2005-05-09 02:34:15 -04:00
TUPLE: %return-to ;
C: %return-to make-vop ;
2005-05-06 18:33:40 -04:00
: %return-to label-vop <%return-to> ;
2005-05-16 17:01:39 -04:00
TUPLE: %jump ;
C: %jump make-vop ;
2005-05-06 18:33:40 -04:00
: %jump label-vop <%jump> ;
2005-05-16 17:01:39 -04:00
TUPLE: %jump-label ;
C: %jump-label make-vop ;
2005-05-06 18:33:40 -04:00
: %jump-label label-vop <%jump-label> ;
2005-05-16 17:01:39 -04:00
TUPLE: %call ;
C: %call make-vop ;
2005-05-06 18:33:40 -04:00
: %call label-vop <%call> ;
2005-05-16 17:01:39 -04:00
TUPLE: %call-label ;
C: %call-label make-vop ;
2005-05-06 18:33:40 -04:00
: %call-label label-vop <%call-label> ;
2005-05-16 17:01:39 -04:00
TUPLE: %jump-t ;
C: %jump-t make-vop ;
2005-05-06 18:33:40 -04:00
: %jump-t <vreg> label/src-vop <%jump-t> ;
2005-05-16 17:01:39 -04:00
2005-05-06 18:33:40 -04:00
! dispatch tables
TUPLE: %dispatch ;
C: %dispatch make-vop ;
: %dispatch <vreg> src-vop <%dispatch> ;
2005-05-16 17:01:39 -04:00
TUPLE: %target-label ;
C: %target-label make-vop ;
2005-05-06 18:33:40 -04:00
: %target-label label-vop <%target-label> ;
2005-05-16 17:01:39 -04:00
2005-05-06 18:33:40 -04:00
! stack operations
TUPLE: %peek ;
C: %peek make-vop ;
M: %peek basic-block? drop t ;
2005-09-08 22:23:54 -04:00
: %peek-d ( vreg n -- vop )
<ds-loc> swap <vreg> src/dest-vop <%peek> ;
: %peek-r ( vreg n -- vop )
<cs-loc> swap <vreg> src/dest-vop <%peek> ;
2005-09-08 22:23:54 -04:00
TUPLE: %replace ;
C: %replace make-vop ;
2005-05-16 17:01:39 -04:00
M: %replace basic-block? drop t ;
2005-09-08 22:23:54 -04:00
: %replace-d ( vreg n -- vop )
<ds-loc> src/dest-vop <%replace> ;
2005-09-08 22:23:54 -04:00
: %replace-r ( vreg n -- vop )
<cs-loc> src/dest-vop <%replace> ;
2005-05-16 17:01:39 -04:00
TUPLE: %inc-d ;
C: %inc-d make-vop ;
2005-09-04 17:07:59 -04:00
: %inc-d ( n -- node ) src-vop <%inc-d> ;
2005-05-16 17:01:39 -04:00
2005-09-08 22:23:54 -04:00
M: %inc-d basic-block? drop t ;
2005-09-04 17:07:59 -04:00
TUPLE: %inc-r ;
2005-09-08 22:23:54 -04:00
C: %inc-r make-vop ;
2005-09-04 17:07:59 -04:00
2005-05-16 17:01:39 -04:00
: %inc-r ( n -- ) src-vop <%inc-r> ;
2005-09-08 22:23:54 -04:00
M: %inc-r basic-block? drop t ;
TUPLE: %immediate ;
C: %immediate make-vop ;
: %immediate ( vreg obj -- vop )
swap <vreg> src/dest-vop <%immediate> ;
M: %immediate basic-block? drop t ;
2005-05-06 18:33:40 -04:00
! indirect load of a literal through a table
TUPLE: %indirect ;
C: %indirect make-vop ;
2005-05-16 17:01:39 -04:00
: %indirect ( vreg obj -- )
swap <vreg> src/dest-vop <%indirect> ;
M: %indirect basic-block? drop t ;
2005-05-06 18:33:40 -04:00
! object slot accessors
TUPLE: %untag ;
C: %untag make-vop ;
: %untag <vreg> dest-vop <%untag> ;
2005-05-16 17:01:39 -04:00
M: %untag basic-block? drop t ;
: slot-vop [ <vreg> ] 2apply 2-vop ;
TUPLE: %slot ;
C: %slot make-vop ;
: %slot ( n vreg ) slot-vop <%slot> ;
2005-05-16 17:01:39 -04:00
M: %slot basic-block? drop t ;
2005-05-06 18:33:40 -04:00
: set-slot-vop
2005-12-04 22:55:02 -05:00
rot <vreg> rot <vreg> rot <vreg> dup >r 3array r> 1array f ;
TUPLE: %set-slot ;
C: %set-slot make-vop ;
2005-09-09 00:17:19 -04:00
2005-05-16 17:01:39 -04:00
: %set-slot ( value obj n )
2005-09-09 00:17:19 -04:00
#! %set-slot writes to vreg obj.
set-slot-vop <%set-slot> ;
2005-09-09 00:17:19 -04:00
2005-05-16 17:01:39 -04:00
M: %set-slot basic-block? drop t ;
2005-05-06 18:33:40 -04:00
! in the 'fast' versions, the object's type and slot number is
2005-05-06 18:33:40 -04:00
! known at compile time, so these become a single instruction
TUPLE: %fast-slot ;
C: %fast-slot make-vop ;
2005-05-16 17:01:39 -04:00
: %fast-slot ( vreg n )
swap <vreg> 2-vop <%fast-slot> ;
2005-05-16 17:01:39 -04:00
M: %fast-slot basic-block? drop t ;
TUPLE: %fast-set-slot ;
C: %fast-set-slot make-vop ;
2005-05-16 17:01:39 -04:00
: %fast-set-slot ( value obj n )
#! %fast-set-slot writes to vreg obj.
>r >r <vreg> r> <vreg> r> over >r 3array r> 1array f
2005-05-16 17:01:39 -04:00
<%fast-set-slot> ;
M: %fast-set-slot basic-block? drop t ;
! Char readers and writers
TUPLE: %char-slot ;
C: %char-slot make-vop ;
: %char-slot ( n vreg ) slot-vop <%char-slot> ;
M: %char-slot basic-block? drop t ;
TUPLE: %set-char-slot ;
C: %set-char-slot make-vop ;
: %set-char-slot ( value ch n )
#! %set-char-slot writes to vreg obj.
set-slot-vop <%set-char-slot> ;
M: %set-char-slot basic-block? drop t ;
TUPLE: %write-barrier ;
C: %write-barrier make-vop ;
2005-08-22 15:33:18 -04:00
: %write-barrier ( ptr ) <vreg> dest-vop <%write-barrier> ;
! fixnum intrinsics
TUPLE: %fixnum+ ;
C: %fixnum+ make-vop ; : %fixnum+ 3-vop <%fixnum+> ;
TUPLE: %fixnum- ;
C: %fixnum- make-vop ; : %fixnum- 3-vop <%fixnum-> ;
TUPLE: %fixnum* ;
C: %fixnum* make-vop ; : %fixnum* 3-vop <%fixnum*> ;
TUPLE: %fixnum-mod ;
C: %fixnum-mod make-vop ; : %fixnum-mod 3-vop <%fixnum-mod> ;
TUPLE: %fixnum/i ;
C: %fixnum/i make-vop ; : %fixnum/i 3-vop <%fixnum/i> ;
TUPLE: %fixnum/mod ;
C: %fixnum/mod make-vop ; : %fixnum/mod f <%fixnum/mod> ;
2005-09-08 22:23:54 -04:00
TUPLE: %fixnum-bitand ;
C: %fixnum-bitand make-vop ; : %fixnum-bitand 3-vop <%fixnum-bitand> ;
2005-09-08 22:23:54 -04:00
M: %fixnum-bitand basic-block? drop t ;
TUPLE: %fixnum-bitor ;
C: %fixnum-bitor make-vop ; : %fixnum-bitor 3-vop <%fixnum-bitor> ;
2005-09-08 22:23:54 -04:00
M: %fixnum-bitor basic-block? drop t ;
TUPLE: %fixnum-bitxor ;
C: %fixnum-bitxor make-vop ; : %fixnum-bitxor 3-vop <%fixnum-bitxor> ;
2005-09-08 22:23:54 -04:00
M: %fixnum-bitxor basic-block? drop t ;
TUPLE: %fixnum-bitnot ;
C: %fixnum-bitnot make-vop ; : %fixnum-bitnot 2-vop <%fixnum-bitnot> ;
2005-09-08 22:23:54 -04:00
M: %fixnum-bitnot basic-block? drop t ;
2005-05-09 22:34:47 -04:00
! At the VOP level, the 'shift' operation is split into five
! distinct operations:
! - shifts with a large positive count: calls runtime to make
! a bignum
! - shifts with a small positive count: %fixnum<<
! - shifts with a small negative count: %fixnum>>
! - shifts with a small negative count: %fixnum>>
! - shifts with a large negative count: %fixnum-sgn
TUPLE: %fixnum<< ;
C: %fixnum<< make-vop ; : %fixnum<< 3-vop <%fixnum<<> ;
2005-09-08 22:23:54 -04:00
TUPLE: %fixnum>> ;
C: %fixnum>> make-vop ; : %fixnum>> 3-vop <%fixnum>>> ;
2005-09-08 22:23:54 -04:00
M: %fixnum>> basic-block? drop t ;
2005-05-09 22:34:47 -04:00
! due to x86 limitations the destination of this VOP must be
! vreg 2 (EDX), and the source must be vreg 0 (EAX).
TUPLE: %fixnum-sgn ;
C: %fixnum-sgn make-vop ; : %fixnum-sgn src/dest-vop <%fixnum-sgn> ;
2005-09-08 22:23:54 -04:00
M: %fixnum-sgn basic-block? drop t ;
2005-05-09 22:34:47 -04:00
! Integer comparison followed by a conditional branch is
! optimized
TUPLE: %jump-fixnum<= ;
C: %jump-fixnum<= make-vop ;
2005-05-16 17:01:39 -04:00
: %jump-fixnum<= 2-in/label-vop <%jump-fixnum<=> ;
TUPLE: %jump-fixnum< ;
C: %jump-fixnum< make-vop ;
2005-05-16 17:01:39 -04:00
: %jump-fixnum< 2-in/label-vop <%jump-fixnum<> ;
TUPLE: %jump-fixnum>= ;
C: %jump-fixnum>= make-vop ;
2005-05-16 17:01:39 -04:00
: %jump-fixnum>= 2-in/label-vop <%jump-fixnum>=> ;
TUPLE: %jump-fixnum> ;
C: %jump-fixnum> make-vop ;
2005-05-16 17:01:39 -04:00
: %jump-fixnum> 2-in/label-vop <%jump-fixnum>> ;
TUPLE: %jump-eq? ;
C: %jump-eq? make-vop ;
2005-05-16 17:01:39 -04:00
: %jump-eq? 2-in/label-vop <%jump-eq?> ;
2005-05-09 02:34:15 -04:00
! some slightly optimized inline assembly
TUPLE: %type ;
C: %type make-vop ;
: %type ( vreg ) <vreg> dest-vop <%type> ;
TUPLE: %tag ;
C: %tag make-vop ;
: %tag ( vreg ) <vreg> dest-vop <%tag> ;
M: %tag basic-block? drop t ;
TUPLE: %getenv ;
C: %getenv make-vop ;
2005-05-16 17:01:39 -04:00
: %getenv swap src/dest-vop <%getenv> ;
M: %getenv basic-block? drop t ;
TUPLE: %setenv ;
C: %setenv make-vop ;
2005-05-16 17:01:39 -04:00
: %setenv 2-in-vop <%setenv> ;
2005-05-08 20:30:38 -04:00
! alien operations
TUPLE: %parameters ;
C: %parameters make-vop ;
2005-12-08 18:14:49 -05:00
M: %parameters stack-reserve vop-inputs first ;
2005-05-16 17:01:39 -04:00
: %parameters ( n -- vop ) src-vop <%parameters> ;
2005-05-08 20:30:38 -04:00
TUPLE: %parameter ;
C: %parameter make-vop ;
: %parameter ( n reg reg-class -- vop ) 3-in-vop <%parameter> ;
2005-05-08 20:30:38 -04:00
TUPLE: %cleanup ;
C: %cleanup make-vop ;
2005-05-16 17:01:39 -04:00
: %cleanup ( n -- vop ) src-vop <%cleanup> ;
2005-05-08 20:30:38 -04:00
TUPLE: %unbox ;
C: %unbox make-vop ;
: %unbox ( n func reg-class -- vop ) 3-in-vop <%unbox> ;
2005-05-08 20:30:38 -04:00
TUPLE: %box ;
C: %box make-vop ;
: %box ( func reg-class -- vop ) 2-in-vop <%box> ;
2005-05-08 20:30:38 -04:00
TUPLE: %alien-invoke ;
C: %alien-invoke make-vop ;
: %alien-invoke ( func lib -- vop ) 2-in-vop <%alien-invoke> ;