-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcpps_jit_function.h
474 lines (426 loc) · 15.2 KB
/
cpps_jit_function.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#pragma once
using namespace cpps;
using namespace llvm;
using namespace llvm::orc;
inline
void testprint(cpps::cpps_value *v)
{
v->tt = 0;
v->value.b = 0;
printf("testprint - hello world\n");
}
template<typename T>
Value* CXX2Value(IRBuilder<>& builder, std::unique_ptr<LLVMContext>& context, T _f) {
llvm::ConstantInt* addressConstant = llvm::ConstantInt::get(*context, llvm::APInt(sizeof(std::uintptr_t) * 8, reinterpret_cast<uint64_t>(_f), false));
llvm::Value* _v = builder.CreateIntToPtr(addressConstant, llvm::Type::getInt8PtrTy(*context));
return _v;
}
#define CVALUE runtime._c
#define DOMAINVALUE runtime._domain
#define STACKVALUE runtime._stack
#define LEFTDOMAINVALUE runtime._leftdomain
#define _CFV(v) CXX2Value(runtime.builder,runtime.context,v)
#define _CFFC(ft,v) FunctionCallee(ft,_CFV(v))
#define FT_VOID FunctionType::get(runtime.builder.getVoidTy(), false)
#define OBJECT_ARRAY(_args,i) runtime.builder.CreateConstGEP1_64(runtime.__moduledata->object_structType, _args, i)
#define DEF_OBJECT_ARRAY(size) runtime.builder.CreateAlloca(runtime.__moduledata->object_structType,INTVALUE(SET_DEFAULT_VARS,size))
inline FunctionType* make_fty(Type* ret, std::initializer_list<Type*>& li)
{
std::vector<Type*> parameters = li;
FunctionType* functionType = FunctionType::get(ret, parameters, false);
return functionType;
}
#define DEFAULT_VARS cpps_jit_builder_runtime &runtime
#define SET_DEFAULT_VARS runtime
template<typename T>
inline FunctionCallee CREATE_FUNCTION(DEFAULT_VARS, T func, Type* ret, std::initializer_list<Type*> vars) { return _CFFC(make_fty(ret, vars), func); }
#define _VOIDTY runtime.builder.getVoidTy()
#define _PTRTY runtime.builder.getInt8PtrTy()
#define _INTTY runtime.builder.getInt64Ty()
#define _UINTTY runtime.builder.getInt64Ty()
#define _BOOLTY runtime.builder.getInt8Ty()
#define _NUMBERTY runtime.builder.getDoubleTy()
//#define CREATE_OBJECT() cpps_jit_init_object(SET_DEFAULT_VARS, CREATE_VAR(cpps::object))
inline std::string cpps_jit_buildlambda(C* c) {
std::string ret = "__jitfunc@" + std::to_string(c->_lambdanum++) + "_";
int i;
int len = 20;
for (i = 0; i < len; ++i)
ret += 'A' + rand() % 26;
return(ret);
}
#define SPC(spaces) for (auto i = 0; i < spaces; i++) printf(" ");
inline void dump(cpps::node* n, int spaces = 0) {
SPC(spaces);
printf("node:type = %d , s = %s\n", n->type, n->s.c_str());
if (!n->empty()) {
SPC(spaces);
printf("[[[[\n");
}
for (size_t i = 0; i < n->l.size(); i++) {
node* _n2 = n->l[i];
if (_n2) dump(_n2, spaces + 1);
}
if (!n->empty()) {
SPC(spaces);
printf("]]]]\n");
}
}
inline Value* create_object(DEFAULT_VARS) {
Value* alloca_struct = runtime.builder.CreateAlloca(runtime.__moduledata->object_structType);
runtime.builder.CreateStore(runtime.__moduledata->object_nil, alloca_struct);
return alloca_struct;
}
template<typename T>
inline Value* INTVALUE(DEFAULT_VARS,T v) {
return llvm::ConstantInt::get(*runtime.context, llvm::APInt(sizeof(T) * 8, static_cast<T>(v), false));
}
inline Value* NUMBERVALUE(DEFAULT_VARS, cpps_number v) {
return llvm::ConstantFP::get(*runtime.context, APFloat(v));
}
inline Value* BOOLVALUE(DEFAULT_VARS,bool v) {
return runtime.builder.getInt8(v);
}
#define CREATE_VAR(_type) runtime.builder.CreateAlloca(runtime.builder.getInt8PtrTy(),INTVALUE(sizeof(_type)))
///
inline void cpp_function_caller(C* c, cpps::object* object, cpps::object** _args,int64 argc )
{
cpps_std_vector vct;
cpps_function* func = object->tofunction();
if (func->getIsNeedC()) {
cpps::object _c = cpps::object::create(c, c);
vct.emplace_back(_c.getval());
}
for (int i = 0; i < argc; i++) {
vct.emplace_back(_args[i]->getval());
}
function_caller(c, *object, vct);
}
inline
void cpps_jit_function_caller(DEFAULT_VARS, Value* _func, Value* _args, Value* _argc)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_function_caller, _VOIDTY, { _PTRTY,_PTRTY,_PTRTY,_INTTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE, _func,_args ,_argc });
}
inline
void cpp_function_caller_ret(C* c, cpps::object* object, cpps::object** _args, int64 argc, cpps::object* ret)
{
cpps_std_vector vct;
cpps_function* func = object->tofunction();
if (func->getIsNeedC()) {
cpps::object _c = cpps::object::create(c, c);
vct.emplace_back(_c.getval());
}
for (int i = 0; i < argc; i++) {
vct.emplace_back(_args[i]->getval());
}
*ret = function_caller(c, *object, vct);
}
inline
void cpps_jit_function_caller_ret(DEFAULT_VARS, Value* _func, Value* _args, Value* _argc,Value *ret)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_function_caller_ret, _VOIDTY, { _PTRTY,_PTRTY,_PTRTY,_INTTY,_PTRTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE, _func,_args ,_argc ,ret });
}
inline
void cpp_quote_real(cpps_domain* left, cpps_value& src, cpps_value& tar, bool isconst)
{
if (src.tt == CPPS_TQUOTECLASSVAR) {
cpps_classvar_quato* f = src.value.quato;
tar = f->getvalue(left);
}
else if (src.tt == CPPS_TREF) {
if (isconst) {
tar = *src.value.value;
}
else {
tar.tt = CPPS_TREF;
tar.value.value = src.value.value;
}
}
else {
if (isconst) {
tar = src;
}
else {
tar.tt = CPPS_TREF;
tar.value.value = &src;
}
}
}
inline
void cpp_try_get_var(C* c,cpps_domain *domain, cpps::object* leftdomain_obj, char *varname, cpps::object* ret)
{
cpps_domain* &leftdomain = leftdomain_obj->getval().value.domain;
cpps_regvar* v = NULL;
if (leftdomain && !leftdomain_obj->isnull())
v = leftdomain->getvar(varname, leftdomain, true, true);
else
v = domain->getvar(varname, leftdomain);
if (v) {
cpp_quote_real(leftdomain ? leftdomain : domain, v->getval(), ret->getval(), false);
}
else {
*ret = cpps::nil;
}
}
inline
void cpps_jit_try_get_Var(DEFAULT_VARS, Value* _varname ,Value *ret)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_try_get_var, _VOIDTY, { _PTRTY,_PTRTY,_PTRTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE, DOMAINVALUE,LEFTDOMAINVALUE,_varname,ret });
}
inline
void cpp_set_value_integer(C*c, cpps::object* ret,cpps_integer v)
{
*ret = v;
}
inline
void cpps_jit_set_value_integer(DEFAULT_VARS, Value* ret,Value *v)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_set_value_integer, _VOIDTY, { _PTRTY,_PTRTY,_INTTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE,ret,v });
}
inline
void cpp_set_value_uinteger(C*c, cpps::object* ret,cpps_uinteger v)
{
*ret = v;
}
inline
void cpps_jit_set_value_uinteger(DEFAULT_VARS, Value* ret,Value *v)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_set_value_uinteger, _VOIDTY, { _PTRTY,_PTRTY,_UINTTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE,ret,v });
}
inline
void cpp_set_value_number(C*c, cpps::object* ret,cpps_number v)
{
*ret = v;
}
inline
void cpps_jit_set_value_number(DEFAULT_VARS, Value* ret,Value *v)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_set_value_integer, _VOIDTY, { _PTRTY,_PTRTY,_NUMBERTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE,ret,v });
}
inline
void cpp_set_value_bool(C*c, cpps::object* ret,bool v)
{
*ret = cpps::object(v);
}
inline
void cpps_jit_set_value_bool(DEFAULT_VARS, Value* ret,Value *v)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_set_value_bool, _VOIDTY, { _PTRTY,_PTRTY,_BOOLTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE,ret,v });
}
inline
void cpp_set_value(C* c, cpps::object* ret, cpps::object* v,bool quote)
{
if (quote)
*ret = v->ref();
else
*ret = v->toreal();
}
inline
void cpps_jit_set_value(DEFAULT_VARS, Value* ret,Value *v ,Value * quote)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_set_value, _VOIDTY, { _PTRTY,_PTRTY,_PTRTY,_BOOLTY });
runtime.builder.CreateCall(cpp_function_callee, { CVALUE,ret,v ,quote });
}
inline
bool cpp_to_bool(C* c, cpps::object* ret)
{
return cpps_to_bool(ret->realval());
}
inline
Value* cpps_jit_to_bool(DEFAULT_VARS, Value* v)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_to_bool, _BOOLTY, { _PTRTY,_PTRTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,v});
}
#define SYMBOL_FUNC2(name) inline void CPPS_PP_CAT(cpp_symbol_,name)(C* c, cpps::object* a, cpps::object* ret)\
{\
CPPS_PP_CAT(cpps_,name)(a->getval(), ret->getval());\
}\
\
inline Value* CPPS_PP_CAT(cpps_jit_symbol_,name)(DEFAULT_VARS, Value* v, Value* ret)\
{\
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &CPPS_PP_CAT(cpp_symbol_,name), _VOIDTY, { _PTRTY,_PTRTY,_PTRTY });\
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE, v , ret});\
}
#define SYMBOL_FUNC(name) inline void CPPS_PP_CAT(cpp_symbol_,name) (C* c, cpps::object* a, cpps::object* b, cpps::object* ret)\
{\
CPPS_PP_CAT(cpps_,name)(a->realval(),b->realval(),ret->getval());\
}\
\
inline Value* CPPS_PP_CAT(cpps_jit_symbol_,name)(DEFAULT_VARS, Value* a, Value* b,Value *ret)\
{\
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &CPPS_PP_CAT(cpp_symbol_,name), _VOIDTY, { _PTRTY,_PTRTY,_PTRTY,_PTRTY });\
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE, a , b , ret});\
}
SYMBOL_FUNC2(rightautoincrease)
SYMBOL_FUNC(equel)
SYMBOL_FUNC(subtract)
////
inline
int64 cpp_foreach_type(C* c, cpps::object* ret)
{
if (ret->ismap()) {
return 1;
}
if (ret->isvector()) {
return 2;
}
if (ret->isset()) {
return 3;
}
if (ret->isrange()) {
return 4;
}
return 0;
}
inline
Value* cpps_jit_foreach_type(DEFAULT_VARS, Value* v)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_foreach_type, _INTTY, { _PTRTY,_PTRTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,v });
}
inline
void cpp_foreach_end(C* c, cpps::object* v, cpps::object* retend,int64 type)
{
if (type == 1) {
cpps::object::map _v(c,*v);
*retend = _v.end_obj(c);
}
if (type == 2) {
cpps::object::vector _v( *v);
*retend = _v.end_obj(c);
}
if (type == 3) {
cpps::object::set _v(c,*v);
*retend = _v.end_obj(c);
}
if (type == 4) {
cpps_range* range = object_cast<cpps_range*>(*v);
*retend = cpps::object(range->end);
}
}
inline
Value* cpps_jit_foreach_end(DEFAULT_VARS, Value* v,Value *retend,Value* type)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_foreach_end, _VOIDTY, { _PTRTY,_PTRTY ,_PTRTY ,_INTTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,v,retend,type });
}
inline
void cpp_foreach_begin(C* c, cpps::object* v, cpps::object* retend,cpps::object* _iPtr, int64 type)
{
if (type == 1) {
cpps::object::map _v(c, *v);
*retend = _v.begin_obj(c);
cpps_create_class_var(cpps_pair, c, cpps_value_iterator, cpps_iterator_ptr);
*_iPtr = cpps_value_iterator;
}
if (type == 2) {
cpps::object::vector _v(*v);
*retend = _v.begin_obj(c);
}
if (type == 3) {
cpps::object::set _v(c, *v);
*retend = _v.begin_obj(c);
}
if (type == 4) {
cpps_range* range = object_cast<cpps_range*>(*v);
*retend = cpps::object(range->begin);
}
}
inline
Value* cpps_jit_foreach_begin(DEFAULT_VARS, Value* v, Value* retend, Value* _iPtr, Value* type)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_foreach_begin, _VOIDTY, { _PTRTY,_PTRTY ,_PTRTY,_PTRTY ,_INTTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,v,retend,_iPtr,type });
}
inline
bool cpp_foreach_condition(C* c, cpps::object* it, cpps::object* end, int64 type)
{
if (type == 4) {
/* cpps_integer _it = it->toint();
cpps_integer _end = end->toint();*/
//printf("%d < %d\n", _it, _end);
return it->value.value.integer < end->value.value.integer;
}
else if (type == 1) {
cpps_hash_map::iterator& _it = *(object_cast<cpps_hash_map::iterator*>(*it));
cpps_hash_map::iterator& _end = *(object_cast<cpps_hash_map::iterator*>(*end));
return _it != _end;
}
else if (type == 2) {
cpps_std_vector::iterator& _it = *(object_cast<cpps_std_vector::iterator*>(*it));
cpps_std_vector::iterator& _end = *(object_cast<cpps_std_vector::iterator*>(*end));
return _it != _end;
}
else if (type == 3) {
cpps_hash_set::iterator& _it = *(object_cast<cpps_hash_set::iterator*>(*it));
cpps_hash_set::iterator& _end = *(object_cast<cpps_hash_set::iterator*>(*end));
return _it != _end;
}
return false;
}
inline
Value* cpps_jit_foreach_condition(DEFAULT_VARS, Value* v,Value* it, Value* retend, Value* type)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_foreach_condition, _BOOLTY, { _PTRTY,_PTRTY ,_PTRTY ,_INTTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,it, retend,type });
}
inline
void cpp_foreach_inc(C* c, cpps::object* it, int64 type)
{
if (type == 1) {
cpps_hash_map::iterator& _it = *(object_cast<cpps_hash_map::iterator*>(*it));
_it++;
}
if (type == 2) {
cpps_std_vector::iterator& _it = *(object_cast<cpps_std_vector::iterator*>(*it));
_it++;
}
if (type == 3) {
cpps_hash_set::iterator& _it = *(object_cast<cpps_hash_set::iterator*>(*it));
_it++;
}
if (type == 4) {
it->getval().value.integer++;
}
}
inline
Value* cpps_jit_foreach_inc(DEFAULT_VARS, Value* it, Value* type)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_foreach_inc, _VOIDTY, { _PTRTY,_PTRTY ,_INTTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,it,type });
}
inline
void cpp_foreach_set_value(C* c, cpps::object* it,cpps::object* _i, int64 type)
{
if (type == 1) {
cpps_hash_map::iterator& _it = *(object_cast<cpps_hash_map::iterator*>(*it));
cpps_pair* mapnode = object_cast<cpps_pair*>(*_i);
mapnode->_first = _it->first;
mapnode->_second = _it->second;
}
if (type == 2) {
cpps_std_vector::iterator& _it = *(object_cast<cpps_std_vector::iterator*>(*it));
*_i = *it;
}
if (type == 3) {
cpps_hash_set::iterator& _it = *(object_cast<cpps_hash_set::iterator*>(*it));
*_i = *it;
}
if (type == 4) {
*_i = cpps::object(it->value.value.integer);
}
}
inline
Value* cpps_jit_foreach_set_value(DEFAULT_VARS, Value* it,Value* _i, Value* type)
{
FunctionCallee cpp_function_callee = CREATE_FUNCTION(SET_DEFAULT_VARS, &cpp_foreach_set_value, _VOIDTY, { _PTRTY,_PTRTY,_PTRTY ,_INTTY });
return runtime.builder.CreateCall(cpp_function_callee, { CVALUE,it,_i,type });
}