-
Notifications
You must be signed in to change notification settings - Fork 4
/
irWriter.mpl
987 lines (826 loc) · 35.3 KB
/
irWriter.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
# 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
"String" use
"algorithm" use
"control" use
"conventions" use
"Block" use
"MplFile" use
"Var" use
"declarations" use
"defaultImpl" use
"pathUtils" use
"processor" use
appendInstruction: [
list: block:;;
offset: block.programTemplate.size;
list @[email protected]
block.programTemplate.size offset - offset makeInstruction @[email protected]
];
getMplSchema: [refToVar: processor: ;; refToVar getVar.mplSchemaId @[email protected]];
getNameById: [index: processor: ;; index @processor.nameBuffer.at makeStringView];
getIrName: [refToVar: processor: ;; refToVar getVar.irNameId @processor getNameById];
getIrType: [refToVar: processor: ;; refToVar @processor getMplSchema.irTypeId @processor getNameById];
getStaticStructIR: [
processor:;
refToVar:;
result: String;
unfinishedVars: RefToVar Array;
unfinishedTerminators: StringView Array;
refToVar @unfinishedVars.append
", " makeStringView @unfinishedTerminators.append
[
unfinishedVars.size 0 > [
current: unfinishedVars.last new;
@unfinishedVars.popBack
current isVirtual [
"Virtual field cannot be processed in static array constant!" failProc
] [
current isPlain [
(current @processor getIrType " " current getPlainConstantIR) @result.catMany
[
currentTerminator: unfinishedTerminators.last;
currentTerminator @result.cat
currentTerminator ", " = ~
@unfinishedTerminators.popBack
] loop
] [
curVar: current getVar;
curVar.data.getTag VarStruct = [
(current @processor getIrType " ") @result.catMany
struct: VarStruct curVar.data.get.get;
struct.homogeneous ["[" makeStringView] ["{" makeStringView] if @result.cat
first: TRUE dynamic;
struct.fields.size [
current: struct.fields.size 1 - i - struct.fields.at.refToVar;
current isVirtual ~ [
current @unfinishedVars.append
first [
struct.homogeneous ["]" makeStringView] ["}" makeStringView] if @unfinishedTerminators.append
FALSE dynamic @first set
] [
", " makeStringView @unfinishedTerminators.append
] if
] when
] times
] [
"Unknown type in static struct!" failProc
] if
] if
] if
TRUE
] &&
] loop
result.size 2 - @[email protected]
@result.makeZ
result
];
createDerefWith: [
refNameId: refTypeId: derefNameId: processor: block: ;;;;;
(" " @derefNameId @processor getNameById " = load " refTypeId @processor getNameById ", " refTypeId @processor getNameById "* " refNameId @processor getNameById) @block appendInstruction
refNameId @[email protected].@irName1 set
derefNameId @[email protected].@irName2 set
];
createDerefTo: [
refToVar: derefNameId: processor: block: ;;;;
refToVar getVar.irNameId refToVar @processor getMplSchema.irTypeId derefNameId @processor @block createDerefWith
];
createDerefToRegister: [
srcRef: processor: block: ;;;
derefNameId: @processor @block generateRegisterIRName;
srcRef getVar.irNameId srcRef @processor getMplSchema.irTypeId derefNameId @processor @block createDerefWith
derefNameId
];
createDerefFromRegisterToRegister: [
srcNameId: srcTypeId: processor: block: ;;;;
derefNameId: @processor @block generateRegisterIRName;
srcNameId srcTypeId derefNameId @processor @block createDerefWith
derefNameId
];
createAllocIRByReg: [
regNameId: regTypeId: processor: block: ;;;;
(" " regNameId @processor getNameById " = alloca " regTypeId @processor getNameById) @block appendInstruction
TRUE @[email protected].@alloca set
];
createAllocIR: [
refToVar: processor: block: ;;;
var: @refToVar getVar;
block.parent 0 = [
processor.options.partial new [
varBlock: block;
[varBlock.file nil? ~] "Topnode in nil file!" assert
varBlock.file.usedInParams ~
] && [
"; global var from another file" toString @[email protected]
] [
(refToVar @processor getIrName " = private local_unnamed_addr global " refToVar @processor getIrType " zeroinitializer") assembleString @[email protected]
] if
processor.prolog.size 1 - @var.@globalDeclarationInstructionIndex set
] [
refToVar getVar.irNameId refToVar @processor getMplSchema.irTypeId @processor @block createAllocIRByReg
refToVar getVar.irNameId @[email protected].@irName1 set
block.program.size 1 - @var.@allocationInstructionIndex set
] if
refToVar new
];
createStaticInitIR: [
refToVar: processor: block: ;;;
var: @refToVar getVar;
[block.parent 0 =] "Can be used only with global vars!" assert
processor.options.partial new [
varBlock: block;
[varBlock.file nil? ~] "Topnode in nil file!" assert
varBlock.file.usedInParams ~
] && [
"; global var from another file" toString @[email protected]
] [
(refToVar @processor getIrName " = private local_unnamed_addr global " refToVar @processor getStaticStructIR) assembleString @[email protected]
] if
processor.prolog.size 1 - @var.@globalDeclarationInstructionIndex set
refToVar new
];
createVarImportIR: [
refToVar: processor: block: ;;;
var: @refToVar getVar;
(refToVar @processor getIrName " = external global " refToVar @processor getIrType) assembleString @[email protected]
processor.prolog.size 1 - @var.@globalDeclarationInstructionIndex set
refToVar new
];
createVarExportIR: [
refToVar: processor: block: ;;;
var: @refToVar getVar;
(refToVar @processor getIrName " = dllexport global " refToVar @processor getIrType " zeroinitializer") assembleString @[email protected]
processor.prolog.size 1 - @var.@globalDeclarationInstructionIndex set
refToVar new
];
createGlobalAliasIR: [
alias: aliasee: aliaseeType: processor: ;;;;
(alias @processor getNameById " = alias " aliaseeType @processor getNameById ", " aliaseeType @processor getNameById "* " aliasee @processor getNameById) assembleString @[email protected]
];
createStoreConstant: [
refWithValue: refToDst: processor: block: ;;;;
s: refWithValue getPlainConstantIR;
(" store " refToDst @processor getIrType " " s ", " refToDst @processor getIrType "* " refToDst @processor getIrName) @block appendInstruction
];
createPlainIR: [
refToVar: processor: block: ;;;
[refToVar isPlain] "Var is not plain" assert
refToVar @refToVar @processor @block createAllocIR @processor @block createStoreConstant
refToVar new
];
createStringIR: [
block:;
processor:;
refToVar:;
string:;
stringId: processor.lastStringId new;
stringName: ("@string." processor.lastStringId) assembleString;
processor.lastStringId 1 + @processor.@lastStringId set
var: @refToVar getVar;
stringName makeStringView @processor findNameInfo @var.@mplNameId set
("getelementptr inbounds ({i32, [" string.size " x i8]}, {i32, [" string.size " x i8]}* " stringName ", i32 0, i32 1, i32 0)") assembleString @processor makeStringId @var.@irNameId set
valueImplementation: string getStringImplementation;
prolog: (stringName " = private unnamed_addr constant {i32, [" string.size " x i8]} {i32 " string.size ", [" string.size " x i8] c\"" valueImplementation "\"}") assembleString;
block.nextStringAttribute.size 0 = ~ [
", " @prolog.cat
block.nextStringAttribute @prolog.cat
] when
@prolog @[email protected]
];
createGetTextSizeIR: [
refToName: refToDst: processor: block:;;;;
int32PtrRegister: @processor @block generateRegisterIRName;
ptrRegister: @processor @block generateRegisterIRName;
int32SizeRegister: @processor @block generateRegisterIRName;
int64SizeRegister: @processor @block generateRegisterIRName;
(" " int32PtrRegister @processor getNameById " = bitcast i8* " refToName @processor getIrName " to i32*") @block appendInstruction
(" " ptrRegister @processor getNameById " = getelementptr i32, i32* " int32PtrRegister @processor getNameById ", i32 -1") @block appendInstruction
(" " int32SizeRegister @processor getNameById " = load i32, i32* " ptrRegister @processor getNameById) @block appendInstruction
processor.options.pointerSize 64nx = [
(" " int64SizeRegister @processor getNameById " = zext i32 " int32SizeRegister @processor getNameById " to i64") @block appendInstruction
int64SizeRegister refToDst @processor @block createStoreFromRegister
] [
int32SizeRegister refToDst @processor @block createStoreFromRegister
] if
];
createTypeDeclaration: [
alias: irType: processor: ;;;
(@alias " = type " @irType) assembleString @[email protected]
];
createStaticGEP: [
resultRefToVar: index: structRefToVar: processor: block:;;;;;
struct: structRefToVar getVar;
realIndex: index VarStruct struct.data.get.get.realFieldIndexes.at;
[realIndex 0 < ~] "Gep index must be non-negative!" assert
(" " resultRefToVar @processor getIrName " = getelementptr " structRefToVar @processor getIrType ", " structRefToVar @processor getIrType "* " structRefToVar @processor getIrName ", i32 0, i32 " realIndex) @block appendInstruction
structRefToVar getVar.irNameId @[email protected].@irName1 set
resultRefToVar getVar.irNameId @[email protected].@irName2 set
];
createDynamicGEP: [
resultRefToVar: indexRefToVar: structRefToVar: processor: block:;;;;;
struct: structRefToVar getVar;
indexRegister: indexRefToVar @processor @block createDerefToRegister;
processor.options.arrayChecks [
structSize: VarStruct struct.data.get.get.fields.size; #in homogenius struct it is = realFieldIndex
checkRegister: @processor @block generateRegisterIRName;
(" " checkRegister @processor getNameById " = icmp ult i32 " indexRegister @processor getNameById ", " structSize) @block appendInstruction
(" br i1 " checkRegister @processor getNameById ", label %label" block.lastBrLabelName 1 + ", label %label" block.lastBrLabelName) @block appendInstruction
@block createLabel
"Index is out of bounds!" @processor @block createFailWithMessage
1 @block createJump
@block createLabel
] when
(" " resultRefToVar @processor getIrName " = getelementptr " structRefToVar @processor getIrType ", " structRefToVar @processor getIrType "* " structRefToVar @processor getIrName ", i32 0, i32 " indexRegister @processor getNameById) @block appendInstruction
];
createGEPInsteadOfAlloc: [
dstRef: index: struct: processor: block: ;;;;;
dstVar: @dstRef getVar;
# create GEP instruction
dstRef index struct @processor @block createStaticGEP
# change allocation instruction
[dstVar.allocationInstructionIndex block.program.size <] "Var is not allocated!" assert
instruction: dstVar.allocationInstructionIndex @[email protected];
block.program.last @instruction set
FALSE @[email protected].@enabled set
dstVar.allocationInstructionIndex @dstVar.@getInstructionIndex set
-1 @dstVar.@allocationInstructionIndex set
];
createStoreFromRegisterToRegister: [
regNameId: destRegNameId: regType: processor: block: ;;;;;
(" store " regType @processor getNameById " " @regNameId @processor getNameById ", " regType @processor getNameById "* " destRegNameId @processor getNameById) @block appendInstruction
regNameId @[email protected].@irName1 set
destRegNameId @[email protected].@irName2 set
];
createStoreFromRegister: [
regNameId: destRefToVar: processor: block: ;;;;
regNameId destRefToVar getVar.irNameId destRefToVar @processor getMplSchema.irTypeId @processor @block createStoreFromRegisterToRegister
];
getValueOrDeref: [
refToVar:;
refToVar staticityOfVar Dynamic > [
refToVar getPlainConstantIR
] [
@refToVar @processor @block makeVarRealCaptured
refToVar @processor @block createDerefToRegister @processor getNameById toString
] if
];
createStringCompare: [
arg1: arg2: result: opName: processor: block: ;;;;;;
@arg1 @processor @block makeVarRealCaptured
@arg2 @processor @block makeVarRealCaptured
var1name: @arg1 @processor getIrName toString;
var2name: @arg2 @processor getIrName toString;
resultReg: @processor @block generateRegisterIRName;
(" " resultReg @processor getNameById " = " @opName " " arg1 @processor getIrType "* " var1name ", " var2name) @block appendInstruction
resultReg result @processor @block createStoreFromRegister
];
createBinaryOperation: [
arg1: arg2: result: opName: processor: block: ;;;;;;
var1name: @arg1 getValueOrDeref;
var2name: @arg2 getValueOrDeref;
resultReg: @processor @block generateRegisterIRName;
(" " resultReg @processor getNameById " = " @opName " " arg1 @processor getIrType " " var1name ", " var2name) @block appendInstruction
resultReg result @processor @block createStoreFromRegister
];
createBinaryOperationDiffTypes: [
arg1: arg2: result: opName: processor: block: ;;;;;;
var1name: @arg1 getValueOrDeref;
var2name: @arg2 getValueOrDeref;
castedReg: @processor @block generateRegisterIRName;
castName: arg1 @processor getStorageSize arg2 @processor getStorageSize > [
arg1 isNat ["zext"] ["sext"] if
] [
"trunc"
] if;
(" " castedReg @processor getNameById " = " castName " " arg2 @processor getIrType " " var2name " to " arg1 @processor getIrType) @block appendInstruction
resultReg: @processor @block generateRegisterIRName;
(" " resultReg @processor getNameById " = " opName " " arg1 @processor getIrType " " var1name ", " castedReg @processor getNameById) @block appendInstruction
resultReg result @processor @block createStoreFromRegister
];
createDirectBinaryOperation: [
arg1: arg2: result: opName: processor: block: ;;;;;;
resultReg: @processor @block generateRegisterIRName;
(" " resultReg @processor getNameById " = " opName " " arg1 @processor getIrType "* " arg1 @processor getIrName ", " arg2 @processor getIrName) @block appendInstruction
resultReg result @processor @block createStoreFromRegister
];
createUnaryOperation: [
arg: result: opName: mopName: processor: block: ;;;;;;
varp: arg @processor @block createDerefToRegister;
resultReg: @processor @block generateRegisterIRName;
(" " resultReg @processor getNameById " = " opName " " arg @processor getIrType " " mopName varp @processor getNameById) @block appendInstruction
resultReg result @processor @block createStoreFromRegister
];
createMemset: [
srcRef: dstRef: processor: block: ;;;;
srcRef dstRef @processor @block setVar
srcRef isVirtual ~ [
loadReg: srcRef @processor @block createDerefToRegister;
loadReg dstRef @processor @block createStoreFromRegister
] when
];
createCheckedCopyToNewWith: [
srcRef: dstRef: doDie: ;;;
srcRef isAutoStruct [
srcRef getVar.temporary [
loadReg: srcRef @processor @block createDerefToRegister;
loadReg dstRef @processor @block createStoreFromRegister
@srcRef markAsUnableToDie
] [
srcRef varIsMoved [
srcRef.mutable [
loadReg: srcRef @processor @block createDerefToRegister;
loadReg dstRef @processor @block createStoreFromRegister
srcRef @processor @block callInit
@srcRef fullUntemporize
] [
"movable variable is not mutable" @processor block compilerError
] if
] [
prevMut: dstRef.mutable;
prevMoved: dstRef.moved;
prevTmp: dstRef getVar.temporary new;
TRUE @dstRef.setMutable
@dstRef fullUntemporize
dstRef @processor @block callInit
srcRef dstRef @processor @block callAssign
prevMut @dstRef.setMutable
prevMoved @dstRef.setMoved
prevTmp @dstRef getVar.@temporary set
] if
] if
doDie [dstRef @[email protected]] when
] [
srcRef isPlain [srcRef staticityOfVar Dynamic >] && [
srcRef dstRef @processor @block createStoreConstant
] [
@srcRef @processor @block makeVarRealCaptured
loadReg: srcRef @processor @block createDerefToRegister;
loadReg dstRef @processor @block createStoreFromRegister
] if
@dstRef makeVarPtrCaptured
] if
];
createCheckedCopyToNew: [processor: block:;; TRUE dynamic createCheckedCopyToNewWith];
createCheckedCopyToNewNoDie: [processor: block:;; FALSE dynamic createCheckedCopyToNewWith];
createCopyToNew: [
newRefToVar: processor: block: ;;;
newRefToVar isVirtual [
oldRefToVar:;
"unable to copy virtual autostruct" @processor block compilerError
] [
@newRefToVar @processor @block createAllocIR @processor @block createCheckedCopyToNew
] if
];
createCastCopyToNew: [
srcRef: dstRef: castName: processor: block: ;;;;;
@dstRef @processor @block createAllocIR !dstRef
loadReg: srcRef @processor @block createDerefToRegister;
castedReg: @processor @block generateRegisterIRName;
(" " castedReg @processor getNameById " = " @castName " " srcRef @processor getIrType " " loadReg @processor getNameById " to " dstRef @processor getIrType) @block appendInstruction
castedReg dstRef @processor @block createStoreFromRegister
];
createCastCopyPtrToNew: [
srcRef: dstRef: castName: processor: block: ;;;;;
@dstRef @processor @block createAllocIR !dstRef
castedReg: @processor @block generateRegisterIRName;
(" " castedReg @processor getNameById " = " @castName " " srcRef @processor getIrType "* " srcRef @processor getIrName " to " dstRef @processor getIrType) @block appendInstruction
castedReg dstRef @processor @block createStoreFromRegister
];
createRefOperation: [
srcRef: dstRef: processor: block: ;;;;
@dstRef @processor @block createAllocIR !dstRef
srcRef getVar.irNameId dstRef @processor @block createStoreFromRegister
];
createRetValue: [
refToVar: processor: block: ;;;
getReg: refToVar @processor @block createDerefToRegister;
(" ret " refToVar @processor getIrType " " getReg @processor getNameById) @block appendInstruction
];
createCallIR: [
refToRet: argList: conventionName: funcName: hasCallTrace: processor: block: ;;;;;;;
haveRet: refToRet.var nil? ~;
retName: 0;
processor.options.hidePrefixes [processor.positions.last.file.name swap beginsWith ~] all [
processor.options.callTrace hasCallTrace and [@processor @block createCallTraceProlog] when
] when
offset: block.programTemplate.size;
haveRet [
@processor @block generateRegisterIRName @retName set
(" " @retName @processor getNameById " = call " conventionName refToRet @processor getIrType " ") @[email protected]
] [
(" call " conventionName "void ") @[email protected]
] if
@funcName @[email protected]
"(" @[email protected]
i: 0 dynamic;
[
i argList.size < [
currentArg: i argList.at;
i 0 > [", " @[email protected]] when
currentArg.irTypeId @processor getNameById @[email protected]
currentArg.byRef ["*" @[email protected]] when
" " @[email protected]
currentArg.irNameId @processor getNameById @[email protected]
i 1 + @i set TRUE
] &&
] loop
")" @[email protected]
block.programTemplate.size offset - offset makeInstruction @[email protected]
@processor @block addDebugLocationForLastInstruction
processor.options.hidePrefixes [processor.positions.last.file.name swap beginsWith ~] all [
processor.options.callTrace hasCallTrace and [@processor @block createCallTraceEpilog] when
] when
retName
];
createLabel: [
block:;
("label" block.lastBrLabelName ":") @block appendInstruction
block.lastBrLabelName 1 + @block.@lastBrLabelName set
];
createBranch: [
timeShift: refToCond: processor: block: ;;;;
condReg: refToCond @processor @block createDerefToRegister;
(" br i1 " condReg @processor getNameById ", label %label" block.lastBrLabelName timeShift - ", label %label" block.lastBrLabelName timeShift - 1 +) @block appendInstruction
];
createJump: [
timeShift: block:;;
(" br label %label" block.lastBrLabelName timeShift - 1 +) @block appendInstruction
];
createPhiNode: [
varType: varName: thenName: elseName: shift: block:;;;;;;
(" " @varName " = phi " @varType " [ " @thenName ", %label" block.lastBrLabelName 3 - shift + " ], [ " @elseName ", %label" block.lastBrLabelName 2 - shift + " ]") @block appendInstruction
];
createComment: [
comment: block:;;
(" ;" comment) @block appendInstruction
];
addStrToProlog: [
what: processor: ;;
what toString @[email protected]
];
createFloatBuiltins: [
processor:;
"declare float @llvm.sin.f32(float)" @processor addStrToProlog
"declare double @llvm.sin.f64(double)" @processor addStrToProlog
"declare float @llvm.cos.f32(float)" @processor addStrToProlog
"declare double @llvm.cos.f64(double)" @processor addStrToProlog
"declare float @llvm.floor.f32(float)" @processor addStrToProlog
"declare double @llvm.floor.f64(double)" @processor addStrToProlog
"declare float @llvm.ceil.f32(float)" @processor addStrToProlog
"declare double @llvm.ceil.f64(double)" @processor addStrToProlog
"declare float @llvm.sqrt.f32(float)" @processor addStrToProlog
"declare double @llvm.sqrt.f64(double)" @processor addStrToProlog
"declare float @llvm.log.f32(float)" @processor addStrToProlog
"declare double @llvm.log.f64(double)" @processor addStrToProlog
"declare float @llvm.log10.f32(float)" @processor addStrToProlog
"declare double @llvm.log10.f64(double)" @processor addStrToProlog
"declare float @llvm.pow.f32(float, float)" @processor addStrToProlog
"declare double @llvm.pow.f64(double, double)" @processor addStrToProlog
];
getLLPriority: [
processor:;
priority: 0 dynamic;
processor.moduleFunctions [
currentBlock: processor.blocks.at.get;
currentBlock.deleted ~ currentBlock.empty ~ and [
currentBlock.globalPriority priority > [
currentBlock.globalPriority @priority set
] when
] when
] each
priority
];
createCheckers: [
processor:;
processor.files.size [
i 1 > [
currentFile: i processor.files.at.get;
currentName: currentFile.name stripExtension nameWithoutBadSymbols;
processor.beginFuncIndex 0 < ~ [
String @processor addStrToProlog
("define void @check." currentName "() { ret void }") assembleString @processor addStrToProlog
] [
currentFile.usedInParams [
String @processor addStrToProlog
("declare void @check." currentName "()") assembleString @processor addStrToProlog
("define void @check." currentName ".call() { call void @check." currentName "() ret void}") assembleString @processor addStrToProlog
] when
] if
processor.options.partial [currentFile.usedInParams ~] && [
String @processor addStrToProlog
("declare void @module." currentName ".ctor()") assembleString @processor addStrToProlog
] when
] when
] times
];
createDtors: [
processor:;
dtorByFile: Int32 Array Array;
processor.files.size @dtorByFile.resize
processor.dtorFunctions [
blockId:;
cur: blockId processor.blocks.at.get;
processor.options.partial ~ [cur.file.usedInParams new] || [
id: cur.file.fileId new;
blockId id @dtorByFile.at.append
] when
] each
processor.files.size [
i 1 > [
currentFile: i processor.files.at.get;
currentName: currentFile.name stripExtension nameWithoutBadSymbols;
String @processor addStrToProlog
processor.options.partial ~ [currentFile.usedInParams new] || [
("define void @module." currentName ".dtor() {") assembleString @processor addStrToProlog
i dtorByFile.at [
cur: processor.blocks.at.get.irName new;
(" call void " cur "()") assembleString @processor addStrToProlog
] each
" ret void" @processor addStrToProlog
"}" @processor addStrToProlog
] [
("declare void @module." currentName ".dtor()") assembleString @processor addStrToProlog
] if
] when
] times
];
addCtorsToBeginFunc: [
processor:;
processor.beginFuncIndex 0 < ~ [
block: processor.beginFuncIndex @[email protected];
previousVersion: @block.@program new;
0 @previousVersion.at @[email protected]
block.beginPosition @[email protected]
processor.moduleFunctions.size [
i 0 > [ # skip definitions
currentFunction: i processor.moduleFunctions.at processor.blocks.at.get;
currentFile: currentFunction.file;
currentName: currentFile.name stripExtension nameWithoutBadSymbols;
(" call void @module." currentName ".ctor()") @block appendInstruction
processor.options.debug [
@processor @block addDebugLocationForLastInstruction
] when
] when
] times
previousVersion.size [
i 0 > [
current: i @previousVersion.at;
@current @[email protected]
] when
] times
block.instructionCountBeforeRet previousVersion.size - block.program.size + @block.!instructionCountBeforeRet
] when
];
addDtorsToEndFunc: [
processor:;
processor.endFuncIndex 0 < ~ [
block: processor.endFuncIndex @[email protected];
previousVersion: @block.@program new;
block.beginPosition @[email protected]
previousVersion.size [
i block.instructionCountBeforeRet < [
current: i @previousVersion.at;
@current @[email protected]
] when
] times
processor.moduleFunctions.size [
i 0 > [ # skip definitions
currentFunction: processor.moduleFunctions.size i - processor.moduleFunctions.at processor.blocks.at.get;
currentFile: currentFunction.file;
currentName: currentFile.name stripExtension nameWithoutBadSymbols;
(" call void @module." currentName ".dtor()") @block appendInstruction
processor.options.debug [
@processor @block addDebugLocationForLastInstruction
] when
] when
] times
previousVersion.size block.instructionCountBeforeRet - [
current: i block.instructionCountBeforeRet + @previousVersion.at;
@current @[email protected]
] times
] when
];
sortInstructions: [
block: processor: ;;
allocs: Instruction Array;
fakePointersAllocs: Instruction Array;
fakePointers: Instruction Array;
noallocs: Instruction Array;
bannedIds: Int32 Array;
block.program.size [
current: i @[email protected];
current.fakeAlloca [
current.irName1 @bannedIds.append
FALSE @current.!enabled
] when
] times
[
bannedIds.size 0 > [
bannedId: bannedIds.last new;
@bannedIds.popBack
block.program.size [
current: i @[email protected];
current.enabled [
current.irName1 bannedId = [
current.irName2 0 < ~ [
current.irName2 @bannedIds.append
] when
FALSE @current.!enabled
] when
current.irName2 bannedId = [
FALSE @current.!enabled
] when
] when
] times
TRUE
] &&
] loop
block.program.size [
current: i @[email protected];
i 0 = [current.alloca new] || [
current.fakePointer [
@current @fakePointersAllocs.append
] [
@current @allocs.append
] if
] [
current.fakePointer [
@current @fakePointers.append
] [
@current @noallocs.append
] if
] if
] times
@allocs [@[email protected]] each
@fakePointersAllocs [@[email protected]] each
@fakePointers [@[email protected]] each
@noallocs [@[email protected]] each
];
addAliasesForUsedNodes: [
processor:;
String @[email protected]
"; Func aliases" toString @[email protected]
@processor.@blocks [
block0: .get;
block0 nodeHasCode [
@block0.@aliases [@[email protected]] each
] when
] each
];
checkBeginEndPoint: [
processor:;
result: TRUE;
processor.beginFuncIndex 0 < processor.endFuncIndex 0 < and [
# ok
] [
processor.beginFuncIndex 0 < ~ processor.endFuncIndex 0 < ~ and [
beginFuncFile: processor.beginFuncIndex processor.blocks.at.get.file;
endFuncFile: processor.endFuncIndex processor.blocks.at.get.file;
beginFuncFile endFuncFile is ~ [
FALSE !result
] when
] [
FALSE !result
] if
] if
result ~ [
("beginFunc and endFunc must be in one module" LF) assembleString @processor.@result.@[email protected]
FALSE @processor.@result.@success set
] when
];
createCallTraceData: [
processor:;
tlPrefix: processor.options.threadModel 1 = ["thread_local "] [""] if;
"%type.callTraceInfo = type {%type.callTraceInfo*, i8*, i32, i32}" toString @[email protected]
processor.beginFuncIndex 0 < ~ [
("@debug.callTracePtr = " tlPrefix "unnamed_addr global %type.callTraceInfo* null") assembleString @[email protected]
] [
("@debug.callTracePtr = external " tlPrefix "unnamed_addr global %type.callTraceInfo*") assembleString @[email protected]
] if
];
createCallTraceProlog: [
processor: block: ;;
ptrRegName: "%debug.oldCallTracePtr";
ctRegName: "%debug.callTraceData";
prevPtrRegName: "%debug.callTraceData.prev";
namePtrRegName: "%debug.callTraceData.name";
linePtrRegName: "%debug.callTraceData.line";
colmPtrRegName: "%debug.callTraceData.column";
block.hasNestedCall ~ [
(" " ctRegName " = alloca %type.callTraceInfo") @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
(" " prevPtrRegName " = getelementptr inbounds %type.callTraceInfo, %type.callTraceInfo* " ctRegName ", i32 0, i32 0") @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
(" " namePtrRegName " = getelementptr inbounds %type.callTraceInfo, %type.callTraceInfo* " ctRegName ", i32 0, i32 1") @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
(" " linePtrRegName " = getelementptr inbounds %type.callTraceInfo, %type.callTraceInfo* " ctRegName ", i32 0, i32 2") @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
(" " colmPtrRegName " = getelementptr inbounds %type.callTraceInfo, %type.callTraceInfo* " ctRegName ", i32 0, i32 3") @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
(" " ptrRegName " = load %type.callTraceInfo*, %type.callTraceInfo** @debug.callTracePtr") @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
(" store %type.callTraceInfo* " ptrRegName ", %type.callTraceInfo** " prevPtrRegName) @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
fileNameVar: (processor.positions.last.file.name "\00") assembleString @processor @block makeVarString;
(" store i8* " fileNameVar @processor getIrName ", i8** " namePtrRegName) @block appendInstruction
TRUE @[email protected].@alloca set #fake for good sorting
TRUE @block.@hasNestedCall set
] when
(" store i32 " processor.positions.last.line ", i32* " linePtrRegName) @block appendInstruction
(" store i32 " processor.positions.last.column ", i32* " colmPtrRegName) @block appendInstruction
(" store %type.callTraceInfo* " ctRegName ", %type.callTraceInfo** @debug.callTracePtr") @block appendInstruction
];
createCallTraceEpilog: [
processor: block:;;
ptrRegName: "%debug.oldCallTracePtr";
(" store %type.callTraceInfo* " ptrRegName ", %type.callTraceInfo** @debug.callTracePtr") @block appendInstruction
];
createGetCallTrace: [
variable: processor: block: ;;;
variableIrType: variable @processor getMplSchema.irTypeId;
processor.options.callTrace [
currentPtrRegName: @processor @block generateRegisterIRName;
(" " currentPtrRegName @processor getNameById " = load %type.callTraceInfo*, %type.callTraceInfo** @debug.callTracePtr") @block appendInstruction
varPtrRegName: @processor @block generateRegisterIRName;
(" " varPtrRegName @processor getNameById " = bitcast %type.callTraceInfo* " currentPtrRegName @processor getNameById " to " variableIrType @processor getNameById) @block appendInstruction
(" store " variableIrType @processor getNameById " " varPtrRegName @processor getNameById ", " variableIrType @processor getNameById "* " variable @processor getIrName) @block appendInstruction
] [
(" store " variableIrType @processor getNameById " null, " variableIrType @processor getNameById "* " variable @processor getIrName) @block appendInstruction
] if
];
# require captures "processor" and "codeNode"
generateVariableIRNameWith: [
hostOfVariable: temporaryRegister: processor: block: ;;;;
temporaryRegister ~ [block.parent 0 =] && [
("@global." processor.globalVarCount) assembleString @processor makeStringId
processor.globalVarCount 1 + @processor.@globalVarCount set
] [
hostOfVariable.lastVarName @processor makeDefaultVarId
hostOfVariable.lastVarName 1 + @hostOfVariable.@lastVarName set
] if
];
generateRegisterIRName: [processor: block: ;; @block TRUE @processor block generateVariableIRNameWith];
{
block: Block Ref;
processor: Processor Ref;
refToDst: RefToVar Ref;
refToSrc: RefToVar Ref;
} () {} [
srcRef: dstRef: processor: block: ;;;;
srcRef isAutoStruct [
@srcRef @processor @block makeVarRealCaptured
@dstRef @processor @block makeVarRealCaptured
@dstRef makeVarPtrCaptured
srcRef getVar.temporary [
# die-bytemove is faster than assign-die, I think
processor.options.verboseIR ["set from temporary" @block createComment] when
dstRef @processor @block callDie
srcRef dstRef @processor @block createMemset
@srcRef markAsUnableToDie
] [
srcRef varIsMoved [
processor.options.verboseIR ["set from moved" @block createComment] when
dstRef @processor @block callDie
srcRef dstRef @processor @block createMemset
srcRef @processor @block callInit
@srcRef fullUntemporize
] [
processor.options.verboseIR ["set; call ASSIGN" @block createComment] when
srcRef isVirtual [
"unable to copy virtual autostruct" @processor block compilerError
] [
srcRef dstRef @processor @block callAssign
] if
] if
] if
] [
srcRef isPlain [
srcRef staticityOfVar Dynamic > [
srcRef dstRef @processor @block setVar
@dstRef @processor @block makeVarRealCaptured
@dstRef makeVarPtrCaptured
dstRef dstRef @processor @block createStoreConstant
] [
@srcRef @processor @block makeVarRealCaptured
@dstRef @processor @block makeVarRealCaptured
@dstRef makeVarPtrCaptured
srcRef dstRef @processor @block createMemset
] if
] [
@srcRef @processor @block makeVarRealCaptured
@dstRef @processor @block makeVarRealCaptured
@dstRef makeVarPtrCaptured
srcRef dstRef @processor @block createMemset
] if
] if
] "createCopyToExists" exportFunction