-
Notifications
You must be signed in to change notification settings - Fork 4
/
builtinImpl.mpl
2155 lines (1903 loc) · 76.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
# Copyright (C) 2021 Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"Array" use
"Owner" use
"String" use
"algorithm" use
"control" use
"file" use
"Block" use
"Var" use
"codeNode" use
"debugWriter" use
"declarations" use
"defaultImpl" use
"irWriter" use
"memory" use
"pathUtils" use
"processSubNodes" use
"processor" use
"staticCall" use
"variable" use
declareBuiltin: [
virtual declareBuiltinName:;
virtual declareBuiltinBody:;
{processor: Processor Ref; block: Block Ref;} () {} [
processor:;
block:;
overload failProc: processor block FailProcForProcessor;
@declareBuiltinBody ucall
] declareBuiltinName exportFunction
];
mplBuiltinProcessAtList: [
compileOnce
result: RefToVar;
processor compilable [
structVar: refToStruct getVar;
indexVar: refToIndex getVar;
(
[processor compilable]
[structVar.data.getTag VarStruct = ~ [ "not a combined" @processor block compilerError] when]
[indexVar.data.getTag VarInt32 = ~ ["index must be Int32" @processor block compilerError] when]
[
struct: VarStruct structVar.data.get.get;
refToIndex staticityOfVar Weak < [
struct.homogeneous [
struct.fields.size 0 > [
# create dynamic getIndex
realRefToStruct: refToStruct;
realStructVar: structVar;
realStruct: struct;
refToStruct staticityOfVar Virtual < ~ [
"can't get dynamic index in virtual struct" @processor block compilerError
] when
@refToIndex @processor @block makeVarRealCaptured
@refToStruct makeVarPtrCaptured
firstField: 0 realStruct.fields.at.refToVar;
fieldRef: firstField @processor @block copyOneVarFromType Dynamic @processor @block makeStorageStaticity;
refToStruct.mutable @fieldRef.setMutable
@fieldRef fullUntemporize
fieldRef staticityOfVar Virtual < ~ [
"dynamic index in combined of virtuals" @processor block compilerError
] [
fieldRef @processor @block makeVarTreeDynamicStoraged
@fieldRef @processor block unglobalize
fieldRef refToIndex realRefToStruct @processor @block createDynamicGEP
fieldVar: @fieldRef getVar;
block.program.size 1 - @fieldVar.@getInstructionIndex set
fieldRef @result set
] if
refToStruct.mutable [
refToStruct @processor @block makeVarTreeDirty
] when
] [
"struct is empty" @processor block compilerError
] if
] [
"dynamic index in non-homogeneous combined" @processor block compilerError
] if
] [
index: VarInt32 indexVar.data.get.end 0 cast;
index @refToStruct @processor @block processStaticAt @result set
] if
]
) sequence
] when
result
];
mplBuiltinProcessAtListKeyObject: [
refToStruct: @processor @block pop;
refToIndex: @processor @block pop;
mplBuiltinProcessAtList
];
mplBuiltinProcessAtListObjectKey: [
refToIndex: @processor @block pop;
refToStruct: @processor @block pop;
mplBuiltinProcessAtList
];
mplNumberBinaryOp: [
exValidator:;
getResultType:;
opFunc:;
getOpName:;
lastTag: new;
firstTag: new;
arg2: @processor @block pop;
arg1: @processor @block pop;
processor compilable [
var1: arg1 getVar;
var2: arg2 getVar;
var1.data.getTag firstTag < [var1.data.getTag lastTag < ~] || ["first argument invalid" @processor block compilerError] [
var2.data.getTag firstTag < [var2.data.getTag lastTag < ~] || ["second argument invalid" @processor block compilerError] [
arg1 arg2 variablesAreSame ~ [
("arguments have different schemas, left is " arg1 @processor block getMplType ", right is " arg2 @processor block getMplType) assembleString @processor block compilerError
] when
] if
] if
processor compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 staticityOfVar Dynamic > arg2 staticityOfVar Dynamic > and [
var1.data.getTag firstTag lastTag [
tag: new;
value1: tag var1.data.get.end new;
value2: tag var2.data.get.end new;
resultType: tag @getResultType call;
value1 value2 @exValidator call
processor compilable [
value1 value2 @opFunc call resultType @processor cutValue makeValuePair resultType @processor @block createVariable
arg1 staticityOfVar arg2 staticityOfVar staticityOfBinResult @processor block makeStaticity
@processor @block createPlainIR @block push
] when
] staticCall
] [
opName: arg1 arg2 @getOpName call;
var1.data.getTag firstTag lastTag [
tag: new;
value1: tag var1.data.get.end new;
value2: tag var2.data.get.end new;
resultType: tag @getResultType call;
result: resultType zeroValue makeValuePair resultType @processor @block createVariable
Dynamic @processor block makeStaticity
@processor @block createAllocIR;
arg1 arg2 @result opName @processor @block createBinaryOperation
result @block push
] staticCall
] if
] when
] when
];
mplNumberBuiltinOp: [
exValidator:;
opFunc:;
getOpName:;
arg: @processor @block pop;
processor compilable [
var: arg getVar;
arg isReal ~ ["argument invalid" @processor block compilerError] when
processor compilable [
arg staticityOfVar Dynamic > [
var.data.getTag VarReal32 VarReal64 1 + [
tag: new;
value: tag var.data.get.end new;
resultType: tag new;
value @exValidator call
processor compilable [
value @opFunc call resultType @processor cutValue makeValuePair resultType @processor @block createVariable
arg staticityOfVar @processor block makeStaticity
@processor @block createPlainIR @block push
] when
] staticCall
] [
@arg @processor @block makeVarRealCaptured
opName: arg @getOpName call;
var.data.getTag VarReal32 VarReal64 1 + [
tag: new;
value: tag var.data.get.end new;
resultType: tag new;
result: resultType zeroValue makeValuePair resultType @processor @block createVariable
Dynamic @processor block makeStaticity
@processor @block createAllocIR;
args: IRArgument Array;
irarg: IRArgument;
arg @processor @block createDerefToRegister @irarg.@irNameId set
arg @processor getMplSchema.irTypeId @irarg.@irTypeId set
FALSE @irarg.@byRef set
irarg @args.append
result args String @opName FALSE dynamic @processor @block createCallIR retName:;
retName result @processor @block createStoreFromRegister
result @block push
] staticCall
] if
] when
] when
];
mplNumberUnaryOp: [
exValidator:;
opFunc:;
getMidOpName:;
getOpName:;
lastTag: new;
firstTag: new;
arg: @processor @block pop;
processor compilable [
var: arg getVar;
var.data.getTag firstTag < [var.data.getTag lastTag < ~] || ["argument invalid" @processor block compilerError] when
processor compilable [
arg staticityOfVar Dynamic > [
var.data.getTag firstTag lastTag [
tag: new;
value: tag var.data.get.end new;
resultType: tag new;
value @exValidator call
processor compilable [
value @opFunc call resultType @processor cutValue makeValuePair resultType @processor @block createVariable
arg staticityOfVar @processor block makeStaticity
@processor @block createPlainIR @block push
] when
] staticCall
] [
@arg @processor @block makeVarRealCaptured
opName: arg @getOpName call;
mopName: arg @getMidOpName call;
var.data.getTag firstTag lastTag [
tag: new;
value: tag var.data.get.end new;
resultType: tag new;
result: resultType zeroValue makeValuePair resultType @processor @block createVariable
Dynamic @processor block makeStaticity
@processor @block createAllocIR;
arg @result opName mopName @processor @block createUnaryOperation
result @block push
] staticCall
] if
] when
] when
];
mplShiftBinaryOp: [
opFunc:;
getOpName:;
arg2: @processor @block pop;
arg1: @processor @block pop;
processor compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 isAnyInt ~ ["first argument invalid" @processor block compilerError] [
arg2 isNat ~ ["second argument invalid" @processor block compilerError] when
] if
processor compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 staticityOfVar Dynamic > arg2 staticityOfVar Dynamic > and [
var1.data.getTag VarNat8 VarIntX 1 + [
tag1: new;
var2.data.getTag VarNat8 VarNatX 1 + [
tag2: new;
value1: tag1 var1.data.get.end new;
value2: tag2 var2.data.get.end new;
resultType: tag1 new;
value2 63n64 > ["shift value must be less than 64" @processor block compilerError] when
processor compilable [
value1 value2 @opFunc call resultType @processor cutValue makeValuePair resultType @processor @block createVariable
arg1 staticityOfVar arg2 staticityOfVar staticityOfBinResult @processor @block makeStaticity
@processor @block createPlainIR @block push
] when
] staticCall
] staticCall
] [
opName: arg1 arg2 @getOpName call;
var1.data.getTag VarNat8 VarIntX 1 + [
tag: new;
resultType: tag new;
result: resultType zeroValue makeValuePair resultType @processor @block createVariable
Dynamic @processor @block makeStaticity
@processor @block createAllocIR;
arg1 @processor getStorageSize arg2 @processor getStorageSize = [
arg1 arg2 @result opName @processor @block createBinaryOperation
] [
arg1 arg2 @result opName @processor @block createBinaryOperationDiffTypes
] if
result @block push
] staticCall
] if
] when
] when
];
parseFieldToSignatureCaptureArray: [
refToStruct:;
result: RefToVar Array;
varStruct: refToStruct getVar;
varStruct.data.getTag VarStruct = ~ ["argument list must be a struct" @processor block compilerError] when
processor compilable [
VarStruct varStruct.data.get.get.fields [
refToVar: .refToVar;
refToVar isVirtual ["input cannot be virtual" @processor block compilerError] when
refToVar @result.append
] each
] when
result
];
parseSignature: [
result: CFunctionSignature;
(
[processor compilable]
[options: @processor @block pop;]
[
optionsVar: options getVar;
optionsVar.data.getTag VarStruct = ~ ["options must be a struct" @processor block compilerError] when
] [
optionsStruct: VarStruct optionsVar.data.get.get;
hasConvention: FALSE dynamic;
optionsStruct.fields [
f:;
f.nameInfo (
processor.specialNames.variadicNameInfo [
variadicRefToVar: f.refToVar;
variadicVar: variadicRefToVar getVar;
(
[processor compilable]
[variadicVar.data.getTag VarCond = ~ ["value must be Cond" @processor block compilerError] when]
[variadicRefToVar staticityOfVar Weak < ["value must be Static" @processor block compilerError] when]
[VarCond variadicVar.data.get.end @result.@variadic set]
) sequence
]
processor.specialNames.conventionNameInfo [
conventionRefToVarRef: f.refToVar;
conventionVarRef: conventionRefToVarRef getVar;
(
[processor compilable]
[conventionVarRef.data.getTag VarRef = ~ ["value must be String Ref" @processor block compilerError] when]
[conventionRefToVarRef staticityOfVar Weak < ["value must be Static" @processor block compilerError] when]
[
conventionRefToVar: VarRef conventionVarRef.data.get.refToVar;
conventionVar: conventionRefToVar getVar;
conventionVar.data.getTag VarString = ~ ["value must be String Ref" @processor block compilerError] when
]
[conventionRefToVar staticityOfVar Weak < ["value must be Static" @processor block compilerError] when]
[
string: VarString conventionVar.data.get;
string @result.@convention set
TRUE @hasConvention set
]
) sequence
] [
("unknown option: " f.nameInfo processor.nameManager.getText) assembleString @processor block compilerError
]
) case
] each
]
[
hasConvention ~ [
String @result.@convention set
] when
return: @processor @block pop;
processor compilable [
return isVirtual [
returnVar: return getVar;
returnVar.data.getTag VarStruct = ~ [(return @processor block getMplType " can not be a return type") assembleString @processor block compilerError] when
] [
#todo: detect temporality
returnVar: return getVar;
returnVar.temporary [
return @[email protected]
] [
@return TRUE dynamic @processor @block createRef @[email protected]
] if
] if
] when
]
[arguments: @processor @block pop;]
[
arguments parseFieldToSignatureCaptureArray @result.@inputs set
]
) sequence
result
];
staticityOfBinResult: [
s1:; s2:;
s1 Dynamic > ~ s2 Dynamic > ~ or [
Dynamic
] [
s1 Weak = s2 Weak = and [
Weak
] [
Static
] if
] if
];
[
field: mplBuiltinProcessAtListKeyObject;
processor compilable [
field @processor @block setRef
] when
] "mplBuiltinExclamation" @declareBuiltin ucall
[
(
[processor compilable]
[refToStr2: @processor @block pop;]
[refToStr2 staticityOfVar Weak < ["must be static string" @processor block compilerError] when]
[
varStr2: refToStr2 getVar;
varStr2.data.getTag VarString = ~ ["must be static string" @processor block compilerError] when
]
[refToStr1: @processor @block pop;]
[refToStr1 staticityOfVar Weak < ["must be static string" @processor block compilerError] when]
[
varStr1: refToStr1 getVar;
varStr1.data.getTag VarString = ~ ["must be static string" @processor block compilerError] when
]
[(VarString varStr1.data.get VarString varStr2.data.get) assembleString @processor @block makeVarString @block push]
) sequence
] "mplBuiltinStrCat" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fmul" makeStringView]["mul" makeStringView] if] [*] [new] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinMul" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fadd" makeStringView]["add" makeStringView] if] [+] [new] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinAdd" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fsub" makeStringView]["sub" makeStringView] if] [-] [new] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinSub" @declareBuiltin ucall
[
VarNat8 VarReal64 1 + [a2:; a1:; a2 isReal ["fdiv" makeStringView][a2 isNat ["udiv" makeStringView] ["sdiv" makeStringView] if] if] [/] [new] [
y:; x:;
y y - y = ["division by zero" @processor block compilerError] when
] mplNumberBinaryOp
] "mplBuiltinDiv" @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
[
arg2: @processor @block pop;
arg1: @processor @block pop;
processor compilable [
comparable: [
arg:;
arg isPlain [arg getVar.data.getTag VarString =] ||
];
var1: arg1 getVar;
var2: arg2 getVar;
arg1 comparable ~ [ "first argument is not comparable" @processor block compilerError ] [
arg2 comparable ~ [ "second argument is not comparable" @processor block compilerError ] [
arg1 arg2 variablesAreSame ~ [
("arguments have different schemas, left is " arg1 @processor block getMplType ", right is " arg2 @processor block getMplType) assembleString @processor block compilerError
] when
] if
] if
processor compilable [
arg1 staticityOfVar Dynamic > arg2 staticityOfVar Dynamic > and [
var1.data.getTag VarString = [
value1: VarString var1.data.get;
value2: VarString var2.data.get;
value1 value2 = makeValuePair VarCond @processor @block createVariable
arg1 staticityOfVar arg2 staticityOfVar staticityOfBinResult @processor block makeStaticity
@processor @block createPlainIR @block push
] [
var1.data.getTag VarCond VarReal64 1 + [
tag: new;
value1: tag var1.data.get.end new;
value2: tag var2.data.get.end new;
value1 value2 = makeValuePair VarCond @processor @block createVariable
arg1 staticityOfVar arg2 staticityOfVar staticityOfBinResult @processor block makeStaticity
@processor @block createPlainIR @block push
] staticCall
] if
] [
var1.data.getTag VarString = [
result: FALSE makeValuePair VarCond @processor @block createVariable Dynamic @processor @block makeStaticity @processor @block createAllocIR;
arg1 arg2 @result "icmp eq" makeStringView @processor @block createStringCompare
result @block push
] [
arg1 isReal [
var1.data.getTag VarReal32 VarReal64 1 + [
tag: new;
result: FALSE makeValuePair VarCond @processor @block createVariable Dynamic @processor @block makeStaticity @processor @block createAllocIR;
arg1 arg2 @result "fcmp oeq" @processor @block createBinaryOperation
result @block push
] staticCall
] [
var1.data.getTag VarCond VarIntX 1 + [
tag: new;
result: FALSE makeValuePair VarCond @processor @block createVariable Dynamic @processor @block makeStaticity @processor @block createAllocIR;
arg1 arg2 @result "icmp eq" @processor @block createBinaryOperation
result @block push
] staticCall
] if
] if
] if
] when
] when
] "mplBuiltinEqual" @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
[
field: mplBuiltinProcessAtListKeyObject;
processor compilable [
field isVirtual [@field @processor @block makeVirtualVarReal @field set] when
@field @processor @block derefAndPush
] when
] "mplBuiltinAt" @declareBuiltin ucall
[
COMPILER_SOURCE_VERSION 0i64 cast makeValuePair VarInt32 @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] "mplBuiltinCompilerVersion" @declareBuiltin ucall
[
processor.options.debug makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] "mplBuiltinDebug" @declareBuiltin ucall
[
FALSE makeValuePair VarCond @processor @block createVariable @processor @block createPlainIR @block push
] "mplBuiltinFalse" @declareBuiltin ucall
[
LF toString @processor @block makeVarString @block push
] "mplBuiltinLF" @declareBuiltin ucall
[
TRUE makeValuePair VarCond @processor @block createVariable @processor @block createPlainIR @block push
] "mplBuiltinTrue" @declareBuiltin ucall
[
TRUE dynamic @processor.@usedFloatBuiltins set
arg2: @processor @block pop;
arg1: @processor @block pop;
processor compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 isReal ~ ["first argument invalid" @processor block compilerError] [
arg2 isReal ~ ["second argument invalid" @processor block compilerError] [
arg1 arg2 variablesAreSame ~ [
("arguments have different schemas, left is " arg1 @processor block getMplType ", right is " arg2 @processor block getMplType) assembleString @processor block compilerError
] when
] if
] if
processor compilable [
var1: arg1 getVar;
var2: arg2 getVar;
arg1 staticityOfVar Dynamic > arg2 staticityOfVar Dynamic > and [
var1.data.getTag VarReal32 VarReal64 1 + [
tag: new;
value1: tag var1.data.get.end new;
value2: tag var2.data.get.end new;
resultType: tag new;
processor compilable [
value1 value2 ^ resultType @processor cutValue makeValuePair resultType @processor @block createVariable
arg1 staticityOfVar arg2 staticityOfVar staticityOfBinResult @processor @block makeStaticity
@processor @block createPlainIR @block push
] when
] staticCall
] [
@arg1 @processor @block makeVarRealCaptured
@arg2 @processor @block makeVarRealCaptured
var1.data.getTag VarReal32 VarReal64 1 + [
tag: new;
resultType: tag new;
result: resultType zeroValue makeValuePair resultType @processor @block createVariable
Dynamic @processor @block makeStaticity
@processor @block createAllocIR;
args: IRArgument Array;
irarg: IRArgument;
FALSE @irarg.@byRef set
arg1 @processor @block createDerefToRegister @irarg.@irNameId set
arg1 @processor getMplSchema.irTypeId @irarg.@irTypeId set
irarg @args.append
arg2 @processor @block createDerefToRegister @irarg.@irNameId set
arg2 @processor getMplSchema.irTypeId @irarg.@irTypeId set
irarg @args.append
result args String tag VarReal32 = ["@llvm.pow.f32" makeStringView] ["@llvm.pow.f64" makeStringView] if FALSE dynamic @processor @block createCallIR retName:;
@retName result @processor @block createStoreFromRegister
result @block push
] staticCall
] if
] when
] when
] "mplBuiltinPow" @declareBuiltin ucall
[
refToSchema: @processor @block pop;
refToVar: @processor @block pop;
processor compilable [
@refToVar @processor @block makeVarRealCaptured
var: refToVar getVar;
varSchema: refToSchema getVar;
var.data.getTag VarNatX = [
var: refToVar getVar;
varSchema: refToSchema getVar;
schemaOfResult: RefToVar;
varSchema.data.getTag VarRef = [
"Unable in current semantic!" failProc
] [
refToSchema @schemaOfResult set
] if
schemaOfResult isVirtual [
"pointee is virtual, cannot cast" @processor block compilerError
] [
refToVar staticityOfVar Dynamic > [VarNatX var.data.get.end 0n64 =] && [
result: schemaOfResult @processor @block getNilVar @processor @block getLastShadow;
schemaOfResult.mutable @result.setMutable
result @block push
] [
refBranch: schemaOfResult makeRefBranch;
FALSE @[email protected]
refToDst: refBranch FALSE @processor @block createRefVariable;
Dynamic makeValuePair @refToDst getVar.@staticity set
refToVar @refToDst "inttoptr" @processor @block createCastCopyToNew
@refToDst @processor @block derefAndPush
] if
] if
] [
"address must be a NatX" @processor block compilerError
] if
] when
] "mplBuiltinAddressToReference" @declareBuiltin ucall
[
refToVar: @processor @block pop;
processor compilable [
refToVar isVirtual [
0nx
] [
refToVar @processor block checkUnsizedData
refToVar @processor getAlignment
] if
0n64 cast makeValuePair VarNatX @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] "mplBuiltinAlignment" @declareBuiltin ucall
[
VarCond VarNatX 1 + [a2:; a1:; "and" makeStringView] [and] [new] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinAnd" @declareBuiltin ucall
[
refToCount: @processor @block pop;
refToElement: @processor @block pop;
processor compilable [
@refToElement fullUntemporize
varCount: refToCount getVar;
varCount.data.getTag VarInt32 = ~ ["count must be Int32" @processor block compilerError] when
processor compilable [
refToCount staticityOfVar Dynamic > ~ ["count must be static" @processor block compilerError] when
processor compilable [
count: VarInt32 varCount.data.get.end 0 cast;
count 0 < [
"count must not be negative" @processor block compilerError
] when
processor compilable [
struct: Struct;
staticity: refToElement staticityOfVar;
staticity Weak = [Dynamic @staticity set] when
i: 0 dynamic;
[
i count < [
element: refToElement @processor @block copyVar staticity @processor @block makeStaticity;
field: Field;
processor.specialNames.emptyNameInfo @field.@nameInfo set
element @field.@refToVar set
field @[email protected]
i 1 + @i set TRUE
] &&
] loop
result: @struct owner VarStruct @processor @block createVariable;
result isVirtual ~ [@result @processor @block createAllocIR @result set] when
resultStruct: VarStruct @result [email protected];
i: 0 dynamic;
[
i resultStruct.fields.size < [
field: i @[email protected];
field.refToVar isVirtual [
field.refToVar isAutoStruct ["unable to copy virtual autostruct" @processor block compilerError] when
] [
@field.@refToVar @processor block unglobalize
block.program.size @field.@refToVar getVar.@allocationInstructionIndex set
"no alloc..." @block createComment # fake instruction
@refToElement @field.@refToVar @processor @block createCheckedCopyToNew
@field.@refToVar markAsUnableToDie
@field.@refToVar i result @processor @block createGEPInsteadOfAlloc
] if
i 1 + @i set processor compilable
] &&
] loop
result @[email protected]
result @block push
] when
] when
] when
] when
] "mplBuiltinArray" @declareBuiltin ucall
[
(
[processor compilable]
[refToText: @processor @block pop;]
[
refToText staticityOfVar Weak < ["name must be static string" @processor block compilerError] when
] [
varText: refToText getVar;
varText.data.getTag VarString = ~ ["name must be static string" @processor block compilerError] when
] [
block: 0 @[email protected];
VarString varText.data.get new @block.!nextStringAttribute
]
) sequence
] "mplBuiltinAttribute" @declareBuiltin ucall
[
@processor @block defaultCall
] "mplBuiltinCall" @declareBuiltin ucall
[
(
[processor compilable]
[refToName: @processor @block pop;]
[
refToName staticityOfVar Weak < ["method name must be a static string" @processor block compilerError] when
]
[
varName: refToName getVar;
varName.data.getTag VarString = ~ ["method name must be a static string" @processor block compilerError] when
]
[
fieldNameInfo: VarString varName.data.get makeStringView @processor findNameInfo;
fieldNameInfo @processor @block pop 0 dynamic @processor @block processMember
]
) sequence
] "mplBuiltinCallField" @declareBuiltin ucall
[
refToSchema: @processor @block pop;
refToVar: @processor @block pop;
processor compilable [
varSrc: refToVar getVar;
varSchema: refToSchema getVar;
varSchema.data.getTag VarRef = [refToSchema isVirtual] && [
VarRef varSchema.data.get.refToVar @processor @block copyVarFromChild @refToSchema set
refToSchema getVar !varSchema
] when
refToVar isNumber refToSchema isNumber and [
processor compilable [
refToVar staticityOfVar Dynamic > [
refToDst: RefToVar;
varSrc.data.getTag VarNat8 VarReal64 1 + [
tagSrc: new;
branchSrc: tagSrc varSrc.data.get.end;
varSchema.data.getTag VarNat8 VarReal64 1 + [
tagDst: new;
branchSchema: tagDst @[email protected];
branchSrc branchSchema cast tagDst @processor cutValue makeValuePair tagDst @processor @block createVariable @processor @block createPlainIR @refToDst set
] staticCall
] staticCall
refToVar staticityOfVar makeValuePair @refToDst getVar.@staticity set
refToDst @block push
] [
refToVar @processor @block makeVarRealCaptured
refToDst: RefToVar;
varSchema: refToSchema getVar;
varSchema.data.getTag VarNat8 VarReal64 1 + [
tagDst: new;
branchSchema: tagDst @[email protected];
branchSchema makeValuePair tagDst @processor @block createVariable @refToDst set
] staticCall
Dynamic makeValuePair @refToDst getVar.@staticity set
# a lot of cases for different casts
refToVar isReal refToSchema isReal or [
refToVar isReal refToSchema isReal and [
#Real to Real
refToVar @processor getStorageSize refToSchema @processor getStorageSize = [
@refToVar @refToDst @processor @block createCopyToNew
] [
refToVar @processor getStorageSize refToSchema @processor getStorageSize < [
refToVar @refToDst "fpext" @processor @block createCastCopyToNew
] [
refToVar @refToDst "fptrunc" @processor @block createCastCopyToNew
] if
] if
] [
refToVar isAnyInt [
#Int to Real
refToVar isNat [
refToVar @refToDst "uitofp" @processor @block createCastCopyToNew
] [
refToVar @refToDst "sitofp" @processor @block createCastCopyToNew
] if
] [
#Real to Int
[refToSchema isAnyInt] "Wrong cast number case!" assert
refToSchema isNat [
refToVar @refToDst "fptoui" @processor @block createCastCopyToNew
] [
refToVar @refToDst "fptosi" @processor @block createCastCopyToNew
] if
] if
] if
] [
#Int to Int
refToVar @processor getStorageSize refToSchema @processor getStorageSize = [
@refToVar @refToDst @processor @block createCopyToNew
] [
refToVar @processor getStorageSize refToSchema @processor getStorageSize < [
refToVar isNat [
refToVar @refToDst "zext" @processor @block createCastCopyToNew
] [
refToVar @refToDst "sext" @processor @block createCastCopyToNew
] if
] [
refToVar @refToDst "trunc" @processor @block createCastCopyToNew
] if
] if
] if
# here ends cast case list
refToDst @block push
] if
] when
] [
"can cast only numbers" @processor block compilerError
] if
] when
] "mplBuiltinCast" @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
[
refToVar: @processor @block pop;
processor compilable [
refToVar getVar.data.getTag VarCode = makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] "mplBuiltinCodeQuestion" @declareBuiltin ucall
[
(
[processor compilable]
[signature: parseSignature;]
[
name: ("null." processor.blocks.size) assembleString;
newBlock: signature name makeStringView TRUE dynamic @processor @block processImportFunction @[email protected];
]
[
gnr: newBlock.varNameInfo @processor @block getName;
cnr: @gnr 0 dynamic @processor @block processor.positions.last.file captureName;
refToVar: cnr.refToVar new;
refToVar @block push
]
) sequence
] "mplBuiltinCodeRef" @declareBuiltin ucall
[
refToVar: @processor @block pop;
processor compilable [
refToVar getVar.data.getTag VarImport = makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] "mplBuiltinCodeRefQuestion" @declareBuiltin ucall
[
refToCode: @processor @block pop;
processor compilable [
code: refToCode getVar;
code.data.getTag VarCode = ~ ["must be [CODE]" @processor block compilerError] [
astNodeArray: VarCode code.data.get.index processor.multiParserResult.memory.at;
astNodeArray.nodes.size Int64 cast makeValuePair VarInt32 @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] if
] when
] "mplBuiltinCodeTokenCount" @declareBuiltin ucall
[
refToOrdinal: @processor @block pop;
refToCode: @processor @block pop;
processor compilable [
varCode: refToCode getVar;
varOrdinal: refToOrdinal getVar;
varCode.data.getTag VarCode = ~ ["First argument must be [CODE]" @processor block compilerError] [
varOrdinal.data.getTag VarInt32 = ~ ["Ordinal must be Int32" @processor block compilerError] [
refToOrdinal staticityOfVar Weak < ["Ordinal must be static" @processor block compilerError] [
astNodeArray: VarCode varCode.data.get.index processor.multiParserResult.memory.at;
ordinal: VarInt32 varOrdinal.data.get.end Int32 cast;
ordinal 0 astNodeArray.nodes.size within ~ ["Ordinal is out of bounds" @processor block compilerError] [
astNode: ordinal astNodeArray.nodes @;
ordinal astNodeArray.nodes @ .positionInfo.fileId processor.files.at.get.text.getStringView
ordinal astNodeArray.nodes @ .positionInfo.offsetStart
ordinal astNodeArray.nodes @ .positionInfo.offsetEnd range
@processor @block makeVarString @block push
] if
] if
] if
] if
] when
] "mplBuiltinCodeTokenRead" @declareBuiltin ucall
[
TRUE dynamic @block.@nodeCompileOnce set
] "mplBuiltinCompileOnce" @declareBuiltin ucall
[TRUE @processor @block defaultMakeConstWith] "mplBuiltinConst" @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