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