Mercurial > hg > octave-nkf
annotate libinterp/corefcn/jit-util.h @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18995
diff
changeset
|
3 Copyright (C) 2012-2015 Max Brister |
15016 | 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 | |
16768 | 23 // Author: Max Brister <max@2bass.com> |
24 | |
15016 | 25 // Some utility classes and functions used throughout jit |
26 | |
27 #if !defined (octave_jit_util_h) | |
28 #define octave_jit_util_h 1 | |
29 | |
20830 | 30 #ifdef HAVE_JIT |
15016 | 31 |
32 #include <stdexcept> | |
33 | |
20830 | 34 #ifdef HAVE_GCCJIT |
35 #include "libgccjit++.h" | |
36 #endif | |
37 | |
38 #if HAVE_LLVM | |
17164 | 39 #if defined(HAVE_LLVM_IR_DATALAYOUT_H) || defined(HAVE_LLVM_DATALAYOUT_H) |
40 #define HAVE_LLVM_DATALAYOUT | |
41 #endif | |
42 | |
15016 | 43 // we don't want to include llvm headers here, as they require |
44 // __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS be defined in the entire | |
45 // compilation unit | |
46 namespace llvm | |
47 { | |
48 class Value; | |
49 class Module; | |
18995
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
50 #ifdef LEGACY_PASSMANAGER |
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
51 namespace legacy { |
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
52 class FunctionPassManager; |
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
53 class PassManager; |
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
54 } |
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
55 #else |
15016 | 56 class FunctionPassManager; |
57 class PassManager; | |
18995
4a4edf0f2077
fix LLVM 3.4 build (bug #41061)
Stefan Mahr <dac922@gmx.de>
parents:
17787
diff
changeset
|
58 #endif |
15016 | 59 class ExecutionEngine; |
60 class Function; | |
61 class BasicBlock; | |
62 class LLVMContext; | |
63 class Type; | |
64 class StructType; | |
65 class Twine; | |
66 class GlobalVariable; | |
67 class TerminatorInst; | |
68 class PHINode; | |
69 | |
70 class ConstantFolder; | |
71 | |
72 template <bool preserveNames> | |
73 class IRBuilderDefaultInserter; | |
74 | |
75 template <bool preserveNames, typename T, typename Inserter> | |
76 class IRBuilder; | |
77 | |
78 typedef IRBuilder<true, ConstantFolder, IRBuilderDefaultInserter<true> > | |
79 IRBuilderD; | |
80 } | |
20830 | 81 #endif /* if HAVE_LLVM */ |
15016 | 82 |
83 class octave_base_value; | |
84 class octave_builtin; | |
85 class octave_value; | |
86 class tree; | |
87 class tree_expression; | |
88 | |
89 // thrown when we should give up on JIT and interpret | |
90 class jit_fail_exception : public std::runtime_error | |
91 { | |
92 public: | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 jit_fail_exception (void) : std::runtime_error ("unknown"), mknown (false) { } |
15016 | 94 jit_fail_exception (const std::string& reason) : std::runtime_error (reason), |
95 mknown (true) | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 { } |
15016 | 97 |
98 bool known (void) const { return mknown; } | |
99 private: | |
100 bool mknown; | |
101 }; | |
102 | |
103 // llvm doesn't provide this, and it's really useful for debugging | |
20830 | 104 #if HAVE_LLVM |
15016 | 105 std::ostream& operator<< (std::ostream& os, const llvm::Value& v); |
20830 | 106 #endif |
15016 | 107 |
108 template <typename HOLDER_T, typename SUB_T> | |
109 class jit_internal_node; | |
110 | |
111 // jit_internal_list and jit_internal_node implement generic embedded doubly | |
112 // linked lists. List items extend from jit_internal_list, and can be placed | |
113 // in nodes of type jit_internal_node. We use CRTP twice. | |
114 template <typename LIST_T, typename NODE_T> | |
115 class | |
116 jit_internal_list | |
117 { | |
118 friend class jit_internal_node<LIST_T, NODE_T>; | |
119 public: | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 jit_internal_list (void) : use_head (0), use_tail (0), muse_count (0) { } |
15016 | 121 |
122 virtual ~jit_internal_list (void) | |
123 { | |
124 while (use_head) | |
125 use_head->stash_value (0); | |
126 } | |
127 | |
128 NODE_T *first_use (void) const { return use_head; } | |
129 | |
130 size_t use_count (void) const { return muse_count; } | |
131 private: | |
132 NODE_T *use_head; | |
133 NODE_T *use_tail; | |
134 size_t muse_count; | |
135 }; | |
136 | |
137 // a node for internal linked lists | |
138 template <typename LIST_T, typename NODE_T> | |
139 class | |
140 jit_internal_node | |
141 { | |
142 public: | |
143 typedef jit_internal_list<LIST_T, NODE_T> jit_ilist; | |
144 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
145 jit_internal_node (void) : mvalue (0), mnext (0), mprev (0) { } |
15016 | 146 |
147 ~jit_internal_node (void) { remove (); } | |
148 | |
149 LIST_T *value (void) const { return mvalue; } | |
150 | |
151 void stash_value (LIST_T *avalue) | |
152 { | |
153 remove (); | |
154 | |
155 mvalue = avalue; | |
156 | |
157 if (mvalue) | |
158 { | |
159 jit_ilist *ilist = mvalue; | |
160 NODE_T *sthis = static_cast<NODE_T *> (this); | |
161 if (ilist->use_head) | |
162 { | |
163 ilist->use_tail->mnext = sthis; | |
164 mprev = ilist->use_tail; | |
165 } | |
166 else | |
167 ilist->use_head = sthis; | |
168 | |
169 ilist->use_tail = sthis; | |
170 ++ilist->muse_count; | |
171 } | |
172 } | |
173 | |
174 NODE_T *next (void) const { return mnext; } | |
175 | |
176 NODE_T *prev (void) const { return mprev; } | |
177 private: | |
178 void remove () | |
179 { | |
180 if (mvalue) | |
181 { | |
182 jit_ilist *ilist = mvalue; | |
183 if (mprev) | |
184 mprev->mnext = mnext; | |
185 else | |
186 // we are the use_head | |
187 ilist->use_head = mnext; | |
188 | |
189 if (mnext) | |
190 mnext->mprev = mprev; | |
191 else | |
192 // we are the use tail | |
193 ilist->use_tail = mprev; | |
194 | |
195 mnext = mprev = 0; | |
196 --ilist->muse_count; | |
197 mvalue = 0; | |
198 } | |
199 } | |
200 | |
201 LIST_T *mvalue; | |
202 NODE_T *mnext; | |
203 NODE_T *mprev; | |
204 }; | |
205 | |
206 // Use like: isa<jit_phi> (value) | |
207 // basically just a short cut type typing dyanmic_cast. | |
208 template <typename T, typename U> | |
209 bool isa (U *value) | |
210 { | |
211 return dynamic_cast<T *> (value); | |
212 } | |
213 | |
214 #define JIT_ASSIGN_ARG(i) the_args[i] = arg ## i; | |
215 #define JIT_EXPAND(ret, fname, type, isconst, N) \ | |
216 ret fname (JIT_PARAM_ARGS OCT_MAKE_DECL_LIST (type, arg, N)) isconst \ | |
217 { \ | |
218 std::vector<type> the_args (N); \ | |
219 OCT_ITERATE_MACRO (JIT_ASSIGN_ARG, N); \ | |
220 return fname (JIT_PARAMS the_args); \ | |
221 } | |
222 | |
20830 | 223 #endif /* ifdef HAVE_JIT */ |
15016 | 224 #endif |