Mercurial > octave-nkf
annotate libinterp/interp-core/jit-ir.h @ 15231:8442131a391a
jit-ir.h (jit_use::stash_value): Explicitly specify parent template types
author | Max Brister <max@2bass.com> |
---|---|
date | Sat, 25 Aug 2012 14:04:38 -0600 |
parents | 2fc554ffbc28 |
children | 2c0259dc1a82 |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
3 Copyright (C) 2012 Max Brister <max@2bass.com> | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #if !defined (octave_jit_ir_h) | |
24 #define octave_jit_ir_h 1 | |
25 | |
26 #ifdef HAVE_LLVM | |
27 | |
28 #include <list> | |
29 #include <stack> | |
30 #include <set> | |
31 | |
32 #include "jit-typeinfo.h" | |
33 | |
34 // The low level octave jit ir | |
35 // this ir is close to llvm, but contains information for doing type inference. | |
36 // We convert the octave parse tree to this IR directly. | |
37 | |
38 #define JIT_VISIT_IR_NOTEMPLATE \ | |
39 JIT_METH(block); \ | |
40 JIT_METH(branch); \ | |
41 JIT_METH(cond_branch); \ | |
42 JIT_METH(call); \ | |
43 JIT_METH(extract_argument); \ | |
44 JIT_METH(store_argument); \ | |
45 JIT_METH(phi); \ | |
46 JIT_METH(variable); \ | |
47 JIT_METH(error_check); \ | |
48 JIT_METH(assign) \ | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
49 JIT_METH(argument) \ |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
50 JIT_METH(magic_end) |
15016 | 51 |
52 #define JIT_VISIT_IR_CONST \ | |
53 JIT_METH(const_bool); \ | |
54 JIT_METH(const_scalar); \ | |
55 JIT_METH(const_complex); \ | |
56 JIT_METH(const_index); \ | |
57 JIT_METH(const_string); \ | |
58 JIT_METH(const_range) | |
59 | |
60 #define JIT_VISIT_IR_CLASSES \ | |
61 JIT_VISIT_IR_NOTEMPLATE \ | |
62 JIT_VISIT_IR_CONST | |
63 | |
64 // forward declare all ir classes | |
65 #define JIT_METH(cname) \ | |
66 class jit_ ## cname; | |
67 | |
68 JIT_VISIT_IR_NOTEMPLATE | |
69 | |
70 #undef JIT_METH | |
71 | |
72 // ABCs which aren't included in JIT_VISIT_IR_ALL | |
73 class jit_instruction; | |
74 class jit_terminator; | |
75 | |
76 template <typename T, jit_type *(*EXTRACT_T)(void), typename PASS_T = T, | |
77 bool QUOTE=false> | |
78 class jit_const; | |
79 | |
80 typedef jit_const<bool, jit_typeinfo::get_bool> jit_const_bool; | |
81 typedef jit_const<double, jit_typeinfo::get_scalar> jit_const_scalar; | |
82 typedef jit_const<Complex, jit_typeinfo::get_complex> jit_const_complex; | |
83 typedef jit_const<octave_idx_type, jit_typeinfo::get_index> jit_const_index; | |
84 | |
85 typedef jit_const<std::string, jit_typeinfo::get_string, const std::string&, | |
86 true> jit_const_string; | |
87 typedef jit_const<jit_range, jit_typeinfo::get_range, const jit_range&> | |
88 jit_const_range; | |
89 | |
90 class jit_ir_walker; | |
91 class jit_use; | |
92 | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
93 // Creates and tracks memory for jit_value and subclasses. |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
94 // Memory managment is simple, all values that are created live as long as the |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
95 // factory. |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
96 class |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
97 jit_factory |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
98 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
99 typedef std::list<jit_value *> value_list; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
100 public: |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
101 ~jit_factory (void); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
102 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
103 const value_list& constants (void) const { return mconstants; } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
104 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
105 template <typename T> |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
106 T *create (void) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
107 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
108 T *ret = new T (); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
109 track_value (ret); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
110 return ret; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
111 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
112 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
113 #define DECL_ARG(n) const ARG ## n& arg ## n |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
114 #define JIT_CREATE(N) \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
115 template <typename T, OCT_MAKE_DECL_LIST (typename, ARG, N)> \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
116 T *create (OCT_MAKE_LIST (DECL_ARG, N)) \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
117 { \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
118 T *ret = new T (OCT_MAKE_ARG_LIST (arg, N)); \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
119 track_value (ret); \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
120 return ret; \ |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
121 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
122 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
123 JIT_CREATE (1) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
124 JIT_CREATE (2) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
125 JIT_CREATE (3) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
126 JIT_CREATE (4) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
127 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
128 #undef JIT_CREATE |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
129 #undef DECL_ARG |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
130 private: |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
131 void track_value (jit_value *v); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
132 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
133 value_list all_values; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
134 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
135 value_list mconstants; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
136 }; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
137 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
138 // A list of basic blocks (jit_block) which form some body of code. |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
139 // |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
140 // We do not directly inherit from std::list because we need to update the |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
141 // blocks stashed location in push_back and insert. |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
142 class |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
143 jit_block_list |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
144 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
145 public: |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
146 typedef std::list<jit_block *>::iterator iterator; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
147 typedef std::list<jit_block *>::const_iterator const_iterator; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
148 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
149 jit_block *back (void) const { return mlist.back (); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
150 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
151 iterator begin (void) { return mlist.begin (); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
152 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
153 const_iterator begin (void) const { return mlist.begin (); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
154 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
155 iterator end (void) { return mlist.end (); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
156 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
157 const_iterator end (void) const { return mlist.end (); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
158 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
159 iterator erase (iterator iter) { return mlist.erase (iter); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
160 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
161 jit_block *front (void) const { return mlist.front (); } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
162 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
163 void insert_after (iterator iter, jit_block *ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
164 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
165 void insert_after (jit_block *loc, jit_block *ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
166 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
167 void insert_before (iterator iter, jit_block *ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
168 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
169 void insert_before (jit_block *loc, jit_block *ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
170 |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
171 std::ostream& print (std::ostream& os, const std::string& header) const; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
172 |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
173 std::ostream& print_dom (std::ostream& os) const; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
174 |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
175 void push_back (jit_block *b); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
176 private: |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
177 std::list<jit_block *> mlist; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
178 }; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
179 |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
180 std::ostream& operator<<(std::ostream& os, const jit_block_list& blocks); |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
181 |
15016 | 182 class |
183 jit_value : public jit_internal_list<jit_value, jit_use> | |
184 { | |
185 public: | |
186 jit_value (void) : llvm_value (0), ty (0), mlast_use (0), | |
187 min_worklist (false) {} | |
188 | |
189 virtual ~jit_value (void); | |
190 | |
191 bool in_worklist (void) const | |
192 { | |
193 return min_worklist; | |
194 } | |
195 | |
196 void stash_in_worklist (bool ain_worklist) | |
197 { | |
198 min_worklist = ain_worklist; | |
199 } | |
200 | |
201 // The block of the first use which is not a jit_error_check | |
202 // So this is not necessarily first_use ()->parent (). | |
203 jit_block *first_use_block (void); | |
204 | |
205 // replace all uses with | |
206 virtual void replace_with (jit_value *value); | |
207 | |
208 jit_type *type (void) const { return ty; } | |
209 | |
210 llvm::Type *type_llvm (void) const | |
211 { | |
212 return ty ? ty->to_llvm () : 0; | |
213 } | |
214 | |
215 const std::string& type_name (void) const | |
216 { | |
217 return ty->name (); | |
218 } | |
219 | |
220 void stash_type (jit_type *new_ty) { ty = new_ty; } | |
221 | |
222 std::string print_string (void) | |
223 { | |
224 std::stringstream ss; | |
225 print (ss); | |
226 return ss.str (); | |
227 } | |
228 | |
229 jit_instruction *last_use (void) const { return mlast_use; } | |
230 | |
231 void stash_last_use (jit_instruction *alast_use) | |
232 { | |
233 mlast_use = alast_use; | |
234 } | |
235 | |
236 virtual bool needs_release (void) const { return false; } | |
237 | |
238 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const = 0; | |
239 | |
240 virtual std::ostream& short_print (std::ostream& os) const | |
241 { return print (os); } | |
242 | |
243 virtual void accept (jit_ir_walker& walker) = 0; | |
244 | |
245 bool has_llvm (void) const | |
246 { | |
247 return llvm_value; | |
248 } | |
249 | |
250 llvm::Value *to_llvm (void) const | |
251 { | |
252 assert (llvm_value); | |
253 return llvm_value; | |
254 } | |
255 | |
256 void stash_llvm (llvm::Value *compiled) | |
257 { | |
258 llvm_value = compiled; | |
259 } | |
260 | |
261 protected: | |
262 std::ostream& print_indent (std::ostream& os, size_t indent = 0) const | |
263 { | |
264 for (size_t i = 0; i < indent * 8; ++i) | |
265 os << " "; | |
266 return os; | |
267 } | |
268 | |
269 llvm::Value *llvm_value; | |
270 private: | |
271 jit_type *ty; | |
272 jit_instruction *mlast_use; | |
273 bool min_worklist; | |
274 }; | |
275 | |
276 std::ostream& operator<< (std::ostream& os, const jit_value& value); | |
277 std::ostream& jit_print (std::ostream& os, jit_value *avalue); | |
278 | |
279 class | |
280 jit_use : public jit_internal_node<jit_value, jit_use> | |
281 { | |
282 public: | |
15231
8442131a391a
jit-ir.h (jit_use::stash_value): Explicitly specify parent template types
Max Brister <max@2bass.com>
parents:
15195
diff
changeset
|
283 // some compilers don't allow us to use jit_internal_node without template |
8442131a391a
jit-ir.h (jit_use::stash_value): Explicitly specify parent template types
Max Brister <max@2bass.com>
parents:
15195
diff
changeset
|
284 // paremeters |
8442131a391a
jit-ir.h (jit_use::stash_value): Explicitly specify parent template types
Max Brister <max@2bass.com>
parents:
15195
diff
changeset
|
285 typedef jit_internal_node<jit_value, jit_use> PARENT_T; |
8442131a391a
jit-ir.h (jit_use::stash_value): Explicitly specify parent template types
Max Brister <max@2bass.com>
parents:
15195
diff
changeset
|
286 |
15016 | 287 jit_use (void) : muser (0), mindex (0) {} |
288 | |
289 // we should really have a move operator, but not until c++11 :( | |
290 jit_use (const jit_use& use) : muser (0), mindex (0) | |
291 { | |
292 *this = use; | |
293 } | |
294 | |
295 jit_use& operator= (const jit_use& use) | |
296 { | |
297 stash_value (use.value (), use.user (), use.index ()); | |
298 return *this; | |
299 } | |
300 | |
301 size_t index (void) const { return mindex; } | |
302 | |
303 jit_instruction *user (void) const { return muser; } | |
304 | |
305 jit_block *user_parent (void) const; | |
306 | |
307 std::list<jit_block *> user_parent_location (void) const; | |
308 | |
309 void stash_value (jit_value *avalue, jit_instruction *auser = 0, | |
310 size_t aindex = -1) | |
311 { | |
15231
8442131a391a
jit-ir.h (jit_use::stash_value): Explicitly specify parent template types
Max Brister <max@2bass.com>
parents:
15195
diff
changeset
|
312 PARENT_T::stash_value (avalue); |
15016 | 313 mindex = aindex; |
314 muser = auser; | |
315 } | |
316 private: | |
317 jit_instruction *muser; | |
318 size_t mindex; | |
319 }; | |
320 | |
321 class | |
322 jit_instruction : public jit_value | |
323 { | |
324 public: | |
325 // FIXME: this code could be so much pretier with varadic templates... | |
326 jit_instruction (void) : mid (next_id ()), mparent (0) | |
327 {} | |
328 | |
329 jit_instruction (size_t nargs) : mid (next_id ()), mparent (0) | |
330 { | |
331 already_infered.reserve (nargs); | |
332 marguments.reserve (nargs); | |
333 } | |
334 | |
335 #define STASH_ARG(i) stash_argument (i, arg ## i); | |
336 #define JIT_INSTRUCTION_CTOR(N) \ | |
337 jit_instruction (OCT_MAKE_DECL_LIST (jit_value *, arg, N)) \ | |
338 : already_infered (N), marguments (N), mid (next_id ()), mparent (0) \ | |
339 { \ | |
340 OCT_ITERATE_MACRO (STASH_ARG, N); \ | |
341 } | |
342 | |
343 JIT_INSTRUCTION_CTOR(1) | |
344 JIT_INSTRUCTION_CTOR(2) | |
345 JIT_INSTRUCTION_CTOR(3) | |
346 JIT_INSTRUCTION_CTOR(4) | |
347 | |
348 #undef STASH_ARG | |
349 #undef JIT_INSTRUCTION_CTOR | |
350 | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
351 jit_instruction (const std::vector<jit_value *>& aarguments) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
352 : already_infered (aarguments.size ()), marguments (aarguments.size ()), |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
353 mid (next_id ()), mparent (0) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
354 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
355 for (size_t i = 0; i < aarguments.size (); ++i) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
356 stash_argument (i, aarguments[i]); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
357 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
358 |
15016 | 359 static void reset_ids (void) |
360 { | |
361 next_id (true); | |
362 } | |
363 | |
364 jit_value *argument (size_t i) const | |
365 { | |
366 return marguments[i].value (); | |
367 } | |
368 | |
369 llvm::Value *argument_llvm (size_t i) const | |
370 { | |
371 assert (argument (i)); | |
372 return argument (i)->to_llvm (); | |
373 } | |
374 | |
375 jit_type *argument_type (size_t i) const | |
376 { | |
377 return argument (i)->type (); | |
378 } | |
379 | |
380 llvm::Type *argument_type_llvm (size_t i) const | |
381 { | |
382 assert (argument (i)); | |
383 return argument_type (i)->to_llvm (); | |
384 } | |
385 | |
386 std::ostream& print_argument (std::ostream& os, size_t i) const | |
387 { | |
388 if (argument (i)) | |
389 return argument (i)->short_print (os); | |
390 else | |
391 return os << "NULL"; | |
392 } | |
393 | |
394 void stash_argument (size_t i, jit_value *arg) | |
395 { | |
396 marguments[i].stash_value (arg, this, i); | |
397 } | |
398 | |
399 void push_argument (jit_value *arg) | |
400 { | |
401 marguments.push_back (jit_use ()); | |
402 stash_argument (marguments.size () - 1, arg); | |
403 already_infered.push_back (0); | |
404 } | |
405 | |
406 size_t argument_count (void) const | |
407 { | |
408 return marguments.size (); | |
409 } | |
410 | |
411 void resize_arguments (size_t acount, jit_value *adefault = 0) | |
412 { | |
413 size_t old = marguments.size (); | |
414 marguments.resize (acount); | |
415 already_infered.resize (acount); | |
416 | |
417 if (adefault) | |
418 for (size_t i = old; i < acount; ++i) | |
419 stash_argument (i, adefault); | |
420 } | |
421 | |
422 const std::vector<jit_use>& arguments (void) const { return marguments; } | |
423 | |
424 // argument types which have been infered already | |
425 const std::vector<jit_type *>& argument_types (void) const | |
426 { return already_infered; } | |
427 | |
428 virtual void push_variable (void) {} | |
429 | |
430 virtual void pop_variable (void) {} | |
431 | |
432 virtual void construct_ssa (void) | |
433 { | |
434 do_construct_ssa (0, argument_count ()); | |
435 } | |
436 | |
437 virtual bool infer (void) { return false; } | |
438 | |
439 void remove (void); | |
440 | |
441 virtual std::ostream& short_print (std::ostream& os) const; | |
442 | |
443 jit_block *parent (void) const { return mparent; } | |
444 | |
445 std::list<jit_instruction *>::iterator location (void) const | |
446 { | |
447 return mlocation; | |
448 } | |
449 | |
450 llvm::BasicBlock *parent_llvm (void) const; | |
451 | |
452 void stash_parent (jit_block *aparent, | |
453 std::list<jit_instruction *>::iterator alocation) | |
454 { | |
455 mparent = aparent; | |
456 mlocation = alocation; | |
457 } | |
458 | |
459 size_t id (void) const { return mid; } | |
460 protected: | |
461 | |
462 // Do SSA replacement on arguments in [start, end) | |
463 void do_construct_ssa (size_t start, size_t end); | |
464 | |
465 std::vector<jit_type *> already_infered; | |
466 private: | |
467 static size_t next_id (bool reset = false) | |
468 { | |
469 static size_t ret = 0; | |
470 if (reset) | |
471 return ret = 0; | |
472 | |
473 return ret++; | |
474 } | |
475 | |
476 std::vector<jit_use> marguments; | |
477 | |
478 size_t mid; | |
479 jit_block *mparent; | |
480 std::list<jit_instruction *>::iterator mlocation; | |
481 }; | |
482 | |
483 // defnie accept methods for subclasses | |
484 #define JIT_VALUE_ACCEPT \ | |
485 virtual void accept (jit_ir_walker& walker); | |
486 | |
487 // for use as a dummy argument during conversion to LLVM | |
488 class | |
489 jit_argument : public jit_value | |
490 { | |
491 public: | |
492 jit_argument (jit_type *atype, llvm::Value *avalue) | |
493 { | |
494 stash_type (atype); | |
495 stash_llvm (avalue); | |
496 } | |
497 | |
498 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
499 { | |
500 print_indent (os, indent); | |
501 return jit_print (os, type ()) << ": DUMMY"; | |
502 } | |
503 | |
504 JIT_VALUE_ACCEPT; | |
505 }; | |
506 | |
507 template <typename T, jit_type *(*EXTRACT_T)(void), typename PASS_T, | |
508 bool QUOTE> | |
509 class | |
510 jit_const : public jit_value | |
511 { | |
512 public: | |
513 typedef PASS_T pass_t; | |
514 | |
515 jit_const (PASS_T avalue) : mvalue (avalue) | |
516 { | |
517 stash_type (EXTRACT_T ()); | |
518 } | |
519 | |
520 PASS_T value (void) const { return mvalue; } | |
521 | |
522 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
523 { | |
524 print_indent (os, indent); | |
525 jit_print (os, type ()) << ": "; | |
526 if (QUOTE) | |
527 os << "\""; | |
528 os << mvalue; | |
529 if (QUOTE) | |
530 os << "\""; | |
531 return os; | |
532 } | |
533 | |
534 JIT_VALUE_ACCEPT; | |
535 private: | |
536 T mvalue; | |
537 }; | |
538 | |
539 class jit_phi_incomming; | |
540 | |
541 class | |
542 jit_block : public jit_value, public jit_internal_list<jit_block, | |
543 jit_phi_incomming> | |
544 { | |
545 typedef jit_internal_list<jit_block, jit_phi_incomming> ILIST_T; | |
546 public: | |
547 typedef std::list<jit_instruction *> instruction_list; | |
548 typedef instruction_list::iterator iterator; | |
549 typedef instruction_list::const_iterator const_iterator; | |
550 | |
551 typedef std::set<jit_block *> df_set; | |
552 typedef df_set::const_iterator df_iterator; | |
553 | |
554 static const size_t NO_ID = static_cast<size_t> (-1); | |
555 | |
556 jit_block (const std::string& aname, size_t avisit_count = 0) | |
557 : mvisit_count (avisit_count), mid (NO_ID), idom (0), mname (aname), | |
558 malive (false) | |
559 {} | |
560 | |
561 virtual void replace_with (jit_value *value); | |
562 | |
563 void replace_in_phi (jit_block *ablock, jit_block *with); | |
564 | |
565 // we have a new internal list, but we want to stay compatable with jit_value | |
566 jit_use *first_use (void) const { return jit_value::first_use (); } | |
567 | |
568 size_t use_count (void) const { return jit_value::use_count (); } | |
569 | |
570 // if a block is alive, then it might be visited during execution | |
571 bool alive (void) const { return malive; } | |
572 | |
573 void mark_alive (void) { malive = true; } | |
574 | |
575 // If we can merge with a successor, do so and return the now empty block | |
576 jit_block *maybe_merge (); | |
577 | |
578 // merge another block into this block, leaving the merge block empty | |
579 void merge (jit_block& merge); | |
580 | |
581 const std::string& name (void) const { return mname; } | |
582 | |
583 jit_instruction *prepend (jit_instruction *instr); | |
584 | |
585 jit_instruction *prepend_after_phi (jit_instruction *instr); | |
586 | |
587 template <typename T> | |
588 T *append (T *instr) | |
589 { | |
590 internal_append (instr); | |
591 return instr; | |
592 } | |
593 | |
594 jit_instruction *insert_before (iterator loc, jit_instruction *instr); | |
595 | |
596 jit_instruction *insert_before (jit_instruction *loc, jit_instruction *instr) | |
597 { | |
598 return insert_before (loc->location (), instr); | |
599 } | |
600 | |
601 jit_instruction *insert_after (iterator loc, jit_instruction *instr); | |
602 | |
603 jit_instruction *insert_after (jit_instruction *loc, jit_instruction *instr) | |
604 { | |
605 return insert_after (loc->location (), instr); | |
606 } | |
607 | |
608 iterator remove (iterator iter) | |
609 { | |
610 jit_instruction *instr = *iter; | |
611 iter = instructions.erase (iter); | |
612 instr->stash_parent (0, instructions.end ()); | |
613 return iter; | |
614 } | |
615 | |
616 jit_terminator *terminator (void) const; | |
617 | |
618 // is the jump from pred alive? | |
619 bool branch_alive (jit_block *asucc) const; | |
620 | |
621 jit_block *successor (size_t i) const; | |
622 | |
623 size_t successor_count (void) const; | |
624 | |
625 iterator begin (void) { return instructions.begin (); } | |
626 | |
627 const_iterator begin (void) const { return instructions.begin (); } | |
628 | |
629 iterator end (void) { return instructions.end (); } | |
630 | |
631 const_iterator end (void) const { return instructions.end (); } | |
632 | |
633 iterator phi_begin (void); | |
634 | |
635 iterator phi_end (void); | |
636 | |
637 iterator nonphi_begin (void); | |
638 | |
639 // must label before id is valid | |
640 size_t id (void) const { return mid; } | |
641 | |
642 // dominance frontier | |
643 const df_set& df (void) const { return mdf; } | |
644 | |
645 df_iterator df_begin (void) const { return mdf.begin (); } | |
646 | |
647 df_iterator df_end (void) const { return mdf.end (); } | |
648 | |
649 // label with a RPO walk | |
650 void label (void) | |
651 { | |
652 size_t number = 0; | |
653 label (mvisit_count, number); | |
654 } | |
655 | |
656 void label (size_t avisit_count, size_t& number) | |
657 { | |
658 if (visited (avisit_count)) | |
659 return; | |
660 | |
661 for (jit_use *use = first_use (); use; use = use->next ()) | |
662 { | |
663 jit_block *pred = use->user_parent (); | |
664 pred->label (avisit_count, number); | |
665 } | |
666 | |
667 mid = number++; | |
668 } | |
669 | |
670 // See for idom computation algorithm | |
671 // Cooper, Keith D.; Harvey, Timothy J; and Kennedy, Ken (2001). | |
672 // "A Simple, Fast Dominance Algorithm" | |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
673 void compute_idom (jit_block& entry_block) |
15016 | 674 { |
675 bool changed; | |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
676 entry_block.idom = &entry_block; |
15016 | 677 do |
678 changed = update_idom (mvisit_count); | |
679 while (changed); | |
680 } | |
681 | |
682 // compute dominance frontier | |
683 void compute_df (void) | |
684 { | |
685 compute_df (mvisit_count); | |
686 } | |
687 | |
688 void create_dom_tree (void) | |
689 { | |
690 create_dom_tree (mvisit_count); | |
691 } | |
692 | |
693 jit_block *dom_successor (size_t idx) const | |
694 { | |
695 return dom_succ[idx]; | |
696 } | |
697 | |
698 size_t dom_successor_count (void) const | |
699 { | |
700 return dom_succ.size (); | |
701 } | |
702 | |
703 // call pop_varaible on all instructions | |
704 void pop_all (void); | |
705 | |
706 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
707 { | |
708 print_indent (os, indent); | |
709 short_print (os) << ": %pred = "; | |
710 for (jit_use *use = first_use (); use; use = use->next ()) | |
711 { | |
712 jit_block *pred = use->user_parent (); | |
713 os << *pred; | |
714 if (use->next ()) | |
715 os << ", "; | |
716 } | |
717 os << std::endl; | |
718 | |
719 for (const_iterator iter = begin (); iter != end (); ++iter) | |
720 { | |
721 jit_instruction *instr = *iter; | |
722 instr->print (os, indent + 1) << std::endl; | |
723 } | |
724 return os; | |
725 } | |
726 | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
727 jit_block *maybe_split (jit_factory& factory, jit_block_list& blocks, |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
728 jit_block *asuccessor); |
15016 | 729 |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
730 jit_block *maybe_split (jit_factory& factory, jit_block_list& blocks, |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
731 jit_block& asuccessor) |
15016 | 732 { |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
733 return maybe_split (factory, blocks, &asuccessor); |
15016 | 734 } |
735 | |
736 // print dominator infomration | |
737 std::ostream& print_dom (std::ostream& os) const; | |
738 | |
739 virtual std::ostream& short_print (std::ostream& os) const | |
740 { | |
741 os << mname; | |
742 if (mid != NO_ID) | |
743 os << mid; | |
744 return os; | |
745 } | |
746 | |
747 llvm::BasicBlock *to_llvm (void) const; | |
748 | |
749 std::list<jit_block *>::iterator location (void) const | |
750 { return mlocation; } | |
751 | |
752 void stash_location (std::list<jit_block *>::iterator alocation) | |
753 { mlocation = alocation; } | |
754 | |
755 // used to prevent visiting the same node twice in the graph | |
756 size_t visit_count (void) const { return mvisit_count; } | |
757 | |
758 // check if this node has been visited yet at the given visit count. If we | |
759 // have not been visited yet, mark us as visited. | |
760 bool visited (size_t avisit_count) | |
761 { | |
762 if (mvisit_count <= avisit_count) | |
763 { | |
764 mvisit_count = avisit_count + 1; | |
765 return false; | |
766 } | |
767 | |
768 return true; | |
769 } | |
770 | |
771 JIT_VALUE_ACCEPT; | |
772 private: | |
773 void internal_append (jit_instruction *instr); | |
774 | |
775 void compute_df (size_t avisit_count); | |
776 | |
777 bool update_idom (size_t avisit_count); | |
778 | |
779 void create_dom_tree (size_t avisit_count); | |
780 | |
781 static jit_block *idom_intersect (jit_block *i, jit_block *j); | |
782 | |
783 size_t mvisit_count; | |
784 size_t mid; | |
785 jit_block *idom; | |
786 df_set mdf; | |
787 std::vector<jit_block *> dom_succ; | |
788 std::string mname; | |
789 instruction_list instructions; | |
790 bool malive; | |
791 std::list<jit_block *>::iterator mlocation; | |
792 }; | |
793 | |
794 // keeps track of phi functions that use a block on incomming edges | |
795 class | |
796 jit_phi_incomming : public jit_internal_node<jit_block, jit_phi_incomming> | |
797 { | |
798 public: | |
799 jit_phi_incomming (void) : muser (0) {} | |
800 | |
801 jit_phi_incomming (jit_phi *auser) : muser (auser) {} | |
802 | |
803 jit_phi_incomming (const jit_phi_incomming& use) : jit_internal_node () | |
804 { | |
805 *this = use; | |
806 } | |
807 | |
808 jit_phi_incomming& operator= (const jit_phi_incomming& use) | |
809 { | |
810 stash_value (use.value ()); | |
811 muser = use.muser; | |
812 return *this; | |
813 } | |
814 | |
815 jit_phi *user (void) const { return muser; } | |
816 | |
817 jit_block *user_parent (void) const; | |
818 private: | |
819 jit_phi *muser; | |
820 }; | |
821 | |
822 // A non-ssa variable | |
823 class | |
824 jit_variable : public jit_value | |
825 { | |
826 public: | |
827 jit_variable (const std::string& aname) : mname (aname), mlast_use (0) {} | |
828 | |
829 const std::string &name (void) const { return mname; } | |
830 | |
831 // manipulate the value_stack, for use during SSA construction. The top of the | |
832 // value stack represents the current value for this variable | |
833 bool has_top (void) const | |
834 { | |
835 return ! value_stack.empty (); | |
836 } | |
837 | |
838 jit_value *top (void) const | |
839 { | |
840 return value_stack.top (); | |
841 } | |
842 | |
843 void push (jit_instruction *v) | |
844 { | |
845 value_stack.push (v); | |
846 mlast_use = v; | |
847 } | |
848 | |
849 void pop (void) | |
850 { | |
851 value_stack.pop (); | |
852 } | |
853 | |
854 jit_instruction *last_use (void) const | |
855 { | |
856 return mlast_use; | |
857 } | |
858 | |
859 void stash_last_use (jit_instruction *instr) | |
860 { | |
861 mlast_use = instr; | |
862 } | |
863 | |
864 // blocks in which we are used | |
865 void use_blocks (jit_block::df_set& result) | |
866 { | |
867 jit_use *use = first_use (); | |
868 while (use) | |
869 { | |
870 result.insert (use->user_parent ()); | |
871 use = use->next (); | |
872 } | |
873 } | |
874 | |
875 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
876 { | |
877 return print_indent (os, indent) << mname; | |
878 } | |
879 | |
880 JIT_VALUE_ACCEPT; | |
881 private: | |
882 std::string mname; | |
883 std::stack<jit_value *> value_stack; | |
884 jit_instruction *mlast_use; | |
885 }; | |
886 | |
887 class | |
888 jit_assign_base : public jit_instruction | |
889 { | |
890 public: | |
891 jit_assign_base (jit_variable *adest) : jit_instruction (), mdest (adest) {} | |
892 | |
893 jit_assign_base (jit_variable *adest, size_t npred) : jit_instruction (npred), | |
894 mdest (adest) {} | |
895 | |
896 jit_assign_base (jit_variable *adest, jit_value *arg0, jit_value *arg1) | |
897 : jit_instruction (arg0, arg1), mdest (adest) {} | |
898 | |
899 jit_variable *dest (void) const { return mdest; } | |
900 | |
901 virtual void push_variable (void) | |
902 { | |
903 mdest->push (this); | |
904 } | |
905 | |
906 virtual void pop_variable (void) | |
907 { | |
908 mdest->pop (); | |
909 } | |
910 | |
911 virtual std::ostream& short_print (std::ostream& os) const | |
912 { | |
913 if (type ()) | |
914 jit_print (os, type ()) << ": "; | |
915 | |
916 dest ()->short_print (os); | |
917 return os << "#" << id (); | |
918 } | |
919 private: | |
920 jit_variable *mdest; | |
921 }; | |
922 | |
923 class | |
924 jit_assign : public jit_assign_base | |
925 { | |
926 public: | |
927 jit_assign (jit_variable *adest, jit_value *asrc) | |
928 : jit_assign_base (adest, adest, asrc), martificial (false) {} | |
929 | |
930 jit_value *overwrite (void) const | |
931 { | |
932 return argument (0); | |
933 } | |
934 | |
935 jit_value *src (void) const | |
936 { | |
937 return argument (1); | |
938 } | |
939 | |
940 // variables don't get modified in an SSA, but COW requires we modify | |
941 // variables. An artificial assign is for when a variable gets modified. We | |
942 // need an assign in the SSA, but the reference counts shouldn't be updated. | |
943 bool artificial (void) const { return martificial; } | |
944 | |
945 void mark_artificial (void) { martificial = true; } | |
946 | |
947 virtual bool infer (void) | |
948 { | |
949 jit_type *stype = src ()->type (); | |
950 if (stype != type()) | |
951 { | |
952 stash_type (stype); | |
953 return true; | |
954 } | |
955 | |
956 return false; | |
957 } | |
958 | |
959 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
960 { | |
961 print_indent (os, indent) << *this << " = " << *src (); | |
962 | |
963 if (artificial ()) | |
964 os << " [artificial]"; | |
965 | |
966 return os; | |
967 } | |
968 | |
969 JIT_VALUE_ACCEPT; | |
970 private: | |
971 bool martificial; | |
972 }; | |
973 | |
974 class | |
975 jit_phi : public jit_assign_base | |
976 { | |
977 public: | |
978 jit_phi (jit_variable *adest, size_t npred) | |
979 : jit_assign_base (adest, npred) | |
980 { | |
981 mincomming.reserve (npred); | |
982 } | |
983 | |
984 // removes arguments form dead incomming jumps | |
985 bool prune (void); | |
986 | |
987 void add_incomming (jit_block *from, jit_value *value) | |
988 { | |
989 push_argument (value); | |
990 mincomming.push_back (jit_phi_incomming (this)); | |
991 mincomming[mincomming.size () - 1].stash_value (from); | |
992 } | |
993 | |
994 jit_block *incomming (size_t i) const | |
995 { | |
996 return mincomming[i].value (); | |
997 } | |
998 | |
999 llvm::BasicBlock *incomming_llvm (size_t i) const | |
1000 { | |
1001 return incomming (i)->to_llvm (); | |
1002 } | |
1003 | |
1004 virtual void construct_ssa (void) {} | |
1005 | |
1006 virtual bool infer (void); | |
1007 | |
1008 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1009 { | |
1010 std::stringstream ss; | |
1011 print_indent (ss, indent); | |
1012 short_print (ss) << " phi "; | |
1013 std::string ss_str = ss.str (); | |
1014 std::string indent_str (ss_str.size (), ' '); | |
1015 os << ss_str; | |
1016 | |
1017 for (size_t i = 0; i < argument_count (); ++i) | |
1018 { | |
1019 if (i > 0) | |
1020 os << indent_str; | |
1021 os << "| "; | |
1022 | |
1023 os << *incomming (i) << " -> "; | |
1024 os << *argument (i); | |
1025 | |
1026 if (i + 1 < argument_count ()) | |
1027 os << std::endl; | |
1028 } | |
1029 | |
1030 return os; | |
1031 } | |
1032 | |
1033 llvm::PHINode *to_llvm (void) const; | |
1034 | |
1035 JIT_VALUE_ACCEPT; | |
1036 private: | |
1037 std::vector<jit_phi_incomming> mincomming; | |
1038 }; | |
1039 | |
1040 class | |
1041 jit_terminator : public jit_instruction | |
1042 { | |
1043 public: | |
1044 #define JIT_TERMINATOR_CONST(N) \ | |
1045 jit_terminator (size_t asuccessor_count, \ | |
1046 OCT_MAKE_DECL_LIST (jit_value *, arg, N)) \ | |
1047 : jit_instruction (OCT_MAKE_ARG_LIST (arg, N)), \ | |
1048 malive (asuccessor_count, false) {} | |
1049 | |
1050 JIT_TERMINATOR_CONST (1) | |
1051 JIT_TERMINATOR_CONST (2) | |
1052 JIT_TERMINATOR_CONST (3) | |
1053 | |
1054 #undef JIT_TERMINATOR_CONST | |
1055 | |
1056 jit_block *successor (size_t idx = 0) const | |
1057 { | |
1058 return static_cast<jit_block *> (argument (idx)); | |
1059 } | |
1060 | |
1061 llvm::BasicBlock *successor_llvm (size_t idx = 0) const | |
1062 { | |
1063 return successor (idx)->to_llvm (); | |
1064 } | |
1065 | |
1066 size_t successor_index (const jit_block *asuccessor) const; | |
1067 | |
1068 std::ostream& print_successor (std::ostream& os, size_t idx = 0) const | |
1069 { | |
1070 if (alive (idx)) | |
1071 os << "[live] "; | |
1072 else | |
1073 os << "[dead] "; | |
1074 | |
1075 return successor (idx)->short_print (os); | |
1076 } | |
1077 | |
1078 // Check if the jump to successor is live | |
1079 bool alive (const jit_block *asuccessor) const | |
1080 { | |
1081 return alive (successor_index (asuccessor)); | |
1082 } | |
1083 | |
1084 bool alive (size_t idx) const { return malive[idx]; } | |
1085 | |
1086 bool alive (int idx) const { return malive[idx]; } | |
1087 | |
1088 size_t successor_count (void) const { return malive.size (); } | |
1089 | |
1090 virtual bool infer (void); | |
1091 | |
1092 llvm::TerminatorInst *to_llvm (void) const; | |
1093 protected: | |
1094 virtual bool check_alive (size_t) const { return true; } | |
1095 private: | |
1096 std::vector<bool> malive; | |
1097 }; | |
1098 | |
1099 class | |
1100 jit_branch : public jit_terminator | |
1101 { | |
1102 public: | |
1103 jit_branch (jit_block *succ) : jit_terminator (1, succ) {} | |
1104 | |
1105 virtual size_t successor_count (void) const { return 1; } | |
1106 | |
1107 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1108 { | |
1109 print_indent (os, indent) << "branch: "; | |
1110 return print_successor (os); | |
1111 } | |
1112 | |
1113 JIT_VALUE_ACCEPT; | |
1114 }; | |
1115 | |
1116 class | |
1117 jit_cond_branch : public jit_terminator | |
1118 { | |
1119 public: | |
1120 jit_cond_branch (jit_value *c, jit_block *ctrue, jit_block *cfalse) | |
1121 : jit_terminator (2, ctrue, cfalse, c) {} | |
1122 | |
1123 jit_value *cond (void) const { return argument (2); } | |
1124 | |
1125 std::ostream& print_cond (std::ostream& os) const | |
1126 { | |
1127 return cond ()->short_print (os); | |
1128 } | |
1129 | |
1130 llvm::Value *cond_llvm (void) const | |
1131 { | |
1132 return cond ()->to_llvm (); | |
1133 } | |
1134 | |
1135 virtual size_t successor_count (void) const { return 2; } | |
1136 | |
1137 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1138 { | |
1139 print_indent (os, indent) << "cond_branch: "; | |
1140 print_cond (os) << ", "; | |
1141 print_successor (os, 0) << ", "; | |
1142 return print_successor (os, 1); | |
1143 } | |
1144 | |
1145 JIT_VALUE_ACCEPT; | |
1146 }; | |
1147 | |
1148 class | |
1149 jit_call : public jit_instruction | |
1150 { | |
1151 public: | |
1152 #define JIT_CALL_CONST(N) \ | |
1153 jit_call (const jit_operation& aoperation, \ | |
1154 OCT_MAKE_DECL_LIST (jit_value *, arg, N)) \ | |
1155 : jit_instruction (OCT_MAKE_ARG_LIST (arg, N)), moperation (aoperation) {} \ | |
1156 \ | |
1157 jit_call (const jit_operation& (*aoperation) (void), \ | |
1158 OCT_MAKE_DECL_LIST (jit_value *, arg, N)) \ | |
1159 : jit_instruction (OCT_MAKE_ARG_LIST (arg, N)), moperation (aoperation ()) \ | |
1160 {} | |
1161 | |
1162 JIT_CALL_CONST (1) | |
1163 JIT_CALL_CONST (2) | |
1164 JIT_CALL_CONST (3) | |
1165 JIT_CALL_CONST (4) | |
1166 | |
1167 #undef JIT_CALL_CONST | |
1168 | |
15067 | 1169 jit_call (const jit_operation& aoperation, |
1170 const std::vector<jit_value *>& args) | |
1171 : jit_instruction (args), moperation (aoperation) | |
1172 {} | |
15016 | 1173 |
1174 const jit_operation& operation (void) const { return moperation; } | |
1175 | |
1176 bool can_error (void) const | |
1177 { | |
1178 return overload ().can_error (); | |
1179 } | |
1180 | |
1181 const jit_function& overload (void) const | |
1182 { | |
1183 return moperation.overload (argument_types ()); | |
1184 } | |
1185 | |
1186 virtual bool needs_release (void) const | |
1187 { | |
1188 return type () && jit_typeinfo::get_release (type ()).valid (); | |
1189 } | |
1190 | |
1191 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1192 { | |
1193 print_indent (os, indent); | |
1194 | |
1195 if (use_count ()) | |
1196 short_print (os) << " = "; | |
1197 os << "call " << moperation.name () << " ("; | |
1198 | |
1199 for (size_t i = 0; i < argument_count (); ++i) | |
1200 { | |
1201 print_argument (os, i); | |
1202 if (i + 1 < argument_count ()) | |
1203 os << ", "; | |
1204 } | |
1205 return os << ")"; | |
1206 } | |
1207 | |
1208 virtual bool infer (void); | |
1209 | |
1210 JIT_VALUE_ACCEPT; | |
1211 private: | |
1212 const jit_operation& moperation; | |
1213 }; | |
1214 | |
1215 // FIXME: This is just ugly... | |
1216 // checks error_state, if error_state is false then goto the normal branche, | |
1217 // otherwise goto the error branch | |
1218 class | |
1219 jit_error_check : public jit_terminator | |
1220 { | |
1221 public: | |
1222 jit_error_check (jit_call *acheck_for, jit_block *normal, jit_block *error) | |
1223 : jit_terminator (2, error, normal, acheck_for) {} | |
1224 | |
1225 jit_call *check_for (void) const | |
1226 { | |
1227 return static_cast<jit_call *> (argument (2)); | |
1228 } | |
1229 | |
1230 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1231 { | |
1232 print_indent (os, indent) << "error_check " << *check_for () << ", "; | |
1233 print_successor (os, 1) << ", "; | |
1234 return print_successor (os, 0); | |
1235 } | |
1236 | |
1237 JIT_VALUE_ACCEPT; | |
1238 protected: | |
1239 virtual bool check_alive (size_t idx) const | |
1240 { | |
1241 return idx == 1 ? true : check_for ()->can_error (); | |
1242 } | |
1243 }; | |
1244 | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1245 // for now only handles the 1D case |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1246 class |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1247 jit_magic_end : public jit_instruction |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1248 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1249 public: |
15067 | 1250 class |
1251 context | |
1252 { | |
1253 public: | |
1254 context (void) : value (0), index (0), count (0) | |
1255 {} | |
1256 | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
1257 context (jit_factory& factory, jit_value *avalue, size_t aindex, |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1258 size_t acount); |
15067 | 1259 |
1260 jit_value *value; | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1261 jit_const_index *index; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1262 jit_const_index *count; |
15067 | 1263 }; |
1264 | |
1265 jit_magic_end (const std::vector<context>& full_context); | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1266 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1267 virtual bool infer (void); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1268 |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1269 const jit_function& overload () const; |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1270 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1271 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const; |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1272 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1273 context resolve_context (void) const; |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1274 |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1275 virtual std::ostream& short_print (std::ostream& os) const |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1276 { |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1277 return os << "magic_end" << "#" << id (); |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1278 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1279 |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1280 JIT_VALUE_ACCEPT; |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1281 private: |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
1282 std::vector<context> contexts; |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1283 }; |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
1284 |
15016 | 1285 class |
1286 jit_extract_argument : public jit_assign_base | |
1287 { | |
1288 public: | |
1289 jit_extract_argument (jit_type *atype, jit_variable *adest) | |
1290 : jit_assign_base (adest) | |
1291 { | |
1292 stash_type (atype); | |
1293 } | |
1294 | |
1295 const std::string& name (void) const | |
1296 { | |
1297 return dest ()->name (); | |
1298 } | |
1299 | |
1300 const jit_function& overload (void) const | |
1301 { | |
1302 return jit_typeinfo::cast (type (), jit_typeinfo::get_any ()); | |
1303 } | |
1304 | |
1305 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1306 { | |
1307 print_indent (os, indent); | |
1308 | |
1309 return short_print (os) << " = extract " << name (); | |
1310 } | |
1311 | |
1312 JIT_VALUE_ACCEPT; | |
1313 }; | |
1314 | |
1315 class | |
1316 jit_store_argument : public jit_instruction | |
1317 { | |
1318 public: | |
1319 jit_store_argument (jit_variable *var) | |
1320 : jit_instruction (var), dest (var) | |
1321 {} | |
1322 | |
1323 const std::string& name (void) const | |
1324 { | |
1325 return dest->name (); | |
1326 } | |
1327 | |
1328 const jit_function& overload (void) const | |
1329 { | |
1330 return jit_typeinfo::cast (jit_typeinfo::get_any (), result_type ()); | |
1331 } | |
1332 | |
1333 jit_value *result (void) const | |
1334 { | |
1335 return argument (0); | |
1336 } | |
1337 | |
1338 jit_type *result_type (void) const | |
1339 { | |
1340 return result ()->type (); | |
1341 } | |
1342 | |
1343 llvm::Value *result_llvm (void) const | |
1344 { | |
1345 return result ()->to_llvm (); | |
1346 } | |
1347 | |
1348 virtual std::ostream& print (std::ostream& os, size_t indent = 0) const | |
1349 { | |
1350 jit_value *res = result (); | |
1351 print_indent (os, indent) << "store "; | |
1352 dest->short_print (os); | |
1353 | |
1354 if (! isa<jit_variable> (res)) | |
1355 { | |
1356 os << " = "; | |
1357 res->short_print (os); | |
1358 } | |
1359 | |
1360 return os; | |
1361 } | |
1362 | |
1363 JIT_VALUE_ACCEPT; | |
1364 private: | |
1365 jit_variable *dest; | |
1366 }; | |
1367 | |
1368 class | |
1369 jit_ir_walker | |
1370 { | |
1371 public: | |
1372 virtual ~jit_ir_walker () {} | |
1373 | |
1374 #define JIT_METH(clname) \ | |
1375 virtual void visit (jit_ ## clname&) = 0; | |
1376 | |
1377 JIT_VISIT_IR_CLASSES; | |
1378 | |
1379 #undef JIT_METH | |
1380 }; | |
1381 | |
1382 template <typename T, jit_type *(*EXTRACT_T)(void), typename PASS_T, bool QUOTE> | |
1383 void | |
1384 jit_const<T, EXTRACT_T, PASS_T, QUOTE>::accept (jit_ir_walker& walker) | |
1385 { | |
1386 walker.visit (*this); | |
1387 } | |
1388 | |
1389 #undef JIT_VALUE_ACCEPT | |
1390 | |
1391 #endif | |
1392 #endif |