-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathast_simulate.cpp
More file actions
4193 lines (4040 loc) · 210 KB
/
Copy pathast_simulate.cpp
File metadata and controls
4193 lines (4040 loc) · 210 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 "daScript/misc/platform.h"
#include "daScript/ast/ast.h"
#include "daScript/ast/ast_match.h"
#include "daScript/ast/ast_expressions.h"
#include "daScript/ast/ast_visitor.h"
#include "daScript/ast/ast_simulate.h"
#include "daScript/simulate/simulate_fusion.h"
#include "daScript/simulate/runtime_array.h"
#include "daScript/simulate/runtime_table_nodes.h"
#include "daScript/simulate/runtime_range.h"
#include "daScript/simulate/runtime_string_delete.h"
#include "daScript/simulate/hash.h"
#include "daScript/simulate/simulate_nodes.h"
#include "daScript/simulate/simulate_visit_op.h"
#include "daScript/misc/gc_node.h"
#include "daScript/simulate/standalone_ctx_utils.h"
das::Context * get_context ( int stackSize=0 );//link time resolved dependencies
namespace das
{
// fusion function pointers (defined here in main lib, set by fusion lib)
void (*g_fusionContextFn) ( Context & context, TextWriter & logs, bool enableFusion ) = nullptr;
void (*g_resetFusionEngineFn) () = nullptr;
// topological sort for the [init] nodes
struct InitSort {
struct Entry {
uint64_t id;
vector<string> pass;
vector<string> before;
vector<string> after;
};
vector<Entry> entires;
struct Node {
uint64_t id;
vector<uint64_t> before;
};
vector<Node> build_unsorted () {
map<uint64_t, Node> nodes;
map<string, set<uint64_t>> tags;
for ( auto & e : entires ) {
Node & n = nodes[e.id];
n.id = e.id;
for ( auto & p : e.pass ) {
tags[p].insert(e.id);
}
}
for ( auto & e : entires ) {
Node & n = nodes[e.id];
for ( auto & b : e.before ) {
for ( auto & t : tags[b] ) {
n.before.push_back(t);
}
}
for ( auto & a : e.after ) {
for ( auto & t : tags[a] ) {
nodes[t].before.push_back(e.id);
}
}
}
vector<Node> unsorted;
for ( auto & n : nodes ) {
unsorted.emplace_back(n.second);
}
return unsorted;
}
vector<uint64_t> sort () {
auto unsorted = build_unsorted();
vector<uint64_t> result;
auto lnodes = unsorted.size();
if ( lnodes != 0 ) {
vector<Node> sorted;
sorted.reserve(lnodes);
while ( unsorted.size() ) {
auto node = das::move(unsorted[0]);
unsorted.erase(unsorted.begin());
if ( node.before.size()==0 ) {
for ( auto & other : unsorted ) {
for ( int i=int(other.before.size())-1; i>=0; --i ) {
if ( other.before[i] == node.id ) {
other.before.erase(other.before.begin()+i);
}
}
}
sorted.emplace_back(node);
} else {
unsorted.emplace_back(node);
}
}
DAS_ASSERTF(sorted.size()==lnodes,"cyclic dependency in [init] nodes");
result.reserve(lnodes);
for ( auto & n : sorted ) {
result.push_back(n.id);
}
}
return result;
}
void addNode ( uint64_t mnh, AnnotationArgumentList & args ) {
Entry e;
e.id = mnh;
for ( auto & arg : args ) {
if ( arg.name=="tag" && arg.type==Type::tString ) {
e.pass.push_back(arg.sValue);
} else if ( arg.name=="before" && arg.type==Type::tString ) {
e.before.push_back(arg.sValue);
} else if ( arg.name=="after" && arg.type==Type::tString ) {
e.after.push_back(arg.sValue);
}
}
entires.push_back(e);
}
};
// Const-string table key: bake the key bytes (into the const-string heap) and their hash
// at simulate time so the runtime node skips the per-lookup hash_blockz64 walk. Returns the
// baked key char* (and sets outHash) when keyType is string and keyExpr is an ExprConstString;
// nullptr otherwise (caller falls back to the regular hashing node).
static __forceinline char * bakeConstStringKey ( Context & context, Expression * keyExpr,
const TypeDecl * keyType, uint64_t & outHash ) {
if ( keyType->baseType != Type::tString || !keyExpr->rtti_isStringConstant() ) return nullptr;
char * keyStr = context.constStringHeap->impl_allocateString(static_cast<ExprConstString*>(keyExpr)->getValue());
outHash = hash_blockz64((uint8_t *)keyStr);
return keyStr;
}
struct SimulateVisitor : Visitor {
Context & context;
das_hash_map<const Expression*, SimNode*> e2v;
das_hash_map<const Expression*, vector<SimNode*>> makeLocalInits;
das_hash_map<const ExprLet*, vector<SimNode*>> letInitVec;
SimulateVisitor ( Context & ctx ) : context(ctx) {}
virtual bool canVisitLabel ( ExprLabel * ) override { return false; }
virtual bool canVisitReader ( ExprReader * ) override { return false; }
void setE ( const Expression * e, SimNode * n ) {
e2v[e] = n;
}
SimNode * getE ( const Expression * e ) {
auto it = e2v.find(e);
DAS_ASSERTF(it != e2v.end(), "SimulateVisitor::getE - expression not found in e2v");
return it->second;
}
// All expression types must be handled by explicit visit() overrides.
virtual ExpressionPtr visitExpression ( Expression * expr ) override {
DAS_ASSERTF(0, "SimulateVisitor: unhandled expression type %s", expr->__rtti);
setE(expr, nullptr);
return expr;
}
// Simulate an expression subtree via the visitor walk
SimNode * simulateExpression ( Expression * expr ) {
expr->visit(*this);
return getE(expr);
}
// --- Helper methods ---
vector<SimNode *> simulateExprMakeVariant(const ExprMakeVariant *mkv);
vector<SimNode *> simulateExprMakeStruct(const ExprMakeStruct *mks);
vector<SimNode *> simulateExprMakeArray(const ExprMakeArray *mka);
vector<SimNode *> simulateExprMakeTuple(const ExprMakeTuple *mkt);
SimNode * sv_makeLocalCMResMove(const LineInfo & at, uint32_t offset, ExpressionPtr rE);
SimNode * sv_makeLocalCMResCopy(const LineInfo & at, uint32_t offset, ExpressionPtr rE);
SimNode * sv_makeLocalRefMove(const LineInfo & at, uint32_t stackTop, uint32_t offset, ExpressionPtr rE);
SimNode * sv_makeLocalRefCopy(const LineInfo & at, uint32_t stackTop, uint32_t offset, ExpressionPtr rE);
SimNode * sv_makeLocalMove(const LineInfo & at, uint32_t stackTop, ExpressionPtr rE);
SimNode * sv_makeLocalCopy(const LineInfo & at, uint32_t stackTop, ExpressionPtr rE);
SimNode * sv_makeCopy(const LineInfo & at, ExpressionPtr lE, ExpressionPtr rE);
SimNode * sv_makeMove(const LineInfo & at, ExpressionPtr lE, ExpressionPtr rE);
SimNode_CallBase * sv_simulateCall(const FunctionPtr & func, const ExprLooksLikeCall * expr, SimNode_CallBase * pCall);
SimNode * sv_trySimulate(const Expression * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType);
SimNode * sv_trySimulate_Var(const ExprVar * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType);
SimNode * sv_trySimulate_Cast(const ExprCast * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType);
SimNode * sv_trySimulate_At(const ExprAt * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType);
SimNode * sv_trySimulate_Swizzle(const ExprSwizzle * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType);
SimNode * sv_trySimulate_Field(const ExprField * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType);
vector<SimNode*> sv_simulateLocal(const ExprMakeLocal * expr);
SimNode * sv_simulateLetInit(const VariablePtr & var, bool local);
vector<SimNode *> sv_simulateLetInits(const ExprLet * pLet);
void sv_simulateLabels(const ExprBlock * expr, SimNode_Block * block, const das_map<int32_t,uint32_t> & ofsmap);
void sv_simulateFinal(const ExprBlock * expr, SimNode_Final * block);
void sv_simulateBlock(const ExprBlock * expr, SimNode_Block * block);
void sv_whileSimulateFinal(ExpressionPtr bod, SimNode_Block * blk);
vector<SimNode *> sv_collectExpressions(const vector<ExpressionPtr> & lis, das_map<int32_t,uint32_t> * ofsmap = nullptr);
SimNode * sv_keepAlive(const LineInfo & at, SimNode * result);
// All ExprConstXxx types simulate identically: ConstValue(at, value)
#define SV_CONST_VISIT(ExprType) \
ExpressionPtr visit(ExprType * expr) override { \
setE(expr, context.code->makeNode<SimNode_ConstValue>(expr->at, expr->value)); \
return expr; \
}
// ExprConst is called as final dispatch after specific ExprConstXxx visitors.
// Those already set e2v, so this is a no-op passthrough.
ExpressionPtr visit(ExprConst * expr) override { return expr; }
SV_CONST_VISIT(ExprFakeContext)
SV_CONST_VISIT(ExprFakeLineInfo)
SV_CONST_VISIT(ExprConstPtr)
SV_CONST_VISIT(ExprConstEnumeration)
SV_CONST_VISIT(ExprConstBitfield)
SV_CONST_VISIT(ExprConstInt8)
SV_CONST_VISIT(ExprConstInt16)
SV_CONST_VISIT(ExprConstInt64)
SV_CONST_VISIT(ExprConstInt)
SV_CONST_VISIT(ExprConstInt2)
SV_CONST_VISIT(ExprConstInt3)
SV_CONST_VISIT(ExprConstInt4)
SV_CONST_VISIT(ExprConstUInt8)
SV_CONST_VISIT(ExprConstUInt16)
SV_CONST_VISIT(ExprConstUInt64)
SV_CONST_VISIT(ExprConstUInt)
SV_CONST_VISIT(ExprConstUInt2)
SV_CONST_VISIT(ExprConstUInt3)
SV_CONST_VISIT(ExprConstUInt4)
SV_CONST_VISIT(ExprConstRange)
SV_CONST_VISIT(ExprConstURange)
SV_CONST_VISIT(ExprConstRange64)
SV_CONST_VISIT(ExprConstURange64)
SV_CONST_VISIT(ExprConstBool)
SV_CONST_VISIT(ExprConstFloat)
SV_CONST_VISIT(ExprConstFloat16)
SV_CONST_VISIT(ExprConstFloat2)
SV_CONST_VISIT(ExprConstFloat3)
SV_CONST_VISIT(ExprConstFloat4)
SV_CONST_VISIT(ExprConstDouble)
#undef SV_CONST_VISIT
ExpressionPtr visit(ExprConstString * expr) override;
ExpressionPtr visit(ExprLooksLikeCall * expr) override { return expr; }
ExpressionPtr visit(ExprVar * expr) override;
ExpressionPtr visit(ExprAddr * expr) override;
ExpressionPtr visit(ExprTypeDecl * expr) override;
ExpressionPtr visit(ExprBreak * expr) override;
ExpressionPtr visit(ExprContinue * expr) override;
ExpressionPtr visit(ExprAssume * expr) override;
ExpressionPtr visit(ExprStaticAssert * expr) override;
ExpressionPtr visit(ExprReader * expr) override;
ExpressionPtr visit(ExprLabel * expr) override;
ExpressionPtr visit(ExprTag * expr) override;
ExpressionPtr visit(ExprIs * expr) override;
ExpressionPtr visit(ExprMakeGenerator * expr) override;
ExpressionPtr visit(ExprYield * expr) override;
ExpressionPtr visit(ExprArrayComprehension * expr) override;
ExpressionPtr visit(ExprRef2Value * expr) override;
ExpressionPtr visit(ExprRef2Ptr * expr) override;
ExpressionPtr visit(ExprPtr2Ref * expr) override;
ExpressionPtr visit(ExprCast * expr) override;
ExpressionPtr visit(ExprAscend * expr) override;
ExpressionPtr visit(ExprDelete * expr) override;
ExpressionPtr visit(ExprMemZero * expr) override;
ExpressionPtr visit(ExprUnsafe * expr) override;
ExpressionPtr visit(ExprWith * expr) override;
ExpressionPtr visit(ExprNullCoalescing * expr) override;
ExpressionPtr visit(ExprGoto * expr) override;
ExpressionPtr visit(ExprOp1 * expr) override;
ExpressionPtr visit(ExprOp2 * expr) override;
ExpressionPtr visit(ExprOp3 * expr) override;
ExpressionPtr visit(ExprCopy * expr) override;
ExpressionPtr visit(ExprMove * expr) override;
ExpressionPtr visit(ExprClone * expr) override;
ExpressionPtr visit(ExprTryCatch * expr) override;
ExpressionPtr visit(ExprAssert * expr) override;
ExpressionPtr visit(ExprQuote * expr) override;
ExpressionPtr visit(ExprDebug * expr) override;
ExpressionPtr visit(ExprInvoke * expr) override;
ExpressionPtr visit(ExprErase * expr) override;
ExpressionPtr visit(ExprSetInsert * expr) override;
ExpressionPtr visit(ExprFind * expr) override;
ExpressionPtr visit(ExprKeyExists * expr) override;
ExpressionPtr visit(ExprTypeInfo * expr) override;
ExpressionPtr visit(ExprCall * expr) override;
ExpressionPtr visit(ExprNamedCall * expr) override;
ExpressionPtr visit(ExprNew * expr) override;
ExpressionPtr visit(ExprAt * expr) override;
ExpressionPtr visit(ExprSafeAt * expr) override;
ExpressionPtr visit(ExprSwizzle * expr) override;
ExpressionPtr visit(ExprField * expr) override;
ExpressionPtr visit(ExprSafeField * expr) override;
ExpressionPtr visit(ExprIsVariant * expr) override;
ExpressionPtr visit(ExprAsVariant * expr) override;
ExpressionPtr visit(ExprSafeAsVariant * expr) override;
ExpressionPtr visit(ExprStringBuilder * expr) override;
ExpressionPtr visit(ExprReturn * expr) override;
ExpressionPtr visit(ExprIfThenElse * expr) override;
ExpressionPtr visit(ExprWhile * expr) override;
ExpressionPtr visit(ExprFor * expr) override;
ExpressionPtr visit(ExprBlock * expr) override;
ExpressionPtr visit(ExprLet * expr) override;
ExpressionPtr visit(ExprMakeBlock * expr) override;
ExpressionPtr visit(ExprMakeVariant * expr) override;
ExpressionPtr visit(ExprMakeStruct * expr) override;
ExpressionPtr visit(ExprMakeArray * expr) override;
ExpressionPtr visit(ExprMakeTuple * expr) override;
};
// Free function wrapper for SimulateVisitor — usable from other translation units.
SimNode * simulateExpression ( Context & context, Expression * expr ) {
SimulateVisitor sv(context);
return sv.simulateExpression(expr);
}
SimNode * trySimulateExpression ( Context & context, const Expression * expr, uint32_t extraOffset, const TypeDeclPtr & r2vType ) {
SimulateVisitor sv(context);
return sv.sv_trySimulate(expr, extraOffset, r2vType);
}
SimNode * simulateLetInit ( Context & context, const VariablePtr & var, bool local ) {
SimulateVisitor sv(context);
if ( var->init ) sv.simulateExpression(var->init);
return sv.sv_simulateLetInit(var, local);
}
vector<SimNode *> simulateLetInits ( Context & context, const ExprLet * pLet ) {
SimulateVisitor sv(context);
for ( auto & var : pLet->variables ) {
if ( var->init ) sv.simulateExpression(var->init);
}
return sv.sv_simulateLetInits(pLet);
}
// common for move and copy
SimNode_CallBase * getCallBase ( SimNode * node ) {
#if DAS_ENABLE_KEEPALIVE
if ( node->rtti_node_isKeepAlive() ) {
SimNode_KeepAlive * ka = static_cast<SimNode_KeepAlive *>(node);
node = ka->value;
}
#endif
DAS_ASSERTF(node->rtti_node_isCallBase(),"we are calling getCallBase on a node, which is not a call base node."
"we should not be here, script compiler should have caught this during compilation.");
return static_cast<SimNode_CallBase *>(node);
}
SimNode * SimulateVisitor::sv_makeLocalCMResMove (const LineInfo & at, uint32_t offset, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetCMResOfs>(rE->at, offset);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
auto * right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetCMResOfs>(rE->at, offset);
return right;
}
}
// now, to the regular move
auto left = context.code->makeNode<SimNode_GetCMResOfs>(at, offset);
auto right = getE(rE);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_MoveRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeLocalCMResCopy(const LineInfo & at, uint32_t offset, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
DAS_ASSERT ( rightType.canCopy() &&
"we are calling makeLocalCMResCopy on a type, which can't be copied."
"we should not be here, script compiler should have caught this during compilation."
"compiler later will likely report internal compilation error.");
auto right = getE(rE);
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetCMResOfs>(rE->at, offset);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetCMResOfs>(rE->at, offset);
return right;
}
}
// wo standard path
auto left = context.code->makeNode<SimNode_GetCMResOfs>(rE->at, offset);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_CopyRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeLocalRefMove (const LineInfo & at, uint32_t stackTop, uint32_t offset, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocalRefOff>(rE->at, stackTop, offset);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocalRefOff>(rE->at, stackTop, offset);
return right;
}
}
// now, to the regular move
auto left = context.code->makeNode<SimNode_GetLocalRefOff>(at, stackTop, offset);
auto right = getE(rE);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_MoveRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeLocalRefCopy(const LineInfo & at, uint32_t stackTop, uint32_t offset, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
DAS_ASSERT ( rightType.canCopy() &&
"we are calling makeLocalRefCopy on a type, which can't be copied."
"we should not be here, script compiler should have caught this during compilation."
"compiler later will likely report internal compilation error.");
auto right = getE(rE);
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocalRefOff>(rE->at, stackTop, offset);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocalRefOff>(rE->at, stackTop, offset);
return right;
}
}
// wo standard path
auto left = context.code->makeNode<SimNode_GetLocalRefOff>(rE->at, stackTop, offset);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_CopyRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeLocalMove (const LineInfo & at, uint32_t stackTop, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocal>(rE->at, stackTop);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocal>(rE->at, stackTop);
return right;
}
}
// now, to the regular move
auto left = context.code->makeNode<SimNode_GetLocal>(at, stackTop);
auto right = getE(rE);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_MoveRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeLocalCopy(const LineInfo & at, uint32_t stackTop, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
DAS_ASSERT ( rightType.canCopy() &&
"we are calling makeLocalCopy on a type, which can't be copied."
"we should not be here, script compiler should have caught this during compilation."
"compiler later will likely report internal compilation error.");
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocal>(rE->at, stackTop);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = context.code->makeNode<SimNode_GetLocal>(rE->at, stackTop);
return right;
}
}
// now, to the regular copy
auto left = context.code->makeNode<SimNode_GetLocal>(rE->at, stackTop);
auto right = getE(rE);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_CopyRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeCopy(const LineInfo & at, ExpressionPtr lE, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
DAS_ASSERT ( (rightType.canCopy() || rightType.isGoodBlockType()) &&
"we are calling makeCopy on a type, which can't be copied."
"we should not be here, script compiler should have caught this during compilation."
"compiler later will likely report internal compilation error.");
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = getE(lE);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = getE(lE);
return right;
}
}
// now, to the regular copy
auto left = getE(lE);
auto right = getE(rE);
if ( rightType.isRef() ) {
return context.code->makeNode<SimNode_CopyRefValue>(at, left, right, rightType.getSizeOf());
} else {
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * SimulateVisitor::sv_makeMove (const LineInfo & at, ExpressionPtr lE, ExpressionPtr rE ) {
const auto & rightType = *rE->type;
// now, call with CMRES
if ( rE->rtti_isCall() ) {
auto cll = static_cast<ExprCall*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = getE(lE);
return right;
}
}
// now, invoke with CMRES
if ( rE->rtti_isInvoke() ) {
auto cll = static_cast<ExprInvoke*>(rE);
if ( cll->allowCmresSkip() ) {
auto right = getE(rE);
getCallBase(right)->cmresEval = getE(lE);
return right;
}
}
// now to the regular one
if ( rightType.isRef() ) {
auto left = getE(lE);
auto right = getE(rE);
return context.code->makeNode<SimNode_MoveRefValue>(at, left, right, rightType.getSizeOf());
} else {
// this here might happen during initialization, by moving value types
// like var t <- 5
// its ok to generate simplified set here
auto left = getE(lE);
auto right = getE(rE);
return context.code->makeValueNode<SimNode_Set>(rightType.getR2VType(), at, left, right);
}
}
SimNode * Function::makeSimNode ( Context & context, const vector<ExpressionPtr> & ) {
if ( copyOnReturn || moveOnReturn ) {
return context.code->makeNodeUnrollAny<SimNode_CallAndCopyOrMove>(int(arguments.size()), at);
} else if ( fastCall ) {
return context.code->makeNodeUnrollAny<SimNode_FastCall>(int(arguments.size()), at);
} else {
return context.code->makeNodeUnrollAny<SimNode_Call>(int(arguments.size()), at);
}
}
SimNode * Function::simulate (Context & context) const {
if ( builtIn ) {
DAS_ASSERTF(0, "can only simulate non built-in function");
return nullptr;
}
for ( auto & ann : annotations ) {
if ( ann->annotation->rtti_isFunctionAnnotation() ) {
auto fann = (FunctionAnnotation *)(ann->annotation);
string err;
auto node = fann->simulate(&context, (Function*)this, ann->arguments, err);
if ( !node ) {
if ( !err.empty() ) {
context.thisProgram->error("integration error, function failed to simulate", err, "",
at, CompilationError::internal_function_annotation );
return nullptr;
}
} else {
return node;
}
}
}
SimulateVisitor sv(context);
sv.simulateExpression(body);
if ( fastCall ) {
DAS_ASSERT(totalStackSize == sizeof(Prologue) && "function can't allocate stack");
DAS_ASSERT((result->isWorkhorseType() || result->isVoid()) && "fastcall can only return a workhorse type");
DAS_ASSERT(body->rtti_isBlock() && "function must contain a block");
auto block = static_cast<ExprBlock*>(body);
if ( block->list.size()==0 ) {
DAS_ASSERT(block->inFunction && block->inFunction->result->isVoid() && "only void function produces fastcall NOP");
return context.code->makeNode<SimNode_NOP>(block->at);
}
if ( block->list.back()->rtti_isReturn() ) {
DAS_ASSERT(block->list.back()->rtti_isReturn() && "fastcall body expr is return");
auto retE = static_cast<ExprReturn*>(block->list.back());
if ( retE->subexpr ) {
return sv.getE(retE->subexpr);
} else {
return context.code->makeNode<SimNode_NOP>(retE->at);
}
} else {
return sv.getE(block->list.back());
}
} else {
#if DAS_DEBUGGER
if ( context.debugger ) {
auto sbody = sv.getE(body);
if ( !sbody->rtti_node_isBlock() ) {
auto block = context.code->makeNode<SimNodeDebug_BlockNF>(sbody->debugInfo);
block->total = 1;
block->list = (SimNode **) context.code->allocate(sizeof(SimNode *)*1);
block->list[0] = sbody;
return block;
} else {
return sbody;
}
} else {
return sv.getE(body);
}
#else
return sv.getE(body);
#endif
}
}
vector<SimNode *> SimulateVisitor::simulateExprMakeVariant(const ExprMakeVariant *mkv) {
gc_guard gc_scope;
vector<SimNode *> simlist;
int index = 0;
auto mkBaseT = mkv->makeType; // element view - makeType may be a fixed-array chain
while ( mkBaseT->baseType==Type::tFixedArray && mkBaseT->firstType ) mkBaseT = mkBaseT->firstType;
int stride = mkv->makeType->getStride();
// init with 0 it its 'default' initialization
if ( stride && mkv->variants.empty() ) {
int bytes = stride;
SimNode * init0;
if ( mkv->useCMRES ) {
if ( bytes==0 ) {
init0 = nullptr;
} else if ( bytes <= 32 ) {
init0 = context.code->makeNodeUnrollNZ<SimNode_InitLocalCMResN>(bytes, mkv->at, mkv->extraOffset);
} else {
init0 = context.code->makeNode<SimNode_InitLocalCMRes>(mkv->at, mkv->extraOffset, bytes);
}
} else if ( mkv->useStackRef ) {
init0 = context.code->makeNode<SimNode_InitLocalRef>(mkv->at, mkv->stackTop, mkv->extraOffset, bytes);
} else {
init0 = context.code->makeNode<SimNode_InitLocal>(mkv->at, mkv->stackTop + mkv->extraOffset, bytes);
}
if (init0) simlist.push_back(init0);
}
// now fields
for ( const auto & decl : mkv->variants ) {
auto fieldVariant = mkBaseT->findArgumentIndex(decl->name);
DAS_ASSERT(fieldVariant!=-1 && "should have failed in type infer otherwise");
// lets set variant index
uint32_t voffset = mkv->extraOffset + index*stride;
auto vconst = new ExprConstInt(mkv->at, int32_t(fieldVariant));
vconst->type = new TypeDecl(Type::tInt);
setE(vconst, context.code->makeNode<SimNode_ConstValue>(vconst->at, vconst->value));
SimNode * svi;
if ( mkv->useCMRES ) {
svi = sv_makeLocalCMResCopy(mkv->at, voffset, vconst);
} else if (mkv->useStackRef) {
svi = sv_makeLocalRefCopy(mkv->at, mkv->stackTop, voffset, vconst);
} else {
svi = sv_makeLocalCopy(mkv->at, mkv->stackTop+voffset, vconst);
}
simlist.push_back(svi);
// field itself
auto fieldOffset = mkBaseT->getVariantFieldOffset(fieldVariant);
uint32_t offset = voffset + fieldOffset;
SimNode * cpy = nullptr;
if ( decl->value->rtti_isMakeLocal() ) {
// so what happens here, is we ask it for the generated commands and append it to this list only
auto mkl = static_cast<ExprMakeLocal*>(decl->value);
auto lsim = sv_simulateLocal(mkl);
simlist.insert(simlist.end(), lsim.begin(), lsim.end());
} else if ( mkv->useCMRES ) {
if ( decl->moveSemantics ){
cpy = sv_makeLocalCMResMove(mkv->at, offset, decl->value);
} else {
cpy = sv_makeLocalCMResCopy(mkv->at, offset, decl->value);
}
} else if ( mkv->useStackRef ) {
if ( decl->moveSemantics ){
cpy = sv_makeLocalRefMove(mkv->at, mkv->stackTop, offset, decl->value);
} else {
cpy = sv_makeLocalRefCopy(mkv->at, mkv->stackTop, offset, decl->value);
}
} else {
if ( decl->moveSemantics ){
cpy = sv_makeLocalMove(mkv->at, mkv->stackTop+offset, decl->value);
} else {
cpy = sv_makeLocalCopy(mkv->at, mkv->stackTop+offset, decl->value);
}
}
if ( cpy ) simlist.push_back(cpy);
index++;
}
return simlist;
}
ExpressionPtr SimulateVisitor::visit(ExprMakeVariant * expr) {
const auto &at = expr->at;
SimNode_Block * block;
if ( expr->useCMRES ) {
block = context.code->makeNode<SimNode_MakeLocalCMRes>(at);
} else {
block = context.code->makeNode<SimNode_MakeLocal>(at, expr->stackTop);
}
auto simlist = sv_simulateLocal(expr);
block->total = int(simlist.size());
block->list = (SimNode **) context.code->allocate(sizeof(SimNode *)*block->total);
for ( uint32_t i=0, is=block->total; i!=is; ++i )
block->list[i] = simlist[i];
setE(expr, block);
return expr;
}
vector<SimNode *> SimulateVisitor::simulateExprMakeStruct(const ExprMakeStruct *mks) {
gc_guard gc_scope;
vector<SimNode *> simlist;
// init with 0
int total = int(mks->structs.size());
auto mkBaseT = mks->makeType; // element view - makeType may be a fixed-array chain
while ( mkBaseT->baseType==Type::tFixedArray && mkBaseT->firstType ) mkBaseT = mkBaseT->firstType;
int stride = mks->makeType->getStride();
// note: if its an empty tuple init, like [[tuple<int;float>]] and its embedded - we need to zero it out
bool emptyEmbeddedTuple = ( mkBaseT->baseType==Type::tTuple && total==0);
bool partialyInitStruct = !mks->doesNotNeedInit && !mks->initAllFields;
if ( (emptyEmbeddedTuple || partialyInitStruct) && stride ) {
// zero provided elements means zero-init the WHOLE declared shape (default<T[N]>) -
// stride is one outer element, which under-counts fixed-array makeTypes
int bytes = total ? total * stride : int(mks->makeType->getSizeOf());
SimNode * init0;
if ( mks->useCMRES ) {
if ( bytes==0 ) {
init0 = nullptr;
} else if ( bytes <= 32 ) {
init0 = context.code->makeNodeUnrollNZ<SimNode_InitLocalCMResN>(bytes, mks->at, mks->extraOffset);
} else {
init0 = context.code->makeNode<SimNode_InitLocalCMRes>(mks->at, mks->extraOffset, bytes);
}
} else if ( mks->useStackRef ) {
init0 = context.code->makeNode<SimNode_InitLocalRef>(mks->at, mks->stackTop, mks->extraOffset, bytes);
} else {
init0 = context.code->makeNode<SimNode_InitLocal>(mks->at, mks->stackTop + mks->extraOffset, bytes);
}
if (init0) simlist.push_back(init0);
}
if ( mkBaseT->baseType == Type::tStructure ) {
for ( int index=0; index != total; ++index ) {
if ( mks->constructor ) {
uint32_t offset = mks->extraOffset + index*stride;
SimNode_CallBase * pCall = (SimNode_CallBase *) context.code->makeNodeUnrollAny<SimNode_CallAndCopyOrMove>(0, mks->at);
DAS_ASSERT(mks->constructor->index!=-1 && "should have failed in type infer otherwise");
pCall->fnPtr = context.getFunction(mks->constructor->index);
if ( mks->useCMRES ) {
pCall->cmresEval = context.code->makeNode<SimNode_GetCMResOfs>(mks->at, offset);
} else if ( mks->useStackRef ) {
pCall->cmresEval = context.code->makeNode<SimNode_GetLocalRefOff>(mks->at, mks->stackTop, offset);
} else {
pCall->cmresEval = context.code->makeNode<SimNode_GetLocal>(mks->at, mks->stackTop + offset);
}
simlist.push_back(pCall);
}
auto & fields = mks->structs[index];
for ( const auto & decl : *fields ) {
auto field = mkBaseT->structType->findField(decl->name);
DAS_ASSERT(field && "should have failed in type infer otherwise");
uint32_t offset = mks->extraOffset + index*stride + field->offset;
SimNode * cpy;
if ( decl->value->rtti_isMakeLocal() ) {
// so what happens here, is we ask it for the generated commands and append it to this list only
auto mkl = static_cast<ExprMakeLocal*>(decl->value);
auto lsim = sv_simulateLocal(mkl);
simlist.insert(simlist.end(), lsim.begin(), lsim.end());
continue;
} else if ( mks->useCMRES ) {
if ( decl->moveSemantics ){
cpy = sv_makeLocalCMResMove(mks->at, offset, decl->value);
} else {
cpy = sv_makeLocalCMResCopy(mks->at, offset, decl->value);
}
} else if ( mks->useStackRef ) {
if ( decl->moveSemantics ){
cpy = sv_makeLocalRefMove(mks->at, mks->stackTop, offset, decl->value);
} else {
cpy = sv_makeLocalRefCopy(mks->at, mks->stackTop, offset, decl->value);
}
} else {
if ( decl->moveSemantics ){
cpy = sv_makeLocalMove(mks->at, mks->stackTop+offset, decl->value);
} else {
cpy = sv_makeLocalCopy(mks->at, mks->stackTop+offset, decl->value);
}
}
if ( !cpy ) {
context.thisProgram->error("internal compilation error, can't generate structure initialization", "", "", mks->at, CompilationError::internal_structure);
}
simlist.push_back(cpy);
}
}
} else {
auto ann = mkBaseT->annotation;
// making fake variable, which points to out field
string fakeName = "__makelocal";
auto fakeVariable = new Variable();
fakeVariable->name = fakeName;
fakeVariable->type = new TypeDecl(Type::tHandle);
fakeVariable->type->annotation = ann;
fakeVariable->at = mks->at;
if ( mks->useCMRES ) {
fakeVariable->aliasCMRES = true;
} else if ( mks->useStackRef ) {
fakeVariable->stackTop = mks->stackTop;
fakeVariable->extraLocalOffset = mks->extraOffset;
fakeVariable->type->ref = true;
if ( total != 1 ) {
// wrap hoists ref onto the fixed-array head (canonical form)
fakeVariable->type = makeFixedArrayTypeDecl(total, fakeVariable->type);
}
}
fakeVariable->generated = true;
// make fake ExprVar which is that field
auto fakeVar = new ExprVar(mks->at, fakeName);
fakeVar->type = fakeVariable->type;
fakeVar->variable = fakeVariable;
fakeVar->local = true;
// make fake expression
ExpressionPtr fakeExpr = fakeVar;
ExprConstInt * indexExpr = nullptr;
if ( mks->useStackRef && total > 1 ) {
// if its stackRef with multiple indices, its actually var[total], and lookup is var[index]
indexExpr = new ExprConstInt(mks->at, 0);
indexExpr->type = new TypeDecl(Type::tInt);
fakeExpr = new ExprAt(mks->at, fakeExpr, indexExpr);
fakeExpr->type = new TypeDecl(Type::tHandle);
fakeExpr->type->annotation = ann;
fakeExpr->type->ref = true;
}
for ( int index=0; index != total; ++index ) {
auto & fields = mks->structs[index];
// adjust var for index
if ( mks->useCMRES ) {
fakeVariable->extraLocalOffset = mks->extraOffset + index*stride;
} else if ( mks->useStackRef ) {
if ( total > 1 ) {
indexExpr->value = cast<int32_t>::from(index);
}
} else {
fakeVariable->stackTop = mks->stackTop + mks->extraOffset + index*stride;
}
// now, setup fields
for ( const auto & decl : *fields ) {
auto fieldType = ann->makeFieldType(decl->name, false);
DAS_ASSERT(fieldType && "how did this infer?");
uint32_t fieldSize = fieldType->getSizeOf();
SimNode * cpy = nullptr;
uint32_t fieldOffset = ann->getFieldOffset(decl->name);
SimNode * simV = simulateExpression(fakeExpr);
auto left = context.code->makeNode<SimNode_FieldDeref>(mks->at, simV, fieldOffset);
auto right = getE(decl->value);
if ( !decl->value->type->isRef() ) {
cpy = context.code->makeValueNode<SimNode_Set>(decl->value->type->getR2VType(), decl->at, left, right);
} else if ( decl->moveSemantics ) {
cpy = context.code->makeNode<SimNode_MoveRefValue>(decl->at, left, right, fieldSize);
} else {
cpy = context.code->makeNode<SimNode_CopyRefValue>(decl->at, left, right, fieldSize);
}
simlist.push_back(cpy);
}
}
}
if ( mks->block ) {
/*
TODO: optimize
there is no point in making fake invoke expression, we can replace 'self' with fake variable we've made
however this needs to happen during infer, and this needs to have different visitor,
so that stack is allocated properly in subexpressions etc
*/
// making fake variable, which points to entire structure
string fakeName = "__makelocal";
auto fakeVariable = new Variable();
fakeVariable->name = fakeName;
fakeVariable->type = new TypeDecl(*mks->type);
if ( mks->useCMRES ) {
fakeVariable->aliasCMRES = true;
fakeVariable->extraLocalOffset = mks->extraOffset;
} else if ( mks->useStackRef ) {
fakeVariable->stackTop = mks->stackTop + mks->extraOffset;
fakeVariable->type->ref = true;
} else {
fakeVariable->stackTop = mks->stackTop + mks->extraOffset;
}
fakeVariable->generated = true;
// make fake ExprVar which is that field
auto fakeVar = new ExprVar(mks->at, fakeName);
fakeVar->type = fakeVariable->type;
fakeVar->variable = fakeVariable;
fakeVar->local = true;
// make fake invoke expression
auto fakeInvoke = new ExprInvoke(mks->at, "invoke");
fakeInvoke->arguments.push_back(mks->block);
fakeInvoke->arguments.push_back(fakeVar);
// simulate it
auto simI = simulateExpression(fakeInvoke);
simlist.push_back(simI);
}
return simlist;
}
ExpressionPtr SimulateVisitor::visit(ExprMakeStruct * expr) {
const auto &at = expr->at;
SimNode_Block * blk;
if ( expr->useCMRES ) {
blk = context.code->makeNode<SimNode_MakeLocalCMRes>(at);
} else {
blk = context.code->makeNode<SimNode_MakeLocal>(at, expr->stackTop);
}
auto simlist = sv_simulateLocal(expr);
blk->total = int(simlist.size());
blk->list = (SimNode **) context.code->allocate(sizeof(SimNode *)*blk->total);
for ( uint32_t i=0, is=blk->total; i!=is; ++i )
blk->list[i] = simlist[i];
setE(expr, blk);
return expr;
}
vector<SimNode *> SimulateVisitor::simulateExprMakeArray(const ExprMakeArray *mka) {
vector<SimNode *> simlist;
// init with 0
int total = int(mka->values.size());
uint32_t stride = mka->recordType->getSizeOf();
if ( !mka->doesNotNeedInit && !mka->initAllFields ) {
int bytes = total * stride;
SimNode * init0;
if ( mka->useCMRES ) {