ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcpjit/cpjit.h
Revision: 1.17
Committed: Mon Oct 17 18:27:05 2005 UTC (20 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +181 -231 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 root 1.17 #include <set>
29 root 1.16 #include <vector>
30    
31 root 1.7 #include <inttypes.h>
32 root 1.6
33 root 1.3 namespace cpjit
34     {
35    
36 root 1.8 #define CPJIT_MAX_ARGS 9
37     #define CPJIT_NULLID ""
38    
39 root 1.7 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 root 1.11 typedef float FLT;
48     typedef double DBL;
49 root 1.9 typedef void * PTR;
50 root 1.7
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 root 1.11 template<> struct typestr<FLT > { static const char str[]; };
62     template<> struct typestr<DBL > { static const char str[]; };
63 root 1.9 template<> struct typestr<PTR > { static const char str[]; };
64 root 1.7 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 root 1.11 template<> struct typestr<FLT *> { static const char str[]; };
73     template<> struct typestr<DBL *> { static const char str[]; };
74 root 1.5
75 root 1.17 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 root 1.8
83 root 1.17 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 root 1.8 {
93 root 1.17 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 root 1.8 {
103 root 1.17 return funptr;
104 root 1.8 }
105    
106 root 1.17 // returns the function pointer
107     void *ptr ();
108    
109    
110     #line 110 "cpjit.h.in"
111     template<typename R>
112     R call ()
113 root 1.8 {
114 root 1.17 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 root 1.8 }
123    
124 root 1.17 #line 110 "cpjit.h.in"
125     template<typename R, typename A1, typename A2>
126     R call (A1 a1, A2 a2)
127 root 1.8 {
128 root 1.17 return ((R (*)(A1, A2)) ptr ())(a1, a2);
129 root 1.8 }
130    
131 root 1.17 #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 root 1.8 {
135 root 1.17 return ((R (*)(A1, A2, A3)) ptr ())(a1, a2, a3);
136 root 1.8 }
137    
138 root 1.17 #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 root 1.15 {
142 root 1.17 return ((R (*)(A1, A2, A3, A4)) ptr ())(a1, a2, a3, a4);
143 root 1.15 }
144    
145 root 1.17 #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 root 1.15 {
149 root 1.17 return ((R (*)(A1, A2, A3, A4, A5)) ptr ())(a1, a2, a3, a4, a5);
150 root 1.15 }
151 root 1.8
152 root 1.17 #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 root 1.5 {
156 root 1.17 return ((R (*)(A1, A2, A3, A4, A5, A6)) ptr ())(a1, a2, a3, a4, a5, a6);
157 root 1.7 }
158 root 1.6
159 root 1.17 #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 root 1.5 {
163 root 1.17 return ((R (*)(A1, A2, A3, A4, A5, A6, A7)) ptr ())(a1, a2, a3, a4, a5, a6, a7);
164 root 1.8 }
165    
166 root 1.17 #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 root 1.8 {
170 root 1.17 return ((R (*)(A1, A2, A3, A4, A5, A6, A7, A8)) ptr ())(a1, a2, a3, a4, a5, a6, a7, a8);
171 root 1.8 }
172    
173 root 1.17 #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 root 1.8 {
177 root 1.17 return ((R (*)(A1, A2, A3, A4, A5, A6, A7, A8, A9)) ptr ())(a1, a2, a3, a4, a5, a6, a7, a8, a9);
178 root 1.8 }
179    
180 root 1.17 #line 116 "cpjit.h.in"
181    
182    
183     #line 118 "cpjit.h.in"
184     template<typename R>
185     R operator ()()
186 root 1.8 {
187 root 1.17 call<R> ();
188 root 1.8 }
189    
190 root 1.17 #line 118 "cpjit.h.in"
191     template<typename R, typename A1>
192     R operator ()(A1 a1)
193 root 1.8 {
194 root 1.17 call<R, A1> (a1);
195 root 1.8 }
196    
197 root 1.17 #line 118 "cpjit.h.in"
198     template<typename R, typename A1, typename A2>
199     R operator ()(A1 a1, A2 a2)
200 root 1.8 {
201 root 1.17 call<R, A1, A2> (a1, a2);
202 root 1.8 }
203    
204 root 1.17 #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 root 1.8 {
208 root 1.17 call<R, A1, A2, A3> (a1, a2, a3);
209 root 1.8 }
210    
211 root 1.17 #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 root 1.8 {
215 root 1.17 call<R, A1, A2, A3, A4> (a1, a2, a3, a4);
216 root 1.8 }
217    
218 root 1.17 #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 root 1.8 {
222 root 1.17 call<R, A1, A2, A3, A4, A5> (a1, a2, a3, a4, a5);
223 root 1.8 }
224    
225 root 1.17 #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 root 1.8 {
229 root 1.17 call<R, A1, A2, A3, A4, A5, A6> (a1, a2, a3, a4, a5, a6);
230 root 1.8 }
231    
232 root 1.17 #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 root 1.8 {
236 root 1.17 call<R, A1, A2, A3, A4, A5, A6, A7> (a1, a2, a3, a4, a5, a6, a7);
237 root 1.8 }
238    
239 root 1.17 #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 root 1.8 {
243 root 1.17 call<R, A1, A2, A3, A4, A5, A6, A7, A8> (a1, a2, a3, a4, a5, a6, a7, a8);
244 root 1.8 }
245    
246 root 1.17 #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 root 1.8 {
250 root 1.17 call<R, A1, A2, A3, A4, A5, A6, A7, A8, A9> (a1, a2, a3, a4, a5, a6, a7, a8, a9);
251 root 1.8 }
252 root 1.17
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 root 1.8 };
292    
293 root 1.17 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 root 1.15
308    
309 root 1.17 #line 179 "cpjit.h.in"
310 root 1.15 template<typename R>
311     struct funbuild0 : funbuild
312 root 1.8 {
313 root 1.15 funbuild0 (env &e)
314     : funbuild(e, typestr<R>::str, (const char *)0)
315 root 1.8 {
316     }
317     };
318    
319 root 1.17 #line 179 "cpjit.h.in"
320 root 1.15 template<typename R, typename A1>
321     struct funbuild1 : funbuild
322 root 1.8 {
323 root 1.15 funbuild1 (env &e)
324     : funbuild(e, typestr<R>::str, typestr<A1>::str, (const char *)0)
325 root 1.8 {
326     }
327     };
328    
329 root 1.17 #line 179 "cpjit.h.in"
330 root 1.15 template<typename R, typename A1, typename A2>
331     struct funbuild2 : funbuild
332 root 1.8 {
333 root 1.15 funbuild2 (env &e)
334     : funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, (const char *)0)
335 root 1.8 {
336     }
337     };
338    
339 root 1.17 #line 179 "cpjit.h.in"
340 root 1.15 template<typename R, typename A1, typename A2, typename A3>
341     struct funbuild3 : funbuild
342 root 1.8 {
343 root 1.15 funbuild3 (env &e)
344     : funbuild(e, typestr<R>::str, typestr<A1>::str, typestr<A2>::str, typestr<A3>::str, (const char *)0)
345 root 1.8 {
346     }
347     };
348    
349 root 1.17 #line 179 "cpjit.h.in"
350 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4>
351     struct funbuild4 : funbuild
352 root 1.8 {
353 root 1.15 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 root 1.8 {
356     }
357     };
358    
359 root 1.17 #line 179 "cpjit.h.in"
360 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
361     struct funbuild5 : funbuild
362 root 1.8 {
363 root 1.15 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 root 1.8 {
366     }
367     };
368    
369 root 1.17 #line 179 "cpjit.h.in"
370 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
371     struct funbuild6 : funbuild
372 root 1.8 {
373 root 1.15 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 root 1.8 {
376     }
377     };
378    
379 root 1.17 #line 179 "cpjit.h.in"
380 root 1.15 template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
381     struct funbuild7 : funbuild
382 root 1.8 {
383 root 1.15 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 root 1.8 {
386     }
387     };
388    
389 root 1.17 #line 179 "cpjit.h.in"
390 root 1.8 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 root 1.13 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 root 1.8 {
396     }
397     };
398    
399 root 1.17 #line 179 "cpjit.h.in"
400 root 1.8 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 root 1.13 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 root 1.8 {
406     }
407 root 1.7 };
408 root 1.6
409 root 1.17 #line 188 "cpjit.h.in"
410 root 1.15
411 root 1.3 }
412 root 1.15