ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libcpjit/cpjit.h.PL
Revision: 1.7
Committed: Wed Oct 12 00:51:49 2005 UTC (21 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +17 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/usr/bin/perl
2    
3     # create cpjit.h
4    
5     open STDOUT, ">cpjit.h"
6     or die "cpijit.h: $!";
7    
8 root 1.6 $CPJIT_MAX_ARGS = 9;
9    
10 root 1.1 print <<EOF;
11 root 1.6 #line 12 "cpjit.h.PL"
12 root 1.1 /*
13 root 1.2 This file is AUTOMAGICALLY created by cpjit.h.PL. Modifications will
14     be lost.
15    
16 root 1.1 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 root 1.3 #include <string>
37 root 1.6 #include <sstream>
38 root 1.4 #include <inttypes.h>
39 root 1.3
40 root 1.1 namespace cpjit
41     {
42    
43 root 1.6 #define CPJIT_MAX_ARGS $CPJIT_MAX_ARGS
44     #define CPJIT_NULLID ""
45    
46 root 1.5 typedef std::string idstr;
47 root 1.3
48 root 1.5 // environment for persistent or temporary storage
49     struct env
50     {
51 root 1.7 std::string path;
52     bool temporary;
53     unsigned long nextid;
54    
55     env (const std::string &path = CPJIT_NULLID, bool temporary = true);
56     ~env ();
57     std::string genid ();
58 root 1.5 };
59    
60 root 1.6 struct fun
61 root 1.5 {
62     env &e;
63     idstr id;
64 root 1.6 std::string source;
65 root 1.7 void *funptr;
66 root 1.6
67     fun (env &e, const idstr &id);
68 root 1.5
69 root 1.6 // is compiled function available?
70 root 1.7 bool compiled ()
71     {
72     return funptr;
73     }
74 root 1.6
75     void set_source (const std::string source)
76     {
77     this->source = source;
78     }
79 root 1.5
80 root 1.7 // returns the function pointer
81 root 1.5 void *ptr ();
82     };
83    
84 root 1.6 struct funbuild : std::ostringstream
85 root 1.5 {
86 root 1.6 env &e;
87     idstr id;
88    
89     funbuild (env &e, const idstr &id, const char *rettype, ...);
90     void finish ();
91    
92     fun *get ()
93     {
94     finish ();
95     fun *f = new fun (e, id);
96     f->set_source (str ());
97     return f;
98     }
99 root 1.5 };
100 root 1.2
101 root 1.5 typedef unsigned char U8;
102     typedef signed char I8;
103     typedef uint16_t U16;
104     typedef int16_t I16;
105     typedef uint32_t U32;
106     typedef int32_t I32;
107     typedef uint64_t U64;
108     typedef int64_t I64;
109     typedef float F32;
110     typedef double F64;
111 root 1.7 typedef void * PTR;
112 root 1.5
113     template<typename T> struct typestr { };
114    
115     template<> struct typestr<U8 > { static const char str[]; };
116     template<> struct typestr<U16 > { static const char str[]; };
117     template<> struct typestr<U32 > { static const char str[]; };
118     template<> struct typestr<U64 > { static const char str[]; };
119     template<> struct typestr<I8 > { static const char str[]; };
120     template<> struct typestr<I16 > { static const char str[]; };
121     template<> struct typestr<I32 > { static const char str[]; };
122     template<> struct typestr<I64 > { static const char str[]; };
123     template<> struct typestr<F32 > { static const char str[]; };
124     template<> struct typestr<F64 > { static const char str[]; };
125 root 1.7 template<> struct typestr<PTR > { static const char str[]; };
126 root 1.5 template<> struct typestr<U8 *> { static const char str[]; };
127     template<> struct typestr<U16 *> { static const char str[]; };
128     template<> struct typestr<U32 *> { static const char str[]; };
129     template<> struct typestr<U64 *> { static const char str[]; };
130     template<> struct typestr<I8 *> { static const char str[]; };
131     template<> struct typestr<I16 *> { static const char str[]; };
132     template<> struct typestr<I32 *> { static const char str[]; };
133     template<> struct typestr<I64 *> { static const char str[]; };
134     template<> struct typestr<F32 *> { static const char str[]; };
135     template<> struct typestr<F64 *> { static const char str[]; };
136 root 1.2
137 root 1.6 EOF
138    
139     for (0 .. $CPJIT_MAX_ARGS) {
140     print <<EOF;
141 root 1.7 #line 143 "cpjit.h.PL"
142 root 1.6
143     template<typename R${\( join "", map ", typename A$_", 1..$_ )}>
144     struct fun$_ : fun
145     {
146 root 1.7 fun$_ (env &e, const idstr &id)
147 root 1.6 : fun (e, id)
148     {
149     }
150    
151     int operator ()(${\( join ", ", map "A$_ a$_", 1..$_ )})
152     {
153     return ((R (*)(${\( join ", ", map "A$_", 1..$_ )})) ptr ())(${\( join ", ", map "a$_", 1..$_ )});
154     }
155     };
156    
157     template<typename R${\( join "", map ", typename A$_", 1..$_ )}>
158     struct funbuild$_ : funbuild
159 root 1.5 {
160 root 1.6 funbuild$_ (env &e, const idstr &id = CPJIT_NULLID)
161     : funbuild (e, id, typestr<R>::str${\( join "", map ", typestr<A$_>::str", 1..$_ )}, (const char *)0)
162 root 1.2 {
163 root 1.5 }
164 root 1.3
165 root 1.6 fun$_<R${\( join "", map ", A$_", 1..$_ )}> *get ()
166 root 1.2 {
167 root 1.6 finish ();
168     fun$_<R${\( join "", map ", A$_", 1..$_ )}> *f = new fun$_<R${\( join "", map ", A$_", 1..$_ )}> (e, id);
169     f->set_source (str ());
170     return f;
171 root 1.5 }
172     };
173 root 1.3
174 root 1.1 EOF
175 root 1.6 }
176 root 1.1
177     print <<EOF;
178     }
179     EOF
180