ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/cpjit.h
Revision: 1.16
Committed: Fri Oct 14 23:40:24 2005 UTC (21 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.15: +37 -25 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.2 /*
2 root 1.15 This file is AUTOMAGICALLY created by cpjit.h.in.
3     Modifications in cpjit.h will be lost.
4 root 1.5
5 root 1.2 cpjit.h -- c portable jit in time compiler
6 root 1.15 Copyright (C) 2005 Marc Lehmann <cpjit\@schmorp.de>
7 root 1.2
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 root 1.3
25 root 1.6 #include <string>
26 root 1.8 #include <sstream>
27 root 1.16 #include <map>
28     #include <vector>
29    
30 root 1.7 #include <inttypes.h>
31 root 1.6
32 root 1.3 namespace cpjit
33     {
34    
35 root 1.8 #define CPJIT_MAX_ARGS 9
36     #define CPJIT_NULLID ""
37    
38 root 1.7 // environment for persistent or temporary storage
39     struct env
40     {
41 root 1.14 std::string path, idxpath;
42 root 1.9 bool temporary;
43 root 1.12 int idxfd;
44 root 1.14 long idxstamp;
45 root 1.16 int nextid;
46 root 1.9
47 root 1.12 void dolock (bool lock);
48     void lock ();
49     void unlock ();
50    
51 root 1.16 bool load_index ();
52 root 1.12 void save_index ();
53    
54 root 1.16 std::map<std::string, std::string> id2file; // id to object/sofile mapping (or empty)
55     std::map<std::string, void *> file2so; // file to loaded so handle
56     std::vector< std::pair<std::string, std::string> > fragments; // not-yet-compiled sources
57    
58     void register_fragment (const std::string &id, const std::string &source);
59     void *dlsym (const std::string &id, const char *symbol);
60     void *sym (const std::string &id, const char *symbol);
61    
62 root 1.12 public:
63 root 1.16 env (const std::string &path = CPJIT_NULLID, bool temporary = false);
64 root 1.9 ~env ();
65 root 1.12
66 root 1.15 // compact all fragments && invalidate ALL pointers
67     void compact ();
68 root 1.13 // completely delete the environment on disk
69     void clear ();
70 root 1.7 };
71    
72 root 1.8 struct fun
73 root 1.7 {
74     env &e;
75 root 1.16 std::string id;
76 root 1.9 void *funptr;
77 root 1.8
78 root 1.13 fun (env &e, const std::string &id, const std::string &source);
79 root 1.8
80     // is compiled function available?
81 root 1.9 bool compiled ()
82     {
83     return funptr;
84     }
85 root 1.7
86 root 1.9 // returns the function pointer
87 root 1.7 void *ptr ();
88     };
89    
90 root 1.8 struct funbuild : std::ostringstream
91 root 1.7 {
92 root 1.8 env &e;
93 root 1.13 std::string retlist, arglist;
94 root 1.8
95 root 1.13 funbuild (env &e, const char *rettype, ...);
96     void finish (std::string &id, std::string &source);
97 root 1.7 };
98 root 1.5
99 root 1.7 typedef unsigned char U8;
100     typedef signed char I8;
101     typedef uint16_t U16;
102     typedef int16_t I16;
103     typedef uint32_t U32;
104     typedef int32_t I32;
105     typedef uint64_t U64;
106     typedef int64_t I64;
107 root 1.11 typedef float FLT;
108     typedef double DBL;
109 root 1.9 typedef void * PTR;
110 root 1.7
111     template<typename T> struct typestr { };
112    
113     template<> struct typestr<U8 > { static const char str[]; };
114     template<> struct typestr<U16 > { static const char str[]; };
115     template<> struct typestr<U32 > { static const char str[]; };
116     template<> struct typestr<U64 > { static const char str[]; };
117     template<> struct typestr<I8 > { static const char str[]; };
118     template<> struct typestr<I16 > { static const char str[]; };
119     template<> struct typestr<I32 > { static const char str[]; };
120     template<> struct typestr<I64 > { static const char str[]; };
121 root 1.11 template<> struct typestr<FLT > { static const char str[]; };
122     template<> struct typestr<DBL > { static const char str[]; };
123 root 1.9 template<> struct typestr<PTR > { static const char str[]; };
124 root 1.7 template<> struct typestr<U8 *> { static const char str[]; };
125     template<> struct typestr<U16 *> { static const char str[]; };
126     template<> struct typestr<U32 *> { static const char str[]; };
127     template<> struct typestr<U64 *> { static const char str[]; };
128     template<> struct typestr<I8 *> { static const char str[]; };
129     template<> struct typestr<I16 *> { static const char str[]; };
130     template<> struct typestr<I32 *> { static const char str[]; };
131     template<> struct typestr<I64 *> { static const char str[]; };
132 root 1.11 template<> struct typestr<FLT *> { static const char str[]; };
133     template<> struct typestr<DBL *> { static const char str[]; };
134 root 1.5
135 root 1.8
136 root 1.16 #line 136 "cpjit.h.in"
137 root 1.8 template<typename R>
138     struct fun0 : fun
139     {
140 root 1.13 fun0 (env &e, const std::string &id, const std::string &source)
141     : fun(e, id, source)
142 root 1.8 {
143     }
144    
145     int operator ()()
146     {
147     return ((R (*)()) ptr ())();
148     }
149     };
150    
151 root 1.16 #line 136 "cpjit.h.in"
152 root 1.15 template<typename R, typename A1>
153     struct fun1 : fun
154 root 1.8 {
155 root 1.15 fun1 (env &e, const std::string &id, const std::string &source)
156     : fun(e, id, source)
157 root 1.8 {
158     }
159    
160 root 1.15 int operator ()(A1 a1)
161 root 1.8 {
162 root 1.15 return ((R (*)(A1)) ptr ())(a1);
163 root 1.8 }
164     };
165    
166 root 1.16 #line 136 "cpjit.h.in"
167 root 1.15 template<typename R, typename A1, typename A2>
168     struct fun2 : fun
169     {
170     fun2 (env &e, const std::string &id, const std::string &source)
171     : fun(e, id, source)
172     {
173     }
174    
175     int operator ()(A1 a1, A2 a2)
176     {
177     return ((R (*)(A1, A2)) ptr ())(a1, a2);
178     }
179     };
180 root 1.8
181 root 1.16 #line 136 "cpjit.h.in"
182 root 1.15 template<typename R, typename A1, typename A2, typename A3>
183     struct fun3 : fun
184 root 1.7 {
185 root 1.15 fun3 (env &e, const std::string &id, const std::string &source)
186 root 1.13 : fun(e, id, source)
187 root 1.5 {
188 root 1.7 }
189 root 1.6
190 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3)
191 root 1.5 {
192 root 1.15 return ((R (*)(A1, A2, A3)) ptr ())(a1, a2, a3);
193 root 1.8 }
194     };
195    
196 root 1.16 #line 136 "cpjit.h.in"
197 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4>
198     struct fun4 : fun
199 root 1.8 {
200 root 1.15 fun4 (env &e, const std::string &id, const std::string &source)
201     : fun(e, id, source)
202 root 1.8 {
203     }
204    
205 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3, A4 a4)
206 root 1.8 {
207 root 1.15 return ((R (*)(A1, A2, A3, A4)) ptr ())(a1, a2, a3, a4);
208 root 1.8 }
209     };
210    
211 root 1.16 #line 136 "cpjit.h.in"
212 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
213     struct fun5 : fun
214 root 1.8 {
215 root 1.15 fun5 (env &e, const std::string &id, const std::string &source)
216 root 1.13 : fun(e, id, source)
217 root 1.8 {
218     }
219    
220 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
221 root 1.8 {
222 root 1.15 return ((R (*)(A1, A2, A3, A4, A5)) ptr ())(a1, a2, a3, a4, a5);
223 root 1.8 }
224     };
225    
226 root 1.16 #line 136 "cpjit.h.in"
227 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
228     struct fun6 : fun
229 root 1.8 {
230 root 1.15 fun6 (env &e, const std::string &id, const std::string &source)
231     : fun(e, id, source)
232 root 1.8 {
233     }
234    
235 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
236 root 1.8 {
237 root 1.15 return ((R (*)(A1, A2, A3, A4, A5, A6)) ptr ())(a1, a2, a3, a4, a5, a6);
238 root 1.8 }
239     };
240    
241 root 1.16 #line 136 "cpjit.h.in"
242 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
243     struct fun7 : fun
244 root 1.8 {
245 root 1.15 fun7 (env &e, const std::string &id, const std::string &source)
246 root 1.13 : fun(e, id, source)
247 root 1.8 {
248     }
249    
250 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7)
251 root 1.8 {
252 root 1.15 return ((R (*)(A1, A2, A3, A4, A5, A6, A7)) ptr ())(a1, a2, a3, a4, a5, a6, a7);
253 root 1.8 }
254     };
255    
256 root 1.16 #line 136 "cpjit.h.in"
257 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
258     struct fun8 : fun
259 root 1.8 {
260 root 1.15 fun8 (env &e, const std::string &id, const std::string &source)
261     : fun(e, id, source)
262 root 1.8 {
263     }
264    
265 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8)
266 root 1.8 {
267 root 1.15 return ((R (*)(A1, A2, A3, A4, A5, A6, A7, A8)) ptr ())(a1, a2, a3, a4, a5, a6, a7, a8);
268 root 1.8 }
269     };
270    
271 root 1.16 #line 136 "cpjit.h.in"
272 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
273     struct fun9 : fun
274 root 1.8 {
275 root 1.15 fun9 (env &e, const std::string &id, const std::string &source)
276 root 1.13 : fun(e, id, source)
277 root 1.8 {
278     }
279    
280 root 1.15 int operator ()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9)
281 root 1.8 {
282 root 1.15 return ((R (*)(A1, A2, A3, A4, A5, A6, A7, A8, A9)) ptr ())(a1, a2, a3, a4, a5, a6, a7, a8, a9);
283 root 1.8 }
284     };
285    
286 root 1.16 #line 150 "cpjit.h.in"
287 root 1.15
288    
289 root 1.16 #line 152 "cpjit.h.in"
290 root 1.15 template<typename R>
291     struct funbuild0 : funbuild
292 root 1.8 {
293 root 1.15 funbuild0 (env &e)
294     : funbuild(e, typestr<R>::str, (const char *)0)
295 root 1.8 {
296     }
297    
298 root 1.15 fun0<R> *get ()
299 root 1.8 {
300 root 1.13 std::string id, source;
301     finish (id, source);
302 root 1.15 return new fun0<R> (e, id, source);
303 root 1.8 }
304     };
305    
306 root 1.16 #line 152 "cpjit.h.in"
307 root 1.15 template<typename R, typename A1>
308     struct funbuild1 : funbuild
309 root 1.8 {
310 root 1.15 funbuild1 (env &e)
311     : funbuild(e, typestr<R>::str, typestr<A1>::str, (const char *)0)
312 root 1.8 {
313     }
314    
315 root 1.15 fun1<R, A1> *get ()
316 root 1.8 {
317 root 1.15 std::string id, source;
318     finish (id, source);
319     return new fun1<R, A1> (e, id, source);
320 root 1.8 }
321     };
322    
323 root 1.16 #line 152 "cpjit.h.in"
324 root 1.15 template<typename R, typename A1, typename A2>
325     struct funbuild2 : funbuild
326 root 1.8 {
327 root 1.15 funbuild2 (env &e)
328     : funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, (const char *)0)
329 root 1.8 {
330     }
331    
332 root 1.15 fun2<R, A1, A2> *get ()
333 root 1.8 {
334 root 1.13 std::string id, source;
335     finish (id, source);
336 root 1.15 return new fun2<R, A1, A2> (e, id, source);
337 root 1.8 }
338     };
339    
340 root 1.16 #line 152 "cpjit.h.in"
341 root 1.15 template<typename R, typename A1, typename A2, typename A3>
342     struct funbuild3 : funbuild
343 root 1.8 {
344 root 1.15 funbuild3 (env &e)
345     : funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, (const char *)0)
346 root 1.8 {
347     }
348    
349 root 1.15 fun3<R, A1, A2, A3> *get ()
350 root 1.8 {
351 root 1.15 std::string id, source;
352     finish (id, source);
353     return new fun3<R, A1, A2, A3> (e, id, source);
354 root 1.8 }
355     };
356    
357 root 1.16 #line 152 "cpjit.h.in"
358 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4>
359     struct funbuild4 : funbuild
360 root 1.8 {
361 root 1.15 funbuild4 (env &e)
362     : funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, (const char *)0)
363 root 1.8 {
364     }
365    
366 root 1.15 fun4<R, A1, A2, A3, A4> *get ()
367 root 1.8 {
368 root 1.13 std::string id, source;
369     finish (id, source);
370 root 1.15 return new fun4<R, A1, A2, A3, A4> (e, id, source);
371 root 1.8 }
372     };
373    
374 root 1.16 #line 152 "cpjit.h.in"
375 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
376     struct funbuild5 : funbuild
377 root 1.8 {
378 root 1.15 funbuild5 (env &e)
379     : funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, typestr<A4>::str, typestr<A5>::str, (const char *)0)
380 root 1.8 {
381     }
382    
383 root 1.15 fun5<R, A1, A2, A3, A4, A5> *get ()
384 root 1.8 {
385 root 1.15 std::string id, source;
386     finish (id, source);
387     return new fun5<R, A1, A2, A3, A4, A5> (e, id, source);
388 root 1.8 }
389     };
390    
391 root 1.16 #line 152 "cpjit.h.in"
392 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
393     struct funbuild6 : funbuild
394 root 1.8 {
395 root 1.15 funbuild6 (env &e)
396     : 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)
397 root 1.8 {
398     }
399    
400 root 1.15 fun6<R, A1, A2, A3, A4, A5, A6> *get ()
401 root 1.8 {
402 root 1.13 std::string id, source;
403     finish (id, source);
404 root 1.15 return new fun6<R, A1, A2, A3, A4, A5, A6> (e, id, source);
405 root 1.8 }
406     };
407    
408 root 1.16 #line 152 "cpjit.h.in"
409 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
410     struct funbuild7 : funbuild
411 root 1.8 {
412 root 1.15 funbuild7 (env &e)
413     : 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)
414 root 1.8 {
415     }
416    
417 root 1.15 fun7<R, A1, A2, A3, A4, A5, A6, A7> *get ()
418 root 1.8 {
419 root 1.15 std::string id, source;
420     finish (id, source);
421     return new fun7<R, A1, A2, A3, A4, A5, A6, A7> (e, id, source);
422 root 1.8 }
423     };
424    
425 root 1.16 #line 152 "cpjit.h.in"
426 root 1.8 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
427     struct funbuild8 : funbuild
428     {
429 root 1.13 funbuild8 (env &e)
430     : 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)
431 root 1.8 {
432     }
433    
434     fun8<R, A1, A2, A3, A4, A5, A6, A7, A8> *get ()
435     {
436 root 1.13 std::string id, source;
437     finish (id, source);
438     return new fun8<R, A1, A2, A3, A4, A5, A6, A7, A8> (e, id, source);
439 root 1.8 }
440     };
441    
442 root 1.16 #line 152 "cpjit.h.in"
443 root 1.8 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
444     struct funbuild9 : funbuild
445     {
446 root 1.13 funbuild9 (env &e)
447     : 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)
448 root 1.8 {
449     }
450    
451     fun9<R, A1, A2, A3, A4, A5, A6, A7, A8, A9> *get ()
452     {
453 root 1.13 std::string id, source;
454     finish (id, source);
455     return new fun9<R, A1, A2, A3, A4, A5, A6, A7, A8, A9> (e, id, source);
456 root 1.7 }
457     };
458 root 1.6
459 root 1.16 #line 168 "cpjit.h.in"
460 root 1.15
461 root 1.3 }
462 root 1.15