… | |
… | |
3 | # create cpjit.h |
3 | # create cpjit.h |
4 | |
4 | |
5 | open STDOUT, ">cpjit.h" |
5 | open STDOUT, ">cpjit.h" |
6 | or die "cpijit.h: $!"; |
6 | or die "cpijit.h: $!"; |
7 | |
7 | |
|
|
8 | $CPJIT_MAX_ARGS = 9; |
|
|
9 | |
8 | print <<EOF; |
10 | print <<EOF; |
9 | #line 10 "cpjit.h.PL" |
11 | #line 12 "cpjit.h.PL" |
10 | /* |
12 | /* |
11 | This file is AUTOMAGICALLY created by cpjit.h.PL. Modifications will |
13 | This file is AUTOMAGICALLY created by cpjit.h.PL. Modifications will |
12 | be lost. |
14 | be lost. |
13 | |
15 | |
14 | cpjit.h -- c portable jit in time compiler |
16 | cpjit.h -- c portable jit in time compiler |
… | |
… | |
30 | along with gvpe; if not, write to the Free Software |
32 | along with gvpe; if not, write to the Free Software |
31 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
33 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
32 | */ |
34 | */ |
33 | |
35 | |
34 | #include <string> |
36 | #include <string> |
|
|
37 | #include <sstream> |
35 | #include <inttypes.h> |
38 | #include <inttypes.h> |
36 | |
39 | |
37 | namespace cpjit |
40 | namespace cpjit |
38 | { |
41 | { |
|
|
42 | |
|
|
43 | #define CPJIT_MAX_ARGS $CPJIT_MAX_ARGS |
|
|
44 | #define CPJIT_NULLID "" |
39 | |
45 | |
40 | typedef std::string idstr; |
46 | typedef std::string idstr; |
41 | |
47 | |
42 | // environment for persistent or temporary storage |
48 | // environment for persistent or temporary storage |
43 | struct env |
49 | struct env |
44 | { |
50 | { |
45 | }; |
51 | }; |
46 | |
52 | |
47 | struct func_base |
53 | struct fun |
48 | { |
54 | { |
49 | env &e; |
55 | env &e; |
50 | idstr id; |
56 | idstr id; |
51 | const char *retval; |
57 | std::string source; |
52 | |
58 | |
53 | func_base (env &e, const idstr &id, const char *retval, const char *args); |
59 | fun (env &e, const idstr &id); |
|
|
60 | |
|
|
61 | // is compiled function available? |
|
|
62 | bool compiled (); |
|
|
63 | |
|
|
64 | void set_source (const std::string source) |
|
|
65 | { |
|
|
66 | this->source = source; |
|
|
67 | } |
54 | |
68 | |
55 | // internal method to return the function pointer |
69 | // internal method to return the function pointer |
56 | void *ptr (); |
70 | void *ptr (); |
57 | }; |
71 | }; |
58 | |
72 | |
59 | struct funcbuilder_base |
73 | struct funbuild : std::ostringstream |
60 | { |
74 | { |
|
|
75 | env &e; |
|
|
76 | idstr id; |
|
|
77 | |
|
|
78 | funbuild (env &e, const idstr &id, const char *rettype, ...); |
|
|
79 | void finish (); |
|
|
80 | |
|
|
81 | fun *get () |
|
|
82 | { |
|
|
83 | finish (); |
|
|
84 | fun *f = new fun (e, id); |
|
|
85 | f->set_source (str ()); |
|
|
86 | return f; |
|
|
87 | } |
61 | }; |
88 | }; |
62 | |
89 | |
63 | typedef unsigned char U8; |
90 | typedef unsigned char U8; |
64 | typedef signed char I8; |
91 | typedef signed char I8; |
65 | typedef uint16_t U16; |
92 | typedef uint16_t U16; |
… | |
… | |
92 | template<> struct typestr<I32 *> { static const char str[]; }; |
119 | template<> struct typestr<I32 *> { static const char str[]; }; |
93 | template<> struct typestr<I64 *> { static const char str[]; }; |
120 | template<> struct typestr<I64 *> { static const char str[]; }; |
94 | template<> struct typestr<F32 *> { static const char str[]; }; |
121 | template<> struct typestr<F32 *> { static const char str[]; }; |
95 | template<> struct typestr<F64 *> { static const char str[]; }; |
122 | template<> struct typestr<F64 *> { static const char str[]; }; |
96 | |
123 | |
97 | template<typename R, typename A1> |
124 | EOF |
98 | struct function : func_base |
125 | |
|
|
126 | for (0 .. $CPJIT_MAX_ARGS) { |
|
|
127 | print <<EOF; |
|
|
128 | #line 129 "cpjit.h.PL" |
|
|
129 | |
|
|
130 | template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
|
|
131 | struct fun$_ : fun |
99 | { |
132 | { |
100 | function (env &e, const idstr &id) |
133 | fun$_ (env &e, const idstr &id = CPJIT_NULLID) |
101 | : func_base (e, id, typestr<R>::str) |
134 | : fun (e, id) |
102 | { |
135 | { |
103 | } |
136 | } |
104 | |
137 | |
105 | int operator ()(A1 a1) |
138 | int operator ()(${\( join ", ", map "A$_ a$_", 1..$_ )}) |
106 | { |
139 | { |
107 | return ((R (*)(A1 a1)) ptr ())(a1); |
140 | return ((R (*)(${\( join ", ", map "A$_", 1..$_ )})) ptr ())(${\( join ", ", map "a$_", 1..$_ )}); |
|
|
141 | } |
|
|
142 | }; |
|
|
143 | |
|
|
144 | template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
|
|
145 | struct funbuild$_ : funbuild |
|
|
146 | { |
|
|
147 | funbuild$_ (env &e, const idstr &id = CPJIT_NULLID) |
|
|
148 | : funbuild (e, id, typestr<R>::str${\( join "", map ", typestr<A$_>::str", 1..$_ )}, (const char *)0) |
|
|
149 | { |
|
|
150 | } |
|
|
151 | |
|
|
152 | fun$_<R${\( join "", map ", A$_", 1..$_ )}> *get () |
|
|
153 | { |
|
|
154 | finish (); |
|
|
155 | fun$_<R${\( join "", map ", A$_", 1..$_ )}> *f = new fun$_<R${\( join "", map ", A$_", 1..$_ )}> (e, id); |
|
|
156 | f->set_source (str ()); |
|
|
157 | return f; |
108 | } |
158 | } |
109 | }; |
159 | }; |
110 | |
160 | |
111 | EOF |
161 | EOF |
|
|
162 | } |
112 | |
163 | |
113 | print <<EOF; |
164 | print <<EOF; |
114 | } |
165 | } |
115 | EOF |
166 | EOF |
116 | |
167 | |