forked from Matway/mpl-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtinImpl.mpl
executable file
·1811 lines (1607 loc) · 55.6 KB
/
builtinImpl.mpl
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
"builtinImpl" module
"control" includeModule
"codeNode" includeModule
"processSubNodes" includeModule
"defaultImpl" includeModule
declareBuiltin: [
virtual declareBuiltinName:;
virtual declareBuiltinBody:;
{processorResult: ProcessorResult Ref; processor: Processor Ref; indexOfNode: Int32; currentNode: CodeNode Ref; multiParserResult: MultiParserResult Cref;} () {} [
processorResult:;
processor:;
copy indexOfNode:;
currentNode:;
multiParserResult:;
failProc: @failProcForProcessor;
@declareBuiltinBody ucall
] declareBuiltinName exportFunction
];
staticnessOfBinResult: [
s1:; s2:;
s1 Dynamic > not s2 Dynamic > not or [
Dynamic
] [
s1 Weak = s2 Weak = and [
Weak
] [
Static
] if
] if
];
mplNumberBinaryOp: [
exValidator:;
getResultType:;
opFunc:;
getOpName:;
copy lastTag:;
copy firstTag:;
arg2: pop;
arg1: pop;
compilable [
var1: arg1 getVar;
var2: arg2 getVar;
var1.data.getTag firstTag < [var1.data.getTag lastTag < not] || ["first argument invalid" makeStringView compilerError] [
var2.data.getTag firstTag < [var2.data.getTag lastTag < not] || ["second argument invalid" makeStringView compilerError] [
arg1 arg2 variablesAreSame not [
("arguments have different schemas, left is " arg1 getMplType ", right is " arg2 getMplType) assembleString compilerError
] when
] if
] if
compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 staticnessOfVar Dynamic > arg2 staticnessOfVar Dynamic > and [
var1.data.getTag firstTag lastTag [
copy tag:;
value1: tag var1.data.get copy;
value2: tag var2.data.get copy;
resultType: tag @getResultType call;
value1 value2 @exValidator call
compilable [
value1 value2 @opFunc call resultType cutValue resultType createVariable
arg1 staticnessOfVar arg2 staticnessOfVar staticnessOfBinResult makeStaticness
createPlainIR push
] when
] staticCall
] [
arg1 makeVarRealCaptured
arg2 makeVarRealCaptured
opName: arg1 arg2 @getOpName call;
var1.data.getTag firstTag lastTag [
copy tag:;
value1: tag var1.data.get copy;
value2: tag var2.data.get copy;
resultType: tag @getResultType call;
result: resultType zeroValue resultType createVariable
Dynamic makeStaticness
createAllocIR;
opName createBinaryOperation
result push
] staticCall
] if
] when
] when
];
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fadd" makeStringView]["add" makeStringView] if] [+] [copy] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinAdd" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fsub" makeStringView]["sub" makeStringView] if] [-] [copy] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinSub" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fmul" makeStringView]["mul" makeStringView] if] [*] [copy] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinMul" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fdiv" makeStringView][a2 isNat ["udiv" makeStringView] ["sdiv" makeStringView] if] if] [/] [copy] [
y:; x:;
y y - y = ["division by zero" compilerError] when
] mplNumberBinaryOp
] "mplBuiltinDiv" @declareBuiltin ucall
[
VarNat8 VarIntX 1 + [a2:; a1:; a2 isNat ["urem" makeStringView] ["srem" makeStringView] if] [mod] [copy] [
y:; x:;
y y - y = ["division by zero" compilerError] when
] mplNumberBinaryOp
] "mplBuiltinMod" @declareBuiltin ucall
[
arg2: pop;
arg1: pop;
compilable [
comparable: [
arg:;
arg isPlain [arg getVar.data.getTag VarString =] ||
];
var1: arg1 getVar;
var2: arg2 getVar;
arg1 comparable not [ "first argument is not comparable" compilerError ] [
arg2 comparable not [ "second argument is not comparable" compilerError ] [
arg1 arg2 variablesAreSame not [
("arguments have different schemas, left is " arg1 getMplType ", right is " arg2 getMplType) assembleString compilerError
] when
] if
] if
compilable [
arg1 staticnessOfVar Dynamic > arg2 staticnessOfVar Dynamic > and [
var1.data.getTag VarString = [
value1: VarString var1.data.get copy;
value2: VarString var2.data.get copy;
value1 value2 = VarCond createVariable
arg1 staticnessOfVar arg2 staticnessOfVar staticnessOfBinResult makeStaticness
createPlainIR push
] [
var1.data.getTag VarCond VarReal64 1 + [
copy tag:;
value1: tag var1.data.get copy;
value2: tag var2.data.get copy;
value1 value2 = VarCond createVariable
arg1 staticnessOfVar arg2 staticnessOfVar staticnessOfBinResult makeStaticness
createPlainIR push
] staticCall
] if
] [
arg1 makeVarRealCaptured
arg2 makeVarRealCaptured
var1.data.getTag VarString = [
result: FALSE VarCond createVariable Dynamic makeStaticness createAllocIR;
"icmp eq" makeStringView createBinaryOperation
result push
] [
arg1 isReal [
var1.data.getTag VarReal32 VarReal64 1 + [
copy tag:;
result: FALSE VarCond createVariable Dynamic makeStaticness createAllocIR;
"fcmp oeq" makeStringView createBinaryOperation
result push
] staticCall
] [
var1.data.getTag VarCond VarIntX 1 + [
copy tag:;
result: FALSE VarCond createVariable Dynamic makeStaticness createAllocIR;
"icmp eq" makeStringView createBinaryOperation
result push
] staticCall
] if
] if
] if
] when
] when
] "mplBuiltinEqual" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [
a2:; a1:; a2 isReal ["fcmp olt" makeStringView][a2 isNat ["icmp ult" makeStringView] ["icmp slt" makeStringView] if] if
] [<] [t:; VarCond] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinLess" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [
a2:; a1:; a2 isReal ["fcmp ogt" makeStringView][a2 isNat ["icmp ugt" makeStringView] ["icmp sgt" makeStringView] if] if
] [>] [t:; VarCond] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinGreater" @declareBuiltin ucall
mplNumberUnaryOp: [
exValidator:;
opFunc:;
getMidOpName:;
getOpName:;
copy lastTag:;
copy firstTag:;
arg: pop;
compilable [
var: arg getVar;
var.data.getTag firstTag < [var.data.getTag lastTag < not] || ["argument invalid" compilerError] when
compilable [
arg staticnessOfVar Dynamic > [
var.data.getTag firstTag lastTag [
copy tag:;
value: tag var.data.get copy;
resultType: tag copy;
value @exValidator call
compilable [
value @opFunc call resultType cutValue resultType createVariable
arg staticnessOfVar makeStaticness
createPlainIR push
] when
] staticCall
] [
arg makeVarRealCaptured
opName: arg @getOpName call;
mopName: arg @getMidOpName call;
var.data.getTag firstTag lastTag [
copy tag:;
value: tag var.data.get copy;
resultType: tag copy;
result: resultType zeroValue resultType createVariable
Dynamic makeStaticness
createAllocIR;
opName mopName createUnaryOperation
result push
] staticCall
] if
] when
] when
];
[
VarCond VarNatX 1 + [a:; "xor" makeStringView] [
a:; a getVar.data.getTag VarCond = ["true, " makeStringView]["-1, " makeStringView] if
] [not] [x:;] mplNumberUnaryOp
] "mplBuiltinNot" @declareBuiltin ucall
[
VarCond VarNatX 1 + [a2:; a1:; "xor" makeStringView] [xor] [copy] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinXor" @declareBuiltin ucall
[
VarCond VarNatX 1 + [a2:; a1:; "and" makeStringView] [and] [copy] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinAnd" @declareBuiltin ucall
[
VarCond VarNatX 1 + [a2:; a1:; "or" makeStringView] [or] [copy] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinOr" @declareBuiltin ucall
[
TRUE VarCond createVariable createPlainIR push
] "mplBuiltinTrue" @declareBuiltin ucall
[
FALSE VarCond createVariable createPlainIR push
] "mplBuiltinFalse" @declareBuiltin ucall
[
LF toString makeVarString push
] "mplBuiltinLF" @declareBuiltin ucall
[
VarInt8 VarReal64 1 + [
a:; a isAnyInt ["sub" makeStringView]["fsub" makeStringView] if
] [
a:; a isAnyInt ["0, " makeStringView]["0x0000000000000000, " makeStringView] if
] [neg] [x:;] mplNumberUnaryOp
] "mplBuiltinNeg" @declareBuiltin ucall
mplNumberBuiltinOp: [
exValidator:;
opFunc:;
getOpName:;
arg: pop;
compilable [
var: arg getVar;
arg isReal not ["argument invalid" makeStringView compilerError] when
compilable [
arg staticnessOfVar Dynamic > [
var.data.getTag VarReal32 VarReal64 1 + [
copy tag:;
value: tag var.data.get copy;
resultType: tag copy;
value @exValidator call
compilable [
value @opFunc call resultType cutValue resultType createVariable
arg staticnessOfVar makeStaticness
createPlainIR push
] when
] staticCall
] [
arg makeVarRealCaptured
opName: arg @getOpName call;
var.data.getTag VarReal32 VarReal64 1 + [
copy tag:;
value: tag var.data.get copy;
resultType: tag copy;
result: resultType zeroValue resultType createVariable
Dynamic makeStaticness
createAllocIR;
args: IRArgument Array;
irarg: IRArgument;
arg createDerefToRegister @irarg.@irNameId set
var.irTypeId @irarg.@irTypeId set
FALSE @irarg.@byRef set
irarg @args.pushBack
result args String @opName createCallIR retName:;
retName result createStoreFromRegister
result push
] staticCall
] if
] when
] when
];
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.sin.f32" makeStringView]["@llvm.sin.f64" makeStringView] if
] [sin] [x:;] mplNumberBuiltinOp
] "mplBuiltinSin" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.cos.f32" makeStringView]["@llvm.cos.f64" makeStringView] if
] [cos] [x:;] mplNumberBuiltinOp
] "mplBuiltinCos" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.sqrt.f32" makeStringView]["@llvm.sqrt.f64" makeStringView] if
] [sqrt] [x:;] mplNumberBuiltinOp
] "mplBuiltinSqrt" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.ceil.f32" makeStringView]["@llvm.ceil.f64" makeStringView] if
] [ceil] [x:;] mplNumberBuiltinOp
] "mplBuiltinCeil" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.floor.f32" makeStringView]["@llvm.floor.f64" makeStringView] if
] [floor] [x:;] mplNumberBuiltinOp
] "mplBuiltinFloor" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.log.f32" makeStringView]["@llvm.log.f64" makeStringView] if
] [log] [x:;] mplNumberBuiltinOp
] "mplBuiltinLog" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.log10.f32" makeStringView]["@llvm.log10.f64" makeStringView] if
] [log10] [x:;] mplNumberBuiltinOp
] "mplBuiltinLog10" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
arg2: pop;
arg1: pop;
compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 isReal not ["first argument invalid" makeStringView compilerError] [
arg2 isReal not ["second argument invalid" makeStringView compilerError] [
arg1 arg2 variablesAreSame not [
("arguments have different schemas, left is " arg1 getMplType ", right is " arg2 getMplType) assembleString compilerError
] when
] if
] if
compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 staticnessOfVar Dynamic > arg2 staticnessOfVar Dynamic > and [
var1.data.getTag VarReal32 VarReal64 1 + [
copy tag:;
value1: tag var1.data.get copy;
value2: tag var2.data.get copy;
resultType: tag copy;
compilable [
value1 value2 ^ resultType cutValue resultType createVariable
arg1 staticnessOfVar arg2 staticnessOfVar staticnessOfBinResult makeStaticness
createPlainIR push
] when
] staticCall
] [
arg1 makeVarRealCaptured
arg2 makeVarRealCaptured
var1.data.getTag VarReal32 VarReal64 1 + [
copy tag:;
resultType: tag copy;
result: resultType zeroValue resultType createVariable
Dynamic makeStaticness
createAllocIR;
args: IRArgument Array;
irarg: IRArgument;
FALSE @irarg.@byRef set
arg1 createDerefToRegister @irarg.@irNameId set
var1.irTypeId @irarg.@irTypeId set
irarg @args.pushBack
arg2 createDerefToRegister @irarg.@irNameId set
var2.irTypeId @irarg.@irTypeId set
irarg @args.pushBack
result args String tag VarReal32 = ["@llvm.pow.f32" makeStringView] ["@llvm.pow.f64" makeStringView] if createCallIR retName:;
@retName result createStoreFromRegister
result push
] staticCall
] if
] when
] when
] "mplBuiltinPow" @declareBuiltin ucall
mplShiftBinaryOp: [
opFunc:;
getOpName:;
arg2: pop;
arg1: pop;
compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 isAnyInt not ["first argument invalid" makeStringView compilerError] [
arg2 isNat not ["second argument invalid" makeStringView compilerError] when
] if
compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 staticnessOfVar Dynamic > arg2 staticnessOfVar Dynamic > and [
var1.data.getTag VarNat8 VarIntX 1 + [
copy tag1:;
var2.data.getTag VarNat8 VarNatX 1 + [
copy tag2:;
value1: tag1 var1.data.get copy;
value2: tag2 var2.data.get copy;
resultType: tag1 copy;
value2 63n64 > ["shift value must be less than 64" makeStringView compilerError] when
compilable [
value1 value2 @opFunc call resultType cutValue resultType createVariable
arg1 staticnessOfVar arg2 staticnessOfVar staticnessOfBinResult makeStaticness
createPlainIR push
] when
] staticCall
] staticCall
] [
arg1 makeVarRealCaptured
arg2 makeVarRealCaptured
opName: arg1 arg2 @getOpName call;
var1.data.getTag VarNat8 VarIntX 1 + [
copy tag:;
resultType: tag copy;
result: resultType zeroValue resultType createVariable
Dynamic makeStaticness
createAllocIR;
arg1 getStorageSize arg2 getStorageSize = [
opName createBinaryOperation
] [
opName createBinaryOperationDiffTypes
] if
result push
] staticCall
] if
] when
] when
];
[
[t2:; t1:; "shl" makeStringView][lshift] mplShiftBinaryOp
] "mplBuiltinLShift" @declareBuiltin ucall
[
[t2:; t1:; t1 isNat ["lshr" makeStringView]["ashr" makeStringView] if][rshift] mplShiftBinaryOp
] "mplBuiltinRShift" @declareBuiltin ucall
[TRUE defaultRef] "mplBuiltinRef" @declareBuiltin ucall
[FALSE defaultRef] "mplBuiltinCref" @declareBuiltin ucall
[TRUE defaultMakeConstWith] "mplBuiltinConst" @declareBuiltin ucall
[
refToVar: pop;
compilable [
refToVar getVar.data.getTag VarRef = [
refToVar isSchema [
"can not deref virtual-reference" makeStringView compilerError
] [
refToVar getPointee push
] if
] [
"not a reference" makeStringView compilerError
] if
] when
] "mplBuiltinDeref" @declareBuiltin ucall
[
defaultCall
] "mplBuiltinCall" @declareBuiltin ucall
[
code: pop;
compilable [
varCode: code getVar;
varCode.data.getTag VarCode = not ["branch else must be a [CODE]" compilerError] when
compilable [
codeIndex: VarCode varCode.data.get.index copy;
astNode: codeIndex @[email protected];
[astNode.data.getTag AstNodeType.Code =] "Not a code!" assert
currentNode.countOfUCall 1 + @currentNode.@countOfUCall set
currentNode.countOfUCall 65535 > ["ucall limit exceeded" compilerError] when
indexArray: AstNodeType.Code astNode.data.get;
indexArray addIndexArrayToProcess
] when
] when
] "mplBuiltinUcall" @declareBuiltin ucall
[
refToVar: pop;
compilable [
refToVar makeVarTreeDynamic
refToVar push
] when
] "mplBuiltinDynamic" @declareBuiltin ucall
[
refToVar: pop;
compilable [
refToVar makeVarTreeDirty
refToVar push
] when
] "mplBuiltinDirty" @declareBuiltin ucall
[
refToVar: pop;
compilable [
refToVar staticnessOfVar Weak = [refToVar Static makeStaticness @refToVar set] when
refToVar push
] when
] "mplBuiltinStatic" @declareBuiltin ucall
[defaultSet] "mplBuiltinSet" @declareBuiltin ucall
mplBuiltinProcessAtList: [
refToStruct: pop;
refToIndex: pop;
compileOnce
result: RefToVar;
compilable [
structVar: refToStruct getVar;
indexVar: refToIndex getVar;
refToStruct isSchema [
(
[compilable]
[
pointee: VarRef structVar.data.get;
pointeeVar: pointee getVar;
pointeeVar.data.getTag VarStruct = not ["not a combined" compilerError] when
]
[indexVar.data.getTag VarInt32 = not ["index must be Int32" compilerError] when ]
[refToIndex staticnessOfVar Weak < [ "index must be static" compilerError] when ]
[
index: VarInt32 indexVar.data.get 0 cast;
struct: VarStruct pointeeVar.data.get.get;
index 0 < [index struct.fields.getSize < not] || ["index is out of bounds" compilerError] when
] [
field: index struct.fields.at.refToVar;
field VarRef TRUE dynamic TRUE dynamic TRUE dynamic createVariableWithVirtual @result set
refToStruct.mutable @result.@mutable set
result fullUntemporize
]
) sequence
] [
(
[compilable]
[structVar.data.getTag VarStruct = not [ "not a combined" compilerError] when]
[indexVar.data.getTag VarInt32 = not ["index must be Int32" compilerError] when]
[
struct: VarStruct structVar.data.get.get;
refToIndex staticnessOfVar Weak < [
struct.homogeneous [
struct.fields.dataSize 0 > [
# create dynamic getIndex
realRefToStruct: refToStruct;
realStructVar: structVar;
realStruct: struct;
refToStruct staticnessOfVar Virtual < not [
"can't get dynamic index in virtual struct" compilerError
] when
refToIndex makeVarRealCaptured
firstField: 0 realStruct.fields.at.refToVar;
fieldRef: firstField copyVarFromParent;
firstField.hostId indexOfNode = not [
fBegin: RefToVar;
fEnd: RefToVar;
fieldRef @fBegin @fEnd ShadowReasonField makeShadowsDynamic
fEnd @fieldRef set
] when
refToStruct.mutable @fieldRef.@mutable set
fieldRef fullUntemporize
fieldRef staticnessOfVar Virtual < not [
"dynamic index is combined of virtuals" compilerError
] [
fieldRef makeVarTreeDynamicStoraged
fieldRef unglobalize
fieldRef refToIndex realRefToStruct createDynamicGEP
fieldVar: fieldRef getVar;
currentNode.program.dataSize 1 - @fieldVar.@getInstructionIndex set
fieldRef @result set
] if
refToStruct.mutable [
refToStruct makeVarTreeDirty
] when
] [
"struct is empty" compilerError
] if
] [
"dynamic index in non-homogeneous combined" compilerError
] if
] [
index: VarInt32 indexVar.data.get 0 cast;
index refToStruct processStaticAt @result set
] if
]
) sequence
] if
] when
result
];
[
field: mplBuiltinProcessAtList;
compilable [
field derefAndPush
] when
] "mplBuiltinAt" @declareBuiltin ucall
[
field: mplBuiltinProcessAtList;
compilable [
field setRef
] when
] "mplBuiltinExclamation" @declareBuiltin ucall
[
else: pop;
then: pop;
condition: pop;
compilable [
varElse: else getVar;
varThen: then getVar;
varCond: condition getVar;
varElse.data.getTag VarCode = not ["branch else must be a [CODE]" compilerError] when
varThen.data.getTag VarCode = not ["branch then must be a [CODE]" compilerError] when
varCond.data.getTag VarCond = not [("condition has a wrong type " condition getMplType) assembleString compilerError] when
compilable [
condition staticnessOfVar Weak > [
value: VarCond varCond.data.get copy;
value [
VarCode varThen.data.get.index "staticIfThen" makeStringView processCall
] [
VarCode varElse.data.get.index "staticIfElse" makeStringView processCall
] if
] [
condition
VarCode varThen.data.get.index @[email protected]
VarCode varElse.data.get.index @[email protected]
processIf
] if
] when
] when
] "mplBuiltinIf" @declareBuiltin ucall
[
else: pop;
then: pop;
condition: pop;
compilable [
varElse: else getVar;
varThen: then getVar;
varCond: condition getVar;
varElse.data.getTag VarCode = not ["branch else must be a [CODE]" makeStringView compilerError] when
varThen.data.getTag VarCode = not ["branch then must be a [CODE]" makeStringView compilerError] when
varCond.data.getTag VarCond = not ["condition has a wrong type" makeStringView compilerError] when
compilable [
condition staticnessOfVar Weak > [
value: VarCond varCond.data.get copy;
codeIndex: value [VarCode varThen.data.get.index copy] [VarCode varElse.data.get.index copy] if;
astNode: codeIndex @[email protected];
[astNode.data.getTag AstNodeType.Code =] "Not a code!" assert
currentNode.countOfUCall 1 + @currentNode.@countOfUCall set
currentNode.countOfUCall 65535 > ["ucall limit exceeded" compilerError] when
indexArray: AstNodeType.Code astNode.data.get;
indexArray addIndexArrayToProcess
] [
"condition must be static" makeStringView compilerError
] if
] when
] when
] "mplBuiltinUif" @declareBuiltin ucall
[
body: pop;
(
[compilable]
[
varBody: body getVar;
varBody.data.getTag VarCode = not ["body must be [CODE]" compilerError] when
] [
astNode: VarCode varBody.data.get.index @[email protected];
astNode processLoop
]
) sequence
] "mplBuiltinLoop" @declareBuiltin ucall
parseFieldToSignatureCaptureArray: [
refToStruct:;
result: RefToVar Array;
varStruct: refToStruct getVar;
varStruct.data.getTag VarStruct = not ["argument list must be a struct" compilerError] when
compilable [
VarStruct varStruct.data.get.get.fields [
refToVar: .value.refToVar;
refToVar isVirtual ["input cannot be virtual" compilerError] when
refToVar @result.pushBack
] each
] when
result
];
parseSignature: [
result: CFunctionSignature;
(
[compilable]
[options: pop;]
[
optionsVar: options getVar;
optionsVar.data.getTag VarStruct = not ["options must be a struct" compilerError] when
] [
optionsStruct: VarStruct optionsVar.data.get.get;
hasConvention: FALSE dynamic;
optionsStruct.fields [
f: .value;
f.nameInfo (
processor.variadicNameInfo [
variadicRefToVar: f.refToVar;
variadicVar: variadicRefToVar getVar;
(
[compilable]
[variadicVar.data.getTag VarCond = not ["value must be Cond" compilerError] when]
[variadicRefToVar staticnessOfVar Weak < ["value must be Static" compilerError] when]
[VarCond variadicVar.data.get @result.@variadic set]
) sequence
]
processor.conventionNameInfo [
conventionRefToVarRef: f.refToVar;
conventionVarRef: conventionRefToVarRef getVar;
(
[compilable]
[conventionVarRef.data.getTag VarRef = not ["value must be String Ref" compilerError] when]
[conventionRefToVarRef staticnessOfVar Weak < ["value must be Static" compilerError] when]
[
conventionRefToVar: VarRef conventionVarRef.data.get;
conventionVar: conventionRefToVar getVar;
conventionVar.data.getTag VarString = not ["value must be String Ref" compilerError] when
]
[conventionRefToVar staticnessOfVar Weak < ["value must be Static" compilerError] when]
[
string: VarString conventionVar.data.get;
string @result.@convention set
TRUE @hasConvention set
]
) sequence
] [
("unknown option: " f.nameInfo processor.nameInfos.at.name) assembleString compilerError
]
) case
] each
]
[
hasConvention not [
String @result.@convention set
] when
return: pop;
compilable [
return isVirtual [
returnVar: return getVar;
returnVar.data.getTag VarStruct = not [(return getMplType " can not be a return type") assembleString compilerError] when
] [
#todo: detect temporality
returnVar: return getVar;
returnVar.temporary [
return @[email protected]
] [
return TRUE dynamic createRef @[email protected]
] if
] if
] when
]
[arguments: pop;]
[
arguments parseFieldToSignatureCaptureArray @result.@inputs set
]
) sequence
result
];
[
(
[compilable]
[currentNode.parent 0 = not ["export must be global" compilerError] when]
[refToName: pop;]
[refToName staticnessOfVar Weak < ["function name must be static string" compilerError] when]
[
varName: refToName getVar;
varName.data.getTag VarString = not ["function name must be static string" compilerError] when
]
[refToBody: pop;]
[
varBody: refToBody getVar;
varBody.data.getTag VarCode = not ["must be a code" compilerError] when
]
[signature: parseSignature;]
[
astNode: VarCode varBody.data.get.index @[email protected];
index: signature astNode VarString varName.data.get makeStringView FALSE dynamic processExportFunction;
]
) sequence
] "mplBuiltinExportFunction" @declareBuiltin ucall
[
(
[compilable]
[currentNode.parent 0 = not ["export must be global" compilerError] when]
[refToName: pop;]
[refToVar: pop;]
[refToName staticnessOfVar Weak < ["variable name must be static string" compilerError] when]
[
varName: refToName getVar;
varName.data.getTag VarString = not ["variable name must be static string" compilerError] when
] [
refToVar isVirtual ["cannot export virtual var" compilerError] when
] [
refToVar getVar.temporary not [
refToVar TRUE dynamic createRef @refToVar set
] when
var: refToVar getVar;
FALSE @var.@temporary set
] [
name: VarString varName.data.get;
oldIrNameId: var.irNameId copy;
oldInstructionIndex: var.globalDeclarationInstructionIndex copy;
("@" name) assembleString makeStringId @var.@irNameId set
instruction: var.globalDeclarationInstructionIndex @[email protected];
refToVar createVarExportIR drop
@[email protected] move @instruction set
TRUE @refToVar.@mutable set
oldIrNameId var.irNameId var.irTypeId createGlobalAliasIR
oldInstructionIndex @var.@globalDeclarationInstructionIndex set
nameInfo: name findNameInfo;
nameInfo refToVar addOverloadForPre
nameInfo refToVar NameCaseLocal addNameInfo
processor.options.debug [
d: nameInfo refToVar addGlobalVariableDebugInfo;
globalInstruction: var.globalDeclarationInstructionIndex @[email protected];
", !dbg !" @globalInstruction.cat
d @globalInstruction.cat
] when
]
) sequence
] "mplBuiltinExportVariable" @declareBuiltin ucall
[
(
[compilable]
[currentNode.parent 0 = not ["import must be global" compilerError] when]
[refToName: pop;]
[refToName staticnessOfVar Weak < ["function name must be static string" compilerError] when]
[
varName: refToName getVar;
varName.data.getTag VarString = not ["function name must be static string" compilerError] when
]
[signature: parseSignature;]
[index: signature VarString varName.data.get makeStringView FALSE dynamic processImportFunction;]
) sequence
] "mplBuiltinImportFunction" @declareBuiltin ucall
[
(
[compilable]
[signature: parseSignature;]
[
name: ("null." processor.nodes.getSize) assembleString;
index: signature name makeStringView TRUE dynamic processImportFunction;
]
[
nullNode: index processor.nodes.at.get;
gnr: nullNode.varNameInfo getName;
cnr: gnr captureName;
refToVar: cnr.refToVar copy;
refToVar push
]
) sequence
] "mplBuiltinCodeRef" @declareBuiltin ucall
[
currentNode.parent 0 = not ["import must be global" compilerError] when
compilable [
refToName: pop;
refToType: pop;
compilable [
refToName staticnessOfVar Weak < ["variable name must be static string" compilerError] when
compilable [
varName: refToName getVar;
varName.data.getTag VarString = not ["variable name must be static string" compilerError] when
compilable [
varType: refToType getVar;
refToType isVirtual [
"variable cant be virtual" compilerError
] [
varType.temporary not [
refToType TRUE dynamic createRef @refToType set
] when
name: VarString varName.data.get;
newRefToVar: refToType copyVar;
newVar: newRefToVar getVar;
TRUE @newRefToVar.@mutable set
FALSE @newVar.@temporary set
("@" name) assembleString makeStringId @newVar.@irNameId set
newRefToVar createVarImportIR makeVarTreeDynamic
nameInfo: name findNameInfo;
nameInfo newRefToVar addOverloadForPre
nameInfo newRefToVar NameCaseLocal addNameInfo
processor.options.debug [newRefToVar isVirtual not] && [
d: nameInfo newRefToVar addGlobalVariableDebugInfo;
globalInstruction: newRefToVar getVar.globalDeclarationInstructionIndex @[email protected];