forked from mozart/mozart2-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeEmitter.oz
2525 lines (2482 loc) · 98.5 KB
/
CodeEmitter.oz
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
%%%
%%% Author:
%%% Leif Kornstaedt <[email protected]>
%%%
%%% Copyright:
%%% Leif Kornstaedt, 1997-2001
%%%
%%% Last change:
%%% $Date$ by $Author$
%%% $Revision$
%%%
%%% This file is part of Mozart, an implementation of Oz 3:
%%% http://www.mozart-oz.org
%%%
%%% See the file "LICENSE" or
%%% http://www.mozart-oz.org/LICENSE.html
%%% for information on usage and redistribution
%%% of this file, and for a DISCLAIMER OF ALL
%%% WARRANTIES.
%%%
%\define DEBUG_EMIT
%\define DEBUG_OPTIMIZER
%\define DEBUG_SHARED
functor
import
CompilerSupport(isCopyableName) at 'x-oz://boot/CompilerSupport'
FD(decl int distinct assign)
Space(new waitStable ask merge)
Debug(getRaiseOnBlock setRaiseOnBlock) at 'x-oz://boot/Debug'
Property(get)
Builtins(getInfo)
System(show)
export
'class': Emitter
Continuations
prepare
Continuations = c(vDebugEntry: 4
vDebugExit: 4
vMakePermanent: 3
vClear: 3
vUnify: 4
vEquateConstant: 4
vEquateRecord: 6
vGetVariable: 3
vCallBuiltin: 5
vCallGlobal: 5
vCallMethod: 7
vCall: 5
vConsCall: 5
vDeconsCall: 6
vCallProcedureRef: 5
vCallConstant: 5
vInlineDot: 7
vInlineAt: 4
vInlineAssign: 4
vGetSelf: 3
vSetSelf: 3
vDefinition: 7
vDefinitionCopy: 8
vShared: ~1
vExHandler: 6
vPopEx: 3
vTestBool: 7
vTestBuiltin: 6
vTestConstant: 7
vMatch: 6
vLockThread: 4
vLockEnd: 3)
define
\ifdef DEBUG_EMIT
proc {ShowVInstr VInstr} % for debugging
L = {Label VInstr}
N = {Width VInstr}
NewVInstr = {MakeTuple L N}
in
{For 1 N 1
proc {$ I} X = VInstr.I in
if {IsFree X} then X
elseif {BitArray.is X} then {BitArray.toList X}
elseif {IsRecord X}
andthen {HasFeature Continuations {Label X}}
then {Label X}
else X
end = NewVInstr.I
end}
{System.show NewVInstr}
end
\endif
local
%%
%% We must not hoist record containing copyable names since
%% procedure code instantiation would be unable to replace them.
%%
fun {IsConstant VArg}
case VArg of constant(C) then
{Not {CompilerSupport.isCopyableName C}}
else false
end
end
fun {GetConstant constant(X)} X end
fun {HoistVArg VArg}
case VArg of record(Atomname RecordArity VArgs) then NewVArgs in
NewVArgs = {Map VArgs HoistVArg}
if {Not {CompilerSupport.isCopyableName Atomname}} andthen
{All NewVArgs IsConstant} andthen
({IsInt RecordArity} orelse
{Not {Some RecordArity CompilerSupport.isCopyableName}})
then Args X in
Args = {Map NewVArgs GetConstant}
X = if {IsInt RecordArity} then
{List.toTuple Atomname Args}
else
{List.toRecord Atomname
{List.zip RecordArity Args fun {$ F X} F#X end}}
end
constant(X)
else record(Atomname RecordArity NewVArgs)
end
else VArg
end
end
in
fun {HoistRecord State Atomname RecordArity VArgs}
if {State getSwitch(recordhoist $)} then
{HoistVArg record(Atomname RecordArity VArgs)}
else
record(Atomname RecordArity VArgs)
end
end
end
fun {NextFreeIndex Used I}
if {Dictionary.member Used I} then {NextFreeIndex Used I + 1}
else I
end
end
fun {OccursInVArgs VArgs Reg}
case VArgs of VArg|VArgr then
case VArg of value(!Reg) then true
elseof record(_ _ VArgs) then
{OccursInVArgs VArgs Reg} orelse {OccursInVArgs VArgr Reg}
else {OccursInVArgs VArgr Reg}
end
[] nil then false
end
end
proc {GetRegs VArgs Hd Tl}
case VArgs of VArg|VArgr then Inter in
case VArg of value(Reg) then
Hd = Reg|Inter
[] record(_ _ VArgs) then
{GetRegs VArgs Hd Inter}
else
Hd = Inter
end
{GetRegs VArgr Inter Tl}
[] nil then
Hd = Tl
end
end
fun {FilterNonlinearRegs Regs}
case Regs of Reg|Regr then
if {Member Reg Regr} then Reg|{FilterNonlinearRegs Regr}
else {FilterNonlinearRegs Regr}
end
[] nil then nil
end
end
fun {GetNonlinearRegs VArgs}
{FilterNonlinearRegs {GetRegs VArgs $ nil}}
end
local
local
proc {DoAssign L H D X N}
if L=<H then
if {BitArray.test D L} then
X.L=N {DoAssign L+1 H D X N+1}
else
{DoAssign L+1 H D X N}
end
end
end
in
proc {AssignFirst _|D X}
{DoAssign {BitArray.low D} {BitArray.high D} D X 0}
end
end
local
proc {DoVector L H D X V I}
if L=<H then
if {BitArray.test D L} then
V.I=X.L {DoVector L+1 H D X V I+1}
else
{DoVector L+1 H D X V I}
end
end
end
in
fun {MakeVector C|D X}
V={MakeTuple v C}
in
{DoVector {BitArray.low D} {BitArray.high D} D X V 1}
V
end
end
in
class RegisterOptimizer
attr
N: 1 % Current Y index
CDs: nil % List of pairs cardinality and bitarrays
% representing distinct constraints
Es: nil % List of pairs of equality constraints
Ns: nil % List of pairs of inequality constraints
Ss: nil % List of hard assignments
feat
Mapping % Maps allocation to real registers
meth init
%% Assumption: register indices start from 1
self.Mapping = {Dictionary.new}
end
meth Distinct(Ys)
case Ys of [_] then skip else
D2={BitArray.fromList Ys}
in
case @CDs
of (_|D1)|CDr then
if {BitArray.subsumes D2 D1} then
CDs <- ({BitArray.card D2}|D2)|CDr
else
CDs <- ({BitArray.card D2}|D2)|@CDs
end
else
CDs <- [{BitArray.card D2}|D2]
end
end
end
meth neq(I1 I2)
Ns <- (I1|I2)|@Ns
end
meth decl(Is ?Y ?I)
J
in
I=@N
N <- I+1
Y=y(J)
{Dictionary.put self.Mapping I J}
{self Distinct(I|Is)}
end
meth isEmpty($)
{Dictionary.isEmpty self.Mapping}
end
meth eq(Y1 Y2)
if Y1\=Y2 then
Es <- (Y1|Y2)|@Es
end
end
meth set(Y I)
if I >= @N then
%% Must accomodate fixed (for debugger) Y registers
N <- I+1
end
Ss <- (Y|I)|@Ss
end
meth Optimize(AddNs $)
LNs = @Ns
LCDs = {Sort @CDs fun {$ C1|_ C2|_}
C1>C2
end}
LEs = @Es
LSs = @Ss
LN = @N-1
\ifdef DEBUG_OPTIMIZER
{System.show 'OPTIMISE'}
local
ShowDist = {Map LCDs fun {$ _|C}
distinct_regs({BitArray.toList C})
end
}
in
{ForAll LEs proc {$ R1|R2}
{System.show eq_reg(R1 R2)}
end}
{ForAll LSs proc {$ R1|R2}
{System.show hard_eq_reg(R1 R2)}
end}
{ForAll ShowDist System.show}
{ForAll LNs proc {$ R1|R2}
{System.show ineq_reg(R1 R2)}
end}
end
\endif
S = {Space.new proc {$ X}
%% Create mapping
X = {MakeTuple regs LN}
%% Process equality constraints
{ForAll LEs
proc {$ Y1|Y2}
X.Y1=X.Y2
end}
if LSs==nil then
%% Take the first distinct and assign directly
if LCDs\=nil then
{AssignFirst LCDs.1 X}
end
else
%% Do hard assignments
{ForAll LSs
proc {$ Y|I}
X.Y=I
end}
end
%% Tell domain constraints
X ::: 0#LN
if AddNs then
%% inequalities
{ForAll LNs proc {$ I1|I2} X.I1 \=: X.I2 end}
end
local
Vs={Map if LSs==nil andthen LCDs\=nil then
LCDs.2
else LCDs
end
fun {$ LCD}
{MakeVector LCD X}
end}
in
%% Post distincts
{ForAll Vs FD.distinct}
%% Assign following distincts
{ForAll Vs proc {$ V}
{FD.assign min V}
end}
end
{FD.assign min X}
end}
T = {Thread.this}
RaiseOnBlock = {Debug.getRaiseOnBlock T}
Alloc Status
in
{Debug.setRaiseOnBlock T false}
{Space.ask S Status}
if Status \= succeeded then
raise ineq_cs_failed end
end
{Debug.setRaiseOnBlock T RaiseOnBlock}
Alloc = {Space.merge S}
{Record.foldLInd Alloc
fun {$ I M J}
%% Because of fixed register assignment (\switch +staticvarname)
%% some registers may not be in use.
if {Dictionary.member self.Mapping I} then
{Dictionary.get self.Mapping I}=J
{Max M J}
else M
end
end ~1}+1
end
%% PR#571, PR#931, PR#1244
%% To fix register allocation bugs we have added inequality
%% constraints between source/target Y registers involved in
%% EmitShared's register shuffling. We first try with those
%% constraints, if it fails we try without those constraints.
%% (i.e. we fall back on the old behaviour) this is expected
%% to be a short term fix until further investigations have
%% made it unnecessary.
%%
%% keving: On the main branch we have removed the fallback. If the constraints
%% are unsolvable the compiler will crash.
meth optimize(Res)
Res = {self Optimize(true $)}
{self reset()}
end
meth reset()
Ns <- nil
CDs <- nil
Es <- nil
Ss <- nil
end
end
end
fun {IsStep Coord}
case {Label Coord} of pos then false
[] unit then false
else true
end
end
%%
%% The Emitter class maintains information about which registers are
%% currently in use. The dictionary UsedX maps each X register index
%% to true if and only if it is currently in use.
%%
%% Each Y register is represented as y(_); the indices are only bound
%% at the end of compilation of a procedure. NamedYs is a dictionary
%% that maps some Y register indices to their print names (for debug
%% information).
%%
class Emitter
attr
Temporaries Permanents
LastAliveRS ShortLivedTemps ShortLivedPerms
UsedX LowestFreeX HighestEverX
NamedYs RegOpt
GRegRef HighestUsedG
LocalEnvSize
CodeHd CodeTl
LocalEnvsInhibited
continuations
%% These are only needed temporarily for call argument initialization
%% and for fulfilling prerequisites for entering a shared code region:
AdjDict DelayedInitsDict DoneDict CurrentID Stack Arity
meth init()
GRegRef <- {NewDictionary}
DelayedInitsDict <- {NewDictionary}
AdjDict <- {NewDictionary}
DoneDict <- {NewDictionary}
end
meth doEmit(FormalRegs AllRegs StartAddr NumberReserved
?Code ?GRegs ?NLiveRegs) RS NewCodeTl NumberOfYs in
Temporaries <- {NewDictionary}
Permanents <- {NewDictionary}
{self makeRegSet(?RS)}
LastAliveRS <- RS
{ForAll FormalRegs proc {$ Reg} {BitArray.set RS Reg} end}
ShortLivedTemps <- nil
ShortLivedPerms <- nil
UsedX <- {NewDictionary}
LowestFreeX <- 0
HighestEverX <- ~1
NamedYs <- {NewDictionary}
RegOpt <- {New RegisterOptimizer init()}
HighestUsedG <- ~1
LocalEnvSize <- _
CodeHd <- allocateL(@LocalEnvSize)|NewCodeTl
CodeTl <- NewCodeTl
LocalEnvsInhibited <- false
continuations <- nil
{List.forAllInd FormalRegs
proc {$ I Reg} Emitter, AllocateThisTemp(I - 1 Reg _) end}
{ForAll AllRegs
proc {$ Reg} Emitter, GetPerm(Reg _) end}
Emitter, EmitAddr(StartAddr)
GRegs = {ForThread @HighestUsedG 0 ~1
fun {$ In I} {Dictionary.get @GRegRef I}|In end nil}
{@RegOpt optimize(?NumberOfYs)}
if {IsFree @LocalEnvSize} then
@LocalEnvSize = NumberOfYs
end
@CodeTl = nil
if {self.state getSwitch(staticvarnames $)} then
if NumberReserved == 0 andthen GRegs == nil then
%% Emitting at least one `...Varname' instruction
%% flags this procedure as having been compiled with
%% the switch +staticvarnames:
Code = @CodeHd#[localVarname('')]
else
Code =
@CodeHd#
{ForThread NumberReserved - 1 0 ~1
fun {$ In I}
localVarname({Dictionary.condGet @NamedYs I ''})|In
end
{Map AllRegs
fun {$ GReg}
globalVarname({Dictionary.condGet @regNames GReg ''})
end}}
end
else
Code = @CodeHd#nil
end
NLiveRegs = @HighestEverX + 1
%% free for garbage collection:
{Dictionary.removeAll @Temporaries}
Temporaries <- unit
{Dictionary.removeAll @Permanents}
Permanents <- unit
LastAliveRS <- unit
CodeHd <- nil
{Dictionary.removeAll @UsedX}
UsedX <- unit
NamedYs <- unit
{Dictionary.removeAll @GRegRef}
end
meth newLabel(?Label)
Label = @nextLabel
nextLabel <- Label + 1
end
meth EmitAddr(Addr)
\ifdef DEBUG_EMIT
{System.printInfo 'Debug:\nDebug:Instruction:\nDebug: '}
{ShowVInstr Addr}
{System.printInfo 'Debug:Continuation stack:\n'}
case @continuations of nil then
{System.printInfo 'Debug: nil\n'}
elseof VInstrs then
{ForAll VInstrs
proc {$ VInstr}
{System.printInfo 'Debug: '}
{ShowVInstr VInstr}
end}
end
\endif
case Addr of nil then OldContinuations in
OldContinuations = @continuations
case OldContinuations
of (VInstr=vShared(OccsRS InitsRS Label Addr))|Rest then
continuations <- Rest
Emitter, LetDie(VInstr)
Emitter, EmitShared(OccsRS InitsRS Label Addr 'skip')
continuations <- OldContinuations
[] nil then
Emitter, DeallocateAndReturn()
end
elseof VInstr then
Emitter, FlushShortLivedRegs()
Emitter, LetDie(VInstr)
case VInstr of vShared(OccsRS InitsRS Label Addr) then
Emitter, EmitShared(OccsRS InitsRS Label Addr 'skip')
elsecase VInstr.(Continuations.{Label VInstr}) of nil then
Emitter, EmitVInstr(VInstr)
Emitter, EmitAddr(nil)
elseof Cont then
continuations <- Cont|@continuations
Emitter, EmitVInstr(VInstr)
case @continuations of NewCont|Rest then
%% Note: NewCont may be different from Cont!
continuations <- Rest
Emitter, EmitAddr(NewCont) % may be nil
end
end
end
end
meth EmitShared(OccsRS InitsRS Label Addr AllocateInstr)
case {Dictionary.condGet @sharedDone Label unit} of unit then
OldContinuations
in
%% Make sure all registers in InitsRS are allocated:
OldContinuations = @continuations
continuations <- Addr|OldContinuations
{ForAll {Dictionary.keys @Permanents}
proc {$ Reg} Emitter, GetReg(Reg _) end}
if {IsDet InitsRS} then
{ForAll {BitArray.toList InitsRS}
proc {$ Reg}
case Emitter, GetReg(Reg $) of none then R in
Emitter, PredictReg(Reg ?R)
Emitter, Emit(createVariable(R))
else skip
end
end}
end
continuations <- OldContinuations
{Dictionary.put @sharedDone Label
{Dictionary.clone @Temporaries}#
{Dictionary.clone @Permanents}}
Emitter, Emit(lbl(Label))
Emitter, Emit(AllocateInstr)
Emitter, EmitAddr(Addr)
[] Ts#Ps then
\ifdef DEBUG_SHARED
{System.show {Dictionary.toRecord currentPermanents @Permanents}}
{System.show {Dictionary.toRecord targetPermanents Ps}}
{System.show {Dictionary.toRecord currentTemporaries @Temporaries}}
{System.show {Dictionary.toRecord targetTemporaries Ts}}
\endif
%% PR#571, PR#931, PR#1244
%% We move register contents around to be in the right place
%% for the code block we are about to jump to.
%% For the X registers this is straightforward, we know the
%% physical source/target register numbers.
%% For the Y registers we don't know the physical numbers until we call
%% "@RegOpt optimize" at the end of code generation.
%% We must ensure that we don't overwrite the value in a Y register that
%% is still required as a source. To ensure this we add inequality
%% constraints between all source and target Y registers
%% (and see comment on the register optimizer)
local
SourceYs = {NewCell nil}
TargetYs = {NewCell nil}
in
{ForAll {Dictionary.entries Ps}
proc {$ Reg#YG}
case YG of (Y=y(_))#I then
case Emitter, GetPerm(Reg $) of none then
%% Remember all Indexes, I, that get overwritten
local L in {Exchange TargetYs L I|L} end
case Emitter, GetTemp(Reg $) of none then
Emitter, Emit(createVariable(Y))
elseof X then
Emitter, Emit(move(X Y))
end
elsecase {Dictionary.get @Permanents Reg} of _#J then
{@RegOpt eq(I J)}
end
else skip
end
end}
Arity <- 0
{ForAll {Dictionary.entries Ts}
proc {$ Reg#(X=x(I))} Instr in
if I >= @Arity then
Arity <- I + 1
end
case {Dictionary.condGet @Permanents Reg none}
of vEquateConstant(_ Constant _ _) then
{Dictionary.remove @Permanents Reg}
putConstant(Constant X)
[] vGetSelf(_ _ _) then
{Dictionary.remove @Permanents Reg}
getSelf(X)
elsecase Emitter, GetTemp(Reg $) of none then
case Emitter, GetPerm(Reg $) of none then
createVariable(X)
elseof YG then
case {Dictionary.condGet @Permanents Reg none}
of y(_)#IndS then
%% Remember all Indexes, I, that get read
local L in {Exchange SourceYs L IndS|L} end
else skip end
move(YG X)
end
[] x(!I) then
%% Optimize the special case that the register
%% already is located in its destination.
'skip'
elseof X2=x(J) then
{Dictionary.put @AdjDict J
I|{Dictionary.condGet @AdjDict J nil}}
move(X2 X)
end = Instr
{Dictionary.put @DelayedInitsDict I Instr}
end}
{ForAll {Access SourceYs}
proc {$ IndS}
{ForAll {Access TargetYs}
proc {$ IndT}
{@RegOpt neq(IndS IndT)} end}
end}
end
%% PR#1329
%% In ConfigureXBank() when we have cycles we call spillTemporary to
%% grab a temp X register to break the cycle. This uses
%% the X register pointed to by LowestFreeX, we must make
%% sure LowestFreeX is really free in both the current (old) X
%% registers and the (new) X registers being constructed.
%% kost@ : 'UsedX' keeps the real registers while
%% 'Temporaries' keeps the abstract ones!
LowestFreeX <- {NextFreeIndex @UsedX @Arity}
Emitter, ConfigureXBank()
Temporaries <- {Dictionary.clone Ts}
Permanents <- {Dictionary.clone Ps}
Emitter, Emit(branch(Label))
end
end
meth FlushShortLivedRegs()
case @ShortLivedTemps of x(I)|Xr then
Emitter, FreeX(I)
ShortLivedTemps <- Xr
Emitter, FlushShortLivedRegs()
[] nil then
ShortLivedPerms <- nil
end
end
meth LetDie(VInstr) RS = @LastAliveRS in
if RS \= VInstr.1 then AliveRS in
AliveRS = case VInstr of vShared(RS _ _ _) then {BitArray.clone RS}
else VInstr.1
end
%% Let all registers die that do not occur in AliveRS.
LastAliveRS <- AliveRS
{BitArray.nimpl RS AliveRS}
Emitter, LetDieSub({BitArray.toList RS})
end
end
meth LetDieSub(Regs)
case Regs of Reg|Regr then
Emitter, FreeReg(Reg)
Emitter, LetDieSub(Regr)
[] nil then skip
end
end
meth EmitVInstr(ThisAddr)
case ThisAddr of vDebugEntry(_ Coord Kind _) then
Emitter, DebugEntry(Coord Kind)
[] vDebugExit(_ Coord Kind _) then
Emitter, DebugExit(Coord Kind)
[] vMakePermanent(_ RegIndices _) then TempX1 TempX2 S D in
Emitter, AllocateShortLivedTemp(?TempX2)
Emitter, AllocateShortLivedTemp(?TempX1)
S = self.staticVarnamesSwitch
D = self.dynamicVarnamesSwitch
{ForAll RegIndices
proc {$ Reg#Index#PrintName}
{Dictionary.put @regNames Reg PrintName}
if S then
case Emitter, GetPerm(Reg $) of g(_) then skip
elseof Perm then
case Perm of none then Y in
Emitter, AllocatePerm(Reg ?Y)
case Emitter, GetTemp(Reg $) of none then
Emitter, Emit(createVariable(Y))
elseof X then
Emitter, Emit(move(X Y))
end
elseof OldY then NewY in
{Dictionary.remove @Permanents Reg}
Emitter, AllocatePerm(Reg ?NewY)
Emitter, Emit(move(OldY NewY))
end
case {Dictionary.get @Permanents Reg} of _#I then
{@RegOpt set(I Index)}
end
{Dictionary.put @NamedYs Index PrintName}
end
end
if D then X1 X2 in
case Emitter, GetTemp(Reg $) of none then
case Emitter, GetPerm(Reg $) of none then
Emitter, PredictTemp(Reg ?X1)
Emitter, Emit(createVariable(X1))
elseof Y then
X1 = TempX1
Emitter, Emit(move(Y X1))
end
elseof X then
X1 = X
end
X2 = TempX2
Emitter, Emit(putConstant(PrintName X2))
Emitter, Emit(callBI('Value.nameVariable' [X1 X2]#nil))
end
end}
[] vClear(_ Regs _) then
if @continuations \= nil then
{ForAll Regs
proc {$ Reg}
case Emitter, GetPerm(Reg $) of Y=y(_) then
if Emitter, IsLast(Reg $) then skip
else Y2 in
{Dictionary.remove @Permanents Reg}
Emitter, AllocatePerm(Reg ?Y2)
Emitter, Emit(move(Y Y2))
end
Emitter, Emit(clear(Y))
[] g(_) then skip % see PR#995
end
end}
end
[] vUnify(_ Reg1 Reg2 _) then IsLast1 IsLast2 in
%% X1 X2 Y1 Y2 L1 L2
%% -- -- -- -- -- --
%% 0 0 0 0 0 0 createVariable(R1)
%% ? ? 1 1 ? ? unify(Y1 Y2)
%% ? 1 1 0 ? ? unify(Y1 X2)
%% 1 ? 0 1 ? ? unify(X1 Y2)
%% 1 1 0 0 ? ? unify(X1 X2)
%%
%% 0 0 0 1 0 ? move(Y2 R1)
%% 0 0 1 0 ? 0 move(Y1 R2)
%% 0 1 0 ? 0 0 move(X2 R1)
%% 1 0 ? 0 0 0 move(X1 R2)
%%
%% 0 1 ? ? 0 1 TransferTemp(Reg2 Reg1)
%% 1 0 ? ? 1 0 TransferTemp(Reg1 Reg2)
Emitter, IsLast(Reg1 ?IsLast1)
Emitter, IsLast(Reg2 ?IsLast2)
case Emitter, GetReg(Reg1 $) of none then
case Emitter, GetReg(Reg2 $) of none then
if IsLast1 then skip
elseif IsLast2 then skip
else R1 in
Emitter, PredictReg(Reg1 ?R1)
Emitter, Emit(createVariable(R1))
end
else skip
end
elseof R1 then
case Emitter, GetReg(Reg2 $) of none then skip
elseof R2 then
Emitter, Unify(R1 R2)
end
end
case Emitter, GetTemp(Reg1 $) of none then
case Emitter, GetTemp(Reg2 $) of none then
case Emitter, GetPerm(Reg1 $) of none then
case Emitter, GetPerm(Reg2 $) of none then skip
elseof YG2 then R1 in
Emitter, PredictReg(Reg1 ?R1)
Emitter, Emit(move(YG2 R1))
end
elseof YG1 then
case Emitter, GetPerm(Reg2 $) of none then R2 in
Emitter, PredictReg(Reg2 ?R2)
Emitter, Emit(move(YG1 R2))
else skip
end
end
elseof X2 then
if IsLast1 then skip
elseif IsLast2 then skip
elsecase Emitter, GetPerm(Reg1 $) of none then R1 in
Emitter, PredictReg(Reg1 ?R1)
Emitter, Emit(move(X2 R1))
else skip
end
end
elseof X1 then
case Emitter, GetTemp(Reg2 $) of none then
if IsLast1 then skip
elseif IsLast2 then skip
elsecase Emitter, GetPerm(Reg2 $) of none then R2 in
Emitter, PredictReg(Reg2 ?R2)
Emitter, Emit(move(X1 R2))
else skip
end
else skip
end
end
case Emitter, GetTemp(Reg1 $) of none then
case Emitter, GetTemp(Reg2 $) of none then skip
elseif IsLast1 then skip
elseif IsLast2 then
Emitter, TransferTemp(Reg2 Reg1)
end
else
case Emitter, GetTemp(Reg2 $) of none then
if IsLast2 then skip
elseif IsLast1 then
Emitter, TransferTemp(Reg1 Reg2)
end
else skip
end
end
[] vEquateConstant(_ Constant Reg Cont) then
case Emitter, GetReg(Reg $) of none then
if self.controlFlowInfoSwitch then R in
%% This is needed for 'name generation' step points:
Emitter, PredictReg(Reg ?R)
Emitter, Emit(putConstant(Constant R))
elseif Emitter, IsLast(Reg $) then skip
elseif
{IsLiteral Constant}
andthen Emitter, TryToUseAsSendMsg(ThisAddr Reg Constant 0
nil Cont $)
then skip
else
{Dictionary.put @Permanents Reg ThisAddr}
end
elseof R then
if {IsNumber Constant} then
Emitter, Emit(getNumber(Constant R))
elseif {IsLiteral Constant} then
Emitter, Emit(getLiteral(Constant R))
else R2 in
Emitter, AllocateShortLivedReg(?R2)
Emitter, Emit(putConstant(Constant R2))
Emitter, Unify(R R2)
end
end
[] vEquateRecord(_ Literal RecordArity Reg VArgs Cont) then
if Emitter, TryToUseAsSendMsg(ThisAddr Reg Literal RecordArity
VArgs Cont $)
then skip
else Regs in
{GetRegs VArgs ?Regs nil}
case Emitter, GetReg(Reg $) of none andthen {Member Reg Regs}
then R in
Emitter, PredictReg(Reg ?R)
Emitter, Emit(createVariable(R))
else skip
end
case Emitter, GetReg(Reg $) of none then
if Emitter, IsLast(Reg $) then skip
else R in
Emitter, PredictReg(Reg ?R)
Emitter, EmitRecordWrite(Literal RecordArity R
{FilterNonlinearRegs Regs} VArgs)
end
elseof R then
Emitter, EmitRecordRead(Literal RecordArity R
{FilterNonlinearRegs Regs} VArgs)
end
end
[] vGetVariable(_ Reg _) then
case Emitter, GetReg(Reg $) of none then skip
else Emitter, FreeReg(Reg)
end
if Emitter, IsLast(Reg $) then
Emitter, Emit(getVoid(1))
else R in
Emitter, PredictReg(Reg ?R)
Emitter, Emit(getVariable(R))
end
[] vCallBuiltin(OccsRS Builtinname Regs Coord Cont) then
BIInfo NewCont2
in
BIInfo = {Builtins.getInfo Builtinname}
NewCont2 =
if {CondSelect BIInfo test false} then NInputs Reg in
NInputs = {Length BIInfo.imods}
Reg = {Nth Regs NInputs + 1}
case Cont
of vTestBool(_ !Reg Addr1 Addr2 _ Coord NewCont) then
if {Not self.controlFlowInfoSwitch}
andthen Emitter, IsFirst(Reg $)
andthen Emitter, DoesNotOccurIn(Reg Addr1 $)
andthen Emitter, DoesNotOccurIn(Reg Addr2 $)
andthen Emitter, DoesNotOccurIn(Reg NewCont $)
then
TestCont =
case Builtinname of 'Value.\'==\'' then
[Reg1 Reg2 _] = Regs
in
case {Dictionary.condGet @Permanents Reg1 none}
of vEquateConstant(_ Constant _ _)
andthen ({IsNumber Constant} orelse
{IsLiteral Constant})
then
vTestConstant(OccsRS Reg2 Constant Addr1 Addr2
Coord NewCont)
elsecase {Dictionary.condGet @Permanents Reg2 none}
of vEquateConstant(_ Constant _ _)
andthen ({IsNumber Constant} orelse
{IsLiteral Constant})
then
vTestConstant(OccsRS Reg1 Constant Addr1 Addr2
Coord NewCont)
else ~1
end
[] 'Value.\'\\=\'' then [Reg1 Reg2 _] = Regs in
case {Dictionary.condGet @Permanents Reg1 none}
of vEquateConstant(_ Constant _ _)
andthen ({IsNumber Constant} orelse
{IsLiteral Constant})
then
vTestConstant(OccsRS Reg2 Constant Addr2 Addr1
Coord NewCont)
elsecase {Dictionary.condGet @Permanents Reg2 none}
of vEquateConstant(_ Constant _ _)
andthen ({IsNumber Constant} orelse
{IsLiteral Constant})
then
vTestConstant(OccsRS Reg1 Constant Addr2 Addr1
Coord NewCont)
else ~1
end
else ~1
end
in
if TestCont \= ~1 then TestCont
elseif
{All {List.drop Regs NInputs}
fun {$ Reg} Emitter, IsFirst(Reg $) end}
then
vTestBuiltin(OccsRS Builtinname Regs Addr1 Addr2
NewCont)
else ~1
end
else ~1
end
else ~1
end
else ~1
end
if NewCont2 \= ~1 then
continuations <- NewCont2|@continuations.2
else XsIn XsOut Unifies in
Emitter, AllocateBuiltinArgs(Regs BIInfo.imods ?XsIn
?XsOut ?Unifies)
Emitter, DebugEntry(Coord 'call')
Emitter, Emit(callBI(Builtinname XsIn#XsOut))
Emitter, EmitUnifies(Unifies)
Emitter, DebugExit(Coord 'call')
end