forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlobalBuiltin.cpp
More file actions
402 lines (327 loc) · 12 KB
/
Copy pathGlobalBuiltin.cpp
File metadata and controls
402 lines (327 loc) · 12 KB
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
#include <hxcpp.h>
#include "Cppia.h"
#if (HXCPP_API_LEVEL>=500)
#include <hx/thread/Thread.hpp>
#endif
namespace hx
{
template<> inline Array<unsigned char> &runValue(Array<unsigned char> &outValue, CppiaCtx *ctx, CppiaExpr *expr)
{
outValue.mPtr = (Array_obj<unsigned char>*) expr->runObject(ctx);
return outValue;
}
template<typename ARG0, int (*FUNC)(ARG0 arg0)>
class IntBuiltin1 : public CppiaExpr
{
public:
Expressions args;
IntBuiltin1(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "IntBuiltin1"; }
ExprType getType() { return etInt; }
void runVoid(CppiaCtx *ctx) { runInt(ctx); }
Float runFloat(CppiaCtx *ctx) { return runInt(ctx); }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runInt(ctx)).mPtr; }
String runString(CppiaCtx *ctx) { return String(runInt(ctx)); }
int runInt(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_CHECK;
return FUNC(val0);
}
};
template<typename ARG0, typename ARG1, int (*FUNC)(ARG0 arg0, ARG1 arg1)>
class IntBuiltin2 : public CppiaExpr
{
public:
Expressions args;
IntBuiltin2(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "IntBuiltin2"; }
ExprType getType() { return etInt; }
void runVoid(CppiaCtx *ctx) { runInt(ctx); }
Float runFloat(CppiaCtx *ctx) { return runInt(ctx); }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runInt(ctx)).mPtr; }
String runString(CppiaCtx *ctx) { return String(runInt(ctx)); }
int runInt(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_CHECK;
ARG1 val1;
runValue(val1, ctx, args[1]);
BCR_CHECK;
return FUNC(val0,val1);
}
};
template<typename ARG0, void (*FUNC)(ARG0)>
class VoidBuiltin1 : public CppiaExpr
{
public:
Expressions args;
VoidBuiltin1(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "VoidBuiltin1"; }
ExprType getType() { return etVoid; }
int runInt(CppiaCtx *ctx) { runVoid(ctx); return 0; }
Float runFloat(CppiaCtx *ctx) { runVoid(ctx); return 0;}
hx::Object *runObject(CppiaCtx *ctx) { runVoid(ctx); return 0; }
String runString(CppiaCtx *ctx) { runVoid(ctx); return String(); }
void runVoid(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_VCHECK;
FUNC(val0);
}
};
template<typename ARG0, typename ARG1, void (*FUNC)(ARG0,ARG1)>
class VoidBuiltin2 : public CppiaExpr
{
public:
Expressions args;
VoidBuiltin2(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "VoidBuiltin2"; }
ExprType getType() { return etVoid; }
int runInt(CppiaCtx *ctx) { runVoid(ctx); return 0; }
Float runFloat(CppiaCtx *ctx) { runVoid(ctx); return 0;}
hx::Object *runObject(CppiaCtx *ctx) { runVoid(ctx); return 0; }
String runString(CppiaCtx *ctx) { runVoid(ctx); return String(); }
void runVoid(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_VCHECK;
ARG1 val1;
runValue(val1, ctx, args[1]);
BCR_VCHECK;
FUNC(val0,val1);
}
};
template<typename ARG0, typename ARG1, typename ARG2, void (*FUNC)(ARG0,ARG1,ARG2)>
class VoidBuiltin3 : public CppiaExpr
{
public:
Expressions args;
VoidBuiltin3(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "VoidBuiltin3"; }
ExprType getType() { return etVoid; }
int runInt(CppiaCtx *ctx) { runVoid(ctx); return 0; }
Float runFloat(CppiaCtx *ctx) { runVoid(ctx); return 0;}
hx::Object *runObject(CppiaCtx *ctx) { runVoid(ctx); return 0; }
String runString(CppiaCtx *ctx) { runVoid(ctx); return String(); }
void runVoid(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_VCHECK;
ARG1 val1;
runValue(val1, ctx, args[1]);
BCR_VCHECK;
ARG2 val2;
runValue(val2, ctx, args[2]);
BCR_VCHECK;
FUNC(val0,val1,val2);
}
#ifdef CPPIA_JIT
static void SLJIT_CALL setFloat(Array_obj<unsigned char> *inBuffer, int inAddr, double *inValue)
{
if (sizeof(ARG2)==sizeof(float))
__hxcpp_memory_set_float(inBuffer,inAddr,*inValue);
else
__hxcpp_memory_set_double(inBuffer,inAddr,*inValue);
}
void genCode(CppiaCompiler *compiler, const JitVal &inDest,ExprType destType)
{
JitTemp obj(compiler,jtPointer);
args[0]->genCode(compiler, obj, etObject);
JitTemp addr(compiler,jtInt);
args[1]->genCode(compiler, addr, etInt);
JitTemp val(compiler,jtFloat);
args[2]->genCode(compiler, val, etFloat);
compiler->callNative( (void *)setFloat, obj, addr, val );
}
#endif
};
template<typename RET, RET (*FUNC)()>
class FloatBuiltin0 : public CppiaExpr
{
public:
Expressions args;
FloatBuiltin0(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "FloatBuiltin0"; }
ExprType getType() { return etFloat; }
int runInt(CppiaCtx *ctx) { return runFloat(ctx); }
void runVoid(CppiaCtx *ctx) { runFloat(ctx); }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runFloat(ctx)).mPtr; }
String runString(CppiaCtx *ctx) { return String(runFloat(ctx)); }
Float runFloat(CppiaCtx *ctx)
{
return FUNC();
}
#ifdef CPPIA_JIT
static void SLJIT_CALL run(double *outResult)
{
*outResult = FUNC();
}
void genCode(CppiaCompiler *compiler, const JitVal &inDest,ExprType destType)
{
if (destType==etFloat && isMemoryVal(inDest) )
{
compiler->callNative( (void *)run, inDest.as(jtFloat));
}
else
{
JitTemp temp(compiler,jtFloat);
compiler->callNative( (void *)run, temp);
compiler->convert(temp, etFloat, inDest, destType);
}
}
#endif
};
template<typename ARG0, typename RET, RET (*FUNC)(ARG0)>
class FloatBuiltin1 : public CppiaExpr
{
public:
Expressions args;
FloatBuiltin1(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "FloatBuiltin1"; }
ExprType getType() { return etFloat; }
int runInt(CppiaCtx *ctx) { return runFloat(ctx); }
void runVoid(CppiaCtx *ctx) { runFloat(ctx); }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runFloat(ctx)).mPtr; }
String runString(CppiaCtx *ctx) { return String(runFloat(ctx)); }
Float runFloat(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_CHECK;
return FUNC(val0);
}
};
template<typename ARG0, typename ARG1, typename RET, RET (*FUNC)(ARG0,ARG1)>
class FloatBuiltin2 : public CppiaExpr
{
public:
Expressions args;
FloatBuiltin2(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "FloatBuiltin2"; }
ExprType getType() { return etFloat; }
int runInt(CppiaCtx *ctx) { return runFloat(ctx); }
void runVoid(CppiaCtx *ctx) { runFloat(ctx); }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runFloat(ctx)).mPtr; }
String runString(CppiaCtx *ctx) { return String(runFloat(ctx)); }
Float runFloat(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_CHECK;
ARG1 val1;
runValue(val1, ctx, args[1]);
BCR_CHECK;
return FUNC(val0,val1);
}
};
template<typename ARG0, typename ARG1, typename ARG2, typename RET, RET (*FUNC)(ARG0,ARG1,ARG2)>
class FloatBuiltin3 : public CppiaExpr
{
public:
Expressions args;
FloatBuiltin3(CppiaExpr *inSrc, Expressions &inArgs) : CppiaExpr(inSrc), args(inArgs) { }
const char *getName() { return "FloatBuiltin3"; }
ExprType getType() { return etFloat; }
int runInt(CppiaCtx *ctx) { return runFloat(ctx); }
void runVoid(CppiaCtx *ctx) { runFloat(ctx); }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runFloat(ctx)).mPtr; }
String runString(CppiaCtx *ctx) { return String(runFloat(ctx)); }
Float runFloat(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_CHECK;
ARG1 val1;
runValue(val1, ctx, args[1]);
BCR_CHECK;
ARG2 val2;
runValue(val2, ctx, args[2]);
BCR_CHECK;
return FUNC(val0,val1,val2);
}
};
#define MEMORY_INT(GETTER,SETTER) \
if (function==HX_CSTRING( #GETTER ) ) \
{ \
if (ioExpressions.size()==1) \
return new IntBuiltin1<int, GETTER>(src,ioExpressions); \
return new IntBuiltin2<Array<unsigned char>, int, GETTER>(src,ioExpressions); \
} \
if (function==HX_CSTRING( #SETTER ) ) \
{ \
if (ioExpressions.size()==2) \
return new VoidBuiltin2<int,int, SETTER>(src,ioExpressions); \
return new VoidBuiltin3<Array<unsigned char>,int,int,SETTER>(src,ioExpressions); \
}
template<typename ARG0, typename RET, RET (*FUNC)(ARG0)>
class ObjectBuiltin1 : public CppiaDynamicExpr
{
public:
Expressions args;
ObjectBuiltin1(CppiaExpr *inSrc, Expressions &inArgs) : CppiaDynamicExpr(inSrc), args(inArgs) { }
const char *getName() { return "ObjectBuiltin1"; }
ExprType getType() { return etObject; }
hx::Object *runObject(CppiaCtx *ctx)
{
ARG0 val0;
runValue(val0, ctx, args[0]);
BCR_CHECK;
return FUNC(val0).mPtr;
}
};
CppiaExpr *createGlobalBuiltin(CppiaExpr *src, String function, Expressions &ioExpressions )
{
MEMORY_INT(__hxcpp_memory_get_byte, __hxcpp_memory_set_byte);
MEMORY_INT(__hxcpp_memory_get_i32, __hxcpp_memory_set_i32);
MEMORY_INT(__hxcpp_memory_get_ui32, __hxcpp_memory_set_ui32);
MEMORY_INT(__hxcpp_memory_get_i16, __hxcpp_memory_set_i16);
MEMORY_INT(__hxcpp_memory_get_ui16, __hxcpp_memory_set_ui16);
if (function==HX_CSTRING("__hxcpp_memory_get_float") )
{
if (ioExpressions.size()==1)
return new FloatBuiltin1<int,float,__hxcpp_memory_get_float>(src,ioExpressions);
return new FloatBuiltin2<Array<unsigned char>,int,float,__hxcpp_memory_get_float>(src,ioExpressions);
}
if (function==HX_CSTRING("__hxcpp_memory_set_float") )
{
if (ioExpressions.size()==2)
return new VoidBuiltin2<int,float,__hxcpp_memory_set_float>(src,ioExpressions);
return new VoidBuiltin3<Array<unsigned char>,int,float,__hxcpp_memory_set_float>(src,ioExpressions);
}
if (function==HX_CSTRING("__hxcpp_memory_get_double") )
{
if (ioExpressions.size()==1)
return new FloatBuiltin1<int,double,__hxcpp_memory_get_double>(src,ioExpressions);
return new FloatBuiltin2<Array<unsigned char>,int,double,__hxcpp_memory_get_double>(src,ioExpressions);
}
if (function==HX_CSTRING("__hxcpp_memory_set_double") )
{
if (ioExpressions.size()==2)
return new VoidBuiltin2<int,double,__hxcpp_memory_set_double>(src,ioExpressions);
return new VoidBuiltin3<Array<unsigned char>,int,double,__hxcpp_memory_set_double>(src,ioExpressions);
}
if (function==HX_CSTRING("__time_stamp") )
{
if (ioExpressions.size()==0)
return new FloatBuiltin0<double,__time_stamp>(src,ioExpressions);
}
if (function==HX_CSTRING("__hxcpp_thread_create") )
{
if (ioExpressions.size()==1)
#if (HXCPP_API_LEVEL>=500)
return new ObjectBuiltin1<hx::Callable<void(void)>, hx::thread::Thread, hx::thread::Thread_obj::create>(src, ioExpressions);
#else
return new ObjectBuiltin1<Dynamic, Dynamic, __hxcpp_thread_create>(src, ioExpressions);
#endif
}
printf("Unknown function : %s(%d)\n", function.out_str(), (int)ioExpressions.size() );
throw (HX_CSTRING("Unknown global:") + function).utf8_str();
return 0;
}
} // end namespace hx