… | |
… | |
41 | { |
41 | { |
42 | |
42 | |
43 | #define CPJIT_MAX_ARGS $CPJIT_MAX_ARGS |
43 | #define CPJIT_MAX_ARGS $CPJIT_MAX_ARGS |
44 | #define CPJIT_NULLID "" |
44 | #define CPJIT_NULLID "" |
45 | |
45 | |
46 | typedef std::string idstr; |
|
|
47 | |
|
|
48 | // environment for persistent or temporary storage |
46 | // environment for persistent or temporary storage |
49 | struct env |
47 | struct env |
50 | { |
48 | { |
51 | std::string path; |
49 | std::string path; |
52 | bool temporary; |
50 | bool temporary; |
53 | int idxfd; |
51 | int idxfd; |
54 | unsigned long nextid; |
|
|
55 | |
52 | |
56 | void dolock (bool lock); |
53 | void dolock (bool lock); |
57 | void lock (); |
54 | void lock (); |
58 | void unlock (); |
55 | void unlock (); |
59 | |
56 | |
… | |
… | |
62 | |
59 | |
63 | public: |
60 | public: |
64 | env (const std::string &path = CPJIT_NULLID, bool temporary = true); |
61 | env (const std::string &path = CPJIT_NULLID, bool temporary = true); |
65 | ~env (); |
62 | ~env (); |
66 | |
63 | |
67 | std::string genid (); |
64 | // completely delete the environment on disk |
|
|
65 | void clear (); |
68 | }; |
66 | }; |
69 | |
67 | |
70 | struct fun |
68 | struct fun |
71 | { |
69 | { |
72 | env &e; |
70 | env &e; |
73 | idstr id; |
|
|
74 | std::string source; |
71 | std::string id, source; |
75 | void *funptr; |
72 | void *funptr; |
76 | |
73 | |
77 | fun (env &e, const idstr &id); |
74 | fun (env &e, const std::string &id, const std::string &source); |
78 | |
75 | |
79 | // is compiled function available? |
76 | // is compiled function available? |
80 | bool compiled () |
77 | bool compiled () |
81 | { |
78 | { |
82 | return funptr; |
79 | return funptr; |
83 | } |
80 | } |
84 | |
|
|
85 | void set_source (const std::string source); |
|
|
86 | |
81 | |
87 | // returns the function pointer |
82 | // returns the function pointer |
88 | void *ptr (); |
83 | void *ptr (); |
89 | }; |
84 | }; |
90 | |
85 | |
91 | struct funbuild : std::ostringstream |
86 | struct funbuild : std::ostringstream |
92 | { |
87 | { |
93 | env &e; |
88 | env &e; |
94 | idstr id; |
89 | std::string retlist, arglist; |
95 | std::string hdr; |
|
|
96 | |
90 | |
97 | funbuild (env &e, const idstr &id, const char *rettype, ...); |
91 | funbuild (env &e, const char *rettype, ...); |
98 | std::string finish (); |
92 | void finish (std::string &id, std::string &source); |
99 | |
|
|
100 | fun *get () |
|
|
101 | { |
|
|
102 | fun *f = new fun (e, id); |
|
|
103 | f->set_source (finish ()); |
|
|
104 | return f; |
|
|
105 | } |
|
|
106 | }; |
93 | }; |
107 | |
94 | |
108 | typedef unsigned char U8; |
95 | typedef unsigned char U8; |
109 | typedef signed char I8; |
96 | typedef signed char I8; |
110 | typedef uint16_t U16; |
97 | typedef uint16_t U16; |
… | |
… | |
143 | |
130 | |
144 | EOF |
131 | EOF |
145 | |
132 | |
146 | for (0 .. $CPJIT_MAX_ARGS) { |
133 | for (0 .. $CPJIT_MAX_ARGS) { |
147 | print <<EOF; |
134 | print <<EOF; |
148 | #line 143 "cpjit.h.PL" |
135 | #line 133 "cpjit.h.PL" |
149 | |
136 | |
150 | template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
137 | template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
151 | struct fun$_ : fun |
138 | struct fun$_ : fun |
152 | { |
139 | { |
153 | fun$_ (env &e, const idstr &id) |
140 | fun$_ (env &e, const std::string &id, const std::string &source) |
154 | : fun (e, id) |
141 | : fun(e, id, source) |
155 | { |
142 | { |
156 | } |
143 | } |
157 | |
144 | |
158 | int operator ()(${\( join ", ", map "A$_ a$_", 1..$_ )}) |
145 | int operator ()(${\( join ", ", map "A$_ a$_", 1..$_ )}) |
159 | { |
146 | { |
… | |
… | |
162 | }; |
149 | }; |
163 | |
150 | |
164 | template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
151 | template<typename R${\( join "", map ", typename A$_", 1..$_ )}> |
165 | struct funbuild$_ : funbuild |
152 | struct funbuild$_ : funbuild |
166 | { |
153 | { |
167 | funbuild$_ (env &e, const idstr &id = CPJIT_NULLID) |
154 | funbuild$_ (env &e) |
168 | : funbuild (e, id, typestr<R>::str${\( join "", map ", typestr<A$_>::str", 1..$_ )}, (const char *)0) |
155 | : funbuild(e, typestr<R>::str${\( join "", map ", typestr<A$_>::str", 1..$_ )}, (const char *)0) |
169 | { |
156 | { |
170 | } |
157 | } |
171 | |
158 | |
172 | fun$_<R${\( join "", map ", A$_", 1..$_ )}> *get () |
159 | fun$_<R${\( join "", map ", A$_", 1..$_ )}> *get () |
173 | { |
160 | { |
174 | fun$_<R${\( join "", map ", A$_", 1..$_ )}> *f = new fun$_<R${\( join "", map ", A$_", 1..$_ )}> (e, id); |
161 | std::string id, source; |
175 | f->set_source (finish ()); |
162 | finish (id, source); |
176 | return f; |
163 | return new fun$_<R${\( join "", map ", A$_", 1..$_ )}> (e, id, source); |
177 | } |
164 | } |
178 | }; |
165 | }; |
179 | |
166 | |
180 | EOF |
167 | EOF |
181 | } |
168 | } |