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 «$max_args» |
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 |
«repeat» |
110 |
template<typename R«, typename A$_|»> |
111 |
R call («A$_ a$_|, ») |
112 |
{ |
113 |
return ((R (*)(«A$_|, »)) ptr ())(«a$_|, »); |
114 |
} |
115 |
«end» |
116 |
|
117 |
«repeat» |
118 |
template<typename R«, typename A$_|»> |
119 |
R operator ()(«A$_ a$_|, ») |
120 |
{ |
121 |
call<R«, A$_|»> («a$_|, »); |
122 |
} |
123 |
«end» |
124 |
}; |
125 |
|
126 |
// environment for persistent or temporary storage |
127 |
struct env |
128 |
{ |
129 |
std::string path, idxpath; |
130 |
bool temporary; |
131 |
int idxfd; |
132 |
long idxstamp; |
133 |
int nextid; |
134 |
|
135 |
void dolock (bool lock); |
136 |
void lock (); |
137 |
void unlock (); |
138 |
|
139 |
bool load_index (); |
140 |
void save_index (); |
141 |
|
142 |
std::map<funid, std::string> id2file; // id to object/sofile mapping (or empty) |
143 |
std::map<std::string, void *> file2so; // file to loaded so handle |
144 |
std::vector<funsrc> fragments; // not-yet-compiled sources |
145 |
// std::set<fun> funs; // should go away |
146 |
|
147 |
void register_fragment (const funsrc &frag); |
148 |
void *dlsym (const std::string &id, const char *symbol); |
149 |
void *sym (const std::string &id, const char *symbol); |
150 |
|
151 |
public: |
152 |
env (const std::string &path = CPJIT_NULLID, bool temporary = false); |
153 |
~env (); |
154 |
|
155 |
struct fun *get (funid id); |
156 |
|
157 |
// compact all fragments && invalidate ALL pointers |
158 |
void compact (); |
159 |
// completely delete the environment on disk |
160 |
void clear (); |
161 |
}; |
162 |
|
163 |
struct funbuild : funsrc |
164 |
{ |
165 |
std::ostringstream bodystream; |
166 |
env &e; |
167 |
|
168 |
funbuild (env &e, const char *rettype, ...); |
169 |
|
170 |
void header (const std::string &hdr); |
171 |
|
172 |
funbuild &operator <<(const char *src); |
173 |
funbuild &operator <<(const std::string &src); |
174 |
|
175 |
fun *get (); |
176 |
}; |
177 |
|
178 |
«repeat» |
179 |
template<typename R«, typename A$_|»> |
180 |
struct funbuild«$_» : funbuild |
181 |
{ |
182 |
funbuild«$_» (env &e) |
183 |
: funbuild(e, typestr<R>::str«, typestr<A$_>::str|», (const char *)0) |
184 |
{ |
185 |
} |
186 |
}; |
187 |
«end» |
188 |
|
189 |
} |
190 |
|