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 ;
9 : VREG: scan-word vreg boa parsed ; parsing
11 M: vreg pprint* \ VREG: pprint-word n>> pprint* ;
16 { 0 } clone vreg-counter set ;
19 0 vreg-counter get [ dup 1+ ] change-nth vreg boa ;
21 : emit ( ... class -- ) boa , ; inline
23 ! ! ! Instructions. Those prefixed with %% are high level
24 ! ! ! instructions eliminated during the elaboration phase.
27 ! Instruction which does not touch vregs.
28 TUPLE: nullary-op < vop ;
31 TUPLE: nop < nullary-op ;
33 : nop ( -- vop ) T{ nop } ;
35 : ?nop ( vop ? -- vop/nop ) [ drop nop ] unless ;
37 ! Instruction with no side effects; if 'out' is never read, we
39 TUPLE: flushable-op < vop out ;
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 ;
45 ! Instruction only used for its side effect, produces no values
46 TUPLE: effect-op < vop in ;
48 TUPLE: binary-op < pure-op in1 in2 ;
50 : inputs ( insn -- in1 in2 ) [ in1>> ] [ in2>> ] bi ; inline
52 : in/out ( insn -- in out ) [ in>> ] [ out>> ] bi ; inline
54 TUPLE: unary-op < pure-op in ;
56 ! Merge point; out is a sequence of vregs in a sequence of
58 TUPLE: %phi < pure-op in ;
60 ! Integer, floating point, condition register copy
61 TUPLE: %copy < unary-op ;
64 TUPLE: constant-op < pure-op value ;
66 TUPLE: %iconst < constant-op ; ! Integer
67 TUPLE: %fconst < constant-op ; ! Float
68 TUPLE: %cconst < constant-op ; ! Comparison result, +lt+ +eq+ +gt+
70 ! Load address of literal table into out
71 TUPLE: %literal-table < pure-op ;
73 ! Load object literal from table.
74 TUPLE: %literal < unary-op object ;
76 ! Read/write ops: candidates for alias analysis
77 TUPLE: read-op < flushable-op ;
78 TUPLE: write-op < effect-op ;
84 TUPLE: %peek < read-op n stack ;
85 TUPLE: %replace < write-op n stack ;
86 TUPLE: %height < nullary-op n stack ;
88 : stack-loc ( insn -- pair ) [ n>> ] [ stack>> ] bi 2array ;
90 TUPLE: commutative-op < binary-op ;
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 ;
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 ;
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 ;
116 ! Float/integer conversion
117 TUPLE: %f>i < unary-op ;
118 TUPLE: %i>f < unary-op ;
120 ! Float boxing/unboxing
121 TUPLE: %%box-float < unary-op ;
122 TUPLE: %%unbox-float < unary-op ;
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 ;
129 TUPLE: %write-barrier < effect-op ;
132 TUPLE: %load < unary-op ;
133 TUPLE: %store < effect-op addr ;
135 ! Control flow; they jump to either the first or second successor
138 ! Unconditional transfer to first successor
139 TUPLE: %b < nullary-op ;
148 : evaluate-cc ( result cc -- ? )
151 { cc<= { +lt+ +eq+ } }
153 { cc>= { +eq+ +gt+ } }
155 { cc/= { +lt+ +gt+ } }
158 TUPLE: cond-branch < effect-op code ;
160 TUPLE: %bi < cond-branch ;
161 TUPLE: %bf < cond-branch ;
163 ! Convert condition register to a boolean
164 TUPLE: boolean-op < unary-op code ;
166 TUPLE: %%iboolean < boolean-op ;
167 TUPLE: %%fboolean < boolean-op ;
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 ;
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 ;
181 TUPLE: %%allot < flushable-op size tag type ;