ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/cpjit.h
Revision: 1.12
Committed: Fri Oct 14 00:21:57 2005 UTC (21 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +10 -0 lines
Log Message:
*** empty log message ***

File Contents

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