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