1 |
/* |
2 |
This file is AUTOMAGICALLY created by cpjit.h.in. |
3 |
Modifications in cpjit.h will be lost. |
4 |
|
5 |
cpjit.h -- c portable jit in time compiler |
6 |
Copyright (C) 2005 Marc Lehmann <cpjit\@schmorp.de> |
7 |
|
8 |
This file is part of libcpjit. |
9 |
|
10 |
libcpjit is free software; you can redistribute it and/or modify |
11 |
it under the terms of the GNU General Public License as published by |
12 |
the Free Software Foundation; either version 2 of the License, or |
13 |
(at your option) any later version. |
14 |
|
15 |
This program is distributed in the hope that it will be useful, |
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
GNU General Public License for more details. |
19 |
|
20 |
You should have received a copy of the GNU General Public License |
21 |
along with gvpe; if not, write to the Free Software |
22 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
*/ |
24 |
|
25 |
#include <string> |
26 |
#include <sstream> |
27 |
#include <map> |
28 |
#include <set> |
29 |
#include <vector> |
30 |
|
31 |
#include <inttypes.h> |
32 |
|
33 |
namespace cpjit |
34 |
{ |
35 |
|
36 |
#define CPJIT_MAX_ARGS 9 |
37 |
#define CPJIT_NULLID "" |
38 |
|
39 |
typedef unsigned char U8; |
40 |
typedef signed char I8; |
41 |
typedef uint16_t U16; |
42 |
typedef int16_t I16; |
43 |
typedef uint32_t U32; |
44 |
typedef int32_t I32; |
45 |
typedef uint64_t U64; |
46 |
typedef int64_t I64; |
47 |
typedef float FLT; |
48 |
typedef double DBL; |
49 |
typedef void * PTR; |
50 |
|
51 |
template<typename T> struct typestr { }; |
52 |
|
53 |
template<> struct typestr<U8 > { static const char str[]; }; |
54 |
template<> struct typestr<U16 > { static const char str[]; }; |
55 |
template<> struct typestr<U32 > { static const char str[]; }; |
56 |
template<> struct typestr<U64 > { static const char str[]; }; |
57 |
template<> struct typestr<I8 > { static const char str[]; }; |
58 |
template<> struct typestr<I16 > { static const char str[]; }; |
59 |
template<> struct typestr<I32 > { static const char str[]; }; |
60 |
template<> struct typestr<I64 > { static const char str[]; }; |
61 |
template<> struct typestr<FLT > { static const char str[]; }; |
62 |
template<> struct typestr<DBL > { static const char str[]; }; |
63 |
template<> struct typestr<PTR > { static const char str[]; }; |
64 |
template<> struct typestr<U8 *> { static const char str[]; }; |
65 |
template<> struct typestr<U16 *> { static const char str[]; }; |
66 |
template<> struct typestr<U32 *> { static const char str[]; }; |
67 |
template<> struct typestr<U64 *> { static const char str[]; }; |
68 |
template<> struct typestr<I8 *> { static const char str[]; }; |
69 |
template<> struct typestr<I16 *> { static const char str[]; }; |
70 |
template<> struct typestr<I32 *> { static const char str[]; }; |
71 |
template<> struct typestr<I64 *> { static const char str[]; }; |
72 |
template<> struct typestr<FLT *> { static const char str[]; }; |
73 |
template<> struct typestr<DBL *> { static const char str[]; }; |
74 |
|
75 |
typedef std::string funid; |
76 |
|
77 |
// the source of a function |
78 |
struct funsrc { |
79 |
funid id; // the unique id (md5 of source) |
80 |
|
81 |
std::set<std::string> hdrlist; // any unique headers |
82 |
|
83 |
std::string retlist; // the return type |
84 |
std::string arglist; // the argument declaration |
85 |
std::string body; // the function body |
86 |
|
87 |
funsrc (); |
88 |
void update_id (const char *pfx); |
89 |
}; |
90 |
|
91 |
struct fun |
92 |
{ |
93 |
struct env &e; |
94 |
funid id; |
95 |
|
96 |
void *funptr; |
97 |
|
98 |
fun (env &e, const funid &id); |
99 |
|
100 |
// is compiled function available? |
101 |
bool compiled () |
102 |
{ |
103 |
return funptr; |
104 |
} |
105 |
|
106 |
// returns the function pointer |
107 |
void *ptr (); |
108 |
|
109 |
|
110 |
#line 110 "cpjit.h.in" |
111 |
template<typename R> |
112 |
R call () |
113 |
{ |
114 |
return ((R (*)()) ptr ())(); |
115 |
} |
116 |
|
117 |
#line 110 "cpjit.h.in" |
118 |
template<typename R, typename A1> |
119 |
R call (A1 a1) |
120 |
{ |
121 |
return ((R (*)(A1)) ptr ())(a1); |
122 |
} |
123 |
|
124 |
#line 110 "cpjit.h.in" |
125 |
template<typename R, typename A1, typename A2> |
126 |
R call (A1 a1, A2 a2) |
127 |
{ |
128 |
return ((R (*)(A1, A2)) ptr ())(a1, a2); |
129 |
} |
130 |
|
131 |
#line 110 "cpjit.h.in" |
132 |
template<typename R, typename A1, typename A2, typename A3> |
133 |
R call (A1 a1, A2 a2, A3 a3) |
134 |
{ |
135 |
return ((R (*)(A1, A2, A3)) ptr ())(a1, a2, a3); |
136 |
} |
137 |
|
138 |
#line 110 "cpjit.h.in" |
139 |
template<typename R, typename A1, typename A2, typename A3, typename A4> |
140 |
R call (A1 a1, A2 a2, A3 a3, A4 a4) |
141 |
{ |
142 |
return ((R (*)(A1, A2, A3, A4)) ptr ())(a1, a2, a3, a4); |
143 |
} |
144 |
|
145 |
#line 110 "cpjit.h.in" |
146 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5> |
147 |
R call (A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) |
148 |
{ |
149 |
return ((R (*)(A1, A2, A3, A4, A5)) ptr ())(a1, a2, a3, a4, a5); |
150 |
} |
151 |
|
152 |
#line 110 "cpjit.h.in" |
153 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> |
154 |
R call (A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) |
155 |
{ |
156 |
return ((R (*)(A1, A2, A3, A4, A5, A6)) ptr ())(a1, a2, a3, a4, a5, a6); |
157 |
} |
158 |
|
159 |
#line 110 "cpjit.h.in" |
160 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> |
161 |
R call (A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) |
162 |
{ |
163 |
return ((R (*)(A1, A2, A3, A4, A5, A6, A7)) ptr ())(a1, a2, a3, a4, a5, a6, a7); |
164 |
} |
165 |
|
166 |
#line 110 "cpjit.h.in" |
167 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> |
168 |
R call (A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) |
169 |
{ |
170 |
return ((R (*)(A1, A2, A3, A4, A5, A6, A7, A8)) ptr ())(a1, a2, a3, a4, a5, a6, a7, a8); |
171 |
} |
172 |
|
173 |
#line 110 "cpjit.h.in" |
174 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> |
175 |
R call (A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) |
176 |
{ |
177 |
return ((R (*)(A1, A2, A3, A4, A5, A6, A7, A8, A9)) ptr ())(a1, a2, a3, a4, a5, a6, a7, a8, a9); |
178 |
} |
179 |
|
180 |
#line 116 "cpjit.h.in" |
181 |
|
182 |
|
183 |
#line 118 "cpjit.h.in" |
184 |
template<typename R> |
185 |
R operator ()() |
186 |
{ |
187 |
call<R> (); |
188 |
} |
189 |
|
190 |
#line 118 "cpjit.h.in" |
191 |
template<typename R, typename A1> |
192 |
R operator ()(A1 a1) |
193 |
{ |
194 |
call<R, A1> (a1); |
195 |
} |
196 |
|
197 |
#line 118 "cpjit.h.in" |
198 |
template<typename R, typename A1, typename A2> |
199 |
R operator ()(A1 a1, A2 a2) |
200 |
{ |
201 |
call<R, A1, A2> (a1, a2); |
202 |
} |
203 |
|
204 |
#line 118 "cpjit.h.in" |
205 |
template<typename R, typename A1, typename A2, typename A3> |
206 |
R operator ()(A1 a1, A2 a2, A3 a3) |
207 |
{ |
208 |
call<R, A1, A2, A3> (a1, a2, a3); |
209 |
} |
210 |
|
211 |
#line 118 "cpjit.h.in" |
212 |
template<typename R, typename A1, typename A2, typename A3, typename A4> |
213 |
R operator ()(A1 a1, A2 a2, A3 a3, A4 a4) |
214 |
{ |
215 |
call<R, A1, A2, A3, A4> (a1, a2, a3, a4); |
216 |
} |
217 |
|
218 |
#line 118 "cpjit.h.in" |
219 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5> |
220 |
R operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) |
221 |
{ |
222 |
call<R, A1, A2, A3, A4, A5> (a1, a2, a3, a4, a5); |
223 |
} |
224 |
|
225 |
#line 118 "cpjit.h.in" |
226 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> |
227 |
R operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) |
228 |
{ |
229 |
call<R, A1, A2, A3, A4, A5, A6> (a1, a2, a3, a4, a5, a6); |
230 |
} |
231 |
|
232 |
#line 118 "cpjit.h.in" |
233 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> |
234 |
R operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) |
235 |
{ |
236 |
call<R, A1, A2, A3, A4, A5, A6, A7> (a1, a2, a3, a4, a5, a6, a7); |
237 |
} |
238 |
|
239 |
#line 118 "cpjit.h.in" |
240 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> |
241 |
R operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) |
242 |
{ |
243 |
call<R, A1, A2, A3, A4, A5, A6, A7, A8> (a1, a2, a3, a4, a5, a6, a7, a8); |
244 |
} |
245 |
|
246 |
#line 118 "cpjit.h.in" |
247 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> |
248 |
R operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) |
249 |
{ |
250 |
call<R, A1, A2, A3, A4, A5, A6, A7, A8, A9> (a1, a2, a3, a4, a5, a6, a7, a8, a9); |
251 |
} |
252 |
|
253 |
#line 124 "cpjit.h.in" |
254 |
}; |
255 |
|
256 |
// environment for persistent or temporary storage |
257 |
struct env |
258 |
{ |
259 |
std::string path, idxpath; |
260 |
bool temporary; |
261 |
int idxfd; |
262 |
long idxstamp; |
263 |
int nextid; |
264 |
|
265 |
void dolock (bool lock); |
266 |
void lock (); |
267 |
void unlock (); |
268 |
|
269 |
bool load_index (); |
270 |
void save_index (); |
271 |
|
272 |
std::map<funid, std::string> id2file; // id to object/sofile mapping (or empty) |
273 |
std::map<std::string, void *> file2so; // file to loaded so handle |
274 |
std::vector<funsrc> fragments; // not-yet-compiled sources |
275 |
// std::set<fun> funs; // should go away |
276 |
|
277 |
void register_fragment (const funsrc &frag); |
278 |
void *dlsym (const std::string &id, const char *symbol); |
279 |
void *sym (const std::string &id, const char *symbol); |
280 |
|
281 |
public: |
282 |
env (const std::string &path = CPJIT_NULLID, bool temporary = false); |
283 |
~env (); |
284 |
|
285 |
struct fun *get (funid id); |
286 |
|
287 |
// compact all fragments && invalidate ALL pointers |
288 |
void compact (); |
289 |
// completely delete the environment on disk |
290 |
void clear (); |
291 |
}; |
292 |
|
293 |
struct funbuild : funsrc |
294 |
{ |
295 |
std::ostringstream bodystream; |
296 |
env &e; |
297 |
|
298 |
funbuild (env &e, const char *rettype, ...); |
299 |
|
300 |
void header (const std::string &hdr); |
301 |
|
302 |
funbuild &operator <<(const char *src); |
303 |
funbuild &operator <<(const std::string &src); |
304 |
|
305 |
fun *get (); |
306 |
}; |
307 |
|
308 |
|
309 |
#line 179 "cpjit.h.in" |
310 |
template<typename R> |
311 |
struct funbuild0 : funbuild |
312 |
{ |
313 |
funbuild0 (env &e) |
314 |
: funbuild(e, typestr<R>::str, (const char *)0) |
315 |
{ |
316 |
} |
317 |
}; |
318 |
|
319 |
#line 179 "cpjit.h.in" |
320 |
template<typename R, typename A1> |
321 |
struct funbuild1 : funbuild |
322 |
{ |
323 |
funbuild1 (env &e) |
324 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, (const char *)0) |
325 |
{ |
326 |
} |
327 |
}; |
328 |
|
329 |
#line 179 "cpjit.h.in" |
330 |
template<typename R, typename A1, typename A2> |
331 |
struct funbuild2 : funbuild |
332 |
{ |
333 |
funbuild2 (env &e) |
334 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, (const char *)0) |
335 |
{ |
336 |
} |
337 |
}; |
338 |
|
339 |
#line 179 "cpjit.h.in" |
340 |
template<typename R, typename A1, typename A2, typename A3> |
341 |
struct funbuild3 : funbuild |
342 |
{ |
343 |
funbuild3 (env &e) |
344 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, (const char *)0) |
345 |
{ |
346 |
} |
347 |
}; |
348 |
|
349 |
#line 179 "cpjit.h.in" |
350 |
template<typename R, typename A1, typename A2, typename A3, typename A4> |
351 |
struct funbuild4 : funbuild |
352 |
{ |
353 |
funbuild4 (env &e) |
354 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, (const char *)0) |
355 |
{ |
356 |
} |
357 |
}; |
358 |
|
359 |
#line 179 "cpjit.h.in" |
360 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5> |
361 |
struct funbuild5 : funbuild |
362 |
{ |
363 |
funbuild5 (env &e) |
364 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, typestr<A5>::str, (const char *)0) |
365 |
{ |
366 |
} |
367 |
}; |
368 |
|
369 |
#line 179 "cpjit.h.in" |
370 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> |
371 |
struct funbuild6 : funbuild |
372 |
{ |
373 |
funbuild6 (env &e) |
374 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, typestr<A5>::str, typestr<A6>::str, (const char *)0) |
375 |
{ |
376 |
} |
377 |
}; |
378 |
|
379 |
#line 179 "cpjit.h.in" |
380 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> |
381 |
struct funbuild7 : funbuild |
382 |
{ |
383 |
funbuild7 (env &e) |
384 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, typestr<A5>::str, typestr<A6>::str, typestr<A7>::str, (const char *)0) |
385 |
{ |
386 |
} |
387 |
}; |
388 |
|
389 |
#line 179 "cpjit.h.in" |
390 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> |
391 |
struct funbuild8 : funbuild |
392 |
{ |
393 |
funbuild8 (env &e) |
394 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, typestr<A5>::str, typestr<A6>::str, typestr<A7>::str, typestr<A8>::str, (const char *)0) |
395 |
{ |
396 |
} |
397 |
}; |
398 |
|
399 |
#line 179 "cpjit.h.in" |
400 |
template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> |
401 |
struct funbuild9 : funbuild |
402 |
{ |
403 |
funbuild9 (env &e) |
404 |
: funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, typestr<A5>::str, typestr<A6>::str, typestr<A7>::str, typestr<A8>::str, typestr<A9>::str, (const char *)0) |
405 |
{ |
406 |
} |
407 |
}; |
408 |
|
409 |
#line 188 "cpjit.h.in" |
410 |
|
411 |
} |
412 |
|