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