ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/cpjit.h.in
Revision: 1.1
Committed: Fri Oct 14 18:34:22 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 <inttypes.h>
28    
29     namespace cpjit
30     {
31    
32     #define CPJIT_MAX_ARGS «$max_args»
33     #define CPJIT_NULLID ""
34    
35     // environment for persistent or temporary storage
36     struct env
37     {
38     std::string path, idxpath;
39     bool temporary;
40     int idxfd;
41     long idxstamp;
42    
43     void dolock (bool lock);
44     void lock ();
45     void unlock ();
46    
47     void load_index ();
48     void save_index ();
49    
50     public:
51     env (const std::string &path = CPJIT_NULLID, bool temporary = true);
52     ~env ();
53    
54     // compact all fragments && invalidate ALL pointers
55     void compact ();
56     // completely delete the environment on disk
57     void clear ();
58     };
59    
60     struct fun
61     {
62     env &e;
63     std::string id, source;
64     void *funptr;
65    
66     fun (env &e, const std::string &id, const std::string &source);
67    
68     // is compiled function available?
69     bool compiled ()
70     {
71     return funptr;
72     }
73    
74     // returns the function pointer
75     void *ptr ();
76     };
77    
78     struct funbuild : std::ostringstream
79     {
80     env &e;
81     std::string retlist, arglist;
82    
83     funbuild (env &e, const char *rettype, ...);
84     void finish (std::string &id, std::string &source);
85     };
86    
87     typedef unsigned char U8;
88     typedef signed char I8;
89     typedef uint16_t U16;
90     typedef int16_t I16;
91     typedef uint32_t U32;
92     typedef int32_t I32;
93     typedef uint64_t U64;
94     typedef int64_t I64;
95     typedef float FLT;
96     typedef double DBL;
97     typedef void * PTR;
98    
99     template<typename T> struct typestr { };
100    
101     template<> struct typestr<U8 > { static const char str[]; };
102     template<> struct typestr<U16 > { static const char str[]; };
103     template<> struct typestr<U32 > { static const char str[]; };
104     template<> struct typestr<U64 > { static const char str[]; };
105     template<> struct typestr<I8 > { static const char str[]; };
106     template<> struct typestr<I16 > { static const char str[]; };
107     template<> struct typestr<I32 > { static const char str[]; };
108     template<> struct typestr<I64 > { static const char str[]; };
109     template<> struct typestr<FLT > { static const char str[]; };
110     template<> struct typestr<DBL > { static const char str[]; };
111     template<> struct typestr<PTR > { static const char str[]; };
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     template<> struct typestr<FLT *> { static const char str[]; };
121     template<> struct typestr<DBL *> { static const char str[]; };
122    
123     «repeat»
124     template<typename R«, typename A$_|»>
125     struct fun«$_» : fun
126     {
127     fun«$_» (env &e, const std::string &id, const std::string &source)
128     : fun(e, id, source)
129     {
130     }
131    
132     int operator ()(«A$_ a$_|, »)
133     {
134     return ((R (*)(«A$_|, »)) ptr ())(«a$_|, »);
135     }
136     };
137     «end»
138    
139     «repeat»
140     template<typename R«, typename A$_|»>
141     struct funbuild«$_» : funbuild
142     {
143     funbuild«$_» (env &e)
144     : funbuild(e, typestr<R>::str«, typestr<A$_>::str|», (const char *)0)
145     {
146     }
147    
148     fun«$_»<R«, A$_|»> *get ()
149     {
150     std::string id, source;
151     finish (id, source);
152     return new fun«$_»<R«, A$_|»> (e, id, source);
153     }
154     };
155     «end»
156    
157     }
158