ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/cpjit.C
Revision: 1.7
Committed: Fri Oct 14 01:42:45 2005 UTC (18 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     cpjit.C -- c portable jit in time compiler
3     Copyright (C) 2005 Marc Lehmann <cpjit@schmorp.de>
4    
5     This file is part of libcpjit.
6    
7     libcpjit is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with gvpe; if not, write to the Free Software
19     Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20     */
21    
22     #include "config.h"
23    
24     #include "cpjit.h"
25    
26 root 1.2 #include <cassert>
27     #include <cstdlib>
28 root 1.1 #include <cstdarg>
29 root 1.2 #include <cstdio>
30 root 1.4 #include <cstring>
31     #include <cerrno>
32    
33 root 1.1 #include <sstream>
34 root 1.2 #include <fstream>
35 root 1.4 #include <stdexcept>
36     #include <iomanip>
37    
38     #include <unistd.h>
39     #include <fcntl.h> // for locking
40 root 1.2
41     #include <sys/stat.h>
42     #include <sys/types.h>
43     #include <dirent.h>
44    
45 root 1.4 #include "md5.h"
46    
47 root 1.2 #include <dlfcn.h>
48 root 1.1
49     #define STR_(str) #str
50     #define STR(str) STR_(str)
51    
52     namespace cpjit
53     {
54    
55     const char typestr<U8 >::str[] = "unsigned " STR(TYPE_INT8);
56     const char typestr<U16 >::str[] = "unsigned " STR(TYPE_INT16);
57     const char typestr<U32 >::str[] = "unsigned " STR(TYPE_INT32);
58     const char typestr<U64 >::str[] = "unsigned " STR(TYPE_INT64);
59 root 1.2 const char typestr<I8 >::str[] = "signed " STR(TYPE_INT8);
60     const char typestr<I16 >::str[] = STR(TYPE_INT16);
61     const char typestr<I32 >::str[] = STR(TYPE_INT32);
62     const char typestr<I64 >::str[] = STR(TYPE_INT64);
63 root 1.4 const char typestr<FLT >::str[] = "float";
64     const char typestr<DBL >::str[] = "double";
65 root 1.1
66 root 1.2 const char typestr<PTR >::str[] = "void" " *";
67    
68 root 1.1 const char typestr<U8 *>::str[] = "unsigned " STR(TYPE_INT8) " *";
69     const char typestr<U16 *>::str[] = "unsigned " STR(TYPE_INT16) " *";
70     const char typestr<U32 *>::str[] = "unsigned " STR(TYPE_INT32) " *";
71     const char typestr<U64 *>::str[] = "unsigned " STR(TYPE_INT64) " *";
72 root 1.2 const char typestr<I8 *>::str[] = "signed " STR(TYPE_INT8) " *";
73     const char typestr<I16 *>::str[] = STR(TYPE_INT16) " *";
74     const char typestr<I32 *>::str[] = STR(TYPE_INT32) " *";
75     const char typestr<I64 *>::str[] = STR(TYPE_INT64) " *";
76 root 1.4 const char typestr<FLT *>::str[] = "float" " *";
77     const char typestr<DBL *>::str[] = "double" " *";
78    
79 root 1.5 struct digest : md5_ctx
80 root 1.4 {
81 root 1.5 digest ();
82     void operator ()(const std::string &data);
83 root 1.4 operator std::string();
84     };
85    
86 root 1.5 digest::digest ()
87 root 1.4 {
88 root 1.5 md5_init_ctx (this);
89     }
90 root 1.4
91 root 1.5 void digest::operator ()(const std::string &data)
92     {
93     md5_process_bytes (data.c_str (), data.size (), this);
94 root 1.4 }
95    
96     digest::operator std::string ()
97     {
98 root 1.5 unsigned char data[16];
99 root 1.4 std::ostringstream s;
100    
101 root 1.5 md5_finish_ctx (this, data);
102    
103     for (int i = 0; i < 16; i++)
104 root 1.4 s << std::setfill ('0') << std::hex << std::setw (2) << (int)data [i];
105    
106     return s.str ();
107     }
108 root 1.1
109 root 1.2 static inline bool
110     is_null (const std::string &s)
111     {
112     return s.empty ();
113     }
114    
115 root 1.4 // error exception builder
116     struct error : std::ostringstream
117     {
118     ~error ()
119     {
120     throw std::runtime_error (str ());
121     }
122     };
123    
124     #define FATAL(msg) do { error o; o << msg << std::endl; } while (0)
125     #define ERRNO " (" << strerror (errno) << ")"
126    
127 root 1.2 env::env (const std::string &path, bool temporary)
128     : path(path), temporary(temporary)
129     {
130 root 1.6 mode_t oflags = O_RDWR | O_EXCL;
131    
132 root 1.2 if (is_null (path))
133     {
134     this->path = tmpnam (0);
135    
136     temporary = true;
137     }
138    
139 root 1.6 if (mkdir (this->path.c_str (), 0777) == 0)
140     oflags |= O_CREAT;
141     else
142     if (temporary)
143     FATAL ("error while creating libcpjit temporary directory" << ERRNO);
144    
145 root 1.4 std::string index = this->path + "/libcpjit.idx";
146 root 1.6 idxfd = open (index.c_str (), oflags, 0666);
147 root 1.4 if (idxfd < 0)
148     FATAL (path << ": unable to open index file" << index);
149    
150     lock ();
151     load_index ();
152     unlock ();
153 root 1.2 }
154    
155     env::~env ()
156     {
157     if (temporary)
158 root 1.5 clear ();
159     }
160    
161     void env::clear ()
162     {
163     DIR *dir = opendir (path.c_str ());
164    
165     if (dir)
166 root 1.2 {
167 root 1.5 struct dirent *de;
168     while ((de = readdir (dir)))
169 root 1.2 {
170 root 1.5 if (de->d_name [0] == '.')
171     continue;
172 root 1.2
173 root 1.5 std::string f = path + "/" + de->d_name;
174     unlink (f.c_str ());
175 root 1.2 }
176    
177 root 1.5 closedir (dir);
178 root 1.2 }
179 root 1.5
180     if (rmdir (path.c_str ()))
181     FATAL (path << ": unable to remove libcpjit directory" << ERRNO);
182 root 1.2 }
183    
184 root 1.4 void env::dolock (bool lock)
185     {
186     struct flock fl;
187    
188     fl.l_type = lock ? F_WRLCK : F_UNLCK;
189     fl.l_whence = SEEK_SET;
190     fl.l_start = 0;
191     fl.l_len = 0;
192    
193     while (fcntl (idxfd, F_SETLKW, &fl) < 0)
194     if (errno != EINTR)
195     FATAL (path << ": unable to lock index file" << ERRNO);
196     }
197    
198     void env::lock ()
199     {
200     dolock (true);
201     }
202    
203     void env::unlock ()
204     {
205     dolock (false);
206     }
207    
208     void
209     env::load_index ()
210     {
211     }
212    
213 root 1.5 void
214     env::save_index ()
215 root 1.2 {
216 root 1.5 std::ifstream f ((this->path + "/libcpjit.idx").c_str ());
217     assert (("not a cpjit directory", f));
218     f.close ();
219 root 1.2 }
220    
221 root 1.5 fun::fun (env &e, const std::string &id, const std::string &source)
222     : e(e), id(id), source(source), funptr(0)
223 root 1.3 {
224     }
225    
226 root 1.2 void *fun::ptr ()
227 root 1.1 {
228 root 1.2 if (!funptr)
229     {
230 root 1.5 e.lock ();
231     e.load_index ();
232    
233     std::string base = e.path + "/" + id;
234 root 1.4
235     std::string fc = base + ".c";
236     std::string fo = base + ".o";
237     std::string fso = base + ".so";
238     std::string fa = base + ".a";
239 root 1.2
240     std::ofstream f (fc.c_str ());
241     f << source;
242     f.close ();
243    
244     system (("gcc -O6 -c -o " + fo + " " + fc).c_str ());
245    
246     unlink (fc.c_str ());
247    
248     system (("gcc -O6 -nostdlib -lgcc -shared -o " + fso + " " + fo).c_str ());
249    
250 root 1.4 printf ("%s\n", fso.c_str ());
251    
252 root 1.2 void *so = dlopen (fso.c_str (), RTLD_LAZY);
253     assert (so);
254    
255     funptr = dlsym (so, id.c_str ());
256     assert (funptr);
257 root 1.5
258     e.save_index ();
259     e.unlock ();
260 root 1.2 }
261    
262     return funptr;
263 root 1.1 }
264    
265 root 1.5 funbuild::funbuild (env &e, const char *rettype, ...)
266     : e(e)
267 root 1.1 {
268 root 1.5 retlist = rettype;
269 root 1.3
270 root 1.5 std::ostringstream argl;
271 root 1.1
272     va_list ap;
273     va_start (ap, rettype);
274    
275     int args = 0;
276     while ((rettype = va_arg (ap, const char *)))
277     {
278     if (args++)
279 root 1.5 argl << ", ";
280 root 1.1
281 root 1.5 argl << rettype << " a" << args;
282 root 1.1 }
283    
284     va_end (ap);
285    
286 root 1.5 arglist = argl.str ();
287 root 1.1 }
288    
289 root 1.5 void
290     funbuild::finish (std::string &id, std::string &source)
291 root 1.1 {
292 root 1.5 digest dgst;
293    
294     dgst (retlist);
295     dgst (arglist);
296     dgst (str ());
297    
298     id = "f_" + (std::string) dgst;
299 root 1.3
300 root 1.5 std::ostringstream o;
301     o << retlist << " " << id << " (" << arglist << ")\n{\n" << str () << "\n}\n";
302     source = o.str ();
303 root 1.1 }
304    
305     }