]> gitweb.factorcode.org Git - factor.git/blob - unfinished/compiler/vops.bluesky/vops.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / unfinished / compiler / vops.bluesky / vops.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: parser prettyprint.backend kernel accessors math
4 math.order sequences namespaces arrays assocs ;
5 IN: compiler.vops
6
7 TUPLE: vreg n ;
8
9 : VREG: scan-word vreg boa parsed ; parsing
10
11 M: vreg pprint* \ VREG: pprint-word n>> pprint* ;
12
13 SYMBOL: vreg-counter
14
15 : init-counter ( -- )
16     { 0 } clone vreg-counter set ;
17
18 : next-vreg ( -- n )
19     0 vreg-counter get [ dup 1+ ] change-nth vreg boa ;
20
21 : emit ( ... class -- ) boa , ; inline
22
23 ! ! ! Instructions. Those prefixed with %% are high level
24 ! ! ! instructions eliminated during the elaboration phase.
25 TUPLE: vop ;
26
27 ! Instruction which does not touch vregs.
28 TUPLE: nullary-op < vop ;
29
30 ! Does nothing
31 TUPLE: nop < nullary-op ;
32
33 : nop ( -- vop ) T{ nop } ;
34
35 : ?nop ( vop ? -- vop/nop ) [ drop nop ] unless ;
36
37 ! Instruction with no side effects; if 'out' is never read, we
38 ! can eliminate it.
39 TUPLE: flushable-op < vop out ;
40
41 ! Instruction which is referentially transparent; we can replace
42 ! repeated computation with a reference to a previous value
43 TUPLE: pure-op < flushable-op ;
44
45 ! Instruction only used for its side effect, produces no values
46 TUPLE: effect-op < vop in ;
47
48 TUPLE: binary-op < pure-op in1 in2 ;
49
50 : inputs ( insn -- in1 in2 ) [ in1>> ] [ in2>> ] bi ; inline
51
52 : in/out ( insn -- in out ) [ in>> ] [ out>> ] bi ; inline
53
54 TUPLE: unary-op < pure-op in ;
55
56 ! Merge point; out is a sequence of vregs in a sequence of
57 ! sequences of vregs
58 TUPLE: %phi < pure-op in ;
59
60 ! Integer, floating point, condition register copy
61 TUPLE: %copy < unary-op ;
62
63 ! Constants
64 TUPLE: constant-op < pure-op value ;
65
66 TUPLE: %iconst < constant-op ; ! Integer
67 TUPLE: %fconst < constant-op ; ! Float
68 TUPLE: %cconst < constant-op ; ! Comparison result, +lt+ +eq+ +gt+
69
70 ! Load address of literal table into out
71 TUPLE: %literal-table < pure-op ;
72
73 ! Load object literal from table.
74 TUPLE: %literal < unary-op object ;
75
76 ! Read/write ops: candidates for alias analysis
77 TUPLE: read-op < flushable-op ;
78 TUPLE: write-op < effect-op ;
79
80 ! Stack shuffling
81 SINGLETON: %data
82 SINGLETON: %retain
83
84 TUPLE: %peek < read-op n stack ;
85 TUPLE: %replace < write-op n stack ;
86 TUPLE: %height < nullary-op n stack ;
87
88 : stack-loc ( insn -- pair ) [ n>> ] [ stack>> ] bi 2array ;
89
90 TUPLE: commutative-op < binary-op ;
91
92 ! Integer arithmetic
93 TUPLE: %iadd < commutative-op ;
94 TUPLE: %isub < binary-op ;
95 TUPLE: %imul < commutative-op ;
96 TUPLE: %idiv < binary-op ;
97 TUPLE: %imod < binary-op ;
98 TUPLE: %icmp < binary-op ;
99
100 ! Bitwise ops
101 TUPLE: %not < unary-op ;
102 TUPLE: %and < commutative-op ;
103 TUPLE: %or  < commutative-op ;
104 TUPLE: %xor < commutative-op ;
105 TUPLE: %shl < binary-op ;
106 TUPLE: %shr < binary-op ;
107 TUPLE: %sar < binary-op ;
108
109 ! Float arithmetic
110 TUPLE: %fadd < commutative-op ;
111 TUPLE: %fsub < binary-op ;
112 TUPLE: %fmul < commutative-op ;
113 TUPLE: %fdiv < binary-op ;
114 TUPLE: %fcmp < binary-op ;
115
116 ! Float/integer conversion
117 TUPLE: %f>i < unary-op ;
118 TUPLE: %i>f < unary-op ;
119
120 ! Float boxing/unboxing
121 TUPLE: %%box-float < unary-op ;
122 TUPLE: %%unbox-float < unary-op ;
123
124 ! High level slot accessors for alias analysis
125 ! tag is f; if its not f, we can generate a faster sequence
126 TUPLE: %%slot < read-op obj slot tag ;
127 TUPLE: %%set-slot < write-op obj slot tag ;
128
129 TUPLE: %write-barrier < effect-op ;
130
131 ! Memory
132 TUPLE: %load < unary-op ;
133 TUPLE: %store < effect-op addr ;
134
135 ! Control flow; they jump to either the first or second successor
136 ! of the BB
137
138 ! Unconditional transfer to first successor
139 TUPLE: %b < nullary-op ;
140
141 SYMBOL: cc<
142 SYMBOL: cc<=
143 SYMBOL: cc=
144 SYMBOL: cc>
145 SYMBOL: cc>=
146 SYMBOL: cc/=
147
148 : evaluate-cc ( result cc -- ? )
149     H{
150         { cc<  { +lt+           } }
151         { cc<= { +lt+ +eq+      } }
152         { cc=  {      +eq+      } }
153         { cc>= {      +eq+ +gt+ } }
154         { cc>  {           +gt+ } }
155         { cc/= { +lt+      +gt+ } }
156     } at memq? ;
157
158 TUPLE: cond-branch < effect-op code ;
159
160 TUPLE: %bi < cond-branch ;
161 TUPLE: %bf < cond-branch ;
162
163 ! Convert condition register to a boolean
164 TUPLE: boolean-op < unary-op code ;
165
166 TUPLE: %%iboolean < boolean-op ;
167 TUPLE: %%fboolean < boolean-op ;
168
169 ! Dispatch table, jumps to successor 0..n-1 depending value of
170 ! in, which must be in the range [0,n)
171 TUPLE: %dispatch < effect-op ;
172
173 ! Procedures
174 TUPLE: %return < nullary-op ;
175 TUPLE: %prolog < nullary-op ;
176 TUPLE: %epilog < nullary-op ;
177 TUPLE: %jump < nullary-op word ;
178 TUPLE: %call < nullary-op word ;
179
180 ! Heap allocation
181 TUPLE: %%allot < flushable-op size tag type ;