forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppia.cpp
More file actions
8260 lines (7153 loc) · 230 KB
/
Cppia.cpp
File metadata and controls
8260 lines (7153 loc) · 230 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <hxcpp.h>
#include <hx/Scriptable.h>
#include <hx/GC.h>
#include <hx/Unordered.h>
#include <stdio.h>
#include <vector>
#include <string>
#include "Cppia.h"
#include "CppiaStream.h"
#include <stdlib.h>
#ifdef HX_ANDROID
#define atof(x) strtod(x,0)
#endif
// Really microsoft?
#ifdef interface
#undef interface
#endif
namespace hx
{
//#define SJLJ_RETURN 1
bool gEnableJit = false;
#ifdef DEBUG_RETURN_TYPE
int gLastRet = etVoid;
#endif
static bool isNumeric(ExprType t) { return t==etInt || t==etFloat; }
void cppiaClassMark(CppiaClassInfo *inClass,hx::MarkContext *__inCtx);
void cppiaClassVisit(CppiaClassInfo *inClass,hx::VisitContext *__inCtx);
int getScriptId(hx::Class inClass);
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
void CppiaTrap( )
{
// Good when using gdb, and to collect a core ...
#if __has_builtin(__builtin_trap)
__builtin_trap();
#else
(* (int *) 0) = 0;
#endif
}
// --- StackLayout ---
// 'this' pointer is in slot 0 and captureSize(0) ...
StackLayout::StackLayout(StackLayout *inParent) :
size( sizeof(void *) ), captureSize(sizeof(void *)), parent(inParent)
{
}
void StackLayout::dump(Array<String> &inStrings, std::string inIndent)
{
CPPIALOG("%sCapture:\n",inIndent.c_str());
for(int i=0;i<captureVars.size();i++)
CPPIALOG("%s %s\n", inIndent.c_str(), inStrings[captureVars[i]->nameId].out_str() );
if (parent)
parent->dump(inStrings,inIndent + " ");
}
CppiaStackVar *StackLayout::findVar(int inId)
{
if (varMap[inId])
return varMap[inId];
CppiaStackVar *var = parent ? parent->findVar(inId) : 0;
if (!var)
return 0;
CppiaStackVar *closureVar = new CppiaStackVar(var,size,captureSize);
varMap[inId] = closureVar;
captureVars.push_back(closureVar);
return closureVar;
}
// --- CppiaCtx functions ----------------------------------------
#ifdef CPPIA_JIT
void CppiaExpr::genCode(CppiaCompiler *compiler, const JitVal &inDest,ExprType destType)
{
compiler->trace(getName());
}
JumpId CppiaExpr::genCompare(CppiaCompiler *compiler,bool inReverse,LabelId inLabel)
{
genCode(compiler,sJitTemp0.as(jtInt), etInt);
// inReverse = false -> jump if not 0
// inReverse = true -> jump if zero
return compiler->compare(inReverse ? cmpI_EQUAL : cmpI_NOT_EQUAL, sJitTemp0, (int)0, inLabel);
}
#endif
// --- CppiaDynamicExpr ----------------------------------------
// Delegates to 'runObject'
int CppiaDynamicExpr::runInt(CppiaCtx *ctx)
{
hx::Object *obj = runObject(ctx);
return ValToInt(obj);
}
Float CppiaDynamicExpr::runFloat(CppiaCtx *ctx)
{
return ValToFloat(runObject(ctx));
}
::String CppiaDynamicExpr::runString(CppiaCtx *ctx)
{
hx::Object *result = runObject(ctx);
BCR_CHECK;
return result ? result->toString() : String();
}
void CppiaDynamicExpr::runVoid(CppiaCtx *ctx)
{
runObject(ctx);
}
// --- CppiaVoidExpr ----------------------------------------
// Delegates to 'runInt'
struct CppiaVoidExpr : public CppiaExpr
{
CppiaVoidExpr(const CppiaExpr *inSrc=0) : CppiaExpr(inSrc) {}
const char *getName() { return "CppiaVoidExpr"; }
ExprType getType() { return etVoid; }
virtual int runInt(CppiaCtx *ctx) { runVoid(ctx); return 0; }
virtual Float runFloat(CppiaCtx *ctx) { runVoid(ctx); return 0.0; }
virtual ::String runString(CppiaCtx *ctx) { runVoid(ctx); return String(); }
virtual hx::Object *runObject(CppiaCtx *ctx) { runVoid(ctx); return 0; }
virtual void runVoid(CppiaCtx *ctx) = 0;
};
// --- CppiaIntExpr ----------------------------------------
// Delegates to 'runInt'
struct CppiaIntExpr : public CppiaExpr
{
CppiaIntExpr(const CppiaExpr *inSrc=0) : CppiaExpr(inSrc) {}
const char *getName() { return "CppiaIntExpr"; }
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) = 0;
};
// --- CppiaBoolExpr ----------------------------------------
// Delegates to 'runInt'
struct CppiaBoolExpr : public CppiaIntExpr
{
CppiaBoolExpr(const CppiaExpr *inSrc=0) : CppiaIntExpr(inSrc) {}
const char *getName() { return "CppiaBoolExpr"; }
hx::Object *runObject(CppiaCtx *ctx) { return Dynamic(runInt(ctx) ? true : false).mPtr; }
String runString(CppiaCtx *ctx) { return runInt(ctx)?HX_CSTRING("true") : HX_CSTRING("false");}
bool isBoolInt() { return true; }
#ifdef CPPIA_JIT
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
{
JumpId notCondition = genCompare(compiler, true, 0);
if (destType==etObject)
compiler->move(inDest, (void *)Dynamic(true).mPtr);
else
compiler->move(inDest, (int)1);
JumpId notDone = compiler->jump();
compiler->comeFrom(notCondition);
if (destType==etObject)
compiler->move(inDest, (void *)Dynamic(false).mPtr);
else
compiler->move(inDest, (int)0);
compiler->comeFrom(notDone);
}
#endif
};
// ------------------------------------------------------
CppiaExpr *createStaticAccess(CppiaExpr *inSrc, FieldStorage inType, void *inPtr);
static void ReadExpressions(Expressions &outExpressions, CppiaStream &stream,int inN=-1)
{
int count = inN>=0 ? inN : stream.getInt();
outExpressions.resize(count);
for(int i=0;i<count;i++)
outExpressions[i] = createCppiaExpr(stream);
}
void LinkExpressions(Expressions &ioExpressions, CppiaModule &data)
{
for(int i=0;i<ioExpressions.size();i++)
ioExpressions[i] = ioExpressions[i]->link(data);
}
CppiaExpr *convertToFunction(CppiaExpr *inExpr);
bool TypeData::isClassOf(Dynamic inInstance)
{
if (cppiaClass)
return cppiaClass->getClass()->VCanCast(inInstance.mPtr);
else if (haxeClass.mPtr)
return __instanceof(inInstance,haxeClass) || isDynamic;
return false;
}
CppiaExpr *convertToFunction(CppiaExpr *inExpr) { return new ScriptCallable(inExpr); }
template<typename T>
static hx::Object *convert(hx::Object *obj)
{
Array_obj<T> *alreadyGood = dynamic_cast<Array_obj<T> *>(obj);
if (alreadyGood)
return alreadyGood;
cpp::VirtualArray_obj *varray = dynamic_cast<cpp::VirtualArray_obj *>(obj);
if (varray)
{
return Array<T>( cpp::VirtualArray(varray) ).mPtr;
}
int n = obj->__length();
Array<T> result = Array_obj<T>::__new(n,n);
for(int i=0;i<n;i++)
result[i] = obj->__GetItem(i);
return result.mPtr;
}
hx::Object *DynamicToArrayType(hx::Object *obj, ArrayType arrayType)
{
switch(arrayType)
{
case arrBool: return convert<bool>(obj);
case arrUnsignedChar: return convert<unsigned char>(obj);
case arrInt: return convert<int>(obj);
case arrFloat: return convert<Float>(obj);
case arrFloat32: return convert<float>(obj);
case arrString: return convert<String>(obj);
case arrAny:
{
ArrayBase *base = dynamic_cast<ArrayBase *>(obj);
if (base)
return new cpp::VirtualArray_obj(base);
return dynamic_cast<cpp::VirtualArray_obj *>(obj);
}
case arrObject: return convert<Dynamic>(obj);
case arrNotArray: throw "Bad cast";
}
return 0;
}
void runFunExpr(CppiaCtx *ctx, ScriptCallable *inFunExpr, hx::Object *inThis, Expressions &inArgs )
{
unsigned char *pointer = ctx->pointer;
inFunExpr->pushArgs(ctx, inThis, inArgs);
BCR_VCHECK;
AutoStack save(ctx,pointer);
ctx->runVoid( inFunExpr );
}
hx::Object *runFunExprDynamic(CppiaCtx *ctx, ScriptCallable *inFunExpr, hx::Object *inThis, Array<Dynamic> &inArgs )
{
unsigned char *pointer = ctx->pointer;
inFunExpr->pushArgsDynamic(ctx, inThis, inArgs);
AutoStack save(ctx,pointer);
return runContextConvertObject(ctx, inFunExpr->getReturnType(), inFunExpr );
}
void runFunExprDynamicVoid(CppiaCtx *ctx, ScriptCallable *inFunExpr, hx::Object *inThis, Array<Dynamic> &inArgs )
{
unsigned char *pointer = ctx->pointer;
inFunExpr->pushArgsDynamic(ctx, inThis, inArgs);
AutoStack save(ctx,pointer);
ctx->runVoid(inFunExpr);
}
struct BlockCallable : public ScriptCallable
{
BlockCallable(CppiaExpr *inExpr) : ScriptCallable(inExpr)
{
}
ExprType getType() { return body->getType(); }
void runVoid(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
ctx->push( ctx->getThis(false) );
AutoStack save(ctx,pointer);
addStackVarsSpace(ctx);
CPPIA_STACK_FRAME(this);
CPPIA_STACK_LINE(this);
body->runVoid(ctx);
}
int runInt(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
ctx->push( ctx->getThis(false) );
AutoStack save(ctx,pointer);
addStackVarsSpace(ctx);
CPPIA_STACK_FRAME(this);
CPPIA_STACK_LINE(this);
return body->runInt(ctx);
}
Float runFloat(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
ctx->push( ctx->getThis(false) );
AutoStack save(ctx,pointer);
addStackVarsSpace(ctx);
CPPIA_STACK_FRAME(this);
CPPIA_STACK_LINE(this);
return body->runFloat(ctx);
}
hx::Object *runObject(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
ctx->push( ctx->getThis(false) );
AutoStack save(ctx,pointer);
addStackVarsSpace(ctx);
CPPIA_STACK_FRAME(this);
CPPIA_STACK_LINE(this);
return body->runObject(ctx);
}
};
struct BlockExpr : public CppiaExpr
{
Expressions expressions;
BlockExpr(CppiaStream &stream)
{
ReadExpressions(expressions,stream);
}
CppiaExpr *link(CppiaModule &data)
{
if (data.layout==0)
{
CppiaExpr *blockFunc = new BlockCallable(this);
return blockFunc->link(data);
}
LinkExpressions(expressions,data);
return this;
}
const char *getName() { return "BlockExpr"; }
virtual ExprType getType()
{
if (expressions.size()==0)
return etNull;
return expressions[expressions.size()-1]->getType();
}
#define BlockExprRun(ret,name,defVal) \
ret name(CppiaCtx *ctx) \
{ \
int last = expressions.size()-1; \
for(int a=0;a<last;a++) \
{ \
CPPIA_STACK_LINE(expressions[a]); \
expressions[a]->runVoid(ctx); \
BCR_CHECK; \
} \
if (last>=0) \
return expressions[last]->name(ctx); \
return defVal; \
}
BlockExprRun(int,runInt,0)
BlockExprRun(Float ,runFloat,0)
BlockExprRun(String,runString,null())
BlockExprRun(hx::Object *,runObject,0)
void runVoid(CppiaCtx *ctx)
{
if (expressions.size()==0)
return;
CppiaExpr **e = &expressions[0];
CppiaExpr **end = e+expressions.size();
for(;e<end && !ctx->breakContReturn && !ctx->exception;e++)
{
CPPIA_STACK_LINE((*e));
(*e)->runVoid(ctx);
}
}
#ifdef CPPIA_JIT
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
{
int n = expressions.size();
int lineOffset = compiler->getLineOffset();
//compiler->trace(filename);
for(int i=0;i<n;i++)
{
if (lineOffset)
compiler->move( sJitFrame.star(etInt)+lineOffset, expressions[i]->line );
//compiler->traceInt("line",expressions[i]->line);
if (i<n-1)
expressions[i]->genCode(compiler);
else
expressions[i]->genCode(compiler, inDest, destType);
}
}
#endif
};
struct IfElseExpr : public CppiaExpr
{
CppiaExpr *condition;
CppiaExpr *doIf;
CppiaExpr *doElse;
const char *getName() { return "IfElseExpr"; }
IfElseExpr(CppiaStream &stream)
{
condition = createCppiaExpr(stream);
doIf = createCppiaExpr(stream);
doElse = createCppiaExpr(stream);
}
CppiaExpr *link(CppiaModule &inModule)
{
condition = condition->link(inModule);
doIf = doIf->link(inModule);
doElse = doElse->link(inModule);
return this;
}
void runVoid(CppiaCtx *ctx)
{
if (condition->runInt(ctx))
doIf->runVoid(ctx);
else
doElse->runVoid(ctx);
}
#define IF_ELSE_RUN(TYPE,NAME) \
TYPE NAME(CppiaCtx *ctx) \
{ \
if (condition->runInt(ctx)) \
{ \
BCR_CHECK; \
return doIf->NAME(ctx); \
} \
BCR_CHECK; \
return doElse->NAME(ctx); \
}
IF_ELSE_RUN(hx::Object *,runObject)
IF_ELSE_RUN(int,runInt)
IF_ELSE_RUN(String,runString)
IF_ELSE_RUN(Float,runFloat)
#ifdef CPPIA_JIT
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
{
JumpId ifNot = condition->genCompare(compiler,true);
doIf->genCode(compiler,inDest,destType);
JumpId doneIf = compiler->jump();
compiler->comeFrom(ifNot);
doElse->genCode(compiler,inDest,destType);
compiler->comeFrom(doneIf);
}
#endif
};
struct IfExpr : public CppiaDynamicExpr
{
CppiaExpr *condition;
CppiaExpr *doIf;
IfExpr(CppiaStream &stream)
{
condition = createCppiaExpr(stream);
doIf = createCppiaExpr(stream);
}
const char *getName() { return "IfExpr"; }
CppiaExpr *link(CppiaModule &inModule)
{
condition = condition->link(inModule);
doIf = doIf->link(inModule);
return this;
}
hx::Object *runObject(CppiaCtx *ctx) { runVoid(ctx); return 0; }
void runVoid(CppiaCtx *ctx)
{
if (condition->runInt(ctx))
{
BCR_VCHECK;
doIf->runVoid(ctx);
}
}
#ifdef CPPIA_JIT
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
{
JumpId ifNot = condition->genCompare(compiler,true);
doIf->genCode(compiler,inDest,destType);
compiler->comeFrom(ifNot);
}
#endif
};
struct CppiaIsNull : public CppiaBoolExpr
{
CppiaExpr *condition;
CppiaIsNull(CppiaStream &stream) { condition = createCppiaExpr(stream); }
const char *getName() { return "IsNull"; }
CppiaExpr *link(CppiaModule &inModule)
{
condition = condition->link(inModule);
return this;
}
int runInt(CppiaCtx *ctx)
{
if (condition->getType()==etString)
return condition->runString(ctx)==null();
else
return condition->runObject(ctx)==0;
}
#ifdef CPPIA_JIT
JumpId genCompare(CppiaCompiler *compiler,bool inReverse,LabelId inLabel)
{
if (condition->getType()==etString)
{
JitTemp val(compiler,jtString);
condition->genCode(compiler, val, etString);
return compiler->compare(inReverse ? cmpP_NOT_EQUAL : cmpP_EQUAL, val.as(jtPointer) + StringOffset::Ptr, (void *)0, inLabel);
}
else
{
condition->genCode(compiler, sJitTemp0, etObject);
// inReverse = false -> jump if not 0
// inReverse = true -> jump if zero
return compiler->compare(inReverse ? cmpP_NOT_EQUAL : cmpP_EQUAL, sJitTemp0.as(jtPointer), (void *)0, inLabel);
}
}
#endif
};
struct CppiaIsNotNull : public CppiaBoolExpr
{
CppiaExpr *condition;
CppiaIsNotNull(CppiaStream &stream) { condition = createCppiaExpr(stream); }
const char *getName() { return "IsNotNull"; }
CppiaExpr *link(CppiaModule &inModule) { condition = condition->link(inModule); return this; }
int runInt(CppiaCtx *ctx)
{
if (condition->getType()==etString)
return condition->runString(ctx)!=null();
else
return condition->runObject(ctx)!=0;
}
#ifdef CPPIA_JIT
JumpId genCompare(CppiaCompiler *compiler,bool inReverse,LabelId inLabel)
{
if (condition->getType()==etString)
{
JitTemp val(compiler,jtString);
condition->genCode(compiler, val, etString);
return compiler->compare(inReverse ? cmpP_EQUAL : cmpP_NOT_EQUAL, val.as(jtPointer) + StringOffset::Ptr, (void *)0, inLabel);
}
else
{
condition->genCode(compiler, sJitTemp0, etObject);
// inReverse = false -> jump if not 0
// inReverse = true -> jump if zero
return compiler->compare(inReverse ? cmpP_EQUAL : cmpP_NOT_EQUAL, sJitTemp0.as(jtPointer), (void *)0, inLabel);
}
}
#endif
};
#ifdef CPPIA_JIT
void genFunctionResult(CppiaCompiler *compiler,const JitVal &inDest, ExprType destType, ExprType returnType, bool isBoolReturn)
{
compiler->checkException();
// result is at 'framePos'
if (isBoolReturn && (destType==etObject || destType==etString))
{
JumpId isZero = compiler->compare(cmpI_EQUAL,JitFramePos(compiler->getCurrentFrameSize()).as(jtInt),(int)0);
if (destType==etObject)
compiler->move(inDest.as(jtPointer),(void *)Dynamic(true).mPtr);
else
{
compiler->move(inDest.as(jtInt),String(true).length);
compiler->move(inDest.as(jtPointer)+StringOffset::Ptr,(void *)String(true).raw_ptr());
}
JumpId done = compiler->jump();
compiler->comeFrom(isZero);
if (destType==etObject)
compiler->move(inDest.as(jtPointer),(void *)Dynamic(false).mPtr);
else
{
compiler->move(inDest.as(jtInt),String(false).length);
compiler->move(inDest.as(jtPointer)+StringOffset::Ptr,(void *)String(false).raw_ptr());
}
compiler->comeFrom(done);
}
else
{
compiler->convertResult( returnType, inDest, destType );
}
}
void genFunctionCall(ScriptCallable *function, CppiaCompiler *compiler,
const JitVal &inDest, ExprType destType, bool isBoolReturn, ExprType returnType,
CppiaExpr *thisExpr, Expressions &args, const JitVal &inThisVal )
{
int framePos = compiler->getCurrentFrameSize();
// Push args...
function->genArgs(compiler,thisExpr,args, inThisVal);
compiler->restoreFrameSize(framePos);
// Store new frame in context ...
compiler->add( sJitCtxFrame, sJitFrame, JitVal(framePos) );
// Compiled yet
if (function->compiled)
{
compiler->call( JitVal( (void *)(function->compiled)), sJitCtx );
}
else
{
// Compiled later
compiler->move( sJitTemp1, JitVal((void *)&function->compiled) );
compiler->call( sJitTemp1.star(), sJitCtx );
}
compiler->setMaxPointer();
genFunctionResult(compiler, inDest, destType, returnType, isBoolReturn);
}
#endif
struct CallFunExpr : public CppiaExpr
{
Expressions args;
CppiaExpr *thisExpr;
ScriptCallable *function;
ExprType returnType;
bool isBoolReturn;
bool isThisCall;
CallFunExpr(const CppiaExpr *inSrc, CppiaExpr *inThisExpr, ScriptCallable *inFunction, Expressions &ioArgs, bool inThisCall )
: CppiaExpr(inSrc)
{
args.swap(ioArgs);
function = inFunction;
thisExpr = inThisExpr;
returnType = etVoid;
isBoolReturn = false;
isThisCall = inThisCall;
}
CppiaExpr *link(CppiaModule &inModule)
{
LinkExpressions(args,inModule);
// Should already be linked
//function = (ScriptCallable *)function->link(inModule);
if (thisExpr)
thisExpr = thisExpr->link(inModule);
returnType = inModule.types[ function->returnTypeId ]->expressionType;
isBoolReturn = inModule.types[ function->returnTypeId ]->haxeClass==ClassOf<bool>();
return this;
}
const char *getName() { return "CallFunExpr"; }
ExprType getType() { return returnType; }
bool isBoolInt() { return isBoolReturn; }
#define CallFunExprVal(ret,name,funcName) \
ret name(CppiaCtx *ctx) \
{ \
unsigned char *pointer = ctx->pointer; \
function->pushArgs(ctx,thisExpr?thisExpr->runObject(ctx):ctx->getThis(false),args); \
BCR_CHECK; \
AutoStack save(ctx,pointer); \
return funcName(ctx, function->getReturnType(), function); \
}
CallFunExprVal(int,runInt, runContextConvertInt);
CallFunExprVal(Float ,runFloat, runContextConvertFloat);
//CallFunExprVal(hx::Object * ,runObject, runContextConvertObject);
//CallFunExprVal(String ,runString, runContextConvertString);
String runString(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
function->pushArgs(ctx,thisExpr?thisExpr->runObject(ctx):ctx->getThis(false),args);
BCR_CHECK;
AutoStack save(ctx,pointer);
if (isBoolReturn)
return String(ctx->runInt(function) ? true : false );
return runContextConvertString(ctx, function->getReturnType(), function);
}
hx::Object *runObject(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
function->pushArgs(ctx,thisExpr?thisExpr->runObject(ctx):ctx->getThis(false),args);
BCR_CHECK;
AutoStack save(ctx,pointer);
if (isBoolReturn)
return Dynamic(ctx->runInt(function) ? true : false ).mPtr;
return runContextConvertObject(ctx, function->getReturnType(), function);
}
void runVoid(CppiaCtx *ctx)
{
unsigned char *pointer = ctx->pointer;
function->pushArgs(ctx,thisExpr?thisExpr->runObject(ctx):ctx->getThis(false),args);
BCR_VCHECK;
AutoStack save(ctx,pointer);
ctx->runVoid(function);
}
#ifdef CPPIA_JIT
static void SLJIT_CALL callScriptable(CppiaCtx *inCtx, ScriptCallable *inScriptable)
{
// compiled?
CPPIALOG("callScriptable %p(%p) -> %p\n", inCtx, CppiaCtx::getCurrent(), inScriptable );
CPPIALOG(" name = %s\n", inScriptable->getName());
inScriptable->runFunction(inCtx);
CPPIALOG(" Done scipt callable\n");
}
// Function Call
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
{
genFunctionCall(function, compiler, inDest, destType, isBoolReturn, returnType,thisExpr, args,
isThisCall ? (JitVal)sJitThis : JitVal());
}
#endif
};
// ---
struct CppiaExprWithValue : public CppiaDynamicExpr
{
Dynamic value;
CppiaExprWithValue(const CppiaExpr *inSrc=0) : CppiaDynamicExpr(inSrc)
{
value.mPtr = 0;
}
hx::Object *runObject(CppiaCtx *ctx) { return value.mPtr; }
void mark(hx::MarkContext *__inCtx) { HX_MARK_MEMBER(value); }
#ifdef HXCPP_VISIT_ALLOCS
void visit(hx::VisitContext *__inCtx) { HX_VISIT_MEMBER(value); }
#endif
const char *getName() { return "CppiaExprWithValue"; }
CppiaExpr *link(CppiaModule &inModule)
{
inModule.markable.push_back(this);
return this;
}
void runVoid(CppiaCtx *ctx) { runObject(ctx); }
#ifdef CPPIA_JIT
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
{
if (destType!=etNull && destType!=etVoid)
compiler->convert( (void *)value.mPtr, etObject, inDest, destType);
}
#endif
};
// ---
#ifdef CPPIA_JIT
void SLJIT_CALL callDynamic(CppiaCtx *ctx, hx::Object *inFunction, int inArgs)
{
if (!inFunction)
{
ctx->exception = Dynamic(HX_CSTRING("Null Function")).mPtr;
return;
}
// ctx.frame points to 0th arg
hx::Object **base = ((hx::Object **)(ctx->frame) );
unsigned char *oldPointer = ctx->pointer;
ctx->pointer = ctx->frame;
//hx::Object **base = ((hx::Object **)(ctx->pointer) ) - inArgs;
//ctx->pointer = (unsigned char *)base;
TRY_NATIVE
#if (HXCPP_API_LEVEL>=500)
Array<Dynamic> argArray = Array_obj<Dynamic>::__new(inArgs, inArgs);
for (int s = 0; s < inArgs; s++)
argArray[s] = base[s];
base[0] = inFunction->__Run(argArray).mPtr;
#else
switch(inArgs)
{
case 0:
base[0] = inFunction->__run().mPtr;
break;
case 1:
base[0] = inFunction->__run(base[0]).mPtr;
break;
case 2:
base[0] = inFunction->__run(base[0],base[1]).mPtr;
break;
case 3:
base[0] = inFunction->__run(base[0],base[1],base[2]).mPtr;
break;
case 4:
base[0] = inFunction->__run(base[0],base[1],base[2],base[3]).mPtr;
break;
case 5:
base[0] = inFunction->__run(base[0],base[1],base[2],base[3],base[4]).mPtr;
break;
default:
{
Array<Dynamic> argArray = Array_obj<Dynamic>::__new(inArgs,inArgs);
for(int s=0;s<inArgs;s++)
argArray[s] = base[s];
base[0] = inFunction->__Run(argArray).mPtr;
}
}
#endif
CATCH_NATIVE
ctx->pointer = oldPointer;
}
#endif
static int idx = 0;
struct CallDynamicFunction : public CppiaExprWithValue
{
Expressions args;
CallDynamicFunction(CppiaModule &inModule, const CppiaExpr *inSrc,
Dynamic inFunction, Expressions &ioArgs )
: CppiaExprWithValue(inSrc)
{
args.swap(ioArgs);
value = inFunction;
inModule.markable.push_back(this);
}
CppiaExpr *link(CppiaModule &inModule)
{
LinkExpressions(args,inModule);
return CppiaExprWithValue::link(inModule);
}
const char *getName() { return "CallDynamicFunction"; }
ExprType getType() { return etObject; }
hx::Object *runObject(CppiaCtx *ctx)
{
int n = args.size();
#if (HXCPP_API_LEVEL>=500)
Array<Dynamic> argVals = Array_obj<Dynamic>::__new(n, n);
for (int a = 0; a < n; a++)
{
argVals[a] = Dynamic(args[a]->runObject(ctx));
BCR_CHECK;
}
return value->__Run(argVals).mPtr;
#else
switch(n)
{
case 0:
return value->__run().mPtr;
case 1:
{
Dynamic arg0( args[0]->runObject(ctx) );
BCR_CHECK;
return value->__run(arg0).mPtr;
}
case 2:
{
Dynamic arg0( args[0]->runObject(ctx) );
BCR_CHECK;
Dynamic arg1( args[1]->runObject(ctx) );
BCR_CHECK;
return value->__run(arg0,arg1).mPtr;
}
case 3:
{
Dynamic arg0( args[0]->runObject(ctx) );
BCR_CHECK;
Dynamic arg1( args[1]->runObject(ctx) );
BCR_CHECK;
Dynamic arg2( args[2]->runObject(ctx) );
BCR_CHECK;
return value->__run(arg0,arg1,arg2).mPtr;
}
case 4:
{
Dynamic arg0( args[0]->runObject(ctx) );
BCR_CHECK;
Dynamic arg1( args[1]->runObject(ctx) );