1 |
#!/usr/bin/perl |
2 |
|
3 |
# create cpjit.h |
4 |
|
5 |
open STDOUT, ">cpjit.h" |
6 |
or die "cpijit.h: $!"; |
7 |
|
8 |
$CPJIT_MAX_ARGS = 9; |
9 |
|
10 |
print <<EOF; |
11 |
#line 12 "cpjit.h.PL" |
12 |
/* |
13 |
This file is AUTOMAGICALLY created by cpjit.h.PL. Modifications will |
14 |
be lost. |
15 |
|
16 |
cpjit.h -- c portable jit in time compiler |
17 |
Copyright (C) 2005 Marc Lehmann <cpjit\@schmorp.de> |
18 |
|
19 |
This file is part of libcpjit. |
20 |
|
21 |
libcpjit is free software; you can redistribute it and/or modify |
22 |
it under the terms of the GNU General Public License as published by |
23 |
the Free Software Foundation; either version 2 of the License, or |
24 |
(at your option) any later version. |
25 |
|
26 |
This program is distributed in the hope that it will be useful, |
27 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
28 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
29 |
GNU General Public License for more details. |
30 |
|
31 |
You should have received a copy of the GNU General Public License |
32 |
along with gvpe; if not, write to the Free Software |
33 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
34 |
*/ |
35 |
|
36 |
#include <string> |
37 |
#include <sstream> |
38 |
#include <inttypes.h> |
39 |
|
40 |
namespace cpjit |
41 |
{ |
42 |
|
43 |
#define CPJIT_MAX_ARGS $CPJIT_MAX_ARGS |
44 |
#define CPJIT_NULLID "" |
45 |
|
46 |
// environment for persistent or temporary storage |
47 |
struct env |
48 |
{ |
49 |
std::string path, idxpath; |
50 |
bool temporary; |
51 |
int idxfd; |
52 |
long idxstamp; |
53 |
|
54 |
void dolock (bool lock); |
55 |
void lock (); |
56 |
void unlock (); |
57 |
|
58 |
void load_index (); |
59 |
void save_index (); |
60 |
|
61 |
public: |
62 |
env (const std::string &path = CPJIT_NULLID, bool temporary = true); |
63 |
~env (); |
64 |
|
65 |
// compact all fragments && invalidate ALL pointers |
66 |
void compact (); |
67 |
// completely delete the environment on disk |
68 |
void clear (); |
69 |
}; |
70 |
|
71 |
struct fun |
72 |
{ |
73 |
env &e; |
74 |
std::string id, source; |
75 |
void *funptr; |
76 |
|
77 |
fun (env &e, const std::string &id, const std::string &source); |
78 |
|
79 |
// is compiled function available? |
80 |
bool compiled () |
81 |
{ |
82 |
return funptr; |
83 |
} |
84 |
|
85 |
// returns the function pointer |
86 |
void *ptr (); |
87 |
}; |
88 |
|
89 |
struct funbuild : std::ostringstream |
90 |
{ |
91 |
env &e; |
92 |
std::string retlist, arglist; |
93 |
|
94 |
funbuild (env &e, const char *rettype, ...); |
95 |
void finish (std::string &id, std::string &source); |
96 |
}; |
97 |
|
98 |
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 |
typedef float FLT; |
107 |
typedef double DBL; |
108 |
typedef void * PTR; |
109 |
|
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 |
template<> struct typestr<FLT > { static const char str[]; }; |
121 |
template<> struct typestr<DBL > { static const char str[]; }; |
122 |
template<> struct typestr<PTR > { static const char str[]; }; |
123 |
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 |
template<> struct typestr<FLT *> { static const char str[]; }; |
132 |
template<> struct typestr<DBL *> { static const char str[]; }; |
133 |
|
134 |
EOF |
135 |
|
136 |
for (0 .. $CPJIT_MAX_ARGS) { |
137 |
print <<EOF; |
138 |
#line 133 "cpjit.h.PL" |
139 |
|
140 |
template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
141 |
struct fun$_ : fun |
142 |
{ |
143 |
fun$_ (env &e, const std::string &id, const std::string &source) |
144 |
: fun(e, id, source) |
145 |
{ |
146 |
} |
147 |
|
148 |
int operator ()(${\( join ", ", map "A$_ a$_", 1..$_ )}) |
149 |
{ |
150 |
return ((R (*)(${\( join ", ", map "A$_", 1..$_ )})) ptr ())(${\( join ", ", map "a$_", 1..$_ )}); |
151 |
} |
152 |
}; |
153 |
|
154 |
template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
155 |
struct funbuild$_ : funbuild |
156 |
{ |
157 |
funbuild$_ (env &e) |
158 |
: funbuild(e, typestr<R>::str${\( join "", map ", typestr<A$_>::str", 1..$_ )}, (const char *)0) |
159 |
{ |
160 |
} |
161 |
|
162 |
fun$_<R${\( join "", map ", A$_", 1..$_ )}> *get () |
163 |
{ |
164 |
std::string id, source; |
165 |
finish (id, source); |
166 |
return new fun$_<R${\( join "", map ", A$_", 1..$_ )}> (e, id, source); |
167 |
} |
168 |
}; |
169 |
|
170 |
EOF |
171 |
} |
172 |
|
173 |
print <<EOF; |
174 |
} |
175 |
EOF |
176 |
|